!-------------------------------------- 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 REESPE(KFIELD,PSP,PGD 28,3
     S     ,KLA, KIBEG, KIEND, KJBEG, KJEND, KDIM)
#if defined (DOC)
*
***s/r REESPE  - Spectral Transform for a number of scalar fields
*
*Author  : P. Gauthier/P. Koclas *ARMA/AES/CMC  November  1997
*Revision:
*          JM Belanger CMDA/SMC  Jul 2000 
*                   . 32 bits conversion 
*     P. Gauthier ARMA#MSC July 2003
*     .           Removal of commulti (not needed with OpenMP) and correction for the treatment
*     .           an odd number of latitudes circles.
*     L. Fillion ARMA/EC 11 May 2010 - Limit printout to processor 0.
*Arguments
*     i   KFIELD            : number of fields to be processed
*     o   PSP(KLA,2,KDIM)   :  array of spectral coefficients
*     i   PGD(KIBEG:KIEND,KFIELD,KJBEG:KJEND)
*     .                     : grid-point field
*     Following parameters are only necessary for the dimensioning
*     of PSP and PGD. Their equivalent global variables are used.
*     i   KLA               : total number of spectral coefficients
*     i   KIBEG, KIEND      : equivalent of NIBEG and NIEND
*     i   KJBEG, KJEND      : equivalent of NJBEG and NJEND
*     i   KDIM              : total number of spectral fields
*     .
#endif
*
      USE procs_topo
      IMPLICIT NONE
*implicits
#include "comdim.cdk"
#include "comlun.cdk"
                                !
      INTEGER :: KFIELD
      INTEGER :: KLA, KIBEG, KIEND, KJBEG, KJEND, KDIM
      REAL*8  :: PSP(KLA,2,KDIM), PGD(KIBEG:KIEND,KDIM,KJBEG:KJEND)
                                !
      INTEGER :: ILEN, JK, JLON, JLAT,INCPU, ISIZ
      REAL*8  :: ZFM(NI+2,KFIELD, NJ)
      IF(myid == 0) THEN
        write(nulout,fmt='(/,4x,A,i3,A)')'REESPE- Spectral transform of ',kfield,' scalar fields'
      endif

                                !     
                                !     1. Transfer global fields to local array
                                !    
      zfm(1:ni,1:kfield,1:nj) = pgd(1:ni,1:kfield,1:nj)
                                !
                                !     2. Apply the FFT
                                !
      CALL FFT3DVAR(ZFM,1,NI+2,KFIELD,1,NJ,KFIELD,-1)
                                !
                                ! 3. Direct Legendre transform
                                !
      CALL REESPEPAR(PSP,ZFM,NI,NJ,KLA, KDIM,KFIELD)
                                !
      END subroutine reespe