!-------------------------------------- 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 sustatsla 2,15
#if defined (DOC)
*
***s/r sustatsla  - Initialize the LAM4D Background Error Statistics.
*
*Author:  Luc Fillion - ARMA/MSC  12 Oct 2005
*Revision: L. Fillion - ARMA/MSC - 18 Jan 2006 - Clean-up.
*     L. Fillion ARMA/MSC May 2006: Upgrade to v10_0_0.
*     L. Fillion ARMA/MSC 10 Aug 2009: Introduce cptot.ne.'NO" to avoid reading ptot matrix in mbal_order .ne. 0 mode.
*     L. Fillion ARMA/MSC 31 May 2010: Writing corrlength for root processor only.
*     L. Fillion ARMA/MSC 12 Jul 2010: Better initialization of Tg related statistics.
**
#endif
      USE procs_topo ,ONLY  :myid
      IMPLICIT NONE
*
*     implicits
*
#include "taglam4d.cdk"
#include "pardim.cdk"
#include "comct0.cdk"
#include "comdim.cdk"
#include "comcva.cdk"
#include "comlun.cdk"
#include "comgdpar.cdk"
#include "compstat.cdk"
#include "comstate.cdk"
#include "comgrd_param.cdk"
#include "comspg.cdk"
*
*     Arguments: None.
*
      integer ierr
!
!!
      WRITE(NULOUT,FMT=9100)
 9100 FORMAT(//,6X,'sustatsla: Sets LAM4D Background Error Statistics')
!
      DAMPLIBG(:,:,:) = 1.0D0
!
      if(nanalvar.eq.4) then
        call readcvsdev
        if(nconf.eq.300) then
          call readcornsla
        else
          call readcorns_min
        endif
      else
!
!       Read in PtoT - operator needed for balance
!
        if(.not.lsw) then
          if(mbal_order.gt.0.and.cptot.ne.'NO') then
            if(lsimulcor) then
              call rdptotla_simul
            else
              if(cptot.eq.'SP') then
                call rdsptotla
              else
                call rdptotla
              endif
            endif
          endif
        endif
!
!       Prepare control variable spectral correlations CORNS
!
        CALL READNML('NAMPSTAT',IERR)
!
        if(lsimulcor) then
          call sucovla
        else
          if(lcornsmin) then
            call readcorns_min
          else
            call readcornsla
            IF(nsexist(nstg).eq.1) THEN
!              call sutg_corla(nulbgst) ! will overwrite Tg corns if already built in stats program and read above by readcornsgl
            endif
          endif
          if(lsdevsim) then
            call sim_cv_sdev_la
          else
            call readcvsdev
          endif
        endif
      endif
!
      if(myid == 0.and.nanalvar.eq.3.and.(.not.lcornsmin)) then
        call corrlengthla(999,.false.,.true.) ! final diagnostic and ensure rhcorl array is representative. 
      else if(nanalvar.eq.4) then
        call corrlengthla2(999,.false.,.true.) ! final diagnostic and ensure rhcorl array is representative. 
      endif
!
      return
      end