!-------------------------------------- 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 cormdl(pgd,pcscl,cdcortyp,kjk,kic,kjc,knk) 13,1
*
***s/r cormdl: Computes horizontal correlations
*
*Author: Luc Fillion CGD/NCAR - Jul 99
*
*Revision: Luc Fillion - ARMA/EC - 16 Jul 08 - Refine and udpdate for validation of 1-Obs experiment in SW mode.
*
      implicit none
*
#include "taglam4d.cdk"
#include "comdim.cdk"
#include "comlun.cdk"
#include "comcst.cdk"
#include "comgdpar.cdk"
#include "comfftla.cdk"
#include "comgrd_param.cdk"
#include "comgrd.cdk"
*
      character*1 cdcortyp
      integer klev,kic,kjc,kjk,knk
      real*8 pcscl
      real*8 pgd(ni,knk,nj)
*
      logical llprint
      integer  ji,jj
      real*8 zx,zy,zr,zcon,zdx,zsum,zdy,zcscl
      real*8 DLALPHA, DLFAC, DLTEMP, DLN, DLCSURN, DLC, DLCORR
*
**
      llprint = .false.
!
      if(pcscl.lt.0.) then
        write(nulout,*) 'cormdl: pcscl = ',pcscl
        write(nulout,*) 'cormdl: kic,kjc = ',kic,kjc
        call abort3d(nulout,'cormdl: pcscl.le.0.')
      endif
!
      zcscl = pcscl
      if(zcscl.eq.0.) then
        zcscl = dylam(kic,kjc)   ! minimum 
      endif
!
      pgd(:,:,:) = 0.0
!
      if (cdcortyp.eq.'G') then
        zdy=dylam(kic,kjc)    ! will use mesh distance at center of Gaussian...
        zdx = zdy
        if(llprint) write(nulout,*) 'cormdl: kic,kjc,zdx,zdy=',kic,kjc,zdx,zdy
c
c     Gaussian correlation
c
        zsum=0.0
        dlc = 1.0/zcscl
        dlc = 0.5*dlc*dlc
        do ji = 1,ni
        do jj = 1,nj
          zx=(ji-kic)*zdx
          zy=(jj-kjc)*zdy
          zr = sqrt(zx**2+zy**2)
          zcon=-(zr**2)*dlc
          if(zcon.lt.-50.) zcon=-50.
          dlcorr = exp(zcon)
          pgd(ji,kjk,jj) = dlcorr
          zsum=zsum+dlcorr**2
        enddo
        enddo
        if(llprint) then
          write(nulout,*) 'cormdl: ni,nj,kic,kjc,zcscl,zdx,zdy=',
     &      ni,nj,kic,kjc,zcscl,zdx,zdy
          do ji = 1,ni
            write(nulout,*) 'cormdl: ji, pdg(ji,1,kjc)=',
     &      ji,pgd(ji,kjk,kjc)
          enddo
        endif
      else
        write(nulout,*) 'CORMDL: Undefined correlation type'
      endif

      return
      end