!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Subroutine OpenDataFile
!
!   * Developer: KITAMORI Taichi
!   * Version: $Id: opendiagdatafile.f90,v 1.1.2.5 2005/09/17 07:52:18 kitamo Exp $ 
!   * Tag Name: $Name: arare3m $
!   * Change History: 
!
!== Overview 
!
!診断量出力ファイルの定義 
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!
                                                                 !=end

subroutine OpenDiagDataFile(history)
                                                                 !=begin
  !== Dependency
  use gt4_history
  use dc_trace, only: BeginSub, EndSub
  use debugset, only: DebugOn
  use gridset,  only: s_X, s_Z, &
       & FileNZ, FileZMin, FileZMax
  use nameset,  only: exptitle, expsrc, expinst
  use fileset,  only: DimDiagFile, RegDiagFile
                                                                 !=end    
  !== 暗黙の型宣言禁止
  implicit none

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

  !== Work
  character(50) ::   OutputFile

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

  
  !==== ファイル名を用意
  if ( DebugOn ) then 
     OutputFile = DimDiagFile
  else
     OutputFile = RegDiagFile
  end if
    
  !=== ヒストリー作成
  call HistoryCreate(                                 &
       & file = OutputFile,                           &
       & title = exptitle,                            &
       & source = expsrc,                             &
       & institution = expinst,                       &
       & dims=(/'z','t'/),                            &
       & dimsizes=(/FileNZ, 0/),                      &
       & longnames=(/'Z-coordinate',                  &
       &             'Time        '/),                &
       & units=(/'m','s'/), origin=0.0,               &
       & interval=0.0,                                &
       & history=history )
  
  !=== 変数出力
  call HistoryPut('z', s_Z( FileZMin: FileZMax ), history )
  
  ! 移流
  call HistoryAddVariable(                            &
       & varname='PotTempTendAdvZ', dims=(/'z','t'/),       &
       & longname='potential tempareture tendency by advection', &
       & units='K/s',                                   &
       & xtype='double',                                &
       & history=history )
  ! 生成(基本場による移流効果)
  call HistoryAddVariable(                            &
       & varname='PotTempTendSrcZ', dims=(/'z','t'/),       &
       & longname='potential tempareture tendency by source term', &
       & units='K/s',                                   &
       & xtype='double',                                &
       & history=history )
  ! 乱流拡散
  call HistoryAddVariable(                            &
       & varname='PotTempTendTurbZ', dims=(/'z','t'/),       &
       & longname='potential tempareture tendency by diffusion', &
       & units='K/s',                                   &
       & xtype='double',                                &
       & history=history )
  ! 放射加熱
  call HistoryAddVariable(                            &
       & varname='PotTempTendRadZ', dims=(/'z','t'/),       &
       & longname='potential tempareture tendency by radiation', &
       & units='K/s',                                   &
       & xtype='double',                                &
       & history=history )
  ! 散逸加熱
  call HistoryAddVariable(                            &
       & varname='PotTempTendDisZ', dims=(/'z','t'/),       &
       & longname='potential tempareture tendency by dissipation', &
       & units='K/s',                                   &
       & xtype='double',                                &
       & history=history )
  ! 地表面加熱
  call HistoryAddVariable(                            &
       & varname='PotTempTendSfcZ', dims=(/'z','t'/),       &
       & longname='potential tempareture tendency by surface heating', &
       & units='K/s',                                   &
       & xtype='double',                                &
       & history=history )

  ! 領域全体の質量
  call HistoryAddVariable(  &
       & varname='MassTotal', dims=(/'t'/),  &
       & longname='total mass',  &
       & units='kg',  & 
       & xtype='double',   &
       & history=history )

  ! 領域全体の運動エネルギー
  call HistoryAddVariable(  &
       & varname='KineticEnergyTotal', dims=(/'t'/),  &
       & longname='total kinetic energy',  &
       & units='J',  & 
       & xtype='double',   &
       & history=history )

  ! 領域全体のポテンシャルエネルギー
  call HistoryAddVariable(  &
       & varname='PotentialEnergyTotal', dims=(/'t'/),  &
       & longname='total potential energy',  &
       & units='J',  & 
       & xtype='double',   &
       & history=history )
  
  call EndSub("OpenDiagDataFile")
  
end subroutine OpenDiagDataFile
