Skip to content
Snippets Groups Projects
zdfosm.F90 212 KiB
Newer Older
Guillaume Samson's avatar
Guillaume Samson committed
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
            DO jj = Njs0, Nje0
               DO ji = Nis0, Nie0
                  ghamu(ji,jj,jk) = ( ghamu(ji,jj,jk) + ghamu(ji+1,jj,jk) ) /   &
                     &              MAX( 1.0_wp, tmask(ji,jj,jk) + tmask (ji+1,jj,jk) ) * umask(ji,jj,jk)
                  ghamv(ji,jj,jk) = ( ghamv(ji,jj,jk) + ghamv(ji,jj+1,jk) ) /   &
                     &              MAX( 1.0_wp, tmask(ji,jj,jk) + tmask (ji,jj+1,jk) ) * vmask(ji,jj,jk)
                  ghamt(ji,jj,jk) = ghamt(ji,jj,jk) * tmask(ji,jj,jk)
                  ghams(ji,jj,jk) = ghams(ji,jj,jk) * tmask(ji,jj,jk)
               END DO
            END DO
         END DO
         ! Lateral boundary conditions on final outputs for hbl, on T-grid (sign unchanged)
         CALL lbc_lnk( 'zdfosm', hbl,  'T', 1.0_wp,   &
            &                    hmle, 'T', 1.0_wp )
         !
         CALL zdf_osm_iomput( "ghamt", tmask * ghamt       )   ! <Tw_NL>
         CALL zdf_osm_iomput( "ghams", tmask * ghams       )   ! <Sw_NL>
         CALL zdf_osm_iomput( "ghamu", umask * ghamu       )   ! <uw_NL>
         CALL zdf_osm_iomput( "ghamv", vmask * ghamv       )   ! <vw_NL>
         CALL zdf_osm_iomput( "hbl",   tmask(:,:,1) * hbl  )   ! Boundary-layer depth
         CALL zdf_osm_iomput( "hmle",  tmask(:,:,1) * hmle )   ! FK layer depth
      END IF
      !
   END SUBROUTINE zdf_osm

   SUBROUTINE zdf_osm_vertical_average( Kbb, Kmm, knlev, pt, ps,   &
      &                                 pb, pu, pv, kp_ext, pdt,   &
      &                                 pds, pdb, pdu, pdv )
      !!---------------------------------------------------------------------
      !!                ***  ROUTINE zdf_vertical_average  ***
      !!
      !! ** Purpose : Determines vertical averages from surface to knlev,
      !!              and optionally the differences between these vertical
      !!              averages and values at an external level
      !!
      !! ** Method  : Averages are calculated from the surface to knlev.
      !!              The external level used to calculate differences is
      !!              knlev+kp_ext
      !!----------------------------------------------------------------------
      INTEGER,                            INTENT(in   )           ::   Kbb, Kmm   ! Ocean time-level indices
      INTEGER,  DIMENSION(A2D(nn_hls-1)), INTENT(in   )           ::   knlev      ! Number of levels to average over.
      REAL(wp), DIMENSION(jpi,jpj),       INTENT(  out)           ::   pt, ps     ! Average temperature and salinity
      REAL(wp), DIMENSION(jpi,jpj),       INTENT(  out)           ::   pb         ! Average buoyancy
      REAL(wp), DIMENSION(jpi,jpj),       INTENT(  out)           ::   pu, pv     ! Average current components
      INTEGER,  DIMENSION(A2D(nn_hls-1)), INTENT(in   ), OPTIONAL ::   kp_ext     ! External-level offsets
      REAL(wp), DIMENSION(jpi,jpj),       INTENT(  out), OPTIONAL ::   pdt        ! Difference between average temperature,
      REAL(wp), DIMENSION(jpi,jpj),       INTENT(  out), OPTIONAL ::   pds        !    salinity,
      REAL(wp), DIMENSION(jpi,jpj),       INTENT(  out), OPTIONAL ::   pdb        !    buoyancy, and
      REAL(wp), DIMENSION(jpi,jpj),       INTENT(  out), OPTIONAL ::   pdu, pdv   !    velocity components and the OSBL
      !!
      INTEGER                              ::   jk, jkflt, jkmax, ji, jj   ! Loop indices
      INTEGER                              ::   ibld_ext                   ! External-layer index
      REAL(wp), DIMENSION(A2D(nn_hls-1)) ::   zthick                     ! Layer thickness
      REAL(wp)                             ::   zthermal                   ! Thermal expansion coefficient
      REAL(wp)                             ::   zbeta                      ! Haline contraction coefficient
      !!----------------------------------------------------------------------
      !
      ! Averages over depth of boundary layer
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         pt(ji,jj) = 0.0_wp
         ps(ji,jj) = 0.0_wp
         pu(ji,jj) = 0.0_wp
         pv(ji,jj) = 0.0_wp
      END_2D
      zthick(:,:) = epsln
      jkflt = jpk
      jkmax = 0
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         IF ( knlev(ji,jj) < jkflt ) jkflt = knlev(ji,jj)
         IF ( knlev(ji,jj) > jkmax ) jkmax = knlev(ji,jj)
      END_2D
      DO_3D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1, 2, jkflt )   ! Upper, flat part of layer
         zthick(ji,jj) = zthick(ji,jj) + e3t(ji,jj,jk,Kmm)
         pt(ji,jj)     = pt(ji,jj)     + e3t(ji,jj,jk,Kmm) * ts(ji,jj,jk,jp_tem,Kmm)
         ps(ji,jj)     = ps(ji,jj)     + e3t(ji,jj,jk,Kmm) * ts(ji,jj,jk,jp_sal,Kmm)
         pu(ji,jj)     = pu(ji,jj)     + e3t(ji,jj,jk,Kmm) *                                        &
            &                               ( uu(ji,jj,jk,Kbb) + uu(ji - 1,jj,jk,Kbb) ) /           &
            &                               MAX( 1.0_wp , umask(ji,jj,jk) + umask(ji - 1,jj,jk) )
         pv(ji,jj)     = pv(ji,jj)     + e3t(ji,jj,jk,Kmm) *                                        &
            &                               ( vv(ji,jj,jk,Kbb) + vv(ji,jj - 1,jk,Kbb) ) /           &
            &                               MAX( 1.0_wp , vmask(ji,jj,jk) + vmask(ji,jj - 1,jk) )         
      END_3D
      DO_3D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1, jkflt+1, jkmax )   ! Lower, non-flat part of layer
         IF ( knlev(ji,jj) >= jk ) THEN
            zthick(ji,jj) = zthick(ji,jj) + e3t(ji,jj,jk,Kmm)
            pt(ji,jj)     = pt(ji,jj)     + e3t(ji,jj,jk,Kmm) * ts(ji,jj,jk,jp_tem,Kmm)
            ps(ji,jj)     = ps(ji,jj)     + e3t(ji,jj,jk,Kmm) * ts(ji,jj,jk,jp_sal,Kmm)
            pu(ji,jj)     = pu(ji,jj)     + e3t(ji,jj,jk,Kmm) *                                        &
               &                               ( uu(ji,jj,jk,Kbb) + uu(ji - 1,jj,jk,Kbb) ) /           &
               &                               MAX( 1.0_wp , umask(ji,jj,jk) + umask(ji - 1,jj,jk) )
            pv(ji,jj)     = pv(ji,jj)     + e3t(ji,jj,jk,Kmm) *                                        &
               &                               ( vv(ji,jj,jk,Kbb) + vv(ji,jj - 1,jk,Kbb) ) /           &
               &                               MAX( 1.0_wp , vmask(ji,jj,jk) + vmask(ji,jj - 1,jk) )
         END IF
      END_3D
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         pt(ji,jj) = pt(ji,jj) / zthick(ji,jj)
         ps(ji,jj) = ps(ji,jj) / zthick(ji,jj)
         pu(ji,jj) = pu(ji,jj) / zthick(ji,jj)
         pv(ji,jj) = pv(ji,jj) / zthick(ji,jj)
         zthermal  = rab_n(ji,jj,1,jp_tem)   ! ideally use nbld not 1??
         zbeta     = rab_n(ji,jj,1,jp_sal)
         pb(ji,jj) = grav * zthermal * pt(ji,jj) - grav * zbeta * ps(ji,jj)
      END_2D
      !
      ! Differences between vertical averages and values at an external layer
      IF ( PRESENT( kp_ext ) ) THEN
         DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
            ibld_ext = knlev(ji,jj) + kp_ext(ji,jj)
            IF ( ibld_ext <= mbkt(ji,jj)-1 ) THEN   ! ag 09/03
               ! Two external levels are available
               pdt(ji,jj) = pt(ji,jj) - ts(ji,jj,ibld_ext,jp_tem,Kmm)
               pds(ji,jj) = ps(ji,jj) - ts(ji,jj,ibld_ext,jp_sal,Kmm)
               pdu(ji,jj) = pu(ji,jj) - ( uu(ji,jj,ibld_ext,Kbb) + uu(ji-1,jj,ibld_ext,Kbb ) ) /              &
                  &                        MAX(1.0_wp , umask(ji,jj,ibld_ext ) + umask(ji-1,jj,ibld_ext ) )
               pdv(ji,jj) = pv(ji,jj) - ( vv(ji,jj,ibld_ext,Kbb) + vv(ji,jj-1,ibld_ext,Kbb ) ) /              &
                  &                        MAX(1.0_wp , vmask(ji,jj,ibld_ext ) + vmask(ji,jj-1,ibld_ext ) )
               zthermal   = rab_n(ji,jj,1,jp_tem)   ! ideally use nbld not 1??
               zbeta      = rab_n(ji,jj,1,jp_sal)
               pdb(ji,jj) = grav * zthermal * pdt(ji,jj) - grav * zbeta * pds(ji,jj)
            ELSE
               pdt(ji,jj) = 0.0_wp
               pds(ji,jj) = 0.0_wp
               pdu(ji,jj) = 0.0_wp
               pdv(ji,jj) = 0.0_wp
               pdb(ji,jj) = 0.0_wp
            ENDIF
         END_2D
      END IF
      !
   END SUBROUTINE zdf_osm_vertical_average

   SUBROUTINE zdf_osm_velocity_rotation_2d( pu, pv, fwd )
      !!---------------------------------------------------------------------
      !!            ***  ROUTINE zdf_velocity_rotation_2d  ***
      !!
      !! ** Purpose : Rotates frame of reference of velocity components pu and
      !!              pv (2d)
      !!
      !! ** Method : The velocity components are rotated into (fwd=.TRUE.) or
      !!             from (fwd=.FALSE.) the frame specified by scos_wind and
      !!             ssin_wind
      !!
      !!----------------------------------------------------------------------      
      REAL(wp),           INTENT(inout), DIMENSION(jpi,jpj) ::   pu, pv   ! Components of current
      LOGICAL,  OPTIONAL, INTENT(in   )                     ::   fwd      ! Forward (default) or reverse rotation
      !!
      INTEGER  ::   ji, jj       ! Loop indices
      REAL(wp) ::   ztmp, zfwd   ! Auxiliary variables
      !!----------------------------------------------------------------------      
      !
      zfwd = 1.0_wp
      IF( PRESENT(fwd) .AND. ( .NOT. fwd ) ) zfwd = -1.0_wp
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         ztmp      = pu(ji,jj)
         pu(ji,jj) = pu(ji,jj) * scos_wind(ji,jj) + zfwd * pv(ji,jj) * ssin_wind(ji,jj)
         pv(ji,jj) = pv(ji,jj) * scos_wind(ji,jj) - zfwd * ztmp      * ssin_wind(ji,jj)
      END_2D
      !
   END SUBROUTINE zdf_osm_velocity_rotation_2d

   SUBROUTINE zdf_osm_velocity_rotation_3d( pu, pv, fwd, ktop, knlev )
      !!---------------------------------------------------------------------
      !!            ***  ROUTINE zdf_velocity_rotation_3d  ***
      !!
      !! ** Purpose : Rotates frame of reference of velocity components pu and
      !!              pv (3d)
      !!
      !! ** Method : The velocity components are rotated into (fwd=.TRUE.) or
      !!             from (fwd=.FALSE.) the frame specified by scos_wind and
      !!             ssin_wind; optionally, the rotation can be restricted at
      !!             each water column to span from the a minimum index ktop to
      !!             the depth index specified in array knlev
      !!
      !!----------------------------------------------------------------------      
      REAL(wp),           INTENT(inout), DIMENSION(jpi,jpj,jpk)   ::   pu, pv   ! Components of current
      LOGICAL,  OPTIONAL, INTENT(in   )                           ::   fwd      ! Forward (default) or reverse rotation
      INTEGER,  OPTIONAL, INTENT(in   )                           ::   ktop     ! Minimum depth index
      INTEGER,  OPTIONAL, INTENT(in   ), DIMENSION(A2D(nn_hls-1)) ::   knlev    ! Array of maximum depth indices
      !!
      INTEGER  ::   ji, jj, jk, jktop, jkmax   ! Loop indices
      REAL(wp) ::   ztmp, zfwd                 ! Auxiliary variables
      LOGICAL  ::   llkbot                     ! Auxiliary variable
      !!----------------------------------------------------------------------      
      !
      zfwd = 1.0_wp
      IF( PRESENT(fwd) .AND. ( .NOT. fwd ) ) zfwd = -1.0_wp
      jktop = 1
      IF( PRESENT(ktop) ) jktop = ktop
      IF( PRESENT(knlev) ) THEN
         jkmax = 0
         DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
            IF ( knlev(ji,jj) > jkmax ) jkmax = knlev(ji,jj)
         END_2D
         llkbot = .FALSE.
      ELSE
         jkmax = jpk
         llkbot = .TRUE.
      END IF
      DO_3D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1, jktop, jkmax )
         IF ( llkbot .OR. knlev(ji,jj) >= jk ) THEN
            ztmp         = pu(ji,jj,jk)
            pu(ji,jj,jk) = pu(ji,jj,jk) * scos_wind(ji,jj) + zfwd * pv(ji,jj,jk) * ssin_wind(ji,jj)
            pv(ji,jj,jk) = pv(ji,jj,jk) * scos_wind(ji,jj) - zfwd * ztmp         * ssin_wind(ji,jj)
         END IF
      END_3D
      !
   END SUBROUTINE zdf_osm_velocity_rotation_3d

   SUBROUTINE zdf_osm_osbl_state( Kmm, pwb_ent, pwb_min, pshear, phbl,   &
      &                           phml, pdh )
      !!---------------------------------------------------------------------
      !!                 ***  ROUTINE zdf_osm_osbl_state  ***
      !!
      !! ** Purpose : Determines the state of the OSBL, stable/unstable,
      !!              shear/ noshear. Also determines shear production,
      !!              entrainment buoyancy flux and interfacial Richardson
      !!              number
      !!
      !! ** Method  :
      !!
      !!----------------------------------------------------------------------
      INTEGER,                            INTENT(in   ) ::   Kmm       ! Ocean time-level index
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(  out) ::   pwb_ent   ! Buoyancy fluxes at base
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(  out) ::   pwb_min   !    of well-mixed layer
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(  out) ::   pshear    ! Production of TKE due to shear across the pycnocline
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   phbl      ! BL depth
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   phml      ! ML depth
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   pdh       ! Pycnocline depth
      !!
      INTEGER :: jj, ji   ! Loop indices
      !!
      REAL(wp), DIMENSION(A2D(nn_hls-1)) ::   zekman
      REAL(wp), DIMENSION(A2D(nn_hls-1)) ::   zri_p, zri_b   ! Richardson numbers
      REAL(wp)                           ::   zshear_u, zshear_v, zwb_shr
      REAL(wp)                           ::   zwcor, zrf_conv, zrf_shear, zrf_langmuir, zr_stokes
      !!
      REAL(wp), PARAMETER ::   pp_a_shr         = 0.4_wp,  pp_b_shr    = 6.5_wp,  pp_a_wb_s = 0.8_wp
      REAL(wp), PARAMETER ::   pp_alpha_c       = 0.2_wp,  pp_alpha_lc = 0.03_wp
      REAL(wp), PARAMETER ::   pp_alpha_ls      = 0.06_wp, pp_alpha_s  = 0.15_wp
      REAL(wp), PARAMETER ::   pp_ri_p_thresh   = 27.0_wp
      REAL(wp), PARAMETER ::   pp_ri_c          = 0.25_wp
      REAL(wp), PARAMETER ::   pp_ek            = 4.0_wp
      REAL(wp), PARAMETER ::   pp_large         = -1e10_wp
      !!----------------------------------------------------------------------
      !
      ! Initialise arrays
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         l_conv(ji,jj)  = .FALSE.
         l_shear(ji,jj) = .FALSE.
         n_ddh(ji,jj)   = 1
      END_2D
      ! Initialise INTENT(  out) arrays
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         pwb_ent(ji,jj) = pp_large
         pwb_min(ji,jj) = pp_large
      END_2D
      !
      ! Determins stability and set flag l_conv
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         IF ( shol(ji,jj) < 0.0_wp ) THEN
            l_conv(ji,jj) = .TRUE.
         ELSE
            l_conv(ji,jj) = .FALSE.
         ENDIF
      END_2D
      !
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         pshear(ji,jj) = 0.0_wp
      END_2D
      zekman(:,:) = EXP( -1.0_wp * pp_ek * ABS( ff_t(A2D(nn_hls-1)) ) * phbl(A2D(nn_hls-1)) /   &
         &               MAX( sustar(A2D(nn_hls-1)), 1.e-8 ) )
      !
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         IF ( l_conv(ji,jj) ) THEN
            IF ( av_db_bl(ji,jj) > 0.0_wp ) THEN
               zri_p(ji,jj) = MAX (  SQRT( av_db_bl(ji,jj) * pdh(ji,jj) / MAX( av_du_bl(ji,jj)**2 + av_dv_bl(ji,jj)**2,     &
                  &                                                          1e-8_wp ) ) * ( phbl(ji,jj) / pdh(ji,jj) ) *   &
                  &                  ( svstr(ji,jj) / MAX( sustar(ji,jj), 1e-6_wp ) )**2 /                                  &
                  &                  MAX( zekman(ji,jj), 1.0e-6_wp ), 5.0_wp )
               IF ( ff_t(ji,jj) >= 0.0_wp ) THEN   ! Northern hemisphere
                  zri_b(ji,jj) = av_db_ml(ji,jj) * pdh(ji,jj) / ( MAX( av_du_ml(ji,jj), 1e-5_wp )**2 +   &
                     &                                          MAX( -1.0_wp * av_dv_ml(ji,jj), 1e-5_wp)**2 )
               ELSE                                ! Southern hemisphere
                  zri_b(ji,jj) = av_db_ml(ji,jj) * pdh(ji,jj) / ( MAX( av_du_ml(ji,jj), 1e-5_wp )**2 +   &
                     &                                          MAX(           av_dv_ml(ji,jj), 1e-5_wp)**2 )
               END IF
               pshear(ji,jj) = pp_a_shr * zekman(ji,jj) *                                                   &
                  &            ( MAX( sustar(ji,jj)**2 * av_du_ml(ji,jj) / phbl(ji,jj), 0.0_wp ) +          &
                  &              pp_b_shr * MAX( -1.0_wp * ff_t(ji,jj) * sustke(ji,jj) * dstokes(ji,jj) *   &
                  &                            av_dv_ml(ji,jj) / phbl(ji,jj), 0.0_wp ) )
               ! Stability dependence
               pshear(ji,jj) = pshear(ji,jj) * EXP( -0.75_wp * MAX( 0.0_wp, ( zri_b(ji,jj) - pp_ri_c ) / pp_ri_c ) )
               !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
               ! Test ensures n_ddh=0 is not selected. Change to zri_p<27 when  !
               ! full code available                                          !
               !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
               IF ( pshear(ji,jj) > 1e-10 ) THEN
                  IF ( zri_p(ji,jj) < pp_ri_p_thresh .AND.   &
                     & MIN( hu(ji,jj,Kmm), hu(ji-1,jj,Kmm), hv(ji,jj,Kmm), hv(ji,jj-1,Kmm) ) > 100.0_wp ) THEN
                     ! Growing shear layer
                     n_ddh(ji,jj) = 0
                     l_shear(ji,jj) = .TRUE.
                  ELSE
                     n_ddh(ji,jj) = 1
                     !             IF ( zri_b <= 1.5 .and. pshear(ji,jj) > 0._wp ) THEN
                     ! Shear production large enough to determine layer charcteristics, but can't maintain a shear layer
                     l_shear(ji,jj) = .TRUE.
                     !             ELSE
                  END IF
               ELSE
                  n_ddh(ji,jj) = 2
                  l_shear(ji,jj) = .FALSE.
               END IF
               ! Shear production may not be zero, but is small and doesn't determine characteristics of pycnocline
               !               pshear(ji,jj) = 0.5 * pshear(ji,jj)
               !               l_shear(ji,jj) = .FALSE.
               !            ENDIF
            ELSE   ! av_db_bl test, note pshear set to zero
               n_ddh(ji,jj) = 2
               l_shear(ji,jj) = .FALSE.
            ENDIF
         ENDIF
      END_2D
      !
      ! Calculate entrainment buoyancy flux due to surface fluxes.
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         IF ( l_conv(ji,jj) ) THEN
            zwcor        = ABS( ff_t(ji,jj) ) * phbl(ji,jj) + epsln
            zrf_conv     = TANH( ( swstrc(ji,jj) / zwcor )**0.69_wp )
            zrf_shear    = TANH( ( sustar(ji,jj) / zwcor )**0.69_wp )
            zrf_langmuir = TANH( ( swstrl(ji,jj) / zwcor )**0.69_wp )
            IF ( nn_osm_SD_reduce > 0 ) THEN
               ! Effective Stokes drift already reduced from surface value
               zr_stokes = 1.0_wp
            ELSE
               ! Effective Stokes drift only reduced by factor rn_zdfodm_adjust_sd,
               ! requires further reduction where BL is deep
               zr_stokes = 1.0 - EXP( -25.0_wp * dstokes(ji,jj) / hbl(ji,jj) * ( 1.0_wp + 4.0_wp * dstokes(ji,jj) / hbl(ji,jj) ) )
            END IF
            pwb_ent(ji,jj) = -2.0_wp * pp_alpha_c * zrf_conv * swbav(ji,jj) -                                          &
               &             pp_alpha_s * zrf_shear * sustar(ji,jj)**3 / phml(ji,jj) +                                 &
               &             zr_stokes * ( pp_alpha_s * EXP( -1.5_wp * sla(ji,jj) ) * zrf_shear * sustar(ji,jj)**3 -   &
               &                           zrf_langmuir * pp_alpha_lc * swstrl(ji,jj)**3 ) / phml(ji,jj)
         ENDIF
      END_2D
      !
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         IF ( l_shear(ji,jj) ) THEN
            IF ( l_conv(ji,jj) ) THEN
               ! Unstable OSBL
               zwb_shr = -1.0_wp * pp_a_wb_s * zri_b(ji,jj) * pshear(ji,jj)
               IF ( n_ddh(ji,jj) == 0 ) THEN
                  ! Developing shear layer, additional shear production possible.

                  !    pshear_u = MAX( zustar(ji,jj)**2 * MAX( av_du_ml(ji,jj), 0._wp ) /  phbl(ji,jj), 0._wp )
                  !    pshear(ji,jj) = pshear(ji,jj) + pshear_u * ( 1.0 - MIN( zri_p(ji,jj) / pp_ri_p_thresh, 1.d0 )**2 )
                  !    pshear(ji,jj) = MIN( pshear(ji,jj), pshear_u )

                  !    zwb_shr = zwb_shr - 0.25 * MAX ( pshear_u, 0._wp) * ( 1.0 - MIN( zri_p(ji,jj) / pp_ri_p_thresh, 1._wp )**2 )
                  !    zwb_shr = MAX( zwb_shr, -0.25 * pshear_u )
               ENDIF
               pwb_ent(ji,jj) = pwb_ent(ji,jj) + zwb_shr
               !           pwb_min(ji,jj) = pwb_ent(ji,jj) + pdh(ji,jj) / phbl(ji,jj) * zwb0(ji,jj)
            ELSE   ! IF ( l_conv ) THEN - ENDIF
               ! Stable OSBL  - shear production not coded for first attempt.
            ENDIF   ! l_conv
         END IF   ! l_shear
         IF ( l_conv(ji,jj) ) THEN
            ! Unstable OSBL
            pwb_min(ji,jj) = pwb_ent(ji,jj) + pdh(ji,jj) / phbl(ji,jj) * 2.0_wp * swbav(ji,jj)
         END IF  ! l_conv
      END_2D
      !
   END SUBROUTINE zdf_osm_osbl_state

   SUBROUTINE zdf_osm_external_gradients( Kmm, kbase, pdtdz, pdsdz, pdbdz )
      !!---------------------------------------------------------------------
      !!                   ***  ROUTINE zdf_osm_external_gradients  ***
      !!
      !! ** Purpose : Calculates the gradients below the OSBL
      !!
      !! ** Method  : Uses nbld and ibld_ext to determine levels to calculate the gradient.
      !!
      !!----------------------------------------------------------------------   
      INTEGER,                            INTENT(in   ) ::   Kmm            ! Ocean time-level index
      INTEGER,  DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   kbase          ! OSBL base layer index
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(  out) ::   pdtdz, pdsdz   ! External gradients of temperature, salinity
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(  out) ::   pdbdz          !    and buoyancy
      !!
      INTEGER  ::   ji, jj, jkb, jkb1
      REAL(wp) ::   zthermal, zbeta
      !!
      REAL(wp), PARAMETER ::   pp_large = -1e10_wp
      !!----------------------------------------------------------------------   
      !
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         pdtdz(ji,jj) = pp_large
         pdsdz(ji,jj) = pp_large
         pdbdz(ji,jj) = pp_large
      END_2D
      !
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         IF ( kbase(ji,jj)+1 < mbkt(ji,jj) ) THEN
            zthermal = rab_n(ji,jj,1,jp_tem)   ! Ideally use nbld not 1??
            zbeta    = rab_n(ji,jj,1,jp_sal)
            jkb = kbase(ji,jj)
            jkb1 = MIN( jkb + 1, mbkt(ji,jj) )
            pdtdz(ji,jj) = -1.0_wp * ( ts(ji,jj,jkb1,jp_tem,Kmm) - ts(ji,jj,jkb,jp_tem,Kmm ) ) / e3w(ji,jj,jkb1,Kmm)
            pdsdz(ji,jj) = -1.0_wp * ( ts(ji,jj,jkb1,jp_sal,Kmm) - ts(ji,jj,jkb,jp_sal,Kmm ) ) / e3w(ji,jj,jkb1,Kmm)
            pdbdz(ji,jj) = grav * zthermal * pdtdz(ji,jj) - grav * zbeta * pdsdz(ji,jj)
         ELSE
            pdtdz(ji,jj) = 0.0_wp
            pdsdz(ji,jj) = 0.0_wp
            pdbdz(ji,jj) = 0.0_wp
         END IF
      END_2D
      !
   END SUBROUTINE zdf_osm_external_gradients

   SUBROUTINE zdf_osm_calculate_dhdt( pdhdt, phbl, pdh, pwb_ent, pwb_min,   &
      &                               pdbdz_bl_ext, pwb_fk_b, pwb_fk, pvel_mle )
      !!---------------------------------------------------------------------
      !!                   ***  ROUTINE zdf_osm_calculate_dhdt  ***
      !!
      !! ** Purpose : Calculates the rate at which hbl changes.
      !!
      !! ** Method  :
      !!
      !!----------------------------------------------------------------------
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(  out) ::   pdhdt          ! Rate of change of hbl
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   phbl           ! BL depth
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   pdh            ! Pycnocline depth
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   pwb_ent        ! Buoyancy entrainment flux
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   pwb_min
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   pdbdz_bl_ext   ! External buoyancy gradients
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(  out) ::   pwb_fk_b       ! MLE buoyancy flux averaged over OSBL
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   pwb_fk         ! Max MLE buoyancy flux
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   pvel_mle       ! Vvelocity scale for dhdt with stable ML and FK
      !!
      INTEGER  ::   jj, ji
      REAL(wp) ::   zgamma_b_nd, zgamma_dh_nd, zpert, zpsi, zari
      REAL(wp) ::   zvel_max, zddhdt
      !!
      REAL(wp), PARAMETER ::   pp_alpha_b = 0.3_wp
      REAL(wp), PARAMETER ::   pp_ddh     = 2.5_wp, pp_ddh_2 = 3.5_wp   ! Also in pycnocline_depth
      REAL(wp), PARAMETER ::   pp_large   = -1e10_wp
      !!----------------------------------------------------------------------
      !
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         pdhdt(ji,jj)    = pp_large
         pwb_fk_b(ji,jj) = pp_large
      END_2D
      !
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         !
         IF ( l_shear(ji,jj) ) THEN
            !
            IF ( l_conv(ji,jj) ) THEN   ! Convective
               !
               IF ( ln_osm_mle ) THEN
                  IF ( hmle(ji,jj) > hbl(ji,jj) ) THEN   ! Fox-Kemper buoyancy flux average over OSBL
                     pwb_fk_b(ji,jj) = pwb_fk(ji,jj) * ( 1.0_wp + hmle(ji,jj) / ( 6.0_wp * hbl(ji,jj) ) *   &
                        &                                         ( -1.0_wp + ( 1.0_wp - 2.0_wp * hbl(ji,jj) / hmle(ji,jj) )**3 ) )
                  ELSE
                     pwb_fk_b(ji,jj) = 0.5_wp * pwb_fk(ji,jj) * hmle(ji,jj) / hbl(ji,jj)
                  ENDIF
                  zvel_max = ( svstr(ji,jj)**3 + 0.5_wp * swstrc(ji,jj)**3 )**p2third / hbl(ji,jj)
                  IF ( ( pwb_ent(ji,jj) + 2.0_wp * pwb_fk_b(ji,jj) ) < 0.0_wp ) THEN   ! OSBL is deepening,
                     !                                                                 !    entrainment > restratification
                     IF ( av_db_bl(ji,jj) > 1e-15_wp ) THEN
                        zgamma_b_nd = MAX( pdbdz_bl_ext(ji,jj), 0.0_wp ) * pdh(ji,jj) /   &
                           &          ( zvel_max + MAX( av_db_bl(ji,jj), 1e-15_wp ) )
                        zpsi = ( 1.0_wp - 0.5_wp * pdh(ji,jj) / phbl(ji,jj) ) *                                                &
                           &   ( swb0(ji,jj) - MIN( ( pwb_min(ji,jj) + 2.0_wp * pwb_fk_b(ji,jj) ), 0.0_wp ) ) * pdh(ji,jj) /   &
                           &   phbl(ji,jj)
                        zpsi = zpsi + 1.75_wp * ( 1.0_wp - 0.5_wp * pdh(ji,jj) / phbl(ji,jj) ) *   &
                           &          ( pdh(ji,jj) / phbl(ji,jj) + zgamma_b_nd ) *   &
                           &          MIN( ( pwb_min(ji,jj) + 2.0_wp * pwb_fk_b(ji,jj) ), 0.0_wp )
                        zpsi = pp_alpha_b * MAX( zpsi, 0.0_wp )
                        pdhdt(ji,jj) = -1.0_wp * ( pwb_ent(ji,jj) + 2.0_wp * pwb_fk_b(ji,jj) ) /      &
                           &                      ( zvel_max + MAX( av_db_bl(ji,jj), 1e-15_wp ) ) +   &
                           &            zpsi / ( zvel_max + MAX( av_db_bl(ji,jj), 1e-15_wp ) )
                        IF ( n_ddh(ji,jj) == 1 ) THEN
                           IF ( ( swstrc(ji,jj) / svstr(ji,jj) )**3 <= 0.5_wp ) THEN
                              zari = MIN( 1.5_wp * av_db_bl(ji,jj) /                                                   &
                                 &        ( phbl(ji,jj) * ( MAX( pdbdz_bl_ext(ji,jj), 0.0_wp ) +                       &
                                 &                               av_db_bl(ji,jj)**2 / MAX( 4.5_wp * svstr(ji,jj)**2,   &
                                 &                                                       1e-12_wp ) ) ), 0.2_wp )
                           ELSE
                              zari = MIN( 1.5_wp * av_db_bl(ji,jj) /                                                    &
                                 &        ( phbl(ji,jj) * ( MAX( pdbdz_bl_ext(ji,jj), 0.0_wp ) +                        &
                                 &                               av_db_bl(ji,jj)**2 / MAX( 4.5_wp * swstrc(ji,jj)**2,   &
                                 &                                                       1e-12_wp ) ) ), 0.2_wp )
                           ENDIF
                           ! Relaxation to dh_ref = zari * hbl
                           zddhdt = -1.0_wp * pp_ddh_2 * ( 1.0_wp - pdh(ji,jj) / ( zari * phbl(ji,jj) ) ) * pwb_ent(ji,jj) /   &
                              &     ( zvel_max + MAX( av_db_bl(ji,jj), 1e-15_wp ) )
                        ELSE IF ( n_ddh(ji,jj) == 0 ) THEN   ! Growing shear layer
                           zddhdt = -1.0_wp * pp_ddh * ( 1.0_wp - 1.6_wp * pdh(ji,jj) / phbl(ji,jj) ) * pwb_ent(ji,jj) /   &
                              &     ( zvel_max + MAX( av_db_bl(ji,jj), 1e-15_wp ) )
                           zddhdt = EXP( -4.0_wp * ABS( ff_t(ji,jj) ) * phbl(ji,jj) / MAX( sustar(ji,jj), 1e-8_wp ) ) * zddhdt
                        ELSE
                           zddhdt = 0.0_wp
                        ENDIF   ! n_ddh
                        pdhdt(ji,jj) = pdhdt(ji,jj) + pp_alpha_b * ( 1.0_wp - 0.5_wp * pdh(ji,jj) / phbl(ji,jj) ) *   &
                           &                            av_db_ml(ji,jj) * MAX( zddhdt, 0.0_wp ) /   &
                           &                            ( zvel_max + MAX( av_db_bl(ji,jj), 1e-15_wp ) )
                     ELSE   ! av_db_bl >0
                        pdhdt(ji,jj) = -1.0_wp * ( pwb_ent(ji,jj) + 2.0_wp * pwb_fk_b(ji,jj) ) /  MAX( zvel_max, 1e-15_wp )
                     ENDIF
                  ELSE   ! pwb_min + 2*pwb_fk_b < 0
                     ! OSBL shoaling due to restratification flux. This is the velocity defined in Fox-Kemper et al (2008)
                     pdhdt(ji,jj) = -1.0_wp * MIN( pvel_mle(ji,jj), hbl(ji,jj) / 10800.0_wp )
                  ENDIF
               ELSE   ! Fox-Kemper not used.
                  zvel_max = -1.0_wp * ( 1.0_wp + 1.0_wp * ( svstr(ji,jj)**3 + 0.5_wp * swstrc(ji,jj)**3 )**pthird *     &
                     &                                                         rn_Dt / hbl(ji,jj) ) * pwb_ent(ji,jj) /   &
                     &       MAX( ( svstr(ji,jj)**3 + 0.5_wp * swstrc(ji,jj)**3 )**pthird, epsln )
                  pdhdt(ji,jj) = -1.0_wp * pwb_ent(ji,jj) / ( zvel_max + MAX( av_db_bl(ji,jj), 1e-15_wp ) )
                  ! added ajgn 23 July as temporay fix
               ENDIF   ! ln_osm_mle
               !
            ELSE   ! l_conv - Stable
               !
               pdhdt(ji,jj) = ( 0.06_wp + 0.52_wp * shol(ji,jj) / 2.0_wp ) * svstr(ji,jj)**3 / hbl(ji,jj) + swbav(ji,jj)
               IF ( pdhdt(ji,jj) < 0.0_wp ) THEN   ! For long timsteps factor in brackets slows the rapid collapse of the OSBL
                  zpert = 2.0_wp * ( 1.0_wp + 0.0_wp * 2.0_wp * svstr(ji,jj) * rn_Dt / hbl(ji,jj) ) * svstr(ji,jj)**2 / hbl(ji,jj)
               ELSE
                  zpert = MAX( svstr(ji,jj)**2 / hbl(ji,jj), av_db_bl(ji,jj) )
               ENDIF
               pdhdt(ji,jj) = 2.0_wp * pdhdt(ji,jj) / MAX( zpert, epsln )
               pdhdt(ji,jj) = MAX( pdhdt(ji,jj), -1.0_wp * hbl(ji,jj) / 5400.0_wp )
               !
            ENDIF   ! l_conv
            !
         ELSE   ! l_shear
            !
            IF ( l_conv(ji,jj) ) THEN   ! Convective
               !
               IF ( ln_osm_mle ) THEN
                  IF ( hmle(ji,jj) > hbl(ji,jj) ) THEN   ! Fox-Kemper buoyancy flux average over OSBL
                     pwb_fk_b(ji,jj) = pwb_fk(ji,jj) *                       &
                        ( 1.0_wp + hmle(ji,jj) / ( 6.0_wp * hbl(ji,jj) ) *   &
                        &          ( -1.0_wp + ( 1.0_wp - 2.0_wp * hbl(ji,jj) / hmle(ji,jj))**3) )
                  ELSE
                     pwb_fk_b(ji,jj) = 0.5_wp * pwb_fk(ji,jj) * hmle(ji,jj) / hbl(ji,jj)
                  ENDIF
                  zvel_max = ( swstrl(ji,jj)**3 + 0.5_wp * swstrc(ji,jj)**3 )**p2third / hbl(ji,jj)
                  IF ( ( pwb_ent(ji,jj) + 2.0_wp * pwb_fk_b(ji,jj) ) < 0.0_wp ) THEN   ! OSBL is deepening,
                     !                                                                 !    entrainment > restratification
                     IF ( av_db_bl(ji,jj) > 0.0_wp .AND. pdbdz_bl_ext(ji,jj) > 0.0_wp ) THEN
                        pdhdt(ji,jj) = -1.0_wp * ( pwb_ent(ji,jj) + 2.0_wp * pwb_fk_b(ji,jj) ) /   &
                           &            ( zvel_max + MAX( av_db_bl(ji,jj), 1e-15_wp ) )
                     ELSE
                        pdhdt(ji,jj) = -1.0_wp * ( pwb_ent(ji,jj) + 2.0_wp * pwb_fk_b(ji,jj) ) / MAX( zvel_max, 1e-15_wp )
                     ENDIF
                  ELSE   ! OSBL shoaling due to restratification flux. This is the velocity defined in Fox-Kemper et al (2008)
                     pdhdt(ji,jj) = -1.0_wp * MIN( pvel_mle(ji,jj), hbl(ji,jj) / 10800.0_wp )
                  ENDIF
               ELSE   ! Fox-Kemper not used
                  zvel_max = -1.0_wp * pwb_ent(ji,jj) / MAX( ( svstr(ji,jj)**3 + 0.5_wp * swstrc(ji,jj)**3 )**pthird, epsln )
                  pdhdt(ji,jj) = -1.0_wp * pwb_ent(ji,jj) / ( zvel_max + MAX( av_db_bl(ji,jj), 1e-15_wp ) )
                  ! added ajgn 23 July as temporay fix
               ENDIF  ! ln_osm_mle
               !
            ELSE                        ! Stable
               !
               pdhdt(ji,jj) = ( 0.06_wp + 0.52_wp * shol(ji,jj) / 2.0_wp ) * svstr(ji,jj)**3 / hbl(ji,jj) + swbav(ji,jj)
               IF ( pdhdt(ji,jj) < 0.0_wp ) THEN
                  ! For long timsteps factor in brackets slows the rapid collapse of the OSBL
                  zpert = 2.0_wp * svstr(ji,jj)**2 / hbl(ji,jj)
               ELSE
                  zpert = MAX( svstr(ji,jj)**2 / hbl(ji,jj), av_db_bl(ji,jj) )
               ENDIF
               pdhdt(ji,jj) = 2.0_wp * pdhdt(ji,jj) / MAX(zpert, epsln)
               pdhdt(ji,jj) = MAX( pdhdt(ji,jj), -1.0_wp * hbl(ji,jj) / 5400.0_wp )
               !
            ENDIF  ! l_conv
            !
         ENDIF ! l_shear
         !
      END_2D
      !
   END SUBROUTINE zdf_osm_calculate_dhdt

   SUBROUTINE zdf_osm_timestep_hbl( Kmm, pdhdt, phbl, phbl_t, pwb_ent,   &
      &                             pwb_fk_b )
      !!---------------------------------------------------------------------
      !!                ***  ROUTINE zdf_osm_timestep_hbl  ***
      !!
      !! ** Purpose : Increments hbl.
      !!
      !! ** Method  : If the change in hbl exceeds one model level the change is
      !!              is calculated by moving down the grid, changing the
      !!              buoyancy jump. This is to ensure that the change in hbl
      !!              does not overshoot a stable layer.
      !!
      !!----------------------------------------------------------------------
      INTEGER,                            INTENT(in   ) ::   Kmm        ! Ocean time-level index
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(inout) ::   pdhdt      ! Rates of change of hbl
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(inout) ::   phbl       ! BL depth
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   phbl_t     ! BL depth
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   pwb_ent    ! Buoyancy entrainment flux
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   pwb_fk_b   ! MLE buoyancy flux averaged over OSBL
      !!
      INTEGER  ::   jk, jj, ji, jm
      REAL(wp) ::   zhbl_s, zvel_max, zdb
      REAL(wp) ::   zthermal, zbeta
      !!----------------------------------------------------------------------
      !
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         IF ( nbld(ji,jj) - nmld(ji,jj) > 1 ) THEN
            !
            ! If boundary layer changes by more than one level, need to check for stable layers between initial and final depths.
            !
            zhbl_s   = hbl(ji,jj)
            jm       = nmld(ji,jj)
            zthermal = rab_n(ji,jj,1,jp_tem)
            zbeta    = rab_n(ji,jj,1,jp_sal)
            !
            IF ( l_conv(ji,jj) ) THEN   ! Unstable
               !
               IF( ln_osm_mle ) THEN
                  zvel_max = ( swstrl(ji,jj)**3 + swstrc(ji,jj)**3 )**p2third / hbl(ji,jj)
               ELSE
                  zvel_max = -1.0_wp * ( 1.0_wp + 1.0_wp * ( svstr(ji,jj)**3 + 0.5_wp * swstrc(ji,jj)**3 )**pthird * rn_Dt /   &
                     &                                     hbl(ji,jj) ) * pwb_ent(ji,jj) /                                     &
                     &       ( svstr(ji,jj)**3 + 0.5_wp * swstrc(ji,jj)**3 )**pthird
               ENDIF
               DO jk = nmld(ji,jj), nbld(ji,jj)
                  zdb = MAX( grav * ( zthermal * ( av_t_bl(ji,jj) - ts(ji,jj,jm,jp_tem,Kmm) ) -   &
                     &                zbeta    * ( av_s_bl(ji,jj) - ts(ji,jj,jm,jp_sal,Kmm) ) ), 0.0_wp ) + zvel_max
                  !
                  IF ( ln_osm_mle ) THEN
                     zhbl_s = zhbl_s + MIN( rn_Dt * ( ( -1.0_wp * pwb_ent(ji,jj) - 2.0_wp * pwb_fk_b(ji,jj) ) / zdb ) /   &
                        &                   REAL( nbld(ji,jj) - nmld(ji,jj), KIND=wp ), e3w(ji,jj,jm,Kmm) )
                  ELSE
                     zhbl_s = zhbl_s + MIN( rn_Dt * ( -1.0_wp * pwb_ent(ji,jj) / zdb ) /   &
                        &                   REAL( nbld(ji,jj) - nmld(ji,jj), KIND=wp ), e3w(ji,jj,jm,Kmm) )
                  ENDIF
                  !                    zhbl_s = MIN(zhbl_s,  gdepw(ji,jj, mbkt(ji,jj) + 1,Kmm) - depth_tol)
                  IF ( zhbl_s >= gdepw(ji,jj,mbkt(ji,jj) + 1,Kmm) ) THEN
                     zhbl_s = MIN( zhbl_s, gdepw(ji,jj, mbkt(ji,jj) + 1, Kmm ) - depth_tol )
                     l_pyc(ji,jj) = .FALSE.
                  ENDIF
                  IF ( zhbl_s >= gdepw(ji,jj,jm+1,Kmm) ) jm = jm + 1
               END DO
               hbl(ji,jj)  = zhbl_s
               nbld(ji,jj) = jm
            ELSE   ! Stable
               DO jk = nmld(ji,jj), nbld(ji,jj)
                  zdb = MAX(  grav * ( zthermal * ( av_t_bl(ji,jj) - ts(ji,jj,jm,jp_tem,Kmm) ) -               &
                     &                 zbeta    * ( av_s_bl(ji,jj) - ts(ji,jj,jm,jp_sal,Kmm) ) ), 0.0_wp ) +   &
                     &  2.0_wp * svstr(ji,jj)**2 / zhbl_s
                  !
                  ! Alan is thuis right? I have simply changed hbli to hbl
                  shol(ji,jj)  = -1.0_wp * zhbl_s / ( ( svstr(ji,jj)**3 + epsln ) / swbav(ji,jj) )
                  pdhdt(ji,jj) = -1.0_wp * ( swbav(ji,jj) - 0.04_wp / 2.0_wp * swstrl(ji,jj)**3 / zhbl_s -   &
                     &                       0.15_wp / 2.0_wp * ( 1.0_wp - EXP( -1.5_wp * sla(ji,jj) ) ) *   &
                     &                                 sustar(ji,jj)**3 / zhbl_s ) *                         &
                     &           ( 0.725_wp + 0.225_wp * EXP( -7.5_wp * shol(ji,jj) ) )
                  pdhdt(ji,jj) = pdhdt(ji,jj) + swbav(ji,jj)
                  zhbl_s = zhbl_s + MIN( pdhdt(ji,jj) / zdb * rn_Dt / REAL( nbld(ji,jj) - nmld(ji,jj), KIND=wp ),   &
                     &                   e3w(ji,jj,jm,Kmm) )
                  
                  !                    zhbl_s = MIN(zhbl_s, gdepw(ji,jj, mbkt(ji,jj) + 1,Kmm) - depth_tol)
                  IF ( zhbl_s >= gdepw(ji,jj,mbkt(ji,jj) + 1,Kmm) ) THEN
Guillaume Samson's avatar
Guillaume Samson committed
                     zhbl_s      = MIN( zhbl_s,  gdepw(ji,jj,mbkt(ji,jj)+1,Kmm) - depth_tol )
                     l_pyc(ji,jj) = .FALSE.
                  ENDIF
                  IF ( zhbl_s >= gdepw(ji,jj,jm,Kmm) ) jm = jm + 1
               END DO
            ENDIF   ! IF ( l_conv )
            hbl(ji,jj)  = MAX( zhbl_s, gdepw(ji,jj,4,Kmm) )
            nbld(ji,jj) = MAX( jm, 4 )
         ELSE
            ! change zero or one model level.
            hbl(ji,jj) = MAX( phbl_t(ji,jj), gdepw(ji,jj,4,Kmm) )
         ENDIF
         phbl(ji,jj) = gdepw(ji,jj,nbld(ji,jj),Kmm)
      END_2D
      !
   END SUBROUTINE zdf_osm_timestep_hbl

   SUBROUTINE zdf_osm_pycnocline_thickness( Kmm, pdh, phml, pdhdt, phbl,   &
      &                                     pwb_ent, pdbdz_bl_ext, pwb_fk_b )
      !!---------------------------------------------------------------------
      !!            ***  ROUTINE zdf_osm_pycnocline_thickness  ***
      !!
      !! ** Purpose : Calculates thickness of the pycnocline
      !!
      !! ** Method  : The thickness is calculated from a prognostic equation
      !!              that relaxes the pycnocine thickness to a diagnostic
      !!              value. The time change is calculated assuming the
      !!              thickness relaxes exponentially. This is done to deal
      !!              with large timesteps.
      !!
      !!----------------------------------------------------------------------
      INTEGER,                            INTENT(in   ) ::   Kmm            ! Ocean time-level index
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(inout) ::   pdh            ! Pycnocline thickness
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(inout) ::   phml           ! ML depth
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   pdhdt          ! BL depth tendency
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   phbl           ! BL depth
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   pwb_ent        ! Buoyancy entrainment flux
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   pdbdz_bl_ext   ! External buoyancy gradients
      REAL(wp), DIMENSION(A2D(nn_hls-1)), INTENT(in   ) ::   pwb_fk_b       ! MLE buoyancy flux averaged over OSBL
      !!
      INTEGER  ::   jj, ji
      INTEGER  ::   inhml
      REAL(wp) ::   zari, ztau, zdh_ref, zddhdt, zvel_max
      REAL(wp) ::   ztmp   ! Auxiliary variable
      !!
      REAL(wp), PARAMETER ::   pp_ddh = 2.5_wp, pp_ddh_2 = 3.5_wp   ! Also in pycnocline_depth
Guillaume Samson's avatar
Guillaume Samson committed
      !!----------------------------------------------------------------------
      !
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         !
         IF ( l_shear(ji,jj) ) THEN
            !
            IF ( l_conv(ji,jj) ) THEN
               !
               IF ( av_db_bl(ji,jj) > 1e-15_wp ) THEN
                  IF ( n_ddh(ji,jj) == 0 ) THEN
                     zvel_max = ( svstr(ji,jj)**3 + 0.5_wp * swstrc(ji,jj)**3 )**p2third / hbl(ji,jj)
                     ! ddhdt for pycnocline determined in osm_calculate_dhdt
                     zddhdt = -1.0_wp * pp_ddh * ( 1.0_wp - 1.6_wp * pdh(ji,jj) / phbl(ji,jj) ) * pwb_ent(ji,jj) /   &
                        &     ( zvel_max + MAX( av_db_bl(ji,jj), 1e-15 ) )
                     zddhdt = EXP( -4.0_wp * ABS( ff_t(ji,jj) ) * phbl(ji,jj) / MAX( sustar(ji,jj), 1e-8 ) ) * zddhdt
                     ! Maximum limit for how thick the shear layer can grow relative to the thickness of the boundary layer
                     dh(ji,jj) = MIN( dh(ji,jj) + zddhdt * rn_Dt, 0.625_wp * hbl(ji,jj) )
                  ELSE   ! Need to recalculate because hbl has been updated
                     IF ( ( swstrc(ji,jj) / svstr(ji,jj) )**3 <= 0.5_wp ) THEN
                        ztmp = svstr(ji,jj)
                     ELSE
                        ztmp = swstrc(ji,jj)
                     END IF
                     zari = MIN( 1.5_wp * av_db_bl(ji,jj) / ( phbl(ji,jj) * ( MAX( pdbdz_bl_ext(ji,jj), 0.0_wp ) +        &
                        &                                                   av_db_bl(ji,jj)**2 / MAX( 4.5_wp * ztmp**2,   &
                        &                                                                           1e-12_wp ) ) ), 0.2_wp )
                     ztau = MAX( av_db_bl(ji,jj) * ( zari * hbl(ji,jj) ) /   &
                        &        ( pp_ddh_2 * MAX( -1.0_wp * pwb_ent(ji,jj), 1e-12_wp ) ), 2.0_wp * rn_Dt )
                     dh(ji,jj) = dh(ji,jj) * EXP( -1.0_wp * rn_Dt / ztau ) +   &
                        &        zari * phbl(ji,jj) * ( 1.0_wp - EXP( -1.0_wp * rn_Dt / ztau ) )
                     IF ( dh(ji,jj) >= hbl(ji,jj) ) dh(ji,jj) = zari * phbl(ji,jj)
                  END IF
               ELSE
                  ztau = MAX( MAX( hbl(ji,jj) / ( svstr(ji,jj)**3 + 0.5_wp * swstrc(ji,jj)**3 )**pthird, epsln), 2.0_wp * rn_Dt )
                  dh(ji,jj) = dh(ji,jj) * EXP( -1.0_wp * rn_Dt / ztau ) +   &
                     &        0.2_wp * phbl(ji,jj) * ( 1.0_wp - EXP( -1.0_wp * rn_Dt / ztau ) )
                  IF ( dh(ji,jj) > hbl(ji,jj) ) dh(ji,jj) = 0.2_wp * hbl(ji,jj)
               END IF
               !
            ELSE   ! l_conv
               ! Initially shear only for entraining OSBL. Stable code will be needed if extended to stable OSBL
               ztau = hbl(ji,jj) / MAX(svstr(ji,jj), epsln)
               IF ( pdhdt(ji,jj) >= 0.0_wp ) THEN   ! Probably shouldn't include wm here
                  ! Boundary layer deepening
                  IF ( av_db_bl(ji,jj) > 0.0_wp ) THEN
                     ! Pycnocline thickness set by stratification - use same relationship as for neutral conditions
                     zari    = MIN( 4.5_wp * ( svstr(ji,jj)**2 ) / MAX( av_db_bl(ji,jj) * phbl(ji,jj), epsln ) + 0.01_wp, 0.2_wp )
                     zdh_ref = MIN( zari, 0.2_wp ) * hbl(ji,jj)
                  ELSE
                     zdh_ref = 0.2_wp * hbl(ji,jj)
                  ENDIF
               ELSE   ! IF(dhdt < 0)
                  zdh_ref = 0.2_wp * hbl(ji,jj)
               ENDIF   ! IF (dhdt >= 0)
               dh(ji,jj) = dh(ji,jj) * EXP( -1.0_wp * rn_Dt / ztau ) + zdh_ref * ( 1.0_wp - EXP( -1.0_wp * rn_Dt / ztau ) )
               IF ( pdhdt(ji,jj) < 0.0_wp .AND. dh(ji,jj) >= hbl(ji,jj) ) dh(ji,jj) = zdh_ref   ! Can be a problem with dh>hbl for
               !                                                                                !    rapid collapse
            ENDIF
            !
         ELSE   ! l_shear = .FALSE., calculate ddhdt here
            !
            IF ( l_conv(ji,jj) ) THEN
               !
               IF( ln_osm_mle ) THEN
                  IF ( ( pwb_ent(ji,jj) + 2.0_wp * pwb_fk_b(ji,jj) ) < 0.0_wp ) THEN   ! OSBL is deepening. Note wb_fk_b is zero if
                     !                                                                 !    ln_osm_mle=F
                     IF ( av_db_bl(ji,jj) > 0.0_wp .AND. pdbdz_bl_ext(ji,jj) > 0.0_wp ) THEN
                        IF ( ( swstrc(ji,jj) / MAX( svstr(ji,jj), epsln) )**3 <= 0.5_wp ) THEN   ! Near neutral stability
                           ztmp = svstr(ji,jj)
                        ELSE   ! Unstable
                           ztmp = swstrc(ji,jj)
                        END IF
                        zari = MIN( 1.5_wp * av_db_bl(ji,jj) /                               &
                           &        ( phbl(ji,jj) * ( MAX( pdbdz_bl_ext(ji,jj), 0.0_wp ) +   &
                           &                          av_db_bl(ji,jj)**2 / MAX( 4.5_wp * ztmp**2 , 1e-12_wp ) ) ), 0.2_wp )
                     ELSE
                        zari = 0.2_wp
                     END IF
                  ELSE
                     zari = 0.2_wp
                  END IF
                  ztau    = 0.2_wp * hbl(ji,jj) / MAX( epsln, ( svstr(ji,jj)**3 + 0.5_wp * swstrc(ji,jj)**3 )**pthird )
                  zdh_ref = zari * hbl(ji,jj)
               ELSE   ! ln_osm_mle
                  IF ( av_db_bl(ji,jj) > 0.0_wp .AND. pdbdz_bl_ext(ji,jj) > 0.0_wp ) THEN
                     IF ( ( swstrc(ji,jj) / MAX( svstr(ji,jj), epsln ) )**3 <= 0.5_wp ) THEN   ! Near neutral stability
                        ztmp = svstr(ji,jj)
                     ELSE   ! Unstable
                        ztmp = swstrc(ji,jj)
                     END IF
                     zari    = MIN( 1.5_wp * av_db_bl(ji,jj) /                               &
                        &           ( phbl(ji,jj) * ( MAX( pdbdz_bl_ext(ji,jj), 0.0_wp ) +   &
                        &                             av_db_bl(ji,jj)**2 / MAX( 4.5_wp * ztmp**2 , 1e-12_wp ) ) ), 0.2_wp )
                  ELSE
                     zari    = 0.2_wp
                  END IF
                  ztau    = hbl(ji,jj) / MAX( epsln, ( svstr(ji,jj)**3 + 0.5_wp * swstrc(ji,jj)**3 )**pthird )
                  zdh_ref = zari * hbl(ji,jj)
               END IF   ! ln_osm_mle
               dh(ji,jj) = dh(ji,jj) * EXP( -1.0_wp * rn_Dt / ztau ) + zdh_ref * ( 1.0_wp - EXP( -1.0_wp * rn_Dt / ztau ) )
               !               IF ( pdhdt(ji,jj) < 0._wp .and. dh(ji,jj) >= hbl(ji,jj) ) dh(ji,jj) = zdh_ref
               IF ( dh(ji,jj) >= hbl(ji,jj) ) dh(ji,jj) = zdh_ref
               ! Alan: this hml is never defined or used
            ELSE   ! IF (l_conv)
               !
               ztau = hbl(ji,jj) / MAX( svstr(ji,jj), epsln )
               IF ( pdhdt(ji,jj) >= 0.0_wp ) THEN   ! Probably shouldn't include wm here
                  ! Boundary layer deepening
                  IF ( av_db_bl(ji,jj) > 0.0_wp ) THEN
                     ! Pycnocline thickness set by stratification - use same relationship as for neutral conditions.
                     zari    = MIN( 4.5_wp * ( svstr(ji,jj)**2 ) / MAX( av_db_bl(ji,jj) * phbl(ji,jj), epsln ) + 0.01_wp , 0.2_wp )
                     zdh_ref = MIN( zari, 0.2_wp ) * hbl(ji,jj)
                  ELSE
                     zdh_ref = 0.2_wp * hbl(ji,jj)
                  END IF
               ELSE   ! IF(dhdt < 0)
                  zdh_ref = 0.2_wp * hbl(ji,jj)
               END IF   ! IF (dhdt >= 0)
               dh(ji,jj) = dh(ji,jj) * EXP( -1.0_wp * rn_Dt / ztau ) + zdh_ref * ( 1.0_wp - EXP( -1.0_wp * rn_Dt / ztau ) )
               IF ( pdhdt(ji,jj) < 0.0_wp .AND. dh(ji,jj) >= hbl(ji,jj) ) dh(ji,jj) = zdh_ref   ! Can be a problem with dh>hbl for
               !                                                                                !    rapid collapse
            END IF   ! IF (l_conv)
            !
         END IF   ! l_shear
         !
         hml(ji,jj)  = hbl(ji,jj) - dh(ji,jj)
         inhml       = MAX( INT( dh(ji,jj) / MAX( e3t(ji,jj,nbld(ji,jj)-1,Kmm), 1e-3_wp ) ), 1 )
         nmld(ji,jj) = MAX( nbld(ji,jj) - inhml, 3 )
         phml(ji,jj) = gdepw(ji,jj,nmld(ji,jj),Kmm)
         pdh(ji,jj)  = phbl(ji,jj) - phml(ji,jj)
         !
      END_2D
      !
   END SUBROUTINE zdf_osm_pycnocline_thickness

   SUBROUTINE zdf_osm_pycnocline_buoyancy_profiles( Kmm, kp_ext, pdbdz, palpha, pdh,   &
      &                                             phbl, pdbdz_bl_ext, phml, pdhdt )
      !!---------------------------------------------------------------------
      !!       ***  ROUTINE zdf_osm_pycnocline_buoyancy_profiles  ***
      !!
      !! ** Purpose : calculate pycnocline buoyancy profiles
      !!
      !! ** Method  : 
      !!
      !!----------------------------------------------------------------------
      INTEGER,                                 INTENT(in   ) ::   Kmm            ! Ocean time-level index
      INTEGER,  DIMENSION(A2D(nn_hls-1)),      INTENT(in   ) ::   kp_ext         ! External-level offsets
      REAL(wp), DIMENSION(A2D(nn_hls-1),jpk),  INTENT(  out) ::   pdbdz          ! Gradients in the pycnocline
      REAL(wp), DIMENSION(A2D(nn_hls-1)),      INTENT(  out) ::   palpha
      REAL(wp), DIMENSION(A2D(nn_hls-1)),      INTENT(in   ) ::   pdh            ! Pycnocline thickness
      REAL(wp), DIMENSION(A2D(nn_hls-1)),      INTENT(in   ) ::   phbl           ! BL depth
      REAL(wp), DIMENSION(A2D(nn_hls-1)),      INTENT(in   ) ::   pdbdz_bl_ext   ! External buoyancy gradients
      REAL(wp), DIMENSION(A2D(nn_hls-1)),      INTENT(in   ) ::   phml           ! ML depth
      REAL(wp), DIMENSION(A2D(nn_hls-1)),      INTENT(in   ) ::   pdhdt          ! Rates of change of hbl
      !!
      INTEGER  ::   jk, jj, ji
      REAL(wp) ::   zbgrad
      REAL(wp) ::   zgamma_b_nd, znd
      REAL(wp) ::   zzeta_m
      REAL(wp) ::   ztmp   ! Auxiliary variable
      !!
      REAL(wp), PARAMETER ::   pp_gamma_b = 2.25_wp
      REAL(wp), PARAMETER ::   pp_large   = -1e10_wp
      !!----------------------------------------------------------------------
      !
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )      
         pdbdz(ji,jj,:) = pp_large
         palpha(ji,jj)  = pp_large
      END_2D
      !
      DO_2D_OVR( nn_hls-1, nn_hls-1, nn_hls-1, nn_hls-1 )
         !
         IF ( nbld(ji,jj) + kp_ext(ji,jj) < mbkt(ji,jj) ) THEN
            !
            IF ( l_conv(ji,jj) ) THEN   ! Convective conditions
               !
               IF ( l_pyc(ji,jj) ) THEN
                  !
                  zzeta_m = 0.1_wp + 0.3_wp / ( 1.0_wp + EXP( -3.5_wp * LOG10( -1.0_wp * shol(ji,jj) ) ) )
                  palpha(ji,jj) = 2.0_wp * ( 1.0_wp - ( 0.80_wp * zzeta_m + 0.5_wp * SQRT( 3.14159_wp / pp_gamma_b ) ) *   &
                     &                                pdbdz_bl_ext(ji,jj) * pdh(ji,jj) / av_db_ml(ji,jj) ) /                &
                     &            ( 0.723_wp + SQRT( 3.14159_wp / pp_gamma_b ) )
                  palpha(ji,jj) = MAX( palpha(ji,jj), 0.0_wp )
                  ztmp = 1.0_wp / MAX( pdh(ji,jj), epsln )
                  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                  ! Commented lines in this section are not needed in new code, once tested !
                  ! can be removed                                                          !
                  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                  ! ztgrad = zalpha * av_dt_ml(ji,jj) * ztmp + zdtdz_bl_ext(ji,jj)
                  ! zsgrad = zalpha * av_ds_ml(ji,jj) * ztmp + zdsdz_bl_ext(ji,jj)
                  zbgrad = palpha(ji,jj) * av_db_ml(ji,jj) * ztmp + pdbdz_bl_ext(ji,jj)
                  zgamma_b_nd = pdbdz_bl_ext(ji,jj) * pdh(ji,jj) / MAX( av_db_ml(ji,jj), epsln )
                  DO jk = 2, nbld(ji,jj)
                     znd = -1.0_wp * ( gdepw(ji,jj,jk,Kmm) - phbl(ji,jj) ) * ztmp
                     IF ( znd <= zzeta_m ) THEN
                        ! zdtdz(ji,jj,jk) = zdtdz_bl_ext(ji,jj) + zalpha * av_dt_ml(ji,jj) * ztmp * &
                        ! &        EXP( -6.0 * ( znd -zzeta_m )**2 )
                        ! zdsdz(ji,jj,jk) = zdsdz_bl_ext(ji,jj) + zalpha * av_ds_ml(ji,jj) * ztmp * &
                        ! & EXP( -6.0 * ( znd -zzeta_m )**2 )
                        pdbdz(ji,jj,jk) = pdbdz_bl_ext(ji,jj) + palpha(ji,jj) * av_db_ml(ji,jj) * ztmp * &
                           & EXP( -6.0_wp * ( znd -zzeta_m )**2 )
                     ELSE
                        ! zdtdz(ji,jj,jk) =  ztgrad * EXP( -pp_gamma_b * ( znd - zzeta_m )**2 )
                        ! zdsdz(ji,jj,jk) =  zsgrad * EXP( -pp_gamma_b * ( znd - zzeta_m )**2 )
                        pdbdz(ji,jj,jk) =  zbgrad * EXP( -1.0_wp * pp_gamma_b * ( znd - zzeta_m )**2 )
                     END IF
                  END DO
               END IF   ! If no pycnocline pycnocline gradients set to zero
               !
            ELSE   ! Stable conditions
               ! If pycnocline profile only defined when depth steady of increasing.
               IF ( pdhdt(ji,jj) > 0.0_wp ) THEN   ! Depth increasing, or steady.
                  IF ( av_db_bl(ji,jj) > 0.0_wp ) THEN
                     IF ( shol(ji,jj) >= 0.5_wp ) THEN   ! Very stable - 'thick' pycnocline
                        ztmp = 1.0_wp / MAX( phbl(ji,jj), epsln )
                        zbgrad = av_db_bl(ji,jj) * ztmp
                        DO jk = 2, nbld(ji,jj)
                           znd = gdepw(ji,jj,jk,Kmm) * ztmp
                           pdbdz(ji,jj,jk) = zbgrad * EXP( -15.0_wp * ( znd - 0.9_wp )**2 )
                        END DO
                     ELSE   ! Slightly stable - 'thin' pycnoline - needed when stable layer begins to form.
                        ztmp = 1.0_wp / MAX( pdh(ji,jj), epsln )
                        zbgrad = av_db_bl(ji,jj) * ztmp
                        DO jk = 2, nbld(ji,jj)
                           znd = -1.0_wp * ( gdepw(ji,jj,jk,Kmm) - phml(ji,jj) ) * ztmp
                           pdbdz(ji,jj,jk) = zbgrad * EXP( -1.75_wp * ( znd + 0.75_wp )**2 )
                        END DO
                     END IF   ! IF (shol >=0.5)
                  END IF      ! IF (av_db_bl> 0.)
               END IF         ! IF (pdhdt >= 0) pdhdt < 0 not considered since pycnocline profile is zero and profile arrays are
               !              !    intialized to zero
               !
            END IF            ! IF (l_conv)
            !
         END IF   ! IF ( nbld(ji,jj) < mbkt(ji,jj) )
         !
      END_2D
      !
      IF ( ln_dia_pyc_scl ) THEN   ! Output of pycnocline gradient profiles
         CALL zdf_osm_iomput( "zdbdz_pyc", wmask(A2D(0),:) * pdbdz(A2D(0),:) )
      END IF
      !
   END SUBROUTINE zdf_osm_pycnocline_buoyancy_profiles

   SUBROUTINE zdf_osm_diffusivity_viscosity( Kbb, Kmm, pdiffut, pviscos, phbl,   &
      &                                      phml, pdh, pdhdt, pshear,           &
      &                                      pwb_ent, pwb_min )
      !!---------------------------------------------------------------------
      !!           ***  ROUTINE zdf_osm_diffusivity_viscosity  ***
      !!
      !! ** Purpose : Determines the eddy diffusivity and eddy viscosity
      !!              profiles in the mixed layer and the pycnocline.
      !!
      !! ** Method  :
      !!
      !!----------------------------------------------------------------------
      INTEGER,                                 INTENT(in   ) ::   Kbb, Kmm       ! Ocean time-level indices
      REAL(wp), DIMENSION(A2D(nn_hls-1),jpk),  INTENT(inout) ::   pdiffut        ! t-diffusivity
      REAL(wp), DIMENSION(A2D(nn_hls-1),jpk),  INTENT(inout) ::   pviscos        ! Viscosity
      REAL(wp), DIMENSION(A2D(nn_hls-1)),      INTENT(in   ) ::   phbl           ! BL depth
      REAL(wp), DIMENSION(A2D(nn_hls-1)),      INTENT(in   ) ::   phml           ! ML depth
      REAL(wp), DIMENSION(A2D(nn_hls-1)),      INTENT(in   ) ::   pdh            ! Pycnocline depth
      REAL(wp), DIMENSION(A2D(nn_hls-1)),      INTENT(in   ) ::   pdhdt          ! BL depth tendency
      REAL(wp), DIMENSION(A2D(nn_hls-1)),      INTENT(in   ) ::   pshear         ! Shear production
      REAL(wp), DIMENSION(A2D(nn_hls-1)),      INTENT(in   ) ::   pwb_ent        ! Buoyancy entrainment flux
      REAL(wp), DIMENSION(A2D(nn_hls-1)),      INTENT(in   ) ::   pwb_min
      !!
      INTEGER ::   ji, jj, jk   ! Loop indices
      !! Scales used to calculate eddy diffusivity and viscosity profiles
      REAL(wp), DIMENSION(A2D(nn_hls-1)) ::   zdifml_sc,    zvisml_sc
      REAL(wp), DIMENSION(A2D(nn_hls-1)) ::   zdifpyc_n_sc, zdifpyc_s_sc
      REAL(wp), DIMENSION(A2D(nn_hls-1)) ::   zvispyc_n_sc, zvispyc_s_sc
      REAL(wp), DIMENSION(A2D(nn_hls-1)) ::   zbeta_d_sc,   zbeta_v_sc
      REAL(wp), DIMENSION(A2D(nn_hls-1)) ::   zb_coup,      zc_coup_vis,  zc_coup_dif
      !!
      REAL(wp) ::   zvel_sc_pyc, zvel_sc_ml, zstab_fac, zz_b
      REAL(wp) ::   za_cubic, zb_d_cubic, zc_d_cubic, zd_d_cubic,   &   ! Coefficients in cubic polynomial specifying diffusivity
         &                    zb_v_cubic, zc_v_cubic, zd_v_cubic        ! and viscosity in pycnocline
      REAL(wp) ::   zznd_ml, zznd_pyc, ztmp
      REAL(wp) ::   zmsku, zmskv
      !!
      REAL(wp), PARAMETER ::   pp_dif_ml     = 0.8_wp,  pp_vis_ml  = 0.375_wp
      REAL(wp), PARAMETER ::   pp_dif_pyc    = 0.15_wp, pp_vis_pyc = 0.142_wp
      REAL(wp), PARAMETER ::   pp_vispyc_shr = 0.15_wp
      !!----------------------------------------------------------------------
      !