!-------------------------------------- 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 SPEREE(KFIELD,PSP,PGD 39,2
     S     ,KLA, KIBEG, KIEND, KJBEG, KJEND, KDIM)
#if defined (DOC)
***s/r SPEREE  - Inverse spectral transform for a number of scalar fields
*
*Author  : P. Gauthier/P.koclas *ARMA/AES CMC  November , 1997
*Revision:
*          1. P. KOCLAS ADD CALL TO FFT  Jan 1998
*       JM Belanger CMDA/SMC  Aug 2000 
*                   . 32 bits conversion
*                     (call to FFFT8 instead of FFT771)
*       P. Koclas CMDA/SMC  Apr 2003 
*                 -remove commulti
*       S. Pellerin, ARMA, August 2008
*                 - Call to fft3dvar
*     Purpose:
*Arguments
*     i   KFIELD            : number of fields to be processed
*     i   PSP(KLA,2,KFIELD) :  array of spectral coefficients
*     o   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
      IMPLICIT NONE
*implicits
#include "comdim.cdk"
#include "comct0.cdk"
*
C
      INTEGER KFIELD,ITEMP,IJUMP,ILOT,JULOT,INFFT
      INTEGER KLA, KIBEG, KIEND, KJBEG, KJEND, KDIM
      REAL*8 PSP(KLA,2,KDIM), PGD(KIBEG:KIEND,KDIM,KJBEG:KJEND)
C
      INTEGER ILEN, JGL, JK, JLON, ILONMAX
C
C
C*    1. Inverse Legendre transform
C        --------------------------
 100  CONTINUE
      CALL SPEREEPAR(KFIELD,PSP,PGD
     S     ,KLA, KIBEG, KIEND, KJBEG, KJEND, KDIM)
C
C     2. Inverse fourier transform
C        -------------------------
 200  CONTINUE
C
C
C     2.1 Reset to zero the modes that are not part of the truncation
C         -----------------------------------------------------------
C
      ILONMAX = NIEND
      DO JGL = 1, NJ
         DO JLON = 2*(NTRUNC+1)+1, ILONMAX
            DO  JK = 1, KFIELD
              PGD(JLON,JK,JGL) = 0.
            END DO
         END DO
      END DO
C
      DO JGL =1,NJ
         DO JK = 1, KFIELD
            PGD(0,JK,JGL) = 0.
         END DO
      END DO
C
C     2.2 Apply the FFT 
C         -------------
C
C
C*    5. INVERSE FOURIER TRANSFORM
C     .  -------------------------
 500  CONTINUE
C
      CALL FFT3DVAR(PGD,NIBEG,NIEND,KFIELD,NJBEG,NJEND,KFIELD,+1)

      RETURN
      END