!= Nakajima et al. (1992) を用いた飽和比湿の算出
!
!= Evaluate saturation specific humidity with Nakajima et al. (1992)
!
! Authors::   Masaki ISHIWATARI, Yasuhiro MORIKAWA
! Version::   $Id: saturate_nha1992.f90,v 1.1.1.1 2008-07-30 08:41:33 morikawa Exp $
! Tag Name::  $Name: dcpam5-20080731 $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module saturate_nha1992
  !
  != Nakajima et al. (1992) を用いた飽和比湿の算出
  !
  != Evaluate saturation specific humidity with Nakajima et al. (1992)
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! Nakajima et al. (1992) で用いられた飽和蒸気圧 $ p^{*} $ に関する以下の式
  ! を用い, 飽和比湿および飽和比湿の温度微分の値を算出します. 
  !
  ! Saturation specific humidity and temperature derivative of it 
  ! are calculated with a folloing formula for saturation watar vapor pressure 
  ! $ p^{*} $ in Nakajima et al. (1992). 
  ! 
  ! \[
  !    p^{*} (T) = p_0^{*} \exp \left( - \frac{l}{RT} \right). 
  ! \]
  !
  ! ここで, $ T $ は温度, $ R $ は普遍気体定数です. 
  ! $ R $ および潜熱 $ l $ , 
  ! 水蒸気飽和曲線の定数 $ p_0^{*} $ は以下のように与えられます. 
  !
  ! where $ T $ is temperature, $ R $ is the gas constant. 
  ! $ R $ and latent heat $ l $ , 
  ! constant for the water vapor saturation curve $ p_0^{*} $ are as follows.
  ! 
  ! * $ R = 8.314 $ [J mol-1 K-1]
  ! * $ l = 43655 $ [J mol-1]
  ! * $ p_0^{*} = 1.4 \times 10^{11} $ [Pa]
  !
  ! 飽和水蒸気圧から飽和比湿 $ q^{*} $ を求める際には, 以下の式を用います. 
  !
  ! Saturation specific humidity $ q^{*} $ is calculated from 
  ! saturation watar vapor pressure as follows.
  !
  ! \[
  !    q^{*} (T, P) = \varepsilon \frac{p^{*} (T)}{p}
  ! \]
  !
  ! ここで $ \varepsilon $ は凝結成分と大気の分子量比, 
  ! $ p $ は気圧です. 
  !
  ! where $ \varepsilon $ is molecular weight ratio of water vapor to air, 
  ! $ p $ is air pressure. 
  ! 
  ! 従って, 飽和比湿, 飽和比湿の温度変化を求める式は以下のように
  ! なります. 
  !
  ! Therefore, saturation specific humidity and temperature derivative of it 
  ! are calculated as follows. 
  !
  ! \[
  !    q^{*} (T, P) = \varepsilon \frac{p_0^{*}}{p} \exp \left( - \frac{l}{RT} \right), \] \[
  !    \DP{q^{*} (T, P)}{T} = \varepsilon \frac{p_0^{*}}{p} \frac{l}{RT^2} \exp \left( - \frac{l}{RT} \right).
  ! \]
  !
  !== Procedures List
  !
  ! CalcQVapSat            :: 飽和比湿の計算
  ! CalcDQVapSatDTemp      :: 飽和比湿の温度微分の計算
  ! ------------  :: ------------
  ! CalcQVapSat            :: Calculate saturation specific humidity
  ! CalcDQVapSatDTemp      :: Calculate temperature derivative of saturation specific humidity
  !
  !--
  !== NAMELIST
  !
  ! NAMELIST#saturate_nha1992_nml
  !++

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

  ! 格子点設定
  ! Grid points settings
  !
  use gridset, only: imax, & ! 経度格子点数. 
                             ! Number of grid points in longitude
    &                jmax, & ! 緯度格子点数. 
                             ! Number of grid points in latitude
    &                kmax    ! 鉛直層数. 
                             ! Number of vertical level

  ! 種別型パラメタ
  ! 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:: CalcQVapSat, CalcDQVapSatDTemp

  ! 公開変数
  ! Public variables
  !
  logical, save, public:: saturate_nha1992_inited = .false.
                              ! 初期設定フラグ. 
                              ! Initialization flag

  ! 非公開変数
  ! Private variables
  !
  real(DP), parameter:: LatentHeat = 43655_DP
                              ! $ l $ [J mol-1]. 
                              ! 水の凝結の潜熱. Latent heat of condensation of water vapor
  real(DP), parameter:: P0 = 1.4e+11_DP
                              ! $ p_0^{*} $ [Pa]. 
                              ! 水蒸気飽和曲線の定数. constant for water vapor saturation curve

  character(*), parameter:: module_name = 'saturate_nha1992'
                              ! モジュールの名称. 
                              ! Module name
  character(*), parameter:: version = &
    & '$Name: dcpam5-20080731 $' // &
    & '$Id: saturate_nha1992.f90,v 1.1.1.1 2008-07-30 08:41:33 morikawa Exp $'
                              ! モジュールのバージョン
                              ! Module version

  ! INTERFACE 文 ; INTERFACE statements
  !
  interface CalcQVapSat
    module procedure CalcQVapSat0  !:doc-priority 40:
    module procedure CalcQVapSat2  !:doc-priority 50:
    module procedure CalcQVapSat3  !:doc-priority 60:
  end interface

  interface CalcDQVapSatDTemp
    module procedure CalcDQVapSatDTemp0  !:doc-priority 40:
    module procedure CalcDQVapSatDTemp2  !:doc-priority 50:
    module procedure CalcDQVapSatDTemp3  !:doc-priority 60:
  end interface

contains

  subroutine CalcQVapSat0( &
    & Temp, Press, &         ! (in)
    & QVapSat &              ! (out)
    & )
    !
    ! 温度 *Temp* と気圧 *Press* を用い, 
    ! 飽和比湿 *QVapSat* を求めます. 
    !
    ! スカラーデータではなくを配列データ与える場合には下記の
    ! サブルーチンを用いてください. 
    !
    ! Calculate saturation specific humidity *QVapSat* using
    ! temperature *Temp* and air pressure *Press*. 
    !
    ! If array data is given instead of scalar data, 
    ! use following subroutines. 
    !

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

    ! 物理定数設定
    ! Physical constants settings
    !
    use constants, only: &
      & GasRUniv, &           ! $ R^{*} $ [J K-1 mol-1].
                              ! 普遍気体定数.  Universal gas constant
      & EpsV                  ! $ \epsilon_v $ . 
                              ! 水蒸気分子量比. 
                              ! Molecular weight of water vapor

    ! 宣言文 ; Declaration statements
    !
    implicit none

    real(DP), intent(in):: Temp
                              ! $ T $ . 温度. Temperature
    real(DP), intent(in):: Press
                              ! $ p $ . 気圧. Air pressure
    real(DP), intent(out):: QVapSat
                              ! $ q^{*} $ . 飽和比湿. Saturation specific humidity

    ! 作業変数
    ! Work variables
    !

    ! 実行文 ; Executable statement
    !

    ! 飽和比湿の計算
    ! Calculate saturation specific humidity
    !
    QVapSat = EpsV * ( P0 / Press ) * exp ( - LatentHeat / ( GasRUniv * Temp ) )

  end subroutine CalcQVapSat0



  subroutine CalcQVapSat2( &
    & xy_Temp, xy_Press, &      ! (in)
    & xy_QVapSat &              ! (out)
    & )
    !
    ! 温度 *xy_Temp* と気圧 *xy_Press* を用い, 
    ! 飽和比湿 *xy_QVapSat* を求めます. 
    !
    ! Calculate saturation specific humidity *xy_QVapSat* using
    ! temperature *xy_Temp* and air pressure *xy_Press*. 
    !

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

    ! 宣言文 ; Declaration statements
    !
    implicit none

    real(DP), intent(in):: xy_Temp (0:imax-1, 1:jmax)
                              ! $ T $ . 温度. Temperature
    real(DP), intent(in):: xy_Press (0:imax-1, 1:jmax)
                              ! $ p $ . 気圧. Air pressure
    real(DP), intent(out):: xy_QVapSat (0:imax-1, 1:jmax)
                              ! $ q^{*} $ . 飽和比湿. Saturation specific humidity

    ! 作業変数
    ! Work variables
    !
    integer:: i               ! 経度方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in longitude
    integer:: j               ! 緯度方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in latitude

    ! 実行文 ; Executable statement
    !

    ! 初期化
    ! Initialization
    !
    if ( .not. saturate_nha1992_inited ) call SaturateInit

    ! 飽和比湿の計算
    ! Calculate saturation specific humidity
    !
    do i = 0, imax - 1
      do j = 1, jmax
        call CalcQVapSat0( &
          & xy_Temp(i,j), xy_Press(i,j), &    ! (in)
          & xy_QVapSat(i,j) )                 ! (out)
      end do
    end do

  end subroutine CalcQVapSat2



  subroutine CalcQVapSat3( &
    & xyz_Temp, xyz_Press, &     ! (in)
    & xyz_QVapSat &              ! (out)
    & )
    !
    ! 温度 *xyz_Temp* と気圧 *xyz_Press* を用い, 
    ! 飽和比湿 *xyz_QVapSat* を求めます. 
    !
    ! Calculate saturation specific humidity *xyz_QVapSat* using
    ! temperature *xyz_Temp* and air pressure *xyz_Press*. 
    !

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

    ! 宣言文 ; Declaration statements
    !
    implicit none

    real(DP), intent(in):: xyz_Temp (0:imax-1, 1:jmax, 1:kmax)
                              ! $ T $ . 温度. Temperature
    real(DP), intent(in):: xyz_Press (0:imax-1, 1:jmax, 1:kmax)
                              ! $ p $ . 気圧. Air pressure
    real(DP), intent(out):: xyz_QVapSat (0:imax-1, 1:jmax, 1:kmax)
                              ! $ q^{*} $ . 飽和比湿. Saturation specific humidity

    ! 作業変数
    ! Work variables
    !
    integer:: i               ! 経度方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in longitude
    integer:: j               ! 緯度方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in latitude
    integer:: k               ! 鉛直方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in vertical direction

    ! 実行文 ; Executable statement
    !

    ! 初期化
    ! Initialization
    !
    if ( .not. saturate_nha1992_inited ) call SaturateInit

    ! 飽和比湿の計算
    ! Calculate saturation specific humidity
    !
    do i = 0, imax - 1
      do j = 1, jmax
        do k = 1, kmax
          call CalcQVapSat0( &
            & xyz_Temp(i,j,k), xyz_Press(i,j,k), &    ! (in)
            & xyz_QVapSat(i,j,k) )                    ! (out)
        end do
      end do
    end do

  end subroutine CalcQVapSat3



  subroutine CalcDQVapSatDTemp0( &
    & Temp, Press, &         ! (in)
    & DQVapSatDTemp &        ! (out)
    & )
    !
    ! 温度 *Temp* と気圧 *Press* を用い, 
    ! 飽和比湿の温度微分 *DQVapSatDTemp* を求めます. 
    !
    ! スカラーデータではなくを配列データ与える場合には下記の
    ! サブルーチンを用いてください. 
    !
    ! Calculate temperature derivative of saturation specific humidity 
    ! *DQVapSatDTemp* using
    ! temperature *Temp* and air pressure *Press*. 
    !
    ! If array data is given instead of scalar data, 
    ! use following subroutines. 
    !

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

    ! 物理定数設定
    ! Physical constants settings
    !
    use constants, only: &
      & GasRUniv, &           ! $ R^{*} $ [J K-1 mol-1].
                              ! 普遍気体定数.  Universal gas constant
      & EpsV                  ! $ \epsilon_v $ . 
                              ! 水蒸気分子量比. 
                              ! Molecular weight of water vapor

    ! 宣言文 ; Declaration statements
    !
    implicit none

    real(DP), intent(in):: Temp
                              ! $ T $ . 温度. Temperature
    real(DP), intent(in):: Press
                              ! $ p $ . 気圧. Air pressure
    real(DP), intent(out):: DQVapSatDTemp
                              ! $ \DP{q^{*}}{T} $ . 飽和比湿の温度微分. 
                              ! Temperature derivative of saturation specific humidity. 

    ! 作業変数
    ! Work variables
    !

    ! 実行文 ; Executable statement
    !

    ! 飽和比湿の温度微分の計算
    ! Calculate temperature derivative of saturation specific humidity
    !
    DQVapSatDTemp = &
      & EpsV * ( P0 / Press ) &
      &   * ( LatentHeat / ( GasRUniv * Temp**2 ) ) &
      &   * exp ( - LatentHeat / ( GasRUniv * Temp ) )

  end subroutine CalcDQVapSatDTemp0



  subroutine CalcDQVapSatDTemp2( &
    & xy_Temp, xy_Press, &         ! (in)
    & xy_DQVapSatDTemp &           ! (out)
    & )
    !
    ! 温度 *xy_Temp* と気圧 *xy_Press* を用い, 
    ! 飽和比湿の温度微分 *xy_DQVapSatDTemp* を求めます. 
    !
    ! Calculate temperature derivative of saturation specific humidity 
    ! *xy_DQVapSatDTemp* using
    ! temperature *xy_Temp* and air pressure *xy_Press*. 
    !

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

    ! 宣言文 ; Declaration statements
    !
    implicit none

    real(DP), intent(in):: xy_Temp (0:imax-1, 1:jmax)
                              ! $ T $ . 温度. Temperature
    real(DP), intent(in):: xy_Press (0:imax-1, 1:jmax)
                              ! $ p $ . 気圧. Air pressure
    real(DP), intent(out):: xy_DQVapSatDTemp (0:imax-1, 1:jmax)
                              ! $ \DP{q^{*}}{T} $ . 飽和比湿の温度微分. 
                              ! Temperature derivative of saturation specific humidity. 

    ! 作業変数
    ! Work variables
    !
    integer:: i               ! 経度方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in longitude
    integer:: j               ! 緯度方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in latitude

    ! 実行文 ; Executable statement
    !

    ! 初期化
    ! Initialization
    !
    if ( .not. saturate_nha1992_inited ) call SaturateInit

    ! 飽和比湿の温度微分の計算
    ! Calculate saturation specific humidity
    !
    do i = 0, imax - 1
      do j = 1, jmax
        call CalcDQVapSatDTemp0( &
          & xy_Temp(i,j), xy_Press(i,j), &    ! (in)
          & xy_DQVapSatDTemp(i,j) )           ! (out)
      end do
    end do

  end subroutine CalcDQVapSatDTemp2



  subroutine CalcDQVapSatDTemp3( &
    & xyz_Temp, xyz_Press, &         ! (in)
    & xyz_DQVapSatDTemp &            ! (out)
    & )
    !
    ! 温度 *xyz_Temp* と気圧 *xyz_Press* を用い, 
    ! 飽和比湿の温度微分 *xyz_DQVapSatDTemp* を求めます. 
    !
    ! Calculate temperature derivative of saturation specific humidity 
    ! *xyz_DQVapSatDTemp* using
    ! temperature *xyz_Temp* and air pressure *xyz_Press*. 
    !

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

    ! 宣言文 ; Declaration statements
    !
    implicit none

    real(DP), intent(in):: xyz_Temp (0:imax-1, 1:jmax, 1:kmax)
                              ! $ T $ . 温度. Temperature
    real(DP), intent(in):: xyz_Press (0:imax-1, 1:jmax, 1:kmax)
                              ! $ p $ . 気圧. Air pressure
    real(DP), intent(out):: xyz_DQVapSatDTemp (0:imax-1, 1:jmax, 1:kmax)
                              ! $ q^{*} $ . 飽和比湿. Saturation specific humidity

    ! 作業変数
    ! Work variables
    !
    integer:: i               ! 経度方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in longitude
    integer:: j               ! 緯度方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in latitude
    integer:: k               ! 鉛直方向に回る DO ループ用作業変数
                              ! Work variables for DO loop in vertical direction

    ! 実行文 ; Executable statement
    !

    ! 初期化
    ! Initialization
    !
    if ( .not. saturate_nha1992_inited ) call SaturateInit

    ! 飽和比湿の温度微分の計算
    ! Calculate saturation specific humidity
    !
    do i = 0, imax - 1
      do j = 1, jmax
        do k = 1, kmax
          call CalcDQVapSatDTemp( &
            & xyz_Temp(i,j,k), xyz_Press(i,j,k), &    ! (in)
            & xyz_DQVapSatDTemp(i,j,k) )              ! (out)
        end do
      end do
    end do

  end subroutine CalcDQVapSatDTemp3



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

    ! 実行文 ; Executable statement
    !

    if ( saturate_nha1992_inited ) return
    call InitCheck

    ! デフォルト値の設定
    ! 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_nha1992_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_nha1992_inited = .true.
  end subroutine SaturateInit



  subroutine InitCheck
    !
    ! 依存モジュールの初期化チェック
    !
    ! Check initialization of dependency modules

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

    ! 格子点設定
    ! Grid points settings
    !
    use gridset, only: gridset_inited

    ! 物理定数設定
    ! Physical constants settings
    !
    use constants, only: constants_inited

    ! 実行文 ; Executable statement
    !

    if ( .not. gridset_inited ) &
      & call MessageNotify( 'E', module_name, '"gridset" module is not initialized.' )

    if ( .not. constants_inited ) &
      & call MessageNotify( 'E', module_name, '"constants" module is not initialized.' )

  end subroutine InitCheck

end module saturate_nha1992
