!-------------------------------------- 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 --------------------------------------
!

      SUBROUTINE post_regres,3
#if defined (DOC)
*
***s/r post_regres  - Write the balance operators: Ptot and Ekman for LAM intercomparisons.
*
*Author:    Luc Fillion - ARMA/EC - 3 Sep. 2008.
*Revision:
*
*Arguments: NONE
*
#endif
      IMPLICIT NONE
*implicits
*
#include "pardim.cdk"
#include "comdim.cdk"
#include "comlun.cdk"
#include "comcorr.cdk"
#include "comgem.cdk"
*
*     Local variables
*
      character*1 cltypvar
      character*2 clnomvar
      character*8 cletiket
      INTEGER ini,inj,ink,IERR, IPAK,jk,jk1,jk2
      INTEGER FNOM, FSTOUV, FSTFRM, FCLOS, VFSTECR
      INTEGER IP1,IP2,IP3, IDATYP, IDATEO
      INTEGER vfstlir
      real   zptop4, zpref4,zrcoef4
      real*8 zps,zlev(nflev)
      real*8 zptot(nflev+1,nflev,nj)
C
      EXTERNAL FNOM, FSTOUV, FSTFRM, FCLOS, VFSTECR
      INTEGER IM,JM,KM
!
!!
      IPAK = -32
      IDATYP = 5
      IP1 = 0
      IP2 = 0
      IP3 = 0 
C
!      DO JK1 = 1,NFLEV+1
!        DO JK2 = 1,NFLEV
!          write(nulout,*) 'post_regres: Write: jk1,jk2,ptot(jk1,jk2,1)=',
!     &         jk1,jk2,ptot(jk1,jk2,1)
!        enddo
!      enddo
!
      cletiket = 'P_TO_TGU'
!
      IERR = VFSTECR(PTOT(1,1,1),PTOT(1,1,1),IPAK,nulinclr
     S        ,IDATEO,0,0,NFLEV+1,NFLEV,NJ
     S        ,IP1,IP2,IP3,'X','ZZ',cletiket,'X'
     S        ,0,0,0,0,IDATYP,.TRUE.)
!
      ini = nflev + 1
      inj = nflev
      ink = nj
      ip1 = -1
      ip2 = -1
      ip3 = -1
      idateo = -1
      cltypvar = 'X'
      clnomvar = 'ZZ'
!
      ierr = vfstlir(zptot,nulinclr,ini,inj,ink,idateo,     ! N-->S
     &               cletiket,ip1,ip2,ip3,cltypvar,clnomvar)
!
!      DO JK1 = 1,NFLEV+1
!        DO JK2 = 1,NFLEV
!          write(nulout,*) 'post_regres:  Read: jk1,jk2,zptot(jk1,jk2,1)=',
!     &         jk1,jk2,zptot(jk1,jk2,1)
!        enddo
!      enddo
C
      IERR = VFSTECR(THETA(1,1),THETA(1,1),IPAK,nulinclr
     S        ,IDATEO,0,0,NFLEV,NJ,1
     S        ,IP1,IP2,IP3,'X','ZZ','THETA   ','X'
     S        ,0,0,0,0,IDATYP,.TRUE.)
C
      RETURN
      END