!---------------------------------------------------------------------
!     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 2005/04/22 15:01:52 sugiyama Exp $ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!$B=i4|(B or $B%j%9%?!<%HMQ$N>l$N>pJs$r(B netCDF $B%U%!%$%k$K=PNO$9$k$?$a$N%k!<%A%s(B
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!$BB.EY$rI>2A$9$k3J;R0LCV$H(B, $B<4$H$7$FF~NO$7$F$"$k:BI8CM$O@09gE*$G$J$$$3$H$KCm0U(B
!
!== 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  
  !== $B0EL[$N7?@k8@6X;_(B
  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)

  !=== $B%R%9%H%j!<:n@.(B
  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 )
  
  !=== $BJQ?t=PNO(B
  call HistoryPut('x', s_X )
  call HistoryPut('z', s_Z )
  call HistoryPut('t', Time )
  
  !=== $BL5<!8505NO(B
  call HistoryAddVariable(                            &
       & varname='Exner_n', dims=(/'x','z'/),         &
       & longname='nondimensional pressure',          &
       & units='1',                                   &
       & xtype='double' )
  !=== $BL5<!8505NO(B
  call HistoryAddVariable(                            &
       & varname='Exner_b', dims=(/'x','z'/),         &
       & longname='nondimensional pressure',          &
       & units='1',                                   &
       & xtype='double' )
  !=== $B2>290L(B
  call HistoryAddVariable(                            &
       & varname='PotTemp_n', dims=(/'x','z'/),       &
       & longname='virtual potential temperature',    &
       & units='K',                                   &
       & xtype='double' )
  !=== $B2>290L(B
  call HistoryAddVariable(                            &
       & varname='PotTemp_b', dims=(/'x','z'/),       &
       & longname='virtual potential temperature',    &
       & units='K',                                   &
       & xtype='double' )
  !=== $BB.EY(B
  call HistoryAddVariable(                            &
       & varname='VelX_n', dims=(/'x','z'/),          &
       & longname='zonal velocity',                   &
       & units='m/s',                                 &
       & xtype='double' )
  !=== $BB.EY(B
  call HistoryAddVariable(                            &
       & varname='VelX_b', dims=(/'x','z'/),          &
       & longname='zonal velocity',                   &
       & units='m/s',                                 &
       & xtype='double' )
  !=== $BB.EY(B
  call HistoryAddVariable(                            &
       & varname='VelZ_n', dims=(/'x','z'/),          &
       & longname='vertical velocity',                &
       & units='m/s',                                 &
       & xtype='double' )
  !=== $BB.EY(B
  call HistoryAddVariable(                            &
       & varname='VelZ_b', dims=(/'x','z'/),          &
       & longname='vertical velocity',                &
       & units='m/s',                                 &
       & xtype='double' )
  !=== $B12G4@-78?t(B
  call HistoryAddVariable(                            &
       & varname='Km_n', dims=(/'x','z'/),            &
       & longname='Km',                               &
       & units='1',                                   &
       & xtype='double' )
  !=== $B12G4@-78?t(B
  call HistoryAddVariable(                            &
       & varname='Km_b', dims=(/'x','z'/),            &
       & longname='Km',                               &
       & units='1',                                   &
       & xtype='double' )
  !=== $B123H;678?t(B
  call HistoryAddVariable(                            &
       & varname='Kh_n', dims=(/'x','z'/),            &
       & longname='Kh',                               &
       & units='1',                                   &
       & xtype='double' )
  !=== $B123H;678?t(B
  call HistoryAddVariable(                            &
       & varname='Kh_b', dims=(/'x','z'/),            &
       & longname='Kh',                               &
       & units='1',                                   &
       & xtype='double' )

  !=== $BL5<!8505NO(B
  call HistoryAddVariable(                             &
       & varname='ExnerBasicZ', dims=(/'x','z'/),      &
       & longname='nondimensional pressure', units='1',&
       & xtype='double' )
  !=== $B290L(B
  call HistoryAddVariable(                            &
       & varname='PotTempBasicZ', dims=(/'x','z'/),    &
       & longname='potential temperature',            &
       & units='K', xtype='double' )
  !=== $BDj05HfG.(B
  call HistoryAddVariable(                            &
       & varname='CpBasicZ', dims=(/'x','z'/),        &
       & longname='Specific Heat',                    &
       & units='J/K kg', xtype='double' )
  !=== $BL)EY(B
  call HistoryAddVariable(                            &
       & varname='DensBasicZ', dims=(/'x','z'/),      &
       & longname='density',                          &
       & units='Kg/m^3', xtype='double' )
  !=== $B2;GHB.EY(B
  call HistoryAddVariable(                            &
       & varname='VelSoundBasicZ', dims=(/'x','z'/),  &
       & longname='sound velocity',                   &
       & units='m/s|2"', xtype='double' )
  !=== $B29EY$N4pK\>l(B
  call HistoryAddVariable(                            &
       & varname='TempBasicZ', dims=(/'x','z'/),  &
       & longname='Temperature of basic state',       &
       & units='K"', xtype='double' ) 
  !=== $B05NO$N4pK\>l(B
  call HistoryAddVariable(                            &
       & varname='PressBasicZ', dims=(/'x','z'/),  &
       & longname='Pressure of basic state',             &
       & units='Pa', xtype='double' ) 

  !=== $B?e>x5$:.9gHf(B
  call HistoryAddVariable(                            &
       & varname='MixRtVBasicZ', dims=(/'x','z'/),  &
       & longname='Mixing ratio of water vaper ',        &
       & units='kg/kg', xtype='double' ) 
  !=== $BJ,;RNL8z2L(B
  call HistoryAddVariable(                            &
       & varname='EffMolWtBasicZ', dims=(/'x','z'/),  &
       & longname='Effect of Mole Weight',            &
       & units='1', xtype='double' ) 

  !=== $BK0OB?e>x5$:.9gHf(B
  call HistoryAddVariable(                             &
       & varname='MixRtSat', dims=(/'x','z'/),&
       & longname='Mixing Ratio of Water Vaper',       &
       & units='1',                                    & 
       & xtype='double' )
  !=== $B?e>x5$:.9gHf(B
  call HistoryAddVariable(                             &
       & varname='MixRtV_n', dims=(/'x','z'/),&
       & longname='Mixing Ratio of Water Vaper',       &
       & units='1',                                    & 
       & xtype='double' )
  !=== $B?e>x5$:.9gHf(B
  call HistoryAddVariable(                             &
       & varname='MixRtV_b', dims=(/'x','z'/),&
       & longname='Mixing Ratio of Water Vaper',       &
       & units='1',                                    & 
       & xtype='double' )
  !=== $B1@N3:.9gHf(B
  call HistoryAddVariable(                             &
       & varname='MixRtC_n', dims=(/'x','z'/),&
       & longname='Mixing Ratio of Water Cloud',       &
       & units='1',                                    & 
       & xtype='double' )
  !=== $B1@N3:.9gHf(B
  call HistoryAddVariable(                             &
       & varname='MixRtC_b', dims=(/'x','z'/),&
       & longname='Mixing Ratio of Water Cloud',      &
       & units='1',                                    & 
       & xtype='double' )
  !=== $B1+N3:.9gHf(B
  call HistoryAddVariable(                             &
       & varname='MixRtR_n', dims=(/'x','z'/), &
       & longname='Mixing Ratio of Water Rain',        &
       & units='1',                                    & 
       & xtype='double' )
  !=== $B1+N3:.9gHf(B
  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' )


  

  !=== $B%U%!%$%k=PNO(B
  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 ) )


  !=== $B%U%!%$%k$rJD$8$k(B
  call HistoryClose
  
  call EndSub("MakeReStartFile")

end subroutine MakeReStartFile
