!= 飽和比湿の算出
!
!= Evaluate saturation specific humidity
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: saturate.F90,v 1.1 2010-09-12 02:08:11 yot Exp $
! Tag Name::  $Name: dcpam5-20120220 $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module saturate
  !
  != 飽和比湿の算出
  !
  != Evaluate saturation specific humidity
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! 飽和比湿および飽和比湿の温度微分の値を算出します. 
  !
  ! Saturation specific humidity and temperature derivative of it are calculated. 
  !
  ! 飽和比湿の計算にはデフォルトでは
  ! Tetens (1930) を用いる (saturate_t1930 参照). 
  ! また, Config.mk の CPPFLAGS に -DLIB_SATURATE_NHA1992 を指定すると
  ! Nakajima et al. (1992) を用いる (saturate_tnha1992 参照). 
  !
  ! By default, Tetens (1930) is used for calculation of 
  ! saturation specific humidity (See "saturate_t1930"). 
  ! If "-DLIB_SATURATE_NHA1992" is specified to "CPPFLAGS" in Config.mk, 
  ! Nakajima et al. (1992) is used (See "saturate_nha1992"). 
  !
  !== References
  !
  !== Procedures List
  !
  ! CalcQVapSat            :: 飽和比湿の計算
  ! CalcDQVapSatDTemp      :: 飽和比湿の温度微分の計算
  ! ------------  :: ------------
  ! CalcQVapSat            :: Calculate saturation specific humidity
  ! CalcDQVapSatDTemp      :: Calculate temperature derivative of saturation specific humidity
  !
  !--
  !== NAMELIST
  !
  ! NAMELIST#saturate_nml
  !++

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

  ! 種別型パラメタ
  ! Kind type parameter
  !
  use dc_types, only: DP, &      ! 倍精度実数型. Double precision. 
    &                 STRING     ! 文字列.       Strings. 

  ! メッセージ出力
  ! Message output
  !
  use dc_message, only: MessageNotify

  ! 格子点設定
  ! 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

#ifdef LIB_SATURATE_NHA1992
    ! Nakajima et al. (1992) を用いた飽和比湿の算出
    ! Evaluate saturation specific humidity with Nakajima et al. (1992)
    !
    use saturate_nha1992, only:                             &
#elif LIB_SATURATE_T1930
    ! Tetens (1930) を用いた飽和比湿の算出
    ! Evaluate saturation specific humidity with Tetens (1930)
    !
    use saturate_t1930, only:                               &
#else
    ! Tetens (1930) を用いた飽和比湿の算出
    ! Evaluate saturation specific humidity with Tetens (1930)
    !
    use saturate_t1930, only:                               &
#endif
      & xy_CalcQVapSatCore        => xy_CalcQVapSat       , &
      & xy_CalcDQVapSatDTempCore  => xy_CalcDQVapSatDTemp , &
      & xyz_CalcQVapSatCore       => xyz_CalcQVapSat      , &
      & xyz_CalcDQVapSatDTempCore => xyz_CalcDQVapSatDTemp



  ! 宣言文 ; Declaration statements
  !
  implicit none
  private

  ! 公開手続き
  ! Public procedure
  !
  public:: xy_CalcQVapSat , xy_CalcDQVapSatDTemp
  public:: xyz_CalcQVapSat, xyz_CalcDQVapSatDTemp

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

  ! 非公開変数
  ! Private variables
  !
  character(*), parameter:: module_name = 'saturate'
                              ! モジュールの名称. 
                              ! Module name

  character(*), parameter:: saturate_scheme = &
#ifdef LIB_SATURATE_NHA1992
    & 'Nakajima et al. (1992)'
#elif LIB_SATURATE_T1930
    & 'Tetens (1930)'
#else
    & 'Tetens (1930)'
#endif

  character(*), parameter:: version = &
    & '$Name: dcpam5-20120220 $' // &
    & '$Id: saturate.F90,v 1.1 2010-09-12 02:08:11 yot Exp $'
                              ! モジュールのバージョン
                              ! Module version


contains

  function xy_CalcQVapSat( 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

    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):: xy_QVapSat(0:imax-1, 1:jmax)
                              ! $ q^{*} $ . 飽和比湿. Saturation specific humidity

    ! 作業変数
    ! Work variables
    !

    ! 実行文 ; Executable statement
    !

    call SaturateInit

    xy_QVapSat = xy_CalcQVapSatCore( 0, imax-1, 1, jmax, xy_Temp, xy_Press )


  end function xy_CalcQVapSat

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

  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
    !

    ! 宣言文 ; 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):: xyz_QVapSat(0:imax-1, 1:jmax, 1:kmax)
                              ! $ q^{*} $ . 飽和比湿. Saturation specific humidity

    ! 作業変数
    ! Work variables
    !

    ! 実行文 ; Executable statement
    !

    call SaturateInit

    xyz_QVapSat = xyz_CalcQVapSatCore( 0, imax-1, 1, jmax, 1, kmax, xyz_Temp, xyz_Press )


  end function xyz_CalcQVapSat

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

  function xy_CalcDQVapSatDTemp( 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

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

    ! 作業変数
    ! Work variables
    !

    ! 実行文 ; Executable statement
    !

    call SaturateInit

    xy_DQVapSatDTemp = xy_CalcDQVapSatDTempCore( 0, imax-1, 1, jmax, xy_Temp, xy_QVapSat )


  end function xy_CalcDQVapSatDTemp

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

  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
    !

    ! 宣言文 ; Declaration statements
    !
    implicit none

    real(DP), intent(in):: xyz_Temp   (0:imax-1, 1:jmax, 1:kmax)
                              ! $ T $ . 温度. Temperature
    real(DP), intent(in):: xyz_QVapSat(0:imax-1, 1:jmax, 1:kmax)
                              ! $ q^{*} $ . 飽和比湿. Saturation specific humidity
    real(DP):: xyz_DQVapSatDTemp(0:imax-1, 1:jmax, 1:kmax)
                              ! $ \DP{q^{*}}{T} $ . 飽和比湿の温度微分. 
                              ! Temperature derivative of saturation specific humidity. 

    ! 作業変数
    ! Work variables
    !

    ! 実行文 ; Executable statement
    !

    call SaturateInit

    xyz_DQVapSatDTemp = xyz_CalcDQVapSatDTempCore( 0, imax-1, 1, jmax, 1, kmax, xyz_Temp, xyz_QVapSat )

  end function xyz_CalcDQVapSatDTemp

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

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

    ! 実行文 ; Executable statement
    !

    if ( saturate_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_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) )
    call MessageNotify( 'M', module_name, '  Scheme of saturation = %c', &
      & c1 = saturate_scheme )
    saturate_inited = .true.
  end subroutine SaturateInit

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

end module saturate
