!= 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ, 蛻晄悄蛟､繝��繧ｿ蜈･蜃ｺ蜉�
!
!= Restart data, initial data input/output
!
! Authors::   Yasuhiro Morikawa, Yoshiyuki O. Takahashi
! Version::   $Id: restart_file_io.f90,v 1.26 2013/09/30 03:03:42 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2010. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module restart_file_io
  !
  != 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ, 蛻晄悄蛟､繝��繧ｿ蜈･蜃ｺ蜉�
  !
  != Restart data, initial data input/output
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ繧ゅ＠縺上�蛻晄悄蛟､繝��繧ｿ縺ｮ蜈･蜃ｺ蜉帙ｒ陦後＞縺ｾ縺�.
  ! 蜈･蜉帙ヵ繧｡繧､繝ｫ, 蜃ｺ蜉帙ヵ繧｡繧､繝ｫ, 繝��繧ｿ縺ｮ蜃ｺ蜉帙�髢馴囈縺ｯ
  ! NAMELIST#restart_file_io_nml 縺ｧ險ｭ螳壹＠縺ｾ縺�.
  !
  ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ縺ｮ蜈･蜉帙ヵ繧｡繧､繝ｫ縺梧欠螳壹＆繧後↑縺��ｴ蜷�,
  ! initial_data 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｧ逕滓�縺輔ｌ縺溷�譛溷､繝��繧ｿ繧貞叙蠕励＠縺ｾ縺�.
  !
  ! Restart data or initial data is input/output.
  ! Settings of input file, output file, and interval of data output
  ! is configured by "NAMELIST#restart_file_io_nml".
  !
  ! If input file of restart data is not set,
  ! initial data is generated in "initial_data" module, and
  ! obtained data from the module.
  !
  !== Procedures List
  !
  ! RestartFileOpen   :: 繝ｪ繧ｹ繧ｿ繝ｼ繝�/蛻晄悄蛟､繝輔ぃ繧､繝ｫ縺ｮ繧ｪ繝ｼ繝励Φ
  ! RestartFileOutput :: 繝ｪ繧ｹ繧ｿ繝ｼ繝�/蛻晄悄蛟､繝輔ぃ繧､繝ｫ縺ｸ縺ｮ繝��繧ｿ蜃ｺ蜉�
  ! InitialFileOutput :: 蛻晄悄蛟､繝輔ぃ繧､繝ｫ縺ｸ縺ｮ繝��繧ｿ蜃ｺ蜉�
  ! RestartFileClose  :: 繝ｪ繧ｹ繧ｿ繝ｼ繝�/蛻晄悄蛟､繝輔ぃ繧､繝ｫ縺ｮ繧ｯ繝ｭ繝ｼ繧ｺ
  ! RestartFileGet    :: 繝ｪ繧ｹ繧ｿ繝ｼ繝�/蛻晄悄蛟､繝輔ぃ繧､繝ｫ縺ｮ蜈･蜉�
  ! ------------      :: ------------
  ! RestartFileOpen   :: Open restart/initial file
  ! RestartFileOutput :: Data output to restart/initial file
  ! InitialFileOutput :: Data output to initial file
  ! RestartFileClose  :: Close restart/initial file
  ! RestartFileGet    :: Input restart/initial 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

  ! 邨��縺ｫ髢｢繧上ｋ驟榊�縺ｮ險ｭ螳�
  ! Settings of array for atmospheric composition
  !
  use composition, only: &
    &                    ncmax, &
                              ! 謌仙�縺ｮ謨ｰ
                              ! Number of composition
    &                    a_QMixName, &
                              ! 謌仙�縺ｮ螟画焚蜷�
                              ! Name of variables for composition
    &                    a_QMixLongName, &
                              ! 謌仙�縺ｮ髟ｷ縺�､画焚蜷�
                              ! Long name of variables for composition
    &                    IndexH2OVap
                              ! 豌ｴ闥ｸ豌励�繧､繝ｳ繝�ャ繧ｯ繧ｹ
                              ! Index for water vapor

  ! 遞ｮ蛻･蝙九ヱ繝ｩ繝｡繧ｿ
  ! 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 gtool_history, only: GT_HISTORY

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

  ! 蜈ｬ髢区焔邯壹″
  ! Public procedure
  !
  public :: RestartFileInit
  public :: RestartFileOpen, RestartFileClose, RestartFileOutPut, InitialFileOutput
  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

  character(STRING), save, public:: InputFile
                              ! 蜈･蜉帙☆繧九Μ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ縺ｮ繝輔ぃ繧､繝ｫ蜷�
                              ! Filename of input restart data

  character(STRING), save, public:: OutputFile
                              ! 蜃ｺ蜉帙☆繧九Μ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ縺ｮ繝輔ぃ繧､繝ｫ蜷�
                              ! Filename of output restart data
  real(DP), save, public:: IntValue
                              ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ縺ｮ蜃ｺ蜉幃俣髫� (謨ｰ蛟､).
                              ! Interval of restart data output (numerical value)
  character(TOKEN), save, public:: IntUnit
                              ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ縺ｮ蜃ｺ蜉幃俣髫� (蜊倅ｽ�).
                              ! Interval of restart data output (unit)
  real(DP), save, public:: IntTime
                              ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ縺ｮ蜃ｺ蜉幃俣髫� [遘綻.
                              ! Interval of restart data output [sec]

  ! 髱槫�髢句､画焚
  ! Private variables
  !
  logical, save:: flag_init_data_save
                              ! 蛻晄悄蛟､繝��繧ｿ繧剃ｽ懈�縺吶ｋ蝣ｴ蜷医�,
                              ! 縺薙�蠑墓焚縺ｫ .true. 繧剃ｸ弱∴縺ｾ縺�.
                              !
                              ! If initial data is created,
                              ! give ".true." to this argument.

  type(GT_HISTORY), save:: gthst_rst
                              ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ逕ｨ gtool_history#GT_HISTORY 螟画焚
                              ! "gtool_history#GT_HISTORY" variable for restart data

  real(DP), save:: PrevOutputTime
                              ! 蜑榊屓縺ｮ蜃ｺ蜉帶凾髢�.
                              ! Previous output time
  logical, save:: flag_output_end
                              ! 險育ｮ玲怙邨よ凾蛻ｻ縺ｮ蜃ｺ蜉帛ｮ御ｺ��繝輔Λ繧ｰ.
                              ! Flag for completion of output at the end time of calculation

  character(*), parameter:: module_name = 'restart_file_io'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: restart_file_io.f90,v 1.26 2013/09/30 03:03:42 yot Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

contains

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

  subroutine RestartFileOpen( flag_init_data )
    !
    ! 繝ｪ繧ｹ繧ｿ繝ｼ繝�/蛻晄悄蛟､繝輔ぃ繧､繝ｫ繧偵が繝ｼ繝励Φ縺励∪縺�.
    !
    ! A restart/initial data 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 and mathematical constants settings
    !
    use constants0, 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: &
      & RestartTime, &        ! 繝ｪ繧ｹ繧ｿ繝ｼ繝磯幕蟋区凾蛻ｻ.
                              ! Restart time of calculation
      & InitialDate           ! 險育ｮ鈴幕蟋区律譎�.
                              ! Start date of calculation

    ! gtool4 繝��繧ｿ蜃ｺ蜉�
    ! Gtool4 data output
    !
    use gtool_history, only: HistoryCreate, HistoryAddVariable, &
      & HistoryPut, HistoryAddAttr

    ! 證ｦ縺ｨ譌･譎ゅ�蜿悶ｊ謇ｱ縺�
    ! Calendar and Date handler
    !
    use dc_calendar, only: DCCalInquire, DCCalDateInquire

    ! 譁�ｭ怜�謫堺ｽ�
    ! Character handling
    !
    use dc_string, only: CPrintf, LChar

    ! 邨�∩霎ｼ縺ｿ髢｢謨ｰ PRESENT 縺ｮ諡｡蠑ｵ迚磯未謨ｰ
    ! Extended functions of intrinsic function "PRESENT"
    !
    use dc_present, only: present_and_true

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none
    logical, intent(in), optional:: flag_init_data
                              ! 蛻晄悄蛟､繝��繧ｿ繧剃ｽ懈�縺吶ｋ蝣ｴ蜷医�,
                              ! 縺薙�蠑墓焚縺ｫ .true. 繧剃ｸ弱∴縺ｾ縺�.
                              !
                              ! If initial data is created,
                              ! give ".true." to this argument.

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    character(STRING):: title_msg
                              ! 陦ｨ鬘後↓莉伜刈縺吶ｋ繝｡繝�そ繝ｼ繧ｸ.
                              ! Message added to title
    real(DP):: origin_time
                              ! 險育ｮ鈴幕蟋区凾蛻ｻ.
                              ! Start time of calculation

    integer:: InitialYear, InitialMonth, InitialDay, InitialHour, InitialMin
                              ! 險育ｮ鈴幕蟋九�蟷ｴ譛域律譎ょ�.
    real(DP):: InitialSec
                              ! 險育ｮ鈴幕蟋九�遘�.

    character(TOKEN):: cal_type
                              ! 證ｦ縺ｮ繧ｿ繧､繝�.
    integer:: month_in_year, hour_in_day, min_in_hour
    integer, pointer:: day_in_month_ptr(:) => null()
    real(DP):: sec_in_min
                              ! 證ｦ縺ｮ隧ｳ邏ｰ諠��ｱ

    logical:: flag_mpi_init

    integer :: n

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹�
    ! Initialization
    !
    flag_init_data_save = present_and_true( flag_init_data )

    if ( .not. restart_file_io_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    if ( restart_file_opened ) return

    ! 陦ｨ鬘後↓莉伜刈縺吶ｋ繝｡繝�そ繝ｼ繧ｸ縺ｮ險ｭ螳�
    ! Configure message added to title
    !
    if ( .not. flag_init_data_save ) then
      title_msg = ' restart data'
    else
      title_msg = ' initial data'
    end if

    ! 譎ょ綾諠��ｱ縺ｮ蜿門ｾ�
    ! Get time information
    !
    if ( .not. flag_init_data_save ) then
      origin_time = RestartTime + IntTime
    else
      origin_time = RestartTime
    end if

    flag_mpi_init = .false.

    ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ヵ繧｡繧､繝ｫ縺ｮ繧ｪ繝ｼ繝励Φ
    ! Open a restart file
    !
    call HistoryCreate( &
      &      file = OutputFile, &                                   ! (in)
      &     title = trim(FileTitle) // trim(title_msg), &           ! (in)
      &    source = FileSource, institution = FileInstitution, &    ! (in)
      &      dims = (/ 'lon    ', 'lat    ', 'sig    ', 'sigm   ', &
      &                'timestr', 'time   ' /), &                   ! (in)
      &  dimsizes = (/ imax, jmax, kmax, kmax + 1, TOKEN, 0 /), &   ! (in)
      & longnames = (/ 'longitude                             ', &
      &                'latitude                              ', &
      &                'sigma at layer midpoints              ', &
      &                'sigma at layer end-points (half level)', &
      &                'number of characters for datetime     ', &    ! (in)
      &                'time                                  ' /), & ! (in)
      &     units = (/ 'degrees_east ', 'degrees_north', &
      &                '1            ', '1            ', &
      &                '1            ',                  &
      &                'sec          ' /), &                           ! (in)
      & xtypes = (/'double', 'double', 'double', &
      &            'double', 'int   ', 'double'/), &                  ! (in)
      &        origind = origin_time, &       ! (in) optional
      &      intervald = IntTime, &           ! (in) optional
!!$      & flag_mpi_split = flag_mpi_init, &     ! (in) optional
      & flag_mpi_split = .true., &            ! (in) optional
      &        history = gthst_rst )          ! (out) optional

    ! 窶ｻ 蜈���險育ｮ励ョ繝ｼ繧ｿ縺九←縺�°縺ｮ繝√ぉ繝�け縺ｮ縺溘ａ縺ｮ繝輔Λ繧ｰ縺ｨ縺励※
    !    菴懈�縺励◆繧ゅ�縺ｧ縺ゅｋ縺溘ａ縲∽ｸ玖ｨ� flag_rst 險ｭ螳壼ｮ御ｺ�ｾ�
    !    蜑企勁莠亥ｮ� (morikawa  2010/06/13)
    !
    ! $ \Delta t $ 縺ｫ髢｢縺吶ｋ諠��ｱ繧定ｿｽ蜉�.
    ! Add information about $ \Delta t $.
    !
!!$    if ( .not. flag_init_data_save ) then
!!$      call HistoryAddVariable( &
!!$        & varname = 'deltime', &             ! (in)
!!$        & dims = (/''/), &                   ! (in)
!!$        & longname = 'delta time', &         ! (in)
!!$        & units = 'sec', xtype = 'double', & ! (in)
!!$        & history = gthst_rst )              ! (inout)
!!$      call HistoryPut( &
!!$        & varname = 'deltime', &            ! (in)
!!$        & array = (/ DelTime /), &          ! (in)
!!$        & history = gthst_rst )             ! (inout)
!!$    end if

    ! 髢句ｧ区律譎よュ蝣ｱ繧� "time" 縺ｮ螻樊ｧ縺ｨ縺励※險ｭ螳�
    ! Set start date information as attributes of "time"
    !
    call DCCalDateInquire( &
      & InitialYear, InitialMonth, InitialDay, & ! (out)
      & InitialHour, InitialMin,   InitialSec, & ! (out)
      & date = InitialDate )               ! (in)

    call HistoryAddAttr( &
      & 'time', attrname = 'origin', &     ! (in)
      & value = 'origin_year origin_month origin_day origin_hour origin_min origin_sec', &
                                           ! (in)
      & history = gthst_rst )              ! (inout)
    call HistoryAddAttr( &
      & 'time', attrname = 'origin_year', & ! (in)
      & value = InitialYear, &                ! (in)
      & history = gthst_rst )               ! (inout)
    call HistoryAddAttr( &
      & 'time', attrname = 'origin_month', & ! (in)
      & value = InitialMonth, &                ! (in)
      & history = gthst_rst )                ! (inout)
    call HistoryAddAttr( &
      & 'time', attrname = 'origin_day', &  ! (in)
      & value = InitialDay, &                 ! (in)
      & history = gthst_rst )               ! (inout)
    call HistoryAddAttr( &
      & 'time', attrname = 'origin_hour', &  ! (in)
      & value = InitialHour, &                 ! (in)
      & history = gthst_rst )                ! (inout)
    call HistoryAddAttr( &
      & 'time', attrname = 'origin_min', &  ! (in)
      & value = InitialMin, &                 ! (in)
      & history = gthst_rst )               ! (inout)
    call HistoryAddAttr( &
      & 'time', attrname = 'origin_sec', &  ! (in)
      & value = InitialSec, &                 ! (in)
      & history = gthst_rst )               ! (inout)

    ! 證ｦ諠��ｱ繧� "time" 縺ｮ螻樊ｧ縺ｨ縺励※險ｭ螳�
    ! Set calendar information as attributes of "time"
    !
    call DCCalInquire( &
      & cal_type         = cal_type, &          ! (out)
      & month_in_year    = month_in_year, &     ! (out)
      & day_in_month_ptr = day_in_month_ptr , & ! (out)
      & hour_in_day      = hour_in_day  , &     ! (out)
      & min_in_hour      = min_in_hour  , &     ! (out)
      & sec_in_min       = sec_in_min )         ! (out)

    call HistoryAddAttr( &
      & 'time', attrname = 'calendar', & ! (in)
      & value = cal_type, &              ! (in)
      & history = gthst_rst )            ! (inout)
    call HistoryAddAttr( &
      & 'time', attrname = 'month_in_year', & ! (in)
      & value = month_in_year, &              ! (in)
      & history = gthst_rst )                 ! (inout)
    call HistoryAddAttr( &
      & 'time', attrname = 'day_in_month', & ! (in)
      & value = day_in_month_ptr, &          ! (in)
      & history = gthst_rst )                ! (inout)
    call HistoryAddAttr( &
      & 'time', attrname = 'hour_in_day', & ! (in)
      & value = hour_in_day, &              ! (in)
      & history = gthst_rst )               ! (inout)
    call HistoryAddAttr( &
      & 'time', attrname = 'min_in_hour', & ! (in)
      & value = min_in_hour, &              ! (in)
      & history = gthst_rst )               ! (inout)
    call HistoryAddAttr( &
      & 'time', attrname = 'sec_in_min', & ! (in)
      & value = sec_in_min, &              ! (in)
      & history = gthst_rst )              ! (inout)

    deallocate( day_in_month_ptr )

    ! 蜀崎ｨ育ｮ励ョ繝ｼ繧ｿ逕ｨ縺ｮ繝輔Λ繧ｰ險ｭ螳�
    ! Set flag for a restart data file.
    !
    call HistoryAddVariable( &
      & varname = 'flag_rst', &               ! (in)
      & dims = (/''/), &                      ! (in)
      & longname = 'flag for restart data', & ! (in)
      & units = '1', xtype = 'int', &         ! (in)
      & history = gthst_rst )                 ! (inout)
    call HistoryAddAttr( &
      & varname = 'flag_rst', &               ! (in)
      & attrname = 'comment', &               ! (in)
      & value = 'If this value is nonzero, this file provides restart data', &
                                                ! (in)
      & history = gthst_rst )                 ! (inout)

    if ( flag_init_data_save ) then
      call HistoryPut( &
        & varname = 'flag_rst', &               ! (in)
        & array = (/ 0 /), &                    ! (in)
        & history = gthst_rst )                 ! (inout)
    else
      call HistoryPut( &
        & varname = 'flag_rst', &               ! (in)
        & array = (/ 1 /), &                    ! (in)
        & history = gthst_rst )                 ! (inout)
    end if

    ! 蟷ｴ譛域律譎ょ�遘貞ｽ｢蠑上�譌･譎よュ蝣ｱ螟画焚縺ｮ險ｭ螳�
    ! Set a date information variable with year-month-day hour:minute:second format
    !
    call HistoryAddVariable( &
      & varname = 'datetime', &                     ! (in)
      & dims =  (/'timestr', 'time   ' /), &        ! (in)
      & longname = 'time represented as strings', & ! (in)
      & units = '1', xtype = 'char', &              ! (in)
      & history = gthst_rst )                       ! (inout) optional

    ! 蠎ｧ讓吶ョ繝ｼ繧ｿ縺ｮ險ｭ螳�
    ! Axes data settings
    !
    call HistoryAddAttr( &
      & 'lon', attrname = 'standard_name', &     ! (in)
      & value = 'longitude', &                   ! (in)
      & history = gthst_rst )                    ! (inout)
    call HistoryAddAttr( &
      & 'lat', attrname = 'standard_name', &     ! (in)
      & value = 'latitude', &                    ! (in)
      & history = gthst_rst )                    ! (inout)
    call HistoryAddAttr( &
      & 'sig', attrname = 'standard_name', &     ! (in)
      & value = 'atmosphere_sigma_coordinate', & ! (in)
      & history = gthst_rst )                    ! (inout)
    call HistoryAddAttr( &
      & 'sigm', attrname = 'standard_name', &    ! (in)
      & value = 'atmosphere_sigma_coordinate', & ! (in)
      & history = gthst_rst )                    ! (inout)
    call HistoryAddAttr( &
      & 'time', attrname = 'standard_name', &    ! (in)
      & value = 'time', &                        ! (in)
      & history = gthst_rst )                    ! (inout)
    call HistoryAddAttr( &
      & 'sig', attrname = 'positive', &          ! (in)
      & value = 'down', &                        ! (in)
      & history = gthst_rst )                    ! (inout)
    call HistoryAddAttr( &
      & 'sigm', attrname = 'positive', &         ! (in)
      & value = 'down', &                        ! (in)
      & history = gthst_rst )                    ! (inout)

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

    ! 蠎ｧ讓咎㍾縺ｿ縺ｮ險ｭ螳�
    ! Axes weights settings
    !
    call HistoryAddVariable( 'lon_weight', &               ! (in)
      & (/'lon'/), &                                       ! (in)
      & 'weight for integration in longitude', 'radian', & ! (in)
      & xtype = 'double', &                                ! (in)
      & history = gthst_rst )                              ! (inout)
    call HistoryAddAttr( &
      & 'lon', attrname = 'gt_calc_weight', &     ! (in)
      & value = 'lon_weight', &                   ! (in)
      & history = gthst_rst )                     ! (inout)
    call HistoryPut( &
      & 'lon_weight', x_Lon_Weight, &             ! (in)
      & history = gthst_rst )                     ! (inout)

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

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

    ! 莠亥�ｱ螟画焚縺ｮ險ｭ螳�
    ! Predictional variables settings
    !
    if ( flag_init_data_save ) then

      ! 蛻晄悄繝��繧ｿ繝輔ぃ繧､繝ｫ逕ｨ
      ! For initial data file
      !
      call HistoryAddVariable( 'U', &                         ! (in)
        & (/ 'lon ', 'lat ', 'sig ', 'time' /), &             ! (in)
        & 'eastward wind', 'm s-1', &                         ! (in)
        & xtype = 'double', &                                 ! (in)
        & history = gthst_rst )                               ! (inout)
      call HistoryAddVariable( 'V', &                         ! (in)
        & (/ 'lon ', 'lat ', 'sig ', 'time' /), &             ! (in)
        & 'northward wind', 'm s-1', &                        ! (in)
        & xtype = 'double', &                                 ! (in)
        & history = gthst_rst )                               ! (inout)
      call HistoryAddVariable( 'Temp', &                      ! (in)
        & (/ 'lon ', 'lat ', 'sig ', 'time' /), &             ! (in)
        & 'temperature', 'K', &                               ! (in)
        & xtype = 'double', &                                 ! (in)
        & history = gthst_rst )                               ! (inout)
      do n = 1, ncmax
        call HistoryAddVariable( a_QMixName(n),                & ! (in)
          & (/ 'lon ', 'lat ', 'sig ', 'time' /),              & ! (in)
          & a_QMixLongName(n), 'kg kg-1',                      & ! (in)
          & xtype = 'double',                                  & ! (in)
          & history = gthst_rst                                & ! (inout)
          & )
      end do
      call HistoryAddVariable( 'Ps', &                        ! (in)
        & (/ 'lon ', 'lat ', 'time' /), &                     ! (in)
        & 'surface pressure', 'Pa', &                         ! (in)
        & xtype = 'double', &                                 ! (in)
        & history = gthst_rst )                               ! (inout)

    else

      ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ繝輔ぃ繧､繝ｫ逕ｨ
      ! For restart data file
      !
      call HistoryAddVariable( 'UB', &                        ! (in)
        & (/ 'lon ', 'lat ', 'sig ', 'time' /), &             ! (in)
        & 'eastward wind (at t-\Delta t)', 'm s-1', &         ! (in)
        & xtype = 'double', &                                 ! (in)
        & history = gthst_rst )                               ! (inout)
      call HistoryAddVariable( 'VB', &                        ! (in)
        & (/ 'lon ', 'lat ', 'sig ', 'time' /), &             ! (in)
        & 'northward wind (at t-\Delta t)', 'm s-1', &        ! (in)
        & xtype = 'double', &                                 ! (in)
        & history = gthst_rst )                               ! (inout)
      call HistoryAddVariable( 'TempB', &                     ! (in)
        & (/ 'lon ', 'lat ', 'sig ', 'time' /), &             ! (in)
        & 'temperature (at t-\Delta t)', 'K', &               ! (in)
        & xtype = 'double', &                                 ! (in)
        & history = gthst_rst )                               ! (inout)
      do n = 1, ncmax
        call HistoryAddVariable( trim(a_QMixName(n))//'B',    & ! (in)
          & (/ 'lon ', 'lat ', 'sig ', 'time' /),             & ! (in)
          & a_QMixLongName(n), 'kg kg-1',                     & ! (in)
          & xtype = 'double',                                 & ! (in)
          & history = gthst_rst                               & ! (inout)
          & )
      end do
      call HistoryAddVariable( 'PsB', &                       ! (in)
        & (/ 'lon ', 'lat ', 'time' /), &                     ! (in)
        & 'surface pressure (at t-\Delta t)', 'Pa', &         ! (in)
        & xtype = 'double', &                                 ! (in)
        & history = gthst_rst )                               ! (inout)

      call HistoryAddVariable( 'UN', &                        ! (in)
        & (/ 'lon ', 'lat ', 'sig ', 'time' /), &             ! (in)
        & 'eastward wind (at t)', 'm s-1', &                  ! (in)
        & xtype = 'double', &                                 ! (in)
        & history = gthst_rst )                               ! (inout)
      call HistoryAddVariable( 'VN', &                        ! (in)
        & (/ 'lon ', 'lat ', 'sig ', 'time' /), &             ! (in)
        & 'northward wind (at t)', 'm s-1', &                 ! (in)
        & xtype = 'double', &                                 ! (in)
        & history = gthst_rst )                               ! (inout)
      call HistoryAddVariable( 'TempN', &                     ! (in)
        & (/ 'lon ', 'lat ', 'sig ', 'time' /), &             ! (in)
        & 'temperature (at t)', 'K', &                        ! (in)
        & xtype = 'double', &                                 ! (in)
        & history = gthst_rst )                               ! (inout)
      do n = 1, ncmax
        call HistoryAddVariable( trim(a_QMixName(n))//'N',    & ! (in)
          & (/ 'lon ', 'lat ', 'sig ', 'time' /),             & ! (in)
          & a_QMixLongName(n), 'kg kg-1',                     & ! (in)
          & xtype = 'double',                                 & ! (in)
          & history = gthst_rst                               & ! (inout)
          & )
      end do
      call HistoryAddVariable( 'PsN', &                       ! (in)
        & (/ 'lon ', 'lat ', 'time' /), &                     ! (in)
        & 'surface pressure (at t)', 'Pa', &                  ! (in)
        & xtype = 'double', &                                 ! (in)
        & history = gthst_rst )                               ! (inout)
    end if

    restart_file_opened = .true.
  end subroutine RestartFileOpen

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

  subroutine RestartFileOutput(                      &
    & xyz_UB, xyz_VB, xyz_TempB, xyzf_QMixB, xy_PsB, &   ! (in)
    & xyz_UN, xyz_VN, xyz_TempN, xyzf_QMixN, xy_PsN  &   ! (in)
    & )
    !
    ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ縺ｮ蜃ｺ蜉帙ｒ陦後＞縺ｾ縺�.
    !
    ! Output restart data

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

    ! 譎ょ綾邂｡逅�
    ! Time control
    !
    use timeset, only: &
      & TimeN, &              ! 繧ｹ繝�ャ繝� $ t $ 縺ｮ譎ょ綾.
                              ! Time of step $ t $.
      & EndTime, &            ! 險育ｮ礼ｵゆｺ�凾蛻ｻ.
                              ! End time of calculation
      & InitialDate           ! 險育ｮ鈴幕蟋区律譎�.
                              ! Start date of calculation

    ! gtool4 繝��繧ｿ蜃ｺ蜉�
    ! Gtool4 data output
    !
    use gtool_history, only: HistoryPut, HistorySetTime

    ! 證ｦ縺ｨ譌･譎ゅ�蜿悶ｊ謇ｱ縺�
    ! Calendar and Date handler
    !
    use dc_calendar, only: DCCalDateInquire

    ! 邨�∩霎ｼ縺ｿ髢｢謨ｰ PRESENT 縺ｮ諡｡蠑ｵ迚磯未謨ｰ
    ! Extended functions of intrinsic function "PRESENT"
    !
    use dc_present, only: present_and_true

    ! 螳｣險譁� ; 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):: xyzf_QMixB (0:imax-1, 1:jmax, 1:kmax, 1:ncmax)
                              ! $ 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):: xyzf_QMixN (0:imax-1, 1:jmax, 1:kmax, 1:ncmax)
                              ! $ q (t) $ .     豈疲ｹｿ. Specific humidity
    real(DP), intent(in):: xy_PsN     (0:imax-1, 1:jmax)
                              ! $ p_s (t) $ .   蝨ｰ陦ｨ髱｢豌怜悸. Surface pressure

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    logical:: flag_output
                              ! 蜃ｺ蜉帙�繝輔Λ繧ｰ.
                              ! Flag for output
    character(TOKEN):: date_str         ! 譌･譎ゅ�蟷ｴ譛域律譎ょ�遘貞ｽ｢蠑�.
                                        ! Date with year-month-day hour:minute:second format
    integer :: n

    ! 螳溯｡梧枚 ; Executable statement
    !
    if ( .not. restart_file_opened ) call RestartFileOpen

    ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ蜃ｺ蜉帙〒縺ゅｋ縺薙→縺ｮ繝√ぉ繝�け
    ! Check restart data output
    !
    if ( flag_init_data_save ) then
      call MessageNotify( 'E', module_name, &
        & 'Now, initial data output mode.' )
    end if

    ! 蜃ｺ蜉帙ち繧､繝溘Φ繧ｰ縺ｮ繝√ぉ繝�け
    ! Check output timing
    !
    flag_output = TimeN - PrevOutputTime >= IntTime
    if ( TimeN >= EndTime .and. .not. flag_output_end ) then
      flag_output = .true.
      flag_output_end = .true.
    end if
    flag_output = ( .not. TimeN == PrevOutputTime ) .and. flag_output
    flag_output = flag_init_data_save .or. flag_output

    if ( .not. flag_output ) return

    ! 谺｡蝗樒畑縺ｫ, 莉雁屓縺ｮ蜃ｺ蜉� (蟶梧悍) 譎ょ綾 繧剃ｿ晏ｭ�
    ! Save output time (expected) in this time, for next time
    !
    PrevOutputTime = PrevOutputTime + IntTime

    ! 譎ょ綾縺ｮ險ｭ螳�
    ! Set time
    !
    call HistorySetTime( timed = TimeN, history = gthst_rst )

    ! "TimeN" 繧貞ｹｴ譛域律譎ょ�遘定｡ｨ險倥↓縺励※ "datetime" 螟画焚縺ｸ蜃ｺ蜉�
    ! Put "TimeN" on "datetime" variable with year-month-day hour:minute:second format
    !
    call DCCalDateInquire( &
      & date_str   = date_str, &   ! (out)
      & elapse_sec = TimeN, &      ! (in) optional
      & date       = InitialDate ) ! (in) optional

    call HistoryPut( 'datetime', date_str, &  ! (in)
      &              history = gthst_rst )    ! (inout) optional

    ! 繝��繧ｿ蜃ｺ蜉�
    ! 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)
    do n = 1, ncmax
      call HistoryPut( &
        & trim(a_QMixName(n))//'B', xyzf_QMixB(:,:,:,n), history = gthst_rst ) ! (in)
    end do
    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)
    do n = 1, ncmax
      call HistoryPut( &
        & trim(a_QMixName(n))//'N', xyzf_QMixN(:,:,:,n), history = gthst_rst ) ! (in)
    end do
    call HistoryPut( &
      & 'PsN', xy_PsN, history = gthst_rst ) ! (in)

  end subroutine RestartFileOutput

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

  subroutine InitialFileOutput(                 &
    & xyz_U, xyz_V, xyz_Temp, xyzf_QMix, xy_Ps  &   ! (in)
    & )
    !
    ! 蛻晄悄蛟､繝��繧ｿ縺ｮ蜃ｺ蜉帙ｒ陦後＞縺ｾ縺�.
    !
    ! Output initial data

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

    ! 譎ょ綾邂｡逅�
    ! Time control
    !
    use timeset, only: &
      & TimeN, &              ! 繧ｹ繝�ャ繝� $ t $ 縺ｮ譎ょ綾.
                              ! Time of step $ t $.
      & InitialDate           ! 險育ｮ鈴幕蟋区律譎�.
                              ! Start date of calculation

    ! gtool4 繝��繧ｿ蜃ｺ蜉�
    ! Gtool4 data output
    !
    use gtool_history, only: HistoryPut, HistorySetTime

    ! 證ｦ縺ｨ譌･譎ゅ�蜿悶ｊ謇ｱ縺�
    ! Calendar and Date handler
    !
    use dc_calendar, only: DCCalDateInquire

    ! 邨�∩霎ｼ縺ｿ髢｢謨ｰ PRESENT 縺ｮ諡｡蠑ｵ迚磯未謨ｰ
    ! Extended functions of intrinsic function "PRESENT"
    !
    use dc_present, only: present_and_true

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none
    real(DP), intent(in):: xyz_U    (0:imax-1, 1:jmax, 1:kmax)
                              ! $ u $ .     譚ｱ隘ｿ鬚ｨ騾�. Eastward wind
    real(DP), intent(in):: xyz_V    (0:imax-1, 1:jmax, 1:kmax)
                              ! $ v $ .     蜊怜圏鬚ｨ騾�. Northward wind
    real(DP), intent(in):: xyz_Temp (0:imax-1, 1:jmax, 1:kmax)
                              ! $ T $ .     貂ｩ蠎ｦ. Temperature
    real(DP), intent(in):: xyzf_QMix(0:imax-1, 1:jmax, 1:kmax, 1:ncmax)
                              ! $ q $ .     豈疲ｹｿ. Specific humidity
    real(DP), intent(in):: xy_Ps    (0:imax-1, 1:jmax)
                              ! $ p_s $ .   蝨ｰ陦ｨ髱｢豌怜悸. Surface pressure

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    character(STRING):: date_str        ! 譌･譎ゅ�蟷ｴ譛域律譎ょ�遘貞ｽ｢蠑�.
                                        ! Date with year-month-day hour:minute:second format
    integer          :: n

    ! 螳溯｡梧枚 ; Executable statement
    !
    if ( .not. restart_file_opened ) call RestartFileOpen( flag_init_data = .true. )

    ! 蛻晄悄蛟､蜃ｺ蜉帙〒縺ゅｋ縺薙→縺ｮ繝√ぉ繝�け
    ! Check initial data output
    !
    if ( .not. flag_init_data_save ) then
      call MessageNotify( 'E', module_name, &
        & 'Now, restart data output mode.' )
    end if

    ! 譎ょ綾縺ｮ險ｭ螳�
    ! Set time
    !
    call HistorySetTime( timed = TimeN, history = gthst_rst )

    ! "TimeN" 繧貞ｹｴ譛域律譎ょ�遘定｡ｨ險倥↓縺励※ "datetime" 螟画焚縺ｸ蜃ｺ蜉�
    ! Put "TimeN" on "datetime" variable with year-month-day hour:minute:second format
    !
    call DCCalDateInquire( &
      & date_str   = date_str, &   ! (out)
      & elapse_sec = TimeN, &      ! (in) optional
      & date       = InitialDate ) ! (in) optional

    call HistoryPut( 'datetime', date_str, &  ! (in)
      &              history = gthst_rst )    ! (inout) optional

    ! 繝��繧ｿ蜃ｺ蜉�
    ! Data output
    !
    call HistoryPut( &
      & 'U', xyz_U, history = gthst_rst )       ! (in)
    call HistoryPut( &
      & 'V', xyz_V, history = gthst_rst )       ! (in)
    call HistoryPut( &
      & 'Temp', xyz_Temp, history = gthst_rst ) ! (in)
    do n = 1, ncmax
      call HistoryPut( &
        & a_QMixName(n), xyzf_QMix(:,:,:,n), history = gthst_rst ) ! (in)
    end do
    call HistoryPut( &
      & 'Ps', xy_Ps, history = gthst_rst )      ! (in)

  end subroutine InitialFileOutput

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

  subroutine RestartFileClose
    !
    ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ繝輔ぃ繧､繝ｫ蜃ｺ蜉帙�邨ゆｺ��逅�ｒ陦後＞縺ｾ縺�.
    !
    ! Terminate restart data files output.

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

    ! gtool4 繝��繧ｿ蜃ｺ蜉�
    ! Gtool4 data output
    !
    use gtool_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, xyzf_QMixB, xy_PsB, & ! (out)
    & xyz_UN, xyz_VN, xyz_TempN, xyzf_QMixN, xy_PsN, & ! (out)
    & flag_initial                                   & ! (out) optional
    & )
    !
    ! 繝ｪ繧ｹ繧ｿ繝ｼ繝�/蛻晄悄蛟､繝��繧ｿ縺ｮ蜈･蜉帙ｒ陦後＞縺ｾ縺�.
    ! 繝ｪ繧ｹ繧ｿ繝ｼ繝�/蛻晄悄蛟､繝��繧ｿ繝輔ぃ繧､繝ｫ縺悟ｭ伜惠縺励↑縺��ｴ蜷医↓縺ｯ,
    ! initial_data 繝｢繧ｸ繝･繝ｼ繝ｫ繧堤畑縺�※, 蛻晄悄蛟､逕滓�繧定｡後＞縺ｾ縺�.
    !
    ! Input restart/initial data.
    ! If a restart/initial data file is not exist,
    ! initial data is created by "initial_data".

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

    ! 蛻晄悄蛟､繝��繧ｿ (繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ) 謠蝉ｾ�
    ! Prepare initial data (restart data)
    !
    use initial_data, only: SetInitData

    ! 譎ょ綾邂｡逅�
    ! Time control
    !
    use timeset, only: TimesetSetInitialDate, TimesetSetTimeN, TimesetSetCalendar, &
      & RestartTime           ! 繝ｪ繧ｹ繧ｿ繝ｼ繝磯幕蟋区凾蛻ｻ.
                              ! Restart time of calculation

    ! gtool4 繝��繧ｿ蜈･蜉�
    ! Gtool4 data input
    !
    use gtool_history, only: HistoryGet, HistoryGetAttr

    ! 譁�ｭ怜�謫堺ｽ�
    ! Character handling
    !
    use dc_string, only: toChar

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

    ! 螳｣險譁� ; 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):: xyzf_QMixB  (0:imax-1, 1:jmax, 1:kmax, 1:ncmax)
                              ! $ 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):: xyzf_QMixN  (0:imax-1, 1:jmax, 1:kmax, 1:ncmax)
                              ! $ q (t) $ .     豈疲ｹｿ. Specific humidity
    real(DP), intent(out):: xy_PsN      (0:imax-1, 1:jmax)
                              ! $ p_s (t) $ .   蝨ｰ陦ｨ髱｢豌怜悸. Surface pressure
    logical , intent(out), optional:: flag_initial
                              ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ繧定ｪｭ縺ｿ霎ｼ繧蝣ｴ蜷医↓縺ｯ,
                              ! .false. 縺�, 蛻晄悄蛟､繝��繧ｿ繧定ｪｭ縺ｿ霎ｼ繧蝣ｴ蜷医↓縺ｯ
                              ! .true. 縺瑚ｿ斐ｊ縺ｾ縺�.
                              !
                              ! If restart data is loaded, .false. is returned.
                              ! On the other hand, if initial data is loaded,
                              ! .true. is returned.

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    integer:: flag_rst        ! 蛻晄悄蛟､繝��繧ｿ縺九ｉ蜈･蜉帶凾縺ｯ 0,
                              ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ蜈･蜉帶凾縺ｯ 1.
                              !
                              ! If initial data is input, this value is 0.
                              ! If restart data is input, this value is 1.

    character(STRING):: time_range
                              ! 譎ょ綾縺ｮ謖�ｮ�.
                              ! Specification of time
    character(TOKEN):: dummy_str
                              ! 蜈･蜉帙メ繧ｧ繝�け逕ｨ縺ｮ繝繝溘�螟画焚
                              ! Dummy variable for check of input
    logical:: get_err
                              ! 蜈･蜉帶凾縺ｮ繧ｨ繝ｩ繝ｼ繝輔Λ繧ｰ.
                              ! Error flag for input
    real(DP):: returned_time
                              ! 蜈･蜉帙ョ繝ｼ繧ｿ縺ｮ譎ょ綾.
                              ! Time of input data.
    logical:: flag_time_exist
                              ! 蜈･蜉帙ョ繝ｼ繧ｿ縺ｮ譎ょ綾逕ｨ縺ｮ繝輔Λ繧ｰ.
                              ! Flag for time of input data.

    integer:: origin_year, origin_month, origin_day, origin_hour, origin_min
                              ! 險育ｮ鈴幕蟋九�蟷ｴ譛域律譎ょ�.
    real(DP):: origin_sec
                              ! 險育ｮ鈴幕蟋九�遘�.
    character(TOKEN):: cal_type
                              ! 證ｦ縺ｮ繧ｿ繧､繝�.
    integer:: month_in_year, hour_in_day, min_in_hour
    integer, pointer:: day_in_month_ptr(:) => null()
    real(DP):: sec_in_min
                              ! 證ｦ縺ｮ隧ｳ邏ｰ諠��ｱ
    real(DP):: rst_time
                              ! 繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ繝輔ぃ繧､繝ｫ縺九ｉ蠕励ｉ繧後ｋ譎ょ綾螟画焚
                              ! Time variable from a restart data file

    logical:: flag_mpi_init

    integer :: n

    ! 螳溯｡梧枚 ; Executable statement
    !

    if ( .not. restart_file_io_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    ! 繝��繧ｿ繧� initial_data 繝｢繧ｸ繝･繝ｼ繝ｫ縺九ｉ蜿門ｾ�
    ! Data is input from "initial_data" module
    !
    if ( trim(InputFile) == '' ) then
      call SetInitData( &
        & xyz_UB, xyz_VB, xyz_TempB, xyzf_QMixB(:,:,:,IndexH2OVap), xy_PsB )   ! (out)

      ! The variables below are initialized here, temporarily.
      !
      xyzf_QMixB(:,:,:,1:IndexH2OVap-1)     = 0.0_DP
      xyzf_QMixB(:,:,:,IndexH2OVap+1:ncmax) = 0.0_DP

      call MessageNotify( 'M', module_name, &
        & 'Initial data (not restart data) is input ' // &
        & 'from a module "initial_data". ' // &
        & '*B (t-dt) and *N (t) are same.' )

      xyz_UN     = xyz_UB
      xyz_VN     = xyz_VB
      xyz_TempN  = xyz_TempB
      xyzf_QMixN = xyzf_QMixB
      xy_PsN     = xy_PsB

      if ( present(flag_initial) ) flag_initial = .true.

    ! 繝��繧ｿ繧� InputFile 縺九ｉ蜿門ｾ�
    ! Data is input from InputFile
    !
    else

!!$      flag_mpi_init = .false.
      flag_mpi_init = .true.

      ! 繝輔ぃ繧､繝ｫ縺ｮ譛臥┌繧堤｢ｺ隱�
      ! Conform an existence of an input file
      !
      call HistoryGetAttr( InputFile, 'lon', 'units', & ! (in)
        & dummy_str, &                                  ! (out)
        & flag_mpi_split = flag_mpi_init, &             ! (in) optional
        & err = get_err )                               ! (out)

      if ( get_err ) then
        call MessageNotify( 'E', module_name, &
          & 'restart/initial data file "%c" is not found.', &
          & c1 = trim(InputFile) )
      end if

      ! 譎ょ綾諠��ｱ縺ｮ蜿門ｾ�
      ! Get time information
      !
      time_range = 'time=' // toChar( RestartTime )

      ! 蛻晄悄蛟､繝��繧ｿ縺九Μ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ縺九�繝√ぉ繝�け
      ! Check whether input data is initial data or restart data
      !
      call HistoryGet( &
        & InputFile, 'flag_rst', &                ! (in)
        & flag_rst, &                             ! (out)
        & err = get_err, &                        ! (out) optional
        & quiet = .true., &                       ! (in) optional
        & flag_mpi_split = flag_mpi_init )        ! (in) optional

      if ( get_err ) then
        ! flag_rst 螟画焚縺瑚ｪｭ縺ｿ霎ｼ繧√↑縺��ｴ蜷医√→繧翫≠縺医★蛻晄悄蛟､繝��繧ｿ縺ｨ隕九↑縺�
        !
        flag_rst = 0
        if ( present(flag_initial) ) flag_initial = .true.

      else if ( flag_rst == 0 ) then
        if ( present(flag_initial) ) flag_initial = .true.

      else
        if ( present(flag_initial) ) flag_initial = .false.
      end if

      ! 迚ｩ逅�㍼繝��繧ｿ縺ｮ譛臥┌縺ｮ繝√ぉ繝�け ("U", "UB" 縺ｮ縺ｿ)
      ! Check existence or nonexistence of physical data (only "U", "UB")
      !
      if ( flag_rst == 0 ) then
        call HistoryGet( &
          & InputFile, 'U', &                       ! (in)
          & xyz_UB, &                               ! (out)
          & err = get_err, &                        ! (out) optional
          & quiet = .true., &                       ! (in) optional
          & flag_mpi_split = flag_mpi_init )        ! (in) optional

        if ( get_err ) then
          call MessageNotify( 'E', module_name, &
            & 'One of necessary variables "U" for start of calculation ' // &
            & 'cannot be read from "%c"', &
            & c1 = trim(InputFile) )
        end if

      else

        call HistoryGet( &
          & InputFile, 'UB', &                      ! (in)
          & xyz_UB, &                               ! (out)
          & range = time_range, &                   ! (in) optional
          & quiet = .true., &                       ! (in) optional
          & err = get_err, &                        ! (out) optional
          & returned_time = returned_time, &        ! (out) optional
          & flag_time_exist = flag_time_exist, &    ! (out) optional
          & flag_mpi_split = flag_mpi_init )        ! (in) optional

        if ( get_err ) then
          call MessageNotify( 'E', module_name, &
            & 'One of necessary variables "UB" for restart ' // &
            & 'cannot be read from "%c"', &
            & c1 = trim(InputFile) )
        end if
      end if

      ! 蛻晄悄蛟､繝��繧ｿ繧貞�蜉帙☆繧句�ｴ蜷医∫ｵ碁℃譎る俣繧� 0 縺ｫ險ｭ螳�
      ! Set 0 to elapsed time, when initial data is input
      !
      if ( flag_rst == 0 ) then
        call TimesetSetTimeN( 0.0_DP )
      end if

      ! 證ｦ縲�幕蟋区律譎ゅ∫ｵ碁℃譎ょ綾縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ縺ｨ荳頑嶌縺崎ｨｭ螳�
      ! Input and overwrite calendar, start date, and elapsed time
      !
      if ( flag_rst /= 0 ) then

        ! 證ｦ縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ
        ! Input calendar
        !
        call HistoryGetAttr( &
          & InputFile, 'time', 'calendar', &          ! (in)
          & cal_type, &                               ! (out)
          & flag_mpi_split = flag_mpi_init )          ! (in) optional

        call HistoryGetAttr( &
          & InputFile, 'time', 'month_in_year', &     ! (in)
          & month_in_year, &                          ! (out)
          & flag_mpi_split = flag_mpi_init )          ! (in) optional

        allocate( day_in_month_ptr(month_in_year) )
        call HistoryGetAttr( &
          & InputFile, 'time', 'day_in_month', &      ! (in)
          & day_in_month_ptr, &                       ! (out)
          & flag_mpi_split = flag_mpi_init )          ! (in) optional

        call HistoryGetAttr( &
          & InputFile, 'time', 'hour_in_day', &       ! (in)
          & hour_in_day, &                            ! (out)
          & flag_mpi_split = flag_mpi_init )          ! (in) optional

        call HistoryGetAttr( &
          & InputFile, 'time', 'min_in_hour', &       ! (in)
          & min_in_hour, &                            ! (out)
          & flag_mpi_split = flag_mpi_init )          ! (in) optional

        call HistoryGetAttr( &
          & InputFile, 'time', 'sec_in_min', &        ! (in)
          & sec_in_min, &                             ! (out)
          & flag_mpi_split = flag_mpi_init )          ! (in) optional

        ! 證ｦ縺ｮ荳頑嶌縺崎ｨｭ螳�
        ! Overwrite calendar
        !
        call TimesetSetCalendar( cal_type, &       ! (in)
          & month_in_year, day_in_month_ptr, &     ! (in)
          & hour_in_day, min_in_hour, sec_in_min ) ! (in)

        deallocate( day_in_month_ptr )

        ! 髢句ｧ区律譎ゅ�隱ｭ縺ｿ霎ｼ縺ｿ
        ! Input start date
        !
        call HistoryGetAttr( &
          & InputFile, 'time', 'origin_year', &     ! (in)
          & origin_year, &                          ! (out)
          & flag_mpi_split = flag_mpi_init )        ! (in) optional

        call HistoryGetAttr( &
          & InputFile, 'time', 'origin_month', &    ! (in)
          & origin_month, &                         ! (out)
          & flag_mpi_split = flag_mpi_init )        ! (in) optional

        call HistoryGetAttr( &
          & InputFile, 'time', 'origin_day', &      ! (in)
          & origin_day, &                           ! (out)
          & flag_mpi_split = flag_mpi_init )        ! (in) optional

        call HistoryGetAttr( &
          & InputFile, 'time', 'origin_hour', &     ! (in)
          & origin_hour, &                          ! (out)
          & flag_mpi_split = flag_mpi_init )        ! (in) optional

        call HistoryGetAttr( &
          & InputFile, 'time', 'origin_min', &      ! (in)
          & origin_min, &                           ! (out)
          & flag_mpi_split = flag_mpi_init )        ! (in) optional

        call HistoryGetAttr( &
          & InputFile, 'time', 'origin_sec', &      ! (in)
          & origin_sec, &                           ! (out)
          & flag_mpi_split = flag_mpi_init )        ! (in) optional

        ! 髢句ｧ区律譎ゅ�荳頑嶌縺崎ｨｭ螳�
        ! Overwrite start date
        !
        call TimesetSetInitialDate( &
          & origin_year, origin_month, origin_day, & ! (in)
          & origin_hour, origin_min,   origin_sec )  ! (in)

        ! 邨碁℃譎る俣縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ
        ! Input elapsed time
        !
        call HistoryGet( &
          & InputFile, 'time', range = time_range, &  ! (in)
          & array = rst_time, &                       ! (out)
          & flag_mpi_split = flag_mpi_init )          ! (in) optional

        call MessageNotify( 'M', module_name, &
          & '  Specified RestartTime from a NAMELIST file            = <%f>, ', &
          & d = (/ RestartTime /) )
        call MessageNotify( 'M', module_name, &
          & '  Actual initial value of TimeN  from a restart file  = <%f>', &
          & d = (/ rst_time /) )

        ! 邨碁℃譎る俣縺ｮ荳頑嶌縺崎ｨｭ螳�
        ! Overwrite elapsed time
        !
        call TimesetSetTimeN( rst_time )

      end if

      ! 繝��繧ｿ蜈･蜉�
      ! Data input
      !
      if ( flag_rst == 0 ) then
        call HistoryGet( &
          & InputFile, 'U', &                 ! (in)
          & array = xyz_UB, &                 ! (out)
          & flag_mpi_split = flag_mpi_init )  ! (in) optional
        call HistoryGet( &
          & InputFile, 'V', &                 ! (in)
          & array = xyz_VB, &                 ! (out)
          & flag_mpi_split = flag_mpi_init )  ! (in) optional
        call HistoryGet( &
          & InputFile, 'Temp', &              ! (in)
          & array = xyz_TempB, &              ! (out)
          & flag_mpi_split = flag_mpi_init )  ! (in) optional
        do n = 1, ncmax
          call HistoryGet(                           &
            & InputFile, trim(a_QMixName(n)),        &              ! (in)
            & array = xyzf_QMixB(:,:,:,n),           &              ! (out)
            & flag_mpi_split = flag_mpi_init         &              ! (in) optional
            & )
        end do
        call HistoryGet( &
          & InputFile, 'Ps', &                ! (in)
          & array = xy_PsB, &                 ! (out)
          & flag_mpi_split = flag_mpi_init )  ! (in) optional

        call MessageNotify( 'M', module_name, &
          & 'Initial data (not restart data) is input ' // &
          & 'from a data file "%c". ' // &
          & '*B (t-dt) and *N (t) are same.', &
          & c1 = trim(InputFile) )

        xyz_UN     = xyz_UB
        xyz_VN     = xyz_VB
        xyz_TempN  = xyz_TempB
        xyzf_QMixN = xyzf_QMixB
        xy_PsN     = xy_PsB

      else
        call HistoryGet( &
          & InputFile, 'UB', range = time_range, &  ! (in)
          & array = xyz_UB, &                       ! (out)
          & flag_mpi_split = flag_mpi_init )        ! (in) optional
        call HistoryGet( &
          & InputFile, 'VB', range = time_range, &  ! (in)
          & array = xyz_VB, &                       ! (out)
          & flag_mpi_split = flag_mpi_init )        ! (in) optional
        call HistoryGet( &
          & InputFile, 'TempB', range = time_range, &  ! (in)
          & array = xyz_TempB, &                       ! (out)
          & flag_mpi_split = flag_mpi_init )           ! (in) optional
        do n = 1, ncmax
          call HistoryGet(                                             &
            & InputFile, trim(a_QMixName(n))//'B', range = time_range, & ! (in)
            & array = xyzf_QMixB(:,:,:,n),                             & ! (out)
            & flag_mpi_split = flag_mpi_init                           & ! (in) optional
            & )
        end do
        call HistoryGet( &
          & InputFile, 'PsB', range = time_range, &  ! (in)
          & array = xy_PsB, &                        ! (out)
          & flag_mpi_split = flag_mpi_init )         ! (in) optional

        call HistoryGet( &
          & InputFile, 'UN', range = time_range, &  ! (in)
          & array = xyz_UN, &                       ! (out)
          & flag_mpi_split = flag_mpi_init )        ! (in) optional
        call HistoryGet( &
          & InputFile, 'VN', range = time_range, &  ! (in)
          & array = xyz_VN, &                       ! (out)
          & flag_mpi_split = flag_mpi_init )        ! (in) optional
        call HistoryGet( &
          & InputFile, 'TempN', range = time_range, &  ! (in)
          & array = xyz_TempN, &                       ! (out)
          & flag_mpi_split = flag_mpi_init )           ! (in) optional
        do n = 1, ncmax
          call HistoryGet(                                             &
            & InputFile, trim(a_QMixName(n))//'N', range = time_range, & ! (in)
            & array = xyzf_QMixN(:,:,:,n),                             & ! (out)
            & flag_mpi_split = flag_mpi_init                           & ! (in) optional
            & )
        end do
        call HistoryGet( &
          & InputFile, 'PsN', range = time_range, &  ! (in)
          & array = xy_PsN, &                        ! (out)
          & flag_mpi_split = flag_mpi_init )         ! (in) optional

        call MessageNotify( 'M', module_name, &
          & 'Restart data (not initial data) is input ' // &
          & 'from a data file "%c". ', &
          & c1 = trim(InputFile) )
      end if

    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: &
      & RestartTime           ! 繝ｪ繧ｹ繧ｿ繝ｼ繝磯幕蟋区凾蛻ｻ.
                              ! Restart time of calculation

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

    ! 證ｦ縺ｨ譌･譎ゅ�蜿悶ｊ謇ｱ縺�
    ! Calendar and Date handler
    !
    use dc_calendar, only: DCCalConvertByUnit

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

    ! 遞ｮ蛻･蝙九ヱ繝ｩ繝｡繧ｿ
    ! Kind type parameter
    !
    use dc_types, only: STDOUT ! 讓呎ｺ門�蜉帙�陬�ｽｮ逡ｪ蜿ｷ. Unit number of standard output

    ! 蛻晄悄蛟､繝��繧ｿ (繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ) 謠蝉ｾ�
    ! Prepare initial data (restart data)
    !
    use initial_data, only: InitDataInit

    ! 螳｣險譁� ; 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

    ! 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

    ! 繝�ヵ繧ｩ繝ｫ繝亥､縺ｮ險ｭ螳�
    ! Default values settings
    !
    InputFile  = ''
    if ( .not. flag_init_data_save ) then
      OutputFile = 'rst.nc'
    else
      OutputFile = 'init.nc'
    end if
    IntValue   = 365.0d0
    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

    ! 蜃ｺ蜉帶凾髢馴俣髫斐�險ｭ螳�
    ! Configure time interval of output
    !
    IntTime = DCCalConvertByUnit( IntValue, IntUnit, 'sec' ) ! (in)
    PrevOutputTime = RestartTime

    ! 繝輔Λ繧ｰ縺ｮ蛻晄悄蛹�
    ! Initialize flag
    !
    flag_output_end = .false.

    ! Initialization of modules used in this module

    ! 蛻晄悄蛟､繝��繧ｿ (繝ｪ繧ｹ繧ｿ繝ｼ繝医ョ繝ｼ繧ｿ) 謠蝉ｾ�
    ! Prepare initial data (restart data)
    !
    call InitDataInit

    ! 蜊ｰ蟄� ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, 'Input:: ' )
    if ( trim(InputFile) /= '' ) then
      call MessageNotify( 'M', module_name, '  InputFile  = %c', c1 = trim(InputFile) )
    else
      call MessageNotify( 'M', module_name, '  InputFile  = <Non>' )
      call MessageNotify( 'M', module_name, '  ** Initial data is generated in "initial_data" module' )
    end if
    call MessageNotify( 'M', module_name, 'Output:: ' )
    call MessageNotify( 'M', module_name, '  OutputFile = %c', c1 = trim(OutputFile) )
    call MessageNotify( 'M', module_name, '  IntTime    = %f [%c]', d = (/ IntValue /), c1 = trim(IntUnit) )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    restart_file_io_inited = .true.

  end subroutine RestartFileInit

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

end module restart_file_io