!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Subroutine OpenDataFile
!
!   * Developer: SUGIYAMA Ko-ichiro
!   * Version: $Id: opendatafile.f90,v 1.2.2.2 2005/09/01 14:44:23 kitamo Exp $ 
!   * Tag Name: $Name: arare3m $
!   * Change History: 
!
!== Overview 
!
!出力ファイルの定義 
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!
                                                                 !=end

subroutine OpenDataFile(history)
                                                                 !=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

  !== Input/Output
  type(GT_HISTORY), intent(inout) :: history

  !== 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, history=history )
  
  ! 変数出力
  call HistoryPut('x', s_X( FileXMin: FileXMax ), history )
  call HistoryPut('z', s_Z( FileZMin: FileZMax ), history )
  
  ! 無次元圧力
  call HistoryAddVariable(                            &
       & varname='Exner', dims=(/'x','z','t'/),       &
       & longname='nondimensional pressure',          &
       & units='1',                                   &
       & xtype='double', history=history )
  ! 温位
  call HistoryAddVariable(                            &
       & varname='PotTemp', dims=(/'x','z','t'/),     &
       & longname='potential temperature',    &
       & units='K',                                   &
       & xtype='double', history=history )
  ! 速度
  call HistoryAddVariable(                            &
       & varname='VelX', dims=(/'x','z','t'/),        &
       & longname='zonal velocity',                   &
       & units='m/s',                                 &
       & xtype='double', history=history )
  ! 速度
  call HistoryAddVariable(                            &
       & varname='VelZ', dims=(/'x','z','t'/),        &
       & longname='vertical velocity',                &
       & units='m/s',                                 &
       & xtype='double', history=history )
  ! 渦粘性係数
  call HistoryAddVariable(                            &
       & varname='Km', dims=(/'x','z','t'/),          &
       & longname='Km',                               &
       & units='1',                                   &
       & xtype='double', history=history )
  ! 渦拡散係数
  call HistoryAddVariable(                            &
       & varname='Kh', dims=(/'x','z','t'/),          &
       & longname='Kh',                               &
       & units='1',                                   &
       & xtype='double', history=history )
  
  call EndSub("OpenDataFile")
  
end subroutine OpenDataFile
