!= リスタートデータ入出力
!
!= Restart data input/output
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: gridset.f90,v 1.5 2007/06/09 13:12:30 sugiyama Exp $ 
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module restart_file_io
  !
  != リスタートデータ入出力
  !
  != Restart data input/output
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  !== Procedures List
  !
  ! RestartFileOpen   :: リスタートファイルのオープン
  ! RestartFileOutput :: リスタートファイルへのデータ出力
  ! RestartFileClose  :: リスタートファイルのクローズ
  ! RestartFileGet    :: リスタートファイルの入力
  ! ------------      :: ------------
  ! RestartFileOpen   :: Open restart file
  ! RestartFileOutput :: Data output to restart file
  ! RestartFileClose  :: Close restart file
  ! RestartFileGet    :: Input restart file
  !
  !== NAMELIST
  !
  ! NAMELIST#restart_file_io_nml
  !

  ! モジュール引用 ; USE statements
  !

  ! 格子点設定
  ! 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

  ! 種別型パラメタ
  ! Kind type parameter
  !
  use dc_types, only: DP, &      ! 倍精度実数型. Double precision. 
    &                 STRING, &  ! 文字列.       Strings. 
    &                 TOKEN      ! キーワード.   Keywords. 

  ! メッセージ出力
  ! Message output
  !
  use dc_message, only: MessageNotify

  ! gtool4 データ出力
  ! Gtool4 data output
  !
  use gt4_history, only: GT_HISTORY

  ! 宣言文 ; Declaration statements
  !
  implicit none
  private

  ! 公開手続き
  ! Public procedure
  !
  public:: RestartFileOpen, RestartFileClose, RestartFileOutPut
  public:: RestartFileGet

  ! 公開変数
  ! Public variables
  !
  logical, save, public:: restart_file_io_inited = .false.
                              ! 初期設定フラグ. 
                              ! Initialization flag

  logical, save, public:: restart_file_opened = .false.
                              ! リスタートファイルのオープンに関するフラグ. 
                              ! Flag of restart file open

  ! 非公開変数
  ! Private variables
  !
  character(STRING), save:: InputFile
                              ! 入力するリスタートデータのファイル名
                              ! filename of input restart data

  character(STRING), save:: OutputFile
                              ! 出力するリスタートデータのファイル名
                              ! filename of output restart data
  real(DP), save:: IntValue
                              ! リスタートデータの出力間隔. 
                              ! Interval of restart data output
  character(TOKEN):: IntUnit
                              ! リスタートデータの出力間隔の単位. 
                              ! Unit for interval of restart data output

  type(GT_HISTORY), save:: gthst_rst
                              ! リスタートデータ用 gt4_history#GT_HISTORY 変数
                              ! "gt4_history#GT_HISTORY" variable for restart data


  integer:: IntStep
                              ! リスタートデータの出力間隔ステップ数
                              ! Number of step of interval of restart data output

  character(*), parameter:: module_name = 'restart_file_io'
                              ! モジュールの名称. 
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id:  $'
                              ! モジュールのバージョン
                              ! Module version

  ! INTERFACE 文 ; INTERFACE statements
  !
  interface RestartFileOpen
    module procedure RestartFileOpen
  end interface

  interface RestartFileClose
    module procedure RestartFileClose
  end interface

  interface RestartFileOutput
    module procedure RestartFileOutput
  end interface

  interface RestartFileGet
    module procedure RestartFileGet
  end interface

contains

  subroutine RestartFileOpen
    !
    ! リスタートファイルをオープンします. 
    !
    ! A restart file is opened. 
    !

    ! モジュール引用 ; USE statements
    !

    ! 出力ファイルの基本情報
    ! Basic information for output files
    ! 
    use fileset, only: &
      & FileTitle, &
                              ! 出力データファイルの表題.
                              ! Title of output data files
      & FileSource, &
                              ! データファイル作成の手段. 
                              ! Source of data file
      & FileInstitution
                              ! データファイルを最終的に変更した組織/個人. 
                              ! Institution or person that changes data files for the last time

    ! 物理定数設定
    ! Physical constants settings
    !
    use constants, only: PI   ! $ \pi $ .
                              ! 円周率.  Circular constant

    ! 座標データ設定
    ! Axes data settings
    !
    use axesset, only: &
      & x_Lon, &
                              ! $ \lambda $ [rad.] . 経度. Longitude
      & x_Lon_Weight, &
                              ! $ \Delta \lambda $ [rad.] . 
                              ! 経度座標重み. 
                              ! Weight of longitude
      & y_Lat, &
                              ! $ \varphi $ [rad.] . 緯度. Latitude
      & y_Lat_Weight, &
                              ! $ \Delta \varphi $ [rad.] . 
                              ! 緯度座標重み. 
                              ! Weight of latitude
      & z_Sigma, &
                              ! $ \sigma $ レベル (整数). 
                              ! Full $ \sigma $ level
      & r_Sigma, &
                              ! $ \sigma $ レベル (半整数). 
                              ! Half $ \sigma $ level
      & z_DelSigma
                              ! $ \Delta \sigma $ (整数). 
                              ! $ \Delta \sigma $ (Full)

    ! 時刻管理
    ! Time control
    !
    use timeset, only: DelTime, & ! $ \Delta t $ [s]
      & TimesetGetStartTime

    ! gtool4 データ出力
    ! Gtool4 data output
    !
    use gt4_history, only: HistoryCreate, HistoryAddVariable, &
      & HistoryPut, HistoryAddAttr

    ! 文字列操作
    ! Character handling
    !
    use dc_string, only: StoA

    ! 宣言文 ; Declaration statements
    !
    implicit none

    ! 作業変数
    ! Work variables
    !
    real(DP):: origin_time
                              ! 計算開始時刻. 
                              ! Start time of calculation

    ! 実行文 ; Executable statement
    !

    ! 初期化
    ! Initialization
    !
    if ( .not. restart_file_io_inited ) call RestartFileInit
    if ( restart_file_opened ) return

    ! 時刻情報の取得
    ! Get time information
    !
    call TimesetGetStartTime( &
      & origin_time, & ! (out)
      & IntUnit  &     ! (in) optional
      & )

    ! リスタートファイルのオープン
    ! Open a restart file
    !
    call HistoryCreate( &
      &      file = OutputFile,   &
      &     title = trim(FileTitle) // ' restart data', &          ! (in)
      &    source = FileSource, institution = FileInstitution, &   ! (in)
      &      dims = StoA( 'lon', 'lat', 'sig', 'sigm', 'time' ), & ! (in)
      &  dimsizes = (/ imax, jmax, kmax, kmax + 1, 0 /), &         ! (in)
      & longnames = StoA( 'longitude', 'latitude', &
      &                   'sigma at layer midpoints', &
      &                   'sigma at layer end-points (half level)', &
      &                   'time' ), &                              ! (in)
      &     units = StoA( 'degree_east', 'degree_north', &
      &                   '1', '1', IntUnit ), &                   ! (in)
      &    origin = real( origin_time ), &                         ! (in)
      &  interval = real( IntValue ), &                            ! (in)
      &   history = gthst_rst )                                    ! (out)

    ! $ \Delta t $ に関する情報を追加
    ! Add information about $ \Delta t $
    !
    call HistoryAddVariable( &
      & varname = 'deltime', &            ! (in)
      & dims = StoA(''), &                ! (in)
      & longname = 'delta time', &        ! (in)
      & units = 's', xtype = 'float', &   ! (in)
      & history = gthst_rst )             ! (inout)
    call HistoryPut( &
      & varname = 'deltime', &            ! (in)
      & array = (/ DelTime /), &          ! (in)
      & history = gthst_rst )             ! (inout)

    ! 座標データの設定
    ! Axes data settings
    !
    call HistoryAddAttr( &
      & varname = 'lon', attrname = 'standard_name', &   ! (in)
      & value = 'longitude', &                           ! (in)
      & history = gthst_rst )                            ! (inout)
    call HistoryAddAttr( &
      & varname = 'lat', attrname = 'standard_name', &   ! (in)
      & value = 'latitude', &                            ! (in)
      & history = gthst_rst )                            ! (inout)
    call HistoryAddAttr( &
      & varname = 'sig', attrname = 'standard_name', &   ! (in)
      & value = 'atmosphere_sigma_coordinate', &         ! (in)
      & history = gthst_rst )                            ! (inout)
    call HistoryAddAttr( &
      & varname = 'sigm', attrname = 'standard_name', &  ! (in)
      & value = 'atmosphere_sigma_coordinate', &         ! (in)
      & history = gthst_rst )                            ! (inout)
    call HistoryAddAttr( &
      & varname = 'time', attrname = 'standard_name', &  ! (in)
      & value = 'time', &                                ! (in)
      & history = gthst_rst )                            ! (inout)
    call HistoryAddAttr( &
      & varname = 'sig', attrname = 'positive', &        ! (in)
      & value = 'down', &                                ! (in)
      & history = gthst_rst )                            ! (inout)
    call HistoryAddAttr( &
      & varname = 'sigm', attrname = 'positive', &       ! (in)
      & value = 'down', &                                ! (in)
      & history = gthst_rst )                            ! (inout)

    call HistoryPut( &
      & varname = 'lon', &               ! (in)
      & array = x_Lon / PI * 180.0_DP, & ! (in)
      & history = gthst_rst )            ! (inout)
    call HistoryPut( &
      & varname = 'lat', &               ! (in)
      & array = y_Lat / PI * 180.0_DP, & ! (in)
      & history = gthst_rst )            ! (inout)
    call HistoryPut( &
      & varname = 'sig', &               ! (in)
      & array = z_Sigma, &               ! (in)
      & history = gthst_rst )            ! (inout)
    call HistoryPut( & 
      & varname = 'sigm', &              ! (in)
      & array = r_Sigma, &               ! (in)
      & history = gthst_rst )            ! (inout)

    ! 座標重みの設定
    ! Axes weights settings
    !
    call HistoryAddVariable( &
      & varname = 'lon_weight', &                           ! (in)
      & dims = StoA('lon'), &                               ! (in)
      & longname = 'weight for integration in longitude', & ! (in)
      & units = 'radian', xtype = 'double', &               ! (in)
      & history = gthst_rst )                               ! (inout)
    call HistoryAddAttr( &
      & varname = 'lon', attrname = 'gt_calc_weight', &     ! (in)
      & value = 'lon_weight', &                             ! (in)
      & history = gthst_rst )                               ! (inout)
    call HistoryPut( &
      & varname = 'lon_weight', array = x_Lon_Weight, &     ! (in)
      & history = gthst_rst )                               ! (inout)

    call HistoryAddVariable( &
      & varname = 'lat_weight', &                           ! (in)
      & dims = StoA('lat'), &                               ! (in)
      & longname = 'weight for integration in latitude', &  ! (in)
      & units = 'radian', xtype = 'double', &               ! (in)
      & history = gthst_rst )                               ! (inout)
    call HistoryAddAttr( &
      & varname = 'lat', attrname = 'gt_calc_weight', &     ! (in)
      & value = 'lat_weight', &                             ! (in)
      & history = gthst_rst )                               ! (inout)
    call HistoryPut( &
      & varname = 'lat_weight', array = y_Lat_Weight, &     ! (in)
      & history = gthst_rst )                               ! (inout)

    call HistoryAddVariable( &
      & varname = 'sig_weight', &                           ! (in)
      & dims = StoA('sig'), &                               ! (in)
      & longname = 'weight for integration in sigma', &     ! (in)
      & units = '1', xtype = 'double', &                    ! (in)
      & history = gthst_rst )                               ! (inout)
    call HistoryAddAttr( &
      & varname = 'sig', attrname = 'gt_calc_weight', &     ! (in)
      & value = 'sig_weight', &                             ! (in)
      & history = gthst_rst )                               ! (inout)
    call HistoryPut( &
      & varname = 'sig_weight', array = z_DelSigma, &       ! (in)
      & history = gthst_rst )                               ! (inout)

    ! 予報変数の設定
    ! Predictional variables settings
    !
    call HistoryAddVariable( &
      & varname = 'UB', &                                   ! (in)
      & dims = StoA('lon', 'lat', 'sig', 'time'), &         ! (in)
      & longname = 'eastward wind (at t-\Delta t)', &       ! (in)
      & units = 'm s-1', xtype = 'double', &                ! (in)
      & history = gthst_rst )                               ! (inout)
    call HistoryAddVariable( &
      & varname = 'VB', &                                   ! (in)
      & dims = StoA('lon', 'lat', 'sig', 'time'), &         ! (in)
      & longname = 'northward wind (at t-\Delta t)', &      ! (in)
      & units = 'm s-1', xtype = 'double', &                ! (in)
      & history = gthst_rst )                               ! (inout)
    call HistoryAddVariable( &
      & varname = 'TempB', &                                ! (in)
      & dims = StoA('lon', 'lat', 'sig', 'time'), &         ! (in)
      & longname = 'temperature (at t-\Delta t)', &         ! (in)
      & units = 'K', xtype = 'double', &                    ! (in)
      & history = gthst_rst )                               ! (inout)
    call HistoryAddVariable( &
      & varname = 'QVapB', &                                ! (in)
      & dims = StoA('lon', 'lat', 'sig', 'time'), &         ! (in)
      & longname = 'specific humidity (at t-\Delta t)', &   ! (in)
      & units = 'kg kg-1', xtype = 'double', &              ! (in)
      & history = gthst_rst )                               ! (inout)
    call HistoryAddVariable( &
      & varname = 'PsB', &                                  ! (in)
      & dims = StoA('lon', 'lat', 'time'), &                ! (in)
      & longname = 'surface pressure (at t-\Delta t)', &    ! (in)
      & units = 'Pa', xtype = 'double', &                   ! (in)
      & history = gthst_rst )                               ! (inout)

    call HistoryAddVariable( &
      & varname = 'UN', &                                   ! (in)
      & dims = StoA('lon', 'lat', 'sig', 'time'), &         ! (in)
      & longname = 'eastward wind (at t)', &                ! (in)
      & units = 'm s-1', xtype = 'double', &                ! (in)
      & history = gthst_rst )                               ! (inout)
    call HistoryAddVariable( &
      & varname = 'VN', &                                   ! (in)
      & dims = StoA('lon', 'lat', 'sig', 'time'), &         ! (in)
      & longname = 'northward wind (at t)', &               ! (in)
      & units = 'm s-1', xtype = 'double', &                ! (in)
      & history = gthst_rst )                               ! (inout)
    call HistoryAddVariable( &
      & varname = 'TempN', &                                ! (in)
      & dims = StoA('lon', 'lat', 'sig', 'time'), &         ! (in)
      & longname = 'temperature (at t)', &                  ! (in)
      & units = 'K', xtype = 'double', &                    ! (in)
      & history = gthst_rst )                               ! (inout)
    call HistoryAddVariable( &
      & varname = 'QVapN', &                                ! (in)
      & dims = StoA('lon', 'lat', 'sig', 'time'), &         ! (in)
      & longname = 'specific humidity (at t)', &            ! (in)
      & units = 'kg kg-1', xtype = 'double', &              ! (in)
      & history = gthst_rst )                               ! (inout)
    call HistoryAddVariable( &
      & varname = 'PsN', &                                  ! (in)
      & dims = StoA('lon', 'lat', 'time'), &                ! (in)
      & longname = 'surface pressure (at t)', &             ! (in)
      & units = 'Pa', xtype = 'double', &                   ! (in)
      & history = gthst_rst )                               ! (inout)

    restart_file_opened = .true.
  end subroutine RestartFileOpen



  subroutine RestartFileOutput( &
    & xyz_UB, xyz_VB, xyz_TempB, xyz_QVapB, xy_PsB, &   ! (in)
    & xyz_UN, xyz_VN, xyz_TempN, xyz_QVapN, xy_PsN  &   ! (in)
    & )
    !
    ! リスタートデータの出力を行います. 
    !
    ! Output restart data

    ! モジュール引用 ; USE statements
    !

    ! gtool4 データ出力
    ! Gtool4 data output
    !
    use gt4_history, only: HistoryPut

    ! 時刻管理
    ! Time control
    !
    use timeset, only: Cstep  ! 現在のステップ数. 
                              ! Current steps

    ! 宣言文 ; Declaration statements
    !
    implicit none
    real(DP), intent(in):: xyz_UB  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ u (t-\Delta t) $ .   東西風速. Eastward wind
    real(DP), intent(in):: xyz_VB  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ v (t-\Delta t) $ .   南北風速. Northward wind
    real(DP), intent(in):: xyz_TempB  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ T (t-\Delta t) $ .   温度. Temperature
    real(DP), intent(in):: xyz_QVapB  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ q (t-\Delta t) $ .   比湿. Specific humidity
    real(DP), intent(in):: xy_PsB (0:imax-1, 1:jmax)
                              ! $ p_s (t-\Delta t) $ . 地表面気圧. Surface pressure
    real(DP), intent(in):: xyz_UN  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ u (t) $ .     東西風速. Eastward wind
    real(DP), intent(in):: xyz_VN  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ v (t) $ .     南北風速. Northward wind
    real(DP), intent(in):: xyz_TempN  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ T (t) $ .     温度. Temperature
    real(DP), intent(in):: xyz_QVapN  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ q (t) $ .     比湿. Specific humidity
    real(DP), intent(in):: xy_PsN (0:imax-1, 1:jmax)
                              ! $ p_s (t) $ .   地表面気圧. Surface pressure

    ! 作業変数
    ! Work variables
    !
    real(DP):: timed           ! 現在時刻. Current time
    real:: timer               ! 現在時刻. Current time

    ! 実行文 ; Executable statement
    !

    if ( .not. restart_file_opened ) call RestartFileOpen

    ! 出力タイミングのチェック
    ! Check output timing
    !
    if ( mod( Cstep - 1, IntStep ) /= 0 ) return

    ! データ出力
    ! Data output
    !
    call HistoryPut( &
      & 'UB', xyz_UB, history = gthst_rst ) ! (in)
    call HistoryPut( &
      & 'VB', xyz_VB, history = gthst_rst ) ! (in)
    call HistoryPut( &
      & 'TempB', xyz_TempB, history = gthst_rst ) ! (in)
    call HistoryPut( &
      & 'QVapB', xyz_QVapB, history = gthst_rst ) ! (in)
    call HistoryPut( &
      & 'PsB', xy_PsB, history = gthst_rst ) ! (in)

    call HistoryPut( &
      & 'UN', xyz_UN, history = gthst_rst ) ! (in)
    call HistoryPut( &
      & 'VN', xyz_VN, history = gthst_rst ) ! (in)
    call HistoryPut( &
      & 'TempN', xyz_TempN, history = gthst_rst ) ! (in)
    call HistoryPut( &
      & 'QVapN', xyz_QVapN, history = gthst_rst ) ! (in)
    call HistoryPut( &
      & 'PsN', xy_PsN, history = gthst_rst ) ! (in)

  end subroutine RestartFileOutput



  subroutine RestartFileClose
    !
    ! リスタートデータファイル出力の終了処理を行います. 
    !
    ! Terminate restart data files output. 

    ! モジュール引用 ; USE statements
    !

    ! gtool4 データ出力
    ! Gtool4 data output
    !
    use gt4_history, only: HistoryClose

    ! 宣言文 ; Declaration statements
    !
    implicit none

    ! 作業変数
    ! Work variables
    !

    ! 実行文 ; Executable statement
    !
    if ( .not. restart_file_opened ) return

    call HistoryClose( history = gthst_rst ) ! (inout)

    restart_file_opened = .false.
  end subroutine RestartFileClose



  subroutine RestartFileGet( &
    & xyz_UB, xyz_VB, xyz_TempB, xyz_QVapB, xy_PsB, &   ! (out)
    & xyz_UN, xyz_VN, xyz_TempN, xyz_QVapN, xy_PsN  &   ! (out)
    & )
    !
    ! リスタートデータの入力を行います. 
    !
    ! Input restart data

    ! モジュール引用 ; USE statements
    !

    ! 初期値データ (リスタートデータ) 提供
    ! Prepare initial data (restart data)
    !
    use initial_data, only: InitDataGet

    ! 時刻管理
    ! Time control
    !
    use timeset, only: TimesetGetStartTime

    ! gtool4 データ出力
    ! Gtool4 data output
    !
    use gt4_history, only: HistoryGet

    ! 文字列操作
    ! Character handling
    !
    use dc_string, only: toChar

    ! 宣言文 ; Declaration statements
    !
    implicit none
    real(DP), intent(out):: xyz_UB  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ u (t-\Delta t) $ .   東西風速. Eastward wind
    real(DP), intent(out):: xyz_VB  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ v (t-\Delta t) $ .   南北風速. Northward wind
    real(DP), intent(out):: xyz_TempB  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ T (t-\Delta t) $ .   温度. Temperature
    real(DP), intent(out):: xyz_QVapB  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ q (t-\Delta t) $ .   比湿. Specific humidity
    real(DP), intent(out):: xy_PsB (0:imax-1, 1:jmax)
                              ! $ p_s (t-\Delta t) $ . 地表面気圧. Surface pressure
    real(DP), intent(out):: xyz_UN  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ u (t) $ .     東西風速. Eastward wind
    real(DP), intent(out):: xyz_VN  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ v (t) $ .     南北風速. Northward wind
    real(DP), intent(out):: xyz_TempN  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ T (t) $ .     温度. Temperature
    real(DP), intent(out):: xyz_QVapN  (0:imax-1, 1:jmax, 1:kmax)
                              ! $ q (t) $ .     比湿. Specific humidity
    real(DP), intent(out):: xy_PsN (0:imax-1, 1:jmax)
                              ! $ p_s (t) $ .   地表面気圧. Surface pressure

    ! 作業変数
    ! Work variables
    !
    real(DP):: start_time
                              ! 計算開始時刻
                              ! Start time of calculation
    character(TOKEN):: time_range
                              ! 時刻の指定
                              ! Specification of time

    ! 実行文 ; Executable statement
    !

    if ( .not. restart_file_io_inited ) call RestartFileInit

    ! データを initial_data モジュールから取得
    ! Data is input from "initial_data" module
    ! 
    if ( trim(InputFile) == '' ) then
      call InitDataGet( &
        & xyz_UB, xyz_VB, xyz_TempB, xyz_QVapB, xy_PsB, &  ! (out)
        & xyz_UN, xyz_VN, xyz_TempN, xyz_QVapN, xy_PsN )   ! (out)

    ! データを InputFile から取得
    ! Data is input from InputFile
    ! 
    else

      ! 時刻情報の取得
      ! Get time information
      !
      call TimesetGetStartTime( start_time ) ! (out)
      time_range = 'time=' // toChar( start_time )

      ! データ入力
      ! Data input
      ! 
      call HistoryGet( &
        & InputFile, 'UB', range = time_range, &  ! (in)
        & array = xyz_UB )                        ! (out)
      call HistoryGet( &
        & InputFile, 'VB', range = time_range, &  ! (in)
        & array = xyz_VB )                        ! (out)
      call HistoryGet( &
        & InputFile, 'TempB', range = time_range, &  ! (in)
        & array = xyz_TempB )                        ! (out)
      call HistoryGet( &
        & InputFile, 'QVapB', range = time_range, &  ! (in)
        & array = xyz_QVapB )                        ! (out)
      call HistoryGet( &
        & InputFile, 'PsB', range = time_range, &  ! (in)
        & array = xy_PsB )                         ! (out)

      call HistoryGet( &
        & InputFile, 'UN', range = time_range, &  ! (in)
        & array = xyz_UN )                        ! (out)
      call HistoryGet( &
        & InputFile, 'VN', range = time_range, &  ! (in)
        & array = xyz_VN )                        ! (out)
      call HistoryGet( &
        & InputFile, 'TempN', range = time_range, &  ! (in)
        & array = xyz_TempN )                        ! (out)
      call HistoryGet( &
        & InputFile, 'QVapN', range = time_range, &  ! (in)
        & array = xyz_QVapN )                        ! (out)
      call HistoryGet( &
        & InputFile, 'PsN', range = time_range, &  ! (in)
        & array = xy_PsN )                         ! (out)

    end if

  end subroutine RestartFileGet



  subroutine RestartFileInit
    !
    ! restart_file_io モジュールの初期化を行います. 
    ! NAMELIST#restart_file_io_nml の読み込みはこの手続きで行われます. 
    !
    ! "restart_file_io" module is initialized. 
    ! "NAMELIST#restart_file_io_nml" is loaded in this procedure. 
    !

    ! モジュール引用 ; USE statements
    !

    ! 時刻管理
    ! Time control
    !
    use timeset, only: DelTime, & ! $ \Delta t $ [s]
      & TimesetGetDelTime

    ! NAMELIST ファイル入力に関するユーティリティ
    ! Utilities for NAMELIST file input
    !
    use namelist_util, only: namelist_filename, NmlutilMsg

    ! ファイル入出力補助
    ! File I/O support
    !
    use dc_iounit, only: FileOpen

    ! 種別型パラメタ
    ! Kind type parameter
    !
    use dc_types, only: STDOUT ! 標準出力の装置番号. Unit number of standard output

    ! 宣言文 ; Declaration statements
    !
    implicit none

    ! 作業変数
    ! Work variables
    !
    integer:: unit_nml        ! NAMELIST ファイルオープン用装置番号. 
                              ! Unit number for NAMELIST file open
    integer:: iostat_nml      ! NAMELIST 読み込み時の IOSTAT. 
                              ! IOSTAT of NAMELIST read

    real(DP):: delta_time
                              ! $ \Delta t $

    ! NAMELIST 変数群
    ! NAMELIST group name
    !
    namelist /restart_file_io_nml/ &
      & InputFile, &
      & OutputFile, &
      & IntValue, IntUnit
          !
          ! デフォルト値については初期化手続 "restart_file_io#RestartFileInit" 
          ! のソースコードを参照のこと. 
          !
          ! Refer to source codes in the initialization procedure
          ! "restart_file_io#RestartFileInit" for the default values. 
          !


    ! 実行文 ; Executable statement
    !

    if ( restart_file_io_inited ) return
    call InitCheck

    ! デフォルト値の設定
    ! Default values settings
    !
    InputFile  = ''
    OutputFile = 'restart.nc'
    IntValue   = 1.0_DP
    IntUnit    = 'day'

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

      call NmlutilMsg( iostat_nml, module_name ) ! (in)
      if ( iostat_nml == 0 ) write( STDOUT, nml = restart_file_io_nml )
    end if


    ! 出力ステップ数の算出
    ! Calculate number of step of output 
    !
    call TimesetGetDelTime( &
      & delta_time, & ! (out)
      & IntUnit  &    ! (in) optional
      & )

    IntStep = max( 1, nint( IntValue / delta_time ) )


    ! 印字 ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, 'Input:: ' )
    call MessageNotify( 'M', module_name, '  InputFile  = %c', c1 = trim(InputFile) )
    call MessageNotify( 'M', module_name, 'Output:: ' )
    call MessageNotify( 'M', module_name, '  OutputFile = %c', c1 = trim(OutputFile) )
    call MessageNotify( 'M', module_name, '  IntValue   = %f', d = (/ IntValue /) )
    call MessageNotify( 'M', module_name, '  IntUnit    = %c', c1 = trim(IntUnit) )
    call MessageNotify( 'M', module_name, '  IntStep    = %d', i = (/ IntStep /) )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    restart_file_io_inited = .true.
  end subroutine RestartFileInit



  subroutine InitCheck
    !
    ! 依存モジュールの初期化チェック
    !
    ! Check initialization of dependency modules

    ! モジュール引用 ; USE statements
    !

    ! NAMELIST ファイル入力に関するユーティリティ
    ! Utilities for NAMELIST file input
    !
    use namelist_util, only: namelist_util_inited

    ! 出力ファイルの基本情報管理
    ! Management basic information for output files
    !
    use fileset, only: fileset_inited

    ! 格子点設定
    ! Grid points settings
    !
    use gridset, only: gridset_inited

    ! 物理定数設定
    ! Physical constants settings
    !
    use constants, only: constants_inited

    ! 座標データ設定
    ! Axes data settings
    !
    use axesset, only: axesset_inited

    ! 時刻管理
    ! Time control
    !
    use timeset, only: timeset_inited


    ! 実行文 ; Executable statement
    !

    if ( .not. namelist_util_inited ) &
      & call MessageNotify( 'E', module_name, '"namelist_util" module is not initialized.' )

    if ( .not. fileset_inited ) &
      & call MessageNotify( 'E', module_name, '"fileset" module is not initialized.' )

    if ( .not. gridset_inited ) &
      & call MessageNotify( 'E', module_name, '"gridset" module is not initialized.' )

    if ( .not. constants_inited ) &
      & call MessageNotify( 'E', module_name, '"constants" module is not initialized.' )

    if ( .not. axesset_inited ) &
      & call MessageNotify( 'E', module_name, '"axesset" module is not initialized.' )

    if ( .not. timeset_inited ) &
      & call MessageNotify( 'E', module_name, '"timeset" module is not initialized.' )


  end subroutine InitCheck

end module restart_file_io
