!= ç«æ˜Ÿè¨ˆç®—ç”¨è¿‘èµ¤å¤–åŠ ç†±çŽ‡è¨ˆç®—
!
!= 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