!-------------------------------------- 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 gausscorns 1,2
*
***s/r gausscorns  - Characterization of the forecast error correlation in terms of Gaussian functions
*                    Grd_typ.eq.'GU'
*
*Author  : Luc Fillon - ARMA/EC - 12 Aug 2008.
*Revision:
* 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"
*
      integer jfi,jfj,jk,ik2,inf2,ji,jj,jla,j1,j2
      integer jrow,jcol,jband,imaxmod,jn
      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,*) 'gausscorns: Done Using small Horiz. Correl. length'
      else if(chcorg.eq.'G') then
        write(nulout,FMT='(8X,"- Gaussian -")')
      endif
!
      call hcorglb_simul0
!
 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 jn = 0, ntrunc
          do jk = 1,nksdim
            corns(jk,jk,jn,1) = rstddev(jk,jn)**2
          enddo
        enddo
      else        ! Baroclinic
        do jn = 0, ntrunc
          do jcol = 1,nksdim
          do jrow = 1,nksdim
            corns(jrow,jcol,jn,1) = rstddev(jrow,jn)
     &         * corns(jrow,jcol,jn,1)* rstddev(jcol,jn)
          enddo
          enddo
        enddo
      endif
!
      return
      end