!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004, 2005, 2006. All rights reserved.
!---------------------------------------------------------------------
!= Module BasicSet
!
!   * Developer: SUGIYAMA Ko-ichiro
!   * Version: $Id: basicset.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
!

module BasicSet
  !
  !$B%G%U%)%k%H$N4pK\>l$r@_Dj$9$k$?$a$NJQ?t;2>H7?%b%8%e!<%k(B
  !$B$?$@$7(B, hogeBasicZ $B$H$J$kG[Ns$O(B, $B30It$+$iCM$rF~NO$9$k$3$H$G(B
  !$B=i4|2=$r9T$&(B. 
  !

  !$B%b%8%e!<%kFI$_9~$_(B
  use dc_message, only: MessageNotify   !$B%a%C%;!<%8=PNO(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          ! $B2=3X<o$N?t(B
  use ChemCalc,   only: ChemCalc_init, &!
    &                   CpPerMassRef,  &!
    &                   CpPerMolRef,   &!
    &                   CvPerMassRef,  &!
    &                   MolWt           !
  use ChemData,   only: GasRUniv,      &!
    &                   ChemData_Init,     &!$B=i4|2=(B
    &                   ChemData_SpcID      !$B2=3X<o$N(B ID

  !$B0EL[$N7?@k8@6X;_(B
  implicit none

  !save $BB0@-(B
  save

  !Public Interface
  real(8)               :: Grav                  !$B=ENO(B [m/s^2]
  real(8)               :: TempSfc               !$BCOI=LL29EY(B [K]
  real(8)               :: PressSfc              !$BCOI=LL05NO(B [Pa]
  real(8)               :: CpDry                 !$B4%Ag@.J,$NDj05HfG.(B [J/K kg]
  real(8)               :: CpDryMol              !$B4%Ag@.J,$NDj05HfG.(B [J/K kg]
  real(8)               :: CvDry                 !$B4%Ag@.J,$NDj@QHfG.(B [J/K kg]
  real(8)               :: MolWtDry              !$B4%Ag@.J,$NJ,;RNL(B   [kg/mol]
  real(8)               :: GasRDry               !$B4%Ag@.J,$N5$BNDj?t(B [J/K kg]
  integer, allocatable  :: SpcWetID(:)           !$B<>=a@.J,$N2=3X<o$N(BID
  real(8), allocatable  :: MolWtWet(:)           !$B<>=a@.J,$NJ,;RNL(B  
  real(8), allocatable  :: xz_DensBasicZ(:,:)    !$BL)EY(B
  real(8), allocatable  :: xz_PressBasicZ(:,:)   !$BL5<!8505NO(B
  real(8), allocatable  :: xz_ExnerBasicZ(:,:)   !$BL5<!8505NO(B
  real(8), allocatable  :: xz_TempBasicZ(:,:)    !$B29EY(B
  real(8), allocatable  :: xz_PotTempBasicZ(:,:) !$B290L(B
  real(8), allocatable  :: xz_VelSoundBasicZ(:,:)!$B2;B.(B
  real(8), allocatable  :: xza_MixRtBasicZ(:,:,:)!$B6E=L@.J,:.9gHf(B
  real(8), allocatable  :: xz_EffMolWtBasicZ(:,:)!$BJ,;RNL8z2L(B
  real(8)               :: SpcWetMolFr(10)       !$B<>=a@.J,$N2=3X<o$NB8:_EY(B
  character(15)         :: SpcWetSymbol(10)      !$B<>=a@.J,$N2=3X<oL>(B
  real(8)               :: Tropopause            !$BBPN.7w7w3&LL9bEY(B
  real(8)               :: Humidity              !$BAjBP<>EY(B 
  character(20)         :: EnvType               !$B4pK\>l$N29EY@_Dj(B, 'Dry' or 'Moist'   

contains

  subroutine BasicSetArray_Init(                         &
    & xz_Press, xz_Exner, xz_Temp, xz_PotTemp, xz_Dens,  &
    & xz_VelSound, xza_MixRt, xz_EffMolWt )

    !
    !$B4pK\>l$NCM$r30It$+$i<hF@$9$k(B. 
    !
    
    !$BF~NOJQ?t(B
    real(8), intent(in) :: xz_Press(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in) :: xz_Exner(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in) :: xz_Temp(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in) :: xz_PotTemp(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in) :: xz_Dens(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in) :: xz_VelSound(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in) :: xza_MixRt(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
    real(8), intent(in) :: xz_EffMolWt(DimXMin:DimXMax, DimZMin:DimZMax)
   
    !---------------------------------------------------------------
    ! *BasicZ $B$JG[Ns$N=i4|2=(B
    !---------------------------------------------------------------
    !$BG[Ns$N3d$jEv$F(B
    allocate( & 
      & xz_DensBasicZ(DimXMin:DimXMax, DimZMin:DimZMax),            &
      & xz_PressBasicZ(DimXMin:DimXMax, DimZMin:DimZMax),           &
      & xz_ExnerBasicZ(DimXMin:DimXMax, DimZMin:DimZMax),           &
      & xz_TempBasicZ(DimXMin:DimXMax, DimZMin:DimZMax),            &
      & xz_PotTempBasicZ(DimXMin:DimXMax, DimZMin:DimZMax),         &
      & xz_VelSoundBasicZ(DimXMin:DimXMax, DimZMin:DimZMax),        &
      & xza_MixRtBasicZ(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum),   &
      & xz_EffMolWtBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)         )
    
    !$BCM$NF~NO(B
    xz_PressBasicZ    = xz_Press
    xz_ExnerBasicZ    = xz_Exner
    xz_TempBasicZ     = xz_Temp
    xz_PotTempBasicZ  = xz_PotTemp
    xz_DensBasicZ     = xz_Dens
    xz_VelSoundBasicZ = xz_VelSound
    xza_MixRtBasicZ    = xza_MixRt
    xz_EffMolWtBasicZ = xz_EffMolWt
    
  end subroutine BasicSetArray_Init


  subroutine BasicSet_Init(cfgfile)
    !
    !$B4pK\>l$N>pJs$r(B Namelist $B$+$i<hF@$7$FCM$r7W;;(B
    !

    !$B0EL[$N7?@k8@6X;_(B
    implicit none
    
    !$BJQ?tDj5A(B
    character(*), intent(in) :: cfgfile         !NAMELIST $B%U%!%$%k(B
!    real(8)                   :: Tropopause      !$BBPN.7w7w3&LL9bEY(B
!    real(8)                   :: CpDryMol        !$B4%Ag@.J,$NDj05HfG.(B [J/K mol]
    integer                   :: SpcDryNum       !$B4%Ag@.J,$N2=3X<o$N?t(B
    integer                   :: SpcWetNum       !$B<>=a@.J,$N2=3X<o$N?t(B
    character(15)             :: SpcDrySymbol(5)!$B4%Ag@.J,$N2=3X<oL>(B
!    character(15)             :: SpcWetSymbol(10)!$B<>=a@.J,$N2=3X<oL>(B
    real(8)                   :: SpcDryMolFr(5) !$B4%Ag@.J,$N2=3X<o$NB8:_EY(B
!    real(8)                   :: SpcWetMolFr(10) !$B<>=a@.J,$N2=3X<o$NB8:_EY(B
    integer, allocatable      :: SpcDryID(:)     !$B4%Ag@.J,$N2=3X<o$N(BID
!    real(8)                   :: Humidity        !$BAjBP<>EY(B
!    character(20)             :: EnvType         !$B4pK\>l$N29EY@_Dj(B, 'Dry' or 'Moist'
    real(8), allocatable      :: PropertyDry(:)  !$B:n6HG[Ns(B
    character(20), allocatable:: Symbol(:)       !$B:n6HG[Ns(B
    integer                   :: s, n1, n2, n3   !$B:n6HJQ?t(B

    !NAMELIST $B$NDj5A(B
    NAMELIST /basicset/ &
      & Grav, TempSfc, PressSfc,   Tropopause,  & 
      & SpcDrySymbol, SpcDryMolFr,              &
      & SpcWetSymbol, SpcWetMolFr,              &
      & EnvType, Humidity                       

    SpcDrySymbol = '' 
    SpcDryMolFr  = 0.0d0
    SpcWetSymbol = '' 
    SpcWetMolFr  = 0.0d0
    
    !$B%U%!%$%k%*!<%W%s(B. $B>pJs<hF@(B. 
    open (10, FILE=cfgfile)
    read(10, NML=basicset)
    close(10)

    !----------------------------------------------------------
    ! $B2=3X<o$NJ*@-CM$N=i4|2=(B
    !----------------------------------------------------------
    !$B%G!<%?%Y!<%9$N=i4|2=(B
    call ChemData_Init
    
    !----------------------------------------------------------
    ! $B4%Ag@.J,$NJ*@-CM$N=i4|2=(B
    !----------------------------------------------------------
    !$B4%Ag@.J,$N8D?t$r?t$($k(B
    SpcDryNum = count(SpcDrySymbol /= "")
    
    !$B2=3X<o$N(B ID $B$r<hF@(B    
    allocate(SpcDryID(SpcDryNum))    
    SpcDryID = ChemData_SpcID(SpcDryNum, SpcDrySymbol(1:SpcDryNum))
    
    !$B2=3X7W;;%b%8%e!<%k$N=i4|2=(B
    call ChemCalc_Init(DimXMin, DimXMax, DimZMin, DimZMax, SpcDryNum, SpcDryID)
    
    !$B:n6HG[Ns$N=`Hw(B
    allocate(PropertyDry(SpcDryNum))

    !$BJ,;RNL(B
    do s = 1, SpcDryNum
      PropertyDry(s) = MolWt(SpcDryID(s))
    end do
    MolWtDry = dot_product(PropertyDry, SpcDryMolFr(1:SpcDryNum)) 
    
    !$BDj05HfG.(B($B%b%kEvNL(B)
    do s = 1, SpcDryNum    
      PropertyDry(s) = CpPerMolRef(SpcDryID(s))
    end do
    CpDryMol = dot_product(PropertyDry, SpcDryMolFr(1:SpcDryNum)) 
    
    !$BDj05HfG.(B
    CpDry    = CpDryMol / MolWtDry
    
    !$B5$BNDj?t(B
    GasRDry = GasRUniv / MolWtDry
    
    !$BDj@QHfG.(B
    CvDry    = CpDry - GasRDry
    
    
    !----------------------------------------------------------
    ! $B<>=a@.J,$N(B ID $B$rF@$k(B
    !----------------------------------------------------------
    !$B<>=a@.J,$N8D?t$r?t$($k(B
    SpcWetNum = count(SpcWetSymbol /= "")
    if (SpcWetNum /= SpcNum) then 
      write(*,*) "SpcWetNum /= SpcNum"
      stop
    end if
    
    !$BG[Ns$N3d$jEv$F(B
    allocate(SpcWetID(SpcWetNum), Symbol(SpcWetNum), MolWtWet(SpcWetNum))

    !SpcWetSymbol $B$NJ8;zNs$+$i(B, -Rain, -Cloud $B$r=|$$$?$b$N$r(B Symbol $B$H$7$FJ]4I(B
    do s = 1, SpcWetNum
      n1 = index(SpcWetSymbol(s), '-Cloud' )
      n2 = index(SpcWetSymbol(s), '-Rain' )
      n3 = max(n1, n2)
      if (n3 == 0) then
        Symbol(s) = SpcWetSymbol(s)
      else
        Symbol(s) = SpcWetSymbol(s)(1:n3-1)
      end if
    end do
    
    !$B2=3X<o$N(B ID $B$r<hF@(B
    SpcWetID = ChemData_SpcID(SpcWetNum, Symbol)
    
    !$B2=3X7W;;%b%8%e!<%k$N=i4|2=(B
    call ChemCalc_Init(DimXMin, DimXMax, DimZMin, DimZMax, SpcWetNum, SpcWetID(:))
    
    !$BJ,;RNL$rJ]4I(B
    do s = 1, SpcWetNum
      MolWtWet(s) = MolWt(SpcWetID(s))
    end do
    

    !----------------------------------------------------------
    ! $B3NG'(B
    !----------------------------------------------------------
    write(*,*) "basicset_init, Grav     ", Grav
    write(*,*) "basicset_init, TempSfc  ", TempSfc
    write(*,*) "basicset_init, PressSfc ", PressSfc

    do s = 1, SpcDryNum
      write(*,*) "basicset_init, SpcDryID ", SpcDryID(s)
      write(*,*) "basicset_init, SpcDrySymbol ", SpcDrySymbol(s)
      write(*,*) "basicset_init, SpcDryMolFr  ", SpcDryMolFr(s)
    end do
    write(*,*) "basicset_init, CpDry    ", CpDry
    write(*,*) "basicset_init, CpDryMol ", CpDryMol
    write(*,*) "basicset_init, CvDry    ", CvDry
    write(*,*) "basicset_init, GasRDry  ", GasRDry
    write(*,*) "basicset_init, MolWtDry ", MolWtDry

    do s = 1, SpcWetNum
      write(*,*) "basicset_init, SpcWetID ", SpcWetID(s)
      write(*,*) "basicset_init, SpcWetSymbol ", SpcWetSymbol(s)
      write(*,*) "basicset_init, SpcWetMolFr  ", SpcWetMolFr(s)
      write(*,*) "basicset_init, MolWtWet ", MolWtWet(s)
    end do

    write(*,*) "basicset_init, Tropopause ", Tropopause
    write(*,*) "basicset_init, EnvType    ", EnvType
    write(*,*) "basicset_init, Humidity   ", Humidity

  end subroutine BasicSet_Init
  
end module BasicSet
