*IF DEF,A03_7A                                                             KMKH7A.2      
C *****************************COPYRIGHT******************************     KMKH7A.3      
C (c) CROWN COPYRIGHT 1998, METEOROLOGICAL OFFICE, All Rights Reserved.    KMKH7A.4      
C                                                                          KMKH7A.5      
C Use, duplication or disclosure of this code is subject to the            KMKH7A.6      
C restrictions as set forth in the contract.                               KMKH7A.7      
C                                                                          KMKH7A.8      
C                Meteorological Office                                     KMKH7A.9      
C                London Road                                               KMKH7A.10     
C                BRACKNELL                                                 KMKH7A.11     
C                Berkshire UK                                              KMKH7A.12     
C                RG12 2SZ                                                  KMKH7A.13     
C                                                                          KMKH7A.14     
C If no contract has been raised with this copy of the code, the use,      KMKH7A.15     
C duplication or disclosure of it is strictly prohibited.  Permission      KMKH7A.16     
C to do so must first be obtained in writing from the Head of Numerical    KMKH7A.17     
C Modelling at the above address.                                          KMKH7A.18     
C ******************************COPYRIGHT******************************    KMKH7A.19     
!  SUBROUTINE KMKH---------------------------------------------------      KMKH7A.20     
!!!                                                                        KMKH7A.21     
!!!  Purpose: To calculate the turbulent mixing coefficients KM and KH     KMKH7A.22     
!!!                                                                        KMKH7A.23     
!!!  Suitable for single column use.                                       KMKH7A.24     
!!!                                                                        KMKH7A.25     
!!!  Model            Modification history                                 KMKH7A.26     
!!! version  date                                                          KMKH7A.27     
!!!                                                                        KMKH7A.28     
!!!   4.5  13/05/98   New deck.  Richard Betts                             KMKH7A.29     
!!!                                                                        KMKH7A.30     
!!!  Documentation: UMDP No.24                                             KMKH7A.31     
!!!                                                                        KMKH7A.32     
!!!---------------------------------------------------------------------   KMKH7A.33     
                                                                           KMKH7A.34     
! Arguments :-                                                             KMKH7A.35     
                                                                           KMKH7A.36     

      SUBROUTINE KMKH (                                                     4,16KMKH7A.37     
     & P_FIELD,P1,P_POINTS,BL_LEVELS                                       KMKH7A.38     
     &,TIMESTEP,P,CCA,BT,BQ,BF,CF,DZL,DTRDZ                                KMKH7A.39     
     &,RDZ,U,V,FTL,FQW                                                     KMKH7A.40     
     &,RHO,Z0M,ZLB,H_BLEND                                                 KMKH7A.41     
     &,QW,QCF,RHOKM,RHO_KM,RHOKH,TL,ZH                                     KMKH7A.42     
     &,CCB,CCT,L_MOM                                                       KMKH7A.43     
     &,NRML,L_BL_LSPICE                                                    KMKH7A.44     
     &,LTIMER                                                              KMKH7A.45     
     & )                                                                   KMKH7A.46     
                                                                           KMKH7A.47     
      IMPLICIT NONE                                                        KMKH7A.48     
                                                                           KMKH7A.49     
      LOGICAL LTIMER             ! IN Flag for TIMER diagnostics           KMKH7A.50     
                                                                           KMKH7A.51     
      LOGICAL                                                              KMKH7A.52     
     & L_MOM       ! IN Switch for convective momentum transport.          KMKH7A.53     
     &,L_BL_LSPICE       ! IN                                              KMKH7A.54     
!                              TRUE  Use scientific treatment of mixed     KMKH7A.55     
!                                    phase precip scheme.                  KMKH7A.56     
!                              FALSE Do not use mixed phase precip         KMKH7A.57     
!                                    considerations                        KMKH7A.58     
                                                                           KMKH7A.59     
      INTEGER                                                              KMKH7A.60     
     & P_FIELD                ! IN No. of P-grid points in whole field     KMKH7A.61     
     &,P1                     ! IN First P-grid point to be processed.     KMKH7A.62     
     &,P_POINTS               ! IN No. of P-grid points to be              KMKH7A.63     
!                                  processed.                              KMKH7A.64     
     &,BL_LEVELS              ! IN No. of atmospheric levels for           KMKH7A.65     
!                                  which boundary layer fluxes are         KMKH7A.66     
!                                  calculated.                             KMKH7A.67     
                                                                           KMKH7A.68     
      REAL                                                                 KMKH7A.69     
     & TIMESTEP               ! IN Timestep in seconds.                    KMKH7A.70     
     &,BQ(P_FIELD,BL_LEVELS)  ! IN A buoyancy parameter (beta q tilde)     KMKH7A.71     
     &,BT(P_FIELD,BL_LEVELS)  ! IN A buoyancy parameter (beta T tilde)     KMKH7A.72     
     &,BF(P_FIELD,BL_LEVELS)  ! IN A buoyancy parameter (beta F tilde)     KMKH7A.73     
     &,P(P_FIELD,BL_LEVELS)   ! IN Pressure at pressure points.            KMKH7A.74     
     &,CCA(P_FIELD)           ! IN Convective Cloud Amount.                KMKH7A.75     
     &,CF(P_FIELD,BL_LEVELS)  ! IN Cloud fractions for boundary levs.      KMKH7A.76     
     &,DZL(P_FIELD,BL_LEVELS) ! IN Layer depths (m).  DZL(,K) is the       KMKH7A.77     
!                                  distance from layer boundary K-1/2      KMKH7A.78     
!                                  to layer boundary K+1/2.  For K=1       KMKH7A.79     
!                                  the lower boundary is the surface.      KMKH7A.80     
     &,DTRDZ(P_FIELD,BL_LEVELS)                                            KMKH7A.81     
!                             ! IN -g.dt/dp for model layers.              KMKH7A.82     
                                                                           KMKH7A.83     
      REAL                    ! Split to keep continuation cards           KMKH7A.84     
     & RDZ(P_FIELD,BL_LEVELS) ! IN Reciprocal of distance between          KMKH7A.85     
!                                  hybrid levels (m-1).  1/RDZ(,K) is      KMKH7A.86     
!                                  the vertical distance from level        KMKH7A.87     
!                                  K-1 to level K, except that for         KMKH7A.88     
!                                  K=1 it is just the height of the        KMKH7A.89     
!                                  lowest atmospheric level.               KMKH7A.90     
     &,RHO(P_FIELD,BL_LEVELS) ! IN Density at theta_levels                 KMKH7A.91     
     &,U(P_FIELD,BL_LEVELS)   ! IN Westerly wind component on P-grid       KMKH7A.92     
!                                  (m per s).                              KMKH7A.93     
     &,V(P_FIELD,BL_LEVELS)   ! IN Southerly wind component on P-grid      KMKH7A.94     
!                                  (m per s).                              KMKH7A.95     
     &,Z0M(P_FIELD)           ! IN Roughness length for momentum (m).      KMKH7A.96     
     &,ZLB(P_FIELD,BL_LEVELS) ! IN ZLB(,K) is height above surface of      KMKH7A.97     
!                                  lower boundary of layer K (m).          KMKH7A.98     
     &,H_BLEND(P_FIELD)       ! IN Blending height for effective           KMKH7A.99     
!                                  roughness scheme passed through         KMKH7A.100    
!                                  EX_COEF                                 KMKH7A.101    
                                                                           KMKH7A.102    
      REAL                    ! Note: for all these INOUT arrays,          KMKH7A.103    
!                                     apart from ZH, level 1 is IN         KMKH7A.104    
!                                     (though not always used), and        KMKH7A.105    
!                                     the other levels are all OUT.        KMKH7A.106    
     & QW(P_FIELD,BL_LEVELS)  ! INOUT Total water content (kg per kg       KMKH7A.107    
!                                        air).                             KMKH7A.108    
     &,QCF(P_FIELD,BL_LEVELS)  ! INOUT Ice water content (kg per kg        KMKH7A.109    
!                                        air).                             KMKH7A.110    
     &,RHOKM(P_FIELD,BL_LEVELS)                                            KMKH7A.111    
!                             ! INOUT Layer K-1 - to - layer K             KMKH7A.112    
!                                     exchange coefficient for             KMKH7A.113    
!                                     momentum .                           KMKH7A.114    
     &,TL(P_FIELD,BL_LEVELS)  ! INOUT Liquid/frozen water temperature      KMKH7A.115    
!                                     (K).                                 KMKH7A.116    
     &,ZH(P_FIELD)            ! INOUT Boundary layer height (m).           KMKH7A.117    
                                                                           KMKH7A.118    
      REAL                                                                 KMKH7A.119    
     & RHO_KM(P_FIELD,2:BL_LEVELS)                                         KMKH7A.120    
!                             ! OUT RHO * KM before interpolation          KMKH7A.121    
!                                   to UV-grid.                            KMKH7A.122    
     &,RHOKH(P_FIELD,BL_LEVELS)                                            KMKH7A.123    
!                             ! OUT Layer K-1 - to - layer K               KMKH7A.124    
!                                   exchange coefficient for FTL,          KMKH7A.125    
!                                   on P-grid.                             KMKH7A.126    
                                                                           KMKH7A.127    
      INTEGER                                                              KMKH7A.128    
     & CCB(P_FIELD)           ! IN  Convective Cloud Base.                 KMKH7A.129    
     &,CCT(P_FIELD)           ! IN  Convective Cloud Top.                  KMKH7A.130    
     &,NRML(P_FIELD)          ! INOUT Number of model layers in the        KMKH7A.131    
!                                     unstable Rapidly Mixing Layer.       KMKH7A.132    
                                                                           KMKH7A.133    
!!----------------------------------------------------------------------   KMKH7A.134    
!    External references :-                                                KMKH7A.135    
      EXTERNAL TIMER, EX_COEF                                              KMKH7A.136    
                                                                           KMKH7A.137    
!!----------------------------------------------------------------------   KMKH7A.138    
!    Local and other symbolic constants :-                                 KMKH7A.139    
*CALL C_LHEAT                                                              KMKH7A.140    
*CALL C_R_CP                                                               KMKH7A.141    
*CALL C_G                                                                  KMKH7A.142    
*CALL C_0_DG_C                                                             KMKH7A.143    
*CALL C_EPSLON                                                             KMKH7A.144    
*CALL C_GAMMA                                                              KMKH7A.145    
                                                                           KMKH7A.146    
      REAL ETAR,GRCP,LCRCP,LFRCP,LS,LSRCP                                  KMKH7A.147    
      PARAMETER (                                                          KMKH7A.148    
     & ETAR=1.0/(1.0-EPSILON)   ! Used in buoyancy parameter BETAC.        KMKH7A.149    
     &,GRCP=G/CP                ! Used in DZTL, FTL calculations.          KMKH7A.150    
     &,LCRCP=LC/CP              ! Latent heat of condensation / CP.        KMKH7A.151    
     &,LFRCP=LF/CP              ! Latent heat of fusion / CP.              KMKH7A.152    
     &,LS=LC+LF                 ! Latent heat of sublimation.              KMKH7A.153    
     &,LSRCP=LS/CP              ! Latent heat of sublimation / CP.         KMKH7A.154    
     &)                                                                    KMKH7A.155    
                                                                           KMKH7A.156    
                                                                           KMKH7A.157    
!  Define local storage.                                                   KMKH7A.158    
                                                                           KMKH7A.159    
!  (a) Workspace. 6*BL_LEVELS-1 blocks of real workspace are required      KMKH7A.160    
!      plus 1 block of logical.                                            KMKH7A.161    
                                                                           KMKH7A.162    
                                                                           KMKH7A.163    
      LOGICAL                                                              KMKH7A.164    
     & TOPBL(P_FIELD)            ! Flag set when top of boundary layer     KMKH7A.165    
!                                ! is reached.                             KMKH7A.166    
      REAL                                                                 KMKH7A.167    
     & FQW(P_FIELD,BL_LEVELS)    ! "Explicit" flux of QW (i.e.             KMKH7A.168    
!                                  evaporation) from layer below,          KMKH7A.169    
!                                  on P-grid (kg per sq m per s).          KMKH7A.170    
     &,FTL(P_FIELD,BL_LEVELS)    ! "Explicit" flux of TL = H/CP            KMKH7A.171    
!                                  (sensible heat/CP) from layer           KMKH7A.172    
!                                   below, on P-grid.                      KMKH7A.173    
     &,RI(P_FIELD,2:BL_LEVELS)   ! Richardson number for lower interface   KMKH7A.174    
!                                  of layer.                               KMKH7A.175    
     &,RIM(P_FIELD,2:BL_LEVELS)  ! Modified Richardson number for lower    KMKH7A.176    
!                                  interface of layer.                     KMKH7A.177    
     &,DTRDZ_RML (P_FIELD)       ! -g.dt/dp for the rapidly mixing layer   KMKH7A.178    
     &,DELTAP (P_FIELD,BL_LEVELS)! -g.dt/dp for the rapidly mixing layer   KMKH7A.179    
     &,DELTAP_RML (P_FIELD)      ! -g.dt/dp for the rapidly mixing layer   KMKH7A.180    
                                                                           KMKH7A.181    
      INTEGER                                                              KMKH7A.182    
     & NTML(P_FIELD)             ! No. of turbulently mixed model levels   KMKH7A.183    
                                                                           KMKH7A.184    
!  (b) Scalars.                                                            KMKH7A.185    
                                                                           KMKH7A.186    
      REAL                                                                 KMKH7A.187    
     & DZB           ! Temporary in calculation of RI(,K).                 KMKH7A.188    
     &,DZQW          ! Difference of QW between "current" and "lower"      KMKH7A.189    
!                       levels.                                            KMKH7A.190    
     &,DZTL          ! Liquid/ice static energy difference between         KMKH7A.191    
!                      adjacent levels.                                    KMKH7A.192    
     &,DZQI          ! Difference of QCF between "current" and "lower"     KMKH7A.193    
!                       levels                                             KMKH7A.194    
     &,DZU           ! Westerly wind shear between adjacent levels.        KMKH7A.195    
     &,DZV           ! Southerly wind shear between adjacent levels.       KMKH7A.196    
     &,DVMOD2        ! Square of modulus of wind shear between adjacent    KMKH7A.197    
!                      levels                                              KMKH7A.198    
     &,DTL_RML_P     ! Explicit TL increment for the rapidly mixing        KMKH7A.199    
!                      layer                                               KMKH7A.200    
     &,DQW_RML_P     ! Explicit QW increment for the rapidly mixing        KMKH7A.201    
!                      layer                                               KMKH7A.202    
     &,DTL_RMLP1_P   ! Explicit TL increment for the model layer above     KMKH7A.203    
!                      the rapidly mixing layer.                           KMKH7A.204    
     &,DQW_RMLP1_P   ! Explicit QW increment for the model layer above     KMKH7A.205    
!                      the rapidly mixing layer.                           KMKH7A.206    
     &,RIT           ! Temporary calculation of the modified Richardson    KMKH7A.207    
!                      number at the interface between the rapidly         KMKH7A.208    
!                      mixing layer and the model layer above it.          KMKH7A.209    
                                                                           KMKH7A.210    
      INTEGER                                                              KMKH7A.211    
     & I             ! Loop counter (horizontal field index).              KMKH7A.212    
     &,K             ! Loop counter (vertical level index).                KMKH7A.213    
     &,MBL           ! Maximum number of model layers allowed in the       KMKH7A.214    
!                      rapidly mixing layer; set to BL_LEVELS-1.           KMKH7A.215    
     &,NRMLP1        ! NRML + 1                                            KMKH7A.216    
     &,NRMLP2        ! NRML + 2                                            KMKH7A.217    
     &,IT_COUNTER    ! Iteration loop counter.                             KMKH7A.218    
                                                                           KMKH7A.219    
!-----------------------------------------------------------------------   KMKH7A.220    
!!  0.  Check that the scalars input to define the grid are consistent.    KMKH7A.221    
!!      See comments to routine SF_EXCH for details.                       KMKH7A.222    
!-----------------------------------------------------------------------   KMKH7A.223    
                                                                           KMKH7A.224    
      IF (LTIMER) THEN                                                     KMKH7A.225    
        CALL TIMER('KMKH    ',3)                                           KMKH7A.226    
      ENDIF                                                                KMKH7A.227    
                                                                           KMKH7A.228    
!  Set MBL, "maximum number of boundary levels" for the purposes of        KMKH7A.229    
!  boundary layer height calculation.                                      KMKH7A.230    
                                                                           KMKH7A.231    
      MBL = BL_LEVELS - 1                                                  KMKH7A.232    
                                                                           KMKH7A.233    
!-----------------------------------------------------------------------   KMKH7A.234    
!! 1 Initialise flag for having reached top of boundary layer              KMKH7A.235    
!!   and also the number of turbulently mixed layers                       KMKH7A.236    
!-----------------------------------------------------------------------   KMKH7A.237    
                                                                           KMKH7A.238    
      DO I=P1,P1+P_POINTS-1                                                KMKH7A.239    
        TOPBL(I) = .FALSE.                                                 KMKH7A.240    
        NTML(I) = 1                                                        KMKH7A.241    
      ENDDO                                                                KMKH7A.242    
                                                                           KMKH7A.243    
      DO K=2,BL_LEVELS                                                     KMKH7A.244    
        DO I=P1,P1+P_POINTS-1                                              KMKH7A.245    
                                                                           KMKH7A.246    
!-----------------------------------------------------------------------   KMKH7A.247    
!! 2.1 Calculate wind shear and other "jumps" between "current" and        KMKH7A.248    
!!     previous (lower) level.                                             KMKH7A.249    
!-----------------------------------------------------------------------   KMKH7A.250    
                                                                           KMKH7A.251    
          DZU = U(I,K) - U(I,K-1)                                          KMKH7A.252    
          DZV = V(I,K) - V(I,K-1)                                          KMKH7A.253    
          DVMOD2 = MAX ( 1.0E-12 , DZU*DZU + DZV*DZV ) ! Used in P243.C1   KMKH7A.254    
          DZTL = TL(I,K) - TL(I,K-1) + GRCP/RDZ(I,K)   ! Used in P243.C2   KMKH7A.255    
          DZQW = QW(I,K) - QW(I,K-1)                   ! Used in P243.C2   KMKH7A.256    
          DZQI = QCF(I,K) - QCF(I,K-1)                                     KMKH7A.257    
                                                                           KMKH7A.258    
          IF (L_BL_LSPICE) THEN                                            KMKH7A.259    
            DZB =  BT(I,K-1)*DZTL + BQ(I,K-1)*DZQW - BF(I,K-1)*DZQI        KMKH7A.260    
          ELSE                                                             KMKH7A.261    
            DZB =  BT(I,K-1)*DZTL + BQ(I,K-1)*DZQW                         KMKH7A.262    
          ENDIF                                                            KMKH7A.263    
                                                                           KMKH7A.264    
!-----------------------------------------------------------------------   KMKH7A.265    
!! 2.2 Calculate Richardson number Ri at the same interface.               KMKH7A.266    
!-----------------------------------------------------------------------   KMKH7A.267    
                                                                           KMKH7A.268    
          RI(I,K) = G * DZB / ( RDZ(I,K) * DVMOD2 )                        KMKH7A.269    
                                                                           KMKH7A.270    
!-----------------------------------------------------------------------   KMKH7A.271    
!! 2.3 If either a stable layer (Ri > 1) or the maximum boundary layer     KMKH7A.272    
!!     height has been reached, set boundary layer height (ZH) to          KMKH7A.273    
!!     the height of the lower boundary of the current layer and set       KMKH7A.274    
!!     the number of rapidly mixing layers if the surface layer is         KMKH7A.275    
!!     unstable (as determined in subroutine SF_EXCH).                     KMKH7A.276    
!-----------------------------------------------------------------------   KMKH7A.277    
                                                                           KMKH7A.278    
          IF ( .NOT.TOPBL(I) .AND. (RI(I,K).GT.1.0 .OR. K.GT.MBL) ) THEN   KMKH7A.279    
            TOPBL(I) = .TRUE.                                              KMKH7A.280    
            ZH(I) = ZLB(I,K)                                               KMKH7A.281    
            NTML(I) = K-1                                                  KMKH7A.282    
            IF ( NRML(I) .GT. 0 ) NRML(I) = K-1                            KMKH7A.283    
          ENDIF                                                            KMKH7A.284    
        ENDDO ! BL_LEVELS                                                  KMKH7A.285    
      ENDDO ! P_POINTS                                                     KMKH7A.286    
                                                                           KMKH7A.287    
!-----------------------------------------------------------------------   KMKH7A.288    
!! 3.  Subroutine EX_COEF.                                                 KMKH7A.289    
!-----------------------------------------------------------------------   KMKH7A.290    
                                                                           KMKH7A.291    
      CALL EX_COEF (                                                       KMKH7A.292    
     & P_FIELD,P1,P_POINTS,BL_LEVELS,                                      KMKH7A.293    
     & CCB,CCT,NTML,L_MOM,                                                 KMKH7A.294    
     & CCA,DZL,RDZ,RI,U,V,                                                 KMKH7A.295    
     & RHO,ZH,ZLB,Z0M,H_BLEND,                                             KMKH7A.296    
     & RHOKM,RHOKH,LTIMER                                                  KMKH7A.297    
     &)                                                                    KMKH7A.298    
                                                                           KMKH7A.299    
!-----------------------------------------------------------------------   KMKH7A.300    
!! 4.  Calculate "explicit" fluxes of TL and QW.                           KMKH7A.301    
!-----------------------------------------------------------------------   KMKH7A.302    
                                                                           KMKH7A.303    
      DO K=2,BL_LEVELS                                                     KMKH7A.304    
!-----------------------------------------------------------------------   KMKH7A.305    
!! 4.1 "Explicit" fluxes of TL and QW, on P-grid.                          KMKH7A.306    
!-----------------------------------------------------------------------   KMKH7A.307    
        DO I=P1,P1+P_POINTS-1                                              KMKH7A.308    
          FTL(I,K) = -RHOKH(I,K) *                                         KMKH7A.309    
     &     ( ( ( TL(I,K) - TL(I,K-1) ) * RDZ(I,K) ) + GRCP )               KMKH7A.310    
!          1 2 3                     3            2        1               KMKH7A.311    
                                                                           KMKH7A.312    
          FQW(I,K)=-RHOKH(I,K) * ( QW(I,K) - QW(I,K-1) )*RDZ(I,K)          KMKH7A.313    
                                                                           KMKH7A.314    
        ENDDO ! P_POINTS                                                   KMKH7A.315    
      ENDDO ! BL_LEVELS                                                    KMKH7A.316    
                                                                           KMKH7A.317    
!-----------------------------------------------------------------------   KMKH7A.318    
!! 4.2 Use explicit fluxes to calculate a modified Richardson number       KMKH7A.319    
!!     at the top of the rapidly mixing layer (if it exists); if this      KMKH7A.320    
!!     indicates that the r.m.l. can deepen due to heat and/or moisture    KMKH7A.321    
!!     input from the surface then increase NRML(I).                       KMKH7A.322    
!-----------------------------------------------------------------------   KMKH7A.323    
                                                                           KMKH7A.324    
! Initialise height difference dz, for the rapidly mixing boundary layer   KMKH7A.325    
                                                                           KMKH7A.326    
      DO I = P1,P1+P_POINTS-1                                              KMKH7A.327    
          DELTAP_RML(I) = 0.0                                              KMKH7A.328    
      ENDDO ! loop over P_POINTS.                                          KMKH7A.329    
                                                                           KMKH7A.330    
! Calculate pressure differences, dp, and -g.dt/dp for model layers        KMKH7A.331    
! and dp for the rapidly mixing layer.                                     KMKH7A.332    
                                                                           KMKH7A.333    
      DO K = 1,BL_LEVELS                                                   KMKH7A.334    
        DO I = P1,P1+P_POINTS-1                                            KMKH7A.335    
          DELTAP(I,K) = -G * TIMESTEP/DTRDZ(I,K)                           KMKH7A.336    
          IF (K .LE. NRML(I) )                                             KMKH7A.337    
     &        DELTAP_RML(I) = DELTAP_RML(I) + DELTAP(I,K)                  KMKH7A.338    
          IF (K .GE. 2) RIM(I,K) = RI(I,K)                                 KMKH7A.339    
        ENDDO ! Loop over p-points                                         KMKH7A.340    
      ENDDO ! Loop over bl-levels                                          KMKH7A.341    
                                                                           KMKH7A.342    
!-----------------------------------------------------------------------   KMKH7A.343    
!! 4.2.1 Iterate BL_LEVELS-2 times; this allows an initial r.m.l. of       KMKH7A.344    
!!       1 model layer to deepen to BL_LEVELS-1 model layers a layer at    KMKH7A.345    
!!       a time in each iteration.  Some of these iterations may be        KMKH7A.346    
!!       redundant if in fact the r.m.l. cannot deepen.                    KMKH7A.347    
!-----------------------------------------------------------------------   KMKH7A.348    
      DO IT_COUNTER = 1,BL_LEVELS-2                                        KMKH7A.349    
                                                                           KMKH7A.350    
        DO I = P1,P1+P_POINTS-1                                            KMKH7A.351    
!         !-------------------------------------------------------------   KMKH7A.352    
!         ! Only check to see if the r.m.l. can deepen if it exists        KMKH7A.353    
!         ! in the first place and has less than its maximum depth.        KMKH7A.354    
!         !-------------------------------------------------------------   KMKH7A.355    
          IF ((NRML(I) .GE. 1) .AND. (NRML(I) .LE. BL_LEVELS-2)) THEN      KMKH7A.356    
            NRMLP1 = NRML(I) + 1                                           KMKH7A.357    
            NRMLP2 = NRML(I) + 2                                           KMKH7A.358    
            DTRDZ_RML(I) =-G * TIMESTEP / DELTAP_RML(I)                    KMKH7A.359    
!           !-----------------------------------------------------------   KMKH7A.360    
!           ! "Explicit" rapidly mixing layer increments to TL and QW.     KMKH7A.361    
!           !-----------------------------------------------------------   KMKH7A.362    
            DTL_RML_P = -DTRDZ_RML(I) * ( FTL(I,NRMLP1) - FTL(I,1) )       KMKH7A.363    
            DQW_RML_P = -DTRDZ_RML(I) * ( FQW(I,NRMLP1) - FQW(I,1) )       KMKH7A.364    
!           !-----------------------------------------------------------   KMKH7A.365    
!           ! "Explicit" increments to TL and QW in the model layer        KMKH7A.366    
!           ! above the rapidly mixing layer.                              KMKH7A.367    
!           !-----------------------------------------------------------   KMKH7A.368    
            DTL_RMLP1_P = -DTRDZ(I,NRMLP1) *                               KMKH7A.369    
     &                           ( FTL(I,NRMLP2) - FTL(I,NRMLP1) )         KMKH7A.370    
            DQW_RMLP1_P = -DTRDZ(I,NRMLP1) *                               KMKH7A.371    
     &                           ( FQW(I,NRMLP2) - FQW(I,NRMLP1) )         KMKH7A.372    
            DZU = U(I,NRMLP1) - U(I,NRML(I))                               KMKH7A.373    
            DZV = V(I,NRMLP1) - V(I,NRML(I))                               KMKH7A.374    
            DVMOD2 = MAX (1.0E-12 , DZU*DZU + DZV*DZV)                     KMKH7A.375    
!           !-----------------------------------------------------------   KMKH7A.376    
!           ! Calculate a modified Richardson number for the interface     KMKH7A.377    
!           ! between the rapidly mixing layer and the model layer above   KMKH7A.378    
!           !-----------------------------------------------------------   KMKH7A.379    
              RIT = RI(I,NRMLP1) + ( G/(RDZ(I,NRMLP1) * DVMOD2) ) *        KMKH7A.380    
     &              ( BT(I,NRML(I)) * ( DTL_RMLP1_P - DTL_RML_P)           KMKH7A.381    
     &              + BQ(I,NRML(I)) * ( DQW_RMLP1_P - DQW_RML_P ) )        KMKH7A.382    
            IF ( RIT .LE. 1.0 ) THEN                                       KMKH7A.383    
!             !---------------------------------------------------------   KMKH7A.384    
!             ! Deepen the rapidly mixing layer by one model layer         KMKH7A.385    
!             !---------------------------------------------------------   KMKH7A.386    
              NRML(I) = NRMLP1                                             KMKH7A.387    
              DELTAP_RML(I) = DELTAP_RML(I) + DELTAP(I,NRMLP1)             KMKH7A.388    
              ZH(I) = ZLB(I,NRMLP2)                                        KMKH7A.389    
              IF (RIT .LT. RI(I,NRMLP1)) RIM(I,NRMLP1) = RIT               KMKH7A.390    
            ENDIF                                                          KMKH7A.391    
          ENDIF ! NRML(I) .GE. 1 and .LE. BL_LEVELS-2                      KMKH7A.392    
        ENDDO ! Loop over p-points                                         KMKH7A.393    
      ENDDO! Loop over iterations                                          KMKH7A.394    
                                                                           KMKH7A.395    
                                                                           KMKH7A.396    
!-----------------------------------------------------------------------   KMKH7A.397    
!! 4.2.2 Adjust the Richardson number at the top of the rapidly mixing     KMKH7A.398    
!!       layer- the 'DZ' in the expression (P243.C1) is set to 100.0 m     KMKH7A.399    
!!       rather than the model level separation (if the latter is          KMKH7A.400    
!!       greater than 100 m).  This is a simple way of adjusting for       KMKH7A.401    
!!       inaccuracies in calculating gradients at inversions when the      KMKH7A.402    
!!       model's vertical resolution is coarse.                            KMKH7A.403    
!-----------------------------------------------------------------------   KMKH7A.404    
                                                                           KMKH7A.405    
      DO K = 2,BL_LEVELS                                                   KMKH7A.406    
        DO I = P1,P1+P_POINTS-1                                            KMKH7A.407    
          IF ( (K-1 .EQ. NRML(I)) .AND. (100.0*RDZ(I,K) .LT. 1.0) )        KMKH7A.408    
     &       RIM(I,K) = 100.0*RDZ(I,K)*RIM(I,K)                            KMKH7A.409    
        ENDDO ! Loop over p-points                                         KMKH7A.410    
      ENDDO ! Loop over bl-levels                                          KMKH7A.411    
                                                                           KMKH7A.412    
!-----------------------------------------------------------------------   KMKH7A.413    
!! 5.  Subroutine EX_COEF using adjusted Richardson Number, RIM            KMKH7A.414    
!-----------------------------------------------------------------------   KMKH7A.415    
                                                                           KMKH7A.416    
      CALL EX_COEF (                                                       KMKH7A.417    
     & P_FIELD,P1,P_POINTS,BL_LEVELS,                                      KMKH7A.418    
     & CCB,CCT,NTML,L_MOM,                                                 KMKH7A.419    
     & CCA,DZL,RDZ,RIM,U,V,                                                KMKH7A.420    
     & RHO,ZH,ZLB,Z0M,H_BLEND,                                             KMKH7A.421    
     & RHOKM,RHOKH,LTIMER                                                  KMKH7A.422    
     &)                                                                    KMKH7A.423    
                                                                           KMKH7A.424    
!-----------------------------------------------------------------------   KMKH7A.425    
!! 5.1 store RHO_KM on P-grid for otput.                                   KMKH7A.426    
!-----------------------------------------------------------------------   KMKH7A.427    
      DO K=2,BL_LEVELS                                                     KMKH7A.428    
        DO I=P1,P1+P_POINTS-1                                              KMKH7A.429    
          RHO_KM(I,K) = RHOKM(I,K)                                         KMKH7A.430    
        ENDDO ! P_POINTS                                                   KMKH7A.431    
      ENDDO ! BL_LEVELS                                                    KMKH7A.432    
                                                                           KMKH7A.433    
      IF (LTIMER) THEN                                                     KMKH7A.434    
        CALL TIMER('KMKH    ',4)                                           KMKH7A.435    
      ENDIF                                                                KMKH7A.436    
                                                                           KMKH7A.437    
      RETURN                                                               KMKH7A.438    
      END                                                                  KMKH7A.439    
*ENDIF                                                                     KMKH7A.440