Index: branches/2011/DEV_r2739_STFC_dCSE/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfgls.F90
===================================================================
--- branches/2011/DEV_r2739_STFC_dCSE/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfgls.F90	(revision 3849)
+++ branches/2011/DEV_r2739_STFC_dCSE/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfgls.F90	(revision 4443)
@@ -236,5 +236,5 @@
       DO jj = 2, jpjm1
          DO ji = 2, jpim1
-            DO jk = 2, jpkm1
+            DO jk = 2, mbkmax(ji,jj)-1 ! jpkm1
 #else
       DO jk = 2, jpkm1
@@ -266,5 +266,5 @@
          DO jj = 2, jpjm1 
             DO ji = 2, jpim1
-               DO jk = 2, jpkm1
+               DO jk = 2, mbkmax(ji,jj)-1 ! jpkm1
 #else
          DO jk = 2, jpkm1
@@ -297,5 +297,5 @@
       DO jj = 2, jpjm1
          DO ji = 2, jpim1
-            DO jk = 2, jpkm1
+            DO jk = 2, mbkmax(ji,jj)-1 ! jpkm1
 #else
       DO jk = 2, jpkm1
@@ -352,5 +352,13 @@
       END DO
       !
+#if defined key_z_first
+      DO jj = 1, jpj, 1
+         DO ji = 1, jpi, 1
+            z_elem_b(ji,jj,mbkmax(ji,jj)) = 1._wp
+         END DO
+      END DO
+#else
       z_elem_b(:,:,jpk) = 1._wp
+#endif
       !
       ! Set surface condition on zwall_psi (1 at the bottom)
@@ -489,5 +497,5 @@
       DO jj = 2, jpjm1
          DO ji = 2, jpim1
-            DO jk = 2, jpkm1                       ! First recurrence : Dk = Dk - Lk * Uk-1 / Dk-1
+            DO jk = 2, mbkmax(ji,jj)-1 ! jpkm1     ! First recurrence : Dk = Dk - Lk * Uk-1 / Dk-1
 #else
       DO jk = 2, jpkm1                             ! First recurrence : Dk = Dk - Lk * Uk-1 / Dk-1
@@ -502,5 +510,5 @@
       DO jj = 2, jpjm1
          DO ji = 2, jpim1
-            DO jk = 2, jpk                         ! Second recurrence : Lk = RHSk - Lk / Dk-1 * Lk-1
+            DO jk = 2, mbkmax(ji,jj) ! jpk         ! Second recurrence : Lk = RHSk - Lk / Dk-1 * Lk-1
 #else
       DO jk = 2, jpk                               ! Second recurrence : Lk = RHSk - Lk / Dk-1 * Lk-1
@@ -515,5 +523,5 @@
       DO jj = 2, jpjm1
          DO ji = 2, jpim1
-            DO jk = jpk-1, 2, -1                   ! Third recurrence : Ek = ( Lk - Uk * Ek+1 ) / Dk
+            DO jk = mbkmax(ji,jj)-1, 2, -1 ! jpk-1, 2, -1    ! Third recurrence : Ek = ( Lk - Uk * Ek+1 ) / Dk
 #else
       DO jk = jpk-1, 2, -1                         ! Third recurrence : Ek = ( Lk - Uk * Ek+1 ) / Dk
@@ -540,5 +548,5 @@
          DO jj = 2, jpjm1
             DO ji = 2, jpim1
-               DO jk = 2, jpkm1
+               DO jk = 2, mbkmax(ji,jj)-1 ! jpkm1
 #else
          DO jk = 2, jpkm1
@@ -555,5 +563,5 @@
          DO jj = 2, jpjm1
             DO ji = 2, jpim1
-               DO jk = 2, jpkm1
+               DO jk = 2, mbkmax(ji,jj)-1 ! jpkm1
 #else
          DO jk = 2, jpkm1
@@ -570,5 +578,5 @@
          DO jj = 2, jpjm1
             DO ji = 2, jpim1
-               DO jk = 2, jpkm1
+               DO jk = 2, mbkmax(ji,jj)-1 ! jpkm1
 #else
          DO jk = 2, jpkm1
@@ -585,5 +593,5 @@
          DO jj = 2, jpjm1
             DO ji = 2, jpim1
-               DO jk = 2, jpkm1
+               DO jk = 2, mbkmax(ji,jj)-1 ! jpkm1
 #else
          DO jk = 2, jpkm1
@@ -608,5 +616,5 @@
       DO jj = 2, jpjm1
          DO ji = 2, jpim1
-            DO jk = 2, jpkm1
+            DO jk = 2, mbkmax(ji,jj)-1 ! jpkm1
 #else
       DO jk = 2, jpkm1
@@ -655,5 +663,13 @@
       END DO
       !
+#if defined key_z_first
+      DO jj = 1, jpj, 1
+         DO ji = 1, jpi, 1
+            z_elem_b(ji,jj,mbkmax(ji,jj)) = 1.0_wp
+         END DO
+      END DO
+#else
       z_elem_b(:,:,jpk) = 1._wp
+#endif
 
       ! Surface boundary condition on psi
@@ -832,5 +848,5 @@
       DO jj = 2, jpjm1
          DO ji = 2, jpim1
-            DO jk = 2, jpkm1                       ! First recurrence : Dk = Dk - Lk * Uk-1 / Dk-1
+            DO jk = 2, mbkmax(ji,jj)-1 !  jpkm1    ! First recurrence : Dk = Dk - Lk * Uk-1 / Dk-1
 #else
       DO jk = 2, jpkm1                             ! First recurrence : Dk = Dk - Lk * Uk-1 / Dk-1
@@ -845,5 +861,5 @@
       DO jj = 2, jpjm1
          DO ji = 2, jpim1
-            DO jk = 2, jpk                         ! Second recurrence : Lk = RHSk - Lk / Dk-1 * Lk-1
+            DO jk = 2, mbkmax(ji,jj) ! jpk         ! Second recurrence : Lk = RHSk - Lk / Dk-1 * Lk-1
 #else
       DO jk = 2, jpk                               ! Second recurrence : Lk = RHSk - Lk / Dk-1 * Lk-1
@@ -858,5 +874,5 @@
       DO jj = 2, jpjm1
          DO ji = 2, jpim1
-            DO jk = jpk-1, 2, -1                   ! Third recurrence : Ek = ( Lk - Uk * Ek+1 ) / Dk
+            DO jk = mbkmax(ji,jj)-1, 2, -1 ! jpk-1, 2, -1  ! Third recurrence : Ek = ( Lk - Uk * Ek+1 ) / Dk
 #else
       DO jk = jpk-1, 2, -1                         ! Third recurrence : Ek = ( Lk - Uk * Ek+1 ) / Dk
@@ -878,5 +894,5 @@
          DO jj = 2, jpjm1
             DO ji = 2, jpim1
-               DO jk = 1, jpkm1
+               DO jk = 1, mbkmax(ji,jj)-1 ! jpkm1
 #else
          DO jk = 1, jpkm1
@@ -893,5 +909,5 @@
          DO jj = 2, jpjm1
             DO ji = 2, jpim1
-               DO jk = 1, jpkm1
+               DO jk = 1, mbkmax(ji,jj)-1 ! jpkm1
 #else
          DO jk = 1, jpkm1
@@ -908,5 +924,5 @@
          DO jj = 2, jpjm1
             DO ji = 2, jpim1
-               DO jk = 1, jpkm1
+               DO jk = 1, mbkmax(ji,jj)-1 ! jpkm1
 #else
          DO jk = 1, jpkm1
@@ -926,5 +942,5 @@
          DO jj = 2, jpjm1
             DO ji = 2, jpim1
-                DO jk = 1, jpkm1
+                DO jk = 1, mbkmax(ji,jj)-1 ! jpkm1
 #else
          DO jk = 1, jpkm1
@@ -944,5 +960,5 @@
       DO jj = 2, jpjm1
          DO ji = 2, jpim1
-            DO jk = 1, jpkm1 ! Note that this set boundary conditions on mxln at the same time
+            DO jk = 1, mbkmax(ji,jj)-1 ! jpkm1 ! Note that this set boundary conditions on mxln at the same time
 #else
       DO jk = 1, jpkm1 ! Note that this set boundary conditions on mxln at the same time
@@ -970,5 +986,5 @@
          DO jj = 2, jpjm1
             DO ji = 2, jpim1
-               DO jk = 2, jpkm1
+               DO jk = 2, mbkmax(ji,jj)-1 ! jpkm1
 #else
          DO jk = 2, jpkm1
@@ -997,5 +1013,5 @@
          DO jj = 2, jpjm1
             DO ji = 2, jpim1
-               DO jk = 2, jpkm1
+               DO jk = 2, mbkmax(ji,jj)-1 ! jpkm1
 #else
          DO jk = 2, jpkm1
@@ -1049,5 +1065,5 @@
       DO jj = 2, jpjm1
          DO ji = 2, jpim1
-            DO jk = 1, jpk
+            DO jk = 1, mbkmax(ji,jj) ! jpk
 #else
       DO jk = 1, jpk
@@ -1071,5 +1087,5 @@
       DO jj = 2, jpjm1
          DO ji = 2, jpim1
-            DO jk = 2, jpkm1      !* vertical eddy viscosity at u- and v-points
+            DO jk = 2, mbkmax(ji,jj)-1 ! jpkm1      !* vertical eddy viscosity at u- and v-points
 #else
       DO jk = 2, jpkm1            !* vertical eddy viscosity at u- and v-points
