C**********************************************************                  LTI.2     
C             SUBROUTINE LTI                                                 LTI.3     
C**********************************************************                  LTI.4     
      SUBROUTINE LTI                                                         LTI.5     
C                                                                            LTI.6     
C     Inverse Legendre transform for the adiabatic part of the timestep.     LTI.7     
C     Transforms from spectral to Fourier space at the current latitude      LTI.8     
C     (pair).  In a global run the resulting arrays are complete             LTI.9     
C     (i.e. even+odd) Fourier coefficients at the northern & southern        LTI.10    
C     hemisphere rows.                                                       LTI.11    
C                                                                            LTI.12    
C     Includes the option either to call the modular routine HEXP for        LTI.13    
C     each field to be transformed, or to call the fast vectorising          LTI.14    
C     routine HEXPV to perform all transforms together.  The choice          LTI.15    
C     is controlled by logical LTVEC.                                        LTI.16    
C                                                                            LTI.17    
C     Each call to HEXP transforms fields having the same symmetry           LTI.18    
C     and type of Legendre Function.  HEXP1 is a separate routine            LTI.19    
C     with improved efficiency for single-level transforms.                  LTI.20    
C                                                                            LTI.21    
C     Version for RSGUP3.                     Mike Blackburn,  12.01.95.     LTI.22    
C                                                                            LTI.23    
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                                                                            GRIDP3.2     
C     Array ordering in GRIDP must correspond to that in SPECTR.             GRIDP3.3     
C     Complex arrays: multi-level arrays are 2-dimensional.                  GRIDP3.4     
C                                                                            GRIDP3.5     
      COMMON/GRIDP/ CHIG(IGL,NL),SFG(IGL,NL),UG(IGL,NL),VG(IGL,NL)           GRIDP3.6     
     :              ,ZG(IGL,NL),DG(IGL,NL),TG(IGL,NL)                        GRIDP3.7     
     :              ,TRAG(IGL,NL,NTRAC)                                      GRIDP3.8     
     :              ,PLG(IGL),PJG(IGL),PMG(IGL)                              GRIDP3.9     
     :              ,SPG(IGL),VPG(IGL),EG(IGL,NL)                            GRIDP3.10    
     :              ,TNLG(IGL,NL),TRANLG(IGL,NL,NTRAC),FUG(IGL,NL)           GRIDP3.11    
     :              ,FVG(IGL,NL),UTG(IGL,NL),UTRAG(IGL,NL,NTRAC)             GRIDP3.12    
     :              ,VTG(IGL,NL),VTRAG(IGL,NL,NTRAC)                         GRIDP3.13    
     :              ,FVGT(IGL,NL),FUGT(IGL,NL)                               GRIDP3.14    
     :              ,GRPAD(NGRPAD)                                           GRIDP3.15    
      COMPLEX CHIG,SFG,UG,VG,ZG,DG,TG,PLG,PJG,PMG                            GRIDP3.16    
     :       ,SPG,VPG,EG,TNLG,FUG,FVG,UTG,VTG,FVGT,FUGT                      GRIDP3.17    
     :       ,TRAG,TRANLG,UTRAG,VTRAG                                        GRIDP3.18    
C                                                                            GRIDP3.19    
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                                                                            POLYNO.2     
C     Polynomial used to aid vectorization of Legendre transforms            POLYNO.3     
C                                                                            POLYNO.4     
      COMMON/POLYNO/POLY(NWJ2,2,4),CMPA(IGL)                                 POLYNO.5     
      COMPLEX CMPA                                                           POLYNO.6     
C                                                                            POLYNO.7     
C                                                                            SPECTR.2     
C     Array ordering in SPECTR must correspond to that in GRIDP.             SPECTR.3     
C                                                                            SPECTR.4     
      COMMON/SPECTR/Z(IGB),D(IGB),T(IGB),TRA(IGB,NTRAC),SP(IGA),GS(IGA)      SPECTR.5     
     :              ,SPA(IGA),VP(IGA),DTE(IGB),TT(IGB)                       SPECTR.6     
     :              ,TRAT(IGB,NTRAC),DT(IGB),ZT(IGB)                         SPECTR.7     
     :              ,ZMI(IGB),DMI(IGB),TMI(IGB)                              SPECTR.8     
     :              ,TRAMI(IGB,NTRAC),SPMI(IGA)                              SPECTR.9     
      COMPLEX Z,D,T,TRA,SP,GS,SPA,VP,DTE,TT,TRAT,DT,ZT                       SPECTR.10    
     :       ,ZMI,DMI,TMI,TRAMI,SPMI                                         SPECTR.11    
C                                                                            SPECTR.12    
C                                                                            LTI.31    
      COMPLEX CEZN                                                           LTI.32    
C                                                                            LTI.33    
C     Preset Fourier arrays.                                                 LTI.34    
C                                                                            LTI.35    
      DO 10 L=1,NL                                                           LTI.36    
         DO 10 I=1,IGL                                                       LTI.37    
            CHIG(I,L)=0.                                                     LTI.38    
            SFG(I,L)=0.                                                      LTI.39    
            UG(I,L)=0.                                                       LTI.40    
            VG(I,L)=0.                                                       LTI.41    
            ZG(I,L)=0.                                                       LTI.42    
            DG(I,L)=0.                                                       LTI.43    
            TG(I,L)=0.                                                       LTI.44    
   10 CONTINUE                                                               LTI.45    
C                                                                            LTI.46    
      DO 11 KK=1,NTRAC                                                       LTI.47    
         DO 11 L=1,NL                                                        LTI.48    
            DO 11 I=1,IGL                                                    LTI.49    
               TRAG(I,L,KK)=0.                                               LTI.50    
   11 CONTINUE                                                               LTI.51    
C                                                                            LTI.52    
      DO 20 I=1,IGL                                                          LTI.53    
         PLG(I)=0.                                                           LTI.54    
         PJG(I)=0.                                                           LTI.55    
   20 CONTINUE                                                               LTI.56    
C                                                                            LTI.57    
C     Remove planetary vorticity in spectral space, so all transforms        LTI.58    
C     use relative vorticity.                                                LTI.59    
C                                                                            LTI.60    
      DO 30 I=1,IGB,IGA                                                      LTI.61    
         Z(I)=Z(I)-EZ                                                        LTI.62    
   30 CONTINUE                                                               LTI.63    
C                                                                            LTI.64    
      IF (LTVEC) THEN                                                        LTI.65    
C                                                                            LTI.66    
C        Call single routine to perform all transforms with maximum          LTI.67    
C        vector efficiency.                                                  LTI.68    
C                                                                            LTI.69    
         CALL HEXPV(Z,D,T,TRA,SP,CHIG,SFG,UG,VG,ZG,DG,TG,TRAG,PLG,PJG)       LTI.70    
C                                                                            LTI.71    
      ELSE                                                                   LTI.72    
C                                                                            LTI.73    
C        Transform prognostic fields & meridional derivative of ln(ps).      LTI.74    
C        D,T,TRA,SP (and DG,TG,TRAG,PLG) must be contiguous in common.       LTI.75    
C                                                                            LTI.76    
         CALL HEXP(Z , ZG,  NL  ,1)                                          LTI.77    
         CALL HEXP(D,DG,(2+NTRAC)*NL+1,2)                                    LTI.78    
         CALL HEXP1(SP,PJG,    1,4)                                          LTI.79    
C                                                                            LTI.80    
C        Wind components: calls to HEXP give following Fourier fields:       LTI.81    
C           SFG  :   streamfunction.                                         LTI.82    
C           CHIG :   velocity potential.                                     LTI.83    
C           UG   :   -U(rotational).                                         LTI.84    
C           VG   :   V(divergent).                                           LTI.85    
C                                                                            LTI.86    
         CALL HEXP(Z,SFG ,NL,5)                                              LTI.87    
         CALL HEXP(D,CHIG,NL,6)                                              LTI.88    
         CALL HEXP(Z,UG  ,NL,7)                                              LTI.89    
         CALL HEXP(D,VG  ,NL,8)                                              LTI.90    
C                                                                            LTI.91    
      ENDIF                                                                  LTI.92    
C                                                                            LTI.93    
C     Restore planetary vorticity in spectral space.                         LTI.94    
C                                                                            LTI.95    
      DO 40 I=1,IGB,IGA                                                      LTI.96    
         Z(I)=Z(I)+EZ                                                        LTI.97    
   40 CONTINUE                                                               LTI.98    
C                                                                            LTI.99    
C     Convert from relative to absolute vorticity in Fourier space:          LTI.100   
C     (real part of) m=0 coefficient only.                                   LTI.101   
C                                                                            LTI.102   
      CEZN=CMPLX(2.0*SI(JH),0.0)                                             LTI.103   
      DO 50 L=1,NL                                                           LTI.104   
         ZG(1,L)=ZG(1,L)+CEZN                                                LTI.105   
         IF (NHEM.EQ.2) ZG(1+IDL,L)=ZG(1+IDL,L)-CEZN                         LTI.106   
   50 CONTINUE                                                               LTI.107   
C                                                                            LTI.108   
C     Sum to give total winds.  CMPA takes x-derivative.                     LTI.109   
C                                                                            LTI.110   
      DO 60 L=1,NL                                                           LTI.111   
         DO 60 I=1,IGL                                                       LTI.112   
            UG(I,L)=CMPA(I)*CHIG(I,L)-UG(I,L)                                LTI.113   
            VG(I,L)=CMPA(I)* SFG(I,L)+VG(I,L)                                LTI.114   
   60 CONTINUE                                                               LTI.115   
C                                                                            LTI.116   
C     Zonal gradient of ln(ps).                                              LTI.117   
C                                                                            LTI.118   
      DO 70 I=1,IGL                                                          LTI.119   
         PMG(I)=CMPA(I)*PLG(I)                                               LTI.120   
   70 CONTINUE                                                               LTI.121   
C                                                                            LTI.122   
      RETURN                                                                 LTI.123   
      END                                                                    LTI.124   
