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

      subroutine gausscornsla 1,3
*
***s/r gausscornsla  - Characterization of the forecast error correlation in terms of Gaussian functions
*                    LAM4D mode version: Grd_typ.eq.'LU'
*
*Author  : Luc Fillon - ARMA/MSC - 17 Jan 2006.
*Revision:
* Luc Fillon - ARMA/EC - 14 Jul 08 - Finalise validation of clcorspace='GD' option.
* Luc Fillion - ARMA/EC - 14 Jan 2009 - Upgrade to v_10_1_2: CORNS extra dimension.
*    -------------------
**    Purpose: to initialize the inverse forecast error correlation
*
*Arguments
*
      implicit none
*implicits
#include "taglam4d.cdk"
#include "comdim.cdk"
#include "comlun.cdk"
#include "comcst.cdk"
#include "comct0.cdk"
#include "comcva.cdk"
#include "comcorr.cdk"
#include "comfftla.cdk"
*
      integer jfi,jfj,jk,ik2,inf2,ji,jj,jla,j1,j2
      integer jrow,jcol,jband,imaxmod
      real*8 zlpsi2, zlchi2, zlt2, zlq2, zlps2, zk2
      real*8 zcon,zfacpp,znorm,zsum
      real*8 zveval(nksdim)
!
      real*8 zvevec(nksdim,nksdim)
      real*8 zwmatv1(nksdim,nksdim), zwmatv2(nksdim,nksdim)
      real*8 zwmatv3(nksdim,nksdim)
      real*8 zwvecv1(nksdim), zwvecv2(nksdim)
*
**
      chcorg = 'G'
      cvcorg = 'G'
!
      corns(:,:,:,:) = 0.0
!
!*1.  Horizontal forecast error correlation
!     -------------------------------------
!
      if(chcorg.eq.'I') then
        write(nulout,FMT='(8X,"- IDENTITY MATRIX USED -")')
        write(nulout,*) 'gausscornsla: Done Using small Horiz. Correl. length'
      else if(chcorg.eq.'G') then
        write(nulout,FMT='(8X,"- Gaussian -")')
      endif
!
      call hcorla_simul0
      if(lcorloc) call corlocla
!
 900  format(100(E13.7,1X) )
!
!*2.  Initialize the vertical error correlations
!     ------------------------------------------
!
      if(.not.lsw) call suvercor
!
!*3.  Build CORNS
!     -----------
!
      if(lsw) then ! Barotropic      
        do jband = 1, nband
          do jk = 1,nksdim
            corns(jk,jk,jband-1,1) = rstddev(jk,jband-1)**2
          enddo
        enddo
      else        ! Baroclinic
        do jband = 1, nband
          write(nulout,*) 'GAUSSCORNSLA: band ',jband
          write(nulout,*) '-----------------------------'
          do jcol = 1,nksdim
          do jrow = 1,nksdim
            corns(jrow,jcol,jband-1,1) = rstddev(jrow,jband-1)
     &         * corns(jrow,jcol,jband-1,1)* rstddev(jcol,jband-1)
          enddo
          enddo
        enddo
      endif
!
      return
      end