!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004, 2005, 2006. All rights reserved.
!---------------------------------------------------------------------
!= Subroutine BasicEnv
!
!   * Developer: SUGIYAMA Ko-ichiro
!   * Version: $Id: basicenv.f90,v 1.1.1.1 2006/04/25 03:43:58 deepconv Exp $
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!$B%G%U%)%k%H$N4pK\>l$r@_Dj$9$k$?$a$NJQ?t;2>H7?%b%8%e!<%k(B
!   * BasicEnvFile_init: $B4pK\>l$NCM$r(B netCDF $B%U%!%$%k$+$i<hF@(B
!   * BasicEnvCalc_Init: $B4pK\>l$N>pJs$r(B Namelist $B$+$i<hF@$7$FCM$r7W;;(B
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!

subroutine BasicEnv()
  !
  !$B%G%U%)%k%H$N4pK\>l$r@_Dj$9$k$?$a$N%5%V%k!<%A%s(B. 
  !$B4pK\>l$r7W;;$7(B, BasicSet $B%b%8%e!<%k$NCM$r=i4|2=$9$k(B. 
  !
  !$B%3%s%Q%$%k$N=g=x$NLdBj$+$i(B, $B4pK\>l$NCM(B(hogeBasicZ $B$JJQ?t(B)$B$r(B
  !$B7W;;$9$kItJ,$r(BBasicSet $B%b%8%e!<%k$+$i@Z$jN%$7$F$$$k(B. 
  !ECCM $B;O$a(B, BasicSet $B<+BN$K0MB8$9$k$,(B hogeBasicZ $B$O(B use $B$7$J$$(B
  !$B30It%5%V%k!<%A%s$rMxMQ$9$k$?$a$G$"$k(B. 
  !

  !$B%b%8%e!<%kFI$_9~$_(B
  use gridset,  only: DimXMin,       &!$BG[Ns$N(B X $BJ}8~$N2<8B(B
    &                 DimXMax,       &!$BG[Ns$N(B X $BJ}8~$N>e8B(B
    &                 DimZMin,       &!$BG[Ns$N(B Z $BJ}8~$N2<8B(B
    &                 DimZMax,       &!$BG[Ns$N(B Z $BJ}8~$N>e8B(B
    &                 SpcNum,        &!$B6E=L@.J,$N?t(B
    &                 s_Z,           &!$B%9%+%i!<3J;RE@$G$N9bEY(B
    &                 DelZ            !Z $BJ}8~$N3J;RE@4V3V(B
  use basicset, only: BasicSetArray_Init, &!
    &                 PressSfc,      &!$B4%Ag@.J,$NDj05HfG.(B  
    &                 GasRDry,       &!$B4%Ag@.J,$NDj05HfG.(B
    &                 CpDry,         &!$B4%Ag@.J,$NDj05HfG.(B
    &                 CvDry,         &!$B4%Ag@.J,$NDj@QHfG.(B
    &                 MolWtDry,      &!$B4%Ag@.J,$NJ,;RNL(B
    &                 Grav,          &!$B=ENO2CB.EY(B
    &                 SpcWetMolFr,   &!$B6E=L@.J,$N=i4|%b%kHf(B
    &                 MolWtWet,      &!$B6E=L@.J,$NJ,;RNL(B
    &                 EnvType,       &!$B4pK\>l$N@_Dj(B
    &                 Tropopause,    &!$BBPN.7w3&LL9bEY(B
    &                 Humidity        !$B4pK\>l$NAjBP<>EY(B
  use Boundary, only: xz_BoundaryXCyc_xz,  &!
    &                 xz_BoundaryZSym_xz,  &!  
    &                 xza_BoundaryXCyc_xza,&!
    &                 xza_BoundaryZSym_xza  !  
  use ECCM,     only: ECCM_Init,     &!
    &                 ECCM_Temp_Press, &!
    &                 ECCM_MolFr
  
  !$B0EL[$N7?@k8@6X;_(B
  implicit none

  !$BJQ?t$NDj5A(B
  real(8) :: xz_DensBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8) :: xz_PressBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8) :: xz_ExnerBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8) :: xz_TempBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8) :: xz_PotTempBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8) :: xz_VelSoundBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8) :: xza_MixRtBasicZ(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
  real(8) :: xz_EffMolWtBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)

  real(8) :: MolFrIni(SpcNum)
  real(8) :: xz_MolFr(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
  real(8) :: xza_MixRtDivMolWt(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
  integer :: i, k, s

  !---------------------------------------------------------------
  ! $BG[Ns$N=i4|2=(B
  !---------------------------------------------------------------
  xz_DensBasicZ     = 0.0d0
  xz_PressBasicZ    = 0.0d0
  xz_ExnerBasicZ    = 0.0d0
  xz_TempBasicZ     = 0.0d0
  xz_PotTempBasicZ  = 0.0d0
  xz_VelSoundBasicZ = 0.0d0
  xza_MixRtBasicZ    = 0.0d0
  xz_EffMolWtBasicZ = 0.0d0

  !---------------------------------------------------------------
  ! ECCM $B%b%8%e!<%k$N=i4|2=(B
  !---------------------------------------------------------------
  call ECCM_Init
       
  !---------------------------------------------------------------
  ! EnvType $B$r85$K(B, $B29EY(B, $B05NO(B, $BAH@.$r7h$a$k(B
  !---------------------------------------------------------------
  if (trim(EnvType) == 'Dry') then 
    MolFrIni = 0.0d0
  elseif (trim(EnvType) == 'Moist') then 
    MolFrIni = SpcWetMolFr(1:SpcNum) 
  end if
 
  do i = DimXMin, DimXMax
    call ECCM_Temp_Press( MolFrIni, xz_TempBasicZ(i,:), xz_PressBasicZ(i,:) ) 
  end do
  
  ! $BBPN.7w3&LL$h$j>e$N07$$(B
  do k = DimZMin, DimZMax      
    if (s_Z(k) > Tropopause) then 
      xz_TempBasicZ(:,k) = xz_TempBasicZ(:,k-1) 
      xz_PressBasicZ(:,k) = &
        & xz_PressBasicZ(:,k-1) &
        &   * dexp( - DelZ * Grav / (GasRDry * xz_TempBasicZ(:,k)) ) 
    end if
  end do
  
  !$B6-3&>r7o(B
  xz_TempBasicZ  = xz_BoundaryXCyc_xz( xz_TempBasicZ )
  xz_TempBasicZ  = xz_BoundaryZSym_xz( xz_TempBasicZ )
  xz_PressBasicZ = xz_BoundaryXCyc_xz( xz_PressBasicZ )
  xz_PressBasicZ = xz_BoundaryZSym_xz( xz_PressBasicZ )
  
  !---------------------------------------------------------------
  ! $B:.9gHf(B
  !---------------------------------------------------------------
  do i = DimXMin, DimXMax      
    call ECCM_MolFr( SpcWetMolFr(1:SpcNum) * Humidity, Humidity, xz_TempBasicZ(i,:), &
      &              xz_PressBasicZ(i,:), xz_MolFr(i,:,:) )
  end do
    
  !$B5$Aj$N%b%kHf$r:.9gHf$KJQ49(B
  do s = 1, SpcNum
    xza_MixRtBasicZ(:,:,s) = xz_MolFr(:,:,s) * MolWtWet(s) / MolWtDry
  end do

  !$BCM$,>.$5$/$J$j$9$.$J$$$h$&$K:GDcCM$rM?$($k(B
!  where (xza_MixRtBasicZ <= 1.0d-20 )
!    xza_MixRtBasicZ = 1.0d-20
!  end where
  
  !$B6-3&>r7o(B
  xza_MixRtBasicZ = xza_BoundaryXCyc_xza( xza_MixRtBasicZ )
  xza_MixRtBasicZ = xza_BoundaryZSym_xza( xza_MixRtBasicZ )
  
      
  !---------------------------------------------------------------
  ! $BJ,;RNL$N8z2L(B
  !---------------------------------------------------------------
  do s = 1, SpcNum
    xza_MixRtDivMolWt(:,:,s) = xza_MixRtBasicZ(:,:,s) / MolWtWet(s)
  end do
  
  xz_EffMolWtBasicZ = &
    & 1.0d0 -  sum(xza_MixRtDivMolWt,3) &
    &            / ((1.0d0 / MolWtDry) + sum(xza_MixRtDivMolWt,3))  &
    & * (1.0d0 + sum(xza_MixRtBasicZ,3) )

  !$B6-3&>r7o(B  
  xz_EffMolWtBasicZ = xz_BoundaryXCyc_xz( xz_EffMolWtBasicZ )
  xz_EffMolWtBasicZ = xz_BoundaryZSym_xz( xz_EffMolWtBasicZ )
  
  
  !---------------------------------------------------------------    
  ! $B290L(B
  !---------------------------------------------------------------
  xz_PotTempBasicZ = &
    & xz_TempBasicZ * (PressSfc / xz_PressBasicZ) ** (GasRDry / CpDry) 

  !$B6-3&>r7o(B  
  xz_PotTempBasicZ = xz_BoundaryXCyc_xz( xz_PotTempBasicZ )
  xz_PotTempBasicZ = xz_BoundaryZSym_xz( xz_PotTempBasicZ )

  
  !---------------------------------------------------------------    
  ! $B%(%/%9%J!<4X?t(B
  !---------------------------------------------------------------
  xz_ExnerBasicZ = xz_TempBasicZ / xz_PotTempBasicZ    

  !$B6-3&>r7o(B
  xz_ExnerBasicZ = xz_BoundaryXCyc_xz( xz_ExnerBasicZ )
  xz_ExnerBasicZ = xz_BoundaryZSym_xz( xz_ExnerBasicZ )

  
  !---------------------------------------------------------------    
  ! $BL)EY(B
  !---------------------------------------------------------------
  xz_DensBasicZ = &
    & PressSfc * (xz_ExnerBasicZ ** (CvDry / GasRDry)) &
    &  / (GasRDry * xz_PotTempBasicZ / xz_EffMolWtBasicZ)

  !$B6-3&>r7o(B
  xz_DensBasicZ = xz_BoundaryXCyc_xz( xz_DensBasicZ )
  xz_DensBasicZ = xz_BoundaryZSym_xz( xz_DensBasicZ )
  

  !---------------------------------------------------------------    
  ! $B2;B.(B
  !---------------------------------------------------------------
  xz_VelSoundBasicZ = &
    & sqrt ( &
    &   CpDry * GasRDry * xz_ExnerBasicZ * xz_PotTempBasicZ &
    &   / (CvDry * xz_EffMolWtBasicZ) &
    & )

  !$B6-3&>r7o(B
  xz_VelSoundBasicZ = xz_BoundaryXCyc_xz( xz_VelSoundBasicZ )
  xz_VelSoundBasicZ = xz_BoundaryZSym_xz( xz_VelSoundBasicZ )

 
  !----------------------------------------------------------
  ! BasicSet $B%b%8%e!<%k$KCM$r@_Dj(B
  !----------------------------------------------------------
  call BasicSetArray_Init(                                  &
    & xz_PressBasicZ,    xz_ExnerBasicZ, xz_TempBasicZ,     &
    & xz_PotTempBasicZ,  xz_DensBasicZ,  xz_VelSoundBasicZ, &
    & xza_MixRtBasicZ, xz_EffMolWtBasicZ )

end subroutine BasicEnv
