!---------------------------------------------------------------------
!     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.4.1 2006/01/13 06:55:04 sugiyama 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_Km_bl,    ss_Kh_bl,     &
     & ss_MixRtSat_bl, ss_MixRtV_bl,       &
     & ss_MixRtC_bl, ss_MixRtR_bl, &
     & fs_VelX_nl,    sf_VelZ_nl,  ss_Exner_nl,  &
     & ss_PotTemp_nl, ss_Km_nl,    ss_Kh_nl,     &
     & ss_MixRtV_nl, ss_MixRtC_nl, ss_MixRtR_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, &
       &                  ss_PressBasicZ,   ss_TempBasicZ,     &
       &                  ss_MixRtVBasicZ, ss_EffMolWtBasicZ
  use average,  only: ss_avr_fs, ss_avr_sf
  use arareset !TEST
                                                                 !=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_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_Km_bl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_Kh_bl(DimXMin:DimXMax, DimZMin:DimZMax)

  real(8), intent(in)  :: ss_MixRtSat_bl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_MixRtV_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_MixRtV_bl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_MixRtC_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_MixRtC_bl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_MixRtR_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_MixRtR_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 )
  
  !=== 無次元圧力
  call HistoryAddVariable(                            &
       & varname='Exner_n', dims=(/'x','z'/),         &
       & longname='nondimensional pressure',          &
       & units='1',                                   &
       & xtype='double' )
  !=== 無次元圧力
  call HistoryAddVariable(                            &
       & varname='Exner_b', dims=(/'x','z'/),         &
       & longname='nondimensional pressure',          &
       & units='1',                                   &
       & xtype='double' )
  !=== 仮温位
  call HistoryAddVariable(                            &
       & varname='PotTemp_n', dims=(/'x','z'/),       &
       & longname='virtual potential temperature',    &
       & units='K',                                   &
       & xtype='double' )
  !=== 仮温位
  call HistoryAddVariable(                            &
       & varname='PotTemp_b', dims=(/'x','z'/),       &
       & longname='virtual potential temperature',    &
       & units='K',                                   &
       & xtype='double' )
  !=== 速度
  call HistoryAddVariable(                            &
       & varname='VelX_n', dims=(/'x','z'/),          &
       & longname='zonal velocity',                   &
       & units='m/s',                                 &
       & xtype='double' )
  !=== 速度
  call HistoryAddVariable(                            &
       & varname='VelX_b', dims=(/'x','z'/),          &
       & longname='zonal velocity',                   &
       & units='m/s',                                 &
       & xtype='double' )
  !=== 速度
  call HistoryAddVariable(                            &
       & varname='VelZ_n', dims=(/'x','z'/),          &
       & longname='vertical velocity',                &
       & units='m/s',                                 &
       & xtype='double' )
  !=== 速度
  call HistoryAddVariable(                            &
       & varname='VelZ_b', dims=(/'x','z'/),          &
       & longname='vertical velocity',                &
       & units='m/s',                                 &
       & xtype='double' )
  !=== 渦粘性係数
  call HistoryAddVariable(                            &
       & varname='Km_n', dims=(/'x','z'/),            &
       & longname='Km',                               &
       & units='1',                                   &
       & xtype='double' )
  !=== 渦粘性係数
  call HistoryAddVariable(                            &
       & varname='Km_b', dims=(/'x','z'/),            &
       & longname='Km',                               &
       & units='1',                                   &
       & xtype='double' )
  !=== 渦拡散係数
  call HistoryAddVariable(                            &
       & varname='Kh_n', dims=(/'x','z'/),            &
       & longname='Kh',                               &
       & units='1',                                   &
       & xtype='double' )
  !=== 渦拡散係数
  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 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='MixRtVBasicZ', dims=(/'x','z'/),  &
       & longname='Mixing ratio of water vaper ',        &
       & units='kg/kg', xtype='double' ) 
  !=== 分子量効果
  call HistoryAddVariable(                            &
       & varname='EffMolWtBasicZ', dims=(/'x','z'/),  &
       & longname='Effect of Mole Weight',            &
       & units='1', xtype='double' ) 

  !=== 飽和水蒸気混合比
  call HistoryAddVariable(                             &
       & varname='MixRtSat', dims=(/'x','z'/),&
       & longname='Mixing Ratio of Water Vaper',       &
       & units='1',                                    & 
       & xtype='double' )
  !=== 水蒸気混合比
  call HistoryAddVariable(                             &
       & varname='MixRtV_n', dims=(/'x','z'/),&
       & longname='Mixing Ratio of Water Vaper',       &
       & units='1',                                    & 
       & xtype='double' )
  !=== 水蒸気混合比
  call HistoryAddVariable(                             &
       & varname='MixRtV_b', dims=(/'x','z'/),&
       & longname='Mixing Ratio of Water Vaper',       &
       & units='1',                                    & 
       & xtype='double' )
  !=== 雲粒混合比
  call HistoryAddVariable(                             &
       & varname='MixRtC_n', dims=(/'x','z'/),&
       & longname='Mixing Ratio of Water Cloud',       &
       & units='1',                                    & 
       & xtype='double' )
  !=== 雲粒混合比
  call HistoryAddVariable(                             &
       & varname='MixRtC_b', dims=(/'x','z'/),&
       & longname='Mixing Ratio of Water Cloud',      &
       & units='1',                                    & 
       & xtype='double' )
  !=== 雨粒混合比
  call HistoryAddVariable(                             &
       & varname='MixRtR_n', dims=(/'x','z'/), &
       & longname='Mixing Ratio of Water Rain',        &
       & units='1',                                    & 
       & xtype='double' )
  !=== 雨粒混合比
  call HistoryAddVariable(                             &
       & varname='MixRtR_b', dims=(/'x','z'/), &
       & longname='Mixing Ratio of Water Rain',        &
       & units='1',                                    & 
       & xtype='double' )


  !--- TEST
  call HistoryAddVariable(                            &
       & varname='ss_dumpH', dims=(/'x','z'/),    &
       & longname='S Dump',                      &
       & units='H_2"',                                   & 
       & xtype='double' )
  call HistoryAddVariable(                            &
       & varname='ss_dumpV', dims=(/'x','z'/),    &
       & longname='S Dump"',                      &
       & units='S_2e',                                   & 
       & xtype='double' )
  call HistoryAddVariable(                            &
       & varname='sf_dumpH', dims=(/'x','z'/),    &
       & longname='S Dump',                      &
       & units='1',                                   & 
       & xtype='double' )
  call HistoryAddVariable(                            &
       & varname='sf_dumpV', dims=(/'x','z'/),    &
       & longname='S Dump',                      &
       & units='1',                                   & 
       & xtype='double' )
  call HistoryAddVariable(                            &
       & varname='fs_dumpH', dims=(/'x','z'/),    &
       & longname='S Dump',                      &
       & units='1',                                   & 
       & xtype='double' )
  call HistoryAddVariable(                            &
       & varname='fs_dumpV', dims=(/'x','z'/),    &
       & longname='S Dump',                      &
       & units='1',                                   & 
       & 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( '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( 'Km_b',      ss_Km_bl      )
  call HistoryPut( 'Kh_b',      ss_Kh_bl      )

  call HistoryPut( 'MixRtSat',     ss_MixRtSat_bl   )
  call HistoryPut( 'MixRtV_n', ss_MixRtV_nl )
  call HistoryPut( 'MixRtV_b', ss_MixRtV_bl )
  call HistoryPut( 'MixRtC_n', ss_MixRtC_nl )
  call HistoryPut( 'MixRtC_b', ss_MixRtC_bl )
  call HistoryPut( 'MixRtR_n',  ss_MixRtR_nl  )
  call HistoryPut( 'MixRtR_b',  ss_MixRtR_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 HistoryPut( 'TempBasicZ',     ss_TempBasicZ )
  call HistoryPut( 'PressBasicZ',    ss_PressBasicZ )
  call HistoryPut( 'MixRtVBasicZ',   ss_MixRtVBasicZ )
  call HistoryPut( 'EffMolWtBasicZ', ss_EffMolWtBasicZ )

  !TEST
  call HistoryPut( 'ss_dumpH', ss_SDumpH )
  call HistoryPut( 'ss_dumpV', ss_SDumpV )
  call HistoryPut( 'fs_dumpH', ss_avr_fs( fs_SDumpH ) )
  call HistoryPut( 'fs_dumpV', ss_avr_fs( fs_SDumpV ) )
  call HistoryPut( 'sf_dumpH', ss_avr_sf( sf_SDumpH ) )
  call HistoryPut( 'sf_dumpV', ss_avr_sf( sf_SDumpV ) )


  !=== ファイルを閉じる
  call HistoryClose
  
  call EndSub("MakeReStartFile")

end subroutine MakeReStartFile
