!= Module ReStartFileIO
!
! Authors::   SUGIYAMA Ko-ichiro, ODAKA Masatsugu
! Version::   $Id: restartfileio.f90,v 1.17 2014/07/08 00:59:55 sugiyama Exp $ 
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2007. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]


module ReStartFileIO_3d
  !
  !リスタート用の場の情報を netCDF ファイルに出力するためのルーチン
  !

  !モジュール読み込み
  use gtool_history
  use dc_types,  only : DP, STRING
  use dc_message,only : MessageNotify
  use dc_string, only : toChar
  use dc_iounit, only : FileOpen
  use mpi_wrapper,   only: myrank, FLAG_LIB_MPI
  use axesset,  only: x_X,        &!X 座標軸(スカラー格子点)
    &                 y_Y,        &!Y 座標軸(スカラー格子点)
    &                 z_Z          !Z 座標軸(スカラー格子点)
  use gridset,  only: &
    &                 imin,           &!X 方向の配列の下限
    &                 imax,           &!X 方向の配列の上限
    &                 jmin,           &!Y 方向の配列の下限
    &                 jmax,           &!Y 方向の配列の上限
    &                 kmin,           &!Z 方向の配列の下限
    &                 kmax,           &!Z 方向の配列の上限
    &                 ncmax              !凝縮成分の数
  use timeset,  only: DelTimeLong, RestartTime 
  use fileset,  only: &
    &                 filetitle,        &!データの表題
    &                 filesource,       &!データを作成する手順
    &                 FileInstitution    !最終変更者・組織
  use basicset_3d, only: basicset_init 
  use namelist_util, only: namelist_filename

  !暗黙の型宣言禁止
  implicit none
  
  !関数を public に指定
  public ReStartFileIO_init
  public ReStartFileio_Finalize
  public ReStartFileio_BasicZ_Get
  public ReStartFileio_Var_Get

  type(GT_HISTORY), save, public   :: rstat
  character(STRING), save, private :: InitialFile = ""
  character(STRING), save, private :: InputFile   = ""
  character(STRING), save, private :: OutputFile  = "output.nc"
  
contains

  subroutine ReStartFileio_Init ( FlagInitData )
    !
    !リスタートファイルの書き出し
    !
   
    !暗黙の型宣言禁止
    implicit none

    !変数定義
    logical, intent(in), optional :: FlagInitData  ! 初期値生成プログラムの場合は .true.

    real(4)            :: SpcID(ncmax)
    integer            :: N, L, M
    integer            :: s    
    integer            :: unit     !装置番号
     
    !NAMELIST から情報を取得
    NAMELIST /restartfileio_nml/ InitialFile, InputFile, OutputFile
    
    call FileOpen(unit, file=namelist_filename, mode='r')
    read(unit, NML=restartfileio_nml)
    close(unit)

    !確認
    if (myrank == 0) then 
      call MessageNotify( "M", &
        & "restartfileioIO_init", "InitialFile  = %c", c1=trim(InitialFile) )
      call MessageNotify( "M", &
        & "restartfileioIO_init", "InputFile  = %c", c1=trim(InputFile) )
      call MessageNotify( "M", &
        & "restartfileioIO_init", "OutputFile = %c", c1=trim(OutputFile) )
    end if

    ! InputFile が空の場合は, 初期値ファイルを使う. 
    if ( InputFile == "" ) then 
      InputFile = InitialFile
    end if

    !初期値生成の場合には, outputfile 名を変更
    if ( present( FlagInitData ) ) then 
      if ( FlagInitData ) then 
        if ( .NOT. InitialFile == "" ) then  ! ファイル名が空ならば
          InputFile  = ""
          OutputFile = InitialFile
        end if
      end if
    end if

    !確認
    if (myrank == 0) then 
      call MessageNotify( "M", &
        & "restartfileioIO_init", "INPUT  = %c", c1=trim(InputFile) )
      call MessageNotify( "M", &
        & "restartfileioIO_init", "OUTPUT = %c", c1=trim(OutputFile) )
    end if

    SpcID = 0.0d0
    do s = 1, ncmax
      SpcID(s) = real( s, 4 )
    end do
    
    N = size(x_X, 1)
    L = size(y_Y, 1)
    M = size(z_Z, 1)
    
    !-------------------------------------------------------------    
    ! ヒストリー作成
    !-------------------------------------------------------------  
    call HistoryCreate(                              &
      & file = Outputfile,                          &
      & title = filetitle,                           &
      & source = filesource,                         &
      & institution = FileInstitution,               &
      & dims=(/'x','y','z','s','t'/),                &
      & dimsizes=(/N, L, M, ncmax, 0/),                 &
      & longnames=(/'X-coordinate',                  &
      &             'Y-coordinate',                  &
      &             'Z-coordinate',                  &
      &             'Species Num ',                  &
      &             'Time        '/),                &
      & units=(/'m  ','m  ','m  ','1  ','sec'/),     &
      & xtypes=(/'double', 'double', 'double', 'double', 'double'/), &
      & flag_mpi_split = FLAG_LIB_MPI,               &
      & origin=0.0, interval=1.0,                    &
      & history=rstat, quiet=.true. )
    
    !-------------------------------------------------------------  
    ! 変数出力
    !-------------------------------------------------------------
    call HistoryPut('x', x_X, rstat )
    call HistoryPut('y', y_Y, rstat )
    call HistoryPut('z', z_Z, rstat )
    call HistoryPut('s', real(SpcID, 4), rstat )

    !無次元圧力の基本場
    call HistoryAddVariable(                             &
      & varname='ExnerBasicZ', dims=(/'x','y','z'/),      &
      & longname='nondimensional pressure', units='1',&
      & xtype='double', history=rstat )
    
    !温位の基本場
    call HistoryAddVariable(                            &
      & varname='PotTempBasicZ', dims=(/'x','y','z'/),    &
      & longname='potential temperature',            &
      & units='K', xtype='double', history=rstat ) 

    !温位の基本場
    call HistoryAddVariable(                            &
      & varname='VPotTempBasicZ', dims=(/'x','y','z'/),    &
      & longname='virtual potential temperature',            &
      & units='K', xtype='double', history=rstat ) 
    
    !密度の基本場
    call HistoryAddVariable(                            &
      & varname='DensBasicZ', dims=(/'x','y','z'/),      &
      & longname='density',                          &
      & units='Kg.m-3', xtype='double', history=rstat )
    
    !音波速度の基本場
    call HistoryAddVariable(                            &
      & varname='VelSoundBasicZ', dims=(/'x','y','z'/),  &
      & longname='sound velocity',                   &
      & units='m.s-2', xtype='double', history=rstat )

    !温度の基本場
    call HistoryAddVariable(                            &
      & varname='TempBasicZ', dims=(/'x','y','z'/),  &
      & longname='Temperature of basic state',       &
      & units='K', xtype='double', history=rstat ) 
    
    !圧力の基本場
    call HistoryAddVariable(                            &
      & varname='PressBasicZ', dims=(/'x','y','z'/),  &
      & longname='Pressure of basic state',             &
      & units='Pa', xtype='double', history=rstat ) 
    
    !水蒸気混合比の基本場
    call HistoryAddVariable(                            &
      & varname='MixRtBasicZ', dims=(/'x','y','z','s'/),  &
      & longname='Mixing ratio of Condensible volatiles',        &
      & units='kg.kg-1', xtype='double', history=rstat ) 
    
    !分子量効果
    call HistoryAddVariable(                         &
      & varname='EffMolWtBasicZ', dims=(/'x','y','z'/),  &
      & longname='Effect of Mole Weight',            &
      & units='1', xtype='double', history=rstat ) 

    !分子量効果
    call HistoryAddVariable(                        &
      & varname='HumBasicZ', dims=(/'x','y','z','s'/),  &
      & longname='Humidity',                        &
      & units='1', xtype='double', history=rstat ) 
    
    !速度
    call HistoryAddVariable(                         &
      & varname='VelX', dims=(/'x','y','z','t'/),        &
      & longname='zonal velocity',                   &
      & units='m.s-1',                                 &
      & xtype='double', history=rstat )
    
    !速度
    call HistoryAddVariable(                         &
      & varname='VelY', dims=(/'x','y','z','t'/),        &
      & longname='meridional velocity',              &
      & units='m.s-1',                                 &
      & xtype='double', history=rstat )
    
    !速度
    call HistoryAddVariable(                         &
      & varname='VelZ', dims=(/'x','y','z','t'/),        &
      & longname='vertical velocity',                &
      & units='m.s-1',                                 &
      & xtype='double', history=rstat )
    
    !無次元圧力
    call HistoryAddVariable(                         &
      & varname='Exner', dims=(/'x','y','z','t'/),       &
      & longname='nondimensional pressure',          &
      & units='1',                                   &
      & xtype='double', history=rstat )
    
    !温位の擾乱
    call HistoryAddVariable(                         &
      & varname='PotTemp', dims=(/'x','y','z','t'/),     &
      & longname='virtual potential temperature',    &
      & units='K',                                   &
      & xtype='double', history=rstat )
    
    !渦粘性係数
    call HistoryAddVariable(                         &
      & varname='Km', dims=(/'x','y','z','t'/),      &
      & longname='Km',                               &
      & units='m2.s-1',                              &
      & xtype='double', history=rstat )
    
    !渦粘性係数
    call HistoryAddVariable(                         &
      & varname='Kh', dims=(/'x','y','z','t'/),      &
      & longname='Kh',                               &
      & units='m2.s-1',                              &
      & xtype='double', history=rstat )

!    !雲密度
!    call HistoryAddVariable(                         &
!      & varname='CDens', dims=(/'x','y','z','t'/),   &
!      & longname='CDens',                            &
!      & units='m2.s-1',                              &
!      & xtype='double', history=rstat )
    
    !混合比
    call HistoryAddVariable(                         &
      & varname='MixRt', dims=(/'x','y','z','s','t'/),&
      & longname='Mixing Ratio',                     &
      & units='kg.kg-1"',                            & 
      & xtype='double', history=rstat )
    
  end subroutine ReStartFileio_Init
  
  
  subroutine ReStartFileIO_Finalize
    !
    !リスタートファイルのクローズ
    !
    
    !モジュール読み込み
    use gtool_history
    
    !暗黙の型宣言禁止
    implicit none
    
    !ファイルを閉じる
    call HistoryClose(rstat, quiet=.true.)
    
  end subroutine ReStartFileIO_Finalize
  

  subroutine ReStartFileio_Var_Get(   &
    & pyz_VelXB,  pyz_VelXN,    & ! (in)
    & xqz_VelYB,  xqz_VelYN,    & ! (in)
    & xyr_VelZB,  xyr_VelZN,    & ! (in)
    & xyz_PotTempB, xyz_PotTempN,   & ! (in)
    & xyz_ExnerB, xyz_ExnerN,   & ! (in)
    & xyza_MixRtB, xyza_MixRtN,   & ! (in)
    & xyz_KmB,    xyz_KmN,      & ! (in)
    & xyz_KhB,    xyz_KhN       & ! (in)
    & )
    !
    !リスタートファイルから情報取得
    !

    !暗黙の型宣言禁止
    implicit none

    !変数定義
    real(DP), intent(out) :: pyz_VelXN &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(out) :: xqz_VelYN &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(out) :: xyr_VelZN &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(out) :: xyz_ExnerN &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(out) :: xyz_PotTempN &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(out) :: xyz_KmN &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(out) :: xyz_KhN &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
!    real(DP), intent(out) :: xyz_CDensN &
!      &                     (imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(out) :: xyza_MixRtN &
      &              (imin:imax,jmin:jmax,kmin:kmax,ncmax)
    real(DP), intent(out) :: pyz_VelXB &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(out) :: xqz_VelYB &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(out) :: xyr_VelZB &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(out) :: xyz_ExnerB &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(out) :: xyz_PotTempB &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(out) :: xyz_KmB &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(out) :: xyz_KhB &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
!    real(DP), intent(out) :: xyz_CDensB &
!      &                     (imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(out) :: xyza_MixRtB &
      &              (imin:imax,jmin:jmax,kmin:kmax,ncmax)

    real(DP)  :: pz_VelXN(imin-1:imax,kmin-1:kmax)
    real(DP)  :: xr_VelZN(imin-1:imax,kmin-1:kmax)
    real(DP)  :: xz_ExnerN(imin-1:imax,kmin-1:kmax)
    real(DP)  :: xz_PotTempN(imin-1:imax,kmin-1:kmax)
    real(DP)  :: xz_KmN(imin-1:imax,kmin-1:kmax)
    real(DP)  :: xz_KhN(imin-1:imax,kmin-1:kmax)
    real(DP)  :: xza_MixRtN(imin-1:imax,kmin-1:kmax,ncmax)
    real(DP)  :: pz_VelXB(imin-1:imax,kmin-1:kmax)
    real(DP)  :: xr_VelZB(imin-1:imax,kmin-1:kmax)
    real(DP)  :: xz_ExnerB(imin-1:imax,kmin-1:kmax)
    real(DP)  :: xz_PotTempB(imin-1:imax,kmin-1:kmax)
    real(DP)  :: xz_KmB(imin-1:imax,kmin-1:kmax)
    real(DP)  :: xz_KhB(imin-1:imax,kmin-1:kmax)
    real(DP)  :: xza_MixRtB(imin-1:imax,kmin-1:kmax,ncmax)

    integer    :: j

    character(STRING)    :: name               !変数名
    character(STRING)    :: TimeN = ""
    character(STRING)    :: TimeB = ""

    !-------------------------------------------------------------
    ! Get a Value from netCDF File
    !-------------------------------------------------------------    
    if (RestartTime /= 0.0d0) then
      timeB = 't=' // toChar( RestartTime - DelTimeLong)
      timeN = 't=' // toChar( RestartTime  )
    else
      timeB = 't=' // toChar( RestartTime )
      timeN = 't=' // toChar( RestartTime )
    end if

!    write(*,*) TimeN, TimeB

    name = "PotTemp"
    call HistoryGet( InputFile, name, xz_PotTempB, range=TimeB, flag_mpi_split = FLAG_LIB_MPI )
    call HistoryGet( InputFile, name, xz_PotTempN, range=TimeN, flag_mpi_split = FLAG_LIB_MPI )

    name = "VelX"
    call HistoryGet( InputFile, name, pz_VelXB, range=TimeB, flag_mpi_split = FLAG_LIB_MPI )
    call HistoryGet( InputFile, name, pz_VelXN, range=TimeN, flag_mpi_split = FLAG_LIB_MPI )

!    name = "VelY"
!    call HistoryGet( InputFile, name, xqz_VelYB, range=TimeB, flag_mpi_split = FLAG_LIB_MPI )
!    call HistoryGet( InputFile, name, xqz_VelYN, range=TimeN, flag_mpi_split = FLAG_LIB_MPI )
    xqz_VelYB = 0.0d0
    xqz_VelYN = 0.0d0

    name = "VelZ"
    call HistoryGet( InputFile, name, xr_VelZB, range=TimeB, flag_mpi_split = FLAG_LIB_MPI )
    call HistoryGet( InputFile, name, xr_VelZN, range=TimeN, flag_mpi_split = FLAG_LIB_MPI )

    name = "Exner"
    call HistoryGet( InputFile, name, xz_ExnerB, range=TimeB, flag_mpi_split = FLAG_LIB_MPI )
    call HistoryGet( InputFile, name, xz_ExnerN, range=TimeN, flag_mpi_split = FLAG_LIB_MPI )

    name = "Km"
    call HistoryGet( InputFile, name, xz_KmB, range=TimeB, flag_mpi_split = FLAG_LIB_MPI )
    call HistoryGet( InputFile, name, xz_KmN, range=TimeN, flag_mpi_split = FLAG_LIB_MPI )
    
    name = "Kh"
    call HistoryGet( InputFile, name, xz_KhB, range=TimeB, flag_mpi_split = FLAG_LIB_MPI )
    call HistoryGet( InputFile, name, xz_KhN, range=TimeN, flag_mpi_split = FLAG_LIB_MPI )

!    name = "CDens"
!    call HistoryGet( InputFile, name, xyz_CDensB, range=TimeB, flag_mpi_split = FLAG_LIB_MPI )
!    call HistoryGet( InputFile, name, xyz_CDensN, range=TimeN, flag_mpi_split = FLAG_LIB_MPI )
      
    name = "MixRt"
    call HistoryGet( InputFile, name, xza_MixRtB, range=TimeB, flag_mpi_split = FLAG_LIB_MPI )
    call HistoryGet( InputFile, name, xza_MixRtN, range=TimeN, flag_mpi_split = FLAG_LIB_MPI )


    do j = jmin, jmax
      xyz_PotTempB(imin:imax,j,kmin:kmax) = xz_PotTempB(imin:imax,kmin:kmax)
      xyz_PotTempN(imin:imax,j,kmin:kmax) = xz_PotTempN(imin:imax,kmin:kmax)
      pyz_VelXB(imin:imax,j,kmin:kmax) = pz_VelXB(imin:imax,kmin:kmax)
      pyz_VelXN(imin:imax,j,kmin:kmax) = pz_VelXN(imin:imax,kmin:kmax)
      xyr_VelZB(imin:imax,j,kmin:kmax) = xr_VelZB(imin:imax,kmin:kmax)
      xyr_VelZN(imin:imax,j,kmin:kmax) = xr_VelZN(imin:imax,kmin:kmax)
      xyz_ExnerB(imin:imax,j,kmin:kmax) = xz_ExnerB(imin:imax,kmin:kmax)
      xyz_ExnerN(imin:imax,j,kmin:kmax) = xz_ExnerN(imin:imax,kmin:kmax)
      xyz_KmB(imin:imax,j,kmin:kmax) = xz_KmB(imin:imax,kmin:kmax)
      xyz_KmN(imin:imax,j,kmin:kmax) = xz_KmN(imin:imax,kmin:kmax)
      xyz_KhB(imin:imax,j,kmin:kmax) = xz_KhB(imin:imax,kmin:kmax)
      xyz_KhN(imin:imax,j,kmin:kmax) = xz_KhN(imin:imax,kmin:kmax)
      xyza_MixRtB(imin:imax,j,kmin:kmax,:) = xza_MixRtB(imin:imax,kmin:kmax,:)
      xyza_MixRtN(imin:imax,j,kmin:kmax,:) = xza_MixRtN(imin:imax,kmin:kmax,:)
    end do

    write(*,*) "VelX", minval(pyz_VelXB), maxval(pyz_VelXB)
    write(*,*) "VelY", minval(xqz_VelYB), maxval(xqz_VelYB)
    write(*,*) "VelZ", minval(xyr_VelZB), maxval(xyr_VelZB)
    write(*,*) "Exner", minval(xyz_ExnerB), maxval(xyz_ExnerB)
    write(*,*) "PTemp", minval(xyz_PotTempB), maxval(xyz_PotTempB)
    write(*,*) "MixRt", minval(xyza_MixRtB), maxval(xyza_MixRtB)
    write(*,*) "Km", minval(xyz_KmB), maxval(xyz_KmB)
    write(*,*) "Kh", minval(xyz_KhB), maxval(xyz_KhB)
    
    write(*,*) "VelX", minval(pyz_VelXN), maxval(pyz_VelXN)
    write(*,*) "VelY", minval(xqz_VelYN), maxval(xqz_VelYN)
    write(*,*) "VelZ", minval(xyr_VelZN), maxval(xyr_VelZN)
    write(*,*) "Exner", minval(xyz_ExnerN), maxval(xyz_ExnerN)
    write(*,*) "PTemp", minval(xyz_PotTempN), maxval(xyz_PotTempN)
    write(*,*) "MixRt", minval(xyza_MixRtN), maxval(xyza_MixRtN)
    write(*,*) "Km", minval(xyz_KmN), maxval(xyz_KmN)
    write(*,*) "Kh", minval(xyz_KhN), maxval(xyz_KhN)
    
  end subroutine ReStartFileio_Var_Get
       


  subroutine ReStartFileio_BasicZ_Get()
    !
    !リスタートファイルから情報取得
    !

    !暗黙の型宣言禁止
    implicit none

    !変数定義
    real(DP)              :: Var2D &
      &                     (imin-1:imax,kmin-1:kmax)
    real(DP)              :: Var3D &
      &                     (imin-1:imax,kmin-1:kmax,ncmax)
    real(DP)              :: xyz_ExnerBasicZ &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
                                               !基本場のエクスナー関数
    real(DP)              :: xyz_DensBasicZ &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
                                               !基本場の密度
    real(DP)              :: xyz_PotTempBasicZ &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
                                               !基本場の温位
    real(DP)              :: xyz_VelSoundBasicZ &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
                                               !基本場の音速
    real(DP)              :: xyz_PressBasicZ &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
                                               !基本場の圧力
    real(DP)              :: xyz_TempBasicZ &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
                                               !基本場の温度
    real(DP)              :: xyza_MixRtBasicZ &
      &                     (imin:imax,jmin:jmax,kmin:kmax, ncmax)
                                               !基本場の混合比
    real(DP)              :: xyz_EffMolWtBasicZ &
      &                     (imin:imax,jmin:jmax,kmin:kmax)
                                               !基本場の分子量効果
    integer              :: j
    character(STRING)    :: name               !変数名

    !-------------------------------------------------------------
    ! 基本場の取得
    !
    name = "DensBasicZ"
    call HistoryGet( InputFile, name, Var2D, flag_mpi_split = FLAG_LIB_MPI )
    do j = jmin, jmax
      xyz_DensBasicZ(imin:imax,j,kmin:kmax) = Var2D(imin:imax,kmin:kmax)
    end do

    name = "ExnerBasicZ"
    call HistoryGet( InputFile, name, Var2D, flag_mpi_split = FLAG_LIB_MPI )
    do j = jmin, jmax
      xyz_ExnerBasicZ(imin:imax,j,kmin:kmax) = Var2D(imin:imax,kmin:kmax)
    end do
    
    name = "PotTempBasicZ"
    call HistoryGet( InputFile, name, Var2D, flag_mpi_split = FLAG_LIB_MPI )
    do j = jmin, jmax
      xyz_PotTempBasicZ(imin:imax,j,kmin:kmax) = Var2D(imin:imax,kmin:kmax)
    end do
    
    name = "VelSoundBasicZ"
    call HistoryGet( InputFile, name, Var2D, flag_mpi_split = FLAG_LIB_MPI )
    do j = jmin, jmax
      xyz_VelSoundBasicZ(imin:imax,j,kmin:kmax) = Var2D(imin:imax,kmin:kmax)
    end do

    name = "TempBasicZ"
    call HistoryGet( InputFile, name, Var2D, flag_mpi_split = FLAG_LIB_MPI )
    do j = jmin, jmax
      xyz_TempBasicZ(imin:imax,j,kmin:kmax) = Var2D(imin:imax,kmin:kmax)
    end do
    
    name = "PressBasicZ"
    call HistoryGet( InputFile, name, Var2D, flag_mpi_split = FLAG_LIB_MPI )
    do j = jmin, jmax
      xyz_PressBasicZ(imin:imax,j,kmin:kmax) = Var2D(imin:imax,kmin:kmax)
    end do
    
    name = "MixRtBasicZ"
    call HistoryGet( InputFile, name, Var3D, flag_mpi_split = FLAG_LIB_MPI )
    do j = jmin, jmax
      xyza_MixRtBasicZ(imin:imax,j,kmin:kmax,:) = Var3D(imin:imax,kmin:kmax,:)
    end do
    
    name = "EffMolWtBasicZ"
    call HistoryGet( InputFile, name, Var2D, flag_mpi_split = FLAG_LIB_MPI )
    do j = jmin, jmax
      xyz_EffMolWtBasicZ(imin:imax,j,kmin:kmax) = Var2D(imin:imax,kmin:kmax)
    end do

    write(*,*) "PressBZ", minval(xyz_PressBasicZ), maxval(xyz_PressBasicZ)
    write(*,*) "ExnerBZ", minval(xyz_ExnerBasicZ), maxval(xyz_ExnerBasicZ)
    write(*,*) "TempBZ",  minval(xyz_TempBasicZ), maxval(xyz_TempBasicZ)
    write(*,*) "PTempBZ", minval(xyz_PotTempBasicZ), maxval(xyz_PotTempBasicZ)
    write(*,*) "DensBZ",  minval(xyz_DensBasicZ), maxval(xyz_DensBasicZ)
    write(*,*) "VelSnd",  minval(xyz_VelSoundBasicZ), maxval(xyz_VelSoundBasicZ)
    write(*,*) "MixRtBZ", minval(xyza_MixRtBasicZ), maxval(xyza_MixRtBasicZ)
    write(*,*) "EffMolWtBZ", minval(xyz_EffMolWtBasicZ), maxval(xyz_EffMolWtBasicZ)

    
    !----------------------------------------------------------
    ! BasicSet モジュールに値を設定
    !----------------------------------------------------------
    call basicset_init( &
      & xyz_PressBasicZ,    &
      & xyz_ExnerBasicZ,    &
      & xyz_TempBasicZ,     &
      & xyz_PotTempBasicZ,    &
      & xyz_DensBasicZ,     &
      & xyz_VelSoundBasicZ, &
      & xyza_MixRtBasicZ,    &
      & xyz_EffMolWtBasicZ  &
      & )
    
  end subroutine ReStartFileio_BasicZ_Get
       
end module ReStartFileIO_3d
