!c Description: 
!c   環境場の計算
!c
!c Current Code Owner: 
!c   sugiyama@gfd-dennou.org
!c
!c Copyright (C) SUGIYAMA Ko-ichiro, 2004, All rights reserved

subroutine basicenv(ss_CpBasicZ, ss_DensBasicZ, &
     & ss_ExnerBasicZ, ss_ThetaBasicZ, ss_VelSoundBasicZ)

  !--- モジュールの呼び出し
  use gridset, only: DimXMin, DimXMax, DimZMin, DimZMax, s_Z
  use physprm, only: GasRUniv
  use arareprm,   only: Grav
  use gacchanprm, only: TempSfc, PressSfc, MolWtDry, CpDry, &
       &                TempType, PressType

  !--- 暗黙の型宣言禁止
  implicit none

  !--- モジュールで定義する変数
  real(8), intent(out) :: ss_CpBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(out) :: ss_DensBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)  
  real(8), intent(out) :: ss_ExnerBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(out) :: ss_ThetaBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(out) :: ss_VelSoundBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)

  !--- 内部変数
  real(8)  :: ss_TempBasicZ(DimXMin:DimXMax, DimZMin:DimZMax) ! 平均温度場
  real(8)  :: ss_CvBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)  :: ss_Z(DimXMin:DimXMax, DimZMin:DimZMax)     ! 2-D 座標
  real(8)  :: GasR
  integer  :: k


  !--- 座標を初期化
  do k = DimZMin, DimZMax
     ss_Z(:,k) = s_Z(k)
  end do

  !--------------------------------------------
  !--- 気体定数を決める
  !--------------------------------------------
  GasR = GasRUniv / MolWtDry
  
  
  !--------------------------------------------
  !--- 比熱を決める
  !---   比熱は乾燥大気を念頭に一様とする. 
  !--------------------------------------------
  ss_CpBasicZ = CpDry / MolWtDry
  ss_CvBasicZ = ss_CpBasicZ - GasR


  !--------------------------------------------
  !--- 温度/温位, 無次元圧力の基本場
  !--------------------------------------------
  if (TempType == "uniform" .AND. PressType == "uniform") then 

     !--- 温度場は TempSfc で一様
     ss_TempBasicZ = TempSfc
     
     !--- 地表面圧力で一定. 定義より値は 1 となる. 
     ss_ExnerBasicZ = 1.0d0
     
     !--- 温位
     ss_ThetaBasicZ = ss_TempBasicZ / ss_ExnerBasicZ 
     
  elseif (TempType == "uniform" .AND. PressType == "hydrostatic") then 
     
     !--- 温度場は TempSfc で一様
     ss_TempBasicZ = TempSfc
     
     !--- 無次元圧力と温位を連立して解く. 
     ss_ExnerBasicZ = &
          & dexp( - Grav * ss_Z / ( ss_CpBasicZ * ss_TempBasicZ ) )

     !--- 無次元圧力を利用して温位を計算
     ss_ThetaBasicZ = ss_TempBasicZ / ss_ExnerBasicZ
     
  elseif (TempType == "adiabat" .AND. PressType == "uniform") then 

     !--- 地表面での温位(= TempSfc)で一定
     ss_ThetaBasicZ = TempSfc

     !--- 地表面圧力で一定. 定義より値は 1 となる. 
     ss_ExnerBasicZ = 1.0d0

  elseif (TempType == "adiabat" .AND. PressType == "hydrostatic") then 
     
     !--- 地表面での温位(= TempSfc)で一定
     ss_ThetaBasicZ = TempSfc

     !--- 無次元圧力を計算. 温位一定
     ss_ExnerBasicZ = &
          & - Grav * ss_Z / ( ss_CpBasicZ * ss_ThetaBasicZ ) + 1.0d0

  else
     write(*,*) "unknown TempType, or PressType"
     write(*,*) TempType, PressType
        
  end if

    
  !--------------------------------------------
  !---- 密度
  !--------------------------------------------
  ss_DensBasicZ = &
       & PressSfc * ( ss_ExnerBasicZ ** (ss_CvBasicZ / GasR) ) &
       &   / ( GasR * ss_ThetaBasicZ )
  
  
  !--------------------------------------------    
  !--- 平均場の音速
  !--------------------------------------------
  ss_VelSoundBasicZ = &
       & ss_CpBasicZ * GasR * ss_ExnerBasicZ * ss_ThetaBasicZ &
       &   / ss_CvBasicZ  
  ss_VelSoundBasicZ = sqrt( ss_VelSoundBasicZ )
  
  
end subroutine basicenv

