!= 螟ｪ髯ｽ謾ｾ蟆�ヵ繝ｩ繝�け繧ｹ縺ｮ險ｭ螳�
!
!= 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