!-------------------------------------- LICENCE BEGIN ------------------------------------
!Environment Canada - Atmospheric Science and Technology License/Disclaimer, 
!                     version 3; Last Modified: May 7, 2008.
!This is free but copyrighted software; you can use/redistribute/modify it under the terms 
!of the Environment Canada - Atmospheric Science and Technology License/Disclaimer 
!version 3 or (at your option) any later version that should be found at: 
!http://collaboration.cmc.ec.gc.ca/science/rpn.comm/license.html 
!
!This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
!without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
!See the above mentioned License/Disclaimer for more details.
!You should have received a copy of the License/Disclaimer along with this software; 
!if not, you can write to: EC-RPN COMM Group, 2121 TransCanada, suite 500, Dorval (Quebec), 
!CANADA, H9P 1J3; or send e-mail to service.rpn@ec.gc.ca
!-------------------------------------- LICENCE END --------------------------------------
****s/r sergout - performs output of time series buffers.
*
#include "model_macros_f.h"
*

      subroutine sergout (F_headr_L) 2,1
*
      implicit none
*
      logical F_headr_L
*
*author
*     Andre Methot - aug 94 v0_14
*
*revision
* v2_00 - Desgagne M.       - initial MPI version
* v2_11 - Lee V.            - add logic for output
* v2_21 - Desgagne M.       - rpn_comm stooge for MPI
* v3_20 - Winger K.         - correct time series handling in climate mode
* v3_30 - Winger K.         - Pres_ptop, Pres_pref, Grd_rcoef added and
*                             ptetik_S goes from 8 to 12 characters
*                             recorrect time series handling in climate mode
*object
*
*arguments
*  Name        I/O                 Description
*----------------------------------------------------------------
* F_headr_L     I         true: first record header will be written
*----------------------------------------------------------------
*
*implicits
#include "glb_ld.cdk"
#include "dcst.cdk"
#include "geomg.cdk"
#include "out3.cdk"
#include "hgc.cdk"
#include "itf_phy_config.cdk"
#include "ptopo.cdk"
#include "xst.cdk"
#include "xstb.cdk"
#include "p_serg.cdk"
#include "lctl.cdk"
#include "pres.cdk"
#include "grd.cdk"
*
      logical wr_L
      integer i, j, k, pnidate(14), pnigrot(4), err
      character*12 ptetik_S
**
*
*     calculation of date stamp
*
      pnidate(14) = Out3_date
      call datmgp2(pnidate)
*
*     Put the character 'G' in front of etiket to allow
*     correct rotation of wind related variables by
*     feseri program
*     
      ptetik_S(1:1)='G'
      ptetik_S(2:12)=Out3_etik_S(2:12)
*
      pnigrot(1) = HGC_ig1ro
      pnigrot(2) = HGC_ig2ro
      pnigrot(3) = HGC_ig3ro
      pnigrot(4) = HGC_ig4ro
*
      call rpn_comm_REDUCE (Xstb_sers,Xstb_sersx,Xst_dimsers,
     $                 "MPI_INTEGER","MPI_BOR",0,"grid",err)
      call rpn_comm_REDUCE (Xstb_serp,Xstb_serpx,Xst_dimserp,
     $                 "MPI_INTEGER","MPI_BOR",0,"grid",err)
      wr_L=.false.
      if (Ptopo_myproc.eq.0) then
         do i=1,Xst_dimsers
            Xstb_sers(i) = Xstb_sersx(i)
         end do
         do i=1,Xst_dimserp
            Xstb_serp(i) = Xstb_serpx(i)
         end do
         wr_L=(.not.P_serg_sroff_L.and.(Lctl_step.le.P_serg_serstp))
      endif
      call serwrit3 ( pnidate, ptetik_S , Geomg_hybm, 1000., 0.0, pnigrot,
     %              Pres_ptop, Pres_pref, Grd_rcoef, 
     %              0.0, real(Dcst_rgasd_8), real(Dcst_grav_8), .false.,
     %              P_cond_satu_L, F_headr_L, wr_L)
      if (Ptopo_myproc.eq.0) then
         do i=1,Xst_dimsers
            Xstb_sers(i) = 0.
         end do
         do i=1,Xst_dimserp
            Xstb_serp(i) = 0.
         end do
      endif
*
      return
      end