!= Module TimeSet
!
! Authors::   SUGIYAMA Ko-ichiro, ODAKA Masatsugu
! Version::   $Id: timeset.f90,v 1.5 2007/08/03 06:32:34 odakker Exp $ 
! Tag Name::  $Name: arare4-20071012 $
! Copyright:: Copyright (C) GFD Dennou Club, 2006. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
!== Overview 
!
!引数に与えられた NAMELIST ファイルから, 時刻に関する情報を取得し, 
!保管するための変数型モジュール
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!
!

module timeset
  !
  !引数に与えられた NAMELIST ファイルから, 時刻に関する情報を取得し, 
  !保管するための変数型モジュール
  !

  !モジュール読み込み
  use dc_types,   only: DP
  use dc_iounit,  only: FileOpen
  use dc_message, only: MessageNotify
  
  !暗黙の型宣言禁止
  implicit none

  !save 属性
  save

  ! Public Interface
  real(DP)          :: DelTimeLong    !長いタイムステップ
  real(DP)          :: DelTimeShort   !短いタイムステップ
  real(DP)          :: TimeInt        !積分時間
  real(DP)          :: TimeDisp       !ファイル出力する時間間隔
  real(DP)          :: DayTime        ! 1 日の長さ [s]
  integer           :: NstepLong      !長いタイムステップのステップ数
  integer           :: NstepShort     !短いタイムステップのステップ数
  integer           :: NstepDisp      !出力するまでのステップ数

contains

  subroutine timeset_init(cfgfile)
    !
    !NAMELIST から必要な情報を読み取り, 時間関連の変数の設定を行う. 
    !

    !暗黙の型宣言禁止
    implicit none

    !入力変数
    character(*), intent(in) :: cfgfile

    !内部変数
    integer                  :: unit
    
    !NAMELIST から情報を取得
    NAMELIST /timeset/ &
         & DelTimeLong, DelTimeShort, TimeInt, TimeDisp, DayTime
    
    call FileOpen(unit, file=cfgfile, mode='r')
    read(unit, NML=timeset)
    close(unit)

    !---------------------------------------------------------------
    ! エラー処理
    !   DelTimeShort < DelTimeLong =< TimeDisp =< TimeInt でない場合, 
    !   エラーを返し終了
    !---------------------------------------------------------------
    if ( TimeDisp > TimeInt ) then
      call MessageNotify( "E", &
        &                 "timeset_init", &
        &                 "TimeInt is smaller than TimeDisp" )
    else if ( DelTimeLong > TimeDisp ) then 
      call MessageNotify( "E", &
        &                 "timeset_init", &
        &                 "TimeDisp is smaller than DelTimeLong" )
    else if ( DelTimeShort > DelTimeLong ) then 
      call MessageNotify( "E", &
        &                 "timeset_init", &
        &                 "DelTimeLong is smaller than DelTimeShort" )
    end if


    !---------------------------------------------------------------
    ! ループ回数の設定
    !---------------------------------------------------------------
    NstepLong = nint( TimeInt / DelTimeLong )
    NstepShort = 2 * nint( DelTimeLong / DelTimeShort )
    
    !積分時間が長い時間ステップで割り切れない場合には警告を出す
    if(mod(TimeInt, DelTimeLong) /= 0) then 
      call MessageNotify( "W", &
        & "timeset_init", "mod(TimeInt, DelTimeLong) is not zero")
    end if
    
    !長い時間ステップが短い時間ステップで割り切れない場合には警告を出す
    if(mod(DelTimeLong, DelTimeShort) /= 0) then 
      call MessageNotify( "W", &
        & "timeset_init", "mod(DelTimeLong, DelTimeShort) is not zero")
    end if
    
    !出力するまでのステップ数を求める
    NStepDisp = nint( TimeDisp / DelTimeLong ) 
    if(mod(TimeDisp, DelTimeLong) /= 0) then 
      call MessageNotify( "W", &
        & "timeset_init", "mod(TimeDisp, DelTimeLong) is not zero")
    end if
    if(mod(NstepLong, NstepDisp) /= 0) then 
      call MessageNotify( "W", &
        & "timeset_init", "mod(NstepLong, NstepDisp) is not zero")
    end if
       
    !---------------------------------------------------------------
    ! 確認
    !---------------------------------------------------------------
    call MessageNotify( "M", &
      & "timeset_init", "DelTimeLong = %f", d=(/DelTimeLong/) )
    call MessageNotify( "M", &
      & "timeset_init", "DelTimeShort = %f", d=(/DelTimeShort/) )
    call MessageNotify( "M", &
      & "timeset_init", "NstepLong = %d", i=(/NstepLong/) )
    call MessageNotify( "M", &
      & "timeset_init", "NstepDisp = %d", i=(/NstepDisp/) )
    call MessageNotify( "M", &
      & "timeset_init", "NstepShort = %d", i=(/NstepShort/) )
    call MessageNotify( "M", &
      & "timeset_init", "TimeInt = %f",  d=(/TimeInt/)  )
    call MessageNotify( "M", &
      & "timeset_init", "TimeDisp= %f", d=(/TimeDisp/) )
    call MessageNotify( "M", &
      & "timeset_init", "DayTime= %f",  d=(/DayTime/)  )
   
  end subroutine timeset_init
  
end module timeset
