C**********************************************************                  VDIFF.2     
C             SUBROUTINE VDIFF                                               VDIFF.3     
C**********************************************************                  VDIFF.4     
      SUBROUTINE VDIFF                                                       VDIFF.5     
C                                                                            VDIFF.6     
C     VERTICAL DIFFUSION OF MOMENTUM, HEAT AND MOISTURE.                     VDIFF.7     
C     CONSTANT DIFFUSION COEFFICIENTS SET IN INITAL.                         VDIFF.8     
C     ASSUMES ZERO FLUX AT UPPER AND LOWER BOUNDARIES.                       VDIFF.9     
C     SURFACE FLUX DEALT WITH SEPARATELY IN BLAYER.                          VDIFF.10    
C                                                                            VDIFF.11    
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                                                                            GRIDPP.2     
C     Array ordering in GRIDP must correspond to that in SPECTR.             GRIDPP.3     
C     Real arrays: multi-level arrays are 2-dimensional.                     GRIDPP.4     
C     the variables have been renamed to coincide with                       GRIDPP.5     
C     variable names in bgcm5 DGRMLT                                         GRIDPP.6     
C                                                                            GRIDPP.7     
C swapped round VLNG and UNLG to check                                       GRIDPP.8     
      COMMON/GRIDP/ CHIG(IGC,NL),SFG(IGC,NL),UG(IGC,NL),VG(IGC,NL)           GRIDPP.9     
     :              ,TTVD(IGC,NL),QTVD(IGC,NL),TG(IGC,NL)                    GRIDPP.10    
     :              ,TRAG(IGC,NL,NTRAC)                                      GRIDPP.11    
     :              ,PLG(IGC),TYBL(IGC),TXBL(IGC)                            GRIDPP.12    
     :              ,SPG(IGC),VPG(IGC),TTRD(IGC,NL)                          GRIDPP.13    
     :              ,TNLG(IGC,NL),TRANLG(IGC,NL,NTRAC),UNLG(IGC,NL)          GRIDPP.14    
     :              ,VNLG(IGC,NL),TTLR(IGC,NL),UTRAG(IGC,NL,NTRAC)           GRIDPP.15    
     :              ,TTCR(IGC,NL),VTRAG(IGC,NL,NTRAC)                        GRIDPP.16    
     :              ,UTVD(IGC,NL),VTVD(IGC,NL)                               GRIDPP.17    
     :         ,ASSBL(IGC),ASHBL(IGC),ASLBL(IGC),ARRCR(IGC),ARRLR(IGC)       GRIDPP.18    
     :         ,arflux(igc,6),asfld(igc,6),acld(igc,4)                       GRIDPP.19    
     :         ,SSBL(IGC),SHBL(IGC),SLBL(IGC),RRCR(IGC),RRLR(IGC)            GRIDPP.20    
     :         ,rflux(igc,6),sfld(igc,6),cld(igc,4)                          GRIDPP.21    
C                                                                            GRIDPP.22    
      COMMON/PHYS/  LBL,LVD,LCR,LLR,LRD,LCUBM,LCBADJ                         PHYSCA.2     
     :              ,TSTAR(IGC,JG),QSTAR(IGC,JG),FRAD(JG,NHEM)               PHYSCA.3     
     :              ,TSTARO(IGC,JG),TDEEPO(IGC,JG),smstar(igc,jg)            PHYSCA.4     
     :              ,tdeep(igc,jg),hsnow(igc,jg),sqstar(igc,jg)              PHYSCA.5     
     :              ,SALB(IGC,JG),SBAL(IGC,JG),BLCD(IGC)                     PHYSCA.6     
     :              ,SVEGE(IGC,JG),CD,DRAG,BLVAD,BLA,BLRH,BLVB(IGC)          PHYSCA.7     
     :              ,AKVV,AKTV,AKQV,ESCONA,ESCONB,EPSIQ,CTQ,CCC              PHYSCA.8     
     : ,ctqi,sdsn,shcs,shcsp,shcsn,skse,sksn,slhf,sd1,sd2,sdw                PHYSCA.9     
     :        ,ssmc,sdsnd,LSL,sasnow,saice,shsstar,shsmax                    PHYSCA.10    
     :     ,LOC,SHCO,SHCI,LNOICE,LOLDBL,LCOND,LNNSK,ITSLL,ITSLO              PHYSCA.11    
     :              ,CCR,RCON,DTBUOY,TSLA,TSLB,TSLC,TSLD,CUT1,CUT2           PHYSCA.12    
     :              ,NLCR,NCUTOP,CURHM,AKTC,AKQC,CUBMT,CBADJT,CBADJP         PHYSCA.13    
     :              ,SKAP(NL),SK(NLM),FWS(NL),CLR(NL),FB(NLM)                PHYSCA.14    
     :              ,TTDC(NL),QTDC(NL),TTMC(NL),QTMC(NL),TC(NL),QC(NL)       PHYSCA.15    
     :              ,CTCR(NL,NHEM),CTLR(NL,NHEM),NAVRD,NAVWT,DELT2C          PHYSCA.16    
      LOGICAL LBL,LVD,LCR,LLR,LRD,LCUBM,LCBADJ,LSL,LOC                       PHYSCA.17    
     :       ,LNOICE,LOLDBL,LCOND,LNNSK                                      PHYSCA.18    
C                                                                            PHYSCA.19    
C                                                                            VDIFF.18    
      REAL BVP(MG),BQP(MG),BTP(MG),BVM(MG),BQM(MG),BTM(MG)                   VDIFF.19    
      REAL FA(NL)                                                            VDIFF.20    
C                                                                            XTRAP.2     
C     Setup moisture variables by equivilencing them to                      XTRAP.3     
C     Tracer No. 1                                                           XTRAP.4     
C                                                                            XTRAP.5     
      REAL QG(IGC,NL),QNLG(IGC,NL),QTLR(IGC,NL),QTCR(IGC,NL)                 XTRAP.6     
      EQUIVALENCE (QG(1,1),TRAG(1,1,1)) , (QNLG(1,1),TRANLG(1,1,1))          XTRAP.7     
     *            ,(QTLR(1,1),UTRAG(1,1,1)),(QTCR(1,1),VTRAG(1,1,1))         XTRAP.8     
C                                                                            XTRAP.9     
C                                                                            VDIFF.22    
      DO 10 L=1,NL                                                           VDIFF.23    
   10 FA(L)=1.0/DSIGMA(L)                                                    VDIFF.24    
C                                                                            VDIFF.25    
      DO 800 IHEM=1,NHEM                                                     VDIFF.26    
        IOFM=(IHEM-1)*MGPP                                                   VDIFF.27    
        DO 20 I=1,MG                                                         VDIFF.28    
          J=I+IOFM                                                           VDIFF.29    
          TLPH=TG(J,2)+TG(J,1)                                               VDIFF.30    
          FTSQ=FB(1)/(TLPH*TLPH)                                             VDIFF.31    
          BVP(I)=AKVV*FTSQ                                                   VDIFF.32    
          BQP(I)=AKQV*FTSQ                                                   VDIFF.33    
          BTP(I)=AKTV*FTSQ                                                   VDIFF.34    
          UTVD(J,1)=FA(1)*BVP(I)*(UG(J,2)-UG(J,1))                           VDIFF.35    
          VTVD(J,1)=FA(1)*BVP(I)*(VG(J,2)-VG(J,1))                           VDIFF.36    
          QTVD(J,1)=FA(1)*BQP(I)*(QG(J,2)-QG(J,1))                           VDIFF.37    
          TTVD(J,1)=FA(1)*BTP(I)*(SK(1)*TG(J,2)-TG(J,1))                     VDIFF.38    
   20   CONTINUE                                                             VDIFF.39    
        DO 30 L=2,NLM                                                        VDIFF.40    
          LP=L+1                                                             VDIFF.41    
          LM=L-1                                                             VDIFF.42    
          DO 30 I=1,MG                                                       VDIFF.43    
            J=I+IOFM                                                         VDIFF.44    
            BVM(I)=BVP(I)                                                    VDIFF.45    
            BQM(I)=BQP(I)                                                    VDIFF.46    
            BTM(I)=BTP(I)                                                    VDIFF.47    
            TLPH=TG(J,LP)+TG(J,L)                                            VDIFF.48    
            FTSQ=FB(L)/(TLPH*TLPH)                                           VDIFF.49    
            BVP(I)=AKVV*FTSQ                                                 VDIFF.50    
            BQP(I)=AKQV*FTSQ                                                 VDIFF.51    
            BTP(I)=AKTV*FTSQ                                                 VDIFF.52    
            IF (L.EQ.NLM) THEN                                               VDIFF.53    
              if (LBL.and.(.not.LOLDBL)) THEN                                VDIFF.54    
                BVP(I)=BVP(I)*5.                                             VDIFF.55    
C                BQP(I)=BQP(I)*5.                                            VDIFF.56    
C                BTP(I)=BTP(I)*5.                                            VDIFF.57    
              endif                                                          VDIFF.58    
            ENDIF                                                            VDIFF.59    
            UTVD(J,L)=FA(L)*( BVP(I)*(UG(J,LP)-UG(J,L))                      VDIFF.60    
     :                 -BVM(I)*(UG(J,L)-UG(J,LM)))                           VDIFF.61    
            VTVD(J,L)=FA(L)*( BVP(I)*(VG(J,LP)-VG(J,L))                      VDIFF.62    
     :                 -BVM(I)*(VG(J,L)-VG(J,LM)))                           VDIFF.63    
            QTVD(J,L)=FA(L)*( BQP(I)*(QG(J,LP)-QG(J,L))                      VDIFF.64    
     :                 -BQM(I)*(QG(J,L)-QG(J,LM)))                           VDIFF.65    
            TTVD(J,L)=FA(L)*( BTP(I)*(SK(L)*TG(J,LP)-TG(J,L))                VDIFF.66    
     :                 -BTM(I)*(TG(J,L)-TG(J,LM)/SK(LM)))                    VDIFF.67    
   30   CONTINUE                                                             VDIFF.68    
        DO 40 I=1,MG                                                         VDIFF.69    
          J=I+IOFM                                                           VDIFF.70    
          UTVD(J,NL)=-FA(NL)*BVP(I)*(UG(J,NL)-UG(J,NLM))                     VDIFF.71    
          VTVD(J,NL)=-FA(NL)*BVP(I)*(VG(J,NL)-VG(J,NLM))                     VDIFF.72    
          QTVD(J,NL)=-FA(NL)*BQP(I)*(QG(J,NL)-QG(J,NLM))                     VDIFF.73    
          TTVD(J,NL)=-FA(NL)*BTP(I)*(TG(J,NL)-TG(J,NLM)/SK(NLM))             VDIFF.74    
   40   CONTINUE                                                             VDIFF.75    
        DO 50 L=1,NL                                                         VDIFF.76    
          DO 50 I=1,MG                                                       VDIFF.77    
            J=I+IOFM                                                         VDIFF.78    
            UG(J,L)=UG(J,L)+DELT2C*UTVD(J,L)                                 VDIFF.79    
            VG(J,L)=VG(J,L)+DELT2C*VTVD(J,L)                                 VDIFF.80    
            QG(J,L)=QG(J,L)+DELT2C*QTVD(J,L)                                 VDIFF.81    
            TG(J,L)=TG(J,L)+DELT2C*TTVD(J,L)                                 VDIFF.82    
   50   CONTINUE                                                             VDIFF.83    
  800 CONTINUE                                                               VDIFF.84    
C                                                                            VDIFF.85    
      RETURN                                                                 VDIFF.86    
      END                                                                    VDIFF.87    
      subroutine abort                                                       NETCDF.53    
      stop 1                                                                 NETCDF.54    
      end                                                                    NETCDF.55    
