C**********************************************************                  HEXPV.2     
C             SUBROUTINE HEXPV                                               HEXPV.3     
C**********************************************************                  HEXPV.4     
      SUBROUTINE HEXPV(Z,D,T,Q,SP,CHIG,SFG,UG,VG,ZG,DG,TG,QG,PLG,PJG)        HEXPV.5     
C                                                                            HEXPV.6     
C     Perform all the indirect Legendre transforms for the adiabatic         HEXPV.7     
C     part of the timestep at the current latitude (pair), in place          HEXPV.8     
C     of separate calls to HEXP for individual transform types.              HEXPV.9     
C                                                                            HEXPV.10    
C     The following types of Legendre function and thence types of           HEXPV.11    
C     transform may be used:                                                 HEXPV.12    
C        ITYPE=1,2  :  ALP   :  ALPN(,,,1)   :  normal transform.            HEXPV.13    
C        ITYPE=3,4  :  DALP  :  ALPN(,,,2)   :  y-derivative.                HEXPV.14    
C        ITYPE=5,6  :  RLP   :  ALPN(,,,3)   :  del(-2).                     HEXPV.15    
C        ITYPE=7,8  :  RDLP  :  ALPN(,,,4)   :  y-derivative of del(-2).     HEXPV.16    
C     An even/odd value of ITYPE denotes a spectral field of even/odd        HEXPV.17    
C     symmetry.                                                              HEXPV.18    
C                                                                            HEXPV.19    
C     Maximum vector efficiency is achieved by chaining all multi-level      HEXPV.20    
C     transforms in one loop and by chaining all single-level transforms     HEXPV.21    
C     in a second loop.                                                      HEXPV.22    
C                                                                            HEXPV.23    
C     All dummy argument arrays are declared complex.                        HEXPV.24    
C     All array dimensions are parameters.                                   HEXPV.25    
C     Multi-level arrays are 3-dimensional.                                  HEXPV.26    
C                                                                            HEXPV.27    
C     Tracers (QNLG,QT) are included conditionally inside inner loops.       HEXPV.28    
C     This does *not* affect vectorisation, since the number of tracers,     HEXPV.29    
C     NTRAC, is a parameter and the relevant IF constructs are removed       HEXPV.30    
C     at compilation.  The same applies to code conditional on NHEM.         HEXPV.31    
C     (Only tested on Cray J90 with cf77).                                   HEXPV.32    
C     Version for RSGUP3.                     Mike Blackburn,  12.01.95.     HEXPV.33    
C     Include tracers for IGCM2.              Piers Forster,   ??.04.97.     HEXPV.34    
C     Conditional separate loops for tracers. Mike Blackburn,  04.09.98.     HEXPV.35    
C                                                                            HEXPV.36    
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    
C                                                                            HEXPV.41    
      COMPLEX     Z(NWJ2,NHEM,NL),D(NWJ2,NHEM,NL),T(NWJ2,NHEM,NL)            HEXPV.42    
     *           ,Q(NWJ2,NHEM,NL*NTRAC)                                      HEXPV.43    
     *           ,SP(NWJ2,NHEM)                                              HEXPV.44    
      COMPLEX     CHIG(IDL,NHEM,NL),SFG(IDL,NHEM,NL)                         HEXPV.45    
     *           ,UG(IDL,NHEM,NL),VG(IDL,NHEM,NL)                            HEXPV.46    
     *           ,ZG(IDL,NHEM,NL),DG(IDL,NHEM,NL),TG(IDL,NHEM,NL)            HEXPV.47    
     *           ,QG(IDL,NHEM,NL*NTRAC)                                      HEXPV.48    
     *           ,PLG(IDL,NHEM),PJG(IDL,NHEM)                                HEXPV.49    
      COMPLEX     TEMP                                                       HEXPV.50    
      REAL        ALPN(NWJ2,2,JGL,4)                                         HEXPV.51    
      EQUIVALENCE (ALPN(1,1,1,1),ALP(1,1,1))                                 HEXPV.52    
C                                                                            HEXPV.53    
C     Transform multi-level fields to create even and odd contributions      HEXPV.54    
C     to the Fourier coefficients at the northern hemisphere latitude.       HEXPV.55    
C     Second subscript of ALPN denotes odd or even subset of Legendre        HEXPV.56    
C     functions, and depends of symmetry of spectral field.                  HEXPV.57    
C     Fourth subscript of ALPN is Legendre function type, (ITYPE+1)/2.       HEXPV.58    
C                                                                            HEXPV.59    
      IM=0                                                                   HEXPV.60    
      IP=0                                                                   HEXPV.61    
      DO 10 M=0,MM-1,MOCT                                                    HEXPV.62    
         IM=IM+1                                                             HEXPV.63    
         DO 10 N=M,NN-1,2                                                    HEXPV.64    
            IP=IP+1                                                          HEXPV.65    
            DO 10 L=1,NL                                                     HEXPV.66    
C              Velocity potential    : type 6.                               HEXPV.67    
               CHIG(IM,1,L)=CHIG(IM,1,L) + ALPN(IP,1,JL,3)*D(IP,1,L)         HEXPV.68    
C              Streamfunction        : type 5.                               HEXPV.69    
               SFG (IM,1,L)=SFG (IM,1,L) + ALPN(IP,2,JL,3)*Z(IP,1,L)         HEXPV.70    
C              Zonal (rot) wind      : type 7.                               HEXPV.71    
               UG  (IM,1,L)=UG  (IM,1,L) + ALPN(IP,2,JL,4)*Z(IP,1,L)         HEXPV.72    
C              Merid (div) wind      : type 8.                               HEXPV.73    
               VG  (IM,1,L)=VG  (IM,1,L) + ALPN(IP,1,JL,4)*D(IP,1,L)         HEXPV.74    
C              (Relative) vorticity  : type 1.                               HEXPV.75    
               ZG  (IM,1,L)=ZG  (IM,1,L) + ALPN(IP,2,JL,1)*Z(IP,1,L)         HEXPV.76    
C              Divergence            : type 2.                               HEXPV.77    
               DG  (IM,1,L)=DG  (IM,1,L) + ALPN(IP,1,JL,1)*D(IP,1,L)         HEXPV.78    
C              Temperature           : type 2.                               HEXPV.79    
               TG  (IM,1,L)=TG  (IM,1,L) + ALPN(IP,1,JL,1)*T(IP,1,L)         HEXPV.80    
               IF (NTRAC.EQ.1) THEN                                          HEXPV.81    
C                 Tracers            : type 2.                               HEXPV.82    
                  QG (IM,1,L)=QG(IM,1,L) + ALPN(IP,1,JL,1)*Q(IP,1,L)         HEXPV.83    
               ENDIF                                                         HEXPV.84    
               IF (NHEM.EQ.2) THEN                                           HEXPV.85    
C                 Velocity potential    : type 6.                            HEXPV.86    
                  CHIG(IM,2,L)=CHIG(IM,2,L) + ALPN(IP,2,JL,3)*D(IP,2,L)      HEXPV.87    
C                 Streamfunction        : type 5.                            HEXPV.88    
                  SFG (IM,2,L)=SFG (IM,2,L) + ALPN(IP,1,JL,3)*Z(IP,2,L)      HEXPV.89    
C                 Zonal (rot) wind      : type 7.                            HEXPV.90    
                  UG  (IM,2,L)=UG  (IM,2,L) + ALPN(IP,1,JL,4)*Z(IP,2,L)      HEXPV.91    
C                 Merid (div) wind      : type 8.                            HEXPV.92    
                  VG  (IM,2,L)=VG  (IM,2,L) + ALPN(IP,2,JL,4)*D(IP,2,L)      HEXPV.93    
C                 (Relative) vorticity  : type 1.                            HEXPV.94    
                  ZG  (IM,2,L)=ZG  (IM,2,L) + ALPN(IP,1,JL,1)*Z(IP,2,L)      HEXPV.95    
C                 Divergence            : type 2.                            HEXPV.96    
                  DG  (IM,2,L)=DG  (IM,2,L) + ALPN(IP,2,JL,1)*D(IP,2,L)      HEXPV.97    
C                 Temperature           : type 2.                            HEXPV.98    
                  TG  (IM,2,L)=TG  (IM,2,L) + ALPN(IP,2,JL,1)*T(IP,2,L)      HEXPV.99    
                  IF (NTRAC.EQ.1) THEN                                       HEXPV.100   
C                    Tracers            : type 2.                            HEXPV.101   
                     QG (IM,2,L)=QG(IM,2,L) + ALPN(IP,2,JL,1)*Q(IP,2,L)      HEXPV.102   
                  ENDIF                                                      HEXPV.103   
               ENDIF                                                         HEXPV.104   
   10 CONTINUE                                                               HEXPV.105   
C                                                                            HEXPV.106   
C     Transform tracers, treated as a single (NL*NTRAC)-level field.         HEXPV.107   
C                                                                            HEXPV.108   
      IF (NTRAC.GT.1) THEN                                                   HEXPV.109   
         IM=0                                                                HEXPV.110   
         IP=0                                                                HEXPV.111   
         DO 12 M=0,MM-1,MOCT                                                 HEXPV.112   
            IM=IM+1                                                          HEXPV.113   
            DO 12 N=M,NN-1,2                                                 HEXPV.114   
               IP=IP+1                                                       HEXPV.115   
               DO 12 L=1,NL*NTRAC                                            HEXPV.116   
C                 Tracers            : type 2.                               HEXPV.117   
                  QG(IM,1,L)=QG(IM,1,L)+ALPN(IP,1,JL,1)*Q(IP,1,L)            HEXPV.118   
                  IF (NHEM.EQ.2) THEN                                        HEXPV.119   
                     QG(IM,2,L)=QG(IM,2,L)+ALPN(IP,2,JL,1)*Q(IP,2,L)         HEXPV.120   
                  ENDIF                                                      HEXPV.121   
   12    CONTINUE                                                            HEXPV.122   
      ENDIF                                                                  HEXPV.123   
C                                                                            HEXPV.124   
C     Transform single-level fields to create even and odd contributions     HEXPV.125   
C     to the Fourier coefficients at the northern hemisphere latitude.       HEXPV.126   
C     Vectorisation is over total wavenumber for each zonal wavenumber.      HEXPV.127   
C                                                                            HEXPV.128   
      IM=0                                                                   HEXPV.129   
      IP=0                                                                   HEXPV.130   
      DO 20 M=0,MM-1,MOCT                                                    HEXPV.131   
         IM=IM+1                                                             HEXPV.132   
         DO 20 N=M,NN-1,2                                                    HEXPV.133   
            IP=IP+1                                                          HEXPV.134   
C           Log (surface pressure)    : type 2.                              HEXPV.135   
            PLG(IM,1)=PLG(IM,1) + ALPN(IP,1,JL,1)*SP(IP,1)                   HEXPV.136   
C           Merid gradient of ln (ps) : type 4.                              HEXPV.137   
            PJG(IM,1)=PJG(IM,1) + ALPN(IP,1,JL,2)*SP(IP,1)                   HEXPV.138   
            IF (NHEM.EQ.2) THEN                                              HEXPV.139   
C              Log (surface pressure)    : type 2.                           HEXPV.140   
               PLG(IM,2)=PLG(IM,2) + ALPN(IP,2,JL,1)*SP(IP,2)                HEXPV.141   
C              Merid gradient of ln (ps) : type 4.                           HEXPV.142   
               PJG(IM,2)=PJG(IM,2) + ALPN(IP,2,JL,2)*SP(IP,2)                HEXPV.143   
            ENDIF                                                            HEXPV.144   
   20 CONTINUE                                                               HEXPV.145   
C                                                                            HEXPV.146   
C     For a global run, sum and difference even and odd contributions        HEXPV.147   
C     to give the complete Fourier transforms at the northern and            HEXPV.148   
C     southern latitude rows: N=E+O, S=E-O.                                  HEXPV.149   
C     For symmetric Fourier fields, even (IM,1) precedes odd (IM,2).         HEXPV.150   
C     For asymmetric Fourier fields, odd (IM,1) precedes even (IM,2).        HEXPV.151   
C                                                                            HEXPV.152   
      IF (NHEM.EQ.2) THEN                                                    HEXPV.153   
C                                                                            HEXPV.154   
C        Multi-level fields.                                                 HEXPV.155   
C                                                                            HEXPV.156   
         DO 30 IM=1,NWW                                                      HEXPV.157   
            DO 30 L=1,NL                                                     HEXPV.158   
C              Velocity potential : symmetric.                               HEXPV.159   
               TEMP=CHIG(IM,1,L)                                             HEXPV.160   
               CHIG(IM,1,L)=TEMP+CHIG(IM,2,L)                                HEXPV.161   
               CHIG(IM,2,L)=TEMP-CHIG(IM,2,L)                                HEXPV.162   
C              Streamfunction : anti-symmetric.                              HEXPV.163   
               TEMP=SFG(IM,1,L)                                              HEXPV.164   
               SFG(IM,1,L)=SFG(IM,2,L)+TEMP                                  HEXPV.165   
               SFG(IM,2,L)=SFG(IM,2,L)-TEMP                                  HEXPV.166   
C              Zonal (rotational) wind : symmetric.                          HEXPV.167   
               TEMP=UG(IM,1,L)                                               HEXPV.168   
               UG(IM,1,L)=TEMP+UG(IM,2,L)                                    HEXPV.169   
               UG(IM,2,L)=TEMP-UG(IM,2,L)                                    HEXPV.170   
C              Meridional (divergent) wind : anti-symmetric.                 HEXPV.171   
               TEMP=VG(IM,1,L)                                               HEXPV.172   
               VG(IM,1,L)=VG(IM,2,L)+TEMP                                    HEXPV.173   
               VG(IM,2,L)=VG(IM,2,L)-TEMP                                    HEXPV.174   
C              Vorticity : anti-symmetric.                                   HEXPV.175   
               TEMP=ZG(IM,1,L)                                               HEXPV.176   
               ZG(IM,1,L)=ZG(IM,2,L)+TEMP                                    HEXPV.177   
               ZG(IM,2,L)=ZG(IM,2,L)-TEMP                                    HEXPV.178   
C              Divergence : symmetric.                                       HEXPV.179   
               TEMP=DG(IM,1,L)                                               HEXPV.180   
               DG(IM,1,L)=TEMP+DG(IM,2,L)                                    HEXPV.181   
               DG(IM,2,L)=TEMP-DG(IM,2,L)                                    HEXPV.182   
C              Temperature : symmetric.                                      HEXPV.183   
               TEMP=TG(IM,1,L)                                               HEXPV.184   
               TG(IM,1,L)=TEMP+TG(IM,2,L)                                    HEXPV.185   
               TG(IM,2,L)=TEMP-TG(IM,2,L)                                    HEXPV.186   
               IF (NTRAC.EQ.1) THEN                                          HEXPV.187   
C                 Tracers : symmetric.                                       HEXPV.188   
                  TEMP=QG(IM,1,L)                                            HEXPV.189   
                  QG(IM,1,L)=TEMP+QG(IM,2,L)                                 HEXPV.190   
                  QG(IM,2,L)=TEMP-QG(IM,2,L)                                 HEXPV.191   
               ENDIF                                                         HEXPV.192   
   30    CONTINUE                                                            HEXPV.193   
C                                                                            HEXPV.194   
C        Tracers, treated as a single (NL*NTRAC)-level field.                HEXPV.195   
C                                                                            HEXPV.196   
         IF (NTRAC.GT.1) THEN                                                HEXPV.197   
            DO 32 IM=1,NWW                                                   HEXPV.198   
               DO 32 L=1,NL*NTRAC                                            HEXPV.199   
C                 Tracers : symmetric.                                       HEXPV.200   
                  TEMP=QG(IM,1,L)                                            HEXPV.201   
                  QG(IM,1,L)=TEMP+QG(IM,2,L)                                 HEXPV.202   
                  QG(IM,2,L)=TEMP-QG(IM,2,L)                                 HEXPV.203   
   32       CONTINUE                                                         HEXPV.204   
         ENDIF                                                               HEXPV.205   
C                                                                            HEXPV.206   
C        Single-level fields.                                                HEXPV.207   
C                                                                            HEXPV.208   
         DO 40 IM=1,NWW                                                      HEXPV.209   
C           Log (surface pressure) : symmetric.                              HEXPV.210   
            TEMP=PLG(IM,1)                                                   HEXPV.211   
            PLG(IM,1)=TEMP+PLG(IM,2)                                         HEXPV.212   
            PLG(IM,2)=TEMP-PLG(IM,2)                                         HEXPV.213   
C           Meridional gradient of ln(ps) : anti-symmetric.                  HEXPV.214   
            TEMP=PJG(IM,1)                                                   HEXPV.215   
            PJG(IM,1)=PJG(IM,2)+TEMP                                         HEXPV.216   
            PJG(IM,2)=PJG(IM,2)-TEMP                                         HEXPV.217   
   40    CONTINUE                                                            HEXPV.218   
C                                                                            HEXPV.219   
      ENDIF                                                                  HEXPV.220   
C                                                                            HEXPV.221   
      RETURN                                                                 HEXPV.222   
      END                                                                    HEXPV.223   
