!= Module ReStartFileIO
!
! Authors::   SUGIYAMA Ko-ichiro
! Version::   $Id: restartfileio.f90,v 1.11 2006/11/22 15:34:56 sugiyama Exp $ 
! Tag Name::  $Name: arare4-20061224 $
! Copyright:: Copyright (C) GFD Dennou Club, 2006. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
!== Overview 
!
!リスタート用の場の情報を netCDF ファイルに出力するためのルーチン
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!速度を評価する格子位置と, 軸として入力してある座標値は整合的でないことに注意
!
!== Future Plans
!

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

  !モジュール読み込み
  use gt4_history
  use dc_message, only: MessageNotify
  use dc_string
  use gridset,  only: s_X,               &!X 座標軸(スカラー格子点)
    &                 s_Z,               &!Z 座標軸(スカラー格子点)
    &                 DimXMin,           &!X 方向の配列の下限
    &                 DimXMax,           &!X 方向の配列の上限
    &                 DimZMin,           &!Z 方向の配列の下限
    &                 DimZMax,           &!Z 方向の配列の上限
    &                 RegXMin,           &! 物理領域の X 方向の下限
    &                 RegXMax,           &! 物理領域の X 方向の上限
    &                 SpcNum              !凝縮成分の数
  use timeset,  only: DelTimeLong
  use fileset,  only: RestartFile,       &!リスタートファイル名
    &                 InitFile,          &!初期ファイル名
    &                 exptitle,          &!データの表題
    &                 expsrc,            &!データを作成する手順
    &                 expinst             !最終変更者・組織
  use basicset, only: BasicSetArray_Init, &!
    &                 PressBasis,    &!温位の基準圧力
    &                 GasRDry,       &!乾燥成分の定圧比熱
    &                 CpDry,         &!乾燥成分の定圧比熱
    &                 CvDry,         &!乾燥成分の定積比熱
    &                 MolWtDry,      &!乾燥成分の分子量
    &                 Grav,          &!重力加速度
    &                 SpcWetMolFr,   &!凝縮成分の初期モル比
    &                 MolWtWet,      &!凝縮成分の分子量
    &                 GasRUniv        !
  use Boundary, only: xz_BoundaryXCyc_xz,  &!
    &                 xz_BoundaryZSym_xz,  &!  
    &                 xza_BoundaryXCyc_xza,&!
    &                 xza_BoundaryZSym_xza  !  

  !暗黙の型宣言禁止
  implicit none
  
  !属性の指定
  private

  !関数を public に指定
  public ReStartFile_Open
  public ReStartFile_OutPut
  public ReStartFile_Close
  public ReStartFile_Get

contains

  subroutine ReStartFile_Open(  )
    !
    !リスタートファイルの書き出し
    !
   
    use basicset, only: xz_ExnerBasicZ,    &!基本場のエクスナー関数
      &                 xz_DensBasicZ,     &!基本場の密度
      &                 xz_PotTempBasicZ,  &!基本場の温位
      &                 xz_VelSoundBasicZ, &!基本場の音速
      &                 xz_PressBasicZ,    &!基本場の圧力
      &                 xz_TempBasicZ,     &!基本場の温度
      &                 xza_MixRtBasicZ,   &!基本場の混合比
      &                 xz_EffMolWtBasicZ   !基本場の分子量効果


    !暗黙の型宣言禁止
    implicit none
    
    !変数定義
    real(4)            :: SpcID(SpcNum)
    integer            :: N, M
    integer            :: s    

    SpcID = 0.0d0
    do s = 1, SpcNum
      SpcID(s) = real( s, 4 )
    end do
    
    N = size(s_X, 1)
    M = size(s_Z, 1)
    
    !-------------------------------------------------------------    
    ! ヒストリー作成
    !-------------------------------------------------------------  
    call HistoryCreate(                              &
      & file = ReStartFile,                          &
      & title = exptitle,                            &
      & source = expsrc,                             &
      & institution = expinst,                       &
      & dims=(/'x','z','s','t'/),                    &
      & dimsizes=(/N, M, SpcNum, 0/),                &
      & longnames=(/'X-coordinate',                  &
      &             'Z-coordinate',                  &
      &             'Species Num ',                  &
      &             'Time        '/),                &
      & units=(/'m','m','1','t'/), origin=0.0,       &
      & interval=0.0 )
    
    !-------------------------------------------------------------  
    ! 変数出力
    !-------------------------------------------------------------
    call HistoryPut('x', s_X )
    call HistoryPut('z', s_Z )
    call HistoryPut('s', real(SpcID, 4) )
    
    !無次元圧力の基本場
    call HistoryAddVariable(                             &
      & varname='ExnerBasicZ', dims=(/'x','z'/),      &
      & longname='nondimensional pressure', units='1',&
      & xtype='double' )
    
    !温位の基本場
    call HistoryAddVariable(                            &
      & varname='PotTempBasicZ', dims=(/'x','z'/),    &
      & longname='potential temperature',            &
      & units='K', xtype='double' )
    
    !密度の基本場
    call HistoryAddVariable(                            &
      & varname='DensBasicZ', dims=(/'x','z'/),      &
      & longname='density',                          &
      & units='Kg/m^3', xtype='double' )
    
    !音波速度の基本場
    call HistoryAddVariable(                            &
      & varname='VelSoundBasicZ', dims=(/'x','z'/),  &
      & longname='sound velocity',                   &
      & units='m/s|2', xtype='double' )
    
    !温度の基本場
    call HistoryAddVariable(                            &
      & varname='TempBasicZ', dims=(/'x','z'/),  &
      & longname='Temperature of basic state',       &
      & units='K', xtype='double' ) 
    
    !圧力の基本場
    call HistoryAddVariable(                            &
      & varname='PressBasicZ', dims=(/'x','z'/),  &
      & longname='Pressure of basic state',             &
      & units='Pa', xtype='double' ) 
    
    !水蒸気混合比の基本場
    call HistoryAddVariable(                            &
      & varname='MixRtBasicZ', dims=(/'x','z','s'/),  &
      & longname='Mixing ratio of Condensible volatiles',        &
      & units='kg/kg', xtype='double' ) 
    
    !分子量効果
    call HistoryAddVariable(                         &
      & varname='EffMolWtBasicZ', dims=(/'x','z'/),  &
      & longname='Effect of Mole Weight',            &
      & units='1', xtype='double' ) 
    
    !無次元圧力
    call HistoryAddVariable(                         &
      & varname='Exner', dims=(/'x','z','t'/),       &
      & longname='nondimensional pressure',          &
      & units='1',                                   &
      & xtype='double' )
    
    !温位の擾乱
    call HistoryAddVariable(                         &
      & varname='PotTemp', dims=(/'x','z','t'/),     &
      & longname='virtual potential temperature',    &
      & units='K',                                   &
      & xtype='double' )
    
    !速度
    call HistoryAddVariable(                         &
      & varname='VelX', dims=(/'x','z','t'/),        &
      & longname='zonal velocity',                   &
      & units='m/s',                                 &
      & xtype='double' )
    
    !速度
    call HistoryAddVariable(                         &
      & varname='VelZ', dims=(/'x','z','t'/),        &
      & longname='vertical velocity',                &
      & units='m/s',                                 &
      & xtype='double' )
    
    !渦粘性係数
    call HistoryAddVariable(                         &
      & varname='Km', dims=(/'x','z','t'/),          &
      & longname='Km',                               &
      & units='1',                                   &
      & xtype='double' )
    
    !渦粘性係数
    call HistoryAddVariable(                         &
      & varname='Kh', dims=(/'x','z','t'/),          &
      & longname='Kh',                               &
      & units='1',                                   &
      & xtype='double' )
    
    !混合比
    call HistoryAddVariable(                         &
      & varname='MixRt', dims=(/'x','z','s','t'/),   &
      & longname='Mixing Ratio',                     &
      & units='kg kg|-1"',                           & 
      & xtype='double' )
    
    !-------------------------------------------------------------
    ! 基本場のファイル出力
    !-------------------------------------------------------------
    call HistoryPut( 'DensBasicZ',     xz_DensBasicZ     )
    call HistoryPut( 'ExnerBasicZ',    xz_ExnerBasicZ    )
    call HistoryPut( 'PotTempBasicZ',  xz_PotTempBasicZ  )
    call HistoryPut( 'VelSoundBasicZ', xz_VelSoundBasicZ )
    call HistoryPut( 'TempBasicZ',     xz_TempBasicZ     )
    call HistoryPut( 'PressBasicZ',    xz_PressBasicZ    )
    call HistoryPut( 'MixRtBasicZ',    xza_MixRtBasicZ   )
    call HistoryPut( 'EffMolWtBasicZ', xz_EffMolWtBasicZ )
    
  end subroutine ReStartFile_Open
  
  
  subroutine ReStartFile_OutPut(        &
    &   Time, xz_PotTemp, xz_Exner, pz_VelX, xr_VelZ, &
    &   xza_MixRt, xz_Km , xz_Kh &
    & )
    !
    !リスタートファイルに予報変数を書き出す
    !
    
    !暗黙の型宣言禁止
    implicit none
    
    !変数定義
    real(8), intent(in)  :: Time
    real(8), intent(in)  :: pz_VelX(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in)  :: xr_VelZ(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in)  :: xz_Exner(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in)  :: xz_PotTemp(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in)  :: xz_Km(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in)  :: xz_Kh(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(in)  :: xza_MixRt(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
    
  
    !------------------------------------------------------------------
    ! ファイル出力
    !------------------------------------------------------------------
    call HistoryPut( 't',       Time       )
    call HistoryPut( 'VelX',    pz_VelX    )
    call HistoryPut( 'VelZ',    xr_VelZ    )
    call HistoryPut( 'Exner',   xz_Exner   ) 
    call HistoryPut( 'PotTemp', xz_PotTemp )
    call HistoryPut( 'Km',      xz_Km      )
    call HistoryPut( 'Kh',      xz_Kh      )
    call HistoryPut( 'MixRt',   xza_MixRt  )    
    
  end subroutine ReStartFile_OutPut
  
  
  subroutine ReStartFile_Close
    !
    !リスタートファイルのクローズ
    !
    
    !モジュール読み込み
    use gt4_history
    
    !暗黙の型宣言禁止
    implicit none

    !ファイルを閉じる
    call HistoryClose
    
  end subroutine ReStartFile_Close


  subroutine ReStartFile_Get(   &
    & ReStartTime,                            &
    & xz_PotTempB, xz_ExnerB, pz_VelXB, xr_VelZB, xza_MixRtB, xz_KmB, xz_KhB, &
    & xz_PotTempN, xz_ExnerN, pz_VelXN, xr_VelZN, xza_MixRtN, xz_KmN, xz_KhN  )
    !
    !リスタートファイルから情報取得
    !

    !暗黙の型宣言禁止
    implicit none

    !変数定義
    real(8), intent(out) :: ReStartTime(2)
    real(8), intent(out) :: pz_VelXN(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xr_VelZN(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xz_ExnerN(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xz_PotTempN(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xz_KmN(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xz_KhN(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xza_MixRtN(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
    real(8), intent(out) :: pz_VelXB(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xr_VelZB(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xz_ExnerB(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xz_PotTempB(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xz_KmB(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xz_KhB(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8), intent(out) :: xza_MixRtB(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
    real(8)              :: DelTime
    real(8)              :: Var2D(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8)              :: Var3D(DimXMin:DimXMax, DimZMin:DimZMax, 2)
    real(8)              :: Var3Ds(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
    real(8)              :: Var4D(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum, 2)
    real(8)              :: xz_ExnerBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
                                               !基本場のエクスナー関数
    real(8)              :: xz_DensBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
                                               !基本場の密度
    real(8)              :: xz_PotTempBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
                                               !基本場の温位
    real(8)              :: xz_VelSoundBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
                                               !基本場の音速
    real(8)              :: xz_PressBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
                                               !基本場の圧力
    real(8)              :: xz_TempBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
                                               !基本場の温度
    real(8)              :: xza_MixRtBasicZ(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
                                               !基本場の混合比
    real(8)              :: xz_EffMolWtBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
                                               !基本場の分子量効果
    character(30)        :: name               !変数名
    character(10)        :: step(2)
    integer              :: t
    logical              :: ResetSW

!    ResetSW = .true.
    ResetSW = .false.


    !-------------------------------------------------------------
    ! Get a Value from netCDF File
    !-------------------------------------------------------------    
    do t = 1, 2
      step(t) = '^' // adjustl(toChar(t))   
    end do

    do t = 1, 2
      name = "t"
      call HistoryGet( InitFile, name, ReStartTime(t), step(t) )
    end do

    do t = 1, 2
      name = "VelX"
      call HistoryGet( InitFile, name, Var3D(:,:,t), step(t) )    
    end do
    pz_VelXB = Var3D(:,:,1)
    pz_VelXN = Var3D(:,:,2)

    do t = 1, 2    
      name = "VelZ"
      call HistoryGet( InitFile, name, Var3D(:,:,t), step(t) )    
    end do
    xr_VelZB = Var3D(:,:,1)
    xr_VelZN = Var3D(:,:,2)

    do t = 1, 2    
      name = "Exner"
      call HistoryGet( InitFile, name, Var3D(:,:,t), step(t) )    
    end do
    xz_ExnerB = Var3D(:,:,1)
    xz_ExnerN = Var3D(:,:,2)

    do t = 1, 2    
      name = "PotTemp"
      call HistoryGet( InitFile, name, Var3D(:,:,t), step(t) )    
    end do
    xz_PotTempB = Var3D(:,:,1)
    xz_PotTempN = Var3D(:,:,2)
    
    do t = 1, 2  
      name = "Km"
      call HistoryGet( InitFile, name, Var3D(:,:,t), step(t) )    
    end do
    xz_KmB = Var3D(:,:,1)
    xz_KmN = Var3D(:,:,2)
    
    do t = 1, 2  
      name = "Kh"
      call HistoryGet( InitFile, name, Var3D(:,:,t), step(t) )    
    end do
    xz_KhB = Var3D(:,:,1)
    xz_KhN = Var3D(:,:,2)
      
    do t = 1, 2
      name = "MixRt"
      call HistoryGet( InitFile, name, Var4D(:,:,:,t), step(t) )    
    end do
    xza_MixRtB = Var4D(:,:,:,1)
    xza_MixRtN = Var4D(:,:,:,2)
    
        
    !-------------------------------------------------------------
    ! 基本場の取得
    !-------------------------------------------------------------
    name = "DensBasicZ"
    call HistoryGet( InitFile, name, Var2D )
    xz_DensBasicZ = Var2D
    
    name = "ExnerBasicZ"
    call HistoryGet( InitFile, name, Var2D )
    xz_ExnerBasicZ = Var2D
    
    name = "PotTempBasicZ"
    call HistoryGet( InitFile, name, Var2D )
    xz_PotTempBasicZ = Var2D
    
    name = "VelSoundBasicZ"
    call HistoryGet( InitFile, name, Var2D )
    xz_VelSoundBasicZ = Var2D
    
    name = "TempBasicZ"
    call HistoryGet( InitFile, name, Var2D )
    xz_TempBasicZ = Var2D
    
    name = "PressBasicZ"
    call HistoryGet( InitFile, name, Var2D )
    xz_PressBasicZ = Var2D
    
    name = "MixRtBasicZ"
    call HistoryGet( InitFile, name, Var3Ds )
    xza_MixRtBasicZ = Var3Ds
    
    name = "EffMolWtBasicZ"
    call HistoryGet( InitFile, name, Var2D )
    xz_EffMolWtBasicZ = Var2D

!    if (ResetSW) then 
!
!      call ReStartFile_ResetBasicZ(                             &
!        & xz_PotTempN, xz_ExnerN, xza_MixRtN,                   &
!        & xz_PressBasicZ,    xz_ExnerBasicZ, xz_TempBasicZ,     &
!        & xz_PotTempBasicZ,  xz_DensBasicZ,  xz_VelSoundBasicZ, &
!        & xza_MixRtBasicZ, xz_EffMolWtBasicZ )
!      
!    end if

    !----------------------------------------------------------
    ! 時間刻みのチェック
    !----------------------------------------------------------
    DelTime = ReStartTime(2) - ReStartTime(1)
    if ( DelTime /= real(DelTimeLong, 4) ) then 
      call MessageNotify( "W", &
        & "ReStartFile_Get", "DelTime in InitFile is not the same as DelTimeLong")
      call MessageNotify( "M", &
        & "ReStartFile_Get", "DelTime=%d", d=(/DelTime/) )
      call MessageNotify( "M", &
        & "ReStartFile_Get", "DelTime=Long%d", d=(/DelTimeLong/) )
    end if
    
    !----------------------------------------------------------
    ! BasicSet モジュールに値を設定
    !----------------------------------------------------------
    call BasicSetArray_Init(                                  &
      & xz_PressBasicZ,    xz_ExnerBasicZ, xz_TempBasicZ,     &
      & xz_PotTempBasicZ,  xz_DensBasicZ,  xz_VelSoundBasicZ, &
      & xza_MixRtBasicZ, xz_EffMolWtBasicZ )
    
  end subroutine ReStartFile_Get


!  subroutine ReStartFile_ResetBasicZ(                           &
!    & xz_PotTemp, xz_Exner, xza_MixRt,                   &
!    & xz_PressBasicZ,    xz_ExnerBasicZ, xz_TempBasicZ,     &
!    & xz_PotTempBasicZ,  xz_DensBasicZ,  xz_VelSoundBasicZ, &
!    & xza_MixRtBasicZ, xz_EffMolWtBasicZ )
!    
!    real(8), intent(inout) :: xz_PotTemp(DimXMin:DimXMax, DimZMin:DimZMax)
!    real(8), intent(inout) :: xz_Exner(DimXMin:DimXMax, DimZMin:DimZMax)
!    real(8), intent(inout) :: xza_MixRt(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
!    real(8), intent(inout) :: xz_ExnerBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
!                                               !基本場のエクスナー関数
!    real(8), intent(inout) :: xz_DensBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
!                                               !基本場の密度
!    real(8), intent(inout) :: xz_PotTempBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
!                                               !基本場の温位
!    real(8), intent(inout) :: xz_VelSoundBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
!                                               !基本場の音速
!    real(8), intent(inout) :: xz_PressBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
!                                               !基本場の圧力
!    real(8), intent(inout) :: xz_TempBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
!                                               !基本場の温度
!    real(8), intent(inout) :: xza_MixRtBasicZ(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
!                                               !基本場の混合比
!    real(8), intent(inout) :: xz_EffMolWtBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
!                                               !基本場の分子量効果
!    real(8) :: xz_PotTempAll(DimXMin:DimXMax, DimZMin:DimZMax)
!    real(8) :: xz_ExnerAll(DimXMin:DimXMax, DimZMin:DimZMax)
!    real(8) :: xz_TempAll(DimXMin:DimXMax, DimZMin:DimZMax)
!    real(8) :: xz_PressAll(DimXMin:DimXMax, DimZMin:DimZMax)
!    real(8) :: xza_MixRtAll(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
!    real(8) :: xza_MixRtDivMolWt(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
!    integer :: s
!
!
!    !---------------------------------------------------------------
!    ! 全量の計算
!    !---------------------------------------------------------------
!    xz_PotTempAll = xz_PotTempBasicZ + xz_PotTemp
!    xz_ExnerAll   = xz_ExnerBasicZ   + xz_Exner
!    xz_TempAll    = xz_PotTempAll * xz_ExnerAll 
!    xz_PressAll   = PressBasis * (xz_ExnerAll ** (CpDry / GasRDry))
!    xza_MixRtAll  = xza_MixRtBasicZ + xza_MixRt
!
!    !---------------------------------------------------------------
!    ! 温度
!    !---------------------------------------------------------------
!    xz_TempBasicZ   = aa_MeanX_aa( xz_TempAll )
!
!    !境界条件  
!    xz_TempBasicZ  = xz_BoundaryXCyc_xz( xz_TempBasicZ )
!    xz_TempBasicZ  = xz_BoundaryZSym_xz( xz_TempBasicZ )
!    
!    !---------------------------------------------------------------
!    ! 圧力
!    !---------------------------------------------------------------
!    xz_PressBasicZ  = aa_MeanX_aa( xz_PressAll )
!
!    !境界条件
!    xz_PressBasicZ = xz_BoundaryXCyc_xz( xz_PressBasicZ )
!    xz_PressBasicZ = xz_BoundaryZSym_xz( xz_PressBasicZ )
!    
!    !---------------------------------------------------------------
!    ! 混合比
!    !---------------------------------------------------------------
!    do s = 1, SpcNum
!      if ( maxval( xza_MixRtBasicZ(:,:,s) ) > 0.0d0) then 
!        xza_MixRtBasicZ(:,:,s) = aa_MeanX_aa( xza_MixRtAll(:,:,s) )
!        xza_MixRt(:,:,s)       = xza_MixRtAll(:,:,s) - xza_MixRtBasicZ(:,:,s)
!      end if
!    end do
!    
!    !境界条件
!    xza_MixRtBasicZ = xza_BoundaryXCyc_xza( xza_MixRtBasicZ )
!    xza_MixRtBasicZ = xza_BoundaryZSym_xza( xza_MixRtBasicZ )
!    xza_MixRt       = xza_BoundaryXCyc_xza( xza_MixRt       )
!    xza_MixRt       = xza_BoundaryZSym_xza( xza_MixRt       )
!    
!    !---------------------------------------------------------------    
!    ! 温位
!    !---------------------------------------------------------------
!    xz_PotTempBasicZ = &
!      & xz_TempBasicZ * (PressBasis / xz_PressBasicZ) ** (GasRDry / CpDry) 
!    xz_PotTemp = xz_PotTempAll - xz_PotTempBasicZ
!    
!    !境界条件  
!    xz_PotTempBasicZ = xz_BoundaryXCyc_xz( xz_PotTempBasicZ )
!    xz_PotTempBasicZ = xz_BoundaryZSym_xz( xz_PotTempBasicZ )
!    xz_PotTemp       = xz_BoundaryXCyc_xz( xz_PotTemp )
!    xz_PotTemp       = xz_BoundaryZSym_xz( xz_PotTemp )
!
!    !---------------------------------------------------------------    
!    ! エクスナー関数
!    !---------------------------------------------------------------
!    xz_ExnerBasicZ = xz_TempBasicZ / xz_PotTempBasicZ    
!    xz_Exner       = xz_ExnerAll - xz_ExnerBasicZ
!    
!    !境界条件
!    xz_ExnerBasicZ = xz_BoundaryXCyc_xz( xz_ExnerBasicZ )
!    xz_ExnerBasicZ = xz_BoundaryZSym_xz( xz_ExnerBasicZ )
!    xz_Exner       = xz_BoundaryXCyc_xz( xz_Exner )
!    xz_Exner       = xz_BoundaryZSym_xz( xz_Exner )
!  
!    !---------------------------------------------------------------
!    ! 分子量の効果
!    !---------------------------------------------------------------
!    do s = 1, SpcNum
!      xza_MixRtDivMolWt(:,:,s) = xza_MixRtBasicZ(:,:,s) / MolWtWet(s)
!    end do
!    
!    xz_EffMolWtBasicZ = &
!      & 1.0d0 -  sum(xza_MixRtDivMolWt,3) &
!      &            / ((1.0d0 / MolWtDry) + sum(xza_MixRtDivMolWt,3))  &
!      & * (1.0d0 + sum(xza_MixRtBasicZ,3) )
!    
!    !境界条件  
!    xz_EffMolWtBasicZ = xz_BoundaryXCyc_xz( xz_EffMolWtBasicZ )
!    xz_EffMolWtBasicZ = xz_BoundaryZSym_xz( xz_EffMolWtBasicZ )
!    
!    !---------------------------------------------------------------    
!    ! 密度
!    !---------------------------------------------------------------
!    xz_DensBasicZ = &
!      & PressBasis * (xz_ExnerBasicZ ** (CvDry / GasRDry)) &
!      &  / (GasRDry * xz_PotTempBasicZ / xz_EffMolWtBasicZ)
!    
!    !境界条件
!    xz_DensBasicZ = xz_BoundaryXCyc_xz( xz_DensBasicZ )
!    xz_DensBasicZ = xz_BoundaryZSym_xz( xz_DensBasicZ )
!    
!    !---------------------------------------------------------------    
!    ! 音速
!    !---------------------------------------------------------------
!    xz_VelSoundBasicZ = &
!      & sqrt ( &
!      &   CpDry * GasRDry * xz_ExnerBasicZ * xz_PotTempBasicZ &
!      &   / (CvDry * xz_EffMolWtBasicZ) &
!      & )
!    
!    !境界条件
!    xz_VelSoundBasicZ = xz_BoundaryXCyc_xz( xz_VelSoundBasicZ )
!    xz_VelSoundBasicZ = xz_BoundaryZSym_xz( xz_VelSoundBasicZ )
!      
!  end subroutine ReStartFile_ResetBasicZ

!!!---------------------------------------------------------------------  
!  function aa_MeanX_aa( var ) 
!    !
!    ! 水平平均値の計算
!    !
!    
!    !暗黙の型宣言禁止
!    implicit none
!    
!    !変数定義
!    real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
!                                     !平均演算の対象となる変数
!    real(8)              :: aa_MeanX_aa(DimXMin:DimXMax, DimZMin:DimZMax)
!                                     !水平平均値
!    integer              :: i
!
!    do i = DimXMin, DimXMax
!      aa_MeanX_aa(i,:) = sum( var(RegXMin+1:RegXMax, :), 1 ) &
!        &                 / real(RegXMax - RegXMin, 8) 
!    end do
!
!  end function aa_MeanX_aa
  

!!!---------------------------------------------------------------------  
!  function aaa_MeanX_aaa( var ) 
!    !
!    ! 水平平均値の計算
!    !
!    
!    !暗黙の型宣言禁止
!    implicit none
!    
!    !変数定義
!    real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
!                                     !平均演算の対象となる変数
!    real(8)              :: aaa_MeanX_aaa(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
!                                     !水平平均値
!    integer              :: i
!    
!    do i = DimXMin, DimXMax
!      aaa_MeanX_aaa(i,:,:) = sum( var(RegXMin+1:RegXMax,:,:), 1 ) &
!        &                     / real(RegXMax - RegXMin, 8) 
!    end do
!    
!  end function aaa_MeanX_aaa
  
       
end module ReStartFileIO
