!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Subroutine OpenDataFile
!
!   * Developer: KITAMORI Taichi
!   * Version: $Id: opendiagdatafile.f90,v 1.1.2.8 2005/11/28 12:57:43 kitamo Exp $ 
!   * Tag Name: $Name:  $
!   * 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='VelZTendAdvZ', dims=(/'z','t'/),       &
       & longname='vertical velocity tendency by advection', &
       & units='m/s^2',                                   &
       & xtype='double',                                &
       & history=history )
  !--- 浮力
  call HistoryAddVariable(                            &
       & varname='VelZTendBuoyZ', dims=(/'z','t'/),       &
       & longname='vertical velocity tendency by buoyancy', &
       & units='m/s^2',                                   &
       & xtype='double',                                &
       & history=history )
  !--- 乱流拡散
  call HistoryAddVariable(                            &
       & varname='VelZTendTurbZ', dims=(/'z','t'/),       &
       & longname='vertical velocity tendency by turbulence', &
       & units='m/s^2',                                   &
       & xtype='double',                                &
       & history=history )
  !--- 圧力勾配
  call HistoryAddVariable(                            &
       & varname='VelZTendPressGradZ', dims=(/'z','t'/),       &
       & longname='vertical velocity tendency by pressure gradient', &
       & units='m/s^2',                                   &
       & 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 HistoryAddVariable(  &
       & varname='AvailPotentialEnergyTotal', dims=(/'t'/),  &
       & longname='total available potential energy',  &
       & units='J',  & 
       & xtype='double',   &
       & history=history )
  
  !--- 領域全体の弾性エネルギー
  call HistoryAddVariable(  &
       & varname='ElasticEnergyTotal', dims=(/'t'/),  &
       & longname='total elastic energy',  &
       & units='J',  & 
       & xtype='double',   &
       & history=history )
       
  !--- エネルギー変化の寄与率
  !--- 放射
  call HistoryAddVariable(  &
       & varname='EnergyTendRadTotal', dims=(/'t'/),  &
       & longname='energy tendency by radiation',  &
       & units='J/s',  & 
       & xtype='double',   &
       & history=history )

  call HistoryAddVariable(  &
       & varname='EnergyTendDisTotal', dims=(/'t'/),  &
       & longname='energy tendency by dissipation',  &
       & units='J/s',  & 
       & xtype='double',   &
       & history=history )

  call HistoryAddVariable(  &
       & varname='EnergyTendPotTempTurbTotal', dims=(/'t'/),  &
       & longname='energy tendency by turbulence of heat',  &
       & units='J/s',  & 
       & xtype='double',   &
       & history=history )

  call HistoryAddVariable(  &
       & varname='EnergyTendVelTurbTotal', dims=(/'t'/),  &
       & longname='energy tendency by turbulence of momentum',  &
       & units='J/s',  & 
       & xtype='double',   &
       & history=history )

  call HistoryAddVariable(  &
       & varname='EnergyTendSfcHeatFluxTotal', dims=(/'t'/),  &
       & longname='energy tendency by surface heat flux',  &
       & units='J/s',  & 
       & xtype='double',   &
       & history=history )

  call HistoryAddVariable(  &
       & varname='EnergyTendSfcMomentumFluxTotal', dims=(/'t'/),  &
       & longname='energy tendency by surface momentum flux',  &
       & units='J/s',  & 
       & xtype='double',   &
       & history=history )

  call HistoryAddVariable(  &
       & varname='EnergyTendPotTempBasicTotal', dims=(/'t'/),  &
       & longname='energy tendency by basic field of potential temperature',  &
       & units='J/s',  & 
       & xtype='double',   &
       & history=history )

  call HistoryAddVariable(  &
       & varname='EnergyTendNonconsvTotal', dims=(/'t'/),  &
       & longname='energy tendency by non-conserve term',  &
       & units='J/s',  & 
       & xtype='double',   &
       & history=history )

  call EndSub("OpenDiagDataFile")
  
end subroutine OpenDiagDataFile
