C**********************************************************                  HEXP.2     
C             SUBROUTINE HEXP                                                HEXP.3     
C**********************************************************                  HEXP.4     
      SUBROUTINE HEXP(SV,GV,NLS,ITYPE)                                       HEXP.5     
C                                                                            HEXP.6     
C     Performs an indirect Legendre transform for a (set of) field(s)        HEXP.7     
C     having a total of NLS levels, from spectral to Fourier space.          HEXP.8     
C                                                                            HEXP.9     
C     The following types of Legendre function and thence types of           HEXP.10    
C     transform may be used:                                                 HEXP.11    
C        ITYPE=1,2  :  ALP   :  ALPN(,,,1)   :  normal transform.            HEXP.12    
C        ITYPE=3,4  :  DALP  :  ALPN(,,,2)   :  y-derivative.                HEXP.13    
C        ITYPE=5,6  :  RLP   :  ALPN(,,,3)   :  del(-2).                     HEXP.14    
C        ITYPE=7,8  :  RDLP  :  ALPN(,,,4)   :  y-derivative of del(-2).     HEXP.15    
C     An even/odd value of ITYPE denotes a spectral field of even/odd        HEXP.16    
C     symmetry.                                                              HEXP.17    
C                                                                            HEXP.18    
C     Version for RSGUP3.                     Mike Blackburn,  10.01.95.     HEXP.19    
C                                                                            HEXP.20    
C                                                                            PARAM1.2     
C     Determines model resolution                                            PARAM1.3     
C                                                                            PARAM1.4     
      PARAMETER(NN=21,MM=21,NHEM=2,NL=5,MOCT=1,MG=64,JG=16,NWJ2=121          NETCDF.35    
     P         ,NCRAY=64,JGL=JG,NTRAC=1,NLEVRF=1)                            NETCDF.36    
                                                                             PARAM1.7     
C                                                                            PARAM1.8     
C                                                                            PARAM2.2     
C     Sets basic constants, especially those needed for array dimensions     PARAM2.3     
C                                                                            PARAM2.4     
      PARAMETER(MH=2,PI=3.14159265359,PI2=2.0*PI                             PARAM2.5     
     +,NNP=NN+1,MGPP=MG+2,JGP=JG+1,JGG=JG*NHEM,JGGP=JGG+1,MJP=NWJ2+NWJ2      PARAM2.6     
     +,NLM=NL-1,NLP=NL+1,NLPP=NL+2,NLA=NL+3,NLB=NL+4,NL2=NL*NL               PARAM2.7     
     +,IDA=(MG+MG+MG)/2+1,IDB=NWJ2*NL,IDC=IDB+IDB,IDD=MGPP*NL                PARAM2.8     
     +,IDE=NL2*NN,IDF=NCRAY*(MG+1),IDG=JG*NL,IDH=JG*MG                       PARAM2.9     
     +,IDI=NNP/2,IDJ=IDI*IDI,IDK=NL*IDI,IDL=MGPP/2,IDM=NNP/2,IDN=IDM*NL      PARAM2.10    
     +,NWW=1+(MM-1)/MOCT)                                                    PARAM2.11    
      PARAMETER(IGA=NWJ2*NHEM,IGB=IDB*NHEM,IGC=MGPP*NHEM,IGD=IDD*NHEM        PARAM2.12    
     +,IGG=IDG*NHEM,IGL=IDL*NHEM,IGM=IDM*NHEM,IGN=IDN*NHEM                   PARAM2.13    
     +,IGO=IGA+IGA,IGP=IGB+IGB,NFTWG=(5+NTRAC)*NL+3                          PARAM2.14    
     +,NFTGW=(6+3*NTRAC)*NL+2,NFTGD=(3+NTRAC)*NL,NLTR=NL*NTRAC)              PARAM2.15    
C     Number of 2D (surface) output fields. This value is                    PARAM2.16    
C     Doubled due to averaged and instantaneous fields.                      PARAM2.17    
      PARAMETER (N2DFLD=21,NGRPAD=N2DFLD*2*IGC)                              PARAM2.18    
C                                                                            PARAM2.19    
C                                                                            BLANK.2     
C     Basic planetary parameters for run plus information about              BLANK.3     
C     vertical grid structure                                                BLANK.4     
C                                                                            BLANK.5     
C     Note that RD and GASCON are identical and CPD is set from RD,AKAP.     BLANK.6     
      COMMON        SQ(NNP),RSQ(NNP),SIGMAH(NLM),SIGMA(NL)                   BLANK.7     
     +              ,T01S2(NLM),T0(NL),ALPHA(NL),DSIGMA(NL),RDSIG(NL)        BLANK.8     
     +              ,TKP(NL),C(NL2),SQH(NNP)                                 BLANK.9     
     +              ,MF,MFP,JZF,NF,NFP                                       BLANK.10    
     +              ,AKAP,GA,GASCON,RADEA,WW,P0,PFAC,EZ,AIOCT                BLANK.11    
     +              ,RD,RV,CPD,CLATNT                                        BLANK.12    
     +              ,LRSTRT,LSHORT,LTVEC,LSTRETCH                            BLANK.13    
     +              ,LFLUX                                                   BLANK.14    
     +              ,LBALAN,LRESTIJ                                          BLANK.15    
     +              ,LCLIM, LPERPET, L22L,LOROG ,LCSFCT                      BLANK.16    
     +              ,LNOISE                                                  BLANK.17    
      COMPLEX EZ,AIOCT                                                       BLANK.18    
      LOGICAL LRSTRT,LSHORT,LTVEC,LSTRETCH,LBALAN,LRESTIJ                    BLANK.19    
     +       ,LFLUX,LNOISE                                                   BLANK.20    
     +       ,LCLIM, LPERPET, L22L,LOROG,LCSFCT                              BLANK.21    
C                                                                            BLANK.22    
C                                                                            LEGAU.2     
C     Legendre polynomials and information about gaussian latitudes          LEGAU.3     
C                                                                            LEGAU.4     
      COMMON/LEGAU/ ALPJ(MJP),DALPJ(MJP)                                     LEGAU.5     
     +              ,ALP(NWJ2,2,JGL),DALP(NWJ2,2,JGL)                        LEGAU.6     
     +              ,RLP(NWJ2,2,JGL),RDLP(NWJ2,2,JGL)                        LEGAU.7     
     +              ,SI(JGG),CS(JGG),SISQ(JGG),CSSQ(JGG),SECSQ(JGG)          LEGAU.8     
     +              ,ALAT(JGG),GWT(JGG),AW(JGG),JH,JL,JINC                   LEGAU.9     
C                                                                            LEGAU.10    
      COMPLEX     SV(IGA,NLS),GV(IGL,NLS),TEMP                               HEXP.25    
      REAL        ALPN(NWJ2,2,JGL,4)                                         HEXP.26    
      EQUIVALENCE (ALPN(1,1,1,1),ALP(1,1,1))                                 HEXP.27    
C                                                                            HEXP.28    
 6900 FORMAT(/' ***ABORT : HEXP CALLED WITH INVALID TYPE =',I5)              HEXP.29    
C                                                                            HEXP.30    
C     Use ITYPE to define transform type and symmetry labels.                HEXP.31    
C     ISPAR is symmetry of spectral field    = 0 for D,T,SP etc.             HEXP.32    
C                                            = 1 for Z.                      HEXP.33    
C     IGPAR is symmetry of Fourier field: same as ISPAR unless transform     HEXP.34    
C                                         involves a d/dy.                   HEXP.35    
C                                                                            HEXP.36    
      IF (ITYPE.LE.0.OR.ITYPE.GE.9) THEN                                     HEXP.37    
         WRITE(2,6900) ITYPE                                                 HEXP.38    
         CALL ABORT                                                          HEXP.39    
      ENDIF                                                                  HEXP.40    
      IALP=(ITYPE+1)/2                                                       HEXP.41    
      ISPAR=MOD(ITYPE,2)                                                     HEXP.42    
      IGPAR=ISPAR                                                            HEXP.43    
      IF (IALP.EQ.2.OR.IALP.EQ.4) IGPAR=1-ISPAR                              HEXP.44    
C                                                                            HEXP.45    
C     Perform inverse Legendre transform from spectral space to form         HEXP.46    
C     the even and odd contributions to the Fourier transforms.              HEXP.47    
C     Separate code for NHEM=1,2 to increase efficiency.                     HEXP.48    
C                                                                            HEXP.49    
      IF (NHEM.EQ.1) THEN                                                    HEXP.50    
         IA=ISPAR+1                                                          HEXP.51    
         IM=0                                                                HEXP.52    
         IP=0                                                                HEXP.53    
         DO 10 M=0,MM-1,MOCT                                                 HEXP.54    
            IM=IM+1                                                          HEXP.55    
            DO 10 N=M,NN-1,2                                                 HEXP.56    
               IP=IP+1                                                       HEXP.57    
               DO 10 L=1,NLS                                                 HEXP.58    
                  GV(IM,L)=GV(IM,L)+ALPN(IP,IA,JL,IALP)*SV(IP,L)             HEXP.59    
   10    CONTINUE                                                            HEXP.60    
      ELSE                                                                   HEXP.61    
         IA=ISPAR+1                                                          HEXP.62    
         IB=2-ISPAR                                                          HEXP.63    
         IM=0                                                                HEXP.64    
         IP=0                                                                HEXP.65    
         DO 20 M=0,MM-1,MOCT                                                 HEXP.66    
            IM=IM+1                                                          HEXP.67    
            IG=IM+IDL                                                        HEXP.68    
            DO 20 N=M,NN-1,2                                                 HEXP.69    
               IP=IP+1                                                       HEXP.70    
               DO 20 L=1,NLS                                                 HEXP.71    
                  GV(IM,L)=GV(IM,L)+ALPN(IP,IA,JL,IALP)*SV(IP     ,L)        HEXP.72    
                  GV(IG,L)=GV(IG,L)+ALPN(IP,IB,JL,IALP)*SV(IP+NWJ2,L)        HEXP.73    
   20    CONTINUE                                                            HEXP.74    
      ENDIF                                                                  HEXP.75    
C                                                                            HEXP.76    
C     For a global run, sum and difference even and odd contributions        HEXP.77    
C     to give the complete Fourier transforms at the northern and            HEXP.78    
C     southern latitude rows.  Separate code for each symmetry:              HEXP.79    
C        IGPAR=0 : even (IA) precedes odd (IB).                              HEXP.80    
C        IGPAR=1 : odd (IA) precedes even (IB).                              HEXP.81    
C                                                                            HEXP.82    
      IF (NHEM.EQ.2) THEN                                                    HEXP.83    
         IF (IGPAR.EQ.0) THEN                                                HEXP.84    
            DO 30 IA=1,NWW                                                   HEXP.85    
               IB=IA+IDL                                                     HEXP.86    
               DO 30 L=1,NLS                                                 HEXP.87    
                  TEMP=GV(IA,L)                                              HEXP.88    
                  GV(IA,L)=TEMP+GV(IB,L)                                     HEXP.89    
                  GV(IB,L)=TEMP-GV(IB,L)                                     HEXP.90    
   30       CONTINUE                                                         HEXP.91    
         ELSE                                                                HEXP.92    
            DO 40 IA=1,NWW                                                   HEXP.93    
               IB=IA+IDL                                                     HEXP.94    
               DO 40 L=1,NLS                                                 HEXP.95    
                  TEMP=GV(IA,L)                                              HEXP.96    
                  GV(IA,L)=GV(IB,L)+TEMP                                     HEXP.97    
                  GV(IB,L)=GV(IB,L)-TEMP                                     HEXP.98    
   40       CONTINUE                                                         HEXP.99    
         ENDIF                                                               HEXP.100   
      ENDIF                                                                  HEXP.101   
C                                                                            HEXP.102   
      RETURN                                                                 HEXP.103   
      END                                                                    HEXP.104   
