Index: /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/CONFIG/SHARED/namelist_ref
===================================================================
--- /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/CONFIG/SHARED/namelist_ref	(revision 11882)
+++ /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/CONFIG/SHARED/namelist_ref	(revision 11883)
@@ -51,4 +51,5 @@
    ln_clobber  = .false.   !  clobber (overwrite) an existing file
    nn_chunksz  =       0   !  chunksize (bytes) for NetCDF file (works only with iom_nf90 routines)
+   nn_abort    =       1   !  output the final state (1) or not (0) in case of model error
 /
 !
@@ -406,4 +407,5 @@
    rn_antarctica_calving_fraction = 0.5 ! Set fraction of total freshwater flux for iceberg calving - remainder goes to iceshelf melting.
    rn_iceshelf_fluxes_tolerance = 1e-6  ! Fractional threshold for detecting differences in icesheet masses (must be positive definite).
+   ln_fix_sea_ice_fluxes = .false. ! Fix sea ice heat flux coupling issues
 /
 !-----------------------------------------------------------------------
Index: /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/DIA/diawri.F90
===================================================================
--- /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/DIA/diawri.F90	(revision 11882)
+++ /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/DIA/diawri.F90	(revision 11883)
@@ -1123,7 +1123,4 @@
 #endif
 
-      IF (cdfile_name == "output.abort") THEN
-         CALL ctl_stop('MPPSTOP', 'NEMO abort from dia_wri_state')
-      END IF
        
 !     IF( nn_timing == 1 )   CALL timing_stop('dia_wri_state') ! not sure this works for routines not called in first timestep
Index: /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90
===================================================================
--- /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90	(revision 11882)
+++ /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/DOM/domain.F90	(revision 11883)
@@ -137,5 +137,5 @@
       NAMELIST/namrun/ cn_ocerst_indir, cn_ocerst_outdir, nn_stocklist, ln_rst_list,               &
          &             nn_no   , cn_exp    , cn_ocerst_in, cn_ocerst_out, ln_rstart , ln_rstdate, nn_rstctl,   &
-         &             nn_it000, nn_itend  , nn_date0    , nn_time0,      nn_leapy     , nn_istate , nn_stock ,   &
+         &             nn_it000, nn_itend  , nn_date0    , nn_time0,      nn_leapy     , nn_istate , nn_abort, nn_stock ,   &
          &             nn_write, ln_dimgnnn, ln_mskland  , ln_cfmeta    , ln_clobber, nn_chunksz, nn_euler
       NAMELIST/namdom/ nn_bathy, rn_bathy , rn_e3zps_min, rn_e3zps_rat, nn_msh, rn_hmin,   &
@@ -183,4 +183,5 @@
          WRITE(numout,*) '      leap year calendar (0/1)        nn_leapy   = ', nn_leapy
          WRITE(numout,*) '      initial state output            nn_istate  = ', nn_istate
+         WRITE(numout,*) '      abort state output              nn_abort   = ', nn_abort
          IF( ln_rst_list ) THEN
             WRITE(numout,*) '      list of restart dump times      nn_stocklist   =', nn_stocklist
Index: /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90
===================================================================
--- /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90	(revision 11882)
+++ /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/IOM/in_out_manager.F90	(revision 11883)
@@ -41,4 +41,5 @@
    INTEGER       ::   nn_leapy         !: Leap year calendar flag (0/1 or 30)
    INTEGER       ::   nn_istate        !: initial state output flag (0/1)
+   INTEGER       ::   nn_abort         !: Output final state in case of error flag (0/1)
    INTEGER       ::   nn_write         !: model standard output frequency
    INTEGER       ::   nn_stock         !: restart file frequency
Index: /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/SBC/cpl_oasis3.F90
===================================================================
--- /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/SBC/cpl_oasis3.F90	(revision 11882)
+++ /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/SBC/cpl_oasis3.F90	(revision 11883)
@@ -151,5 +151,14 @@
       INTEGER :: id_part
       INTEGER :: paral(5)       ! OASIS3 box partition
-      INTEGER :: ishape(2,2)    ! shape of arrays passed to PSMILe
+      INTEGER :: ishape(4)      ! Shape of 2D arrays passed to PSMILe. 
+                                ! Redundant from OASIS3-MCT vn4.0 onwards but required
+                                ! to satisfy interface and for backward compatibility.
+      INTEGER :: ishape0d1d(2)  ! Shape of 0D or 1D arrays passed to PSMILe.
+      INTEGER :: var_nodims(2)  ! Number of coupling field dimensions.
+                                ! var_nodims(1) is redundant from OASIS3-MCT vn4.0 onwards
+                                ! but retained for backward compatibility. 
+                                ! var_nodims(2) is the number of fields in a bundle 
+                                ! or 1 for unbundled fields (bundles are not yet catered for
+                                ! in NEMO hence we default to 1).  
       INTEGER :: ji,jc,jm       ! local loop indicees
       CHARACTER(LEN=64) :: zclname
@@ -182,6 +191,12 @@
       !     nl* is set to the global values 1 and jp*glo.
       !
-      ishape(:,1) = (/ 1, nlei-nldi+1 /)
-      ishape(:,2) = (/ 1, nlej-nldj+1 /)
+      ishape(1) = 1
+      ishape(2) = nlei-nldi+1
+      ishape(3) = 1
+      ishape(4) = nlej-nldj+1
+
+      ishape0d1d(1) = 0
+      ishape0d1d(2) = 0
+
       !
       ! ... Allocate memory for data exchange
@@ -243,5 +258,11 @@
 #endif
                   IF( ln_ctl ) WRITE(numout,*) "Define", ji, jc, jm, " "//TRIM(zclname), " for ", OASIS_Out
-                  CALL oasis_def_var (ssnd(ji)%nid(jc,jm), zclname, id_part   , (/ 2, 0 /),   &
+
+                  !
+                  ! ... Set the field dimension and bundle count
+                  var_nodims(1) = 2
+                  var_nodims(2) = 1 ! Modify this value to cater for bundled fields.    
+
+                  CALL oasis_def_var (ssnd(ji)%nid(jc,jm), zclname, id_part   , var_nodims,   &
                      &                OASIS_Out          , ishape , OASIS_REAL, nerror )
                   IF ( nerror /= OASIS_Ok ) THEN
@@ -288,6 +309,10 @@
 #endif
                   IF( ln_ctl ) WRITE(numout,*) "Define", ji, jc, jm, " "//TRIM(zclname), " for ", OASIS_In
-                  CALL oasis_def_var (srcv(ji)%nid(jc,jm), zclname, id_part   , (/ 2, 0 /),   &
-                     &                OASIS_In           , ishape , OASIS_REAL, nerror )
+
+                  var_nodims(1) = 2
+                  var_nodims(2) = 1 ! Modify this value to cater for bundled fields.     
+
+                  CALL oasis_def_var (srcv(ji)%nid(jc,jm), zclname, id_part   , var_nodims,   &
+                                      OASIS_In           , ishape , OASIS_REAL, nerror )
                   IF ( nerror /= OASIS_Ok ) THEN
                      WRITE(numout,*) 'Failed to define transient ', ji, jc, jm, " "//TRIM(zclname)
Index: /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/SBC/sbc_ice.F90
===================================================================
--- /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/SBC/sbc_ice.F90	(revision 11882)
+++ /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/SBC/sbc_ice.F90	(revision 11883)
@@ -124,5 +124,6 @@
 #if defined key_lim2 || defined key_cice
    ! already defined in ice.F90 for LIM3
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::  a_i
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::  a_i                ! Sea ice fraction on categories
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::  a_i_last_couple    ! Sea ice fraction on categories at the last coupling point
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::  ht_i, ht_s
 #endif
Index: /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/SBC/sbccpl.F90
===================================================================
--- /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/SBC/sbccpl.F90	(revision 11882)
+++ /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/SBC/sbccpl.F90	(revision 11883)
@@ -54,4 +54,8 @@
    USE lib_fortran, ONLY: glob_sum
 
+#if defined key_oasis3
+   USE mod_oasis, ONLY : OASIS_Sent, OASIS_ToRest, OASIS_SentOut, OASIS_ToRestOut
+#endif
+
    IMPLICIT NONE
    PRIVATE
@@ -154,4 +158,12 @@
 	                                              ! i.e. specifically nmol/L (= umol/m3)
 
+#if ! defined key_oasis3
+   ! Dummy variables to enable compilation when oasis3 is not being used
+   INTEGER                    ::   OASIS_Sent        = -1
+   INTEGER                    ::   OASIS_SentOut     = -1
+   INTEGER                    ::   OASIS_ToRest      = -1
+   INTEGER                    ::   OASIS_ToRestOut   = -1
+#endif
+
    !                                                         !!** namelist namsbc_cpl **
    TYPE ::   FLD_C
@@ -175,4 +187,6 @@
    LOGICAL     ::   ln_usecplmask          !  use a coupling mask file to merge data received from several models
                                            !   -> file cplmask.nc with the float variable called cplmask (jpi,jpj,nn_cplmodel)
+   LOGICAL, PUBLIC ::   ln_fix_sea_ice_fluxes     ! Apply sea ice flux bug fixes (GMED#449)
+
    TYPE ::   DYNARR     
       REAL(wp), POINTER, DIMENSION(:,:,:)    ::   z3   
@@ -200,5 +214,5 @@
       !!             ***  FUNCTION sbc_cpl_alloc  ***
       !!----------------------------------------------------------------------
-      INTEGER :: ierr(3)
+      INTEGER :: ierr(4)
       !!----------------------------------------------------------------------
       ierr(:) = 0
@@ -212,5 +226,8 @@
       ! Hardwire only two models as nn_cplmodel has not been read in
       ! from the namelist yet.
-      ALLOCATE( xcplmask(jpi,jpj,0:2) , STAT=ierr(3) )   
+      ALLOCATE( xcplmask(jpi,jpj,0:2) , STAT=ierr(3) ) 
+#if defined key_cice
+      ALLOCATE( a_i_last_couple(jpi,jpj,jpl) , STAT=ierr(4) ) 
+#endif
       !
       sbc_cpl_alloc = MAXVAL( ierr )
@@ -247,5 +264,6 @@
          &                  ln_usecplmask, nn_coupled_iceshelf_fluxes, ln_iceshelf_init_atmos,        &
          &                  rn_greenland_total_fw_flux, rn_greenland_calving_fraction, &
-         &                  rn_antarctica_total_fw_flux, rn_antarctica_calving_fraction, rn_iceshelf_fluxes_tolerance
+         &                  rn_antarctica_total_fw_flux, rn_antarctica_calving_fraction, rn_iceshelf_fluxes_tolerance, &
+         &                  ln_fix_sea_ice_fluxes
       !!---------------------------------------------------------------------
 
@@ -317,4 +335,5 @@
          WRITE(numout,*)'  nn_cplmodel                         = ', nn_cplmodel
          WRITE(numout,*)'  ln_usecplmask                       = ', ln_usecplmask
+         WRITE(numout,*)'  ln_fix_sea_ice_fluxes               = ', ln_fix_sea_ice_fluxes
          WRITE(numout,*)'  nn_coupled_iceshelf_fluxes          = ', nn_coupled_iceshelf_fluxes
          WRITE(numout,*)'  ln_iceshelf_init_atmos              = ', ln_iceshelf_init_atmos
@@ -1758,7 +1777,15 @@
          IF ( TRIM(sn_rcv_emp%clcat) == 'yes' ) THEN
             ! zemp_ice is the sum of frcv(jpr_ievp)%z3(:,:,1) over all layers - snow
-            zemp_ice(:,:) = - frcv(jpr_snow)%z3(:,:,1)
+            IF ( ln_fix_sea_ice_fluxes ) THEN
+               zemp_ice(:,:) = - frcv(jpr_snow)%z3(:,:,1) * zicefr(:,:)
+            ELSE
+               zemp_ice(:,:) = - frcv(jpr_snow)%z3(:,:,1)
+            ENDIF
             DO jl=1,jpl
-               zemp_ice(:,:   ) = zemp_ice(:,:) + frcv(jpr_ievp)%z3(:,:,jl)
+               IF ( ln_fix_sea_ice_fluxes ) THEN
+                  zemp_ice(:,:   ) = zemp_ice(:,:) + frcv(jpr_ievp)%z3(:,:,jl) * a_i_last_couple(:,:,jl)
+               ELSE
+                  zemp_ice(:,:   ) = zemp_ice(:,:) + frcv(jpr_ievp)%z3(:,:,jl)
+               ENDIF
             ENDDO
             ! latent heat coupled for each category in CICE
@@ -2269,4 +2296,17 @@
          END SELECT
          IF( ssnd(jps_fice)%laction )   CALL cpl_snd( jps_fice, isec, ztmp3, info )
+
+#if defined key_lim2 || defined key_cice
+         ! If this coupling was successful then save ice fraction for use between coupling points.
+         ! This is needed for some calculations where the ice fraction at the last coupling point
+         ! is needed.
+         IF( info == OASIS_Sent     .OR. info == OASIS_ToRest .OR.   &
+                     & info == OASIS_SentOut  .OR. info == OASIS_ToRestOut ) THEN
+           IF ( sn_snd_thick%clcat == 'yes' ) THEN
+             a_i_last_couple(:,:,:) = a_i(:,:,:)
+           ENDIF
+         ENDIF
+#endif
+         
       ENDIF
       
Index: /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/SBC/sbcice_cice.F90
===================================================================
--- /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/SBC/sbcice_cice.F90	(revision 11882)
+++ /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/SBC/sbcice_cice.F90	(revision 11883)
@@ -395,5 +395,9 @@
          ELSE IF (ksbc == jp_purecpl) THEN
             DO jl=1,ncat
-               ztmpn(:,:,jl)=qla_ice(:,:,jl)*a_i(:,:,jl)
+               IF ( ln_fix_sea_ice_fluxes ) THEN
+                  ztmpn(:,:,jl)=qla_ice(:,:,jl)*a_i_last_couple(:,:,jl)
+               ELSE
+                  ztmpn(:,:,jl)=qla_ice(:,:,jl)*a_i(:,:,jl)
+               ENDIF
             ENDDO
 	 ELSE
@@ -408,5 +412,9 @@
 !  Convert to GBM
             IF (ksbc == jp_flx .OR. ksbc == jp_purecpl) THEN
-               ztmp(:,:) = botmelt(:,:,jl)*a_i(:,:,jl)
+               IF ( ln_fix_sea_ice_fluxes ) THEN
+                  ztmp(:,:) = botmelt(:,:,jl)*a_i_last_couple(:,:,jl)
+               ELSE
+                  ztmp(:,:) = botmelt(:,:,jl)*a_i(:,:,jl)
+               ENDIF
             ELSE
                ztmp(:,:) = botmelt(:,:,jl)
@@ -417,5 +425,9 @@
 !  Convert to GBM
             IF (ksbc == jp_flx .OR. ksbc == jp_purecpl) THEN
-               ztmp(:,:) = (topmelt(:,:,jl)+botmelt(:,:,jl))*a_i(:,:,jl) 
+               IF ( ln_fix_sea_ice_fluxes ) THEN
+                  ztmp(:,:) = (topmelt(:,:,jl)+botmelt(:,:,jl))*a_i_last_couple(:,:,jl) 
+               ELSE
+                  ztmp(:,:) = (topmelt(:,:,jl)+botmelt(:,:,jl))*a_i(:,:,jl)
+               ENDIF
             ELSE
                ztmp(:,:) = (topmelt(:,:,jl)+botmelt(:,:,jl))
Index: /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/step.F90
===================================================================
--- /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/step.F90	(revision 11882)
+++ /branches/UKMO/dev_r5518_GO6_starthour_obsoper/NEMOGCM/NEMO/OPA_SRC/step.F90	(revision 11883)
@@ -374,5 +374,6 @@
       IF( indic < 0        )   THEN
                                CALL ctl_stop( 'step: indic < 0' )
-                               CALL dia_wri_state( 'output.abort', kstp )
+                               IF( nn_abort > 0 ) CALL dia_wri_state( 'output.abort', kstp )
+                               CALL ctl_stop('MPPSTOP', 'NEMO abort from stp')
       ENDIF
       IF( kstp == nit000   )   THEN
