!= 荳ｻ謌仙�逶ｸ螟牙喧
!
!= Phase change of atmospheric major component
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: saturate_major_comp.f90,v 1.1 2013/09/30 02:55:27 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module saturate_major_comp
  !
  != 荳ｻ謌仙�逶ｸ螟牙喧
  !
  != Phase change of atmospheric major component
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  !== References
  !
  ! * Pollack, J. B., C. B. Leovy, P. W. Greiman, and Y. Mintz, 1981:
  !   A Martian general circulation experiment with large topography,
  !   <i>J. Atmos. Sci.</i>, <b>38</b>, 3--29.
  !
  !== Procedures List
  !
!!$  ! DryConvAdjust :: 荵ｾ辯･蟇ｾ豬∬ｪｿ遽
!!$  ! ------------  :: ------------
!!$  ! DryConvAdjust :: Dry convective adjustment
  !
  !== NAMELIST
  !
  ! NAMELIST#saturate_major_comp_nml
  !

  ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
  !

  ! 遞ｮ蛻･蝙九ヱ繝ｩ繝｡繧ｿ
  ! Kind type parameter
  !
  use dc_types, only: DP, &      ! 蛟咲ｲｾ蠎ｦ螳滓焚蝙�. Double precision.
    &                 STRING     ! 譁�ｭ怜�.       Strings.

  ! NAMELIST 繝輔ぃ繧､繝ｫ蜈･蜉帙↓髢｢縺吶ｋ繝ｦ繝ｼ繝�ぅ繝ｪ繝�ぅ
  ! Utilities for NAMELIST file input
  !
  use namelist_util, only: MaxNmlArySize
                              ! NAMELIST 縺九ｉ隱ｭ縺ｿ霎ｼ繧驟榊�縺ｮ譛螟ｧ繧ｵ繧､繧ｺ.
                              ! Maximum size of arrays loaded from NAMELIST

  ! 繝｡繝�そ繝ｼ繧ｸ蜃ｺ蜉�
  ! Message output
  !
  use dc_message, only: MessageNotify

  ! 螳｣險譁� ; Declaration statements
  !
  implicit none
  private

  integer, save      :: IndexSpc
  integer, parameter :: IndexSpcCO2     = 1

  real(DP), parameter :: LatentHeatCO2Subl = 5.9d5
                              ! Latent heat of CO2 (J kg^{-1})
                              ! This value is obtained from Pollack et al.
                              ! (1981) and Forget et al. (1998).

  ! 蜈ｬ髢区焔邯壹″
  ! Public procedure
  !
  public :: SaturateMajorCompCalcCondTemp
  public :: SaturateMajorCompInqLatentHeat
  public :: SaturateMajorCompInit

  ! 蜈ｬ髢句､画焚
  ! Public variables
  !
  logical, save, public:: saturate_major_comp_inited = .false.
                              ! 蛻晄悄險ｭ螳壹ヵ繝ｩ繧ｰ.
                              ! Initialization flag

  ! 髱槫�髢句､画焚
  ! Private variables
  !
  character(*), parameter:: module_name = 'saturate_major_comp'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: saturate_major_comp.f90,v 1.1 2013/09/30 02:55:27 yot Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

  interface SaturateMajorCompCalcCondTemp
    module procedure &
      SaturateMajorCompCalcCondTemp3D, &
      SaturateMajorCompCalcCondTemp2D
  end interface

contains

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

  subroutine SaturateMajorCompCalcCondTemp3D(  &
    & xyz_Press,                               &  ! (in)
    & xyz_TempCond                             &  ! (inout)
    & )
    !
    ! 荳ｻ謌仙�逶ｸ螟牙喧
    !
    ! Major component phase change
    !

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
    !

    !=
    != CO2 condensation temperature (Pollack et al., 1981)
    !
    use saturate_co2_p81, only : xyz_SaturateCO2P81TempCond

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none

    real(DP), intent(in ):: xyz_Press   (:,:,:)
                              ! $ p $ . 豌怜悸 (謨ｴ謨ｰ繝ｬ繝吶Ν).
                              ! Air pressure (full level)
    real(DP), intent(out):: xyz_TempCond(:,:,:)
                              !
                              ! Condensation temperature

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹�
    ! Initialization
    !
    if ( .not. saturate_major_comp_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    select case ( IndexSpc )
    case ( IndexSpcCO2 )
      ! CO2 condensation temperature

      xyz_TempCond = xyz_SaturateCO2P81TempCond( xyz_Press )

    end select

  end subroutine SaturateMajorCompCalcCondTemp3D

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

  subroutine SaturateMajorCompCalcCondTemp2D(  &
    & xy_Press,                                &  ! (in)
    & xy_TempCond                              &  ! (inout)
    & )
    !
    ! 荳ｻ謌仙�逶ｸ螟牙喧
    !
    ! Major component phase change
    !

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
    !

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none

    real(DP), intent(in ):: xy_Press   (:,:)
                              ! $ p $ . 豌怜悸 (謨ｴ謨ｰ繝ｬ繝吶Ν).
                              ! Air pressure (full level)
    real(DP), intent(out):: xy_TempCond(:,:)
                              !
                              ! Condensation temperature

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    real(DP) :: xyz_Press   (size(xy_Press   ,1),size(xy_Press   ,2),1)
    real(DP) :: xyz_TempCond(size(xy_TempCond,1),size(xy_TempCond,2),1)

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹�
    ! Initialization
    !
    if ( .not. saturate_major_comp_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    xyz_Press(:,:,1) = xy_Press

    call SaturateMajorCompCalcCondTemp3D(  &
      & xyz_Press,                         &  ! (in)
      & xyz_TempCond                       &  ! (inout)
      & )

    xy_TempCond = xyz_TempCond(:,:,1)

  end subroutine SaturateMajorCompCalcCondTemp2D

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

  function SaturateMajorCompInqLatentHeat( ) result( LatentHeat )
    !
    ! 荳ｻ謌仙�縺ｮ貎懃�繧定ｿ斐＠縺励∪縺�.
    !
    ! Inquiry of latent heat of major component
    !

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
    !

    ! 繝｡繝�そ繝ｼ繧ｸ蜃ｺ蜉�
    ! Message output
    !
    use dc_message, only: MessageNotify

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none

    real(DP) :: LatentHeat

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹�
    ! Initialization
    !
    if ( .not. saturate_major_comp_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    ! Set latent heat
    select case ( IndexSpc )
    case ( IndexSpcCO2  )
      LatentHeat = LatentHeatCO2Subl
    case default
      call MessageNotify( 'E', module_name, 'This specice is not supported.' )
    end select

  end function SaturateMajorCompInqLatentHeat

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

  subroutine SaturateMajorCompInit(  &
    & CondMajCompName                & ! (in)
    & )
    !
    ! saturate_major_comp 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蛻晄悄蛹悶ｒ陦後＞縺ｾ縺�.
    ! NAMELIST#saturate_major_comp_nml 縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ縺ｯ縺薙�謇狗ｶ壹″縺ｧ陦後ｏ繧後∪縺�.
    !
    ! "saturate_major_comp" module is initialized.
    ! "NAMELIST#saturate_major_comp_nml" is loaded in this procedure.
    !

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
    !

!!$    ! NAMELIST 繝輔ぃ繧､繝ｫ蜈･蜉帙↓髢｢縺吶ｋ繝ｦ繝ｼ繝�ぅ繝ｪ繝�ぅ
!!$    ! Utilities for NAMELIST file input
!!$    !
!!$    use namelist_util, only: namelist_filename, NmlutilMsg, NmlutilAryValid
!!$
!!$    ! 繝輔ぃ繧､繝ｫ蜈･蜃ｺ蜉幄｣懷勧
!!$    ! 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

    !=
    != CO2 condensation temperature (Pollack et al., 1981)
    !
    use saturate_co2_p81, only : SaturateCO2P81Init

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none

    character(*), intent(in) :: CondMajCompName
                                        ! Condensable major component

!!$    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_major_comp_nml/ &
!!$      & FlagUse

          ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｫ縺､縺�※縺ｯ蛻晄悄蛹匁焔邯� "saturate_major_comp#MajorCompSaturateInit"
          ! 縺ｮ繧ｽ繝ｼ繧ｹ繧ｳ繝ｼ繝峨ｒ蜿ら�縺ｮ縺薙→.
          !
          ! Refer to source codes in the initialization procedure
          ! "saturate_major_comp#MajorCompSaturateInit" for the default values.
          !

    ! 螳溯｡梧枚 ; Executable statement
    !

    if ( saturate_major_comp_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_major_comp_nml, &  ! (out)
!!$        & iostat = iostat_nml )             ! (out)
!!$      close( unit_nml )
!!$
!!$      call NmlutilMsg( iostat_nml, module_name ) ! (in)
!!$!      if ( iostat_nml == 0 ) write( STDOUT, nml = cumulus_adjust_nml )
!!$    end if

    ! Set index
    select case ( CondMajCompName )
    case ( 'CO2' )
      IndexSpc = IndexSpcCO2
    case default
      call MessageNotify( 'E', module_name, 'This specice is not supported.' )
    end select

    ! Initialization of modules called in this module
    !

    !=
    != CO2 condensation temperature (Pollack et al., 1981)
    !
    call SaturateCO2P81Init

    ! 蜊ｰ蟄� ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    saturate_major_comp_inited = .true.

  end subroutine SaturateMajorCompInit

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

end module saturate_major_comp