C**********************************************************                  LTD.2     
C             SUBROUTINE LTD                                                 LTD.3     
C**********************************************************                  LTD.4     
      SUBROUTINE LTD                                                         LTD.5     
C                                                                            LTD.6     
C     Direct Legendre transform for the adiabatic part of the timestep.      LTD.7     
C     Transforms from Fourier to spectral space at the current latitude      LTD.8     
C     (pair).  In a global run the input arrays are complete (even+odd)      LTD.9     
C     Fourier coefficients at the northern & southern hemisphere rows.       LTD.10    
C                                                                            LTD.11    
C     Includes the option either to call the modular routine HANAL for       LTD.12    
C     each field to be transformed, or to call the fast vectorising          LTD.13    
C     routine HANALV to perform all transforms together.  The choice         LTD.14    
C     is controlled by logical LTVEC.                                        LTD.15    
C                                                                            LTD.16    
C     Each call to HANAL transforms fields having the same symmetry          LTD.17    
C     and type of Legendre Function.  HANAL1 is a separate routine           LTD.18    
C     with improved efficiency for single-level transforms.                  LTD.19    
C                                                                            LTD.20    
C     The Fourier work array passed to HANAL must be dimensioned with        LTD.21    
C     (at least) the maximum number of levels used in the HANAL calls.       LTD.22    
C                                                                            LTD.23    
C     Version for RSGUP3.                     Mike Blackburn,  12.01.95.     LTD.24    
C     ANSI work arrays for HANAL,SPDEL2.      Mike Blackburn,  04.09.96.     LTD.25    
C                                                                            LTD.26    
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    
      REAL    FILT(0:NN)                                                     LTD.34    
      COMPLEX GWORK(IGL,(3+NTRAC)*NL+2)                                      LTD.35    
C                                                                            LTD.36    
C     Prepare Fourier arrays:                                                LTD.37    
C     - change sign of terms which contribute negatively to tendency,        LTD.38    
C     - apply (1-mu**2) weighting,                                           LTD.39    
C     - take zonal derivatives,                                              LTD.40    
C     - make copies of effective momentum tendencies.                        LTD.41    
C                                                                            LTD.42    
      DO 10 L=1,NL                                                           LTD.43    
         DO 10 I=1,IGL                                                       LTD.44    
            EG(I,L)=-0.5*EG(I,L)*SECSQ(JH)                                   LTD.45    
            VTG(I,L)=-VTG(I,L)                                               LTD.46    
            TNLG(I,L)=TNLG(I,L)-CMPA(I)*UTG(I,L)*SECSQ(JH)                   LTD.47    
            FVGT(I,L)=FVG(I,L)                                               LTD.48    
            FUGT(I,L)=-FUG(I,L)                                              LTD.49    
            FUG(I,L)=CMPA(I)*FUG(I,L)*SECSQ(JH)                              LTD.50    
            FVG(I,L)=CMPA(I)*FVG(I,L)*SECSQ(JH)                              LTD.51    
   10 CONTINUE                                                               LTD.52    
C                                                                            LTD.53    
C     Tracer transforms depend on the form of advection used.                LTD.54    
C                                                                            LTD.55    
      IF (LFLUX) THEN                                                        LTD.56    
         AFAC=-1.0                                                           LTD.57    
      ELSE                                                                   LTD.58    
         AFAC=1.0                                                            LTD.59    
      ENDIF                                                                  LTD.60    
      DO 11 KK=1,NTRAC                                                       LTD.61    
         DO 11 L=1,NL                                                        LTD.62    
            DO 11 I=1,IGL                                                    LTD.63    
               TRANLG(I,L,KK)=AFAC*TRANLG(I,L,KK)                            LTD.64    
     *                        -CMPA(I)*UTRAG(I,L,KK)*SECSQ(JH)               LTD.65    
               VTRAG(I,L,KK)=-VTRAG(I,L,KK)                                  LTD.66    
   11 CONTINUE                                                               LTD.67    
C                                                                            LTD.68    
      IF (LTVEC) THEN                                                        LTD.69    
C                                                                            LTD.70    
C        Call single routine to perform all transforms with maximum          LTD.71    
C        vector efficiency.                                                  LTD.72    
C        *** NOTE : THE INPUT FOURIER FIELDS ARE MODIFIED IF GLOBAL ***      LTD.73    
C                                                                            LTD.74    
         CALL HANALV(SPA,VP,DTE,TT,TRAT,DT,ZT,SPG,VPG,EG                     LTD.75    
     +              ,TNLG,TRANLG,FUG,FVG,VTG,VTRAG,FVGT,FUGT)                LTD.76    
C                                                                            LTD.77    
      ELSE                                                                   LTD.78    
C                                                                            LTD.79    
C        Main transform of even fields:                                      LTD.80    
C        SPG to FUG (and SPA to DT) must be contiguous in common.            LTD.81    
C                                                                            LTD.82    
         CALL HANAL(SPG,GWORK,SPA,(3+NTRAC)*NL+2,2)                          LTD.83    
C                                                                            LTD.84    
C        Remaining transforms: VTG,FVGT (and TT,DT) must be contiguous.      LTD.85    
C                                                                            LTD.86    
         CALL HANAL(FVG,GWORK,ZT,NL,1)                                       LTD.87    
         CALL HANAL(VTG,GWORK,TT,(2+NTRAC)*NL,4)                             LTD.88    
         CALL HANAL(FUGT,GWORK,ZT,NL,3)                                      LTD.89    
C                                                                            LTD.90    
      ENDIF                                                                  LTD.91    
C                                                                            LTD.92    
C     At the last latitude, take del**2 of the energy term and add to        LTD.93    
C     the divergence tendency.                                               LTD.94    
C                                                                            LTD.95    
      IF (JH.EQ.JG) THEN                                                     LTD.96    
         CALL SPDEL2(DTE,FILT,NWJ2,NN,MM,MOCT,NHEM,NL,0,2)                   LTD.97    
         DO 20 I=1,IGB                                                       LTD.98    
            DT(I)=DT(I)+DTE(I)                                               LTD.99    
   20    CONTINUE                                                            LTD.100   
      ENDIF                                                                  LTD.101   
C                                                                            LTD.102   
      RETURN                                                                 LTD.103   
      END                                                                    LTD.104   
