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

      subroutine dft2d(psp,pgd,psdft2d,knindxy,klen2d, 4,2
     &                 kjk,knla,kni,knj,knksdim)
!
!Author: Luc Fillion - CGD/NCAR - 23 sept 2002
!
!Revision : L. Fillion - MSC - 06 jul 04
!           - Update to NCAR fft: fftpack5
!           
!
      IMPLICIT NONE
!implicits
#include "taglam4d.cdk"
#include "comdim.cdk"
#include "comfftla.cdk"
!
      integer kni,knj,knksdim,knla,klen2d,kjk
      integer knindxy(kni/2+1,knj)
      real*8 pgd(kni,knksdim,knj)
      real*8 psp(knla,2,knksdim)   ! fourier coefficients
      real*8 psdft2d(klen2d)
!
      integer ji,jj,ila,ilenwrk,ind,ier
      real*8 zcon
      real*8 za(2*(kni/2+1),knj)
      real*8 zwork(knj,2*(kni/2+1))
      real*8 zsdft2d(klen2d)
!
!!
       call rfft2i_8(kni,knj,psdft2d,klen2d,ier)
!      print *,'sufftla: mlen2d=',klen2d
!      do ji=1,klen2d
!        print *,'dft2d: ji,psdft2d(ji)=',ji,psdft2d(ji)
!      enddo
      do ji=1,kni
      do jj=1,knj
        za(ji,jj)=pgd(ji,kjk,jj)
      enddo
      enddo
      ilenwrk=2*(kni/2+1)
      call rfft2f_8(2*(kni/2+1),kni,knj,za,psdft2d,klen2d,zwork,
     &            ilenwrk,ier)
!
      do ji=1,kni/2+1
        ind=2*(ji-1)+1
        do jj=1,knj
          ila=knindxy(ji,jj)
          psp(ila,1,kjk)=za(ind,jj)
          psp(ila,2,kjk)=za(ind+1,jj)
!          print *,'dft2d: ji,jj,ind,ila,za(ind,jj),za(ind+1,jj)=',
!     &      ji,jj,ind,ila,za(ind,jj),za(ind+1,jj)
        enddo
      enddo
!
      return
      end