Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdy_oce.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdy_oce.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdy_oce.F90	(revision 7567)
@@ -71,4 +71,11 @@
       REAL, POINTER, DIMENSION(:,:)   ::  ht_s  !: now snow thickness
 #endif
+#if defined key_top
+      CHARACTER(LEN=20)                   :: cn_obc  !: type of boundary condition to apply
+      REAL(wp)                            :: rn_fac  !: multiplicative scaling factor
+      REAL(wp), POINTER, DIMENSION(:,:)   :: trc     !: now field of the tracer
+      LOGICAL                             :: dmp     !: obc damping term
+#endif
+
    END TYPE OBC_DATA
 
@@ -83,4 +90,7 @@
    LOGICAL                    ::   ln_mask_file             !: =T read bdymask from file
    LOGICAL                    ::   ln_vol                   !: =T volume correction             
+   !JT
+   LOGICAL, DIMENSION(jp_bdy) ::   ln_sponge                !: =T use sponge layer 
+   !JT
    !
    INTEGER                    ::   nb_bdy                   !: number of open boundary sets
@@ -101,6 +111,14 @@
    LOGICAL, DIMENSION(jp_bdy) ::   ln_tra_dmp               !: =T Tracer damping
    LOGICAL, DIMENSION(jp_bdy) ::   ln_dyn3d_dmp             !: =T Baroclinic velocity damping
+   
+!   !JT
+   LOGICAL, DIMENSION(jp_bdy) ::   ln_ssh_bdy               !: =T USE SSH BDY - name list switch
+!   !JT
+   
    REAL(wp),    DIMENSION(jp_bdy) ::   rn_time_dmp              !: Damping time scale in days
    REAL(wp),    DIMENSION(jp_bdy) ::   rn_time_dmp_out          !: Damping time scale in days at radiation outflow points
+   !JT
+   REAL(wp)                   ::   rn_sponge                  !: multiplier of diffusion for sponge layer
+   !JT
 
    CHARACTER(len=20), DIMENSION(jp_bdy) ::   cn_ice_lim       ! Choice of boundary condition for sea ice variables 
@@ -118,4 +136,7 @@
    REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:), TARGET ::   bdyumask   !: Mask defining computational domain at U-points
    REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:), TARGET ::   bdyvmask   !: Mask defining computational domain at V-points
+   !JT
+   REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:) ::   sponge_factor !: Multiplier for diffusion for sponge layer
+   !JT
 
    REAL(wp)                                    ::   bdysurftot !: Lateral surface of unstructured open boundary
@@ -147,5 +168,6 @@
       !!----------------------------------------------------------------------
       !
-      ALLOCATE( bdytmask(jpi,jpj) , bdyumask(jpi,jpj), bdyvmask(jpi,jpj),     &  
+      !JT ALLOCATE( bdytmask(jpi,jpj) , bdyumask(jpi,jpj), bdyvmask(jpi,jpj),     &  
+      ALLOCATE( bdytmask(jpi,jpj) , bdyumask(jpi,jpj), bdyvmask(jpi,jpj),sponge_factor(jpi,jpj),     &     
          &      STAT=bdy_oce_alloc )
       !
@@ -154,4 +176,7 @@
       bdyumask(:,:) = 1._wp
       bdyvmask(:,:) = 1._wp
+      !JT
+      sponge_factor(:,:) = 1._wp
+      !JT
       ! 
       IF( lk_mpp             )   CALL mpp_sum ( bdy_oce_alloc )
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdydta.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdydta.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdydta.F90	(revision 7567)
@@ -37,4 +37,8 @@
 #endif
    USE sbcapr
+#if defined key_top
+   USE par_trc
+   USE trc, ONLY: trn
+#endif
 
    IMPLICIT NONE
@@ -394,4 +398,23 @@
 #endif
       ! end jchanut tschanges
+      
+      
+      !JT use sshn (ssh now) if ln_ssh_bdy set to false in the name list
+      DO ib_bdy = 1, nb_bdy   
+        nblen => idx_bdy(ib_bdy)%nblen
+        dta => dta_bdy(ib_bdy)
+         
+        ilen1(:) = nblen(:)
+        !JT IF( .NOT. dta%ll_ssh ) THEN 
+        IF( .NOT. ln_ssh_bdy(ib_bdy) ) THEN 
+          igrd = 1 ! t Grid
+          DO ib = 1, ilen1(igrd)
+              ii = idx_bdy(ib_bdy)%nbi(ib,igrd)
+              ij = idx_bdy(ib_bdy)%nbj(ib,igrd)
+              dta_bdy(ib_bdy)%ssh(ib) = sshn(ii,ij) * tmask(ii,ij,1)         
+          END DO 
+        END IF
+      END DO 
+      !JT
 
       IF ( ln_apr_obc ) THEN
@@ -782,10 +805,26 @@
             IF( dta%ll_v2d ) ALLOCATE( dta%v2d(nblen(3)) )
          ENDIF
-         IF ( nn_dyn2d_dta(ib_bdy) .eq. 1 .or. nn_dyn2d_dta(ib_bdy) .eq. 3 ) THEN
-            IF( dta%ll_ssh ) THEN
-               if(lwp) write(numout,*) '++++++ dta%ssh pointing to fnow'
-               jfld = jfld + 1
-               dta%ssh => bf(jfld)%fnow(:,1,1)
-            ENDIF
+         IF ( nn_dyn2d_dta(ib_bdy) .eq. 1 .or. nn_dyn2d_dta(ib_bdy) .eq. 3 ) THEN         
+            !JT IF( dta%ll_ssh ) THEN
+            !JT    if(lwp) write(numout,*) '++++++ dta%ssh pointing to fnow'
+            !JT    jfld = jfld + 1
+            !JT    dta%ssh => bf(jfld)%fnow(:,1,1)   
+            !JT ENDIF
+            
+            !JT 
+            !JT allocate ssh if dta%ll_ssh set too false, as may still use it
+            IF (dta%ll_ssh) THEN
+                IF( dta%ll_ssh ) THEN
+                  if(lwp) write(numout,*) '++++++ dta%ssh pointing to fnow'
+                  jfld = jfld + 1
+                  dta%ssh => bf(jfld)%fnow(:,1,1)
+                ENDIF
+            ELSE
+              if(lwp) write(numout,*) '++++++ dta%ssh allocated space'
+              !ALLOCATE( dta_bdy(ib_bdy)%ssh(nblen(1)) )            
+              ALLOCATE( dta%ssh(nblen(1)) )            
+            ENDIF
+            !JT if 
+            
             IF ( dta%ll_u2d ) THEN
                IF ( ln_full_vel_array(ib_bdy) ) THEN
@@ -814,5 +853,5 @@
             IF( dta%ll_u3d ) ALLOCATE( dta_bdy(ib_bdy)%u3d(nblen(2),jpk) )
             IF( dta%ll_v3d ) ALLOCATE( dta_bdy(ib_bdy)%v3d(nblen(3),jpk) )
-         ENDIF
+         ENDIF        
          IF ( nn_dyn3d_dta(ib_bdy) .eq. 1 .or. &
            &  ( ln_full_vel_array(ib_bdy) .and. ( nn_dyn2d_dta(ib_bdy) .eq. 1 .or. nn_dyn2d_dta(ib_bdy) .eq. 3 ) ) ) THEN
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdydyn3d.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdydyn3d.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdydyn3d.F90	(revision 7567)
@@ -33,5 +33,5 @@
    !!----------------------------------------------------------------------
    !! NEMO/OPA 3.3 , NEMO Consortium (2010)
-   !! $Id$
+   !! $Id$ 
    !! Software governed by the CeCILL licence (NEMOGCM/NEMO_CeCILL.txt)
    !!----------------------------------------------------------------------
@@ -59,6 +59,10 @@
          CASE('specified')
             CALL bdy_dyn3d_spe( idx_bdy(ib_bdy), dta_bdy(ib_bdy), kt, ib_bdy )
+         CASE('zerograd') 
+            CALL bdy_dyn3d_zgrad( idx_bdy(ib_bdy), dta_bdy(ib_bdy), kt, ib_bdy )
          CASE('zero')
             CALL bdy_dyn3d_zro( idx_bdy(ib_bdy), dta_bdy(ib_bdy), kt, ib_bdy )
+         CASE('neumann')
+            CALL bdy_dyn3d_nmn( idx_bdy(ib_bdy), ib_bdy )
          CASE('orlanski')
             CALL bdy_dyn3d_orlanski( idx_bdy(ib_bdy), dta_bdy(ib_bdy), ib_bdy, ll_npo=.false. )
@@ -117,4 +121,54 @@
 
    END SUBROUTINE bdy_dyn3d_spe
+
+   SUBROUTINE bdy_dyn3d_zgrad( idx, dta, kt , ib_bdy )
+      !!----------------------------------------------------------------------
+      !!                  ***  SUBROUTINE bdy_dyn3d_zgrad  ***
+      !!
+      !! ** Purpose : - Enforce a zero gradient of normal velocity
+      !!
+      !!----------------------------------------------------------------------
+      INTEGER                     ::   kt
+      TYPE(OBC_INDEX), INTENT(in) ::   idx  ! OBC indices
+      TYPE(OBC_DATA),  INTENT(in) ::   dta  ! OBC external data
+      INTEGER,         INTENT(in) ::   ib_bdy  ! BDY set index
+      !!
+      INTEGER  ::   jb, jk         ! dummy loop indices
+      INTEGER  ::   ii, ij, igrd   ! local integers
+      REAL(wp) ::   zwgt           ! boundary weight
+      INTEGER  ::   fu, fv
+      !!----------------------------------------------------------------------
+      !
+      IF( nn_timing == 1 ) CALL timing_start('bdy_dyn3d_zgrad')
+      !
+      igrd = 2                      ! Copying tangential velocity into bdy points
+      DO jb = 1, idx%nblenrim(igrd)
+         DO jk = 1, jpkm1
+            ii   = idx%nbi(jb,igrd)
+            ij   = idx%nbj(jb,igrd)
+            fu   = ABS( ABS (NINT( idx%flagu(jb,igrd) ) ) - 1 )
+            ua(ii,ij,jk) = ua(ii,ij,jk) * REAL( 1 - fu) + ( ua(ii,ij+fu,jk) * umask(ii,ij+fu,jk) &
+                        &+ ua(ii,ij-fu,jk) * umask(ii,ij-fu,jk) ) * umask(ii,ij,jk) * REAL( fu )
+         END DO
+      END DO
+      !
+      igrd = 3                      ! Copying tangential velocity into bdy points
+      DO jb = 1, idx%nblenrim(igrd)
+         DO jk = 1, jpkm1
+            ii   = idx%nbi(jb,igrd)
+            ij   = idx%nbj(jb,igrd)
+            fv   = ABS( ABS (NINT( idx%flagv(jb,igrd) ) ) - 1 )
+            va(ii,ij,jk) = va(ii,ij,jk) * REAL( 1 - fv ) + ( va(ii+fv,ij,jk) * vmask(ii+fv,ij,jk) &
+                        &+ va(ii-fv,ij,jk) * vmask(ii-fv,ij,jk) ) * vmask(ii,ij,jk) * REAL( fv )
+         END DO
+      END DO
+      CALL lbc_bdy_lnk( ua, 'U', -1., ib_bdy )   ! Boundary points should be updated  
+      CALL lbc_bdy_lnk( va, 'V', -1., ib_bdy )   
+      !
+      IF( kt .eq. nit000 ) CLOSE( unit = 102 )
+
+      IF( nn_timing == 1 ) CALL timing_stop('bdy_dyn3d_zgrad')
+
+   END SUBROUTINE bdy_dyn3d_zgrad
 
    SUBROUTINE bdy_dyn3d_zro( idx, dta, kt, ib_bdy )
@@ -303,4 +357,37 @@
    END SUBROUTINE bdy_dyn3d_dmp
 
+   SUBROUTINE bdy_dyn3d_nmn( idx, ib_bdy )
+      !!----------------------------------------------------------------------
+      !!                 ***  SUBROUTINE bdy_dyn3d_nmn  ***
+      !!             
+      !!              - Apply Neumann condition to baroclinic velocities. 
+      !!              - Wrapper routine for bdy_nmn
+      !! 
+      !!
+      !!----------------------------------------------------------------------
+      TYPE(OBC_INDEX),              INTENT(in) ::   idx  ! OBC indices
+      INTEGER,                      INTENT(in) ::   ib_bdy  ! BDY set index
+
+      INTEGER  ::   jb, igrd                               ! dummy loop indices
+      !!----------------------------------------------------------------------
+
+      IF( nn_timing == 1 ) CALL timing_start('bdy_dyn3d_nmn')
+      !
+      !! Note that at this stage the ub and ua arrays contain the baroclinic velocities. 
+      !
+      igrd = 2      ! Neumann bc on u-velocity; 
+      !            
+      CALL bdy_nmn( idx, igrd, ua )
+
+      igrd = 3      ! Neumann bc on v-velocity
+      !  
+      CALL bdy_nmn( idx, igrd, va )
+      !
+      CALL lbc_bdy_lnk( ua, 'U', -1., ib_bdy )    ! Boundary points should be updated
+      CALL lbc_bdy_lnk( va, 'V', -1., ib_bdy )
+      !
+      IF( nn_timing == 1 ) CALL timing_stop('bdy_dyn3d_nmn')
+      !
+   END SUBROUTINE bdy_dyn3d_nmn
 #else
    !!----------------------------------------------------------------------
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdyini.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdyini.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdyini.F90	(revision 7567)
@@ -49,5 +49,5 @@
    !!----------------------------------------------------------------------
    !! NEMO/OPA 4.0 , NEMO Consortium (2011)
-   !! $Id$
+   !! $Id$ 
    !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
    !!----------------------------------------------------------------------
@@ -102,7 +102,12 @@
          &             cn_ice_lim, nn_ice_lim_dta,                           &
          &             rn_ice_tem, rn_ice_sal, rn_ice_age,                 &
-         &             ln_vol, nn_volctl, nn_rimwidth
+         &             ln_vol, nn_volctl, ln_sponge, rn_sponge, nn_rimwidth
       !!
       NAMELIST/nambdy_index/ ctypebdy, nbdyind, nbdybeg, nbdyend
+      
+      
+!      ! JT
+      NAMELIST/nambdy_ssh/ ln_ssh_bdy
+!      ! JT
       INTEGER  ::   ios                 ! Local integer output status for namelist read
       !!----------------------------------------------------------------------
@@ -132,4 +137,39 @@
 902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'nambdy in configuration namelist', lwp )
       IF(lwm) WRITE ( numond, nambdy )
+      
+      
+      
+      
+      
+      
+      
+      
+      
+      !JT Read nambdy_ssh namelist 
+      REWIND( numnam_ref )              ! Namelist nambdy in reference namelist :Unstructured open boundaries  
+      READ  ( numnam_ref, nambdy_ssh, IOSTAT = ios, ERR = 905)
+905   IF( ios /= 0 ) CALL ctl_nam ( ios , 'nambdy_ssh in reference namelist', lwp )
+
+      REWIND( numnam_cfg )              ! Namelist nambdy in configuration namelist :Unstructured open boundaries
+      READ  ( numnam_cfg, nambdy_ssh, IOSTAT = ios, ERR = 906)
+906   IF( ios /= 0 ) CALL ctl_nam ( ios , 'nambdy_ssh in configuration namelist', lwp )
+      IF(lwm) WRITE ( numond, nambdy_ssh )
+      
+      IF(lwp) WRITE(numout,*)
+      IF(lwp) WRITE(numout,*) 'nambdy_ssh : use of ssh boundaries'
+      IF(lwp) WRITE(numout,*) '~~~~~~~~'
+      IF(lwp) WRITE(numout,*) '      ln_ssh_bdy: '
+      DO ib_bdy = 1,nb_bdy
+        IF(lwp) WRITE(numout,*) '      ln_ssh_bdy(',ib_bdy,'): ',ln_ssh_bdy(ib_bdy)
+      ENDDO
+      IF(lwp) WRITE(numout,*) '~~~~~~~~'
+      IF(lwp) WRITE(numout,*) 
+      !JT
+
+
+
+
+
+
 
       ! -----------------------------------------
@@ -185,4 +225,20 @@
           CASE DEFAULT   ;   CALL ctl_stop( 'unrecognised value for cn_dyn2d' )
         END SELECT
+        
+        !JT override dta_bdy(ib_bdy)%ll_ssh with namelist value (ln_ssh_bdy)
+        IF(lwp) WRITE(numout,*) 'nambdy_ssh : use of ssh boundaries'
+        IF(lwp) WRITE(numout,*) '~~~~~~~~'
+        IF(lwp) WRITE(numout,*) '      ib_bdy: ',ib_bdy
+        IF(lwp) WRITE(numout,*) '      Prior to Implementation of nambdy_ssh'
+        IF(lwp) WRITE(numout,*) '      dta_bdy(ib_bdy)%ll_ssh: ',dta_bdy(ib_bdy)%ll_ssh
+        
+        dta_bdy(ib_bdy)%ll_ssh = ln_ssh_bdy(ib_bdy)
+        
+        IF(lwp) WRITE(numout,*) '      After to Implementation of nambdy_ssh'
+        IF(lwp) WRITE(numout,*) '      dta_bdy(ib_bdy)%ll_ssh: ',dta_bdy(ib_bdy)%ll_ssh
+        IF(lwp) WRITE(numout,*) '~~~~~~~~'
+        
+        !JT         
+        
         IF( cn_dyn2d(ib_bdy) /= 'none' ) THEN
            SELECT CASE( nn_dyn2d_dta(ib_bdy) )                   ! 
@@ -213,4 +269,12 @@
              dta_bdy(ib_bdy)%ll_u3d = .true.
              dta_bdy(ib_bdy)%ll_v3d = .true.
+          CASE('neumann')
+             IF(lwp) WRITE(numout,*) '      Neumann conditions'
+             dta_bdy(ib_bdy)%ll_u3d = .false.
+             dta_bdy(ib_bdy)%ll_v3d = .false.
+          CASE('zerograd')
+             IF(lwp) WRITE(numout,*) '      Zero gradient for baroclinic velocities'
+             dta_bdy(ib_bdy)%ll_u3d = .false.
+             dta_bdy(ib_bdy)%ll_v3d = .false.
           CASE('zero')
              IF(lwp) WRITE(numout,*) '      Zero baroclinic velocities (runoff case)'
@@ -365,4 +429,16 @@
         IF(lwp) WRITE(numout,*) '      Width of relaxation zone = ', nn_rimwidth(ib_bdy)
         IF(lwp) WRITE(numout,*)
+        
+        IF( ln_sponge(ib_bdy) ) THEN                     ! check sponge layer choice 
+          IF(lwp) WRITE(numout,*) '      Sponge layer applied at open boundaries' 
+          IF(lwp) WRITE(numout,*) '      Multiplier for diffusion in sponge layer : ', rn_sponge 
+          IF(lwp) WRITE(numout,*) 
+        ELSE 
+          IF(lwp) WRITE(numout,*) '      No Sponge layer applied at open boundaries' 
+          IF(lwp) WRITE(numout,*) 
+        ENDIF 
+ 
+        
+        
 
       ENDDO
@@ -1092,4 +1168,20 @@
             END DO
          END DO 
+          
+         
+          !JT
+         ! Compute multiplier for diffusion for sponge layer 
+         ! ------------------------------------------------- 
+         IF( ln_sponge(ib_bdy) ) THEN 
+            igrd=1
+            DO ib = 1, idx_bdy(ib_bdy)%nblen(igrd) 
+               nbi => idx_bdy(ib_bdy)%nbi(ib,igrd) 
+               nbj => idx_bdy(ib_bdy)%nbj(ib,igrd) 
+               nbr => idx_bdy(ib_bdy)%nbr(ib,igrd) 
+               sponge_factor(nbi,nbj) = 1.0 + (rn_sponge-1.0) * ( 1.- TANH( FLOAT( nbr - 1 ) *0.5 ) ) 
+            END DO 
+         ENDIF 
+          !JT
+
 
          ! Compute damping coefficients
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdylib.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdylib.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdylib.F90	(revision 7567)
@@ -26,8 +26,9 @@
    PUBLIC   bdy_orlanski_2d     ! routine called where?
    PUBLIC   bdy_orlanski_3d     ! routine called where?
+   PUBLIC   bdy_nmn     ! routine called where?
 
    !!----------------------------------------------------------------------
    !! NEMO/OPA 3.3 , NEMO Consortium (2010)
-   !! $Id$
+   !! $Id$ 
    !! Software governed by the CeCILL licence (NEMOGCM/NEMO_CeCILL.txt)
    !!----------------------------------------------------------------------
@@ -354,4 +355,76 @@
    END SUBROUTINE bdy_orlanski_3d
 
+   SUBROUTINE bdy_nmn( idx, igrd, phia )
+      !!----------------------------------------------------------------------
+      !!                 ***  SUBROUTINE bdy_nmn  ***
+      !!                    
+      !! ** Purpose : Duplicate the value at open boundaries, zero gradient.
+      !! 
+      !!----------------------------------------------------------------------
+      INTEGER,                    INTENT(in)     ::   igrd     ! grid index
+      REAL(wp), DIMENSION(:,:,:), INTENT(inout)  ::   phia     ! model after 3D field (to be updated)
+      TYPE(OBC_INDEX), INTENT(in) ::   idx  ! OBC indices
+      !! 
+      REAL(wp) ::   zcoef, zcoef1, zcoef2
+      REAL(wp), POINTER, DIMENSION(:,:,:)        :: pmask      ! land/sea mask for field
+      REAL(wp), POINTER, DIMENSION(:,:)        :: bdypmask      ! land/sea mask for field
+      INTEGER  ::   ib, ik   ! dummy loop indices
+      INTEGER  ::   ii, ij, ip, jp   ! 2D addresses
+      !!----------------------------------------------------------------------
+      !
+      IF( nn_timing == 1 ) CALL timing_start('bdy_nmn')
+      !
+      SELECT CASE(igrd)
+         CASE(1)
+            pmask => tmask(:,:,:)
+            bdypmask => bdytmask(:,:)
+         CASE(2)
+            pmask => umask(:,:,:)
+            bdypmask => bdyumask(:,:)
+         CASE(3)
+            pmask => vmask(:,:,:)
+            bdypmask => bdyvmask(:,:)
+         CASE DEFAULT ;   CALL ctl_stop( 'unrecognised value for igrd in bdy_nmn' )
+      END SELECT
+      DO ib = 1, idx%nblenrim(igrd)
+         ii = idx%nbi(ib,igrd)
+         ij = idx%nbj(ib,igrd)
+         DO ik = 1, jpkm1
+            ! search the sense of the gradient
+            zcoef1 = bdypmask(ii-1,ij  )*pmask(ii-1,ij,ik) +  bdypmask(ii+1,ij  )*pmask(ii+1,ij,ik)
+            zcoef2 = bdypmask(ii  ,ij-1)*pmask(ii,ij-1,ik) +  bdypmask(ii  ,ij+1)*pmask(ii,ij+1,ik)
+            IF ( nint(zcoef1+zcoef2) == 0) THEN
+               ! corner **** we probably only want to set the tangentail component for the dynamics here
+               zcoef = pmask(ii-1,ij,ik) + pmask(ii+1,ij,ik) +  pmask(ii,ij-1,ik) +  pmask(ii,ij+1,ik)
+               IF (zcoef > .5_wp) THEN ! Only set none isolated points.
+                 phia(ii,ij,ik) = phia(ii-1,ij  ,ik) * pmask(ii-1,ij  ,ik) + &
+                   &              phia(ii+1,ij  ,ik) * pmask(ii+1,ij  ,ik) + &
+                   &              phia(ii  ,ij-1,ik) * pmask(ii  ,ij-1,ik) + &
+                   &              phia(ii  ,ij+1,ik) * pmask(ii  ,ij+1,ik)
+                 phia(ii,ij,ik) = ( phia(ii,ij,ik) / zcoef ) * pmask(ii,ij,ik)
+               ELSE
+                 phia(ii,ij,ik) = phia(ii,ij  ,ik) * pmask(ii,ij  ,ik)
+               ENDIF
+            ELSEIF ( nint(zcoef1+zcoef2) == 2) THEN
+               ! oblique corner **** we probably only want to set the normal component for the dynamics here
+               zcoef = pmask(ii-1,ij,ik)*bdypmask(ii-1,ij  ) + pmask(ii+1,ij,ik)*bdypmask(ii+1,ij  ) + &
+                   &   pmask(ii,ij-1,ik)*bdypmask(ii,ij -1 ) +  pmask(ii,ij+1,ik)*bdypmask(ii,ij+1  )
+               phia(ii,ij,ik) = phia(ii-1,ij  ,ik) * pmask(ii-1,ij  ,ik)*bdypmask(ii-1,ij  ) + &
+                   &            phia(ii+1,ij  ,ik) * pmask(ii+1,ij  ,ik)*bdypmask(ii+1,ij  )  + &
+                   &            phia(ii  ,ij-1,ik) * pmask(ii  ,ij-1,ik)*bdypmask(ii,ij -1 ) + &
+                   &            phia(ii  ,ij+1,ik) * pmask(ii  ,ij+1,ik)*bdypmask(ii,ij+1  )
+ 
+               phia(ii,ij,ik) = ( phia(ii,ij,ik) / MAX(1._wp, zcoef) ) * pmask(ii,ij,ik)
+            ELSE
+               ip = nint(bdypmask(ii+1,ij  )*pmask(ii+1,ij,ik) - bdypmask(ii-1,ij  )*pmask(ii-1,ij,ik))
+               jp = nint(bdypmask(ii  ,ij+1)*pmask(ii,ij+1,ik) - bdypmask(ii  ,ij-1)*pmask(ii,ij-1,ik))
+               phia(ii,ij,ik) = phia(ii+ip,ij+jp,ik) * pmask(ii+ip,ij+jp,ik) * pmask(ii,ij,ik)
+            ENDIF
+         END DO
+      END DO
+      !
+      IF( nn_timing == 1 ) CALL timing_stop('bdy_nmn')
+      !
+   END SUBROUTINE bdy_nmn
 
 #else
@@ -366,4 +439,7 @@
       WRITE(*,*) 'bdy_orlanski_3d: You should not have seen this print! error?', kt
    END SUBROUTINE bdy_orlanski_3d
+   SUBROUTINE bdy_nmn( idx, igrd, phia )      ! Empty routine
+      WRITE(*,*) 'bdy_nmn: You should not have seen this print! error?', kt
+   END SUBROUTINE bdy_nmn
 #endif
 
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdytides.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdytides.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/BDY/bdytides.F90	(revision 7567)
@@ -102,4 +102,5 @@
 
       REWIND(numnam_cfg)
+      REWIND(numnam_ref)   ! slwa
 
       DO ib_bdy = 1, nb_bdy
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/dia25h.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/dia25h.F90	(revision 7567)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/dia25h.F90	(revision 7567)
@@ -0,0 +1,344 @@
+MODULE dia25h 
+   !!======================================================================
+   !!                       ***  MODULE  diaharm  ***
+   !! Harmonic analysis of tidal constituents 
+   !!======================================================================
+   !! History :  3.6  !  2014  (E O'Dea)  Original code
+   !!----------------------------------------------------------------------
+   USE oce             ! ocean dynamics and tracers variables
+   USE dom_oce         ! ocean space and time domain
+   USE diainsitutem, ONLY: rinsitu_t, theta2t
+   USE in_out_manager  ! I/O units
+   USE iom             ! I/0 library
+   USE wrk_nemo        ! working arrays
+#if defined key_zdftke 
+   USE zdftke, ONLY: en
+#endif
+   USE zdf_oce, ONLY: avt, avm
+#if defined key_zdfgls
+   USE zdfgls, ONLY: mxln
+   USE zdf_oce, ONLY: en
+#endif
+
+   IMPLICIT NONE
+   PRIVATE
+
+   LOGICAL , PUBLIC ::   ln_dia25h     !:  25h mean output
+   PUBLIC   dia_25h_init               ! routine called by nemogcm.F90
+   PUBLIC   dia_25h                    ! routine called by diawri.F90
+
+  !! * variables for calculating 25-hourly means
+   REAL(wp),SAVE, ALLOCATABLE,   DIMENSION(:,:,:) ::   tn_25h  , sn_25h, rinsitu_t_25h  
+   REAL(wp),SAVE, ALLOCATABLE,   DIMENSION(:,:)   ::   sshn_25h 
+   REAL(wp),SAVE, ALLOCATABLE,   DIMENSION(:,:,:) ::   un_25h  , vn_25h  , wn_25h
+   REAL(wp),SAVE, ALLOCATABLE,   DIMENSION(:,:,:) ::   avt_25h , avm_25h
+#if defined key_zdfgls || key_zdftke
+   REAL(wp),SAVE, ALLOCATABLE,   DIMENSION(:,:,:) ::   en_25h
+#endif
+#if defined key_zdfgls 
+   REAL(wp),SAVE, ALLOCATABLE,   DIMENSION(:,:,:) ::   rmxln_25h
+#endif
+   INTEGER, SAVE :: cnt_25h     ! Counter for 25 hour means
+
+
+
+   !!----------------------------------------------------------------------
+   !! NEMO/OPA 3.6 , NEMO Consortium (2014)
+   !! $Id$
+   !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
+   !!----------------------------------------------------------------------
+CONTAINS
+
+   SUBROUTINE dia_25h_init 
+      !!---------------------------------------------------------------------------
+      !!                  ***  ROUTINE dia_25h_init  ***
+      !!     
+      !! ** Purpose: Initialization of 25h mean namelist 
+      !!        
+      !! ** Method : Read namelist
+      !!   History
+      !!   3.6  !  08-14  (E. O'Dea) Routine to initialize dia_25h
+      !!---------------------------------------------------------------------------
+      !!
+      INTEGER ::   ios                 ! Local integer output status for namelist read
+      INTEGER ::   ierror              ! Local integer for memory allocation
+      !
+      NAMELIST/nam_dia25h/ ln_dia25h
+      !!----------------------------------------------------------------------
+      !
+      REWIND ( numnam_ref )              ! Read Namelist nam_dia25h in reference namelist : 25hour mean diagnostics
+      READ   ( numnam_ref, nam_dia25h, IOSTAT=ios, ERR= 901 )
+901   IF( ios /= 0 ) CALL ctl_nam ( ios , 'nam_dia25h in reference namelist', lwp )
+
+      REWIND( numnam_cfg )              ! Namelist nam_dia25h in configuration namelist  25hour diagnostics
+      READ  ( numnam_cfg, nam_dia25h, IOSTAT = ios, ERR = 902 )
+902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'nam_dia25h in configuration namelist', lwp )
+      IF(lwm) WRITE ( numond, nam_dia25h )
+
+      IF(lwp) THEN                   ! Control print
+         WRITE(numout,*)
+         WRITE(numout,*) 'dia_25h_init : Output 25 hour mean diagnostics'
+         WRITE(numout,*) '~~~~~~~~~~~~'
+         WRITE(numout,*) 'Namelist nam_dia25h : set 25h outputs '
+         WRITE(numout,*) 'Switch for 25h diagnostics (T) or not (F)  ln_dia25h  = ', ln_dia25h
+      ENDIF
+      IF( .NOT. ln_dia25h )   RETURN
+      ! ------------------- !
+      ! 1 - Allocate memory !
+      ! ------------------- !
+      ALLOCATE( tn_25h(jpi,jpj,jpk), STAT=ierror )
+      IF( ierror > 0 ) THEN
+         CALL ctl_stop( 'dia_25h: unable to allocate tn_25h' )   ;   RETURN
+      ENDIF
+      ALLOCATE( sn_25h(jpi,jpj,jpk), STAT=ierror )
+      IF( ierror > 0 ) THEN
+         CALL ctl_stop( 'dia_25h: unable to allocate sn_25h' )   ;   RETURN
+      ENDIF
+      ALLOCATE( rinsitu_t_25h(jpi,jpj,jpk), STAT=ierror )
+      IF( ierror > 0 ) THEN
+         CALL ctl_stop( 'dia_25h: unable to allocate rinsitu_t_25h' )   ;   RETURN
+      ENDIF
+      ALLOCATE( un_25h(jpi,jpj,jpk), STAT=ierror )
+      IF( ierror > 0 ) THEN
+         CALL ctl_stop( 'dia_25h: unable to allocate un_25h' )   ;   RETURN
+      ENDIF
+      ALLOCATE( vn_25h(jpi,jpj,jpk), STAT=ierror )
+      IF( ierror > 0 ) THEN
+         CALL ctl_stop( 'dia_25h: unable to allocate vn_25h' )   ;   RETURN
+      ENDIF
+      ALLOCATE( wn_25h(jpi,jpj,jpk), STAT=ierror )
+      IF( ierror > 0 ) THEN
+         CALL ctl_stop( 'dia_25h: unable to allocate wn_25h' )   ;   RETURN
+      ENDIF
+      ALLOCATE( avt_25h(jpi,jpj,jpk), STAT=ierror )
+      IF( ierror > 0 ) THEN
+         CALL ctl_stop( 'dia_25h: unable to allocate avt_25h' )   ;   RETURN
+      ENDIF
+      ALLOCATE( avm_25h(jpi,jpj,jpk), STAT=ierror )
+      IF( ierror > 0 ) THEN
+         CALL ctl_stop( 'dia_25h: unable to allocate avm_25h' )   ;   RETURN
+      ENDIF
+# if defined key_zdfgls || defined key_zdftke
+      ALLOCATE( en_25h(jpi,jpj,jpk), STAT=ierror )
+      IF( ierror > 0 ) THEN
+         CALL ctl_stop( 'dia_25h: unable to allocate en_25h' )   ;   RETURN
+      ENDIF
+#endif
+# if defined key_zdfgls 
+      ALLOCATE( rmxln_25h(jpi,jpj,jpk), STAT=ierror )
+      IF( ierror > 0 ) THEN
+         CALL ctl_stop( 'dia_25h: unable to allocate rmxln_25h' )   ;   RETURN
+      ENDIF
+#endif
+      ALLOCATE( sshn_25h(jpi,jpj), STAT=ierror )
+      IF( ierror > 0 ) THEN
+         CALL ctl_stop( 'dia_25h: unable to allocate sshn_25h' )   ;   RETURN
+      ENDIF
+      ! ------------------------- !
+      ! 2 - Assign Initial Values !
+      ! ------------------------- !
+      cnt_25h = 1  ! sets the first value of sum at timestep 1 (note - should strictly be at timestep zero so before values used where possible) 
+      tn_25h(:,:,:) = tsb(:,:,:,jp_tem)
+      sn_25h(:,:,:) = tsb(:,:,:,jp_sal)
+      CALL theta2t
+      rinsitu_t_25h(:,:,:) = rinsitu_t(:,:,:)
+      sshn_25h(:,:) = sshb(:,:)
+      un_25h(:,:,:) = ub(:,:,:)
+      vn_25h(:,:,:) = vb(:,:,:)
+      wn_25h(:,:,:) = wn(:,:,:)
+      avt_25h(:,:,:) = avt(:,:,:)
+      avm_25h(:,:,:) = avm(:,:,:)
+# if defined key_zdfgls || defined key_zdftke
+         en_25h(:,:,:) = en(:,:,:)
+#endif
+# if defined key_zdfgls
+         rmxln_25h(:,:,:) = mxln(:,:,:)
+#endif
+#if defined key_lim3 || defined key_lim2
+         CALL ctl_stop('STOP', 'dia_25h not setup yet to do tidemean ice')
+#endif 
+
+      ! -------------------------- !
+      ! 3 - Return to dia_wri      !
+      ! -------------------------- !
+
+
+   END SUBROUTINE dia_25h_init
+
+
+   SUBROUTINE dia_25h( kt )  
+      !!----------------------------------------------------------------------
+      !!                 ***  ROUTINE dia_25h  ***
+      !!         
+      !!
+      !!--------------------------------------------------------------------
+      !!                   
+      !! ** Purpose :   Write diagnostics with M2/S2 tide removed
+      !!
+      !! ** Method  :   
+      !!      25hr mean outputs for shelf seas
+      !!
+      !! History :
+      !!   ?.0  !  07-04  (A. Hines) New routine, developed from dia_wri_foam
+      !!   3.4  !  02-13  (J. Siddorn) Routine taken from old dia_wri_foam
+      !!   3.6  !  08-14  (E. O'Dea) adapted for VN3.6
+      !!----------------------------------------------------------------------
+      !! * Modules used
+
+      IMPLICIT NONE
+
+      !! * Arguments
+      INTEGER, INTENT( in ) ::   kt      ! ocean time-step index
+
+
+      !! * Local declarations
+      INTEGER ::   ji, jj, jk
+
+      LOGICAL ::   ll_print = .FALSE.    ! =T print and flush numout
+      REAL(wp)                         ::   zsto, zout, zmax, zjulian, zdt, zmdi  ! temporary reals
+      INTEGER                          ::   i_steps                               ! no of timesteps per hour
+      REAL(wp), DIMENSION(jpi,jpj    ) ::   zw2d, un_dm, vn_dm                    ! temporary workspace
+      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zw3d                                  ! temporary workspace
+      REAL(wp), DIMENSION(jpi,jpj,3)   ::   zwtmb                                 ! temporary workspace
+      INTEGER                          ::   iyear0, nimonth0,iday0                ! start year,imonth,day
+
+      !!----------------------------------------------------------------------
+
+      ! 0. Initialisation
+      ! -----------------
+      ! Define frequency of summing to create 25 h mean
+      zdt = rdt
+      IF( nacc == 1 ) zdt = rdtmin
+
+      IF( MOD( 3600,INT(zdt) ) == 0 ) THEN
+         i_steps = 3600/INT(zdt)
+      ELSE
+         CALL ctl_stop('STOP', 'dia_wri_tide: timestep must give MOD(3600,rdt) = 0 otherwise no hourly values are possible')
+      ENDIF
+
+#if defined key_lim3 || defined key_lim2
+      CALL ctl_stop('STOP', 'dia_wri_tide not setup yet to do tidemean ice')
+#endif
+
+      ! local variable for debugging
+      ll_print = ll_print .AND. lwp
+
+      ! Sum of 25 hourly instantaneous values to give a 25h mean from 24hours
+      ! every day
+      IF( MOD( kt, i_steps ) == 0  .and. kt .ne. nn_it000 ) THEN
+
+         IF (lwp) THEN
+              WRITE(numout,*) 'dia_wri_tide : Summing instantaneous hourly diagnostics at timestep ',kt
+              WRITE(numout,*) '~~~~~~~~~~~~ '
+         ENDIF
+
+         tn_25h(:,:,:)        = tn_25h(:,:,:) + tsn(:,:,:,jp_tem)
+         sn_25h(:,:,:)        = sn_25h(:,:,:) + tsn(:,:,:,jp_sal)
+         CALL theta2t
+         rinsitu_t_25h(:,:,:)  = rinsitu_t_25h(:,:,:) + rinsitu_t(:,:,:)
+         sshn_25h(:,:)        = sshn_25h(:,:) + sshn (:,:)
+         un_25h(:,:,:)        = un_25h(:,:,:) + un(:,:,:)
+         vn_25h(:,:,:)        = vn_25h(:,:,:) + vn(:,:,:)
+         wn_25h(:,:,:)        = wn_25h(:,:,:) + wn(:,:,:)
+         avt_25h(:,:,:)       = avt_25h(:,:,:) + avt(:,:,:)
+         avm_25h(:,:,:)       = avm_25h(:,:,:) + avm(:,:,:)
+# if defined key_zdfgls || defined key_zdftke
+         en_25h(:,:,:)        = en_25h(:,:,:) + en(:,:,:)
+#endif
+# if defined key_zdfgls
+         rmxln_25h(:,:,:)      = rmxln_25h(:,:,:) + mxln(:,:,:)
+#endif
+         cnt_25h = cnt_25h + 1
+
+         IF (lwp) THEN
+            WRITE(numout,*) 'dia_tide : Summed the following number of hourly values so far',cnt_25h
+         ENDIF
+
+      ENDIF ! MOD( kt, i_steps ) == 0
+
+         ! Write data for 25 hour mean output streams
+      IF( cnt_25h .EQ. 25 .AND.  MOD( kt, i_steps*24) == 0 .AND. kt .NE. nn_it000 ) THEN
+
+            IF(lwp) THEN
+               WRITE(numout,*) 'dia_wri_tide : Writing 25 hour mean tide diagnostics at timestep', kt
+               WRITE(numout,*) '~~~~~~~~~~~~ '
+            ENDIF
+
+            tn_25h(:,:,:)        = tn_25h(:,:,:) / 25.0_wp
+            sn_25h(:,:,:)        = sn_25h(:,:,:) / 25.0_wp
+            rinsitu_t_25h(:,:,:)  = rinsitu_t_25h(:,:,:) / 25.0_wp
+            sshn_25h(:,:)        = sshn_25h(:,:) / 25.0_wp
+            un_25h(:,:,:)        = un_25h(:,:,:) / 25.0_wp
+            vn_25h(:,:,:)        = vn_25h(:,:,:) / 25.0_wp
+            wn_25h(:,:,:)        = wn_25h(:,:,:) / 25.0_wp
+            avt_25h(:,:,:)       = avt_25h(:,:,:) / 25.0_wp
+            avm_25h(:,:,:)       = avm_25h(:,:,:) / 25.0_wp
+# if defined key_zdfgls || defined key_zdftke
+            en_25h(:,:,:)        = en_25h(:,:,:) / 25.0_wp
+#endif
+# if defined key_zdfgls
+            rmxln_25h(:,:,:)       = rmxln_25h(:,:,:) / 25.0_wp
+#endif
+
+            IF (lwp)  WRITE(numout,*) 'dia_wri_tide : Mean calculated by dividing 25 hour sums and writing output'
+            zmdi=1.e+20 !missing data indicator for masking
+            ! write tracers (instantaneous)
+            zw3d(:,:,:) = tn_25h(:,:,:)*tmask(:,:,:) + zmdi*(1.0-tmask(:,:,:))
+            CALL iom_put("temper25h", zw3d)   ! potential temperature
+            CALL theta2t                                                                    ! calculate insitu temp
+            zw3d(:,:,:) = rinsitu_t_25h(:,:,:)*tmask(:,:,:) + zmdi*(1.0-tmask(:,:,:))
+            CALL iom_put("tempis25h", zw3d)   ! in-situ temperature
+            zw3d(:,:,:) = sn_25h(:,:,:)*tmask(:,:,:) + zmdi*(1.0-tmask(:,:,:))
+            CALL iom_put( "salin25h", zw3d  )   ! salinity
+            zw2d(:,:) = sshn_25h(:,:)*tmask(:,:,1) + zmdi*(1.0-tmask(:,:,1))
+            CALL iom_put( "ssh25h", zw2d )   ! sea surface 
+
+
+            ! Write velocities (instantaneous)
+            zw3d(:,:,:) = un_25h(:,:,:)*umask(:,:,:) + zmdi*(1.0-umask(:,:,:))
+            CALL iom_put("vozocrtx25h", zw3d)    ! i-current
+            zw3d(:,:,:) = vn_25h(:,:,:)*vmask(:,:,:) + zmdi*(1.0-vmask(:,:,:))
+            CALL iom_put("vomecrty25h", zw3d  )   ! j-current
+
+            zw3d(:,:,:) = wn_25h(:,:,:)*tmask(:,:,:) + zmdi*(1.0-tmask(:,:,:))
+            CALL iom_put("vomecrtz25h", zw3d )   ! k-current
+            zw3d(:,:,:) = avt_25h(:,:,:)*tmask(:,:,:) + zmdi*(1.0-tmask(:,:,:))
+            CALL iom_put("avt25h", zw3d )   ! diffusivity
+            zw3d(:,:,:) = avm_25h(:,:,:)*tmask(:,:,:) + zmdi*(1.0-tmask(:,:,:))
+            CALL iom_put("avm25h", zw3d)   ! viscosity
+#if defined key_zdftke || defined key_zdfgls 
+!           zw3d(:,:,:) = en_25h(:,:,:)*tmask(:,:,:) + zmdi*(1.0-tmask(:,:,:))
+!           CALL iom_put("tke25h", zw3d)   ! tke
+#endif
+#if defined key_zdfgls 
+!           zw3d(:,:,:) = rmxln_25h(:,:,:)*tmask(:,:,:) + zmdi*(1.0-tmask(:,:,:))
+!           CALL iom_put( "mxln25h",zw3d)
+#endif
+
+            ! After the write reset the values to cnt=1 and sum values equal current value 
+            tn_25h(:,:,:) = tsn(:,:,:,jp_tem)
+            sn_25h(:,:,:) = tsn(:,:,:,jp_sal)
+            CALL theta2t
+            rinsitu_t_25h(:,:,:) = rinsitu_t(:,:,:)
+            sshn_25h(:,:) = sshn (:,:)
+            un_25h(:,:,:) = un(:,:,:)
+            vn_25h(:,:,:) = vn(:,:,:)
+            wn_25h(:,:,:) = wn(:,:,:)
+            avt_25h(:,:,:) = avt(:,:,:)
+            avm_25h(:,:,:) = avm(:,:,:)
+# if defined key_zdfgls || defined key_zdftke
+            en_25h(:,:,:) = en(:,:,:)
+#endif
+# if defined key_zdfgls
+            rmxln_25h(:,:,:) = mxln(:,:,:)
+#endif
+            cnt_25h = 1
+            IF (lwp)  WRITE(numout,*) 'dia_wri_tide : After 25hr mean write, reset sum to current value and cnt_25h to one for overlapping average',cnt_25h
+
+      ENDIF !  cnt_25h .EQ. 25 .AND.  MOD( kt, i_steps * 24) == 0 .AND. kt .NE. nn_it000
+
+
+   END SUBROUTINE dia_25h 
+
+   !!======================================================================
+END MODULE dia25h
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diaar5.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diaar5.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diaar5.F90	(revision 7567)
@@ -39,4 +39,5 @@
    REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:  ) ::   thick0       ! ocean thickness (interior domain)
    REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   sn0          ! initial salinity
+   REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   tn0          ! initial temperature
       
    !! * Substitutions
@@ -56,5 +57,5 @@
       !!----------------------------------------------------------------------
       !
-      ALLOCATE( area(jpi,jpj), thick0(jpi,jpj) , sn0(jpi,jpj,jpk) , STAT=dia_ar5_alloc )
+      ALLOCATE( area(jpi,jpj), thick0(jpi,jpj) , sn0(jpi,jpj,jpk), tn0(jpi,jpj,jpk) , STAT=dia_ar5_alloc )
       !
       IF( lk_mpp             )   CALL mpp_sum ( dia_ar5_alloc )
@@ -121,4 +122,38 @@
       zssh_steric = - zarho / area_tot
       CALL iom_put( 'sshthster', zssh_steric )
+      CALL iom_put( 'sshthster_mat', -zbotpres )
+
+      !                     
+      ztsn(:,:,:,jp_tem) = tn0(:,:,:)                    ! thermohaline ssh
+      ztsn(:,:,:,jp_sal) = tsn(:,:,:,jp_sal) 
+      CALL eos( ztsn, zrhd, fsdept_n(:,:,:) )                       ! now in situ density using initial temperature
+      !
+      zbotpres(:,:) = 0._wp                        ! no atmospheric surface pressure, levitating sea-ice
+      DO jk = 1, jpkm1
+         zbotpres(:,:) = zbotpres(:,:) + fse3t(:,:,jk) * zrhd(:,:,jk)
+      END DO
+      IF( .NOT.lk_vvl ) THEN
+         IF ( ln_isfcav ) THEN
+            DO ji=1,jpi
+               DO jj=1,jpj
+                  zbotpres(ji,jj) = zbotpres(ji,jj) + sshn(ji,jj) * zrhd(ji,jj,mikt(ji,jj)) + riceload(ji,jj)
+               END DO
+            END DO
+         ELSE
+            zbotpres(:,:) = zbotpres(:,:) + sshn(:,:) * zrhd(:,:,1)
+         END IF
+      END IF
+      !                                         
+      zarho = SUM( area(:,:) * zbotpres(:,:) ) 
+      IF( lk_mpp )   CALL mpp_sum( zarho )
+      zssh_steric = - zarho / area_tot
+      CALL iom_put( 'sshhlster', zssh_steric )
+      !JT
+      CALL iom_put( 'sshhlster_mat', -zbotpres )
+      !JT
+      
+
+      
+      !JT
       
       !                                         ! steric sea surface height
@@ -147,4 +182,7 @@
       zssh_steric = - zarho / area_tot
       CALL iom_put( 'sshsteric', zssh_steric )
+      !JT
+      CALL iom_put( 'sshsteric_mat', -zbotpres )
+      !JT
       
       !                                         ! ocean bottom pressure
@@ -211,4 +249,5 @@
       REAL(wp) ::   zztmp  
       REAL(wp), POINTER, DIMENSION(:,:,:,:) ::   zsaldta   ! Jan/Dec levitus salinity
+      REAL(wp), POINTER, DIMENSION(:,:,:,:) ::   ztemdta   ! Jan/Dec levitus salinity
       ! reading initial file
       LOGICAL  ::   ln_tsd_init      !: T & S data flag
@@ -234,4 +273,5 @@
       !
       CALL wrk_alloc( jpi , jpj , jpk, jpts, zsaldta )
+      CALL wrk_alloc( jpi , jpj , jpk, jpts, ztemdta )
       !                                      ! allocate dia_ar5 arrays
       IF( dia_ar5_alloc() /= 0 )   CALL ctl_stop( 'STOP', 'dia_ar5_init : unable to allocate arrays' )
@@ -253,6 +293,13 @@
       CALL iom_get  ( inum, jpdom_data, TRIM(sn_sal%clvar), zsaldta(:,:,:,2), 12 )
       CALL iom_close( inum )
+
+      CALL iom_open ( TRIM( cn_dir )//TRIM(sn_tem%clname), inum )
+      CALL iom_get  ( inum, jpdom_data, TRIM(sn_tem%clvar), ztemdta(:,:,:,1), 1  )
+      CALL iom_get  ( inum, jpdom_data, TRIM(sn_tem%clvar), ztemdta(:,:,:,2), 12 )
+      CALL iom_close( inum )
       sn0(:,:,:) = 0.5_wp * ( zsaldta(:,:,:,1) + zsaldta(:,:,:,2) )        
-      sn0(:,:,:) = sn0(:,:,:) * tmask(:,:,:)
+      tn0(:,:,:) = 0.5_wp * ( ztemdta(:,:,:,1) + ztemdta(:,:,:,2) )        
+      sn0(:,:,:) = sn0(:,:,:) * tmask(:,:,:)  
+      tn0(:,:,:) = tn0(:,:,:) * tmask(:,:,:)
       IF( ln_zps ) THEN               ! z-coord. partial steps
          DO jj = 1, jpj               ! interpolation of salinity at the last ocean level (i.e. the partial step)
@@ -262,4 +309,5 @@
                   zztmp = ( gdept_1d(ik) - gdept_0(ji,jj,ik) ) / ( gdept_1d(ik) - gdept_1d(ik-1) )
                   sn0(ji,jj,ik) = ( 1._wp - zztmp ) * sn0(ji,jj,ik) + zztmp * sn0(ji,jj,ik-1)
+                  tn0(ji,jj,ik) = ( 1._wp - zztmp ) * tn0(ji,jj,ik) + zztmp * tn0(ji,jj,ik-1)
                ENDIF
             END DO
@@ -268,4 +316,5 @@
       !
       CALL wrk_dealloc( jpi , jpj , jpk, jpts, zsaldta )
+      CALL wrk_dealloc( jpi , jpj , jpk, jpts, ztemdta )
       !
       IF( nn_timing == 1 )   CALL timing_stop('dia_ar5_init')
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diadct.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diadct.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diadct.F90	(revision 7567)
@@ -54,9 +54,10 @@
   PUBLIC   dia_dct      ! routine called by step.F90
   PUBLIC   dia_dct_init ! routine called by opa.F90
-  PUBLIC   diadct_alloc ! routine called by nemo_init in nemogcm.F90 
+  PUBLIC   diadct_alloc ! routine called by nemo_init in nemogcm.F90
   PRIVATE  readsec
   PRIVATE  removepoints
   PRIVATE  transport
   PRIVATE  dia_dct_wri
+  PRIVATE  dia_dct_wri_NOOS
 
 #include "domzgr_substitute.h90"
@@ -69,11 +70,13 @@
   INTEGER :: nn_dctwri     ! Frequency of output
   INTEGER :: nn_secdebug   ! Number of the section to debug
+  INTEGER :: nn_dct_h    = 1     ! Frequency of computation for NOOS hourly files
+  INTEGER :: nn_dctwri_h = 1     ! Frequency of output for NOOS hourly files
    
-  INTEGER, PARAMETER :: nb_class_max  = 10
-  INTEGER, PARAMETER :: nb_sec_max    = 150
-  INTEGER, PARAMETER :: nb_point_max  = 2000
-  INTEGER, PARAMETER :: nb_type_class = 10
-  INTEGER, PARAMETER :: nb_3d_vars    = 3 
-  INTEGER, PARAMETER :: nb_2d_vars    = 2 
+  INTEGER, PARAMETER :: nb_class_max  = 11   ! maximum number of classes, i.e. depth levels or density classes
+  INTEGER, PARAMETER :: nb_sec_max    = 30   ! maximum number of sections
+  INTEGER, PARAMETER :: nb_point_max  = 375  ! maximum number of points in a single section
+  INTEGER, PARAMETER :: nb_type       = 14   ! types of calculations, i.e. pos transport, neg transport, heat transport, salt transport
+  INTEGER, PARAMETER :: nb_3d_vars    = 5
+  INTEGER, PARAMETER :: nb_2d_vars    = 2
   INTEGER            :: nb_sec 
 
@@ -101,5 +104,6 @@
                                                      ztem            ,&! temperature classes(99 if you don't want)
                                                      zlay              ! level classes      (99 if you don't want)
-     REAL(wp), DIMENSION(nb_type_class,nb_class_max)  :: transport     ! transport output
+     REAL(wp), DIMENSION(nb_type,nb_class_max)        :: transport     ! transport output
+     REAL(wp), DIMENSION(nb_type,nb_class_max)        :: transport_h   ! transport output
      REAL(wp)                                         :: slopeSection  ! slope of the section
      INTEGER                                          :: nb_point      ! number of points in the section
@@ -108,26 +112,32 @@
 
   TYPE(SECTION),DIMENSION(nb_sec_max) :: secs ! Array of sections
- 
-  REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:) ::  transports_3d 
-  REAL(wp), ALLOCATABLE, DIMENSION(:,:,:)   ::  transports_2d  
+
+  REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:) ::  transports_3d
+  REAL(wp), ALLOCATABLE, DIMENSION(:,:,:)   ::  transports_2d
+  REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:) ::  transports_3d_h
+  REAL(wp), ALLOCATABLE, DIMENSION(:,:,:)   ::  transports_2d_h
+  REAL(wp), ALLOCATABLE, DIMENSION(:,:,:)   ::  z_hr_output
 
    !! $Id$
 CONTAINS
 
- 
-  INTEGER FUNCTION diadct_alloc() 
-     !!---------------------------------------------------------------------- 
-     !!                   ***  FUNCTION diadct_alloc  *** 
-     !!---------------------------------------------------------------------- 
-     INTEGER :: ierr(2) 
-     !!---------------------------------------------------------------------- 
-
-     ALLOCATE(transports_3d(nb_3d_vars,nb_sec_max,nb_point_max,jpk), STAT=ierr(1) ) 
-     ALLOCATE(transports_2d(nb_2d_vars,nb_sec_max,nb_point_max)    , STAT=ierr(2) ) 
-
-     diadct_alloc = MAXVAL( ierr ) 
-     IF( diadct_alloc /= 0 )   CALL ctl_warn('diadct_alloc: failed to allocate arrays') 
- 
-  END FUNCTION diadct_alloc 
+  INTEGER FUNCTION diadct_alloc()
+     !!--------------------------------------------------------------------nb_sec--
+     !!                   ***  FUNCTION diadct_alloc  ***
+     !!----------------------------------------------------------------------
+     INTEGER :: ierr(2)
+     !!----------------------------------------------------------------------
+     !
+     ALLOCATE(transports_3d(nb_3d_vars,nb_sec_max,nb_point_max,jpk),   STAT=ierr(1) )
+     ALLOCATE(transports_2d(nb_2d_vars,nb_sec_max,nb_point_max)    ,   STAT=ierr(2) )
+     ALLOCATE(transports_3d_h(nb_3d_vars,nb_sec_max,nb_point_max,jpk), STAT=ierr(3) )
+     ALLOCATE(transports_2d_h(nb_2d_vars,nb_sec_max,nb_point_max)    , STAT=ierr(4) )
+     ALLOCATE(z_hr_output(nb_sec_max,24,nb_class_max)                , STAT=ierr(5) )
+        !
+     diadct_alloc = MAXVAL( ierr )
+     IF( diadct_alloc /= 0 )   CALL ctl_warn('diadct_alloc: failed to allocate arrays')
+     !
+  END FUNCTION diadct_alloc
+
 
   SUBROUTINE dia_dct_init
@@ -152,4 +162,26 @@
 902  IF( ios /= 0 ) CALL ctl_nam ( ios , 'namdct in configuration namelist', lwp )
      IF(lwm) WRITE ( numond, namdct )
+     
+     
+     
+     
+     
+     
+     
+     
+     !NAMELIST/namdct/nn_dct,nn_dctwri,nn_secdebug
+
+     !IF( nn_timing == 1 )   CALL timing_start('dia_dct_init')
+
+     !read namelist
+     !REWIND( numnam )
+     !READ  ( numnam, namdct )
+
+     IF( ln_NOOS ) THEN
+        nn_dct=3600./rdt         ! hard coded for NOOS transects, to give 25 hour means 
+        nn_dctwri=86400./rdt
+        nn_dct_h=1       ! hard coded for NOOS transects, to give hourly data
+        nn_dctwri_h=3600./rdt
+     ENDIF
 
      IF( lwp ) THEN
@@ -157,6 +189,13 @@
         WRITE(numout,*) "diadct_init: compute transports through sections "
         WRITE(numout,*) "~~~~~~~~~~~~~~~~~~~~~"
-        WRITE(numout,*) "       Frequency of computation: nn_dct    = ",nn_dct
-        WRITE(numout,*) "       Frequency of write:       nn_dctwri = ",nn_dctwri
+        IF( ln_NOOS ) THEN
+           WRITE(numout,*) "       Frequency of computation hard coded to be every hour: nn_dct    = ",nn_dct
+           WRITE(numout,*) "       Frequency of write hard coded to average 25 instantaneous hour values: nn_dctwri = ",nn_dctwri
+           WRITE(numout,*) "       Frequency of hourly computation hard coded to be every timestep: nn_dct_h  = ",nn_dct_h
+           WRITE(numout,*) "       Frequency of hourly write hard coded to every hour: nn_dctwri_h = ",nn_dctwri_h
+        ELSE
+           WRITE(numout,*) "       Frequency of computation: nn_dct    = ",nn_dct
+           WRITE(numout,*) "       Frequency of write:       nn_dctwri = ",nn_dctwri
+        ENDIF
 
         IF      ( nn_secdebug .GE. 1 .AND. nn_secdebug .LE. nb_sec_max )THEN
@@ -174,15 +213,26 @@
      !Read section_ijglobal.diadct
      CALL readsec
+     
+     !IF (lwp) write(numout,*) 'dct after readsec'
+     
 
      !open output file
-     IF( lwm ) THEN
-        CALL ctl_opn( numdct_vol,  'volume_transport', 'NEW', 'FORMATTED', 'SEQUENTIAL', -1, numout,  .FALSE. )
-        CALL ctl_opn( numdct_heat, 'heat_transport'  , 'NEW', 'FORMATTED', 'SEQUENTIAL', -1, numout,  .FALSE. )
-        CALL ctl_opn( numdct_salt, 'salt_transport'  , 'NEW', 'FORMATTED', 'SEQUENTIAL', -1, numout,  .FALSE. )
+     IF( lwp ) THEN
+        IF( ln_NOOS ) THEN
+           CALL ctl_opn( numdct_NOOS  ,'NOOS_transport'  , 'NEW', 'FORMATTED', 'SEQUENTIAL', -1, numout,  .FALSE. )
+           CALL ctl_opn( numdct_NOOS_h,'NOOS_transport_h', 'NEW', 'FORMATTED', 'SEQUENTIAL', -1, numout,  .FALSE. )
+        ELSE
+           CALL ctl_opn( numdct_vol , 'volume_transport', 'NEW', 'FORMATTED', 'SEQUENTIAL', -1, numout,  .FALSE. )
+           CALL ctl_opn( numdct_temp, 'heat_transport'  , 'NEW', 'FORMATTED', 'SEQUENTIAL', -1, numout,  .FALSE. )
+           CALL ctl_opn( numdct_sal , 'salt_transport'  , 'NEW', 'FORMATTED', 'SEQUENTIAL', -1, numout,  .FALSE. )
+        ENDIF
      ENDIF
 
-     ! Initialise arrays to zero 
-     transports_3d(:,:,:,:)=0.0 
-     transports_2d(:,:,:)  =0.0 
+     ! Initialise arrays to zero
+     transports_3d(:,:,:,:)  =0._wp
+     transports_2d(:,:,:)    =0._wp
+     transports_3d_h(:,:,:,:)=0._wp
+     transports_2d_h(:,:,:)  =0._wp
+     z_hr_output(:,:,:)      =0._wp
 
      IF( nn_timing == 1 )   CALL timing_stop('dia_dct_init')
@@ -195,16 +245,16 @@
      !!               ***  ROUTINE diadct  ***  
      !!
-     !!  Purpose :: Compute section transports and write it in numdct files 
-     !!   
-     !!  Method  :: All arrays initialised to zero in dct_init 
-     !!             Each nn_dct time step call subroutine 'transports' for 
-     !!               each section to sum the transports over each grid cell. 
-     !!             Each nn_dctwri time step: 
-     !!               Divide the arrays by the number of summations to gain 
-     !!               an average value 
-     !!               Call dia_dct_sum to sum relevant grid boxes to obtain 
-     !!               totals for each class (density, depth, temp or sal) 
-     !!               Call dia_dct_wri to write the transports into file 
-     !!               Reinitialise all relevant arrays to zero 
+     !!  Purpose :: Compute section transports and write it in numdct files
+     !!  
+     !!  Method  :: All arrays initialised to zero in dct_init
+     !!             Each nn_dct time step call subroutine 'transports' for
+     !!               each section to sum the transports.
+     !!             Each nn_dctwri time step:
+     !!               Divide the arrays by the number of summations to gain
+     !!               an average value
+     !!               Call dia_dct_sum to sum relevant grid boxes to obtain
+     !!               totals for each class (density, depth, temp or sal)
+     !!               Call dia_dct_wri to write the transports into file
+     !!               Reinitialise all relevant arrays to zero
      !!---------------------------------------------------------------------
      !! * Arguments
@@ -213,19 +263,28 @@
      !! * Local variables
      INTEGER             :: jsec,            &! loop on sections
-                            itotal            ! nb_sec_max*nb_type_class*nb_class_max
+                            itotal            ! nb_sec_max*nb_type*nb_class_max
      LOGICAL             :: lldebug =.FALSE.  ! debug a section  
-     
-     INTEGER , DIMENSION(1)             :: ish   ! tmp array for mpp_sum
-     INTEGER , DIMENSION(3)             :: ish2  !   "
-     REAL(wp), POINTER, DIMENSION(:)    :: zwork !   "  
-     REAL(wp), POINTER, DIMENSION(:,:,:):: zsum  !   "
+
+     
+     INTEGER , DIMENSION(1)             :: ish      ! tmp array for mpp_sum
+     INTEGER , DIMENSION(3)             :: ish2     !   "
+     REAL(wp), POINTER, DIMENSION(:)    :: zwork    !   " 
+     REAL(wp), POINTER, DIMENSION(:,:,:):: zsum     !   "
+
+
 
      !!---------------------------------------------------------------------    
+     
+     
+     !WRITE(numout,*) "diadct ind ii: ",kt,nproc, narea, mig(1),mig(jpi),nlci
+     !WRITE(numout,*) "diadct ind jj: ",kt,nproc, narea, mjg(1),mig(jpj),nlcj
+
+     
      IF( nn_timing == 1 )   CALL timing_start('dia_dct')
 
      IF( lk_mpp )THEN
-        itotal = nb_sec_max*nb_type_class*nb_class_max
-        CALL wrk_alloc( itotal                                , zwork ) 
-        CALL wrk_alloc( nb_sec_max,nb_type_class,nb_class_max , zsum  )
+        itotal = nb_sec_max*nb_type*nb_class_max
+        CALL wrk_alloc( itotal                          , zwork ) 
+        CALL wrk_alloc( nb_sec_max,nb_type,nb_class_max , zsum  )
      ENDIF    
  
@@ -239,13 +298,19 @@
          WRITE(numout,*) "~~~~~~~~~~~~~~~~~~~~~~~~~"
          WRITE(numout,*) "nb_sec = ",nb_sec
+         WRITE(numout,*) "nn_dct = ",nn_dct
+         WRITE(numout,*) "ln_NOOS = ",ln_NOOS
+         WRITE(numout,*) "nb_sec = ",nb_sec
+         WRITE(numout,*) "nb_sec_max = ",nb_sec_max
+         WRITE(numout,*) "nb_type = ",nb_type
+         WRITE(numout,*) "nb_class_max = ",nb_class_max
      ENDIF
 
  
      ! Compute transport and write only at nn_dctwri
-     IF( MOD(kt,nn_dct)==0 ) THEN 
+     IF ( MOD(kt,nn_dct)==0 .or. &               ! compute transport every nn_dct time steps
+         (ln_NOOS .and. kt==nn_it000 ) )  THEN   ! also include first time step when calculating NOOS 25 hour averages
 
         DO jsec=1,nb_sec
 
-           !debug this section computing ?
            lldebug=.FALSE.
            IF( (jsec==nn_secdebug .OR. nn_secdebug==-1) .AND.  kt==nit000+nn_dct-1 .AND. lwp ) lldebug=.TRUE. 
@@ -253,24 +318,28 @@
            !Compute transport through section  
            CALL transport(secs(jsec),lldebug,jsec) 
+           
+           !IF( lwp ) WRITE(numout,*) "diadct: call transport subroutine (kt, jsec) : ",kt,jsec
 
         ENDDO
+        !IF( lwp ) WRITE(numout,*) "diadct: called transport subroutine (kt, nb_sec) : ",kt, nb_sec
              
         IF( MOD(kt,nn_dctwri)==0 )THEN
 
-           IF( lwp .AND. kt==nit000+nn_dctwri-1 )WRITE(numout,*)"      diadct: average transports and write at kt = ",kt         
+           IF( lwp .AND. kt==nit000+nn_dctwri-1 )WRITE(numout,*)"      diadct: average and write at kt = ",kt         
   
-           !! divide arrays by nn_dctwri/nn_dct to obtain average 
-           transports_3d(:,:,:,:)=transports_3d(:,:,:,:)/(nn_dctwri/nn_dct) 
-           transports_2d(:,:,:)  =transports_2d(:,:,:)  /(nn_dctwri/nn_dct) 
- 
-           ! Sum over each class 
-           DO jsec=1,nb_sec 
-              CALL dia_dct_sum(secs(jsec),jsec) 
-           ENDDO 
-
+           !! divide arrays by nn_dctwri/nn_dct to obtain average
+           transports_3d(:,:,:,:)=transports_3d(:,:,:,:)/(nn_dctwri/nn_dct)
+           transports_2d(:,:,:)  =transports_2d(:,:,:)  /(nn_dctwri/nn_dct)
+
+           ! Sum over each class
+           DO jsec=1,nb_sec
+              CALL dia_dct_sum(secs(jsec),jsec)
+           ENDDO
+ 
            !Sum on all procs 
            IF( lk_mpp )THEN
-              ish(1)  =  nb_sec_max*nb_type_class*nb_class_max 
-              ish2    = (/nb_sec_max,nb_type_class,nb_class_max/)
+              zsum(:,:,:)=0.0_wp
+              ish(1)  =  nb_sec_max*nb_type*nb_class_max 
+              ish2    = (/nb_sec_max,nb_type,nb_class_max/)
               DO jsec=1,nb_sec ; zsum(jsec,:,:) = secs(jsec)%transport(:,:) ; ENDDO
               zwork(:)= RESHAPE(zsum(:,:,:), ish )
@@ -283,10 +352,13 @@
            DO jsec=1,nb_sec
 
-              IF( lwm )CALL dia_dct_wri(kt,jsec,secs(jsec))
+              IF( lwp .and. .not. ln_NOOS )CALL dia_dct_wri(kt,jsec,secs(jsec))
+              IF( lwp .and.       ln_NOOS )CALL dia_dct_wri_NOOS(kt,jsec,secs(jsec))   ! use NOOS specific formatting
+            
             
               !nullify transports values after writing
-              transports_3d(:,jsec,:,:)=0.
-              transports_2d(:,jsec,:  )=0.
+              transports_3d(:,jsec,:,:)=0.0
+              transports_2d(:,jsec,:  )=0.0
               secs(jsec)%transport(:,:)=0.  
+              IF ( ln_NOOS ) CALL transport(secs(jsec),lldebug,jsec)  ! reinitialise for next 25 hour instantaneous average (overlapping values)
 
            ENDDO
@@ -295,9 +367,66 @@
 
      ENDIF
+     IF ( MOD(kt,nn_dct_h)==0 ) THEN            ! compute transport every nn_dct_h time steps
+     !IF ( MOD(kt,nn_dct)==0 .or. &               ! compute transport every nn_dct time steps
+     !    (ln_NOOS .and. kt==nn_it000 ) )  THEN   ! also include first time step when calculating NOOS 25 hour averages
+
+        DO jsec=1,nb_sec
+
+           lldebug=.FALSE.
+           IF( (jsec==nn_secdebug .OR. nn_secdebug==-1) .AND.  kt==nit000+nn_dct_h-1 .AND. lwp ) lldebug=.TRUE. 
+
+           !Compute transport through section  
+           CALL transport_h(secs(jsec),lldebug,jsec) 
+
+        ENDDO
+             
+        IF( MOD(kt,nn_dctwri_h)==0 )THEN
+
+           IF( lwp .AND. kt==nit000+nn_dctwri_h-1 )WRITE(numout,*)"      diadct: average and write hourly files at kt = ",kt         
+  
+           !! divide arrays by nn_dctwri/nn_dct to obtain average
+           transports_3d_h(:,:,:,:)=transports_3d_h(:,:,:,:)/(nn_dctwri_h/nn_dct_h)
+           transports_2d_h(:,:,:)  =transports_2d_h(:,:,:)  /(nn_dctwri_h/nn_dct_h)
+
+           ! Sum over each class
+           DO jsec=1,nb_sec
+              CALL dia_dct_sum_h(secs(jsec),jsec)
+           ENDDO
+ 
+           !Sum on all procs 
+          IF( lk_mpp )THEN
+              ish(1)  =  nb_sec_max*nb_type*nb_class_max 
+              ish2    = (/nb_sec_max,nb_type,nb_class_max/)
+              DO jsec=1,nb_sec ; zsum(jsec,:,:) = secs(jsec)%transport_h(:,:) ; ENDDO
+              zwork(:)= RESHAPE(zsum(:,:,:), ish )
+              CALL mpp_sum(zwork, ish(1))
+              zsum(:,:,:)= RESHAPE(zwork,ish2)
+              DO jsec=1,nb_sec ; secs(jsec)%transport_h(:,:) = zsum(jsec,:,:) ; ENDDO
+           ENDIF
+
+           !Write the transport
+           DO jsec=1,nb_sec
+
+              IF( lwp .and. ln_NOOS ) THEN
+                !WRITE(numout,*) "diadct: call dia_dct_wri_NOOS_h (kt,nn_dctwri_h, kt/nn_dctwri_h,jsec) : ",kt,nn_dctwri_h, kt/nn_dctwri_h,jsec
+                CALL dia_dct_wri_NOOS_h(kt/nn_dctwri_h,jsec,secs(jsec))   ! use NOOS specific formatting
+                !WRITE(numout,*) "diadct: called dia_dct_wri_NOOS_h (kt,jsec) : ",kt,jsec
+              endif
+              !nullify transports values after writing
+              transports_3d_h(:,jsec,:,:)=0.0
+              transports_2d_h(:,jsec,:)=0.0
+              secs(jsec)%transport_h(:,:)=0.  
+              IF ( ln_NOOS ) CALL transport_h(secs(jsec),lldebug,jsec)  ! reinitialise for next 25 hour instantaneous average (overlapping values)
+
+           ENDDO
+
+        ENDIF 
+
+     ENDIF    
 
      IF( lk_mpp )THEN
-        itotal = nb_sec_max*nb_type_class*nb_class_max
-        CALL wrk_dealloc( itotal                                , zwork ) 
-        CALL wrk_dealloc( nb_sec_max,nb_type_class,nb_class_max , zsum  )
+        itotal = nb_sec_max*nb_type*nb_class_max
+        CALL wrk_dealloc( itotal                          , zwork ) 
+        CALL wrk_dealloc( nb_sec_max,nb_type,nb_class_max , zsum  )
      ENDIF    
 
@@ -320,4 +449,5 @@
      INTEGER :: isec, iiglo, ijglo, iiloc, ijloc,iost,i1 ,i2  ! temporary  integer
      INTEGER :: jsec, jpt                                     ! dummy loop indices
+                                                              ! heat/salt tranport is actived
 
      INTEGER, DIMENSION(2) :: icoord 
@@ -336,6 +466,27 @@
      !open input file
      !---------------
-     CALL ctl_opn( numdct_in, 'section_ijglobal.diadct', 'OLD', 'UNFORMATTED', 'SEQUENTIAL', -1, numout, .FALSE. )
- 
+     !IF (lwp) THEN
+       !write(numout,*) 'dct low-level pre open: little endian '
+       !OPEN(UNIT=107,FILE='section_ijglobal.diadct', FORM='UNFORMATTED', ACCESS='SEQUENTIAL', STATUS='OLD',convert='LITTLE_ENDIAN')
+       
+       write(numout,*) 'dct low-level pre open: big endian :',nproc,narea
+       OPEN(UNIT=107,FILE='section_ijglobal.diadct', FORM='UNFORMATTED', ACCESS='SEQUENTIAL', STATUS='OLD',convert='BIG_ENDIAN')
+       
+       !write(numout,*) 'dct low-level pre open: SWAP '
+       !OPEN(UNIT=107,FILE='section_ijglobal.diadct', FORM='UNFORMATTED', ACCESS='SEQUENTIAL', STATUS='OLD',convert='SWAP')
+       
+       !write(numout,*) 'dct low-level pre open: NATIVE '
+       !OPEN(UNIT=107,FILE='section_ijglobal.diadct', FORM='UNFORMATTED', ACCESS='SEQUENTIAL', STATUS='OLD',convert='NATIVE')
+       
+       write(numout,*) 'dct low-level opened :',nproc,narea
+       READ(107) isec
+       write(numout,*) 'dct low-level isec ', isec,nproc,narea
+       CLOSE(107)
+     !ENDIF
+     
+     
+     CALL ctl_opn( numdct_in, 'section_ijglobal.diadct', 'OLD', 'UNFORMATTED', 'SEQUENTIAL', -1, numout, .TRUE. )
+ 
+     
      !---------------
      !Read input file
@@ -350,19 +501,55 @@
         !---------------
         secs(jsec)%name=''
-        secs(jsec)%llstrpond    = .FALSE.  ; secs(jsec)%ll_ice_section = .FALSE.
-        secs(jsec)%ll_date_line = .FALSE.  ; secs(jsec)%nb_class       = 0
-        secs(jsec)%zsigi        = 99._wp   ; secs(jsec)%zsigp          = 99._wp
-        secs(jsec)%zsal         = 99._wp   ; secs(jsec)%ztem           = 99._wp
-        secs(jsec)%zlay         = 99._wp         
-        secs(jsec)%transport    =  0._wp   ; secs(jsec)%nb_point       = 0
+        !IF( lwp )  write(numout,*)  secs(jsec)%name
+        secs(jsec)%llstrpond      = .FALSE.
+        !IF( lwp )  write(numout,*) secs(jsec)%llstrpond 
+        secs(jsec)%ll_ice_section = .FALSE.
+        !IF( lwp )  write(numout,*) secs
+        secs(jsec)%ll_date_line   = .FALSE.
+        !IF( lwp )  write(numout,*) secs(jsec)%ll_date_line
+        secs(jsec)%nb_class       = 0
+        !IF( lwp )  write(numout,*) secs(jsec)%nb_class
+        secs(jsec)%zsigi          = 99._wp
+        !IF( lwp )  write(numout,*) secs(jsec)%zsigi
+        secs(jsec)%zsigp          = 99._wp
+        !IF( lwp )  write(numout,*)  secs(jsec)%zsigp
+        secs(jsec)%zsal           = 99._wp
+        !IF( lwp )  write(numout,*) secs(jsec)%zsal
+        secs(jsec)%ztem           = 99._wp
+        !IF( lwp )  write(numout,*) secs(jsec)%ztem
+        secs(jsec)%zlay           = 99._wp
+        !IF( lwp )  write(numout,*) secs(jsec)%zlay
+        secs(jsec)%transport      =  0._wp
+        !IF( lwp )  write(numout,*) secs(jsec)%transport
+        secs(jsec)%transport_h    =  0._wp
+        !IF( lwp )  write(numout,*) secs(jsec)%transport_h
+        secs(jsec)%nb_point       = 0
+        !IF( lwp )  write(numout,*) secs(jsec)%nb_point 
 
         !read section's number / name / computing choices / classes / slopeSection / points number
         !-----------------------------------------------------------------------------------------
-        READ(numdct_in,iostat=iost)isec
-        IF (iost .NE. 0 )EXIT !end of file 
+        
+        !write(numout,*) 'dct isec ', isec
+        !write(numout,*) 'dct numdct_in ', numdct_in
+        READ(numdct_in,iostat=iost) isec
+        !write(numout,*) 'dct iost ', iost
+        !IF (iost .NE. 0 ) EXIT
+        IF (iost .NE. 0 ) then
+          write(numout,*) 'unable to read section_ijglobal.diadct. iost = ',iost
+          !nb_sec = 2
+          EXIT !end of file 
+        ENDIF
+        
+        !write(numout,*) 'dct isec', isec
+        
         WRITE(cltmp,'(a,i4.4,a,i4.4)')'diadct: read sections : Problem of section number: isec= ',isec,' and jsec= ',jsec
+        !WRITE(numout,'(a,i4.4,a,i4.4)')'diadct: read sections : Problem of section number: isec= ',isec,' and jsec= ',jsec
+        
+        !write(numout,*) 'dct isec, jsec', isec,jsec
+        
+        
         IF( jsec .NE. isec )  CALL ctl_stop( cltmp )
 
-        IF( lwp .AND. ( jsec==nn_secdebug .OR. nn_secdebug==-1 ) )WRITE(numout,*)"isec ",isec 
+        !IF( lwp .AND. ( jsec==nn_secdebug .OR. nn_secdebug==-1 ) )WRITE(numout,*)"isec ",isec 
 
         READ(numdct_in)secs(jsec)%name
@@ -388,5 +575,5 @@
 
             WRITE(numout,*)       "   Section name :                       ",TRIM(secs(jsec)%name)
-            WRITE(numout,*)       "      Compute heat and salt transport ? ",secs(jsec)%llstrpond
+            WRITE(numout,*)       "      Compute temperature and salinity transports ? ",secs(jsec)%llstrpond
             WRITE(numout,*)       "      Compute ice transport ?           ",secs(jsec)%ll_ice_section
             WRITE(numout,*)       "      Section crosses date-line ?       ",secs(jsec)%ll_date_line
@@ -399,5 +586,6 @@
             WRITE(numout,clformat)"      Temperature classes :             ",secs(jsec)%ztem
             WRITE(numout,clformat)"      Depth classes :                   ",secs(jsec)%zlay
-        ENDIF               
+        ENDIF     
+        
 
         IF( iptglo .NE. 0 )THEN
@@ -411,4 +599,5 @@
               coordtemp(jpt)%I = i1 
               coordtemp(jpt)%J = i2
+              !WRITE(numout,*)'diadct: coordtemp:', jpt,i1,i2
            ENDDO
            READ(numdct_in)directemp(1:iptglo)
@@ -432,11 +621,29 @@
 
               IF( iiglo==jpidta .AND. nimpp==1 ) iiglo = 2
-
+              
+              !WRITE(numout,*)"diadct readsec: reg/glo domain:",narea,nlci,nimpp,mig(1),nlcj,njmpp,mjg(1)
+
+      !diadct init: reg/glo domain: 3*1,  24,  22,  24,  22,  2*1
               iiloc=iiglo-jpizoom+1-nimpp+1   ! local coordinates of the point
               ijloc=ijglo-jpjzoom+1-njmpp+1   !  "
-
+              !WRITE(numout,*)"      List of limits of each processor:",jpt,nimpp,iiloc,iiglo,jpizoom+1,nimpp+1,ijloc,ijglo,jpjzoom+1,njmpp+1,nlei,nlej
+
+               
+              !WRITE(*,*)"diadct readsec: ii: ",narea, jpt,iiglo,mig(1),mig(jpi),nlci
+              !WRITE(*,*)"diadct readsec: jj: ",narea, jpt,ijglo,mjg(1),mjg(jpj),nlcj
+              !WRITE(*,*)"diadct readsec: log: ",narea, iiglo .GE. mig(1),iiglo .LE. mig(1)+nlci-1 ,ijglo .GE. mjg(1),ijglo .LE. mjg(1)+nlcj-1  
+              !WRITE(*,*)"diadct readsec: log: ",narea, iiglo .GE. mig(1),iiglo .LE. mig(jpi),ijglo .GE. mjg(1),ijglo .LE. mjg(jpj)
               !verify if the point is on the local domain:(1,nlei)*(1,nlej)
               IF( iiloc .GE. 1 .AND. iiloc .LE. nlei .AND. &
                   ijloc .GE. 1 .AND. ijloc .LE. nlej       )THEN
+              !IF( iiglo .GE. mig(1) .AND. iiglo .LE. mig(1)+nlci-1 .AND. &
+              !    ijglo .GE. mjg(1) .AND. ijglo .LE. mjg(1)+nlcj-1       )THEN
+                  
+                  
+              !IF( iiglo .GE. mig(1) .AND. iiglo .LE. mig(jpi) .AND. &
+              !    ijglo .GE. mjg(1) .AND. ijglo .LE. mjg(jpj)       )THEN
+                  WRITE(*,*)"diadct readsec: assigned proc!",narea,nproc,jpt
+                  
+                  
                  iptloc = iptloc + 1                                                 ! count local points
                  secs(jsec)%listPoint(iptloc) = POINT_SECTION(mi0(iiglo),mj0(ijglo)) ! store local coordinates
@@ -459,10 +666,10 @@
            ENDIF
 
-              IF(jsec==nn_secdebug .AND. secs(jsec)%nb_point .NE. 0)THEN
+           IF(jsec==nn_secdebug .AND. secs(jsec)%nb_point .NE. 0)THEN
               DO jpt = 1,iptloc
                  iiglo = secs(jsec)%listPoint(jpt)%I + jpizoom - 1 + nimpp - 1
                  ijglo = secs(jsec)%listPoint(jpt)%J + jpjzoom - 1 + njmpp - 1
               ENDDO
-              ENDIF
+           ENDIF
 
            !remove redundant points between processors
@@ -501,8 +708,13 @@
 
      ENDDO !end of the loop on jsec
+     
+     !IF (lwp) write(numout,*) 'dct end of readsec loop, after exit'
  
      nb_sec = jsec-1   !number of section read in the file
 
      CALL wrk_dealloc( nb_point_max, directemp )
+     
+     !IF (lwp) write(numout,*) 'dct after dealloc'
+     
      !
   END SUBROUTINE readsec
@@ -586,4 +798,5 @@
      CALL wrk_dealloc(    nb_point_max, idirec )
      CALL wrk_dealloc( 2, nb_point_max, icoord )
+
   END SUBROUTINE removepoints
 
@@ -592,18 +805,18 @@
      !!                     ***  ROUTINE transport  ***
      !!
-     !!  Purpose ::  Compute the transport for each point in a section 
+     !!  Purpose ::  Compute the transport for each point in a section
+     !!
+     !!  Method  ::  Loop over each segment, and each vertical level and add the transport
+     !!              Be aware :          
+     !!              One section is a sum of segments
+     !!              One segment is defined by 2 consecutive points in sec%listPoint
+     !!              All points of sec%listPoint are positioned on the F-point of the cell
      !! 
-     !!  Method  ::  Loop over each segment, and each vertical level and add the transport 
-     !!              Be aware :           
-     !!              One section is a sum of segments 
-     !!              One segment is defined by 2 consecutive points in sec%listPoint 
-     !!              All points of sec%listPoint are positioned on the F-point of the cell 
-     !! 
-     !!              There are two loops:                  
-     !!              loop on the segment between 2 nodes 
-     !!              loop on the level jk !!
-     !! 
-     !!  Output  ::  Arrays containing the volume,density,heat,salt transports for each i
-     !!              point in a section, summed over each nn_dct. 
+     !!              There are two loops:                 
+     !!              loop on the segment between 2 nodes
+     !!              loop on the level jk
+     !!
+     !! ** Output: Arrays containing the volume,density,salinity,temperature etc
+     !!            transports for each point in a section, summed over each nn_dct.
      !!
      !!-------------------------------------------------------------------------------------------
@@ -614,15 +827,15 @@
     
      !! * Local variables
-     INTEGER             :: jk, jseg, jclass,jl,                 &!loop on level/segment/classes/ice categories
-                            isgnu, isgnv                          ! 
-     REAL(wp)            :: zumid, zvmid,                        &!U/V velocity on a cell segment 
-                            zumid_ice, zvmid_ice,                &!U/V ice velocity 
-                            zTnorm                                !transport of velocity through one cell's sides 
-     REAL(wp)            :: ztn, zsn, zrhoi, zrhop, zsshn, zfsdep !temperature/salinity/potential density/ssh/depth at u/v point
+     INTEGER             :: jk,jseg,jclass,    &!loop on level/segment/classes 
+                            isgnu  , isgnv      !
+     REAL(wp):: zumid        , zvmid        ,  &!U/V velocity on a cell segment
+                zumid_ice    , zvmid_ice    ,  &!U/V ice velocity
+                zTnorm                          !transport of velocity through one cell's sides
+     REAL(wp):: ztn, zsn, zrhoi, zrhop, zsshn, zfsdep ! temperature/salinity/ssh/potential density /depth at u/v point
 
      TYPE(POINT_SECTION) :: k
      !!--------------------------------------------------------
 
-     IF( ld_debug )WRITE(numout,*)'      Compute transport'
+     IF( ld_debug )WRITE(numout,*)'      Compute transport (jsec,sec%nb_point,sec%slopeSection) : ', jsec,sec%nb_point,sec%slopeSection
 
      !---------------------------!
@@ -701,59 +914,61 @@
            END SELECT
 
-           !---------------------------| 
-           !     LOOP ON THE LEVEL     | 
-           !---------------------------| 
-           !Sum of the transport on the vertical  
-           DO jk=1,mbathy(k%I,k%J) 
+           !---------------------------|
+           !     LOOP ON THE LEVEL     |
+           !---------------------------|
+           !Sum of the transport on the vertical 
+           DO jk=1,mbathy(k%I,k%J)
  
               ! compute temperature, salinity, insitu & potential density, ssh and depth at U/V point 
-              SELECT CASE( sec%direction(jseg) ) 
-              CASE(0,1) 
-                 ztn   = interp(k%I,k%J,jk,'V',tsn(:,:,:,jp_tem) ) 
-                 zsn   = interp(k%I,k%J,jk,'V',tsn(:,:,:,jp_sal) ) 
-                 zrhop = interp(k%I,k%J,jk,'V',rhop) 
-                 zrhoi = interp(k%I,k%J,jk,'V',rhd*rau0+rau0) 
-                 zsshn =  0.5*( sshn(k%I,k%J)    + sshn(k%I,k%J+1)    ) * vmask(k%I,k%J,1) 
-              CASE(2,3) 
-                 ztn   = interp(k%I,k%J,jk,'U',tsn(:,:,:,jp_tem) ) 
-                 zsn   = interp(k%I,k%J,jk,'U',tsn(:,:,:,jp_sal) ) 
-                 zrhop = interp(k%I,k%J,jk,'U',rhop) 
-                 zrhoi = interp(k%I,k%J,jk,'U',rhd*rau0+rau0) 
-                 zsshn =  0.5*( sshn(k%I,k%J)    + sshn(k%I+1,k%J)    ) * umask(k%I,k%J,1)  
-              END SELECT 
- 
-              zfsdep= fsdept(k%I,k%J,jk) 
-  
-              !compute velocity with the correct direction 
-              SELECT CASE( sec%direction(jseg) ) 
-              CASE(0,1)   
-                 zumid=0. 
-                 zvmid=isgnv*vn(k%I,k%J,jk)*vmask(k%I,k%J,jk) 
-              CASE(2,3) 
-                 zumid=isgnu*un(k%I,k%J,jk)*umask(k%I,k%J,jk) 
-                 zvmid=0. 
-              END SELECT 
- 
-              !zTnorm=transport through one cell; 
-              !velocity* cell's length * cell's thickness 
-              zTnorm=zumid*e2u(k%I,k%J)*  fse3u(k%I,k%J,jk)+     & 
-                     zvmid*e1v(k%I,k%J)*  fse3v(k%I,k%J,jk) 
+              SELECT CASE( sec%direction(jseg) )
+              CASE(0,1)
+                 ztn   = interp(k%I,k%J,jk,'V',tsn(:,:,:,jp_tem) )
+                 zsn   = interp(k%I,k%J,jk,'V',tsn(:,:,:,jp_sal) )
+                 zrhop = interp(k%I,k%J,jk,'V',rhop)
+                 zrhoi = interp(k%I,k%J,jk,'V',rhd*rau0+rau0)
+                 zsshn =  0.5*( sshn(k%I,k%J)    + sshn(k%I,k%J+1)    ) * vmask(k%I,k%J,1)
+              CASE(2,3)
+                 ztn   = interp(k%I,k%J,jk,'U',tsn(:,:,:,jp_tem) )
+                 zsn   = interp(k%I,k%J,jk,'U',tsn(:,:,:,jp_sal) )
+                 zrhop = interp(k%I,k%J,jk,'U',rhop)
+                 zrhoi = interp(k%I,k%J,jk,'U',rhd*rau0+rau0)
+                 zsshn =  0.5*( sshn(k%I,k%J)    + sshn(k%I+1,k%J)    ) * umask(k%I,k%J,1) 
+              END SELECT
+
+              zfsdep= fsdept(k%I,k%J,jk)
+ 
+              !compute velocity with the correct direction
+              SELECT CASE( sec%direction(jseg) )
+              CASE(0,1)  
+                 zumid=0.
+                 zvmid=isgnv*vn(k%I,k%J,jk)*vmask(k%I,k%J,jk)
+              CASE(2,3)
+                 zumid=isgnu*un(k%I,k%J,jk)*umask(k%I,k%J,jk)
+                 zvmid=0.
+              END SELECT
+
+              !zTnorm=transport through one cell;
+              !velocity* cell's length * cell's thickness
+              zTnorm=zumid*e2u(k%I,k%J)*  fse3u(k%I,k%J,jk)+     &
+                     zvmid*e1v(k%I,k%J)*  fse3v(k%I,k%J,jk)
 
 #if ! defined key_vvl
-              !add transport due to free surface 
-              IF( jk==1 )THEN 
-                 zTnorm = zTnorm + zumid* e2u(k%I,k%J) * zsshn * umask(k%I,k%J,jk) + & 
-                                   zvmid* e1v(k%I,k%J) * zsshn * vmask(k%I,k%J,jk) 
-              ENDIF 
+              !add transport due to free surface
+              IF( jk==1 )THEN
+                 zTnorm = zTnorm + zumid* e2u(k%I,k%J) * zsshn * umask(k%I,k%J,jk) + &
+                                   zvmid* e1v(k%I,k%J) * zsshn * vmask(k%I,k%J,jk)
+              ENDIF
 #endif
-              !COMPUTE TRANSPORT  
- 
-              transports_3d(1,jsec,jseg,jk) = transports_3d(1,jsec,jseg,jk) + zTnorm 
-  
-              IF ( sec%llstrpond ) THEN 
-                 transports_3d(2,jsec,jseg,jk) = transports_3d(2,jsec,jseg,jk)  + zTnorm * ztn * zrhop * rcp
-                 transports_3d(3,jsec,jseg,jk) = transports_3d(3,jsec,jseg,jk)  + zTnorm * zsn * zrhop * 0.001
+              !COMPUTE TRANSPORT 
+
+              transports_3d(1,jsec,jseg,jk) = transports_3d(1,jsec,jseg,jk) + zTnorm
+ 
+              IF ( sec%llstrpond ) THEN
+                 transports_3d(2,jsec,jseg,jk) = transports_3d(2,jsec,jseg,jk)  + zTnorm * zrhoi
+                 transports_3d(3,jsec,jseg,jk) = transports_3d(3,jsec,jseg,jk)  + zTnorm * zrhop
+                 transports_3d(4,jsec,jseg,jk) = transports_3d(4,jsec,jseg,jk)  + zTnorm * 4.e+3_wp * (ztn+273.15) * 1026._wp
+                 transports_3d(5,jsec,jseg,jk) = transports_3d(5,jsec,jseg,jk)  + zTnorm * 0.001 * zsn * 1026._wp
               ENDIF
-   
+
            ENDDO !end of loop on the level
 
@@ -779,24 +994,13 @@
    
               zTnorm=zumid_ice*e2u(k%I,k%J)+zvmid_ice*e1v(k%I,k%J)
-
-#if defined key_lim2   
-              transports_2d(1,jsec,jseg) = transports_2d(1,jsec,jseg) + (zTnorm)*   & 
-                                   (1.0 - frld(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J))  & 
-                                  *(hsnif(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J) +  & 
-                                    hicif(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J))
-              transports_2d(2,jsec,jseg) = transports_2d(2,jsec,jseg) + (zTnorm)*   & 
-                                    (1.0 -  frld(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J))
-#endif
-#if defined key_lim3
-              DO jl=1,jpl
-                 transports_2d(1,jsec,jseg) = transports_2d(1,jsec,jseg) + (zTnorm)*     &
-                                   a_i(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J,jl) * &
-                                  ( ht_i(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J,jl) +  &
-                                    ht_s(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J,jl) )
-                                   
-                 transports_2d(2,jsec,jseg) = transports_2d(2,jsec,jseg) + (zTnorm)*   &
-                                   a_i(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J,jl)
-              ENDDO
-#endif
+   
+              transports_2d(1,jsec,jseg) = transports_2d(1,jsec,jseg) + (zTnorm)*   &
+                                   (1.0 - frld(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J))  &
+                                  *(hsnif(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J) +  &
+                                    hicif(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J)) &
+                                   +zice_vol_pos
+              transports_2d(2,jsec,jseg) = transports_2d(2,jsec,jseg) + (zTnorm)*   &
+                                    (1.0 -  frld(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J))  &
+                                   +zice_surf_pos
    
            ENDIF !end of ice case
@@ -805,200 +1009,848 @@
         ENDDO !end of loop on the segment
 
-     ENDIF !end of sec%nb_point =0 case
+     ENDIF   !end of sec%nb_point =0 case
      !
   END SUBROUTINE transport
-  
-  SUBROUTINE dia_dct_sum(sec,jsec) 
+
+  SUBROUTINE transport_h(sec,ld_debug,jsec)
+     !!-------------------------------------------------------------------------------------------
+     !!                     ***  ROUTINE hourly_transport  ***
+     !!
+     !!              Exactly as routine transport but for data summed at
+     !!              each time step and averaged each hour
+     !!
+     !!  Purpose ::  Compute the transport for each point in a section
+     !!
+     !!  Method  ::  Loop over each segment, and each vertical level and add the transport
+     !!              Be aware :          
+     !!              One section is a sum of segments
+     !!              One segment is defined by 2 consecutive points in sec%listPoint
+     !!              All points of sec%listPoint are positioned on the F-point of the cell
+     !! 
+     !!              There are two loops:                 
+     !!              loop on the segment between 2 nodes
+     !!              loop on the level jk
+     !!
+     !! ** Output: Arrays containing the volume,density,salinity,temperature etc
+     !!            transports for each point in a section, summed over each nn_dct.
+     !!
+     !!-------------------------------------------------------------------------------------------
+     !! * Arguments
+     TYPE(SECTION),INTENT(INOUT) :: sec
+     LOGICAL      ,INTENT(IN)    :: ld_debug
+     INTEGER      ,INTENT(IN)    :: jsec        ! numeric identifier of section
+    
+     !! * Local variables
+     INTEGER             :: jk,jseg,jclass,    &!loop on level/segment/classes 
+                            isgnu  , isgnv      !
+     REAL(wp):: zumid        , zvmid        ,  &!U/V velocity on a cell segment
+                zumid_ice    , zvmid_ice    ,  &!U/V ice velocity
+                zTnorm                          !transport of velocity through one cell's sides
+     REAL(wp):: ztn, zsn, zrhoi, zrhop, zsshn, zfsdep ! temperature/salinity/ssh/potential density /depth at u/v point
+
+     TYPE(POINT_SECTION) :: k
+     !!--------------------------------------------------------
+
+     IF( ld_debug )WRITE(numout,*)'      Compute transport'
+
+     !---------------------------!
+     !  COMPUTE TRANSPORT        !
+     !---------------------------!
+     IF(sec%nb_point .NE. 0)THEN   
+
+        !----------------------------------------------------------------------------------------------------
+        !Compute sign for velocities:
+        !
+        !convention:
+        !   non horizontal section: direction + is toward left hand of section
+        !       horizontal section: direction + is toward north of section
+        !
+        !
+        !       slopeSection < 0     slopeSection > 0       slopeSection=inf            slopeSection=0
+        !       ----------------      -----------------     ---------------             --------------
+        !
+        !   isgnv=1         direction +      
+        !  ______         _____             ______                                                   
+        !        |           //|            |                  |                         direction +   
+        !        | isgnu=1  // |            |isgnu=1           |isgnu=1                     /|\
+        !        |_______  //         ______|    \\            | ---\                        |
+        !               |             | isgnv=-1  \\ |         | ---/ direction +       ____________
+        !               |             |          __\\|         |                    
+        !               |             |     direction +        |                      isgnv=1                                 
+        !                                                      
+        !----------------------------------------------------------------------------------------------------
+        isgnu = 1
+        IF( sec%slopeSection .GT. 0 ) THEN  ; isgnv = -1 
+        ELSE                                ; isgnv =  1
+        ENDIF
+
+        IF( ld_debug )write(numout,*)"isgnu isgnv ",isgnu,isgnv
+
+        !--------------------------------------!
+        ! LOOP ON THE SEGMENT BETWEEN 2 NODES  !
+        !--------------------------------------!
+        DO jseg=1,MAX(sec%nb_point-1,0)
+              
+           !-------------------------------------------------------------------------------------------
+           ! Select the appropriate coordinate for computing the velocity of the segment
+           !
+           !                      CASE(0)                                    Case (2)
+           !                      -------                                    --------
+           !  listPoint(jseg)                 listPoint(jseg+1)       listPoint(jseg)  F(i,j)      
+           !      F(i,j)----------V(i+1,j)-------F(i+1,j)                               |
+           !                                                                            |
+           !                                                                            |
+           !                                                                            |
+           !                      Case (3)                                            U(i,j)
+           !                      --------                                              |
+           !                                                                            |
+           !  listPoint(jseg+1) F(i,j+1)                                                |
+           !                        |                                                   |
+           !                        |                                                   |
+           !                        |                                 listPoint(jseg+1) F(i,j-1)
+           !                        |                                            
+           !                        |                                            
+           !                     U(i,j+1)                                            
+           !                        |                                       Case(1)     
+           !                        |                                       ------      
+           !                        |                                            
+           !                        |                 listPoint(jseg+1)             listPoint(jseg)                           
+           !                        |                 F(i-1,j)-----------V(i,j) -------f(jseg)                           
+           ! listPoint(jseg)     F(i,j)
+           ! 
+           !-------------------------------------------------------------------------------------------
+
+           SELECT CASE( sec%direction(jseg) )
+           CASE(0)  ;   k = sec%listPoint(jseg)
+           CASE(1)  ;   k = POINT_SECTION(sec%listPoint(jseg)%I+1,sec%listPoint(jseg)%J)
+           CASE(2)  ;   k = sec%listPoint(jseg)
+           CASE(3)  ;   k = POINT_SECTION(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J+1)
+           END SELECT
+
+           !---------------------------|
+           !     LOOP ON THE LEVEL     |
+           !---------------------------|
+           !Sum of the transport on the vertical 
+           DO jk=1,mbathy(k%I,k%J)
+
+              ! compute temparature, salinity, insitu & potential density, ssh and depth at U/V point
+              SELECT CASE( sec%direction(jseg) )
+              CASE(0,1)
+                 ztn   = interp(k%I,k%J,jk,'V',tsn(:,:,:,jp_tem) )
+                 zsn   = interp(k%I,k%J,jk,'V',tsn(:,:,:,jp_sal) )
+                 zrhop = interp(k%I,k%J,jk,'V',rhop)
+                 zrhoi = interp(k%I,k%J,jk,'V',rhd*rau0+rau0)
+                 zsshn =  0.5*( sshn(k%I,k%J)    + sshn(k%I,k%J+1)    ) * vmask(k%I,k%J,1)
+              CASE(2,3)
+                 ztn   = interp(k%I,k%J,jk,'U',tsn(:,:,:,jp_tem) )
+                 zsn   = interp(k%I,k%J,jk,'U',tsn(:,:,:,jp_sal) )
+                 zrhop = interp(k%I,k%J,jk,'U',rhop)
+                 zrhoi = interp(k%I,k%J,jk,'U',rhd*rau0+rau0)
+                 zsshn =  0.5*( sshn(k%I,k%J)    + sshn(k%I+1,k%J)    ) * umask(k%I,k%J,1) 
+              END SELECT
+
+              !JT zfsdep= gdept(k%I,k%J,jk)
+              !JT zfsdep= gdept_0(k%I,k%J,jk)
+              zfsdep= fsdept(k%I,k%J,jk)
+ 
+              !compute velocity with the correct direction
+              SELECT CASE( sec%direction(jseg) )
+              CASE(0,1)  
+                 zumid=0.
+                 zvmid=isgnv*vn(k%I,k%J,jk)*vmask(k%I,k%J,jk)
+              CASE(2,3)
+                 zumid=isgnu*un(k%I,k%J,jk)*umask(k%I,k%J,jk)
+                 zvmid=0.
+              END SELECT
+
+              !zTnorm=transport through one cell;
+              !velocity* cell's length * cell's thickness
+              zTnorm=zumid*e2u(k%I,k%J)*  fse3u(k%I,k%J,jk)+     &
+                     zvmid*e1v(k%I,k%J)*  fse3v(k%I,k%J,jk)
+
+#if ! defined key_vvl
+              !add transport due to free surface
+              IF( jk==1 )THEN
+                 zTnorm = zTnorm + zumid* e2u(k%I,k%J) * zsshn * umask(k%I,k%J,jk) + &
+                                   zvmid* e1v(k%I,k%J) * zsshn * vmask(k%I,k%J,jk)
+              ENDIF
+#endif
+              !COMPUTE TRANSPORT 
+
+              transports_3d_h(1,jsec,jseg,jk) = transports_3d_h(1,jsec,jseg,jk) + zTnorm
+ 
+              IF ( sec%llstrpond ) THEN
+                 transports_3d_h(2,jsec,jseg,jk) = transports_3d_h(2,jsec,jseg,jk)  + zTnorm * zrhoi
+                 transports_3d_h(3,jsec,jseg,jk) = transports_3d_h(3,jsec,jseg,jk)  + zTnorm * zrhop
+                 transports_3d_h(4,jsec,jseg,jk) = transports_3d_h(4,jsec,jseg,jk)  + zTnorm * 4.e+3_wp * (ztn+273.15) * 1026._wp
+                 transports_3d_h(5,jsec,jseg,jk) = transports_3d_h(5,jsec,jseg,jk)  + zTnorm * 0.001 * zsn * 1026._wp
+              ENDIF
+
+           ENDDO !end of loop on the level
+
+#if defined key_lim2 || defined key_lim3
+
+           !ICE CASE    
+           !------------
+           IF( sec%ll_ice_section )THEN
+              SELECT CASE (sec%direction(jseg))
+              CASE(0)
+                 zumid_ice = 0
+                 zvmid_ice =  isgnv*0.5*(v_ice(k%I,k%J+1)+v_ice(k%I+1,k%J+1))
+              CASE(1)
+                 zumid_ice = 0
+                 zvmid_ice =  isgnv*0.5*(v_ice(k%I,k%J+1)+v_ice(k%I+1,k%J+1))
+              CASE(2)
+                 zvmid_ice = 0
+                 zumid_ice =  isgnu*0.5*(u_ice(k%I+1,k%J)+u_ice(k%I+1,k%J+1))
+              CASE(3)
+                 zvmid_ice = 0
+                 zumid_ice =  isgnu*0.5*(u_ice(k%I+1,k%J)+u_ice(k%I+1,k%J+1))
+              END SELECT
+   
+              zTnorm=zumid_ice*e2u(k%I,k%J)+zvmid_ice*e1v(k%I,k%J)
+   
+              transports_2d_h(1,jsec,jseg) = transports_2d_h(1,jsec,jseg) + (zTnorm)*   &
+                                   (1.0 - frld(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J))  &
+                                  *(hsnif(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J) +  &
+                                    hicif(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J)) &
+                                   +zice_vol_pos
+              transports_2d_h(2,jsec,jseg) = transports_2d_h(2,jsec,jseg) + (zTnorm)*   &
+                                    (1.0 -  frld(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J))  &
+                                   +zice_surf_pos
+   
+           ENDIF !end of ice case
+#endif
+ 
+        ENDDO !end of loop on the segment
+
+     ENDIF   !end of sec%nb_point =0 case
+     !
+  END SUBROUTINE transport_h
+ 
+  SUBROUTINE dia_dct_sum(sec,jsec)
+     !!-------------------------------------------------------------
+     !! Purpose: Average the transport over nn_dctwri time steps 
+     !! and sum over the density/salinity/temperature/depth classes
+     !!
+     !! Method: 
+     !!           Sum over relevant grid cells to obtain values
+     !!           for each
+     !!              There are several loops:                 
+     !!              loop on the segment between 2 nodes
+     !!              loop on the level jk
+     !!              loop on the density/temperature/salinity/level classes
+     !!              test on the density/temperature/salinity/level
+     !!
+     !!  ** Method  :Transport through a given section is equal to the sum of transports
+     !!              computed on each proc.
+     !!              On each proc,transport is equal to the sum of transport computed through
+     !!              segments linking each point of sec%listPoint  with the next one.   
+     !!
+     !!-------------------------------------------------------------
+     !! * arguments
+     TYPE(SECTION),INTENT(INOUT) :: sec
+     INTEGER      ,INTENT(IN)    :: jsec        ! numeric identifier of section
+
+     TYPE(POINT_SECTION) :: k
+     INTEGER  :: jk,jseg,jclass                        !loop on level/segment/classes 
+     REAL(wp) :: ztn, zsn, zrhoi, zrhop, zsshn, zfsdep ! temperature/salinity/ssh/potential density /depth at u/v point
+     !!-------------------------------------------------------------
+
+     !! Sum the relevant segments to obtain values for each class
+     IF(sec%nb_point .NE. 0)THEN   
+
+        !--------------------------------------!
+        ! LOOP ON THE SEGMENT BETWEEN 2 NODES  !
+        !--------------------------------------!
+        DO jseg=1,MAX(sec%nb_point-1,0)
+           
+           !-------------------------------------------------------------------------------------------
+           ! Select the appropriate coordinate for computing the velocity of the segment
+           !
+           !                      CASE(0)                                    Case (2)
+           !                      -------                                    --------
+           !  listPoint(jseg)                 listPoint(jseg+1)       listPoint(jseg)  F(i,j)      
+           !      F(i,j)----------V(i+1,j)-------F(i+1,j)                               |
+           !                                                                            |
+           !                                                                            |
+           !                                                                            |
+           !                      Case (3)                                            U(i,j)
+           !                      --------                                              |
+           !                                                                            |
+           !  listPoint(jseg+1) F(i,j+1)                                                |
+           !                        |                                                   |
+           !                        |                                                   |
+           !                        |                                 listPoint(jseg+1) F(i,j-1)
+           !                        |                                            
+           !                        |                                            
+           !                     U(i,j+1)                                            
+           !                        |                                       Case(1)     
+           !                        |                                       ------      
+           !                        |                                            
+           !                        |                 listPoint(jseg+1)             listPoint(jseg)                           
+           !                        |                 F(i-1,j)-----------V(i,j) -------f(jseg)                           
+           ! listPoint(jseg)     F(i,j)
+           ! 
+           !-------------------------------------------------------------------------------------------
+
+           SELECT CASE( sec%direction(jseg) )
+           CASE(0)  ;   k = sec%listPoint(jseg)
+           CASE(1)  ;   k = POINT_SECTION(sec%listPoint(jseg)%I+1,sec%listPoint(jseg)%J)
+           CASE(2)  ;   k = sec%listPoint(jseg)
+           CASE(3)  ;   k = POINT_SECTION(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J+1)
+           END SELECT
+
+           !---------------------------|
+           !     LOOP ON THE LEVEL     |
+           !---------------------------|
+           !Sum of the transport on the vertical 
+           DO jk=1,mbathy(k%I,k%J)
+
+              ! compute temperature, salinity, insitu & potential density, ssh and depth at U/V point
+              SELECT CASE( sec%direction(jseg) )
+              CASE(0,1)
+                 ztn   = interp(k%I,k%J,jk,'V',tsn(:,:,:,jp_tem) )
+                 zsn   = interp(k%I,k%J,jk,'V',tsn(:,:,:,jp_sal) )
+                 zrhop = interp(k%I,k%J,jk,'V',rhop)
+                 zrhoi = interp(k%I,k%J,jk,'V',rhd*rau0+rau0)
+                 zsshn =  0.5*( sshn(k%I,k%J)    + sshn(k%I,k%J+1)    ) * vmask(k%I,k%J,1)
+              CASE(2,3)
+                 ztn   = interp(k%I,k%J,jk,'U',tsn(:,:,:,jp_tem) )
+                 zsn   = interp(k%I,k%J,jk,'U',tsn(:,:,:,jp_sal) )
+                 zrhop = interp(k%I,k%J,jk,'U',rhop)
+                 zrhoi = interp(k%I,k%J,jk,'U',rhd*rau0+rau0)
+                 zsshn =  0.5*( sshn(k%I,k%J)    + sshn(k%I+1,k%J)    ) * umask(k%I,k%J,1) 
+              END SELECT
+
+              !JT zfsdep= gdept(k%I,k%J,jk)
+              !JT zfsdep= gdept_0(k%I,k%J,jk)
+              zfsdep= fsdept(k%I,k%J,jk) 
+ 
+              !-------------------------------
+              !  LOOP ON THE DENSITY CLASSES |
+              !-------------------------------
+              !The computation is made for each density/temperature/salinity/depth class
+              DO jclass=1,MAX(1,sec%nb_class-1)
+ 
+                 !----------------------------------------------!
+                 !TEST ON THE DENSITY/SALINITY/TEMPERATURE/LEVEL! 
+                 !----------------------------------------------!
+ 
+                 IF ( (                                                    &
+                    ((( zrhop .GE. (sec%zsigp(jclass)+1000.  )) .AND.      &
+                    (   zrhop .LE. (sec%zsigp(jclass+1)+1000. ))) .OR.     &
+                    ( sec%zsigp(jclass) .EQ. 99.)) .AND.                   &
+
+                    ((( zrhoi .GE. (sec%zsigi(jclass) + 1000.  )) .AND.    &
+                    (   zrhoi .LE. (sec%zsigi(jclass+1)+1000. ))) .OR.     &
+                    ( sec%zsigi(jclass) .EQ. 99.)) .AND.                   &
+
+                    ((( zsn .GT. sec%zsal(jclass)) .AND.                   &
+                    (   zsn .LE. sec%zsal(jclass+1))) .OR.                 &
+                    ( sec%zsal(jclass) .EQ. 99.)) .AND.                    &
+
+                    ((( ztn .GE. sec%ztem(jclass)) .AND.                   &
+                    (   ztn .LE. sec%ztem(jclass+1))) .OR.                 &
+                    ( sec%ztem(jclass) .EQ.99.)) .AND.                     &
+
+                    ((( zfsdep .GE. sec%zlay(jclass)) .AND.                &
+                    (   zfsdep .LE. sec%zlay(jclass+1))) .OR.              &
+                    ( sec%zlay(jclass) .EQ. 99. ))                         &
+                                                                   ))   THEN
+
+                    !SUM THE TRANSPORTS FOR EACH CLASSES FOR THE POSITIVE AND NEGATIVE DIRECTIONS
+                    !----------------------------------------------------------------------------
+                    IF (transports_3d(1,jsec,jseg,jk) .GE. 0.0) THEN 
+                       sec%transport(1,jclass) = sec%transport(1,jclass)+transports_3d(1,jsec,jseg,jk)
+                    ELSE
+                       sec%transport(2,jclass) = sec%transport(2,jclass)+transports_3d(1,jsec,jseg,jk)
+                    ENDIF
+                    IF( sec%llstrpond )THEN
+
+                       IF( transports_3d(1,jsec,jseg,jk) .NE. 0._wp ) THEN
+
+                          IF (transports_3d(2,jsec,jseg,jk)/transports_3d(1,jsec,jseg,jk) .GE. 0.0 ) THEN
+                             sec%transport(3,jclass) = sec%transport(3,jclass)+transports_3d(2,jsec,jseg,jk)/transports_3d(1,jsec,jseg,jk)
+                          ELSE
+                             sec%transport(4,jclass) = sec%transport(4,jclass)+transports_3d(2,jsec,jseg,jk)/transports_3d(1,jsec,jseg,jk)
+                          ENDIF
+
+                          IF ( transports_3d(3,jsec,jseg,jk)/transports_3d(1,jsec,jseg,jk) .GE. 0.0 ) THEN
+                             sec%transport(5,jclass) = sec%transport(5,jclass)+transports_3d(3,jsec,jseg,jk)/transports_3d(1,jsec,jseg,jk)
+                          ELSE
+                             sec%transport(6,jclass) = sec%transport(6,jclass)+transports_3d(3,jsec,jseg,jk)/transports_3d(1,jsec,jseg,jk)
+                          ENDIF
+
+                       ENDIF
+
+                       IF ( transports_3d(4,jsec,jseg,jk) .GE. 0.0 ) THEN
+                          sec%transport(7,jclass) = sec%transport(7,jclass)+transports_3d(4,jsec,jseg,jk)
+                       ELSE
+                          sec%transport(8,jclass) = sec%transport(8,jclass)+transports_3d(4,jsec,jseg,jk)
+                       ENDIF
+
+                       IF ( transports_3d(5,jsec,jseg,jk) .GE. 0.0 ) THEN
+                          sec%transport( 9,jclass) = sec%transport( 9,jclass)+transports_3d(5,jsec,jseg,jk)
+                       ELSE
+                          sec%transport(10,jclass) = sec%transport(10,jclass)+transports_3d(5,jsec,jseg,jk)
+                       ENDIF
+
+                    ELSE
+                       sec%transport( 3,jclass) = 0._wp
+                       sec%transport( 4,jclass) = 0._wp
+                       sec%transport( 5,jclass) = 0._wp
+                       sec%transport( 6,jclass) = 0._wp
+                       sec%transport( 7,jclass) = 0._wp
+                       sec%transport( 8,jclass) = 0._wp
+                       sec%transport( 9,jclass) = 0._wp
+                       sec%transport(10,jclass) = 0._wp
+                    ENDIF
+
+                 ENDIF ! end of test if point is in class
+   
+              ENDDO ! end of loop on the classes
+
+           ENDDO ! loop over jk
+
+#if defined key_lim2 || defined key_lim3
+
+           !ICE CASE    
+           IF( sec%ll_ice_section )THEN
+
+              IF ( transports_2d(1,jsec,jseg) .GE. 0.0 ) THEN
+                 sec%transport(11,1) = sec%transport(11,1)+transports_2d(1,jsec,jseg)
+              ELSE
+                 sec%transport(12,1) = sec%transport(12,1)+transports_2d(1,jsec,jseg)
+              ENDIF
+
+              IF ( transports_2d(3,jsec,jseg) .GE. 0.0 ) THEN
+                 sec%transport(13,1) = sec%transport(13,1)+transports_2d(2,jsec,jseg)
+              ELSE
+                 sec%transport(14,1) = sec%transport(14,1)+transports_2d(2,jsec,jseg)
+              ENDIF
+
+           ENDIF !end of ice case
+#endif
+ 
+        ENDDO !end of loop on the segment
+
+     ELSE  !if sec%nb_point =0
+        sec%transport(1:2,:)=0.
+        IF (sec%llstrpond) sec%transport(3:10,:)=0.
+        IF (sec%ll_ice_section) sec%transport( 11:14,:)=0.
+     ENDIF !end of sec%nb_point =0 case
+
+  END SUBROUTINE dia_dct_sum
+ 
+  SUBROUTINE dia_dct_sum_h(sec,jsec)
+     !!-------------------------------------------------------------
+     !! Exactly as dia_dct_sum but for hourly files containing data summed at each time step
+     !!
+     !! Purpose: Average the transport over nn_dctwri time steps 
+     !! and sum over the density/salinity/temperature/depth classes
+     !!
+     !! Method: 
+     !!           Sum over relevant grid cells to obtain values
+     !!           for each
+     !!              There are several loops:                 
+     !!              loop on the segment between 2 nodes
+     !!              loop on the level jk
+     !!              loop on the density/temperature/salinity/level classes
+     !!              test on the density/temperature/salinity/level
+     !!
+     !!  ** Method  :Transport through a given section is equal to the sum of transports
+     !!              computed on each proc.
+     !!              On each proc,transport is equal to the sum of transport computed through
+     !!              segments linking each point of sec%listPoint  with the next one.   
+     !!
+     !!-------------------------------------------------------------
+     !! * arguments
+     TYPE(SECTION),INTENT(INOUT) :: sec
+     INTEGER      ,INTENT(IN)    :: jsec        ! numeric identifier of section
+
+     TYPE(POINT_SECTION) :: k
+     INTEGER  :: jk,jseg,jclass                        !loop on level/segment/classes 
+     REAL(wp) :: ztn, zsn, zrhoi, zrhop, zsshn, zfsdep ! temperature/salinity/ssh/potential density /depth at u/v point
+     !!-------------------------------------------------------------
+
+     !! Sum the relevant segments to obtain values for each class
+     IF(sec%nb_point .NE. 0)THEN   
+
+        !--------------------------------------!
+        ! LOOP ON THE SEGMENT BETWEEN 2 NODES  !
+        !--------------------------------------!
+        DO jseg=1,MAX(sec%nb_point-1,0)
+           
+           !-------------------------------------------------------------------------------------------
+           ! Select the appropriate coordinate for computing the velocity of the segment
+           !
+           !                      CASE(0)                                    Case (2)
+           !                      -------                                    --------
+           !  listPoint(jseg)                 listPoint(jseg+1)       listPoint(jseg)  F(i,j)      
+           !      F(i,j)----------V(i+1,j)-------F(i+1,j)                               |
+           !                                                                            |
+           !                                                                            |
+           !                                                                            |
+           !                      Case (3)                                            U(i,j)
+           !                      --------                                              |
+           !                                                                            |
+           !  listPoint(jseg+1) F(i,j+1)                                                |
+           !                        |                                                   |
+           !                        |                                                   |
+           !                        |                                 listPoint(jseg+1) F(i,j-1)
+           !                        |                                            
+           !                        |                                            
+           !                     U(i,j+1)                                            
+           !                        |                                       Case(1)     
+           !                        |                                       ------      
+           !                        |                                            
+           !                        |                 listPoint(jseg+1)             listPoint(jseg)                           
+           !                        |                 F(i-1,j)-----------V(i,j) -------f(jseg)                           
+           ! listPoint(jseg)     F(i,j)
+           ! 
+           !-------------------------------------------------------------------------------------------
+
+           SELECT CASE( sec%direction(jseg) )
+           CASE(0)  ;   k = sec%listPoint(jseg)
+           CASE(1)  ;   k = POINT_SECTION(sec%listPoint(jseg)%I+1,sec%listPoint(jseg)%J)
+           CASE(2)  ;   k = sec%listPoint(jseg)
+           CASE(3)  ;   k = POINT_SECTION(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J+1)
+           END SELECT
+
+           !---------------------------|
+           !     LOOP ON THE LEVEL     |
+           !---------------------------|
+           !Sum of the transport on the vertical 
+           DO jk=1,mbathy(k%I,k%J)
+
+              ! compute temperature, salinity, insitu & potential density, ssh and depth at U/V point
+              SELECT CASE( sec%direction(jseg) )
+              CASE(0,1)
+                 ztn   = interp(k%I,k%J,jk,'V',tsn(:,:,:,jp_tem) )
+                 zsn   = interp(k%I,k%J,jk,'V',tsn(:,:,:,jp_sal) )
+                 zrhop = interp(k%I,k%J,jk,'V',rhop)
+                 zrhoi = interp(k%I,k%J,jk,'V',rhd*rau0+rau0)
+                 zsshn =  0.5*( sshn(k%I,k%J)    + sshn(k%I,k%J+1)    ) * vmask(k%I,k%J,1)
+              CASE(2,3)
+                 ztn   = interp(k%I,k%J,jk,'U',tsn(:,:,:,jp_tem) )
+                 zsn   = interp(k%I,k%J,jk,'U',tsn(:,:,:,jp_sal) )
+                 zrhop = interp(k%I,k%J,jk,'U',rhop)
+                 zrhoi = interp(k%I,k%J,jk,'U',rhd*rau0+rau0)
+                 zsshn =  0.5*( sshn(k%I,k%J)    + sshn(k%I+1,k%J)    ) * umask(k%I,k%J,1) 
+              END SELECT
+
+              !JT zfsdep= gdept(k%I,k%J,jk)
+              !JT zfsdep= gdept_0(k%I,k%J,jk)
+              zfsdep= fsdept(k%I,k%J,jk)
+ 
+              !-------------------------------
+              !  LOOP ON THE DENSITY CLASSES |
+              !-------------------------------
+              !The computation is made for each density/heat/salt/... class
+              DO jclass=1,MAX(1,sec%nb_class-1)
+
+                 !----------------------------------------------!
+                 !TEST ON THE DENSITY/SALINITY/TEMPERATURE/LEVEL! 
+                 !----------------------------------------------!
+ 
+                 IF ( (                                                    &
+                    ((( zrhop .GE. (sec%zsigp(jclass)+1000.  )) .AND.      &
+                    (   zrhop .LE. (sec%zsigp(jclass+1)+1000. ))) .OR.     &
+                    ( sec%zsigp(jclass) .EQ. 99.)) .AND.                   &
+
+                    ((( zrhoi .GE. (sec%zsigi(jclass) + 1000.  )) .AND.    &
+                    (   zrhoi .LE. (sec%zsigi(jclass+1)+1000. ))) .OR.     &
+                    ( sec%zsigi(jclass) .EQ. 99.)) .AND.                   &
+
+                    ((( zsn .GT. sec%zsal(jclass)) .AND.                   &
+                    (   zsn .LE. sec%zsal(jclass+1))) .OR.                 &
+                    ( sec%zsal(jclass) .EQ. 99.)) .AND.                    &
+
+                    ((( ztn .GE. sec%ztem(jclass)) .AND.                   &
+                    (   ztn .LE. sec%ztem(jclass+1))) .OR.                 &
+                    ( sec%ztem(jclass) .EQ.99.)) .AND.                     &
+
+                    ((( zfsdep .GE. sec%zlay(jclass)) .AND.                &
+                    (   zfsdep .LE. sec%zlay(jclass+1))) .OR.              &
+                    ( sec%zlay(jclass) .EQ. 99. ))                         &
+                                                                   ))   THEN
+
+                    !SUM THE TRANSPORTS FOR EACH CLASSES FOR THE POSITIVE AND NEGATIVE DIRECTIONS
+                    !----------------------------------------------------------------------------
+                    IF (transports_3d_h(1,jsec,jseg,jk) .GE. 0.0) THEN 
+                       sec%transport_h(1,jclass) = sec%transport_h(1,jclass)+transports_3d_h(1,jsec,jseg,jk)
+                    ELSE
+                       sec%transport_h(2,jclass) = sec%transport_h(2,jclass)+transports_3d_h(1,jsec,jseg,jk)
+                    ENDIF
+                    IF( sec%llstrpond )THEN
+
+                       IF( transports_3d_h(1,jsec,jseg,jk) .NE. 0._wp ) THEN
+
+                          IF (transports_3d_h(2,jsec,jseg,jk)/transports_3d_h(1,jsec,jseg,jk) .GE. 0.0 ) THEN
+                             sec%transport_h(3,jclass) = sec%transport_h(3,jclass)+transports_3d_h(2,jsec,jseg,jk)/transports_3d_h(1,jsec,jseg,jk)
+                          ELSE
+                             sec%transport_h(4,jclass) = sec%transport_h(4,jclass)+transports_3d_h(2,jsec,jseg,jk)/transports_3d_h(1,jsec,jseg,jk)
+                          ENDIF
+
+                          IF ( transports_3d_h(3,jsec,jseg,jk)/transports_3d_h(1,jsec,jseg,jk) .GE. 0.0 ) THEN
+                             sec%transport_h(5,jclass) = sec%transport_h(5,jclass)+transports_3d_h(3,jsec,jseg,jk)/transports_3d_h(1,jsec,jseg,jk)
+                          ELSE
+                             sec%transport_h(6,jclass) = sec%transport_h(6,jclass)+transports_3d_h(3,jsec,jseg,jk)/transports_3d_h(1,jsec,jseg,jk)
+                          ENDIF
+
+                       ENDIF
+
+                       IF ( transports_3d_h(4,jsec,jseg,jk) .GE. 0.0 ) THEN
+                          sec%transport_h(7,jclass) = sec%transport_h(7,jclass)+transports_3d_h(4,jsec,jseg,jk)
+                       ELSE
+                          sec%transport_h(8,jclass) = sec%transport_h(8,jclass)+transports_3d_h(4,jsec,jseg,jk)
+                       ENDIF
+
+                       IF ( transports_3d_h(5,jsec,jseg,jk) .GE. 0.0 ) THEN
+                          sec%transport_h( 9,jclass) = sec%transport_h( 9,jclass)+transports_3d_h(5,jsec,jseg,jk)
+                       ELSE
+                          sec%transport_h(10,jclass) = sec%transport_h(10,jclass)+transports_3d_h(5,jsec,jseg,jk)
+                       ENDIF
+
+                    ELSE
+                       sec%transport_h( 3,jclass) = 0._wp
+                       sec%transport_h( 4,jclass) = 0._wp
+                       sec%transport_h( 5,jclass) = 0._wp
+                       sec%transport_h( 6,jclass) = 0._wp
+                       sec%transport_h( 7,jclass) = 0._wp
+                       sec%transport_h( 8,jclass) = 0._wp
+                       sec%transport_h( 9,jclass) = 0._wp
+                       sec%transport_h(10,jclass) = 0._wp
+                    ENDIF
+
+                 ENDIF ! end of test if point is in class
+   
+              ENDDO ! end of loop on the classes
+
+           ENDDO ! loop over jk
+
+#if defined key_lim2 || defined key_lim3
+
+           !ICE CASE    
+           IF( sec%ll_ice_section )THEN
+
+              IF ( transports_2d_h(1,jsec,jseg) .GE. 0.0 ) THEN
+                 sec%transport_h(11,1) = sec%transport_h(11,1)+transports_2d_h(1,jsec,jseg)
+              ELSE
+                 sec%transport_h(12,1) = sec%transport_h(12,1)+transports_2d_h(1,jsec,jseg)
+              ENDIF
+
+              IF ( transports_2d_h(3,jsec,jseg) .GE. 0.0 ) THEN
+                 sec%transport_h(13,1) = sec%transport_h(13,1)+transports_2d_h(2,jsec,jseg)
+              ELSE
+                 sec%transport_h(14,1) = sec%transport_h(14,1)+transports_2d_h(2,jsec,jseg)
+              ENDIF
+
+           ENDIF !end of ice case
+#endif
+ 
+        ENDDO !end of loop on the segment
+
+     ELSE  !if sec%nb_point =0
+        sec%transport_h(1:2,:)=0.
+        IF (sec%llstrpond) sec%transport_h(3:10,:)=0.
+        IF (sec%ll_ice_section) sec%transport_h( 11:14,:)=0.
+     ENDIF !end of sec%nb_point =0 case
+
+  END SUBROUTINE dia_dct_sum_h
+ 
+  SUBROUTINE dia_dct_wri_NOOS(kt,ksec,sec)
+     !!-------------------------------------------------------------
+     !! Write transport output in numdct using NOOS formatting 
+     !! 
+     !! Purpose: Write  transports in ascii files
+     !! 
+     !! Method:
+     !!        1. Write volume transports in "volume_transport"
+     !!           Unit: Sv : area * Velocity / 1.e6 
+     !! 
+     !!        2. Write heat transports in "heat_transport"
+     !!           Unit: Peta W : area * Velocity * T * rhau * Cp / 1.e15
+     !! 
+     !!        3. Write salt transports in "salt_transport"
+     !!           Unit: 10^9 g m^3 / s : area * Velocity * S / 1.e6
+     !!
      !!------------------------------------------------------------- 
-     !! Purpose: Average the transport over nn_dctwri time steps  
-     !! and sum over the density/salinity/temperature/depth classes 
+     !!arguments
+     INTEGER, INTENT(IN)          :: kt          ! time-step
+     TYPE(SECTION), INTENT(INOUT) :: sec         ! section to write   
+     INTEGER ,INTENT(IN)          :: ksec        ! section number
+
+     !!local declarations
+     INTEGER               :: jclass,ji             ! Dummy loop
+     CHARACTER(len=2)      :: classe             ! Classname 
+     REAL(wp)              :: zbnd1,zbnd2        ! Class bounds
+     REAL(wp)              :: zslope             ! section's slope coeff
+     !
+     REAL(wp), POINTER, DIMENSION(:):: zsumclasses ! 1D workspace 
+     !!------------------------------------------------------------- 
+     
+     
+     IF( lwp ) THEN
+        WRITE(numout,*) " "
+        WRITE(numout,*) "dia_dct_wri_NOOS: write transports through sections at timestep: ", kt
+        WRITE(numout,*) "~~~~~~~~~~~~~~~~~~~~~"
+     ENDIF   
+        
+     CALL wrk_alloc(nb_type , zsumclasses )  
+
+     zsumclasses(:)=0._wp
+     zslope = sec%slopeSection       
+
+     WRITE(numdct_NOOS,'(I4,a1,I2,a1,I2,a12,i3,a17,i3,a10,a25)') nyear,'.',nmonth,'.',nday,'   Transect:',ksec-1,'   No. of layers:',sec%nb_class-1,'   Name: ',sec%name
+
+     DO jclass=1,MAX(1,sec%nb_class-1)
+        zsumclasses(1:nb_type)=zsumclasses(1:nb_type)+sec%transport(1:nb_type,jclass)
+     ENDDO
+ 
+     classe   = 'total   '
+     zbnd1   = 0._wp
+     zbnd2   = 0._wp
+
+     IF ( zslope .gt. 0._wp .and. zslope .ne. 10000._wp ) THEN
+        WRITE(numdct_NOOS,'(9e12.4E2)') -(zsumclasses( 1)+zsumclasses( 2)), -zsumclasses( 2),-zsumclasses( 1),   &
+                                        -(zsumclasses( 7)+zsumclasses( 8)), -zsumclasses( 8),-zsumclasses( 7),   &
+                                        -(zsumclasses( 9)+zsumclasses(10)), -zsumclasses(10),-zsumclasses( 9)
+     ELSE
+        WRITE(numdct_NOOS,'(9e12.4E2)')   zsumclasses( 1)+zsumclasses( 2) ,  zsumclasses( 1), zsumclasses( 2),   &
+                                          zsumclasses( 7)+zsumclasses( 8) ,  zsumclasses( 7), zsumclasses( 8),   &
+                                          zsumclasses( 9)+zsumclasses(10) ,  zsumclasses( 9), zsumclasses(10)
+     ENDIF 
+
+     DO jclass=1,MAX(1,sec%nb_class-1)
+   
+        classe   = 'N       '
+        zbnd1   = 0._wp
+        zbnd2   = 0._wp
+
+        !insitu density classes transports
+        IF( ( sec%zsigi(jclass)   .NE. 99._wp ) .AND. &
+            ( sec%zsigi(jclass+1) .NE. 99._wp )       )THEN
+           classe = 'DI       '
+           zbnd1 = sec%zsigi(jclass)
+           zbnd2 = sec%zsigi(jclass+1)
+        ENDIF
+        !potential density classes transports
+        IF( ( sec%zsigp(jclass)   .NE. 99._wp ) .AND. &
+            ( sec%zsigp(jclass+1) .NE. 99._wp )       )THEN
+           classe = 'DP      '
+           zbnd1 = sec%zsigp(jclass)
+           zbnd2 = sec%zsigp(jclass+1)
+        ENDIF
+        !depth classes transports
+        IF( ( sec%zlay(jclass)    .NE. 99._wp ) .AND. &
+            ( sec%zlay(jclass+1)  .NE. 99._wp )       )THEN 
+           classe = 'Z       '
+           zbnd1 = sec%zlay(jclass)
+           zbnd2 = sec%zlay(jclass+1)
+        ENDIF
+        !salinity classes transports
+        IF( ( sec%zsal(jclass) .NE. 99._wp    ) .AND. &
+            ( sec%zsal(jclass+1) .NE. 99._wp  )       )THEN
+           classe = 'S       '
+           zbnd1 = sec%zsal(jclass)
+           zbnd2 = sec%zsal(jclass+1)   
+        ENDIF
+        !temperature classes transports
+        IF( ( sec%ztem(jclass) .NE. 99._wp     ) .AND. &
+            ( sec%ztem(jclass+1) .NE. 99._wp     )       ) THEN
+           classe = 'T       '
+           zbnd1 = sec%ztem(jclass)
+           zbnd2 = sec%ztem(jclass+1)
+        ENDIF
+                  
+        !write volume transport per class
+        IF ( zslope .gt. 0._wp .and. zslope .ne. 10000._wp ) THEN
+           WRITE(numdct_NOOS,'(9e12.4E2)') -(sec%transport( 1,jclass)+sec%transport( 2,jclass)),-sec%transport( 2,jclass),-sec%transport( 1,jclass), &
+                                           -(sec%transport( 7,jclass)+sec%transport( 8,jclass)),-sec%transport( 8,jclass),-sec%transport( 7,jclass), &
+                                           -(sec%transport( 9,jclass)+sec%transport(10,jclass)),-sec%transport(10,jclass),-sec%transport( 9,jclass)
+        ELSE
+           WRITE(numdct_NOOS,'(9e12.4E2)')   sec%transport( 1,jclass)+sec%transport( 2,jclass) , sec%transport( 1,jclass), sec%transport( 2,jclass), &
+                                             sec%transport( 7,jclass)+sec%transport( 8,jclass) , sec%transport( 7,jclass), sec%transport( 8,jclass), &
+                                             sec%transport( 9,jclass)+sec%transport(10,jclass) , sec%transport( 9,jclass), sec%transport(10,jclass)
+        ENDIF
+
+     ENDDO
+     
+     !CALL FLUSH(numdct_NOOS)
+
+     CALL wrk_dealloc(nb_type , zsumclasses )  
+
+  END SUBROUTINE dia_dct_wri_NOOS
+
+  SUBROUTINE dia_dct_wri_NOOS_h(hr,ksec,sec)
+     !!-------------------------------------------------------------
+     !! As routine dia_dct_wri_NOOS but for hourly output files
+     !!
+     !! Write transport output in numdct using NOOS formatting 
      !! 
-     !! Method:   Sum over relevant grid cells to obtain values  
-     !!           for each class
-     !!              There are several loops:                  
-     !!              loop on the segment between 2 nodes 
-     !!              loop on the level jk 
-     !!              loop on the density/temperature/salinity/level classes 
-     !!              test on the density/temperature/salinity/level 
+     !! Purpose: Write  transports in ascii files
      !! 
-     !!  Note:    Transport through a given section is equal to the sum of transports 
-     !!           computed on each proc. 
-     !!           On each proc,transport is equal to the sum of transport computed through 
-     !!           segments linking each point of sec%listPoint  with the next one.    
-     !! 
+     !! Method:
+     !!        1. Write volume transports in "volume_transport"
+     !!           Unit: Sv : area * Velocity / 1.e6 
+     !!
      !!------------------------------------------------------------- 
-     !! * arguments 
-     TYPE(SECTION),INTENT(INOUT) :: sec 
-     INTEGER      ,INTENT(IN)    :: jsec        ! numeric identifier of section 
- 
-     TYPE(POINT_SECTION) :: k 
-     INTEGER  :: jk,jseg,jclass                        ! dummy variables for looping on level/segment/classes  
-     REAL(wp) :: ztn, zsn, zrhoi, zrhop, zsshn, zfsdep ! temperature/salinity/ssh/potential density /depth at u/v point 
+     !!arguments
+     INTEGER, INTENT(IN)          :: hr          ! hour
+     TYPE(SECTION), INTENT(INOUT) :: sec         ! section to write   
+     INTEGER ,INTENT(IN)          :: ksec        ! section number
+
+     !!local declarations
+     INTEGER               :: jclass,jhr            ! Dummy loop
+     CHARACTER(len=2)      :: classe             ! Classname 
+     REAL(wp)              :: zbnd1,zbnd2        ! Class bounds
+     REAL(wp)              :: zslope             ! section's slope coeff
+     !
+     REAL(wp), POINTER, DIMENSION(:):: zsumclasses ! 1D workspace 
      !!------------------------------------------------------------- 
- 
-     !! Sum the relevant segments to obtain values for each class 
-     IF(sec%nb_point .NE. 0)THEN    
- 
-        !--------------------------------------! 
-        ! LOOP ON THE SEGMENT BETWEEN 2 NODES  ! 
-        !--------------------------------------! 
-        DO jseg=1,MAX(sec%nb_point-1,0) 
-            
-           !------------------------------------------------------------------------------------------- 
-           ! Select the appropriate coordinate for computing the velocity of the segment 
-           ! 
-           !                      CASE(0)                                    Case (2) 
-           !                      -------                                    -------- 
-           !  listPoint(jseg)                 listPoint(jseg+1)       listPoint(jseg)  F(i,j)       
-           !      F(i,j)----------V(i+1,j)-------F(i+1,j)                               | 
-           !                                                                            | 
-           !                                                                            | 
-           !                                                                            | 
-           !                      Case (3)                                            U(i,j) 
-           !                      --------                                              | 
-           !                                                                            | 
-           !  listPoint(jseg+1) F(i,j+1)                                                | 
-           !                        |                                                   | 
-           !                        |                                                   | 
-           !                        |                                 listPoint(jseg+1) F(i,j-1) 
-           !                        |                                             
-           !                        |                                             
-           !                     U(i,j+1)                                             
-           !                        |                                       Case(1)      
-           !                        |                                       ------       
-           !                        |                                             
-           !                        |                 listPoint(jseg+1)             listPoint(jseg)                            
-           !                        |                 F(i-1,j)-----------V(i,j) -------f(jseg)                            
-           ! listPoint(jseg)     F(i,j) 
-           !  
-           !------------------------------------------------------------------------------------------- 
- 
-           SELECT CASE( sec%direction(jseg) ) 
-           CASE(0)  ;   k = sec%listPoint(jseg) 
-           CASE(1)  ;   k = POINT_SECTION(sec%listPoint(jseg)%I+1,sec%listPoint(jseg)%J) 
-           CASE(2)  ;   k = sec%listPoint(jseg) 
-           CASE(3)  ;   k = POINT_SECTION(sec%listPoint(jseg)%I,sec%listPoint(jseg)%J+1) 
-           END SELECT 
- 
-           !---------------------------| 
-           !     LOOP ON THE LEVEL     | 
-           !---------------------------| 
-           !Sum of the transport on the vertical  
-           DO jk=1,mbathy(k%I,k%J) 
- 
-              ! compute temperature, salinity, insitu & potential density, ssh and depth at U/V point 
-              SELECT CASE( sec%direction(jseg) ) 
-              CASE(0,1) 
-                 ztn   = interp(k%I,k%J,jk,'V',tsn(:,:,:,jp_tem) ) 
-                 zsn   = interp(k%I,k%J,jk,'V',tsn(:,:,:,jp_sal) ) 
-                 zrhop = interp(k%I,k%J,jk,'V',rhop) 
-                 zrhoi = interp(k%I,k%J,jk,'V',rhd*rau0+rau0) 
-
-              CASE(2,3) 
-                 ztn   = interp(k%I,k%J,jk,'U',tsn(:,:,:,jp_tem) ) 
-                 zsn   = interp(k%I,k%J,jk,'U',tsn(:,:,:,jp_sal) ) 
-                 zrhop = interp(k%I,k%J,jk,'U',rhop) 
-                 zrhoi = interp(k%I,k%J,jk,'U',rhd*rau0+rau0) 
-                 zsshn =  0.5*( sshn(k%I,k%J)    + sshn(k%I+1,k%J)    ) * umask(k%I,k%J,1)  
-              END SELECT 
- 
-              zfsdep= fsdept(k%I,k%J,jk) 
-  
-              !------------------------------- 
-              !  LOOP ON THE DENSITY CLASSES | 
-              !------------------------------- 
-              !The computation is made for each density/temperature/salinity/depth class 
-              DO jclass=1,MAX(1,sec%nb_class-1) 
- 
-                 !----------------------------------------------! 
-                 !TEST ON THE DENSITY/SALINITY/TEMPERATURE/LEVEL!  
-                 !----------------------------------------------! 
-
-                 IF ( (                                                    & 
-                    ((( zrhop .GE. (sec%zsigp(jclass)+1000.  )) .AND.      & 
-                    (   zrhop .LE. (sec%zsigp(jclass+1)+1000. ))) .OR.     & 
-                    ( sec%zsigp(jclass) .EQ. 99.)) .AND.                   & 
- 
-                    ((( zrhoi .GE. (sec%zsigi(jclass) + 1000.  )) .AND.    & 
-                    (   zrhoi .LE. (sec%zsigi(jclass+1)+1000. ))) .OR.     & 
-                    ( sec%zsigi(jclass) .EQ. 99.)) .AND.                   & 
- 
-                    ((( zsn .GT. sec%zsal(jclass)) .AND.                   & 
-                    (   zsn .LE. sec%zsal(jclass+1))) .OR.                 & 
-                    ( sec%zsal(jclass) .EQ. 99.)) .AND.                    & 
- 
-                    ((( ztn .GE. sec%ztem(jclass)) .AND.                   & 
-                    (   ztn .LE. sec%ztem(jclass+1))) .OR.                 & 
-                    ( sec%ztem(jclass) .EQ.99.)) .AND.                     & 
- 
-                    ((( zfsdep .GE. sec%zlay(jclass)) .AND.                & 
-                    (   zfsdep .LE. sec%zlay(jclass+1))) .OR.              & 
-                    ( sec%zlay(jclass) .EQ. 99. ))                         & 
-                                                                   ))   THEN 
- 
-                    !SUM THE TRANSPORTS FOR EACH CLASSES FOR THE POSITIVE AND NEGATIVE DIRECTIONS 
-                    !---------------------------------------------------------------------------- 
-                    IF (transports_3d(1,jsec,jseg,jk) .GE. 0.0) THEN  
-                       sec%transport(1,jclass) = sec%transport(1,jclass)+transports_3d(1,jsec,jseg,jk)*1.E-6 
-                    ELSE 
-                       sec%transport(2,jclass) = sec%transport(2,jclass)+transports_3d(1,jsec,jseg,jk)*1.E-6 
-                    ENDIF 
-                    IF( sec%llstrpond )THEN 
- 
-                       IF ( transports_3d(2,jsec,jseg,jk) .GE. 0.0 ) THEN 
-                          sec%transport(3,jclass) = sec%transport(3,jclass)+transports_3d(2,jsec,jseg,jk) 
-                       ELSE 
-                          sec%transport(4,jclass) = sec%transport(4,jclass)+transports_3d(2,jsec,jseg,jk) 
-                       ENDIF 
- 
-                       IF ( transports_3d(3,jsec,jseg,jk) .GE. 0.0 ) THEN 
-                          sec%transport(5,jclass) = sec%transport(5,jclass)+transports_3d(3,jsec,jseg,jk) 
-                       ELSE 
-                          sec%transport(6,jclass) = sec%transport(6,jclass)+transports_3d(3,jsec,jseg,jk) 
-                       ENDIF 
- 
-                    ELSE 
-                       sec%transport( 3,jclass) = 0._wp 
-                       sec%transport( 4,jclass) = 0._wp 
-                       sec%transport( 5,jclass) = 0._wp 
-                       sec%transport( 6,jclass) = 0._wp 
-                    ENDIF 
- 
-                 ENDIF ! end of test if point is in class 
-    
-              ENDDO ! end of loop on the classes 
- 
-           ENDDO ! loop over jk 
- 
-#if defined key_lim2 || defined key_lim3 
- 
-           !ICE CASE     
-           IF( sec%ll_ice_section )THEN 
- 
-              IF ( transports_2d(1,jsec,jseg) .GE. 0.0 ) THEN 
-                 sec%transport( 7,1) = sec%transport( 7,1)+transports_2d(1,jsec,jseg)*1.E-6 
-              ELSE 
-                 sec%transport( 8,1) = sec%transport( 8,1)+transports_2d(1,jsec,jseg)*1.E-6 
-              ENDIF 
- 
-              IF ( transports_2d(3,jsec,jseg) .GE. 0.0 ) THEN 
-                 sec%transport( 9,1) = sec%transport( 9,1)+transports_2d(2,jsec,jseg)*1.E-6 
-              ELSE 
-                 sec%transport(10,1) = sec%transport(10,1)+transports_2d(2,jsec,jseg)*1.E-6 
-              ENDIF 
- 
-           ENDIF !end of ice case 
-#endif 
-  
-        ENDDO !end of loop on the segment 
- 
-     ELSE  !if sec%nb_point =0 
-        sec%transport(1:2,:)=0. 
-        IF (sec%llstrpond) sec%transport(3:6,:)=0. 
-        IF (sec%ll_ice_section) sec%transport(7:10,:)=0. 
-     ENDIF !end of sec%nb_point =0 case 
- 
-  END SUBROUTINE dia_dct_sum 
-  
+
+     IF( lwp ) THEN
+        WRITE(numout,*) " "
+        WRITE(numout,*) "dia_dct_wri_NOOS_h: write transports through sections at timestep: ", hr
+        WRITE(numout,*) "~~~~~~~~~~~~~~~~~~~~~"
+     ENDIF   
+     
+     CALL wrk_alloc(nb_type , zsumclasses ) 
+
+     zsumclasses(:)=0._wp
+     zslope = sec%slopeSection       
+
+     DO jclass=1,MAX(1,sec%nb_class-1)
+        zsumclasses(1:nb_type)=zsumclasses(1:nb_type)+sec%transport_h(1:nb_type,jclass)
+     ENDDO
+ 
+     !write volume transport per class
+     IF ( zslope .gt. 0._wp .and. zslope .ne. 10000._wp ) THEN
+        z_hr_output(ksec,hr,1)=-(zsumclasses(1)+zsumclasses(2))
+     ELSE
+        z_hr_output(ksec,hr,1)= (zsumclasses(1)+zsumclasses(2))
+     ENDIF
+
+     DO jclass=1,MAX(1,sec%nb_class-1)
+        IF ( zslope .gt. 0._wp .and. zslope .ne. 10000._wp ) THEN
+           z_hr_output(ksec,hr,jclass+1)=-(sec%transport_h(1,jclass)+sec%transport_h(2,jclass))
+        ELSE
+           z_hr_output(ksec,hr,jclass+1)= (sec%transport_h(1,jclass)+sec%transport_h(2,jclass))
+        ENDIF
+     ENDDO
+
+     IF ( hr .eq. 48._wp ) THEN
+        WRITE(numdct_NOOS_h,'(I4,a1,I2,a1,I2,a12,i3,a17,i3)') nyear,'.',nmonth,'.',nday,'   Transect:',ksec-1,'   No. of layers:',sec%nb_class-1
+        DO jhr=25,48
+           WRITE(numdct_NOOS_h,'(11F12.1)')  z_hr_output(ksec,jhr,1), (z_hr_output(ksec,jhr,jclass+1), jclass=1,MAX(1,10) )
+        ENDDO
+     ENDIF
+
+     CALL wrk_dealloc(nb_type , zsumclasses )
+
+  END SUBROUTINE dia_dct_wri_NOOS_h
+
   SUBROUTINE dia_dct_wri(kt,ksec,sec)
      !!-------------------------------------------------------------
@@ -1012,8 +1864,8 @@
      !! 
      !!        2. Write heat transports in "heat_transport"
-     !!           Unit: Peta W : area * Velocity * T * rhop * Cp * 1.e-15
+     !!           Unit: Peta W : area * Velocity * T * rhau * Cp / 1.e15
      !! 
      !!        3. Write salt transports in "salt_transport"
-     !!           Unit: 10^9 Kg/m^2/s : area * Velocity * S * rhop * 1.e-9 
+     !!           Unit: 10^9 g m^3 / s : area * Velocity * S / 1.e6
      !!
      !!------------------------------------------------------------- 
@@ -1031,5 +1883,5 @@
      REAL(wp), POINTER, DIMENSION(:):: zsumclasses ! 1D workspace 
      !!------------------------------------------------------------- 
-     CALL wrk_alloc(nb_type_class , zsumclasses )  
+     CALL wrk_alloc(nb_type , zsumclasses )  
 
      zsumclasses(:)=0._wp
@@ -1042,5 +1894,5 @@
         zbnd1   = 0._wp
         zbnd2   = 0._wp
-        zsumclasses(1:nb_type_class)=zsumclasses(1:nb_type_class)+sec%transport(1:nb_type_class,jclass)
+        zsumclasses(1:nb_type)=zsumclasses(1:nb_type)+sec%transport(1:nb_type,jclass)
 
    
@@ -1090,13 +1942,13 @@
 
            !write heat transport per class:
-           WRITE(numdct_heat,119) ndastp,kt,ksec,sec%name,zslope,  &
+           WRITE(numdct_temp,119) ndastp,kt,ksec,sec%name,zslope,  &
                               jclass,classe,zbnd1,zbnd2,&
-                              sec%transport(3,jclass)*1.e-15,sec%transport(4,jclass)*1.e-15, &
-                              ( sec%transport(3,jclass)+sec%transport(4,jclass) )*1.e-15
+                              sec%transport(7,jclass)*1000._wp*rcp/1.e15,sec%transport(8,jclass)*1000._wp*rcp/1.e15, &
+                              ( sec%transport(7,jclass)+sec%transport(8,jclass) )*1000._wp*rcp/1.e15
            !write salt transport per class
-           WRITE(numdct_salt,119) ndastp,kt,ksec,sec%name,zslope,  &
+           WRITE(numdct_sal ,119) ndastp,kt,ksec,sec%name,zslope,  &
                               jclass,classe,zbnd1,zbnd2,&
-                              sec%transport(5,jclass)*1.e-9,sec%transport(6,jclass)*1.e-9,&
-                              (sec%transport(5,jclass)+sec%transport(6,jclass))*1.e-9
+                              sec%transport(9,jclass)*1000._wp/1.e9,sec%transport(10,jclass)*1000._wp/1.e9,&
+                              (sec%transport(9,jclass)+sec%transport(10,jclass))*1000._wp/1.e9
         ENDIF
 
@@ -1115,13 +1967,13 @@
 
         !write total heat transport
-        WRITE(numdct_heat,119) ndastp,kt,ksec,sec%name,zslope, &
+        WRITE(numdct_temp,119) ndastp,kt,ksec,sec%name,zslope, &
                            jclass,"total",zbnd1,zbnd2,&
-                           zsumclasses(3)*1.e-15,zsumclasses(4)*1.e-15,&
-                           (zsumclasses(3)+zsumclasses(4) )*1.e-15
+                           zsumclasses(7)* 1000._wp*rcp/1.e15,zsumclasses(8)* 1000._wp*rcp/1.e15,&
+                           (zsumclasses(7)+zsumclasses(8) )* 1000._wp*rcp/1.e15
         !write total salt transport
-        WRITE(numdct_salt,119) ndastp,kt,ksec,sec%name,zslope, &
+        WRITE(numdct_sal ,119) ndastp,kt,ksec,sec%name,zslope, &
                            jclass,"total",zbnd1,zbnd2,&
-                           zsumclasses(5)*1.e-9,zsumclasses(6)*1.e-9,&
-                           (zsumclasses(5)+zsumclasses(6))*1.e-9
+                           zsumclasses(9)*1000._wp/1.e9,zsumclasses(10)*1000._wp/1.e9,&
+                           (zsumclasses(9)+zsumclasses(10))*1000._wp/1.e9
      ENDIF
 
@@ -1131,11 +1983,11 @@
         WRITE(numdct_vol,118) ndastp,kt,ksec,sec%name,zslope,&
                               jclass,"ice_vol",zbnd1,zbnd2,&
-                              sec%transport(7,1),sec%transport(8,1),&
-                              sec%transport(7,1)+sec%transport(8,1)
+                              sec%transport(11,1),sec%transport(12,1),&
+                              sec%transport(11,1)+sec%transport(12,1)
         !write total ice surface transport
         WRITE(numdct_vol,118) ndastp,kt,ksec,sec%name,zslope,&
                               jclass,"ice_surf",zbnd1,zbnd2,&
-                              sec%transport(9,1),sec%transport(10,1), &
-                              sec%transport(9,1)+sec%transport(10,1) 
+                              sec%transport(13,1),sec%transport(14,1), &
+                              sec%transport(13,1)+sec%transport(14,1) 
      ENDIF
                                               
@@ -1143,5 +1995,5 @@
 119 FORMAT(I8,1X,I8,1X,I4,1X,A30,1X,f9.2,1X,I4,3X,A8,1X,2F12.4,5X,3E15.6)
 
-     CALL wrk_dealloc(nb_type_class , zsumclasses )  
+     CALL wrk_dealloc(nb_type , zsumclasses )  
   END SUBROUTINE dia_dct_wri
 
@@ -1149,5 +2001,5 @@
   !!----------------------------------------------------------------------
   !!
-  !!   Purpose: compute temperature/salinity/density at U-point or V-point
+  !!   Purpose: compute Temperature/Salinity/density at U-point or V-point
   !!   --------
   !!
@@ -1158,7 +2010,7 @@
   !! 
   !!
-  !!    |    I          |    I+1           |    Z=temperature/salinity/density at U-poinT
+  !!    |    I          |    I+1           |    Z=Temperature/Salinity/density at U-poinT
   !!    |               |                  |
-  !!  ----------------------------------------  1. Veritcal interpolation: compute zbis
+  !!  ----------------------------------------  1. Veritcale interpolation: compute zbis
   !!    |               |                  |       interpolation between ptab(I,J,K) and ptab(I,J,K+1)
   !!    |               |                  |       zbis = 
@@ -1245,10 +2097,11 @@
      zdep2 = fsdept(ii2,ij2,kk) - zdepu
 
-     ! weights
+     !weights
      zwgt1 = SQRT( ( 0.5 * zet1 ) * ( 0.5 * zet1 ) + ( zdep1 * zdep1 ) )
      zwgt2 = SQRT( ( 0.5 * zet2 ) * ( 0.5 * zet2 ) + ( zdep2 * zdep2 ) )
   
      ! result
-     interp = zmsk * ( zwgt2 *  ptab(ii1,ij1,kk) + zwgt1 *  ptab(ii1,ij1,kk) ) / ( zwgt2 + zwgt1 )   
+     !JT interp = zmsk * ( zwgt2 *  ptab(ii1,ij1,kk) + zwgt1 *  ptab(ii1,ij1,kk) ) / ( zwgt2 + zwgt1 )   
+     interp = umask(ii1,ij1,kk) * ( zwgt2 *  ptab(ii1,ij1,kk) + zwgt1 *  ptab(ii1,ij1,kk) ) / ( zwgt2 + zwgt1 )   
 
 
@@ -1275,14 +2128,17 @@
            zbis = ptab(ii2,ij2,kk) + zwgt1 * ( ptab(ii2,ij2,kk-1) - ptab(ii2,ij2,kk) ) 
            ! result
-            interp = zmsk * ( zet2 * ptab(ii1,ij1,kk) + zet1 * zbis )/( zet1 + zet2 )
+            !JT interp = zmsk * ( zet2 * ptab(ii1,ij1,kk) + zet1 * zbis )/( zet1 + zet2 )
+            interp = umask(ii1,ij1,kk) * ( zet2 * ptab(ii1,ij1,kk) + zet1 * zbis )/( zet1 + zet2 )
         ELSE
            ! zbis
            zbis = ptab(ii1,ij1,kk) + zwgt2 * ( ptab(ii1,ij1,kk-1) - ptab(ii1,ij2,kk) )
            ! result
-           interp = zmsk * ( zet2 * zbis + zet1 * ptab(ii2,ij2,kk) )/( zet1 + zet2 )
+           !JT interp = zmsk * ( zet2 * zbis + zet1 * ptab(ii2,ij2,kk) )/( zet1 + zet2 )
+           interp = umask(ii1,ij1,kk) * ( zet2 * zbis + zet1 * ptab(ii2,ij2,kk) )/( zet1 + zet2 )
         ENDIF    
 
      ELSE
-        interp = zmsk * (  zet2 * ptab(ii1,ij1,kk) + zet1 * ptab(ii2,ij2,kk) )/( zet1 + zet2 )
+        !JT interp = zmsk * (  zet2 * ptab(ii1,ij1,kk) + zet1 * ptab(ii2,ij2,kk) )/( zet1 + zet2 )
+        interp = umask(ii1,ij1,kk) * (  zet2 * ptab(ii1,ij1,kk) + zet1 * ptab(ii2,ij2,kk) )/( zet1 + zet2 )
      ENDIF
 
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diainsitutem.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diainsitutem.F90	(revision 7567)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diainsitutem.F90	(revision 7567)
@@ -0,0 +1,124 @@
+MODULE diainsitutem
+   !!======================================================================
+   !!                       ***  MODULE  diaharm  ***
+   !! Harmonic analysis of tidal constituents
+   !!======================================================================
+   !! Initial version: D. Acreman June 2006 Original Code
+   !! History :  3.6  !  2014  (E O'Dea) for 3.6 25hour mean output
+   !!----------------------------------------------------------------------
+
+  USE dom_oce  ! ocean space and time domain
+  USE oce,            ONLY: tsn
+  USE par_oce,        ONLY: jpi, jpj, jpk, jpkm1
+  USE lbclnk,         ONLY: lbc_lnk
+  USE lib_mpp         ! MPP library
+
+  IMPLICIT NONE
+  PRIVATE
+
+  PUBLIC theta2t
+
+  !! * Accessibility
+  PUBLIC insitu_tem_alloc          ! routines called by step.F90
+
+  REAL(wp), PUBLIC, ALLOCATABLE, DIMENSION(:,:,:) :: rinsitu_t
+
+   !! * Substitutions
+#  include "domzgr_substitute.h90"
+
+CONTAINS
+
+   REAL FUNCTION insitu_tem_alloc()
+      !!----------------------------------------------------------------------
+      INTEGER, DIMENSION(2) :: ierr
+      !!----------------------------------------------------------------------
+      !
+      ierr = 0
+      !
+      ALLOCATE( rinsitu_t(jpi,jpj,jpk), STAT=ierr(1) )
+         !
+      insitu_tem_alloc = MAXVAL(ierr)
+      IF( lk_mpp )   CALL mpp_sum( insitu_tem_alloc )
+      !
+  END FUNCTION insitu_tem_alloc
+
+!-----------------------------------------------------------------------------------
+!
+! Calculate the insitu temperature by integrating the adiabatic lapse rate from zero 
+! to pressure at depth of tracer level. Based on UM subroutine POTTEM and function ATG
+!
+! Initial version: D. Acreman June 2006
+!
+!-----------------------------------------------------------------------------------
+
+  SUBROUTINE theta2t()
+
+    INTEGER, PARAMETER :: inum_steps=10                ! number of steps in integration
+    INTEGER            :: jstep                        ! iteration counter
+    INTEGER            :: ji, jj, jk                  ! loop indices
+    REAL(wp), DIMENSION(jpi,jpj,jpk) :: zP            ! pressure (decibars)
+    REAL(wp), DIMENSION(jpi,jpj,jpk) :: zT            ! temperature at pressure p
+    REAL(wp), DIMENSION(jpi,jpj,jpk) :: zTB           ! temperature at p-dp
+    REAL(wp), DIMENSION(jpi,jpj,jpk) :: zTA           ! temperature at p+dp
+    REAL(wp), DIMENSION(jpi,jpj,jpk) :: zDP           ! pressure step
+    REAL(wp), DIMENSION(jpi,jpj,jpk) :: zSS           ! salinity(PSU) - 35.0
+    REAL(wp), DIMENSION(jpi,jpj,jpk) :: zLAPSE        ! adiabatic lapse rate
+
+!CDIR IEXPAND (ATG)
+
+! Set the pressure interval for the integration. The integration is carried out from 
+! zero (pressure at the surface) to the pressure at the depth of the tracer point. The
+! pressure in decibars is represented by the depth in metres. Pressures are "Oceanographic"
+! pressures equal to absolute pressure minus one atmosphere
+     zDP(:,:,:) = 0.0
+     DO jk = 1, jpkm1
+        DO jj = 1, jpj
+           DO ji = 1, jpi
+              ! These loops expanded for case where fsdept may be 1D
+              zDP(ji,jj,jk) = fsdept(ji,jj,jk) / real(inum_steps)
+           END DO
+        END DO
+     END DO
+
+! Salinity at each point
+     zSS(:,:,:)      = tsn(:,:,:,jp_sal) - 35.0 
+
+! Set initial values of temperature and pressure. zT is the temperature at pressure zP, 
+! zTB is the temperature at pressure zP-zdP and zTA is the temperature at pressure zP+zdP
+     zT(:,:,:)  = tsn(:,:,:,jp_tem)
+     zP(:,:,:)  = 0.0         ! Pressure at surface
+     CALL ATG(zP, zT, zSS, zLAPSE)
+     zTB(:,:,:) = zT(:,:,:)  - zLAPSE(:,:,:) * zDP(:,:,:)
+
+     interation: DO jstep=1, inum_steps
+        ! Calculate lapse rate (dT/dP) and hence TA
+        CALL ATG(zP, zT, zSS, zLAPSE)
+        zTA(:,:,:) = zTB(:,:,:) + 2.0 * zLAPSE(:,:,:) * zDP(:,:,:)
+        ! Have calculated TB, T and TA for this pressure, now advance solution by dP
+        zP(:,:,:)  = zP(:,:,:)  + zDP
+        zTB(:,:,:) = zT(:,:,:)
+        zT(:,:,:)  = zTA(:,:,:)
+
+     END DO interation
+
+     rinsitu_t(:,:,:) = zT(:,:,:) * tmask(:,:,:)
+     CALL lbc_lnk( rinsitu_t,  'T', 1.0)
+
+   END SUBROUTINE theta2t
+
+   SUBROUTINE ATG(P,T,DS,zLAPSE)
+
+     REAL, INTENT(IN   ) :: P(jpi,jpj,jpk)        ! PRESSURE (DECIBARS)
+     REAL, INTENT(IN   ) :: T(jpi,jpj,jpk)        ! TEMPERATURE (DEG C)
+     REAL, INTENT(IN   ) :: DS(jpi,jpj,jpk)       ! SALINITY (PSU) -35.0
+     REAL, INTENT(  OUT) :: zLAPSE(jpi,jpj,jpk)   ! LAPSE RATE
+
+       zLAPSE = ((( -2.1687E-16*T+1.8676E-14)*T-4.6206E-13)*P &
+           + ((2.7759E-12*T-1.1351E-10)*DS+((-5.4481E-14*T    &
+           + 8.733E-12)*T-6.7795E-10)*T+1.8741E-8))*P         &
+           + (-4.2393E-8*T+1.8932E-6)*DS                      &
+           + ((6.6228E-10*T-6.836E-8)*T+8.5258E-6)*T+3.5803E-5 
+
+   END SUBROUTINE ATG
+
+END MODULE diainsitutem
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diapea.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diapea.F90	(revision 7567)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diapea.F90	(revision 7567)
@@ -0,0 +1,267 @@
+MODULE diapea
+   !!======================================================================
+   !!                       ***  MODULE  diapea  ***
+   !! Potential Energy Anomaly 
+   !!======================================================================
+   !! History :  3.6  !  12/2016  (J Tinker)  Original code
+   !!----------------------------------------------------------------------
+   USE oce             ! ocean dynamics and tracers variables
+   USE dom_oce         ! ocean space and time domain
+   USE in_out_manager  ! I/O units
+   USE iom             ! I/0 library
+   USE wrk_nemo        ! working arrays
+   USE eosbn2          ! Equation of state - in situ and potential density
+   USE phycst          ! physical constant
+
+   IMPLICIT NONE
+   PRIVATE 
+   
+   PUBLIC   dia_pea_init            ! routine called by nemogcm.F90
+   PUBLIC   dia_pea                 ! routine called by diawri.F90
+   REAL(wp), PUBLIC, SAVE, ALLOCATABLE,   DIMENSION(:,:)  ::   pea,peat,peas   
+   REAL(wp), SAVE, ALLOCATABLE,   DIMENSION(:,:,:)  ::   wgt_co_mat   ! Weighting array for proportion of grid shallower than cut off depth
+   REAL(wp), SAVE, ALLOCATABLE,   DIMENSION(:,:) ::   t_zmean, s_zmean  !Depth mean temperature and salinity: 2d fields
+   REAL(wp), SAVE, ALLOCATABLE,   DIMENSION(:,:,:) ::   t_zmean_mat, s_zmean_mat  !Depth mean temperature and salinity: 3d fields
+   REAL(wp) ::   zcutoff
+   !!----------------------------------------------------------------------
+   !! NEMO/OPA 3.6 , NEMO Consortium (2014)
+   !! $Id$
+   !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
+   !!----------------------------------------------------------------------
+CONTAINS
+
+   SUBROUTINE dia_pea_init
+      ! Local variables
+      INTEGER :: ji,jj,jk  ! Dummy loop indices
+      REAL(wp) :: sumz,tmpsumz
+      INTEGER  :: ierr                ! error integer for IOM_get
+      
+      zcutoff = 200.!200m
+      
+   ALLOCATE( pea(jpi,jpj),  STAT= ierr )
+   IF( ierr /= 0 )   CALL ctl_stop( 'dia_pea_init: failed to allocate pea array' )
+   
+   ALLOCATE( peat(jpi,jpj),  STAT= ierr )
+   IF( ierr /= 0 )   CALL ctl_stop( 'dia_pea_init: failed to allocate peat array' )
+   
+   ALLOCATE( peas(jpi,jpj),  STAT= ierr )
+   IF( ierr /= 0 )   CALL ctl_stop( 'dia_pea_init: failed to allocate peas array' )   
+      
+   ALLOCATE( t_zmean_mat(jpi,jpj,jpk),  STAT= ierr )
+   IF( ierr /= 0 )   CALL ctl_stop( 'dia_pea_init: failed to allocate t_zmean_mat array' )
+   
+   ALLOCATE( s_zmean_mat(jpi,jpj,jpk),  STAT= ierr )
+   IF( ierr /= 0 )   CALL ctl_stop( 'dia_pea_init: failed to allocate s_zmean_mat array' )
+   
+   ALLOCATE( t_zmean(jpi,jpj),  STAT= ierr )
+   IF( ierr /= 0 )   CALL ctl_stop( 'dia_pea_init: failed to allocate t_zmean array' )
+   
+   ALLOCATE( s_zmean(jpi,jpj),  STAT= ierr )
+   IF( ierr /= 0 )   CALL ctl_stop( 'dia_pea_init: failed to allocate s_zmean array' )
+   
+   ALLOCATE( wgt_co_mat(jpi,jpj,jpk),  STAT= ierr )
+   IF( ierr /= 0 )   CALL ctl_stop( 'dia_pea_init: failed to allocate wgt_co_mat array' )
+          
+    ! create wgt_co_mat mat, with the proportion of the grid (gdept_0) below cut off (200m)
+    
+    DO jj = 1,jpj
+        DO ji = 1,jpi
+            IF ( tmask(ji,jj,1) == 1.0_wp ) THEN
+              sumz = 0.
+              DO jk = 1,jpk
+                IF ( tmask(ji,jj,jk) == 1.0_wp ) THEN
+                  tmpsumz = sumz + e3t_n(ji,jj,jk)
+                  IF (sumz .ge. zcutoff) THEN
+                    ! Already too deep
+                    wgt_co_mat(ji,jj,jk) = 0.
+                  ELSE IF (tmpsumz .le. zcutoff) THEN
+                    ! Too shallow
+                    wgt_co_mat(ji,jj,jk) = 1.
+                  ELSE
+                    !proprotion of grid box above cut off depth
+                    wgt_co_mat(ji,jj,jk) = (zcutoff-Sumz)/e3t_n(ji,jj,jk)
+                  END IF
+                  sumz = tmpsumz
+                endif
+              END DO
+              
+            ELSE
+              !if land, set to 0.
+              DO jk = 1,jpk
+                wgt_co_mat(ji,jj,jk) = 0.
+              END DO
+                
+            ENDIF
+        END DO
+   END DO
+   
+
+   END SUBROUTINE dia_pea_init
+   
+
+   SUBROUTINE dia_pea(kt)
+   INTEGER, INTENT( in ) ::   kt      ! ocean time-step index
+    
+   INTEGER :: ji,jj,jk,ierr  ! Dummy loop indices
+   REAL(wp) :: tmpdenom, tmpnum, maxz
+   !rau0
+   
+   REAL(wp), ALLOCATABLE,   DIMENSION(:,:,:,:) ::   ts_pea_mat,ts_pea_mat_TS_mean,ts_pea_mat_S_mean
+   REAL(wp), ALLOCATABLE,   DIMENSION(:,:,:) ::   tmp_pea_rho,tmp_pea_TS_mean_rho,tmp_pea_S_mean_rho
+   REAL(wp) ::   int_y_pea,int_y_pea_t
+   
+   
+      
+   ALLOCATE( ts_pea_mat(jpi,jpj,jpk,2),  STAT= ierr )
+   IF( ierr /= 0 )   CALL ctl_stop( 'dia_pea_init: failed to allocate ts_pea_mat array' )    
+   ALLOCATE( ts_pea_mat_TS_mean(jpi,jpj,jpk,2),  STAT= ierr )
+   IF( ierr /= 0 )   CALL ctl_stop( 'dia_pea_init: failed to allocate ts_pea_mat_TS_mean array' )   
+   ALLOCATE( ts_pea_mat_S_mean(jpi,jpj,jpk,2),  STAT= ierr )
+   IF( ierr /= 0 )   CALL ctl_stop( 'dia_pea_init: failed to allocate ts_pea_mat_S_mean array' )
+   ALLOCATE( tmp_pea_rho(jpi,jpj,jpk),  STAT= ierr )
+   IF( ierr /= 0 )   CALL ctl_stop( 'dia_pea_init: failed to allocate tmp_pea_rho array' )
+   ALLOCATE( tmp_pea_TS_mean_rho(jpi,jpj,jpk),  STAT= ierr )
+   IF( ierr /= 0 )   CALL ctl_stop( 'dia_pea_init: failed to allocate tmp_pea_TS_mean_rho array' )
+   ALLOCATE( tmp_pea_S_mean_rho(jpi,jpj,jpk),  STAT= ierr )
+   IF( ierr /= 0 )   CALL ctl_stop( 'dia_pea_init: failed to allocate tmp_pea_S_mean_rho array' )
+
+    pea(:,:)=0.0d0
+    peaT(:,:)=0.0d0
+    peaS(:,:)=0.0d0
+    
+    
+    
+    !calculate the depth mean temperature and salinity of the upper 200m. Save this into a 3d array. Set the value where tmask=0 to be tsn. 
+    
+     DO jj = 1,jpj
+         DO ji = 1,jpi
+            IF ( tmask(ji,jj,1) == 1.0_wp ) THEN  ! if a sea point. 
+            
+             !Depth mean temperature
+             tmpdenom = 0.
+             tmpnum = 0.
+             DO jk = 1,jpk
+                IF ( tmask(ji,jj,jk) == 1.0_wp ) THEN
+                  tmpnum = tmpnum + (wgt_co_mat(ji,jj,jk)*e3t_n(ji,jj,jk)*tsn(ji,jj,jk,jp_tem))
+                  tmpdenom = tmpdenom + (wgt_co_mat(ji,jj,jk)*e3t_n(ji,jj,jk))
+                endif
+             END DO
+             t_zmean(ji,jj) = tmpnum/tmpdenom
+             
+             !Depth mean salinity
+             tmpdenom = 0.
+             tmpnum = 0.
+             DO jk = 1,jpk
+                IF ( tmask(ji,jj,jk) == 1.0_wp ) THEN
+                  tmpnum     = tmpnum + (wgt_co_mat(ji,jj,jk)*e3t_n(ji,jj,jk)*tsn(ji,jj,jk,jp_sal))
+                  tmpdenom = tmpdenom + (wgt_co_mat(ji,jj,jk)*e3t_n(ji,jj,jk))
+                endif
+             END DO
+             s_zmean(ji,jj) = tmpnum/tmpdenom
+             
+             !save into a 3d grid
+             DO jk = 1,jpk
+               IF ( tmask(ji,jj,jk) == 1.0_wp ) THEN
+                 t_zmean_mat(ji,jj,jk) = t_zmean(ji,jj)
+                 s_zmean_mat(ji,jj,jk) = s_zmean(ji,jj)
+               else
+                 t_zmean_mat(ji,jj,jk) = tsn(ji,jj,jk,jp_tem)
+                 s_zmean_mat(ji,jj,jk) = tsn(ji,jj,jk,jp_sal)
+               endif
+             END DO
+            else
+              t_zmean(ji,jj) = tsn(ji,jj,1,jp_tem)
+              s_zmean(ji,jj) = tsn(ji,jj,1,jp_sal)
+              DO jk = 1,jpk
+                t_zmean_mat(ji,jj,jk) = tsn(ji,jj,jk,jp_tem)
+                s_zmean_mat(ji,jj,jk) = tsn(ji,jj,jk,jp_sal)
+              END DO
+            endif
+            
+         END DO
+     END DO
+      
+   !Calculate the density from the depth varying, and depth average temperature and salinity
+   !-----------------------------
+   !-----------------------------
+   
+   ts_pea_mat(:,:,:,:) = tsn(:,:,:,:)
+   
+   ts_pea_mat_TS_mean(:,:,:,1) = t_zmean_mat(:,:,:)
+   ts_pea_mat_TS_mean(:,:,:,2) = s_zmean_mat(:,:,:)
+   
+   ts_pea_mat_S_mean(:,:,:,1) = t_zmean_mat(:,:,:)
+   ts_pea_mat_S_mean(:,:,:,2) = tsn(:,:,:,jp_sal)
+   
+   CALL eos ( ts_pea_mat,         tmp_pea_rho,         gdept_n(:,:,:) )
+   CALL eos ( ts_pea_mat_TS_mean, tmp_pea_TS_mean_rho, gdept_n(:,:,:) )
+   CALL eos ( ts_pea_mat_S_mean,  tmp_pea_S_mean_rho,  gdept_n(:,:,:) )
+   tmp_pea_rho = (tmp_pea_rho * rau0) + rau0
+   tmp_pea_TS_mean_rho = (tmp_pea_TS_mean_rho * rau0) + rau0
+   tmp_pea_S_mean_rho = (tmp_pea_S_mean_rho * rau0) + rau0
+   
+   
+   ! to test the density calculation
+   !CALL iom_put( "tmp_pea_rho" , tmp_pea_rho )                 ! pea
+   !CALL iom_put( "tmp_pea_TS_mean_rho" , tmp_pea_TS_mean_rho )                 ! pea
+   
+   
+   ! Caluclation of the PEA.
+    DO jj = 1,jpj
+      DO ji = 1,jpi
+        pea(ji,jj) = 0.
+        peat(ji,jj) = 0.
+        peas(ji,jj) = 0.
+        maxz = 0.
+        int_y_pea = 0.
+        int_y_pea_t = 0.
+        IF ( tmask(ji,jj,1) == 1.0_wp ) THEN ! for sea points
+        
+          ! the depth integrated calculation is summed up over the depths, and then divided by the depth
+          DO jk = 1,jpk
+            !for each level...
+            
+            IF ( tmask(ji,jj,jk) == 1.0_wp ) THEN ! if above the sea bed...
+              int_y_pea =   -((tmp_pea_TS_mean_rho(ji,jj,jk)) - (tmp_pea_rho(ji,jj,jk)))*9.81*gdept_n(ji,jj,jk)*wgt_co_mat(ji,jj,jk)*e3t_n(ji,jj,jk)
+              int_y_pea_t = -((tmp_pea_S_mean_rho(ji,jj,jk))  - (tmp_pea_rho(ji,jj,jk)))*9.81*gdept_n(ji,jj,jk)*wgt_co_mat(ji,jj,jk)*e3t_n(ji,jj,jk)
+            else
+              int_y_pea = 0.
+              int_y_pea_t = 0.
+            endif
+            
+            ! check that the sum is not NaN. 
+            if ( int_y_pea .ne.  int_y_pea    ) int_y_pea = 0.
+            if ( int_y_pea_t .ne. int_y_pea_t )  int_y_pea_t = 0.
+            !if ( (int_y_pea*int_y_pea    ) .gt. 1.0e6 ) int_y_pea = 0.
+            !if ( (int_y_pea_t*int_y_pea_t) .gt. 1.0e6 ) int_y_pea_t = 0.
+            
+            pea(ji,jj) =  pea(ji,jj) + int_y_pea
+            peat(ji,jj) =  peat(ji,jj) + int_y_pea_t
+            maxz = maxz + (e3t_n(ji,jj,jk)*wgt_co_mat(ji,jj,jk))
+          enddo
+            
+            
+          !divide by the depth
+          pea(ji,jj) = pea(ji,jj)/maxz
+          peat(ji,jj) = peat(ji,jj)/maxz
+          peas(ji,jj) = pea(ji,jj) - peat(ji,jj)
+            
+            
+          else
+            pea(ji,jj) = 0.
+            peat(ji,jj) = 0.
+            peas(ji,jj) = 0.
+          endif
+       enddo
+    enddo
+!    
+     CALL iom_put( "pea" , pea )                 ! pea
+     CALL iom_put( "peat" , peat )               ! pea
+     CALL iom_put( "peas" , peas )               ! pea
+           
+
+    DEALLOCATE(ts_pea_mat,ts_pea_mat_TS_mean,ts_pea_mat_S_mean,tmp_pea_rho,tmp_pea_TS_mean_rho,tmp_pea_S_mean_rho)
+   
+   END SUBROUTINE dia_pea
+
+END MODULE diapea
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diaregmean.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diaregmean.F90	(revision 7567)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diaregmean.F90	(revision 7567)
@@ -0,0 +1,708 @@
+MODULE diaregmean 
+   !!======================================================================
+   !!                       ***  MODULE  diaharm  ***
+   !! Timeseries of Regional Means 
+   !!======================================================================
+   !! History :  3.6  !  11/2016  (J Tinker)  Original code
+   !!----------------------------------------------------------------------
+   USE oce             ! ocean dynamics and tracers variables
+   USE dom_oce         ! ocean space and time domain
+   USE in_out_manager  ! I/O units
+   USE iom             ! I/0 library
+   USE wrk_nemo        ! working arrays
+   USE diatmb          ! Top,middle,bottom output
+   USE diapea          ! Top,middle,bottom output
+   USE zdfmxl          ! MLD
+
+   IMPLICIT NONE
+   PRIVATE
+
+   LOGICAL , PUBLIC ::   ln_diaregmean  ! region mean calculation
+   PUBLIC   dia_regmean_init            ! routine called by nemogcm.F90
+   PUBLIC   dia_regmean                 ! routine called by diawri.F90
+   !PUBLIC   dia_wri_region_mean         
+   !PUBLIC   dia_calctmb_region_mean
+   
+   
+
+   
+   
+   LOGICAL :: ln_diaregmean_ascii  ! region mean calculation ascii output
+   LOGICAL :: ln_diaregmean_bin  ! region mean calculation binary output
+   LOGICAL :: ln_diaregmean_nc  ! region mean calculation netcdf output
+   
+   REAL(wp), SAVE, ALLOCATABLE,   DIMENSION(:,:,:)  ::   tmp_region_mask_real   ! tempory region_mask of reals
+   INTEGER, SAVE, ALLOCATABLE,   DIMENSION(:,:,:)   ::   region_mask            ! region_mask matrix
+   INTEGER                                          ::   nmasks                 ! Number of mask files in region_mask.nc file - 
+   INTEGER, SAVE, ALLOCATABLE,   DIMENSION(:)       ::   nreg_mat               ! Number of regions in each mask
+   
+   REAL(wp),  ALLOCATABLE,   DIMENSION(:,:,:) ::   tmp_field_mat !: temporary region_mask
+   INTEGER  ::   tmp_field_cnt                                   ! tmp_field_cnt integer
+   !!----------------------------------------------------------------------
+   !! NEMO/OPA 3.6 , NEMO Consortium (2014)
+   !! $Id$
+   !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
+   !!----------------------------------------------------------------------
+CONTAINS
+
+   SUBROUTINE dia_regmean_init 
+      !!---------------------------------------------------------------------------
+      !!                  ***  ROUTINE dia_regmean_init  ***
+      !!     
+      !! ** Purpose: Initialization of region mask namelist 
+      !!        
+      !! ** Method : Read namelist
+      !!   History
+      !!   3.6  !  11-16  (J Tinker) Routine to initialize dia_regmean
+      !!---------------------------------------------------------------------------
+      !!
+      INTEGER ::   ios                  ! Local integer output status for namelist read
+      INTEGER  ::  inum                 ! temporary logical unit ! copied from DOM/domzgr.F90
+      INTEGER  ::   ierr                ! error integer for IOM_get
+      INTEGER  ::   idmaskvar           ! output of iom_varid
+      INTEGER  ::   maskno              ! counter for number of masks
+      INTEGER  ::   jj,ji               ! i and j index
+      INTEGER  ::   tmpint              ! temporary integer
+      REAL(wp),  ALLOCATABLE,   DIMENSION(:,:) ::   tmpregion !: temporary region_mask
+      INTEGER, DIMENSION(3) ::   zdimsz   ! number of elements in each of the 3 dimensions (i.e., lon, lat, no of masks, 297,  375,  4) for an array
+      INTEGER               ::   zndims   ! number of dimensions in an array (i.e. 3, )
+      !
+      NAMELIST/nam_diaregmean/ ln_diaregmean,ln_diaregmean_ascii,ln_diaregmean_bin,ln_diaregmean_nc
+      
+      ! read in Namelist. 
+      !!----------------------------------------------------------------------
+      !
+      REWIND ( numnam_ref )              ! Read Namelist nam_diatmb in referdiatmbence namelist : TMB diagnostics
+      READ   ( numnam_ref, nam_diaregmean, IOSTAT=ios, ERR= 901 )
+901   IF( ios /= 0 ) CALL ctl_nam ( ios , 'nam_diaregmean in reference namelist', lwp )
+
+      REWIND( numnam_cfg )              ! Namelist nam_diatmb in configuration namelist  TMB diagnostics
+      READ  ( numnam_cfg, nam_diaregmean, IOSTAT = ios, ERR = 902 )
+902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'nam_diaregmean in configuration namelist', lwp )
+      IF(lwm) WRITE ( numond, nam_diaregmean )
+
+      IF(lwp) THEN                   ! Control print
+          WRITE(numout,*)
+          WRITE(numout,*) 'dia_regmean_init : Output regional mean Diagnostics'
+          WRITE(numout,*) '~~~~~~~~~~~~'
+          WRITE(numout,*) 'Namelist nam_regmean : set regmeanoutputs '
+          WRITE(numout,*) 'Switch for regmean diagnostics (T) or not (F)  ln_diaregmean  = ', ln_diaregmean
+          WRITE(numout,*) 'Switch for regmean ascii output (T) or not (F)  ln_diaregmean_ascii  = ', ln_diaregmean_ascii
+          WRITE(numout,*) 'Switch for regmean binary output (T) or not (F)  ln_diaregmean_bin  = ', ln_diaregmean_bin
+          WRITE(numout,*) 'Switch for regmean netcdf output (T) or not (F)  ln_diaregmean_nc  = ', ln_diaregmean_nc
+      ENDIF
+      
+      !ALLOCATE( tmp_field_mat(jpi,jpj,9),  STAT= ierr )
+      !ALLOCATE( tmp_field_mat(jpi,jpj,7),  STAT= ierr ) !SS/NB/DT T/S, SSH
+      !ALLOCATE( tmp_field_mat(jpi,jpj,8),  STAT= ierr ) !SS/NB/DT T/S, SSH, MLD
+      ALLOCATE( tmp_field_mat(jpi,jpj,11),  STAT= ierr ) !SS/NB/DT T/S, SSH, MLD, PEA, PEAT, PEAS
+          IF( ierr /= 0 )   CALL ctl_stop( 'tmp_field_mat: failed to allocate tmp_region_mask_real array' )
+      tmp_field_mat(:,:,:) = 0.
+      tmp_field_cnt = 0
+      
+      IF (ln_diaregmean) THEN
+      
+      
+         
+          ! Open region mask for region means, and retrieve the size of the mask (number of levels)          
+          CALL iom_open ( 'region_mask.nc', inum )
+          idmaskvar = iom_varid( inum, 'mask', kdimsz=zdimsz, kndims=zndims, ldstop = .FALSE.)          
+          nmasks = zdimsz(3)
+          
+          ! read in the region mask (which contains floating point numbers) into a temporary array of reals.
+          ALLOCATE( tmp_region_mask_real(jpi,jpj,nmasks),  STAT= ierr )
+          IF( ierr /= 0 )   CALL ctl_stop( 'dia_regmean_init: failed to allocate tmp_region_mask_real array' )
+          
+          ! Use jpdom_unknown to read in a n layer mask.
+          tmp_region_mask_real(:,:,:) = 0
+          CALL iom_get( inum, jpdom_unknown, 'mask', tmp_region_mask_real(1:nlci,1:nlcj,1:nmasks),   &
+              &          kstart = (/ mig(1),mjg(1),1 /), kcount = (/ nlci,nlcj,nmasks /) )
+          
+          CALL iom_close( inum )
+          
+          !Convert the region mask of reals into one of integers. 
+          
+          ALLOCATE( region_mask(jpi,jpj,nmasks),  STAT= ierr )
+          IF( ierr /= 0 )   CALL ctl_stop( 'dia_regmean_init: failed to allocate region_mask array' )
+          region_mask(:,:,:) = 0
+          region_mask = int(tmp_region_mask_real(:,:,:))
+          DEALLOCATE( tmp_region_mask_real)
+          
+          
+          ALLOCATE( nreg_mat(nmasks),  STAT= ierr )
+          IF( ierr /= 0 )   CALL ctl_stop( 'dia_regmean_init: failed to allocate nreg_mat array' )
+
+          ! work out the number of regions in each mask, asssuming land is 0, and the regions are consectively numbered, 
+          ! without missing any number, so the number of regions is the maximum number + 1 (for land). mpp_max across the 
+          ! processors to get the global maxima
+          DO maskno = 1,nmasks
+              tmpint = maxval(region_mask(:,:,maskno))
+              CALL mpp_max( tmpint )
+              nreg_mat(maskno) = tmpint + 1
+          END DO
+        
+          IF(lwp) THEN
+              ! if writing out as binary and text, open the files. 
+              IF ( ln_diaregmean_bin ) THEN
+                  ! Open binary for region means
+                  OPEN( UNIT=73, FILE='region_mean_timeseries.dat', FORM='UNFORMATTED', STATUS='REPLACE' )
+              ENDIF
+              
+              IF ( ln_diaregmean_ascii ) THEN
+                  ! Open text files for region means
+                  OPEN( UNIT=37, FILE='region_mean_timeseries.txt', FORM='FORMATTED', STATUS='REPLACE' )
+              ENDIF
+          ENDIF
+     ENDIF
+
+   END SUBROUTINE dia_regmean_init
+
+   SUBROUTINE dia_calctmb_region_mean( pinfield,pouttmb )
+      !!---------------------------------------------------------------------
+      !!                  ***  ROUTINE dia_calctmb_region_mean  ***
+      !!                   
+      !! ** Purpose :    Find the Top, Mid, Bottom and Top minus Bottom fields of water Column
+      !!
+      !! ** Method  :   
+      !!      use mbathy to find surface, mid and bottom of model levels
+      !!
+      !! History :
+      !!   3.6  !  08-14  (E. O'Dea) Routine based on dia_wri_foam
+      !!----------------------------------------------------------------------
+      !! * Modules used
+
+      ! Routine to map 3d field to top, middle, bottom
+      IMPLICIT NONE
+
+
+      ! Routine arguments
+      REAL(wp), DIMENSION(jpi, jpj, jpk), INTENT(IN   ) :: pinfield    ! Input 3d field and mask
+      !REAL(wp), DIMENSION(jpi, jpj, 4  ), INTENT(  OUT) :: pouttmb     ! Output top, middle, bottom and surface minus bed
+      REAL(wp), DIMENSION(jpi, jpj, 3  ), INTENT(  OUT) :: pouttmb     ! Output top, middle, bottom and surface minus bed
+
+      ! Local variables
+      INTEGER :: ji,jj,jk  ! Dummy loop indices
+
+      ! Local Real
+      REAL(wp)                         ::   zmdi  !  set masked values
+
+      zmdi=1.e+20 !missing data indicator for masking
+
+      ! Calculate top
+      pouttmb(:,:,1) = pinfield(:,:,1)*tmask(:,:,1)  + zmdi*(1.0-tmask(:,:,1))
+
+     ! Calculate middle
+      !DO jj = 1,jpj
+      !    DO ji = 1,jpi
+      !        jk              = max(1,mbathy(ji,jj)/2)
+      !        pouttmb(ji,jj,2) = pinfield(ji,jj,jk)*tmask(ji,jj,jk)  + zmdi*(1.0-tmask(ji,jj,jk))
+      !    END DO
+      !END DO
+
+      ! Calculate bottom, and top minus bottom
+      DO jj = 1,jpj
+          DO ji = 1,jpi
+              jk              = max(1,mbathy(ji,jj) - 1)
+              !pouttmb(ji,jj,3) = pinfield(ji,jj,jk)*tmask(ji,jj,jk)  + zmdi*(1.0-tmask(ji,jj,jk))
+              !pouttmb(ji,jj,4) = (pouttmb(ji,jj,1) - pouttmb(ji,jj,3))*tmask(ji,jj,1)  + zmdi*(1.0-tmask(ji,jj,1))
+              pouttmb(ji,jj,2) = pinfield(ji,jj,jk)*tmask(ji,jj,jk)  + zmdi*(1.0-tmask(ji,jj,jk))
+              pouttmb(ji,jj,3) = (pouttmb(ji,jj,1) - pouttmb(ji,jj,2))*tmask(ji,jj,1)  + zmdi*(1.0-tmask(ji,jj,1))
+          END DO
+      END DO
+
+   END SUBROUTINE dia_calctmb_region_mean
+
+
+   SUBROUTINE dia_regmean( kt ) 
+      !!----------------------------------------------------------------------
+      !!                 ***  ROUTINE dia_regmean  ***
+      !! ** Purpose :   Produce regional mean diagnostics
+      !!
+      !! ** Method  :   calls dia_wri_region_mean to calculate and write the regional means for a number of variables, 
+      !!                (calling dia_calctmb_region_mean where necessary).
+      !!                
+      !!                Closes all text and binary files on last time step
+      !!                
+      !!      
+      !!      
+      !!
+      !! History :
+      !!   3.6  !  11-16  (J. Tinker) 
+      !!         
+      !!--------------------------------------------------------------------
+      REAL(wp), POINTER, DIMENSION(:,:,:) :: zwtmbT    ! temporary T workspace 
+      REAL(wp), POINTER, DIMENSION(:,:,:) :: zwtmbS    ! temporary S workspace 
+      REAL(wp)                         ::   zmdi      ! set masked values
+      INTEGER, INTENT( in ) ::   kt      ! ocean time-step index
+      
+      REAL(wp)                         ::   zdt  ! temporary reals
+      INTEGER                          ::   i_steps, ierr         ! no of timesteps per hour, allocation error index
+      INTEGER                          ::   maskno,jj,ji,jm,nreg ! indices of mask, i and j, and number of regions
+      REAL(wp)                         ::   start_reg_mean_sub,finish_reg_mean_sub
+      
+      zmdi=1.e+20 !missing data indicator for maskin
+
+      IF (ln_diaregmean) THEN
+          ! If regional mean calculations required by namelist
+          ! -----------------
+          ! identify hourly time steps (not used)
+          zdt = rdt
+          IF( nacc == 1 ) zdt = rdtmin
+
+          IF( MOD( 3600,INT(zdt) ) == 0 ) THEN
+              i_steps = 3600/INT(zdt)
+          ELSE
+              CALL ctl_stop('STOP', 'dia_regmean: timestep must give MOD(3600,rdt) = 0 otherwise no hourly values are possible')
+          ENDIF
+          
+          !i_steps = 1
+          
+          !Extract 2d fields from 3d T and S with dia_calctmb_region_mean
+          CALL wrk_alloc( jpi , jpj, 3 , zwtmbT )
+          CALL wrk_alloc( jpi , jpj, 3 , zwtmbS )
+              
+          CALL dia_calctmb_region_mean(  tsn(:,:,:,jp_tem),zwtmbT)
+          CALL dia_calctmb_region_mean(  tsn(:,:,:,jp_sal),zwtmbS)
+              
+          tmp_field_mat(:,:,1) = tmp_field_mat(:,:,1) + zwtmbT(:,:,1)
+          tmp_field_mat(:,:,2) = tmp_field_mat(:,:,2) + zwtmbT(:,:,2)
+          tmp_field_mat(:,:,3) = tmp_field_mat(:,:,3) + zwtmbT(:,:,3)
+          tmp_field_mat(:,:,4) = tmp_field_mat(:,:,4) + zwtmbS(:,:,1)
+          tmp_field_mat(:,:,5) = tmp_field_mat(:,:,5) + zwtmbS(:,:,2)
+          tmp_field_mat(:,:,6) = tmp_field_mat(:,:,6) + zwtmbS(:,:,3)
+          tmp_field_mat(:,:,7) = tmp_field_mat(:,:,7) + sshn(:,:)
+          tmp_field_mat(:,:,8) = tmp_field_mat(:,:,8) + hmld_kara(:,:)!hmlp(:,:)
+          tmp_field_mat(:,:,9) = tmp_field_mat(:,:,9) + pea(:,:)
+          tmp_field_mat(:,:,10) = tmp_field_mat(:,:,10) + peat(:,:)
+          tmp_field_mat(:,:,11) = tmp_field_mat(:,:,11) + peas(:,:)
+          
+          
+          !tmp_field_mat(:,:,1) = tmp_field_mat(:,:,1) + zwtmbT(:,:,1)
+          !tmp_field_mat(:,:,2) = tmp_field_mat(:,:,2) + zwtmbT(:,:,2)
+          !tmp_field_mat(:,:,3) = tmp_field_mat(:,:,3) + zwtmbT(:,:,3)
+          !tmp_field_mat(:,:,4) = tmp_field_mat(:,:,4) + zwtmbT(:,:,4)
+          !tmp_field_mat(:,:,5) = tmp_field_mat(:,:,5) + zwtmbS(:,:,1)
+          !tmp_field_mat(:,:,6) = tmp_field_mat(:,:,6) + zwtmbS(:,:,2)
+          !tmp_field_mat(:,:,7) = tmp_field_mat(:,:,7) + zwtmbS(:,:,3)
+          !tmp_field_mat(:,:,8) = tmp_field_mat(:,:,8) + zwtmbS(:,:,4)
+          !tmp_field_mat(:,:,9) = tmp_field_mat(:,:,9) + sshn(:,:)
+          
+          tmp_field_cnt = tmp_field_cnt + 1
+          
+          IF( MOD( kt, i_steps ) == 0  .and. kt .ne. nn_it000 ) THEN
+
+              
+              
+              !CALL dia_wri_region_mean(kt, "sst" , tmp_field_mat(:,:,1)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Temperature
+              !!CALL dia_wri_region_mean(kt, "mdt" , tmp_field_mat(:,:,2)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Temperature
+              !CALL dia_wri_region_mean(kt, "nbt" , tmp_field_mat(:,:,3)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Temperature
+              !CALL dia_wri_region_mean(kt, "dft" , tmp_field_mat(:,:,4)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Temperature
+!
+!              CALL dia_wri_region_mean(kt, "sss" , tmp_field_mat(:,:,5)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Salinity 
+!              !CALL dia_wri_region_mean(kt, "mds" , tmp_field_mat(:,:,6)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Salinity
+!              CALL dia_wri_region_mean(kt, "nbs" , tmp_field_mat(:,:,7)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Salinity
+!              CALL dia_wri_region_mean(kt, "dfs" , tmp_field_mat(:,:,8)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Salinity;
+!
+!              CALL dia_wri_region_mean(kt, "ssh" , tmp_field_mat(:,:,9)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Temperature
+              
+              
+
+              
+              
+              !CALL cpu_time(start_reg_mean_sub)
+              
+              CALL dia_wri_region_mean(kt, "sst" , tmp_field_mat(:,:,1)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Temperature
+              CALL dia_wri_region_mean(kt, "nbt" , tmp_field_mat(:,:,2)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Temperature
+              CALL dia_wri_region_mean(kt, "dft" , tmp_field_mat(:,:,3)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Temperature
+
+              CALL dia_wri_region_mean(kt, "sss" , tmp_field_mat(:,:,4)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Salinity 
+              CALL dia_wri_region_mean(kt, "nbs" , tmp_field_mat(:,:,5)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Salinity
+              CALL dia_wri_region_mean(kt, "dfs" , tmp_field_mat(:,:,6)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Salinity;
+
+              CALL dia_wri_region_mean(kt, "ssh" , tmp_field_mat(:,:,7)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Temperature
+              
+              CALL dia_wri_region_mean(kt, "mldkara" , tmp_field_mat(:,:,8)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Temperature
+
+              CALL dia_wri_region_mean(kt, "pea" , tmp_field_mat(:,:,9)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Temperature
+              CALL dia_wri_region_mean(kt, "peat" , tmp_field_mat(:,:,10)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Temperature
+              CALL dia_wri_region_mean(kt, "peas" , tmp_field_mat(:,:,11)/real(tmp_field_cnt,wp))!,region_mask,nreg_mat(:,:,maskno),maskno,nreg_mat(maskno)  )    ! tmb Temperature
+              
+              !CALL cpu_time(finish_reg_mean_sub)          
+              !WRITE(numout,'("kt = ",i05,";  Reg_mean_sub Time = ",f10.3," milliseconds: ",f6.3,";  ",f6.3)') &
+              !  & kt,(finish_reg_mean_sub-start_reg_mean_sub)*1000.,finish_reg_mean_sub,start_reg_mean_sub
+              
+              tmp_field_mat(:,:,:) = 0.
+              tmp_field_cnt = 0
+      
+          ENDIF
+          
+          
+          
+          
+          
+          ! If on the last time step, close binary and ascii files. 
+          IF( kt == nitend ) THEN
+              IF(lwp) THEN
+                  IF ( ln_diaregmean_bin ) THEN
+                      !Closing binary files for regional mean time series.
+                      CLOSE(73)
+                  ENDIF
+                  IF ( ln_diaregmean_ascii ) THEN
+                      !Closing text files for regional mean time series.
+                      CLOSE(37)
+                  ENDIF
+              DEALLOCATE( region_mask, nreg_mat, tmp_field_mat)
+              ENDIF
+          ENDIF
+            
+          
+      ELSE
+          CALL ctl_warn('dia_regmean: regmean diagnostic is set to false you should not have seen this')
+      ENDIF
+      
+   END SUBROUTINE dia_regmean
+   
+   
+   SUBROUTINE dia_wri_region_mean(kt, name,         infield  )
+      !!---------------------------------------------------------------------
+      !!                  ***  ROUTINE dia_tmb  ***
+      !!                   
+      !! ** Purpose :   Calculate and write region mean time series for 2d arrays
+      !!
+      !! ** Method  :   
+      !!      use 
+      !!
+      !! History :
+      !!   ??  !  15/10/2015  (JTinker) Routine taken from old dia_wri_foam
+      !!----------------------------------------------------------------------
+      !! * Modules used
+      !use lib_mpp
+      !use lib_fortr
+      IMPLICIT NONE
+      
+      INTEGER, INTENT(in) ::   kt
+      CHARACTER (len=15) , INTENT(IN   ) ::    name
+      REAL(wp), DIMENSION(jpi, jpj), INTENT(IN   ) :: infield    ! Input 3d field and mask
+      
+      ! Local variables
+      INTEGER, DIMENSION(jpi, jpj) :: internal_region_mask    ! Input 3d field and mask
+      REAL(wp), ALLOCATABLE, DIMENSION(:) ::   zrmet_ave,zrmet_tot,zrmet_var,zrmet_cnt,zrmet_mask_id,zrmet_reg_id
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:) ::   zrmet_out
+      REAL(wp), ALLOCATABLE,   DIMENSION(:) ::   ave_mat,tot_mat,num_mat,var_mat,ssq_mat,cnt_mat,reg_id_mat,mask_id_mat   !: region_mask
+      !REAL(wp), ALLOCATABLE,   DIMENSION(:) ::   tot_mat_2
+      !REAL(wp), ALLOCATABLE,   DIMENSION(:) ::      !: region_mask
+      REAL(wp)                         ::   zmdi      ! set masked values
+      INTEGER :: maskno,nreg  ! ocean time-step indexocean time step            
+      INTEGER :: ji,jj,jk,ind,jm ! Dummy loop indices
+      INTEGER :: reg_ind_cnt ! Dummy loop indices
+      
+      INTEGER  ::   ierr      
+      REAL(wp)  :: tmpreal
+      CHARACTER(LEN=80) :: FormatString,nreg_string
+      REAL(wp), ALLOCATABLE, DIMENSION(:,:,:) ::   dummy_zrmet
+      
+      !REAL(wp) ::     start_reg_mean_loop,finish_reg_mean_loop,start_reg_mean_mpp, finish_reg_mean_mpp,start_reg_mean_mpp_mat,finish_reg_mean_mpp_mat
+      
+      
+      zmdi=1.e+20 !missing data indicator for maskin
+      
+      !Allocate output arrays for iomput, set to zmdi, and set a region counter = 1
+      
+      !ALLOCATE( zrmet_ave(jpi,jpj,n_regions_output),  STAT= ierr )
+      !  IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate zrmet_ave array' )
+      !ALLOCATE( zrmet_tot(jpi,jpj,n_regions_output),  STAT= ierr )
+      !  IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate zrmet_tot array' )
+      !ALLOCATE( zrmet_var(jpi,jpj,n_regions_output),  STAT= ierr )
+      !  IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate zrmet_var array' )
+      !ALLOCATE( zrmet_cnt(jpi,jpj,n_regions_output),  STAT= ierr )
+      !  IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate zrmet_cnt array' )
+      !ALLOCATE( zrmet_mask_id(jpi,jpj,n_regions_output),  STAT= ierr )
+      !  IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate zrmet_mask_id array' )
+      !ALLOCATE( zrmet_reg_id(jpi,jpj,n_regions_output),  STAT= ierr )
+      !  IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate zrmet_reg_id array' )
+      
+      
+      ALLOCATE( zrmet_ave(n_regions_output),  STAT= ierr )
+        IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate zrmet_ave array' )
+      ALLOCATE( zrmet_tot(n_regions_output),  STAT= ierr )
+        IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate zrmet_tot array' )
+      ALLOCATE( zrmet_var(n_regions_output),  STAT= ierr )
+        IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate zrmet_var array' )
+      ALLOCATE( zrmet_cnt(n_regions_output),  STAT= ierr )
+        IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate zrmet_cnt array' )
+      ALLOCATE( zrmet_mask_id(n_regions_output),  STAT= ierr )
+        IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate zrmet_mask_id array' )
+      ALLOCATE( zrmet_reg_id(n_regions_output),  STAT= ierr )
+        IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate zrmet_reg_id array' )
+      
+      ALLOCATE( zrmet_out(jpi,jpj,n_regions_output),  STAT= ierr )
+        IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate zrmet_reg_id array' )
+      
+      !zrmet_ave(:,:,:) = zmdi
+      !zrmet_tot(:,:,:) = zmdi
+      !zrmet_var(:,:,:) = zmdi
+      !zrmet_cnt(:,:,:) = zmdi
+      !zrmet_mask_id(:,:,:) = zmdi
+      !zrmet_reg_id(:,:,:) = zmdi
+      
+      
+      zrmet_ave(:) = zmdi
+      zrmet_tot(:) = zmdi
+      zrmet_var(:) = zmdi
+      zrmet_cnt(:) = zmdi
+      zrmet_mask_id(:) = zmdi
+      zrmet_reg_id(:) = zmdi
+      
+      reg_ind_cnt = 1
+      
+      
+      ! loop though the masks
+      DO maskno = 1,nmasks
+          
+          
+          ! For each mask, get the number of regions (nreg), and a local copy of the region. 
+          nreg = nreg_mat(maskno)
+          internal_region_mask = region_mask(:,:,maskno)
+          
+          ! allocate temporary stat arrays, and set to zero
+          ALLOCATE( ave_mat(nreg),  STAT= ierr )
+          IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate ave_mat array' )
+          ALLOCATE( tot_mat(nreg),      STAT= ierr )
+          IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate tot_mat array' )
+          !ALLOCATE( tot_mat_2(nreg),  STAT= ierr )
+          !IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate tot_mat_2 array' )
+          
+          ALLOCATE( num_mat(nreg),  STAT= ierr )
+          IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate num_mat array' )
+          ALLOCATE( var_mat(nreg),  STAT= ierr )
+          IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate var_mat array' )
+          ALLOCATE( ssq_mat(nreg),  STAT= ierr )
+          IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate ssq_mat array' )
+          ALLOCATE( cnt_mat(nreg),  STAT= ierr )
+          IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate cnt_mat array' )
+          ALLOCATE( reg_id_mat(nreg),  STAT= ierr )
+          IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate reg_id_mat array' )
+          ALLOCATE( mask_id_mat(nreg),  STAT= ierr )
+          IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate mask_id_mat array' )
+          
+          ave_mat(:) = 0.
+          tot_mat(:) = 0.
+          num_mat(:) = 0.
+          var_mat(:) = 0.
+          cnt_mat(:) = 0.
+          ssq_mat(:) = 0.
+          reg_id_mat(:) = 0.
+          mask_id_mat(:) = 0.
+          
+          
+          ! loop though the array. for each sea grid box where tmask == 1), 
+          ! read which region the grid box is in, add the value of the gridbox (and its square) 
+          ! to the total for that region, and then increment the counter for that region.
+          !CALL cpu_time(start_reg_mean_loop)
+          !WRITE(numout,*) kt,start_reg_mean_loop
+          DO ji = 1,jpi
+              DO jj = 1,jpj
+                    IF ( tmask(ji,jj,1) == 1.0_wp ) THEN
+                        ind = internal_region_mask(ji,jj)+1
+                        tot_mat(ind) = tot_mat(ind) + (infield(ji,jj))
+                        ssq_mat(ind) = ssq_mat(ind) + ( infield(ji,jj) *  infield(ji,jj))
+                        cnt_mat(ind) = cnt_mat(ind) + 1.
+                    ENDIF
+              END DO
+          END DO
+          !CALL cpu_time(finish_reg_mean_loop)          
+          !WRITE(numout,'("kt = ",i05,"; maskno = ",i02,";  Reg_mean_loop Time = ",f6.3," milliseconds.",f6.3,";, ",f6.3)') &
+          !      &  kt,maskno,(finish_reg_mean_loop-start_reg_mean_loop)*1000.,finish_reg_mean_loop,start_reg_mean_loop
+          
+          ! sum the totals, the counts, and the squares across the processors
+          !CALL cpu_time(start_reg_mean_mpp)
+          
+          
+          
+          !IF( lk_mpp ) THEN
+          !    DO jj = 1,nreg
+          !        tmpreal = tot_mat(jj)
+          !        CALL mpp_sum( tmpreal )
+          !        tot_mat(jj) = tmpreal
+          !        
+          !        tmpreal = ssq_mat(jj)
+          !        CALL mpp_sum( tmpreal )
+          !        ssq_mat(jj) = tmpreal
+          !        
+          !        tmpreal = cnt_mat(jj)
+          !        CALL mpp_sum( tmpreal )
+          !        cnt_mat(jj) = tmpreal
+          !    END DO
+          !ENDIF
+          
+          
+          
+          !if (lwp) WRITE(numout,*) "mpp_sum loop tot_mat",tot_mat
+          !CALL cpu_time(finish_reg_mean_mpp)          
+          !WRITE(numout,'("kt = ",i05,"; maskno = ",i02,";  Reg_mean_mpp Time = ",f6.3," milliseconds.",f6.3,";, ",f6.3)') &
+          !      &  kt,maskno,(finish_reg_mean_loop-start_reg_mean_mpp)*1000.,finish_reg_mean_mpp,start_reg_mean_mpp
+          
+          !DO jj = 1,nreg
+          !   tot_mat_2(jj) = tot_mat(jj)
+          !enddo
+          !CALL cpu_time( start_reg_mean_mpp_mat )
+          
+          
+          CALL mpp_sum( tot_mat,nreg )
+          CALL mpp_sum( ssq_mat,nreg )
+          CALL mpp_sum( cnt_mat,nreg )
+          
+          
+          
+          !CALL cpu_time(finish_reg_mean_mpp_mat)          
+          !WRITE(numout,'("kt = ",i05,"; maskno = ",i02,";  Reg_mean_mpp_mat Time = ",f6.3," milliseconds.",f6.3,";, ",f6.3)') &
+          !      &  kt,maskno,(finish_reg_mean_mpp_mat-start_reg_mean_mpp_mat)*1000.,finish_reg_mean_loop,start_reg_mean_mpp_mat
+                
+          
+          !DO jj = 1,nreg
+          !    WRITE(numout,*) "mpp_sum array tot_mat",jj,tot_mat(jj),ssq_mat(jj),cnt_mat(jj)
+          !enddo
+          
+          !calculate the mean and variance from the total, sum of squares and the count. 
+          
+          ave_mat = tot_mat(:)/cnt_mat(:)
+          var_mat = ssq_mat(:)/cnt_mat(:) - (ave_mat(:)*ave_mat(:))
+          
+          
+          !mask array of mask and region number. 
+          DO jj = 1,nreg
+              reg_id_mat(jj) = real(jj-1)
+              mask_id_mat(jj) = real(maskno)
+          END DO
+          
+          
+          !write text and binary, and note region statistics for current mask for later iom_put
+          IF( lwp ) THEN 
+          
+              !Write out ascii and binary if requred
+              IF ( ln_diaregmean_bin ) THEN
+                  !Writing out regional mean time series to binary files
+                  WRITE(73) name,kt,maskno,n_regions_output
+                  WRITE(73) ave_mat
+                  WRITE(73) tot_mat
+                  WRITE(73) var_mat
+                  WRITE(73) ssq_mat
+                  WRITE(73) cnt_mat
+              ENDIF
+              
+              IF ( ln_diaregmean_ascii  ) THEN
+                  !Writing out regional mean time series to text files
+
+                  WRITE(nreg_string, "(I5)") nreg
+                  FormatString = "(A11,"//trim(nreg_string)//"F15.3)"
+                  WRITE(37, FMT="(A4,I6,I6)") name,kt,maskno            
+                  WRITE(37, FMT=trim(FormatString)) trim(name)//" "//"ave_mat:", ave_mat
+                  WRITE(37, FMT=trim(FormatString)) trim(name)//" "//"tot_mat:", tot_mat
+                  WRITE(37, FMT=trim(FormatString)) trim(name)//" "//"var_mat:", var_mat
+                  WRITE(37, FMT=trim(FormatString)) trim(name)//" "//"ssq_mat:", ssq_mat
+                  WRITE(37, FMT=trim(FormatString)) trim(name)//" "//"cnt_mat:", cnt_mat
+                  WRITE(37, FMT=trim(FormatString)) trim(name)//" "//"reg_mat:", reg_id_mat
+                  WRITE(37, FMT=trim(FormatString)) trim(name)//" "//"msk_mat:", mask_id_mat
+
+              ENDIF
+              
+              DO jm = 1,nreg
+                  !zrmet_ave(    :,:,reg_ind_cnt) =     ave_mat(jm)
+                  !zrmet_tot(    :,:,reg_ind_cnt) =     tot_mat(jm)
+                  !zrmet_var(    :,:,reg_ind_cnt) =     var_mat(jm)
+                  !zrmet_cnt(    :,:,reg_ind_cnt) =     cnt_mat(jm)
+                  !zrmet_reg_id( :,:,reg_ind_cnt) =  reg_id_mat(jm)
+                  !zrmet_mask_id(:,:,reg_ind_cnt) = mask_id_mat(jm)
+                  
+                  zrmet_ave(    reg_ind_cnt) =     ave_mat(jm)
+                  zrmet_tot(    reg_ind_cnt) =     tot_mat(jm)
+                  zrmet_var(    reg_ind_cnt) =     var_mat(jm)
+                  zrmet_cnt(    reg_ind_cnt) =     cnt_mat(jm)
+                  zrmet_reg_id( reg_ind_cnt) =  reg_id_mat(jm)
+                  zrmet_mask_id(reg_ind_cnt) = mask_id_mat(jm)
+                
+                  reg_ind_cnt = reg_ind_cnt + 1 
+              END DO
+          
+          ENDIF
+        
+          DEALLOCATE(ave_mat,tot_mat,num_mat,var_mat,ssq_mat,cnt_mat,reg_id_mat,mask_id_mat)
+          !DEALLOCATE(tot_mat_2)
+      END DO
+      
+      
+      !With current field_def.xml and iodef.xml, these fields must be output, so set to dummy values if not required.
+      
+      IF ( ln_diaregmean_nc ) THEN
+      
+      
+      
+          !CALL iom_put( "reg_" // trim(name) // '_ave', zrmet_ave )
+          !CALL iom_put( "reg_" // trim(name) // '_tot', zrmet_tot )
+          !CALL iom_put( "reg_" // trim(name) // '_var', zrmet_var )
+          !CALL iom_put( "reg_" // trim(name) // '_cnt', zrmet_cnt )
+          !CALL iom_put( "reg_" // trim(name) // '_reg_id', zrmet_reg_id )
+          !CALL iom_put( "reg_" // trim(name) // '_mask_id', zrmet_mask_id )
+      
+          DO jm = 1,n_regions_output
+            zrmet_out(:,:,jm) = zrmet_ave(jm)
+          END DO      
+          CALL iom_put( "reg_" // trim(name) // '_ave', zrmet_out )
+          zrmet_out(:,:,:) = 0
+      
+          DO jm = 1,n_regions_output
+            zrmet_out(:,:,jm) = zrmet_tot(jm)
+          END DO          
+          CALL iom_put( "reg_" // trim(name) // '_tot', zrmet_out )
+          zrmet_out(:,:,:) = 0
+      
+          DO jm = 1,n_regions_output
+            zrmet_out(:,:,jm) = zrmet_var(jm)
+          END DO          
+          CALL iom_put( "reg_" // trim(name) // '_var', zrmet_out )
+          zrmet_out(:,:,:) = 0
+      
+          DO jm = 1,n_regions_output
+            zrmet_out(:,:,jm) = zrmet_cnt(jm)
+          END DO          
+          CALL iom_put( "reg_" // trim(name) // '_cnt', zrmet_out )
+          zrmet_out(:,:,:) = 0
+      
+          DO jm = 1,n_regions_output
+            zrmet_out(:,:,jm) = zrmet_reg_id(jm)
+          END DO          
+          CALL iom_put( "reg_" // trim(name) // '_reg_id', zrmet_out )
+          zrmet_out(:,:,:) = 0
+      
+          DO jm = 1,n_regions_output
+            zrmet_out(:,:,jm) = zrmet_mask_id(jm)
+          END DO          
+          CALL iom_put( "reg_" // trim(name) // '_mask_id', zrmet_out )
+          zrmet_out(:,:,:) = 0
+      ELSE
+        
+          ALLOCATE( dummy_zrmet(jpi,jpj,n_regions_output),  STAT= ierr )
+            IF( ierr /= 0 )   CALL ctl_stop( 'dia_wri_region_mean: failed to allocate dummy_zrmet array' )
+
+          DO jm = 1,n_regions_output
+              dummy_zrmet(:,:,jm) =     real(jm,wp)
+          END DO
+
+          DO jm = 1,9
+              CALL iom_put( "reg_" // trim(name) // '_ave', dummy_zrmet )
+              CALL iom_put( "reg_" // trim(name) // '_tot', dummy_zrmet )
+              CALL iom_put( "reg_" // trim(name) // '_var', dummy_zrmet )
+              CALL iom_put( "reg_" // trim(name) // '_cnt', dummy_zrmet )
+              CALL iom_put( "reg_" // trim(name) // '_reg_id', dummy_zrmet )
+              CALL iom_put( "reg_" // trim(name) // '_mask_id', dummy_zrmet )
+          END DO
+    
+          DEALLOCATE( dummy_zrmet)
+      ENDIF
+      
+      DEALLOCATE(zrmet_ave,zrmet_tot,zrmet_var,zrmet_cnt,zrmet_mask_id,zrmet_reg_id,zrmet_out)
+   END SUBROUTINE dia_wri_region_mean
+
+
+   !!======================================================================
+END MODULE diaregmean
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diatmb.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diatmb.F90	(revision 7567)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diatmb.F90	(revision 7567)
@@ -0,0 +1,176 @@
+MODULE diatmb 
+   !!======================================================================
+   !!                       ***  MODULE  diaharm  ***
+   !! Harmonic analysis of tidal constituents 
+   !!======================================================================
+   !! History :  3.6  !  2014  (E O'Dea)  Original code
+   !!----------------------------------------------------------------------
+   USE oce             ! ocean dynamics and tracers variables
+   USE dom_oce         ! ocean space and time domain
+   USE in_out_manager  ! I/O units
+   USE iom             ! I/0 library
+   USE wrk_nemo        ! working arrays
+
+
+   IMPLICIT NONE
+   PRIVATE
+
+   LOGICAL , PUBLIC ::   ln_diatmb     !: Top Middle and Bottom output 
+   PUBLIC   dia_tmb_init            ! routine called by nemogcm.F90
+   PUBLIC   dia_tmb                 ! routine called by diawri.F90
+
+   !!----------------------------------------------------------------------
+   !! NEMO/OPA 3.6 , NEMO Consortium (2014)
+   !! $Id$
+   !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
+   !!----------------------------------------------------------------------
+CONTAINS
+
+   SUBROUTINE dia_tmb_init 
+      !!---------------------------------------------------------------------------
+      !!                  ***  ROUTINE dia_tmb_init  ***
+      !!     
+      !! ** Purpose: Initialization of tmb namelist 
+      !!        
+      !! ** Method : Read namelist
+      !!   History
+      !!   3.6  !  08-14  (E. O'Dea) Routine to initialize dia_tmb
+      !!---------------------------------------------------------------------------
+      !!
+      INTEGER ::   ios                 ! Local integer output status for namelist read
+      !
+      NAMELIST/nam_diatmb/ ln_diatmb
+      !!----------------------------------------------------------------------
+      !
+      REWIND ( numnam_ref )              ! Read Namelist nam_diatmb in reference namelist : TMB diagnostics
+      READ   ( numnam_ref, nam_diatmb, IOSTAT=ios, ERR= 901 )
+901   IF( ios /= 0 ) CALL ctl_nam ( ios , 'nam_diatmb in reference namelist', lwp )
+ 
+      REWIND( numnam_cfg )              ! Namelist nam_diatmb in configuration namelist  TMB diagnostics
+      READ  ( numnam_cfg, nam_diatmb, IOSTAT = ios, ERR = 902 )
+902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'nam_diatmb in configuration namelist', lwp )
+      IF(lwm) WRITE ( numond, nam_diatmb )
+
+      IF(lwp) THEN                   ! Control print
+         WRITE(numout,*)
+         WRITE(numout,*) 'dia_tmb_init : Output Top, Middle, Bottom Diagnostics'
+         WRITE(numout,*) '~~~~~~~~~~~~'
+         WRITE(numout,*) 'Namelist nam_diatmb : set tmb outputs '
+         WRITE(numout,*) 'Switch for TMB diagnostics (T) or not (F)  ln_diatmb  = ', ln_diatmb
+      ENDIF
+
+   END SUBROUTINE dia_tmb_init
+
+   SUBROUTINE dia_calctmb( pinfield,pouttmb )
+      !!---------------------------------------------------------------------
+      !!                  ***  ROUTINE dia_tmb  ***
+      !!                   
+      !! ** Purpose :    Find the Top, Mid and Bottom fields of water Column
+      !!
+      !! ** Method  :   
+      !!      use mbathy to find surface, mid and bottom of model levels
+      !!
+      !! History :
+      !!   3.6  !  08-14  (E. O'Dea) Routine based on dia_wri_foam
+      !!----------------------------------------------------------------------
+      !! * Modules used
+
+      ! Routine to map 3d field to top, middle, bottom
+      IMPLICIT NONE
+
+
+      ! Routine arguments
+      REAL(wp), DIMENSION(jpi, jpj, jpk), INTENT(IN   ) :: pinfield    ! Input 3d field and mask
+      REAL(wp), DIMENSION(jpi, jpj, 3  ), INTENT(  OUT) :: pouttmb     ! Output top, middle, bottom
+
+
+
+      ! Local variables
+      INTEGER :: ji,jj,jk  ! Dummy loop indices
+
+      ! Local Real
+      REAL(wp)                         ::   zmdi  !  set masked values
+
+      zmdi=1.e+20 !missing data indicator for masking
+
+      ! Calculate top
+      pouttmb(:,:,1) = pinfield(:,:,1)*tmask(:,:,1)  + zmdi*(1.0-tmask(:,:,1))
+
+      ! Calculate middle
+      DO jj = 1,jpj
+         DO ji = 1,jpi
+            jk              = max(1,mbathy(ji,jj)/2)
+            pouttmb(ji,jj,2) = pinfield(ji,jj,jk)*tmask(ji,jj,jk)  + zmdi*(1.0-tmask(ji,jj,jk))
+         END DO
+      END DO
+
+      ! Calculate bottom
+      DO jj = 1,jpj
+         DO ji = 1,jpi
+            jk              = max(1,mbathy(ji,jj) )
+            pouttmb(ji,jj,3) = pinfield(ji,jj,jk)*tmask(ji,jj,jk)  + zmdi*(1.0-tmask(ji,jj,jk))
+         END DO
+      END DO
+
+   END SUBROUTINE dia_calctmb
+
+
+
+   SUBROUTINE dia_tmb
+      !!----------------------------------------------------------------------
+      !!                 ***  ROUTINE dia_tmb  ***
+      !! ** Purpose :   Write diagnostics for Top, Mid and Bottom of water Column
+      !!
+      !! ** Method  :   
+      !!      use mbathy to find surface, mid and bottom of model levels
+      !!      calls calctmb to retrieve TMB values before sending to iom_put
+      !!
+      !! History :
+      !!   3.6  !  08-14  (E. O'Dea) 
+      !!         
+      !!--------------------------------------------------------------------
+      REAL(wp), POINTER, DIMENSION(:,:,:) :: zwtmb    ! temporary workspace 
+      REAL(wp)                         ::   zmdi      ! set masked values
+
+      zmdi=1.e+20 !missing data indicator for maskin
+
+      IF (ln_diatmb) THEN
+         CALL wrk_alloc( jpi , jpj, 3 , zwtmb )
+         CALL dia_calctmb(  tsn(:,:,:,jp_tem),zwtmb )
+         !ssh already output but here we output it masked
+         CALL iom_put( "sshnmasked" , sshn(:,:)*tmask(:,:,1) + zmdi*(1.0 - tmask(:,:,1)) )   ! tmb Temperature
+         CALL iom_put( "top_temp" , zwtmb(:,:,1) )    ! tmb Temperature
+         CALL iom_put( "mid_temp" , zwtmb(:,:,2) )    ! tmb Temperature
+         CALL iom_put( "bot_temp" , zwtmb(:,:,3) )    ! tmb Temperature
+         CALL iom_put( "dif_temp" , (zwtmb(:,:,1) - zwtmb(:,:,3))*tmask(:,:,1)+zmdi*(1.0-tmask(:,:,1 ) ) )   ! tmb Temperature
+         
+!         CALL iom_put( "sotrefml" , hmld_tref(:,:) )    ! "T criterion Mixed Layer Depth
+
+         CALL dia_calctmb(  tsn(:,:,:,jp_sal),zwtmb )
+         CALL iom_put( "top_sal" , zwtmb(:,:,1) )    ! tmb Salinity 
+         CALL iom_put( "mid_sal" , zwtmb(:,:,2) )    ! tmb Salinity
+         CALL iom_put( "bot_sal" , zwtmb(:,:,3) )    ! tmb Salinity
+         CALL iom_put( "dif_sal" , (zwtmb(:,:,1) - zwtmb(:,:,3))*tmask(:,:,1)+zmdi*(1.0-tmask(:,:,1 ) ) )   ! tmb Salinity
+         
+         CALL dia_calctmb(  un(:,:,:),zwtmb )
+         CALL iom_put( "top_u" , zwtmb(:,:,1) )    ! tmb  U Velocity
+         CALL iom_put( "mid_u" , zwtmb(:,:,2) )    ! tmb  U Velocity
+         CALL iom_put( "bot_u" , zwtmb(:,:,3) )    ! tmb  U Velocity
+         CALL iom_put( "dif_u" , (zwtmb(:,:,1) - zwtmb(:,:,3))*tmask(:,:,1)+zmdi*(1.0-tmask(:,:,1 ) ) )   ! tmb  U Velocity
+         
+!Called in  dynspg_ts.F90        CALL iom_put( "baro_u" , un_b )    ! Barotropic  U Velocity
+
+         CALL dia_calctmb(  vn(:,:,:),zwtmb )
+         CALL iom_put( "top_v" , zwtmb(:,:,1) )    ! tmb  V Velocity
+         CALL iom_put( "mid_v" , zwtmb(:,:,2) )    ! tmb  V Velocity
+         CALL iom_put( "bot_v" , zwtmb(:,:,3) )    ! tmb  V Velocity
+         CALL iom_put( "dif_v" , (zwtmb(:,:,1) - zwtmb(:,:,3))*tmask(:,:,1)+zmdi*(1.0-tmask(:,:,1 ) ) )   ! tmb  V Velocity
+         
+!Called in  dynspg_ts.F90       CALL iom_put( "baro_v" , vn_b )    ! Barotropic  V Velocity
+      ELSE
+         CALL ctl_warn('dia_tmb: tmb diagnostic is set to false you should not have seen this')
+      ENDIF
+
+   END SUBROUTINE dia_tmb
+   !!======================================================================
+END MODULE diatmb
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diawri.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diawri.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DIA/diawri.F90	(revision 7567)
@@ -44,4 +44,8 @@
    USE in_out_manager  ! I/O manager
    USE diadimg         ! dimg direct access file format output
+   USE diatmb          ! Top,middle,bottom output
+   USE dia25h          ! 25h Mean output
+   USE diaregmean      ! regionalmean
+   USE diapea          ! pea
    USE iom
    USE ioipsl
@@ -379,4 +383,22 @@
       CALL wrk_dealloc( jpi , jpj      , z2d )
       CALL wrk_dealloc( jpi , jpj, jpk , z3d )
+      !
+      ! If we want tmb values 
+
+      IF (ln_diatmb) THEN
+         CALL dia_tmb
+      ENDIF
+      IF (ln_dia25h) THEN
+         CALL dia_25h( kt )
+      ENDIF
+      
+      CALL dia_pea( kt )
+      
+      IF (ln_diaregmean) THEN
+          
+         !write(*,*) kt,narea,'diawri before dia_regmean'
+         CALL dia_regmean( kt )
+         !write(*,*) kt,narea,'diawri after dia_regmean'
+      ENDIF
       !
       IF( nn_timing == 1 )   CALL timing_stop('dia_wri')
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90	(revision 7567)
@@ -135,6 +135,7 @@
       !!----------------------------------------------------------------------
       USE ioipsl
+      NAMELIST/namrun/ ln_NOOS  
       NAMELIST/namrun/ cn_ocerst_indir, cn_ocerst_outdir, nn_stocklist, ln_rst_list,               &
-         &             nn_no   , cn_exp    , cn_ocerst_in, cn_ocerst_out, ln_rstart , nn_rstctl,   &
+         &             nn_no   , cn_exp    , cn_ocerst_in, cn_ocerst_out, ln_rstdate, ln_rstart , nn_rstctl,   &
          &             nn_it000, nn_itend  , nn_date0    , nn_leapy     , nn_istate , nn_stock ,   &
          &             nn_write, ln_dimgnnn, ln_mskland  , ln_cfmeta    , ln_clobber, nn_chunksz, nn_euler
@@ -174,4 +175,5 @@
          WRITE(numout,*) '      restart output directory        cn_ocerst_outdir= ', cn_ocerst_outdir
          WRITE(numout,*) '      restart logical                 ln_rstart  = ', ln_rstart
+         WRITE(numout,*) '      datestamping of restarts        ln_rstdate  = ', ln_rstdate
          WRITE(numout,*) '      start with forward time step    nn_euler   = ', nn_euler
          WRITE(numout,*) '      control of time step            nn_rstctl  = ', nn_rstctl
@@ -189,4 +191,7 @@
          WRITE(numout,*) '      multi file dimgout              ln_dimgnnn = ', ln_dimgnnn
          WRITE(numout,*) '      mask land points                ln_mskland = ', ln_mskland
+         ! JT
+         WRITE(numout,*) '      NOOS transect diagnostics       ln_NOOS    = ', ln_NOOS
+         ! JT
          WRITE(numout,*) '      additional CF standard metadata ln_cfmeta  = ', ln_cfmeta
          WRITE(numout,*) '      overwrite an existing file      ln_clobber = ', ln_clobber
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DOM/dommsk.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DOM/dommsk.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DOM/dommsk.F90	(revision 7567)
@@ -31,4 +31,5 @@
    USE wrk_nemo        ! Memory allocation
    USE timing          ! Timing
+   USE iom    ! slwa
 
    IMPLICIT NONE
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DYN/dynldf_bilap.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DYN/dynldf_bilap.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DYN/dynldf_bilap.F90	(revision 7567)
@@ -24,4 +24,8 @@
    USE wrk_nemo        ! Memory Allocation
    USE timing          ! Timing
+#if defined key_bdy  
+   USE bdy_oce         ! needed for extra diffusion in rim  
+#endif
+
 
    IMPLICIT NONE
@@ -79,4 +83,6 @@
       REAL(wp), POINTER, DIMENSION(:,:  ) :: zcu, zcv
       REAL(wp), POINTER, DIMENSION(:,:,:) :: zuf, zut, zlu, zlv
+      !
+      REAL(wp), DIMENSION(jpi,jpj) :: zfactor  ! multiplier for diffusion
       !!----------------------------------------------------------------------
       !
@@ -90,5 +96,14 @@
          WRITE(numout,*) 'dyn_ldf_bilap : iso-level bilaplacian operator'
          WRITE(numout,*) '~~~~~~~~~~~~~'
+#if defined key_bdy 
+         IF(lwp) WRITE(numout,*) '~~~~~ using sponge_factor'
+#endif
       ENDIF
+
+#if defined key_bdy 
+      zfactor(:,:) = sponge_factor(:,:) 
+#else 
+      zfactor(:,:) = 1.0 
+#endif 
 
 !!bug gm this should be enough
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DYN/dynldf_iso.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DYN/dynldf_iso.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DYN/dynldf_iso.F90	(revision 7567)
@@ -30,4 +30,7 @@
    USE wrk_nemo        ! Memory Allocation
    USE timing          ! Timing
+#if defined key_bdy  
+   USE bdy_oce         ! needed for extra diffusion in rim  
+#endif
 
    IMPLICIT NONE
@@ -115,4 +118,6 @@
       REAL(wp) ::   zuav, zvav, zuwslpi, zuwslpj, zvwslpi, zvwslpj   !   -      -
       !
+      REAL(wp), DIMENSION(jpi,jpj) :: zfactor  ! multiplier for diffusion
+      !
       REAL(wp), POINTER, DIMENSION(:,:) :: ziut, zjuf, zjvt, zivf, zdku, zdk1u, zdkv, zdk1v
       !!----------------------------------------------------------------------
@@ -126,4 +131,7 @@
          IF(lwp) WRITE(numout,*) 'dyn_ldf_iso : iso-neutral laplacian diffusive operator or '
          IF(lwp) WRITE(numout,*) '~~~~~~~~~~~   s-coordinate horizontal diffusive operator'
+#if defined key_bdy 
+         IF(lwp) WRITE(numout,*) '~~~~~ using sponge_factor'
+#endif
          !                                      ! allocate dyn_ldf_bilap arrays
          IF( dyn_ldf_iso_alloc() /= 0 )   CALL ctl_stop('STOP', 'dyn_ldf_iso: failed to allocate arrays')
@@ -155,4 +163,9 @@
       ENDIF
 
+#if defined key_bdy 
+      zfactor(:,:) = sponge_factor(:,:) 
+#else 
+      zfactor(:,:) = 1.0 
+#endif 
       !                                                ! ===============
       DO jk = 1, jpkm1                                 ! Horizontal slab
@@ -199,10 +212,10 @@
             DO jj = 2, jpjm1
                DO ji = fs_2, jpi   ! vector opt.
-                  zabe1 = (fsahmt(ji,jj,jk)+ahmb0) * e2t(ji,jj) * fse3t(ji,jj,jk) / e1t(ji,jj)
+                  zabe1 = zfactor(ji,jj) * (fsahmt(ji,jj,jk)+ahmb0) * e2t(ji,jj) * fse3t(ji,jj,jk) / e1t(ji,jj)
 
                   zmskt = 1./MAX(  umask(ji-1,jj,jk  )+umask(ji,jj,jk+1)   &
                      &           + umask(ji-1,jj,jk+1)+umask(ji,jj,jk  ), 1. )
 
-                  zcof1 = - aht0 * e2t(ji,jj) * zmskt * 0.5  * ( uslp(ji-1,jj,jk) + uslp(ji,jj,jk) )
+                  zcof1 = - zfactor(ji,jj) * aht0 * e2t(ji,jj) * zmskt * 0.5  * ( uslp(ji-1,jj,jk) + uslp(ji,jj,jk) )
 
                   ziut(ji,jj) = (  zabe1 * ( ub(ji,jj,jk) - ub(ji-1,jj,jk) )   &
@@ -216,10 +229,10 @@
          DO jj = 1, jpjm1
             DO ji = 1, fs_jpim1   ! vector opt.
-               zabe2 = ( fsahmf(ji,jj,jk) + ahmb0 ) * e1f(ji,jj) * fse3f(ji,jj,jk) / e2f(ji,jj)
+               zabe2 = zfactor(ji,jj) * ( fsahmf(ji,jj,jk) + ahmb0 ) * e1f(ji,jj) * fse3f(ji,jj,jk) / e2f(ji,jj)
 
                zmskf = 1./MAX(  umask(ji,jj+1,jk  )+umask(ji,jj,jk+1)   &
                   &           + umask(ji,jj+1,jk+1)+umask(ji,jj,jk  ), 1. )
 
-               zcof2 = - aht0 * e1f(ji,jj) * zmskf * 0.5  * ( vslp(ji+1,jj,jk) + vslp(ji,jj,jk) )
+               zcof2 = - zfactor(ji,jj) * aht0 * e1f(ji,jj) * zmskf * 0.5  * ( vslp(ji+1,jj,jk) + vslp(ji,jj,jk) )
 
                zjuf(ji,jj) = (  zabe2 * ( ub(ji,jj+1,jk) - ub(ji,jj,jk) )   &
@@ -237,10 +250,10 @@
          DO jj = 2, jpjm1
             DO ji = 1, fs_jpim1   ! vector opt.
-               zabe1 = ( fsahmf(ji,jj,jk) + ahmb0 ) * e2f(ji,jj) * fse3f(ji,jj,jk) / e1f(ji,jj)
+               zabe1 = zfactor(ji,jj) * ( fsahmf(ji,jj,jk) + ahmb0 ) * e2f(ji,jj) * fse3f(ji,jj,jk) / e1f(ji,jj)
 
                zmskf = 1./MAX(  vmask(ji+1,jj,jk  )+vmask(ji,jj,jk+1)   &
                   &           + vmask(ji+1,jj,jk+1)+vmask(ji,jj,jk  ), 1. )
 
-               zcof1 = - aht0 * e2f(ji,jj) * zmskf * 0.5 * ( uslp(ji,jj+1,jk) + uslp(ji,jj,jk) )
+               zcof1 = - zfactor(ji,jj) * aht0 * e2f(ji,jj) * zmskf * 0.5 * ( uslp(ji,jj+1,jk) + uslp(ji,jj,jk) )
 
                zivf(ji,jj) = (  zabe1 * ( vb(ji+1,jj,jk) - vb(ji,jj,jk) )   &
@@ -269,10 +282,10 @@
             DO jj = 2, jpj
                DO ji = 1, fs_jpim1   ! vector opt.
-                  zabe2 = (fsahmt(ji,jj,jk)+ahmb0) * e1t(ji,jj) * fse3t(ji,jj,jk) / e2t(ji,jj)
+                  zabe2 = zfactor(ji,jj) * (fsahmt(ji,jj,jk)+ahmb0) * e1t(ji,jj) * fse3t(ji,jj,jk) / e2t(ji,jj)
 
                   zmskt = 1./MAX(  vmask(ji,jj-1,jk  )+vmask(ji,jj,jk+1)   &
                      &           + vmask(ji,jj-1,jk+1)+vmask(ji,jj,jk  ), 1. )
 
-                  zcof2 = - aht0 * e1t(ji,jj) * zmskt * 0.5 * ( vslp(ji,jj-1,jk) + vslp(ji,jj,jk) )
+                  zcof2 = - zfactor(ji,jj) * aht0 * e1t(ji,jj) * zmskt * 0.5 * ( vslp(ji,jj-1,jk) + vslp(ji,jj,jk) )
 
                   zjvt(ji,jj) = (  zabe2 * ( vb(ji,jj,jk) - vb(ji,jj-1,jk) )   &
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90	(revision 7567)
@@ -41,4 +41,5 @@
    USE timing          ! Timing    
    USE sbcapr          ! surface boundary condition: atmospheric pressure
+   USE diatmb          ! Top,middle,bottom output
    USE dynadv, ONLY: ln_dynadv_vec
 #if defined key_agrif
@@ -144,4 +145,5 @@
       INTEGER  ::   ji, jj, jk, jn   		! dummy loop indices
       INTEGER  ::   ikbu, ikbv, noffset    	! local integers
+      REAL(wp) ::   zmdi
       REAL(wp) ::   zraur, z1_2dt_b, z2dt_bf  	! local scalars
       REAL(wp) ::   zx1, zy1, zx2, zy2         !   -      -
@@ -169,4 +171,5 @@
       CALL wrk_alloc( jpi, jpj, zhf )
       !
+      zmdi=1.e+20                               !  missing data indicator for masking
       !                                         !* Local constant initialization
       z1_12 = 1._wp / 12._wp 
@@ -926,4 +929,8 @@
       CALL wrk_dealloc( jpi, jpj, zhf )
       !
+      IF ( ln_diatmb ) THEN
+         CALL iom_put( "baro_u" , un_b*umask(:,:,1)+zmdi*(1-umask(:,:,1 ) ) )  ! Barotropic  U Velocity
+         CALL iom_put( "baro_v" , vn_b*vmask(:,:,1)+zmdi*(1-vmask(:,:,1 ) ) )  ! Barotropic  V Velocity
+      ENDIF
       IF( nn_timing == 1 )  CALL timing_stop('dyn_spg_ts')
       !
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/ICB/icbrst.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/ICB/icbrst.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/ICB/icbrst.F90	(revision 7567)
@@ -18,6 +18,8 @@
    !!----------------------------------------------------------------------
    USE par_oce        ! NEMO parameters
+   USE phycst         ! for rday
    USE dom_oce        ! NEMO domain
    USE in_out_manager ! NEMO IO routines
+   USE ioipsl, ONLY : ju2ymds    ! for calendar
    USE lib_mpp        ! NEMO MPI library, lk_mpp in particular
    USE netcdf         ! netcdf routines for IO
@@ -231,6 +233,10 @@
       INTEGER ::   jn   ! dummy loop index
       INTEGER ::   ix_dim, iy_dim, ik_dim, in_dim
-      CHARACTER(len=256)     :: cl_path
-      CHARACTER(len=256)     :: cl_filename
+      INTEGER             ::   iyear, imonth, iday
+      REAL (wp)           ::   zsec
+      REAL (wp)           ::   zfjulday
+      CHARACTER(len=256)  :: cl_path
+      CHARACTER(len=256)  :: cl_filename
+      CHARACTER(LEN=20)   ::   clkt     ! ocean time-step deine as a character
       TYPE(iceberg), POINTER :: this
       TYPE(point)  , POINTER :: pt
@@ -240,8 +246,18 @@
       cl_path = TRIM(cn_ocerst_outdir)
       IF( cl_path(LEN_TRIM(cl_path):) /= '/' ) cl_path = TRIM(cl_path) // '/'
+      IF ( ln_rstdate ) THEN
+         zfjulday = fjulday + rdttra(1) / rday
+         IF( ABS(zfjulday - REAL(NINT(zfjulday),wp)) < 0.1 / rday )   zfjulday = REAL(NINT(zfjulday),wp)   ! avoid truncation error
+         CALL ju2ymds( zfjulday, iyear, imonth, iday, zsec )           
+         WRITE(clkt, '(i4.4,2i2.2)') iyear, imonth, iday
+      ELSE
+         IF( kt > 999999999 ) THEN   ;   WRITE(clkt, *       ) kt
+         ELSE                        ;   WRITE(clkt, '(i8.8)') kt
+         ENDIF
+      ENDIF
       IF( lk_mpp ) THEN
-         WRITE(cl_filename,'(A,"_icebergs_",I8.8,"_restart_",I4.4,".nc")') TRIM(cexper), kt, narea-1
+         WRITE(cl_filename,'(A,"_icebergs_",A,"_restart_",I4.4,".nc")') TRIM(cexper), TRIM(ADJUSTL(clkt)), narea-1
       ELSE
-         WRITE(cl_filename,'(A,"_icebergs_",I8.8,"_restart.nc")') TRIM(cexper), kt
+         WRITE(cl_filename,'(A,"_icebergs_",A,"_restart.nc")') TRIM(cexper), TRIM(ADJUSTL(clkt))
       ENDIF
       IF (nn_verbose_level >= 0) WRITE(numout,'(2a)') 'icebergs, write_restart: creating ',TRIM(cl_path)//TRIM(cl_filename)
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90	(revision 7567)
@@ -30,4 +30,5 @@
    CHARACTER(lc) ::   cn_ocerst_outdir !: restart output directory
    LOGICAL       ::   ln_rstart        !: start from (F) rest or (T) a restart file
+   LOGICAL       ::   ln_rstdate       !: datestamping of restarts 
    LOGICAL       ::   ln_rst_list      !: output restarts at list of times (T) or by frequency (F)
    INTEGER       ::   nn_no            !: job number
@@ -41,5 +42,5 @@
    INTEGER       ::   nn_write         !: model standard output frequency
    INTEGER       ::   nn_stock         !: restart file frequency
-   INTEGER, DIMENSION(10) :: nn_stocklist  !: restart dump times
+   INTEGER, DIMENSION(100) :: nn_stocklist  !: restart dump times
    LOGICAL       ::   ln_dimgnnn       !: type of dimgout. (F): 1 file for all proc
                                                        !:                  (T): 1 file per proc
@@ -47,4 +48,7 @@
    LOGICAL       ::   ln_cfmeta        !: output additional data to netCDF files required for compliance with the CF metadata standard
    LOGICAL       ::   ln_clobber       !: clobber (overwrite) an existing file
+   !JT
+   LOGICAL       ::   ln_NOOS          !: NOOS transects  diagnostics
+   !JT
    INTEGER       ::   nn_chunksz       !: chunksize (bytes) for NetCDF file (works only with iom_nf90 routines)
 #if defined key_netcdf4
@@ -83,5 +87,5 @@
    INTEGER       ::   nwrite                      !: model standard output frequency
    INTEGER       ::   nstock                      !: restart file frequency
-   INTEGER, DIMENSION(10) :: nstocklist           !: restart dump times
+   INTEGER, DIMENSION(100) :: nstocklist           !: restart dump times
 
    !!----------------------------------------------------------------------
@@ -131,6 +135,13 @@
    INTEGER ::   numdct_in       =   -1      !: logical unit for transports computing
    INTEGER ::   numdct_vol      =   -1      !: logical unit for voulume transports output
-   INTEGER ::   numdct_heat     =   -1      !: logical unit for heat    transports output
-   INTEGER ::   numdct_salt     =   -1      !: logical unit for salt    transports output
+   !JT INTEGER ::   numdct_heat     =   -1      !: logical unit for heat    transports output
+   !JT INTEGER ::   numdct_salt     =   -1      !: logical unit for salt    transports output
+   !JT
+   INTEGER ::   numdct_temp =   -1      !: logical unit for heat    transports output
+   INTEGER ::   numdct_sal  =   -1      !: logical unit for salt    transports output
+
+   INTEGER ::   numdct_NOOS     =   -1      !: logical unit for NOOS    transports output
+   INTEGER ::   numdct_NOOS_h   =   -1      !: logical unit for NOOS hourly transports output
+   !JT
    INTEGER ::   numfl           =   -1      !: logical unit for floats ascii output
    INTEGER ::   numflo          =   -1      !: logical unit for floats ascii output
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/IOM/iom.F90	(revision 7567)
@@ -44,5 +44,5 @@
    USE ioipsl, ONLY :  ju2ymds    ! for calendar
    USE crs             ! Grid coarsening
-
+    
    IMPLICIT NONE
    PUBLIC   !   must be public to be able to access iom_def through iom
@@ -55,5 +55,12 @@
    PUBLIC iom_init, iom_swap, iom_open, iom_close, iom_setkt, iom_varid, iom_get, iom_gettime, iom_rstput, iom_put
    PUBLIC iom_getatt, iom_use, iom_context_finalize
-
+   
+   
+   !JT REGION MEANS
+   !INTEGER , PUBLIC ::   n_regions_output = 100
+   
+   INTEGER , PUBLIC ::   n_regions_output
+   !JT REGION MEANS
+   
    PRIVATE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
    PRIVATE iom_g0d, iom_g1d, iom_g2d, iom_g3d, iom_get_123d
@@ -106,4 +113,83 @@
       REAL(wp), ALLOCATABLE, DIMENSION(:,:) :: z_bnds
       !!----------------------------------------------------------------------
+      
+      
+      
+      !JT REGION MEANS
+      !! read namelist to see if the region mask code is called, if so read the region mask, and count the regions. 
+      
+      
+      REAL(wp),  ALLOCATABLE,   DIMENSION(:,:) ::   tmpregion !: temporary region_mask
+      INTEGER, DIMENSION(3) ::   zdimsz   ! number of elements in each of the 3 dimensions (i.e., lon, lat, no of masks, 297,  375,  4) for an array
+      INTEGER               ::   zndims   ! number of dimensions in an array (i.e. 3, )
+      INTEGER :: inum, nmasks,ierr,maskno,idmaskvar,tmpint
+      REAL(wp), ALLOCATABLE,   DIMENSION(:,:,:)  ::   tmp_region_mask_real   ! tempory region_mask of reals
+      
+      LOGICAL ::   ln_diaregmean  ! region mean calculation
+   
+    
+      INTEGER ::   ios                  ! Local integer output status for namelist read
+      LOGICAL :: ln_diaregmean_ascii  ! region mean calculation ascii output
+      LOGICAL :: ln_diaregmean_bin  ! region mean calculation binary output
+      LOGICAL :: ln_diaregmean_nc  ! region mean calculation netcdf output
+    
+      
+      
+      NAMELIST/nam_diaregmean/ ln_diaregmean,ln_diaregmean_ascii,ln_diaregmean_bin,ln_diaregmean_nc
+      
+      ! read in Namelist. 
+      !!----------------------------------------------------------------------
+      !
+      REWIND ( numnam_ref )              ! Read Namelist nam_diatmb in referdiatmbence namelist : TMB diagnostics
+      READ   ( numnam_ref, nam_diaregmean, IOSTAT=ios, ERR= 901 )
+901   IF( ios /= 0 ) CALL ctl_nam ( ios , 'nam_diaregmean in reference namelist', lwp )
+
+      REWIND( numnam_cfg )              ! Namelist nam_diatmb in configuration namelist  TMB diagnostics
+      READ  ( numnam_cfg, nam_diaregmean, IOSTAT = ios, ERR = 902 )
+902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'nam_diaregmean in configuration namelist', lwp )
+      IF(lwm) WRITE ( numond, nam_diaregmean )
+
+      IF (ln_diaregmean) THEN
+      
+        ! Open region mask for region means, and retrieve the size of the mask (number of levels)          
+          CALL iom_open ( 'region_mask.nc', inum )
+          idmaskvar = iom_varid( inum, 'mask', kdimsz=zdimsz, kndims=zndims, ldstop = .FALSE.)          
+          nmasks = zdimsz(3)
+          
+          ! read in the region mask (which contains floating point numbers) into a temporary array of reals.
+          ALLOCATE( tmp_region_mask_real(jpi,jpj,nmasks),  STAT= ierr )
+          IF( ierr /= 0 )   CALL ctl_stop( 'dia_regmean_init: failed to allocate tmp_region_mask_real array' )
+          
+          ! Use jpdom_unknown to read in a n layer mask.
+          tmp_region_mask_real(:,:,:) = 0
+          CALL iom_get( inum, jpdom_unknown, 'mask', tmp_region_mask_real(1:nlci,1:nlcj,1:nmasks),   &
+              &          kstart = (/ mig(1),mjg(1),1 /), kcount = (/ nlci,nlcj,nmasks /) )
+          
+          CALL iom_close( inum )
+          !Convert the region mask of reals into one of integers. 
+          
+          
+          n_regions_output = 0
+          DO maskno = 1,nmasks
+              tmpint = maxval(int(tmp_region_mask_real(:,:,maskno)))
+              CALL mpp_max( tmpint )
+              n_regions_output = n_regions_output + (tmpint + 1)
+          END DO
+      
+          
+        
+        WRITE(numout,*)  'IOM: n_regions_output: ',n_regions_output
+        
+      ELSE
+        n_regions_output = 1
+      ENDIF
+      
+      
+      
+      !JT REGION MEANS
+      
+      
+      
+      
 #if ! defined key_xios2
       ALLOCATE( z_bnds(jpk,2) )
@@ -227,4 +313,12 @@
       CALL iom_set_axis_attr( "iax_20C", (/ REAL(20,wp) /) )
       CALL iom_set_axis_attr( "iax_28C", (/ REAL(28,wp) /) )
+      
+      
+      
+      ! JT Region means. 
+      CALL iom_set_axis_attr( "region", (/ (REAL(ji,wp), ji=1,n_regions_output) /) )
+
+      !CALL iom_set_axis_attr( "region", (/ (REAL(ji,wp), ji=1,100) /) )
+
       
       ! automatic definitions of some of the xml attributs
@@ -1246,5 +1340,18 @@
       REAL(wp), DIMENSION(:)  , OPTIONAL, INTENT(in) ::   paxis
       REAL(wp), DIMENSION(:,:), OPTIONAL, INTENT(in) ::   bounds
+      
+      !INTEGER :: iind_JT
+      
+      
+      !write(numout,*) 'IOM/iom.F90:iom_set_axis_attr: ',cdid
+      
       IF ( PRESENT(paxis) ) THEN
+      
+        !write(numout,*) 'IOM/iom.F90:iom_set_axis_attr paxis size for: ',cdid,SIZE(paxis)
+        !write(numout,*) 'IOM/iom.F90:iom_set_axis_attr paxis values for: ',cdid,paxis
+        !do iind_JT = 1,SIZE(paxis)        
+        !  write(numout,*) 'IOM/iom.F90:iom_set_axis_attr paxis individual values for: ',cdid,iind_JT,paxis(iind_JT)
+        !end do
+        
 #if ! defined key_xios2
          IF ( xios_is_valid_axis     (cdid) )   CALL xios_set_axis_attr     ( cdid, size=SIZE(paxis), value=paxis )
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/IOM/restart.F90	(revision 7567)
@@ -21,4 +21,5 @@
    USE in_out_manager  ! I/O manager
    USE iom             ! I/O module
+   USE ioipsl, ONLY : ju2ymds    ! for calendar
    USE eosbn2          ! equation of state            (eos bn2 routine)
    USE trdmxl_oce      ! ocean active mixed layer tracers trends variables
@@ -54,8 +55,11 @@
       !!----------------------------------------------------------------------
       INTEGER, INTENT(in) ::   kt     ! ocean time-step
+      INTEGER             ::   iyear, imonth, iday
+      REAL (wp)           ::   zsec
+      REAL (wp)           ::   zfjulday
       !!
       CHARACTER(LEN=20)   ::   clkt     ! ocean time-step deine as a character
       CHARACTER(LEN=50)   ::   clname   ! ocean output restart file name
-      CHARACTER(lc)       ::   clpath   ! full path to ocean output restart file
+      CHARACTER(LEN=150)  ::   clpath   ! full path to ocean output restart file
       !!----------------------------------------------------------------------
       !
@@ -81,7 +85,18 @@
       IF( kt == nitrst - 1 .OR. nstock == 1 .OR. ( kt == nitend .AND. .NOT. lrst_oce ) ) THEN
          IF( nitrst <= nitend .AND. nitrst > 0 ) THEN 
-            ! beware of the format used to write kt (default is i8.8, that should be large enough...)
-            IF( nitrst > 999999999 ) THEN   ;   WRITE(clkt, *       ) nitrst
-            ELSE                            ;   WRITE(clkt, '(i8.8)') nitrst
+            IF ( ln_rstdate ) THEN
+               zfjulday = fjulday + rdttra(1) / rday
+               IF( ABS(zfjulday - REAL(NINT(zfjulday),wp)) < 0.1 / rday )   zfjulday = REAL(NINT(zfjulday),wp)   ! avoid truncation error
+               CALL ju2ymds( zfjulday, iyear, imonth, iday, zsec )           
+               WRITE(clkt, '(i4.4,2i2.2)') iyear, imonth, iday            
+               IF ( ln_rst_list .AND. ( kt .NE. nitend) ) THEN
+                 ! JT IF ( nstock_list_in_use_JT .AND. ( kt .NE. nitend - 1) ) THEN
+                 WRITE(clkt, '(i4.4,2i2.2,a1,i10.10)') iyear, imonth, iday,'_',kt !JT
+               ENDIF
+            ELSE
+               ! beware of the format used to write kt (default is i8.8, that should be large enough...)
+               IF( nitrst > 999999999 ) THEN   ;   WRITE(clkt, *       ) nitrst
+               ELSE                            ;   WRITE(clkt, '(i8.8)') nitrst
+               ENDIF
             ENDIF
             ! create the file
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/SBC/sbc_oce.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/SBC/sbc_oce.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/SBC/sbc_oce.F90	(revision 7567)
@@ -121,4 +121,7 @@
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   sprecip           !: solid precipitation                          [Kg/m2/s]
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fr_i              !: ice fraction = 1 - lead fraction      (between 0 to 1)
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   pressnow          !: UKMO SHELF pressure
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   apgu              !: UKMO SHELF pressure forcing
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   apgv              !: UKMO SHELF pressure forcing
 #if defined key_cpl_carbon_cycle
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   atm_co2           !: atmospheric pCO2                             [ppm]
@@ -171,4 +174,5 @@
 #endif
          &      ssu_m  (jpi,jpj) , sst_m(jpi,jpj) , frq_m(jpi,jpj) ,      &
+         &      pressnow(jpi,jpj), apgu(jpi,jpj)    , apgv(jpi,jpj) ,     &
          &      ssv_m  (jpi,jpj) , sss_m(jpi,jpj) , ssh_m(jpi,jpj) , STAT=ierr(4) )
          !
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/SBC/sbcflx.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/SBC/sbcflx.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/SBC/sbcflx.F90	(revision 7567)
@@ -28,5 +28,5 @@
    PUBLIC sbc_flx       ! routine called by step.F90
 
-   INTEGER , PARAMETER ::   jpfld   = 5   ! maximum number of files to read 
+   INTEGER , PARAMETER ::   jpfld   = 6   ! maximum number of files to read 
    INTEGER , PARAMETER ::   jp_utau = 1   ! index of wind stress (i-component) file
    INTEGER , PARAMETER ::   jp_vtau = 2   ! index of wind stress (j-component) file
@@ -34,5 +34,8 @@
    INTEGER , PARAMETER ::   jp_qsr  = 4   ! index of solar heat file
    INTEGER , PARAMETER ::   jp_emp  = 5   ! index of evaporation-precipation file
+   INTEGER , PARAMETER ::   jp_press = 6  ! index of pressure for UKMO shelf fluxes
    TYPE(FLD), ALLOCATABLE, DIMENSION(:) ::   sf    ! structure of input fields (file informations, fields read)
+   LOGICAL , PUBLIC    ::   ln_shelf_flx = .FALSE. ! UKMO SHELF specific flux flag
+   INTEGER             ::   jpfld_local   ! maximum number of files to read (locally modified depending on ln_shelf_flx) 
 
    !! * Substitutions
@@ -82,9 +85,16 @@
       REAL(wp) ::   zcdrag = 1.5e-3       ! drag coefficient
       REAL(wp) ::   ztx, zty, zmod, zcoef ! temporary variables
+      REAL     ::   cs                    ! UKMO SHELF: Friction co-efficient at surface
+      REAL     ::   totwindspd            ! UKMO SHELF: Magnitude of wind speed vector
+
+      REAL(wp) ::   rhoa  = 1.22         ! Air density kg/m3
+      REAL(wp) ::   cdrag = 1.5e-3       ! drag coefficient 
       !!
       CHARACTER(len=100) ::  cn_dir                               ! Root directory for location of flx files
       TYPE(FLD_N), DIMENSION(jpfld) ::   slf_i                    ! array of namelist information structures
-      TYPE(FLD_N) ::   sn_utau, sn_vtau, sn_qtot, sn_qsr, sn_emp  ! informations about the fields to be read
-      NAMELIST/namsbc_flx/ cn_dir, sn_utau, sn_vtau, sn_qtot, sn_qsr, sn_emp
+      TYPE(FLD_N) ::   sn_utau, sn_vtau, sn_qtot, sn_qsr, sn_emp, sn_press  !  informations about the fields to be read
+      LOGICAL     ::   ln_foam_flx  = .FALSE.                     ! UKMO FOAM specific flux flag
+      NAMELIST/namsbc_flx/ cn_dir, sn_utau, sn_vtau, sn_qtot, sn_qsr, sn_emp,   &
+      &                    ln_foam_flx, sn_press, ln_shelf_flx
       !!---------------------------------------------------------------------
       !
@@ -109,9 +119,18 @@
          slf_i(jp_emp ) = sn_emp
          !
-         ALLOCATE( sf(jpfld), STAT=ierror )        ! set sf structure
+            ALLOCATE( sf(jpfld), STAT=ierror )        ! set sf structure
+            IF( ln_shelf_flx ) slf_i(jp_press) = sn_press
+   
+            ! define local jpfld depending on shelf_flx logical
+            IF( ln_shelf_flx ) THEN
+               jpfld_local = jpfld
+            ELSE
+               jpfld_local = jpfld-1
+            ENDIF
+            !
          IF( ierror > 0 ) THEN   
             CALL ctl_stop( 'sbc_flx: unable to allocate sf structure' )   ;   RETURN  
          ENDIF
-         DO ji= 1, jpfld
+         DO ji= 1, jpfld_local
             ALLOCATE( sf(ji)%fnow(jpi,jpj,1) )
             IF( slf_i(ji)%ln_tint ) ALLOCATE( sf(ji)%fdta(jpi,jpj,1,2) )
@@ -132,10 +151,39 @@
          ENDIF
 !CDIR COLLAPSE
+            !!UKMO SHELF effect of atmospheric pressure on SSH
+            ! If using ln_apr_dyn, this is done there so don't repeat here.
+            IF( ln_shelf_flx .AND. .NOT. ln_apr_dyn) THEN
+               DO jj = 1, jpjm1
+                  DO ji = 1, jpim1
+                     apgu(ji,jj) = (-1.0/rau0)*(sf(jp_press)%fnow(ji+1,jj,1)-sf(jp_press)%fnow(ji,jj,1))/e1u(ji,jj)
+                     apgv(ji,jj) = (-1.0/rau0)*(sf(jp_press)%fnow(ji,jj+1,1)-sf(jp_press)%fnow(ji,jj,1))/e2v(ji,jj)
+                  END DO
+               END DO
+            ENDIF ! ln_shelf_flx
+      
          DO jj = 1, jpj                                           ! set the ocean fluxes from read fields
             DO ji = 1, jpi
-               utau(ji,jj) = sf(jp_utau)%fnow(ji,jj,1)
-               vtau(ji,jj) = sf(jp_vtau)%fnow(ji,jj,1)
-               qns (ji,jj) = sf(jp_qtot)%fnow(ji,jj,1) - sf(jp_qsr)%fnow(ji,jj,1)
-               emp (ji,jj) = sf(jp_emp )%fnow(ji,jj,1)
+                   IF( ln_shelf_flx ) THEN
+                      !! UKMO SHELF - need atmospheric pressure to calculate Haney forcing
+                      pressnow(ji,jj) = sf(jp_press)%fnow(ji,jj,1)
+                      !! UKMO SHELF flux files contain wind speed not wind stress
+                      totwindspd = sqrt((sf(jp_utau)%fnow(ji,jj,1))**2.0 + (sf(jp_vtau)%fnow(ji,jj,1))**2.0)
+                      cs = 0.63 + (0.066 * totwindspd)
+                      utau(ji,jj) = cs * (rhoa/rau0) * sf(jp_utau)%fnow(ji,jj,1) * totwindspd
+                      vtau(ji,jj) = cs * (rhoa/rau0) * sf(jp_vtau)%fnow(ji,jj,1) * totwindspd
+                   ELSE
+                      utau(ji,jj) = sf(jp_utau)%fnow(ji,jj,1)
+                      vtau(ji,jj) = sf(jp_vtau)%fnow(ji,jj,1)
+                   ENDIF
+                   qsr (ji,jj) = sf(jp_qsr )%fnow(ji,jj,1)
+                   IF( ln_foam_flx .OR. ln_shelf_flx ) THEN
+                      !! UKMO FOAM flux files contain non-solar heat flux (qns) rather than total heat flux (qtot)
+                      qns (ji,jj) = sf(jp_qtot)%fnow(ji,jj,1)
+                      !! UKMO FOAM flux files contain the net DOWNWARD freshwater flux P-E rather then E-P
+                      emp (ji,jj) = -1. * sf(jp_emp )%fnow(ji,jj,1)
+                   ELSE
+                      qns (ji,jj) = sf(jp_qtot)%fnow(ji,jj,1) - sf(jp_qsr)%fnow(ji,jj,1)
+                      emp (ji,jj) = sf(jp_emp )%fnow(ji,jj,1)
+                   ENDIF
             END DO
          END DO
@@ -143,4 +191,11 @@
          qns(:,:) = qns(:,:) - emp(:,:) * sst_m(:,:) * rcp        ! mass flux is at SST
          !
+   
+            !! UKMO FOAM wind fluxes need lbc_lnk calls owing to a bug in interp.exe
+            IF( ln_foam_flx ) THEN
+               CALL lbc_lnk( utau(:,:), 'U', -1. )
+               CALL lbc_lnk( vtau(:,:), 'V', -1. )
+            ENDIF
+    
          !                                                        ! module of wind stress and wind speed at T-point
          zcoef = 1. / ( zrhoa * zcdrag )
@@ -162,5 +217,5 @@
             WRITE(numout,*) 
             WRITE(numout,*) '        read daily momentum, heat and freshwater fluxes OK'
-            DO jf = 1, jpfld
+            DO jf = 1, jpfld_local
                IF( jf == jp_utau .OR. jf == jp_vtau )   zfact =     1.
                IF( jf == jp_qtot .OR. jf == jp_qsr  )   zfact =     0.1
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/SBC/sbcmod.F90	(revision 7567)
@@ -326,4 +326,9 @@
       !!----------------------------------------------------------------------
       INTEGER, INTENT(in) ::   kt       ! ocean time step
+      REAL(wp)                         ::   zmdi  ! temporary reals
+      
+      
+      zmdi=1.e+20 !missing data indicator for masking
+      
       !!---------------------------------------------------------------------
       !
@@ -443,11 +448,16 @@
             &                    'at it= ', kt,' date= ', ndastp
          IF(lwp) WRITE(numout,*) '~~~~'
-         CALL iom_rstput( kt, nitrst, numrow, 'utau_b' , utau )
-         CALL iom_rstput( kt, nitrst, numrow, 'vtau_b' , vtau )
-         CALL iom_rstput( kt, nitrst, numrow, 'qns_b'  , qns  )
+         !CALL iom_rstput( kt, nitrst, numrow, 'utau_b' , utau*tmask(:,:,1)+zmdi*(1-tmask(:,:,1 ) ) ) ! (land masked) # JT
+         !CALL iom_rstput( kt, nitrst, numrow, 'vtau_b' , vtau*tmask(:,:,1)+zmdi*(1-tmask(:,:,1 ) ) ) ! (land masked) # JT
+         !CALL iom_rstput( kt, nitrst, numrow, 'qns_b'  ,  qns*tmask(:,:,1)+zmdi*(1-tmask(:,:,1 ) )  ) ! (land masked) # JT
+         CALL iom_rstput( kt, nitrst, numrow, 'utau_b' , utau*tmask(:,:,1) ) ! (land masked) # JT
+         CALL iom_rstput( kt, nitrst, numrow, 'vtau_b' , vtau*tmask(:,:,1) ) ! (land masked) # JT
+         CALL iom_rstput( kt, nitrst, numrow, 'qns_b'  ,  qns*tmask(:,:,1) ) ! (land masked) # JT
          ! The 3D heat content due to qsr forcing is treated in traqsr
          ! CALL iom_rstput( kt, nitrst, numrow, 'qsr_b'  , qsr  )
-         CALL iom_rstput( kt, nitrst, numrow, 'emp_b'  , emp  )
-         CALL iom_rstput( kt, nitrst, numrow, 'sfx_b'  , sfx  )
+         !CALL iom_rstput( kt, nitrst, numrow, 'emp_b'  , emp*tmask(:,:,1)+zmdi*(1-tmask(:,:,1 ) )  ) ! (land masked) # JT
+         !CALL iom_rstput( kt, nitrst, numrow, 'sfx_b'  , sfx*tmask(:,:,1)+zmdi*(1-tmask(:,:,1 ) )  ) ! (land masked) # JT
+         CALL iom_rstput( kt, nitrst, numrow, 'emp_b'  , emp*tmask(:,:,1)  ) ! (land masked) # JT
+         CALL iom_rstput( kt, nitrst, numrow, 'sfx_b'  , sfx*tmask(:,:,1)  ) ! (land masked) # JT
       ENDIF
 
@@ -456,19 +466,22 @@
       !                                                ! ---------------------------------------- !
       IF( MOD( kt-1, nn_fsbc ) == 0 ) THEN
-         CALL iom_put( "empmr" , emp  - rnf )                   ! upward water flux
+         CALL iom_put( "empmr" , (emp  - rnf)*tmask(:,:,1)+zmdi*(1-tmask(:,:,1 ) ) )    ! upward water flux (land masked) # JT
+         !CALL iom_put( "empmr" , emp  - rnf )                   ! upward water flux
          CALL iom_put( "saltflx", sfx  )                        ! downward salt flux  
                                                                 ! (includes virtual salt flux beneath ice 
                                                                 ! in linear free surface case)
          CALL iom_put( "fmmflx", fmmflx  )                      ! Freezing-melting water flux
-         CALL iom_put( "qt"    , qns  + qsr )                   ! total heat flux 
-         CALL iom_put( "qns"   , qns        )                   ! solar heat flux
-         CALL iom_put( "qsr"   ,       qsr  )                   ! solar heat flux
+         !CALL iom_put( "qt"    , qns  + qsr )                   ! total heat flux 
+         CALL iom_put( "qt"    , (qns  + qsr)*tmask(:,:,1)+zmdi*(1-tmask(:,:,1 ) ) )    ! total heat flux (land masked) # JT
+         CALL iom_put( "qns"   , qns*tmask(:,:,1)+zmdi*(1-tmask(:,:,1 ) )        )                   ! solar heat flux (land masked) # JT
+         CALL iom_put( "qsr"   ,       qsr*tmask(:,:,1)+zmdi*(1-tmask(:,:,1 ) )  )                   ! solar heat flux (land masked) # JT
          IF( nn_ice > 0 .OR. nn_components == jp_iam_opa )   CALL iom_put( "ice_cover", fr_i )   ! ice fraction 
-         CALL iom_put( "taum"  , taum       )                   ! wind stress module 
-         CALL iom_put( "wspd"  , wndm       )                   ! wind speed  module over free ocean or leads in presence of sea-ice
-      ENDIF
-      !
-      CALL iom_put( "utau", utau )   ! i-wind stress   (stress can be updated at 
-      CALL iom_put( "vtau", vtau )   ! j-wind stress    each time step in sea-ice)
+         !CALL iom_put( "taum"  , taum       )                   ! wind stress module  
+         CALL iom_put( "taum", taum*tmask(:,:,1)+zmdi*(1-tmask(:,:,1 ) ) )   ! wind stress module (land masked) # JT
+         CALL iom_put( "wspd"  , wndm*tmask(:,:,1)+zmdi*(1-tmask(:,:,1 ) )       )                   ! wind speed  module over free ocean or leads in presence of sea-ice (land masked) # JT
+      ENDIF
+      !
+      CALL iom_put( "utau", utau*tmask(:,:,1)+zmdi*(1-tmask(:,:,1 ) ) )   ! i-wind stress   (stress can be updated at  (land masked) # JT
+      CALL iom_put( "vtau", vtau*tmask(:,:,1)+zmdi*(1-tmask(:,:,1 ) ) )   ! j-wind stress    each time step in sea-ice) (land masked) # JT
       !
       IF(ln_ctl) THEN         ! print mean trends (used for debugging)
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssr.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssr.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/SBC/sbcssr.F90	(revision 7567)
@@ -42,4 +42,5 @@
    LOGICAL         ::   ln_sssr_bnd     ! flag to bound erp term 
    REAL(wp)        ::   rn_sssr_bnd     ! ABS(Max./Min.) value of erp term [mm/day]
+   LOGICAL         ::   ln_UKMO_haney   ! UKMO specific flag to calculate Haney forcing  
 
    REAL(wp) , ALLOCATABLE, DIMENSION(:) ::   buffer   ! Temporary buffer for exchange
@@ -79,4 +80,12 @@
       INTEGER  ::   ierror   ! return error code
       !!
+      REAL(wp) ::   sst1,sst2                      ! sea surface temperature
+      REAL(wp) ::   e_sst1, e_sst2                 ! saturation vapour pressure
+      REAL(wp) ::   qs1,qs2                        ! specific humidity
+      REAL(wp) ::   pr_tmp                         ! temporary variable for pressure
+ 
+      REAL(wp), DIMENSION(jpi,jpj) ::  hny_frc1    ! Haney forcing for sensible heat, correction for latent heat   
+      REAL(wp), DIMENSION(jpi,jpj) ::  hny_frc2    ! Haney forcing for sensible heat, correction for latent heat   
+      !!
       CHARACTER(len=100) ::  cn_dir          ! Root directory for location of ssr files
       TYPE(FLD_N) ::   sn_sst, sn_sss        ! informations about the fields to be read
@@ -95,11 +104,33 @@
             !
             IF( nn_sstr == 1 ) THEN                                   !* Temperature restoring term
-               DO jj = 1, jpj
-                  DO ji = 1, jpi
-                     zqrp = rn_dqdt * ( sst_m(ji,jj) - sf_sst(1)%fnow(ji,jj,1) )
-                     qns(ji,jj) = qns(ji,jj) + zqrp
-                     qrp(ji,jj) = zqrp
-                  END DO
-               END DO
+                  IF( ln_UKMO_haney ) THEN
+                     DO jj = 1, jpj
+                        DO ji = 1, jpi
+                           sst1   =  sst_m(ji,jj)
+                           sst2   =  sf_sst(1)%fnow(ji,jj,1)   
+                           e_sst1 = 10**((0.7859+0.03477*sst1)/(1.+0.00412*sst1))
+                           e_sst2 = 10**((0.7859+0.03477*sst2)/(1.+0.00412*sst2))         
+                           pr_tmp = 0.01*pressnow(ji,jj)  !pr_tmp = 1012.0
+                           qs1    = (0.62197*e_sst1)/(pr_tmp-0.378*e_sst1)
+                           qs2    = (0.62197*e_sst2)/(pr_tmp-0.378*e_sst2)
+                           hny_frc1(ji,jj) = sst1-sst2                   
+                           hny_frc2(ji,jj) = qs1-qs2                     
+                          !Might need to mask off land points.
+                           hny_frc1(ji,jj)=-hny_frc1(ji,jj)*wndm(ji,jj)*1.42
+                           hny_frc2(ji,jj)=-hny_frc2(ji,jj)*wndm(ji,jj)*4688.0
+                           ! JT Have masked out the land points 
+                           qns(ji,jj)=qns(ji,jj)+(hny_frc1(ji,jj)+hny_frc2(ji,jj))*tmask(ji,jj,1)
+                           qrp(ji,jj) = 0.e0
+                        END DO
+                     END DO
+                  ELSE
+                     DO jj = 1, jpj
+                        DO ji = 1, jpi
+                           zqrp = rn_dqdt * ( sst_m(ji,jj) - sf_sst(1)%fnow(ji,jj,1) )
+                           qns(ji,jj) = qns(ji,jj) + zqrp
+                           qrp(ji,jj) = zqrp
+                        END DO
+                     END DO
+                  ENDIF
                CALL iom_put( "qrp", qrp )                             ! heat flux damping
             ENDIF
@@ -163,5 +194,5 @@
       CHARACTER(len=100) ::  cn_dir          ! Root directory for location of ssr files
       TYPE(FLD_N) ::   sn_sst, sn_sss        ! informations about the fields to be read
-      NAMELIST/namsbc_ssr/ cn_dir, nn_sstr, nn_sssr, rn_dqdt, rn_deds, sn_sst, sn_sss, ln_sssr_bnd, rn_sssr_bnd
+      NAMELIST/namsbc_ssr/ cn_dir, nn_sstr, nn_sssr, rn_dqdt, rn_deds, sn_sst, sn_sss, ln_sssr_bnd, rn_sssr_bnd, ln_UKMO_haney
       INTEGER     ::  ios
       !!----------------------------------------------------------------------
@@ -189,4 +220,5 @@
          WRITE(numout,*) '      flag to bound erp term                 ln_sssr_bnd = ', ln_sssr_bnd
          WRITE(numout,*) '      ABS(Max./Min.) erp threshold           rn_sssr_bnd = ', rn_sssr_bnd, ' mm/day'
+         WRITE(numout,*) '      Haney forcing                          ln_UKMO_haney = ', ln_UKMO_haney
       ENDIF
       !
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/eosbn2.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/eosbn2.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/eosbn2.F90	(revision 7567)
@@ -1241,5 +1241,6 @@
       IF(lwm) WRITE( numond, nameos )
       !
-      rau0        = 1026._wp                 !: volumic mass of reference     [kg/m3]
+      rau0        = 1020._wp                 !: volumic mass of reference     [kg/m3]
+!     rau0        = 1026._wp                 !: volumic mass of reference     [kg/m3]
       rcp         = 3991.86795711963_wp      !: heat capacity     [J/K]
       !
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/traadv_tvd.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/traadv_tvd.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/traadv_tvd.F90	(revision 7567)
@@ -100,4 +100,8 @@
          IF( ( cdtype == 'TRA' .AND. l_trdtra ) .OR. ( cdtype == 'TRC' .AND. l_trdtrc ) ) l_trd = .TRUE.
       ENDIF
+! slwa unless you use l_trdtra too, the above switches off trend calculations for l_trdtrc
+         l_trd = .FALSE.
+         IF( ( cdtype == 'TRA' .AND. l_trdtra ) .OR. ( cdtype == 'TRC' .AND. l_trdtrc ) ) l_trd = .TRUE.
+!slwa
       !
       IF( l_trd )  THEN
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/tradwl.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/tradwl.F90	(revision 7567)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/tradwl.F90	(revision 7567)
@@ -0,0 +1,238 @@
+MODULE tradwl
+   !!======================================================================
+   !!                       ***  MODULE  tradwl  ***
+   !! Ocean physics: solar radiation penetration in the top ocean levels
+   !!======================================================================
+   !! History :  POLCOMS  !  1996-10  (J. Holt)  Original code
+   !!   NEMO     3.2  !  2010-03  (E. O'Dea)  Import to Nemo for use in Shelf Model
+   !!----------------------------------------------------------------------
+
+   !!----------------------------------------------------------------------
+   !!   tra_dwl      : trend due to the solar radiation penetration
+   !!   tra_dwl_init : solar radiation penetration initialization
+   !!----------------------------------------------------------------------
+   USE oce             ! ocean dynamics and active tracers
+   USE dom_oce         ! ocean space and time domain
+   USE sbc_oce         ! surface boundary condition: ocean
+   USE trc_oce         ! share SMS/Ocean variables
+   USE trd_oce         ! trends: ocean variables
+   USE trdtra          ! ocean active tracers trends 
+   USE in_out_manager  ! I/O manager
+   USE phycst          ! physical constants
+   USE prtctl          ! Print control
+   USE iom             ! I/O manager
+   USE fldread         ! read input fields
+
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC   tra_dwl        ! routine called by step.F90 (ln_tradwl=T)
+
+   !                                           !!* Namelist namtra_qsr: penetrative solar radiation
+   LOGICAL , PUBLIC ::   ln_tradwl  = .TRUE.    ! light absorption (dwl) flag
+   LOGICAL , PUBLIC ::   ln_vary_lambda  = .TRUE.    ! vary Lambda or not flag
+   
+   !! * Substitutions
+#  include "domzgr_substitute.h90"
+#  include "vectopt_loop_substitute.h90"
+   !!----------------------------------------------------------------------
+   !! NEMO/OPA 3.2 , LOCEAN-IPSL (2009) 
+   !! $Id$
+   !! Software governed by the CeCILL licence (modipsl/doc/NEMO_CeCILL.txt)
+   !!----------------------------------------------------------------------
+
+CONTAINS
+
+   SUBROUTINE tra_dwl( kt )
+      !!----------------------------------------------------------------------
+      !!                  ***  ROUTINE tra_qsr  ***
+      !!
+      !! ** Purpose :   Compute the temperature trend due to the solar radiation
+      !!      penetration and add it to the general temperature trend.
+      !!
+      !! ** Method  : The profile of the solar radiation within the ocean is defined
+      !!
+      !!          Jason Holt Oct 96
+      !!
+      !!          Calculates change in temperature due to penetrating
+      !!          radiation, with cooling at the surface layer
+      !!
+      !!          rad=rad0*exp(lambda*z)
+      !!
+      !!       Heat input into box is between z=K and z=K+1 is RAD(K)-RAD(K+1)
+      !!
+      !!
+      !! ** Action  : - update ta with the penetrative solar radiation trend
+      !!              - save the trend in ttrd ('key_trdtra')
+      !!
+      !!----------------------------------------------------------------------
+      USE oce, ONLY :   ztrdt => ua   ! use ua as 3D workspace   
+      USE oce, ONLY :   ztrds => va   ! use va as 3D workspace   
+      !!
+      INTEGER, INTENT(in) ::   kt     ! ocean time-step
+      !!
+      INTEGER  ::   ji, jj, jk           ! dummy loop indices
+      INTEGER  ::   irgb                 ! temporary integers
+      REAL(wp) ::   zchl, zcoef, zsi0r   ! temporary scalars
+      REAL(wp) ::   zc0, zc1, zc2, zc3   !    -         -
+      REAL(wp), DIMENSION(jpi,jpj)     ::   zekb, zekg, zekr            ! 2D workspace
+      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   ze0, ze1 , ze2, ze3, zea    ! 3D workspace
+      !!----------------------------------------------------------------------
+      !! HERE GO VARIABLES USED IN POLCOMS CLEAN UP LATER
+
+      integer i,j,k
+!      real*8 dtmp(n-1)
+      real*8 dtmp(jpkm1)
+      real*8 z1,z2,Rad0,Rad1,Rad2,rD,SurfOut,cp
+      logical first
+      save first
+      data first/.true./
+      !!--------------------------End of POLCOMS variables Note instead of using saves
+      !!--------------------------Could shift this into initial code
+
+      IF( kt == nit000 ) THEN
+         IF(lwp) WRITE(numout,*)
+         IF(lwp) WRITE(numout,*) 'tra_dwl : penetration of the surface solar radiation'
+         IF(lwp) WRITE(numout,*) '~~~~~~~'
+         CALL tra_dwl_init
+         IF( .NOT.ln_tradwl )   RETURN
+      ENDIF
+
+      IF( l_trdtra ) THEN      ! Save ta and sa trends
+         ztrdt(:,:,:) = tsa(:,:,:,jp_tem)
+         ztrds(:,:,:) = 0.e0
+      ENDIF
+!--------------------------------------------------------------------
+!  Set transmissivity
+!--------------------------------------------------------------------
+!
+!  Normal value
+!
+!---------------------------------------------------------------------------
+!
+!  Convert Heat fluxes to units used in POL subroutine dwl
+!
+!---------------------------------------------------------------------------
+      cp=3986.0d0
+
+    DO jj = 2, jpj
+         DO ji = fs_2, fs_jpim1
+           qsr(ji,jj)  = qsr(ji,jj)  * (r1_rau0_rcp)
+         ENDDO       !ji
+    ENDDO            !jj
+!--------------------------------------------------------------------------------
+      if ( first ) then
+        rlambda2(:,:) = 0.0
+        first=.false.
+           if ( ln_vary_lambda ) then
+!        do j=1,jesub    ! Original Polcoms style Loop
+!          do i=1,iesub  ! Original Polcoms style Loop
+
+        do jj=2,jpjm1
+          do ji = fs_2, fs_jpim1   ! vector opt.
+
+!            if(ipexb(i,j).ne. 0) then  (Mask, use Tmask instead)
+
+              rlambda2(ji,jj)=-0.033*log(hbatt(ji,jj))+0.2583    ! JIAs formula
+              rlambda2(ji,jj)=max(0.05,rlambda2(ji,jj))     ! limit in deep water
+          enddo ! ji
+        enddo ! jj
+      rlambda = 0.0
+       else
+      rLambda=0.154
+       endif ! If vary lambda
+      endif ! If first
+
+!      do j=1,jesub      ! Original Polcoms Style Loop
+!        do i=1,iesub    ! Original Polcoms Style Loop
+              do jk=2,jpk
+        DO jj=2,jpjm1
+            DO ji = fs_2, fs_jpim1   ! vector opt.
+
+!--------------------------------------------------------------------
+! Calculate change in temperature
+!--------------------------------------------------------------------
+!
+!        rad0 = hfl_in(i,j)   ! change hfl_in to qsr I assume
+
+               rad0 = qsr(ji,jj)
+                     rD = rLambda2(ji,jj)  +rLambda      !  Transmissivity to be used here
+!       if rlambda 0 then rlambda2 not zer and vica versa 
+
+                z2=gdepw_0(ji,jj,jk-1)    ! grid box is from z=z1 to z=z2
+                z1=gdepw_0(ji,jj,jk)
+
+                Rad2=Rad0*(exp(-z2*rD)) ! radiation entering box
+                Rad1=Rad0*(exp(-z1*rD)) ! radiation leaving box
+
+
+                dtmp(jk)=1.0/(e3t_0(ji,jj,jk))*(Rad2-Rad1) !change in temperature
+                tsa(ji,jj,jk,jp_tem) = tsa(ji,jj,jk,jp_tem) + dtmp(jk)
+         enddo  ! ji
+         enddo  ! jj
+             enddo !jk
+
+
+      IF( l_trdtra ) THEN     ! qsr tracers trends saved for diagnostics
+         ztrdt(:,:,:) = tsa(:,:,:,jp_tem) - ztrdt(:,:,:)
+         !CEODCALL trd_mod( ztrdt, ztrds, jptra_trd_qsr, 'TRA', kt )
+      ENDIF
+      !                       ! print mean trends (used for debugging)
+      IF(ln_ctl)   CALL prt_ctl( tab3d_1=tsa(:,:,:,jp_tem), clinfo1=' qsr  - Ta: ', mask1=tmask, clinfo3='tra-ta' )
+      !
+   END SUBROUTINE tra_dwl
+
+
+   SUBROUTINE tra_dwl_init
+      !!----------------------------------------------------------------------
+      !!                  ***  ROUTINE tra_dwl_init  ***
+      !!
+      !! ** Purpose :   Initialization for the penetrative solar radiation for Downwell routine
+      !!
+      !! ** Method  :   The profile of solar radiation within the ocean is set
+      !!      from two length scale of penetration (rn_si0,rn_si1) and a ratio
+      !!      (rn_abs). These parameters are read in the namtra_qsr namelist. The
+      !!      default values correspond to clear water (type I in Jerlov' 
+      !!      (1968) classification.
+      !!         called by tra_qsr at the first timestep (nit000)
+      !!
+      !! ** Action  : - initialize rn_si0, rn_si1 and rn_abs
+      !!
+      !! Reference : Jerlov, N. G., 1968 Optical Oceanography, Elsevier, 194pp.
+      !!----------------------------------------------------------------------
+      INTEGER  ::   ji, jj, jk            ! dummy loop indices
+      INTEGER  ::   ios                   ! Local integer output status for namelist read
+      INTEGER  ::   irgb, ierror          ! temporary integer
+      INTEGER  ::   ioptio, nqsr          ! temporary integer
+      REAL(wp) ::   zc0  , zc1            ! temporary scalars
+      REAL(wp) ::   zc2  , zc3  , zchl    !    -         -
+      REAL(wp) ::   zsi0r, zsi1r, zcoef   !    -         -
+      REAL(wp), DIMENSION(jpi,jpj)     ::   zekb, zekg, zekr              ! 2D workspace
+      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   ze0 , ze1 , ze2 , ze3 , zea   ! 3D workspace
+      !!
+      CHARACTER(len=100) ::   cn_dir   ! Root directory for location of ssr files
+      TYPE(FLD_N)        ::   sn_chl   ! informations about the chlorofyl field to be read
+      NAMELIST/namtra_dwl/  ln_tradwl, ln_vary_lambda
+      !!----------------------------------------------------------------------
+
+      REWIND( numnam_ref )            ! Read Namelist namtra_dwl in reference namelist :
+      READ  ( numnam_ref, namtra_dwl, IOSTAT = ios, ERR = 901)
+901   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namtra_qsr in reference namelist', lwp )
+      
+      REWIND( numnam_cfg )            ! Read Namelist namtra_dwl in configuration namelist :
+      READ  ( numnam_cfg, namtra_dwl, IOSTAT = ios, ERR = 902)
+902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namtra_qsr in configuration namelist', lwp )
+      !
+      IF(lwp) THEN                ! control print
+         WRITE(numout,*)
+         WRITE(numout,*) 'tra_dwl_init : '
+         WRITE(numout,*) '~~~~~~~~~~~~'
+         WRITE(numout,*) '   Namelist namtra_dwl : set the parameter of penetration'
+         WRITE(numout,*) '      Light penetration (T) or not (F)         ln_tradwl  = ', ln_tradwl
+         WRITE(numout,*) '      Vary Lambda  (T) or not (F))             ln_vary_lambda  = ', ln_vary_lambda
+      ENDIF
+
+   END SUBROUTINE tra_dwl_init
+
+   !!======================================================================
+END MODULE tradwl
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/traldf_iso.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/traldf_iso.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/traldf_iso.F90	(revision 7567)
@@ -32,4 +32,7 @@
    USE wrk_nemo        ! Memory Allocation
    USE timing          ! Timing
+#if defined key_bdy  
+   USE bdy_oce  
+#endif  
 
    IMPLICIT NONE
@@ -102,4 +105,7 @@
       REAL(wp), DIMENSION(jpi,jpj,jpk,kjpt), INTENT(inout) ::   pta        ! tracer trend 
       REAL(wp)                             , INTENT(in   ) ::   pahtb0     ! background diffusion coef
+      !JT
+      REAL(wp), DIMENSION(jpi,jpj)     ::   zfactor  ! multiplier for diffusion
+      !JT
       !
       INTEGER  ::  ji, jj, jk, jn   ! dummy loop indices
@@ -118,10 +124,18 @@
       !
 
+      !
+#if defined key_bdy 
+      zfactor(:,:) = sponge_factor(:,:) 
+#else 
+      zfactor(:,:) = 1.0 
+#endif
       IF( kt == kit000 )  THEN
          IF(lwp) WRITE(numout,*)
          IF(lwp) WRITE(numout,*) 'tra_ldf_iso : rotated laplacian diffusion operator on ', cdtype
          IF(lwp) WRITE(numout,*) '~~~~~~~~~~~'
+#if defined key_bdy 
+         IF(lwp) WRITE(numout,*) '~~~~~ using sponge_factor'
+#endif
       ENDIF
-      !
       !                                                          ! ===========
       DO jn = 1, kjpt                                            ! tracer loop
@@ -200,6 +214,6 @@
             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)
+                  zabe1 =  zfactor(ji,jj) * ( fsahtu(ji,jj,jk) + pahtb0 ) * re2u_e1u(ji,jj) * fse3u_n(ji,jj,jk)
+                  zabe2 =  zfactor(ji,jj) * ( fsahtv(ji,jj,jk) + pahtb0 ) * re1v_e2v(ji,jj) * fse3v_n(ji,jj,jk)
                   !
                   zmsku = 1. / MAX(  tmask(ji+1,jj,jk  ) + tmask(ji,jj,jk+1)   &
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/traldf_lap.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/traldf_lap.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/traldf_lap.F90	(revision 7567)
@@ -26,4 +26,7 @@
    USE lib_mpp         ! MPP library
    USE timing          ! Timing
+#if defined key_bdy  
+   USE bdy_oce  
+#endif  
 
    IMPLICIT NONE
@@ -73,4 +76,7 @@
       REAL(wp), DIMENSION(jpi,jpj,jpk,kjpt), INTENT(in   ) ::   ptb        ! before and now tracer fields
       REAL(wp), DIMENSION(jpi,jpj,jpk,kjpt), INTENT(inout) ::   pta        ! tracer trend 
+      !JT
+      REAL(wp), DIMENSION(jpi,jpj)     ::   zfactor  ! multiplier for diffusion
+      !JT
       !
       INTEGER  ::   ji, jj, jk, jn       ! dummy loop indices
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/traqsr.F90	(revision 7567)
@@ -46,4 +46,5 @@
    LOGICAL , PUBLIC ::   ln_qsr_ice   !: light penetration for ice-model LIM3 (clem)
    INTEGER , PUBLIC ::   nn_chldta    !: use Chlorophyll data (=1) or not (=0)
+   INTEGER , PUBLIC ::   nn_kd490dta  !: use kd490dta data (=1) or not (=0)
    REAL(wp), PUBLIC ::   rn_abs       !: fraction absorbed in the very near surface (RGB & 2 bands)
    REAL(wp), PUBLIC ::   rn_si0       !: very near surface depth of extinction      (RGB & 2 bands)
@@ -54,4 +55,5 @@
    REAL(wp) ::   xsi1r                           !: inverse of rn_si1
    TYPE(FLD), ALLOCATABLE, DIMENSION(:) ::   sf_chl   ! structure of input Chl (file informations, fields read)
+   TYPE(FLD), ALLOCATABLE, DIMENSION(:) ::   sf_kd490 ! structure of input kd490 (file informations, fields read)
    INTEGER, PUBLIC ::   nksr              ! levels below which the light cannot penetrate ( depth larger than 391 m)
    REAL(wp), DIMENSION(3,61) ::   rkrgb   !: tabulated attenuation coefficients for RGB absorption
@@ -306,4 +308,48 @@
             !
          ENDIF
+! slwa
+         IF( nn_kd490dta == 1 ) THEN                      !  use KD490 data read in   !
+            !                                             ! ------------------------- !
+               nksr = jpk - 1
+               !
+               CALL fld_read( kt, 1, sf_kd490 )     ! Read kd490 data and provide it at the current time step
+               !
+               zcoef  = ( 1. - rn_abs )
+               ze0(:,:,1) = rn_abs  * qsr(:,:)
+               ze1(:,:,1) = zcoef * qsr(:,:)
+               zea(:,:,1) =         qsr(:,:)
+               !
+               DO jk = 2, nksr+1
+!CDIR NOVERRCHK
+                  DO jj = 1, jpj
+!CDIR NOVERRCHK   
+                     DO ji = 1, jpi
+                        zc0 = ze0(ji,jj,jk-1) * EXP( - fse3t(ji,jj,jk-1) * xsi0r     )
+                        zc1 = ze1(ji,jj,jk-1) * EXP( - fse3t(ji,jj,jk-1) * sf_kd490(1)%fnow(ji,jj,1) )
+                        ze0(ji,jj,jk) = zc0
+                        ze1(ji,jj,jk) = zc1
+                        zea(ji,jj,jk) = ( zc0 + zc1 ) * tmask(ji,jj,jk)
+                     END DO
+                  END DO
+               END DO
+               ! clem: store attenuation coefficient of the first ocean level
+               IF ( ln_qsr_ice ) THEN
+                  DO jj = 1, jpj
+                     DO ji = 1, jpi
+                        zzc0 = rn_abs * EXP( - fse3t(ji,jj,1) * xsi0r     )
+                        zzc1 = zcoef  * EXP( - fse3t(ji,jj,1) * sf_kd490(1)%fnow(ji,jj,1) )
+                        fraqsr_1lev(ji,jj) = 1.0 - ( zzc0 + zzc1 ) * tmask(ji,jj,2) 
+                     END DO
+                  END DO
+               ENDIF
+               !
+               DO jk = 1, nksr                                        ! compute and add qsr trend to ta
+                  qsr_hc(:,:,jk) = r1_rau0_rcp * ( zea(:,:,jk) - zea(:,:,jk+1) )
+               END DO
+               zea(:,:,nksr+1:jpk) = 0.e0     ! 
+               CALL iom_put( 'qsr3d', zea )   ! Shortwave Radiation 3D distribution
+               !
+        ENDIF   ! use KD490 data
+!slwa
          !
          !                                        Add to the general trend
@@ -374,7 +420,8 @@
       CHARACTER(len=100) ::   cn_dir   ! Root directory for location of ssr files
       TYPE(FLD_N)        ::   sn_chl   ! informations about the chlorofyl field to be read
-      !!
-      NAMELIST/namtra_qsr/  sn_chl, cn_dir, ln_traqsr, ln_qsr_rgb, ln_qsr_2bd, ln_qsr_bio, ln_qsr_ice,  &
-         &                  nn_chldta, rn_abs, rn_si0, rn_si1
+      TYPE(FLD_N)        ::   sn_kd490 ! informations about the kd490 field to be read
+      !!
+      NAMELIST/namtra_qsr/  sn_chl, sn_kd490, cn_dir, ln_traqsr, ln_qsr_rgb, ln_qsr_2bd, ln_qsr_bio, ln_qsr_ice,  &
+         &                  nn_chldta, rn_abs, rn_si0, rn_si1, nn_kd490dta
       !!----------------------------------------------------------------------
 
@@ -409,4 +456,5 @@
          WRITE(numout,*) '      RGB & 2 bands: shortess depth of extinction  rn_si0 = ', rn_si0
          WRITE(numout,*) '      2 bands: longest depth of extinction         rn_si1 = ', rn_si1
+         WRITE(numout,*) '      read in KD490 data                       nn_kd490dta  = ', nn_kd490dta
       ENDIF
 
@@ -422,4 +470,5 @@
          IF( ln_qsr_2bd  )   ioptio = ioptio + 1
          IF( ln_qsr_bio  )   ioptio = ioptio + 1
+         IF( nn_kd490dta == 1 )   ioptio = ioptio + 1
          !
          IF( ioptio /= 1 ) &
@@ -431,4 +480,5 @@
          IF( ln_qsr_2bd                      )   nqsr =  3
          IF( ln_qsr_bio                      )   nqsr =  4
+         IF( nn_kd490dta == 1                )   nqsr =  5
          !
          IF(lwp) THEN                   ! Print the choice
@@ -438,4 +488,5 @@
             IF( nqsr ==  3 )   WRITE(numout,*) '         2 bands light penetration'
             IF( nqsr ==  4 )   WRITE(numout,*) '         bio-model light penetration'
+            IF( nqsr ==  5 )   WRITE(numout,*) '         KD490 light penetration'
          ENDIF
          !
@@ -447,6 +498,18 @@
          xsi0r = 1.e0 / rn_si0
          xsi1r = 1.e0 / rn_si1
+         IF( nn_kd490dta == 1 ) THEN           !* KD490 data : set sf_kd490 structure
+            IF(lwp) WRITE(numout,*)
+            IF(lwp) WRITE(numout,*) '        KD490 read in a file'
+            ALLOCATE( sf_kd490(1), STAT=ierror )
+            IF( ierror > 0 ) THEN
+               CALL ctl_stop( 'tra_qsr_init: unable to allocate sf_kd490 structure' )   ;   RETURN
+            ENDIF
+            ALLOCATE( sf_kd490(1)%fnow(jpi,jpj,1)   )
+            IF( sn_kd490%ln_tint )ALLOCATE( sf_kd490(1)%fdta(jpi,jpj,1,2) )
+            !                                        ! fill sf_kd490 with sn_kd490 and control print
+            CALL fld_fill( sf_kd490, (/ sn_kd490 /), cn_dir, 'tra_qsr_init',   &
+               &                                         'Solar penetration function of read KD490', 'namtra_qsr' )
          !                                ! ---------------------------------- !
-         IF( ln_qsr_rgb ) THEN            !  Red-Green-Blue light penetration  !
+         ELSEIF( ln_qsr_rgb ) THEN            !  Red-Green-Blue light penetration  !
             !                             ! ---------------------------------- !
             !
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/trasbc.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/trasbc.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRA/trasbc.F90	(revision 7567)
@@ -25,4 +25,5 @@
    USE trd_oce         ! trends: ocean variables
    USE trdtra          ! trends manager: tracers 
+   USE tradwl          ! solar radiation penetration (downwell method)
    !
    USE in_out_manager  ! I/O manager
@@ -138,5 +139,5 @@
 
 !!gm      IF( .NOT.ln_traqsr )   qsr(:,:) = 0.e0   ! no solar radiation penetration
-      IF( .NOT.ln_traqsr ) THEN     ! no solar radiation penetration
+      IF( .NOT.ln_traqsr .and. .NOT.ln_tradwl ) THEN     ! no solar radiation penetration
          qns(:,:) = qns(:,:) + qsr(:,:)      ! total heat flux in qns
          qsr(:,:) = 0.e0                     ! qsr set to zero
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRD/trdtra.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRD/trdtra.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/TRD/trdtra.F90	(revision 7567)
@@ -203,7 +203,12 @@
          DO jj = 2, jpjm1
             DO ji = fs_2, fs_jpim1   ! vector opt.
+#if defined key_tracer_budget
+!              ptrd(ji,jj,jk) = - (     pf (ji,jj,jk) - pf (ji-ii,jj-ij,jk-ik)  )  * tmask(ji,jj,jk)
+               ptrd(ji,jj,jk) = -      pf (ji,jj,jk) * tmask(ji,jj,jk)
+#else
                ptrd(ji,jj,jk) = - (     pf (ji,jj,jk) - pf (ji-ii,jj-ij,jk-ik)                        &
                  &                  - ( pun(ji,jj,jk) - pun(ji-ii,jj-ij,jk-ik) ) * ptn(ji,jj,jk)  )   &
                  &              / ( e1t(ji,jj) * e2t(ji,jj) * fse3t(ji,jj,jk) )  * tmask(ji,jj,jk)
+#endif
             END DO
          END DO
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfmxl.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfmxl.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/ZDF/zdfmxl.F90	(revision 7567)
@@ -22,4 +22,6 @@
    USE timing          ! Timing
    USE trc_oce, ONLY : lk_offline ! offline flag
+   USE lbclnk 
+   USE eosbn2          ! Equation of state
 
    IMPLICIT NONE
@@ -27,4 +29,5 @@
 
    PUBLIC   zdf_mxl       ! called by step.F90
+   PUBLIC   zdf_mxl_tref  ! called by asminc.F90
    PUBLIC   zdf_mxl_alloc ! Used in zdf_tke_init
 
@@ -33,7 +36,26 @@
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hmlp    !: mixed layer depth  (rho=rho0+zdcrit) [m]
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hmlpt   !: mixed layer depth at t-points        [m]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hmld_tref  !: mixed layer depth at t-points - temperature criterion [m]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hmld_kara  !: mixed layer depth of Kara et al   [m]
 
    REAL(wp), PUBLIC ::   rho_c = 0.01_wp    !: density criterion for mixed layer depth
    REAL(wp)         ::   avt_c = 5.e-4_wp   ! Kz criterion for the turbocline depth
+
+   ! Namelist variables for  namzdf_karaml 
+ 
+   LOGICAL   :: ln_kara            ! Logical switch for calculating kara mixed
+                                     ! layer
+   LOGICAL   :: ln_kara_write25h   ! Logical switch for 25 hour outputs
+   INTEGER   :: jpmld_type         ! mixed layer type             
+   REAL(wp)  :: ppz_ref            ! depth of initial T_ref 
+   REAL(wp)  :: ppdT_crit          ! Critical temp diff  
+   REAL(wp)  :: ppiso_frac         ! Fraction of ppdT_crit used 
+   
+   !Used for 25h mean
+   LOGICAL, PRIVATE :: kara_25h_init = .TRUE.   !Logical used to initalise 25h 
+                                                !outputs. Necissary, because we need to
+                                                !initalise the kara_25h on the zeroth
+                                                !timestep (i.e in the nemogcm_init call)
+   REAL, PRIVATE, ALLOCATABLE, DIMENSION(:,:) :: hmld_kara_25h
 
    !! * Substitutions
@@ -52,5 +74,6 @@
       zdf_mxl_alloc = 0      ! set to zero if no array to be allocated
       IF( .NOT. ALLOCATED( nmln ) ) THEN
-         ALLOCATE( nmln(jpi,jpj), hmld(jpi,jpj), hmlp(jpi,jpj), hmlpt(jpi,jpj), STAT= zdf_mxl_alloc )
+         ALLOCATE( nmln(jpi,jpj), hmld(jpi,jpj), hmlp(jpi,jpj), hmlpt(jpi,jpj), &
+         &                           hmld_tref(jpi,jpj), STAT= zdf_mxl_alloc )
          !
          IF( lk_mpp             )   CALL mpp_sum ( zdf_mxl_alloc )
@@ -123,4 +146,7 @@
       END DO
       ! depth of the mixing and mixed layers
+
+      CALL zdf_mxl_kara( kt ) ! kara MLD
+
       DO jj = 1, jpj
          DO ji = 1, jpi
@@ -146,4 +172,293 @@
    END SUBROUTINE zdf_mxl
 
+
+   SUBROUTINE zdf_mxl_tref()
+      !!----------------------------------------------------------------------
+      !!                  ***  ROUTINE zdf_mxl_tref  ***
+      !!                   
+      !! ** Purpose :   Compute the mixed layer depth with temperature criteria.
+      !!
+      !! ** Method  :   The temperature-defined mixed layer depth is required
+      !!                   when assimilating SST in a 2D analysis. 
+      !!
+      !! ** Action  :   hmld_tref
+      !!----------------------------------------------------------------------
+      !
+      INTEGER  ::   ji, jj, jk   ! dummy loop indices
+      REAL(wp) ::   t_ref               ! Reference temperature  
+      REAL(wp) ::   temp_c = 0.2        ! temperature criterion for mixed layer depth  
+      !!----------------------------------------------------------------------
+      !
+      ! Initialise array
+      IF( zdf_mxl_alloc() /= 0 )   CALL ctl_stop( 'STOP', 'zdf_mxl_tref : unable to allocate arrays' )
+      
+      !For the AMM model assimiation uses a temperature based mixed layer depth  
+      !This is defined here  
+      DO jj = 1, jpj  
+         DO ji = 1, jpi  
+           hmld_tref(ji,jj)=fsdept(ji,jj,1  )   
+           IF(ssmask(ji,jj) > 0.)THEN  
+             t_ref=tsn(ji,jj,1,jp_tem) 
+             DO jk=2,jpk  
+               IF(ssmask(ji,jj)==0.)THEN  
+                  hmld_tref(ji,jj)=fsdept(ji,jj,jk )  
+                  EXIT  
+               ELSEIF( ABS(tsn(ji,jj,jk,jp_tem)-t_ref) < temp_c)THEN  
+                  hmld_tref(ji,jj)=fsdept(ji,jj,jk )  
+               ELSE  
+                  EXIT  
+               ENDIF  
+             ENDDO  
+           ENDIF  
+         ENDDO  
+      ENDDO
+
+   END SUBROUTINE zdf_mxl_tref
+
+   SUBROUTINE zdf_mxl_kara( kt ) 
+      !!---------------------------------------------------------------------------------- 
+      !!                    ***  ROUTINE zdf_mxl_kara  *** 
+      !                                                                        
+      !   Calculate mixed layer depth according to the definition of          
+      !   Kara et al, 2000, JGR, 105, 16803.  
+      ! 
+      !   If mld_type=1 the mixed layer depth is calculated as the depth at which the  
+      !   density has increased by an amount equivalent to a temperature difference of  
+      !   0.8C at the surface. 
+      ! 
+      !   For other values of mld_type the mixed layer is calculated as the depth at  
+      !   which the temperature differs by 0.8C from the surface temperature.  
+      !                                                                        
+      !   Original version: David Acreman                                      
+      ! 
+      !!-----------------------------------------------------------------------------------
+     
+      INTEGER, INTENT( in ) ::   kt   ! ocean time-step index 
+ 
+      NAMELIST/namzdf_karaml/ ln_kara,jpmld_type, ppz_ref, ppdT_crit, &
+      &                       ppiso_frac, ln_kara_write25h 
+ 
+      ! Local variables                                                        
+      REAL, DIMENSION(jpi,jpk) :: ppzdep      ! depth for use in calculating d(rho) 
+      REAL(wp), DIMENSION(jpi,jpj,jpts) :: ztsn_2d  !Local version of tsn 
+      LOGICAL :: ll_found(jpi,jpj)              ! Is T_b to be found by interpolation ? 
+      LOGICAL :: ll_belowml(jpi,jpj,jpk)        ! Flag points below mixed layer when ll_found=F 
+      INTEGER :: ji, jj, jk                     ! loop counter 
+      INTEGER :: ik_ref(jpi,jpj)                ! index of reference level 
+      INTEGER :: ik_iso(jpi,jpj)                ! index of last uniform temp level 
+      REAL    :: zT(jpi,jpj,jpk)                ! Temperature or denisty 
+      REAL    :: zT_ref(jpi,jpj)                ! reference temperature 
+      REAL    :: zT_b                           ! base temperature 
+      REAL    :: zdTdz(jpi,jpj,jpk-2)           ! gradient of zT 
+      REAL    :: zmoddT(jpi,jpj,jpk-2)          ! Absolute temperature difference 
+      REAL    :: zdz                            ! depth difference 
+      REAL    :: zdT                            ! temperature difference 
+      REAL    :: zdelta_T(jpi,jpj)              ! difference critereon 
+      REAL    :: zRHO1(jpi,jpj), zRHO2(jpi,jpj) ! Densities
+      INTEGER, SAVE :: idt, i_steps
+      INTEGER, SAVE :: i_cnt_25h     ! Counter for 25 hour means
+      INTEGER :: ios                 ! Local integer output status for namelist read
+
+     
+ 
+      !!------------------------------------------------------------------------------------- 
+ 
+      IF( kt == nit000 ) THEN 
+         ! Default values 
+         ln_kara      = .FALSE.
+         ln_kara_write25h = .FALSE. 
+         jpmld_type   = 1     
+         ppz_ref      = 10.0 
+         ppdT_crit    = 0.2  
+         ppiso_frac   = 0.1   
+         ! Read namelist
+         REWIND ( numnam_ref )
+         READ   ( numnam_ref, namzdf_karaml, IOSTAT=ios, ERR= 901 )
+901      IF( ios /= 0 ) CALL ctl_nam ( ios , 'namzdf_karaml in reference namelist', lwp )
+
+         REWIND( numnam_cfg )              ! Namelist nam_diadiaop in configuration namelist  3D hourly diagnostics
+         READ  ( numnam_cfg,  namzdf_karaml, IOSTAT = ios, ERR = 902 )
+902      IF( ios /= 0 ) CALL ctl_nam ( ios , 'namzdf_karaml in configuration namelist', lwp )
+         IF(lwm) WRITE ( numond, namzdf_karaml )
+ 
+
+         WRITE(numout,*) '===== Kara mixed layer =====' 
+         WRITE(numout,*) 'ln_kara = ',    ln_kara
+         WRITE(numout,*) 'jpmld_type = ', jpmld_type 
+         WRITE(numout,*) 'ppz_ref = ',    ppz_ref 
+         WRITE(numout,*) 'ppdT_crit = ',  ppdT_crit 
+         WRITE(numout,*) 'ppiso_frac = ', ppiso_frac
+         WRITE(numout,*) 'ln_kara_write25h = ', ln_kara_write25h
+         WRITE(numout,*) '============================' 
+      
+         IF ( .NOT.ln_kara ) THEN
+            WRITE(numout,*) "ln_kara not set; Kara mixed layer not calculated" 
+         ELSE
+            IF (.NOT. ALLOCATED(hmld_kara) ) ALLOCATE( hmld_kara(jpi,jpj) )
+            IF ( ln_kara_write25h .AND. kara_25h_init ) THEN
+               i_cnt_25h = 0
+               IF (.NOT. ALLOCATED(hmld_kara_25h) ) &
+               &   ALLOCATE( hmld_kara_25h(jpi,jpj) )
+               hmld_kara_25h = 0._wp
+               IF( nacc == 1 ) THEN
+                  idt = INT(rdtmin)
+               ELSE
+                  idt = INT(rdt)
+               ENDIF
+               IF( MOD( 3600,idt) == 0 ) THEN 
+                  i_steps = 3600 / idt  
+               ELSE 
+                  CALL ctl_stop('STOP', &
+                  & 'zdf_mxl_kara: timestep must give MOD(3600,rdt)'// &
+                  & ' = 0 otherwise no hourly values are possible') 
+               ENDIF  
+            ENDIF
+         ENDIF
+      ENDIF
+      
+      IF ( ln_kara ) THEN
+         
+         !set critical ln_kara
+         ztsn_2d = tsn(:,:,1,:)
+         ztsn_2d(:,:,jp_tem) = ztsn_2d(:,:,jp_tem) + ppdT_crit
+     
+         ! Set the mixed layer depth criterion at each grid point 
+         ppzdep = 0._wp
+         IF( jpmld_type == 1 ) THEN                                         
+            CALL eos ( tsn(:,:,1,:), &
+            &          ppzdep(:,:), zRHO1(:,:) ) 
+            CALL eos ( ztsn_2d(:,:,:), &
+            &           ppzdep(:,:), zRHO2(:,:) ) 
+            zdelta_T(:,:) = abs( zRHO1(:,:) - zRHO2(:,:) ) * rau0 
+            ! RHO from eos (2d version) doesn't calculate north or east halo: 
+            CALL lbc_lnk( zdelta_T, 'T', 1. ) 
+            zT(:,:,:) = rhop(:,:,:) 
+         ELSE 
+            zdelta_T(:,:) = ppdT_crit                      
+            zT(:,:,:) = tsn(:,:,:,jp_tem)                           
+         ENDIF 
+     
+         ! Calculate the gradient of zT and absolute difference for use later 
+         DO jk = 1 ,jpk-2 
+            zdTdz(:,:,jk)  =    ( zT(:,:,jk+1) - zT(:,:,jk) ) / fse3w(:,:,jk+1) 
+            zmoddT(:,:,jk) = abs( zT(:,:,jk+1) - zT(:,:,jk) ) 
+         END DO 
+     
+         ! Find density/temperature at the reference level (Kara et al use 10m).          
+         ! ik_ref is the index of the box centre immediately above or at the reference level 
+         ! Find ppz_ref in the array of model level depths and find the ref    
+         ! density/temperature by linear interpolation.                                   
+         ik_ref = -1
+         DO jk = jpkm1, 2, -1 
+            WHERE( fsdept(:,:,jk) > ppz_ref ) 
+               ik_ref(:,:) = jk - 1 
+               zT_ref(:,:) = zT(:,:,jk-1) + &
+               &             zdTdz(:,:,jk-1) * ( ppz_ref - fsdept(:,:,jk-1) ) 
+            ENDWHERE 
+         END DO
+         IF ( ANY( ik_ref  < 0 ) .OR. ANY( ik_ref  > jpkm1 ) ) THEN
+            CALL ctl_stop( "STOP", &
+            & "zdf_mxl_kara: unable to find reference level for kara ML" ) 
+         ELSE
+            ! If the first grid box centre is below the reference level then use the 
+            ! top model level to get zT_ref 
+            WHERE( fsdept(:,:,1) > ppz_ref )  
+               zT_ref = zT(:,:,1) 
+               ik_ref = 1 
+            ENDWHERE 
+     
+            ! Search for a uniform density/temperature region where adjacent levels          
+            ! differ by less than ppiso_frac * deltaT.                                      
+            ! ik_iso is the index of the last level in the uniform layer  
+            ! ll_found indicates whether the mixed layer depth can be found by interpolation 
+            ik_iso(:,:)   = ik_ref(:,:) 
+            ll_found(:,:) = .false. 
+            DO jj = 1, nlcj 
+               DO ji = 1, nlci 
+                 !CDIR NOVECTOR 
+                  DO jk = ik_ref(ji,jj), mbathy(ji,jj)-1 
+                     IF( zmoddT(ji,jj,jk) > ( ppiso_frac * zdelta_T(ji,jj) ) ) THEN 
+                        ik_iso(ji,jj)   = jk 
+                        ll_found(ji,jj) = ( zmoddT(ji,jj,jk) > zdelta_T(ji,jj) ) 
+                        EXIT 
+                     ENDIF 
+                  END DO 
+               END DO 
+            END DO 
+     
+            ! Use linear interpolation to find depth of mixed layer base where possible 
+            hmld_kara(:,:) = ppz_ref 
+            DO jj = 1, jpj 
+               DO ji = 1, jpi 
+                  IF( ll_found(ji,jj) .and. tmask(ji,jj,1) == 1.0 ) THEN 
+                     zdz =  abs( zdelta_T(ji,jj) / zdTdz(ji,jj,ik_iso(ji,jj)) ) 
+                     hmld_kara(ji,jj) = fsdept(ji,jj,ik_iso(ji,jj)) + zdz 
+                  ENDIF 
+               END DO 
+            END DO 
+     
+            ! If ll_found = .false. then calculate MLD using difference of zdelta_T    
+            ! from the reference density/temperature 
+     
+            ! Prevent this section from working on land points 
+            WHERE( tmask(:,:,1) /= 1.0 ) 
+               ll_found = .true. 
+            ENDWHERE 
+     
+            DO jk = 1, jpk 
+               ll_belowml(:,:,jk) = abs( zT(:,:,jk) - zT_ref(:,:) ) >= &
+               & zdelta_T(:,:)
+            END DO 
+     
+            ! Set default value where interpolation cannot be used (ll_found=false)  
+            DO jj = 1, jpj 
+               DO ji = 1, jpi 
+                  IF( .NOT. ll_found(ji,jj) )  &
+                  &   hmld_kara(ji,jj) = fsdept(ji,jj,mbathy(ji,jj)) 
+               END DO 
+            END DO 
+     
+            DO jj = 1, jpj 
+               DO ji = 1, jpi 
+                  !CDIR NOVECTOR 
+                  DO jk = ik_ref(ji,jj)+1, mbathy(ji,jj) 
+                     IF( ll_found(ji,jj) ) EXIT 
+                     IF( ll_belowml(ji,jj,jk) ) THEN                
+                        zT_b = zT_ref(ji,jj) + zdelta_T(ji,jj) * &
+                        &      SIGN(1.0, zdTdz(ji,jj,jk-1) ) 
+                        zdT  = zT_b - zT(ji,jj,jk-1)                                      
+                        zdz  = zdT / zdTdz(ji,jj,jk-1)                                       
+                        hmld_kara(ji,jj) = fsdept(ji,jj,jk-1) + zdz 
+                        EXIT                                                   
+                     ENDIF 
+                  END DO 
+               END DO 
+            END DO 
+     
+            hmld_kara(:,:) = hmld_kara(:,:) * tmask(:,:,1) 
+ 
+            IF(  ln_kara_write25h  ) THEN
+               !Double IF required as i_steps not defined if ln_kara_write25h =
+               ! FALSE
+               IF ( ( MOD( kt, i_steps ) == 0 ) .OR.  kara_25h_init ) THEN
+                  hmld_kara_25h = hmld_kara_25h + hmld_kara
+                  i_cnt_25h = i_cnt_25h + 1
+                  IF ( kara_25h_init ) kara_25h_init = .FALSE.
+               ENDIF
+            ENDIF
+ 
+#if defined key_iomput 
+            IF( kt /= nit000 ) THEN 
+               CALL iom_put( "mldkara"  , hmld_kara )   
+               IF( ( MOD( i_cnt_25h, 25) == 0 ) .AND.  ln_kara_write25h ) &
+                  CALL iom_put( "kara25h"  , ( hmld_kara_25h / 25._wp ) )
+            ENDIF
+#endif
+ 
+         ENDIF
+      ENDIF
+       
+   END SUBROUTINE zdf_mxl_kara 
+
    !!======================================================================
 END MODULE zdfmxl
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/nemogcm.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/nemogcm.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/nemogcm.F90	(revision 7567)
@@ -85,4 +85,8 @@
    USE stopar
    USE stopts
+   USE diatmb          ! Top,middle,bottom output
+   USE diaregmean      ! Top,middle,bottom output
+   USE diapea          ! Top,middle,bottom output
+   USE dia25h          ! 25h mean output
 
    IMPLICIT NONE
@@ -475,4 +479,13 @@
       IF( lk_asminc     )   CALL asm_inc_init   ! Initialize assimilation increments
       IF(lwp) WRITE(numout,*) 'Euler time step switch is ', neuler
+                            CALL dia_tmb_init  ! TMB outputs
+                            CALL mpp_max( nstop )
+                            !write(*,*) nstop, narea, 'nstop nemogcm before dia_regmean_init'
+                             
+                            CALL dia_regmean_init  ! TMB outputs
+                            CALL dia_pea_init  ! TMB outputs
+                            CALL mpp_max( nstop )
+                            !write(*,*)  nstop, narea, 'nstop nemogcm finished dia_regmean_init'
+                            CALL dia_25h_init  ! 25h mean  outputs
       !
    END SUBROUTINE nemo_init
@@ -608,6 +621,11 @@
       IF( numout          /=  6 )   CLOSE( numout          )   ! standard model output file
       IF( numdct_vol      /= -1 )   CLOSE( numdct_vol      )   ! volume transports
-      IF( numdct_heat     /= -1 )   CLOSE( numdct_heat     )   ! heat transports
-      IF( numdct_salt     /= -1 )   CLOSE( numdct_salt     )   ! salt transports
+      !JT IF( numdct_heat     /= -1 )   CLOSE( numdct_heat     )   ! heat transports
+      !JT IF( numdct_salt     /= -1 )   CLOSE( numdct_salt     )   ! salt transports
+      IF( numdct_vol  /= -1 )   CLOSE( numdct_vol  )   ! volume transports
+      IF( numdct_temp /= -1 )   CLOSE( numdct_temp )   ! heat transports
+      IF( numdct_sal  /= -1 )   CLOSE( numdct_sal  )   ! salt transports
+      IF( numdct_NOOS /= -1 )   CLOSE( numdct_NOOS )   ! NOOS transports
+
 
       !
@@ -630,4 +648,5 @@
       USE ldftra_oce, ONLY: ldftra_oce_alloc
       USE trc_oce   , ONLY: trc_oce_alloc
+      USE diainsitutem, ONLY: insitu_tem_alloc
 #if defined key_diadct 
       USE diadct    , ONLY: diadct_alloc 
@@ -646,4 +665,5 @@
       ierr = ierr + ldftra_oce_alloc()          ! ocean lateral  physics : tracers
       ierr = ierr + zdf_oce_alloc   ()          ! ocean vertical physics
+      ierr = ierr + insitu_tem_alloc()
       !
       ierr = ierr + trc_oce_alloc   ()          ! shared TRC / TRA arrays
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/step.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/step.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/step.F90	(revision 7567)
@@ -255,4 +255,5 @@
                              CALL tra_sbc    ( kstp )       ! surface boundary condition
       IF( ln_traqsr      )   CALL tra_qsr    ( kstp )       ! penetrative solar radiation qsr
+      IF( ln_tradwl      )   CALL tra_dwl    ( kstp )       ! Polcoms Style Short Wave Radiation 
       IF( ln_trabbc      )   CALL tra_bbc    ( kstp )       ! bottom heat flux
       IF( lk_trabbl      )   CALL tra_bbl    ( kstp )       ! advective (and/or diffusive) bottom boundary layer scheme
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/step_oce.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/step_oce.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/step_oce.F90	(revision 7567)
@@ -25,4 +25,5 @@
    USE sbcrnf           ! surface boundary condition: runoff variables
    USE sbccpl           ! surface boundary condition: coupled formulation (call send at end of step)
+   USE sbcflx           ! surface boundary condition: Fluxes
    USE sbc_oce          ! surface boundary condition: ocean
    USE sbctide          ! Tide initialisation
@@ -30,4 +31,5 @@
 
    USE traqsr           ! solar radiation penetration      (tra_qsr routine)
+   USE tradwl           ! POLCOMS style solar radiation    (tra_dwl routine) 
    USE trasbc           ! surface boundary condition       (tra_sbc routine)
    USE trabbc           ! bottom boundary condition        (tra_bbc routine)
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/trc_oce.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/trc_oce.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/OPA_SRC/trc_oce.F90	(revision 7567)
@@ -27,4 +27,6 @@
    REAL(wp), PUBLIC, SAVE, ALLOCATABLE, DIMENSION(:,:,:) ::   etot3         !: light absortion coefficient
    REAL(wp), PUBLIC, SAVE, ALLOCATABLE, DIMENSION(:,:,:) ::   facvol        !: volume for degraded regions
+   REAL(wp), PUBLIC, SAVE, ALLOCATABLE, DIMENSION(:,:)   ::   rlambda2      !: Lambda2 for downwell version of Short wave Radiation
+   REAL(wp), PUBLIC                                      ::   rlambda       !: Lambda  for downwell version of Short wave Radiation
 
 #if defined key_top 
@@ -78,12 +80,14 @@
       !!                  ***  trc_oce_alloc  ***
       !!----------------------------------------------------------------------
-      INTEGER ::   ierr(2)        ! Local variables
+      INTEGER ::   ierr(3)        ! Local variables
       !!----------------------------------------------------------------------
       ierr(:) = 0
                      ALLOCATE( etot3 (jpi,jpj,jpk), STAT=ierr(1) )
       IF( lk_degrad) ALLOCATE( facvol(jpi,jpj,jpk), STAT=ierr(2) )
+                    ALLOCATE( rlambda2(jpi,jpj),   STAT=ierr(3) )
       trc_oce_alloc  = MAXVAL( ierr )
       !
       IF( trc_oce_alloc /= 0 )   CALL ctl_warn('trc_oce_alloc: failed to allocate etot3 array')
+      IF( trc_oce_alloc /= 0 )   CALL ctl_warn('trc_oce_alloc: failed to allocate etot3, facvol or rlambda2 array')
    END FUNCTION trc_oce_alloc
 
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/MY_TRC/trcsms_my_trc.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/MY_TRC/trcsms_my_trc.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/MY_TRC/trcsms_my_trc.F90	(revision 7567)
@@ -18,4 +18,5 @@
    USE trd_oce
    USE trdtrc
+   USE trcbc, only : trc_bc_read
 
    IMPLICIT NONE
@@ -55,4 +56,6 @@
 
       IF( l_trdtrc )  CALL wrk_alloc( jpi, jpj, jpk, ztrmyt )
+
+      CALL trc_bc_read  ( kt )       ! tracers: surface and lateral Boundary Conditions
 
       IF( l_trdtrc ) THEN      ! Save the trends in the ixed layer
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/MY_TRC/trcwri_my_trc.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/MY_TRC/trcwri_my_trc.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/MY_TRC/trcwri_my_trc.F90	(revision 7567)
@@ -19,9 +19,17 @@
 
    PUBLIC trc_wri_my_trc 
+#if defined key_tracer_budget
+   REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:), SAVE :: trb_temp ! slwa
+#endif
+
 
 #  include "top_substitute.h90"
 CONTAINS
 
+#if defined key_tracer_budget
+   SUBROUTINE trc_wri_my_trc (kt, fl) ! slwa
+#else
    SUBROUTINE trc_wri_my_trc
+#endif
       !!---------------------------------------------------------------------
       !!                     ***  ROUTINE trc_wri_trc  ***
@@ -29,14 +37,67 @@
       !! ** Purpose :   output passive tracers fields 
       !!---------------------------------------------------------------------
+#if defined key_tracer_budget
+      INTEGER, INTENT( in ), OPTIONAL     :: fl 
+      INTEGER, INTENT( in )               :: kt
+      REAL(wp), DIMENSION(jpi,jpj,jpk)    :: trpool !tracer pool temporary output
+#endif
       CHARACTER (len=20)   :: cltra
-      INTEGER              :: jn
+      INTEGER              :: jn,jk
       !!---------------------------------------------------------------------
  
       ! write the tracer concentrations in the file
       ! ---------------------------------------
+
+
+#if defined key_tracer_budget
+      IF( PRESENT(fl)) THEN
+! depth integrated
+! for strict budgetting write this out at end of timestep as an average between 'now' and 'after' at kt
+         DO jn = jp_myt0, jp_myt1 
+            trpool(:,:,:) = 0.5 * ( trn(:,:,:,jn) * fse3t_a(:,:,:) +  &
+                                        trb_temp(:,:,:,jn) * fse3t(:,:,:) )
+!
+            cltra = TRIM( ctrcnm(jn) )                  ! output of tracer density 
+            CALL iom_put( cltra, trpool(:,:,:) / (0.5* (fse3t(:,:,:) + fse3t_a(:,:,:) ) ) )
+!
+            cltra = TRIM( ctrcnm(jn) )//"_pool"     ! volume integrated output
+            DO jk = 1, jpk
+               trpool(:,:,jk) = trpool(:,:,jk) * e1t(:,:) * e2t(:,:)
+            END DO
+            CALL iom_put( cltra, trpool)
+
+!           cltra = TRIM( ctrcnm(jn) )//"_pool"     ! volume integrated output
+!           DO jk = 1, jpk
+!              trpool(:,:,jk) = 0.5 * ( trn(:,:,jk,jn) * fse3t_a(:,:,jk) +  & 
+!                                       trb_temp(:,:,jk,jn) * fse3t(:,:,jk) ) * & 
+!                                       e1t(:,:) * e2t(:,:)
+!           END DO
+!           CALL iom_put( cltra, trpool)
+!           cltra = TRIM( ctrcnm(jn) )                  ! output of tracer density 
+!           CALL iom_put( cltra, trpool(:,:,:) / (0.5* (fse3t(:,:,:) + fse3t_a(:,:,:) ) ) )
+         END DO
+         CALL iom_put( "DEPTH" , 0.5* (fse3t(:,:,:) + fse3t_a(:,:,:) ) )  !  equivalent 'depth' at same time as tracer pool output
+      ELSE
+
+         IF( kt == nittrc000 ) THEN
+           ALLOCATE(trb_temp(jpi,jpj,jpk,jptra))  ! slwa
+         ENDIF
+         trb_temp(:,:,:,:)=trn(:,:,:,:) ! slwa save for tracer budget (unfiltered trn)
+
+!        DO jn = jp_myt0, jp_myt1
+!           cltra = TRIM( ctrcnm(jn) )                  ! short title for tracer
+!           CALL iom_put( cltra, trn(:,:,:,jn) ) 
+!        END DO
+! write out depths and areas in double precision for tracer budget calculations
+         CALL iom_put( "AREA" , e1t(:,:) * e2t(:,:))
+!        CALL iom_put( "DEPTH" , fse3t(:,:,:) )  ! need depth at same time as tracer output
+
+      END IF
+#else
       DO jn = jp_myt0, jp_myt1
          cltra = TRIM( ctrcnm(jn) )                  ! short title for tracer
          CALL iom_put( cltra, trn(:,:,:,jn) )
       END DO
+#endif
       !
    END SUBROUTINE trc_wri_my_trc
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/TRP/trcldf.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/TRP/trcldf.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/TRP/trcldf.F90	(revision 7567)
@@ -56,5 +56,5 @@
       INTEGER, INTENT( in ) ::   kt   ! ocean time-step index
       !!
-      INTEGER            :: jn
+      INTEGER            :: jn, jk
       CHARACTER (len=22) :: charout
       REAL(wp), POINTER, DIMENSION(:,:,:,:) ::   ztrtrd
@@ -105,4 +105,9 @@
         DO jn = 1, jptra
            ztrtrd(:,:,:,jn) = tra(:,:,:,jn) - ztrtrd(:,:,:,jn)
+#if defined key_tracer_budget
+           DO jk = 1, jpkm1
+             ztrtrd(:,:,jk,jn) = ztrtrd(:,:,jk,jn) * e1t(:,:) * e2t(:,:) * fse3t(:,:,jk)  ! slwa
+           END DO
+#endif
            CALL trd_tra( kt, 'TRC', jn, jptra_ldf, ztrtrd(:,:,:,jn) )
         END DO
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/TRP/trcnxt.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/TRP/trcnxt.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/TRP/trcnxt.F90	(revision 7567)
@@ -33,4 +33,7 @@
    USE trdtra
    USE tranxt
+   USE trcbdy          ! BDY open boundaries
+   USE bdy_par, only: lk_bdy
+   USE iom
 # if defined key_agrif
    USE agrif_top_interp
@@ -93,4 +96,7 @@
       CHARACTER (len=22) :: charout
       REAL(wp), POINTER, DIMENSION(:,:,:,:) ::  ztrdt 
+#if defined key_tracer_budget
+      REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::  ztrdt_m1 ! slwa
+#endif
       !!----------------------------------------------------------------------
       !
@@ -101,4 +107,18 @@
          WRITE(numout,*) 'trc_nxt : time stepping on passive tracers'
       ENDIF
+#if defined key_tracer_budget
+      IF( kt == nittrc000 .AND. l_trdtrc ) THEN
+         ALLOCATE( ztrdt_m1(jpi,jpj,jpk,jptra) )  ! slwa
+         IF( ln_rsttr .AND.    &                     ! Restart: read in restart  file
+            iom_varid( numrtr, 'atf_trend_'//TRIM(ctrcnm(1)), ldstop = .FALSE. ) > 0 ) THEN
+            IF(lwp) WRITE(numout,*) '          nittrc000-nn_dttrc ATF tracer trend read in the restart file'
+            DO jn = 1, jptra
+               CALL iom_get( numrtr, jpdom_autoglo, 'atf_trend_'//TRIM(ctrcnm(jn)), ztrdt_m1(:,:,:,jn) )   ! before tracer trend for atf
+            END DO
+         ELSE          
+           ztrdt_m1=0.0
+         ENDIF
+      ENDIF
+#endif
 
 #if defined key_agrif
@@ -111,7 +131,5 @@
 
 
-#if defined key_bdy
-!!      CALL bdy_trc( kt )               ! BDY open boundaries
-#endif
+      IF( lk_bdy )  CALL trc_bdy( kt )               ! BDY open boundaries
 
 
@@ -149,9 +167,37 @@
                zfact = 1.e0 / r2dt(jk)  
                ztrdt(:,:,jk,jn) = ( trb(:,:,jk,jn) - ztrdt(:,:,jk,jn) ) * zfact 
-               CALL trd_tra( kt, 'TRC', jn, jptra_atf, ztrdt )
+!slwa          CALL trd_tra( kt, 'TRC', jn, jptra_atf, ztrdt )
+#if defined key_tracer_budget
+               ztrdt(:,:,jk,jn) = ztrdt(:,:,jk,jn) * e1t(:,:) * e2t(:,:) * e3t_n(:,:,jk)  ! slwa vvl
+               !ztrdt(:,:,jk,jn) = ztrdt(:,:,jk,jn) * e1t(:,:) * e2t(:,:) * e3t_0(:,:,jk)  ! slwa CHANGE for vvl
+#endif
             END DO
+#if defined key_tracer_budget
+! slwa budget code
+              CALL trd_tra( kt, 'TRC', jn, jptra_atf, ztrdt_m1(:,:,:,jn) )
+#else
+              CALL trd_tra( kt, 'TRC', jn, jptra_atf, ztrdt(:,:,:,jn) )
+#endif
          END DO
+#if defined key_tracer_budget
+        ztrdt_m1(:,:,:,:) = ztrdt(:,:,:,:)    ! need previous time step for budget slwa
+#endif
          CALL wrk_dealloc( jpi, jpj, jpk, jptra, ztrdt ) 
       END IF
+
+#if defined key_tracer_budget
+      !                                           Write in the tracer restart file
+      !                                          *******************************
+      IF( lrst_trc ) THEN
+         IF(lwp) WRITE(numout,*)
+         IF(lwp) WRITE(numout,*) 'trc : ATF trend at last time step for tracer budget written in tracer restart file ',   &
+            &                    'at it= ', kt,' date= ', ndastp
+         IF(lwp) WRITE(numout,*) '~~~~'
+         DO jn = 1, jptra
+            CALL iom_rstput( kt, nitrst, numrtw, 'atf_trend_'//TRIM(ctrcnm(jn)), ztrdt_m1(:,:,:,jn) )
+         END DO
+      ENDIF
+#endif
+
       !
       IF(ln_ctl)   THEN  ! print mean trends (used for debugging)
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/TRP/trcrad.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/TRP/trcrad.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/TRP/trcrad.F90	(revision 7567)
@@ -18,4 +18,7 @@
    USE trdtra
    USE prtctl_trc          ! Print control for debbuging
+#if defined key_tracer_budget
+   USE iom
+#endif
 
    IMPLICIT NONE
@@ -110,4 +113,7 @@
       REAL(wp) :: zcoef, ztrcorn, ztrmasn   !    "         "
       REAL(wp), POINTER, DIMENSION(:,:,:) ::   ztrtrdb, ztrtrdn   ! workspace arrays
+#if defined key_tracer_budget
+      REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::  ztrtrdb_m1 ! slwa 
+#endif
       REAL(wp) :: zs2rdt
       LOGICAL ::   lldebug = .FALSE.
@@ -116,4 +122,18 @@
  
       IF( l_trdtrc )  CALL wrk_alloc( jpi, jpj, jpk, ztrtrdb, ztrtrdn )
+#if defined key_tracer_budget
+      IF( kt == nittrc000 .AND. l_trdtrc) THEN
+         ALLOCATE( ztrtrdb_m1(jpi,jpj,jpk,jptra) )  ! slwa
+         IF( ln_rsttr .AND.    &                     ! Restart: read in restart  file
+            iom_varid( numrtr, 'rdb_trend_'//TRIM(ctrcnm(1)), ldstop = .FALSE. ) > 0 ) THEN
+            IF(lwp) WRITE(numout,*) '          nittrc000-nn_dttrc RDB tracer trend read in the restart file'
+            DO jn = 1, jptra
+               CALL iom_get( numrtr, jpdom_autoglo, 'rdb_trend_'//TRIM(ctrcnm(jn)), ztrtrdb_m1(:,:,:,jn) )   ! before tracer trend for rdb
+            END DO
+         ELSE
+           ztrtrdb_m1=0.0
+         ENDIF
+      ENDIF
+#endif
       
       IF( PRESENT( cpreserv )  ) THEN   !  total tracer concentration is preserved 
@@ -156,5 +176,15 @@
                ztrtrdb(:,:,:) = ( ptrb(:,:,:,jn) - ztrtrdb(:,:,:) ) * zs2rdt
                ztrtrdn(:,:,:) = ( ptrn(:,:,:,jn) - ztrtrdn(:,:,:) ) * zs2rdt 
+#if defined key_tracer_budget
+! slwa budget code
+               DO jk = 1, jpkm1
+                  ztrtrdb(:,:,jk) = ztrtrdb(:,:,jk) * e1t(:,:) * e2t(:,:) * fse3t(:,:,jk)
+                  ztrtrdn(:,:,jk) = ztrtrdn(:,:,jk) * e1t(:,:) * e2t(:,:) * fse3t(:,:,jk)
+               END DO
+               CALL trd_tra( kt, 'TRC', jn, jptra_radb, ztrtrdb_m1(:,:,:,jn) )
+               ztrtrdb_m1(:,:,:,jn)=ztrtrdb(:,:,:)
+#else
                CALL trd_tra( kt, 'TRC', jn, jptra_radb, ztrtrdb )       ! Asselin-like trend handling
+#endif
                CALL trd_tra( kt, 'TRC', jn, jptra_radn, ztrtrdn )       ! standard     trend handling
               !
@@ -187,5 +217,15 @@
                ztrtrdb(:,:,:) = ( ptrb(:,:,:,jn) - ztrtrdb(:,:,:) ) * zs2rdt
                ztrtrdn(:,:,:) = ( ptrn(:,:,:,jn) - ztrtrdn(:,:,:) ) * zs2rdt 
+#if defined key_tracer_budget
+! slwa budget code
+               DO jk = 1, jpkm1
+                  ztrtrdb(:,:,jk) = ztrtrdb(:,:,jk) * e1t(:,:) * e2t(:,:) * fse3t(:,:,jk)
+                  ztrtrdn(:,:,jk) = ztrtrdn(:,:,jk) * e1t(:,:) * e2t(:,:) * fse3t(:,:,jk)
+               END DO
+               CALL trd_tra( kt, 'TRC', jn, jptra_radb, ztrtrdb_m1(:,:,:,jn) )
+               ztrtrdb_m1(:,:,:,jn)=ztrtrdb(:,:,:)
+#else
                CALL trd_tra( kt, 'TRC', jn, jptra_radb, ztrtrdb )       ! Asselin-like trend handling
+#endif
                CALL trd_tra( kt, 'TRC', jn, jptra_radn, ztrtrdn )       ! standard     trend handling
               !
@@ -195,4 +235,18 @@
 
       ENDIF
+
+#if defined key_tracer_budget
+      !                                           Write in the tracer restart file
+      !                                          *******************************
+      IF( lrst_trc ) THEN
+         IF(lwp) WRITE(numout,*)
+         IF(lwp) WRITE(numout,*) 'trc : RDB trend at last time step for tracer budget written in tracer restart file ',   &
+            &                    'at it= ', kt,' date= ', ndastp
+         IF(lwp) WRITE(numout,*) '~~~~'
+         DO jn = 1, jptra
+            CALL iom_rstput( kt, nitrst, numrtw, 'rdb_trend_'//TRIM(ctrcnm(jn)), ztrtrdb_m1(:,:,:,jn) )
+         END DO
+      ENDIF
+#endif
 
       IF( l_trdtrc )  CALL wrk_dealloc( jpi, jpj, jpk, ztrtrdb, ztrtrdn )
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/TRP/trcsbc.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/TRP/trcsbc.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/TRP/trcsbc.F90	(revision 7567)
@@ -113,4 +113,5 @@
            sbc_trc_b(:,:,:) = 0._wp
          ENDIF
+         sbc_trc(:,:,:) = 0._wp    !slwa initialise for vvl
       ELSE                                         ! Swap of forcing fields
          IF( ln_top_euler ) THEN
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/TRP/trctrp.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/TRP/trctrp.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/TRP/trctrp.F90	(revision 7567)
@@ -27,4 +27,6 @@
    USE trcsbc          ! surface boundary condition          (trc_sbc routine)
    USE zpshde          ! partial step: hor. derivative       (zps_hde routine)
+   USE trcbdy          ! BDY open boundaries
+   USE bdy_par, only: lk_bdy
 
 #if defined key_agrif
@@ -68,4 +70,5 @@
          IF( ln_trcdmp )        CALL trc_dmp( kstp )            ! internal damping trends
          IF( ln_trcdmp_clo )    CALL trc_dmp_clo( kstp )        ! internal damping trends on closed seas only
+         IF( lk_bdy )           CALL trc_bdy_dmp( kstp )        ! BDY damping trends
                                 CALL trc_adv( kstp )            ! horizontal & vertical advection 
                                 CALL trc_ldf( kstp )            ! lateral mixing
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trc.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trc.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trc.F90	(revision 7567)
@@ -14,4 +14,7 @@
    USE par_oce
    USE par_trc
+#if defined key_bdy
+   USE bdy_oce, only: nb_bdy, OBC_DATA
+#endif
    
    IMPLICIT NONE
@@ -91,4 +94,9 @@
        CHARACTER(len = 20)  :: clunit   !: unit
        LOGICAL              :: llinit   !: read in a file or not
+#if defined  key_my_trc
+       LOGICAL              :: llsbc   !: read in a file or not
+       LOGICAL              :: llcbc   !: read in a file or not
+       LOGICAL              :: llobc   !: read in a file or not
+#endif
        LOGICAL              :: llsave   !: save the tracer or not
    END TYPE PTRACER
@@ -191,4 +199,11 @@
 # endif
    !
+#if defined key_bdy
+   CHARACTER(len=20), PUBLIC, ALLOCATABLE,  SAVE,  DIMENSION(:)   ::  cn_trc_dflt          ! Default OBC condition for all tracers
+   CHARACTER(len=20), PUBLIC, ALLOCATABLE,  SAVE,  DIMENSION(:)   ::  cn_trc               ! Choice of boundary condition for tracers
+   INTEGER,           PUBLIC, ALLOCATABLE,  SAVE,  DIMENSION(:)   ::  nn_trcdmp_bdy        !: =T Tracer damping
+   ! External data structure of BDY for TOP. Available elements: cn_obc, ll_trc, trcnow, dmp
+   TYPE(OBC_DATA),    PUBLIC, ALLOCATABLE, DIMENSION(:,:), TARGET ::  trcdta_bdy           !: bdy external data (local process)
+#endif
 
    !!----------------------------------------------------------------------
@@ -213,4 +228,11 @@
          &      cvol(jpi,jpj,jpk)     , rdttrc(jpk)           , trai(jptra)           ,       &
          &      ctrcnm(jptra)         , ctrcln(jptra)         , ctrcun(jptra)         ,       & 
+#if defined key_my_trc
+         &      ln_trc_sbc(jptra)     , ln_trc_cbc(jptra)     , ln_trc_obc(jptra)     ,       &
+#endif
+#if defined key_bdy
+         &      cn_trc_dflt(nb_bdy)   , cn_trc(nb_bdy)        , nn_trcdmp_bdy(nb_bdy) ,       &
+         &      trcdta_bdy(jptra,nb_bdy)                                              ,       &
+#endif
          &      ln_trc_ini(jptra)     , ln_trc_wri(jptra)     , qsr_mean(jpi,jpj)     ,  STAT = trc_alloc  )  
 
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcbc.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcbc.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcbc.F90	(revision 7567)
@@ -4,4 +4,6 @@
    !! TOP :  module for passive tracer boundary conditions
    !!=====================================================================
+   !! History :  3.5 !  2014-04  (M. Vichi, T. Lovato)  Original
+   !!            3.6 !  2015-03  (T . Lovato) Revision and BDY support
    !!----------------------------------------------------------------------
 #if  defined key_top 
@@ -9,5 +11,5 @@
    !!   'key_top'                                                TOP model 
    !!----------------------------------------------------------------------
-   !!   trc_dta    : read and time interpolated passive tracer data
+   !!   trc_bc       : read and time interpolated tracer Boundary Conditions
    !!----------------------------------------------------------------------
    USE par_trc       !  passive tracers parameters
@@ -17,4 +19,7 @@
    USE lib_mpp       !  MPP library
    USE fldread       !  read input fields
+#if defined key_bdy
+   USE bdy_oce, only: nb_bdy , idx_bdy, ln_coords_file, rn_time_dmp, rn_time_dmp_out
+#endif
 
    IMPLICIT NONE
@@ -30,18 +35,16 @@
    INTEGER  , SAVE, PUBLIC, ALLOCATABLE, DIMENSION(:)  :: n_trc_indsbc ! index of tracer with SBC data
    INTEGER  , SAVE, PUBLIC, ALLOCATABLE, DIMENSION(:)  :: n_trc_indcbc ! index of tracer with CBC data
-   INTEGER  , SAVE, PUBLIC                             :: ntra_obc     ! MAX( 1, nb_trcxxx ) to avoid compilation error with bounds checking
-   INTEGER  , SAVE, PUBLIC                             :: ntra_sbc     ! MAX( 1, nb_trcxxx ) to avoid compilation error with bounds checking
-   INTEGER  , SAVE, PUBLIC                             :: ntra_cbc     ! MAX( 1, nb_trcxxx ) to avoid compilation error with bounds checking
-   REAL(wp) , SAVE, PUBLIC, ALLOCATABLE, DIMENSION(:)  :: rf_trofac   ! multiplicative factor for OBCtracer values
-   TYPE(FLD), SAVE, PUBLIC, ALLOCATABLE, DIMENSION(:)  :: sf_trcobc   ! structure of data input OBC (file informations, fields read)
    REAL(wp) , SAVE, PUBLIC, ALLOCATABLE, DIMENSION(:)  :: rf_trsfac   ! multiplicative factor for SBC tracer values
    TYPE(FLD), SAVE, PUBLIC, ALLOCATABLE, DIMENSION(:)  :: sf_trcsbc   ! structure of data input SBC (file informations, fields read)
    REAL(wp) , SAVE, PUBLIC, ALLOCATABLE, DIMENSION(:)  :: rf_trcfac   ! multiplicative factor for CBC tracer values
    TYPE(FLD), SAVE, PUBLIC, ALLOCATABLE, DIMENSION(:)  :: sf_trccbc   ! structure of data input CBC (file informations, fields read)
+   REAL(wp) , SAVE, PUBLIC, ALLOCATABLE, DIMENSION(:,:)  :: rf_trofac    ! multiplicative factor for OBCtracer values
+   TYPE(FLD), SAVE, PUBLIC, ALLOCATABLE, DIMENSION(:,:), TARGET  :: sf_trcobc    ! structure of data input OBC (file informations, fields read)
+   TYPE(MAP_POINTER), ALLOCATABLE, DIMENSION(:,:) :: nbmap_ptr   ! array of pointers to nbmap
 
    !! * Substitutions
 #  include "domzgr_substitute.h90"
    !!----------------------------------------------------------------------
-   !! NEMO/OPA 3.3 , NEMO Consortium (2010)
+   !! NEMO/OPA 3.6 , NEMO Consortium (2015)
    !! $Id$
    !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
@@ -60,12 +63,15 @@
       !
       INTEGER,INTENT(IN) :: ntrc                           ! number of tracers
-      INTEGER            :: jl, jn                         ! dummy loop indices
+      INTEGER            :: jl, jn , ib, ibd, ii, ij, ik   ! dummy loop indices
       INTEGER            :: ierr0, ierr1, ierr2, ierr3     ! temporary integers
       INTEGER            ::  ios                           ! Local integer output status for namelist read
+      INTEGER            :: nblen, igrd                    ! support arrays for BDY
       CHARACTER(len=100) :: clndta, clntrc
       !
-      CHARACTER(len=100) :: cn_dir
+      CHARACTER(len=100) :: cn_dir_sbc, cn_dir_cbc, cn_dir_obc
+
       TYPE(FLD_N), ALLOCATABLE, DIMENSION(:) :: slf_i  ! local array of namelist informations on the fields to read
-      TYPE(FLD_N), DIMENSION(jpmaxtrc) :: sn_trcobc    ! open
+      TYPE(FLD_N), DIMENSION(jpmaxtrc,2) :: sn_trcobc  ! open
+      TYPE(FLD_N), DIMENSION(jpmaxtrc) :: sn_trcobc2   ! to read in multiple (2) open bdy
       TYPE(FLD_N), DIMENSION(jpmaxtrc) :: sn_trcsbc    ! surface
       TYPE(FLD_N), DIMENSION(jpmaxtrc) :: sn_trccbc    ! coastal
@@ -74,8 +80,16 @@
       REAL(wp)   , DIMENSION(jpmaxtrc) :: rn_trcfac    ! multiplicative factor for tracer values
       !!
-      NAMELIST/namtrc_bc/ cn_dir, sn_trcobc, rn_trofac, sn_trcsbc, rn_trsfac, sn_trccbc, rn_trcfac 
+      NAMELIST/namtrc_bc/ cn_dir_sbc, cn_dir_cbc, cn_dir_obc, sn_trcobc2, rn_trofac, sn_trcsbc, rn_trsfac, sn_trccbc, rn_trcfac
+#if defined key_bdy
+      NAMELIST/namtrc_bdy/ cn_trc_dflt, cn_trc, nn_trcdmp_bdy
+#endif
       !!----------------------------------------------------------------------
       IF( nn_timing == 1 )  CALL timing_start('trc_bc_init')
       !
+      IF( lwp ) THEN
+         WRITE(numout,*) ' '
+         WRITE(numout,*) 'trc_bc_init : Tracers Boundary Conditions (BC)'
+         WRITE(numout,*) '~~~~~~~~~~~ '
+      ENDIF
       !  Initialisation and local array allocation
       ierr0 = 0  ;  ierr1 = 0  ;  ierr2 = 0  ;  ierr3 = 0  
@@ -107,31 +121,5 @@
       n_trc_indcbc(:) = 0
       !
-      DO jn = 1, ntrc
-         IF( ln_trc_obc(jn) ) THEN
-             nb_trcobc       = nb_trcobc + 1 
-             n_trc_indobc(jn) = nb_trcobc 
-         ENDIF
-         IF( ln_trc_sbc(jn) ) THEN
-             nb_trcsbc       = nb_trcsbc + 1
-             n_trc_indsbc(jn) = nb_trcsbc
-         ENDIF
-         IF( ln_trc_cbc(jn) ) THEN
-             nb_trccbc       = nb_trccbc + 1
-             n_trc_indcbc(jn) = nb_trccbc
-         ENDIF
-      ENDDO
-      ntra_obc = MAX( 1, nb_trcobc )   ! To avoid compilation error with bounds checking
-      IF( lwp ) WRITE(numout,*) ' '
-      IF( lwp ) WRITE(numout,*) ' Number of passive tracers to be initialized with open boundary data :', nb_trcobc
-      IF( lwp ) WRITE(numout,*) ' '
-      ntra_sbc = MAX( 1, nb_trcsbc )   ! To avoid compilation error with bounds checking
-      IF( lwp ) WRITE(numout,*) ' '
-      IF( lwp ) WRITE(numout,*) ' Number of passive tracers to be initialized with surface boundary data :', nb_trcsbc
-      IF( lwp ) WRITE(numout,*) ' '
-      ntra_cbc = MAX( 1, nb_trccbc )   ! To avoid compilation error with bounds checking
-      IF( lwp ) WRITE(numout,*) ' '
-      IF( lwp ) WRITE(numout,*) ' Number of passive tracers to be initialized with coastal boundary data :', nb_trccbc
-      IF( lwp ) WRITE(numout,*) ' '
-
+      ! Read Boundary Conditions Namelists
       REWIND( numnat_ref )              ! Namelist namtrc_bc in reference namelist : Passive tracer data structure
       READ  ( numnat_ref, namtrc_bc, IOSTAT = ios, ERR = 901)
@@ -139,58 +127,163 @@
 
       REWIND( numnat_cfg )              ! Namelist namtrc_bc in configuration namelist : Passive tracer data structure
+#if defined key_bdy
+      DO ib = 1, nb_bdy
+#endif
       READ  ( numnat_cfg, namtrc_bc, IOSTAT = ios, ERR = 902 )
 902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namtrc_bc in configuration namelist', lwp )
       IF(lwm) WRITE ( numont, namtrc_bc )
-
-      ! print some information for each 
+#if defined key_bdy
+        sn_trcobc(:,ib)=sn_trcobc2(:)
+      ENDDO
+#endif
+
+#if defined key_bdy
+      REWIND( numnat_ref )              ! Namelist namtrc_bc in reference namelist : Passive tracer data structure
+      READ  ( numnat_ref, namtrc_bdy, IOSTAT = ios, ERR = 903)
+903   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namtrc_bdy in reference namelist', lwp )
+
+      REWIND( numnat_cfg )              ! Namelist namtrc_bc in configuration namelist : Passive tracer data structure
+      READ  ( numnat_cfg, namtrc_bdy, IOSTAT = ios, ERR = 904 )
+904   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namtrc_bdy in configuration namelist', lwp )
+      IF(lwm) WRITE ( numont, namtrc_bdy )
+      ! setup up preliminary informations for BDY structure
+      DO jn = 1, ntrc
+         DO ib = 1, nb_bdy
+            ! Set type of obc in BDY data structure (around here we may plug user override of obc type from nml)
+            IF ( ln_trc_obc(jn) ) THEN
+               trcdta_bdy(jn,ib)%cn_obc = TRIM( cn_trc(ib) )
+            ELSE
+               trcdta_bdy(jn,ib)%cn_obc = TRIM( cn_trc_dflt(ib) )
+            ENDIF
+            ! set damping use in BDY data structure
+            trcdta_bdy(jn,ib)%dmp = .false.
+            IF(nn_trcdmp_bdy(ib) .EQ. 1 .AND. ln_trc_obc(jn) ) trcdta_bdy(jn,ib)%dmp = .true.
+            IF(nn_trcdmp_bdy(ib) .EQ. 2 ) trcdta_bdy(jn,ib)%dmp = .true.
+            IF(trcdta_bdy(jn,ib)%cn_obc == 'frs' .AND. nn_trcdmp_bdy(ib) .NE. 0 )  &
+                & CALL ctl_stop( 'Use FRS OR relaxation' )
+            IF (nn_trcdmp_bdy(ib) .LT. 0 .OR. nn_trcdmp_bdy(ib) .GT. 2)            &
+                & CALL ctl_stop( 'Not a valid option for nn_trcdmp_bdy. Allowed: 0,1,2.' )
+         ENDDO
+      ENDDO
+
+#else
+      ! Force all tracers OBC to false if bdy not used
+      ln_trc_obc = .false.
+#endif
+      ! compose BC data indexes
+      DO jn = 1, ntrc
+         IF( ln_trc_obc(jn) ) THEN
+             nb_trcobc       = nb_trcobc + 1  ; n_trc_indobc(jn) = nb_trcobc
+         ENDIF
+         IF( ln_trc_sbc(jn) ) THEN
+             nb_trcsbc       = nb_trcsbc + 1  ; n_trc_indsbc(jn) = nb_trcsbc
+         ENDIF
+         IF( ln_trc_cbc(jn) ) THEN
+             nb_trccbc       = nb_trccbc + 1  ; n_trc_indcbc(jn) = nb_trccbc
+         ENDIF
+      ENDDO
+
+      ! Print summmary of Boundary Conditions
       IF( lwp ) THEN
+         WRITE(numout,*) ' '
+         WRITE(numout,'(a,i3)') '   Total tracers to be initialized with SURFACE BCs data:', nb_trcsbc
+         IF ( nb_trcsbc > 0 ) THEN
+            WRITE(numout,*) '   #trc        NAME        Boundary     Mult.Fact. '
          DO jn = 1, ntrc
-            IF( ln_trc_obc(jn) )  THEN    
-               clndta = TRIM( sn_trcobc(jn)%clvar ) 
-               IF(lwp) WRITE(numout,*) 'Preparing to read OBC data file for passive tracer number :', jn, ' name : ', clndta, & 
-               &               ' multiplicative factor : ', rn_trofac(jn)
-            ENDIF
-            IF( ln_trc_sbc(jn) )  THEN    
-               clndta = TRIM( sn_trcsbc(jn)%clvar ) 
-               IF(lwp) WRITE(numout,*) 'Preparing to read SBC data file for passive tracer number :', jn, ' name : ', clndta, & 
-               &               ' multiplicative factor : ', rn_trsfac(jn)
-            ENDIF
-            IF( ln_trc_cbc(jn) )  THEN    
-               clndta = TRIM( sn_trccbc(jn)%clvar ) 
-               IF(lwp) WRITE(numout,*) 'Preparing to read CBC data file for passive tracer number :', jn, ' name : ', clndta, & 
-               &               ' multiplicative factor : ', rn_trcfac(jn)
+               IF ( ln_trc_sbc(jn) ) WRITE(numout,9001) jn, TRIM( sn_trcsbc(jn)%clvar ), 'SBC', rn_trsfac(jn)
+            ENDDO
+            ENDIF
+         WRITE(numout,'(2a)') '   SURFACE BC data repository : ', TRIM(cn_dir_sbc)
+
+         WRITE(numout,*) ' '
+         WRITE(numout,'(a,i3)') '   Total tracers to be initialized with COASTAL BCs data:', nb_trccbc
+         IF ( nb_trccbc > 0 ) THEN
+            WRITE(numout,*) '   #trc        NAME        Boundary     Mult.Fact. '
+            DO jn = 1, ntrc
+               IF ( ln_trc_cbc(jn) ) WRITE(numout, 9001) jn, TRIM( sn_trccbc(jn)%clvar ), 'CBC', rn_trcfac(jn)
+            ENDDO
+            ENDIF
+         WRITE(numout,'(2a)') '   COASTAL BC data repository : ', TRIM(cn_dir_cbc)
+
+         WRITE(numout,*) ' '
+         WRITE(numout,'(a,i3)') '   Total tracers to be initialized with OPEN BCs data:', nb_trcobc
+#if defined key_bdy
+         IF ( nb_trcobc > 0 ) THEN
+            WRITE(numout,*) '   #trc        NAME        Boundary     Mult.Fact.   OBC Settings'
+            DO jn = 1, ntrc
+               DO ib = 1, nb_bdy
+                 IF ( ln_trc_obc(jn) )  WRITE(numout, 9001) jn, TRIM( sn_trcobc(jn,ib)%clvar ), 'OBC', rn_trofac(jn), (trcdta_bdy(jn,ib)%cn_obc)
+               ENDDO
+               !IF ( ln_trc_obc(jn) )  WRITE(numout, 9001) jn, TRIM( sn_trcobc(jn,ib)%clvar ), 'OBC', rn_trofac(jn), (trcdta_bdy(jn,ib)%cn_obc,ib=1,nb_bdy)
+               IF ( .NOT. ln_trc_obc(jn) )  WRITE(numout, 9002) jn, 'Set data to IC and use default condition', (trcdta_bdy(jn,ib)%cn_obc,ib=1,nb_bdy)
+            ENDDO
+            WRITE(numout,*) ' '
+            DO ib = 1, nb_bdy
+                IF (nn_trcdmp_bdy(ib) .EQ. 0) WRITE(numout,9003) '   Boundary ',ib,' -> NO damping of tracers'
+                IF (nn_trcdmp_bdy(ib) .EQ. 1) WRITE(numout,9003) '   Boundary ',ib,' -> damping ONLY for tracers with external data provided'
+                IF (nn_trcdmp_bdy(ib) .EQ. 2) WRITE(numout,9003) '   Boundary ',ib,' -> damping of ALL tracers'
+                IF (nn_trcdmp_bdy(ib) .GT. 0) THEN
+                   WRITE(numout,9003) '     USE damping parameters from nambdy for boundary ', ib,' : '
+                   WRITE(numout,'(a,f10.2,a)') '     - Inflow damping time scale  : ',rn_time_dmp(ib),' days'
+                   WRITE(numout,'(a,f10.2,a)') '     - Outflow damping time scale : ',rn_time_dmp_out(ib),' days'
             ENDIF
          END DO
       ENDIF
-      !
-      ! The following code is written this way to reduce memory usage and repeated for each boundary data
-      ! MAV: note that this is just a placeholder and the dimensions must be changed according to 
-      !      what will be done with BDY. A new structure will probably need to be included
-      !
+#endif
+         WRITE(numout,'(2a)') '   OPEN BC data repository : ', TRIM(cn_dir_obc)
+      ENDIF
+9001  FORMAT(2x,i5, 3x, a15, 3x, a5, 6x, e11.3, 4x, 10a13)
+9002  FORMAT(2x,i5, 3x, a41, 3x, 10a13)
+9003  FORMAT(a, i5, a)
+
+      !
+#if defined key_bdy
       ! OPEN Lateral boundary conditions
-      IF( nb_trcobc > 0 ) THEN       !  allocate only if the number of tracer to initialise is greater than zero
-         ALLOCATE( sf_trcobc(nb_trcobc), rf_trofac(nb_trcobc), STAT=ierr1 )
+      IF( nb_trcobc > 0 ) THEN 
+         ALLOCATE ( sf_trcobc(nb_trcobc,nb_bdy), rf_trofac(nb_trcobc,nb_bdy), nbmap_ptr(nb_trcobc,nb_bdy), STAT=ierr1 )
          IF( ierr1 > 0 ) THEN
             CALL ctl_stop( 'trc_bc_init: unable to allocate  sf_trcobc structure' )   ;   RETURN
          ENDIF
-         !
+
+         igrd = 1                       ! Everything is at T-points here
+
+         DO ib = 1, nb_bdy
          DO jn = 1, ntrc
-            IF( ln_trc_obc(jn) ) THEN      ! update passive tracers arrays with input data read from file
+
+               nblen = idx_bdy(ib)%nblen(igrd)
+
+               IF ( ln_trc_obc(jn) ) THEN
+               ! Initialise from external data
                jl = n_trc_indobc(jn)
-               slf_i(jl)    = sn_trcobc(jn)
-               rf_trofac(jl) = rn_trofac(jn)
-                                            ALLOCATE( sf_trcobc(jl)%fnow(jpi,jpj,jpk)   , STAT=ierr2 )
-               IF( sn_trcobc(jn)%ln_tint )  ALLOCATE( sf_trcobc(jl)%fdta(jpi,jpj,jpk,2) , STAT=ierr3 )
+                  slf_i(jl)    = sn_trcobc(jn,ib)
+                  rf_trofac(jl,ib) = rn_trofac(jn)
+                                               ALLOCATE( sf_trcobc(jl,ib)%fnow(nblen,1,jpk)   , STAT=ierr2 )
+                  IF( sn_trcobc(jn,ib)%ln_tint )  ALLOCATE( sf_trcobc(jl,ib)%fdta(nblen,1,jpk,2) , STAT=ierr3 )
                IF( ierr2 + ierr3 > 0 ) THEN
                  CALL ctl_stop( 'trc_bc_init : unable to allocate passive tracer OBC data arrays' )   ;   RETURN
                ENDIF
-            ENDIF
-            !   
+                  trcdta_bdy(jn,ib)%trc => sf_trcobc(jl,ib)%fnow(:,1,:)
+                  trcdta_bdy(jn,ib)%rn_fac = rf_trofac(jl,ib)
+                  ! create OBC mapping array
+                  nbmap_ptr(jl,ib)%ptr => idx_bdy(ib)%nbmap(:,igrd)
+                  nbmap_ptr(jl,ib)%ll_unstruc = ln_coords_file(igrd)
+               ELSE
+               ! Initialise obc arrays from initial conditions
+                  ALLOCATE ( trcdta_bdy(jn,ib)%trc(nblen,jpk) )
+                  DO ibd = 1, nblen
+                     DO ik = 1, jpkm1
+                        ii = idx_bdy(ib)%nbi(ibd,igrd)
+                        ij = idx_bdy(ib)%nbj(ibd,igrd)
+                        trcdta_bdy(jn,ib)%trc(ibd,ik) = trn(ii,ij,ik,jn) * tmask(ii,ij,ik)
+                     END DO
+                  END DO
+                  trcdta_bdy(jn,ib)%rn_fac = 1._wp
+            ENDIF
          ENDDO
-         !                         ! fill sf_trcdta with slf_i and control print
-         CALL fld_fill( sf_trcobc, slf_i, cn_dir, 'trc_bc_init', 'Passive tracer OBC data', 'namtrc_bc' )
-         !
-      ENDIF
-      !
+            CALL fld_fill( sf_trcobc(:,ib), slf_i, cn_dir_obc, 'trc_bc_init', 'Passive tracer OBC data', 'namtrc_bc' )
+         ENDDO
+
+      ENDIF
+#endif
       ! SURFACE Boundary conditions
       IF( nb_trcsbc > 0 ) THEN       !  allocate only if the number of tracer to initialise is greater than zero
@@ -214,5 +307,5 @@
          ENDDO
          !                         ! fill sf_trcsbc with slf_i and control print
-         CALL fld_fill( sf_trcsbc, slf_i, cn_dir, 'trc_bc_init', 'Passive tracer SBC data', 'namtrc_bc' )
+         CALL fld_fill( sf_trcsbc, slf_i, cn_dir_sbc, 'trc_bc_init', 'Passive tracer SBC data', 'namtrc_bc' )
          !
       ENDIF
@@ -239,5 +332,5 @@
          ENDDO
          !                         ! fill sf_trccbc with slf_i and control print
-         CALL fld_fill( sf_trccbc, slf_i, cn_dir, 'trc_bc_init', 'Passive tracer CBC data', 'namtrc_bc' )
+         CALL fld_fill( sf_trccbc, slf_i, cn_dir_cbc, 'trc_bc_init', 'Passive tracer CBC data', 'namtrc_bc' )
          !
       ENDIF
@@ -249,5 +342,5 @@
 
 
-   SUBROUTINE trc_bc_read(kt)
+   SUBROUTINE trc_bc_read(kt, jit)
       !!----------------------------------------------------------------------
       !!                   ***  ROUTINE trc_bc_init  ***
@@ -264,33 +357,62 @@
       !! * Arguments
       INTEGER, INTENT( in ) ::   kt      ! ocean time-step index
-
+      INTEGER, INTENT( in ), OPTIONAL ::   jit   ! subcycle time-step index (for timesplitting option)
+      INTEGER :: ib
       !!---------------------------------------------------------------------
       !
       IF( nn_timing == 1 )  CALL timing_start('trc_bc_read')
 
-      IF( kt == nit000 ) THEN
-         IF(lwp) WRITE(numout,*)
-         IF(lwp) WRITE(numout,*) 'trc_bc_read : Surface boundary conditions for passive tracers.'
-         IF(lwp) WRITE(numout,*) '~~~~~~~ '
-      ENDIF
-
-      ! OPEN boundary conditions: DOES NOT WORK. Waiting for stable BDY
+      IF( kt == nit000 .AND. lwp) THEN
+         WRITE(numout,*)
+         WRITE(numout,*) 'trc_bc_read : Surface boundary conditions for passive tracers.'
+         WRITE(numout,*) '~~~~~~~~~~~ '
+      ENDIF
+
+      IF ( PRESENT(jit) ) THEN 
+
+         ! OPEN boundary conditions (use time_offset=+1 as they are applied at the end of the step)
       IF( nb_trcobc > 0 ) THEN
-        if (lwp) write(numout,'(a,i5,a,i5)') '   reading OBC data for ', nb_trcobc ,' variables at step ', kt
-        CALL fld_read(kt,1,sf_trcobc)
-        ! vertical interpolation on s-grid and partial step to be added
+           if (lwp) write(numout,'(a,i5,a,i10)') '   reading OBC data for ', nb_trcobc ,' variable(s) at step ', kt
+           DO ib = 1,nb_bdy
+             CALL fld_read(kt=kt, kn_fsbc=1, sd=sf_trcobc(:,ib), map=nbmap_ptr(:,ib), kit=jit, kt_offset=+1)
+           ENDDO
       ENDIF
 
       ! SURFACE boundary conditions       
       IF( nb_trcsbc > 0 ) THEN
-        if (lwp) write(numout,'(a,i5,a,i5)') '   reading SBC data for ', nb_trcsbc ,' variables at step ', kt
-        CALL fld_read(kt,1,sf_trcsbc)
+           if (lwp) write(numout,'(a,i5,a,i10)') '   reading SBC data for ', nb_trcsbc ,' variable(s) at step ', kt
+           CALL fld_read(kt=kt, kn_fsbc=1, sd=sf_trcsbc, kit=jit)
       ENDIF
 
       ! COASTAL boundary conditions       
       IF( nb_trccbc > 0 ) THEN
-        if (lwp) write(numout,'(a,i5,a,i5)') '   reading CBC data for ', nb_trccbc ,' variables at step ', kt
-        CALL fld_read(kt,1,sf_trccbc)
+           if (lwp) write(numout,'(a,i5,a,i10)') '   reading CBC data for ', nb_trccbc ,' variable(s) at step ', kt
+           CALL fld_read(kt=kt, kn_fsbc=1, sd=sf_trccbc, kit=jit)
       ENDIF   
+
+      ELSE
+
+         ! OPEN boundary conditions (use time_offset=+1 as they are applied at the end of the step)
+         IF( nb_trcobc > 0 ) THEN
+           if (lwp) write(numout,'(a,i5,a,i10)') '   reading OBC data for ', nb_trcobc ,' variable(s) at step ', kt
+           DO ib = 1,nb_bdy
+             CALL fld_read(kt=kt, kn_fsbc=1, sd=sf_trcobc(:,ib), map=nbmap_ptr(:,ib), kt_offset=+1)
+           ENDDO
+         ENDIF
+
+         ! SURFACE boundary conditions
+         IF( nb_trcsbc > 0 ) THEN
+           if (lwp) write(numout,'(a,i5,a,i10)') '   reading SBC data for ', nb_trcsbc ,' variable(s) at step ', kt
+           CALL fld_read(kt=kt, kn_fsbc=1, sd=sf_trcsbc)
+         ENDIF
+
+         ! COASTAL boundary conditions
+         IF( nb_trccbc > 0 ) THEN
+           if (lwp) write(numout,'(a,i5,a,i10)') '   reading CBC data for ', nb_trccbc ,' variable(s) at step ', kt
+           CALL fld_read(kt=kt, kn_fsbc=1, sd=sf_trccbc)
+         ENDIF
+
+      ENDIF
+
       !
       IF( nn_timing == 1 )  CALL timing_stop('trc_bc_read')
@@ -303,4 +425,10 @@
    !!----------------------------------------------------------------------
 CONTAINS
+
+   SUBROUTINE trc_bc_init( ntrc )        ! Empty routine
+      INTEGER,INTENT(IN) :: ntrc                           ! number of tracers
+      WRITE(*,*) 'trc_bc_init: You should not have seen this print! error?', kt
+   END SUBROUTINE trc_bc_init
+
    SUBROUTINE trc_bc_read( kt )        ! Empty routine
       WRITE(*,*) 'trc_bc_read: You should not have seen this print! error?', kt
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcbdy.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcbdy.F90	(revision 7567)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcbdy.F90	(revision 7567)
@@ -0,0 +1,293 @@
+MODULE trcbdy
+   !!======================================================================
+   !!                       ***  MODULE  bdytrc  ***
+   !! Ocean tracers:   Apply boundary conditions for tracers in TOP component
+   !!======================================================================
+   !! History :  1.0  !  2005-01  (J. Chanut, A. Sellar)  Original code
+   !!            3.0  !  2008-04  (NEMO team)  add in the reference version
+   !!            3.4  !  2011     (D. Storkey) rewrite in preparation for OBC-BDY merge
+   !!            3.5  !  2012     (S. Mocavero, I. Epicoco) Optimization of BDY communications
+   !!            3.6  !  2015     (T. Lovato) Adapt BDY for tracers in TOP component
+   !!----------------------------------------------------------------------
+#if defined key_bdy && key_top
+   !!----------------------------------------------------------------------
+   !!   'key_bdy'                     Unstructured Open Boundary Conditions
+   !!----------------------------------------------------------------------
+   !!   trc_bdy            : Apply open boundary conditions to T and S
+   !!   trc_bdy_frs        : Apply Flow Relaxation Scheme
+   !!----------------------------------------------------------------------
+   USE timing                       ! Timing
+   USE oce_trc                      ! ocean dynamics and tracers variables
+   USE par_trc
+   USE trc                          ! ocean space and time domain variables 
+   USE bdylib                       ! for orlanski library routines
+   USE lbclnk                       ! ocean lateral boundary conditions (or mpp link)
+   USE in_out_manager               ! I/O manager
+   USE bdy_oce, only: idx_bdy, OBC_INDEX, BDYTMASK, lk_bdy       ! ocean open boundary conditions
+
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC trc_bdy      ! routine called in trcnxt.F90 
+   PUBLIC trc_bdy_dmp  ! routine called in trcstp.F90 
+
+   !!----------------------------------------------------------------------
+   !! NEMO/OPA 3.6 , NEMO Consortium (2015)
+   !! $Id$
+   !! Software governed by the CeCILL licence (NEMOGCM/NEMO_CeCILL.txt)
+   !!----------------------------------------------------------------------
+CONTAINS
+
+   SUBROUTINE trc_bdy( kt )
+      !!----------------------------------------------------------------------
+      !!                  ***  SUBROUTINE trc_bdy  ***
+      !!
+      !! ** Purpose : - Apply open boundary conditions for tracers in TOP component
+      !!                and scale the tracer data
+      !!
+      !!----------------------------------------------------------------------
+      INTEGER, INTENT( in ) :: kt     ! Main time step counter
+      !!
+      INTEGER               :: ib_bdy, jn ! Loop indeces
+      !!----------------------------------------------------------------------
+      !
+      IF( nn_timing == 1 ) CALL timing_start('trc_bdy')
+      !
+      DO jn = 1, jptra
+         DO ib_bdy=1, nb_bdy
+
+            SELECT CASE( trcdta_bdy(jn,ib_bdy)%cn_obc )
+            CASE('none')
+               CYCLE
+            CASE('frs')
+               CALL bdy_trc_frs( jn, idx_bdy(ib_bdy), trcdta_bdy(jn,ib_bdy), kt )
+            CASE('specified')
+               CALL bdy_trc_spe( jn, idx_bdy(ib_bdy), trcdta_bdy(jn,ib_bdy), kt )
+            CASE('neumann')
+               CALL bdy_trc_nmn( jn, idx_bdy(ib_bdy), trcdta_bdy(jn,ib_bdy), kt )
+            CASE('orlanski')
+               CALL bdy_trc_orlanski( jn, idx_bdy(ib_bdy), trcdta_bdy(jn,ib_bdy), ll_npo=.false. )
+            CASE('orlanski_npo')
+               CALL bdy_trc_orlanski( jn, idx_bdy(ib_bdy), trcdta_bdy(jn,ib_bdy), ll_npo=.true. )
+            CASE DEFAULT
+               CALL ctl_stop( 'bdy_tra : unrecognised option for open boundaries for T and S' )
+            END SELECT
+
+            ! Boundary points should be updated
+            CALL lbc_bdy_lnk( tra(:,:,:,jn), 'T', 1., ib_bdy )
+
+         ENDDO
+      ENDDO
+      !
+      IF( nn_timing == 1 ) CALL timing_stop('trc_bdy')
+
+   END SUBROUTINE trc_bdy
+
+   SUBROUTINE bdy_trc_frs( jn, idx, dta, kt )
+      !!----------------------------------------------------------------------
+      !!                 ***  SUBROUTINE bdy_trc_frs  ***
+      !!                    
+      !! ** Purpose : Apply the Flow Relaxation Scheme for tracers at open boundaries.
+      !! 
+      !! Reference : Engedahl H., 1995, Tellus, 365-382.
+      !!----------------------------------------------------------------------
+      INTEGER,         INTENT(in) ::   kt
+      INTEGER,         INTENT(in) ::   jn   ! Tracer index
+      TYPE(OBC_INDEX), INTENT(in) ::   idx  ! OBC indices
+      TYPE(OBC_DATA),  INTENT(in) ::   dta  ! OBC external data
+      !! 
+      REAL(wp) ::   zwgt           ! boundary weight
+      INTEGER  ::   ib, ik, igrd   ! dummy loop indices
+      INTEGER  ::   ii, ij         ! 2D addresses
+      !!----------------------------------------------------------------------
+      !
+      IF( nn_timing == 1 ) CALL timing_start('bdy_trc_frs')
+      ! 
+      igrd = 1                       ! Everything is at T-points here
+      DO ib = 1, idx%nblen(igrd)
+         DO ik = 1, jpkm1
+            ii = idx%nbi(ib,igrd)
+            ij = idx%nbj(ib,igrd)
+            zwgt = idx%nbw(ib,igrd)
+            tra(ii,ij,ik,jn) = ( tra(ii,ij,ik,jn) + zwgt * ( ( dta%trc(ib,ik) * dta%rn_fac)  & 
+                        &  - tra(ii,ij,ik,jn) ) ) * tmask(ii,ij,ik)
+         END DO
+      END DO 
+      !
+      IF( kt .eq. nit000 ) CLOSE( unit = 102 )
+      !
+      IF( nn_timing == 1 ) CALL timing_stop('bdy_trc_frs')
+      !
+   END SUBROUTINE bdy_trc_frs
+  
+   SUBROUTINE bdy_trc_spe( jn, idx, dta, kt )
+      !!----------------------------------------------------------------------
+      !!                 ***  SUBROUTINE bdy_trc_frs  ***
+      !!                    
+      !! ** Purpose : Apply a specified value for tracers at open boundaries.
+      !! 
+      !!----------------------------------------------------------------------
+      INTEGER,         INTENT(in) ::   kt
+      INTEGER,         INTENT(in) ::   jn   ! Tracer index
+      TYPE(OBC_INDEX), INTENT(in) ::   idx  ! OBC indices
+      TYPE(OBC_DATA),  INTENT(in) ::   dta  ! OBC external data
+      !! 
+      REAL(wp) ::   zwgt           ! boundary weight
+      INTEGER  ::   ib, ik, igrd   ! dummy loop indices
+      INTEGER  ::   ii, ij         ! 2D addresses
+      !!----------------------------------------------------------------------
+      !
+      IF( nn_timing == 1 ) CALL timing_start('bdy_trc_spe')
+      !
+      igrd = 1                       ! Everything is at T-points here
+      DO ib = 1, idx%nblenrim(igrd)
+         ii = idx%nbi(ib,igrd)
+         ij = idx%nbj(ib,igrd)
+         DO ik = 1, jpkm1
+            tra(ii,ij,ik,jn) = dta%trc(ib,ik) * dta%rn_fac * tmask(ii,ij,ik)
+         END DO
+      END DO
+      !
+      IF( kt .eq. nit000 ) CLOSE( unit = 102 )
+      !
+      IF( nn_timing == 1 ) CALL timing_stop('bdy_trc_spe')
+      !
+   END SUBROUTINE bdy_trc_spe
+
+   SUBROUTINE bdy_trc_nmn( jn, idx, dta, kt )
+      !!----------------------------------------------------------------------
+      !!                 ***  SUBROUTINE bdy_trc_nmn  ***
+      !!                    
+      !! ** Purpose : Duplicate the value for tracers at open boundaries.
+      !! 
+      !!----------------------------------------------------------------------
+      INTEGER,         INTENT(in) ::   kt
+      INTEGER,         INTENT(in) ::   jn   ! Tracer index
+      TYPE(OBC_INDEX), INTENT(in) ::   idx  ! OBC indices
+      TYPE(OBC_DATA),  INTENT(in) ::   dta  ! OBC external data
+      !! 
+      REAL(wp) ::   zwgt           ! boundary weight
+      INTEGER  ::   ib, ik, igrd   ! dummy loop indices
+      INTEGER  ::   ii, ij, zcoef, zcoef1, zcoef2, ip, jp   ! 2D addresses
+      !!----------------------------------------------------------------------
+      !
+      IF( nn_timing == 1 ) CALL timing_start('bdy_trc_nmn')
+      !
+      igrd = 1                       ! Everything is at T-points here
+      DO ib = 1, idx%nblenrim(igrd)
+         ii = idx%nbi(ib,igrd)
+         ij = idx%nbj(ib,igrd)
+         DO ik = 1, jpkm1
+            ! search the sense of the gradient
+            zcoef1 = bdytmask(ii-1,ij  ) +  bdytmask(ii+1,ij  )
+            zcoef2 = bdytmask(ii  ,ij-1) +  bdytmask(ii  ,ij+1)
+            IF ( zcoef1+zcoef2 == 0) THEN
+               ! corner
+               zcoef = tmask(ii-1,ij,ik) + tmask(ii+1,ij,ik) +  tmask(ii,ij-1,ik) +  tmask(ii,ij+1,ik)
+               tra(ii,ij,ik,jn) = tra(ii-1,ij  ,ik,jn) * tmask(ii-1,ij  ,ik) + &
+                 &                tra(ii+1,ij  ,ik,jn) * tmask(ii+1,ij  ,ik) + &
+                 &                tra(ii  ,ij-1,ik,jn) * tmask(ii  ,ij-1,ik) + &
+                 &                tra(ii  ,ij+1,ik,jn) * tmask(ii  ,ij+1,ik)
+               tra(ii,ij,ik,jn) = ( tra(ii,ij,ik,jn) / MAX( 1, zcoef) ) * tmask(ii,ij,ik)
+            ELSE
+               ip = bdytmask(ii+1,ij  ) - bdytmask(ii-1,ij  )
+               jp = bdytmask(ii  ,ij+1) - bdytmask(ii  ,ij-1)
+               tra(ii,ij,ik,jn) = tra(ii+ip,ij+jp,ik,jn) * tmask(ii+ip,ij+jp,ik)
+            ENDIF
+         END DO
+      END DO
+      !
+      IF( kt .eq. nit000 ) CLOSE( unit = 102 )
+      !
+      IF( nn_timing == 1 ) CALL timing_stop('bdy_trc_nmn')
+      !
+   END SUBROUTINE bdy_trc_nmn
+ 
+
+   SUBROUTINE bdy_trc_orlanski( jn, idx, dta, ll_npo )
+      !!----------------------------------------------------------------------
+      !!                 ***  SUBROUTINE bdy_trc_orlanski  ***
+      !!             
+      !!              - Apply Orlanski radiation to tracers of TOP component. 
+      !!              - Wrapper routine for bdy_orlanski_3d
+      !! 
+      !!
+      !! References:  Marchesiello, McWilliams and Shchepetkin, Ocean Modelling vol. 3 (2001)    
+      !!----------------------------------------------------------------------
+      INTEGER,                      INTENT(in) ::   jn      ! Tracer index
+      TYPE(OBC_INDEX),              INTENT(in) ::   idx     ! OBC indices
+      TYPE(OBC_DATA),               INTENT(in) ::   dta     ! OBC external data
+      LOGICAL,                      INTENT(in) ::   ll_npo  ! switch for NPO version
+
+      INTEGER  ::   igrd                                    ! grid index
+      !!----------------------------------------------------------------------
+
+      IF( nn_timing == 1 ) CALL timing_start('bdy_trc_orlanski')
+      !
+      igrd = 1      ! Orlanski bc on tracers; 
+      !            
+      CALL bdy_orlanski_3d( idx, igrd, trb(:,:,:,jn), tra(:,:,:,jn), (dta%trc * dta%rn_fac), ll_npo )
+      !
+      IF( nn_timing == 1 ) CALL timing_stop('bdy_trc_orlanski')
+      !
+
+   END SUBROUTINE bdy_trc_orlanski
+
+   SUBROUTINE trc_bdy_dmp( kt )
+      !!----------------------------------------------------------------------
+      !!                 ***  SUBROUTINE trc_bdy_dmp  ***
+      !!                    
+      !! ** Purpose : Apply damping for tracers at open boundaries.
+      !!             It currently applies the damping to all tracers!!!
+      !! 
+      !!----------------------------------------------------------------------
+      INTEGER,         INTENT(in) ::   kt
+      !! 
+      INTEGER  ::   jn             ! Tracer index
+      REAL(wp) ::   zwgt           ! boundary weight
+      REAL(wp) ::   zta, zsa, ztime
+      INTEGER  ::   ib, ik, igrd   ! dummy loop indices
+      INTEGER  ::   ii, ij         ! 2D addresses
+      INTEGER  ::   ib_bdy         ! Loop index
+      !!----------------------------------------------------------------------
+      !
+      IF( nn_timing == 1 ) CALL timing_start('trc_bdy_dmp')
+      !
+      DO jn = 1, jptra
+         DO ib_bdy=1, nb_bdy
+            IF ( trcdta_bdy(jn, ib_bdy)%dmp ) THEN
+               igrd = 1                       ! Everything is at T-points here
+               DO ib = 1, idx_bdy(ib_bdy)%nblen(igrd)
+                  ii = idx_bdy(ib_bdy)%nbi(ib,igrd)
+                  ij = idx_bdy(ib_bdy)%nbj(ib,igrd)
+                  zwgt = idx_bdy(ib_bdy)%nbd(ib,igrd)
+                  DO ik = 1, jpkm1
+                     zta = zwgt * ( trcdta_bdy(jn, ib_bdy)%trc(ib,ik) - trb(ii,ij,ik,jn) ) * tmask(ii,ij,ik)
+                     tra(ii,ij,ik,jn) = tra(ii,ij,ik,jn) + zta
+                  END DO
+               END DO
+            ENDIF
+         ENDDO
+      ENDDO
+      !
+      IF( nn_timing == 1 ) CALL timing_stop('trc_bdy_dmp')
+      !
+   END SUBROUTINE trc_bdy_dmp
+ 
+#else
+   !!----------------------------------------------------------------------
+   !!   Dummy module                   NO Unstruct Open Boundary Conditions
+   !!----------------------------------------------------------------------
+CONTAINS
+   SUBROUTINE trc_bdy(kt)      ! Empty routine
+      WRITE(*,*) 'trc_bdy: You should not have seen this print! error?', kt
+   END SUBROUTINE trc_bdy
+
+   SUBROUTINE trc_bdy_dmp(kt)      ! Empty routine
+      WRITE(*,*) 'trc_bdy_dmp: You should not have seen this print! error?', kt
+   END SUBROUTINE trc_bdy_dmp
+
+#endif
+
+   !!======================================================================
+END MODULE trcbdy
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcdta.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcdta.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcdta.F90	(revision 7567)
@@ -9,4 +9,5 @@
    !!            3.4   !  2010-11  (C. Ethe, G. Madec)  use of fldread + dynamical allocation 
    !!            3.5   !  2013-08  (M. Vichi)  generalization for other BGC models
+   !!            3.6   !  2015-03  (T. Lovato) revision of code log info
    !!----------------------------------------------------------------------
 #if  defined key_top 
@@ -72,4 +73,10 @@
       IF( nn_timing == 1 )  CALL timing_start('trc_dta_init')
       !
+      IF( lwp ) THEN
+         WRITE(numout,*) ' '
+         WRITE(numout,*) '  trc_dta_init : Tracers Initial Conditions (IC)'
+         WRITE(numout,*) '  ~~~~~~~~~~~ '
+      ENDIF
+      !
       !  Initialisation
       ierr0 = 0  ;  ierr1 = 0  ;  ierr2 = 0  ;  ierr3 = 0  
@@ -77,5 +84,5 @@
       ALLOCATE( n_trc_index(ntrc), slf_i(ntrc), STAT=ierr0 )
       IF( ierr0 > 0 ) THEN
-         CALL ctl_stop( 'trc_nam: unable to allocate n_trc_index' )   ;   RETURN
+         CALL ctl_stop( 'trc_dta_init: unable to allocate n_trc_index' )   ;   RETURN
       ENDIF
       nb_trcdta      = 0
@@ -97,9 +104,9 @@
       REWIND( numnat_ref )              ! Namelist namtrc_dta in reference namelist : Passive tracer input data
       READ  ( numnat_ref, namtrc_dta, IOSTAT = ios, ERR = 901)
-901   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namtrc_dta in reference namelist', lwp )
+901   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namtrc_dta_init in reference namelist', lwp )
 
       REWIND( numnat_cfg )              ! Namelist namtrc_dta in configuration namelist : Passive tracer input data
       READ  ( numnat_cfg, namtrc_dta, IOSTAT = ios, ERR = 902 )
-902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namtrc_dta in configuration namelist', lwp )
+902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namtrc_dta_init in configuration namelist', lwp )
       IF(lwm) WRITE ( numont, namtrc_dta )
 
@@ -109,12 +116,14 @@
                clndta = TRIM( sn_trcdta(jn)%clvar ) 
                clntrc = TRIM( ctrcnm   (jn)       ) 
+               if (jn > jptra) clntrc='Dummy' ! By pass weird formats in ocean.output if ntrc > jptra
                zfact  = rn_trfac(jn)
                IF( clndta /=  clntrc ) THEN 
-                  CALL ctl_warn( 'trc_dta_init: passive tracer data initialisation :  ',   &
-                  &              'the variable name in the data file : '//clndta//   & 
-                  &              '  must be the same than the name of the passive tracer : '//clntrc//' ')
+                  CALL ctl_warn( 'trc_dta_init: passive tracer data initialisation    ',   &
+                  &              'Input name of data file : '//TRIM(clndta)//   &
+                  &              ' differs from that of tracer : '//TRIM(clntrc)//' ')
                ENDIF
-               WRITE(numout,*) ' read an initial file for passive tracer number :', jn, ' name : ', clndta, & 
-               &               ' multiplicative factor : ', zfact
+               WRITE(numout,*) ' '
+               WRITE(numout,'(a, i3,3a,e11.3)') ' Read IC file for tracer number :', &
+               &            jn, ', name : ', TRIM(clndta), ', Multiplicative Scaling factor : ', zfact
             ENDIF
          END DO
@@ -124,5 +133,5 @@
          ALLOCATE( sf_trcdta(nb_trcdta), rf_trfac(nb_trcdta), STAT=ierr1 )
          IF( ierr1 > 0 ) THEN
-            CALL ctl_stop( 'trc_dta_ini: unable to allocate  sf_trcdta structure' )   ;   RETURN
+            CALL ctl_stop( 'trc_dta_init: unable to allocate  sf_trcdta structure' )   ;   RETURN
          ENDIF
          !
@@ -135,5 +144,5 @@
                IF( sn_trcdta(jn)%ln_tint )  ALLOCATE( sf_trcdta(jl)%fdta(jpi,jpj,jpk,2) , STAT=ierr3 )
                IF( ierr2 + ierr3 > 0 ) THEN
-                 CALL ctl_stop( 'trc_dta : unable to allocate passive tracer data arrays' )   ;   RETURN
+                 CALL ctl_stop( 'trc_dta_init : unable to allocate passive tracer data arrays' )   ;   RETURN
                ENDIF
             ENDIF
@@ -141,5 +150,5 @@
          ENDDO
          !                         ! fill sf_trcdta with slf_i and control print
-         CALL fld_fill( sf_trcdta, slf_i, cn_dir, 'trc_dta', 'Passive tracer data', 'namtrc' )
+         CALL fld_fill( sf_trcdta, slf_i, cn_dir, 'trc_dta_init', 'Passive tracer data', 'namtrc' )
          !
       ENDIF
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcini.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcini.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcini.F90	(revision 7567)
@@ -32,4 +32,5 @@
    USE sbc_oce
    USE trcice          ! tracers in sea ice
+   USE trcbc,   only : trc_bc_init ! generalized Boundary Conditions
  
    IMPLICIT NONE
@@ -110,6 +111,6 @@
       ENDIF
 
+      ! Initialisation of tracers Initial Conditions
       IF( ln_trcdta )      CALL trc_dta_init(jptra)
-
 
       IF( ln_rsttr ) THEN
@@ -140,8 +141,18 @@
             CALL wrk_dealloc( jpi, jpj, jpk, ztrcdta )
         ENDIF
+! slwa temporary insert initialise tracer
+            trn(:,:,:,:)  = 0._wp
+            if(nproc.eq.39)then
+              DO jn = 1, jptra
+                  trn(:,:,:,jn) = 100._wp * tmask(:,:,:)
+              ENDDO
+            endif
+!!!! slwa temp
         !
         trb(:,:,:,:) = trn(:,:,:,:)
         ! 
       ENDIF
+! Initialisation of tracers Boundary Conditions  - here so that you can use initial condition as boundary
+      IF( lk_my_trc )     CALL trc_bc_init(jptra)
  
       tra(:,:,:,:) = 0._wp
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcnam.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcnam.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcnam.F90	(revision 7567)
@@ -34,4 +34,10 @@
    PUBLIC trc_nam_run  ! called in trcini
    PUBLIC trc_nam      ! called in trcini
+   PUBLIC trc_nam_dia
+#if defined key_trdmxl_trc  || defined key_trdtrc
+   NAMELIST/namtrc_trd/ nn_trd_trc, nn_ctls_trc, rn_ucf_trc, &
+      &                ln_trdmxl_trc_restart, ln_trdmxl_trc_instant, &
+      &                cn_trdrst_trc_in, cn_trdrst_trc_out, ln_trdtrc
+#endif
 
    !! * Substitutions
@@ -57,4 +63,8 @@
       !!---------------------------------------------------------------------
       INTEGER  ::   jn                  ! dummy loop indice
+#if defined key_trdmxl_trc  || defined key_trdtrc
+      INTEGER :: ios
+#endif
+
       !                                        !   Parameters of the run 
       IF( .NOT. lk_offline ) CALL trc_nam_run
@@ -304,4 +314,9 @@
          ctrcun    (jn) = TRIM( sn_tracer(jn)%clunit  )
          ln_trc_ini(jn) =       sn_tracer(jn)%llinit
+#if defined key_my_trc
+         ln_trc_sbc(jn) =       sn_tracer(jn)%llsbc
+         ln_trc_cbc(jn) =       sn_tracer(jn)%llcbc
+         ln_trc_obc(jn) =       sn_tracer(jn)%llobc
+#endif
          ln_trc_wri(jn) =       sn_tracer(jn)%llsave
       END DO
@@ -322,7 +337,7 @@
       INTEGER ::  ierr
 #if defined key_trdmxl_trc  || defined key_trdtrc
-      NAMELIST/namtrc_trd/ nn_trd_trc, nn_ctls_trc, rn_ucf_trc, &
-         &                ln_trdmxl_trc_restart, ln_trdmxl_trc_instant, &
-         &                cn_trdrst_trc_in, cn_trdrst_trc_out, ln_trdtrc
+!     NAMELIST/namtrc_trd/ nn_trd_trc, nn_ctls_trc, rn_ucf_trc, &
+!        &                ln_trdmxl_trc_restart, ln_trdmxl_trc_instant, &
+!        &                cn_trdrst_trc_in, cn_trdrst_trc_out, ln_trdtrc
 #endif
       NAMELIST/namtrc_dia/ ln_diatrc, ln_diabio, nn_writedia, nn_writebio
@@ -330,8 +345,4 @@
       INTEGER  ::   ios                 ! Local integer output status for namelist read
       !!---------------------------------------------------------------------
-
-      IF(lwp) WRITE(numout,*) 
-      IF(lwp) WRITE(numout,*) 'trc_nam_dia : read the passive tracer diagnostics options'
-      IF(lwp) WRITE(numout,*) '~~~~~~~'
 
       IF(lwp) WRITE(numout,*)
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcstp.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcstp.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcstp.F90	(revision 7567)
@@ -100,4 +100,8 @@
          IF( lrst_trc )            CALL trc_rst_wri  ( kt )       ! write tracer restart file
          IF( lk_trdmxl_trc  )      CALL trd_mxl_trc  ( kt )       ! trends: Mixed-layer
+#if defined key_tracer_budget
+!slwa tracer budget
+         IF( lk_iomput ) CALL trc_wri (kt, 2) 
+#endif
          !
          IF( nn_dttrc /= 1   )     CALL trc_sub_reset( kt )       ! resetting physical variables when sub-stepping
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcsub.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcsub.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcsub.F90	(revision 7567)
@@ -20,5 +20,5 @@
 #endif
 #if defined key_zdfgls
-   USE zdfgls, ONLY: en
+!  USE zdfgls, ONLY: en
 #endif
    USE trabbl
Index: branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcwri.F90
===================================================================
--- branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcwri.F90	(revision 7566)
+++ branches/UKMO/CO6_shelfclimate/NEMOGCM/NEMO/TOP_SRC/trcwri.F90	(revision 7567)
@@ -32,5 +32,9 @@
 CONTAINS
 
+#if defined key_tracer_budget
+   SUBROUTINE trc_wri( kt , fl)  !slwa
+#else
    SUBROUTINE trc_wri( kt )
+#endif
       !!---------------------------------------------------------------------
       !!                     ***  ROUTINE trc_wri  ***
@@ -39,4 +43,7 @@
       !!---------------------------------------------------------------------
       INTEGER, INTENT( in )     :: kt
+#if defined key_tracer_budget
+      INTEGER, INTENT( in ), OPTIONAL     :: fl  ! slwa
+#endif
       !
       INTEGER                   :: jn
@@ -59,5 +66,10 @@
       IF( lk_cfc     )   CALL trc_wri_cfc        ! surface fluxes of CFC
       IF( lk_c14b    )   CALL trc_wri_c14b       ! surface fluxes of C14
+#if defined key_tracer_budget
+      IF( .NOT.PRESENT(fl) .AND. lk_my_trc  )   CALL trc_wri_my_trc (kt)     ! MY_TRC  tracers   slwa
+      IF( PRESENT(fl) .AND. lk_my_trc  )   CALL trc_wri_my_trc (kt, fl)    ! MY_TRC  tracers for budget slwa
+#else
       IF( lk_my_trc  )   CALL trc_wri_my_trc     ! MY_TRC  tracers
+#endif
       !
       IF( nn_timing == 1 )  CALL timing_stop('trc_wri')
