!= å¤ªé™½æ”¾å°„ãƒ•ãƒ©ãƒƒã‚¯ã‚¹ã®è¨å®š
!
!= Set solar constant
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: set_solarconst.f90,v 1.2 2013/03/01 14:14:16 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module set_solarconst
  !
  != å¤ªé™½æ”¾å°„ãƒ•ãƒ©ãƒƒã‚¯ã‚¹ã®è¨å®š
  !
  != Set solar constant
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  !
  !
  !
  !
  !== References
  !
!!$  !  Roewe, D., and K.-N. Liou, Influence of cirrus clouds on the infrared cooling
!!$  !    rate in the troposphere and lower stratosphere, J. Appl. Met., 17, 92-106, 1978.
  !
  !== Procedures List
  !
!!$  ! RadiationFluxDennouAGCM :: æ”¾å°„ãƒ•ãƒ©ãƒƒã‚¯ã‚¹ã®è¨ˆç®—
!!$  ! RadiationDTempDt        :: æ”¾å°„ãƒ•ãƒ©ãƒƒã‚¯ã‚¹ã«ã‚ˆã‚‹æ¸©åº¦å¤‰åŒ–ã®è¨ˆç®—
!!$  ! RadiationFluxOutput     :: æ”¾å°„ãƒ•ãƒ©ãƒƒã‚¯ã‚¹ã®å‡ºåŠ›
!!$  ! RadiationFinalize       :: çµ‚äº†å‡¦ç† (ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«å†…éƒ¨ã®å¤‰æ•°ã®å‰²ã‚Šä»˜ã‘è§£é™¤)
!!$  ! ------------            :: ------------
!!$  ! RadiationFluxDennouAGCM :: Calculate radiation flux
!!$  ! RadiationDTempDt        :: Calculate temperature tendency with radiation flux
!!$  ! RadiationFluxOutput     :: Output radiation fluxes
!!$  ! RadiationFinalize       :: Termination (deallocate variables in this module)
  !
  !== NAMELIST
  !
  ! NAMELIST#set_solarconst_nml
  !

  ! USE statements
  !

  !
  ! Kind type parameter
  !
  use dc_types, only: DP, &      ! Double precision.
    &                 STRING, &  ! Strings.
    &                 TOKEN      ! Keywords.

  ! ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸å‡ºåŠ›
  ! Message output
  !
  use dc_message, only: MessageNotify

  ! Declaration statements
  !
  implicit none
  private

  !
  ! Public procedure
  !
  public:: SetSolarConst
  public:: SetSolarConstInit

  character(*), parameter:: module_name = 'set_solarconst'
                              ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®åç§°.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: set_solarconst.f90,v 1.2 2013/03/01 14:14:16 yot Exp $'
                              ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®ãƒãƒ¼ã‚¸ãƒ§ãƒ³
                              ! Module version

  real(DP), save         :: SolarConst

  character(STRING), save:: SolarConstFile
  character(STRING), save:: SolarConstVarName

  logical, save :: set_solarconst_inited = .false.
                              ! åˆæœŸè¨å®šãƒ•ãƒ©ã‚°.
                              ! Initialization flag

contains

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

  subroutine SetSolarConst( &
    & SolarFlux             & ! (out)
    & )

    ! USE statements
    !

    ! ãƒ’ã‚¹ãƒˆãƒªãƒ‡ãƒ¼ã‚¿å‡ºåŠ›
    ! History data output
    !
    use gtool_historyauto, only: HistoryAutoAddVariable, HistoryAutoPut

    ! æ™‚åˆ»ç®¡ç†
    ! Time control
    !
    use timeset, only: &
      & TimeN, &              ! ã‚¹ãƒ†ãƒƒãƒ— $ t $ ã®æ™‚åˆ».
                              ! Time of step $ t $.
      & TimesetClockStart, TimesetClockStop

    ! æ™‚ç³»åˆ—ãƒ‡ãƒ¼ã‚¿ã®èªã¿è¾¼ã¿
    ! Reading time series
    !
    use read_time_series, only: SetValuesFromTimeSeriesWrapper

    real(DP), intent(out):: SolarFlux

    !
    ! Work variables
    !

    ! åˆæœŸåŒ–ç¢ºèª
    ! Initialization check
    !
    if ( .not. set_solarconst_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    ! è¨ˆç®—æ™‚é–“è¨ˆæ¸¬é–‹å§‹
    ! Start measurement of computation time
    !
    call TimesetClockStart( module_name )

    if ( SolarConstFile == '' ) then

      SolarFlux = SolarConst

    else

      call SetValuesFromTimeSeriesWrapper(   &
        & "SolarConst",                      &
        & SolarConstFile, SolarConstVarName, &
        & SolarFlux                          & ! (inout)
        & )

    end if

    call HistoryAutoPut( TimeN, "SolarConst", SolarFlux )

    ! è¨ˆç®—æ™‚é–“è¨ˆæ¸¬ä¸€æ™‚åœæ¢
    ! Pause measurement of computation time
    !
    call TimesetClockStop( module_name )

  end subroutine SetSolarConst

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

  subroutine SetSolarConstInit

    ! ãƒ•ã‚¡ã‚¤ãƒ«å…¥å‡ºåŠ›è£œåŠ©
    ! File I/O support
    !
    use dc_iounit, only: FileOpen

    ! ãƒ’ã‚¹ãƒˆãƒªãƒ‡ãƒ¼ã‚¿å‡ºåŠ›
    ! History data output
    !
    use gtool_historyauto, only: HistoryAutoAddVariable

    ! NAMELIST ãƒ•ã‚¡ã‚¤ãƒ«å…¥åŠ›ã«é–¢ã™ã‚‹ãƒ¦ãƒ¼ãƒ†ã‚£ãƒªãƒ†ã‚£
    ! Utilities for NAMELIST file input
    !
    use namelist_util, only: namelist_filename, NmlutilMsg, NmlutilAryValid

    !
    ! Work variables
    !
    integer:: unit_nml        ! NAMELIST ãƒ•ã‚¡ã‚¤ãƒ«ã‚ªãƒ¼ãƒ—ãƒ³ç”¨è£…ç½®ç•ªå·.
                              ! Unit number for NAMELIST file open
    integer:: iostat_nml      ! NAMELIST èªã¿è¾¼ã¿æ™‚ã® IOSTAT.
                              ! IOSTAT of NAMELIST read

    ! NAMELIST å¤‰æ•°ç¾¤
    ! NAMELIST group name
    !
    namelist /set_solarconst_nml/ &
      & SolarConst,     &
      & SolarConstFile, &
      & SolarConstVarName
          !
          ! ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆå€¤ã«ã¤ã„ã¦ã¯åˆæœŸåŒ–æ‰‹ç¶š "set_solarconst#SetSolarConstInit"
          ! ã®ã‚½ãƒ¼ã‚¹ã‚³ãƒ¼ãƒ‰ã‚’å‚ç…§ã®ã“ã¨.
          !
          ! Refer to source codes in the initialization procedure
          ! "set_solarconst#SetSolarConstInit" for the default values.
          !

    if ( set_solarconst_inited ) return

    ! ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆå€¤ã®è¨å®š
    ! Default values settings
    !

!!$    SolarConst     = 1380.0_DP
    SolarConst        = 1366.0_DP

    SolarConstFile    = ''
    SolarConstVarName = "SolarConst"

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

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

    call HistoryAutoAddVariable( "SolarConst", & ! (in)
      & (/ 'time' /),                          & ! (in)
      & "solar constant", 'W m-2' )              ! (in)

    ! å°å— ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, '  SolarConst        = %f', d = (/ SolarConst /) )
    call MessageNotify( 'M', module_name, '  SolarConstFile    = %c', c1 = trim( SolarConstFile ) )
    call MessageNotify( 'M', module_name, '  SolarConstVarName = %c', c1 = trim( SolarConstVarName ) )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    set_solarconst_inited = .true.

  end subroutine SetSolarConstInit

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

end module set_solarconst