Index: /branches/2011/DEV_r2739_STFC_dCSE/NEMOGCM/NEMO/OPA_SRC/DYN/dynnxt.F90
===================================================================
--- /branches/2011/DEV_r2739_STFC_dCSE/NEMOGCM/NEMO/OPA_SRC/DYN/dynnxt.F90	(revision 4454)
+++ /branches/2011/DEV_r2739_STFC_dCSE/NEMOGCM/NEMO/OPA_SRC/DYN/dynnxt.F90	(revision 4455)
@@ -152,4 +152,19 @@
          END DO
       ELSE                                            ! applied on thickness weighted velocity
+
+#if defined key_z_first
+         DO jj = 1, jpj, 1
+            DO ji = 1, jpi, 1
+               DO jk = 1, mbkmax(ji,jj)-1 ! jpkm1
+                  ua(ji,jj,jk) = ( ub(ji,jj,jk) * fse3u_b(ji,jj,jk)      &
+               &           + z2dt * ua(ji,jj,jk) * fse3u_n(ji,jj,jk)  )   &
+               &         / fse3u_a(ji,jj,jk) * umask(ji,jj,jk)
+                  va(ji,jj,jk) = ( vb(ji,jj,jk) * fse3v_b(ji,jj,jk)      &
+               &           + z2dt * va(ji,jj,jk) * fse3v_n(ji,jj,jk)  )   &
+               &         / fse3v_a(ji,jj,jk) * vmask(ji,jj,jk)
+               END DO
+            END DO
+         END DO
+#else
          DO jk = 1, jpkm1
             ua(:,:,jk) = (          ub(:,:,jk) * fse3u_b(:,:,jk)      &
@@ -160,4 +175,5 @@
                &         / fse3v_a(:,:,jk) * vmask(:,:,jk)
          END DO
+#endif
       ENDIF
 
@@ -238,5 +254,5 @@
          DO jj = 1, jpj
             DO ji = 1, jpi
-               DO jk = 1, jpkm1
+               DO jk = 1, mbkmax(ji,jj)-1 ! jpkm1
                   un(ji,jj,jk) = ua(ji,jj,jk)                ! un <-- ua
                   vn(ji,jj,jk) = va(ji,jj,jk)
@@ -257,5 +273,5 @@
             DO jj = 1, jpj
                DO ji = 1, jpi    
-                  DO jk = 1, jpkm1                              
+                  DO jk = 1, mbkmax(ji,jj)-1 ! jpkm1                              
 #else
             DO jk = 1, jpkm1                              
@@ -278,4 +294,15 @@
             ! Before scale factor at t-points
             ! -------------------------------
+#if defined key_z_first
+            DO jj = 1, jpj
+               DO ji = 1, jpi    
+                  DO jk = 1, mbkmax(ji,jj)-1 ! jpkm1
+                     fse3t_b(ji,jj,jk) = fse3t_n(ji,jj,jk)                             &
+                  &              + atfp * (  fse3t_b(ji,jj,jk) + fse3t_a(ji,jj,jk)     &
+                  &                         - 2.e0 * fse3t_n(ji,jj,jk)            )
+                  END DO
+               END DO
+            ENDDO
+#else
             DO jk = 1, jpkm1
                fse3t_b(:,:,jk) = fse3t_n(:,:,jk)                                   &
@@ -283,4 +310,5 @@
                   &                         - 2.e0 * fse3t_n(:,:,jk)            )
             ENDDO
+#endif
             ! Add volume filter correction only at the first level of t-point scale factors
             zec = atfp * rdt / rau0
@@ -302,5 +330,5 @@
                DO jj = 1, jpjm1
                   DO ji = 1, jpim1
-                     DO jk = 1, jpkm1
+                     DO jk = 1, mbkmax(ji,jj)-1 ! jpkm1
 #else
                DO jk = 1, jpkm1
@@ -320,6 +348,17 @@
                CALL lbc_lnk( fse3v_b(:,:,:), 'V', 1. )
                ! Add initial scale factor to scale factor anomaly
+#if defined key_z_first
+               DO jj = 1, jpjm1
+                  DO ji = 1, jpim1
+                     DO jk = 1, mbkmax(ji,jj)-1 ! jpkm1
+                        fse3u_b(ji,jj,jk) = fse3u_b(ji,jj,jk) + fse3u_0(ji,jj,jk)
+                        fse3v_b(ji,jj,jk) = fse3v_b(ji,jj,jk) + fse3v_0(ji,jj,jk)
+                     END DO
+                  END DO
+               END DO
+#else
                fse3u_b(:,:,:) = fse3u_b(:,:,:) + fse3u_0(:,:,:)
                fse3v_b(:,:,:) = fse3v_b(:,:,:) + fse3v_0(:,:,:)
+#endif
                ! Leap-Frog - Asselin filter and swap: applied on velocity
                ! -----------------------------------
@@ -327,5 +366,5 @@
                DO jj = 1, jpj
                   DO ji = 1, jpi
-                     DO jk = 1, jpkm1
+                     DO jk = 1, mbkmax(ji,jj)-1 ! jpkm1
 #else
                DO jk = 1, jpkm1
@@ -351,5 +390,5 @@
                DO jj = 1, jpjm1
                   DO ji = 1, jpim1
-                     DO jk = 1, jpkm1
+                     DO jk = 1, mbkmax(ji,jj)-1 ! jpkm1
 #else
                DO jk = 1, jpkm1
@@ -369,6 +408,17 @@
                CALL lbc_lnk( ze3v_f, 'V', 1. )
                ! Add initial scale factor to scale factor anomaly
+#if defined key_z_first
+               DO jj = 1, jpjm1
+                  DO ji = 1, jpim1
+                     DO jk = 1, mbkmax(ji,jj)-1 ! jpkm1
+                        ze3u_f(ji,jj,jk) = ze3u_f(ji,jj,jk) + fse3u_0(ji,jj,jk)
+                        ze3v_f(ji,jj,jk) = ze3v_f(ji,jj,jk) + fse3v_0(ji,jj,jk)
+                     END DO
+                  END DO
+               END DO
+#else
                ze3u_f(:,:,:) = ze3u_f(:,:,:) + fse3u_0(:,:,:)
                ze3v_f(:,:,:) = ze3v_f(:,:,:) + fse3v_0(:,:,:)
+#endif
                ! Leap-Frog - Asselin filter and swap: applied on thickness weighted velocity
                ! -----------------------------------             ===========================
@@ -376,5 +426,5 @@
                DO jj = 1, jpj
                   DO ji = 1, jpim1
-                     DO jk = 1, jpkm1
+                     DO jk = 1, mbkmax(ji,jj)-1 ! jpkm1
 #else
                DO jk = 1, jpkm1
@@ -399,6 +449,17 @@
                   END DO
                END DO
+#if defined key_z_first
+               DO jj = 1, jpj
+                  DO ji = 1, jpim1
+                     DO jk = 1, mbkmax(ji,jj)-1 ! jpkm1
+                        fse3u_b(ji,jj,jk) = ze3u_f(ji,jj,jk)                   ! e3u_b <-- filtered scale factor
+                        fse3v_b(ji,jj,jk) = ze3v_f(ji,jj,jk)
+                     END DO
+                  END DO
+               END DO
+#else
                fse3u_b(:,:,:) = ze3u_f(:,:,:)                   ! e3u_b <-- filtered scale factor
                fse3v_b(:,:,:) = ze3v_f(:,:,:)
+#endif
                CALL lbc_lnk( ub, 'U', -1. )                     ! lateral boundary conditions
                CALL lbc_lnk( vb, 'V', -1. )
