Index: branches/2011/DEV_r2739_STFC_dCSE/NEMOGCM/NEMO/OPA_SRC/TRD/trdmld.F90
===================================================================
--- branches/2011/DEV_r2739_STFC_dCSE/NEMOGCM/NEMO/OPA_SRC/TRD/trdmld.F90	(revision 2740)
+++ branches/2011/DEV_r2739_STFC_dCSE/NEMOGCM/NEMO/OPA_SRC/TRD/trdmld.F90	(revision 3211)
@@ -53,4 +53,13 @@
    INTEGER ::   ionce, icount                   
 
+   !! * Control permutation of array indices
+#  include "oce_ftrans.h90"
+#  include "dom_oce_ftrans.h90"
+#  include "trdmld_oce_ftrans.h90"
+#  include "ldftra_oce_ftrans.h90"
+#  include "zdf_oce_ftrans.h90"
+#  include "ldfslp_ftrans.h90"
+#  include "zdfddm_ftrans.h90"
+
    !! * Substitutions
 #  include "domzgr_substitute.h90"
@@ -98,6 +107,12 @@
       INTEGER                         , INTENT( in ) ::   ktrd       ! ocean trend index
       CHARACTER(len=2)                , INTENT( in ) ::   ctype      ! 2D surface/bottom or 3D interior physics
-      REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT( in ) ::   pttrdmld   ! temperature trend 
-      REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT( in ) ::   pstrdmld   ! salinity trend 
+
+      !! DCSE_NEMO: This style defeats ftrans
+!     REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT( in ) ::   pttrdmld   ! temperature trend 
+!     REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT( in ) ::   pstrdmld   ! salinity trend 
+
+!FTRANS pttrdmld pstrdmld :I :I :z
+      REAL(wp), INTENT( in ) ::   pttrdmld(jpi,jpj,jpk)   ! temperature trend 
+      REAL(wp), INTENT( in ) ::   pstrdmld(jpi,jpj,jpk)   ! salinity trend 
       !
       INTEGER ::   ji, jj, jk, isum
@@ -160,8 +175,15 @@
          ! ... Weights for vertical averaging
          wkx(:,:,:) = 0.e0
+#if defined key_z_first
+         DO jj = 1,jpj                 ! initialize wkx with vertical scale factor in mixed-layer
+            DO ji = 1,jpi
+               DO jk = 1, jpktrd
+                  IF( jk < nmld(ji,jj) )          wkx(ji,jj,jk) = fse3t(ji,jj,jk) * tmask(ji,jj,jk)
+#else
          DO jk = 1, jpktrd             ! initialize wkx with vertical scale factor in mixed-layer
             DO jj = 1,jpj
                DO ji = 1,jpi
                   IF( jk - nmld(ji,jj) < 0.e0 )   wkx(ji,jj,jk) = fse3t(ji,jj,jk) * tmask(ji,jj,jk)
+#endif
                END DO
             END DO
@@ -169,11 +191,31 @@
          
          rmld(:,:) = 0.e0                ! compute mixed-layer depth : rmld
+#if defined key_z_first
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               DO jk = 1, jpktrd
+                  rmld(ji,jj) = rmld(ji,jj) + wkx(ji,jj,jk)
+               END DO
+            END DO
+         END DO
+#else
          DO jk = 1, jpktrd
             rmld(:,:) = rmld(:,:) + wkx(:,:,jk)
          END DO
-         
+#endif
+         
+#if defined key_z_first
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               DO jk = 1, jpktrd             ! compute integration weights
+                  wkx(ji,jj,jk) = wkx(ji,jj,jk) / MAX( 1., rmld(ji,jj) )
+               END DO
+            END DO
+         END DO
+#else
          DO jk = 1, jpktrd             ! compute integration weights
             wkx(:,:,jk) = wkx(:,:,jk) / MAX( 1., rmld(:,:) )
          END DO
+#endif
 
          icount = 0                    ! <<< flag = off : control surface & integr. weights
@@ -186,8 +228,19 @@
       SELECT CASE (ctype)
       CASE ( '3D' )   ! mean T/S trends in the mixed-layer
+#if defined key_z_first
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               DO jk = 1, jpktrd
+                  tmltrd(ji,jj,ktrd) = tmltrd(ji,jj,ktrd) + pttrdmld(ji,jj,jk) * wkx(ji,jj,jk)   ! temperature
+                  smltrd(ji,jj,ktrd) = smltrd(ji,jj,ktrd) + pstrdmld(ji,jj,jk) * wkx(ji,jj,jk)   ! salinity
+               END DO
+            END DO
+         END DO
+#else
          DO jk = 1, jpktrd
             tmltrd(:,:,ktrd) = tmltrd(:,:,ktrd) + pttrdmld(:,:,jk) * wkx(:,:,jk)   ! temperature
             smltrd(:,:,ktrd) = smltrd(:,:,ktrd) + pstrdmld(:,:,jk) * wkx(:,:,jk)   ! salinity
          END DO
+#endif
       CASE ( '2D' )   ! forcing at upper boundary of the mixed-layer
          tmltrd(:,:,ktrd) = tmltrd(:,:,ktrd) + pttrdmld(:,:,1) * wkx(:,:,1)        ! non penetrative
@@ -198,5 +251,15 @@
       !
    END SUBROUTINE trd_mld_zint
-    
+
+   !! * Reset control of array index permutation
+!FTRANS CLEAR
+#  include "oce_ftrans.h90"
+#  include "dom_oce_ftrans.h90"
+#  include "trdmld_oce_ftrans.h90"
+#  include "ldftra_oce_ftrans.h90"
+#  include "zdf_oce_ftrans.h90"
+#  include "ldfslp_ftrans.h90"
+#  include "zdfddm_ftrans.h90"
+
 
    SUBROUTINE trd_mld( kt )
@@ -261,5 +324,11 @@
       LOGICAL :: lldebug = .TRUE.
       REAL(wp) :: zavt, zfn, zfn2
+
+#if defined key_z_first
+      REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  ztmltrd2, zsmltrd2   ! only needed for mean diagnostics
+#else
       REAL(wp), POINTER, DIMENSION(:,:,:) ::  ztmltrd2, zsmltrd2   ! only needed for mean diagnostics
+#endif
+
 #if defined key_dimgout
       INTEGER ::  iyear,imon,iday
@@ -269,4 +338,11 @@
       
       ! Check that the workspace arrays are all OK to be used
+#if defined key_z_first
+      IF( wrk_in_use(2, 1,2,3,4,5,6,7,8,9,10,11,12,13,14) ) THEN
+         CALL ctl_stop('trd_mld : requested workspace arrays unavailable')   ;   RETURN
+      END IF
+      ALLOCATE( ztmltrd2(jpi,jpj,jpltrd) )
+      ALLOCATE( zsmltrd2(jpi,jpj,jpltrd) )
+#else
       IF( wrk_in_use(2, 1,2,3,4,5,6,7,8,9,10,11,12,13,14)  .OR. &
           wrk_in_use(3, 1,2)                                 ) THEN
@@ -280,4 +356,5 @@
       ztmltrd2 => wrk_3d_1(1:,:,1:jpltrd)
       zsmltrd2 => wrk_3d_2(1:,:,1:jpltrd)
+#endif
 
       ! ======================================================================
@@ -333,8 +410,19 @@
       ! --------------------------------
       tml(:,:) = 0.e0   ;   sml(:,:) = 0.e0
+#if defined key_z_first
+      DO jj = 1, jpj
+         DO ji = 1, jpi
+            DO jk = 1, jpktrd - 1
+               tml(ji,jj) = tml(ji,jj) + wkx(ji,jj,jk) * tn(ji,jj,jk)
+               sml(ji,jj) = sml(ji,jj) + wkx(ji,jj,jk) * sn(ji,jj,jk) 
+            END DO
+         END DO
+      END DO
+#else
       DO jk = 1, jpktrd - 1
          tml(:,:) = tml(:,:) + wkx(:,:,jk) * tn(:,:,jk)
          sml(:,:) = sml(:,:) + wkx(:,:,jk) * sn(:,:,jk) 
       END DO
+#endif
 
       ! II.3 Initialize mixed-layer "before" arrays for the 1rst analysis window    
@@ -740,7 +828,13 @@
       IF( lrst_oce )   CALL trd_mld_rst_write( kt ) 
 
+#if defined key_z_first
+      IF( wrk_not_released(2, 1,2,3,4,5,6,7,8,9,10,11,12,13,14) )   &
+          CALL ctl_stop('trd_mld : failed to release workspace arrays.')
+      DEALLOCATE( ztmltrd2, zsmltrd2 )
+#else
       IF( wrk_not_released(2, 1,2,3,4,5,6,7,8,9,10,11,12,13,14)  .OR. &
           wrk_not_released(3, 1,2)                                )   &
           CALL ctl_stop('trd_mld : failed to release workspace arrays.')
+#endif
       !
    END SUBROUTINE trd_mld
