!= 邁｡蜊倡洒豕｢謾ｾ蟆�Δ繝�Ν Ver. 2.0
!
!= simple short wave radiation model Ver. 2.0
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: rad_Earth_SW_V2_6.f90,v 1.1 2015/01/29 12:16:19 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!
module rad_simple_SW_V2_0

  !
  != 邁｡蜊倡洒豕｢謾ｾ蟆�Δ繝�Ν Ver. 2.0
  !
  != simple short wave radiation model Ver. 2.0
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! 蝨ｰ逅�､ｧ豌怜髄縺醍洒豕｢謾ｾ蟆�Δ繝�Ν.
  !
  ! This is a simple short wave radiation model.
  !
  ! This module is created by simplifying rad_Earth_SW_V2_6.f90.
  !
  !== References
  !
  !
  !== Procedures List
  !
  ! RadSimpleSWV20Flux  :: 謾ｾ蟆�ヵ繝ｩ繝�け繧ｹ縺ｮ險育ｮ�
  ! ------------        :: ------------
  ! RadSimpleSWV20Flux  :: Calculate radiation flux
  !
  !== NAMELIST
  !
  ! NAMELIST#rad_simple_SW_V2_0_nml
  !

  ! USE statements
  !

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

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

  ! 譬ｼ蟄千せ險ｭ螳�
  ! 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: &
    & TimesetClockStart, TimesetClockStop

  implicit none

  private

  logical , save      :: FlagRayleighScattering

  real(DP), save      :: RayleighScatCoefRatio
                             ! Ratio of Rayleight scattering coefficient
                             ! to that of Earth's atmosphere

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

  public :: RadSimpleSWV20Flux
  public :: RadSimpleSWV20Init

  character(*), parameter:: module_name = 'rad_simple_SW_V2_0'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: rad_Earth_SW_V2_6.f90,v 1.1 2015/01/29 12:16:19 yot Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

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

contains

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

  subroutine RadSimpleSWV20Flux(                               &
    & xy_SurfAlbedo,                                           &
    & xyz_DelAtmMass,                                          &
    & xyr_RadSUwFlux, xyr_RadSDwFlux                           &
    & )

    ! USE statements
    !

    ! 螟ｪ髯ｽ謾ｾ蟆�ヵ繝ｩ繝�け繧ｹ縺ｮ險ｭ螳�
    ! Set solar constant
    !
    use set_solarconst, only : SetSolarConst

    ! 遏ｭ豕｢蜈･蟆� (螟ｪ髯ｽ蜈･蟆�)
    ! Short wave (insolation) incoming
    !
    use rad_short_income, only : RadShortIncome

    !
    ! Solve radiative transfer equation in two stream approximation
    !
    use rad_rte_two_stream_app, only: &
      & RadRTETwoStreamAppSW,         &
      & RadRTETwoStreamAppHomogAtm

    ! Chou and Lee (1996) 縺ｫ繧医ｋ遏ｭ豕｢謾ｾ蟆�Δ繝�Ν
    ! Short wave radiation model described by Chou and Lee (1996)
    !
    use rad_CL1996, only :       &
      & RadCL1996NumBands      , &
      & RadCL1996UVVISParams

    real(DP), intent(in ) :: xy_SurfAlbedo    (0:imax-1, 1:jmax )
    real(DP), intent(in ) :: xyz_DelAtmMass   (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) :: SolarConst

    real(DP) :: xy_InAngle    (0:imax-1, 1:jmax)
                              ! sec (蜈･蟆�ｧ�).
                              ! sec (angle of incidence)
    real(DP) :: DistFromStarScld
                               ! Distance between the central star and the planet
    real(DP) :: DiurnalMeanFactor

    integer  :: nbands1
    integer  :: nbands2

    real(DP) :: UVVISFracSolarFlux
    real(DP) :: UVVISO3AbsCoef
    real(DP) :: UVVISRayScatCoef

    real(DP) :: SolarFluxTOA

!!$    real(DP) :: xyz_SSA       (0:imax-1, 1:jmax, 1:kmax)
!!$    real(DP) :: xyz_AF        (0:imax-1, 1:jmax, 1:kmax)

    real(DP), parameter :: RayScatSinScatAlb = 1.0d0 - 1.0d-10
    real(DP), parameter :: RayScatAsymFact   = 0.0d0

    real(DP)            :: xyz_RayScatDelOptDep( 0:imax-1, 1:jmax, 1:kmax )

    real(DP)            :: xyz_DelTotOptDep( 0:imax-1, 1:jmax, 1:kmax )
    real(DP)            :: xyr_TotOptDep   ( 0:imax-1, 1:jmax, 0:kmax )
    real(DP)            :: xyr_RadFlux     ( 0:imax-1, 1:jmax, 0:kmax )
    real(DP)            :: xyr_RadUwFlux   ( 0:imax-1, 1:jmax, 0:kmax )
    real(DP)            :: xyr_RadDwFlux   ( 0:imax-1, 1:jmax, 0:kmax )

    real(DP) :: TotUVVISFracSolarFlux

    integer  :: i
    integer  :: j
    integer  :: k
    integer  :: l

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

    ! 險育ｮ玲凾髢楢ｨ域ｸｬ髢句ｧ�
    ! Start measurement of computation time
    !
    call TimesetClockStart( module_name )

    ! 螟ｪ髯ｽ謾ｾ蟆�ヵ繝ｩ繝�け繧ｹ縺ｮ險ｭ螳�
    ! Set solar constant
    !
    call SetSolarConst( &
      & SolarConst      & ! (out)
      & )

    ! 遏ｭ豕｢蜈･蟆��險育ｮ�
    ! Calculate short wave (insolation) incoming radiation
    !
    call RadShortIncome(                       &
      & xy_InAngle        = xy_InAngle,        & ! (out) optional
      & DistFromStarScld  = DistFromStarScld,  & ! (out) optional
      & DiurnalMeanFactor = DiurnalMeanFactor  &
      & )

    call RadCL1996NumBands(   &
      & nbands1, nbands2      & ! (out)
      & )

    ! Initialization
    !
    xyr_RadSUwFlux = 0.0_DP
    xyr_RadSDwFlux = 0.0_DP

    ! * 14286 to 57143 cm-1 (0.175 to 0.70 micron):
    !   * Rayleigh scattering,
    !
    do l = 1, nbands1

      ! UV and Visible optical properties and solar flux
      !
      call RadCL1996UVVISParams(                                &
        & l,                                                    & ! (in )
        & UVVISFracSolarFlux, UVVISO3AbsCoef, UVVISRayScatCoef  & ! (out)
        & )

      ! Rayleigh scattering coefficient is multiplied by a ratio
      UVVISRayScatCoef = UVVISRayScatCoef * RayleighScatCoefRatio

      SolarFluxTOA = UVVISFracSolarFlux * SolarConst / DistFromStarScld**2 * DiurnalMeanFactor

      ! Rayleigh scattering
      !
      if ( FlagRayleighScattering ) then
        xyz_RayScatDelOptDep = UVVISRayScatCoef * xyz_DelAtmMass
      else
        xyz_RayScatDelOptDep = 0.0d0
      end if

      ! Total optical parameter
      !
      xyz_DelTotOptDep =            &
        & + xyz_RayScatDelOptDep
      !
      xyr_TotOptDep(:,:,kmax) = 0.0d0
      do k = kmax-1, 0, -1
        xyr_TotOptDep(:,:,k) = xyr_TotOptDep(:,:,k+1) + xyz_DelTotOptDep(:,:,k+1)
      end do
      !
!!$      xyz_SSA = RayScatSinScatAlb
!!$      do k = 1, kmax
!!$        do j = 1, jmax
!!$          do i = 0, imax-1
!!$            if ( xyz_SSA(i,j,k) >= 1.0d0 ) then
!!$              xyz_SSA(i,j,k) = 1.0d0 - 1.0d-10
!!$            end if
!!$          end do
!!$        end do
!!$      end do
!!$      xyz_AF  = RayScatAsymFact
!!$
!!$      call RadRTETwoStreamAppSW(        &
!!$        & xyz_SSA, xyz_AF,              & ! (in)
!!$        & xyr_TotOptDep,                & ! (in)
!!$        & xy_SurfAlbedo,                & ! (in)
!!$        & SolarFluxTOA, xy_InAngle,     & ! (in)
!!$        & xyr_RadUwFlux, xyr_RadDwFlux  & ! (out)
!!$        & )

      call RadRTETwoStreamAppHomogAtm(                         &
        & xy_SurfAlbedo, SolarFluxTOA, xy_InAngle,             & ! (in )
        & RayScatSinScatAlb, RayScatAsymFact, xyr_TotOptDep,   & ! (in )
        & xyr_RadUwFlux, xyr_RadDwFlux                         & ! (out)
        & )

      xyr_RadSUwFlux = xyr_RadSUwFlux + xyr_RadUwFlux
      xyr_RadSDwFlux = xyr_RadSDwFlux + xyr_RadDwFlux

    end do

    ! * 1000 to 14286 cm-1 (0.70-10 micron):
    !

    SolarFluxTOA = SolarConst / DistFromStarScld**2 * DiurnalMeanFactor

    TotUVVISFracSolarFlux = 0.0_DP
    do l = 1, nbands1

      ! UV and Visible optical properties and solar flux
      !
      call RadCL1996UVVISParams(                                &
        & l,                                                    & ! (in )
        & UVVISFracSolarFlux, UVVISO3AbsCoef, UVVISRayScatCoef  & ! (out)
        & )

      TotUVVISFracSolarFlux = TotUVVISFracSolarFlux + UVVISFracSolarFlux
    end do
    if ( TotUVVISFracSolarFlux > 1.0_DP ) then
      call MessageNotify( 'E', module_name, &
        & 'TotUVVISFracSolarFlux, %f, is greater than 1.', &
        & d = (/TotUVVISFracSolarFlux/) )
    end if

    SolarFluxTOA = SolarFluxTOA * ( 1.0_DP - TotUVVISFracSolarFlux )

    xyr_TotOptDep = 0.0_DP

!!$    xyz_SSA = 1.0_DP - 1.0e-100_DP
!!$    xyz_AF  = 0.0_DP
!!$
!!$    call RadRTETwoStreamAppSW(        &
!!$      & xyz_SSA, xyz_AF,              & ! (in)
!!$      & xyr_TotOptDep,                & ! (in)
!!$      & xy_SurfAlbedo,                & ! (in)
!!$      & SolarFluxTOA, xy_InAngle,     & ! (in)
!!$      & xyr_RadUwFlux, xyr_RadDwFlux  & ! (out)
!!$      & )

    call RadRTETwoStreamAppHomogAtm(                &
      & xy_SurfAlbedo, SolarFluxTOA, xy_InAngle,    & ! (in )
      & 1.0_DP - 1.0e-10_DP, 0.0_DP, xyr_TotOptDep, & ! (in )
      & xyr_RadUwFlux, xyr_RadDwFlux                & ! (out)
      & )

    xyr_RadSUwFlux = xyr_RadSUwFlux + xyr_RadUwFlux
    xyr_RadSDwFlux = xyr_RadSDwFlux + xyr_RadDwFlux

    ! 險育ｮ玲凾髢楢ｨ域ｸｬ荳譎ょ●豁｢
    ! Pause measurement of computation time
    !
    call TimesetClockStop( module_name )

  end subroutine RadSimpleSWV20Flux

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

  subroutine RadSimpleSWV20Init

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

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

    ! 螟ｪ髯ｽ謾ｾ蟆�ヵ繝ｩ繝�け繧ｹ縺ｮ險ｭ螳�
    ! Set solar constant
    !
    use set_solarconst, only : SetSolarConstInit

    ! 遏ｭ豕｢蜈･蟆� (螟ｪ髯ｽ蜈･蟆�)
    ! Short wave (insolation) incoming
    !
    use rad_short_income, only : RadShortIncomeInit

    !
    ! Solve radiative transfer equation in two stream approximation
    !
    use rad_rte_two_stream_app, only: RadRTETwoStreamAppInit

    ! Chou and Lee (1996) 縺ｫ繧医ｋ遏ｭ豕｢謾ｾ蟆�Δ繝�Ν
    ! Short wave radiation model described by Chou and Lee (1996)
    !
    use rad_CL1996, only : RadCL1996Init

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

    if ( rad_simple_SW_V2_0_inited ) return

    ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｮ險ｭ螳�
    ! Default values settings
    !
    FlagRayleighScattering = .true.
    RayleighScatCoefRatio  = 1.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_SW_V2_0_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
    !

    ! 螟ｪ髯ｽ謾ｾ蟆�ヵ繝ｩ繝�け繧ｹ縺ｮ險ｭ螳�
    ! Set solar constant
    !
    call SetSolarConstInit

    ! 遏ｭ豕｢蜈･蟆� (螟ｪ髯ｽ蜈･蟆�)
    ! Short wave (insolation) incoming
    !
    call RadShortIncomeInit

    !
    ! Solve radiative transfer equation in two stream approximation
    !
    call RadRTETwoStreamAppInit

    ! Chou and Lee (1996) 縺ｫ繧医ｋ遏ｭ豕｢謾ｾ蟆�Δ繝�Ν
    ! Short wave radiation model described by Chou and Lee (1996)
    !
    call RadCL1996Init

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

    rad_simple_SW_V2_0_inited = .true.

  end subroutine RadSimpleSWV20Init

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

end module rad_simple_SW_V2_0