!= Dennou AGCM 縺ｮ蠑上ｒ逕ｨ縺�◆鬟ｽ蜥梧ｯ疲ｹｿ縺ｮ邂怜�
!
!= Evaluate saturation specific humidity with method used by Dennou AGCM
!
! Authors::   Yasuhiro MORIKAWA, Yoshiyuki O. Takahashi
! Version::   $Id: saturate_DennouAGCMOnIce.f90,v 1.1 2014/06/29 07:26:30 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module saturate_DennouAGCMOnIce
  !
  != Dennou AGCM 縺ｮ蠑上ｒ逕ｨ縺�◆鬟ｽ蜥梧ｯ疲ｹｿ縺ｮ邂怜�
  !
  != Evaluate saturation specific humidity with method used by Dennou AGCM
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! Dennou AGCM 縺ｧ逕ｨ縺�ｉ繧後※縺�◆蠑上ｒ逕ｨ縺�※鬟ｽ蜥瑚頂豌怜悸繧定ｨ育ｮ励＠縺ｾ縺�.
  ! 縺薙�蠑上�, 蜈�� 豐ｼ蜿｣ (1992) 縺ｫ萓昴▲縺ｦ縺�∪縺�.
  ! 縺溘□縺�, 貎懃�縺ｫ縺ｯ, 蜃晉ｵ先ｽ懃�縺ｨ蜃咲ｵ先ｽ懃�縺ｮ蜥後ｒ逕ｨ縺�∪縺�.
  !
  ! Saturation specific humidity and temperature derivative of it
  ! are calculated with a formula used in Dennou AGCM.
  ! This formula is originaly obtained from Numaguti (1992).
  ! However, the value of latent heat is estimated as sum of vaporization
  ! and melting.
  !
  !== References
  !
  ! * Numaguti, 1992:
  !   <title in Japanese>,
  !   Doctor thesis, University of Tokyo.
  !
  !== Procedures List
  !
  ! CalcQVapSat            :: 鬟ｽ蜥梧ｯ疲ｹｿ縺ｮ險育ｮ�
  ! CalcDQVapSatDTemp      :: 鬟ｽ蜥梧ｯ疲ｹｿ縺ｮ貂ｩ蠎ｦ蠕ｮ蛻��險育ｮ�
  ! ------------  :: ------------
  ! CalcQVapSat            :: Calculate saturation specific humidity
  ! CalcDQVapSatDTemp      :: Calculate temperature derivative of saturation specific humidity
  !
  !--
  !== NAMELIST
  !
  ! NAMELIST#saturate_t1930_nml
  !++

  ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
  !

  ! 遞ｮ蛻･蝙九ヱ繝ｩ繝｡繧ｿ
  ! Kind type parameter
  !
  use dc_types, only: DP, &      ! 蛟咲ｲｾ蠎ｦ螳滓焚蝙�. Double precision.
    &                 STRING     ! 譁�ｭ怜�.       Strings.

  ! 繝｡繝�そ繝ｼ繧ｸ蜃ｺ蜉�
  ! Message output
  !
  use dc_message, only: MessageNotify

  ! 螳｣險譁� ; Declaration statements
  !
  implicit none
  private

  ! 蜈ｬ髢区焔邯壹″
  ! Public procedure
  !
  public:: xyz_CalcQVapSat
  public:: xyz_CalcDQVapSatDTemp
  public:: SaturateInit

  ! 蜈ｬ髢句､画焚
  ! Public variables
  !
  logical, save, public:: saturate_DennouAGCMOnIce_inited = .false.
                              ! 蛻晄悄險ｭ螳壹ヵ繝ｩ繧ｰ.
                              ! Initialization flag

  ! 髱槫�髢句､画焚
  ! Private variables
  !
  real(DP), parameter:: Es0 = 611.0_DP
                              ! 0 邃��鬟ｽ蜥瑚頂豌怜悸 [Pa].
                              ! Saturation water vapor pressure at 0 deg C [Pa]

  character(*), parameter:: module_name = 'saturate_DennouAGCMOnIce'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: saturate_DennouAGCMOnIce.f90,v 1.1 2014/06/29 07:26:30 yot Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

contains

  !--------------------------------------------------------------------------------------

  function xyz_CalcQVapSat( xyz_Temp, xyz_Press ) result( xyz_QVapSat )
    !
    ! 貂ｩ蠎ｦ *Temp* 縺ｨ豌怜悸 *Press* 繧堤畑縺�,
    ! 鬟ｽ蜥梧ｯ疲ｹｿ *QVapSat* 繧呈ｱゅａ縺ｾ縺�.
    !
    ! Calculate saturation specific humidity *QVapSat* using
    ! temperature *Temp* and air pressure *Press*.
    !

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
    !

    ! 迚ｩ逅�ｮ壽焚險ｭ螳�
    ! Physical constants settings
    !
    use constants, only: &
      & GasRWet, &
                              ! $ R_v $ [J kg-1 K-1].
                              ! 蜃晉ｵ先�蛻��豌嶺ｽ灘ｮ壽焚.
                              ! Gas constant of condensible elements
      & LatentHeat, &
                              ! $ L $ [J kg-1] .
                              ! 蜃晉ｵ舌�貎懃�.
                              ! Latent heat of condensation
      & LatentHeatFusion, &
                              ! $ L $ [J kg-1] .
                              ! 陞崎ｧ｣縺ｮ貎懃�.
                              ! Latent heat of fusion
      & EpsV                  ! $ \epsilon_v $ .
                              ! 豌ｴ闥ｸ豌怜�蟄宣㍼豈�.
                              ! Molecular weight of water vapor

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none

    real(DP), intent(in):: xyz_Temp (:,:,:)
                              ! $ T $ . 貂ｩ蠎ｦ. Temperature
    real(DP), intent(in):: xyz_Press(:,:,:)
                              ! $ p $ . 豌怜悸. Air pressure

    real(DP):: xyz_QVapSat(1:size(xyz_Temp,1), 1:size(xyz_Temp,2), 1:size(xyz_Temp,3))
                              ! $ q^{*} $ . 鬟ｽ蜥梧ｯ疲ｹｿ. Saturation specific humidity

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    real(DP):: LatentHeatLocal
                              !
                              ! Latent heat used in this routine

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹也｢ｺ隱�
    ! Initialization check
    !

    if ( .not. saturate_DennouAGCMOnIce_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    ! Set a value for latent heat
    LatentHeatLocal = LatentHeat + LatentHeatFusion

    xyz_QVapSat = &
      & EpsV * Es0 * exp( LatentHeatLocal / GasRWet * ( 1./273. - 1./xyz_Temp ) ) &
      & / xyz_Press

  end function xyz_CalcQVapSat

  !--------------------------------------------------------------------------------------

  function xyz_CalcDQVapSatDTemp( xyz_Temp, xyz_QVapSat ) result( xyz_DQVapSatDTemp )
    !
    ! 貂ｩ蠎ｦ *Temp* 縺ｨ鬟ｽ蜥梧ｯ疲ｹｿ *QVapSat* 繧堤畑縺�,
    ! 鬟ｽ蜥梧ｯ疲ｹｿ縺ｮ貂ｩ蠎ｦ蠕ｮ蛻� *DQVapSatDTemp* 繧呈ｱゅａ縺ｾ縺�.
    !
    ! Calculate temperature derivative of saturation specific humidity
    ! *DQVapSatDTemp* using
    ! temperature *Temp* and saturation specific humidity *QVapSat*.
    !

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
    !

    ! 迚ｩ逅�ｮ壽焚險ｭ螳�
    ! Physical constants settings
    !
    use constants, only: &
      & GasRWet, &
                              ! $ R_v $ [J kg-1 K-1].
                              ! 蜃晉ｵ先�蛻��豌嶺ｽ灘ｮ壽焚.
                              ! Gas constant of condensible elements
      & LatentHeat, &
                              ! $ L $ [J kg-1] .
                              ! 蜃晉ｵ舌�貎懃�.
                              ! Latent heat of condensation
      & LatentHeatFusion, &
                              ! $ L $ [J kg-1] .
                              ! 陞崎ｧ｣縺ｮ貎懃�.
                              ! Latent heat of fusion
      & EpsV                  ! $ \epsilon_v $ .
                              ! 豌ｴ闥ｸ豌怜�蟄宣㍼豈�.
                              ! Molecular weight of water vapor

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none

    real(DP), intent(in):: xyz_Temp   (:,:,:)
                              ! $ T $ . 貂ｩ蠎ｦ. Temperature
    real(DP), intent(in):: xyz_QVapSat(:,:,:)
                              ! $ q^{*} $ . 鬟ｽ蜥梧ｯ疲ｹｿ. Saturation specific humidity
    real(DP):: xyz_DQVapSatDTemp(size(xyz_Temp,1), size(xyz_Temp,2), size(xyz_Temp,3))
                              ! $ \DP{q^{*}}{T} $ . 鬟ｽ蜥梧ｯ疲ｹｿ縺ｮ貂ｩ蠎ｦ蠕ｮ蛻�.
                              ! Temperature derivative of saturation specific humidity.

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    real(DP):: LatentHeatLocal
                              !
                              ! Latent heat used in this routine

    ! 螳溯｡梧枚 ; Executable statement
    !

    if ( .not. saturate_DennouAGCMOnIce_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    ! Set a value for latent heat
    LatentHeatLocal = LatentHeat + LatentHeatFusion

    xyz_DQVapSatDTemp = LatentHeatLocal * xyz_QVapSat / ( GasRWet * xyz_Temp**2 )

  end function xyz_CalcDQVapSatDTemp

  !--------------------------------------------------------------------------------------

  subroutine SaturateInit
    !
    ! saturate_DennouAGCM 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蛻晄悄蛹悶ｒ陦後＞縺ｾ縺�.
    !--
    ! NAMELIST#saturate_DennouAGCM_nml 縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ縺ｯ縺薙�謇狗ｶ壹″縺ｧ陦後ｏ繧後∪縺�.
    !++
    !
    ! "saturate_DennouAGCM" module is initialized.
    !--
    ! "NAMELIST#saturate_DennouAGCM_nml" is loaded in this procedure.
    !++

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
    !

    ! NAMELIST 繝輔ぃ繧､繝ｫ蜈･蜉帙↓髢｢縺吶ｋ繝ｦ繝ｼ繝�ぅ繝ｪ繝�ぅ
    ! Utilities for NAMELIST file input
    !
    use namelist_util, only: namelist_filename, NmlutilMsg

    ! 繝輔ぃ繧､繝ｫ蜈･蜃ｺ蜉幄｣懷勧
    ! File I/O support
    !
    use dc_iounit, only: FileOpen

    ! 遞ｮ蛻･蝙九ヱ繝ｩ繝｡繧ｿ
    ! Kind type parameter
    !
    use dc_types, only: STDOUT ! 讓呎ｺ門�蜉帙�陬�ｽｮ逡ｪ蜿ｷ. Unit number of standard output

    ! 譁�ｭ怜�謫堺ｽ�
    ! Character handling
    !
    use dc_string, only: StoA

    ! 繝偵せ繝医Μ繝��繧ｿ蜃ｺ蜉�
    ! History data output
    !
    use gtool_historyauto, only: HistoryAutoAddVariable

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none

!!$    integer:: unit_nml        ! NAMELIST 繝輔ぃ繧､繝ｫ繧ｪ繝ｼ繝励Φ逕ｨ陬�ｽｮ逡ｪ蜿ｷ.
!!$                              ! Unit number for NAMELIST file open
!!$    integer:: iostat_nml      ! NAMELIST 隱ｭ縺ｿ霎ｼ縺ｿ譎ゅ� IOSTAT.
!!$                              ! IOSTAT of NAMELIST read

    ! NAMELIST 螟画焚鄒､
    ! NAMELIST group name
    !
!!$    namelist /saturate_DennouAGCMOnIce_nml/ &
!!$      & FlagSublimation
          !
          ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｫ縺､縺�※縺ｯ蛻晄悄蛹匁焔邯� "saturate_DennouAGCM#SaturateInit"
          ! 縺ｮ繧ｽ繝ｼ繧ｹ繧ｳ繝ｼ繝峨ｒ蜿ら�縺ｮ縺薙→.
          !
          ! Refer to source codes in the initialization procedure
          ! "saturate_DennouAGCM#SaturateInit" for the default values.
          !

    ! 螳溯｡梧枚 ; Executable statement
    !

    if ( saturate_DennouAGCMOnIce_inited ) return

    ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｮ險ｭ螳�
    ! Default values settings
    !

    ! NAMELIST 縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ
    ! NAMELIST is input
    !
!!$    if ( trim(namelist_filename) /= '' ) then
!!$      call FileOpen( unit_nml, &          ! (out)
!!$        & namelist_filename, mode = 'r' ) ! (in)
!!$
!!$      rewind( unit_nml )
!!$      read( unit_nml, &           ! (in)
!!$        & nml = saturate_DennouAGCM_nml, &  ! (out)
!!$        & iostat = iostat_nml )   ! (out)
!!$      close( unit_nml )
!!$
!!$      call NmlutilMsg( iostat_nml, module_name ) ! (in)
!!$    end if

    ! 蜊ｰ蟄� ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
!!$    call MessageNotify( 'M', module_name, '  FlagSublimation = %b', l = (/ FlagSublimation /) )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    saturate_DennouAGCMOnIce_inited = .true.

  end subroutine SaturateInit

  !--------------------------------------------------------------------------------------

end module saturate_DennouAGCMOnIce