!---------------------------------------------------------------------
!     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.2.1 2005/07/12 09:10:16 kitamo Exp $ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!出力ファイルの定義 
!
!== 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    
  !== 暗黙の型宣言禁止
  implicit none

  !== Work
  character(50) ::   OutputFile


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

  
  !==== ファイル名を用意
  if ( DebugOn ) then 
     OutputFile = DimFile
  else
     OutputFile = RegFile
  end if
    
  !=== ヒストリー作成
  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 )
  
  ! 変数出力
  call HistoryPut('x', s_X( FileXMin: FileXMax ) )
  call HistoryPut('z', s_Z( FileZMin: FileZMax ) )
  
  ! 無次元圧力
  call HistoryAddVariable(                            &
       & varname='Exner', dims=(/'x','z','t'/),       &
       & longname='nondimensional pressure',          &
       & units='1',                                   &
       & xtype='double' )
  ! 温位
  call HistoryAddVariable(                            &
       & varname='PotTemp', dims=(/'x','z','t'/),     &
       & longname='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 EndSub("OpenDataFile")
  
end subroutine OpenDataFile
