!= 轣ｫ譏溯ｨ育ｮ礼畑霑題ｵ､螟門刈辭ｱ邇�ｨ育ｮ�
!
!= Calculation of near infrared heating rate in the case of Mars
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: rad_Mars_NIR.f90,v 1.4 2012/10/08 12:48:44 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module rad_Mars_NIR
  !
  != 轣ｫ譏溯ｨ育ｮ礼畑霑題ｵ､螟門刈辭ｱ邇�ｨ育ｮ�
  !
  != Calculation of near infrared heating rate in the case of Mars
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  !== Procedures List
  !
!!$  ! DryConvAdjust :: 荵ｾ辯･蟇ｾ豬∬ｪｿ遽
!!$  ! ------------  :: ------------
!!$  ! DryConvAdjust :: Dry convective adjustment
  !
  !== NAMELIST
  !
  ! NAMELIST#rad_M_NIR_nml
  !

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

  ! 譬ｼ蟄千せ險ｭ螳�
  ! 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

  ! 遞ｮ蛻･蝙九ヱ繝ｩ繝｡繧ｿ
  ! 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

  ! 蜈ｬ髢区焔邯壹″
  ! Public procedure
  !
  public :: RadMarsNIRInit
  public :: RadMarsNIR
  public :: RadMarsNIRINOUT

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

  ! 髱槫�髢句､画焚
  ! Private variables
  !
  logical, save:: FlagUse
                              ! 菴ｿ逕ｨ繝輔Λ繧ｰ
                              ! flag for use of this scheme

  character(*), parameter:: module_name = 'rad_Mars_NIR'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: rad_Mars_NIR.f90,v 1.4 2012/10/08 12:48:44 yot Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

contains

  subroutine RadMarsNIR(         &
    & xyz_Press,                 &  ! (in)
    & xyz_DTempDt                &  ! (out)
    & )
    !
    ! 轣ｫ譏溯ｨ育ｮ礼畑霑題ｵ､螟門刈辭ｱ邇�ｨ育ｮ�
    !
    ! Calculation of near infrared heating rate in the case of Mars
    !
    ! Calculation schume of solar nir infrared heating rate follows
    ! that shown in Forget et al., 1999

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

    ! 譎ょ綾邂｡逅�
    ! Time control
    !
    use timeset, only: &
      & DelTime, &            ! $ \Delta t $
      & TimeN, &              ! 繧ｹ繝�ャ繝� $ t $ 縺ｮ譎ょ綾. Time of step $ t $.
      & TimesetClockStart, TimesetClockStop

    ! 繝偵せ繝医Μ繝��繧ｿ蜃ｺ蜉�
    ! History data output
    !
    use gtool_historyauto, only: HistoryAutoPut

    ! 譌･莉倥♀繧医�譎ょ綾縺ｮ蜿悶ｊ謇ｱ縺�
    ! Date and time handler
    !
    use dc_calendar, only: DCCalInquire

    ! 迚ｩ逅��謨ｰ蟄ｦ螳壽焚險ｭ螳�
    ! Physical and mathematical constants settings
    !
    use constants0, only: &
      & PI                    ! $ \pi $ .
                              ! 蜀�捉邇�.  Circular constant

    ! 遏ｭ豕｢蜈･蟆� (螟ｪ髯ｽ蜈･蟆�)
    ! Short wave (insolation) incoming
    !
    use rad_short_income, only : RadShortIncome

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

    real(DP), intent(in ):: xyz_Press  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ p $ . 豌怜悸 (謨ｴ謨ｰ繝ｬ繝吶Ν).
                              ! Air pressure (full level)
    real(DP), intent(out):: xyz_DTempDt(0:imax-1, 1:jmax, 1:kmax)
                              ! 貂ｩ蠎ｦ螟牙喧邇�.
                              ! Temperature tendency

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    real(DP) :: qnir0
    real(DP) :: RefPress
    real(DP) :: NLTEPress

    integer  :: hour_in_day
    integer  :: min_in_hour
    real(DP) :: sec_in_min

    real(DP) :: DistFromStarScld
    real(DP) :: DiurnalMeanFactor
    real(DP) :: xy_CosZet  (0:imax-1, 1:jmax)
    real(DP) :: xy_CosZetSq(0:imax-1, 1:jmax)

    real(DP) :: MuFac

    integer:: i               ! 邨悟ｺｦ譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in longitude
    integer:: j               ! 邱ｯ蠎ｦ譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in latitude
    integer:: k               ! 驩帷峩譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in vertical direction

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 險育ｮ玲凾髢楢ｨ域ｸｬ髢句ｧ�
    ! Start measurement of computation time
    !
    call TimesetClockStart( module_name )

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

    call DCCalInquire( &
      & hour_in_day      = hour_in_day  , &     ! (out)
      & min_in_hour      = min_in_hour  , &     ! (out)
      & sec_in_min       = sec_in_min     &     ! (out)
      & )

    ! constants
    !
    qnir0     = 1.3_DP / ( hour_in_day * min_in_hour * sec_in_min )
    RefPress  = 700.0_DP
    NLTEPress = 0.0075_DP

    call RadShortIncome( &
      & DistFromStarScld   = DistFromStarScld,   & ! (out) optional
      & xy_CosZet          = xy_CosZet,          & ! (out) optional
      & DiurnalMeanFactor  = DiurnalMeanFactor,  & ! (out) optional
      & FlagOutput         = .false.             & ! (in)  optional
      & )

    xy_CosZetSq = xy_CosZet**2

    do j = 1, jmax
      do i = 0, imax-1

        if( xy_CosZet(i,j) < 0.0_DP ) then
          do k = 1, kmax
            xyz_DTempDt(i,j,k) = 0.0_DP
          end do
        else
          MuFac = sqrt( ( 1224.0_DP * xy_CosZetSq(i,j) + 1.0d0 ) / 1225.0_DP )
          do k = 1, kmax
            xyz_DTempDt(i,j,k) = &
              & qnir0 / DistFromStarScld**2 &
              & * sqrt( RefPress / xyz_Press(i,j,k) * MuFac ) &
              & / ( 1.0d0 + NLTEPress / xyz_Press(i,j,k) )
          end do
        end if

      end do
    end do

    xyz_DTempDt = xyz_DTempDt * DiurnalMeanFactor

    ! 繝偵せ繝医Μ繝��繧ｿ蜃ｺ蜉�
    ! History data output
    !
    call HistoryAutoPut( TimeN, 'DTempDtMNIR', xyz_DTempDt )

    ! 險育ｮ玲凾髢楢ｨ域ｸｬ荳譎ょ●豁｢
    ! Pause measurement of computation time
    !
    call TimesetClockStop( module_name )

  end subroutine RadMarsNIR

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

  subroutine RadMarsNIRINOUT(  &
    & xyz_Press,                 &  ! (in   )
    & xyz_DTempDt                &  ! (inout)
    & )
    !
    ! 轣ｫ譏溯ｨ育ｮ礼畑霑題ｵ､螟門刈辭ｱ邇�ｨ育ｮ�
    !
    ! Calculation of near infrared heating rate in the case of Mars
    !
    ! Calculation schume of solar nir infrared heating rate follows
    ! that shown in Forget et al., 1999

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

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

    real(DP), intent(in   ):: xyz_Press (0:imax-1, 1:jmax, 1:kmax)
                              ! $ p $ . 豌怜悸 (謨ｴ謨ｰ繝ｬ繝吶Ν).
                              ! Air pressure (full level)
    real(DP), intent(inout):: xyz_DTempDt(0:imax-1, 1:jmax, 1:kmax)
                              ! 貂ｩ蠎ｦ螟牙喧邇�.
                              ! Temperature tendency

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    real(DP) :: xyz_DTempDtLocal(0:imax-1, 1:jmax, 1:kmax)

    ! 螳溯｡梧枚 ; Executable statement
    !

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

    call RadMarsNIR(         &
      & xyz_Press,           &  ! (in )
      & xyz_DTempDtLocal     &  ! (out)
      & )

    xyz_DTempDt = xyz_DTempDt + xyz_DTempDtLocal

  end subroutine RadMarsNIRINOUT

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

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

    ! 繝偵せ繝医Μ繝��繧ｿ蜃ｺ蜉�
    ! History data output
    !
    use gtool_historyauto, only: HistoryAutoAddVariable

    ! 蠎ｧ讓吶ョ繝ｼ繧ｿ險ｭ螳�
    ! Axes data settings
    !
    use axesset, only: &
      & AxnameX, &
      & AxnameY, &
      & AxnameZ, &
      & AxnameT

    ! 遏ｭ豕｢蜈･蟆� (螟ｪ髯ｽ蜈･蟆�)
    ! Short wave (insolation) incoming
    !
    use rad_short_income, only : RadShortIncomeInit

    ! 螳｣險譁� ; 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 /rad_Mars_NIR_nml/ &
      & FlagUse

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

    ! 螳溯｡梧枚 ; Executable statement
    !

    if ( rad_Mars_NIR_inited ) return

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

    ! 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 = rad_Mars_NIR_nml,    &  ! (out)
        & iostat = iostat_nml )         ! (out)
      close( unit_nml )

      call NmlutilMsg( iostat_nml, module_name ) ! (in)
    end if

    ! 繝偵せ繝医Μ繝��繧ｿ蜃ｺ蜉帙�縺溘ａ縺ｮ縺ｸ縺ｮ螟画焚逋ｻ骭ｲ
    ! Register of variables for history data output
    !
    call HistoryAutoAddVariable( 'DTempDtMNIR',    &
      & (/ AxnameX, AxnameY, AxnameZ, AxnameT /),  &
      & 'near infrared heating rate', 'K s-1' )

    ! Initialization of modules used in this module
    !

    ! 遏ｭ豕｢蜈･蟆� (螟ｪ髯ｽ蜈･蟆�)
    ! Short wave (insolation) incoming
    !
    call RadShortIncomeInit

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

    rad_Mars_NIR_inited = .true.

  end subroutine RadMarsNIRInit

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

end module rad_Mars_NIR