!-------------------------------------- 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 SU1CSE1(KULSTAT,KULCORNS,KULSTDEV) 5,2
#if defined (DOC)
*
***s/r SU1CSE1  - Set-up for the computations of forecast error
*     .           correlations based on normalized increments
*
*Author  : P. Gauthier *ARMA/AES  August 12, 1994
*Revision: M. Buehner  1998
*          C. Charette *ARMA/AES  Jan 1999
*          - Added LBALDIV, LNODIV
*          P. Koclas   *CMC/CMDA  Jul 1999
*          -  Y2K conversion (minor.. change default date)
*          L.Fillion - ARMA/MSC - 28 Sep 2005 - Introduce LAM4D option.
*            . clean the code.
*          L.Fillion - ARMA/EC - Update lam4d to v_10_0_3.
*          L.Fillion - ARMA/EC - Oct 2008 - Include further logical switch lpsifromglb,lpsifromlam, etc...
*          L.Fillion - ARMA/EC - 12 Jan 2009 - Upgrade lam4d to v_10_1_2.
*    -------------------
**    Purpose: set-up of all that is required to characterize the
*     .        i. the files containing the normalized increments
*     .       ii. decide if it is a restart job in which case the
*     .           spectral correlations and variances must be read
*     .           from a file on unit KULCORNS and KULSTDEV respectively
*     .           The number of members of the ensemble already processed
*     .           is also read on these files file (on KULCORNS). See the post-
*     .           processing step of CSE1
*     .
*Arguments
*     KULSTAT: logical unit on which the file is located
*     KULCORNS: logical unit number
*     KULSTDEV: logical unit number
*
*     Remark: the parameters that define the files and their content
*     ------  are within COMCSE1 and controlled by the corresponding
*     .       namelist NAMCSE1. The way the file is accessed is controlled
*     .       as usual through COMGDPAR
#endif
      IMPLICIT NONE
*implicits
#include "comdim.cdk"
#include "comlun.cdk"
#include "comgrd_param.cdk"
#include "comcorr.cdk"
#include "comcva.cdk"
#include "comcse1.cdk"
#include "comgrd.cdk"
#include "comfftla.cdk"
C
      INTEGER KULSTAT, KULCORNS, KULSTDEV
C	
      INTEGER IERR, JN, JK1, JK2, J, jband, jm
C
C*    1. Default values
C     -----------------
C
      NFLSTAT = 1
      CFLNAMES(1 ) = 'FSTSTAT '
C
      DO J =2, 200
         CFLNAMES(J) = '--'
      END DO
C
      NENSEMBLE   = 0
      LSTATREAD    = .FALSE.
      LSTATCON    = .FALSE.
C
      cstats_step='ALL    '
      CFLCORNS  = '  '
      CFLSTDEV  = '  '
      if(grd_typ.ne.'LU') then
        CFLPTOT   = 'p_to_t.fst'
        CFLCORNS  = 'corr.fst'
        CFLSTDEV  = 'rstd.fst'
      endif
!
      NCSE1 = 3
      LBALDIV   = .TRUE.
      lnodiv    = .FALSE.
      NREGION   = 0
      lmcstats  = .FALSE.
      lpsifromglb  = .FALSE.
      lpsifromlam  = .FALSE.
      lflt_low  = .FALSE.
      lflt_high  = .FALSE.
      mflt_trunc  = -999
C
C*    2.  Read the NAMELIST NAMCSE1
C     -----------------------------
C
      WRITE(NULOUT, 9210)
 9210 FORMAT(///,5X,"SU1CSE1- Set-up for the computations of"
     S     ," forecast error correlations based on"
     S     ,/,14x," normalized increments")
C
      CALL READNML('NAMCSE1',IERR)
C
      IF(NCSE1.EQ.100) LSTATREAD = .TRUE.
C
      WRITE(NULOUT,*) 'cstats_step:',cstats_step
      WRITE(NULOUT,*) 'CETIKETERR:',CETIKETERR
      WRITE(NULOUT,*) 'CFLCORNS:',CFLCORNS
      WRITE(NULOUT,*) 'NFLSTAT:',NFLSTAT
      WRITE(NULOUT,*) 'LMCSTATS:',LMCSTATS
      WRITE(NULOUT,*) 'mbal_order:',mbal_order
      WRITE(NULOUT,*) 'lpsifromglb:',lpsifromglb
      WRITE(NULOUT,*) 'lpsifromlam:',lpsifromlam
      WRITE(NULOUT,*) 'lflt_low:',lflt_low
      WRITE(NULOUT,*) 'lflt_high:',lflt_high
      WRITE(NULOUT,*) 'mflt_trunc:',mflt_trunc
C
C     2.1 Reset LBALDIV
C
      IF (LNODIV) THEN
         LBALDIV = .FALSE.
      ENDIF
C
      NANALVAR = 0
C
C*    3. Initializing CORNS and RSTDDEV
C     ---------------------------------
C
      IF(LSTATREAD) THEN
*
*     .  3.1 Read CORNS and RSTDDEV from a file
*
         CALL READCORNS(KULCORNS,CFLCORNS,KULSTDEV,CFLSTDEV,
     S                  NDATESTAT,NENSEMBLE)
C
      ELSE
C
C*    .   3.2 Case where the computation is started from scratch.
C
        NENSEMBLE = 0
        NDATESTAT = 010189000
        rstddev(:,:) = 0.
        corns(:,:,:,:) = 0.
      END IF
C
      WRITE(NULOUT,9411)NENSEMBLE, NDATESTAT
 9411 FORMAT(//,4X,"Current estimate of the correlations is based"
     S     ," on an ensemble of ",I3," members starting at date"
     S     ,1X,I10)
C
      RETURN
      END