C**********************************************************                  HEXP1.2     
C             SUBROUTINE HEXP1                                               HEXP1.3     
C**********************************************************                  HEXP1.4     
      SUBROUTINE HEXP1(SV,GV,NLS,ITYPE)                                      HEXP1.5     
C                                                                            HEXP1.6     
C     Performs an indirect Legendre transform for a single-level field,      HEXP1.7     
C     from spectral to Fourier space.                                        HEXP1.8     
C                                                                            HEXP1.9     
C     The following types of Legendre function and thence types of           HEXP1.10    
C     transform may be used:                                                 HEXP1.11    
C        ITYPE=1,2  :  ALP   :  ALPN(,,,1)   :  normal transform.            HEXP1.12    
C        ITYPE=3,4  :  DALP  :  ALPN(,,,2)   :  y-derivative.                HEXP1.13    
C        ITYPE=5,6  :  RLP   :  ALPN(,,,3)   :  del(-2).                     HEXP1.14    
C        ITYPE=7,8  :  RDLP  :  ALPN(,,,4)   :  y-derivative of del(-2).     HEXP1.15    
C     An even/odd value of ITYPE denotes a spectral field of even/odd        HEXP1.16    
C     symmetry.                                                              HEXP1.17    
C                                                                            HEXP1.18    
C     Version for RSGUP3.                     Mike Blackburn,  10.01.95.     HEXP1.19    
C                                                                            HEXP1.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),GV(IGL),TEMP                                           HEXP1.25    
      REAL ALPN(NWJ2,2,JGL,4)                                                HEXP1.26    
      EQUIVALENCE (ALPN(1,1,1,1),ALP(1,1,1))                                 HEXP1.27    
C                                                                            HEXP1.28    
 6900 FORMAT(/' ***ABORT : HEXP1 CALLED WITH INVALID TYPE =',I5)             HEXP1.29    
 6910 FORMAT(/' ***ABORT : HEXP1 CALLED WITH NLS =',I3,' : MUST BE 1')       HEXP1.30    
C                                                                            HEXP1.31    
C     Check this is a single-level call.                                     HEXP1.32    
C                                                                            HEXP1.33    
      IF (NLS.NE.1) THEN                                                     HEXP1.34    
         WRITE(2,6910) NLS                                                   HEXP1.35    
         CALL ABORT                                                          HEXP1.36    
      ENDIF                                                                  HEXP1.37    
C                                                                            HEXP1.38    
C     Use ITYPE to define transform type and symmetry labels.                HEXP1.39    
C     ISPAR is symmetry of spectral field    = 0 for D,T,SP etc.             HEXP1.40    
C                                            = 1 for Z.                      HEXP1.41    
C     IGPAR is symmetry of Fourier field: same as ISPAR unless transform     HEXP1.42    
C                                         involves a d/dy.                   HEXP1.43    
C                                                                            HEXP1.44    
      IF (ITYPE.LE.0.OR.ITYPE.GE.9) THEN                                     HEXP1.45    
         WRITE(2,6900) ITYPE                                                 HEXP1.46    
         CALL ABORT                                                          HEXP1.47    
      ENDIF                                                                  HEXP1.48    
      IALP=(ITYPE+1)/2                                                       HEXP1.49    
      ISPAR=MOD(ITYPE,2)                                                     HEXP1.50    
      IGPAR=ISPAR                                                            HEXP1.51    
      IF (IALP.EQ.2.OR.IALP.EQ.4) IGPAR=1-ISPAR                              HEXP1.52    
C                                                                            HEXP1.53    
C     Perform inverse Legendre transform from spectral space to form         HEXP1.54    
C     the even and odd contributions to the Fourier transforms.              HEXP1.55    
C     Separate code for NHEM=1,2 to increase efficiency.                     HEXP1.56    
C                                                                            HEXP1.57    
      IF (NHEM.EQ.1) THEN                                                    HEXP1.58    
         IA=ISPAR+1                                                          HEXP1.59    
         IM=0                                                                HEXP1.60    
         IP=0                                                                HEXP1.61    
         DO 10 M=0,MM-1,MOCT                                                 HEXP1.62    
            IM=IM+1                                                          HEXP1.63    
            DO 10 N=M,NN-1,2                                                 HEXP1.64    
               IP=IP+1                                                       HEXP1.65    
               GV(IM)=GV(IM) + ALPN(IP,IA,JL,IALP)*SV(IP)                    HEXP1.66    
   10    CONTINUE                                                            HEXP1.67    
      ELSE                                                                   HEXP1.68    
         IA=ISPAR+1                                                          HEXP1.69    
         IB=2-ISPAR                                                          HEXP1.70    
         IM=0                                                                HEXP1.71    
         IP=0                                                                HEXP1.72    
         DO 20 M=0,MM-1,MOCT                                                 HEXP1.73    
            IM=IM+1                                                          HEXP1.74    
            DO 20 N=M,NN-1,2                                                 HEXP1.75    
               IP=IP+1                                                       HEXP1.76    
               GV(IM    )=GV(IM    ) + ALPN(IP,IA,JL,IALP)*SV(IP     )       HEXP1.77    
               GV(IM+IDL)=GV(IM+IDL) + ALPN(IP,IB,JL,IALP)*SV(IP+NWJ2)       HEXP1.78    
   20    CONTINUE                                                            HEXP1.79    
      ENDIF                                                                  HEXP1.80    
C                                                                            HEXP1.81    
C     For a global run, sum and difference even and odd contributions        HEXP1.82    
C     to give the complete Fourier transforms at the northern and            HEXP1.83    
C     southern latitude rows.  Separate code for each symmetry:              HEXP1.84    
C        IGPAR=0 : even (IA) precedes odd (IB).                              HEXP1.85    
C        IGPAR=1 : odd (IA) precedes even (IB).                              HEXP1.86    
C                                                                            HEXP1.87    
      IF (NHEM.EQ.2) THEN                                                    HEXP1.88    
         IF (IGPAR.EQ.0) THEN                                                HEXP1.89    
            DO 30 IA=1,NWW                                                   HEXP1.90    
               IB=IA+IDL                                                     HEXP1.91    
               TEMP=GV(IA)                                                   HEXP1.92    
               GV(IA)=TEMP+GV(IB)                                            HEXP1.93    
               GV(IB)=TEMP-GV(IB)                                            HEXP1.94    
   30       CONTINUE                                                         HEXP1.95    
         ELSE                                                                HEXP1.96    
            DO 40 IA=1,NWW                                                   HEXP1.97    
               IB=IA+IDL                                                     HEXP1.98    
               TEMP=GV(IA)                                                   HEXP1.99    
               GV(IA)=GV(IB)+TEMP                                            HEXP1.100   
               GV(IB)=GV(IB)-TEMP                                            HEXP1.101   
   40       CONTINUE                                                         HEXP1.102   
         ENDIF                                                               HEXP1.103   
      ENDIF                                                                  HEXP1.104   
C                                                                            HEXP1.105   
      RETURN                                                                 HEXP1.106   
      END                                                                    HEXP1.107   
