!=
!
!= Routines for GABLS tests
!
! Authors::   Yoshiyuki O. TAKAHASHI
! Version::   $Id: gabls.f90,v 1.1 2013/09/21 14:58:39 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module gabls
  !
  !=
  !
  != Routines for GABLS tests
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  !
  !
  !
  !
  !== Procedures List
  !
!!$  ! ShortIncoming      :: 遏ｭ豕｢蜈･蟆� (螟ｪ髯ｽ蜈･蟆�) 縺ｮ險育ｮ�
!!$  ! ------------       :: ------------
!!$  ! ShortIncoming      :: Calculate short wave (insolation) incoming radiation.
  !
  !== NAMELIST
  !
!!$  ! NAMELIST#rad_short_income_nml
  !

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

  ! 遞ｮ蛻･蝙九ヱ繝ｩ繝｡繧ｿ
  ! Kind type parameter
  !
  use dc_types, only: DP, &      ! 蛟咲ｲｾ蠎ｦ螳滓焚蝙�. Double precision.
    &                 STRING, &  ! 譁�ｭ怜�.       Strings.
    &                 TOKEN      ! 繧ｭ繝ｼ繝ｯ繝ｼ繝�.   Keywords.

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

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

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

  ! 譎ょ綾邂｡逅�
  ! Time control
  !
  use timeset, only: &
    & TimeN,                & !
    & InitialDate             ! 險育ｮ鈴幕蟋区律譎�.
                              ! Start date of calculation

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

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

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

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

!!$  logical,  save:: FlagAnnualMean
!!$                              ! 蟷ｴ蟷ｳ蝮��蟆�ヵ繝ｩ繧ｰ.
!!$                              ! Flag for annual mean incoming radiation.

  character(*), parameter:: module_name = 'gabls'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: gabls.f90,v 1.1 2013/09/21 14:58:39 yot Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

contains

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

  subroutine SetGabls2SurfTemp(     &
    & xy_SurfTemp                   & ! (out)
    & )
    !
    !
    !
    ! Set surface temperature
    !

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

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

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

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none
    real(DP), intent(out) :: xy_SurfTemp(0:imax-1, 1:jmax)
                              !
                              ! surface temperature

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    integer         :: hour_in_a_day
    integer         :: min_in_a_hour
    real(DP)        :: sec_in_a_min

    integer           :: Year
    integer           :: Month
    integer           :: Day
    integer           :: Hour
    integer           :: Min
    real(DP)          :: Sec
    type(DC_CAL_DATE) :: Date1999Oct22UT0000

    real(DP) :: Time1999Oct22UT0000

    real(DP) :: HourFrom1999Oct22UT0000

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹也｢ｺ隱�
    ! Initialization check
    !
    if ( .not. gabls_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

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

    Year  = 1999
    Month =   10
    Day   =   22
    Hour  =    0
    Min   =    0
    Sec   =    0.0_DP
    call DCCalDateCreate( &
      & year = Year, month = Month, day = Day, &
      & hour = Hour, min   = Min  , sec = Sec, &
      & date = Date1999Oct22UT0000             &
      & )

    Time1999Oct22UT0000 = &
      & DCCalDateDifference( Date1999Oct22UT0000, InitialDate )

    HourFrom1999Oct22UT0000 = TimeN + Time1999Oct22UT0000
    HourFrom1999Oct22UT0000 = &
      & HourFrom1999Oct22UT0000 / ( min_in_a_hour * sec_in_a_min )

    if ( HourFrom1999Oct22UT0000 <= 17.4_DP ) then
      xy_SurfTemp = &
        & - 10.0_DP - 25.0_DP * cos( 0.22 * HourFrom1999Oct22UT0000 + 0.2_DP )
    else if ( HourFrom1999Oct22UT0000 <= 30.0_DP ) then
      xy_SurfTemp = &
        & - 0.54_DP * HourFrom1999Oct22UT0000 + 15.2_DP
    else if ( HourFrom1999Oct22UT0000 <= 41.9_DP ) then
      xy_SurfTemp = &
        & -  7.0_DP - 25.0_DP * cos( 0.21 * HourFrom1999Oct22UT0000 + 1.8_DP )
    else if ( HourFrom1999Oct22UT0000 <= 53.3_DP ) then
      xy_SurfTemp = &
        & - 0.37_DP * HourFrom1999Oct22UT0000 + 18.0_DP
    else if ( HourFrom1999Oct22UT0000 <= 65.6_DP ) then
      xy_SurfTemp = &
        & -  4.0_DP - 25.0_DP * cos( 0.22 * HourFrom1999Oct22UT0000 + 2.5_DP )
    else
      xy_SurfTemp = &
        &    4.4_DP
    end if

    xy_SurfTemp = xy_SurfTemp + 273.15_DP

  end subroutine SetGabls2SurfTemp

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

  subroutine GablsInit
    !
    ! rad_short_income 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蛻晄悄蛹悶ｒ陦後＞縺ｾ縺�.
    ! NAMELIST#rad_short_income_nml 縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ縺ｯ縺薙�謇狗ｶ壹″縺ｧ陦後ｏ繧後∪縺�.
    !
    ! "rad_short_income" module is initialized.
    ! "NAMELIST#rad_short_income_nml" is loaded in this procedure.
    !

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

    ! 遞ｮ蛻･蝙九ヱ繝ｩ繝｡繧ｿ
    ! Kind type parameter
    !
    use dc_types, only: STDOUT ! 讓呎ｺ門�蜉帙�陬�ｽｮ逡ｪ蜿ｷ. Unit number of standard output

    ! 繝輔ぃ繧､繝ｫ蜈･蜃ｺ蜉幄｣懷勧
    ! File I/O support
    !
    use dc_iounit, only: FileOpen

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

    ! 證ｦ縺ｨ譌･譎ゅ�蜿悶ｊ謇ｱ縺�
    ! Calendar and Date handler
    !
    use dc_calendar, only: &
      & DC_CAL_DATE, &          ! 譌･譎ゅｒ陦ｨ迴ｾ縺吶ｋ繝��繧ｿ蝙�.
                                ! Data type for date and time
      & DCCalDateInquire, DCCalDateCreate, DCCalDateDifference, &
      & DCCalConvertByUnit

    ! NAMELIST 繝輔ぃ繧､繝ｫ蜈･蜉帙↓髢｢縺吶ｋ繝ｦ繝ｼ繝�ぅ繝ｪ繝�ぅ
    ! Utilities for NAMELIST file input
    !
    use namelist_util, only: namelist_filename, NmlutilMsg, NmlutilAryValid

    ! 螳｣險譁� ; 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 /gabls_nml/                                     &
!!$      & FlagRadSynchronous
          !
          ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｫ縺､縺�※縺ｯ蛻晄悄蛹匁焔邯� "rad_short_income#RadShortIncomeInit"
          ! 縺ｮ繧ｽ繝ｼ繧ｹ繧ｳ繝ｼ繝峨ｒ蜿ら�縺ｮ縺薙→.
          !
          ! Refer to source codes in the initialization procedure
          ! "rad_short_income#RadShortIncomeInit" for the default values.
          !

    ! 螳溯｡梧枚 ; Executable statement
    !

    if ( gabls_inited ) return

    ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｮ險ｭ螳�
    ! Default values settings
    !
!!$    FlagRadSynchronous       = .false.

    ! 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 = gabls_nml, &           ! (out)
!!$        & iostat = iostat_nml )        ! (out)
!!$      close( unit_nml )
!!$
!!$      call NmlutilMsg( iostat_nml, module_name ) ! (in)
!!$    end if

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

    gabls_inited = .true.

  end subroutine GablsInit

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

end module gabls