!= Tetens (1930) を用いた飽和比湿の算出
!
!= Evaluate saturation specific humidity with Tetens (1930)
!
! Authors::   Yoshiyuki O. Takahashi, Yasuhiro MORIKAWA
! Version::   $Id: saturate_t1930.f90,v 1.2 2011-06-19 10:56:27 yot Exp $
! Tag Name::  $Name: dcpam5-20120229 $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module saturate_t1930
  !
  != Tetens (1930) を用いた飽和比湿の算出
  !
  != Evaluate saturation specific humidity with Tetens (1930)
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! Tetens (1930) より, 飽和蒸気圧 $ p^{*} $ に関する以下の式
  ! を用い, 飽和比湿および飽和比湿の温度微分の値を算出します. 
  !
  ! Saturation specific humidity and temperature derivative of it 
  ! are calculated with a folloing formula for saturation watar vapor pressure 
  ! $ p^{*} $ in Tetens (1930). 
  !
  ! \[
  !    q^{*} (T, p) 
  !      = 611 \varepsilon 
  !        \frac{ \exp \left( \frac{l}{R_v} 
  !                           \left( \frac{1}{273} - \frac{1}{T} \right)
  !                    \right) } { p } \] \[
  !    \DP{q^{*} (T, p)}{T} 
  !      = 611 \varepsilon \frac{l}{R_v T^2}
  !        \frac{ \exp \left( \frac{l}{R_v} 
  !                           \left( \frac{1}{273} - \frac{1}{T} \right)
  !                    \right) } { p }
  !      = \frac{q^{*} l}{R_v T^2}
  ! \]
  !
  ! ここで, $ T $ は温度, $ p $ は気圧, $ R_v $ は凝結成分の気体定数,  
  ! $ \varepsilon $ は凝結成分と大気の分子量比, 
  ! $ l $ は凝結成分の潜熱です. 
  !
  !== References
  !
  ! * Tetens, O., 1930: 
  !   Uder einige meteorologische Begriffe.
  !   <i>Zeitschrift fur Geophysik</i>, <b>6</b>, 297--309.
  !
  !== 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:: xy_CalcQVapSat , xy_CalcDQVapSatDTemp
  public:: xyz_CalcQVapSat, xyz_CalcDQVapSatDTemp

  ! 公開変数
  ! Public variables
  !
  logical, save, public:: saturate_t1930_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_t1930'
                              ! モジュールの名称. 
                              ! Module name
  character(*), parameter:: version = &
    & '$Name: dcpam5-20120229 $' // &
    & '$Id: saturate_t1930.f90,v 1.2 2011-06-19 10:56:27 yot Exp $'
                              ! モジュールのバージョン
                              ! Module version

contains

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

  function xyz_CalcQVapSat( is, ie, js, je, ks, ke, 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
      & EpsV                  ! $ \epsilon_v $ . 
                              ! 水蒸気分子量比. 
                              ! Molecular weight of water vapor

    ! 宣言文 ; Declaration statements
    !
    implicit none

    integer , intent(in):: is
    integer , intent(in):: ie
    integer , intent(in):: js
    integer , intent(in):: je
    integer , intent(in):: ks
    integer , intent(in):: ke
    real(DP), intent(in):: xyz_Temp (is:ie, js:je, ks:ke)
                              ! $ T $ . 温度. Temperature
    real(DP), intent(in):: xyz_Press(is:ie, js:je, ks:ke)
                              ! $ p $ . 気圧. Air pressure

    real(DP):: xyz_QVapSat(is:ie, js:je, ks:ke)
                              ! $ q^{*} $ . 飽和比湿. Saturation specific humidity

    ! 作業変数
    ! Work variables
    !

    ! 実行文 ; Executable statement
    !

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


  end function xyz_CalcQVapSat

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

  function xyz_CalcDQVapSatDTemp( is, ie, js, je, ks, ke, 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
      & EpsV                  ! $ \epsilon_v $ . 
                              ! 水蒸気分子量比. 
                              ! Molecular weight of water vapor

    ! 宣言文 ; Declaration statements
    !
    implicit none

    integer , intent(in):: is
    integer , intent(in):: ie
    integer , intent(in):: js
    integer , intent(in):: je
    integer , intent(in):: ks
    integer , intent(in):: ke
    real(DP), intent(in):: xyz_Temp   (is:ie, js:je, ks:ke)
                              ! $ T $ . 温度. Temperature
    real(DP), intent(in):: xyz_QVapSat(is:ie, js:je, ks:ke)
                              ! $ q^{*} $ . 飽和比湿. Saturation specific humidity
    real(DP):: xyz_DQVapSatDTemp(is:ie, js:je, ks:ke)
                              ! $ \DP{q^{*}}{T} $ . 飽和比湿の温度微分. 
                              ! Temperature derivative of saturation specific humidity. 

    ! 作業変数
    ! Work variables
    !

    ! 実行文 ; Executable statement
    !
    xyz_DQVapSatDTemp = LatentHeat * xyz_QVapSat / ( GasRWet * xyz_Temp**2 )


  end function xyz_CalcDQVapSatDTemp

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

  function xy_CalcQVapSat( is, ie, js, je, xy_Temp, xy_Press ) result( xy_QVapSat )
    !
    ! 温度 *Temp* と気圧 *Press* を用い, 
    ! 飽和比湿 *QVapSat* を求めます. 
    !
    ! Calculate saturation specific humidity *QVapSat* using
    ! temperature *Temp* and air pressure *Press*. 
    !

    ! モジュール引用 ; USE statements
    !

    ! 宣言文 ; Declaration statements
    !
    implicit none

    integer , intent(in):: is
    integer , intent(in):: ie
    integer , intent(in):: js
    integer , intent(in):: je
    real(DP), intent(in):: xy_Temp (is:ie, js:je)
                              ! $ T $ . 温度. Temperature
    real(DP), intent(in):: xy_Press(is:ie, js:je)
                              ! $ p $ . 気圧. Air pressure

    real(DP):: xy_QVapSat(is:ie, js:je)
                              ! $ q^{*} $ . 飽和比湿. Saturation specific humidity

    ! 作業変数
    ! Work variables
    !
    real(DP):: xyz_Temp   (is:ie, js:je, 1:1)
                              ! $ T $ . 温度. Temperature
    real(DP):: xyz_Press  (is:ie, js:je, 1:1)
                              ! $ p $ . 気圧. Air pressure
    real(DP):: xyz_QVapSat(is:ie, js:je, 1:1)
                              ! $ q^{*} $ . 飽和比湿. Saturation specific humidity


    xyz_Temp (:,:,1) = xy_Temp
    xyz_Press(:,:,1) = xy_Press

    xyz_QVapSat = xyz_CalcQVapSat( is, ie, js, je, 1, 1, xyz_Temp, xyz_Press )

    xy_QVapSat = xyz_QVapSat(:,:,1)


  end function xy_CalcQVapSat

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

  function xy_CalcDQVapSatDTemp( is, ie, js, je, xy_Temp, xy_QVapSat ) result( xy_DQVapSatDTemp )
    !
    ! 温度 *Temp* と飽和比湿 *QVapSat* を用い, 
    ! 飽和比湿の温度微分 *DQVapSatDTemp* を求めます. 
    !
    ! Calculate temperature derivative of saturation specific humidity 
    ! *DQVapSatDTemp* using
    ! temperature *Temp* and saturation specific humidity *QVapSat*. 
    !

    ! モジュール引用 ; USE statements
    !

    ! 宣言文 ; Declaration statements
    !
    implicit none

    integer , intent(in):: is
    integer , intent(in):: ie
    integer , intent(in):: js
    integer , intent(in):: je
    real(DP), intent(in):: xy_Temp   (is:ie, js:je)
                              ! $ T $ . 温度. Temperature
    real(DP), intent(in):: xy_QVapSat(is:ie, js:je)
                              ! $ q^{*} $ . 飽和比湿. Saturation specific humidity
    real(DP):: xy_DQVapSatDTemp(is:ie, js:je)
                              ! $ \DP{q^{*}}{T} $ . 飽和比湿の温度微分. 
                              ! Temperature derivative of saturation specific humidity. 

    ! 作業変数
    ! Work variables
    !
    real(DP):: xyz_Temp   (is:ie, js:je, 1:1)
                              ! $ T $ . 温度. Temperature
    real(DP):: xyz_QVapSat(is:ie, js:je, 1:1)
                              ! $ q^{*} $ . 飽和比湿. Saturation specific humidity
    real(DP):: xyz_DQVapSatDTemp(is:ie, js:je, 1:1)
                              ! $ \DP{q^{*}}{T} $ . 飽和比湿の温度微分. 
                              ! Temperature derivative of saturation specific humidity. 


    ! 実行文 ; Executable statement
    !

    xyz_Temp   (:,:,1) = xy_Temp
    xyz_QVapSat(:,:,1) = xy_QVapSat

    xyz_DQVapSatDTemp = xyz_CalcDQVapSatDTemp( is, ie, js, je, 1, 1, xyz_Temp, xyz_QVapSat )

    xy_DQVapSatDTemp = xyz_DQVapSatDTemp(:,:,1)


  end function xy_CalcDQVapSatDTemp

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

  subroutine SaturateInit
    !
    ! saturate_t1930 モジュールの初期化を行います. 
    !--
    ! NAMELIST#saturate_t1930_nml の読み込みはこの手続きで行われます. 
    !++
    !
    ! "saturate_t1930" module is initialized. 
    !--
    ! "NAMELIST#saturate_t1930_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_t1930_nml/ 
          !
          ! デフォルト値については初期化手続 "saturate_t1930#SaturateInit" 
          ! のソースコードを参照のこと. 
          !
          ! Refer to source codes in the initialization procedure
          ! "saturate_t1930#SaturateInit" for the default values. 
          !

    ! 実行文 ; Executable statement
    !

    if ( saturate_t1930_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_t1930_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, '-- version = %c', c1 = trim(version) )

    saturate_t1930_inited = .true.

  end subroutine SaturateInit

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

end module saturate_t1930
