!-------------------------------------- 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 GDSP 22,4
#if defined (DOC)
*
C**s/r GDSP  - Spectral Transform
*
*     Purpose: spectral transform of the model state including conversion of
*     .        wind images to vorticity and divergence
*
*     Author  : P. Gauthier/P.Koclas *ARMA/AES and CMC
*     .                   - May 1997 Complete rewriting of the transform
*     .                   to have the most external loop to be on the zonal wavenumber
*     .                   and then to have a parallel treatment of the transform
*     Revision:
*
*     JM Belanger CMDA/SMC  Jul 2000 
*                   . 32 bits conversion 
*     P. Koclas CMDA/SMC  Apr 2003 
*                   . removed commulti and stkmem
*     P. Gauthier ARMA#MSC July 2003
*     .             Bugfix needed to handle an odd number of latitude circles
*     Luc Fillion/Christian Page - Summer 2003
*               - Include NCAR 2D-FFT for Limited-Area analysis
*     Luc Fillion - 07 Jul 2004 - Update to NCAR's new fftpack5.
*     Luc Fillion - oct 2004 - Add RPN's 2D FFT option.
*     Luc Fillion - ARMA/EC - 14 Aug 2007 - Update to v_10_0_3.
*     Luc Fillion - ARMA/EC - 11 May 2009 - Update to v_10_2_2.
*
#endif
*
      IMPLICIT NONE
*implicits
#include "comlun.cdk"
#include "pardim.cdk"
#include "comdim.cdk"
#include "comgrd_param.cdk"
#include "comgd0.cdk"
#include "comsp.cdk"
#include "comfftla.cdk"
#include "comgem.cdk"
C     
      INTEGER :: ILEN, JK, JLON, JLAT, INCPU, ISIZ, jla
C
      REAL*8 :: ZFM(NI+2,NKSDIM,NJ)
      real*8 :: zfmla(ni,nksdim,nj), zsp(nla,2,nksdim)
      real*8 :: zgdxy(ni,nj)
C     
C
C     1. Fourier transform all fields for all latitudes
C        ----------------------------------------------
C
      if (grd_typ.eq.'GU') then
        zfm(:,:,:) = 0.d0
                                !
        zfm(1:ni,1:nksdim,1:nj) = gd(1:ni,1:nksdim,1:nj)
                                !
C
        CALL FFT3DVAR(ZFM,1,NI+2,NKSDIM,1,NJ,NKSDIM,-1)
      else if (grd_typ.eq.'LU') then
        do jk = 1, nksdim
          do jlat = 1, nj
            do jlon = 1, ni
              zfmla(jlon,jk,jlat) = gd(jlon,jk,jlat)
              zgdxy(jlon,jlat) = gd(jlon,jk,jlat)
            end do
          end do
          if(lrpnfft) then
            call dft2dr(zsp(1,1,jk),zgdxy)
          else
            call dft2d(zsp,zfmla,sdft2d,nindxy,mlen2d,
     &                 jk,nla,ni,nj,nksdim)
          endif
          do jla = 1, nla
            sp(jla,1,jk) = zsp(jla,1,jk)
            sp(jla,2,jk) = zsp(jla,2,jk)
          enddo
        enddo
      endif
C
C     2. Direct Legendre transform including wind transformations
C        --------------------------------------------------------
C     
 200  CONTINUE
      if (grd_typ.eq.'GU') then
        CALL GDSPPAR(ZFM,NI,NKSDIM,NJ)
      endif
C
      END subroutine gdsp