!=
!
!= set dust flux
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: set_dust_flux.f90,v 1.2 2014/05/07 09:39:22 murashin Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module set_dust_flux
  !
  !=
  !
  != set dust flux
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  !
  !
  !
  !
  !== Procedures List
  !
  ! SetDustFlux     :: 繝繧ｹ繝医ヵ繝ｩ繝�け繧ｹ縺ｮ險ｭ螳�
  ! SetDustFluxInit :: 蛻晄悄蛹�
  ! --------------- :: ------------
  ! SetDustFlux     :: Set dust flux
  ! SetDustFluxInit :: Initialization
  !
  !== NAMELIST
  !
  ! NAMELIST#set_dust_flux_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: &
    & TimeN,                & !
    & InitialDate             ! 險育ｮ鈴幕蟋区律譎�.
                              ! Start date of calculation

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

  ! 蜈ｬ髢区焔邯壹″
  ! Public procedure
  !
  public:: SetDustFlux
  public:: SetDustFluxInit

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

  ! 髱槫�髢句､画焚
  ! Private variables
  !
  real(DP), save :: StartDay
                              ! start day of dust injection
  real(DP), save :: DustFlux
                              ! Dust flux

  real(DP), save :: sec_in_a_day

!!$  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.
!!$
!!$  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 = 'set_dust_flux'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: set_dust_flux.f90,v 1.2 2014/05/07 09:39:22 murashin Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

contains

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

  subroutine SetDustFlux( &
    & xy_SurfDustFlux     & ! (out)
    & )
    !
    !
    !
    ! Set dust flux
    !

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

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

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

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !

    ! 螳溯｡梧枚 ; Executable statement
    !

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

    if ( TimeN >= sec_in_a_day * StartDay ) then
      xy_SurfDustFlux = DustFlux
    else
      xy_SurfDustFlux = 0.0_DP
    end if

  end subroutine SetDustFlux

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

  subroutine SetDustFluxInit
    !
    ! set_dust_flux 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蛻晄悄蛹悶ｒ陦後＞縺ｾ縺�.
    ! NAMELIST#set_dust_flux_nml 縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ縺ｯ縺薙�謇狗ｶ壹″縺ｧ陦後ｏ繧後∪縺�.
    !
    ! "set_dust_flux" module is initialized.
    ! "NAMELIST#set_dust_flux_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: &
      & DCCalInquire

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

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

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

    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 /set_dust_flux_nml/ &
      & StartDay, DustFlux
          !
          ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｫ縺､縺�※縺ｯ蛻晄悄蛹匁焔邯� "set_dust_flux#SetDustFluxInit"
          ! 縺ｮ繧ｽ繝ｼ繧ｹ繧ｳ繝ｼ繝峨ｒ蜿ら�縺ｮ縺薙→.
          !
          ! Refer to source codes in the initialization procedure
          ! "set_dust_flux#SetDustFluxInit" for the default values.
          !

    ! 螳溯｡梧枚 ; Executable statement
    !

    if ( set_dust_flux_inited ) return

    ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｮ險ｭ螳�
    ! Default values settings
    !
    StartDay = 0.0_DP
    DustFlux = 1.0e-6_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 = set_dust_flux_nml,  &  ! (out)
        & iostat = iostat_nml )        ! (out)
      close( unit_nml )

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

    !
    ! Preparation of length of a day in second
    !
    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

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

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

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

    set_dust_flux_inited = .true.

  end subroutine SetDustFluxInit

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

end module set_dust_flux