!= 税下孺季の换叫
!
!= 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-20120413 $
! 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-20120413 $' // &
    & '$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