!-------------------------------------- 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 writeptotla(kulcorns) 2,4
*
#if defined (DOC)
*
***s/r writeptotla  - For LAM4D option: write the balance operators
*
*Author:  Luc Fillion - ARMA/MSC - Oct 2005.
*Revision: Luc Fillion - ARMA/EC - 11 Dec 2008 - Update w.r.t new approack using cstats_step key.
*
*Arguments: NONE
*
#endif
      IMPLICIT NONE
      integer kulcorns
*implicits
*
*     Global variables
*
#include "taglam4d.cdk"
#include "pardim.cdk"
#include "comdim.cdk"
#include "comcorr.cdk"
#include "comcse1.cdk"
#include "comgem.cdk"
*
*     Local variables
*
      INTEGER IULPTOT
C
C     *    RPN Standard files parameters
C
      INTEGER IERR, IPAK, jj
      INTEGER FNOM, FSTOUV, FSTFRM, FCLOS, VFSTECR
      INTEGER IP1,IP2,IP3, IDATYP, IDATEO
C
      EXTERNAL FNOM, FSTOUV, FSTFRM, FCLOS, VFSTECR
C
cjmo modification temporaire pour valider le code stat avec v804a
      INTEGER IM,JM,KM
!
!!
      iulptot = kulcorns
      IULPTOT = 57
      IERR =  FNOM  (IULPTOT,CFLPTOT,'RND',0)
      IERR =  FSTOUV(IULPTOT,'RND')
C
      IPAK = -32
      IDATYP = 5
      IP1 = 0
      IP2 = 0
      IP3 = nensemble
      IDATEO = ndatestat
C
C WRITES THE LEVELS IN PTOT FILE
C
      IERR = VFSTECR(VLEV,VLEV,IPAK,IULPTOT
     +        ,IDATEO,0,0,NFLEV,1,1
     +        ,IP1,IP2,IP3,'X','LV','ETALEVLS','X'
     +        ,0,0,0,0,IDATYP,.TRUE.)
C
 330  CONTINUE
!
         IP1=1
         IERR = VFSTECR(PTOT(1,1,1),PTOT(1,1,1),IPAK,IULPTOT
     S        ,IDATEO,0,0,NFLEV+1,NFLEV,NJ
     S        ,IP1,IP2,IP3,'X','ZZ','P_TO_TNJ','X'
     S        ,0,0,0,0,IDATYP,.TRUE.)
!
       do jj=1,nj
         IP1=jj
         IERR = VFSTECR(PTOT(1,1,jj),PTOT(1,1,jj),IPAK,IULPTOT
     S        ,IDATEO,0,0,NFLEV+1,NFLEV,1
     S        ,IP1,IP2,IP3,'X','ZZ','P_TO_TJJ','X'
     S        ,0,0,0,0,IDATYP,.TRUE.)
       enddo
C
      IP1 = 0
         IERR = VFSTECR(THETA(1,1),THETA(1,1),IPAK,IULPTOT
     S        ,IDATEO,0,0,NFLEV,NJ,1
     S        ,IP1,IP2,IP3,'X','ZZ','THETA   ','X'
     S        ,0,0,0,0,IDATYP,.TRUE.)
C
 400  CONTINUE
      IERR =  FSTFRM(IULPTOT)
      IERR =  FCLOS (IULPTOT)
C
      RETURN
      END