Index: NEMO/branches/UKMO/NEMO_4.0.4_CO9_package-mix-lyr/src/OCE/SBC/cpl_oasis3.F90
===================================================================
--- NEMO/branches/UKMO/NEMO_4.0.4_CO9_package-mix-lyr/src/OCE/SBC/cpl_oasis3.F90	(revision 15504)
+++ NEMO/branches/UKMO/NEMO_4.0.4_CO9_package-mix-lyr/src/OCE/SBC/cpl_oasis3.F90	(revision 15505)
@@ -27,4 +27,7 @@
 #if defined key_oasis3
    USE mod_oasis                    ! OASIS3-MCT module
+#endif 
+#if defined key_iomput && defined key_xios25  
+   USE xios, ONLY:xios_oasis_enddef
 #endif
    USE par_oce                      ! ocean parameters
@@ -310,4 +313,10 @@
       IF( agrif_fixed() == Agrif_Nb_Fine_Grids() ) THEN
 #endif
+      !  
+#if defined key_iomput && defined key_xios25  
+!needed for XIOS 2.5  
+      CALL xios_oasis_enddef()  
+#endif  
+      ! 
       CALL oasis_enddef(nerror)
       IF( nerror /= OASIS_Ok )   CALL oasis_abort ( ncomp_id, 'cpl_define', 'Failure in oasis_enddef')
Index: NEMO/branches/UKMO/NEMO_4.0.4_CO9_package-mix-lyr/src/OCE/ZDF/zdfmxl.F90
===================================================================
--- NEMO/branches/UKMO/NEMO_4.0.4_CO9_package-mix-lyr/src/OCE/ZDF/zdfmxl.F90	(revision 15504)
+++ NEMO/branches/UKMO/NEMO_4.0.4_CO9_package-mix-lyr/src/OCE/ZDF/zdfmxl.F90	(revision 15505)
@@ -28,11 +28,12 @@
    PUBLIC   zdf_mxl   ! called by zdfphy.F90
 
-   INTEGER , PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   nmln    !: number of level in the mixed layer (used by LDF, ZDF, TRD, TOP)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hmld    !: mixing layer depth (turbocline)      [m]   (used by TOP)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hmlp    !: mixed layer depth  (rho=rho0+zdcrit) [m]   (used by LDF)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hmlpt   !: depth of the last T-point inside the mixed layer [m] (used by LDF)
-   REAL(wp), PUBLIC, ALLOCATABLE,       DIMENSION(:,:) ::   hmld_zint  !: vertically-interpolated mixed layer depth   [m]
-   REAL(wp), PUBLIC, ALLOCATABLE,       DIMENSION(:,:) ::   htc_mld    ! Heat content of hmld_zint
-   LOGICAL, PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)    :: ll_found   ! Is T_b to be found by interpolation ?
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   :: hmld_tref  !: mixed layer depth at t-points - temperature criterion [m]  
+   INTEGER , PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   :: nmln       !: number of level in the mixed layer (used by TOP)  
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   :: hmld       !: mixing layer depth (turbocline)      [m]  
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   :: hmlp       !: mixed layer depth  (rho=rho0+zdcrit) [m]  
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   :: hmlpt      !: depth of the last T-point inside the mixed layer [m]  
+   REAL(wp), PUBLIC, ALLOCATABLE,       DIMENSION(:,:,:) :: hmld_zint  !: vertically-interpolated mixed layer depth   [m]    
+   REAL(wp), PUBLIC, ALLOCATABLE,       DIMENSION(:,:,:) :: htc_mld    ! Heat content of hmld_zint   
+   LOGICAL, PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)    :: ll_found   ! Is T_b to be found by interpolation ?    
    LOGICAL, PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:)  :: ll_belowml ! Flag points below mixed layer when ll_found=F
 
@@ -47,4 +48,17 @@
    END TYPE MXL_ZINT
 
+!Used for 25h mean   
+   LOGICAL, PRIVATE :: mld_25h_init = .TRUE.    !Logical used to initalise 25h   
+                                                !outputs. Necessary, because we need to   
+                                                !initalise the mld_25h on the zeroth   
+                                                !timestep (i.e in the nemogcm_init call)   
+   LOGICAL, PRIVATE :: mld_25h_write = .FALSE.  !Logical confirm 25h calculating/processing   
+   INTEGER, SAVE :: i_cnt_25h                   ! Counter for 25 hour means   
+   INTEGER, PRIVATE :: nn_mld_diag = 0          ! number of diagnostics 
+   INTEGER, PRIVATE, PARAMETER :: MAX_DIAG = 5  ! maximum number of diagnostics 
+   LOGICAL, PRIVATE, DIMENSION(MAX_DIAG) :: cmld_zint, cmld_mld 
+ 
+   REAL(wp),SAVE, ALLOCATABLE, DIMENSION(:,:,:) :: hmld_zint_25h
+
    !!----------------------------------------------------------------------
    !! NEMO/OCE 4.0 , NEMO Consortium (2018)
@@ -60,6 +74,8 @@
       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), hmld_zint(jpi,jpj),     &
- 	&          htc_mld(jpi,jpj), ll_found(jpi,jpj), ll_belowml(jpi,jpj,jpk), STAT= zdf_mxl_alloc )         
+         ALLOCATE( nmln(jpi,jpj), hmld(jpi,jpj), hmlp(jpi,jpj), hmlpt(jpi,jpj), hmld_zint(jpi,jpj, MAX_DIAG),       &  
+                   htc_mld(jpi,jpj,MAX_DIAG), ll_found(jpi,jpj), ll_belowml(jpi,jpj,jpk), STAT= zdf_mxl_alloc ) 
+         ! 
+         ALLOCATE(hmld_tref(jpi,jpj))
          !
          CALL mpp_sum ( 'zdfmxl', zdf_mxl_alloc )
@@ -179,5 +195,5 @@
       !!----------------------------------------------------------------------------------- 
 
-      TYPE(MXL_ZINT), INTENT(in)  :: sf
+      TYPE(MXL_ZINT), DIMENSION(MAX_DIAG), INTENT(in)  :: sf
 
       ! Diagnostic criteria
@@ -202,132 +218,136 @@
       REAL, DIMENSION(jpi,jpj)    :: zdelta_T      ! difference critereon 
       REAL, DIMENSION(jpi,jpj)    :: zRHO1, zRHO2  ! Densities 
-      INTEGER :: ji, jj, jk                             ! loop counter 
+      INTEGER :: ji, jj, jk, jn                         ! loop counter 
 
       !!------------------------------------------------------------------------------------- 
       !  
       ! Unpack structure
-      nn_mld_type = sf%mld_type
-      rn_zref     = sf%zref
-      rn_dT_crit  = sf%dT_crit
-      rn_iso_frac = sf%iso_frac
-
-      ! Set the mixed layer depth criterion at each grid point 
-      IF( nn_mld_type == 0 ) THEN
-         zdelta_T(:,:) = rn_dT_crit
-         zT(:,:,:) = rhop(:,:,:)
-      ELSE IF( nn_mld_type == 1 ) THEN
-         ppzdep(:,:)=0.0 
-         call eos ( tsn(:,:,1,:), ppzdep(:,:), zRHO1(:,:) ) 
+      DO jn=1, nn_mld_diag
+         IF( cmld_zint(jn) .OR. cmld_mld(jn) ) THEN
+            nn_mld_type = sf(jn)%mld_type
+            rn_zref     = sf(jn)%zref
+            rn_dT_crit  = sf(jn)%dT_crit
+            rn_iso_frac = sf(jn)%iso_frac
+
+            ! Set the mixed layer depth criterion at each grid point 
+            IF( nn_mld_type == 0 ) THEN
+               zdelta_T(:,:) = rn_dT_crit
+               zT(:,:,:) = rhop(:,:,:)
+            ELSE IF( nn_mld_type == 1 ) THEN
+               ppzdep(:,:)=0.0 
+               call eos ( tsn(:,:,1,:), ppzdep(:,:), zRHO1(:,:) ) 
 ! Use zT temporarily as a copy of tsn with rn_dT_crit added to SST 
 ! [assumes number of tracers less than number of vertical levels] 
-         zT(:,:,1:jpts)=tsn(:,:,1,1:jpts) 
-         zT(:,:,jp_tem)=zT(:,:,1)+rn_dT_crit 
-         CALL eos( zT(:,:,1:jpts), ppzdep(:,:), zRHO2(:,:) ) 
-         zdelta_T(:,:) = abs( zRHO1(:,:) - zRHO2(:,:) ) * rau0 
-         ! RHO from eos (2d version) doesn't calculate north or east halo: 
-         CALL lbc_lnk( 'zdfmxl', zdelta_T, 'T', 1. ) 
-         zT(:,:,:) = rhop(:,:,:) 
-      ELSE 
-         zdelta_T(:,:) = rn_dT_crit                      
-         zT(:,:,:) = tsn(:,:,:,jp_tem)                           
-      END IF 
-
-      ! Calculate the gradient of zT and absolute difference for use later 
-      DO jk = 1 ,jpk-2 
-         zdTdz(:,:,jk)  =    ( zT(:,:,jk+1) - zT(:,:,jk) ) / e3w_n(:,:,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 rn_zref in the array of model level depths and find the ref    
-      ! density/temperature by linear interpolation.                                   
-      DO jk = jpkm1, 2, -1 
-         WHERE ( gdept_n(:,:,jk) > rn_zref ) 
-           ik_ref(:,:) = jk - 1 
-           zT_ref(:,:) = zT(:,:,jk-1) + zdTdz(:,:,jk-1) * ( rn_zref - gdept_n(:,:,jk-1) ) 
-         END WHERE 
-      END DO 
-
-      ! If the first grid box centre is below the reference level then use the 
-      ! top model level to get zT_ref 
-      WHERE ( gdept_n(:,:,1) > rn_zref )  
-         zT_ref = zT(:,:,1) 
-         ik_ref = 1 
-      END WHERE 
-
-      ! The number of active tracer levels is 1 less than the number of active w levels 
-      ikmt(:,:) = mbkt(:,:) - 1 
-
-      ! Initialize / reset
-      ll_found(:,:) = .false.
-
-      IF ( rn_iso_frac - zepsilon > 0. ) THEN
-         ! Search for a uniform density/temperature region where adjacent levels          
-         ! differ by less than rn_iso_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(:,:) 
-         DO jj = 1, nlcj 
-            DO ji = 1, nlci 
+               zT(:,:,1:jpts)=tsn(:,:,1,1:jpts) 
+               zT(:,:,jp_tem)=zT(:,:,1)+rn_dT_crit 
+               CALL eos( zT(:,:,1:jpts), ppzdep(:,:), zRHO2(:,:) ) 
+               zdelta_T(:,:) = abs( zRHO1(:,:) - zRHO2(:,:) ) * rau0 
+               ! RHO from eos (2d version) doesn't calculate north or east halo: 
+               CALL lbc_lnk( 'zdfmxl', zdelta_T, 'T', 1. ) 
+               zT(:,:,:) = rhop(:,:,:) 
+            ELSE 
+               zdelta_T(:,:) = rn_dT_crit                      
+               zT(:,:,:) = tsn(:,:,:,jp_tem)                           
+            END IF 
+
+            ! Calculate the gradient of zT and absolute difference for use later 
+            DO jk = 1 ,jpk-2 
+               zdTdz(:,:,jk)  =    ( zT(:,:,jk+1) - zT(:,:,jk) ) / e3w_n(:,:,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 rn_zref in the array of model level depths and find the ref    
+            ! density/temperature by linear interpolation.                                   
+            DO jk = jpkm1, 2, -1 
+               WHERE ( gdept_n(:,:,jk) > rn_zref ) 
+                 ik_ref(:,:) = jk - 1 
+                 zT_ref(:,:) = zT(:,:,jk-1) + zdTdz(:,:,jk-1) * ( rn_zref - gdept_n(:,:,jk-1) ) 
+               END WHERE 
+            END DO 
+
+            ! If the first grid box centre is below the reference level then use the 
+            ! top model level to get zT_ref 
+            WHERE ( gdept_n(:,:,1) > rn_zref )  
+               zT_ref = zT(:,:,1) 
+               ik_ref = 1 
+            END WHERE 
+
+            ! The number of active tracer levels is 1 less than the number of active w levels 
+            ikmt(:,:) = mbkt(:,:) - 1 
+
+            ! Initialize / reset
+            ll_found(:,:) = .false.
+
+            IF ( rn_iso_frac - zepsilon > 0. ) THEN
+               ! Search for a uniform density/temperature region where adjacent levels          
+               ! differ by less than rn_iso_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(:,:) 
+               DO jj = 1, nlcj 
+                  DO ji = 1, nlci 
 !CDIR NOVECTOR 
-               DO jk = ik_ref(ji,jj), ikmt(ji,jj)-1 
-                  IF ( zmoddT(ji,jj,jk) > ( rn_iso_frac * zdelta_T(ji,jj) ) ) THEN 
-                     ik_iso(ji,jj)   = jk 
-                     ll_found(ji,jj) = ( zmoddT(ji,jj,jk) > zdelta_T(ji,jj) ) 
-                     EXIT 
-                  END IF 
+                     DO jk = ik_ref(ji,jj), ikmt(ji,jj)-1 
+                        IF ( zmoddT(ji,jj,jk) > ( rn_iso_frac * zdelta_T(ji,jj) ) ) THEN 
+                           ik_iso(ji,jj)   = jk 
+                           ll_found(ji,jj) = ( zmoddT(ji,jj,jk) > zdelta_T(ji,jj) ) 
+                           EXIT 
+                        END IF 
+                     END DO 
+                  END DO 
+               END DO 
+
+               ! Use linear interpolation to find depth of mixed layer base where possible 
+               hmld_zint(:,:,jn) = rn_zref 
+               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_zint(ji,jj,jn) = gdept_n(ji,jj,ik_iso(ji,jj)) + zdz 
+                     END IF 
+                  END DO 
+               END DO 
+            END IF
+
+            ! 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. 
+            END WHERE 
+
+            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_zint(ji,jj,jn) = gdept_n(ji,jj,ikmt(ji,jj)) 
                END DO 
             END DO 
-         END DO 
-
-         ! Use linear interpolation to find depth of mixed layer base where possible 
-         hmld_zint(:,:) = rn_zref 
-         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_zint(ji,jj) = gdept_n(ji,jj,ik_iso(ji,jj)) + zdz 
-               END IF 
+
+            DO jj = 1, jpj 
+               DO ji = 1, jpi 
+!CDIR NOVECTOR 
+                  DO jk = ik_ref(ji,jj)+1, ikmt(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_zint(ji,jj,jn) = gdept_n(ji,jj,jk-1) + zdz 
+                        EXIT                                                   
+                     END IF 
+                  END DO 
+               END DO 
             END DO 
-         END DO 
-      END IF
-
-      ! 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. 
-      END WHERE 
- 
-      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_zint(ji,jj) = gdept_n(ji,jj,ikmt(ji,jj)) 
-         END DO 
-      END DO 
-
-      DO jj = 1, jpj 
-         DO ji = 1, jpi 
-!CDIR NOVECTOR 
-            DO jk = ik_ref(ji,jj)+1, ikmt(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_zint(ji,jj) = gdept_n(ji,jj,jk-1) + zdz 
-                  EXIT                                                   
-               END IF 
-            END DO 
-         END DO 
-      END DO 
-
-      hmld_zint(:,:) = hmld_zint(:,:)*tmask(:,:,1) 
+
+            hmld_zint(:,:,jn) = hmld_zint(:,:,jn)*tmask(:,:,1) 
+         END IF
+      END DO
       !  
    END SUBROUTINE zdf_mxl_zint_mld
@@ -344,5 +364,5 @@
       INTEGER, INTENT(in) ::   kt   ! ocean time-step index
 
-      INTEGER :: ji, jj, jk
+      INTEGER :: ji, jj, jk, jn
       INTEGER :: ikmax
       REAL(wp) :: zc, zcoef
@@ -360,53 +380,57 @@
       ENDIF
 
-      ! Find last whole model T level above the MLD
-      ilevel(:,:)   = 0
-      zthick_0(:,:) = 0._wp
-
-      DO jk = 1, jpkm1  
-         DO jj = 1, jpj
-            DO ji = 1, jpi                    
-               zthick_0(ji,jj) = zthick_0(ji,jj) + e3t_n(ji,jj,jk)
-               IF( zthick_0(ji,jj) < hmld_zint(ji,jj) )   ilevel(ji,jj) = jk
+      DO jn=1, nn_mld_diag
+         IF( cmld_mld(jn) ) THEN
+            ! Find last whole model T level above the MLD
+            ilevel(:,:)   = 0
+            zthick_0(:,:) = 0._wp
+
+            DO jk = 1, jpkm1  
+               DO jj = 1, jpj
+                  DO ji = 1, jpi                    
+                     zthick_0(ji,jj) = zthick_0(ji,jj) + e3t_n(ji,jj,jk)
+                     IF( zthick_0(ji,jj) < hmld_zint(ji,jj,jn) )   ilevel(ji,jj) = jk
+                  END DO
+               END DO
+               WRITE(numout,*) 'zthick_0(jk =',jk,') =',zthick_0(2,2)
+               WRITE(numout,*) 'gdepw_n(jk+1 =',jk+1,') =',gdepw_n(2,2,jk+1)
             END DO
-         END DO
-         WRITE(numout,*) 'zthick_0(jk =',jk,') =',zthick_0(2,2)
-         WRITE(numout,*) 'gdepw_n(jk+1 =',jk+1,') =',gdepw_n(2,2,jk+1)
+
+            ! Surface boundary condition
+            IF( ln_linssh ) THEN  ;   zthick(:,:) = sshn(:,:)   ;   htc_mld(:,:,jn) = tsn(:,:,1,jp_tem) * sshn(:,:) * tmask(:,:,1)
+            ELSE                  ;   zthick(:,:) = 0._wp       ;   htc_mld(:,:,jn) = 0._wp                                   
+            ENDIF
+
+            ! Deepest whole T level above the MLD
+            ikmax = MIN( MAXVAL( ilevel(:,:) ), jpkm1 )
+
+            ! Integration down to last whole model T level
+            DO jk = 1, ikmax
+               DO jj = 1, jpj
+                  DO ji = 1, jpi
+                     zc = e3t_n(ji,jj,jk) * REAL( MIN( MAX( 0, ilevel(ji,jj) - jk + 1 ) , 1  )  )    ! 0 below ilevel
+                     zthick(ji,jj) = zthick(ji,jj) + zc
+                     htc_mld(ji,jj,jn) = htc_mld(ji,jj,jn) + zc * tsn(ji,jj,jk,jp_tem) * tmask(ji,jj,jk)
+                  END DO
+               END DO
+            END DO
+
+            ! Subsequent partial T level
+            zthick(:,:) = hmld_zint(:,:,jn) - zthick(:,:)   !   remaining thickness to reach MLD
+
+            DO jj = 1, jpj
+               DO ji = 1, jpi
+                  htc_mld(ji,jj,jn) = htc_mld(ji,jj,jn) + tsn(ji,jj,ilevel(ji,jj)+1,jp_tem)  & 
+            &                      * MIN( e3t_n(ji,jj,ilevel(ji,jj)+1), zthick(ji,jj) ) * tmask(ji,jj,ilevel(ji,jj)+1)
+               END DO
+            END DO
+
+            WRITE(numout,*) 'htc_mld(after) =',htc_mld(2,2,jn)
+
+            ! Convert to heat content
+            zcoef = rau0 * rcp
+            htc_mld(:,:,jn) = zcoef * htc_mld(:,:,jn)
+         END IF
       END DO
-
-      ! Surface boundary condition
-      IF( ln_linssh ) THEN  ;   zthick(:,:) = sshn(:,:)   ;   htc_mld(:,:) = tsn(:,:,1,jp_tem) * sshn(:,:) * tmask(:,:,1)   
-      ELSE                  ;   zthick(:,:) = 0._wp       ;   htc_mld(:,:) = 0._wp                                   
-      ENDIF
-
-      ! Deepest whole T level above the MLD
-      ikmax = MIN( MAXVAL( ilevel(:,:) ), jpkm1 )
-
-      ! Integration down to last whole model T level
-      DO jk = 1, ikmax
-         DO jj = 1, jpj
-            DO ji = 1, jpi
-               zc = e3t_n(ji,jj,jk) * REAL( MIN( MAX( 0, ilevel(ji,jj) - jk + 1 ) , 1  )  )    ! 0 below ilevel
-               zthick(ji,jj) = zthick(ji,jj) + zc
-               htc_mld(ji,jj) = htc_mld(ji,jj) + zc * tsn(ji,jj,jk,jp_tem) * tmask(ji,jj,jk)
-            END DO
-         END DO
-      END DO
-
-      ! Subsequent partial T level
-      zthick(:,:) = hmld_zint(:,:) - zthick(:,:)   !   remaining thickness to reach MLD
-
-      DO jj = 1, jpj
-         DO ji = 1, jpi
-            htc_mld(ji,jj) = htc_mld(ji,jj) + tsn(ji,jj,ilevel(ji,jj)+1,jp_tem)  & 
-      &                      * MIN( e3t_n(ji,jj,ilevel(ji,jj)+1), zthick(ji,jj) ) * tmask(ji,jj,ilevel(ji,jj)+1)
-         END DO
-      END DO
-
-      WRITE(numout,*) 'htc_mld(after) =',htc_mld(2,2)
-
-      ! Convert to heat content
-      zcoef = rau0 * rcp
-      htc_mld(:,:) = zcoef * htc_mld(:,:)
 
    END SUBROUTINE zdf_mxl_zint_htc
@@ -426,12 +450,8 @@
       INTEGER :: jn
 
-      INTEGER :: nn_mld_diag = 0    ! number of diagnostics
-
       CHARACTER(len=1) :: cmld
-
-      TYPE(MXL_ZINT) :: sn_mld1, sn_mld2, sn_mld3, sn_mld4, sn_mld5
-      TYPE(MXL_ZINT), SAVE, DIMENSION(5) ::   mld_diags
-
-      NAMELIST/namzdf_mldzint/ nn_mld_diag, sn_mld1, sn_mld2, sn_mld3, sn_mld4, sn_mld5
+      TYPE(MXL_ZINT), SAVE, DIMENSION(MAX_DIAG) ::   mld_diags
+
+      NAMELIST/namzdf_mldzint/ nn_mld_diag, mld_diags
 
       !!----------------------------------------------------------------------
@@ -447,21 +467,28 @@
          IF(lwm) WRITE ( numond, namzdf_mldzint )
 
-         IF( nn_mld_diag > 5 )   CALL ctl_stop( 'STOP', 'zdf_mxl_ini: Specify no more than 5 MLD definitions' )
-
-         mld_diags(1) = sn_mld1
-         mld_diags(2) = sn_mld2
-         mld_diags(3) = sn_mld3
-         mld_diags(4) = sn_mld4
-         mld_diags(5) = sn_mld5
-
-         IF( lwp .AND. (nn_mld_diag > 0) ) THEN
-            WRITE(numout,*) '=============== Vertically-interpolated mixed layer ================'
-            WRITE(numout,*) '(Diagnostic number, nn_mld_type, rn_zref, rn_dT_crit, rn_iso_frac)'
+         WRITE(cmld,'(I1)') MAX_DIAG 
+         IF( nn_mld_diag > MAX_DIAG )   CALL ctl_stop( 'STOP', 'zdf_mxl_ini: Specify no more than ', 'cmld', ' MLD definitions' )
+
+         cmld_zint=.false.
+         cmld_mld=.false.
+         IF( nn_mld_diag > 0 ) THEN
+            IF( lwp ) THEN
+               WRITE(numout,*) '=============== Vertically-interpolated mixed layer ================'
+               WRITE(numout,*) '(Diagnostic number, nn_mld_type, rn_zref, rn_dT_crit, rn_iso_frac)'
+            END IF
+
             DO jn = 1, nn_mld_diag
-               WRITE(numout,*) 'MLD criterion',jn,':'
-               WRITE(numout,*) '    nn_mld_type =', mld_diags(jn)%mld_type
-               WRITE(numout,*) '    rn_zref ='    , mld_diags(jn)%zref
-               WRITE(numout,*) '    rn_dT_crit =' , mld_diags(jn)%dT_crit
-               WRITE(numout,*) '    rn_iso_frac =', mld_diags(jn)%iso_frac
+               ! Check if the diagnostics is being written to the output
+               WRITE(cmld,'(I1)') jn
+               IF( iom_use( "mldzint_"//cmld ) ) cmld_zint(jn)=.true. 
+               IF( iom_use( "mldhtc_"//cmld ) )  cmld_mld(jn) =.true. 
+
+               IF( lwp ) THEN
+                  WRITE(numout,*) 'MLD criterion',jn,':'
+                  WRITE(numout,*) '    nn_mld_type =', mld_diags(jn)%mld_type
+                  WRITE(numout,*) '    rn_zref ='    , mld_diags(jn)%zref
+                  WRITE(numout,*) '    rn_dT_crit =' , mld_diags(jn)%dT_crit
+                  WRITE(numout,*) '    rn_iso_frac =', mld_diags(jn)%iso_frac
+               END IF
             END DO
             WRITE(numout,*) '===================================================================='
@@ -470,17 +497,15 @@
 
       IF( nn_mld_diag > 0 ) THEN
+         CALL zdf_mxl_zint_mld( mld_diags )
+         CALL zdf_mxl_zint_htc( kt )
+
          DO jn = 1, nn_mld_diag
             WRITE(cmld,'(I1)') jn
-            IF( iom_use( "mldzint_"//cmld ) .OR. iom_use( "mldhtc_"//cmld ) ) THEN
-               CALL zdf_mxl_zint_mld( mld_diags(jn) )
-
-               IF( iom_use( "mldzint_"//cmld ) ) THEN
-                  CALL iom_put( "mldzint_"//cmld, hmld_zint(:,:) )
-               ENDIF
-
-               IF( iom_use( "mldhtc_"//cmld ) )  THEN
-                  CALL zdf_mxl_zint_htc( kt )
-                  CALL iom_put( "mldhtc_"//cmld , htc_mld(:,:)   )
-               ENDIF
+            IF( cmld_zint(jn) ) THEN
+               CALL iom_put( "mldzint_"//cmld, hmld_zint(:,:,jn) )
+            ENDIF
+
+            IF( cmld_mld(jn) )  THEN
+               CALL iom_put( "mldhtc_"//cmld , htc_mld(:,:,jn)   )
             ENDIF
          END DO
