!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Subroutine OpenDataFile
!
!   * Developer: SUGIYAMA Ko-ichiro (sugiyama@gfd-dennou.org)
!   * Version: $Id: opendatafile.f90,v 1.2 2005/04/22 15:01:52 sugiyama Exp $ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!$B=PNO%U%!%$%k$NDj5A(B 
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!
                                                                 !=end

subroutine OpenDataFile
                                                                 !=begin
  !== Dependency
  use gt4_history
  use dc_trace, only: BeginSub, EndSub
  use debugset, only: DebugOn
  use gridset,  only: s_X, s_Z, &
       & FileNX, FileNZ, FileXMin, FileXMax, FileZMin, FileZMax
  use nameset,  only: exptitle, expsrc, expinst
  use fileset,  only: DimFile, RegFile
                                                                 !=end    
  !== $B0EL[$N7?@k8@6X;_(B
  implicit none

  !== Work
  character(50) ::   OutputFile


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

  
  !==== $B%U%!%$%kL>$rMQ0U(B
  if ( DebugOn ) then 
     OutputFile = DimFile
  else
     OutputFile = RegFile
  end if
    
  !=== $B%R%9%H%j!<:n@.(B
  call HistoryCreate(                                 &
       & file = OutputFile,                           &
       & title = exptitle,                            &
       & source = expsrc,                             &
       & institution = expinst,                       &
       & dims=(/'x','z','t'/),                        &
       & dimsizes=(/FileNX, FileNZ, 0/),              &
       & 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( FileXMin: FileXMax ) )
  call HistoryPut('z', s_Z( FileZMin: FileZMax ) )
  
  ! $BL5<!8505NO(B
  call HistoryAddVariable(                            &
       & varname='Exner', dims=(/'x','z','t'/),       &
       & longname='nondimensional pressure',          &
       & units='1',                                   &
       & xtype='double' )
  ! $B290L(B
  call HistoryAddVariable(                            &
       & varname='DelPotTemp', dims=(/'x','z','t'/),     &
       & longname='virtual potential temperature',    &
       & units='K',                                   &
       & xtype='double' )
  ! $B290L(B
  call HistoryAddVariable(                            &
       & varname='PotTemp', dims=(/'x','z','t'/),     &
       & longname='virtual potential temperature',    &
       & units='K',                                   &
       & xtype='double' )
  ! $B2>290L(B
  call HistoryAddVariable(                            &
       & varname='VPotTemp', dims=(/'x','z','t'/),     &
       & longname='virtual potential temperature',    &
       & units='K',                                   &
       & xtype='double' )
  ! $B29EY(B
  call HistoryAddVariable(                            &
       & varname='Temp', dims=(/'x','z','t'/),     &
       & longname='temperature',    &
       & units='K',                                   &
       & xtype='double' )
  ! $BB.EY(B
  call HistoryAddVariable(                            &
       & varname='VelX', dims=(/'x','z','t'/),        &
       & longname='zonal velocity',                   &
       & units='m/s',                                 &
       & xtype='double' )
  ! $BB.EY(B
  call HistoryAddVariable(                            &
       & varname='VelZ', dims=(/'x','z','t'/),        &
       & longname='vertical velocity',                &
       & units='m/s',                                 &
       & xtype='double' )
  ! $B12G4@-78?t(B
  call HistoryAddVariable(                            &
       & varname='Km', dims=(/'x','z','t'/),          &
       & longname='Km',                               &
       & units='1',                                   &
       & xtype='double' )
  ! $B123H;678?t(B
  call HistoryAddVariable(                            &
       & varname='Kh', dims=(/'x','z','t'/),          &
       & longname='Kh',                               &
       & units='1',                                   &
       & xtype='double' )
  
  ! $BIbNO9`(B
  call HistoryAddVariable(                            &
       & varname='Buoy', dims=(/'x','z','t'/),          &
       & longname='Buoyancy',                               &
       & units='1',                                   &
       & xtype='double' )
  
  ! $BK0OB?e>x5$:.9gHf(B
  call HistoryAddVariable(                             &
       & varname='MixRtSat', dims=(/'x','z','t'/),&
       & longname='Mixing Ratio of Saturated Water Vaper', &
       & units='1',                                    & 
       & xtype='double' )

  call HistoryAddVariable(                             &
       & varname='DelMixRtSat', dims=(/'x','z','t'/),&
       & longname='Mixing Ratio of Saturated Water Vaper', &
       & units='1',                                    & 
       & xtype='double' )

  ! $B?e>x5$:.9gHf(B
  call HistoryAddVariable(                             &
       & varname='MixRtV', dims=(/'x','z','t'/),&
       & longname='Mixing Ratio of Water Vaper',       &
       & units='1',                                    & 
       & xtype='double' )

  ! $B?e>x5$:.9gHf(B
  call HistoryAddVariable(                             &
       & varname='DelMixRtV', dims=(/'x','z','t'/),&
       & longname='Mixing Ratio of Water Vaper',       &
       & units='1',                                    & 
       & xtype='double' )

  ! $B1@N3:.9gHf(B
  call HistoryAddVariable(                             &
       & varname='MixRtC', dims=(/'x','z','t'/),&
       & longname='Mixing Ratio of Water Cloud',       &
       & units='1',                                    & 
       & xtype='double' )

  ! $B1+N3:.9gHf(B
  call HistoryAddVariable(                             &
       & varname='MixRtR', dims=(/'x','z','t'/), &
       & longname='Mixing Ratio of Water Rain',        &
       & units='1',                                    & 
       & xtype='double' )

  ! $B?e>x5$L)EY(B
  call HistoryAddVariable(                             &
       & varname='RhoV', dims=(/'x','z','t'/),         &
       & longname='Water Density',                     &
       & units='kg/m^3',                               & 
       & xtype='double' )

  !--- $B>xH/$K$h$k1+?e$+$i?e>x5$$X$NJQ49(B
  call HistoryAddVariable(                             &
       & varname='EVrv', dims=(/'x','z','t'/), &
       & longname='Evaporation From Rain To Vaper',        &
       & units='1',                                    & 
       & xtype='double' )

  !--- $BJ;9g@.D9$K$h$k1@?e$+$i1+?e$X$NJQ49(B
  call HistoryAddVariable(                             &
       & varname='CNcr', dims=(/'x','z','t'/), &
       & longname='AutoCondensation From Cloud To Rain',        &
       & units='1',                                    & 
       & xtype='double' )
  
  !--- $B6E7k$K$h$k?e>x5$$+$i1@?e$X$NJQ49(B
  call HistoryAddVariable(                             &
       & varname='CLcr', dims=(/'x','z','t'/), &
       & longname='Collection From Cloud To Rain',        &
       & units='1',                                    & 
       & xtype='double' )

  !--- $B1+N3=*C<B.EY(B
  call HistoryAddVariable(                             &
       & varname='VelZRain', dims=(/'x','z','t'/), &
       & longname='Rain fall velocity',        &
       & units='m/s',                                    & 
       & xtype='double' )

  !--- $B6E7k$K$h$k?e>x5$$+$i1@?e$X$NJQ49(B. 
  call HistoryAddVariable(                             &
       & varname='PRr', dims=(/'x','z','t'/), &
       & longname='Precipitation Rain',        &
       & units='1',                                    & 
       & xtype='double' )
  
  call EndSub("OpenDataFile")
  
end subroutine OpenDataFile
