!= 邁｡蜊俶叛蟆�Δ繝�Ν
!
!= Simple radiation model
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: rad_simple.f90,v 1.5 2013/05/25 06:47:34 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module rad_simple
  !
  != 邁｡蜊俶叛蟆�Δ繝�Ν
  !
  != Simple radiation model
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  !
  !
  ! This is a simple radiation model.
  !
  !== References
  !
  !
  !
  !== 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#rad_Simple_nml
  !

  ! USE statements
  !

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

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

  implicit none

  private

  ! Private variables
  !
!!$  real(DP), save :: SWOptDepAtRefPress
!!$  real(DP), save :: SWRefPress
!!$  real(DP), save :: SWOrd
!!$  real(DP), save :: LWOptDepAtRefPress
!!$  real(DP), save :: LWRefPress
!!$  real(DP), save :: LWOrd

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

  public :: RadSimpleFlux
  public :: RadSimpleInit

  character(*), parameter:: module_name = 'rad_simple'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: rad_simple.f90,v 1.5 2013/05/25 06:47:34 yot Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

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

contains

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

  subroutine RadSimpleFlux(                                                    &
    & xy_SurfAlbedo, xy_SurfTemp, xyr_Press, xyz_Press, xyz_Temp, xyz_QH2OVap, &
    & xyr_RadSUwFlux, xyr_RadSDwFlux,                                          &
    & xyr_RadLUwFlux, xyr_RadLDwFlux,                                          &
    & xyra_DelRadLUwFlux, xyra_DelRadLDwFlux                                   &
    & )

    ! USE statements
    !

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

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

    !
    ! Physical constants settings
    !
    use constants, only: Grav     ! $ g $ [m s-2].
                                  !
                                  ! Gravitational acceleration

    ! 蠎ｧ讓吶ョ繝ｼ繧ｿ險ｭ螳�
    ! Axes data settings
    !
    use axesset, only : y_Lat

    ! 邁｡蜊倡洒豕｢謾ｾ蟆�Δ繝�Ν Ver. 2.0
    ! simple short wave radiation model Ver. 2.0
    !
    use rad_simple_SW_V2_0, only : RadSimpleSWV20Flux

    ! 髟ｷ豕｢繝輔Λ繝�け繧ｹ縺ｮ險育ｮ�
    ! Calculate long wave flux
    !
    use rad_simple_LW, only : RadSimpleLWFlux

    real(DP), intent(in ) :: xy_SurfAlbedo( 0:imax-1, 1:jmax )
    real(DP), intent(in ) :: xy_SurfTemp  ( 0:imax-1, 1:jmax )
    real(DP), intent(in ) :: xyr_Press    ( 0:imax-1, 1:jmax, 0:kmax )
    real(DP), intent(in ) :: xyz_Press    ( 0:imax-1, 1:jmax, 1:kmax )
    real(DP), intent(in ) :: xyz_Temp     ( 0:imax-1, 1:jmax, 1:kmax )
    real(DP), intent(in ) :: xyz_QH2OVap  ( 0:imax-1, 1:jmax, 1:kmax )
    real(DP), intent(out) :: xyr_RadSUwFlux    ( 0:imax-1, 1:jmax, 0:kmax )
    real(DP), intent(out) :: xyr_RadSDwFlux    ( 0:imax-1, 1:jmax, 0:kmax )
    real(DP), intent(out) :: xyr_RadLUwFlux    (0:imax-1, 1:jmax, 0:kmax)
    real(DP), intent(out) :: xyr_RadLDwFlux    (0:imax-1, 1:jmax, 0:kmax)
    real(DP), intent(out) :: xyra_DelRadLUwFlux(0:imax-1, 1:jmax, 0:kmax, 0:1)
    real(DP), intent(out) :: xyra_DelRadLDwFlux(0:imax-1, 1:jmax, 0:kmax, 0:1)

    ! Work variables
    !
    real(DP) :: DistFromStarScld
                               ! Distance between the central star and the planet
    real(DP) :: DiurnalMeanFactor

    real(DP) :: SolarConst
    real(DP) :: SolarFluxTOA
!!$    real(DP) :: QeRatio
    real(DP) :: xyz_SSA       (0:imax-1, 1:jmax, 1:kmax)
    real(DP) :: xyz_AF        (0:imax-1, 1:jmax, 1:kmax)
    real(DP) :: xy_InAngle    (0:imax-1, 1:jmax)
    real(DP) :: xyz_DelAtmMass(0:imax-1, 1:jmax, 1:kmax)
    real(DP) :: xyr_OptDep    (0:imax-1, 1:jmax, 0:kmax)

    real(DP) :: xy_SurfAlbedoLW(0:imax-1, 1:jmax)
    real(DP) :: xy_SurfEmis    (0:imax-1, 1:jmax)

    integer  :: k

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

    do k = 1, kmax
      xyz_DelAtmMass(:,:,k) = ( xyr_Press(:,:,k-1) - xyr_Press(:,:,k ) ) / Grav
    end do

    ! Short wave radiation
    !

    ! 邁｡蜊倡洒豕｢謾ｾ蟆�Δ繝�Ν Ver. 2.0
    ! simple short wave radiation model Ver. 2.0
    !
    call RadSimpleSWV20Flux(                                     &
      & xy_SurfAlbedo,                                           &
      & xyz_DelAtmMass,                                          &
      & xyr_RadSUwFlux, xyr_RadSDwFlux                           &
      & )

    ! Long wave radiation
    !

    xy_SurfAlbedoLW = 0.0_DP
    xy_SurfEmis     = 1.0_DP

    call RadSimpleLWFlux(                                         &
      & xy_SurfAlbedoLW, xy_SurfEmis,                             & ! (in)
      & xyr_Press, xyz_Press, xyz_Temp, xy_SurfTemp,              & ! (in)
      & xyz_DelAtmMass, xyz_QH2OVap,                              & ! (in)
      & xyr_RadLUwFlux, xyr_RadLDwFlux,                           & ! (out)
      & xyra_DelRadLUwFlux, xyra_DelRadLDwFlux                    & ! (out)
      & )

  end subroutine RadSimpleFlux

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

  subroutine RadSimpleInit

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

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

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

    ! 邁｡蜊倡洒豕｢謾ｾ蟆�Δ繝�Ν Ver. 2.0
    ! simple short wave radiation model Ver. 2.0
    !
    use rad_simple_SW_V2_0, only : RadSimpleSWV20Init

    ! 謾ｾ蟆�ヵ繝ｩ繝�け繧ｹ (邁｡蜊倬聞豕｢繝舌Φ繝峨Δ繝�Ν)
    ! Radiation flux (simple longwave band model)
    !
    use rad_simple_LW, only : RadSimpleLWInit

    ! 螳｣險譁� ; Declaration statements
    !

    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_Simple_nml/ &
!!$      & SWOptDepAtRefPress, SWRefPress, SWOrd, &
!!$      & LWOptDepAtRefPress, LWRefPress, LWOrd
          !
          ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｫ縺､縺�※縺ｯ蛻晄悄蛹匁焔邯� "rad_Simple#RadSimpleInit"
          ! 縺ｮ繧ｽ繝ｼ繧ｹ繧ｳ繝ｼ繝峨ｒ蜿ら�縺ｮ縺薙→.
          !
          ! Refer to source codes in the initialization procedure
          ! "rad_Simple#RadSimpleInit" for the default values.
          !

    if ( rad_Simple_inited ) return

    ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｮ險ｭ螳�
    ! Default values settings
    !
!!$    SWOptDepAtRefPress =  3.0_DP
!!$    SWRefPress         =  3.0d5
!!$    SWOrd              =  1.0_DP
!!$
!!$    LWOptDepAtRefPress = 80.0_DP
!!$    LWRefPress         =  3.0d5
!!$    LWOrd              =  2.0_DP

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

    ! Initialization of modules used in this module
    !

    ! 邁｡蜊倡洒豕｢謾ｾ蟆�Δ繝�Ν Ver. 2.0
    ! simple short wave radiation model Ver. 2.0
    !
    call RadSimpleSWV20Init

    ! 謾ｾ蟆�ヵ繝ｩ繝�け繧ｹ (邁｡蜊倬聞豕｢繝舌Φ繝峨Δ繝�Ν)
    ! Radiation flux (simple longwave band model)
    !
    call RadSimpleLWInit

    ! 蜊ｰ蟄� ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
!!$    call MessageNotify( 'M', module_name, 'SWOptDepAtRefPress = %f', d = (/ SWOptDepAtRefPress /) )
!!$    call MessageNotify( 'M', module_name, 'SWRefPress         = %f', d = (/ SWRefPress /) )
!!$    call MessageNotify( 'M', module_name, 'SWOrd              = %f', d = (/ SWOrd /) )
!!$    call MessageNotify( 'M', module_name, 'LWOptDepAtRefPress = %f', d = (/ LWOptDepAtRefPress /) )
!!$    call MessageNotify( 'M', module_name, 'LWRefPress         = %f', d = (/ LWRefPress /) )
!!$    call MessageNotify( 'M', module_name, 'LWOrd              = %f', d = (/ LWOrd /) )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    rad_Simple_inited = .true.

  end subroutine RadSimpleInit

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

end module rad_simple