C***********************************************************                 DANALV.2     
C              SUBROUTINE DANALV                                             DANALV.3     
C***********************************************************                 DANALV.4     
      SUBROUTINE DANALV(TT,QT,DT,ZT,TNLG,QNLG,FUG,FVG,FVGT,FUGT)             DANALV.5     
C                                                                            DANALV.6     
C     Perform all the direct Legendre transforms for the diabatic            DANALV.7     
C     part of the timestep at the current latitude (pair), in place          DANALV.8     
C     of separate calls to HANAL for individual transform types.             DANALV.9     
C                                                                            DANALV.10    
C     The following types of Legendre function and thence types of           DANALV.11    
C     transform may be used:                                                 DANALV.12    
C        ITYPE=1,2  :  ALP   :  ALPN(,,,1)   :  normal transform.            DANALV.13    
C        ITYPE=3,4  :  DALP  :  ALPN(,,,2)   :  y-derivative.                DANALV.14    
C        ITYPE=5,6  :  RLP   :  ALPN(,,,3)   :  del(-2).                     DANALV.15    
C        ITYPE=7,8  :  RDLP  :  ALPN(,,,4)   :  y-derivative of del(-2).     DANALV.16    
C     An even/odd value of ITYPE denotes a spectral field of even/odd        DANALV.17    
C     symmetry.                                                              DANALV.18    
C                                                                            DANALV.19    
C     Maximum vector efficiency is achieved by chaining all multi-level      DANALV.20    
C     transforms in one loop and by chaining all single-level transforms     DANALV.21    
C     in a second loop.                                                      DANALV.22    
C                                                                            DANALV.23    
C     All dummy argument arrays are declared complex.                        DANALV.24    
C     All array dimensions are parameters.                                   DANALV.25    
C     Multi-level arrays are 3-dimensional.                                  DANALV.26    
C                                                                            DANALV.27    
C     Tracers (QNLG,QT) are included conditionally inside inner loops.       DANALV.28    
C     This does *not* affect vectorisation, since the number of tracers,     DANALV.29    
C     NTRAC, is a parameter and the relevant IF constructs are removed       DANALV.30    
C     at compilation.  The same applies to code conditional on NHEM.         DANALV.31    
C     (Only tested on Cray J90 with cf77).                                   DANALV.32    
C                                                                            DANALV.33    
C     NOTE: The y-derivative transforms use integration by parts and         DANALV.34    
C           are valid only if the input field has zero zonal mean at         DANALV.35    
C           both poles.                                                      DANALV.36    
C                                                                            DANALV.37    
C     NOTE: *** THE INPUT FOURIER FIELDS ARE MODIFIED IF GLOBAL ***          DANALV.38    
C                                                                            DANALV.39    
C     Original version for IGCM2.             Mike Blackburn,  04.09.98.     DANALV.40    
C                                                                            DANALV.41    
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                                                                            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                                                                            DANALV.47    
      COMPLEX     TT(NWJ2,NHEM,NL),QT(NWJ2,NHEM,NL*NTRAC)                    DANALV.48    
     *           ,DT(NWJ2,NHEM,NL),ZT(NWJ2,NHEM,NL)                          DANALV.49    
      COMPLEX     TNLG(IDL,NHEM,NL),QNLG(IDL,NHEM,NL*NTRAC)                  DANALV.50    
     *           ,FUG(IDL,NHEM,NL),FVG(IDL,NHEM,NL)                          DANALV.51    
     *           ,FVGT(IDL,NHEM,NL),FUGT(IDL,NHEM,NL)                        DANALV.52    
      COMPLEX     TEMP                                                       DANALV.53    
      REAL        ALPN(NWJ2,2,JGL,4)                                         DANALV.54    
      EQUIVALENCE (ALPN(1,1,1,1),ALP(1,1,1))                                 DANALV.55    
C                                                                            DANALV.56    
C     For a global run, sum and difference the complete Fourier              DANALV.57    
C     transforms at the northern and southern latitude rows to give          DANALV.58    
C     the even and odd contributions : E=(N+S)/2, O=(N-S)/2.                 DANALV.59    
C     For Fourier fields symmetric about equator  : even precedes odd.       DANALV.60    
C     For Fourier fields asymmetric about equator : odd precedes even.       DANALV.61    
C                                                                            DANALV.62    
      IF (NHEM.EQ.2) THEN                                                    DANALV.63    
C                                                                            DANALV.64    
C        Multi-level fields.                                                 DANALV.65    
C                                                                            DANALV.66    
         DO 20 IM=1,NWW                                                      DANALV.67    
            DO 20 L=1,NL                                                     DANALV.68    
C              Temperature tendency : (complete) : symmetric.                DANALV.69    
               TEMP=TNLG(IM,1,L)                                             DANALV.70    
               TNLG(IM,1,L)=0.5*(TEMP+TNLG(IM,2,L))                          DANALV.71    
               TNLG(IM,2,L)=0.5*(TEMP-TNLG(IM,2,L))                          DANALV.72    
               IF (NTRAC.EQ.1) THEN                                          DANALV.73    
C                 Tracer tendency   : (complete) : symmetric.                DANALV.74    
                  TEMP=QNLG(IM,1,L)                                          DANALV.75    
                  QNLG(IM,1,L)=0.5*(TEMP+QNLG(IM,2,L))                       DANALV.76    
                  QNLG(IM,2,L)=0.5*(TEMP-QNLG(IM,2,L))                       DANALV.77    
               ENDIF                                                         DANALV.78    
C              Divergence tendency  : d/dx part  : symmetric.                DANALV.79    
               TEMP=FUG(IM,1,L)                                              DANALV.80    
               FUG(IM,1,L)=0.5*(TEMP+FUG(IM,2,L))                            DANALV.81    
               FUG(IM,2,L)=0.5*(TEMP-FUG(IM,2,L))                            DANALV.82    
C              Vorticity tendency   : d/dx part  : anti-symmetric.           DANALV.83    
               TEMP=FVG(IM,1,L)                                              DANALV.84    
               FVG(IM,1,L)=0.5*(TEMP-FVG(IM,2,L))                            DANALV.85    
               FVG(IM,2,L)=0.5*(TEMP+FVG(IM,2,L))                            DANALV.86    
C              Divergence tendency  : d/dy part  : anti-symmetric.           DANALV.87    
               TEMP=FVGT(IM,1,L)                                             DANALV.88    
               FVGT(IM,1,L)=0.5*(TEMP-FVGT(IM,2,L))                          DANALV.89    
               FVGT(IM,2,L)=0.5*(TEMP+FVGT(IM,2,L))                          DANALV.90    
C              Vorticity tendency   : d/dy part  : symmetric.                DANALV.91    
               TEMP=FUGT(IM,1,L)                                             DANALV.92    
               FUGT(IM,1,L)=0.5*(TEMP+FUGT(IM,2,L))                          DANALV.93    
               FUGT(IM,2,L)=0.5*(TEMP-FUGT(IM,2,L))                          DANALV.94    
   20    CONTINUE                                                            DANALV.95    
C                                                                            DANALV.96    
C        Tracers, treated as a single (NL*NTRAC)-level field.                DANALV.97    
C                                                                            DANALV.98    
         IF (NTRAC.GT.1) THEN                                                DANALV.99    
            DO 22 IM=1,NWW                                                   DANALV.100   
               DO 22 L=1,NL*NTRAC                                            DANALV.101   
C                 Tracer tendency   : (complete) : symmetric.                DANALV.102   
                  TEMP=QNLG(IM,1,L)                                          DANALV.103   
                  QNLG(IM,1,L)=0.5*(TEMP+QNLG(IM,2,L))                       DANALV.104   
                  QNLG(IM,2,L)=0.5*(TEMP-QNLG(IM,2,L))                       DANALV.105   
   22       CONTINUE                                                         DANALV.106   
         ENDIF                                                               DANALV.107   
C                                                                            DANALV.108   
      ENDIF                                                                  DANALV.109   
C                                                                            DANALV.110   
C     Set up the appropriate Gaussian weight for the current latitude,       DANALV.111   
C     dependent on transform type.                                           DANALV.112   
C     Assumes JH in /LEGAU/ contains latitude counter from calling loop.     DANALV.113   
C                                                                            DANALV.114   
      AW1256=AW(JH)*CSSQ(JH)                                                 DANALV.115   
      AW3478=-AW(JH)                                                         DANALV.116   
C                                                                            DANALV.117   
C     Calculate POLY array in a vector loop before the main transforms,      DANALV.118   
C     for the required Legendre Function types.                              DANALV.119   
C     Both even and odd functions are required, irrespective of NHEM.        DANALV.120   
C     Second subscript of ALPN denotes odd or even subset of Legendre        DANALV.121   
C     functions, and depends of symmetry of spectral field.                  DANALV.122   
C     Fourth subscript of ALPN is Legendre function type, (ITYPE+1)/2.       DANALV.123   
C                                                                            DANALV.124   
      DO 30 IHEM=1,2                                                         DANALV.125   
         DO 30 IP=1,NWJ2                                                     DANALV.126   
            POLY(IP,IHEM,1)=AW1256*ALPN(IP,IHEM,JH,1)                        DANALV.127   
            POLY(IP,IHEM,2)=AW3478*ALPN(IP,IHEM,JH,2)                        DANALV.128   
   30 CONTINUE                                                               DANALV.129   
C                                                                            DANALV.130   
C     Transform multi-level fields.                                          DANALV.131   
C     Inner loop vectorisation is over total wavenumber, to access           DANALV.132   
C     spectral memory sequentially, avoiding skip distances being a          DANALV.133   
C     multiple of 8 (which causes memory bank conflicts on Cray vector       DANALV.134   
C     machines).                                                             DANALV.135   
C                                                                            DANALV.136   
      IM=0                                                                   DANALV.137   
      IP=0                                                                   DANALV.138   
      DO 50 M=0,MM-1,MOCT                                                    DANALV.139   
         IM=IM+1                                                             DANALV.140   
         IPM=IP                                                              DANALV.141   
         DO 50 L=1,NL                                                        DANALV.142   
            IP=IPM                                                           DANALV.143   
            DO 50 N=M,NN-1,2                                                 DANALV.144   
               IP=IP+1                                                       DANALV.145   
C              Temperature tendency : (complete)       : type 2.             DANALV.146   
               TT(IP,1,L)=TT(IP,1,L) + POLY(IP,1,1)*TNLG(IM,1,L)             DANALV.147   
               IF (NTRAC.EQ.1) THEN                                          DANALV.148   
C                 Tracer tendency   : (complete)       : type 2.             DANALV.149   
                  QT(IP,1,L)=QT(IP,1,L) + POLY(IP,1,1)*QNLG(IM,1,L)          DANALV.150   
               ENDIF                                                         DANALV.151   
C              Divergence tendency  : d/dx part        : type 2.             DANALV.152   
C              Divergence tendency  : d/dy part        : type 4.             DANALV.153   
               DT(IP,1,L)=DT(IP,1,L) + POLY(IP,1,1)*FUG (IM,1,L)             DANALV.154   
     *                               + POLY(IP,1,2)*FVGT(IM,1,L)             DANALV.155   
C              Vorticity tendency   : d/dx part        : type 1.             DANALV.156   
C              Vorticity tendency   : d/dy part        : type 3.             DANALV.157   
               ZT(IP,1,L)=ZT(IP,1,L) + POLY(IP,2,1)*FVG (IM,1,L)             DANALV.158   
     *                               + POLY(IP,2,2)*FUGT(IM,1,L)             DANALV.159   
               IF (NHEM.EQ.2) THEN                                           DANALV.160   
C                 Temperature tendency : (complete)       : type 2.          DANALV.161   
                  TT(IP,2,L)=TT(IP,2,L) + POLY(IP,2,1)*TNLG(IM,2,L)          DANALV.162   
                  IF (NTRAC.EQ.1) THEN                                       DANALV.163   
C                    Tracer tendency   : (complete)       : type 2.          DANALV.164   
                     QT(IP,2,L)=QT(IP,2,L) + POLY(IP,2,1)*QNLG(IM,2,L)       DANALV.165   
                  ENDIF                                                      DANALV.166   
C                 Divergence tendency  : d/dx part        : type 2.          DANALV.167   
C                 Divergence tendency  : d/dy part        : type 4.          DANALV.168   
                  DT(IP,2,L)=DT(IP,2,L) + POLY(IP,2,1)*FUG (IM,2,L)          DANALV.169   
     *                                  + POLY(IP,2,2)*FVGT(IM,2,L)          DANALV.170   
C                 Vorticity tendency   : d/dx part        : type 1.          DANALV.171   
C                 Vorticity tendency   : d/dy part        : type 3.          DANALV.172   
                  ZT(IP,2,L)=ZT(IP,2,L) + POLY(IP,1,1)*FVG (IM,2,L)          DANALV.173   
     *                                  + POLY(IP,1,2)*FUGT(IM,2,L)          DANALV.174   
               ENDIF                                                         DANALV.175   
   50 CONTINUE                                                               DANALV.176   
C                                                                            DANALV.177   
C     Transform tracers, treated as a single (NL*NTRAC)-level field.         DANALV.178   
C                                                                            DANALV.179   
      IF (NTRAC.GT.1) THEN                                                   DANALV.180   
         IM=0                                                                DANALV.181   
         IP=0                                                                DANALV.182   
         DO 60 M=0,MM-1,MOCT                                                 DANALV.183   
            IM=IM+1                                                          DANALV.184   
            IPM=IP                                                           DANALV.185   
            DO 60 L=1,NL*NTRAC                                               DANALV.186   
               IP=IPM                                                        DANALV.187   
               DO 60 N=M,NN-1,2                                              DANALV.188   
                  IP=IP+1                                                    DANALV.189   
C                 Tracer tendency   : (complete)       : type 2.             DANALV.190   
                  QT(IP,1,L)=QT(IP,1,L) + POLY(IP,1,1)*QNLG(IM,1,L)          DANALV.191   
                  IF (NHEM.EQ.2) THEN                                        DANALV.192   
                     QT(IP,2,L)=QT(IP,2,L) + POLY(IP,2,1)*QNLG(IM,2,L)       DANALV.193   
                  ENDIF                                                      DANALV.194   
   60    CONTINUE                                                            DANALV.195   
      ENDIF                                                                  DANALV.196   
C                                                                            DANALV.197   
      RETURN                                                                 DANALV.198   
      END                                                                    DANALV.199   
