C**********************************************************                  MGRMLT.2     
C             SUBROUTINE MGRMLT                                              MGRMLT.3     
C**********************************************************                  MGRMLT.4     
      SUBROUTINE MGRMLT                                                      MGRMLT.5     
C                                                                            MGRMLT.6     
C     Computes nonlinear tendencies in grid point space                      MGRMLT.7     
C     for the present latitude                                               MGRMLT.8     
C                                                                            MGRMLT.9     
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                                                                            GRIDP2.2     
C     Array ordering in GRIDP must correspond to that in SPECTR.             GRIDP2.3     
C     Real arrays: multi-level arrays are 2-dimensional.                     GRIDP2.4     
C                                                                            GRIDP2.5     
      COMMON/GRIDP/ CHIG(IGC,NL),SFG(IGC,NL),UG(IGC,NL),VG(IGC,NL)           GRIDP2.6     
     :              ,ZG(IGC,NL),DG(IGC,NL),TG(IGC,NL)                        GRIDP2.7     
     :              ,TRAG(IGC,NL,NTRAC)                                      GRIDP2.8     
     :              ,PLG(IGC),PJG(IGC),PMG(IGC)                              GRIDP2.9     
     :              ,SPG(IGC),VPG(IGC),EG(IGC,NL)                            GRIDP2.10    
     :              ,TNLG(IGC,NL),TRANLG(IGC,NL,NTRAC),FUG(IGC,NL)           GRIDP2.11    
     :              ,FVG(IGC,NL),UTG(IGC,NL),UTRAG(IGC,NL,NTRAC)             GRIDP2.12    
     :              ,VTG(IGC,NL),VTRAG(IGC,NL,NTRAC)                         GRIDP2.13    
     :              ,FVGT(IGC,NL),FUGT(IGC,NL)                               GRIDP2.14    
     :              ,GRPAD(NGRPAD)                                           GRIDP2.15    
C                                                                            GRIDP2.16    
      DIMENSION SDOTP(MG,NLM),SUMD(MG),TPTA(MG),TPTB(MG)                     MGRMLT.15    
      DIMENSION CC(NL,NL)                                                    MGRMLT.16    
      EQUIVALENCE (CC(1,1),C(1))                                             MGRMLT.17    
C                                                                            MGRMLT.18    
      IOFM=0                                                                 MGRMLT.19    
C                                                                            MGRMLT.20    
C     Loop over hemispheres                                                  MGRMLT.21    
C                                                                            MGRMLT.22    
      DO 800 IHEM=1,NHEM                                                     MGRMLT.23    
         DO 100 I=1,MG                                                       MGRMLT.24    
            J=I+IOFM                                                         MGRMLT.25    
            SUMD(I)=0.0                                                      MGRMLT.26    
            VPG(J)=0.0                                                       MGRMLT.27    
C                                                                            MGRMLT.28    
C           Change from Ln(PSTAR) to PSTAR                                   MGRMLT.29    
C                                                                            MGRMLT.30    
            SPG(J)=EXP(PLG(J))-1.0                                           MGRMLT.31    
C                                                                            MGRMLT.32    
 100     CONTINUE                                                            MGRMLT.33    
         DO 110 L=1,NLM                                                      MGRMLT.34    
            DO 120 I=1,MG                                                    MGRMLT.35    
              J=I+IOFM                                                       MGRMLT.36    
              SUMD(I)=SUMD(I)+DSIGMA(L)*DG(J,L)                              MGRMLT.37    
              VPG(J)=VPG(J)+DSIGMA(L)*SECSQ(JH)*(UG(J,L)*PMG(J)+             MGRMLT.38    
     1               VG(J,L)*PJG(J))                                         MGRMLT.39    
              SDOTP(I,L)=SUMD(I)+VPG(J)                                      MGRMLT.40    
 120        CONTINUE                                                         MGRMLT.41    
 110     CONTINUE                                                            MGRMLT.42    
         DO 121 I=1,MG                                                       MGRMLT.43    
            J=I+IOFM                                                         MGRMLT.44    
            SUMD(I)=SUMD(I)+DSIGMA(NL)*DG(J,NL)                              MGRMLT.45    
            VPG(J)=VPG(J)+DSIGMA(NL)*SECSQ(JH)*(UG(J,NL)*PMG(J)+             MGRMLT.46    
     1             VG(J,NL)*PJG(J))                                          MGRMLT.47    
 121     CONTINUE                                                            MGRMLT.48    
         DO 130 L=1,NLM                                                      MGRMLT.49    
            DO 140 I=1,MG                                                    MGRMLT.50    
               J=I+IOFM                                                      MGRMLT.51    
               SDOTP(I,L)=SIGMAH(L)*(SUMD(I)+VPG(J))-SDOTP(I,L)              MGRMLT.52    
 140        CONTINUE                                                         MGRMLT.53    
 130     CONTINUE                                                            MGRMLT.54    
         DO 150 I=1,MG                                                       MGRMLT.55    
            SUMD(I)=0.0                                                      MGRMLT.56    
 150     CONTINUE                                                            MGRMLT.57    
         DO 160 L=1,NL                                                       MGRMLT.58    
            DO 170 I=1,MG                                                    MGRMLT.59    
               TPTA(I)=0.0                                                   MGRMLT.60    
               TPTB(I)=0.0                                                   MGRMLT.61    
 170        CONTINUE                                                         MGRMLT.62    
            DO 180 LL=1,L                                                    MGRMLT.63    
               DO 190 I=1,MG                                                 MGRMLT.64    
                  J=I+IOFM                                                   MGRMLT.65    
                  VGPG=SECSQ(JH)*(UG(J,LL)*PMG(J)+VG(J,LL)*PJG(J))           MGRMLT.66    
                  TPTA(I)=TPTA(I)+CC(LL,L)*VGPG                              MGRMLT.67    
                  TPTB(I)=TPTB(I)+CC(LL,L)*(VGPG+DG(J,LL))                   MGRMLT.68    
 190           CONTINUE                                                      MGRMLT.69    
 180        CONTINUE                                                         MGRMLT.70    
            DO 200 I=1,MG                                                    MGRMLT.71    
               J=I+IOFM                                                      MGRMLT.72    
               UTG(J,L)=UG(J,L)*TG(J,L)                                      MGRMLT.73    
               VTG(J,L)=VG(J,L)*TG(J,L)                                      MGRMLT.74    
               EG(J,L)=UG(J,L)*UG(J,L)+VG(J,L)*VG(J,L)                       MGRMLT.75    
 200        CONTINUE                                                         MGRMLT.76    
            DO 202 KK=1,NTRAC                                                MGRMLT.77    
               DO 201 I=1,MG                                                 MGRMLT.78    
                  J=I+IOFM                                                   MGRMLT.79    
                  UTRAG(J,L,KK)=UG(J,L)*TRAG(J,L,KK)                         MGRMLT.80    
                  VTRAG(J,L,KK)=VG(J,L)*TRAG(J,L,KK)                         MGRMLT.81    
  201          CONTINUE                                                      MGRMLT.82    
  202       CONTINUE                                                         MGRMLT.83    
            IF (L.GT.1.AND.L.LT.NL) THEN                                     MGRMLT.84    
               DO 210 I=1,MG                                                 MGRMLT.85    
                  J=I+IOFM                                                   MGRMLT.86    
                  TSUM=SECSQ(JH)*(UG(J,L)*PMG(J)+VG(J,L)*PJG(J))             MGRMLT.87    
                  SUMD(I)=SUMD(I)+TSUM*DSIGMA(L)                             MGRMLT.88    
                  TNLG(J,L)=TG(J,L)*DG(J,L)+AKAP*TG(J,L)*(TSUM-TPTB(I))+     MGRMLT.89    
     1                      TKP(L)*(TSUM-TPTA(I))-                           MGRMLT.90    
     2                      RDSIG(L)*(SDOTP(I,L)*(TG(J,L+1)-TG(J,L))+        MGRMLT.91    
     3                      SDOTP(I,L-1)*(TG(J,L)-                           MGRMLT.92    
     4                      TG(J,L-1))+VPG(J)*(T01S2(L)*SIGMAH(L)+           MGRMLT.93    
     5                      T01S2(L-1)*SIGMAH(L-1))-                         MGRMLT.94    
     6                      SUMD(I)*(T01S2(L-1)+T01S2(L))+                   MGRMLT.95    
     7                      TSUM*DSIGMA(L)*T01S2(L-1))                       MGRMLT.96    
                  FVG(J,L)=-UG(J,L)*ZG(J,L)-PJG(J)*TG(J,L)-                  MGRMLT.97    
     1                     RDSIG(L)*(SDOTP(I,L)*(VG(J,L+1)-VG(J,L))+         MGRMLT.98    
     2                     SDOTP(I,L-1)*(VG(J,L)-VG(J,L-1)))                 MGRMLT.99    
                  FUG(J,L)=VG(J,L)*ZG(J,L)-PMG(J)*TG(J,L)-                   MGRMLT.100   
     1                     RDSIG(L)*(SDOTP(I,L)*(UG(J,L+1)-UG(J,L))+         MGRMLT.101   
     2                     SDOTP(I,L-1)*(UG(J,L)-UG(J,L-1)))                 MGRMLT.102   
 210           CONTINUE                                                      MGRMLT.103   
               DO 213 KK=1,NTRAC                                             MGRMLT.104   
                  IF (.NOT.LFLUX) THEN                                       MGRMLT.105   
                     DO 211 I=1,MG                                           MGRMLT.106   
                        J=I+IOFM                                             MGRMLT.107   
                        TRANLG(J,L,KK)=TRAG(J,L,KK)*DG(J,L)-RDSIG(L)*        MGRMLT.108   
     1                    (SDOTP(I,L  )*(TRAG(J,L+1,KK)-TRAG(J,L  ,KK))      MGRMLT.109   
     2                    +SDOTP(I,L-1)*(TRAG(J,L  ,KK)-TRAG(J,L-1,KK)))     MGRMLT.110   
  211                CONTINUE                                                MGRMLT.111   
                  ELSE                                                       MGRMLT.112   
                     DO 212 I=1,MG                                           MGRMLT.113   
                        J=I+IOFM                                             MGRMLT.114   
                        TRANLG(J,L,KK)=RDSIG(L)*                             MGRMLT.115   
     1                    (SDOTP(I,L  )*(TRAG(J,L+1,KK)+TRAG(J,L  ,KK))      MGRMLT.116   
     2                    -SDOTP(I,L-1)*(TRAG(J,L  ,KK)+TRAG(J,L-1,KK)))     MGRMLT.117   
  212                CONTINUE                                                MGRMLT.118   
                  ENDIF                                                      MGRMLT.119   
  213          CONTINUE                                                      MGRMLT.120   
            ELSE                                                             MGRMLT.121   
               FAC=1.0                                                       MGRMLT.122   
               FAC2=1.                                                       MGRMLT.123   
               K=L                                                           MGRMLT.124   
               IF (L.EQ.NL) THEN                                             MGRMLT.125   
                  K=L-1                                                      MGRMLT.126   
                  FAC=0.0                                                    MGRMLT.127   
                  FAC2=-1.                                                   MGRMLT.128   
               ENDIF                                                         MGRMLT.129   
               DO 220 I=1,MG                                                 MGRMLT.130   
                  J=I+IOFM                                                   MGRMLT.131   
                  TSUM=SECSQ(JH)*(UG(J,L)*PMG(J)+VG(J,L)*PJG(J))             MGRMLT.132   
                  SUMD(I)=SUMD(I)+TSUM*DSIGMA(L)*FAC                         MGRMLT.133   
                  TNLG(J,L)=TG(J,L)*DG(J,L)+AKAP*TG(J,L)*(TSUM-TPTB(I))+     MGRMLT.134   
     1                      TKP(L)*(TSUM-TPTA(I))-                           MGRMLT.135   
     2                      RDSIG(L)*(SDOTP(I,K)*(TG(J,K+1)-TG(J,K))+        MGRMLT.136   
     3                      T01S2(K)*(SIGMAH(K)*VPG(J)-SUMD(I)))             MGRMLT.137   
                  FVG(J,L)=-UG(J,L)*ZG(J,L)-PJG(J)*TG(J,L)-                  MGRMLT.138   
     1                     RDSIG(L)*(SDOTP(I,K)*(VG(J,K+1)-VG(J,K)))         MGRMLT.139   
                  FUG(J,L)=VG(J,L)*ZG(J,L)-PMG(J)*TG(J,L)-                   MGRMLT.140   
     1                     RDSIG(L)*(SDOTP(I,K)*(UG(J,K+1)-UG(J,K)))         MGRMLT.141   
 220           CONTINUE                                                      MGRMLT.142   
               DO 223 KK=1,NTRAC                                             MGRMLT.143   
                  IF (.NOT.LFLUX) THEN                                       MGRMLT.144   
                     DO 221 I=1,MG                                           MGRMLT.145   
                        J=I+IOFM                                             MGRMLT.146   
                        TRANLG(J,L,KK)=TRAG(J,L,KK)*DG(J,L)-RDSIG(L)*        MGRMLT.147   
     1                         SDOTP(I,K)*(TRAG(J,K+1,KK)-TRAG(J,K,KK))      MGRMLT.148   
  221                CONTINUE                                                MGRMLT.149   
                  ELSE                                                       MGRMLT.150   
                     DO 222 I=1,MG                                           MGRMLT.151   
                        J=I+IOFM                                             MGRMLT.152   
                        TRANLG(J,L,KK)=RDSIG(L)*FAC2*                        MGRMLT.153   
     1                         SDOTP(I,K)*(TRAG(J,K+1,KK)+TRAG(J,K,KK))      MGRMLT.154   
  222                CONTINUE                                                MGRMLT.155   
                  ENDIF                                                      MGRMLT.156   
  223          CONTINUE                                                      MGRMLT.157   
            ENDIF                                                            MGRMLT.158   
 160     CONTINUE                                                            MGRMLT.159   
         IOFM=MGPP                                                           MGRMLT.160   
 800  CONTINUE                                                               MGRMLT.161   
      RETURN                                                                 MGRMLT.162   
      END                                                                    MGRMLT.163   
