Index: branches/UKMO/AMM15_v3_6_STABLE_package_collate_BGC_DA/NEMOGCM/NEMO/OPA_SRC/ASM/asmphyto2dbal_ersem.F90
===================================================================
--- branches/UKMO/AMM15_v3_6_STABLE_package_collate_BGC_DA/NEMOGCM/NEMO/OPA_SRC/ASM/asmphyto2dbal_ersem.F90	(revision 10667)
+++ branches/UKMO/AMM15_v3_6_STABLE_package_collate_BGC_DA/NEMOGCM/NEMO/OPA_SRC/ASM/asmphyto2dbal_ersem.F90	(revision 10707)
@@ -100,99 +100,100 @@
       !!---------------------------------------------------------------------------
       !!
-      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_chlnan      ! Assim chlnan y/n
-      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_chlnan    ! chlnan increments
-      LOGICAL,  INTENT(in   )                               :: ld_chlpic      ! Assim chlpic y/n
-      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_chlpic    ! chlpic increments
-      LOGICAL,  INTENT(in   )                               :: ld_chldin      ! Assim chldin y/n
-      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_chldin    ! chldin 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)       :: totalk_bkg     ! Total alkalinity
-      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
+      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_chlnan        ! Assim chlnan y/n
+      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_chlnan      ! chlnan increments
+      LOGICAL,  INTENT(in   )                               :: ld_chlpic        ! Assim chlpic y/n
+      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_chlpic      ! chlpic increments
+      LOGICAL,  INTENT(in   )                               :: ld_chldin        ! Assim chldin y/n
+      REAL(wp), INTENT(inout), DIMENSION(jpi,jpj)           :: pinc_chldin      ! chldin 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)       :: totalk_bkg       ! Total alkalinity
+      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)                                              :: zmassc         ! Carbon molar mass
-      REAL(wp)                                              :: zmassn         ! Nitrogen molar mass
-      REAL(wp)                                              :: z4qnc          ! Z4/qnc (mesozoo N:C)
-      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          ! Fractions
-      REAL(wp)                                              :: zfrac_chl1     ! 
-      REAL(wp)                                              :: zfrac_chl2     ! 
-      REAL(wp)                                              :: zfrac_chl3     ! 
-      REAL(wp)                                              :: zfrac_chl4     ! 
-      REAL(wp)                                              :: zfrac_p1n      ! 
-      REAL(wp)                                              :: zfrac_p2n      ! 
-      REAL(wp)                                              :: zfrac_p3n      ! 
-      REAL(wp)                                              :: zfrac_p4n      ! 
-      REAL(wp)                                              :: zfrac_z4n      ! 
-      REAL(wp)                                              :: zfrac_z5n      ! 
-      REAL(wp)                                              :: zfrac_z6n      ! 
-      REAL(wp)                                              :: zfrac_n3n      ! 
-      REAL(wp)                                              :: zfrac_n4n      ! 
-      REAL(wp)                                              :: zfrac_r4n      ! 
-      REAL(wp)                                              :: zfrac_r6n      ! 
-      REAL(wp)                                              :: zfrac_r8n      ! 
-      REAL(wp)                                              :: zrat_chl1_p1n  ! Ratios
-      REAL(wp)                                              :: zrat_p1c_p1n   ! 
-      REAL(wp)                                              :: zrat_p1p_p1n   ! 
-      REAL(wp)                                              :: zrat_p1s_p1n   ! 
-      REAL(wp)                                              :: zrat_chl2_p2n  ! 
-      REAL(wp)                                              :: zrat_p2c_p2n   ! 
-      REAL(wp)                                              :: zrat_p2p_p2n   ! 
-      REAL(wp)                                              :: zrat_chl3_p3n  ! 
-      REAL(wp)                                              :: zrat_p3c_p3n   ! 
-      REAL(wp)                                              :: zrat_p3p_p3n   ! 
-      REAL(wp)                                              :: zrat_chl4_p4n  ! 
-      REAL(wp)                                              :: zrat_p4c_p4n   ! 
-      REAL(wp)                                              :: zrat_p4p_p4n   ! 
-      REAL(wp)                                              :: zrat_z4c_z4n   ! 
-      REAL(wp)                                              :: zrat_z5c_z5n   ! 
-      REAL(wp)                                              :: zrat_z5p_z5n   ! 
-      REAL(wp)                                              :: zrat_z6c_z6n   ! 
-      REAL(wp)                                              :: zrat_z6p_z6n   ! 
-      REAL(wp)                                              :: zrat_r4c_r4n   ! 
-      REAL(wp)                                              :: zrat_r4p_r4n   ! 
-      REAL(wp)                                              :: zrat_r6c_r6n   ! 
-      REAL(wp)                                              :: zrat_r6p_r6n   ! 
-      REAL(wp)                                              :: zrat_r6s_r6n   ! 
-      REAL(wp)                                              :: zrat_r8c_r8n   ! 
-      REAL(wp)                                              :: zrat_r8p_r8n   ! 
-      REAL(wp)                                              :: zrat_r8s_r8n   ! 
-      REAL(wp)                                              :: zrat_p1c_chl1  ! 
-      REAL(wp)                                              :: zrat_p1n_chl1  ! 
-      REAL(wp)                                              :: zrat_p1p_chl1  ! 
-      REAL(wp)                                              :: zrat_p1s_chl1  ! 
-      REAL(wp)                                              :: zrat_p2c_chl2  ! 
-      REAL(wp)                                              :: zrat_p2n_chl2  ! 
-      REAL(wp)                                              :: zrat_p2p_chl2  ! 
-      REAL(wp)                                              :: zrat_p3c_chl3  ! 
-      REAL(wp)                                              :: zrat_p3n_chl3  ! 
-      REAL(wp)                                              :: zrat_p3p_chl3  ! 
-      REAL(wp)                                              :: zrat_p4c_chl4  ! 
-      REAL(wp)                                              :: zrat_p4n_chl4  ! 
-      REAL(wp)                                              :: zrat_p4p_chl4  ! 
-      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
-      REAL(wp),                DIMENSION(jpi,jpj)           :: pinc_chltot_temp
+      INTEGER                                               :: ji, jj, jk, jn   ! Loop counters
+      INTEGER                                               :: jkmax            ! Loop index
+      INTEGER,                 DIMENSION(6)                 :: i_tracer         ! Tracer indices
+      REAL(wp)                                              :: zmassc           ! Carbon molar mass
+      REAL(wp)                                              :: zmassn           ! Nitrogen molar mass
+      REAL(wp)                                              :: z4qnc            ! Z4/qnc (mesozoo N:C)
+      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            ! Fractions
+      REAL(wp)                                              :: zfrac_chl1       ! 
+      REAL(wp)                                              :: zfrac_chl2       ! 
+      REAL(wp)                                              :: zfrac_chl3       ! 
+      REAL(wp)                                              :: zfrac_chl4       ! 
+      REAL(wp)                                              :: zfrac_p1n        ! 
+      REAL(wp)                                              :: zfrac_p2n        ! 
+      REAL(wp)                                              :: zfrac_p3n        ! 
+      REAL(wp)                                              :: zfrac_p4n        ! 
+      REAL(wp)                                              :: zfrac_z4n        ! 
+      REAL(wp)                                              :: zfrac_z5n        ! 
+      REAL(wp)                                              :: zfrac_z6n        ! 
+      REAL(wp)                                              :: zfrac_n3n        ! 
+      REAL(wp)                                              :: zfrac_n4n        ! 
+      REAL(wp)                                              :: zfrac_r4n        ! 
+      REAL(wp)                                              :: zfrac_r6n        ! 
+      REAL(wp)                                              :: zfrac_r8n        ! 
+      REAL(wp)                                              :: zrat_chl1_p1n    ! Ratios
+      REAL(wp)                                              :: zrat_p1c_p1n     ! 
+      REAL(wp)                                              :: zrat_p1p_p1n     ! 
+      REAL(wp)                                              :: zrat_p1s_p1n     ! 
+      REAL(wp)                                              :: zrat_chl2_p2n    ! 
+      REAL(wp)                                              :: zrat_p2c_p2n     ! 
+      REAL(wp)                                              :: zrat_p2p_p2n     ! 
+      REAL(wp)                                              :: zrat_chl3_p3n    ! 
+      REAL(wp)                                              :: zrat_p3c_p3n     ! 
+      REAL(wp)                                              :: zrat_p3p_p3n     ! 
+      REAL(wp)                                              :: zrat_chl4_p4n    ! 
+      REAL(wp)                                              :: zrat_p4c_p4n     ! 
+      REAL(wp)                                              :: zrat_p4p_p4n     ! 
+      REAL(wp)                                              :: zrat_z4c_z4n     ! 
+      REAL(wp)                                              :: zrat_z5c_z5n     ! 
+      REAL(wp)                                              :: zrat_z5p_z5n     ! 
+      REAL(wp)                                              :: zrat_z6c_z6n     ! 
+      REAL(wp)                                              :: zrat_z6p_z6n     ! 
+      REAL(wp)                                              :: zrat_r4c_r4n     ! 
+      REAL(wp)                                              :: zrat_r4p_r4n     ! 
+      REAL(wp)                                              :: zrat_r6c_r6n     ! 
+      REAL(wp)                                              :: zrat_r6p_r6n     ! 
+      REAL(wp)                                              :: zrat_r6s_r6n     ! 
+      REAL(wp)                                              :: zrat_r8c_r8n     ! 
+      REAL(wp)                                              :: zrat_r8p_r8n     ! 
+      REAL(wp)                                              :: zrat_r8s_r8n     ! 
+      REAL(wp)                                              :: zrat_p1c_chl1    ! 
+      REAL(wp)                                              :: zrat_p1n_chl1    ! 
+      REAL(wp)                                              :: zrat_p1p_chl1    ! 
+      REAL(wp)                                              :: zrat_p1s_chl1    ! 
+      REAL(wp)                                              :: zrat_p2c_chl2    ! 
+      REAL(wp)                                              :: zrat_p2n_chl2    ! 
+      REAL(wp)                                              :: zrat_p2p_chl2    ! 
+      REAL(wp)                                              :: zrat_p3c_chl3    ! 
+      REAL(wp)                                              :: zrat_p3n_chl3    ! 
+      REAL(wp)                                              :: zrat_p3p_chl3    ! 
+      REAL(wp)                                              :: zrat_p4c_chl4    ! 
+      REAL(wp)                                              :: zrat_p4n_chl4    ! 
+      REAL(wp)                                              :: zrat_p4p_chl4    ! 
+      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
+      REAL(wp),                DIMENSION(jpi,jpj)           :: pinc_chltot_temp ! Temporary array
+      INTEGER,                 DIMENSION(1)                 :: zkmt             ! No. sea points in column
       !!---------------------------------------------------------------------------
       
@@ -300,7 +301,7 @@
          modparm(8)  = 0.05                              ! z_mort_1
          modparm(9)  = 1.0                               ! z_mort_2
-         modparm(10) = 6.625                             ! c2n_p
-         modparm(11) = 5.625                             ! c2n_z
-         modparm(12) = 7.5                               ! c2n_d
+         !modparm(10) = 6.625                            ! c2n_p - set later per grid point
+         !modparm(11) = 5.625                            ! c2n_z - set later per grid point
+         !modparm(12) = 7.5                              ! c2n_d - set later per grid point
          modparm(13) = 0.01                              ! graze_threshold
          modparm(14) = 2.0                               ! holling_coef
@@ -372,20 +373,49 @@
             END DO
          END DO
-         n2be_p = zmassn + ( zmassc * modparm(10) )
-         n2be_z = zmassn + ( zmassc * modparm(11) )
-         n2be_d = zmassn + ( zmassc * modparm(12) )
-
-         ! 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 )
+
+         ! Call nitrogen balancing routine - loop over grid points due to variable C:N ratios
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               ! Phytoplankton C:N
+               modparm(10) = ( tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_p1c) + &
+                  &            tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_p2c) + &
+                  &            tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_p3c) + &
+                  &            tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_p4c)     ) / &
+                  &          ( tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_p1n) + &
+                  &            tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_p2n) + &
+                  &            tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_p3n) + &
+                  &            tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_p4n)     )
+               ! Zooplankton C:N
+               modparm(11) = ( tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_z4c) + &
+                  &            tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_z5c) + &
+                  &            tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_z6c)     ) / &
+                  &          ( (tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_z4c) * z4qnc) + &
+                  &            tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_z5n) + &
+                  &            tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_z6n)     )
+               ! Detritus C:N
+               modparm(12) = ( tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_r4c) + &
+                  &            tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_r6c) + &
+                  &            tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_r8c)     ) / &
+                  &          ( tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_r4n) + &
+                  &            tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_r6n) + &
+                  &            tracer_bkg(ji,jj,1,jp_fabm_m1+jp_fabm_r8n)     )
+               ! Nitrogen to biomass equivalent for PZD
+               n2be_p = zmassn + ( zmassc * modparm(10) )
+               n2be_z = zmassn + ( zmassc * modparm(11) )
+               n2be_d = zmassn + ( zmassc * modparm(12) )
+               zkmt(:) = INT(SUM(tmask(ji,jj,:)))
+               CALL bio_analysis( 1, 1, jpk, gdepw_n(ji,jj,2:jpk), i_tracer, modparm,                 &
+                  &               n2be_p, n2be_z, n2be_d, assimparm,                                  &
+                  &               INT(pincper), 1, zkmt, tmask(ji,jj,:),                              &
+                  &               pmld(ji,jj), mld_max_bkg(ji,jj), pinc_chltot(ji,jj), cchl_p(ji,jj), &
+                  &               nbal_active, phyt_avg_bkg(ji,jj),                                   &
+                  &               gl_active, pgrow_avg_bkg(ji,jj), ploss_avg_bkg(ji,jj),              &
+                  &               subsurf_active, deepneg_active,                                     &
+                  &               deeppos_active, nutprof_active,                                     &
+                  &               bstate(ji,jj,:,:), outincs(ji,jj,:,:),                              &
+                  &               diag_active, diag(ji,jj,:),                                         &
+                  &               diag_fulldepth_active, diag_fulldepth(ji,jj,:,:) )
+            END DO
+         END DO
          
          ! Loop over each grid point partioning the increments
