!= 遏ｭ豕｢蜈･蟆� (螟ｪ髯ｽ蜈･蟆�)
!
!= Short wave (insolation) incoming
!
! Authors::   Satoshi NODA, Yasuhiro MORIKAWA, Yukiko YAMADA, Yoshiyuki O. TAKAHASHI
! Version::   $Id: rad_short_income.f90,v 1.6 2013/05/25 06:33:57 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module rad_short_income
  !
  != 遏ｭ豕｢蜈･蟆� (螟ｪ髯ｽ蜈･蟆�)
  !
  != Short wave (insolation) incoming
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! 遏ｭ豕｢蜈･蟆� (螟ｪ髯ｽ蜈･蟆�) 繧定ｨ育ｮ励＠縺ｾ縺�.
  !
  ! Calculate short wave (insolation) incoming radiation.
  !
  !== 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

  ! 蠎ｧ讓吶ョ繝ｼ繧ｿ險ｭ螳�
  ! Axes data settings
  !
  use axesset, only: &
    & y_Lat,   & ! $ \varphi $ [rad.] . 邱ｯ蠎ｦ. Latitude
    & x_Lon      ! $ \lambda $ [rad.] . 邨悟ｺｦ. Longitude

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

  ! 蜈ｬ髢区焔邯壹″
  ! Public procedure
  !
  public:: RadShortIncome
  public:: RadShortIncomeInit

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

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

  logical,  save:: FlagAnnualMean
                              ! 蟷ｴ蟷ｳ蝮��蟆�ヵ繝ｩ繧ｰ.
                              ! Flag for annual mean incoming radiation.
  logical,  save:: FlagDiurnalMean
                              ! 譌･蟷ｳ蝮��蟆�ヵ繝ｩ繧ｰ.
                              ! Flag for diurnal mean incoming radiation.
  logical,  save:: FlagPerpetual
                              ! 蟄｣遽螟牙喧縺ｪ縺怜�蟆�ヵ繝ｩ繧ｰ.
                              ! Flag for perpetual incoming radiation.

  real(DP), save :: PerpSinDel
                              ! sine of declination angle for perpetual experiments
  real(DP), save :: PerpDistFromStarScld
                              ! distance between a planet and a central star for
                              ! perpetual experiment
  logical,  save:: FlagSpecifySolDay
                              !
                              ! Flag for specifying solar day
  real(DP), save :: SolDay
                              ! length of solar day

  ! NOTE:
  !   Distance between the central star and the planet, and the declination can be
  !   determined by specifying following three parameters:
  !     PerLonFromVE     : the longitude of the perihelion measured from the vernal
  !                        equinox,
  !     LonFromVEAtEpoch : the longitude of the planet at epoch,
  !     epoch            : date or time.
  !
  real(DP), save:: PerLonFromVE
                              ! 譏･蛻�°繧画ｸｬ縺｣縺溯ｿ第律轤ｹ縺ｮ邨悟ｺｦ (霑第律轤ｹ鮟�ｵ�) (degree)
                              ! Longitude of the perihelion from vernal equinox (degree)
  real(DP), save:: LonFromVEAtEpoch
                              ! 蜈�悄縺ｫ縺翫￠繧区ヱ譏溘�邨悟ｺｦ (鮟�ｵ�) (degree)
                              ! Longitude of the planet at epoch (degree)
  real(DP), save:: TimeAtEpoch
                              ! 蜈�悄縺ｫ縺翫￠繧区凾蛻ｻ (sec)
                              ! Time at epoch (sec)

  real(DP), save:: EpsOrb
                              ! 襍､驕灘だ譁懆ｧ�.
                              ! Inclination of equator to orbit.
  real(DP), save:: Eccentricity
                              ! 髮｢蠢�紫.
                              ! Eccentricity.

  integer,  save:: MaxItrEccAnomaly
                              ! 髮｢蠢�ｿ醍せ隗偵ｒ險育ｮ励☆繧区凾縺ｮ譛螟ｧ郢ｰ繧願ｿ斐＠蝗樊焚.
                              ! Maximum iteration number of times
                              ! to calculate eccentric anomaly.
  real(DP), save:: ThreEccAnomalyError
                              ! 髮｢蠢�ｿ醍せ隗偵ｒ險育ｮ励☆繧区凾縺ｮ隱､蟾ｮ縺ｮ險ｱ螳ｹ縺励″縺�､.
                              ! Threshold of error
                              ! to calculate eccentric anomaly.

  real(DP), save:: IncomAIns
                              ! $ A_{ins} $ . 蟷ｴ蟷ｳ蝮��蟆��菫よ焚.
                              ! Coefficient of annual mean incoming radiation.
  real(DP), save:: IncomBIns
                              ! $ B_{ins} $ . 蟷ｴ蟷ｳ蝮��蟆��菫よ焚. AIns 縺ｫ蜷後§.
                              ! Coefficient of annual mean incoming radiation.
                              ! Same as "AIns".
  real(DP), save:: IncomAZet
                              ! $ A_{\zeta} $ . 蟷ｴ蟷ｳ蝮��蟆�ｧ偵�菫よ焚. AIns 縺ｫ蜷後§.
                              ! Coefficient of annual mean incoming radiation.
                              ! Same as "AIns".
  real(DP), save:: IncomBZet
                              ! $ B_{\zeta} $ . 蟷ｴ蟷ｳ蝮��蟆�ｧ偵�菫よ焚. AIns 縺ｫ蜷後§.
                              ! Coefficient of annual mean incoming radiation.
                              ! Same as "AIns".

  real(DP), allocatable, save:: xy_InAngle (:,:)
                              ! sec (蜈･蟆�ｧ�).
                              ! sec (angle of incidence)

  logical:: FlagRadSynchronous
                              ! 蜷梧悄蝗櫁ｻ｢譌･蟆��繝輔Λ繧ｰ
                              ! Flag for synchronous rotation

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

contains

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

  subroutine RadShortIncome(                    &
    & xy_InAngle, DistFromStarScld, xy_CosZet,  & ! (out) optional
    & DiurnalMeanFactor, PlanetLonFromVE,       & ! (out) optional
    & FlagOutput                                & ! (in)  optional
    & )
    !
    ! 遏ｭ豕｢蜈･蟆� (螟ｪ髯ｽ蜈･蟆�) 繧定ｨ育ｮ励＠縺ｾ縺�.
    !
    ! Calculate short wave (insolation) incoming radiation.
    !

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

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

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

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none
    real(DP), intent(out), optional :: xy_InAngle (0:imax-1, 1:jmax)
                              ! sec ( 蜈･蟆�ｧ� ).
                              ! sec ( Incident angle )
    real(DP), intent(out), optional :: DistFromStarScld
                              ! 霆碁％髟ｷ蜊雁ｾ�〒繧ｹ繧ｱ繝ｼ繝ｪ繝ｳ繧ｰ縺励◆諱呈弌縺九ｉ縺ｮ霍晞屬
                              ! Distance from the star scaled
                              ! by semimajor axis of the planet's orbit
    real(DP), intent(out), optional :: xy_CosZet(0:imax-1, 1:jmax)
                              ! cos( 蜈･蟆�ｧ� )
                              ! cos( Incident angle )
    real(DP), intent(out), optional :: DiurnalMeanFactor
    real(DP), intent(out), optional :: PlanetLonFromVE
                              ! 荳ｭ蠢�弌繧剃ｸｭ蠢�→縺吶ｋ蠎ｧ讓吶↓縺翫￠繧区丼蛻�せ縺九ｉ貂ｬ縺｣縺滓ヱ譏溘�邨悟ｺｦ
                              ! Longitude of the planet measured from the vernal equinox
                              ! in the coordinate that the central star is located on
                              ! the origin.
    logical , intent(in ), optional :: FlagOutput

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    integer:: i                ! 邨悟ｺｦ譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                               ! Work variables for DO loop in longitude
    integer:: j                ! 邱ｯ蠎ｦ譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                               ! Work variables for DO loop in latitude
    real(DP):: SinDel          ! 襍､邱ｯ
                               ! Declination
    real(DP):: CosZet          ! 蜈･蟆�ｧ�
                               ! Incidence angle
    real(DP):: AngleMaxLon     ! 蜈･蟆�′譛螟ｧ縺ｨ縺ｪ繧狗ｷｯ蠎ｦ

    real(DP):: HourAngle       ! 譎りｧ�
                               ! Hour angle
    real(DP):: ClockByDay      ! 譎ょ綾繧呈律縺ｧ陦ｨ險倥＠縺溘ｂ縺ｮ (0.0 - 1.0)
                               ! Clock expressed by day (0.0 - 1.0)

    real(DP) :: xy_InAngleLV       (0:imax-1, 1:jmax)
                               ! sec ( 蜈･蟆�ｧ� ).
                               ! sec ( Incident angle )
                               ! (local variable)
    real(DP) :: DistFromStarScldLV
                               ! Distance between the central star and the planet
                               ! (local variable)
    real(DP) :: xy_CosZetLV        (0:imax-1, 1:jmax)
                               ! cos( 蜈･蟆�ｧ� )
                               ! cos( Incident angle )
                               ! (local variable)
    real(DP) :: PlanetLonFromVELV

    integer         :: hour_in_a_day, min_in_a_hour
    real(DP)        :: sec_in_a_min, sec_in_a_day

    ! 螳溯｡梧枚 ; Executable statement
    !

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

    ! Set flag for diurnally averaged insolation : This is temporary one.
    !
    if ( present( DiurnalMeanFactor ) ) then
      if ( ( .not. FlagAnnualMean ) .and. FlagDiurnalMean ) then
        DiurnalMeanFactor = 1.0_DP / PI
      else
        DiurnalMeanFactor = 1.0_DP
      end if
    end if

    ! 蜷梧悄蝗櫁ｻ｢譌･蟆��繝輔Λ繧ｰ
    ! Flag for synchronous rotation
    if ( .not. FlagRadSynchronous ) then

      ! 蟷ｴ, 譌･蟷ｳ蝮�律蟆��險育ｮ�
      ! Calculate annual mean, daily mean insolation
      !

      if ( FlagAnnualMean .and. FlagDiurnalMean ) then

        do i = 0, imax - 1
          do j = 1, jmax
            xy_CosZetLV(i,j) = IncomAIns + IncomBIns * cos( y_Lat(j) )**2

            if ( xy_CosZetLV(i,j) > 0.0_DP ) then
              xy_InAngleLV(i,j) = 1.0_DP / xy_CosZetLV(i,j)
            else
              xy_InAngleLV(i,j) = 0.
            end if

          end do
        end do

        DistFromStarScldLV = 1.0_DP
        SinDel             = 0.0_DP
        PlanetLonFromVELV  = 0.0_DP

      else if ( .not. FlagAnnualMean ) then

        ! Set sine of declination and distance between a planet and a central star
        ! scaled with semi-major axis
        !
        if ( FlagPerpetual ) then
          SinDel             = PerpSinDel
          DistFromStarScldLV = PerpDistFromStarScld
        else
          call ShortIncomCalcOrbParam(                       &
            & SinDel, DistFromStarScldLV, PlanetLonFromVELV  & ! (out) optional
            & )
          if ( present( PlanetLonFromVE ) ) PlanetLonFromVE = PlanetLonFromVELV
        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)
          & )
        sec_in_a_day  = hour_in_a_day * min_in_a_hour * sec_in_a_min

        if ( FlagSpecifySolDay ) then
          ! case with solar day which is different from sec_in_a_day (rotation
          ! period)
          ClockByDay = mod( TimeN / SolDay, 1.0_DP )
        else
          ! case with solar day which is the same as sec_in_a_day (rotation
          ! period)
          ClockByDay = DCCalDateEvalSecOfDay( TimeN, date = InitialDate )
          ClockByDay = ClockByDay / sec_in_a_day
        end if

!!$        write( 6, * ) '###', TimeN, ClockByDay, mod( TimeN / sec_in_a_day, 1.0_DP )
!!$        write( 60, * ) TimeN, ClockByDay, mod( TimeN / sec_in_a_day, 1.0_DP )
!!$        call flush( 60 )

        do i = 0, imax - 1
          do j = 1, jmax

            if ( FlagDiurnalMean ) then
              HourAngle = 0.0_DP
            else
              HourAngle = ClockByDay * 2.0_DP * PI - PI + x_Lon(i)
            end if

            CosZet = sin( y_Lat(j) ) * SinDel &
              &  + cos( y_Lat(j) ) * sqrt( 1.0_DP - SinDel**2 ) * cos( HourAngle )

            xy_CosZetLV(i,j) = CosZet

            if ( CosZet > 0.0_DP ) then
              xy_InAngleLV(i,j)       = 1.0_DP / CosZet
            else
              xy_InAngleLV(i,j)       = 0.
            end if

          end do
        end do

      else

        ! 蟇ｾ蠢懊＠縺ｦ縺�↑縺��蟆�ち繧､繝�
        ! not implemented insolation type
        !
        call MessageNotify( 'E', module_name, 'This type of insolation is not impremented' )

      end if

    else

      ! 遏ｭ豕｢蜈･蟆� (螟ｪ髯ｽ蜈･蟆�) 繧定ｨ育ｮ励＠縺ｾ縺�.
      ! 蜷梧悄蝗櫁ｻ｢諠第弌繧呈Φ螳壹＠縺ｦ縺翫ｊ,
      ! 蟶ｸ縺ｫ邨悟ｺｦ 90 蠎ｦ縺ｧ譛螟ｧ蜈･蟆�, 邨悟ｺｦ 180-360 蠎ｦ縺ｧ蜈･蟆�ぞ繝ｭ縺ｨ縺ｪ縺｣縺ｦ縺�∪縺�.
      !
      ! Calculate short wave (insolation) incoming radiation.
      ! A planet with synchronized rotation are assumed.
      ! Incoming is max at latitude 90 deg., and zero between 180 and 360 deg. constantly.
      do i = 0, imax - 1
         AngleMaxLon  = - PI / 2.0_DP + x_Lon( i )
         do j = 1, jmax
            CosZet = cos( y_Lat(j) ) * cos( AngleMaxLon )

            xy_CosZetLV(i,j) = CosZet

            if ( CosZet > 0.0_DP ) then
               xy_InAngleLV(i,j) = 1.0_DP / CosZet
            else
               xy_InAngleLV(i,j) = 0.0_DP
            end if

            DistFromStarScldLV = 1.0_DP
         end do
      end do

    end if

    if ( present( xy_InAngle ) ) then
      xy_InAngle = xy_InAngleLV
    end if
    if ( present( xy_CosZet ) ) then
      xy_CosZet = xy_CosZetLV
    end if
    if ( present( DistFromStarScld ) ) then
      DistFromStarScld = DistFromStarScldLV
    end if

    ! 繝偵せ繝医Μ繝��繧ｿ蜃ｺ蜉�
    ! History data output
    !
    if ( present( FlagOutput ) ) then
      if ( FlagOutput ) then
        call HistoryAutoPut( TimeN, 'Decl'            , asin(SinDel)*180.0_DP/PI      )
        call HistoryAutoPut( TimeN, 'DistFromStarScld', DistFromStarScldLV            )
        call HistoryAutoPut( TimeN, 'PlanetLonFromVE' , PlanetLonFromVELV*180.0_DP/PI )
      end if
    else
      call HistoryAutoPut( TimeN, 'Decl'            , asin(SinDel)*180.0_DP/PI      )
      call HistoryAutoPut( TimeN, 'DistFromStarScld', DistFromStarScldLV            )
      call HistoryAutoPut( TimeN, 'PlanetLonFromVE' , PlanetLonFromVELV*180.0_DP/PI )
    end if

  end subroutine RadShortIncome

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

  subroutine ShortIncomCalcOrbParam(             &
    & SinDel, DistFromStarScld, PlanetLonFromVE  & ! (out)
    & )
    !
    ! 遏ｭ豕｢蜈･蟆� (螟ｪ髯ｽ蜈･蟆�) 繧定ｨ育ｮ励＠縺ｾ縺�.
    !
    ! Calculate short wave (insolation) incoming radiation.
    !

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

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

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none
    real(DP), intent(out) :: SinDel
                              ! 襍､邱ｯ
                              ! Declination
    real(DP), intent(out) :: DistFromStarScld
                              ! 霆碁％髟ｷ蜊雁ｾ�〒繧ｹ繧ｱ繝ｼ繝ｪ繝ｳ繧ｰ縺励◆諱呈弌縺九ｉ縺ｮ霍晞屬
                              ! Distance from the star scaled
                              ! by semimajor axis of the planet's orbit
    real(DP), intent(out) :: PlanetLonFromVE
                              ! 荳ｭ蠢�弌繧剃ｸｭ蠢�→縺吶ｋ蠎ｧ讓吶↓縺翫￠繧区丼蛻�せ縺九ｉ貂ｬ縺｣縺滓ヱ譏溘�邨悟ｺｦ
                              ! Longitude of the planet measured from the vernal equinox
                              ! in the coordinate that the central star is located on
                              ! the origin.

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    integer:: itr              ! 繧､繝�Ξ繝ｼ繧ｷ繝ｧ繝ｳ譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                               ! Work variables for DO loop in iteration direction

    real(DP):: MeanAnomaly     ! 蟷ｳ蝮�ｿ醍せ隗�
                               ! Mean anomaly
    real(DP):: EccAnomaly      ! 髮｢蠢�ｿ醍せ隗�
                               ! eccentric anomaly
    real(DP):: EccAnomalyError ! 繝九Η繝ｼ繝医Φ豕輔↓縺翫￠繧矩屬蠢�ｿ醍せ隗偵�隱､蟾ｮ
                               ! error of eccentric anomaly in Newton method
    real(DP):: TrueAnomaly     ! 逵溽せ髮｢隗�
                               ! true anomaly

    integer         :: hour_in_a_day, min_in_a_hour, day_in_a_year
    integer, pointer:: day_in_month_ptr(:) => null()
    real(DP)        :: sec_in_a_min, sec_in_a_day, sec_in_a_year

    ! 螳溯｡梧枚 ; Executable statement
    !

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

    ! 蟄｣遽螟牙喧, 譌･螟牙喧縺後≠繧句�ｴ蜷医�險育ｮ�
    ! Calculate with seasonal change and diurnal change
    !
    call DCCalInquire( &
      & day_in_month_ptr = day_in_month_ptr , & ! (out)
      & hour_in_day      = hour_in_a_day,     & ! (out)
      & min_in_hour      = min_in_a_hour,     & ! (out)
      & sec_in_min       = sec_in_a_min       & ! (out)
      & )
    ! Add 1 to day_in_month_ptr(2) if it is leap year.
    !
    if ( DCCalDateChkLeapYear( TimeN, InitialDate ) ) then
      day_in_month_ptr(2) = day_in_month_ptr(2) + 1
    end if

    day_in_a_year = sum( day_in_month_ptr )
    deallocate( day_in_month_ptr )
    sec_in_a_day  = hour_in_a_day * min_in_a_hour * sec_in_a_min
    sec_in_a_year = day_in_a_year * sec_in_a_day

    MeanAnomaly  =                                               &
      &   2.0_DP * PI * ( TimeN - TimeAtEpoch ) / sec_in_a_year  &
      & + ( LonFromVEAtEpoch - PerLonFromVE ) * PI / 180.0_DP
    MeanAnomaly  = mod( MeanAnomaly, 2.0_DP * PI )

    ! 繝九Η繝ｼ繝医Φ豕輔ｒ逕ｨ縺�※蟷ｳ蝮�ｿ醍せ隗偵°繧蛾屬蠢�ｿ醍せ隗偵ｒ豎ゅａ繧�.
    ! Calculate eccentric anomaly from mean anomaly by using Newton method.

    EccAnomaly = MeanAnomaly
    do itr = 1, MaxItrEccAnomaly
      EccAnomalyError = EccAnomaly                                        &
        & - Eccentricity * sin(EccAnomaly) - MeanAnomaly
      if ( abs(EccAnomalyError) <= ThreEccAnomalyError ) exit
      EccAnomaly      = EccAnomaly                                        &
        & - EccAnomalyError / ( 1.0_DP - Eccentricity * cos(EccAnomaly) )
      EccAnomaly      = mod( EccAnomaly, 2.0 * PI )
    end do
    if ( itr > MaxItrEccAnomaly ) then
      call MessageNotify( 'E', module_name,                        &
        & 'Calculation for eccentric anomaly does not converge.' )
    end if

    DistFromStarScld = 1.0_DP - Eccentricity * cos( EccAnomaly )

    TrueAnomaly = 2.0_DP                                                    &
      & * atan(                                                             &
      &         sqrt( ( 1.0d0 + Eccentricity ) / ( 1.0d0 - Eccentricity ) ) &
      &           * tan( EccAnomaly / 2.0_DP )                              &
      &       )

    PlanetLonFromVE = TrueAnomaly + PerLonFromVE * PI / 180.0_DP
    PlanetLonFromVE = mod( PlanetLonFromVE, 2.0_DP * PI )

    SinDel = sin( EpsOrb * PI / 180.0_DP ) * sin( PlanetLonFromVE )

        ! code for debug
!!$        write( 60, * ) TimeN/sec_in_a_day, DCCalDateChkLeapYear(TimeN,date=InitialDate), day_in_a_year
!!$        write(  6, * ) TimeN/sec_in_a_day, DCCalDateChkLeapYear(TimeN,date=InitialDate), day_in_a_year
!!$        call flush( 60 )

!!$        write( 60, * ) TimeN/sec_in_a_day, asin(SinDel)*180.0/PI, DistFromStarScld, PlanetLonFromVE*180.0_DP/PI
!!$        write(  6, * ) TimeN/sec_in_a_day, asin(SinDel)*180.0/PI, DistFromStarScld, PlanetLonFromVE*180.0_DP/PI
!!$        call flush( 60 )

  end subroutine ShortIncomCalcOrbParam

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

  subroutine RadShortIncomeInit
    !
    ! 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:: EpochYear, EpochMonth, EpochDay, EpochHour, EpochMin
                              ! 蜈�悄譌･譎� (蟷ｴ譛域律譎ょ�).
                              ! "TimeAtEpoch" 縺瑚ｲ�縺ｮ蝣ｴ蜷医↓縺薙■繧峨′菴ｿ逕ｨ縺輔ｌ繧�.
                              !
                              ! Date at epoch (year, month, day, hour, minute)
                              ! These are used when "TimeAtEpoch" is negative.
    real(DP):: EpochSec
                              ! 蜈�悄譌･譎� (遘�).
                              ! "TimeAtEpoch" 縺瑚ｲ�縺ｮ蝣ｴ蜷医↓縺薙■繧峨′菴ｿ逕ｨ縺輔ｌ繧�.
                              !
                              ! Date at epoch (second)
                              ! These are used when "TimeAtEpoch" is negative.

    type(DC_CAL_DATE):: EpochDate
                              ! 蜈�悄縺ｮ譌･譎�
                              ! Date at epoch

    real(DP)         :: PerpDelDeg
                              ! Declination angle in degree used for perpetual experiment

    real(DP)         :: SolDayValue
    character(TOKEN) :: SolDayUnit

    integer:: unit_nml        ! NAMELIST 繝輔ぃ繧､繝ｫ繧ｪ繝ｼ繝励Φ逕ｨ陬�ｽｮ逡ｪ蜿ｷ.
                              ! Unit number for NAMELIST file open
    integer:: iostat_nml      ! NAMELIST 隱ｭ縺ｿ霎ｼ縺ｿ譎ゅ� IOSTAT.
                              ! IOSTAT of NAMELIST read

    logical         :: FlagUseOfEpochDate
    character(TOKEN):: date_print

    ! NAMELIST 螟画焚鄒､
    ! NAMELIST group name
    !
    namelist /rad_short_income_nml/                                     &
      & FlagRadSynchronous,                                             &
      & FlagAnnualMean, FlagDiurnalMean, FlagPerpetual,                 &
      & PerpDelDeg, PerpDistFromStarScld,                               &
      & EpsOrb,                                                         &
      & PerLonFromVE,                                                   &
      & LonFromVEAtEpoch,                                               &
      & Eccentricity,                                                   &
      & TimeAtEpoch,                                                    &
      & EpochYear, EpochMonth, EpochDay, EpochHour, EpochMin, EpochSec, &
      & MaxItrEccAnomaly, ThreEccAnomalyError,                          &
      & IncomAIns, IncomBIns, IncomAZet, IncomBZet,                     &
      & FlagSpecifySolDay, SolDayValue, SolDayUnit
          !
          ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｫ縺､縺�※縺ｯ蛻晄悄蛹匁焔邯� "rad_short_income#RadShortIncomeInit"
          ! 縺ｮ繧ｽ繝ｼ繧ｹ繧ｳ繝ｼ繝峨ｒ蜿ら�縺ｮ縺薙→.
          !
          ! Refer to source codes in the initialization procedure
          ! "rad_short_income#RadShortIncomeInit" for the default values.
          !

    ! 螳溯｡梧枚 ; Executable statement
    !

    if ( rad_short_income_inited ) return

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

    !---

    PerpDelDeg               = 0.0_DP
    PerpDistFromStarScld     = 1.0_DP

    !---

    EpsOrb               =   23.5_DP    ! Earth-like value
    PerLonFromVE         =    0.0_DP
    LonFromVEAtEpoch     =  280.0_DP    ! This value results in the fact that the planet
                                        ! is located at the position of vernal equinox
                                        ! at 80 days after calculation with the use of
                                        ! "360day" calendar.
    Eccentricity         =    0.0_DP
    TimeAtEpoch          =    0.0_DP
    EpochYear            =   -1
    EpochMonth           =   -1
    EpochDay             =   -1
    EpochHour            =   -1
    EpochMin             =   -1
    EpochSec             =   -1.0_DP

    !---

    ! Sample values for the Earth
    !  References:
    !    Duffett-Smith, P., Practical astronomy with your calculator Third Edition,
    !    Cambridge University Press, pp.185, 1988.
    !
!!$    EpsOrb               =   23.44_DP                ! Rika nenpyo (Chronological
!!$                                                     ! Scientific Tables 2010)
!!$    PerLonFromVE         =  102.768413_DP + 180.0_DP ! Duffett-Smith (1988), p.105
!!$                                                     ! modified (plus 180 degrees)
!!$    LonFromVEAtEpoch     =   99.403308_DP + 180.0_DP ! Duffett-Smith (1988), p.105
!!$                                                     ! modified (plus 180 degrees)
!!$    Eccentricity         =    0.016713_DP            ! Duffett-Smith (1988), p.105
!!$    TimeAtEpoch          =   -1.0_DP                 ! EpochXXX written below are used
!!$                                                     ! because this is negative.
!!$    EpochYear            = 1990                      ! Duffett-Smith (1988), p.105
!!$    EpochMonth           =    1
!!$    EpochDay             =    1
!!$    EpochHour            =    0
!!$    EpochMin             =    0
!!$    EpochSec             =    0.0_DP
    !---

    ! Sample values for Mars
    !  References:
    !    Allison, M., Geophys. Res. Lett., 24, 1967-1970, 1997.
    !
!!$    EpsOrb               =   25.19_DP              ! Allison (1997)
!!$    PerLonFromVE         =  250.98_DP              ! Allison (1997) (modified)
!!$    LonFromVEAtEpoch     =  -10.342_DP             ! Arbitrarily set for clarity
!!$                                                   ! This results in Ls ~ 0 at Time = 0
!!$    Eccentricity         =    0.0934_DP            ! Allison (1997), value at epoch J2000
!!$    TimeAtEpoch          =    0.0_DP               ! Arbitrarily set for clarity
!!$    EpochYear            =   -1                    ! not used
!!$    EpochMonth           =   -1
!!$    EpochDay             =   -1
!!$    EpochHour            =   -1
!!$    EpochMin             =   -1
!!$    EpochSec             =   -1.0_DP

    !---

    MaxItrEccAnomaly     = 20
    ThreEccAnomalyError  = 1e-6_DP

    IncomAIns            = 0.127_DP   ! see dcpam document for reference
    IncomBIns            = 0.183_DP   ! see dcpam document for reference
    IncomAZet            = 0.410_DP   ! see dcpam document for reference
    IncomBZet            = 0.590_DP   ! see dcpam document for reference

    FlagSpecifySolDay = .false.
    SolDayValue       = 0.0_DP
    SolDayUnit        = 'sec'

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

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

    PerpSinDel = sin( PerpDelDeg * PI / 180.0_DP )

    FlagUseOfEpochDate = .false.

    if ( TimeAtEpoch < 0.0_DP ) then
      call DCCalDateCreate(    &
        & year  = EpochYear,   & ! (in)
        & month = EpochMonth,  & ! (in)
        & day   = EpochDay,    & ! (in)
        & hour  = EpochHour,   & ! (in)
        & min   = EpochMin,    & ! (in)
        & sec   = EpochSec,    & ! (in)
        & date  = EpochDate )    ! (out) optional

      TimeAtEpoch = DCCalDateDifference( &
        &                                 start_date = InitialDate, &  ! (in)
        &                                 end_date   = EpochDate    &  ! (in)
        &                              )

      FlagUseOfEpochDate = .true.
    end if

    SolDay = DCCalConvertByUnit( SolDayValue, SolDayUnit, 'sec' )

    ! 菫晏ｭ倡畑縺ｮ螟画焚縺ｮ蜑ｲ繧贋ｻ倥￠
    ! Allocate variables for saving
    !

    ! 繝偵せ繝医Μ繝��繧ｿ蜃ｺ蜉帙�縺溘ａ縺ｮ縺ｸ縺ｮ螟画焚逋ｻ骭ｲ
    ! Register of variables for history data output
    !
!!$    call HistoryAutoAddVariable( 'xxxxx' , &
!!$      & (/ 'lon ', 'lat ', 'sig', 'time'/), &
!!$      & 'xxxx', 'W m-2' )

    call HistoryAutoAddVariable( 'ISR' , &
      & (/ 'lon ', 'lat ', 'time'/),     &
      & 'incoming shortwave', 'W m-2' )

    call HistoryAutoAddVariable( 'Decl' , &
      & (/ 'time'/),                      &
      & 'declination angle', 'degree' )

    call HistoryAutoAddVariable( 'DistFromStarScld' , &
      & (/ 'time'/),                                  &
      & 'distance between the central star and the planet', '1' )

    call HistoryAutoAddVariable( 'PlanetLonFromVE' , &
      & (/ 'time'/),                                 &
      & 'planetary longitude from the vernal equinox', 'degree' )

    ! 蜊ｰ蟄� ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, 'ShortIncomming:' )
    call MessageNotify( 'M', module_name, '  FlagRadSynchronous       = %b', l = (/ FlagRadSynchronous /) )
    call MessageNotify( 'M', module_name, '  FlagAnnualMean           = %b', l = (/ FlagAnnualMean            /) )
    call MessageNotify( 'M', module_name, '  FlagDiurnalMean          = %b', l = (/ FlagDiurnalMean           /) )
    call MessageNotify( 'M', module_name, '  FlagPerpetual            = %b', l = (/ FlagPerpetual             /) )
    call MessageNotify( 'M', module_name, '  PerpDelDeg               = %f', d = (/ PerpDelDeg                /) )
    call MessageNotify( 'M', module_name, '  PerpDistFromStarScld     = %f', d = (/ PerpDistFromStarScld      /) )
    call MessageNotify( 'M', module_name, '  EpsOrb                   = %f', d = (/ EpsOrb                   /) )
    call MessageNotify( 'M', module_name, '  PerLonFromVE             = %f', d = (/ PerLonFromVE             /) )
    call MessageNotify( 'M', module_name, '  Eccentricity             = %f', d = (/ Eccentricity             /) )

    if ( FlagUseOfEpochDate ) then
      call DCCalDateInquire( date_print, date = EpochDate )
      call MessageNotify( 'M', module_name, '  EpochDate  = %c', &
        & c1 = trim(date_print) )
    end if
    call MessageNotify( 'M', module_name, '  TimeAtEpoch              = %f', d = (/ TimeAtEpoch              /) )
    call MessageNotify( 'M', module_name, '  LonFromVEAtEpoch         = %f', d = (/ LonFromVEAtEpoch         /) )

    call MessageNotify( 'M', module_name, '  MaxItrEccAnomaly         = %d', i = (/ MaxItrEccAnomaly         /) )
    call MessageNotify( 'M', module_name, '  ThreEccAnomalyError      = %f', d = (/ ThreEccAnomalyError      /) )
    call MessageNotify( 'M', module_name, '  IncomAIns                = %f', d = (/ IncomAIns                /) )
    call MessageNotify( 'M', module_name, '  IncomBIns                = %f', d = (/ IncomBIns                /) )
    call MessageNotify( 'M', module_name, '  IncomAZet                = %f', d = (/ IncomAZet                /) )
    call MessageNotify( 'M', module_name, '  IncomBZet                = %f', d = (/ IncomBZet                /) )

    call MessageNotify( 'M', module_name, '  FlagSpecifySolDay        = %b', l = (/ FlagSpecifySolDay        /) )
    call MessageNotify( 'M', module_name, '  SolDay                   = %f', d = (/ SolDay                   /) )

    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    rad_short_income_inited = .true.

  end subroutine RadShortIncomeInit

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

end module rad_short_income