Index: branches/UKMO/dev_r5107_hadgem3_cplseq/NEMOGCM/NEMO/OPA_SRC/TRA/traldf_iso.F90
===================================================================
--- branches/UKMO/dev_r5107_hadgem3_cplseq/NEMOGCM/NEMO/OPA_SRC/TRA/traldf_iso.F90	(revision 5477)
+++ branches/UKMO/dev_r5107_hadgem3_cplseq/NEMOGCM/NEMO/OPA_SRC/TRA/traldf_iso.F90	(revision 5572)
@@ -28,8 +28,6 @@
    USE in_out_manager  ! I/O manager
    USE iom             ! I/O library
-#if defined key_diaar5
    USE phycst          ! physical constants
    USE lbclnk          ! ocean lateral boundary conditions (or mpp link)
-#endif
    USE wrk_nemo        ! Memory Allocation
    USE timing          ! Timing
@@ -106,10 +104,8 @@
       !
       INTEGER  ::  ji, jj, jk, jn   ! dummy loop indices
+      INTEGER  ::  ikt
       REAL(wp) ::  zmsku, zabe1, zcof1, zcoef3   ! local scalars
       REAL(wp) ::  zmskv, zabe2, zcof2, zcoef4   !   -      -
       REAL(wp) ::  zcoef0, zbtr, ztra            !   -      -
-#if defined key_diaar5
-      REAL(wp)                         ::   zztmp               ! local scalar
-#endif
       REAL(wp), POINTER, DIMENSION(:,:  ) ::  z2d
       REAL(wp), POINTER, DIMENSION(:,:,:) ::  zdkt, zdk1t, zdit, zdjt, ztfw 
@@ -149,43 +145,59 @@
             END DO
          END DO
+
+         ! partial cell correction
          IF( ln_zps ) THEN      ! partial steps correction at the last ocean level 
             DO jj = 1, jpjm1
                DO ji = 1, fs_jpim1   ! vector opt.
 ! IF useless if zpshde defines pgu everywhere
-                  IF (mbku(ji,jj) > 1) zdit(ji,jj,mbku(ji,jj)) = pgu(ji,jj,jn)          
-                  IF (mbkv(ji,jj) > 1) zdjt(ji,jj,mbkv(ji,jj)) = pgv(ji,jj,jn)
-                  ! (ISF)
+                  zdit(ji,jj,mbku(ji,jj)) = pgu(ji,jj,jn)          
+                  zdjt(ji,jj,mbkv(ji,jj)) = pgv(ji,jj,jn)
+               END DO
+            END DO
+         ENDIF
+         IF( ln_zps .AND. ln_isfcav ) THEN      ! partial steps correction at the first wet level beneath a cavity
+            DO jj = 1, jpjm1
+               DO ji = 1, fs_jpim1   ! vector opt.
                   IF (miku(ji,jj) > 1) zdit(ji,jj,miku(ji,jj)) = pgui(ji,jj,jn)          
                   IF (mikv(ji,jj) > 1) zdjt(ji,jj,mikv(ji,jj)) = pgvi(ji,jj,jn)     
                END DO
             END DO
-         ENDIF
+         END IF
 
          !!----------------------------------------------------------------------
          !!   II - horizontal trend  (full)
          !!----------------------------------------------------------------------
-!CDIR PARALLEL DO PRIVATE( zdk1t ) 
-         !                                                ! ===============
-         DO jj = 1, jpj                                 ! Horizontal slab
-            !                                             ! ===============
-            DO ji = 1, jpi   ! vector opt.
-               DO jk = mikt(ji,jj), jpkm1
-               ! 1. Vertical tracer gradient at level jk and jk+1
-               ! ------------------------------------------------
-               ! surface boundary condition: zdkt(jk=1)=zdkt(jk=2)
-                  zdk1t(ji,jj,jk) = ( ptb(ji,jj,jk,jn) - ptb(ji,jj,jk+1,jn) ) * tmask(ji,jj,jk+1)
-               !
-                  IF( jk == mikt(ji,jj) ) THEN  ;   zdkt(ji,jj,jk) = zdk1t(ji,jj,jk)
-                  ELSE                          ;   zdkt(ji,jj,jk) = ( ptb(ji,jj,jk-1,jn) - ptb(ji,jj,jk,jn) ) * tmask(ji,jj,jk)
-                  ENDIF
+!!!!!!!!!!CDIR PARALLEL DO PRIVATE( zdk1t ) 
+            ! 1. Vertical tracer gradient at level jk and jk+1
+            ! ------------------------------------------------
+         ! 
+         ! interior value 
+         DO jk = 2, jpkm1               
+            DO jj = 1, jpj
+               DO ji = 1, jpi   ! vector opt.
+                  zdk1t(ji,jj,jk) = ( ptb(ji,jj,jk,jn  ) - ptb(ji,jj,jk+1,jn) ) * wmask(ji,jj,jk+1)
+                  !
+                  zdkt(ji,jj,jk)  = ( ptb(ji,jj,jk-1,jn) - ptb(ji,jj,jk,jn  ) ) * wmask(ji,jj,jk)
                END DO
             END DO
          END DO
-
-            ! 2. Horizontal fluxes
-            ! --------------------   
-         DO jj = 1 , jpjm1
-            DO ji = 1, fs_jpim1   ! vector opt.
-               DO jk = mikt(ji,jj), jpkm1
+         ! surface boundary condition: zdkt(jk=1)=zdkt(jk=2)
+         zdk1t(:,:,1) = ( ptb(:,:,1,jn  ) - ptb(:,:,2,jn) ) * wmask(:,:,2)
+         zdkt (:,:,1) = zdk1t(:,:,1)
+         IF ( ln_isfcav ) THEN
+            DO jj = 1, jpj
+               DO ji = 1, jpi   ! vector opt.
+                  ikt = mikt(ji,jj) ! surface level
+                  zdk1t(ji,jj,ikt) = ( ptb(ji,jj,ikt,jn  ) - ptb(ji,jj,ikt+1,jn) ) * wmask(ji,jj,ikt+1)
+                  zdkt (ji,jj,ikt) = zdk1t(ji,jj,ikt)
+               END DO
+            END DO
+         END IF
+
+         ! 2. Horizontal fluxes
+         ! --------------------   
+         DO jk = 1, jpkm1
+            DO jj = 1 , jpjm1
+               DO ji = 1, fs_jpim1   ! vector opt.
                   zabe1 = ( fsahtu(ji,jj,jk) + pahtb0 ) * re2u_e1u(ji,jj) * fse3u_n(ji,jj,jk)
                   zabe2 = ( fsahtv(ji,jj,jk) + pahtb0 ) * re1v_e2v(ji,jj) * fse3v_n(ji,jj,jk)
@@ -208,12 +220,10 @@
                END DO
             END DO
-         END DO
 
             ! II.4 Second derivative (divergence) and add to the general trend
             ! ----------------------------------------------------------------
-         DO jj = 2 , jpjm1
-            DO ji = fs_2, fs_jpim1   ! vector opt.
-               DO jk = mikt(ji,jj), jpkm1
-                  zbtr = 1.0 / ( e1t(ji,jj) * e2t(ji,jj) * fse3t(ji,jj,jk) )
+            DO jj = 2 , jpjm1
+               DO ji = fs_2, fs_jpim1   ! vector opt.
+                  zbtr = 1.0 / ( e12t(ji,jj) * fse3t_n(ji,jj,jk) )
                   ztra = zbtr * ( zftu(ji,jj,jk) - zftu(ji-1,jj,jk) + zftv(ji,jj,jk) - zftv(ji,jj-1,jk)  )
                   pta(ji,jj,jk,jn) = pta(ji,jj,jk,jn) + ztra
@@ -225,38 +235,39 @@
          !
          ! "Poleward" diffusive heat or salt transports (T-S case only)
-         IF( cdtype == 'TRA' .AND. ln_diaptr .AND. ( MOD( kt, nn_fptr ) == 0 ) ) THEN
+         IF( cdtype == 'TRA' .AND. ln_diaptr ) THEN
             ! note sign is reversed to give down-gradient diffusive transports (#1043)
-            IF( jn == jp_tem)   htr_ldf(:) = ptr_vj( -zftv(:,:,:) )
-            IF( jn == jp_sal)   str_ldf(:) = ptr_vj( -zftv(:,:,:) )
+            IF( jn == jp_tem)   htr_ldf(:) = ptr_sj( -zftv(:,:,:) )
+            IF( jn == jp_sal)   str_ldf(:) = ptr_sj( -zftv(:,:,:) )
          ENDIF
  
-#if defined key_diaar5
-         IF( cdtype == 'TRA' .AND. jn == jp_tem  ) THEN
-            z2d(:,:) = 0._wp 
-            ! note sign is reversed to give down-gradient diffusive transports (#1043)
-            zztmp = -1.0_wp * rau0 * rcp
-            DO jk = 1, jpkm1
-               DO jj = 2, jpjm1
-                  DO ji = fs_2, fs_jpim1   ! vector opt.
-                     z2d(ji,jj) = z2d(ji,jj) + zftu(ji,jj,jk) 
+         IF( iom_use("udiff_heattr") .OR. iom_use("vdiff_heattr") ) THEN
+           !
+           IF( cdtype == 'TRA' .AND. jn == jp_tem  ) THEN
+               z2d(:,:) = 0._wp 
+               DO jk = 1, jpkm1
+                  DO jj = 2, jpjm1
+                     DO ji = fs_2, fs_jpim1   ! vector opt.
+                        z2d(ji,jj) = z2d(ji,jj) + zftu(ji,jj,jk) 
+                     END DO
                   END DO
                END DO
-            END DO
-            z2d(:,:) = zztmp * z2d(:,:)
-            CALL lbc_lnk( z2d, 'U', -1. )
-            CALL iom_put( "udiff_heattr", z2d )                  ! heat transport in i-direction
-            z2d(:,:) = 0._wp 
-            DO jk = 1, jpkm1
-               DO jj = 2, jpjm1
-                  DO ji = fs_2, fs_jpim1   ! vector opt.
-                     z2d(ji,jj) = z2d(ji,jj) + zftv(ji,jj,jk) 
+               z2d(:,:) = - rau0_rcp * z2d(:,:)     ! note sign is reversed to give down-gradient diffusive transports (#1043)
+               CALL lbc_lnk( z2d, 'U', -1. )
+               CALL iom_put( "udiff_heattr", z2d )                  ! heat transport in i-direction
+               !
+               z2d(:,:) = 0._wp 
+               DO jk = 1, jpkm1
+                  DO jj = 2, jpjm1
+                     DO ji = fs_2, fs_jpim1   ! vector opt.
+                        z2d(ji,jj) = z2d(ji,jj) + zftv(ji,jj,jk) 
+                     END DO
                   END DO
                END DO
-            END DO
-            z2d(:,:) = zztmp * z2d(:,:)
-            CALL lbc_lnk( z2d, 'V', -1. )
-            CALL iom_put( "vdiff_heattr", z2d )                  !  heat transport in i-direction
-         END IF
-#endif
+               z2d(:,:) = - rau0_rcp * z2d(:,:)     ! note sign is reversed to give down-gradient diffusive transports (#1043)
+               CALL lbc_lnk( z2d, 'V', -1. )
+               CALL iom_put( "vdiff_heattr", z2d )                  !  heat transport in i-direction
+            END IF
+            !
+         ENDIF
 
          !!----------------------------------------------------------------------
@@ -278,5 +289,5 @@
             DO jj = 2, jpjm1
                DO ji = fs_2, fs_jpim1   ! vector opt.
-                  zcoef0 = - fsahtw(ji,jj,jk) * tmask(ji,jj,jk) * tmask(ji,jj,jk-1)
+                  zcoef0 = - fsahtw(ji,jj,jk) * wmask(ji,jj,jk)
                   !
                   zmsku = 1./MAX(   umask(ji  ,jj,jk-1) + umask(ji-1,jj,jk)      &
