Index: branches/UKMO/dev_r5518_GO6_package_asm_3d_bgc/NEMOGCM/NEMO/OPA_SRC/ASM/asmbgc.F90
===================================================================
--- branches/UKMO/dev_r5518_GO6_package_asm_3d_bgc/NEMOGCM/NEMO/OPA_SRC/ASM/asmbgc.F90	(revision 9431)
+++ branches/UKMO/dev_r5518_GO6_package_asm_3d_bgc/NEMOGCM/NEMO/OPA_SRC/ASM/asmbgc.F90	(revision 9432)
@@ -57,6 +57,6 @@
 #endif
 #if defined key_medusa && defined key_foam_medusa
-   USE asmlogchlbal_medusa, ONLY: & ! logchl balancing for MEDUSA
-      & asm_logchl_bal_medusa
+   USE asmphyto2dbal_medusa, ONLY: & ! phyto2d balancing for MEDUSA
+      & asm_phyto2d_bal_medusa
    USE asmpco2bal, ONLY:    & ! pCO2 balancing for MEDUSA
       & asm_pco2_bal
@@ -73,6 +73,6 @@
       & mld_max
 #elif defined key_hadocc
-   USE asmlogchlbal_hadocc, ONLY: & ! logchl balancing for HadOCC
-      & asm_logchl_bal_hadocc
+   USE asmphyto2dbal_hadocc, ONLY: & ! phyto2d balancing for HadOCC
+      & asm_phyto2d_bal_hadocc
    USE asmpco2bal, ONLY:    & ! pCO2 balancing for HadOCC
       & asm_pco2_bal
@@ -996,5 +996,5 @@
             CASE ( 4 )                   ! Temperature criterion (0.2 K change from surface) [T points]
                !zmld(:,:) = hmld_tref(:,:)
-               CALL ctl_stop( ' hmld_tref mixed layer requested for LogChl assimilation,', &
+               CALL ctl_stop( ' hmld_tref mixed layer requested for phyto2d assimilation,', &
                   &           ' but is not available in this version' )
             CASE ( 5 )                   ! Density criterion (0.01 kg/m^3 change from 10m) [T points]
@@ -1006,32 +1006,32 @@
 
 #if defined key_medusa && defined key_foam_medusa
-         CALL asm_logchl_bal_medusa( (ln_slchltotinc .OR. ln_schltotinc), &
-            &                        zinc_chltot,                         &
-            &                        ln_slchldiainc,                      &
-            &                        zinc_chldia,                         &
-            &                        ln_slchlnoninc,                      &
-            &                        zinc_chlnon,                         &
-            &                        ln_slphytotinc,                      &
-            &                        zinc_phytot,                         &
-            &                        ln_slphydiainc,                      &
-            &                        zinc_phydia,                         &
-            &                        ln_slphynoninc,                      &
-            &                        zinc_phynon,                         &
-            &                        zincper,                             &
-            &                        rn_maxchlinc, ln_phytobal, zmld,     &
-            &                        pgrow_avg_bkg, ploss_avg_bkg,        &
-            &                        phyt_avg_bkg, mld_max_bkg,           &
-            &                        tracer_bkg, phyto2d_balinc )
+         CALL asm_phyto2d_bal_medusa( (ln_slchltotinc .OR. ln_schltotinc), &
+            &                         zinc_chltot,                         &
+            &                         ln_slchldiainc,                      &
+            &                         zinc_chldia,                         &
+            &                         ln_slchlnoninc,                      &
+            &                         zinc_chlnon,                         &
+            &                         ln_slphytotinc,                      &
+            &                         zinc_phytot,                         &
+            &                         ln_slphydiainc,                      &
+            &                         zinc_phydia,                         &
+            &                         ln_slphynoninc,                      &
+            &                         zinc_phynon,                         &
+            &                         zincper,                             &
+            &                         rn_maxchlinc, ln_phytobal, zmld,     &
+            &                         pgrow_avg_bkg, ploss_avg_bkg,        &
+            &                         phyt_avg_bkg, mld_max_bkg,           &
+            &                         tracer_bkg, phyto2d_balinc )
 #elif defined key_hadocc
-         CALL asm_logchl_bal_hadocc( (ln_slchltotinc .OR. ln_schltotinc), &
-            &                        zinc_chltot,                         &
-            &                        ln_slphytotinc,                      &
-            &                        zinc_phytot,                         &
-            &                        zincper,                             &
-            &                        rn_maxchlinc, ln_phytobal, zmld,     &
-            &                        pgrow_avg_bkg, ploss_avg_bkg,        &
-            &                        phyt_avg_bkg, mld_max_bkg,           &
-            &                        cchl_p_bkg(:,:,1),                   &
-            &                        tracer_bkg, phyto2d_balinc )
+         CALL asm_phyto2d_bal_hadocc( (ln_slchltotinc .OR. ln_schltotinc), &
+            &                         zinc_chltot,                         &
+            &                         ln_slphytotinc,                      &
+            &                         zinc_phytot,                         &
+            &                         zincper,                             &
+            &                         rn_maxchlinc, ln_phytobal, zmld,     &
+            &                         pgrow_avg_bkg, ploss_avg_bkg,        &
+            &                         phyt_avg_bkg, mld_max_bkg,           &
+            &                         cchl_p_bkg(:,:,1),                   &
+            &                         tracer_bkg, phyto2d_balinc )
 #else
          CALL ctl_stop( 'Attempting to assimilate phyto2d data, ', &
Index: branches/UKMO/dev_r5518_GO6_package_asm_3d_bgc/NEMOGCM/NEMO/OPA_SRC/ASM/asmlogchlbal_hadocc.F90
===================================================================
--- branches/UKMO/dev_r5518_GO6_package_asm_3d_bgc/NEMOGCM/NEMO/OPA_SRC/ASM/asmlogchlbal_hadocc.F90	(revision 9431)
+++ 	(revision )
@@ -1,285 +1,0 @@
-MODULE asmlogchlbal_hadocc
-   !!======================================================================
-   !!                       ***  MODULE asmlogchlbal_hadocc  ***
-   !! Calculate increments to HadOCC based on surface logchl increments
-   !!
-   !! IMPORTANT NOTE: This calls the bioanalysis routine of Hemmings et al.
-   !! For licensing reasons this is kept in its own internal Met Office
-   !! branch (dev/frdf/vn3.6_nitrogen_balancing) rather than in the Paris
-   !! repository, and must be merged in when building.
-   !!
-   !!======================================================================
-   !! History :  3.6  ! 2017-08  (D. Ford)     Adapted from bioanal.F90
-   !!----------------------------------------------------------------------
-#if defined key_asminc && defined key_hadocc
-   !!----------------------------------------------------------------------
-   !! 'key_asminc'          : assimilation increment interface
-   !! 'key_hadocc'          : HadOCC model
-   !!----------------------------------------------------------------------
-   !! asm_logchl_bal_hadocc : routine to calculate increments to HadOCC
-   !!----------------------------------------------------------------------
-   USE par_kind,      ONLY: wp             ! kind parameters
-   USE par_oce,       ONLY: jpi, jpj, jpk  ! domain array sizes
-   USE dom_oce,       ONLY: gdepw_n        ! domain information
-   USE iom                                 ! i/o
-   USE par_hadocc                          ! HadOCC parameters
-   USE had_bgc_stnd,  ONLY: kmt            ! HadOCC parameters
-   USE had_bgc_const                       ! HadOCC parameters
-   USE par_trc,       ONLY: jptra          ! Tracer parameters
-   USE bioanalysis                         ! Nitrogen balancing
-
-   IMPLICIT NONE
-   PRIVATE                   
-
-   PUBLIC asm_logchl_bal_hadocc
-
-   ! Default values for biological assimilation parameters
-   ! Should match Hemmings et al. (2008)
-   REAL(wp), PARAMETER :: balnutext  =  0.6    !: Default nutrient balancing factor
-   REAL(wp), PARAMETER :: balnutmin  =  0.1    !: Fraction of phytoplankton loss to nutrient
-   REAL(wp), PARAMETER :: r          =  1      !: Reliability of model specific growth rate
-   REAL(wp), PARAMETER :: beta_g     =  0.05   !: Low rate bias correction for growth rate estimator
-   REAL(wp), PARAMETER :: beta_l     =  0.05   !: Low rate bias correction for primary loss rate estimator
-   REAL(wp), PARAMETER :: beta_m     =  0.05   !: Low rate bias correction for secondary loss rate estimator
-   REAL(wp), PARAMETER :: a_g        =  0.2    !: Error s.d. for log10 of growth rate estimator
-   REAL(wp), PARAMETER :: a_l        =  0.4    !: Error s.d. for log10 of primary loss rate estimator
-   REAL(wp), PARAMETER :: a_m        =  0.7    !: Error s.d. for log10 of secondary loss rate estimator
-   REAL(wp), PARAMETER :: zfracb0    =  0.7    !: Base zooplankton fraction of loss to Z & D
-   REAL(wp), PARAMETER :: zfracb1    =  0      !: Phytoplankton sensitivity of zooplankton fraction
-   REAL(wp), PARAMETER :: qrfmax     =  1.1    !: Maximum nutrient limitation reduction factor
-   REAL(wp), PARAMETER :: qafmax     =  1.1    !: Maximum nutrient limitation amplification factor
-   REAL(wp), PARAMETER :: zrfmax     =  2      !: Maximum zooplankton reduction factor
-   REAL(wp), PARAMETER :: zafmax     =  2      !: Maximum zooplankton amplification factor
-   REAL(wp), PARAMETER :: prfmax     =  10     !: Maximum phytoplankton reduction factor (secondary)
-   REAL(wp), PARAMETER :: incphymin  =  0.0001 !: Minimum size of non-zero phytoplankton increment
-   REAL(wp), PARAMETER :: integnstep =  20     !: Number of steps for p.d.f. integral evaluation
-   REAL(wp), PARAMETER :: pthreshold =  0.01   !: Fractional threshold level for setting p.d.f.
-   !
-   LOGICAL,  PARAMETER :: diag_active           = .TRUE.  !: Depth-independent diagnostics
-   LOGICAL,  PARAMETER :: diag_fulldepth_active = .TRUE.  !: Full-depth diagnostics
-   LOGICAL,  PARAMETER :: gl_active             = .TRUE.  !: Growth/loss-based balancing
-   LOGICAL,  PARAMETER :: nbal_active           = .TRUE.  !: Nitrogen balancing
-   LOGICAL,  PARAMETER :: subsurf_active        = .TRUE.  !: Increments below MLD
-   LOGICAL,  PARAMETER :: deepneg_active        = .FALSE. !: Negative primary increments below MLD
-   LOGICAL,  PARAMETER :: deeppos_active        = .FALSE. !: Positive primary increments below MLD
-   LOGICAL,  PARAMETER :: nutprof_active        = .TRUE.  !: Secondary increments
-
-CONTAINS
-
-   SUBROUTINE asm_logchl_bal_hadocc( ld_chltot,                      &
-      &                              pinc_chltot,                    &
-      &                              ld_phytot,                      &
-      &                              pinc_phytot,                    &
-      &                              pincper,                        &
-      &                              p_maxchlinc, ld_phytobal, pmld, &
-      &                              pgrow_avg_bkg, ploss_avg_bkg,   &
-      &                              phyt_avg_bkg, mld_max_bkg,      &
-      &                              cchl_p_bkg,                     &
-      &                              tracer_bkg, phyto2d_balinc )
-      !!---------------------------------------------------------------------------
-      !!                    ***  ROUTINE asm_logchl_bal_hadocc  ***
-      !!
-      !! ** Purpose :   calculate increments to HadOCC from 2d phytoplankton increments
-      !!
-      !! ** Method  :   call nitrogen balancing scheme
-      !!
-      !! ** Action  :   populate phyto2d_balinc
-      !!
-      !! References :   Hemmings et al., 2008, J. Mar. Res.
-      !!                Ford et al., 2012, Ocean Sci.
-      !!---------------------------------------------------------------------------
-      !!
-      LOGICAL,  INTENT(in   )                               :: ld_chltot      ! Assim chltot y/n
-      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_chltot    ! chltot increments
-      LOGICAL,  INTENT(in   )                               :: ld_phytot      ! Assim phytot y/n
-      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_phytot    ! phytot increments
-      REAL(wp), INTENT(in   )                               :: pincper        ! Assimilation period
-      REAL(wp), INTENT(in   )                               :: p_maxchlinc    ! Max chl increment
-      LOGICAL,  INTENT(in   )                               :: ld_phytobal    ! Balancing y/n
-      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pmld           ! Mixed layer depth
-      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: pgrow_avg_bkg  ! Avg phyto growth
-      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: ploss_avg_bkg  ! Avg phyto loss
-      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: phyt_avg_bkg   ! Avg phyto
-      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: mld_max_bkg    ! Max MLD
-      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: cchl_p_bkg     ! Surface C:Chl
-      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj,jpk,jptra) :: tracer_bkg     ! State variables
-      REAL(wp), INTENT(  out), DIMENSION(jpi,jpj,jpk,jptra) :: phyto2d_balinc ! Balancing increments
-      !!
-      INTEGER                                               :: ji, jj, jk, jn ! Loop counters
-      INTEGER                                               :: jkmax          ! Loop index
-      INTEGER,                 DIMENSION(6)                 :: i_tracer       ! Tracer indices
-      REAL(wp),                DIMENSION(16)                :: modparm        ! Model parameters
-      REAL(wp),                DIMENSION(20)                :: assimparm      ! Assimilation parameters
-      REAL(wp),                DIMENSION(jpi,jpj,jpk,6)     :: bstate         ! Background state
-      REAL(wp),                DIMENSION(jpi,jpj,jpk,6)     :: outincs        ! Balancing increments
-      REAL(wp),                DIMENSION(jpi,jpj,22)        :: diag           ! Depth-indep diagnostics
-      REAL(wp),                DIMENSION(jpi,jpj,jpk,22)    :: diag_fulldepth ! Full-depth diagnostics
-      !!---------------------------------------------------------------------------
-
-      IF ( ld_chltot ) THEN
-         ! If p_maxchlinc > 0 then cap total absolute chlorophyll increment at that value
-         DO jj = 1, jpj
-            DO ji = 1, jpi
-               IF ( p_maxchlinc > 0.0 ) THEN
-                  pinc_chltot(ji,jj) = MAX( -1.0 * p_maxchlinc, MIN( pinc_chltot(ji,jj), p_maxchlinc ) )
-               ENDIF
-            END DO
-         END DO
-      ELSE
-         CALL ctl_stop( ' No PFT assimilation quite yet' )
-      ENDIF
-      
-      IF ( ld_phytobal ) THEN   ! Nitrogen balancing
-
-         ! Set up model parameters to be passed into Hemmings balancing routine
-         modparm(1)  = grow_sat
-         modparm(2)  = psmax
-         modparm(3)  = par
-         modparm(4)  = alpha
-         modparm(5)  = resp_rate
-         modparm(6)  = pmort_rate
-         modparm(7)  = phyto_min
-         modparm(8)  = z_mort_1
-         modparm(9)  = z_mort_2
-         modparm(10) = c2n_p
-         modparm(11) = c2n_z
-         modparm(12) = c2n_d
-         modparm(13) = graze_threshold
-         modparm(14) = holling_coef
-         modparm(15) = graze_sat
-         modparm(16) = graze_max
-
-         ! Set up assimilation parameters to be passed into balancing routine
-         ! Not sure what assimparm(1) is meant to be, but it doesn't get used
-         assimparm(2)  = balnutext
-         assimparm(3)  = balnutmin
-         assimparm(4)  = r
-         assimparm(5)  = beta_g
-         assimparm(6)  = beta_l
-         assimparm(7)  = beta_m
-         assimparm(8)  = a_g
-         assimparm(9)  = a_l
-         assimparm(10) = a_m
-         assimparm(11) = zfracb0
-         assimparm(12) = zfracb1
-         assimparm(13) = qrfmax
-         assimparm(14) = qafmax
-         assimparm(15) = zrfmax
-         assimparm(16) = zafmax
-         assimparm(17) = prfmax
-         assimparm(18) = incphymin
-         assimparm(19) = integnstep
-         assimparm(20) = pthreshold
-
-         ! Set up external tracer indices array bstate
-         i_tracer(1) = 1   ! nutrient
-         i_tracer(2) = 2   ! phytoplankton
-         i_tracer(3) = 3   ! zooplankton
-         i_tracer(4) = 4   ! detritus
-         i_tracer(5) = 5   ! DIC
-         i_tracer(6) = 6   ! Alkalinity
-
-         ! Set background state
-         bstate(:,:,:,i_tracer(1)) = tracer_bkg(:,:,:,jp_had_nut)
-         bstate(:,:,:,i_tracer(2)) = tracer_bkg(:,:,:,jp_had_phy)
-         bstate(:,:,:,i_tracer(3)) = tracer_bkg(:,:,:,jp_had_zoo)
-         bstate(:,:,:,i_tracer(4)) = tracer_bkg(:,:,:,jp_had_pdn)
-         bstate(:,:,:,i_tracer(5)) = tracer_bkg(:,:,:,jp_had_dic)
-         bstate(:,:,:,i_tracer(6)) = tracer_bkg(:,:,:,jp_had_alk)
-
-         ! Call nitrogen balancing routine
-         CALL bio_analysis( jpi, jpj, jpk, ZDZ(:,:,:), i_tracer, modparm,               &
-            &               n2be_p, n2be_z, n2be_d, assimparm,                          &
-            &               INT(pincper), 1, kmt(:,:), tmask(:,:,:),                    &
-            &               pmld(:,:), mld_max_bkg(:,:), pinc_chltot(:,:), cchl_p_bkg(:,:), &
-            &               nbal_active, phyt_avg_bkg(:,:),                             &
-            &               gl_active, pgrow_avg_bkg(:,:), ploss_avg_bkg(:,:),          &
-            &               subsurf_active, deepneg_active,                             &
-            &               deeppos_active, nutprof_active,                             &
-            &               bstate, outincs,                                            &
-            &               diag_active, diag,                                          &
-            &               diag_fulldepth_active, diag_fulldepth )
-
-         ! Save balancing increments
-         phyto2d_balinc(:,:,:,jp_had_nut) = outincs(:,:,:,i_tracer(1))
-         phyto2d_balinc(:,:,:,jp_had_phy) = outincs(:,:,:,i_tracer(2))
-         phyto2d_balinc(:,:,:,jp_had_zoo) = outincs(:,:,:,i_tracer(3))
-         phyto2d_balinc(:,:,:,jp_had_pdn) = outincs(:,:,:,i_tracer(4))
-         phyto2d_balinc(:,:,:,jp_had_dic) = outincs(:,:,:,i_tracer(5))
-         phyto2d_balinc(:,:,:,jp_had_alk) = outincs(:,:,:,i_tracer(6))
-      
-      ELSE   ! No nitrogen balancing
-      
-         ! Initialise phytoplankton increment to zero
-         phyto2d_balinc(:,:,:,jp_had_phy) = 0.0
-         
-         ! Convert surface chlorophyll increment to phytoplankton nitrogen
-         phyto2d_balinc(:,:,1,jp_had_phy) = ( cchl_p_bkg(:,:) / (mw_carbon * c2n_p) ) * pinc_chltot(:,:)
-         
-         ! Propagate through mixed layer
-         DO jj = 1, jpj
-            DO ji = 1, jpi
-               !
-               jkmax = jpk-1
-               DO jk = jpk-1, 1, -1
-                  IF ( ( pmld(ji,jj) >  gdepw_n(ji,jj,jk)   ) .AND. &
-                     & ( pmld(ji,jj) <= gdepw_n(ji,jj,jk+1) ) ) THEN
-                     pmld(ji,jj) = gdepw_n(ji,jj,jk+1)
-                     jkmax = jk
-                  ENDIF
-               END DO
-               !
-               DO jk = 2, jkmax
-                  phyto2d_balinc(ji,jj,jk,jp_had_phy) = phyto2d_balinc(ji,jj,1,jp_had_phy)
-               END DO
-               !
-            END DO
-         END DO
-
-         ! Set other balancing increments to zero
-         phyto2d_balinc(:,:,:,jp_had_nut) = 0.0
-         phyto2d_balinc(:,:,:,jp_had_zoo) = 0.0
-         phyto2d_balinc(:,:,:,jp_had_pdn) = 0.0
-         phyto2d_balinc(:,:,:,jp_had_dic) = 0.0
-         phyto2d_balinc(:,:,:,jp_had_alk) = 0.0
-      
-      ENDIF
-
-   END SUBROUTINE asm_logchl_bal_hadocc
-
-#else
-   !!----------------------------------------------------------------------
-   !!   Default option : Empty routine
-   !!----------------------------------------------------------------------
-CONTAINS
-   SUBROUTINE asm_logchl_bal_hadocc( ld_chltot,                      &
-      &                              pinc_chltot,                    &
-      &                              ld_phytot,                      &
-      &                              pinc_phytot,                    &
-      &                              pincper,                        &
-      &                              p_maxchlinc, ld_phytobal, pmld, &
-      &                              pgrow_avg_bkg, ploss_avg_bkg,   &
-      &                              phyt_avg_bkg, mld_max_bkg,      &
-      &                              cchl_p_bkg,                     &
-      &                              tracer_bkg, phyto2d_balinc )
-      LOGICAL :: ld_chltot
-      REAL    :: pinc_chltot(:,:)
-      LOGICAL :: ld_phytot
-      REAL    :: pinc_phytot(:,:)
-      REAL    :: pincper
-      REAL    :: p_maxchlinc
-      LOGICAL :: ld_phytobal
-      REAL    :: pmld(:,:)
-      REAL    :: pgrow_avg_bkg(:,:)
-      REAL    :: ploss_avg_bkg(:,:)
-      REAL    :: phyt_avg_bkg(:,:)
-      REAL    :: mld_max_bkg(:,:)
-      REAL    :: cchl_p_bkg(:,:)
-      REAL    :: tracer_bkg(:,:,:,:)
-      REAL    :: phyto2d_balinc(:,:,:,:)
-      WRITE(*,*) 'asm_logchl_bal_hadocc: You should not have seen this print! error?'
-   END SUBROUTINE asm_logchl_bal_hadocc
-#endif
-
-   !!======================================================================
-END MODULE asmlogchlbal_hadocc
Index: branches/UKMO/dev_r5518_GO6_package_asm_3d_bgc/NEMOGCM/NEMO/OPA_SRC/ASM/asmlogchlbal_medusa.F90
===================================================================
--- branches/UKMO/dev_r5518_GO6_package_asm_3d_bgc/NEMOGCM/NEMO/OPA_SRC/ASM/asmlogchlbal_medusa.F90	(revision 9431)
+++ 	(revision )
@@ -1,440 +1,0 @@
-MODULE asmlogchlbal_medusa
-   !!======================================================================
-   !!                       ***  MODULE asmlogchlbal_medusa  ***
-   !! Calculate increments to MEDUSA based on surface logchl increments
-   !!
-   !! IMPORTANT NOTE: This calls the bioanalysis routine of Hemmings et al.
-   !! For licensing reasons this is kept in its own internal Met Office
-   !! branch (dev/frdf/vn3.6_nitrogen_balancing) rather than in the Paris
-   !! repository, and must be merged in when building.
-   !!
-   !!======================================================================
-   !! History :  3.6  ! 2017-08 (D. Ford)  Adapted from asmlogchlbal_hadocc
-   !!----------------------------------------------------------------------
-#if defined key_asminc && defined key_medusa && defined key_foam_medusa
-   !!----------------------------------------------------------------------
-   !! 'key_asminc'          : assimilation increment interface
-   !! 'key_medusa'          : MEDUSA model
-   !! 'key_foam_medusa'     : MEDUSA extras for FOAM OBS and ASM
-   !!----------------------------------------------------------------------
-   !! asm_logchl_bal_medusa : routine to calculate increments to MEDUSA
-   !!----------------------------------------------------------------------
-   USE par_kind,      ONLY: wp             ! kind parameters
-   USE par_oce,       ONLY: jpi, jpj, jpk  ! domain array sizes
-   USE dom_oce,       ONLY: gdepw_n        ! domain information
-   USE zdftmx,        ONLY: ln_tmx_itf, &  ! Indonesian Throughflow
-      &                     mask_itf       ! tidal mixing mask
-   USE iom                                 ! i/o
-   USE sms_medusa                          ! MEDUSA parameters
-   USE par_medusa                          ! MEDUSA parameters
-   USE par_trc,       ONLY: jptra          ! Tracer parameters
-   USE bioanalysis                         ! Nitrogen balancing
-
-   IMPLICIT NONE
-   PRIVATE                   
-
-   PUBLIC asm_logchl_bal_medusa
-
-   ! Default values for biological assimilation parameters
-   ! Should match Hemmings et al. (2008)
-   REAL(wp), PARAMETER :: balnutext  =  0.6    !: Default nutrient balancing factor
-   REAL(wp), PARAMETER :: balnutmin  =  0.1    !: Fraction of phytoplankton loss to nutrient
-   REAL(wp), PARAMETER :: r          =  1      !: Reliability of model specific growth rate
-   REAL(wp), PARAMETER :: beta_g     =  0.05   !: Low rate bias correction for growth rate estimator
-   REAL(wp), PARAMETER :: beta_l     =  0.05   !: Low rate bias correction for primary loss rate estimator
-   REAL(wp), PARAMETER :: beta_m     =  0.05   !: Low rate bias correction for secondary loss rate estimator
-   REAL(wp), PARAMETER :: a_g        =  0.2    !: Error s.d. for log10 of growth rate estimator
-   REAL(wp), PARAMETER :: a_l        =  0.4    !: Error s.d. for log10 of primary loss rate estimator
-   REAL(wp), PARAMETER :: a_m        =  0.7    !: Error s.d. for log10 of secondary loss rate estimator
-   REAL(wp), PARAMETER :: zfracb0    =  0.7    !: Base zooplankton fraction of loss to Z & D
-   REAL(wp), PARAMETER :: zfracb1    =  0      !: Phytoplankton sensitivity of zooplankton fraction
-   REAL(wp), PARAMETER :: qrfmax     =  1.1    !: Maximum nutrient limitation reduction factor
-   REAL(wp), PARAMETER :: qafmax     =  1.1    !: Maximum nutrient limitation amplification factor
-   REAL(wp), PARAMETER :: zrfmax     =  2      !: Maximum zooplankton reduction factor
-   REAL(wp), PARAMETER :: zafmax     =  2      !: Maximum zooplankton amplification factor
-   REAL(wp), PARAMETER :: prfmax     =  10     !: Maximum phytoplankton reduction factor (secondary)
-   REAL(wp), PARAMETER :: incphymin  =  0.0001 !: Minimum size of non-zero phytoplankton increment
-   REAL(wp), PARAMETER :: integnstep =  20     !: Number of steps for p.d.f. integral evaluation
-   REAL(wp), PARAMETER :: pthreshold =  0.01   !: Fractional threshold level for setting p.d.f.
-   !
-   LOGICAL,  PARAMETER :: diag_active           = .TRUE.  !: Depth-independent diagnostics
-   LOGICAL,  PARAMETER :: diag_fulldepth_active = .TRUE.  !: Full-depth diagnostics
-   LOGICAL,  PARAMETER :: gl_active             = .TRUE.  !: Growth/loss-based balancing
-   LOGICAL,  PARAMETER :: nbal_active           = .TRUE.  !: Nitrogen balancing
-   LOGICAL,  PARAMETER :: subsurf_active        = .TRUE.  !: Increments below MLD
-   LOGICAL,  PARAMETER :: deepneg_active        = .FALSE. !: Negative primary increments below MLD
-   LOGICAL,  PARAMETER :: deeppos_active        = .FALSE. !: Positive primary increments below MLD
-   LOGICAL,  PARAMETER :: nutprof_active        = .TRUE.  !: Secondary increments
-
-CONTAINS
-
-   SUBROUTINE asm_logchl_bal_medusa( ld_chltot,                      &
-      &                              pinc_chltot,                    &
-      &                              ld_chldia,                      &
-      &                              pinc_chldia,                    &
-      &                              ld_chlnon,                      &
-      &                              pinc_chlnon,                    &
-      &                              ld_phytot,                      &
-      &                              pinc_phytot,                    &
-      &                              ld_phydia,                      &
-      &                              pinc_phydia,                    &
-      &                              ld_phynon,                      &
-      &                              pinc_phynon,                    &
-      &                              pincper,                        &
-      &                              p_maxchlinc, ld_phytobal, pmld, &
-      &                              pgrow_avg_bkg, ploss_avg_bkg,   &
-      &                              phyt_avg_bkg, mld_max_bkg,      &
-      &                              tracer_bkg, phyto2d_balinc )
-      !!---------------------------------------------------------------------------
-      !!                    ***  ROUTINE asm_logchl_bal_medusa  ***
-      !!
-      !! ** Purpose :   calculate increments to MEDUSA from 2d phytoplankton increments
-      !!
-      !! ** Method  :   average up MEDUSA to look like HadOCC
-      !!                call nitrogen balancing scheme
-      !!                separate back out to MEDUSA
-      !!
-      !! ** Action  :   populate phyto2d_balinc
-      !!
-      !! References :   Hemmings et al., 2008, J. Mar. Res.
-      !!                Ford et al., 2012, Ocean Sci.
-      !!---------------------------------------------------------------------------
-      !!
-      LOGICAL,  INTENT(in   )                               :: ld_chltot      ! Assim chltot y/n
-      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_chltot    ! chltot increments
-      LOGICAL,  INTENT(in   )                               :: ld_chldia      ! Assim chldia y/n
-      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_chldia    ! chldia increments
-      LOGICAL,  INTENT(in   )                               :: ld_chlnon      ! Assim chlnon y/n
-      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_chlnon    ! chlnon increments
-      LOGICAL,  INTENT(in   )                               :: ld_phytot      ! Assim phytot y/n
-      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_phytot    ! phytot increments
-      LOGICAL,  INTENT(in   )                               :: ld_phydia      ! Assim phydia y/n
-      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_phydia    ! phydia increments
-      LOGICAL,  INTENT(in   )                               :: ld_phynon      ! Assim phynon y/n
-      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_phynon    ! phynon increments
-      REAL(wp), INTENT(in   )                               :: pincper        ! Assimilation period
-      REAL(wp), INTENT(in   )                               :: p_maxchlinc    ! Max chl increment
-      LOGICAL,  INTENT(in   )                               :: ld_phytobal    ! Balancing y/n
-      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pmld           ! Mixed layer depth
-      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: pgrow_avg_bkg  ! Avg phyto growth
-      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: ploss_avg_bkg  ! Avg phyto loss
-      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: phyt_avg_bkg   ! Avg phyto
-      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: mld_max_bkg    ! Max MLD
-      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj,jpk,jptra) :: tracer_bkg     ! State variables
-      REAL(wp), INTENT(  out), DIMENSION(jpi,jpj,jpk,jptra) :: phyto2d_balinc ! Balancing increments
-      !!
-      INTEGER                                               :: ji, jj, jk, jn ! Loop counters
-      INTEGER                                               :: jkmax          ! Loop index
-      INTEGER,                 DIMENSION(6)                 :: i_tracer       ! Tracer indices
-      REAL(wp)                                              :: n2be_p         ! N:biomass for total phy
-      REAL(wp)                                              :: n2be_z         ! N:biomass for total zoo
-      REAL(wp)                                              :: n2be_d         ! N:biomass for detritus
-      REAL(wp)                                              :: zfrac_chn      ! Fraction of jpchn
-      REAL(wp)                                              :: zfrac_chd      ! Fraction of jpchd
-      REAL(wp)                                              :: zfrac_phn      ! Fraction of jpphn
-      REAL(wp)                                              :: zfrac_phd      ! Fraction of jpphd
-      REAL(wp)                                              :: zfrac_zmi      ! Fraction of jpzmi
-      REAL(wp)                                              :: zfrac_zme      ! Fraction of jpzme
-      REAL(wp)                                              :: zrat_pds_phd   ! Ratio of jppds:jpphd
-      REAL(wp)                                              :: zrat_chd_phd   ! Ratio of jpchd:jpphd
-      REAL(wp)                                              :: zrat_chn_phn   ! Ratio of jpchn:jpphn
-      REAL(wp)                                              :: zrat_dtc_det   ! Ratio of jpdtc:jpdet
-      REAL(wp),                DIMENSION(jpi,jpj)           :: medusa_chl     ! MEDUSA total chlorophyll
-      REAL(wp),                DIMENSION(jpi,jpj)           :: cchl_p         ! C:Chl for total phy
-      REAL(wp),                DIMENSION(16)                :: modparm        ! Model parameters
-      REAL(wp),                DIMENSION(20)                :: assimparm      ! Assimilation parameters
-      REAL(wp),                DIMENSION(jpi,jpj,jpk,6)     :: bstate         ! Background state
-      REAL(wp),                DIMENSION(jpi,jpj,jpk,6)     :: outincs        ! Balancing increments
-      REAL(wp),                DIMENSION(jpi,jpj,22)        :: diag           ! Depth-indep diagnostics
-      REAL(wp),                DIMENSION(jpi,jpj,jpk,22)    :: diag_fulldepth ! Full-depth diagnostics
-      !!---------------------------------------------------------------------------
-
-      IF ( ld_chltot ) THEN
-         ! If p_maxchlinc > 0 then cap total absolute chlorophyll increment at that value
-         DO jj = 1, jpj
-            DO ji = 1, jpi
-               IF ( p_maxchlinc > 0.0 ) THEN
-                  pinc_chltot(ji,jj) = MAX( -1.0 * p_maxchlinc, MIN( pinc_chltot(ji,jj), p_maxchlinc ) )
-               ENDIF
-            END DO
-         END DO
-      ELSE
-         CALL ctl_stop( ' No PFT assimilation quite yet' )
-      ENDIF
-      
-      IF ( ld_phytobal ) THEN   ! Nitrogen balancing
-
-         ! Set up model parameters to be passed into Hemmings balancing routine.
-         ! For now these are hardwired to the standard HadOCC parameter values
-         ! (except C:N ratios) as this is what the scheme was developed for.
-         ! Obviously, HadOCC and MEDUSA are rather different models, so this
-         ! isn't ideal, but there's not always direct analogues between the two
-         ! parameter sets, so it's the easiest way to get something running.
-         ! In the longer term, some serious MarMOT-based development is required.
-         modparm(1)  = 0.1                               ! grow_sat
-         modparm(2)  = 2.0                               ! psmax
-         modparm(3)  = 0.845                             ! par
-         modparm(4)  = 0.02                              ! alpha
-         modparm(5)  = 0.05                              ! resp_rate
-         modparm(6)  = 0.05                              ! pmort_rate
-         modparm(7)  = 0.01                              ! phyto_min
-         modparm(8)  = 0.05                              ! z_mort_1
-         modparm(9)  = 1.0                               ! z_mort_2
-         modparm(10) = ( xthetapn  + xthetapd  ) / 2.0   ! c2n_p
-         modparm(11) = ( xthetazmi + xthetazme ) / 2.0   ! c2n_z
-         modparm(12) = xthetad                           ! c2n_d
-         modparm(13) = 0.01                              ! graze_threshold
-         modparm(14) = 2.0                               ! holling_coef
-         modparm(15) = 0.5                               ! graze_sat
-         modparm(16) = 2.0                               ! graze_max
-
-         ! Set up assimilation parameters to be passed into balancing routine
-         ! Not sure what assimparm(1) is meant to be, but it doesn't get used
-         assimparm(2)  = balnutext
-         assimparm(3)  = balnutmin
-         assimparm(4)  = r
-         assimparm(5)  = beta_g
-         assimparm(6)  = beta_l
-         assimparm(7)  = beta_m
-         assimparm(8)  = a_g
-         assimparm(9)  = a_l
-         assimparm(10) = a_m
-         assimparm(11) = zfracb0
-         assimparm(12) = zfracb1
-         assimparm(13) = qrfmax
-         assimparm(14) = qafmax
-         assimparm(15) = zrfmax
-         assimparm(16) = zafmax
-         assimparm(17) = prfmax
-         assimparm(18) = incphymin
-         assimparm(19) = integnstep
-         assimparm(20) = pthreshold
-
-         ! Set up external tracer indices array bstate
-         i_tracer(1) = 1   ! nutrient
-         i_tracer(2) = 2   ! phytoplankton
-         i_tracer(3) = 3   ! zooplankton
-         i_tracer(4) = 4   ! detritus
-         i_tracer(5) = 5   ! DIC
-         i_tracer(6) = 6   ! Alkalinity
-
-         ! Set background state
-         bstate(:,:,:,i_tracer(1)) = tracer_bkg(:,:,:,jpdin)
-         bstate(:,:,:,i_tracer(2)) = tracer_bkg(:,:,:,jpphn) + tracer_bkg(:,:,:,jpphd)
-         bstate(:,:,:,i_tracer(3)) = tracer_bkg(:,:,:,jpzmi) + tracer_bkg(:,:,:,jpzme)
-         bstate(:,:,:,i_tracer(4)) = tracer_bkg(:,:,:,jpdet)
-         bstate(:,:,:,i_tracer(5)) = tracer_bkg(:,:,:,jpdic)
-         bstate(:,:,:,i_tracer(6)) = tracer_bkg(:,:,:,jpalk)
-
-         ! Calculate carbon to chlorophyll ratio for combined phytoplankton
-         ! and nitrogen to biomass equivalent for PZD
-         ! Hardwire nitrogen mass to 14.01 for now as it doesn't seem to be set in MEDUSA
-         cchl_p(:,:) = 0.0
-         DO jj = 1, jpj
-            DO ji = 1, jpi
-               IF ( ( tracer_bkg(ji,jj,1,jpchn) + tracer_bkg(ji,jj,1,jpchd ) ) .GT. 0.0 ) THEN
-                  cchl_p(ji,jj) = xmassc * ( ( tracer_bkg(ji,jj,1,jpphn) * xthetapn ) +      &
-                     &                       ( tracer_bkg(ji,jj,1,jpphd) * xthetapd )   ) /  &
-                     &            ( tracer_bkg(ji,jj,1,jpchn) + tracer_bkg(ji,jj,1,jpchd ) )
-               ENDIF
-            END DO
-         END DO
-         n2be_p = 14.01 + ( xmassc * ( ( xthetapn  + xthetapd  ) / 2.0 ) )
-         n2be_z = 14.01 + ( xmassc * ( ( xthetazmi + xthetazme ) / 2.0 ) )
-         n2be_d = 14.01 + ( xmassc * xthetad )
-
-         ! Call nitrogen balancing routine
-         CALL bio_analysis( jpi, jpj, jpk, gdepw_n(:,:,2:jpk), i_tracer, modparm,   &
-            &               n2be_p, n2be_z, n2be_d, assimparm,                      &
-            &               INT(pincper), 1, INT(SUM(tmask,3)), tmask(:,:,:),       &
-            &               pmld(:,:), mld_max_bkg(:,:), pinc_chltot(:,:), cchl_p(:,:), &
-            &               nbal_active, phyt_avg_bkg(:,:),                         &
-            &               gl_active, pgrow_avg_bkg(:,:), ploss_avg_bkg(:,:),      &
-            &               subsurf_active, deepneg_active,                         &
-            &               deeppos_active, nutprof_active,                         &
-            &               bstate, outincs,                                        &
-            &               diag_active, diag,                                      &
-            &               diag_fulldepth_active, diag_fulldepth )
-         
-         ! Loop over each grid point partioning the increments
-         phyto2d_balinc(:,:,:,:) = 0.0
-         DO jk = 1, jpk
-            DO jj = 1, jpj
-               DO ji = 1, jpi
-
-                  IF ( ( tracer_bkg(ji,jj,jk,jpphn) > 0.0 ) .AND. ( tracer_bkg(ji,jj,jk,jpphd) > 0.0 ) ) THEN
-                     ! Phytoplankton nitrogen and silicate split up based on existing ratios
-                     zfrac_phn = tracer_bkg(ji,jj,jk,jpphn) / (tracer_bkg(ji,jj,jk,jpphn) + tracer_bkg(ji,jj,jk,jpphd))
-                     zfrac_phd = 1.0 - zfrac_phn
-                     zrat_pds_phd = tracer_bkg(ji,jj,jk,jppds) / tracer_bkg(ji,jj,jk,jpphd)
-                     phyto2d_balinc(ji,jj,jk,jpphn) = outincs(ji,jj,jk,i_tracer(2)) * zfrac_phn
-                     phyto2d_balinc(ji,jj,jk,jpphd) = outincs(ji,jj,jk,i_tracer(2)) * zfrac_phd
-                     phyto2d_balinc(ji,jj,jk,jppds) = phyto2d_balinc(ji,jj,jk,jpphd) * zrat_pds_phd
-
-                     ! Chlorophyll split up based on existing ratios to phytoplankton nitrogen
-                     ! Not using pinc_chltot directly as it's only 2D
-                     ! This method should give same results at surface as splitting pinc_chltot would
-                     zrat_chn_phn = tracer_bkg(ji,jj,jk,jpchn) / tracer_bkg(ji,jj,jk,jpphn)
-                     zrat_chd_phd = tracer_bkg(ji,jj,jk,jpchd) / tracer_bkg(ji,jj,jk,jpphd)
-                     phyto2d_balinc(ji,jj,jk,jpchn) = phyto2d_balinc(ji,jj,jk,jpphn) * zrat_chn_phn
-                     phyto2d_balinc(ji,jj,jk,jpchd) = phyto2d_balinc(ji,jj,jk,jpphd) * zrat_chd_phd
-                  ENDIF
-
-                  IF ( ( tracer_bkg(ji,jj,jk,jpzmi) > 0.0 ) .AND. ( tracer_bkg(ji,jj,jk,jpzme) > 0.0 ) ) THEN
-                     ! Zooplankton nitrogen split up based on existing ratios
-                     zfrac_zmi = tracer_bkg(ji,jj,jk,jpzmi) / (tracer_bkg(ji,jj,jk,jpzmi) + tracer_bkg(ji,jj,jk,jpzme))
-                     zfrac_zme = 1.0 - zfrac_zmi
-                     phyto2d_balinc(ji,jj,jk,jpzmi) = outincs(ji,jj,jk,i_tracer(3)) * zfrac_zmi
-                     phyto2d_balinc(ji,jj,jk,jpzme) = outincs(ji,jj,jk,i_tracer(3)) * zfrac_zme
-                  ENDIF
-
-                  ! Nitrogen nutrient straight from balancing scheme
-                  phyto2d_balinc(ji,jj,jk,jpdin) = outincs(ji,jj,jk,i_tracer(1))
-
-                  ! Nitrogen detritus straight from balancing scheme
-                  phyto2d_balinc(ji,jj,jk,jpdet) = outincs(ji,jj,jk,i_tracer(4))
-
-                  ! DIC straight from balancing scheme
-                  phyto2d_balinc(ji,jj,jk,jpdic) = outincs(ji,jj,jk,i_tracer(5))
-
-                  ! Alkalinity straight from balancing scheme
-                  phyto2d_balinc(ji,jj,jk,jpalk) = outincs(ji,jj,jk,i_tracer(6))
-
-                  ! Remove diatom silicate increment from nutrient silicate to conserve mass
-                  IF ( ( tracer_bkg(ji,jj,jk,jpsil) - phyto2d_balinc(ji,jj,jk,jppds) ) > 0.0 ) THEN
-                     phyto2d_balinc(ji,jj,jk,jpsil) = phyto2d_balinc(ji,jj,jk,jppds) * (-1.0)
-                  ENDIF
-
-                  IF ( ( tracer_bkg(ji,jj,jk,jpdet) > 0.0 ) .AND. ( tracer_bkg(ji,jj,jk,jpdtc) > 0.0 ) ) THEN
-                     ! Carbon detritus based on existing ratios
-                     zrat_dtc_det = tracer_bkg(ji,jj,jk,jpdtc) / tracer_bkg(ji,jj,jk,jpdet)
-                     phyto2d_balinc(ji,jj,jk,jpdtc) = phyto2d_balinc(ji,jj,jk,jpdet) * zrat_dtc_det
-                  ENDIF
-
-                  ! Do nothing with iron or oxygen for the time being
-                  phyto2d_balinc(ji,jj,jk,jpfer) = 0.0
-                  phyto2d_balinc(ji,jj,jk,jpoxy) = 0.0
-                  
-               END DO
-            END DO
-         END DO
-      
-      ELSE   ! No nitrogen balancing
-      
-         ! Initialise individual chlorophyll increments to zero
-         phyto2d_balinc(:,:,:,jpchn) = 0.0
-         phyto2d_balinc(:,:,:,jpchd) = 0.0
-         
-         ! Split up total surface chlorophyll increments
-         DO jj = 1, jpj
-            DO ji = 1, jpi
-               IF ( medusa_chl(ji,jj) > 0.0 ) THEN
-                  zfrac_chn = tracer_bkg(ji,jj,1,jpchn) / medusa_chl(ji,jj)
-                  zfrac_chd = 1.0 - zfrac_chn
-                  phyto2d_balinc(ji,jj,1,jpchn) = pinc_chltot(ji,jj) * zfrac_chn
-                  phyto2d_balinc(ji,jj,1,jpchd) = pinc_chltot(ji,jj) * zfrac_chd
-               ENDIF
-            END DO
-         END DO
-         
-         ! Propagate through mixed layer
-         DO jj = 1, jpj
-            DO ji = 1, jpi
-               !
-               jkmax = jpk-1
-               DO jk = jpk-1, 1, -1
-                  IF ( ( pmld(ji,jj) >  gdepw_n(ji,jj,jk)   ) .AND. &
-                     & ( pmld(ji,jj) <= gdepw_n(ji,jj,jk+1) ) ) THEN
-                     pmld(ji,jj) = gdepw_n(ji,jj,jk+1)
-                     jkmax = jk
-                  ENDIF
-               END DO
-               !
-               DO jk = 2, jkmax
-                  phyto2d_balinc(ji,jj,jk,jpchn) = phyto2d_balinc(ji,jj,1,jpchn)
-                  phyto2d_balinc(ji,jj,jk,jpchd) = phyto2d_balinc(ji,jj,1,jpchd)
-               END DO
-               !
-            END DO
-         END DO
-
-         ! Set other balancing increments to zero
-         phyto2d_balinc(:,:,:,jpphn) = 0.0
-         phyto2d_balinc(:,:,:,jpphd) = 0.0
-         phyto2d_balinc(:,:,:,jppds) = 0.0
-         phyto2d_balinc(:,:,:,jpzmi) = 0.0
-         phyto2d_balinc(:,:,:,jpzme) = 0.0
-         phyto2d_balinc(:,:,:,jpdin) = 0.0
-         phyto2d_balinc(:,:,:,jpsil) = 0.0
-         phyto2d_balinc(:,:,:,jpfer) = 0.0
-         phyto2d_balinc(:,:,:,jpdet) = 0.0
-         phyto2d_balinc(:,:,:,jpdtc) = 0.0
-         phyto2d_balinc(:,:,:,jpdic) = 0.0
-         phyto2d_balinc(:,:,:,jpalk) = 0.0
-         phyto2d_balinc(:,:,:,jpoxy) = 0.0
-
-      ENDIF
-      
-      ! If performing extra tidal mixing in the Indonesian Throughflow,
-      ! increments have been found to make the carbon cycle unstable
-      ! Therefore, mask these out
-      IF ( ln_tmx_itf ) THEN
-         DO jn = 1, jptra
-            DO jk = 1, jpk
-               phyto2d_balinc(:,:,jk,jn) = phyto2d_balinc(:,:,jk,jn) * ( 1.0 - mask_itf(:,:) )
-            END DO
-         END DO
-      ENDIF
-
-   END SUBROUTINE asm_logchl_bal_medusa
-
-#else
-   !!----------------------------------------------------------------------
-   !!   Default option : Empty routine
-   !!----------------------------------------------------------------------
-CONTAINS
-   SUBROUTINE asm_logchl_bal_medusa( ld_chltot,                      &
-      &                              pinc_chltot,                    &
-      &                              ld_chldia,                      &
-      &                              pinc_chldia,                    &
-      &                              ld_chlnon,                      &
-      &                              pinc_chlnon,                    &
-      &                              ld_phytot,                      &
-      &                              pinc_phytot,                    &
-      &                              ld_phydia,                      &
-      &                              pinc_phydia,                    &
-      &                              ld_phynon,                      &
-      &                              pinc_phynon,                    &
-      &                              pincper,                        &
-      &                              p_maxchlinc, ld_phytobal, pmld, &
-      &                              pgrow_avg_bkg, ploss_avg_bkg,   &
-      &                              phyt_avg_bkg, mld_max_bkg,      &
-      &                              tracer_bkg, phyto2d_balinc )
-      LOGICAL :: ld_chltot
-      REAL    :: pinc_chltot(:,:)
-      LOGICAL :: ld_chldia
-      REAL    :: pinc_chldia(:,:)
-      LOGICAL :: ld_chlnon
-      REAL    :: pinc_chlnon(:,:)
-      LOGICAL :: ld_phytot
-      REAL    :: pinc_phytot(:,:)
-      LOGICAL :: ld_phydia
-      REAL    :: pinc_phydia(:,:)
-      LOGICAL :: ld_phynon
-      REAL    :: pinc_phynon(:,:)
-      REAL    :: pincper
-      REAL    :: p_maxchlinc
-      LOGICAL :: ld_phytobal
-      REAL    :: pmld(:,:)
-      REAL    :: pgrow_avg_bkg(:,:)
-      REAL    :: ploss_avg_bkg(:,:)
-      REAL    :: phyt_avg_bkg(:,:)
-      REAL    :: mld_max_bkg(:,:)
-      REAL    :: tracer_bkg(:,:,:,:)
-      REAL    :: phyto2d_balinc(:,:,:,:)
-      WRITE(*,*) 'asm_logchl_bal_medusa: You should not have seen this print! error?'
-   END SUBROUTINE asm_logchl_bal_medusa
-#endif
-
-   !!======================================================================
-END MODULE asmlogchlbal_medusa
Index: branches/UKMO/dev_r5518_GO6_package_asm_3d_bgc/NEMOGCM/NEMO/OPA_SRC/ASM/asmphyto2dbal_hadocc.F90
===================================================================
--- branches/UKMO/dev_r5518_GO6_package_asm_3d_bgc/NEMOGCM/NEMO/OPA_SRC/ASM/asmphyto2dbal_hadocc.F90	(revision 9432)
+++ branches/UKMO/dev_r5518_GO6_package_asm_3d_bgc/NEMOGCM/NEMO/OPA_SRC/ASM/asmphyto2dbal_hadocc.F90	(revision 9432)
@@ -0,0 +1,285 @@
+MODULE asmphyto2dbal_hadocc
+   !!======================================================================
+   !!                       ***  MODULE asmphyto2dbal_hadocc  ***
+   !! Calculate increments to HadOCC based on surface phyto2d increments
+   !!
+   !! IMPORTANT NOTE: This calls the bioanalysis routine of Hemmings et al.
+   !! For licensing reasons this is kept in its own internal Met Office
+   !! branch (dev/frdf/vn3.6_nitrogen_balancing) rather than in the Paris
+   !! repository, and must be merged in when building.
+   !!
+   !!======================================================================
+   !! History :  3.6  ! 2017-08  (D. Ford)     Adapted from bioanal.F90
+   !!----------------------------------------------------------------------
+#if defined key_asminc && defined key_hadocc
+   !!----------------------------------------------------------------------
+   !! 'key_asminc'          : assimilation increment interface
+   !! 'key_hadocc'          : HadOCC model
+   !!----------------------------------------------------------------------
+   !! asm_phyto2d_bal_hadocc : routine to calculate increments to HadOCC
+   !!----------------------------------------------------------------------
+   USE par_kind,      ONLY: wp             ! kind parameters
+   USE par_oce,       ONLY: jpi, jpj, jpk  ! domain array sizes
+   USE dom_oce,       ONLY: gdepw_n        ! domain information
+   USE iom                                 ! i/o
+   USE par_hadocc                          ! HadOCC parameters
+   USE had_bgc_stnd,  ONLY: kmt            ! HadOCC parameters
+   USE had_bgc_const                       ! HadOCC parameters
+   USE par_trc,       ONLY: jptra          ! Tracer parameters
+   USE bioanalysis                         ! Nitrogen balancing
+
+   IMPLICIT NONE
+   PRIVATE                   
+
+   PUBLIC asm_phyto2d_bal_hadocc
+
+   ! Default values for biological assimilation parameters
+   ! Should match Hemmings et al. (2008)
+   REAL(wp), PARAMETER :: balnutext  =  0.6    !: Default nutrient balancing factor
+   REAL(wp), PARAMETER :: balnutmin  =  0.1    !: Fraction of phytoplankton loss to nutrient
+   REAL(wp), PARAMETER :: r          =  1      !: Reliability of model specific growth rate
+   REAL(wp), PARAMETER :: beta_g     =  0.05   !: Low rate bias correction for growth rate estimator
+   REAL(wp), PARAMETER :: beta_l     =  0.05   !: Low rate bias correction for primary loss rate estimator
+   REAL(wp), PARAMETER :: beta_m     =  0.05   !: Low rate bias correction for secondary loss rate estimator
+   REAL(wp), PARAMETER :: a_g        =  0.2    !: Error s.d. for log10 of growth rate estimator
+   REAL(wp), PARAMETER :: a_l        =  0.4    !: Error s.d. for log10 of primary loss rate estimator
+   REAL(wp), PARAMETER :: a_m        =  0.7    !: Error s.d. for log10 of secondary loss rate estimator
+   REAL(wp), PARAMETER :: zfracb0    =  0.7    !: Base zooplankton fraction of loss to Z & D
+   REAL(wp), PARAMETER :: zfracb1    =  0      !: Phytoplankton sensitivity of zooplankton fraction
+   REAL(wp), PARAMETER :: qrfmax     =  1.1    !: Maximum nutrient limitation reduction factor
+   REAL(wp), PARAMETER :: qafmax     =  1.1    !: Maximum nutrient limitation amplification factor
+   REAL(wp), PARAMETER :: zrfmax     =  2      !: Maximum zooplankton reduction factor
+   REAL(wp), PARAMETER :: zafmax     =  2      !: Maximum zooplankton amplification factor
+   REAL(wp), PARAMETER :: prfmax     =  10     !: Maximum phytoplankton reduction factor (secondary)
+   REAL(wp), PARAMETER :: incphymin  =  0.0001 !: Minimum size of non-zero phytoplankton increment
+   REAL(wp), PARAMETER :: integnstep =  20     !: Number of steps for p.d.f. integral evaluation
+   REAL(wp), PARAMETER :: pthreshold =  0.01   !: Fractional threshold level for setting p.d.f.
+   !
+   LOGICAL,  PARAMETER :: diag_active           = .TRUE.  !: Depth-independent diagnostics
+   LOGICAL,  PARAMETER :: diag_fulldepth_active = .TRUE.  !: Full-depth diagnostics
+   LOGICAL,  PARAMETER :: gl_active             = .TRUE.  !: Growth/loss-based balancing
+   LOGICAL,  PARAMETER :: nbal_active           = .TRUE.  !: Nitrogen balancing
+   LOGICAL,  PARAMETER :: subsurf_active        = .TRUE.  !: Increments below MLD
+   LOGICAL,  PARAMETER :: deepneg_active        = .FALSE. !: Negative primary increments below MLD
+   LOGICAL,  PARAMETER :: deeppos_active        = .FALSE. !: Positive primary increments below MLD
+   LOGICAL,  PARAMETER :: nutprof_active        = .TRUE.  !: Secondary increments
+
+CONTAINS
+
+   SUBROUTINE asm_phyto2d_bal_hadocc( ld_chltot,                      &
+      &                               pinc_chltot,                    &
+      &                               ld_phytot,                      &
+      &                               pinc_phytot,                    &
+      &                               pincper,                        &
+      &                               p_maxchlinc, ld_phytobal, pmld, &
+      &                               pgrow_avg_bkg, ploss_avg_bkg,   &
+      &                               phyt_avg_bkg, mld_max_bkg,      &
+      &                               cchl_p_bkg,                     &
+      &                               tracer_bkg, phyto2d_balinc )
+      !!---------------------------------------------------------------------------
+      !!                    ***  ROUTINE asm_phyto2d_bal_hadocc  ***
+      !!
+      !! ** Purpose :   calculate increments to HadOCC from 2d phytoplankton increments
+      !!
+      !! ** Method  :   call nitrogen balancing scheme
+      !!
+      !! ** Action  :   populate phyto2d_balinc
+      !!
+      !! References :   Hemmings et al., 2008, J. Mar. Res.
+      !!                Ford et al., 2012, Ocean Sci.
+      !!---------------------------------------------------------------------------
+      !!
+      LOGICAL,  INTENT(in   )                               :: ld_chltot      ! Assim chltot y/n
+      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_chltot    ! chltot increments
+      LOGICAL,  INTENT(in   )                               :: ld_phytot      ! Assim phytot y/n
+      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_phytot    ! phytot increments
+      REAL(wp), INTENT(in   )                               :: pincper        ! Assimilation period
+      REAL(wp), INTENT(in   )                               :: p_maxchlinc    ! Max chl increment
+      LOGICAL,  INTENT(in   )                               :: ld_phytobal    ! Balancing y/n
+      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pmld           ! Mixed layer depth
+      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: pgrow_avg_bkg  ! Avg phyto growth
+      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: ploss_avg_bkg  ! Avg phyto loss
+      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: phyt_avg_bkg   ! Avg phyto
+      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: mld_max_bkg    ! Max MLD
+      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: cchl_p_bkg     ! Surface C:Chl
+      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj,jpk,jptra) :: tracer_bkg     ! State variables
+      REAL(wp), INTENT(  out), DIMENSION(jpi,jpj,jpk,jptra) :: phyto2d_balinc ! Balancing increments
+      !!
+      INTEGER                                               :: ji, jj, jk, jn ! Loop counters
+      INTEGER                                               :: jkmax          ! Loop index
+      INTEGER,                 DIMENSION(6)                 :: i_tracer       ! Tracer indices
+      REAL(wp),                DIMENSION(16)                :: modparm        ! Model parameters
+      REAL(wp),                DIMENSION(20)                :: assimparm      ! Assimilation parameters
+      REAL(wp),                DIMENSION(jpi,jpj,jpk,6)     :: bstate         ! Background state
+      REAL(wp),                DIMENSION(jpi,jpj,jpk,6)     :: outincs        ! Balancing increments
+      REAL(wp),                DIMENSION(jpi,jpj,22)        :: diag           ! Depth-indep diagnostics
+      REAL(wp),                DIMENSION(jpi,jpj,jpk,22)    :: diag_fulldepth ! Full-depth diagnostics
+      !!---------------------------------------------------------------------------
+
+      IF ( ld_chltot ) THEN
+         ! If p_maxchlinc > 0 then cap total absolute chlorophyll increment at that value
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               IF ( p_maxchlinc > 0.0 ) THEN
+                  pinc_chltot(ji,jj) = MAX( -1.0 * p_maxchlinc, MIN( pinc_chltot(ji,jj), p_maxchlinc ) )
+               ENDIF
+            END DO
+         END DO
+      ELSE
+         CALL ctl_stop( ' No PFT assimilation quite yet' )
+      ENDIF
+      
+      IF ( ld_phytobal ) THEN   ! Nitrogen balancing
+
+         ! Set up model parameters to be passed into Hemmings balancing routine
+         modparm(1)  = grow_sat
+         modparm(2)  = psmax
+         modparm(3)  = par
+         modparm(4)  = alpha
+         modparm(5)  = resp_rate
+         modparm(6)  = pmort_rate
+         modparm(7)  = phyto_min
+         modparm(8)  = z_mort_1
+         modparm(9)  = z_mort_2
+         modparm(10) = c2n_p
+         modparm(11) = c2n_z
+         modparm(12) = c2n_d
+         modparm(13) = graze_threshold
+         modparm(14) = holling_coef
+         modparm(15) = graze_sat
+         modparm(16) = graze_max
+
+         ! Set up assimilation parameters to be passed into balancing routine
+         ! Not sure what assimparm(1) is meant to be, but it doesn't get used
+         assimparm(2)  = balnutext
+         assimparm(3)  = balnutmin
+         assimparm(4)  = r
+         assimparm(5)  = beta_g
+         assimparm(6)  = beta_l
+         assimparm(7)  = beta_m
+         assimparm(8)  = a_g
+         assimparm(9)  = a_l
+         assimparm(10) = a_m
+         assimparm(11) = zfracb0
+         assimparm(12) = zfracb1
+         assimparm(13) = qrfmax
+         assimparm(14) = qafmax
+         assimparm(15) = zrfmax
+         assimparm(16) = zafmax
+         assimparm(17) = prfmax
+         assimparm(18) = incphymin
+         assimparm(19) = integnstep
+         assimparm(20) = pthreshold
+
+         ! Set up external tracer indices array bstate
+         i_tracer(1) = 1   ! nutrient
+         i_tracer(2) = 2   ! phytoplankton
+         i_tracer(3) = 3   ! zooplankton
+         i_tracer(4) = 4   ! detritus
+         i_tracer(5) = 5   ! DIC
+         i_tracer(6) = 6   ! Alkalinity
+
+         ! Set background state
+         bstate(:,:,:,i_tracer(1)) = tracer_bkg(:,:,:,jp_had_nut)
+         bstate(:,:,:,i_tracer(2)) = tracer_bkg(:,:,:,jp_had_phy)
+         bstate(:,:,:,i_tracer(3)) = tracer_bkg(:,:,:,jp_had_zoo)
+         bstate(:,:,:,i_tracer(4)) = tracer_bkg(:,:,:,jp_had_pdn)
+         bstate(:,:,:,i_tracer(5)) = tracer_bkg(:,:,:,jp_had_dic)
+         bstate(:,:,:,i_tracer(6)) = tracer_bkg(:,:,:,jp_had_alk)
+
+         ! Call nitrogen balancing routine
+         CALL bio_analysis( jpi, jpj, jpk, ZDZ(:,:,:), i_tracer, modparm,               &
+            &               n2be_p, n2be_z, n2be_d, assimparm,                          &
+            &               INT(pincper), 1, kmt(:,:), tmask(:,:,:),                    &
+            &               pmld(:,:), mld_max_bkg(:,:), pinc_chltot(:,:), cchl_p_bkg(:,:), &
+            &               nbal_active, phyt_avg_bkg(:,:),                             &
+            &               gl_active, pgrow_avg_bkg(:,:), ploss_avg_bkg(:,:),          &
+            &               subsurf_active, deepneg_active,                             &
+            &               deeppos_active, nutprof_active,                             &
+            &               bstate, outincs,                                            &
+            &               diag_active, diag,                                          &
+            &               diag_fulldepth_active, diag_fulldepth )
+
+         ! Save balancing increments
+         phyto2d_balinc(:,:,:,jp_had_nut) = outincs(:,:,:,i_tracer(1))
+         phyto2d_balinc(:,:,:,jp_had_phy) = outincs(:,:,:,i_tracer(2))
+         phyto2d_balinc(:,:,:,jp_had_zoo) = outincs(:,:,:,i_tracer(3))
+         phyto2d_balinc(:,:,:,jp_had_pdn) = outincs(:,:,:,i_tracer(4))
+         phyto2d_balinc(:,:,:,jp_had_dic) = outincs(:,:,:,i_tracer(5))
+         phyto2d_balinc(:,:,:,jp_had_alk) = outincs(:,:,:,i_tracer(6))
+      
+      ELSE   ! No nitrogen balancing
+      
+         ! Initialise phytoplankton increment to zero
+         phyto2d_balinc(:,:,:,jp_had_phy) = 0.0
+         
+         ! Convert surface chlorophyll increment to phytoplankton nitrogen
+         phyto2d_balinc(:,:,1,jp_had_phy) = ( cchl_p_bkg(:,:) / (mw_carbon * c2n_p) ) * pinc_chltot(:,:)
+         
+         ! Propagate through mixed layer
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               !
+               jkmax = jpk-1
+               DO jk = jpk-1, 1, -1
+                  IF ( ( pmld(ji,jj) >  gdepw_n(ji,jj,jk)   ) .AND. &
+                     & ( pmld(ji,jj) <= gdepw_n(ji,jj,jk+1) ) ) THEN
+                     pmld(ji,jj) = gdepw_n(ji,jj,jk+1)
+                     jkmax = jk
+                  ENDIF
+               END DO
+               !
+               DO jk = 2, jkmax
+                  phyto2d_balinc(ji,jj,jk,jp_had_phy) = phyto2d_balinc(ji,jj,1,jp_had_phy)
+               END DO
+               !
+            END DO
+         END DO
+
+         ! Set other balancing increments to zero
+         phyto2d_balinc(:,:,:,jp_had_nut) = 0.0
+         phyto2d_balinc(:,:,:,jp_had_zoo) = 0.0
+         phyto2d_balinc(:,:,:,jp_had_pdn) = 0.0
+         phyto2d_balinc(:,:,:,jp_had_dic) = 0.0
+         phyto2d_balinc(:,:,:,jp_had_alk) = 0.0
+      
+      ENDIF
+
+   END SUBROUTINE asm_phyto2d_bal_hadocc
+
+#else
+   !!----------------------------------------------------------------------
+   !!   Default option : Empty routine
+   !!----------------------------------------------------------------------
+CONTAINS
+   SUBROUTINE asm_phyto2d_bal_hadocc( ld_chltot,                      &
+      &                               pinc_chltot,                    &
+      &                               ld_phytot,                      &
+      &                               pinc_phytot,                    &
+      &                               pincper,                        &
+      &                               p_maxchlinc, ld_phytobal, pmld, &
+      &                               pgrow_avg_bkg, ploss_avg_bkg,   &
+      &                               phyt_avg_bkg, mld_max_bkg,      &
+      &                               cchl_p_bkg,                     &
+      &                               tracer_bkg, phyto2d_balinc )
+      LOGICAL :: ld_chltot
+      REAL    :: pinc_chltot(:,:)
+      LOGICAL :: ld_phytot
+      REAL    :: pinc_phytot(:,:)
+      REAL    :: pincper
+      REAL    :: p_maxchlinc
+      LOGICAL :: ld_phytobal
+      REAL    :: pmld(:,:)
+      REAL    :: pgrow_avg_bkg(:,:)
+      REAL    :: ploss_avg_bkg(:,:)
+      REAL    :: phyt_avg_bkg(:,:)
+      REAL    :: mld_max_bkg(:,:)
+      REAL    :: cchl_p_bkg(:,:)
+      REAL    :: tracer_bkg(:,:,:,:)
+      REAL    :: phyto2d_balinc(:,:,:,:)
+      WRITE(*,*) 'asm_phyto2d_bal_hadocc: You should not have seen this print! error?'
+   END SUBROUTINE asm_phyto2d_bal_hadocc
+#endif
+
+   !!======================================================================
+END MODULE asmphyto2dbal_hadocc
Index: branches/UKMO/dev_r5518_GO6_package_asm_3d_bgc/NEMOGCM/NEMO/OPA_SRC/ASM/asmphyto2dbal_medusa.F90
===================================================================
--- branches/UKMO/dev_r5518_GO6_package_asm_3d_bgc/NEMOGCM/NEMO/OPA_SRC/ASM/asmphyto2dbal_medusa.F90	(revision 9432)
+++ branches/UKMO/dev_r5518_GO6_package_asm_3d_bgc/NEMOGCM/NEMO/OPA_SRC/ASM/asmphyto2dbal_medusa.F90	(revision 9432)
@@ -0,0 +1,440 @@
+MODULE asmphyto2dbal_medusa
+   !!======================================================================
+   !!                       ***  MODULE asmphyto2dbal_medusa  ***
+   !! Calculate increments to MEDUSA based on surface phyto2d increments
+   !!
+   !! IMPORTANT NOTE: This calls the bioanalysis routine of Hemmings et al.
+   !! For licensing reasons this is kept in its own internal Met Office
+   !! branch (dev/frdf/vn3.6_nitrogen_balancing) rather than in the Paris
+   !! repository, and must be merged in when building.
+   !!
+   !!======================================================================
+   !! History :  3.6  ! 2017-08 (D. Ford)  Adapted from asmphyto2dbal_hadocc
+   !!----------------------------------------------------------------------
+#if defined key_asminc && defined key_medusa && defined key_foam_medusa
+   !!----------------------------------------------------------------------
+   !! 'key_asminc'          : assimilation increment interface
+   !! 'key_medusa'          : MEDUSA model
+   !! 'key_foam_medusa'     : MEDUSA extras for FOAM OBS and ASM
+   !!----------------------------------------------------------------------
+   !! asm_phyto2d_bal_medusa : routine to calculate increments to MEDUSA
+   !!----------------------------------------------------------------------
+   USE par_kind,      ONLY: wp             ! kind parameters
+   USE par_oce,       ONLY: jpi, jpj, jpk  ! domain array sizes
+   USE dom_oce,       ONLY: gdepw_n        ! domain information
+   USE zdftmx,        ONLY: ln_tmx_itf, &  ! Indonesian Throughflow
+      &                     mask_itf       ! tidal mixing mask
+   USE iom                                 ! i/o
+   USE sms_medusa                          ! MEDUSA parameters
+   USE par_medusa                          ! MEDUSA parameters
+   USE par_trc,       ONLY: jptra          ! Tracer parameters
+   USE bioanalysis                         ! Nitrogen balancing
+
+   IMPLICIT NONE
+   PRIVATE                   
+
+   PUBLIC asm_phyto2d_bal_medusa
+
+   ! Default values for biological assimilation parameters
+   ! Should match Hemmings et al. (2008)
+   REAL(wp), PARAMETER :: balnutext  =  0.6    !: Default nutrient balancing factor
+   REAL(wp), PARAMETER :: balnutmin  =  0.1    !: Fraction of phytoplankton loss to nutrient
+   REAL(wp), PARAMETER :: r          =  1      !: Reliability of model specific growth rate
+   REAL(wp), PARAMETER :: beta_g     =  0.05   !: Low rate bias correction for growth rate estimator
+   REAL(wp), PARAMETER :: beta_l     =  0.05   !: Low rate bias correction for primary loss rate estimator
+   REAL(wp), PARAMETER :: beta_m     =  0.05   !: Low rate bias correction for secondary loss rate estimator
+   REAL(wp), PARAMETER :: a_g        =  0.2    !: Error s.d. for log10 of growth rate estimator
+   REAL(wp), PARAMETER :: a_l        =  0.4    !: Error s.d. for log10 of primary loss rate estimator
+   REAL(wp), PARAMETER :: a_m        =  0.7    !: Error s.d. for log10 of secondary loss rate estimator
+   REAL(wp), PARAMETER :: zfracb0    =  0.7    !: Base zooplankton fraction of loss to Z & D
+   REAL(wp), PARAMETER :: zfracb1    =  0      !: Phytoplankton sensitivity of zooplankton fraction
+   REAL(wp), PARAMETER :: qrfmax     =  1.1    !: Maximum nutrient limitation reduction factor
+   REAL(wp), PARAMETER :: qafmax     =  1.1    !: Maximum nutrient limitation amplification factor
+   REAL(wp), PARAMETER :: zrfmax     =  2      !: Maximum zooplankton reduction factor
+   REAL(wp), PARAMETER :: zafmax     =  2      !: Maximum zooplankton amplification factor
+   REAL(wp), PARAMETER :: prfmax     =  10     !: Maximum phytoplankton reduction factor (secondary)
+   REAL(wp), PARAMETER :: incphymin  =  0.0001 !: Minimum size of non-zero phytoplankton increment
+   REAL(wp), PARAMETER :: integnstep =  20     !: Number of steps for p.d.f. integral evaluation
+   REAL(wp), PARAMETER :: pthreshold =  0.01   !: Fractional threshold level for setting p.d.f.
+   !
+   LOGICAL,  PARAMETER :: diag_active           = .TRUE.  !: Depth-independent diagnostics
+   LOGICAL,  PARAMETER :: diag_fulldepth_active = .TRUE.  !: Full-depth diagnostics
+   LOGICAL,  PARAMETER :: gl_active             = .TRUE.  !: Growth/loss-based balancing
+   LOGICAL,  PARAMETER :: nbal_active           = .TRUE.  !: Nitrogen balancing
+   LOGICAL,  PARAMETER :: subsurf_active        = .TRUE.  !: Increments below MLD
+   LOGICAL,  PARAMETER :: deepneg_active        = .FALSE. !: Negative primary increments below MLD
+   LOGICAL,  PARAMETER :: deeppos_active        = .FALSE. !: Positive primary increments below MLD
+   LOGICAL,  PARAMETER :: nutprof_active        = .TRUE.  !: Secondary increments
+
+CONTAINS
+
+   SUBROUTINE asm_phyto2d_bal_medusa( ld_chltot,                      &
+      &                               pinc_chltot,                    &
+      &                               ld_chldia,                      &
+      &                               pinc_chldia,                    &
+      &                               ld_chlnon,                      &
+      &                               pinc_chlnon,                    &
+      &                               ld_phytot,                      &
+      &                               pinc_phytot,                    &
+      &                               ld_phydia,                      &
+      &                               pinc_phydia,                    &
+      &                               ld_phynon,                      &
+      &                               pinc_phynon,                    &
+      &                               pincper,                        &
+      &                               p_maxchlinc, ld_phytobal, pmld, &
+      &                               pgrow_avg_bkg, ploss_avg_bkg,   &
+      &                               phyt_avg_bkg, mld_max_bkg,      &
+      &                               tracer_bkg, phyto2d_balinc )
+      !!---------------------------------------------------------------------------
+      !!                    ***  ROUTINE asm_phyto2d_bal_medusa  ***
+      !!
+      !! ** Purpose :   calculate increments to MEDUSA from 2d phytoplankton increments
+      !!
+      !! ** Method  :   average up MEDUSA to look like HadOCC
+      !!                call nitrogen balancing scheme
+      !!                separate back out to MEDUSA
+      !!
+      !! ** Action  :   populate phyto2d_balinc
+      !!
+      !! References :   Hemmings et al., 2008, J. Mar. Res.
+      !!                Ford et al., 2012, Ocean Sci.
+      !!---------------------------------------------------------------------------
+      !!
+      LOGICAL,  INTENT(in   )                               :: ld_chltot      ! Assim chltot y/n
+      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_chltot    ! chltot increments
+      LOGICAL,  INTENT(in   )                               :: ld_chldia      ! Assim chldia y/n
+      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_chldia    ! chldia increments
+      LOGICAL,  INTENT(in   )                               :: ld_chlnon      ! Assim chlnon y/n
+      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_chlnon    ! chlnon increments
+      LOGICAL,  INTENT(in   )                               :: ld_phytot      ! Assim phytot y/n
+      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_phytot    ! phytot increments
+      LOGICAL,  INTENT(in   )                               :: ld_phydia      ! Assim phydia y/n
+      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_phydia    ! phydia increments
+      LOGICAL,  INTENT(in   )                               :: ld_phynon      ! Assim phynon y/n
+      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_phynon    ! phynon increments
+      REAL(wp), INTENT(in   )                               :: pincper        ! Assimilation period
+      REAL(wp), INTENT(in   )                               :: p_maxchlinc    ! Max chl increment
+      LOGICAL,  INTENT(in   )                               :: ld_phytobal    ! Balancing y/n
+      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pmld           ! Mixed layer depth
+      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: pgrow_avg_bkg  ! Avg phyto growth
+      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: ploss_avg_bkg  ! Avg phyto loss
+      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: phyt_avg_bkg   ! Avg phyto
+      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj)           :: mld_max_bkg    ! Max MLD
+      REAL(wp), INTENT(in   ), DIMENSION(jpi,jpj,jpk,jptra) :: tracer_bkg     ! State variables
+      REAL(wp), INTENT(  out), DIMENSION(jpi,jpj,jpk,jptra) :: phyto2d_balinc ! Balancing increments
+      !!
+      INTEGER                                               :: ji, jj, jk, jn ! Loop counters
+      INTEGER                                               :: jkmax          ! Loop index
+      INTEGER,                 DIMENSION(6)                 :: i_tracer       ! Tracer indices
+      REAL(wp)                                              :: n2be_p         ! N:biomass for total phy
+      REAL(wp)                                              :: n2be_z         ! N:biomass for total zoo
+      REAL(wp)                                              :: n2be_d         ! N:biomass for detritus
+      REAL(wp)                                              :: zfrac_chn      ! Fraction of jpchn
+      REAL(wp)                                              :: zfrac_chd      ! Fraction of jpchd
+      REAL(wp)                                              :: zfrac_phn      ! Fraction of jpphn
+      REAL(wp)                                              :: zfrac_phd      ! Fraction of jpphd
+      REAL(wp)                                              :: zfrac_zmi      ! Fraction of jpzmi
+      REAL(wp)                                              :: zfrac_zme      ! Fraction of jpzme
+      REAL(wp)                                              :: zrat_pds_phd   ! Ratio of jppds:jpphd
+      REAL(wp)                                              :: zrat_chd_phd   ! Ratio of jpchd:jpphd
+      REAL(wp)                                              :: zrat_chn_phn   ! Ratio of jpchn:jpphn
+      REAL(wp)                                              :: zrat_dtc_det   ! Ratio of jpdtc:jpdet
+      REAL(wp),                DIMENSION(jpi,jpj)           :: medusa_chl     ! MEDUSA total chlorophyll
+      REAL(wp),                DIMENSION(jpi,jpj)           :: cchl_p         ! C:Chl for total phy
+      REAL(wp),                DIMENSION(16)                :: modparm        ! Model parameters
+      REAL(wp),                DIMENSION(20)                :: assimparm      ! Assimilation parameters
+      REAL(wp),                DIMENSION(jpi,jpj,jpk,6)     :: bstate         ! Background state
+      REAL(wp),                DIMENSION(jpi,jpj,jpk,6)     :: outincs        ! Balancing increments
+      REAL(wp),                DIMENSION(jpi,jpj,22)        :: diag           ! Depth-indep diagnostics
+      REAL(wp),                DIMENSION(jpi,jpj,jpk,22)    :: diag_fulldepth ! Full-depth diagnostics
+      !!---------------------------------------------------------------------------
+
+      IF ( ld_chltot ) THEN
+         ! If p_maxchlinc > 0 then cap total absolute chlorophyll increment at that value
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               IF ( p_maxchlinc > 0.0 ) THEN
+                  pinc_chltot(ji,jj) = MAX( -1.0 * p_maxchlinc, MIN( pinc_chltot(ji,jj), p_maxchlinc ) )
+               ENDIF
+            END DO
+         END DO
+      ELSE
+         CALL ctl_stop( ' No PFT assimilation quite yet' )
+      ENDIF
+      
+      IF ( ld_phytobal ) THEN   ! Nitrogen balancing
+
+         ! Set up model parameters to be passed into Hemmings balancing routine.
+         ! For now these are hardwired to the standard HadOCC parameter values
+         ! (except C:N ratios) as this is what the scheme was developed for.
+         ! Obviously, HadOCC and MEDUSA are rather different models, so this
+         ! isn't ideal, but there's not always direct analogues between the two
+         ! parameter sets, so it's the easiest way to get something running.
+         ! In the longer term, some serious MarMOT-based development is required.
+         modparm(1)  = 0.1                               ! grow_sat
+         modparm(2)  = 2.0                               ! psmax
+         modparm(3)  = 0.845                             ! par
+         modparm(4)  = 0.02                              ! alpha
+         modparm(5)  = 0.05                              ! resp_rate
+         modparm(6)  = 0.05                              ! pmort_rate
+         modparm(7)  = 0.01                              ! phyto_min
+         modparm(8)  = 0.05                              ! z_mort_1
+         modparm(9)  = 1.0                               ! z_mort_2
+         modparm(10) = ( xthetapn  + xthetapd  ) / 2.0   ! c2n_p
+         modparm(11) = ( xthetazmi + xthetazme ) / 2.0   ! c2n_z
+         modparm(12) = xthetad                           ! c2n_d
+         modparm(13) = 0.01                              ! graze_threshold
+         modparm(14) = 2.0                               ! holling_coef
+         modparm(15) = 0.5                               ! graze_sat
+         modparm(16) = 2.0                               ! graze_max
+
+         ! Set up assimilation parameters to be passed into balancing routine
+         ! Not sure what assimparm(1) is meant to be, but it doesn't get used
+         assimparm(2)  = balnutext
+         assimparm(3)  = balnutmin
+         assimparm(4)  = r
+         assimparm(5)  = beta_g
+         assimparm(6)  = beta_l
+         assimparm(7)  = beta_m
+         assimparm(8)  = a_g
+         assimparm(9)  = a_l
+         assimparm(10) = a_m
+         assimparm(11) = zfracb0
+         assimparm(12) = zfracb1
+         assimparm(13) = qrfmax
+         assimparm(14) = qafmax
+         assimparm(15) = zrfmax
+         assimparm(16) = zafmax
+         assimparm(17) = prfmax
+         assimparm(18) = incphymin
+         assimparm(19) = integnstep
+         assimparm(20) = pthreshold
+
+         ! Set up external tracer indices array bstate
+         i_tracer(1) = 1   ! nutrient
+         i_tracer(2) = 2   ! phytoplankton
+         i_tracer(3) = 3   ! zooplankton
+         i_tracer(4) = 4   ! detritus
+         i_tracer(5) = 5   ! DIC
+         i_tracer(6) = 6   ! Alkalinity
+
+         ! Set background state
+         bstate(:,:,:,i_tracer(1)) = tracer_bkg(:,:,:,jpdin)
+         bstate(:,:,:,i_tracer(2)) = tracer_bkg(:,:,:,jpphn) + tracer_bkg(:,:,:,jpphd)
+         bstate(:,:,:,i_tracer(3)) = tracer_bkg(:,:,:,jpzmi) + tracer_bkg(:,:,:,jpzme)
+         bstate(:,:,:,i_tracer(4)) = tracer_bkg(:,:,:,jpdet)
+         bstate(:,:,:,i_tracer(5)) = tracer_bkg(:,:,:,jpdic)
+         bstate(:,:,:,i_tracer(6)) = tracer_bkg(:,:,:,jpalk)
+
+         ! Calculate carbon to chlorophyll ratio for combined phytoplankton
+         ! and nitrogen to biomass equivalent for PZD
+         ! Hardwire nitrogen mass to 14.01 for now as it doesn't seem to be set in MEDUSA
+         cchl_p(:,:) = 0.0
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               IF ( ( tracer_bkg(ji,jj,1,jpchn) + tracer_bkg(ji,jj,1,jpchd ) ) .GT. 0.0 ) THEN
+                  cchl_p(ji,jj) = xmassc * ( ( tracer_bkg(ji,jj,1,jpphn) * xthetapn ) +      &
+                     &                       ( tracer_bkg(ji,jj,1,jpphd) * xthetapd )   ) /  &
+                     &            ( tracer_bkg(ji,jj,1,jpchn) + tracer_bkg(ji,jj,1,jpchd ) )
+               ENDIF
+            END DO
+         END DO
+         n2be_p = 14.01 + ( xmassc * ( ( xthetapn  + xthetapd  ) / 2.0 ) )
+         n2be_z = 14.01 + ( xmassc * ( ( xthetazmi + xthetazme ) / 2.0 ) )
+         n2be_d = 14.01 + ( xmassc * xthetad )
+
+         ! Call nitrogen balancing routine
+         CALL bio_analysis( jpi, jpj, jpk, gdepw_n(:,:,2:jpk), i_tracer, modparm,   &
+            &               n2be_p, n2be_z, n2be_d, assimparm,                      &
+            &               INT(pincper), 1, INT(SUM(tmask,3)), tmask(:,:,:),       &
+            &               pmld(:,:), mld_max_bkg(:,:), pinc_chltot(:,:), cchl_p(:,:), &
+            &               nbal_active, phyt_avg_bkg(:,:),                         &
+            &               gl_active, pgrow_avg_bkg(:,:), ploss_avg_bkg(:,:),      &
+            &               subsurf_active, deepneg_active,                         &
+            &               deeppos_active, nutprof_active,                         &
+            &               bstate, outincs,                                        &
+            &               diag_active, diag,                                      &
+            &               diag_fulldepth_active, diag_fulldepth )
+         
+         ! Loop over each grid point partioning the increments
+         phyto2d_balinc(:,:,:,:) = 0.0
+         DO jk = 1, jpk
+            DO jj = 1, jpj
+               DO ji = 1, jpi
+
+                  IF ( ( tracer_bkg(ji,jj,jk,jpphn) > 0.0 ) .AND. ( tracer_bkg(ji,jj,jk,jpphd) > 0.0 ) ) THEN
+                     ! Phytoplankton nitrogen and silicate split up based on existing ratios
+                     zfrac_phn = tracer_bkg(ji,jj,jk,jpphn) / (tracer_bkg(ji,jj,jk,jpphn) + tracer_bkg(ji,jj,jk,jpphd))
+                     zfrac_phd = 1.0 - zfrac_phn
+                     zrat_pds_phd = tracer_bkg(ji,jj,jk,jppds) / tracer_bkg(ji,jj,jk,jpphd)
+                     phyto2d_balinc(ji,jj,jk,jpphn) = outincs(ji,jj,jk,i_tracer(2)) * zfrac_phn
+                     phyto2d_balinc(ji,jj,jk,jpphd) = outincs(ji,jj,jk,i_tracer(2)) * zfrac_phd
+                     phyto2d_balinc(ji,jj,jk,jppds) = phyto2d_balinc(ji,jj,jk,jpphd) * zrat_pds_phd
+
+                     ! Chlorophyll split up based on existing ratios to phytoplankton nitrogen
+                     ! Not using pinc_chltot directly as it's only 2D
+                     ! This method should give same results at surface as splitting pinc_chltot would
+                     zrat_chn_phn = tracer_bkg(ji,jj,jk,jpchn) / tracer_bkg(ji,jj,jk,jpphn)
+                     zrat_chd_phd = tracer_bkg(ji,jj,jk,jpchd) / tracer_bkg(ji,jj,jk,jpphd)
+                     phyto2d_balinc(ji,jj,jk,jpchn) = phyto2d_balinc(ji,jj,jk,jpphn) * zrat_chn_phn
+                     phyto2d_balinc(ji,jj,jk,jpchd) = phyto2d_balinc(ji,jj,jk,jpphd) * zrat_chd_phd
+                  ENDIF
+
+                  IF ( ( tracer_bkg(ji,jj,jk,jpzmi) > 0.0 ) .AND. ( tracer_bkg(ji,jj,jk,jpzme) > 0.0 ) ) THEN
+                     ! Zooplankton nitrogen split up based on existing ratios
+                     zfrac_zmi = tracer_bkg(ji,jj,jk,jpzmi) / (tracer_bkg(ji,jj,jk,jpzmi) + tracer_bkg(ji,jj,jk,jpzme))
+                     zfrac_zme = 1.0 - zfrac_zmi
+                     phyto2d_balinc(ji,jj,jk,jpzmi) = outincs(ji,jj,jk,i_tracer(3)) * zfrac_zmi
+                     phyto2d_balinc(ji,jj,jk,jpzme) = outincs(ji,jj,jk,i_tracer(3)) * zfrac_zme
+                  ENDIF
+
+                  ! Nitrogen nutrient straight from balancing scheme
+                  phyto2d_balinc(ji,jj,jk,jpdin) = outincs(ji,jj,jk,i_tracer(1))
+
+                  ! Nitrogen detritus straight from balancing scheme
+                  phyto2d_balinc(ji,jj,jk,jpdet) = outincs(ji,jj,jk,i_tracer(4))
+
+                  ! DIC straight from balancing scheme
+                  phyto2d_balinc(ji,jj,jk,jpdic) = outincs(ji,jj,jk,i_tracer(5))
+
+                  ! Alkalinity straight from balancing scheme
+                  phyto2d_balinc(ji,jj,jk,jpalk) = outincs(ji,jj,jk,i_tracer(6))
+
+                  ! Remove diatom silicate increment from nutrient silicate to conserve mass
+                  IF ( ( tracer_bkg(ji,jj,jk,jpsil) - phyto2d_balinc(ji,jj,jk,jppds) ) > 0.0 ) THEN
+                     phyto2d_balinc(ji,jj,jk,jpsil) = phyto2d_balinc(ji,jj,jk,jppds) * (-1.0)
+                  ENDIF
+
+                  IF ( ( tracer_bkg(ji,jj,jk,jpdet) > 0.0 ) .AND. ( tracer_bkg(ji,jj,jk,jpdtc) > 0.0 ) ) THEN
+                     ! Carbon detritus based on existing ratios
+                     zrat_dtc_det = tracer_bkg(ji,jj,jk,jpdtc) / tracer_bkg(ji,jj,jk,jpdet)
+                     phyto2d_balinc(ji,jj,jk,jpdtc) = phyto2d_balinc(ji,jj,jk,jpdet) * zrat_dtc_det
+                  ENDIF
+
+                  ! Do nothing with iron or oxygen for the time being
+                  phyto2d_balinc(ji,jj,jk,jpfer) = 0.0
+                  phyto2d_balinc(ji,jj,jk,jpoxy) = 0.0
+                  
+               END DO
+            END DO
+         END DO
+      
+      ELSE   ! No nitrogen balancing
+      
+         ! Initialise individual chlorophyll increments to zero
+         phyto2d_balinc(:,:,:,jpchn) = 0.0
+         phyto2d_balinc(:,:,:,jpchd) = 0.0
+         
+         ! Split up total surface chlorophyll increments
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               IF ( medusa_chl(ji,jj) > 0.0 ) THEN
+                  zfrac_chn = tracer_bkg(ji,jj,1,jpchn) / medusa_chl(ji,jj)
+                  zfrac_chd = 1.0 - zfrac_chn
+                  phyto2d_balinc(ji,jj,1,jpchn) = pinc_chltot(ji,jj) * zfrac_chn
+                  phyto2d_balinc(ji,jj,1,jpchd) = pinc_chltot(ji,jj) * zfrac_chd
+               ENDIF
+            END DO
+         END DO
+         
+         ! Propagate through mixed layer
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               !
+               jkmax = jpk-1
+               DO jk = jpk-1, 1, -1
+                  IF ( ( pmld(ji,jj) >  gdepw_n(ji,jj,jk)   ) .AND. &
+                     & ( pmld(ji,jj) <= gdepw_n(ji,jj,jk+1) ) ) THEN
+                     pmld(ji,jj) = gdepw_n(ji,jj,jk+1)
+                     jkmax = jk
+                  ENDIF
+               END DO
+               !
+               DO jk = 2, jkmax
+                  phyto2d_balinc(ji,jj,jk,jpchn) = phyto2d_balinc(ji,jj,1,jpchn)
+                  phyto2d_balinc(ji,jj,jk,jpchd) = phyto2d_balinc(ji,jj,1,jpchd)
+               END DO
+               !
+            END DO
+         END DO
+
+         ! Set other balancing increments to zero
+         phyto2d_balinc(:,:,:,jpphn) = 0.0
+         phyto2d_balinc(:,:,:,jpphd) = 0.0
+         phyto2d_balinc(:,:,:,jppds) = 0.0
+         phyto2d_balinc(:,:,:,jpzmi) = 0.0
+         phyto2d_balinc(:,:,:,jpzme) = 0.0
+         phyto2d_balinc(:,:,:,jpdin) = 0.0
+         phyto2d_balinc(:,:,:,jpsil) = 0.0
+         phyto2d_balinc(:,:,:,jpfer) = 0.0
+         phyto2d_balinc(:,:,:,jpdet) = 0.0
+         phyto2d_balinc(:,:,:,jpdtc) = 0.0
+         phyto2d_balinc(:,:,:,jpdic) = 0.0
+         phyto2d_balinc(:,:,:,jpalk) = 0.0
+         phyto2d_balinc(:,:,:,jpoxy) = 0.0
+
+      ENDIF
+      
+      ! If performing extra tidal mixing in the Indonesian Throughflow,
+      ! increments have been found to make the carbon cycle unstable
+      ! Therefore, mask these out
+      IF ( ln_tmx_itf ) THEN
+         DO jn = 1, jptra
+            DO jk = 1, jpk
+               phyto2d_balinc(:,:,jk,jn) = phyto2d_balinc(:,:,jk,jn) * ( 1.0 - mask_itf(:,:) )
+            END DO
+         END DO
+      ENDIF
+
+   END SUBROUTINE asm_phyto2d_bal_medusa
+
+#else
+   !!----------------------------------------------------------------------
+   !!   Default option : Empty routine
+   !!----------------------------------------------------------------------
+CONTAINS
+   SUBROUTINE asm_phyto2d_bal_medusa( ld_chltot,                      &
+      &                              pinc_chltot,                    &
+      &                              ld_chldia,                      &
+      &                              pinc_chldia,                    &
+      &                              ld_chlnon,                      &
+      &                              pinc_chlnon,                    &
+      &                              ld_phytot,                      &
+      &                              pinc_phytot,                    &
+      &                              ld_phydia,                      &
+      &                              pinc_phydia,                    &
+      &                              ld_phynon,                      &
+      &                              pinc_phynon,                    &
+      &                              pincper,                        &
+      &                              p_maxchlinc, ld_phytobal, pmld, &
+      &                              pgrow_avg_bkg, ploss_avg_bkg,   &
+      &                              phyt_avg_bkg, mld_max_bkg,      &
+      &                              tracer_bkg, phyto2d_balinc )
+      LOGICAL :: ld_chltot
+      REAL    :: pinc_chltot(:,:)
+      LOGICAL :: ld_chldia
+      REAL    :: pinc_chldia(:,:)
+      LOGICAL :: ld_chlnon
+      REAL    :: pinc_chlnon(:,:)
+      LOGICAL :: ld_phytot
+      REAL    :: pinc_phytot(:,:)
+      LOGICAL :: ld_phydia
+      REAL    :: pinc_phydia(:,:)
+      LOGICAL :: ld_phynon
+      REAL    :: pinc_phynon(:,:)
+      REAL    :: pincper
+      REAL    :: p_maxchlinc
+      LOGICAL :: ld_phytobal
+      REAL    :: pmld(:,:)
+      REAL    :: pgrow_avg_bkg(:,:)
+      REAL    :: ploss_avg_bkg(:,:)
+      REAL    :: phyt_avg_bkg(:,:)
+      REAL    :: mld_max_bkg(:,:)
+      REAL    :: tracer_bkg(:,:,:,:)
+      REAL    :: phyto2d_balinc(:,:,:,:)
+      WRITE(*,*) 'asm_phyto2d_bal_medusa: You should not have seen this print! error?'
+   END SUBROUTINE asm_phyto2d_bal_medusa
+#endif
+
+   !!======================================================================
+END MODULE asmphyto2dbal_medusa
