!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Program MakeReStartFile
!
!   * Developer: SUGIYAMA Ko-ichiro (sugiyama@gfd-dennou.org)
!   * Version: $Id: makerestartfile.f90,v 1.2.2.2 2005/12/10 04:33:08 kitamo Exp $ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!初期 or リスタート用の場の情報を netCDF ファイルに出力するためのルーチン
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!速度を評価する格子位置と, 軸として入力してある座標値は整合的でないことに注意
!
!== Future Plans
!
                                                                 !=end
subroutine MakeReStartFile( File, Time, &
     & fs_VelX_bl,    sf_VelZ_bl,  ss_Exner_bl,  &
     & ss_PotTemp_bl, ss_DensCloud_bl, ss_Km_bl,    ss_Kh_bl,     &
     & fs_VelX_nl,    sf_VelZ_nl,  ss_Exner_nl,  &
     & ss_PotTemp_nl, ss_DensCloud_nl, ss_Km_nl,    ss_Kh_nl      )
                                                                 !=begin
  !== Dependency
  use gt4_history
  use dc_trace, only: BeginSub, EndSub
  use gridset,  only: s_X, s_Z, DimXMin, DimXMax, DimZMin, DimZMax
  use nameset,  only: exptitle, expsrc, expinst
  use debugset, only: DebugOn
  use basicset, only: ss_CpBasicZ, ss_DensBasicZ, ss_ExnerBasicZ, &
       &                  ss_PotTempBasicZ, ss_VelSoundBasicZ 
                                                                 !=end  
  !== 暗黙の型宣言禁止
  implicit none
                                                                 !=begin
  !== Input
  real(8), intent(in)  :: Time(2)
  real(8), intent(in)  :: fs_VelX_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: sf_VelZ_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_Exner_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_PotTemp_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_DensCloud_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_Km_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_Kh_nl(DimXMin:DimXMax, DimZMin:DimZMax)

  real(8), intent(in)  :: fs_VelX_bl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: sf_VelZ_bl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_Exner_bl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_PotTemp_bl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_DensCloud_bl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_Km_bl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_Kh_bl(DimXMin:DimXMax, DimZMin:DimZMax)

  character(*), intent(in) :: File
                                                                 !=end

  !== Work
  integer   :: N, M

  call BeginSub("MakeReStartFile",   &
&                fmt="%c",         &
&     c1="Initialize output file name and variables.")

  N = size(s_X, 1)
  M = size(s_Z, 1)

  !=== ヒストリー作成
  call HistoryCreate(                                 &
       & file = File,                            &
       & title = exptitle,                            &
       & source = expsrc,                             &
       & institution = expinst,                       &
       & dims=(/'x','z','t'/),                        &
       & dimsizes=(/N, M, 2/),                        &
       & longnames=(/'X-coordinate',                  &
       &             'Z-coordinate',                  &
       &             'Time        '/),                &
       & units=(/'m','m','s'/), origin=0.0,           &
       & interval=0.0 )
  
  !=== 変数出力
  call HistoryPut('x', s_X )
  call HistoryPut('z', s_Z )
  call HistoryPut('t', Time )
  
  !=== 無次元圧力(t=Δt)
  call HistoryAddVariable(                            &
       & varname='Exner_n', dims=(/'x','z'/),         &
       & longname='nondimensional pressure',          &
       & units='1',                                   &
       & xtype='double' )
  !=== 無次元圧力(t=0)
  call HistoryAddVariable(                            &
       & varname='Exner_b', dims=(/'x','z'/),         &
       & longname='nondimensional pressure',          &
       & units='1',                                   &
       & xtype='double' )
  !=== 温位(t=Δt)
  call HistoryAddVariable(                            &
       & varname='PotTemp_n', dims=(/'x','z'/),       &
       & longname='potential temperature',    &
       & units='K',                                   &
       & xtype='double' )
  !=== 温位(t=0)
  call HistoryAddVariable(                            &
       & varname='PotTemp_b', dims=(/'x','z'/),       &
       & longname='potential temperature',    &
       & units='K',                                   &
       & xtype='double' )
  !=== 雲密度(t=Δt)
  call HistoryAddVariable(                            &
       & varname='DensCloud_n', dims=(/'x','z'/),       &
       & longname='density of cloud',    &
       & units='kg/m^3',                                   &
       & xtype='double' )
  !=== 雲密度(t=0)
  call HistoryAddVariable(                            &
       & varname='DensCloud_b', dims=(/'x','z'/),       &
       & longname='density of cloud',    &
       & units='kg/m^3',                                   &
       & xtype='double' )
  !=== 水平速度(t=Δt)
  call HistoryAddVariable(                            &
       & varname='VelX_n', dims=(/'x','z'/),          &
       & longname='zonal velocity',                   &
       & units='m/s',                                 &
       & xtype='double' )
  !=== 水平速度(t=0)
  call HistoryAddVariable(                            &
       & varname='VelX_b', dims=(/'x','z'/),          &
       & longname='zonal velocity',                   &
       & units='m/s',                                 &
       & xtype='double' )
  !=== 鉛直速度(t=Δt)
  call HistoryAddVariable(                            &
       & varname='VelZ_n', dims=(/'x','z'/),          &
       & longname='vertical velocity',                &
       & units='m/s',                                 &
       & xtype='double' )
  !=== 鉛直速度(t=0)
  call HistoryAddVariable(                            &
       & varname='VelZ_b', dims=(/'x','z'/),          &
       & longname='vertical velocity',                &
       & units='m/s',                                 &
       & xtype='double' )
  !=== 渦粘性係数(t=Δt)
  call HistoryAddVariable(                            &
       & varname='Km_n', dims=(/'x','z'/),            &
       & longname='Km',                               &
       & units='1',                                   &
       & xtype='double' )
  !=== 渦粘性係数(t=0)
  call HistoryAddVariable(                            &
       & varname='Km_b', dims=(/'x','z'/),            &
       & longname='Km',                               &
       & units='1',                                   &
       & xtype='double' )
  !=== 渦拡散係数(t=Δt)
  call HistoryAddVariable(                            &
       & varname='Kh_n', dims=(/'x','z'/),            &
       & longname='Kh',                               &
       & units='1',                                   &
       & xtype='double' )
  !=== 渦拡散係数(t=0)
  call HistoryAddVariable(                            &
       & varname='Kh_b', dims=(/'x','z'/),            &
       & longname='Kh',                               &
       & units='1',                                   &
       & xtype='double' )
  !=== 基本場の無次元圧力
  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='CpBasicZ', dims=(/'x','z'/),        &
       & longname='Specific Heat',                    &
       & units='J/K kg', 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 HistoryPut( 'VelX_n',    fs_VelX_nl    )
  call HistoryPut( 'VelZ_n',    sf_VelZ_nl    )
  call HistoryPut( 'Exner_n',   ss_Exner_nl   ) 
  call HistoryPut( 'PotTemp_n', ss_PotTemp_nl )
  call HistoryPut( 'DensCloud_n', ss_DensCloud_nl )
  call HistoryPut( 'Km_n',      ss_Km_nl      )
  call HistoryPut( 'Kh_n',      ss_Kh_nl      )
  
  call HistoryPut( 'VelX_b',    fs_VelX_bl    )
  call HistoryPut( 'VelZ_b',    sf_VelZ_bl    )
  call HistoryPut( 'Exner_b',   ss_Exner_bl   ) 
  call HistoryPut( 'PotTemp_b', ss_PotTemp_bl )
  call HistoryPut( 'DensCloud_b', ss_DensCloud_bl )
  call HistoryPut( 'Km_b',      ss_Km_bl      )
  call HistoryPut( 'Kh_b',      ss_Kh_bl      )
  
  call HistoryPut( 'CpBasicZ',       ss_CpBasicZ       )
  call HistoryPut( 'DensBasicZ',     ss_DensBasicZ     )
  call HistoryPut( 'ExnerBasicZ',    ss_ExnerBasicZ    )
  call HistoryPut( 'PotTempBasicZ',  ss_PotTempBasicZ  )
  call HistoryPut( 'VelSoundBasicZ', ss_VelSoundBasicZ )

  !=== ファイルを閉じる
  call HistoryClose

  
  call EndSub("MakeReStartFile")

end subroutine MakeReStartFile
