C**********************************************************                  CUBM.2     
C             SUBROUTINE CUBM                                                CUBM.3     
C**********************************************************                  CUBM.4     
      SUBROUTINE CUBM(NCRB,NCT,J,IHEM)                                       CUBM.5     
C                                                                            CUBM.6     
C     BETTS-MILLER NON-PRECIPITATING CONVECTION. FOLLOWS ECMWF TECH REP      CUBM.7     
C     NO 43. VARIABLE TIMESCALE AND CLOUD-TOP FLUXES WHEN SHALLOW.           CUBM.8     
C     OPTIONAL RAINOUT OVER A SPECIFIED REL-HUM.                             CUBM.9     
C                                                                            CUBM.10    
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                                                                            BATS.2     
C     Constant arrays and variables associated with time and vertical        BATS.3     
C     differencing. Also counters.                                           BATS.4     
C                                                                            BATS.5     
      COMMON/BATS/  BM1(IDE),AK(NNP),AQ(NL2),G(NL2),TAU(NL2)                 BATS.6     
     +              ,KOUNT,KITS,KSTART,KTOTAL,KRUN,BEGDAY,ITSPD              BATS.7     
     +              ,DELT,DELT2,CV,CG,CT,CQ,PNU,PNU2,PNU21                   BATS.8     
     +              ,NTRACO,CTRA(NTRAC),KOLOUR(NTRAC),RGG(NL2)               BATS.9     
     +              ,BEGDOY,DOY                                              BATS.10    
C                                                                            BATS.11    
       COMMON/CPIERS/ICFLAG(IGC,5,2),CFRAC(IGC,5),PNET(IGC,JG)               CPIERS.2     
     :               ,SNET(IGC,JG),RRFLUX(IGC,JG,6)                          CPIERS.3     
     :               ,TTSW(IGC,NL),TTLW(IGC,NL)                              CPIERS.4     
C                                                                            CUBM.19    
      REAL PL(NL),TH(NL),PSL(NL),TSL(NL),SCRIP(NL),PFK(NL)                   CUBM.20    
     :,TR(NL),QR(NL),THR(NL)                                                 CUBM.21    
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     
      FTSL(T,Q,P)=TSLA+TSLB/(TSLC*LOG(T)-LOG(Q*P)+TSLD)                      CUBM.23    
C                                                                            CUBM.24    
 2001 FORMAT(' CUBM QMIN APPLIED: I,J,L,NCRB,NCRT,QG = ',5I5,F10.4)          CUBM.25    
 2002 FORMAT(' CUBM QMIN APPLIED FOR QM: I,J,NCRB,NCRT,QM = ',4I5,F10.4)     CUBM.26    
 2004 FORMAT(' CUBM MID LEVEL SWAP IGNORED: I,J,NCRB,NCRT =',4I5)            CUBM.27    
 2103 FORMAT(' CUBM LAPSE RATE LIMIT IMPOSED: I,J,NCRB,NCRT = ',4I5)         CUBM.28    
 2104 FORMAT(' CUBM LAPSE RATE LIMIT IMPOSED: I,J,NCRB,NCRT,TLIM             CUBM.29    
     +                                                     = ',4I5,F7.4)     CUBM.30    
C                                                                            CUBM.31    
C Minimum moisture for mixing line calculations.                             CUBM.32    
      QGMIN=1.E-6                                                            CUBM.33    
      IF(NCT.GE.NCUTOP) THEN                                                 CUBM.34    
        IF(BLVB(J).EQ.0.0)RETURN                                             CUBM.35    
        NCRT=NCT-1                                                           CUBM.36    
        CUTAU=CUT1/(BLVB(J)*BLCD(J))                                         CUBM.37    
      ELSEIF (NCRB.GE.NCUTOP) THEN                                           CUBM.38    
C Limit the convective top for swap points to the maximum                    CUBM.39    
C height of shallow convection.                                              CUBM.40    
C Note that NCRT includes the level above cloud top.                         CUBM.41    
        NCRT=NCUTOP-1                                                        CUBM.42    
        CUTAU=CUBMT                                                          CUBM.43    
      ELSE                                                                   CUBM.44    
C No adjustment for mid-level swap points.                                   CUBM.45    
C         WRITE(2,2005) J,JH,NCRB,NCT                                        CUBM.46    
C 2005    FORMAT(' CUBM MID LEVEL SWAP IGNORED: I,J,NCRB,NCRT =',4I5)        CUBM.47    
         RETURN                                                              CUBM.48    
      ENDIF                                                                  CUBM.49    
C Stable limit to mixing line slope d(theta)/dp=-10^-3 K/Pa                  CUBM.50    
      TLIM=-1.0E-3*P0/CT                                                     CUBM.51    
C shallow convection                                                         CUBM.52    
      ICFLAG(J,4,1)=NCRB                                                     CUBM.53    
      ICFLAG(J,4,2)=NCRT                                                     CUBM.54    
      CFRAC(J,4)=0.35  ! cloud fraction for all shallow convection           CUBM.55    
      NLEV=1+NCRB-NCRT                                                       CUBM.56    
      RKAP=1.0/AKAP                                                          CUBM.57    
      DO 10 L=NCRT,NCRB                                                      CUBM.58    
       PL(L)=SIGMA(L)*PLG(J)                                                 CUBM.59    
       PFK(L)=PL(L)**AKAP                                                    CUBM.60    
       TH(L)=TG(J,L)/PFK(L)                                                  CUBM.61    
C Apply a consistent minimum moisture criterion in the mixing line           CUBM.62    
C calculations.                                                              CUBM.63    
C       IF (QG(J,L).LT.QGMIN) THEN                                           CUBM.64    
C         WRITE(2,2001) J,JH,L,NCRB,NCRT,CQ*QG(J,L)                          CUBM.65    
C       ENDIF                                                                CUBM.66    
       QQG=MAX(QG(J,L),QGMIN)                                                CUBM.67    
       TSL(L)=FTSL(TG(J,L),QQG,PL(L))                                        CUBM.68    
       PSL(L)=PL(L)*(TSL(L)/TG(J,L))**RKAP                                   CUBM.69    
       SCRIP(L)=PSL(L)-PL(L)                                                 CUBM.70    
   10 CONTINUE                                                               CUBM.71    
C                                                                            CUBM.72    
      THM=0.5*(TH(NCRB)+TH(NCRT))                                            CUBM.73    
      QM=0.5*(QG(J,NCRB)+QG(J,NCRT))                                         CUBM.74    
C      IF (QM.LT.QGMIN) THEN                                                 CUBM.75    
C         WRITE(2,2002) J,JH,NCRB,NCRT,CQ*QM                                 CUBM.76    
C      ENDIF                                                                 CUBM.77    
      QM=MAX(QM,QGMIN)                                                       CUBM.78    
      TMT=THM*PFK(NCRT)                                                      CUBM.79    
      TSLM=FTSL(TMT,QM,PL(NCRT))                                             CUBM.80    
      PSLM=PL(NCRT)*(TSLM/TMT)**RKAP                                         CUBM.81    
      TLAPSM=(THM-TH(NCRB))/(PSLM-PSL(NCRB))                                 CUBM.82    
      TLAPSM=MAX(TLAPSM,TLIM) ! limit stability                              CUBM.83    
      TLAPSM=MIN(TLAPSM,0.0) ! prevents instability                          CUBM.84    
      IF (TLAPSM.GT.0.0) THEN                                                CUBM.85    
         WRITE(2,2103) J,JH,NCRB,NCRT                                        CUBM.86    
      ELSEIF (TLAPSM.LT.TLIM)THEN                                            CUBM.87    
         WRITE(2,2104) J,JH,NCRB,NCRT,TLIM                                   CUBM.88    
      ENDIF                                                                  CUBM.89    
      SINT=SSUM(NLEV,DSIGMA(NCRT),1)                                         CUBM.90    
      SCRINT=SDOT(NLEV,SCRIP(NCRT),1,DSIGMA(NCRT),1)/SINT                    CUBM.91    
      SCRINT=MIN(0.0,SCRINT)                                                 CUBM.92    
C                                                                            CUBM.93    
      DO 20 L=NCRT,NCRB                                                      CUBM.94    
        THR(L)=TH(NCRB)+TLAPSM*(PL(L)-PL(NCRB))                              CUBM.95    
        TR(L) =THR(L)*PFK(L)                                                 CUBM.96    
   20 CONTINUE                                                               CUBM.97    
      TGINT=SDOT(NLEV,TG(J,NCRT),IGC,DSIGMA(NCRT),1)/SINT                    CUBM.98    
      TRINT=SDOT(NLEV,TR(NCRT),1,DSIGMA(NCRT),1)/SINT                        CUBM.99    
      DTINT=TGINT-TRINT                                                      CUBM.100   
      DO 25 L=NCRT,NCRB                                                      CUBM.101   
        TR(L)=TR(L)+DTINT                                                    CUBM.102   
        PSL(L)=SCRINT+PL(L)                                                  CUBM.103   
        TSL(L)=TR(L)*(PSL(L)/PL(L))**AKAP                                    CUBM.104   
        QR(L)=PQSAT(TSL(L))/PSL(L)                                           CUBM.105   
   25 CONTINUE                                                               CUBM.106   
      QGINT=SDOT(NLEV,QG(J,NCRT),IGC,DSIGMA(NCRT),1)/SINT                    CUBM.107   
      QRINT=SDOT(NLEV,QR(NCRT),1,DSIGMA(NCRT),1)/SINT                        CUBM.108   
      DQINT=QGINT-QRINT                                                      CUBM.109   
      DO 30 L=NCRT,NCRB                                                      CUBM.110   
        QR(L)=QR(L)+DQINT                                                    CUBM.111   
        TTMC(L)=(TR(L)-TG(J,L))/CUTAU                                        CUBM.112   
        QTMC(L)=(QR(L)-QG(J,L))/CUTAU                                        CUBM.113   
   30 CONTINUE                                                               CUBM.114   
C                                                                            CUBM.115   
      IF(CURHM.LT.1.00001) THEN                                              CUBM.116   
        DO 40 L=NCRT,NCRB                                                    CUBM.117   
          TGP=TG(J,L)+DELT2C*TTMC(L)                                         CUBM.118   
          QGP=QG(J,L)+DELT2C*QTMC(L)                                         CUBM.119   
          QS=CURHM*PQSAT(TGP)/PL(L)                                          CUBM.120   
          QEXS=QGP-QS                                                        CUBM.121   
C     Choose appropriate value of latent heat based on ground T              CUBM.122   
c     ie, we assume it snows if the ground is colder than 273.15K            CUBM.123   
          IF (tstar(j,jh).gt.0.363224029) THEN                               CUBM.124   
            ctquse=ctq                                                       CUBM.125   
          ELSE                                                               CUBM.126   
            ctquse=ctqi                                                      CUBM.127   
          ENDIF                                                              CUBM.128   
          QEX=QEXS/(1.0+CTQ*QS*ESCONB/(TGP*TGP))                             CUBM.129   
          IF (QEXS.LT.0.0) THEN                                              CUBM.130   
           QEX=0.0                                                           CUBM.131   
          END IF                                                             CUBM.132   
          QTCOR=-QEX/DELT2C                                                  CUBM.133   
          TTMC(L)=TTMC(L)-CTQUSE*QTCOR                                       CUBM.134   
          QTMC(L)=QTMC(L)+QTCOR                                              CUBM.135   
          RRCR(J)=RRCR(J)-QTCOR*CLR(L)*PLG(J)                                CUBM.136   
   40   CONTINUE                                                             CUBM.137   
      ENDIF                                                                  CUBM.138   
C                                                                            CUBM.139   
      DO 50 L=NCRT,NCRB                                                      CUBM.140   
        TG(J,L)=TG(J,L)+DELT2C*TTMC(L)                                       CUBM.141   
        QG(J,L)=QG(J,L)+DELT2C*QTMC(L)                                       CUBM.142   
        CTCR(L,IHEM)=CTCR(L,IHEM)+1.0                                        CUBM.143   
   50 CONTINUE                                                               CUBM.144   
      CTCR(NCRB,IHEM)=CTCR(NCRB,IHEM)-1.0                                    CUBM.145   
      CTCR(NCRT,IHEM)=CTCR(NCRT,IHEM)-1.0                                    CUBM.146   
C                                                                            CUBM.147   
      RETURN                                                                 CUBM.148   
      END                                                                    CUBM.149   
