!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Subroutine OutputDataFile
!
!   * Developer: SUGIYAMA Ko-ichiro (sugiyama@gfd-dennou.org)
!   * Version: $Id: outputdatafile.f90,v 1.2 2005/04/22 15:01:52 sugiyama Exp $ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!$B%U%!%$%k=PNO(B. $BD9$$;~4V%9%F%C%W$NCM$r=PNO(B.
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!
                                                                 !=end

subroutine OutputDataFile(Time, &
     & fs_VelX_nl, sf_VelZ_nl, ss_Exner_nl, ss_PotTemp_nl, ss_Km_nl, ss_Kh_nl,&
     & ss_MixRtV_nl, ss_MixRtC_nl, ss_MixRtR_nl, &
     & ss_Temp_nl, &
     & ss_EVrv_nl, & 
     & ss_CNcr_nl,&
     & ss_CLcr_nl,& 
     & ss_VelZRain_nl,& 
     & ss_PRr_nl, &
     & ss_MixRtSat_nl, &
     & sf_Buoy_nl &
     )
                                                                 !=begin
  !== Dependency
  use gt4_history
  use dc_trace, only: BeginSub, EndSub
  use gridset, only: FileXMin, FileXMax, FileZMin, FileZMax, &
       &             DimXMin,  DimXMax,  DimZMin,  DimZMax
  use average, only: ss_avr_fs, ss_avr_sf
  use basicset, only: ss_PotTempBasicZ, ss_EffMolWtBasicZ, ss_MixRtVBasicZ
                                                                 !=end
  !== $B0EL[$N7?@k8@6X;_(B
  implicit none
                                                                 !=begin
  !== Input
  real(8), intent(in) :: Time
  real(8), intent(in) :: fs_VelX_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in) :: sf_VelZ_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in) :: ss_Exner_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in) :: ss_PotTemp_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in) :: ss_Km_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in) :: ss_Kh_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in) :: ss_MixRtV_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in) :: ss_MixRtC_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in) :: ss_MixRtR_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in) :: ss_Temp_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in) :: &
       & ss_EVrv_nl(DimXMin:DimXMax, DimZMin:DimZMax), & 
       & ss_CNcr_nl(DimXMin:DimXMax, DimZMin:DimZMax),&
       & ss_CLcr_nl(DimXMin:DimXMax, DimZMin:DimZMax),& 
       & ss_VelZRain_nl(DimXMin:DimXMax, DimZMin:DimZMax),& 
       & ss_PRr_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
       & ss_MixRtSat_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
       & sf_Buoy_nl(DimXMin:DimXMax, DimZMin:DimZMax)
                                                                 !=end
  !== Work
  real(8)             :: ss_VelX_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)             :: ss_VelZ_nl(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)             :: ss_Buoy_nl(DimXMin:DimXMax, DimZMin:DimZMax)

  
  call BeginSub("OutputDataFile", &
       &         fmt="%c",            &
       &         c1="Output variables in netCDF file.")


  !=== $B3J;RE@0LCV$rJQ49(B
  ss_VelX_nl = ss_avr_fs( fs_VelX_nl )
  ss_VelZ_nl = ss_avr_sf( sf_VelZ_nl )
  ss_Buoy_nl = ss_avr_sf( sf_Buoy_nl )


  !=== $BCM$r=PNO(B
  call HistoryPut( 't', Time )
  
  call HistoryPut( 'Exner',   &
    & ss_Exner_nl(FileXMin:FileXMax, FileZMin:FileZMax)   )
  call HistoryPut( 'DelPotTemp', &
    & ss_PotTemp_nl(FileXMin:FileXMax, FileZMin:FileZMax) )
  call HistoryPut( 'PotTemp', &
    & ss_PotTemp_nl(FileXMin:FileXMax, FileZMin:FileZMax) &
    &  + ss_PotTempBasicZ(FileXMin:FileXMax, FileZMin:FileZMax) )
  call HistoryPut( 'VPotTemp', &
    & (ss_PotTemp_nl(FileXMin:FileXMax, FileZMin:FileZMax) &
    &  + ss_PotTempBasicZ(FileXMin:FileXMax, FileZMin:FileZMax)) &
    &  / ss_EffMolWtBasicZ(FileXMin:FileXMax, FileZMin:FileZMax) )
  call HistoryPut( 'Km',      &
    & ss_Km_nl(FileXMin:FileXMax, FileZMin:FileZMax)      )
  call HistoryPut( 'Kh',      &
    & ss_Kh_nl(FileXMin:FileXMax, FileZMin:FileZMax)      )
  call HistoryPut( 'VelX',    &
    & ss_VelX_nl(FileXMin:FileXMax, FileZMin:FileZMax)    )
  call HistoryPut( 'VelZ',    &
    & ss_VelZ_nl(FileXMin:FileXMax, FileZMin:FileZMax)    )
  call HistoryPut( 'DelMixRtV',    &
    & ss_MixRtV_nl(FileXMin:FileXMax, FileZMin:FileZMax)  )
  call HistoryPut( 'MixRtV',    &
    & ss_MixRtV_nl(FileXMin:FileXMax, FileZMin:FileZMax)    &
    & + ss_MixRtVBasicZ(FileXMin:FileXMax, FileZMin:FileZMax)    )
  call HistoryPut( 'MixRtC',    &
    & ss_MixRtC_nl(FileXMin:FileXMax, FileZMin:FileZMax)    )
  call HistoryPut( 'MixRtR',    &
    & ss_MixRtR_nl(FileXMin:FileXMax, FileZMin:FileZMax)    )
  call HistoryPut( 'MixRtSat',    &
    & ss_MixRtSat_nl(FileXMin:FileXMax, FileZMin:FileZMax)    )
  call HistoryPut( 'Temp', &
    & ss_Temp_nl(FileXMin:FileXMax, FileZMin:FileZMax) )
  
  call HistoryPut( 'EVrv',    &
    & ss_EVrv_nl(FileXMin:FileXMax, FileZMin:FileZMax)    )
  call HistoryPut( 'CNcr',    &
    & ss_CNcr_nl(FileXMin:FileXMax, FileZMin:FileZMax)    )
  call HistoryPut( 'CLcr',    &
    & ss_CLcr_nl(FileXMin:FileXMax, FileZMin:FileZMax)    )
  call HistoryPut( 'VelZRain',    &
    & ss_VelZRain_nl(FileXMin:FileXMax, FileZMin:FileZMax)    )
  call HistoryPut( 'PRr',    &
    & ss_PRr_nl(FileXMin:FileXMax, FileZMin:FileZMax)    )
  call HistoryPut( 'Buoy',    &
    & ss_Buoy_nl(FileXMin:FileXMax, FileZMin:FileZMax)    )
  
  call EndSub("OutputDataFile")
  
end subroutine OutputDataFile

