!= 髮ｪ縺ｨ豬ｷ豌ｷ縺ｮ螳壽焚縺ｮ險ｭ螳�
!
!= Setting constants of snow and sea ice
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: constants_snowseaice.f90,v 1.5 2015/01/29 12:07:29 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module constants_snowseaice
  !
  != 髮ｪ縺ｨ豬ｷ豌ｷ縺ｮ螳壽焚縺ｮ險ｭ螳�
  !
  != Setting constants of snow and sea ice
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! 髮ｪ縺ｨ豬ｷ豌ｷ縺ｮ螳壽焚縺ｮ險ｭ螳壹♀繧医�菫晉ｮ｡繧定｡後＞縺ｾ縺�.
  ! 縺薙ｌ繧峨�蛟､縺ｯ NAMELIST 螟画焚鄒､ constants_snowseaice_nml 縺ｫ繧医▲縺ｦ螟画峩縺吶ｋ縺薙→縺�
  ! 蜿ｯ閭ｽ縺ｧ縺�.
  !
  ! Constants of snow and sea ice are set and stored.
  ! These values can be changed by NAMELIST group name
  ! "constants_snowseaice_nml".
  !
  !== Procedures List
  !
  ! ConstantsSnowSeaIceInit :: 迚ｩ逅�ｮ壽焚縺ｮ險ｭ螳�
  ! ------------  :: ------------
  ! ConstantsSnowSeaIceInit :: Settings of constants of snow and sea ice
  !
  !== NAMELIST
  !
  ! NAMELIST#constants_snowseaice_nml
  !

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

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

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

  ! 蜈ｬ髢区焔邯壹″
  ! Public procedure
  !
  public:: ConstantsSnowSeaIceInit

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

  real(DP), public, save:: ThresholdSurfSnow
  real(DP), public, save:: SnowAlbedo
  real(DP), public, save:: SnowThermCondCoef
                              ! Thermal conductivity of snow (W m-1 K-1)
  real(DP), public, save:: SnowVolHeatCap
                              ! Volumetric heat capacity of snow (J m-3 K-1)
  real(DP), public, save:: SnowDens
                              ! Density of snow (kg m-3)
  real(DP), public, save:: SnowMaxThermDepth
                              ! Maximum thermal depth of snow (m)
  real(DP), public, save:: TempCondWater
                        ! <Japanese>
                        ! Condensation temperature of water

  real(DP), public, save:: SeaIceVolHeatCap
                              ! Volumetric heat capacity of sea ice (J m-3 K-1)
  real(DP), public, save:: SeaIceThermCondCoef
                              ! Thermal conductivity of sea ice (W m-1 K-1)

  real(DP), public, save:: SeaIceThreshold
  real(DP), public, save:: SeaIceThickness
  real(DP), public, save:: TempBelowSeaIce

  real(DP), public, save:: SeaIceAlbedo

  real(DP), public, save:: CO2IceThreshold
  real(DP), public, save:: CO2IceAlbedoS
  real(DP), public, save:: CO2IceAlbedoN
  real(DP), public, save:: CO2IceEmisS
  real(DP), public, save:: CO2IceEmisN

  ! 髱槫�髢句､画焚
  ! Private variables
  !

  character(*), parameter:: module_name = 'constants_snowseaice'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: constants_snowseaice.f90,v 1.5 2015/01/29 12:07:29 yot Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

contains

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

  subroutine ConstantsSnowSeaIceInit
    !
    ! constants_snowseaice 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蛻晄悄蛹悶ｒ陦後＞縺ｾ縺�.
    ! NAMELIST#constants_snowseaice_nml 縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ縺ｯ縺薙�謇狗ｶ壹″縺ｧ陦後ｏ繧後∪縺�.
    !
    ! "constants_snowseaice" module is initialized.
    ! NAMELIST#constants_snowseaice_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

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

    ! 螳｣險譁� ; 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 /constants_snowseaice_nml/ &
      & ThresholdSurfSnow  , &
      & SnowAlbedo         , &
      & SnowThermCondCoef  , &
      & SnowVolHeatCap     , &
      & SnowDens           , &
      & SnowMaxThermDepth  , &
      & TempCondWater      , &
      & SeaIceVolHeatCap   , &
      & SeaIceThermCondCoef, &
      & SeaIceThreshold    , &
      & SeaIceThickness    , &
      & TempBelowSeaIce    , &
      & SeaIceAlbedo       , &
      & CO2IceAlbedoS      , &
      & CO2IceAlbedoN      , &
      & CO2IceEmisS        , &
      & CO2IceEmisN

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

    ! 螳溯｡梧枚 ; Executable statement
    !

    if ( constants_snowseaice_inited ) return

    ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｮ險ｭ螳�
    ! Default values settings
    !

    ThresholdSurfSnow  = 0.1_DP * 1.0e3_DP * 0.01e0_DP
                        ! 1 cm snow depth
                        ! 0.1 * 1000 (kg m-3; water density) x 1 cm
                        ! This value is selected arbitrarily.
!!$    SnowAlbedo         = 0.7d0
    SnowAlbedo         = 0.75_DP
                        ! This value is selected arbitrarily.

    ! MEMO for snow properties by Hillet
    !* snow (f=0.95) : k = 0.15e-3, C = 0.05
    !* snow (f=0.8 ) : k = 0.32e-3, C = 0.2
    !* snow (f=0.5 ) : k = 1.7e-3 , C = 0.5
    !f: porosity
    !k [cal/(cm s K)] conductivity
    !C [cal/(cm s K)] heat capacity

    SnowThermCondCoef = 0.32e-3_DP * 4.219e2_DP
                              ! Thermal conductivity of snow (W m-1 K-1)
!!$    SnowVolHeatCap     = 0.2_DP * 4.219e2_DP
    SnowVolHeatCap    = 2.0e6_DP * 200.0_DP / 1000.0_DP
                        ! This value is roughly based on ECMWF IFS value.
                        ! p.121, IFSPart4.pdf
                              ! Volumetric heat capacity of snow (J m-3 K-1)

    SnowDens          = 200.0_DP
                        ! This value is selected arbitrarily.
                              ! Density of snow (kg m-3)

    SnowMaxThermDepth = 1.0_DP
                        ! This value is based on ECMWF IFS value.
                        ! p.121, IFSPart4.pdf
                              ! Maximum thermal depth of snow (m)

    TempCondWater      = 273.15_DP
                        ! <Japanese>
                        ! Condensation temperature of water

!!$    ! https://www.myroms.org/wiki/index.php/Sea-Ice_Model
!!$    SeaIceDens         = 900.0d0   ! no reliable reference
!!$    SeaIceSpecHeat     = 2093.0d0  ! no reliable reference
!!$    SeaIceHeatDiffCoef = 2.04d0 / ( SeaIceDens * SeaIceSpecHeat )  ! 2.04d0 (W (m K)-1),  no reliable reference

!!$    SeaIceDens         = 0.92d3
!!$    ! kg m-3
!!$    SeaIceHeatDiffCoef = 2.04d0 / ( SeaIceDens * SeaIceSpecHeat )
!!$    ! 2.04d0 (W (m K)-1),  no reliable reference
!!$    SeaIceSpecHeat     = 1.9d6 / SeaIceDens
!!$    ! no reliable reference

    SeaIceVolHeatCap    = 1.9e6_DP
    ! J m-3 K-1
    ! Value of ice in Table 12.1 by Hillel (2004).
    SeaIceThermCondCoef = 2.2_DP
    ! W m-1 K-1
    ! Value of ice in Table 12.2 by Hillel (2004).

    ! Reference
    !
    ! Hillet, D.,
    !   Introduction to Environmental Soil Physics,
    !   Elsevier Academic Press, pp494, 2004.

    SeaIceThreshold    = 0.5_DP            ! arbitrarily set, no reliable reference
    SeaIceThickness    = 2.0_DP            ! arbitrarily set, no reliable reference
    TempBelowSeaIce    = 273.15_DP - 2.0_DP ! arbitrarily set, no reliable reference

!!$    SeaIceAlbedo       = 0.5_DP            ! arbitrarily set, no reliable reference
    SeaIceAlbedo       = 0.75_DP           ! arbitrarily set, no reliable reference

    CO2IceThreshold = 1.0_DP         ! No reference
    CO2IceAlbedoS   = 0.75_DP        ! No reference
    CO2IceAlbedoN   = 0.75_DP        ! No reference
    CO2IceEmisS     = 1.0_DP         ! No reference
    CO2IceEmisN     = 1.0_DP         ! No reference

    ! NAMELIST 縺九ｉ縺ｮ蜈･蜉�
    ! Input from NAMELIST
    !
    if ( trim(namelist_filename) /= '' ) then
      call FileOpen( unit_nml, &          ! (out)
        & namelist_filename, mode = 'r' ) ! (in)

      rewind( unit_nml )
      read( unit_nml,                     &  ! (in)
        & nml = constants_snowseaice_nml, &  ! (out)
        & iostat = iostat_nml            )   ! (out)
      close( unit_nml )

      call NmlutilMsg( iostat_nml, module_name ) ! (in)
      if ( iostat_nml == 0 ) write( STDOUT, nml = constants_snowseaice_nml )
    end if

    ! 蜊ｰ蟄� ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, '  ThresholdSurfSnow   = %f', d = (/ ThresholdSurfSnow   /) )
    call MessageNotify( 'M', module_name, '  SnowAlbedo          = %f', d = (/ SnowAlbedo          /) )
    call MessageNotify( 'M', module_name, '  SnowThermCondCoef   = %f', d = (/ SnowThermCondCoef   /) )
    call MessageNotify( 'M', module_name, '  SnowVolHeatCap      = %f', d = (/ SnowVolHeatCap      /) )
    call MessageNotify( 'M', module_name, '  SnowDens            = %f', d = (/ SnowDens            /) )
    call MessageNotify( 'M', module_name, '  SnowMaxThermDepth   = %f', d = (/ SnowMaxThermDepth   /) )
    call MessageNotify( 'M', module_name, '  TempCondWater       = %f', d = (/ TempCondWater       /) )
    call MessageNotify( 'M', module_name, '  SeaIceThreshold     = %f', d = (/ SeaIceThreshold     /) )
    call MessageNotify( 'M', module_name, '  SeaIceVolHeatCap    = %f', d = (/ SeaIceVolHeatCap    /) )
    call MessageNotify( 'M', module_name, '  SeaIceThermCondCoef = %f', d = (/ SeaIceThermCondCoef /) )

!!$    call MessageNotify( 'M', module_name, '  SeaIceDens         = %f', d = (/ SeaIceDens         /) )
!!$    call MessageNotify( 'M', module_name, '  SeaIceSpecHeat     = %f', d = (/ SeaIceSpecHeat     /) )
!!$    call MessageNotify( 'M', module_name, '  SeaIceHeatDiffCoef = %f', d = (/ SeaIceHeatDiffCoef /) )

    call MessageNotify( 'M', module_name, '  SeaIceThickness     = %f', d = (/ SeaIceThickness     /) )
    call MessageNotify( 'M', module_name, '  TempBelowSeaIce     = %f', d = (/ TempBelowSeaIce     /) )
    call MessageNotify( 'M', module_name, '  SeaIceAlbedo        = %f', d = (/ SeaIceAlbedo        /) )
    call MessageNotify( 'M', module_name, '  CO2IceAlbedoS       = %f', d = (/ CO2IceAlbedoS       /) )
    call MessageNotify( 'M', module_name, '  CO2IceAlbedoN       = %f', d = (/ CO2IceAlbedoN       /) )
    call MessageNotify( 'M', module_name, '  CO2IceEmisS         = %f', d = (/ CO2IceEmisS       /) )
    call MessageNotify( 'M', module_name, '  CO2IceEmisN         = %f', d = (/ CO2IceEmisN       /) )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    constants_snowseaice_inited = .true.

  end subroutine ConstantsSnowSeaIceInit

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

end module constants_snowseaice