!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Subroutine OutputDiagDataFile
!
!   * Developer: KITAMORI Taichi
!   * Version: $Id: outputdiagdatafile.f90,v 1.1.2.9 2005/11/28 12:57:43 kitamo Exp $ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!診断量をファイル出力.
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!
                                                                 !=end

subroutine OutputDiagDataFile(history,   & 
     & Time, & 
     & s_PotTempTendAdvZ_nl,  & 
     & s_PotTempTendSrcZ_nl,  & 
     & s_PotTempTendTurbZ_nl, & 
     & s_PotTempTendRadZ_nl,  &
     & s_PotTempTendDisZ_nl,  &
     & s_PotTempTendSfcZ_nl,  &
     & f_VelZTendAdvZ,        &
     & f_VelZTendBuoyZ,       &
     & f_VelZTendTurbZ,       & 
     & f_VelZTendPressGradZ,  &
     & MassTotal,             &
     & KineticEnergyTotal,    &
     & PotentialEnergyTotal,  &
     & AvailPotentialEnergyTotal,  &
     & ElasticEnergyTotal,    &
     & EnergyTendRadTotal,    &
     & EnergyTendDisTotal,    &
     & EnergyTendPotTempTurbTotal,  &
     & EnergyTendVelTurbTotal,      &
     & EnergyTendSfcHeatFluxTotal,  &
     & EnergyTendSfcMomentumFluxTotal, &
     & EnergyTendPotTempBasicTotal,  &
     & EnergyTendNonconsvTotal)

                                                                 !=begin
  !== Dependency
  use gt4_history
  use dc_trace, only: BeginSub, EndSub
  use gridset, only: FileZMin, FileZMax, DimZMin,  DimZMax
  use average, only: s_avrz_f
                                                                !=end
  !== 暗黙の型宣言禁止
  implicit none
                                                                 !=begin
  !== Input
  type(GT_HISTORY), intent(inout) :: history
  real(8), intent(in) :: Time
  real(8), intent(in) :: s_PotTempTendAdvZ_nl(DimZMin:DimZMax)
  real(8), intent(in) :: s_PotTempTendSrcZ_nl(DimZMin:DimZMax)
  real(8), intent(in) :: s_PotTempTendTurbZ_nl(DimZMin:DimZMax)
  real(8), intent(in) :: s_PotTempTendRadZ_nl(DimZMin:DimZMax)
  real(8), intent(in) :: s_PotTempTendDisZ_nl(DimZMin:DimZMax)
  real(8), intent(in) :: s_PotTempTendSfcZ_nl(DimZMin:DimZMax)
  real(8), intent(in) :: f_VelZTendAdvZ(DimZMin:DimZMax)
  real(8), intent(in) :: f_VelZTendBuoyZ(DimZMin:DimZMax)
  real(8), intent(in) :: f_VelZTendTurbZ(DimZMin:DimZMax)
  real(8), intent(in) :: f_VelZTendPressGradZ(DimZMin:DimZMax)
  real(8), intent(in) :: MassTotal
  real(8), intent(in) :: KineticEnergyTotal
  real(8), intent(in) :: PotentialEnergyTotal
  real(8), intent(in) :: AvailPotentialEnergyTotal
  real(8), intent(in) :: ElasticEnergyTotal
  real(8), intent(in) :: EnergyTendRadTotal
  real(8), intent(in) :: EnergyTendDisTotal
  real(8), intent(in) :: EnergyTendPotTempTurbTotal
  real(8), intent(in) :: EnergyTendVelTurbTotal
  real(8), intent(in) :: EnergyTendSfcHeatFluxTotal
  real(8), intent(in) :: EnergyTendSfcMomentumFluxTotal
  real(8), intent(in) :: EnergyTendPotTempBasicTotal
  real(8), intent(in) :: EnergyTendNonconsvTotal
                                                                !=end
  !== Work 
  real(8)             :: s_VelZTendAdvZ(DimZMin:DimZMax)
  real(8)             :: s_VelZTendBuoyZ(DimZMin:DimZMax)
  real(8)             :: s_VelZTendTurbZ(DimZMin:DimZMax)
  real(8)             :: s_VelZTendPressGradZ(DimZMin:DimZMax)

  call BeginSub("OutputDiagDataFile", &
       &         fmt="%c",            &
       &         c1="Output diagnostic values in netCDF file.")

  !=== 値を出力
  call HistoryPut( 't', Time, history )

  !--- 熱の式の各項の寄与

  call HistoryPut( 'PotTempTendAdvZ',    &
       & s_PotTempTendAdvZ_nl(FileZMin:FileZMax), history )
  call HistoryPut( 'PotTempTendSrcZ',    &
       & s_PotTempTendSrcZ_nl(FileZMin:FileZMax), history )
  call HistoryPut( 'PotTempTendTurbZ',    &
       & s_PotTempTendTurbZ_nl(FileZMin:FileZMax), history )
  call HistoryPut( 'PotTempTendRadZ',    &
       & s_PotTempTendRadZ_nl(FileZMin:FileZMax), history )
  call HistoryPut( 'PotTempTendDisZ',    &
       & s_PotTempTendDisZ_nl(FileZMin:FileZMax), history )
  call HistoryPut( 'PotTempTendSfcZ',    &
       & s_PotTempTendSfcZ_nl(FileZMin:FileZMax), history )

  !--- 鉛直速度の式の各項の寄与
  s_VelZTendAdvZ       = s_avrz_f(f_VelZTendAdvZ)
  s_VelZTendBuoyZ      = s_avrz_f(f_VelZTendBuoyZ)
  s_VelZTendTurbZ      = s_avrz_f(f_VelZTendTurbZ)
  s_VelZTendPressGradZ = s_avrz_f(f_VelZTendPressGradZ)

  call HistoryPut( 'VelZTendAdvZ',       &
       & s_VelZTendAdvZ(FileZMin:FileZMax), history)
  call HistoryPut( 'VelZTendBuoyZ',       &
       & s_VelZTendBuoyZ(FileZMin:FileZMax), history)
  call HistoryPut( 'VelZTendTurbZ',       &
       & s_VelZTendTurbZ(FileZMin:FileZMax), history)
  call HistoryPut( 'VelZTendPressGradZ',       &
       & s_VelZTendPressGradZ(FileZMin:FileZMax), history)

  !--- 保存量とその変化率

  call HistoryPut( 'MassTotal', MassTotal, history )
  call HistoryPut( 'KineticEnergyTotal', KineticEnergyTotal, history )
  call HistoryPut( 'PotentialEnergyTotal', PotentialEnergyTotal, history )
  call HistoryPut( 'AvailPotentialEnergyTotal', AvailPotentialEnergyTotal, history )
  call HistoryPut( 'ElasticEnergyTotal', ElasticEnergyTotal, history )

  call HistoryPut( 'EnergyTendRadTotal', EnergyTendRadTotal, history)
  call HistoryPut( 'EnergyTendDisTotal', EnergyTendDisTotal, history)
  call HistoryPut( 'EnergyTendPotTempTurbTotal', EnergyTendPotTempTurbTotal, history)
  call HistoryPut( 'EnergyTendVelTurbTotal', EnergyTendVelTurbTotal, history)
  call HistoryPut( 'EnergyTendSfcHeatFluxTotal',   &
                &   EnergyTendSfcHeatFluxTotal, history)
  call HistoryPut( 'EnergyTendSfcMomentumFluxTotal',  &
                &   EnergyTendSfcMomentumFluxTotal, history)
  call HistoryPut( 'EnergyTendPotTempBasicTotal',  &
                &   EnergyTendPotTempBasicTotal, history)
  call HistoryPut( 'EnergyTendNonconsvTotal',  &
                &   EnergyTendNonconsvTotal, history)

  call EndSub("OutputDiagDataFile")
  
end subroutine OutputDiagDataFile

