!= 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

  logical, save      :: FlagSublimation
                              !
                              ! flag for treating sublimation

  ! å…¬é–‹æ‰‹ç¶šã
  ! 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