Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/ice.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/ice.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/ice.F90	(revision 5038)
@@ -105,5 +105,5 @@
    !! ** Global variables                                                 |
    !!-------------|-------------|---------------------------------|-------|
-   !! a_i         | a_i_b       |    Ice concentration            |       |
+   !! a_i         | a_i_1d      |    Ice concentration            |       |
    !! v_i         |      -      |    Ice volume per unit area     | m     |
    !! v_s         |      -      |    Snow volume per unit area    | m     |
@@ -111,7 +111,7 @@
    !! oa_i        !      -      !    Sea ice areal age content    | day   |
    !! e_i         !      -      !    Ice enthalpy                 | 10^9 J| 
-   !!      -      ! q_i_b       !    Ice enthalpy per unit vol.   | J/m3  | 
+   !!      -      ! q_i_1d      !    Ice enthalpy per unit vol.   | J/m3  | 
    !! e_s         !      -      !    Snow enthalpy                | 10^9 J| 
-   !!      -      ! q_s_b       !    Snow enthalpy per unit vol.  | J/m3  | 
+   !!      -      ! q_s_1d      !    Snow enthalpy per unit vol.  | J/m3  | 
    !!                                                                     |
    !!-------------|-------------|---------------------------------|-------|
@@ -120,12 +120,12 @@
    !!-------------|-------------|---------------------------------|-------|
    !!                                                                     |
-   !! ht_i        | ht_i_b      |    Ice thickness                | m     |
-   !! ht_s        ! ht_s_b      |    Snow depth                   | m     |
-   !! sm_i        ! sm_i_b      |    Sea ice bulk salinity        ! ppt   |
-   !! s_i         ! s_i_b       |    Sea ice salinity profile     ! ppt   |
+   !! ht_i        | ht_i_1d     |    Ice thickness                | m     |
+   !! ht_s        ! ht_s_1d     |    Snow depth                   | m     |
+   !! sm_i        ! sm_i_1d     |    Sea ice bulk salinity        ! ppt   |
+   !! s_i         ! s_i_1d      |    Sea ice salinity profile     ! ppt   |
    !! o_i         !      -      |    Sea ice Age                  ! days  |
-   !! t_i         ! t_i_b       |    Sea ice temperature          ! K     |
-   !! t_s         ! t_s_b       |    Snow temperature             ! K     |
-   !! t_su        ! t_su_b      |    Sea ice surface temperature  ! K     |
+   !! t_i         ! t_i_1d      |    Sea ice temperature          ! K     |
+   !! t_s         ! t_s_1d      |    Snow temperature             ! K     |
+   !! t_su        ! t_su_1d     |    Sea ice surface temperature  ! K     |
    !!                                                                     |
    !! notes: the ice model only sees a bulk (i.e., vertically averaged)   |
@@ -142,5 +142,5 @@
    !! ***         Category-summed state variables (diagnostic)        *** |
    !! ******************************************************************* |
-   !! at_i        | at_i_b      |    Total ice concentration      |       |
+   !! at_i        | at_i_1d     |    Total ice concentration      |       |
    !! vt_i        |      -      |    Total ice vol. per unit area | m     |
    !! vt_s        |      -      |    Total snow vol. per unit ar. | m     |
@@ -166,27 +166,17 @@
 
    !                                     !!** ice-dynamic namelist (namicedyn) **
-   INTEGER , PUBLIC ::   nbiter           !: number of sub-time steps for relaxation
-   INTEGER , PUBLIC ::   nbitdr           !: maximum number of iterations for relaxation
    INTEGER , PUBLIC ::   nevp             !: number of iterations for subcycling
-   INTEGER , PUBLIC ::   nlay_i = 5            !: number of layers in the ice
-
-   !                                     !!** ice-dynamic namelist (namicedyn) **
    REAL(wp), PUBLIC ::   epsd             !: tolerance parameter for dynamic
-   REAL(wp), PUBLIC ::   alpha            !: coefficient for semi-implicit coriolis
-   REAL(wp), PUBLIC ::   dm               !: diffusion constant for dynamics
    REAL(wp), PUBLIC ::   om               !: relaxation constant
-   REAL(wp), PUBLIC ::   resl             !: maximum value for the residual of relaxation
    REAL(wp), PUBLIC ::   cw               !: drag coefficient for oceanic stress
-   REAL(wp), PUBLIC ::   angvg            !: turning angle for oceanic stress
    REAL(wp), PUBLIC ::   pstar            !: determines ice strength (N/M), Hibler JPO79
    REAL(wp), PUBLIC ::   c_rhg            !: determines changes in ice strength
-   REAL(wp), PUBLIC ::   etamn            !: minimun value for viscosity : has to be 0
    REAL(wp), PUBLIC ::   creepl           !: creep limit : has to be under 1.0e-9
    REAL(wp), PUBLIC ::   ecc              !: eccentricity of the elliptical yield curve
    REAL(wp), PUBLIC ::   ahi0             !: sea-ice hor. eddy diffusivity coeff. (m2/s)
-   REAL(wp), PUBLIC ::   telast           !: timescale for elastic waves (s) !SB
-   REAL(wp), PUBLIC ::   alphaevp         !: coeficient of the internal stresses !SB
-   REAL(wp), PUBLIC ::   unit_fac = 1.e+09_wp  !: conversion factor for ice / snow enthalpy
-   REAL(wp), PUBLIC ::   hminrhg = 0.001_wp    !: clem : ice volume (a*h, in m) below which ice velocity is set to ocean velocity
+   REAL(wp), PUBLIC ::   telast           !: timescale for elastic waves (s)
+   REAL(wp), PUBLIC ::   relast           !: ratio => telast/rdt_ice (1/3 or 1/9 depending on nb of subcycling nevp) 
+   REAL(wp), PUBLIC ::   alphaevp         !: coeficient of the internal stresses 
+   REAL(wp), PUBLIC ::   hminrhg          !: ice volume (a*h, in m) below which ice velocity is set to ocean velocity
 
    !                                     !!** ice-salinity namelist (namicesal) **
@@ -202,10 +192,9 @@
 
    !                                     !!** ice-salinity namelist (namicesal) **
-   INTEGER , PUBLIC ::   num_sal          !: salinity configuration used in the model
+   INTEGER , PUBLIC ::   num_sal             !: salinity configuration used in the model
    !                                         ! 1 - constant salinity in both space and time
    !                                         ! 2 - prognostic salinity (s(z,t))
    !                                         ! 3 - salinity profile, constant in time
-   INTEGER , PUBLIC ::   sal_prof    = 1           !: salinity profile or not 
-   INTEGER , PUBLIC ::   thcon_i_swi          !: thermal conductivity: =1 Untersteiner (1964) ; =2 Pringle et al (2007)
+   INTEGER , PUBLIC ::   thcon_i_swi         !: thermal conductivity: =0 Untersteiner (1964) ; =1 Pringle et al (2007)
 
    !                                     !!** ice-mechanical redistribution namelist (namiceitdme)
@@ -220,5 +209,4 @@
    REAL(wp), PUBLIC ::   Craft            !: coefficient for smoothness of the hyperbolic tangent in rafting
    REAL(wp), PUBLIC ::   ridge_por        !: initial porosity of ridges (0.3 regular value)
-   REAL(wp), PUBLIC ::   sal_max_ridge    !: maximum ridged ice salinity (ppt)
    REAL(wp), PUBLIC ::   betas            !: coef. for partitioning of snowfall between leads and sea ice
    REAL(wp), PUBLIC ::   kappa_i          !: coef. for the extinction of radiation Grenfell et al. (2006) [1/m]
@@ -228,13 +216,19 @@
    !                                     !!** ice-mechanical redistribution namelist (namiceitdme)
    INTEGER , PUBLIC ::   ridge_scheme_swi !: scheme used for ice ridging
-   INTEGER , PUBLIC ::   raftswi          !: rafting of ice or not                        
+   INTEGER , PUBLIC ::   raft_swi         !: rafting of ice or not                        
    INTEGER , PUBLIC ::   partfun_swi      !: participation function: =0 Thorndike et al. (1975), =1 Lipscomb et al. (2007)
-   INTEGER , PUBLIC ::   transfun_swi     !: transfer function: =0 Hibler 1980, =1 Lipscomb et al. 2007
    INTEGER , PUBLIC ::   brinstren_swi    !: use brine volume to diminish ice strength
 
    REAL(wp), PUBLIC ::   usecc2           !:  = 1.0 / ( ecc * ecc )
    REAL(wp), PUBLIC ::   rhoco            !: = rau0 * cw
-   REAL(wp), PUBLIC ::   sangvg, cangvg   !: sin and cos of the turning angle for ocean stress
-   REAL(wp), PUBLIC ::   pstarh           !: pstar / 2.0
+
+   !                                     !!** switch for presence of ice or not 
+   REAL(wp), PUBLIC ::   rswitch
+
+   !                                     !!** define some parameters 
+   REAL(wp), PUBLIC, PARAMETER ::   unit_fac = 1.e+09_wp  !: conversion factor for ice / snow enthalpy
+   REAL(wp), PUBLIC, PARAMETER ::   epsi06   = 1.e-06_wp  !: small number 
+   REAL(wp), PUBLIC, PARAMETER ::   epsi10   = 1.e-10_wp  !: small number 
+   REAL(wp), PUBLIC, PARAMETER ::   epsi20   = 1.e-20_wp  !: small number 
 
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   u_oce, v_oce   !: surface ocean velocity used in ice dynamics
@@ -249,46 +243,60 @@
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   shear_i        !: Shear of the velocity field [s-1]
    !
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   firic       !: IR flux over the ice (diag only)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fcsic       !: Sensible heat flux over the ice (diag only)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fleic       !: Latent heat flux over the ice (diag only)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   qlatic      !: latent flux
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   rdvosif     !: Variation of volume at surface (diag only)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   rdvobif     !: Variation of ice volume at the bottom ice (diag only)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fdvolif     !: Total variation of ice volume (diag only)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   rdvonif     !: Lateral Variation of ice volume (diag only)
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   sist        !: Average Sea-Ice Surface Temperature [Kelvin]
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   icethi      !: total ice thickness (for all categories) (diag only)
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   t_bo        !: Sea-Ice bottom temperature [Kelvin]     
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hicifp      !: Ice production/melting==>!obsolete... can be removed
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   frld        !: Leads fraction = 1 - ice fraction
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   pfrld       !: Leads fraction at previous time  
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   phicif      !: Old ice thickness
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fbif        !: Heat flux at the ice base
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   rdm_snw     !: Variation of snow mass over 1 time step     [Kg/m2]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   rdq_snw     !: Heat content associated with rdm_snw        [J/m2]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   rdm_ice     !: Variation of ice mass over 1 time step      [Kg/m2]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   rdq_ice     !: Heat content associated with rdm_ice        [J/m2]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   qldif       !: heat balance of the lead (or of the open ocean)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   qcmif       !: Energy needed to bring the ocean to freezing 
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fdtcn       !: net downward heat flux from the ice to the ocean
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   qdtcn       !: energy from the ice to the ocean
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fstric      !: transmitted solar radiation under ice
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fscmbq      !: associated with lead chipotage with solar flux
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   ffltbif     !: related to max heat contained in brine pockets (?)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fsbbq       !: Also linked with the solar flux below the ice (?)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   qfvbq       !: store energy in case of total lateral ablation (?)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   dmgwi       !: Variation of the mass of snow ice
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   sfx_thd     !: salt flux due to ice growth/melt                      [PSU/m2/s]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   qlead       !: heat balance of the lead (or of the open ocean)
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fhtur       !: net downward heat flux from the ice to the ocean
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fhld        !: heat flux from the lead used for bottom melting
+
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   wfx_snw    !: snow-ocean mass exchange over 1 time step [kg/m2]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   wfx_spr    !: snow precipitation on ice over 1 time step [kg/m2]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   wfx_sub    !: snow sublimation over 1 time step [kg/m2]
+
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   wfx_ice    !: ice-ocean mass exchange over 1 time step [kg/m2]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   wfx_sni    !: snow ice growth component of wfx_ice [kg/m2]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   wfx_opw    !: lateral ice growth component of wfx_ice [kg/m2]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   wfx_bog    !: bottom ice growth component of wfx_ice [kg/m2]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   wfx_dyn    !: dynamical ice growth component of wfx_ice [kg/m2]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   wfx_bom    !: bottom melt component of wfx_ice [kg/m2]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   wfx_sum    !: surface melt component of wfx_ice [kg/m2]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   wfx_res    !: residual component of wfx_ice [kg/m2]
+
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   sfx_bog     !: salt flux due to ice growth/melt                      [PSU/m2/s]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   sfx_bom     !: salt flux due to ice growth/melt                      [PSU/m2/s]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   sfx_sum     !: salt flux due to ice growth/melt                      [PSU/m2/s]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   sfx_sni     !: salt flux due to ice growth/melt                      [PSU/m2/s]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   sfx_opw     !: salt flux due to ice growth/melt                      [PSU/m2/s]
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   sfx_bri     !: salt flux due to brine rejection                      [PSU/m2/s]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   sfx_mec     !: salt flux due to porous ridged ice formation          [PSU/m2/s]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   sfx_dyn     !: salt flux due to porous ridged ice formation          [PSU/m2/s]
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   sfx_res     !: residual salt flux due to correction of ice thickness [PSU/m2/s]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fhbri       !: heat flux due to brine rejection
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fheat_mec   !: heat flux associated with porous ridged ice formation [???]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fheat_res   !: residual heat flux due to correction of ice thickness
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fmmec       !: mass flux due to snow loss during compression         [Kg/m2/s]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fhmec       !: heat flux due to snow loss during compression
+
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hfx_bog     !: total heat flux causing bottom ice growth 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hfx_bom     !: total heat flux causing bottom ice melt 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hfx_sum     !: total heat flux causing surface ice melt 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hfx_opw     !: total heat flux causing open water ice formation
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hfx_dif     !: total heat flux causing Temp change in the ice 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hfx_snw     !: heat flux for snow melt 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hfx_err     !: heat flux error after heat diffusion 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hfx_err_rem !: heat flux error after heat remapping 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hfx_in      !: heat flux available for thermo transformations 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hfx_out     !: heat flux remaining at the end of thermo transformations 
+
+   ! heat flux associated with ice-atmosphere mass exchange
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hfx_sub     !: heat flux for sublimation 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hfx_spr     !: heat flux of the snow precipitation 
+
+   ! heat flux associated with ice-ocean mass exchange
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hfx_thd     !: ice-ocean heat flux from thermo processes (limthd_dh) 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hfx_dyn     !: ice-ocean heat flux from mecanical processes (limitd_me) 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hfx_res     !: residual heat flux due to correction of ice thickness
+
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   ftr_ice   !: transmitted solar radiation under ice
 
    ! temporary arrays for dummy version of the code
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   dh_i_surf2D, dh_i_bott2D, fstbif, fsup2D, focea2D, q_s
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   dh_i_surf2D, dh_i_bott2D, q_s
 
    !!--------------------------------------------------------------------------
@@ -321,11 +329,6 @@
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   smt_i          !: mean sea ice salinity averaged over all categories [PSU]
 
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   at_i_typ     !: total area   contained in each ice type [m^2]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   vt_i_typ     !: total volume contained in each ice type [m^3]
-
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::   t_s        !: Snow temperatures [K]
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::   e_s        !: Snow ...      
-
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:)   ::   e_i_cat    !: ! go to trash
       
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::   t_i        !: ice temperatures          [K]
@@ -348,9 +351,9 @@
    !! * Old values of global variables
    !!--------------------------------------------------------------------------
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:)   ::   old_v_s, old_v_i               !: snow and ice volumes
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:)   ::   old_a_i, old_smv_i, old_oa_i   !: ???
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::   old_e_s                        !: snow heat content
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::   old_e_i                        !: ice temperatures
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)     ::   old_u_ice, old_v_ice           !: ice velocity (gv6 and gv7)
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:)   ::   v_s_b, v_i_b               !: snow and ice volumes
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:)   ::   a_i_b, smv_i_b, oa_i_b     !:
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::   e_s_b                      !: snow heat content
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::   e_i_b                      !: ice temperatures
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)     ::   u_ice_b, v_ice_b           !: ice velocity
       
 
@@ -366,5 +369,5 @@
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   d_sm_i_fl  , d_sm_i_gd                 !:
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   d_sm_i_se  , d_sm_i_si  , d_sm_i_la    !:
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   d_oa_i_thd , d_oa_i_trp , s_i_newice   !:
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   d_oa_i_thd , d_oa_i_trp                !:
 
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::   d_e_s_thd  , d_e_s_trp     !:
@@ -375,14 +378,6 @@
    !! * Ice thickness distribution variables
    !!--------------------------------------------------------------------------
-   ! REMOVE
-   INTEGER , PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:)   ::   ice_types      !: Vector connecting types and categories
-   INTEGER , PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   ice_cat_bounds !: Matrix containing the integer upper and 
-   !                                                                       !  lower boundaries of ice thickness categories
-   ! REMOVE
-   INTEGER , PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:)   ::   ice_ncat_types !: nb of thickness categories in each ice type
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:)   ::   hi_max         !: Boundary of ice thickness categories in thickness space
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:)   ::   hi_mean        !: Mean ice thickness in catgories 
-   ! REMOVE
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   hi_max_typ     !: Boundary of ice thickness categories in thickness space
 
    !!--------------------------------------------------------------------------
@@ -404,15 +399,12 @@
    LOGICAL , PUBLIC                                      ::   ln_limdiahsb  !: flag for ice diag (T) or not (F)
    LOGICAL , PUBLIC                                      ::   ln_limdiaout  !: flag for ice diag (T) or not (F)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   v_newice   !: volume of ice formed in the leads
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   dv_dt_thd  !: thermodynamic growth rates 
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   izero, fstroc, fhbricat
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   diag_sni_gr   ! snow ice growth 
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   diag_lat_gr   ! lateral ice growth 
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   diag_bot_gr   ! bottom ice growth 
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   diag_dyn_gr   ! dynamical ice growth 
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   diag_bot_me   ! vertical bottom melt 
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   diag_sur_me   ! vertical surface melt
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   diag_res_pr   ! production (growth+melt) due to limupdate
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   diag_trp_vi   ! transport of ice volume
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   dv_dt_thd     !: thermodynamic growth rates 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   diag_trp_vi   !: transport of ice volume
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   diag_trp_vs   !: transport of snw volume
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   diag_trp_ei   !: transport of ice enthalpy (W/m2)
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   diag_trp_es   !: transport of snw enthalpy (W/m2)
+   !
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   diag_heat_dhc !: snw/ice heat content variation   [W/m2] 
+   !
    INTEGER , PUBLIC ::   jiindx, jjindx        !: indexes of the debugging point
 
@@ -430,5 +422,5 @@
       INTEGER :: ice_alloc
       !
-      INTEGER :: ierr(20), ii
+      INTEGER :: ierr(19), ii
       !!-----------------------------------------------------------------
 
@@ -447,19 +439,16 @@
 
       ii = ii + 1
-      ALLOCATE( firic    (jpi,jpj) , fcsic  (jpi,jpj) , fleic  (jpi,jpj) , qlatic   (jpi,jpj) ,     &
-         &      rdvosif  (jpi,jpj) , rdvobif(jpi,jpj) , fdvolif(jpi,jpj) , rdvonif  (jpi,jpj) ,     &
-         &      sist     (jpi,jpj) , icethi (jpi,jpj) , t_bo   (jpi,jpj) , hicifp   (jpi,jpj) ,     &
-         &      frld     (jpi,jpj) , pfrld  (jpi,jpj) , phicif (jpi,jpj) , fbif     (jpi,jpj) ,     &
-         &      rdm_snw  (jpi,jpj) , rdq_snw(jpi,jpj) , rdm_ice(jpi,jpj) , rdq_ice  (jpi,jpj) ,     &
-         &                                              qldif  (jpi,jpj) , qcmif    (jpi,jpj) ,     &
-         &      fdtcn    (jpi,jpj) , qdtcn  (jpi,jpj) , fstric (jpi,jpj) , fscmbq   (jpi,jpj) ,     &
-         &      ffltbif  (jpi,jpj) , fsbbq  (jpi,jpj) , qfvbq  (jpi,jpj) , dmgwi    (jpi,jpj) ,     &
-         &      sfx_res  (jpi,jpj) , sfx_bri(jpi,jpj) , sfx_mec(jpi,jpj) , fheat_mec(jpi,jpj) ,     &
-         &      fhbri    (jpi,jpj) , fmmec  (jpi,jpj) , sfx_thd(jpi,jpj) , fhmec    (jpi,jpj) ,     &
-         &      fheat_res(jpi,jpj)                                                            , STAT=ierr(ii) )
-
-      ii = ii + 1
-      ALLOCATE( dh_i_surf2D(jpi,jpj) , dh_i_bott2D(jpi,jpj) , fstbif(jpi,jpj) ,     &
-         &      fsup2D     (jpi,jpj) , focea2D    (jpi,jpj) , q_s   (jpi,jpj) , STAT=ierr(ii) )
+      ALLOCATE( sist   (jpi,jpj) , icethi (jpi,jpj) , t_bo   (jpi,jpj) ,      &
+         &      frld   (jpi,jpj) , pfrld  (jpi,jpj) , phicif (jpi,jpj) ,      &
+         &      wfx_snw(jpi,jpj) , wfx_ice(jpi,jpj) , wfx_sub(jpi,jpj) ,    &
+         &      wfx_bog(jpi,jpj) , wfx_dyn(jpi,jpj) , wfx_bom(jpi,jpj) , wfx_sum(jpi,jpj) ,     &
+         &      wfx_res(jpi,jpj) , wfx_sni(jpi,jpj) , wfx_opw(jpi,jpj) , wfx_spr(jpi,jpj) ,  qlead  (jpi,jpj) ,     &
+         &      fhtur  (jpi,jpj) , ftr_ice(jpi,jpj,jpl) ,      &
+         &      sfx_res(jpi,jpj) , sfx_bri(jpi,jpj) , sfx_dyn(jpi,jpj) ,      &
+         &      sfx_bog(jpi,jpj) , sfx_bom(jpi,jpj) , sfx_sum(jpi,jpj) , sfx_sni(jpi,jpj) , sfx_opw(jpi,jpj) ,   &
+         &      hfx_res(jpi,jpj) , hfx_snw(jpi,jpj) , hfx_sub(jpi,jpj) , hfx_err(jpi,jpj) , hfx_err_rem(jpi,jpj), &
+         &      hfx_in (jpi,jpj) , hfx_out(jpi,jpj) , fhld(jpi,jpj) ,  &
+         &      hfx_sum(jpi,jpj) , hfx_bom(jpi,jpj) , hfx_bog(jpi,jpj) , hfx_dif(jpi,jpj) , hfx_opw(jpi,jpj) , &
+         &      hfx_thd(jpi,jpj) , hfx_dyn(jpi,jpj) , hfx_spr(jpi,jpj) ,  STAT=ierr(ii) )
 
       ! * Ice global state variables
@@ -475,8 +464,8 @@
          &      bv_i (jpi,jpj) , smt_i(jpi,jpj)                                   , STAT=ierr(ii) )
       ii = ii + 1
-      ALLOCATE( t_s(jpi,jpj,nlay_s,jpl) , at_i_typ(jpi,jpj,jpm) ,                            &
-         &      e_s(jpi,jpj,nlay_s,jpl) , vt_i_typ(jpi,jpj,jpm) , e_i_cat(jpi,jpj,jpl) , STAT=ierr(ii) )
-      ii = ii + 1
-      ALLOCATE( t_i(jpi,jpj,jkmax,jpl) , e_i(jpi,jpj,jkmax,jpl) , s_i(jpi,jpj,jkmax,jpl) , STAT=ierr(ii) )
+      ALLOCATE( t_s(jpi,jpj,nlay_s,jpl) ,                            &
+         &      e_s(jpi,jpj,nlay_s,jpl) , STAT=ierr(ii) )
+      ii = ii + 1
+      ALLOCATE( t_i(jpi,jpj,nlay_i+1,jpl) , e_i(jpi,jpj,nlay_i+1,jpl) , s_i(jpi,jpj,nlay_i+1,jpl) , STAT=ierr(ii) )
 
       ! * Moments for advection
@@ -494,13 +483,13 @@
          &      STAT=ierr(ii) )
       ii = ii + 1
-      ALLOCATE( sxe (jpi,jpj,jkmax,jpl) , sye (jpi,jpj,jkmax,jpl) , sxxe(jpi,jpj,jkmax,jpl) ,     &
-         &      syye(jpi,jpj,jkmax,jpl) , sxye(jpi,jpj,jkmax,jpl)                           , STAT=ierr(ii) )
+      ALLOCATE( sxe (jpi,jpj,nlay_i+1,jpl) , sye (jpi,jpj,nlay_i+1,jpl) , sxxe(jpi,jpj,nlay_i+1,jpl) ,     &
+         &      syye(jpi,jpj,nlay_i+1,jpl) , sxye(jpi,jpj,nlay_i+1,jpl)                           , STAT=ierr(ii) )
 
       ! * Old values of global variables
       ii = ii + 1
-      ALLOCATE( old_v_s  (jpi,jpj,jpl) , old_v_i  (jpi,jpj,jpl) , old_e_s(jpi,jpj,nlay_s,jpl) ,     &
-         &      old_a_i  (jpi,jpj,jpl) , old_smv_i(jpi,jpj,jpl) , old_e_i(jpi,jpj,jkmax ,jpl) ,     &
-         &      old_oa_i (jpi,jpj,jpl)                                                        ,     &
-         &      old_u_ice(jpi,jpj)     , old_v_ice(jpi,jpj)                                   , STAT=ierr(ii) )
+      ALLOCATE( v_s_b  (jpi,jpj,jpl) , v_i_b  (jpi,jpj,jpl) , e_s_b(jpi,jpj,nlay_s,jpl) ,     &
+         &      a_i_b  (jpi,jpj,jpl) , smv_i_b(jpi,jpj,jpl) , e_i_b(jpi,jpj,nlay_i+1 ,jpl) ,     &
+         &      oa_i_b (jpi,jpj,jpl)                                                        ,     &
+         &      u_ice_b(jpi,jpj)     , v_ice_b(jpi,jpj)                                   , STAT=ierr(ii) )
 
       ! * Increment of global variables
@@ -509,21 +498,19 @@
          &      d_v_i_thd(jpi,jpj,jpl) , d_v_i_trp (jpi,jpj,jpl) , d_smv_i_thd(jpi,jpj,jpl) , d_smv_i_trp(jpi,jpj,jpl) ,   &     
          &      d_sm_i_fl(jpi,jpj,jpl) , d_sm_i_gd (jpi,jpj,jpl) , d_sm_i_se  (jpi,jpj,jpl) , d_sm_i_si  (jpi,jpj,jpl) ,   &
-         &      d_sm_i_la(jpi,jpj,jpl) , d_oa_i_thd(jpi,jpj,jpl) , d_oa_i_trp (jpi,jpj,jpl) , s_i_newice (jpi,jpj,jpl) ,   &
+         &      d_sm_i_la(jpi,jpj,jpl) , d_oa_i_thd(jpi,jpj,jpl) , d_oa_i_trp (jpi,jpj,jpl) ,   &
          &     STAT=ierr(ii) )
       ii = ii + 1
-      ALLOCATE( d_e_s_thd(jpi,jpj,nlay_s,jpl) , d_e_i_thd(jpi,jpj,jkmax,jpl) , d_u_ice_dyn(jpi,jpj) ,     &
-         &      d_e_s_trp(jpi,jpj,nlay_s,jpl) , d_e_i_trp(jpi,jpj,jkmax,jpl) , d_v_ice_dyn(jpi,jpj) , STAT=ierr(ii) )
+      ALLOCATE( d_e_s_thd(jpi,jpj,nlay_s,jpl) , d_e_i_thd(jpi,jpj,nlay_i,jpl) , d_u_ice_dyn(jpi,jpj) ,     &
+         &      d_e_s_trp(jpi,jpj,nlay_s,jpl) , d_e_i_trp(jpi,jpj,nlay_i,jpl) , d_v_ice_dyn(jpi,jpj) , STAT=ierr(ii) )
       
       ! * Ice thickness distribution variables
       ii = ii + 1
-      ALLOCATE( ice_types(jpl) , ice_cat_bounds(jpm,2) , ice_ncat_types  (jpm) ,     &
-         &      hi_max (0:jpl) , hi_mean(jpl)          , hi_max_typ(0:jpl,jpm) , STAT=ierr(ii) )
+      ALLOCATE( hi_max(0:jpl), hi_mean(jpl),  STAT=ierr(ii) )
 
       ! * Ice diagnostics
       ii = ii + 1
-      ALLOCATE( dv_dt_thd(jpi,jpj,jpl) , diag_sni_gr(jpi,jpj) , diag_lat_gr(jpi,jpj) ,     &
-         &      izero    (jpi,jpj,jpl) , diag_bot_gr(jpi,jpj) , diag_dyn_gr(jpi,jpj) ,     &
-         &      fstroc   (jpi,jpj,jpl) , diag_bot_me(jpi,jpj) , diag_sur_me(jpi,jpj) ,     &
-         &      fhbricat (jpi,jpj,jpl) , diag_res_pr(jpi,jpj) , diag_trp_vi(jpi,jpj) , v_newice(jpi,jpj) , STAT=ierr(ii) )
+      ALLOCATE( dv_dt_thd(jpi,jpj,jpl),    &
+         &      diag_trp_vi(jpi,jpj), diag_trp_vs  (jpi,jpj), diag_trp_ei(jpi,jpj),   & 
+         &      diag_trp_es(jpi,jpj), diag_heat_dhc(jpi,jpj),  STAT=ierr(ii) )
 
       ice_alloc = MAXVAL( ierr(:) )
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/iceini.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/iceini.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/iceini.F90	(revision 5038)
@@ -66,6 +66,6 @@
       !
       !                                ! adequation jpk versus ice/snow layers/categories
-      IF( jpl   > jpk  .OR.  jpm    > jpk .OR.                                    &
-          jkmax > jpk  .OR.  nlay_s > jpk      )   CALL ctl_stop( 'STOP',         &
+      IF( jpl > jpk .OR. (nlay_i+1) > jpk .OR. nlay_s > jpk )   &
+         &      CALL ctl_stop( 'STOP',                     &
          &     'ice_init: the 3rd dimension of workspace arrays is too small.',   &
          &     'use more ocean levels or less ice/snow layers/categories.' )
@@ -74,5 +74,5 @@
       CALL ctl_opn( numnam_ice_ref, 'namelist_ice_ref',    'OLD',     'FORMATTED', 'SEQUENTIAL', -1, numout, lwp ) 
       CALL ctl_opn( numnam_ice_cfg, 'namelist_ice_cfg',    'OLD',     'FORMATTED', 'SEQUENTIAL', -1, numout, lwp )
-      CALL ctl_opn( numoni,         'output.namelist.ice', 'UNKNOWN', 'FORMATTED', 'SEQUENTIAL', -1, numout, lwp, 1 )
+      IF(lwm) CALL ctl_opn( numoni, 'output.namelist.ice', 'UNKNOWN', 'FORMATTED', 'SEQUENTIAL', -1, numout, lwp, 1 )
       !
       CALL ice_run                     ! set some ice run parameters
@@ -89,4 +89,5 @@
       CALL lim_itd_ini                 ! ice thickness distribution initialization
       !
+      CALL lim_itd_me_init             ! ice thickness distribution initialization
       !                                ! Initial sea-ice state
       IF( .NOT. ln_rstart ) THEN              ! start from rest
@@ -141,5 +142,5 @@
       READ  ( numnam_ice_cfg, namicerun, IOSTAT = ios, ERR = 902 )
 902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namicerun in configuration namelist', lwp )
-      WRITE ( numoni, namicerun )
+      IF(lwm) WRITE ( numoni, namicerun )
       !
       !IF( lk_mpp .AND. ln_nicep ) THEN
@@ -173,5 +174,5 @@
       !!              limistate (only) and is changed to 99 m in ice_init
       !!------------------------------------------------------------------
-      INTEGER  ::   jl, jm               ! dummy loop index
+      INTEGER  ::   jl                   ! dummy loop index
       REAL(wp) ::   zc1, zc2, zc3, zx1   ! local scalars
       !!------------------------------------------------------------------
@@ -184,33 +185,6 @@
       ! 1) Ice thickness distribution parameters initialization    
       !------------------------------------------------------------------------------!
-
-      !- Types boundaries (integer)
-      !----------------------------
-      ice_cat_bounds(1,1) = 1
-      ice_cat_bounds(1,2) = jpl
-
-      !- Number of ice thickness categories in each ice type
-      DO jm = 1, jpm
-         ice_ncat_types(jm) = ice_cat_bounds(jm,2) - ice_cat_bounds(jm,1) + 1 
-      END DO
-
-      !- Make the correspondence between thickness categories and ice types
-      !---------------------------------------------------------------------
-      DO jm = 1, jpm       !over types
-         DO jl = ice_cat_bounds(jm,1), ice_cat_bounds(jm,2) !over thickness categories
-            ice_types(jl) = jm
-         END DO
-      END DO
-
       IF(lwp) THEN  
-         WRITE(numout,*) ' Number of ice types jpm =      ', jpm
          WRITE(numout,*) ' Number of ice categories jpl = ', jpl
-         DO jm = 1, jpm
-            WRITE(numout,*) ' Ice type ', jm
-            WRITE(numout,*) ' Number of thickness categories ', ice_ncat_types(jm)
-            WRITE(numout,*) ' Thickness category boundaries  ', ice_cat_bounds(jm,1:2)
-         END DO
-         WRITE(numout,*) 'Ice type vector', ice_types(1:jpl)
-         WRITE(numout,*)
       ENDIF
 
@@ -218,33 +192,17 @@
       !----------------------------------
       hi_max(:) = 0._wp
-      hi_max_typ(:,:) = 0._wp
-
-      !- Type 1 - undeformed ice
-      zc1 =  3._wp / REAL( ice_cat_bounds(1,2) - ice_cat_bounds(1,1) + 1 , wp )
+
+      zc1 =  3._wp / REAL( jpl, wp )
       zc2 = 10._wp * zc1
       zc3 =  3._wp
 
-      DO jl = ice_cat_bounds(1,1), ice_cat_bounds(1,2)
-         zx1 = REAL( jl-1 , wp ) / REAL( ice_cat_bounds(1,2) - ice_cat_bounds(1,1) + 1 , wp )
+      DO jl = 1, jpl
+         zx1 = REAL( jl-1, wp ) / REAL( jpl, wp )
          hi_max(jl) = hi_max(jl-1) + zc1 + zc2 * (1._wp + TANH( zc3 * (zx1 - 1._wp ) ) )
       END DO
 
-      !- Fill in the hi_max_typ vector, useful in other circumstances
-      ! Tricky trick: hi_max_typ is actually not used in the code and will be removed in a
-      ! next flyspray at this time, the tricky trick will also be removed (Martin, march 08)
-      DO jl = ice_cat_bounds(1,1), ice_cat_bounds(1,2)
-         hi_max_typ(jl,1) = hi_max(jl)
-      END DO
-
-      IF(lwp) WRITE(numout,*) ' Thickness category boundaries independently of ice type '
+      IF(lwp) WRITE(numout,*) ' Thickness category boundaries '
       IF(lwp) WRITE(numout,*) ' hi_max ', hi_max(0:jpl)
 
-      IF(lwp) WRITE(numout,*) ' Thickness category boundaries inside ice types '
-      IF(lwp) THEN 
-         DO jm = 1, jpm
-            WRITE(numout,*) ' Type number ', jm
-            WRITE(numout,*) ' hi_max_typ : ', hi_max_typ(0:ice_ncat_types(jm),jm)
-         END DO
-      ENDIF
       !
       DO jl = 1, jpl
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limadv.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limadv.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limadv.F90	(revision 5038)
@@ -30,8 +30,4 @@
    PUBLIC   lim_adv_x   ! called by lim_trp
    PUBLIC   lim_adv_y   ! called by lim_trp
-
-   REAL(wp)  ::   epsi20 = 1.e-20_wp   ! constant values
-   REAL(wp)  ::   rzero  = 0._wp       !    -       -
-   REAL(wp)  ::   rone   = 1._wp       !    -       -
 
    !! * Substitutions
@@ -84,10 +80,10 @@
       DO jj = 1, jpj
          DO ji = 1, jpi
-            zslpmax = MAX( rzero, ps0(ji,jj) )
+            zslpmax = MAX( 0._wp, ps0(ji,jj) )
             zs1max  = 1.5 * zslpmax
             zs1new  = MIN( zs1max, MAX( -zs1max, psx(ji,jj) ) )
             zs2new  = MIN(  2.0 * zslpmax - 0.3334 * ABS( zs1new ),      &
                &            MAX( ABS( zs1new ) - zslpmax, psxx(ji,jj) )  )
-            zin0    = ( 1.0 - MAX( rzero, SIGN( rone, -zslpmax) ) ) * tms(ji,jj)   ! Case of empty boxes & Apply mask
+            zin0    = ( 1.0 - MAX( 0._wp, SIGN( 1._wp, -zslpmax) ) ) * tms(ji,jj)   ! Case of empty boxes & Apply mask
 
             ps0 (ji,jj) = zslpmax  
@@ -106,6 +102,6 @@
       DO jj = 1, jpj                      !  Flux from i to i+1 WHEN u GT 0 
          DO ji = 1, jpi
-            zbet(ji,jj)  =  MAX( rzero, SIGN( rone, put(ji,jj) ) )
-            zalf         =  MAX( rzero, put(ji,jj) ) * zrdt * e2u(ji,jj) / psm(ji,jj)
+            zbet(ji,jj)  =  MAX( 0._wp, SIGN( 1._wp, put(ji,jj) ) )
+            zalf         =  MAX( 0._wp, put(ji,jj) ) * zrdt * e2u(ji,jj) / psm(ji,jj)
             zalfq        =  zalf * zalf
             zalf1        =  1.0 - zalf
@@ -133,5 +129,5 @@
       DO jj = 1, jpjm1                      !  Flux from i+1 to i when u LT 0.
          DO ji = 1, fs_jpim1
-            zalf          = MAX( rzero, -put(ji,jj) ) * zrdt * e2u(ji,jj) / psm(ji+1,jj) 
+            zalf          = MAX( 0._wp, -put(ji,jj) ) * zrdt * e2u(ji,jj) / psm(ji+1,jj) 
             zalg  (ji,jj) = zalf
             zalfq         = zalf * zalf
@@ -269,10 +265,10 @@
       DO jj = 1, jpj
          DO ji = 1, jpi
-            zslpmax = MAX( rzero, ps0(ji,jj) )
+            zslpmax = MAX( 0._wp, ps0(ji,jj) )
             zs1max  = 1.5 * zslpmax
             zs1new  = MIN( zs1max, MAX( -zs1max, psy(ji,jj) ) )
             zs2new  = MIN(  ( 2.0 * zslpmax - 0.3334 * ABS( zs1new ) ),   &
                &             MAX( ABS( zs1new )-zslpmax, psyy(ji,jj) )  )
-            zin0    = ( 1.0 - MAX( rzero, SIGN( rone, -zslpmax) ) ) * tms(ji,jj)   ! Case of empty boxes & Apply mask
+            zin0    = ( 1.0 - MAX( 0._wp, SIGN( 1._wp, -zslpmax) ) ) * tms(ji,jj)   ! Case of empty boxes & Apply mask
             !
             ps0 (ji,jj) = zslpmax  
@@ -291,6 +287,6 @@
       DO jj = 1, jpj                     !  Flux from j to j+1 WHEN v GT 0   
          DO ji = 1, jpi
-            zbet(ji,jj)  =  MAX( rzero, SIGN( rone, pvt(ji,jj) ) )
-            zalf         =  MAX( rzero, pvt(ji,jj) ) * zrdt * e1v(ji,jj) / psm(ji,jj)
+            zbet(ji,jj)  =  MAX( 0._wp, SIGN( 1._wp, pvt(ji,jj) ) )
+            zalf         =  MAX( 0._wp, pvt(ji,jj) ) * zrdt * e1v(ji,jj) / psm(ji,jj)
             zalfq        =  zalf * zalf
             zalf1        =  1.0 - zalf
@@ -318,5 +314,5 @@
       DO jj = 1, jpjm1                   !  Flux from j+1 to j when v LT 0.
          DO ji = 1, jpi
-            zalf          = ( MAX(rzero, -pvt(ji,jj) ) * zrdt * e1v(ji,jj) ) / psm(ji,jj+1) 
+            zalf          = ( MAX(0._wp, -pvt(ji,jj) ) * zrdt * e1v(ji,jj) ) / psm(ji,jj+1) 
             zalg  (ji,jj) = zalf
             zalfq         = zalf * zalf
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limcons.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limcons.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limcons.F90	(revision 5038)
@@ -7,4 +7,5 @@
    !!            3.0  ! 2004-06  (M. Vancoppenolle)   Energy Conservation 
    !!            4.0  ! 2011-02  (G. Madec)  add mpp considerations
+   !!             -   ! 2014-05  (C. Rousset) add lim_cons_hsm
    !!----------------------------------------------------------------------
 #if defined key_lim3
@@ -14,4 +15,5 @@
    !!    lim_cons     :   checks whether energy, mass and salt are conserved 
    !!----------------------------------------------------------------------
+   USE phycst         ! physical constants
    USE par_ice        ! LIM-3 parameter
    USE ice            ! LIM-3 variables
@@ -28,4 +30,5 @@
    PUBLIC   lim_column_sum_energy
    PUBLIC   lim_cons_check
+   PUBLIC   lim_cons_hsm
 
    !!----------------------------------------------------------------------
@@ -70,8 +73,8 @@
       !! ** Method  : Arithmetics
       !!---------------------------------------------------------------------
-      INTEGER                               , INTENT(in   ) ::   ksum   !: number of categories
-      INTEGER                               , INTENT(in   ) ::   klay   !: number of vertical layers
-      REAL(wp), DIMENSION(jpi,jpj,jkmax,jpl), INTENT(in   ) ::   pin   !: input field
-      REAL(wp), DIMENSION(jpi,jpj)          , INTENT(  out) ::   pout   !: output field
+      INTEGER                                  , INTENT(in   ) ::   ksum   !: number of categories
+      INTEGER                                  , INTENT(in   ) ::   klay   !: number of vertical layers
+      REAL(wp), DIMENSION(jpi,jpj,nlay_i+1,jpl), INTENT(in   ) ::   pin   !: input field
+      REAL(wp), DIMENSION(jpi,jpj)             , INTENT(  out) ::   pout   !: output field
       !
       INTEGER ::   jk, jl   ! dummy loop indices
@@ -151,4 +154,69 @@
    END SUBROUTINE lim_cons_check
 
+
+   SUBROUTINE lim_cons_hsm( icount, cd_routine, zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b )
+      !!-------------------------------------------------------------------
+      !!               ***  ROUTINE lim_cons_hsm ***
+      !!
+      !! ** Purpose : Test the conservation of heat, salt and mass for each routine
+      !!
+      !! ** Method  :
+      !!---------------------------------------------------------------------
+      INTEGER         , INTENT(in)    :: icount      ! determine wether this is the beggining of the routine (0) or the end (1)
+      CHARACTER(len=*), INTENT(in)    :: cd_routine  ! name of the routine
+      REAL(wp)        , INTENT(inout) :: zvi_b, zsmv_b, zei_b, zfs_b, zfw_b, zft_b 
+      REAL(wp)                        :: zvi,   zsmv,   zei,   zfs,   zfw,   zft
+      REAL(wp)                        :: zvmin, zamin, zamax 
+
+      IF( icount == 0 ) THEN
+
+         zvi_b  = glob_sum( SUM(   v_i(:,:,:)*rhoic + v_s(:,:,:)*rhosn, dim=3 ) * area(:,:) * tms(:,:) )
+         zsmv_b = glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) )
+         zei_b  = glob_sum( SUM(   e_i(:,:,1:nlay_i,:), dim=3 ) + SUM( e_s(:,:,1:nlay_s,:), dim=3 ) )
+         zfw_b  = glob_sum( - ( wfx_bog(:,:) + wfx_bom(:,:) + wfx_sum(:,:) + wfx_sni(:,:) + wfx_opw(:,:) +  &
+            &                   wfx_res(:,:) + wfx_dyn(:,:) + wfx_snw(:,:) + wfx_sub(:,:) + wfx_spr(:,:)    &
+            &             ) * area(:,:) * tms(:,:) )
+         zfs_b  = glob_sum(   ( sfx_bri(:,:) + sfx_bog(:,:) + sfx_bom(:,:) + sfx_sum(:,:) + sfx_sni(:,:) +  &
+            &                   sfx_opw(:,:) + sfx_res(:,:) + sfx_dyn(:,:)                                  &
+            &                 ) * area(:,:) * tms(:,:) )
+         zft_b  = glob_sum(   ( hfx_sum(:,:) + hfx_bom(:,:) + hfx_bog(:,:) + hfx_dif(:,:) + hfx_opw(:,:) + hfx_snw(:,:)  & 
+            &                 - hfx_thd(:,:) - hfx_dyn(:,:) - hfx_res(:,:) - hfx_sub(:,:) - hfx_spr(:,:)   &
+            &                  ) * area(:,:) / unit_fac * tms(:,:) )
+
+      ELSEIF( icount == 1 ) THEN
+
+         zfs  = glob_sum(   ( sfx_bri(:,:) + sfx_bog(:,:) + sfx_bom(:,:) + sfx_sum(:,:) + sfx_sni(:,:) +  &
+            &                 sfx_opw(:,:) + sfx_res(:,:) + sfx_dyn(:,:)                                  & 
+            &                ) * area(:,:) * tms(:,:) ) - zfs_b
+         zfw  = glob_sum( - ( wfx_bog(:,:) + wfx_bom(:,:) + wfx_sum(:,:) + wfx_sni(:,:) + wfx_opw(:,:) +  &
+            &                 wfx_res(:,:) + wfx_dyn(:,:) + wfx_snw(:,:) + wfx_sub(:,:) + wfx_spr(:,:)    &
+            &                ) * area(:,:) * tms(:,:) ) - zfw_b
+         zft  = glob_sum(   ( hfx_sum(:,:) + hfx_bom(:,:) + hfx_bog(:,:) + hfx_dif(:,:) + hfx_opw(:,:) + hfx_snw(:,:)  & 
+            &               - hfx_thd(:,:) - hfx_dyn(:,:) - hfx_res(:,:) - hfx_sub(:,:) - hfx_spr(:,:)   &
+            &                ) * area(:,:) / unit_fac * tms(:,:) ) - zft_b
+ 
+         zvi  = ( glob_sum( SUM(   v_i(:,:,:)*rhoic + v_s(:,:,:)*rhosn, dim=3 ) * area(:,:) * tms(:,:) ) - zvi_b ) * r1_rdtice - zfw 
+         zsmv = ( glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) ) - zsmv_b ) * r1_rdtice + ( zfs / rhoic )
+         zei  =   glob_sum( SUM( e_i(:,:,1:nlay_i,:), dim=3 ) + SUM( e_s(:,:,1:nlay_s,:), dim=3 ) ) * r1_rdtice - zei_b * r1_rdtice + zft
+
+         zvmin = glob_min(v_i)
+         zamax = glob_max(SUM(a_i,dim=3))
+         zamin = glob_min(a_i)
+       
+         IF(lwp) THEN
+            IF ( ABS( zvi    ) >  1.e-4 ) WRITE(numout,*) 'violation volume [kg/day]     (',cd_routine,') = ',(zvi * rday)
+            IF ( ABS( zsmv   ) >  1.e-4 ) WRITE(numout,*) 'violation saline [psu*m3/day] (',cd_routine,') = ',(zsmv * rday)
+            IF ( ABS( zei    ) >  1.    ) WRITE(numout,*) 'violation enthalpy [1e9 J]    (',cd_routine,') = ',(zei)
+            IF ( zvmin <  0.            ) WRITE(numout,*) 'violation v_i<0  [m]          (',cd_routine,') = ',(zvmin)
+            IF( cd_routine /= 'limtrp' .AND. cd_routine /= 'limitd_me' .AND. zamax > amax+1.e-10 ) THEN
+                                          WRITE(numout,*) 'violation a_i>amax            (',cd_routine,') = ',zamax
+            ENDIF
+            IF ( zamin <  0.            ) WRITE(numout,*) 'violation a_i<0               (',cd_routine,') = ',zamin
+         ENDIF
+
+      ENDIF
+
+   END SUBROUTINE lim_cons_hsm
+
 #else
    !!----------------------------------------------------------------------
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limdiahsb.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limdiahsb.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limdiahsb.F90	(revision 5038)
@@ -18,4 +18,5 @@
    USE dom_oce         ! ocean domain
    USE sbc_oce         ! surface boundary condition: ocean fields
+   USE sbc_ice         ! Surface boundary condition: sea-ice fields
    USE daymod          ! model calendar
    USE phycst          ! physical constant
@@ -34,9 +35,6 @@
    !!PUBLIC   lim_diahsb_rst   ! routine called by ice_init.F90
 
-   REAL(dp) ::   frc_sal, frc_vol   ! global forcing trends
-   REAL(dp) ::   bg_grme            ! global ice growth+melt trends
-   REAL(wp) ::   epsi06 = 1.e-6_wp  ! small number
-   REAL(wp) ::   epsi03 = 1.e-3_wp  ! small number
-
+   real(wp) ::   frc_sal, frc_vol   ! global forcing trends
+   real(wp) ::   bg_grme            ! global ice growth+melt trends
 
    !! * Substitutions
@@ -59,10 +57,15 @@
       !!---------------------------------------------------------------------------
       !!
-      REAL(dp)   ::   zbg_ivo, zbg_svo, zbg_are, zbg_sal ,zbg_tem ,zbg_ihc ,zbg_shc
-      REAL(dp)   ::   zbg_sfx, zbg_sfx_bri, zbg_sfx_thd, zbg_sfx_res, zbg_sfx_mec 
-      REAL(dp)   ::   zbg_emp, zbg_emp_bog, zbg_emp_lag, zbg_emp_sig, zbg_emp_dyg, zbg_emp_bom, zbg_emp_sum, zbg_emp_res 
-      REAL(dp)   ::   z_frc_vol, z_frc_sal, z_bg_grme 
-      REAL(dp)   ::   z1_area                     !    -     -
-      REAL(dp)   ::   zinda, zindb
+      real(wp)   ::   zbg_ivo, zbg_svo, zbg_are, zbg_sal ,zbg_tem ,zbg_ihc ,zbg_shc
+      real(wp)   ::   zbg_sfx, zbg_sfx_bri, zbg_sfx_bog, zbg_sfx_bom, zbg_sfx_sum, zbg_sfx_sni,   &
+      &               zbg_sfx_opw, zbg_sfx_res, zbg_sfx_dyn 
+      real(wp)   ::   zbg_vfx, zbg_vfx_bog, zbg_vfx_opw, zbg_vfx_sni, zbg_vfx_dyn
+      real(wp)   ::   zbg_vfx_bom, zbg_vfx_sum, zbg_vfx_res, zbg_vfx_spr, zbg_vfx_snw, zbg_vfx_sub  
+      real(wp)   ::   zbg_hfx_dhc, zbg_hfx_spr
+      real(wp)   ::   zbg_hfx_res, zbg_hfx_sub, zbg_hfx_dyn, zbg_hfx_thd, zbg_hfx_snw, zbg_hfx_out, zbg_hfx_in   
+      real(wp)   ::   zbg_hfx_sum, zbg_hfx_bom, zbg_hfx_bog, zbg_hfx_dif, zbg_hfx_opw
+      real(wp)   ::   z_frc_vol, z_frc_sal, z_bg_grme 
+      real(wp)   ::   z1_area                     !    -     -
+      REAL(wp)   ::   ztmp
       !!---------------------------------------------------------------------------
       IF( nn_timing == 1 )   CALL timing_start('lim_diahsb')
@@ -71,7 +74,7 @@
 
       ! 1/area
-      z1_area = 1.d0 / MAX( glob_sum( area(:,:) * tms(:,:) ), epsi06 )
-
-      zinda = MAX( 0.d0 , SIGN( 1.d0 , glob_sum( area(:,:) * tms(:,:) ) - epsi06 ) )
+      z1_area = 1._wp / MAX( glob_sum( area(:,:) * tms(:,:) ), epsi06 )
+
+      rswitch = MAX( 0._wp , SIGN( 1._wp , glob_sum( area(:,:) * tms(:,:) ) - epsi06 ) )
       ! ----------------------- !
       ! 1 -  Content variations !
@@ -86,22 +89,49 @@
       !zbg_shc = glob_sum( et_s(:,:) * area(:,:) * tms(:,:) ) / MAX( zbg_svo,epsi06 ) ! snow heat content
 
-      zbg_ihc = glob_sum( et_i(:,:) * 1.e-11 ) ! ice heat content  [10^9*1.e-11 J]
-      zbg_shc = glob_sum( et_s(:,:) * 1.e-11 ) ! snow heat content [10^9*1.e-11 J]
-
-      zbg_emp     = zinda * glob_sum(         emp(:,:) * area(:,:) * tms(:,:) ) * z1_area         * r1_rau0 * rday
-      zbg_emp_bog = zinda * glob_sum( diag_bot_gr(:,:) * area(:,:) * tms(:,:) ) * z1_area * rhoic * r1_rau0 * rday
-      zbg_emp_lag = zinda * glob_sum( diag_lat_gr(:,:) * area(:,:) * tms(:,:) ) * z1_area * rhoic * r1_rau0 * rday
-      zbg_emp_sig = zinda * glob_sum( diag_sni_gr(:,:) * area(:,:) * tms(:,:) ) * z1_area * rhoic * r1_rau0 * rday
-      zbg_emp_dyg = zinda * glob_sum( diag_dyn_gr(:,:) * area(:,:) * tms(:,:) ) * z1_area * rhoic * r1_rau0 * rday
-      zbg_emp_bom = zinda * glob_sum( diag_bot_me(:,:) * area(:,:) * tms(:,:) ) * z1_area * rhoic * r1_rau0 * rday
-      zbg_emp_sum = zinda * glob_sum( diag_sur_me(:,:) * area(:,:) * tms(:,:) ) * z1_area * rhoic * r1_rau0 * rday
-      zbg_emp_res = zinda * glob_sum( diag_res_pr(:,:) * area(:,:) * tms(:,:) ) * z1_area * rhoic * r1_rau0 * rday
-
-      zbg_sfx     = zinda * glob_sum(     sfx(:,:) * area(:,:) * tms(:,:) ) * z1_area * r1_rau0 * rday
-      zbg_sfx_bri = zinda * glob_sum( sfx_bri(:,:) * area(:,:) * tms(:,:) ) * z1_area * r1_rau0 * rday
-      zbg_sfx_thd = zinda * glob_sum( sfx_thd(:,:) * area(:,:) * tms(:,:) ) * z1_area * r1_rau0 * rday
-      zbg_sfx_res = zinda * glob_sum( sfx_res(:,:) * area(:,:) * tms(:,:) ) * z1_area * r1_rau0 * rday
-      zbg_sfx_mec = zinda * glob_sum( sfx_mec(:,:) * area(:,:) * tms(:,:) ) * z1_area * r1_rau0 * rday
-      
+      ! Volume
+      ztmp = rswitch * z1_area * r1_rau0 * rday
+      zbg_vfx     = ztmp * glob_sum(     emp(:,:) * area(:,:) * tms(:,:) )
+      zbg_vfx_bog = ztmp * glob_sum( wfx_bog(:,:) * area(:,:) * tms(:,:) )
+      zbg_vfx_opw = ztmp * glob_sum( wfx_opw(:,:) * area(:,:) * tms(:,:) )
+      zbg_vfx_sni = ztmp * glob_sum( wfx_sni(:,:) * area(:,:) * tms(:,:) )
+      zbg_vfx_dyn = ztmp * glob_sum( wfx_dyn(:,:) * area(:,:) * tms(:,:) )
+      zbg_vfx_bom = ztmp * glob_sum( wfx_bom(:,:) * area(:,:) * tms(:,:) )
+      zbg_vfx_sum = ztmp * glob_sum( wfx_sum(:,:) * area(:,:) * tms(:,:) )
+      zbg_vfx_res = ztmp * glob_sum( wfx_res(:,:) * area(:,:) * tms(:,:) )
+      zbg_vfx_spr = ztmp * glob_sum( wfx_spr(:,:) * area(:,:) * tms(:,:) )
+      zbg_vfx_snw = ztmp * glob_sum( wfx_snw(:,:) * area(:,:) * tms(:,:) )
+      zbg_vfx_sub = ztmp * glob_sum( wfx_sub(:,:) * area(:,:) * tms(:,:) )
+
+      ! Salt
+      zbg_sfx     = ztmp * glob_sum(     sfx(:,:) * area(:,:) * tms(:,:) )
+      zbg_sfx_bri = ztmp * glob_sum( sfx_bri(:,:) * area(:,:) * tms(:,:) )
+      zbg_sfx_res = ztmp * glob_sum( sfx_res(:,:) * area(:,:) * tms(:,:) )
+      zbg_sfx_dyn = ztmp * glob_sum( sfx_dyn(:,:) * area(:,:) * tms(:,:) )
+
+      zbg_sfx_bog = ztmp * glob_sum( sfx_bog(:,:) * area(:,:) * tms(:,:) )
+      zbg_sfx_opw = ztmp * glob_sum( sfx_opw(:,:) * area(:,:) * tms(:,:) )
+      zbg_sfx_sni = ztmp * glob_sum( sfx_sni(:,:) * area(:,:) * tms(:,:) )
+      zbg_sfx_bom = ztmp * glob_sum( sfx_bom(:,:) * area(:,:) * tms(:,:) )
+      zbg_sfx_sum = ztmp * glob_sum( sfx_sum(:,:) * area(:,:) * tms(:,:) )
+
+      ! Heat budget
+      zbg_ihc      = glob_sum( et_i(:,:) * 1.e-20 ) ! ice heat content  [1.e-20 J]
+      zbg_shc      = glob_sum( et_s(:,:) * 1.e-20 ) ! snow heat content [1.e-20 J]
+      zbg_hfx_dhc  = glob_sum( diag_heat_dhc(:,:) * area(:,:) * tms(:,:) ) ! [in W]
+      zbg_hfx_spr  = glob_sum( hfx_spr(:,:) * area(:,:) * tms(:,:) ) ! [in W]
+
+      zbg_hfx_thd  = glob_sum( hfx_thd(:,:) * area(:,:) * tms(:,:) ) ! [in W]
+      zbg_hfx_dyn  = glob_sum( hfx_dyn(:,:) * area(:,:) * tms(:,:) ) ! [in W]
+      zbg_hfx_res  = glob_sum( hfx_res(:,:) * area(:,:) * tms(:,:) ) ! [in W]
+      zbg_hfx_sub  = glob_sum( hfx_sub(:,:) * area(:,:) * tms(:,:) ) ! [in W]
+      zbg_hfx_snw  = glob_sum( hfx_snw(:,:) * area(:,:) * tms(:,:) ) ! [in W]
+      zbg_hfx_sum  = glob_sum( hfx_sum(:,:) * area(:,:) * tms(:,:) ) ! [in W]
+      zbg_hfx_bom  = glob_sum( hfx_bom(:,:) * area(:,:) * tms(:,:) ) ! [in W]
+      zbg_hfx_bog  = glob_sum( hfx_bog(:,:) * area(:,:) * tms(:,:) ) ! [in W]
+      zbg_hfx_dif  = glob_sum( hfx_dif(:,:) * area(:,:) * tms(:,:) ) ! [in W]
+      zbg_hfx_opw  = glob_sum( hfx_opw(:,:) * area(:,:) * tms(:,:) ) ! [in W]
+      zbg_hfx_out  = glob_sum( hfx_out(:,:) * area(:,:) * tms(:,:) ) ! [in W]
+      zbg_hfx_in   = glob_sum(  hfx_in(:,:) * area(:,:) * tms(:,:) ) ! [in W]
+    
       ! --------------------------------------------- !
       ! 2 - Trends due to forcing and ice growth/melt !
@@ -109,6 +139,6 @@
       z_frc_vol = r1_rau0 * glob_sum( - emp(:,:) * area(:,:) * tms(:,:) ) ! volume fluxes
       z_frc_sal = r1_rau0 * glob_sum(   sfx(:,:) * area(:,:) * tms(:,:) ) ! salt fluxes
-      z_bg_grme = glob_sum( ( diag_bot_gr(:,:) + diag_lat_gr(:,:) + diag_sni_gr(:,:) + diag_dyn_gr(:,:) + &
-                          &    diag_bot_me(:,:) + diag_sur_me(:,:) + diag_res_pr(:,:) ) * area(:,:) * tms(:,:) ) ! volume fluxes
+      z_bg_grme = glob_sum( - ( wfx_bog(:,:) + wfx_opw(:,:) + wfx_sni(:,:) + wfx_dyn(:,:) + &
+                          &     wfx_bom(:,:) + wfx_sum(:,:) + wfx_res(:,:) + wfx_snw(:,:) + wfx_sub(:,:) ) * area(:,:) * tms(:,:) ) ! volume fluxes
       !
       frc_vol  = frc_vol  + z_frc_vol  * rdt_ice
@@ -123,33 +153,64 @@
       ! 3 - Diagnostics writing !
       ! ----------------------- !
-      zindb = MAX( 0.d0 , SIGN( 1.d0 , zbg_ivo - epsi06 ) )
-      !
+      rswitch = MAX( 0._wp , SIGN( 1._wp , zbg_ivo - epsi06 ) )
+      !
+      IF( iom_use('ibgvoltot') )   &
       CALL iom_put( 'ibgvoltot' , zbg_ivo * rhoic * r1_rau0 * 1.e-9        )   ! ice volume (km3 equivalent liquid)         
+      IF( iom_use('sbgvoltot') )   &
       CALL iom_put( 'sbgvoltot' , zbg_svo * rhosn * r1_rau0 * 1.e-9        )   ! snw volume (km3 equivalent liquid)       
+      IF( iom_use('ibgarea') )   &
       CALL iom_put( 'ibgarea'   , zbg_are * 1.e-6                          )   ! ice area   (km2)
-      CALL iom_put( 'ibgsaline' , zindb * zbg_sal / MAX( zbg_ivo, epsi06 ) )   ! ice saline (psu)
-      CALL iom_put( 'ibgtemper' , zindb * zbg_tem / MAX( zbg_ivo, epsi06 ) )   ! ice temper (C)
+      IF( iom_use('ibgsaline') )   &
+      CALL iom_put( 'ibgsaline' , rswitch * zbg_sal / MAX( zbg_ivo, epsi06 ) )   ! ice saline (psu)
+      IF( iom_use('ibgtemper') )   &
+      CALL iom_put( 'ibgtemper' , rswitch * zbg_tem / MAX( zbg_ivo, epsi06 ) )   ! ice temper (C)
       CALL iom_put( 'ibgheatco' , zbg_ihc                                  )   ! ice heat content (1.e20 J)        
       CALL iom_put( 'sbgheatco' , zbg_shc                                  )   ! snw heat content (1.e20 J)
+      IF( iom_use('ibgsaltco') )   &
       CALL iom_put( 'ibgsaltco' , zbg_sal * rhoic * r1_rau0 * 1.e-9        )   ! ice salt content (psu*km3 equivalent liquid)        
 
-      CALL iom_put( 'ibgemp'    , zbg_emp                                  )   ! volume flux emp (m/day liquid)
-      CALL iom_put( 'ibgempbog' , zbg_emp_bog                              )   ! volume flux bottom growth     -(m/day equivalent liquid)
-      CALL iom_put( 'ibgemplag' , zbg_emp_lag                              )   ! volume flux open water growth -
-      CALL iom_put( 'ibgempsig' , zbg_emp_sig                              )   ! volume flux snow ice growth   -
-      CALL iom_put( 'ibgempdyg' , zbg_emp_dyg                              )   ! volume flux dynamic growth    -
-      CALL iom_put( 'ibgempbom' , zbg_emp_bom                              )   ! volume flux bottom melt       -
-      CALL iom_put( 'ibgempsum' , zbg_emp_sum                              )   ! volume flux surface melt      -
-      CALL iom_put( 'ibgempres' , zbg_emp_res                              )   ! volume flux resultant         -
+      CALL iom_put( 'ibgvfx'    , zbg_vfx                                  )   ! volume flux emp (m/day liquid)
+      CALL iom_put( 'ibgvfxbog' , zbg_vfx_bog                              )   ! volume flux bottom growth     -(m/day equivalent liquid)
+      CALL iom_put( 'ibgvfxopw' , zbg_vfx_opw                              )   ! volume flux open water growth -
+      CALL iom_put( 'ibgvfxsni' , zbg_vfx_sni                              )   ! volume flux snow ice growth   -
+      CALL iom_put( 'ibgvfxdyn' , zbg_vfx_dyn                              )   ! volume flux dynamic growth    -
+      CALL iom_put( 'ibgvfxbom' , zbg_vfx_bom                              )   ! volume flux bottom melt       -
+      CALL iom_put( 'ibgvfxsum' , zbg_vfx_sum                              )   ! volume flux surface melt      -
+      CALL iom_put( 'ibgvfxres' , zbg_vfx_res                              )   ! volume flux resultant         -
+      CALL iom_put( 'ibgvfxspr' , zbg_vfx_spr                              )   ! volume flux from snow precip         -
+      CALL iom_put( 'ibgvfxsnw' , zbg_vfx_snw                              )   ! volume flux from snow melt         -
+      CALL iom_put( 'ibgvfxsub' , zbg_vfx_sub                              )   ! volume flux from sublimation         -
           
       CALL iom_put( 'ibgsfx'    , zbg_sfx                                  )   ! salt flux         -(psu*m/day equivalent liquid)       
       CALL iom_put( 'ibgsfxbri' , zbg_sfx_bri                              )   ! salt flux brines  -      
-      CALL iom_put( 'ibgsfxthd' , zbg_sfx_thd                              )   ! salt flux thermo  -    
-      CALL iom_put( 'ibgsfxmec' , zbg_sfx_mec                              )   ! salt flux dynamic -    
+      CALL iom_put( 'ibgsfxdyn' , zbg_sfx_dyn                              )   ! salt flux dynamic -    
       CALL iom_put( 'ibgsfxres' , zbg_sfx_res                              )   ! salt flux result  -    
+      CALL iom_put( 'ibgsfxbog' , zbg_sfx_bog                              )   ! salt flux bottom growth   
+      CALL iom_put( 'ibgsfxopw' , zbg_sfx_opw                              )   ! salt flux open water growth -
+      CALL iom_put( 'ibgsfxsni' , zbg_sfx_sni                              )   ! salt flux snow ice growth   -
+      CALL iom_put( 'ibgsfxbom' , zbg_sfx_bom                              )   ! salt flux bottom melt       -
+      CALL iom_put( 'ibgsfxsum' , zbg_sfx_sum                              )   ! salt flux surface melt      -
+
+      CALL iom_put( 'ibghfxdhc' , zbg_hfx_dhc                              )   ! Heat content variation in snow and ice [W]
+      CALL iom_put( 'ibghfxspr' , zbg_hfx_spr                              )   ! Heat content of snow precip [W]
+
+      CALL iom_put( 'ibghfxres' , zbg_hfx_res                              )   ! 
+      CALL iom_put( 'ibghfxsub' , zbg_hfx_sub                              )   ! 
+      CALL iom_put( 'ibghfxdyn' , zbg_hfx_dyn                              )   ! 
+      CALL iom_put( 'ibghfxthd' , zbg_hfx_thd                              )   ! 
+      CALL iom_put( 'ibghfxsnw' , zbg_hfx_snw                              )   ! 
+      CALL iom_put( 'ibghfxsum' , zbg_hfx_sum                              )   ! 
+      CALL iom_put( 'ibghfxbom' , zbg_hfx_bom                              )   ! 
+      CALL iom_put( 'ibghfxbog' , zbg_hfx_bog                              )   ! 
+      CALL iom_put( 'ibghfxdif' , zbg_hfx_dif                              )   ! 
+      CALL iom_put( 'ibghfxopw' , zbg_hfx_opw                              )   ! 
+      CALL iom_put( 'ibghfxout' , zbg_hfx_out                              )   ! 
+      CALL iom_put( 'ibghfxin'  , zbg_hfx_in                               )   ! 
 
       CALL iom_put( 'ibgfrcvol' , frc_vol * 1.e-9                          )   ! vol - forcing     (km3 equivalent liquid) 
       CALL iom_put( 'ibgfrcsfx' , frc_sal * 1.e-9                          )   ! sal - forcing     (psu*km3 equivalent liquid)   
-      CALL iom_put( 'ibggrme'   , bg_grme * rhoic * r1_rau0 * 1.e-9        )   ! vol growth + melt (km3 equivalent liquid)         
+      IF( iom_use('ibgvolgrm') )   &
+      CALL iom_put( 'ibgvolgrm' , bg_grme * r1_rau0 * 1.e-9                )   ! vol growth + melt (km3 equivalent liquid)         
+
       !
       IF( lrst_ice )   CALL lim_diahsb_rst( numit, 'WRITE' )
@@ -190,7 +251,7 @@
       ! 2 - initial conservation variables !
       ! ---------------------------------- !
-      !frc_vol = 0.d0                                           ! volume       trend due to forcing
-      !frc_sal = 0.d0                                           ! salt content   -    -   -    -         
-      !bg_grme = 0.d0                                           ! ice growth + melt volume trend
+      !frc_vol = 0._wp                                          ! volume       trend due to forcing
+      !frc_sal = 0._wp                                          ! salt content   -    -   -    -         
+      !bg_grme = 0._wp                                          ! ice growth + melt volume trend
       !
       CALL lim_diahsb_rst( nstart, 'READ' )  !* read or initialize all required files
@@ -226,8 +287,8 @@
            IF(lwp) WRITE(numout,*) ' lim_diahsb at initial state '
            IF(lwp) WRITE(numout,*) '~~~~~~~'
-           frc_vol  = 0.d0                                           
-           frc_sal  = 0.d0                                                  
-           bg_grme  = 0.d0                                        
-       ENDIF	
+           frc_vol  = 0._wp                                          
+           frc_sal  = 0._wp                                                 
+           bg_grme  = 0._wp                                       
+       ENDIF
 
      ELSEIF( TRIM(cdrw) == 'WRITE' ) THEN   ! Create restart file
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limdyn.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limdyn.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limdyn.F90	(revision 5038)
@@ -30,4 +30,5 @@
    USE lib_fortran      ! glob_sum
    USE timing          ! Timing
+   USE limcons        ! conservation tests
 
    IMPLICIT NONE
@@ -63,9 +64,9 @@
       INTEGER  ::   i_j1, i_jpj       ! Starting/ending j-indices for rheology
       REAL(wp) ::   zcoef             ! local scalar
-      REAL(wp), POINTER, DIMENSION(:)   ::   zind           ! i-averaged indicator of sea-ice
+      REAL(wp), POINTER, DIMENSION(:)   ::   zswitch        ! i-averaged indicator of sea-ice
       REAL(wp), POINTER, DIMENSION(:)   ::   zmsk           ! i-averaged of tmask
       REAL(wp), POINTER, DIMENSION(:,:) ::   zu_io, zv_io   ! ice-ocean velocity
-      REAL(wp) :: zchk_v_i, zchk_smv, zchk_fs, zchk_fw, zchk_v_i_b, zchk_smv_b, zchk_fs_b, zchk_fw_b ! Check conservation (C Rousset)
-      REAL(wp) :: zchk_vmin, zchk_amin, zchk_amax ! Check errors (C Rousset)
+      !
+      REAL(wp) :: zvi_b, zsmv_b, zei_b, zfs_b, zfw_b, zft_b 
      !!---------------------------------------------------------------------
 
@@ -73,16 +74,5 @@
 
       CALL wrk_alloc( jpi, jpj, zu_io, zv_io )
-      CALL wrk_alloc( jpj, zind, zmsk )
-
-      ! -------------------------------
-      !- check conservation (C Rousset)
-      IF (ln_limdiahsb) THEN
-         zchk_v_i_b = glob_sum( SUM(   v_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) )
-         zchk_smv_b = glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) )
-         zchk_fw_b  = glob_sum( rdm_ice(:,:) * area(:,:) * tms(:,:) )
-         zchk_fs_b  = glob_sum( ( sfx_bri(:,:) + sfx_thd(:,:) + sfx_res(:,:) + sfx_mec(:,:) ) * area(:,:) * tms(:,:) )
-      ENDIF
-      !- check conservation (C Rousset)
-      ! -------------------------------
+      CALL wrk_alloc( jpj, zswitch, zmsk )
 
       IF( kt == nit000 )   CALL lim_dyn_init   ! Initialization (first time-step only)
@@ -90,6 +80,9 @@
       IF( ln_limdyn ) THEN
          !
-         old_u_ice(:,:) = u_ice(:,:) * tmu(:,:)
-         old_v_ice(:,:) = v_ice(:,:) * tmv(:,:)
+         ! conservation test
+         IF( ln_limdiahsb ) CALL lim_cons_hsm(0, 'limdyn', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
+
+         u_ice_b(:,:) = u_ice(:,:) * tmu(:,:)
+         v_ice_b(:,:) = v_ice(:,:) * tmv(:,:)
 
          ! Rheology (ice dynamics)
@@ -107,5 +100,5 @@
             !
             DO jj = 1, jpj
-               zind(jj) = SUM( 1.0 - at_i(:,jj) )   ! = REAL(jpj) if ocean everywhere on a j-line
+               zswitch(jj) = SUM( 1.0 - at_i(:,jj) )   ! = REAL(jpj) if ocean everywhere on a j-line
                zmsk(jj) = SUM( tmask(:,jj,1)    )   ! = 0         if land  everywhere on a j-line
             END DO
@@ -117,5 +110,5 @@
                i_j1  = njeq
                i_jpj = jpj
-               DO WHILE ( i_j1 <= jpj .AND. zind(i_j1) == FLOAT(jpi) .AND. zmsk(i_j1) /=0 )
+               DO WHILE ( i_j1 <= jpj .AND. zswitch(i_j1) == FLOAT(jpi) .AND. zmsk(i_j1) /=0 )
                   i_j1 = i_j1 + 1
                END DO
@@ -127,5 +120,5 @@
                i_j1  =  1
                i_jpj = njeq
-               DO WHILE ( i_jpj >= 1 .AND. zind(i_jpj) == FLOAT(jpi) .AND. zmsk(i_jpj) /=0 )
+               DO WHILE ( i_jpj >= 1 .AND. zswitch(i_jpj) == FLOAT(jpi) .AND. zmsk(i_jpj) /=0 )
                   i_jpj = i_jpj - 1
                END DO
@@ -139,5 +132,5 @@
                !                                 ! latitude strip
                i_j1  = 1
-               DO WHILE ( i_j1 <= jpj .AND. zind(i_j1) == FLOAT(jpi) .AND. zmsk(i_j1) /=0 )
+               DO WHILE ( i_j1 <= jpj .AND. zswitch(i_j1) == FLOAT(jpi) .AND. zmsk(i_j1) /=0 )
                   i_j1 = i_j1 + 1
                END DO
@@ -145,5 +138,5 @@
 
                i_jpj  = jpj
-               DO WHILE ( i_jpj >= 1  .AND. zind(i_jpj) == FLOAT(jpi) .AND. zmsk(i_jpj) /=0 )
+               DO WHILE ( i_jpj >= 1  .AND. zswitch(i_jpj) == FLOAT(jpi) .AND. zmsk(i_jpj) /=0 )
                   i_jpj = i_jpj - 1
                END DO
@@ -171,4 +164,7 @@
             END DO
          END DO
+         !
+         ! conservation test
+         IF( ln_limdiahsb ) CALL lim_cons_hsm(1, 'limdyn', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
          !
       ELSE      ! no ice dynamics : transmit directly the atmospheric stress to the ocean
@@ -224,30 +220,6 @@
       ENDIF
       !
-      ! -------------------------------
-      !- check conservation (C Rousset)
-      IF (ln_limdiahsb) THEN
-         zchk_fs  = glob_sum( ( sfx_bri(:,:) + sfx_thd(:,:) + sfx_res(:,:) + sfx_mec(:,:) ) * area(:,:) * tms(:,:) ) - zchk_fs_b
-         zchk_fw  = glob_sum( rdm_ice(:,:) * area(:,:) * tms(:,:) ) - zchk_fw_b
- 
-         zchk_v_i = ( glob_sum( SUM(   v_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) ) - zchk_v_i_b - ( zchk_fw / rhoic ) ) / rdt_ice
-         zchk_smv = ( glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) ) - zchk_smv_b ) / rdt_ice + ( zchk_fs / rhoic )
-
-         zchk_vmin = glob_min(v_i)
-         zchk_amax = glob_max(SUM(a_i,dim=3))
-         zchk_amin = glob_min(a_i)
-
-         IF(lwp) THEN
-            IF ( ABS( zchk_v_i   ) >  1.e-5 ) WRITE(numout,*) 'violation volume [m3/day]     (limdyn) = ',(zchk_v_i * rday)
-            IF ( ABS( zchk_smv   ) >  1.e-4 ) WRITE(numout,*) 'violation saline [psu*m3/day] (limdyn) = ',(zchk_smv * rday)
-            IF ( zchk_vmin <  0.            ) WRITE(numout,*) 'violation v_i<0  [mm]         (limdyn) = ',(zchk_vmin * 1.e-3)
-            !IF ( zchk_amax >  amax+1.e-10   ) WRITE(numout,*) 'violation a_i>amax            (limdyn) = ',zchk_amax
-            IF ( zchk_amin <  0.            ) WRITE(numout,*) 'violation a_i<0               (limdyn) = ',zchk_amin
-         ENDIF
-      ENDIF
-      !- check conservation (C Rousset)
-      ! -------------------------------
-
       CALL wrk_dealloc( jpi, jpj, zu_io, zv_io )
-      CALL wrk_dealloc( jpj, zind, zmsk )
+      CALL wrk_dealloc( jpj, zswitch, zmsk )
       !
       IF( nn_timing == 1 )  CALL timing_stop('limdyn')
@@ -269,8 +241,7 @@
       !!-------------------------------------------------------------------
       INTEGER  ::   ios                 ! Local integer output status for namelist read
-      NAMELIST/namicedyn/ epsd, alpha,     &
-         &                dm, nbiter, nbitdr, om, resl, cw, angvg, pstar,   &
-         &                c_rhg, etamn, creepl, ecc, ahi0, &
-         &                nevp, telast, alphaevp, hminrhg
+      NAMELIST/namicedyn/ epsd, om, cw, pstar,   &
+         &                c_rhg, creepl, ecc, ahi0,     &
+         &                nevp, relast, alphaevp, hminrhg
       !!-------------------------------------------------------------------
 
@@ -282,5 +253,5 @@
       READ  ( numnam_ice_cfg, namicedyn, IOSTAT = ios, ERR = 902 )
 902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namicedyn in configuration namelist', lwp )
-      WRITE ( numoni, namicedyn )
+      IF(lwm) WRITE ( numoni, namicedyn )
       
       IF(lwp) THEN                        ! control print
@@ -289,36 +260,22 @@
          WRITE(numout,*) '~~~~~~~~~~~~'
          WRITE(numout,*) '   tolerance parameter                              epsd   = ', epsd
-         WRITE(numout,*) '   coefficient for semi-implicit coriolis           alpha  = ', alpha
-         WRITE(numout,*) '   diffusion constant for dynamics                  dm     = ', dm
-         WRITE(numout,*) '   number of sub-time steps for relaxation          nbiter = ', nbiter
-         WRITE(numout,*) '   maximum number of iterations for relaxation      nbitdr = ', nbitdr
          WRITE(numout,*) '   relaxation constant                              om     = ', om
-         WRITE(numout,*) '   maximum value for the residual of relaxation     resl   = ', resl
          WRITE(numout,*) '   drag coefficient for oceanic stress              cw     = ', cw
-         WRITE(numout,*) '   turning angle for oceanic stress                 angvg  = ', angvg
          WRITE(numout,*) '   first bulk-rheology parameter                    pstar  = ', pstar
          WRITE(numout,*) '   second bulk-rhelogy parameter                    c_rhg  = ', c_rhg
-         WRITE(numout,*) '   minimun value for viscosity                      etamn  = ', etamn
          WRITE(numout,*) '   creep limit                                      creepl = ', creepl
          WRITE(numout,*) '   eccentricity of the elliptical yield curve       ecc    = ', ecc
          WRITE(numout,*) '   horizontal diffusivity coeff. for sea-ice        ahi0   = ', ahi0
          WRITE(numout,*) '   number of iterations for subcycling              nevp   = ', nevp
-         WRITE(numout,*) '   timescale for elastic waves                      telast = ', telast
+         WRITE(numout,*) '   ratio of elastic timescale over ice time step    relast = ', relast
          WRITE(numout,*) '   coefficient for the solution of int. stresses  alphaevp = ', alphaevp
          WRITE(numout,*) '   min ice thickness for rheology calculations     hminrhg = ', hminrhg
       ENDIF
       !
-      IF( angvg /= 0._wp ) THEN
-         CALL ctl_warn( 'lim_dyn_init: turning angle for oceanic stress not properly coded for EVP ',   &
-            &           '(see limsbc module). We force  angvg = 0._wp'  )
-         angvg = 0._wp
-      ENDIF
-      
       usecc2 = 1._wp / ( ecc * ecc )
       rhoco  = rau0  * cw
-      angvg  = angvg * rad
-      sangvg = SIN( angvg )
-      cangvg = COS( angvg )
-      pstarh = pstar * 0.5_wp
+
+      ! elastic damping
+      telast = relast * rdt_ice
 
       !  Diffusion coefficients.
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limhdf.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limhdf.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limhdf.F90	(revision 5038)
@@ -83,10 +83,8 @@
       zdiv0(:, 1 ) = 0._wp
       zdiv0(:,jpj) = 0._wp
-      IF( .NOT.lk_vopt_loop ) THEN
-         zflu (jpi,:) = 0._wp   
-         zflv (jpi,:) = 0._wp
-         zdiv0(1,  :) = 0._wp
-         zdiv0(jpi,:) = 0._wp
-      ENDIF
+      zflu (jpi,:) = 0._wp   
+      zflv (jpi,:) = 0._wp
+      zdiv0(1,  :) = 0._wp
+      zdiv0(jpi,:) = 0._wp
 
       zconv = 1._wp           !==  horizontal diffusion using a Crant-Nicholson scheme  ==!
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limistate.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limistate.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limistate.F90	(revision 5038)
@@ -6,5 +6,5 @@
    !! History :  2.0  ! 2004-01 (C. Ethe, G. Madec)  Original code
    !!            4.0  ! 2011-02 (G. Madec) dynamical allocation
-   !!             -   ! 2012    (C. Rousset) add par_oce (for jp_sal)...bug?
+   !!             -   ! 2014    (C. Rousset) add N/S initializations
    !!----------------------------------------------------------------------
 #if defined key_lim3
@@ -26,5 +26,4 @@
    USE dom_ice          ! sea-ice domain
    USE in_out_manager   ! I/O manager
-   USE lbclnk           ! lateral boundary condition - MPP exchanges
    USE lib_mpp          ! MPP library
    USE lib_fortran      ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
@@ -36,16 +35,18 @@
    PUBLIC   lim_istate      ! routine called by lim_init.F90
 
-   !! * Module variables
    !                          !!** init namelist (namiceini) **
-   REAL(wp) ::   ttest   ! threshold water temperature for initial sea ice
-   REAL(wp) ::   hninn   ! initial snow thickness in the north
-   REAL(wp) ::   hnins   ! initial snow thickness in the south
-   REAL(wp) ::   hginn   ! initial ice thickness in the north
-   REAL(wp) ::   hgins   ! initial ice thickness in the south
-   REAL(wp) ::   aginn   ! initial leads area in the north
-   REAL(wp) ::   agins   ! initial leads area in the south
-   REAL(wp) ::   sinn    ! initial salinity 
-   REAL(wp) ::   sins  
-
+   REAL(wp) ::   thres_sst   ! threshold water temperature for initial sea ice
+   REAL(wp) ::   hts_ini_n   ! initial snow thickness in the north
+   REAL(wp) ::   hts_ini_s   ! initial snow thickness in the south
+   REAL(wp) ::   hti_ini_n   ! initial ice thickness in the north
+   REAL(wp) ::   hti_ini_s   ! initial ice thickness in the south
+   REAL(wp) ::   ati_ini_n   ! initial leads area in the north
+   REAL(wp) ::   ati_ini_s   ! initial leads area in the south
+   REAL(wp) ::   smi_ini_n   ! initial salinity 
+   REAL(wp) ::   smi_ini_s   ! initial salinity
+   REAL(wp) ::   tmi_ini_n   ! initial temperature
+   REAL(wp) ::   tmi_ini_s   ! initial temperature
+
+   LOGICAL  ::  ln_limini    ! initialization or not
    !!----------------------------------------------------------------------
    !!   LIM 3.0,  UCL-LOCEAN-IPSL (2008)
@@ -53,5 +54,4 @@
    !! Software governed by the CeCILL licence (modipsl/doc/NEMO_CeCILL.txt)
    !!----------------------------------------------------------------------
-
 CONTAINS
 
@@ -77,5 +77,5 @@
       !!
       !! ** Notes   : o_i, t_su, t_s, t_i, s_i must be filled everywhere, even
-      !!              where there is no ice (clem: I do not know why but it is mandatory) 
+      !!              where there is no ice (clem: I do not know why, is it mandatory?) 
       !!
       !! History :
@@ -90,16 +90,17 @@
       INTEGER    :: i_hemis, i_fill, jl0  
       REAL(wp)   :: ztest_1, ztest_2, ztest_3, ztest_4, ztests, zsigma, zarg, zA, zV, zA_cons, zV_cons, zconv
-      REAL(wp), POINTER, DIMENSION(:)     :: zhm_i_ini, zat_i_ini, zvt_i_ini, zhm_s_ini, zsm_i_ini
-      REAL(wp), POINTER, DIMENSION(:,:)   :: zht_i_ini, za_i_ini, zv_i_ini
-      REAL(wp), POINTER, DIMENSION(:,:)   :: zidto    ! ice indicator
+      REAL(wp), POINTER, DIMENSION(:)     :: zht_i_ini, zat_i_ini, zvt_i_ini, zht_s_ini, zsm_i_ini, ztm_i_ini
+      REAL(wp), POINTER, DIMENSION(:,:)   :: zh_i_ini, za_i_ini, zv_i_ini
+      REAL(wp), POINTER, DIMENSION(:,:)   :: zswitch    ! ice indicator
       INTEGER,  POINTER, DIMENSION(:,:)   :: zhemis   ! hemispheric index
       !--------------------------------------------------------------------
 
-      CALL wrk_alloc( jpi, jpj, zidto )
+      CALL wrk_alloc( jpi, jpj, zswitch )
       CALL wrk_alloc( jpi, jpj, zhemis )
-      CALL wrk_alloc( jpl,   2, zht_i_ini,  za_i_ini,  zv_i_ini )
-      CALL wrk_alloc(   2,      zhm_i_ini, zat_i_ini, zvt_i_ini, zhm_s_ini, zsm_i_ini )
-
-      epsi20   = 1.0e-20
+      CALL wrk_alloc( jpl,   2, zh_i_ini,  za_i_ini,  zv_i_ini )
+      CALL wrk_alloc(   2,      zht_i_ini, zat_i_ini, zvt_i_ini, zht_s_ini, zsm_i_ini, ztm_i_ini )
+
+      epsi20   = 1.e-20_wp
+
       IF(lwp) WRITE(numout,*)
       IF(lwp) WRITE(numout,*) 'lim_istate : Ice initialization '
@@ -112,27 +113,31 @@
       CALL lim_istate_init     !  reading the initials parameters of the ice
 
-!!gm  in lim2  the initialisation if only done if required in the namelist :
-!!gm      IF( .NOT. ln_limini ) THEN
-!!gm  this should be added in lim3 namelist...
+      ! surface temperature
+      DO jl = 1, jpl ! loop over categories
+         t_su  (:,:,jl) = rtt * tms(:,:)
+         tn_ice(:,:,jl) = rtt * tms(:,:)
+      END DO
+
+      ! basal temperature (considered at freezing point)
+      t_bo(:,:) = ( eos_fzp( tsn(:,:,1,jp_sal) ) + rt0 ) * tms(:,:) 
+
+      IF( ln_limini ) THEN
 
       !--------------------------------------------------------------------
       ! 2) Basal temperature, ice mask and hemispheric index
       !--------------------------------------------------------------------
-
-      ! Basal temperature is set to the freezing point of seawater in Celsius
-      t_bo(:,:) = tfreez( tsn(:,:,1,jp_sal) ) * tmask(:,:,1)       ! freezing/melting point of sea water [Celcius]
 
       DO jj = 1, jpj                                       ! ice if sst <= t-freez + ttest
          DO ji = 1, jpi
-            IF( tsn(ji,jj,1,jp_tem)  - t_bo(ji,jj) >= ttest ) THEN   ;   zidto(ji,jj) = 0._wp      ! no ice
-            ELSE                                                     ;   zidto(ji,jj) = 1._wp      !    ice
+            IF( ( tsn(ji,jj,1,jp_tem)  - ( t_bo(ji,jj) - rt0 ) ) * tms(ji,jj) >= thres_sst ) THEN 
+               zswitch(ji,jj) = 0._wp * tms(ji,jj)    ! no ice
+            ELSE                                                                                   
+               zswitch(ji,jj) = 1._wp * tms(ji,jj)    !    ice
             ENDIF
          END DO
       END DO
 
-      t_bo(:,:) = t_bo(:,:) + rt0                          ! conversion to Kelvin
 
       ! Hemispheric index
-      ! MV 2011 new initialization
       DO jj = 1, jpj
          DO ji = 1, jpi
@@ -144,5 +149,4 @@
          END DO
       END DO
-      ! END MV 2011 new initialization
 
       !--------------------------------------------------------------------
@@ -153,11 +157,12 @@
       ! 3.1) Hemisphere-dependent arrays
       !-----------------------------
-      ! assign initial thickness, concentration, snow depth and salinity to
-      ! an hemisphere-dependent array
-      zhm_i_ini(1) = hginn ; zhm_i_ini(2) = hgins  ! ice thickness
-      zat_i_ini(1) = aginn ; zat_i_ini(2) = agins  ! ice concentration
-      zvt_i_ini(:) = zhm_i_ini(:) * zat_i_ini(:)   ! ice volume
-      zhm_s_ini(1) = hninn ; zhm_s_ini(2) = hnins  ! snow depth
-      zsm_i_ini(1) = sinn  ; zsm_i_ini(2) = sins   ! bulk ice salinity
+      ! assign initial thickness, concentration, snow depth and salinity to an hemisphere-dependent array
+      zht_i_ini(1) = hti_ini_n ; zht_i_ini(2) = hti_ini_s  ! ice thickness
+      zht_s_ini(1) = hts_ini_n ; zht_s_ini(2) = hts_ini_s  ! snow depth
+      zat_i_ini(1) = ati_ini_n ; zat_i_ini(2) = ati_ini_s  ! ice concentration
+      zsm_i_ini(1) = smi_ini_n ; zsm_i_ini(2) = smi_ini_s  ! bulk ice salinity
+      ztm_i_ini(1) = tmi_ini_n ; ztm_i_ini(2) = tmi_ini_s  ! temperature (ice and snow)
+
+      zvt_i_ini(:) = zht_i_ini(:) * zat_i_ini(:)   ! ice volume
 
       !---------------------------------------------------------------------
@@ -183,35 +188,35 @@
             ! *** 1 category to fill
             IF ( i_fill .EQ. 1 ) THEN
-               zht_i_ini(1,i_hemis)       = zhm_i_ini(i_hemis)
-               za_i_ini(1,i_hemis)        = zat_i_ini(i_hemis)
-               zht_i_ini(2:jpl,i_hemis)   = 0._wp
-               za_i_ini(2:jpl,i_hemis)    = 0._wp
+               zh_i_ini(1,i_hemis)       = zht_i_ini(i_hemis)
+               za_i_ini(1,i_hemis)       = zat_i_ini(i_hemis)
+               zh_i_ini(2:jpl,i_hemis)   = 0._wp
+               za_i_ini(2:jpl,i_hemis)   = 0._wp
             ELSE
 
-            ! *** >1 categores to fill
-            !--- Ice thicknesses in the i_fill - 1 first categories
+               ! *** >1 categores to fill
+               !--- Ice thicknesses in the i_fill - 1 first categories
                DO jl = 1, i_fill - 1
-                  zht_i_ini(jl,i_hemis)    = 0.5 * ( hi_max(jl) + hi_max(jl-1) )
+                  zh_i_ini(jl,i_hemis)    = 0.5 * ( hi_max(jl) + hi_max(jl-1) )
                END DO
-
-            !--- jl0: most likely index where cc will be maximum
+               
+               !--- jl0: most likely index where cc will be maximum
                DO jl = 1, jpl
-                  IF ( ( zhm_i_ini(i_hemis) .GT. hi_max(jl-1) ) .AND. &
-                       ( zhm_i_ini(i_hemis) .LE. hi_max(jl)   ) ) THEN
+                  IF ( ( zht_i_ini(i_hemis) .GT. hi_max(jl-1) ) .AND. &
+                     ( zht_i_ini(i_hemis) .LE. hi_max(jl)   ) ) THEN
                      jl0 = jl
                   ENDIF
                END DO
                jl0 = MIN(jl0, i_fill)
-
-            !--- Concentrations
+               
+               !--- Concentrations
                za_i_ini(jl0,i_hemis)      = zat_i_ini(i_hemis) / SQRT(REAL(jpl))
                DO jl = 1, i_fill - 1
                   IF ( jl .NE. jl0 ) THEN
-                     zsigma               = 0.5 * zhm_i_ini(i_hemis)
-                     zarg                 = ( zht_i_ini(jl,i_hemis) - zhm_i_ini(i_hemis) ) / zsigma
+                     zsigma               = 0.5 * zht_i_ini(i_hemis)
+                     zarg                 = ( zh_i_ini(jl,i_hemis) - zht_i_ini(i_hemis) ) / zsigma
                      za_i_ini(jl,i_hemis) = za_i_ini(jl0,i_hemis) * EXP(-zarg**2)
                   ENDIF
-               END DO 
-
+               END DO
+               
                zA = 0. ! sum of the areas in the jpl categories 
                DO jl = 1, i_fill - 1
@@ -221,14 +226,14 @@
                IF ( i_fill .LT. jpl ) za_i_ini(i_fill+1:jpl, i_hemis) = 0._wp
          
-            !--- Ice thickness in the last category
+               !--- Ice thickness in the last category
                zV = 0. ! sum of the volumes of the N-1 categories
                DO jl = 1, i_fill - 1
-                  zV = zV + za_i_ini(jl,i_hemis)*zht_i_ini(jl,i_hemis)
+                  zV = zV + za_i_ini(jl,i_hemis)*zh_i_ini(jl,i_hemis)
                END DO
-               zht_i_ini(i_fill,i_hemis) = ( zvt_i_ini(i_hemis) - zV ) / za_i_ini(i_fill,i_hemis) 
-               IF ( i_fill .LT. jpl ) zht_i_ini(i_fill+1:jpl, i_hemis) = 0._wp
-
-            !--- volumes
-               zv_i_ini(:,i_hemis) = za_i_ini(:,i_hemis) * zht_i_ini(:,i_hemis)
+               zh_i_ini(i_fill,i_hemis) = ( zvt_i_ini(i_hemis) - zV ) / za_i_ini(i_fill,i_hemis) 
+               IF ( i_fill .LT. jpl ) zh_i_ini(i_fill+1:jpl, i_hemis) = 0._wp
+
+               !--- volumes
+               zv_i_ini(:,i_hemis) = za_i_ini(:,i_hemis) * zh_i_ini(:,i_hemis)
                IF ( i_fill .LT. jpl ) zv_i_ini(i_fill+1:jpl, i_hemis) = 0._wp
 
@@ -262,10 +267,10 @@
 
             ! Test 3: thickness of the last category is in-bounds ?
-            IF ( zht_i_ini(i_fill, i_hemis) .GT. hi_max(i_fill-1) ) THEN
+            IF ( zh_i_ini(i_fill, i_hemis) .GT. hi_max(i_fill-1) ) THEN
                ztest_3 = 1
             ELSE
                ! this write is useful
-               IF(lwp) WRITE(numout,*) ' * TEST 3 THICKNESS OF THE LAST CATEGORY OUT OF BOUNDS *** zht_i_ini(i_fill,i_hemis) = ', &
-               zht_i_ini(i_fill,i_hemis), ' hi_max(jpl-1) = ', hi_max(i_fill-1)
+               IF(lwp) WRITE(numout,*) ' * TEST 3 THICKNESS OF THE LAST CATEGORY OUT OF BOUNDS *** zh_i_ini(i_fill,i_hemis) = ', &
+               zh_i_ini(i_fill,i_hemis), ' hi_max(jpl-1) = ', hi_max(i_fill-1)
                ztest_3 = 0
             ENDIF
@@ -288,17 +293,14 @@
 
       IF(lwp) THEN 
-         WRITE(numout,*), ' ztests : ', ztests
+         WRITE(numout,*) ' ztests : ', ztests
          IF ( ztests .NE. 4 ) THEN
             WRITE(numout,*)
-            WRITE(numout,*), ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! '
-            WRITE(numout,*), ' !!!! RED ALERT                  !!! '
-            WRITE(numout,*), ' !!!! BIIIIP BIIIP BIIIIP BIIIIP !!!'
-            WRITE(numout,*), ' !!!! Something is wrong in the LIM3 initialization procedure '
-            WRITE(numout,*), ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! '
+            WRITE(numout,*) ' !!!! ALERT                  !!! '
+            WRITE(numout,*) ' !!!! Something is wrong in the LIM3 initialization procedure '
             WRITE(numout,*)
-            WRITE(numout,*), ' *** ztests is not equal to 4 '
-            WRITE(numout,*), ' *** ztest_i (i=1,4) = ', ztest_1, ztest_2, ztest_3, ztest_4
-            WRITE(numout,*), ' zat_i_ini : ', zat_i_ini(i_hemis)
-            WRITE(numout,*), ' zhm_i_ini : ', zhm_i_ini(i_hemis)
+            WRITE(numout,*) ' *** ztests is not equal to 4 '
+            WRITE(numout,*) ' *** ztest_i (i=1,4) = ', ztest_1, ztest_2, ztest_3, ztest_4
+            WRITE(numout,*) ' zat_i_ini : ', zat_i_ini(i_hemis)
+            WRITE(numout,*) ' zht_i_ini : ', zht_i_ini(i_hemis)
          ENDIF ! ztests .NE. 4
       ENDIF
@@ -314,10 +316,10 @@
          DO jj = 1, jpj
             DO ji = 1, jpi
-               a_i(ji,jj,jl)   = zidto(ji,jj) * za_i_ini (jl,zhemis(ji,jj))  ! concentration
-               ht_i(ji,jj,jl)  = zidto(ji,jj) * zht_i_ini(jl,zhemis(ji,jj))  ! ice thickness
-               ht_s(ji,jj,jl)  = ht_i(ji,jj,jl) * ( zhm_s_ini( zhemis(ji,jj) ) / zhm_i_ini( zhemis(ji,jj) ) )  ! snow depth
-               sm_i(ji,jj,jl)  = zidto(ji,jj) * zsm_i_ini(zhemis(ji,jj)) + ( 1._wp - zidto(ji,jj) ) * s_i_min ! salinity
-               o_i(ji,jj,jl)   = zidto(ji,jj) * 1._wp + ( 1._wp - zidto(ji,jj) ) ! age
-               t_su(ji,jj,jl)  = zidto(ji,jj) * 270.0 + ( 1._wp - zidto(ji,jj) ) * 270.0 ! surf temp
+               a_i(ji,jj,jl)   = zswitch(ji,jj) * za_i_ini (jl,zhemis(ji,jj))  ! concentration
+               ht_i(ji,jj,jl)  = zswitch(ji,jj) * zh_i_ini(jl,zhemis(ji,jj))  ! ice thickness
+               ht_s(ji,jj,jl)  = ht_i(ji,jj,jl) * ( zht_s_ini( zhemis(ji,jj) ) / zht_i_ini( zhemis(ji,jj) ) )  ! snow depth
+               sm_i(ji,jj,jl)  = zswitch(ji,jj) * zsm_i_ini(zhemis(ji,jj)) !+ ( 1._wp - zswitch(ji,jj) ) * s_i_min ! salinity
+               o_i(ji,jj,jl)   = zswitch(ji,jj) * 1._wp + ( 1._wp - zswitch(ji,jj) ) ! age
+               t_su(ji,jj,jl)  = zswitch(ji,jj) * ztm_i_ini(zhemis(ji,jj)) + ( 1._wp - zswitch(ji,jj) ) * rtt ! surf temp
 
                ! This case below should not be used if (ht_s/ht_i) is ok in namelist
@@ -343,10 +345,10 @@
             DO jj = 1, jpj
                DO ji = 1, jpi
-                   t_s(ji,jj,jk,jl) = zidto(ji,jj) * 270.0 + ( 1._wp - zidto(ji,jj) ) * rtt
+                   t_s(ji,jj,jk,jl) = zswitch(ji,jj) * ztm_i_ini(zhemis(ji,jj)) + ( 1._wp - zswitch(ji,jj) ) * rtt
                    ! Snow energy of melting
-                   e_s(ji,jj,jk,jl) = zidto(ji,jj) * rhosn * ( cpic * ( rtt - t_s(ji,jj,jk,jl) ) + lfus )
+                   e_s(ji,jj,jk,jl) = zswitch(ji,jj) * rhosn * ( cpic * ( rtt - t_s(ji,jj,jk,jl) ) + lfus )
                    ! Change dimensions
                    e_s(ji,jj,jk,jl) = e_s(ji,jj,jk,jl) / unit_fac
-                   ! Multiply by volume, so that heat content in 10^9 Joules
+                   ! Multiply by volume, so that heat content in Joules
                    e_s(ji,jj,jk,jl) = e_s(ji,jj,jk,jl) * area(ji,jj) * v_s(ji,jj,jl) / nlay_s
                END DO ! ji
@@ -360,10 +362,10 @@
             DO jj = 1, jpj
                DO ji = 1, jpi
-                   t_i(ji,jj,jk,jl) = zidto(ji,jj) * 270.00 + ( 1._wp - zidto(ji,jj) ) * rtt 
-                   s_i(ji,jj,jk,jl) = zidto(ji,jj) * zsm_i_ini(zhemis(ji,jj)) + ( 1._wp - zidto(ji,jj) ) * s_i_min
+                   t_i(ji,jj,jk,jl) = zswitch(ji,jj) * ztm_i_ini(zhemis(ji,jj)) + ( 1._wp - zswitch(ji,jj) ) * rtt 
+                   s_i(ji,jj,jk,jl) = zswitch(ji,jj) * zsm_i_ini(zhemis(ji,jj)) !+ ( 1._wp - zswitch(ji,jj) ) * s_i_min
                    ztmelts          = - tmut * s_i(ji,jj,jk,jl) + rtt !Melting temperature in K
 
                    ! heat content per unit volume
-                   e_i(ji,jj,jk,jl) = zidto(ji,jj) * rhoic * (   cpic    * ( ztmelts - t_i(ji,jj,jk,jl) ) &
+                   e_i(ji,jj,jk,jl) = zswitch(ji,jj) * rhoic * (   cpic    * ( ztmelts - t_i(ji,jj,jk,jl) ) &
                       +   lfus    * ( 1._wp - (ztmelts-rtt) / MIN((t_i(ji,jj,jk,jl)-rtt),-epsi20) ) &
                       -   rcp     * ( ztmelts - rtt ) )
@@ -372,6 +374,5 @@
                    e_i(ji,jj,jk,jl) = e_i(ji,jj,jk,jl) / unit_fac 
 
-                   ! Mutliply by ice volume, and divide by number of layers 
-                   ! to get heat content in 10^9 J
+                   ! Mutliply by ice volume, and divide by number of layers to get heat content in J
                    e_i(ji,jj,jk,jl) = e_i(ji,jj,jk,jl) * area(ji,jj) * v_i(ji,jj,jl) / nlay_i
                END DO ! ji
@@ -380,8 +381,40 @@
       END DO ! jk
 
+      tn_ice (:,:,:) = t_su (:,:,:)
+
+      ELSE 
+         ! if ln_limini=false
+         a_i  (:,:,:) = 0._wp
+         v_i  (:,:,:) = 0._wp
+         v_s  (:,:,:) = 0._wp
+         smv_i(:,:,:) = 0._wp
+         oa_i (:,:,:) = 0._wp
+         ht_i (:,:,:) = 0._wp
+         ht_s (:,:,:) = 0._wp
+         sm_i (:,:,:) = 0._wp
+         o_i  (:,:,:) = 0._wp
+
+         e_i(:,:,:,:) = 0._wp
+         e_s(:,:,:,:) = 0._wp
+
+         DO jl = 1, jpl
+            DO jk = 1, nlay_i
+               t_i(:,:,jk,jl) = rtt * tms(:,:)
+            END DO
+            DO jk = 1, nlay_s
+               t_s(:,:,jk,jl) = rtt * tms(:,:)
+            END DO
+         END DO
+      
+      ENDIF ! ln_limini
+      
+      at_i (:,:) = 0.0_wp
+      DO jl = 1, jpl
+         at_i (:,:) = at_i (:,:) + a_i (:,:,jl)
+      END DO
+      !
       !--------------------------------------------------------------------
       ! 4) Global ice variables for output diagnostics                    | 
       !--------------------------------------------------------------------
-      fsbbq (:,:)     = 0._wp
       u_ice (:,:)     = 0._wp
       v_ice (:,:)     = 0._wp
@@ -390,8 +423,4 @@
       stress12_i(:,:) = 0._wp
 
-# if defined key_coupled
-      albege(:,:)   = 0.8 * tms(:,:)
-# endif
-
       !--------------------------------------------------------------------
       ! 5) Moments for advection
@@ -428,52 +457,9 @@
       sxyage (:,:,:)  = 0._wp
 
-      !--------------------------------------------------------------------
-      ! 6) Lateral boundary conditions                                    | 
-      !--------------------------------------------------------------------
-
-      DO jl = 1, jpl
-
-         CALL lbc_lnk( a_i(:,:,jl)  , 'T', 1. )
-         CALL lbc_lnk( v_i(:,:,jl)  , 'T', 1. )
-         CALL lbc_lnk( v_s(:,:,jl)  , 'T', 1. )
-         CALL lbc_lnk( smv_i(:,:,jl), 'T', 1. )
-         CALL lbc_lnk( oa_i(:,:,jl) , 'T', 1. )
-
-         CALL lbc_lnk( ht_i(:,:,jl) , 'T', 1. )
-         CALL lbc_lnk( ht_s(:,:,jl) , 'T', 1. )
-         CALL lbc_lnk( sm_i(:,:,jl) , 'T', 1. )
-         CALL lbc_lnk( o_i(:,:,jl)  , 'T', 1. )
-         CALL lbc_lnk( t_su(:,:,jl) , 'T', 1. )
-         DO jk = 1, nlay_s
-            CALL lbc_lnk(t_s(:,:,jk,jl), 'T', 1. )
-            CALL lbc_lnk(e_s(:,:,jk,jl), 'T', 1. )
-         END DO
-         DO jk = 1, nlay_i
-            CALL lbc_lnk(t_i(:,:,jk,jl), 'T', 1. )
-            CALL lbc_lnk(e_i(:,:,jk,jl), 'T', 1. )
-         END DO
-         !
-         a_i(:,:,jl) = tms(:,:) * a_i(:,:,jl)
-      END DO
-      
-      at_i (:,:) = 0.0_wp
-      DO jl = 1, jpl
-         at_i (:,:) = at_i (:,:) + a_i (:,:,jl)
-      END DO
-
-      CALL lbc_lnk( at_i , 'T', 1. )
-      at_i(:,:) = tms(:,:) * at_i(:,:)                       ! put 0 over land
-      !
-      CALL lbc_lnk( fsbbq  , 'T', 1. )
-      !
-      !--------------------------------------------------------------------
-      ! 6) ????                                                           | 
-      !--------------------------------------------------------------------
-      tn_ice (:,:,:) = t_su (:,:,:)
-
-      CALL wrk_dealloc( jpi, jpj, zidto )
+
+      CALL wrk_dealloc( jpi, jpj, zswitch )
       CALL wrk_dealloc( jpi, jpj, zhemis )
-      CALL wrk_dealloc( jpl,   2, zht_i_ini,  za_i_ini,  zv_i_ini )
-      CALL wrk_dealloc(   2,      zhm_i_ini, zat_i_ini, zvt_i_ini, zhm_s_ini, zsm_i_ini )
+      CALL wrk_dealloc( jpl,   2, zh_i_ini,  za_i_ini,  zv_i_ini )
+      CALL wrk_dealloc(   2,      zht_i_ini, zat_i_ini, zvt_i_ini, zht_s_ini, zsm_i_ini, ztm_i_ini )
 
    END SUBROUTINE lim_istate
@@ -495,6 +481,6 @@
       !!  8.5  ! 07-11 (M. Vancoppenolle) rewritten initialization
       !!-----------------------------------------------------------------------------
-      NAMELIST/namiceini/ ttest, hninn, hnins, hginn, hgins, aginn, agins, sinn, sins
-      !
+      NAMELIST/namiceini/ ln_limini, thres_sst, hts_ini_n, hts_ini_s, hti_ini_n, hti_ini_s,  &
+         &                                      ati_ini_n, ati_ini_s, smi_ini_n, smi_ini_s, tmi_ini_n, tmi_ini_s
       INTEGER :: ios                 ! Local integer output status for namelist read
       !!-----------------------------------------------------------------------------
@@ -507,5 +493,5 @@
       READ  ( numnam_ice_cfg, namiceini, IOSTAT = ios, ERR = 902 )
 902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namiceini in configuration namelist', lwp )
-      WRITE ( numoni, namiceini )
+      IF(lwm) WRITE ( numoni, namiceini )
 
       ! Define the initial parameters
@@ -516,13 +502,16 @@
          WRITE(numout,*) 'lim_istate_init : ice parameters inititialisation '
          WRITE(numout,*) '~~~~~~~~~~~~~~~'
-         WRITE(numout,*) '   threshold water temp. for initial sea-ice    ttest      = ', ttest
-         WRITE(numout,*) '   initial snow thickness in the north          hninn      = ', hninn
-         WRITE(numout,*) '   initial snow thickness in the south          hnins      = ', hnins 
-         WRITE(numout,*) '   initial ice thickness  in the north          hginn      = ', hginn
-         WRITE(numout,*) '   initial ice thickness  in the south          hgins      = ', hgins
-         WRITE(numout,*) '   initial ice concentr.  in the north          aginn      = ', aginn
-         WRITE(numout,*) '   initial ice concentr.  in the north          agins      = ', agins
-         WRITE(numout,*) '   initial  ice salinity  in the north          sinn       = ', sinn
-         WRITE(numout,*) '   initial  ice salinity  in the south          sins       = ', sins
+         WRITE(numout,*) '   initialization with ice (T) or not (F)       ln_limini   = ', ln_limini
+         WRITE(numout,*) '   threshold water temp. for initial sea-ice    thres_sst  = ', thres_sst
+         WRITE(numout,*) '   initial snow thickness in the north          hts_ini_n  = ', hts_ini_n
+         WRITE(numout,*) '   initial snow thickness in the south          hts_ini_s  = ', hts_ini_s 
+         WRITE(numout,*) '   initial ice thickness  in the north          hti_ini_n  = ', hti_ini_n
+         WRITE(numout,*) '   initial ice thickness  in the south          hti_ini_s  = ', hti_ini_s
+         WRITE(numout,*) '   initial ice concentr.  in the north          ati_ini_n  = ', ati_ini_n
+         WRITE(numout,*) '   initial ice concentr.  in the north          ati_ini_s  = ', ati_ini_s
+         WRITE(numout,*) '   initial  ice salinity  in the north          smi_ini_n  = ', smi_ini_n
+         WRITE(numout,*) '   initial  ice salinity  in the south          smi_ini_s  = ', smi_ini_s
+         WRITE(numout,*) '   initial  ice/snw temp  in the north          tmi_ini_n  = ', tmi_ini_n
+         WRITE(numout,*) '   initial  ice/snw temp  in the south          tmi_ini_s  = ', tmi_ini_s
       ENDIF
 
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limitd_me.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limitd_me.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limitd_me.F90	(revision 5038)
@@ -5,5 +5,5 @@
    !!======================================================================
    !! History :  LIM  ! 2006-02  (M. Vancoppenolle) Original code 
-   !!            3.2  ! 2009-07  (M. Vancoppenolle, Y. Aksenov, G. Madec) bug correction in smsw & sfx_mec
+   !!            3.2  ! 2009-07  (M. Vancoppenolle, Y. Aksenov, G. Madec) bug correction in smsw & sfx_dyn
    !!            4.0  ! 2011-02  (G. Madec) dynamical allocation
    !!----------------------------------------------------------------------
@@ -22,5 +22,4 @@
    USE limthd_lac       ! LIM
    USE limvar           ! LIM
-   USE limcons          ! LIM
    USE in_out_manager   ! I/O manager
    USE lbclnk           ! lateral boundary condition - MPP exchanges
@@ -30,7 +29,8 @@
   ! Check budget (Rousset)
    USE iom              ! I/O manager
-   USE lib_fortran     ! glob_sum
+   USE lib_fortran      ! glob_sum
    USE limdiahsb
-   USE timing          ! Timing
+   USE timing           ! Timing
+   USE limcons          ! conservation tests
 
    IMPLICIT NONE
@@ -42,8 +42,4 @@
    PUBLIC   lim_itd_me_zapsmall
    PUBLIC   lim_itd_me_alloc        ! called by iceini.F90
-
-   REAL(wp) ::   epsi20 = 1.e-20_wp   ! constant values
-   REAL(wp) ::   epsi10 = 1.e-10_wp   ! constant values
-   REAL(wp) ::   epsi06 = 1.e-06_wp   ! constant values
 
    !-----------------------------------------------------------------------
@@ -143,16 +139,10 @@
       REAL(wp), POINTER, DIMENSION(:,:) ::   esnow_mlt       ! energy needed to melt snow in ocean (J m-2)
       REAL(wp), POINTER, DIMENSION(:,:) ::   vt_i_init, vt_i_final  !  ice volume summed over categories
-      REAL(wp) :: zchk_v_i, zchk_smv, zchk_fs, zchk_fw, zchk_v_i_b, zchk_smv_b, zchk_fs_b, zchk_fw_b ! Check conservation (C Rousset)
-      REAL(wp) :: zchk_vmin, zchk_amin, zchk_amax ! Check errors (C Rousset)
-      ! mass and salt flux (clem)
-      REAL(wp), POINTER, DIMENSION(:,:,:) ::   zviold, zvsold, zsmvold   ! old ice volume...
+      !
+      REAL(wp) :: zvi_b, zsmv_b, zei_b, zfs_b, zfw_b, zft_b 
       !!-----------------------------------------------------------------------------
       IF( nn_timing == 1 )  CALL timing_start('limitd_me')
 
       CALL wrk_alloc( jpi, jpj, closing_net, divu_adv, opning, closing_gross, msnow_mlt, esnow_mlt, vt_i_init, vt_i_final )
-
-      CALL wrk_alloc( jpi, jpj, jpl, zviold, zvsold, zsmvold )   ! clem
-
-      IF( numit == nstart  )   CALL lim_itd_me_init   ! Initialization (first time-step only)
 
       IF(ln_ctl) THEN
@@ -162,19 +152,7 @@
 
       IF( ln_limdyn ) THEN          !   Start ridging and rafting   !
-      ! -------------------------------
-      !- check conservation (C Rousset)
-      IF (ln_limdiahsb) THEN
-         zchk_v_i_b = glob_sum( SUM(   v_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) )
-         zchk_smv_b = glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) )
-         zchk_fw_b  = glob_sum( rdm_ice(:,:) * area(:,:) * tms(:,:) )
-         zchk_fs_b  = glob_sum( ( sfx_bri(:,:) + sfx_thd(:,:) + sfx_res(:,:) + sfx_mec(:,:) ) * area(:,:) * tms(:,:) )
-      ENDIF
-      !- check conservation (C Rousset)
-      ! -------------------------------
-
-      ! mass and salt flux init (clem)
-      zviold(:,:,:) = v_i(:,:,:)
-      zvsold(:,:,:) = v_s(:,:,:)
-      zsmvold(:,:,:) = smv_i(:,:,:)
+
+      ! conservation test
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(0, 'limitd_me', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
 
       !-----------------------------------------------------------------------------!
@@ -362,6 +340,6 @@
             ! 5) Heat, salt and freshwater fluxes
             !-----------------------------------------------------------------------------!
-            fmmec(ji,jj) = fmmec(ji,jj) + msnow_mlt(ji,jj) * r1_rdtice     ! fresh water source for ocean
-            fhmec(ji,jj) = fhmec(ji,jj) + esnow_mlt(ji,jj) * r1_rdtice     ! heat sink for ocean
+            wfx_snw(ji,jj) = wfx_snw(ji,jj) + msnow_mlt(ji,jj) * r1_rdtice     ! fresh water source for ocean
+            hfx_dyn(ji,jj) = hfx_dyn(ji,jj) + esnow_mlt(ji,jj) * unit_fac / area(ji,jj) * r1_rdtice  ! heat sink for ocean (<0, W.m-2)
 
          END DO
@@ -399,17 +377,4 @@
       CALL lim_itd_me_zapsmall
 
-      !--------------------------------
-      ! Update mass/salt fluxes (clem)
-      !--------------------------------
-      DO jl = 1, jpl
-         DO jj = 1, jpj 
-            DO ji = 1, jpi
-               diag_dyn_gr(ji,jj) = diag_dyn_gr(ji,jj) + ( v_i(ji,jj,jl) - zviold(ji,jj,jl) ) * r1_rdtice
-               rdm_ice(ji,jj) = rdm_ice(ji,jj) + ( v_i(ji,jj,jl) - zviold(ji,jj,jl) ) * rhoic 
-               rdm_snw(ji,jj) = rdm_snw(ji,jj) + ( v_s(ji,jj,jl) - zvsold(ji,jj,jl) ) * rhosn 
-               sfx_mec(ji,jj) = sfx_mec(ji,jj) - ( smv_i(ji,jj,jl) - zsmvold(ji,jj,jl) ) * rhoic * r1_rdtice 
-            END DO
-         END DO
-      END DO
 
       IF(ln_ctl) THEN     ! Control print
@@ -445,33 +410,10 @@
       ENDIF
 
-      ! -------------------------------
-      !- check conservation (C Rousset)
-      IF (ln_limdiahsb) THEN
-         zchk_fs  = glob_sum( ( sfx_bri(:,:) + sfx_thd(:,:) + sfx_res(:,:) + sfx_mec(:,:) ) * area(:,:) * tms(:,:) ) - zchk_fs_b
-         zchk_fw  = glob_sum( rdm_ice(:,:) * area(:,:) * tms(:,:) ) - zchk_fw_b
- 
-         zchk_v_i = ( glob_sum( SUM(   v_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) ) - zchk_v_i_b - ( zchk_fw / rhoic ) ) * r1_rdtice
-         zchk_smv = ( glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) ) - zchk_smv_b ) * r1_rdtice + ( zchk_fs / rhoic )
-
-         zchk_vmin = glob_min(v_i)
-         zchk_amax = glob_max(SUM(a_i,dim=3))
-         zchk_amin = glob_min(a_i)
-       
-         IF(lwp) THEN
-            IF ( ABS( zchk_v_i   ) >  1.e-5 ) WRITE(numout,*) 'violation volume [m3/day]     (limitd_me) = ',(zchk_v_i * rday)
-            IF ( ABS( zchk_smv   ) >  1.e-4 ) WRITE(numout,*) 'violation saline [psu*m3/day] (limitd_me) = ',(zchk_smv * rday)
-            IF ( zchk_vmin <  0.            ) WRITE(numout,*) 'violation v_i<0  [mm]         (limitd_me) = ',(zchk_vmin * 1.e-3)
-            IF ( zchk_amax >  kamax+epsi10  ) WRITE(numout,*) 'violation a_i>amax            (limitd_me) = ',zchk_amax
-            IF ( zchk_amin <  0.            ) WRITE(numout,*) 'violation a_i<0               (limitd_me) = ',zchk_amin
-         ENDIF
-      ENDIF
-      !- check conservation (C Rousset)
-      ! -------------------------------
+      ! conservation test
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(1, 'limitd_me', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
 
       ENDIF  ! ln_limdyn=.true.
       !
       CALL wrk_dealloc( jpi, jpj, closing_net, divu_adv, opning, closing_gross, msnow_mlt, esnow_mlt, vt_i_init, vt_i_final )
-      !
-      CALL wrk_dealloc( jpi, jpj, jpl, zviold, zvsold, zsmvold )   ! clem
       !
       IF( nn_timing == 1 )  CALL timing_stop('limitd_me')
@@ -670,5 +612,4 @@
       !!---------------------------------------------------------------------!
       INTEGER ::   ji,jj, jl    ! dummy loop indices
-      INTEGER ::   krdg_index   ! 
       REAL(wp) ::   Gstari, astari, hi, hrmean, zdummy   ! local scalar
       REAL(wp), POINTER, DIMENSION(:,:)   ::   zworka    ! temporary array used here
@@ -746,8 +687,6 @@
       !-----------------------------------------------------------------
 
-      krdg_index = 1
-
-      IF( krdg_index == 0 ) THEN       !--- Linear formulation (Thorndike et al., 1975)
-         DO jl = 0, ice_cat_bounds(1,2)       ! only undeformed ice participates
+      IF( partfun_swi == 0 ) THEN       !--- Linear formulation (Thorndike et al., 1975)
+         DO jl = 0, jpl    
             DO jj = 1, jpj 
                DO ji = 1, jpi
@@ -772,11 +711,11 @@
             Gsum(:,:,jl) = EXP( -Gsum(:,:,jl) * astari ) * zdummy
          END DO !jl
-         DO jl = 0, ice_cat_bounds(1,2)
+         DO jl = 0, jpl
              athorn(:,:,jl) = Gsum(:,:,jl-1) - Gsum(:,:,jl)
          END DO
          !
-      ENDIF ! krdg_index
-
-      IF( raftswi == 1 ) THEN      ! Ridging and rafting ice participation functions
+      ENDIF ! partfun_swi
+
+      IF( raft_swi == 1 ) THEN      ! Ridging and rafting ice participation functions
          !
          DO jl = 1, jpl
@@ -794,5 +733,5 @@
          END DO ! jl
 
-      ELSE  ! raftswi = 0
+      ELSE  ! raft_swi = 0
          !
          DO jl = 1, jpl
@@ -802,5 +741,5 @@
       ENDIF
 
-      IF ( raftswi == 1 ) THEN
+      IF ( raft_swi == 1 ) THEN
 
          IF( MAXVAL(aridge + araft - athorn(:,:,1:jpl)) .GT. epsi10 ) THEN
@@ -908,6 +847,6 @@
       INTEGER ::   ij                ! horizontal index, combines i and j loops
       INTEGER ::   icells            ! number of cells with aicen > puny
-      REAL(wp) ::   zindb, zsrdg2   ! local scalar
       REAL(wp) ::   hL, hR, farea, zdummy, zdummy0, ztmelts    ! left and right limits of integration
+      REAL(wp) ::   zsstK            ! SST in Kelvin
 
       INTEGER , POINTER, DIMENSION(:) ::   indxi, indxj   ! compressed indices
@@ -917,5 +856,5 @@
 
       REAL(wp), POINTER, DIMENSION(:,:,:) ::   aicen_init, vicen_init   ! ice  area    & volume before ridging
-      REAL(wp), POINTER, DIMENSION(:,:,:) ::   vsnon_init, esnon_init   ! snow volume  & energy before ridging
+      REAL(wp), POINTER, DIMENSION(:,:,:) ::   vsnwn_init, esnwn_init   ! snow volume  & energy before ridging
       REAL(wp), POINTER, DIMENSION(:,:,:) ::   smv_i_init, oa_i_init    ! ice salinity & age    before ridging
 
@@ -952,7 +891,7 @@
       CALL wrk_alloc( jpi, jpj,             vrdg1, vrdg2, vsw  , srdg1, srdg2, smsw )
       CALL wrk_alloc( jpi, jpj,             afrft, arft1, arft2, virft, vsrft, esrft, smrft, oirft1, oirft2 )
-      CALL wrk_alloc( jpi, jpj, jpl,        aicen_init, vicen_init, vsnon_init, esnon_init, smv_i_init, oa_i_init )
-      CALL wrk_alloc( jpi, jpj, jkmax,      eirft, erdg1, erdg2, ersw )
-      CALL wrk_alloc( jpi, jpj, jkmax, jpl, eicen_init )
+      CALL wrk_alloc( jpi, jpj, jpl,        aicen_init, vicen_init, vsnwn_init, esnwn_init, smv_i_init, oa_i_init )
+      CALL wrk_alloc( jpi, jpj, nlay_i+1,      eirft, erdg1, erdg2, ersw )
+      CALL wrk_alloc( jpi, jpj, nlay_i+1, jpl, eicen_init )
 
       ! Conservation check
@@ -1008,5 +947,5 @@
          aicen_init(:,:,jl) = a_i(:,:,jl)
          vicen_init(:,:,jl) = v_i(:,:,jl)
-         vsnon_init(:,:,jl) = v_s(:,:,jl)
+         vsnwn_init(:,:,jl) = v_s(:,:,jl)
          !
          smv_i_init(:,:,jl) = smv_i(:,:,jl)
@@ -1014,5 +953,5 @@
       END DO !jl
 
-      esnon_init(:,:,:) = e_s(:,:,1,:)
+      esnwn_init(:,:,:) = e_s(:,:,1,:)
 
       DO jl = 1, jpl  
@@ -1091,17 +1030,17 @@
             !     / rafting category n1.
             !--------------------------------------------------------------------------
-            vrdg1(ji,jj) = vicen_init(ji,jj,jl1) * afrac(ji,jj) / ( 1._wp + ridge_por )
+            vrdg1(ji,jj) = vicen_init(ji,jj,jl1) * afrac(ji,jj)
             vrdg2(ji,jj) = vrdg1(ji,jj) * ( 1. + ridge_por )
             vsw  (ji,jj) = vrdg1(ji,jj) * ridge_por
 
-            vsrdg(ji,jj) = vsnon_init(ji,jj,jl1) * afrac(ji,jj)
-            esrdg(ji,jj) = esnon_init(ji,jj,jl1) * afrac(ji,jj)
-            srdg1(ji,jj) = smv_i_init(ji,jj,jl1) * afrac(ji,jj) / ( 1._wp + ridge_por )
-            srdg2(ji,jj) = smv_i_init(ji,jj,jl1) * afrac(ji,jj)
+            vsrdg(ji,jj) = vsnwn_init(ji,jj,jl1) * afrac(ji,jj)
+            esrdg(ji,jj) = esnwn_init(ji,jj,jl1) * afrac(ji,jj)
+            srdg1(ji,jj) = smv_i_init(ji,jj,jl1) * afrac(ji,jj)
+            srdg2(ji,jj) = smv_i_init(ji,jj,jl1) * afrac(ji,jj) !! MV HC 2014 this line seems useless
 
             ! rafting volumes, heat contents ...
             virft(ji,jj) = vicen_init(ji,jj,jl1) * afrft(ji,jj)
-            vsrft(ji,jj) = vsnon_init(ji,jj,jl1) * afrft(ji,jj)
-            esrft(ji,jj) = esnon_init(ji,jj,jl1) * afrft(ji,jj)
+            vsrft(ji,jj) = vsnwn_init(ji,jj,jl1) * afrft(ji,jj)
+            esrft(ji,jj) = esnwn_init(ji,jj,jl1) * afrft(ji,jj)
             smrft(ji,jj) = smv_i_init(ji,jj,jl1) * afrft(ji,jj) 
 
@@ -1120,14 +1059,11 @@
             ! Salinity
             !-------------
-            smsw(ji,jj)  = sss_m(ji,jj) * vsw(ji,jj) * rhoic / rau0       ! salt content of seawater frozen in voids
-
-            zsrdg2       = srdg1(ji,jj) + smsw(ji,jj)                     ! salt content of new ridge
-
-            srdg2(ji,jj) = MIN( s_i_max * vrdg2(ji,jj) , zsrdg2 )         ! impose a maximum salinity
+            smsw(ji,jj)  = vsw(ji,jj) * sss_m(ji,jj)                      ! salt content of seawater frozen in voids !! MV HC2014
+            srdg2(ji,jj) = srdg1(ji,jj) + smsw(ji,jj)                     ! salt content of new ridge
+
+            !srdg2(ji,jj) = MIN( s_i_max * vrdg2(ji,jj) , zsrdg2 )         ! impose a maximum salinity
             
-            !                                                             ! excess of salt is flushed into the ocean
-            !sfx_mec(ji,jj) = sfx_mec(ji,jj) + ( zsrdg2 - srdg2(ji,jj) ) * rhoic * r1_rdtice
-
-            !rdm_ice(ji,jj) = rdm_ice(ji,jj) + vsw(ji,jj) * rhoic    ! gurvan: increase in ice volume du to seawater frozen in voids             
+            sfx_dyn(ji,jj) = sfx_dyn(ji,jj) - smsw(ji,jj) * rhoic * r1_rdtice
+            wfx_dyn(ji,jj) = wfx_dyn(ji,jj) - vsw (ji,jj) * rhoic * r1_rdtice   ! gurvan: increase in ice volume du to seawater frozen in voids             
 
             !------------------------------------            
@@ -1158,6 +1094,7 @@
                &                                + rhosn*vsrft(ji,jj)*(1.0-fsnowrft)
 
-            esnow_mlt(ji,jj) = esnow_mlt(ji,jj) + esrdg(ji,jj)*(1.0-fsnowrdg)         &   !rafting included
-               &                                + esrft(ji,jj)*(1.0-fsnowrft)          
+            ! in 1e-9 Joules (same as e_s)
+            esnow_mlt(ji,jj) = esnow_mlt(ji,jj) - esrdg(ji,jj)*(1.0-fsnowrdg)         &   !rafting included
+               &                                - esrft(ji,jj)*(1.0-fsnowrft)          
 
             !-----------------------------------------------------------------
@@ -1184,29 +1121,27 @@
                jj = indxj(ij)
                ! heat content of ridged ice
-               erdg1(ji,jj,jk)      = eicen_init(ji,jj,jk,jl1) * afrac(ji,jj) / ( 1._wp + ridge_por ) 
+               erdg1(ji,jj,jk)      = eicen_init(ji,jj,jk,jl1) * afrac(ji,jj) 
                eirft(ji,jj,jk)      = eicen_init(ji,jj,jk,jl1) * afrft(ji,jj)
                e_i  (ji,jj,jk,jl1)  = e_i(ji,jj,jk,jl1) - erdg1(ji,jj,jk) - eirft(ji,jj,jk)
-               ! sea water heat content
-               ztmelts          = - tmut * sss_m(ji,jj) + rtt
-               ! heat content per unit volume
-               zdummy0          = - rcp * ( sst_m(ji,jj) + rt0 - rtt ) * vsw(ji,jj)
-
-               ! corrected sea water salinity
-               zindb  = MAX( 0._wp , SIGN( 1._wp , vsw(ji,jj) - epsi20 ) )
-               zdummy = zindb * ( srdg1(ji,jj) - srdg2(ji,jj) ) / MAX( ridge_por * vsw(ji,jj), epsi20 )
-
-               ztmelts          = - tmut * zdummy + rtt
-               ersw(ji,jj,jk)   = - rcp * ( ztmelts - rtt ) * vsw(ji,jj)
-
-               ! heat flux
-               fheat_mec(ji,jj) = fheat_mec(ji,jj) + ( ersw(ji,jj,jk) - zdummy0 ) * r1_rdtice
+               
+               
+               ! enthalpy of the trapped seawater (J/m2, >0)
+               ! clem: if sst>0, then ersw <0 (is that possible?)
+               zsstK  = sst_m(ji,jj) + rt0
+               ersw(ji,jj,jk)   = - rhoic * vsw(ji,jj) * rcp * ( zsstK - rt0 ) / REAL( nlay_i )
+
+               ! heat flux to the ocean
+               hfx_dyn(ji,jj) = hfx_dyn(ji,jj) + ersw(ji,jj,jk) * r1_rdtice  ! > 0 [W.m-2] ocean->ice flux 
 
                ! Correct dimensions to avoid big values
-               ersw(ji,jj,jk)   = ersw(ji,jj,jk) * 1.e-09
-
-               ! Mutliply by ice volume, and divide by number of layers to get heat content in 10^9 J
-               ersw (ji,jj,jk)  = ersw(ji,jj,jk) * area(ji,jj) * vsw(ji,jj) / REAL( nlay_i )
+               ersw(ji,jj,jk)   = ersw(ji,jj,jk) / unit_fac
+
+               ! Mutliply by ice volume, and divide by number of layers to get heat content in 1.e9 J
+               ! it is added to sea ice because the sign convention is the opposite of the sign convention for the ocean 
+               !! MV HC 2014
+               ersw (ji,jj,jk)  = ersw(ji,jj,jk) * area(ji,jj)
 
                erdg2(ji,jj,jk)  = erdg1(ji,jj,jk) + ersw(ji,jj,jk)
+
             END DO ! ij
          END DO !jk
@@ -1253,5 +1188,5 @@
          !-------------------------------------------------------------------------------
          !        jl1 looping 1-jpl
-         DO jl2  = ice_cat_bounds(1,1), ice_cat_bounds(1,2) 
+         DO jl2  = 1, jpl 
             ! over categories to which ridged ice is transferred
 !CDIR NODEP
@@ -1298,5 +1233,5 @@
          END DO                 ! jl2 (new ridges)            
 
-         DO jl2 = ice_cat_bounds(1,1), ice_cat_bounds(1,2) 
+         DO jl2 = 1, jpl 
 
 !CDIR NODEP
@@ -1361,7 +1296,7 @@
       CALL wrk_dealloc( jpi, jpj,             vrdg1, vrdg2, vsw  , srdg1, srdg2, smsw )
       CALL wrk_dealloc( jpi, jpj,             afrft, arft1, arft2, virft, vsrft, esrft, smrft, oirft1, oirft2 )
-      CALL wrk_dealloc( jpi, jpj, jpl,        aicen_init, vicen_init, vsnon_init, esnon_init, smv_i_init, oa_i_init )
-      CALL wrk_dealloc( jpi, jpj, jkmax,      eirft, erdg1, erdg2, ersw )
-      CALL wrk_dealloc( jpi, jpj, jkmax, jpl, eicen_init )
+      CALL wrk_dealloc( jpi, jpj, jpl,        aicen_init, vicen_init, vsnwn_init, esnwn_init, smv_i_init, oa_i_init )
+      CALL wrk_dealloc( jpi, jpj, nlay_i+1,      eirft, erdg1, erdg2, ersw )
+      CALL wrk_dealloc( jpi, jpj, nlay_i+1, jpl, eicen_init )
       !
    END SUBROUTINE lim_itd_me_ridgeshift
@@ -1404,9 +1339,7 @@
       !!-------------------------------------------------------------------
       INTEGER :: ios                 ! Local integer output status for namelist read
-      NAMELIST/namiceitdme/ ridge_scheme_swi, Cs, Cf, fsnowrdg, fsnowrft,& 
-         Gstar, astar,                                &
-         Hstar, raftswi, hparmeter, Craft, ridge_por, &
-         sal_max_ridge,  partfun_swi, transfun_swi,   &
-         brinstren_swi
+      NAMELIST/namiceitdme/ ridge_scheme_swi, Cs, Cf, fsnowrdg, fsnowrft,              & 
+        &                   Gstar, astar, Hstar, raft_swi, hparmeter, Craft, ridge_por, &
+        &                   partfun_swi, brinstren_swi
       !!-------------------------------------------------------------------
       !
@@ -1418,5 +1351,5 @@
       READ  ( numnam_ice_cfg, namiceitdme, IOSTAT = ios, ERR = 902 )
 902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namiceitdme in configuration namelist', lwp )
-      WRITE ( numoni, namiceitdme )
+      IF(lwm) WRITE ( numoni, namiceitdme )
       !
       IF (lwp) THEN                          ! control print
@@ -1432,11 +1365,9 @@
          WRITE(numout,*)'   Equivalent to G* for an exponential part function       astar           ', astar
          WRITE(numout,*)'   Quantity playing a role in max ridged ice thickness     Hstar           ', Hstar
-         WRITE(numout,*)'   Rafting of ice sheets or not                            raftswi         ', raftswi
+         WRITE(numout,*)'   Rafting of ice sheets or not                            raft_swi        ', raft_swi
          WRITE(numout,*)'   Parmeter thickness (threshold between ridge-raft)       hparmeter       ', hparmeter
          WRITE(numout,*)'   Rafting hyperbolic tangent coefficient                  Craft           ', Craft  
          WRITE(numout,*)'   Initial porosity of ridges                              ridge_por       ', ridge_por
-         WRITE(numout,*)'   Maximum salinity of ridging ice                         sal_max_ridge   ', sal_max_ridge
          WRITE(numout,*)'   Switch for part. function (0) linear (1) exponential    partfun_swi     ', partfun_swi
-         WRITE(numout,*)'   Switch for tran. function (0) linear (1) exponential    transfun_swi    ', transfun_swi
          WRITE(numout,*)'   Switch for including brine volume in ice strength comp. brinstren_swi   ', brinstren_swi
       ENDIF
@@ -1462,5 +1393,5 @@
 
       REAL(wp), POINTER, DIMENSION(:,:) ::   zmask   ! 2D workspace
-      REAL(wp)                          ::   zmask_glo
+      REAL(wp)                          ::   zmask_glo, zsal, zvi, zvs, zei, zes
 !!gm      REAL(wp) ::   xtmp      ! temporary variable
       !!-------------------------------------------------------------------
@@ -1468,9 +1399,10 @@
       CALL wrk_alloc( jpi, jpj, zmask )
 
+      ! to be sure that at_i is the sum of a_i(jl)
+      at_i(:,:) = SUM( a_i(:,:,:), dim=3 )
+
       DO jl = 1, jpl
-
          !-----------------------------------------------------------------
          ! Count categories to be zapped.
-         ! Abort model in case of negative area.
          !-----------------------------------------------------------------
          icells = 0
@@ -1478,8 +1410,7 @@
          DO jj = 1, jpj
             DO ji = 1, jpi
-               IF(  ( a_i(ji,jj,jl) >= -epsi10 .AND. a_i(ji,jj,jl) <  0._wp   ) .OR.   &
-                  & ( a_i(ji,jj,jl) >  0._wp   .AND. a_i(ji,jj,jl) <= epsi10  ) .OR.   &
-                  & ( v_i(ji,jj,jl) == 0._wp   .AND. a_i(ji,jj,jl) >  0._wp   ) .OR.   &
-                  & ( v_i(ji,jj,jl) >  0._wp   .AND. v_i(ji,jj,jl) <= epsi10  ) )   zmask(ji,jj) = 1._wp
+               IF( a_i(ji,jj,jl) <= epsi10 .OR. v_i(ji,jj,jl) <= epsi10 .OR. at_i(ji,jj) <= epsi10 ) THEN
+                  zmask(ji,jj) = 1._wp
+               ENDIF
             END DO
          END DO
@@ -1494,8 +1425,9 @@
             DO jj = 1 , jpj
                DO ji = 1 , jpi
-!!gm                  xtmp = e_i(ji,jj,jk,jl) / area(ji,jj) * r1_rdtice
-!!gm                  xtmp = xtmp * unit_fac
-                  ! fheat_res(ji,jj) = fheat_res(ji,jj) - xtmp
+                  zei  = e_i(ji,jj,jk,jl)
                   e_i(ji,jj,jk,jl) = e_i(ji,jj,jk,jl) * ( 1._wp - zmask(ji,jj) )
+                  t_i(ji,jj,jk,jl) = t_i(ji,jj,jk,jl) * ( 1._wp - zmask(ji,jj) ) + rtt * zmask(ji,jj)
+                  ! update exchanges with ocean
+                  hfx_res(ji,jj)   = hfx_res(ji,jj) + ( e_i(ji,jj,jk,jl) - zei ) * unit_fac / area(ji,jj) * r1_rdtice ! W.m-2 <0
                END DO
             END DO
@@ -1504,5 +1436,9 @@
          DO jj = 1 , jpj
             DO ji = 1 , jpi
-
+               
+               zsal = smv_i(ji,jj,jl)
+               zvi  = v_i(ji,jj,jl)
+               zvs  = v_s(ji,jj,jl)
+               zes  = e_s(ji,jj,1,jl)
                !-----------------------------------------------------------------
                ! Zap snow energy and use ocean heat to melt snow
@@ -1514,9 +1450,4 @@
                ! fluxes are positive to the ocean
                ! here the flux has to be negative for the ocean
-!!gm               xtmp = ( rhosn*cpic*( rtt-t_s(ji,jj,1,jl) ) + rhosn*lfus ) * r1_rdtice
-               !           fheat_res(ji,jj) = fheat_res(ji,jj) - xtmp
-
-!!gm               xtmp = ( rhosn*cpic*( rtt-t_s(ji,jj,1,jl) ) + rhosn*lfus ) * r1_rdtice !RB   ???????
-
                t_s(ji,jj,1,jl) = rtt * zmask(ji,jj) + t_s(ji,jj,1,jl) * ( 1._wp - zmask(ji,jj) )
 
@@ -1524,13 +1455,5 @@
                ! zap ice and snow volume, add water and salt to ocean
                !-----------------------------------------------------------------
-
-               !           xtmp = (rhoi*vicen(i,j,n) + rhos*vsnon(i,j,n)) / dt
-               !           sfx_res(ji,jj)  = sfx_res(ji,jj) + ( sss_m(ji,jj)                  )   &
-               !                                            * rhosn * v_s(ji,jj,jl) * r1_rdtice
-               !           sfx_res(ji,jj)  = sfx_res(ji,jj) + ( sss_m(ji,jj) - sm_i(ji,jj,jl) )   & 
-               !                                            * rhoic * v_i(ji,jj,jl) * r1_rdtice
-               !           sfx (i,j)      = sfx (i,j)      + xtmp
-
-               ato_i(ji,jj)    = a_i  (ji,jj,jl) *       zmask(ji,jj)   + ato_i(ji,jj)
+               ato_i(ji,jj)    = a_i  (ji,jj,jl) *           zmask(ji,jj)   + ato_i(ji,jj)
                a_i  (ji,jj,jl) = a_i  (ji,jj,jl) * ( 1._wp - zmask(ji,jj) )
                v_i  (ji,jj,jl) = v_i  (ji,jj,jl) * ( 1._wp - zmask(ji,jj) )
@@ -1539,9 +1462,21 @@
                oa_i (ji,jj,jl) = oa_i (ji,jj,jl) * ( 1._wp - zmask(ji,jj) )
                smv_i(ji,jj,jl) = smv_i(ji,jj,jl) * ( 1._wp - zmask(ji,jj) )
-               !
+               e_s(ji,jj,1,jl) = e_s(ji,jj,1,jl) * ( 1._wp - zmask(ji,jj) )
+               ! additional condition
+               IF( v_s(ji,jj,jl) <= epsi10 ) THEN
+                  v_s(ji,jj,jl)   = 0._wp
+                  e_s(ji,jj,1,jl) = 0._wp
+               ENDIF
+               ! update exchanges with ocean
+               sfx_res(ji,jj)  = sfx_res(ji,jj) - ( smv_i(ji,jj,jl) - zsal ) * rhoic * r1_rdtice
+               wfx_res(ji,jj)  = wfx_res(ji,jj) - ( v_i(ji,jj,jl)   - zvi  ) * rhoic * r1_rdtice
+               wfx_snw(ji,jj)  = wfx_snw(ji,jj) - ( v_s(ji,jj,jl)   - zvs  ) * rhosn * r1_rdtice
+               hfx_res(ji,jj)  = hfx_res(ji,jj) + ( e_s(ji,jj,1,jl) - zes ) * unit_fac / area(ji,jj) * r1_rdtice ! W.m-2 <0
             END DO
          END DO
-         !
-      END DO                 ! jl 
+      END DO ! jl 
+
+      ! to be sure that at_i is the sum of a_i(jl)
+      at_i(:,:) = SUM( a_i(:,:,:), dim=3 )
       !
       CALL wrk_dealloc( jpi, jpj, zmask )
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limitd_th.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limitd_th.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limitd_th.F90	(revision 5038)
@@ -6,5 +6,5 @@
    !! History :   -   !          (W. H. Lipscomb and E.C. Hunke) CICE (c) original code
    !!            3.0  ! 2005-12  (M. Vancoppenolle) adaptation to LIM-3
-   !!             -   ! 2006-06  (M. Vancoppenolle) adaptation to include salt, age and types
+   !!             -   ! 2006-06  (M. Vancoppenolle) adaptation to include salt, age
    !!             -   ! 2007-04  (M. Vancoppenolle) Mass conservation checked
    !!----------------------------------------------------------------------
@@ -35,4 +35,5 @@
    USE lib_fortran      ! to use key_nosignedzero
    USE timing          ! Timing
+   USE limcons        ! conservation tests
 
    IMPLICIT NONE
@@ -44,7 +45,4 @@
    PUBLIC   lim_itd_fitline
    PUBLIC   lim_itd_shiftice
-
-   REAL(wp) ::   epsi10 = 1.e-10_wp   !
-   REAL(wp) ::   epsi06 = 1.e-6_wp   !
 
    !!----------------------------------------------------------------------
@@ -65,20 +63,12 @@
       INTEGER, INTENT(in) ::   kt   ! time step index
       !
-      INTEGER ::   jl, ja, jm, jbnd1, jbnd2   ! ice types    dummy loop index         
-      REAL(wp) :: zchk_v_i, zchk_smv, zchk_fs, zchk_fw, zchk_v_i_b, zchk_smv_b, zchk_fs_b, zchk_fw_b ! Check conservation (C Rousset)
-      REAL(wp) :: zchk_vmin, zchk_amin, zchk_amax ! Check errors (C Rousset)
+      INTEGER ::   ji, jj, jk, jl   ! dummy loop index         
+      !
+      REAL(wp) :: zvi_b, zsmv_b, zei_b, zfs_b, zfw_b, zft_b 
       !!------------------------------------------------------------------
       IF( nn_timing == 1 )  CALL timing_start('limitd_th')
 
-      ! -------------------------------
-      !- check conservation (C Rousset)
-      IF (ln_limdiahsb) THEN
-         zchk_v_i_b = glob_sum( SUM(   v_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) )
-         zchk_smv_b = glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) )
-         zchk_fw_b  = glob_sum( rdm_ice(:,:) * area(:,:) * tms(:,:) )
-         zchk_fs_b  = glob_sum( ( sfx_bri(:,:) + sfx_thd(:,:) + sfx_res(:,:) + sfx_mec(:,:) ) * area(:,:) * tms(:,:) )
-       ENDIF
-      !- check conservation (C Rousset)
-      ! -------------------------------
+      ! conservation test
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(0, 'limitd_th', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
 
       IF( kt == nit000 .AND. lwp ) THEN
@@ -93,9 +83,5 @@
       ! Given thermodynamic growth rates, transport ice between
       ! thickness categories.
-      DO jm = 1, jpm
-         jbnd1 = ice_cat_bounds(jm,1)
-         jbnd2 = ice_cat_bounds(jm,2)
-         IF( ice_ncat_types(jm) > 1 )   CALL lim_itd_th_rem( jbnd1, jbnd2, jm, kt )
-      END DO
+      IF( jpl > 1 )   CALL lim_itd_th_rem( 1, jpl, kt )
       !
       CALL lim_var_glo2eqv    ! only for info
@@ -105,9 +91,8 @@
       !  3) Add frazil ice growing in leads.
       !------------------------------------------------------------------------------|
-
       CALL lim_thd_lac
       CALL lim_var_glo2eqv    ! only for info
-
-     IF(ln_ctl) THEN   ! Control print
+     
+      IF(ln_ctl) THEN   ! Control print
          CALL prt_ctl_info(' ')
          CALL prt_ctl_info(' - Cell values : ')
@@ -131,37 +116,16 @@
             CALL prt_ctl(tab2d_1=sm_i  (:,:,jl)   , clinfo1= ' lim_itd_th  : sm_i     : ')
             CALL prt_ctl(tab2d_1=smv_i (:,:,jl)   , clinfo1= ' lim_itd_th  : smv_i    : ')
-            DO ja = 1, nlay_i
+            DO jk = 1, nlay_i
                CALL prt_ctl_info(' ')
-               CALL prt_ctl_info(' - Layer : ', ivar1=ja)
+               CALL prt_ctl_info(' - Layer : ', ivar1=jk)
                CALL prt_ctl_info('   ~~~~~~~')
-               CALL prt_ctl(tab2d_1=t_i(:,:,ja,jl) , clinfo1= ' lim_itd_th  : t_i      : ')
-               CALL prt_ctl(tab2d_1=e_i(:,:,ja,jl) , clinfo1= ' lim_itd_th  : e_i      : ')
+               CALL prt_ctl(tab2d_1=t_i(:,:,jk,jl) , clinfo1= ' lim_itd_th  : t_i      : ')
+               CALL prt_ctl(tab2d_1=e_i(:,:,jk,jl) , clinfo1= ' lim_itd_th  : e_i      : ')
             END DO
          END DO
       ENDIF
       !
-      ! -------------------------------
-      !- check conservation (C Rousset)
-      IF( ln_limdiahsb ) THEN
-         zchk_fs  = glob_sum( ( sfx_bri(:,:) + sfx_thd(:,:) + sfx_res(:,:) + sfx_mec(:,:) ) * area(:,:) * tms(:,:) ) - zchk_fs_b
-         zchk_fw  = glob_sum( rdm_ice(:,:) * area(:,:) * tms(:,:) ) - zchk_fw_b
- 
-         zchk_v_i = ( glob_sum( SUM(   v_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) ) - zchk_v_i_b - ( zchk_fw / rhoic ) ) * r1_rdtice
-         zchk_smv = ( glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) ) - zchk_smv_b ) * r1_rdtice + ( zchk_fs / rhoic )
-
-         zchk_vmin = glob_min(v_i)
-         zchk_amax = glob_max(SUM(a_i,dim=3))
-         zchk_amin = glob_min(a_i)
-
-         IF(lwp) THEN
-            IF ( ABS( zchk_v_i   ) >  1.e-5 ) WRITE(numout,*) 'violation volume [m3/day]     (limitd_th) = ',(zchk_v_i * rday)
-            IF ( ABS( zchk_smv   ) >  1.e-4 ) WRITE(numout,*) 'violation saline [psu*m3/day] (limitd_th) = ',(zchk_smv * rday)
-            IF ( zchk_vmin <  0.            ) WRITE(numout,*) 'violation v_i<0  [mm]         (limitd_th) = ',(zchk_vmin * 1.e-3)
-            IF ( zchk_amax >  amax+epsi10   ) WRITE(numout,*) 'violation a_i>amax            (limitd_th) = ',zchk_amax
-            IF ( zchk_amin <  0.            ) WRITE(numout,*) 'violation a_i<0               (limitd_th) = ',zchk_amin
-         ENDIF
-       ENDIF
-      !- check conservation (C Rousset)
-      ! -------------------------------
+      ! conservation test
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(1, 'limitd_th', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
       !
      IF( nn_timing == 1 )  CALL timing_stop('limitd_th')
@@ -169,5 +133,5 @@
    !
 
-   SUBROUTINE lim_itd_th_rem( klbnd, kubnd, ntyp, kt )
+   SUBROUTINE lim_itd_th_rem( klbnd, kubnd, kt )
       !!------------------------------------------------------------------
       !!                ***  ROUTINE lim_itd_th_rem ***
@@ -182,5 +146,4 @@
       INTEGER , INTENT (in) ::   klbnd   ! Start thickness category index point
       INTEGER , INTENT (in) ::   kubnd   ! End point on which the  the computation is applied
-      INTEGER , INTENT (in) ::   ntyp    ! Number of the type used
       INTEGER , INTENT (in) ::   kt      ! Ocean time step 
       !
@@ -190,5 +153,5 @@
       REAL(wp) ::   zx1, zwk1, zdh0, zetamin, zdamax   ! local scalars
       REAL(wp) ::   zx2, zwk2, zda0, zetamax           !   -      -
-      REAL(wp) ::   zx3,             zareamin, zindb   !   -      -
+      REAL(wp) ::   zx3,             zareamin          !   -      -
       CHARACTER (len = 15) :: fieldid
 
@@ -200,5 +163,5 @@
       REAL(wp), POINTER, DIMENSION(:,:,:) ::   hL          ! left boundary for the ITD for each thickness
       REAL(wp), POINTER, DIMENSION(:,:,:) ::   hR          ! left boundary for the ITD for each thickness
-      REAL(wp), POINTER, DIMENSION(:,:,:) ::   zht_i_o     ! old ice thickness
+      REAL(wp), POINTER, DIMENSION(:,:,:) ::   zht_i_b     ! old ice thickness
       REAL(wp), POINTER, DIMENSION(:,:,:) ::   dummy_es
       REAL(wp), POINTER, DIMENSION(:,:,:) ::   zdaice, zdvice          ! local increment of ice area and volume
@@ -218,5 +181,5 @@
       CALL wrk_alloc( jpi,jpj, zremap_flag )    ! integer
       CALL wrk_alloc( jpi,jpj,jpl-1, zdonor )   ! integer
-      CALL wrk_alloc( jpi,jpj,jpl, zdhice, g0, g1, hL, hR, zht_i_o, dummy_es )
+      CALL wrk_alloc( jpi,jpj,jpl, zdhice, g0, g1, hL, hR, zht_i_b, dummy_es )
       CALL wrk_alloc( jpi,jpj,jpl-1, zdaice, zdvice )   
       CALL wrk_alloc( jpi,jpj,jpl+1, zhbnew, kkstart = 0 )   
@@ -247,5 +210,4 @@
          WRITE(numout,*) ' klbnd :       ', klbnd
          WRITE(numout,*) ' kubnd :       ', kubnd
-         WRITE(numout,*) ' ntyp  :       ', ntyp 
       ENDIF
 
@@ -254,9 +216,9 @@
          DO jj = 1, jpj
             DO ji = 1, jpi
-               zindb             = 1.0 - MAX( 0.0, SIGN( 1.0, - a_i(ji,jj,jl) + epsi10 ) )     !0 if no ice and 1 if yes
-               ht_i(ji,jj,jl)    = v_i(ji,jj,jl) / MAX( a_i(ji,jj,jl), epsi10 ) * zindb
-               zindb             = 1.0 - MAX( 0.0, SIGN( 1.0, - old_a_i(ji,jj,jl) + epsi10) ) !0 if no ice and 1 if yes
-               zht_i_o(ji,jj,jl) = old_v_i(ji,jj,jl) / MAX( old_a_i(ji,jj,jl), epsi10 ) * zindb
-               IF( a_i(ji,jj,jl) > epsi06 )   zdhice(ji,jj,jl) = ht_i(ji,jj,jl) - zht_i_o(ji,jj,jl) 
+               rswitch             = 1.0 - MAX( 0.0, SIGN( 1.0, - a_i(ji,jj,jl) + epsi10 ) )     !0 if no ice and 1 if yes
+               ht_i(ji,jj,jl)    = v_i(ji,jj,jl) / MAX( a_i(ji,jj,jl), epsi10 ) * rswitch
+               rswitch             = 1.0 - MAX( 0.0, SIGN( 1.0, - a_i_b(ji,jj,jl) + epsi10) ) !0 if no ice and 1 if yes
+               zht_i_b(ji,jj,jl) = v_i_b(ji,jj,jl) / MAX( a_i_b(ji,jj,jl), epsi10 ) * rswitch
+               IF( a_i(ji,jj,jl) > epsi10 )   zdhice(ji,jj,jl) = ht_i(ji,jj,jl) - zht_i_b(ji,jj,jl) 
             END DO
          END DO
@@ -302,17 +264,13 @@
             ij = nind_j(ji)
             !
-            IF ( ( zht_i_o(ii,ij,jl) .GT. epsi10 ) .AND. & 
-               ( zht_i_o(ii,ij,jl+1) .GT. epsi10 ) ) THEN
+            zhbnew(ii,ij,jl) = hi_max(jl)
+            IF ( a_i_b(ii,ij,jl) > epsi10 .AND. a_i_b(ii,ij,jl+1) > epsi10 ) THEN
                !interpolate between adjacent category growth rates
-               zslope = ( zdhice(ii,ij,jl+1)     - zdhice(ii,ij,jl) ) / &
-                  ( zht_i_o   (ii,ij,jl+1) - zht_i_o   (ii,ij,jl) )
-               zhbnew(ii,ij,jl) = hi_max(jl) + zdhice(ii,ij,jl) + &
-                  zslope * ( hi_max(jl) - zht_i_o(ii,ij,jl) )
-            ELSEIF (zht_i_o(ii,ij,jl).gt.epsi10) THEN
+               zslope           = ( zdhice(ii,ij,jl+1) - zdhice(ii,ij,jl) ) / ( zht_i_b(ii,ij,jl+1) - zht_i_b(ii,ij,jl) )
+               zhbnew(ii,ij,jl) = hi_max(jl) + zdhice(ii,ij,jl) + zslope * ( hi_max(jl) - zht_i_b(ii,ij,jl) )
+            ELSEIF ( a_i_b(ii,ij,jl) > epsi10) THEN
                zhbnew(ii,ij,jl) = hi_max(jl) + zdhice(ii,ij,jl)
-            ELSEIF (zht_i_o(ii,ij,jl+1).gt.epsi10) THEN
+            ELSEIF ( a_i_b(ii,ij,jl+1) > epsi10) THEN
                zhbnew(ii,ij,jl) = hi_max(jl) + zdhice(ii,ij,jl+1)
-            ELSE
-               zhbnew(ii,ij,jl) = hi_max(jl)
             ENDIF
          END DO
@@ -320,30 +278,17 @@
          !- 4.2 Check that each zhbnew lies between adjacent values of ice thickness
          DO ji = 1, nbrem
-            ! jl, ji
             ii = nind_i(ji)
             ij = nind_j(ji)
-            ! jl, ji
-            IF ( ( a_i(ii,ij,jl) .GT.epsi10) .AND. & 
-               ( ht_i(ii,ij,jl).GE. zhbnew(ii,ij,jl) ) &
-               ) THEN
+            IF( a_i(ii,ij,jl) > epsi10 .AND. ht_i(ii,ij,jl) >= zhbnew(ii,ij,jl) ) THEN
                zremap_flag(ii,ij) = 0
-            ELSEIF ( ( a_i(ii,ij,jl+1) .GT. epsi10 ) .AND. &
-               ( ht_i(ii,ij,jl+1).LE. zhbnew(ii,ij,jl) ) &
-               ) THEN
+            ELSEIF( a_i(ii,ij,jl+1) > epsi10 .AND. ht_i(ii,ij,jl+1) <= zhbnew(ii,ij,jl) ) THEN
                zremap_flag(ii,ij) = 0
             ENDIF
 
             !- 4.3 Check that each zhbnew does not exceed maximal values hi_max  
-            ! jl, ji
-            IF (zhbnew(ii,ij,jl).gt.hi_max(jl+1)) THEN
-               zremap_flag(ii,ij) = 0
-            ENDIF
-            ! jl, ji
-            IF (zhbnew(ii,ij,jl).lt.hi_max(jl-1)) THEN
-               zremap_flag(ii,ij) = 0
-            ENDIF
-            ! jl, ji
-         END DO !ji
-         ! ji
+            IF( zhbnew(ii,ij,jl) > hi_max(jl+1) ) zremap_flag(ii,ij) = 0
+            IF( zhbnew(ii,ij,jl) < hi_max(jl-1) ) zremap_flag(ii,ij) = 0
+         END DO
+
       END DO !jl
 
@@ -354,11 +299,11 @@
       DO jj = 1, jpj
          DO ji = 1, jpi
-            IF ( zremap_flag(ji,jj) == 1 ) THEN
+            IF( zremap_flag(ji,jj) == 1 ) THEN
                nbrem         = nbrem + 1
                nind_i(nbrem) = ji
                nind_j(nbrem) = jj
             ENDIF
-         END DO !ji
-      END DO !jj
+         END DO 
+      END DO 
 
       !-----------------------------------------------------------------------------------------------
@@ -367,6 +312,6 @@
       DO jj = 1, jpj
          DO ji = 1, jpi
-            zhb0(ji,jj) = hi_max_typ(0,ntyp) ! 0eme
-            zhb1(ji,jj) = hi_max_typ(1,ntyp) ! 1er
+            zhb0(ji,jj) = hi_max(0) ! 0eme
+            zhb1(ji,jj) = hi_max(1) ! 1er
 
             zhbnew(ji,jj,klbnd-1) = 0._wp
@@ -380,5 +325,5 @@
             ENDIF
 
-            IF( zhbnew(ji,jj,kubnd) < hi_max(kubnd-1) )   zhbnew(ji,jj,kubnd) = hi_max(kubnd-1)
+            IF( zhbnew(ji,jj,kubnd) < hi_max(kubnd-1) ) zhbnew(ji,jj,kubnd) = hi_max(kubnd-1)
 
          END DO !jj
@@ -389,5 +334,5 @@
       !-----------------------------------------------------------------------------------------------
       !- 7.1 g(h) for category 1 at start of time step
-      CALL lim_itd_fitline( klbnd, zhb0, zhb1, zht_i_o(:,:,klbnd),         &
+      CALL lim_itd_fitline( klbnd, zhb0, zhb1, zht_i_b(:,:,klbnd),         &
          &                  g0(:,:,klbnd), g1(:,:,klbnd), hL(:,:,klbnd),   &
          &                  hR(:,:,klbnd), zremap_flag )
@@ -414,5 +359,5 @@
                   ! Constrain new thickness <= ht_i
                   zdamax = a_i(ii,ij,klbnd) * & 
-                     (1.0 - ht_i(ii,ij,klbnd)/zht_i_o(ii,ij,klbnd)) ! zdamax > 0
+                     (1.0 - ht_i(ii,ij,klbnd)/zht_i_b(ii,ij,klbnd)) ! zdamax > 0
                   !ice area lost due to melting of thin ice
                   zda0   = MIN(zda0, zdamax)
@@ -428,10 +373,7 @@
             ELSE ! if ice accretion
                ! ji, a_i > epsi10; zdh0 > 0
-               IF ( ntyp .EQ. 1 ) zhbnew(ii,ij,klbnd-1) = MIN(zdh0,hi_max(klbnd)) 
+               zhbnew(ii,ij,klbnd-1) = MIN(zdh0,hi_max(klbnd)) 
                ! zhbnew was 0, and is shifted to the right to account for thin ice
                ! growth in openwater (F0 = f1)
-               IF ( ntyp .NE. 1 ) zhbnew(ii,ij,0) = 0 
-               ! in other types there is
-               ! no open water growth (F0 = 0)
             ENDIF ! zdh0 
 
@@ -444,6 +386,5 @@
       DO jl = klbnd, kubnd
          CALL lim_itd_fitline(jl, zhbnew(:,:,jl-1), zhbnew(:,:,jl), ht_i(:,:,jl), &
-            g0(:,:,jl), g1(:,:,jl), hL(:,:,jl), hR(:,:,jl),     &
-            zremap_flag)
+            g0(:,:,jl), g1(:,:,jl), hL(:,:,jl), hR(:,:,jl), zremap_flag)
       END DO
 
@@ -493,6 +434,5 @@
             nd   = zdonor(ii,ij,jl)
             zdaice(ii,ij,jl) = g1(ii,ij,nd)*zx2 + g0(ii,ij,nd)*zx1
-            zdvice(ii,ij,jl) = g1(ii,ij,nd)*zx3 + g0(ii,ij,nd)*zx2 + &
-               zdaice(ii,ij,jl)*hL(ii,ij,nd)
+            zdvice(ii,ij,jl) = g1(ii,ij,nd)*zx3 + g0(ii,ij,nd)*zx2 + zdaice(ii,ij,jl)*hL(ii,ij,nd)
 
          END DO ! ji
@@ -511,8 +451,7 @@
          ii = nind_i(ji)
          ij = nind_j(ji)
-         IF ( ( a_i(ii,ij,1) > epsi10 ) .AND. ( ht_i(ii,ij,1) < hiclim ) ) THEN
+         IF ( a_i(ii,ij,1) > epsi10 .AND. ht_i(ii,ij,1) < hiclim ) THEN
             a_i(ii,ij,1)  = a_i(ii,ij,1) * ht_i(ii,ij,1) / hiclim 
             ht_i(ii,ij,1) = hiclim
-            v_i(ii,ij,1)  = a_i(ii,ij,1) * ht_i(ii,ij,1) !clem-useless
          ENDIF
       END DO !ji
@@ -542,5 +481,5 @@
       CALL wrk_dealloc( jpi,jpj, zremap_flag )    ! integer
       CALL wrk_dealloc( jpi,jpj,jpl-1, zdonor )   ! integer
-      CALL wrk_dealloc( jpi,jpj,jpl, zdhice, g0, g1, hL, hR, zht_i_o, dummy_es )
+      CALL wrk_dealloc( jpi,jpj,jpl, zdhice, g0, g1, hL, hR, zht_i_b, dummy_es )
       CALL wrk_dealloc( jpi,jpj,jpl-1, zdaice, zdvice )   
       CALL wrk_dealloc( jpi,jpj,jpl+1, zhbnew, kkstart = 0 )   
@@ -647,6 +586,4 @@
       REAL(wp) ::   zdo_aice           ! ice age times volume transferred
       REAL(wp) ::   zdaTsf             ! aicen*Tsfcn transferred
-      REAL(wp) ::   zindsn             ! snow or not
-      REAL(wp) ::   zindb              ! ice or not
 
       INTEGER, POINTER, DIMENSION(:) ::   nind_i, nind_j   ! compressed indices for i/j directions
@@ -775,6 +712,6 @@
 
             jl1 = zdonor(ii,ij,jl)
-            zindb             = MAX( 0.0 , SIGN( 1.0 , v_i(ii,ij,jl1) - epsi10 ) )
-            zworka(ii,ij)   = zdvice(ii,ij,jl) / MAX(v_i(ii,ij,jl1),epsi10) * zindb
+            rswitch             = MAX( 0.0 , SIGN( 1.0 , v_i(ii,ij,jl1) - epsi10 ) )
+            zworka(ii,ij)   = zdvice(ii,ij,jl) / MAX(v_i(ii,ij,jl1),epsi10) * rswitch
             IF( jl1 == jl) THEN   ;   jl2 = jl1+1
             ELSE                    ;   jl2 = jl 
@@ -799,5 +736,5 @@
             !--------------
 
-            zdvsnow          = v_s(ii,ij,jl1) * zworka(ii,ij)
+            zdvsnow        = v_s(ii,ij,jl1) * zworka(ii,ij)
             v_s(ii,ij,jl1) = v_s(ii,ij,jl1) - zdvsnow
             v_s(ii,ij,jl2) = v_s(ii,ij,jl2) + zdvsnow 
@@ -807,5 +744,5 @@
             !--------------------
 
-            zdesnow              = e_s(ii,ij,1,jl1) * zworka(ii,ij)
+            zdesnow            = e_s(ii,ij,1,jl1) * zworka(ii,ij)
             e_s(ii,ij,1,jl1)   = e_s(ii,ij,1,jl1) - zdesnow
             e_s(ii,ij,1,jl2)   = e_s(ii,ij,1,jl2) + zdesnow
@@ -815,5 +752,5 @@
             !--------------
 
-            zdo_aice             = oa_i(ii,ij,jl1) * zdaice(ii,ij,jl)
+            zdo_aice           = oa_i(ii,ij,jl1) * zdaice(ii,ij,jl)
             oa_i(ii,ij,jl1)    = oa_i(ii,ij,jl1) - zdo_aice
             oa_i(ii,ij,jl2)    = oa_i(ii,ij,jl2) + zdo_aice
@@ -823,5 +760,5 @@
             !--------------
 
-            zdsm_vice            = smv_i(ii,ij,jl1) * zworka(ii,ij)
+            zdsm_vice          = smv_i(ii,ij,jl1) * zworka(ii,ij)
             smv_i(ii,ij,jl1)   = smv_i(ii,ij,jl1) - zdsm_vice
             smv_i(ii,ij,jl2)   = smv_i(ii,ij,jl2) + zdsm_vice
@@ -831,5 +768,5 @@
             !---------------------
 
-            zdaTsf               = t_su(ii,ij,jl1) * zdaice(ii,ij,jl)
+            zdaTsf             = t_su(ii,ij,jl1) * zdaice(ii,ij,jl)
             zaTsfn(ii,ij,jl1)  = zaTsfn(ii,ij,jl1) - zdaTsf
             zaTsfn(ii,ij,jl2)  = zaTsfn(ii,ij,jl2) + zdaTsf 
@@ -872,5 +809,5 @@
                   ht_i(ji,jj,jl)  =  v_i   (ji,jj,jl) / a_i(ji,jj,jl) 
                   t_su(ji,jj,jl)  =  zaTsfn(ji,jj,jl) / a_i(ji,jj,jl) 
-                  zindsn          =  1.0 - MAX(0.0,SIGN(1.0,-v_s(ji,jj,jl)+epsi10)) !0 if no ice and 1 if yes
+                  rswitch         =  1.0 - MAX(0.0,SIGN(1.0,-v_s(ji,jj,jl)+epsi10)) !0 if no ice and 1 if yes
                ELSE
                   ht_i(ji,jj,jl)  = 0._wp
@@ -888,5 +825,5 @@
    
 
-   SUBROUTINE lim_itd_th_reb( klbnd, kubnd, ntyp )
+   SUBROUTINE lim_itd_th_reb( klbnd, kubnd )
       !!------------------------------------------------------------------
       !!                ***  ROUTINE lim_itd_th_reb ***
@@ -898,5 +835,4 @@
       INTEGER , INTENT (in) ::   klbnd   ! Start thickness category index point
       INTEGER , INTENT (in) ::   kubnd   ! End point on which the  the computation is applied
-      INTEGER , INTENT (in) ::   ntyp    ! number of the ice type involved in the rebinning process
       !
       INTEGER ::   ji,jj, jl   ! dummy loop indices
@@ -910,4 +846,5 @@
       REAL(wp), POINTER, DIMENSION(:,:) ::   vt_s_init, vt_s_final   ! snow volume summed over categories
       !!------------------------------------------------------------------
+      !! clem 2014/04: be carefull, rebining does not conserve salt(maybe?) => the difference is taken into account in limupdate
       
       CALL wrk_alloc( jpi,jpj,jpl, zdonor )   ! interger
@@ -937,12 +874,12 @@
 
       !------------------------------------------------------------------------------
-      ! 2) Make sure thickness of cat klbnd is at least hi_max_typ(klbnd)
+      ! 2) Make sure thickness of cat klbnd is at least hi_max(klbnd)
       !------------------------------------------------------------------------------
       DO jj = 1, jpj 
          DO ji = 1, jpi 
             IF( a_i(ji,jj,klbnd) > epsi10 ) THEN
-               IF( ht_i(ji,jj,klbnd) <= hi_max_typ(0,ntyp) .AND. hi_max_typ(0,ntyp) > 0._wp ) THEN
-                  a_i(ji,jj,klbnd)  = v_i(ji,jj,klbnd) / hi_max_typ(0,ntyp) 
-                  ht_i(ji,jj,klbnd) = hi_max_typ(0,ntyp)
+               IF( ht_i(ji,jj,klbnd) <= hi_max(0) .AND. hi_max(0) > 0._wp ) THEN
+                  a_i(ji,jj,klbnd)  = v_i(ji,jj,klbnd) / hi_max(0) 
+                  ht_i(ji,jj,klbnd) = hi_max(0)
                ENDIF
             ENDIF
@@ -1015,38 +952,36 @@
 
 !clem-change
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               IF( a_i(ji,jj,jl+1) > epsi10 .AND. ht_i(ji,jj,jl+1) <= hi_max(jl) ) THEN
+                  !
+                  zshiftflag = 1
+                  zdonor(ji,jj,jl) = jl + 1
+                  zdaice(ji,jj,jl) = a_i(ji,jj,jl+1) 
+                  zdvice(ji,jj,jl) = v_i(ji,jj,jl+1)
+               ENDIF
+            END DO                 ! ji
+         END DO                 ! jj
+
+         IF(lk_mpp)   CALL mpp_max( zshiftflag )
+         
+         IF( zshiftflag == 1 ) THEN            ! Shift ice between categories
+            CALL lim_itd_shiftice( klbnd, kubnd, zdonor, zdaice, zdvice )
+            ! Reset shift parameters
+            zdonor(:,:,jl) = 0
+            zdaice(:,:,jl) = 0._wp
+            zdvice(:,:,jl) = 0._wp
+         ENDIF
+!clem-change
+
+!         ! clem-change begin: why not doing that?
 !         DO jj = 1, jpj
 !            DO ji = 1, jpi
-!               IF( a_i(ji,jj,jl+1) >  epsi10 .AND.   &
-!                  ht_i(ji,jj,jl+1) <= hi_max(jl) ) THEN
-!                  !
-!                  zshiftflag = 1
-!                  zdonor(ji,jj,jl) = jl + 1
-!                  zdaice(ji,jj,jl) = a_i(ji,jj,jl+1) 
-!                  zdvice(ji,jj,jl) = v_i(ji,jj,jl+1)
+!               IF( a_i(ji,jj,jl+1) > epsi10 .AND. ht_i(ji,jj,jl+1) <= hi_max(jl) ) THEN
+!                  ht_i(ji,jj,jl+1) = hi_max(jl) + epsi10
+!                  a_i (ji,jj,jl+1) = v_i(ji,jj,jl+1) / ht_i(ji,jj,jl+1) 
 !               ENDIF
 !            END DO                 ! ji
 !         END DO                 ! jj
-!
-!         IF(lk_mpp)   CALL mpp_max( zshiftflag )
-!         
-!         IF( zshiftflag == 1 ) THEN            ! Shift ice between categories
-!            CALL lim_itd_shiftice( klbnd, kubnd, zdonor, zdaice, zdvice )
-!            ! Reset shift parameters
-!            zdonor(:,:,jl) = 0
-!            zdaice(:,:,jl) = 0._wp
-!            zdvice(:,:,jl) = 0._wp
-!         ENDIF
-!clem-change
-
-         ! clem-change begin: why not doing that?
-         DO jj = 1, jpj
-            DO ji = 1, jpi
-               IF( a_i(ji,jj,jl+1) >  epsi10 .AND.   &
-                  ht_i(ji,jj,jl+1) <= hi_max(jl) ) THEN
-                  ht_i(ji,jj,jl+1) = hi_max(jl) + epsi10
-                  a_i (ji,jj,jl+1) = v_i(ji,jj,jl+1) / ht_i(ji,jj,jl+1) 
-               ENDIF
-            END DO                 ! ji
-         END DO                 ! jj
          ! clem-change end
 
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limrhg.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limrhg.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limrhg.F90	(revision 5038)
@@ -50,8 +50,4 @@
    PUBLIC   lim_rhg        ! routine called by lim_dyn (or lim_dyn_2)
 
-   REAL(wp) ::   epsi10 = 1.e-10_wp   !
-   REAL(wp) ::   rzero   = 0._wp   ! constant values
-   REAL(wp) ::   rone    = 1._wp   ! constant values
-      
    !! * Substitutions
 #  include "vectopt_loop_substitute.h90"
@@ -121,5 +117,6 @@
       CHARACTER (len=50) ::   charout
       REAL(wp) ::   zt11, zt12, zt21, zt22, ztagnx, ztagny, delta                         !
-      REAL(wp) ::   za, zstms, zsang, zmask   ! local scalars
+      REAL(wp) ::   za, zstms, zmask   ! local scalars
+      REAL(wp) ::   zc1, zc2, zc3             ! ice mass
 
       REAL(wp) ::   dtevp              ! time step for subcycling
@@ -127,10 +124,10 @@
       REAL(wp) ::   z0, zr, zcca, zccb ! temporary scalars
       REAL(wp) ::   zu_ice2, zv_ice1   !
-      REAL(wp) ::   zddc, zdtc, zzdst   ! delta on corners and on centre
+      REAL(wp) ::   zddc, zdtc         ! delta on corners and on centre
+      REAL(wp) ::   zdst               ! shear at the center of the grid point
       REAL(wp) ::   zdsshx, zdsshy     ! term for the gradient of ocean surface
       REAL(wp) ::   sigma1, sigma2     ! internal ice stress
 
       REAL(wp) ::   zresm         ! Maximal error on ice velocity
-      REAL(wp) ::   zindb         ! ice (1) or not (0)      
       REAL(wp) ::   zdummy        ! dummy argument
       REAL(wp) ::   zintb, zintn  ! dummy argument
@@ -142,6 +139,4 @@
       REAL(wp), POINTER, DIMENSION(:,:) ::   zcorl1, zcorl2   ! coriolis parameter on U/V points
       REAL(wp), POINTER, DIMENSION(:,:) ::   za1ct , za2ct    ! temporary arrays
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zc1              ! ice mass
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zusw             ! temporary weight for ice strength computation
       REAL(wp), POINTER, DIMENSION(:,:) ::   u_oce1, v_oce1   ! ocean u/v component on U points                           
       REAL(wp), POINTER, DIMENSION(:,:) ::   u_oce2, v_oce2   ! ocean u/v component on V points
@@ -149,8 +144,6 @@
       REAL(wp), POINTER, DIMENSION(:,:) ::   zf1   , zf2      ! arrays for internal stresses
       
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zdd   , zdt      ! Divergence and tension at centre of grid cells
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zdt              ! tension at centre of grid cells
       REAL(wp), POINTER, DIMENSION(:,:) ::   zds              ! Shear on northeast corner of grid cells
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zdst             ! Shear on centre of grid cells
-      REAL(wp), POINTER, DIMENSION(:,:) ::   deltat, deltac   ! Delta at centre and corners of grid cells
       REAL(wp), POINTER, DIMENSION(:,:) ::   zs1   , zs2      ! Diagonal stress tensor components zs1 and zs2 
       REAL(wp), POINTER, DIMENSION(:,:) ::   zs12             ! Non-diagonal stress tensor component zs12
@@ -162,7 +155,7 @@
 
       CALL wrk_alloc( jpi,jpj, zpresh, zfrld1, zmass1, zcorl1, za1ct , zpreshc, zfrld2, zmass2, zcorl2, za2ct )
-      CALL wrk_alloc( jpi,jpj, zc1   , u_oce1, u_oce2, u_ice2, zusw  , v_oce1 , v_oce2, v_ice1                )
-      CALL wrk_alloc( jpi,jpj, zf1   , deltat, zu_ice, zf2   , deltac, zv_ice , zdd   , zdt    , zds  , zdst  )
-      CALL wrk_alloc( jpi,jpj, zdd   , zdt   , zds   , zs1   , zs2   , zs12   , zresr , zpice                 )
+      CALL wrk_alloc( jpi,jpj, u_oce1, u_oce2, u_ice2, v_oce1 , v_oce2, v_ice1                )
+      CALL wrk_alloc( jpi,jpj, zf1   , zu_ice, zf2   , zv_ice , zdt    , zds  )
+      CALL wrk_alloc( jpi,jpj, zdt   , zds   , zs1   , zs2   , zs12   , zresr , zpice                 )
 
 #if  defined key_lim2 && ! defined key_lim2_vp
@@ -181,12 +174,14 @@
       !
       !------------------------------------------------------------------------------!
-      ! 1) Ice-Snow mass (zc1), ice strength (zpresh)                                !
+      ! 1) Ice strength (zpresh)                                !
       !------------------------------------------------------------------------------!
       !
       ! Put every vector to 0
-      zpresh (:,:) = 0._wp   ;   zc1   (:,:) = 0._wp
+      delta_i(:,:) = 0._wp   ;
+      zpresh (:,:) = 0._wp   ;  
       zpreshc(:,:) = 0._wp
       u_ice2 (:,:) = 0._wp   ;   v_ice1(:,:) = 0._wp
-      zdd    (:,:) = 0._wp   ;   zdt   (:,:) = 0._wp   ;   zds(:,:) = 0._wp
+      divu_i (:,:) = 0._wp   ;   zdt   (:,:) = 0._wp   ;   zds(:,:) = 0._wp
+      shear_i(:,:) = 0._wp
 
 #if defined key_lim3
@@ -198,5 +193,4 @@
 !CDIR NOVERRCHK
          DO ji = 1 , jpi
-            zc1(ji,jj)    = tms(ji,jj) * ( rhosn * vt_s(ji,jj) + rhoic * vt_i(ji,jj) )
 #if defined key_lim3
             zpresh(ji,jj) = tms(ji,jj) *  strength(ji,jj)
@@ -220,10 +214,9 @@
                &              tms(ji+1,jj)   * wght(ji+1,jj+1,2,1) + &
                &              tms(ji,jj)     * wght(ji+1,jj+1,1,1)
-            zusw(ji,jj)    = 1.0 / MAX( zstms, epsd )
             zpreshc(ji,jj) = (  zpresh(ji+1,jj+1) * wght(ji+1,jj+1,2,2) + &
                &                zpresh(ji,jj+1)   * wght(ji+1,jj+1,1,2) + &
                &                zpresh(ji+1,jj)   * wght(ji+1,jj+1,2,1) + & 
                &                zpresh(ji,jj)     * wght(ji+1,jj+1,1,1)   &
-               &             ) * zusw(ji,jj)
+               &             ) / MAX( zstms, epsd )
          END DO
       END DO
@@ -267,4 +260,8 @@
          DO ji = fs_2, fs_jpim1
 
+            zc1 = tms(ji  ,jj  ) * ( rhosn * vt_s(ji  ,jj  ) + rhoic * vt_i(ji  ,jj  ) )
+            zc2 = tms(ji+1,jj  ) * ( rhosn * vt_s(ji+1,jj  ) + rhoic * vt_i(ji+1,jj  ) )
+            zc3 = tms(ji  ,jj+1) * ( rhosn * vt_s(ji  ,jj+1) + rhoic * vt_i(ji  ,jj+1) )
+
             zt11 = tms(ji  ,jj) * e1t(ji  ,jj)
             zt12 = tms(ji+1,jj) * e1t(ji+1,jj)
@@ -277,6 +274,6 @@
 
             ! Mass, coriolis coeff. and currents
-            zmass1(ji,jj) = ( zt12*zc1(ji,jj) + zt11*zc1(ji+1,jj) ) / (zt11+zt12+epsd)
-            zmass2(ji,jj) = ( zt22*zc1(ji,jj) + zt21*zc1(ji,jj+1) ) / (zt21+zt22+epsd)
+            zmass1(ji,jj) = ( zt12*zc1 + zt11*zc2 ) / (zt11+zt12+epsd)
+            zmass2(ji,jj) = ( zt22*zc1 + zt21*zc3 ) / (zt21+zt22+epsd)
             zcorl1(ji,jj) = zmass1(ji,jj) * ( e1t(ji+1,jj)*fcor(ji,jj) + e1t(ji,jj)*fcor(ji+1,jj) )   &
                &                          / ( e1t(ji,jj) + e1t(ji+1,jj) + epsd )
@@ -346,5 +343,5 @@
                !  
                !- Divergence, tension and shear (Section a. Appendix B of Hunke & Dukowicz, 2002)
-               !- zdd(:,:), zdt(:,:): divergence and tension at centre of grid cells
+               !- divu_i(:,:), zdt(:,:): divergence and tension at centre of grid cells
                !- zds(:,:): shear on northeast corner of grid cells
                !
@@ -355,5 +352,5 @@
                !                      bugs (Martin, for Miguel).
                !
-               !- ALSO: arrays zdd, zdt, zds and delta could 
+               !- ALSO: arrays zdt, zds and delta could 
                !  be removed in the future to minimise memory demand.
                !
@@ -363,10 +360,10 @@
                !
                !
-               zdd(ji,jj) = ( e2u(ji,jj)*u_ice(ji,jj)                      &
-                  &          -e2u(ji-1,jj)*u_ice(ji-1,jj)                  &
-                  &          +e1v(ji,jj)*v_ice(ji,jj)                      &
-                  &          -e1v(ji,jj-1)*v_ice(ji,jj-1)                  &
-                  &          )                                             &
-                  &         / area(ji,jj)
+               divu_i(ji,jj) = ( e2u(ji,jj)*u_ice(ji,jj)                      &
+                  &             -e2u(ji-1,jj)*u_ice(ji-1,jj)                  &
+                  &             +e1v(ji,jj)*v_ice(ji,jj)                      &
+                  &             -e1v(ji,jj-1)*v_ice(ji,jj-1)                  &
+                  &             )                                             &
+                  &            / area(ji,jj)
 
                zdt(ji,jj) = ( ( u_ice(ji,jj)/e2u(ji,jj)                    &
@@ -410,5 +407,5 @@
 
                !- Calculate Delta at centre of grid cells
-               zzdst      = (  e2u(ji  , jj) * v_ice1(ji  ,jj)          &
+               zdst      = (  e2u(ji  , jj) * v_ice1(ji  ,jj)          &
                   &          - e2u(ji-1, jj) * v_ice1(ji-1,jj)          &
                   &          + e1v(ji, jj  ) * u_ice2(ji,jj  )          &
@@ -417,26 +414,11 @@
                   &         / area(ji,jj)
 
-               delta = SQRT( zdd(ji,jj)*zdd(ji,jj) + ( zdt(ji,jj)*zdt(ji,jj) + zzdst*zzdst ) * usecc2 )  
-               ! MV rewriting
-               ! deltat(ji,jj) = MAX( SQRT(zdd(ji,jj)**2 + (zdt(ji,jj)**2 + zzdst**2)*usecc2), creepl )
-               !!gm faster to replace the line above with simply:
-               !!                deltat(ji,jj) = MAX( delta, creepl )
-               !!gm end  
-               deltat(ji,jj) = delta + creepl
-               ! END MV
+               delta = SQRT( divu_i(ji,jj)*divu_i(ji,jj) + ( zdt(ji,jj)*zdt(ji,jj) + zdst*zdst ) * usecc2 )  
+               delta_i(ji,jj) = delta + creepl
                !-Calculate stress tensor components zs1 and zs2 
                !-at centre of grid cells (see section 3.5 of CICE user's guide).
-               !zs1(ji,jj) = ( zs1(ji,jj) - dtotel*( ( 1._wp - alphaevp) * zs1(ji,jj) +   &
-               !   &          ( delta / deltat(ji,jj) - zdd(ji,jj) / deltat(ji,jj) ) * zpresh(ji,jj) ) )  &       
-               !   &          / ( 1._wp + alphaevp * dtotel )
-
-               !zs2(ji,jj) = ( zs2(ji,jj) - dtotel * ( ( 1._wp - alphaevp ) * ecc2 * zs2(ji,jj) -   &
-               !              zdt(ji,jj) / deltat(ji,jj) * zpresh(ji,jj) ) )   &
-               !   &          / ( 1._wp + alphaevp * ecc2 * dtotel )
-
-               ! new formulation from S. Bouillon to help stabilizing the code (no need of alphaevp)
-               zs1(ji,jj) = ( zs1(ji,jj) + dtotel * ( ( zdd(ji,jj) / deltat(ji,jj) - delta / deltat(ji,jj) )  &
+               zs1(ji,jj) = ( zs1(ji,jj) + dtotel * ( ( divu_i(ji,jj) / delta_i(ji,jj) - delta / delta_i(ji,jj) )  &
                   &         * zpresh(ji,jj) ) ) / ( 1._wp + dtotel )
-               zs2(ji,jj) = ( zs2(ji,jj) + dtotel * ( ecci * zdt(ji,jj) / deltat(ji,jj) * zpresh(ji,jj) ) )  &
+               zs2(ji,jj) = ( zs2(ji,jj) + dtotel * ( ecci * zdt(ji,jj) / delta_i(ji,jj) * zpresh(ji,jj) ) )  &
                   &         / ( 1._wp + dtotel )
 
@@ -470,14 +452,9 @@
                   &        / ( e1f(ji,jj) * e2f(ji,jj) )
 
-               deltac(ji,jj) = SQRT(zddc**2+(zdtc**2+zds(ji,jj)**2)*usecc2) + creepl
+               zddc = SQRT(zddc**2+(zdtc**2+zds(ji,jj)**2)*usecc2) + creepl
 
                !-Calculate stress tensor component zs12 at corners (see section 3.5 of CICE user's guide).
-               !zs12(ji,jj) = ( zs12(ji,jj) - dtotel * ( (1.0-alphaevp) * ecc2 * zs12(ji,jj) - zds(ji,jj) /  &
-               !   &          ( 2._wp * deltac(ji,jj) ) * zpreshc(ji,jj) ) )  &
-               !   &          / ( 1._wp + alphaevp * ecc2 * dtotel ) 
-
-               ! new formulation from S. Bouillon to help stabilizing the code (no need of alphaevp)
                zs12(ji,jj) = ( zs12(ji,jj) + dtotel *  &
-                  &          ( ecci * zds(ji,jj) / ( 2._wp * deltac(ji,jj) ) * zpreshc(ji,jj) ) )  &
+                  &          ( ecci * zds(ji,jj) / ( 2._wp * zddc ) * zpreshc(ji,jj) ) )  &
                   &          / ( 1.0 + dtotel ) 
 
@@ -514,6 +491,5 @@
 !CDIR NOVERRCHK
                DO ji = fs_2, fs_jpim1
-                  zmask        = (1.0-MAX(rzero,SIGN(rone,-zmass1(ji,jj))))*tmu(ji,jj)
-                  zsang        = SIGN ( 1.0 , fcor(ji,jj) ) * sangvg
+                  zmask        = (1.0-MAX(0._wp,SIGN(1._wp,-zmass1(ji,jj))))*tmu(ji,jj)
                   z0           = zmass1(ji,jj)/dtevp
 
@@ -525,7 +501,7 @@
                      (zv_ice1-v_oce1(ji,jj))**2) * (1.0-zfrld1(ji,jj))
                   zr           = z0*u_ice(ji,jj) + zf1(ji,jj) + za1ct(ji,jj) + &
-                     za*(cangvg*u_oce1(ji,jj)-zsang*v_oce1(ji,jj))
-                  zcca         = z0+za*cangvg
-                  zccb         = zcorl1(ji,jj)+za*zsang
+                     za*(u_oce1(ji,jj))
+                  zcca         = z0+za
+                  zccb         = zcorl1(ji,jj)
                   u_ice(ji,jj) = (zr+zccb*zv_ice1)/(zcca+epsd)*zmask 
 
@@ -538,5 +514,4 @@
 #endif
 #if defined key_bdy
-         ! clem: change u_ice and v_ice at the boundary for each iteration
          CALL bdy_ice_lim_dyn( 'U' )
 #endif         
@@ -547,6 +522,5 @@
                DO ji = fs_2, fs_jpim1
 
-                  zmask        = (1.0-MAX(rzero,SIGN(rone,-zmass2(ji,jj))))*tmv(ji,jj)
-                  zsang        = SIGN(1.0,fcor(ji,jj))*sangvg
+                  zmask        = (1.0-MAX(0._wp,SIGN(1._wp,-zmass2(ji,jj))))*tmv(ji,jj)
                   z0           = zmass2(ji,jj)/dtevp
                   ! SB modif because ocean has no slip boundary condition
@@ -557,7 +531,7 @@
                      (v_ice(ji,jj)-v_oce2(ji,jj))**2)*(1.0-zfrld2(ji,jj))
                   zr           = z0*v_ice(ji,jj) + zf2(ji,jj) + &
-                     za2ct(ji,jj) + za*(cangvg*v_oce2(ji,jj)+zsang*u_oce2(ji,jj))
-                  zcca         = z0+za*cangvg
-                  zccb         = zcorl2(ji,jj)+za*zsang
+                     za2ct(ji,jj) + za*(v_oce2(ji,jj))
+                  zcca         = z0+za
+                  zccb         = zcorl2(ji,jj)
                   v_ice(ji,jj) = (zr-zccb*zu_ice2)/(zcca+epsd)*zmask
 
@@ -570,5 +544,4 @@
 #endif
 #if defined key_bdy
-         ! clem: change u_ice and v_ice at the boundary for each iteration
          CALL bdy_ice_lim_dyn( 'V' )
 #endif         
@@ -579,6 +552,5 @@
 !CDIR NOVERRCHK
                DO ji = fs_2, fs_jpim1
-                  zmask        = (1.0-MAX(rzero,SIGN(rone,-zmass2(ji,jj))))*tmv(ji,jj)
-                  zsang        = SIGN(1.0,fcor(ji,jj))*sangvg
+                  zmask        = (1.0-MAX(0._wp,SIGN(1._wp,-zmass2(ji,jj))))*tmv(ji,jj)
                   z0           = zmass2(ji,jj)/dtevp
                   ! SB modif because ocean has no slip boundary condition
@@ -590,7 +562,7 @@
                      (v_ice(ji,jj)-v_oce2(ji,jj))**2)*(1.0-zfrld2(ji,jj))
                   zr           = z0*v_ice(ji,jj) + zf2(ji,jj) + &
-                     za2ct(ji,jj) + za*(cangvg*v_oce2(ji,jj)+zsang*u_oce2(ji,jj))
-                  zcca         = z0+za*cangvg
-                  zccb         = zcorl2(ji,jj)+za*zsang
+                     za2ct(ji,jj) + za*(v_oce2(ji,jj))
+                  zcca         = z0+za
+                  zccb         = zcorl2(ji,jj)
                   v_ice(ji,jj) = (zr-zccb*zu_ice2)/(zcca+epsd)*zmask
 
@@ -603,5 +575,4 @@
 #endif
 #if defined key_bdy
-         ! clem: change u_ice and v_ice at the boundary for each iteration
          CALL bdy_ice_lim_dyn( 'V' )
 #endif         
@@ -611,12 +582,6 @@
 !CDIR NOVERRCHK
                DO ji = fs_2, fs_jpim1
-                  zmask        = (1.0-MAX(rzero,SIGN(rone,-zmass1(ji,jj))))*tmu(ji,jj)
-                  zsang        = SIGN(1.0,fcor(ji,jj))*sangvg
+                  zmask        = (1.0-MAX(0._wp,SIGN(1._wp,-zmass1(ji,jj))))*tmu(ji,jj)
                   z0           = zmass1(ji,jj)/dtevp
-                  ! SB modif because ocean has no slip boundary condition
-                  ! GG Bug
-                  !                   zv_ice1       = 0.5*( (v_ice(ji,jj)+v_ice(ji,jj-1))*e1t(ji+1,jj)      &
-                  !                      &                 +(v_ice(ji+1,jj)+v_ice(ji+1,jj-1))*e1t(ji,jj))   &
-                  !                      &               /(e1t(ji+1,jj)+e1t(ji,jj)) * tmu(ji,jj)
                   zv_ice1       = 0.5*( (v_ice(ji,jj)+v_ice(ji,jj-1))*e1t(ji,jj)      &
                      &                 +(v_ice(ji+1,jj)+v_ice(ji+1,jj-1))*e1t(ji+1,jj))   &
@@ -626,7 +591,7 @@
                      (zv_ice1-v_oce1(ji,jj))**2)*(1.0-zfrld1(ji,jj))
                   zr           = z0*u_ice(ji,jj) + zf1(ji,jj) + za1ct(ji,jj) + &
-                     za*(cangvg*u_oce1(ji,jj)-zsang*v_oce1(ji,jj))
-                  zcca         = z0+za*cangvg
-                  zccb         = zcorl1(ji,jj)+za*zsang
+                     za*(u_oce1(ji,jj))
+                  zcca         = z0+za
+                  zccb         = zcorl1(ji,jj)
                   u_ice(ji,jj) = (zr+zccb*zv_ice1)/(zcca+epsd)*zmask 
                END DO ! ji
@@ -638,5 +603,4 @@
 #endif
 #if defined key_bdy
-         ! clem: change u_ice and v_ice at the boundary for each iteration
          CALL bdy_ice_lim_dyn( 'U' )
 #endif         
@@ -661,6 +625,4 @@
       ! 4) Prevent ice velocities when the ice is thin
       !------------------------------------------------------------------------------!
-      !clem : add hminrhg in the namelist
-      !
       ! If the ice thickness is below hminrhg (5cm) then ice velocity should equal the
       ! ocean velocity, 
@@ -670,6 +632,4 @@
 !CDIR NOVERRCHK
          DO ji = fs_2, fs_jpim1
-            zindb  = MAX( 0.0, SIGN( 1.0, at_i(ji,jj) - epsi10 ) ) 
-            !zdummy = zindb * vt_i(ji,jj) / MAX(at_i(ji,jj) , epsi10 )
             zdummy = vt_i(ji,jj)
             IF ( zdummy .LE. hminrhg ) THEN
@@ -687,5 +647,4 @@
 #endif
 #if defined key_bdy
-      ! clem: change u_ice and v_ice at the boundary
       CALL bdy_ice_lim_dyn( 'U' )
       CALL bdy_ice_lim_dyn( 'V' )
@@ -694,6 +653,4 @@
       DO jj = k_j1+1, k_jpj-1 
          DO ji = fs_2, fs_jpim1
-            zindb  = MAX( 0.0, SIGN( 1.0, at_i(ji,jj) - epsi10 ) ) 
-            !zdummy = zindb * vt_i(ji,jj) / MAX(at_i(ji,jj) , epsi10 )
             zdummy = vt_i(ji,jj)
             IF ( zdummy .LE. hminrhg ) THEN
@@ -717,17 +674,15 @@
 !CDIR NOVERRCHK
          DO ji = fs_2, jpim1   !RB bug no vect opt due to tmi
-            !- zdd(:,:), zdt(:,:): divergence and tension at centre 
+            !- divu_i(:,:), zdt(:,:): divergence and tension at centre 
             !- zds(:,:): shear on northeast corner of grid cells
-            zindb  = MAX( 0.0, SIGN( 1.0, at_i(ji,jj) - epsi10 ) ) 
-            !zdummy = zindb * vt_i(ji,jj) / MAX(at_i(ji,jj) , epsi10 )
             zdummy = vt_i(ji,jj)
             IF ( zdummy .LE. hminrhg ) THEN
 
-               zdd(ji,jj) = ( e2u(ji,jj)*u_ice(ji,jj)                      &
-                  &          -e2u(ji-1,jj)*u_ice(ji-1,jj)                  &
-                  &          +e1v(ji,jj)*v_ice(ji,jj)                      &
-                  &          -e1v(ji,jj-1)*v_ice(ji,jj-1)                  &
-                  &         )                                              &
-                  &         / area(ji,jj)
+               divu_i(ji,jj) = ( e2u(ji,jj)*u_ice(ji,jj)                      &
+                  &             -e2u(ji-1,jj)*u_ice(ji-1,jj)                  &
+                  &             +e1v(ji,jj)*v_ice(ji,jj)                      &
+                  &             -e1v(ji,jj-1)*v_ice(ji,jj-1)                  &
+                  &            )                                              &
+                  &            / area(ji,jj)
 
                zdt(ji,jj) = ( ( u_ice(ji,jj)/e2u(ji,jj)                    &
@@ -751,16 +706,11 @@
                   &        * tmi(ji+1,jj) * tmi(ji+1,jj+1)
 
-               zdst(ji,jj) = (  e2u( ji  , jj   ) * v_ice1(ji  ,jj  )    &
+               zdst = (  e2u( ji  , jj   ) * v_ice1(ji  ,jj  )    &
                   &           - e2u( ji-1, jj   ) * v_ice1(ji-1,jj  )    &
                   &           + e1v( ji  , jj   ) * u_ice2(ji  ,jj  )    &
                   &           - e1v( ji  , jj-1 ) * u_ice2(ji  ,jj-1)  ) / area(ji,jj)
 
-!              deltat(ji,jj) = SQRT(    zdd(ji,jj)*zdd(ji,jj)   & 
-!                  &                 + ( zdt(ji,jj)*zdt(ji,jj) + zdst(ji,jj)*zdst(ji,jj) ) * usecc2 & 
-!                  &                          ) + creepl
-               ! MV rewriting
-               delta = SQRT( zdd(ji,jj)*zdd(ji,jj) + ( zdt(ji,jj)*zdt(ji,jj) + zdst(ji,jj)*zdst(ji,jj) ) * usecc2 )  
-               deltat(ji,jj) = delta + creepl
-               ! END MV
+               delta = SQRT( divu_i(ji,jj)*divu_i(ji,jj) + ( zdt(ji,jj)*zdt(ji,jj) + zdst*zdst ) * usecc2 )  
+               delta_i(ji,jj) = delta + creepl
             
             ENDIF ! zdummy
@@ -777,12 +727,10 @@
       DO jj = k_j1+1, k_jpj-1
          DO ji = fs_2, fs_jpim1
-            divu_i (ji,jj) = zdd   (ji,jj)
-            delta_i(ji,jj) = deltat(ji,jj)
             ! begin TECLIM change 
-            zdst(ji,jj)= (  e2u( ji  , jj   ) * v_ice1(ji,jj)           &   
+            zdst= (  e2u( ji  , jj   ) * v_ice1(ji,jj)           &   
                &          - e2u( ji-1, jj   ) * v_ice1(ji-1,jj)         &   
                &          + e1v( ji  , jj   ) * u_ice2(ji,jj)           &   
                &          - e1v( ji  , jj-1 ) * u_ice2(ji,jj-1) ) / area(ji,jj) 
-            shear_i(ji,jj) = SQRT( zdt(ji,jj) * zdt(ji,jj) + zdst(ji,jj) * zdst(ji,jj) )
+            shear_i(ji,jj) = SQRT( zdt(ji,jj) * zdt(ji,jj) + zdst * zdst )
             ! end TECLIM change
          END DO
@@ -838,7 +786,7 @@
       !
       CALL wrk_dealloc( jpi,jpj, zpresh, zfrld1, zmass1, zcorl1, za1ct , zpreshc, zfrld2, zmass2, zcorl2, za2ct )
-      CALL wrk_dealloc( jpi,jpj, zc1   , u_oce1, u_oce2, u_ice2, zusw  , v_oce1 , v_oce2, v_ice1                )
-      CALL wrk_dealloc( jpi,jpj, zf1   , deltat, zu_ice, zf2   , deltac, zv_ice , zdd   , zdt    , zds  , zdst  )
-      CALL wrk_dealloc( jpi,jpj, zdd   , zdt   , zds   , zs1   , zs2   , zs12   , zresr , zpice                 )
+      CALL wrk_dealloc( jpi,jpj, u_oce1, u_oce2, u_ice2, v_oce1 , v_oce2, v_ice1                )
+      CALL wrk_dealloc( jpi,jpj, zf1   , zu_ice, zf2   , zv_ice , zdt    , zds  )
+      CALL wrk_dealloc( jpi,jpj, zdt   , zds   , zs1   , zs2   , zs12   , zresr , zpice                 )
 
    END SUBROUTINE lim_rhg
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limrst.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limrst.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limrst.F90	(revision 5038)
@@ -142,5 +142,5 @@
          CALL iom_rstput( iter, nitrst, numriw, znam , z2d )
       END DO
-
+      
       DO jl = 1, jpl 
          WRITE(zchar,'(I1)') jl
@@ -162,5 +162,4 @@
       CALL iom_rstput( iter, nitrst, numriw, 'u_ice'        , u_ice      )
       CALL iom_rstput( iter, nitrst, numriw, 'v_ice'        , v_ice      )
-      CALL iom_rstput( iter, nitrst, numriw, 'fsbbq'        , fsbbq      )
       CALL iom_rstput( iter, nitrst, numriw, 'stress1_i'    , stress1_i  )
       CALL iom_rstput( iter, nitrst, numriw, 'stress2_i'    , stress2_i  )
@@ -309,6 +308,6 @@
       INTEGER :: ji, jj, jk, jl, indx
       REAL(wp) ::   zfice, ziter
-      REAL(wp) ::   zs_inf, z_slope_s, zsmax, zsmin, zalpha, zindb   ! local scalars used for the salinity profile
-      REAL(wp), POINTER, DIMENSION(:)  ::   zs_zero 
+      REAL(wp) ::   zs_inf, z_slope_s, zsmax, zsmin, zalpha   ! local scalars used for the salinity profile
+      REAL(wp), POINTER, DIMENSION(:)   ::   zs_zero 
       REAL(wp), POINTER, DIMENSION(:,:) ::   z2d
       CHARACTER(len=15) ::   znam
@@ -393,5 +392,4 @@
       CALL iom_get( numrir, jpdom_autoglo, 'u_ice'     , u_ice      )
       CALL iom_get( numrir, jpdom_autoglo, 'v_ice'     , v_ice      )
-      CALL iom_get( numrir, jpdom_autoglo, 'fsbbq'     , fsbbq      )
       CALL iom_get( numrir, jpdom_autoglo, 'stress1_i' , stress1_i  )
       CALL iom_get( numrir, jpdom_autoglo, 'stress2_i' , stress2_i  )
@@ -522,4 +520,15 @@
       END DO
       !
+      ! clem: I do not understand why the following IF is needed
+      !       I suspect something inconsistent in the main code with option num_sal=1
+      IF( num_sal == 1 ) THEN
+         DO jl = 1, jpl 
+            sm_i(:,:,jl) = bulk_sal
+            DO jk = 1, nlay_i 
+               s_i(:,:,jk,jl) = bulk_sal
+            END DO
+         END DO
+      ENDIF
+      !
       !CALL iom_close( numrir ) !clem: closed in sbcice_lim.F90
       !
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limsbc.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limsbc.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limsbc.F90	(revision 5038)
@@ -27,12 +27,10 @@
    USE par_ice          ! ice parameters
    USE dom_oce          ! ocean domain
-   USE domvvl           ! ocean vertical scale factors
-   USE dom_ice,    ONLY : tms
+   USE dom_ice,    ONLY : tms, area
    USE ice              ! LIM sea-ice variables
    USE sbc_ice          ! Surface boundary condition: sea-ice fields
    USE sbc_oce          ! Surface boundary condition: ocean fields
    USE sbccpl
-   USE cpl_oasis3, ONLY : lk_cpl
-   USE oce       , ONLY : iatte, oatte, sshn, sshb, snwice_mass, snwice_mass_b, snwice_fmass
+   USE oce       , ONLY : fraqsr_1lev, sshn, sshb, snwice_mass, snwice_mass_b, snwice_fmass
    USE albedo           ! albedo parameters
    USE lbclnk           ! ocean lateral boundary condition - MPP exchanges
@@ -43,4 +41,6 @@
    USE lib_fortran      ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
    USE traqsr           ! clem: add penetration of solar flux into the calculation of heat budget
+   USE iom
+   USE domvvl           ! Variable volume
 
    IMPLICIT NONE
@@ -50,7 +50,4 @@
    PUBLIC   lim_sbc_flx    ! called by sbc_ice_lim
    PUBLIC   lim_sbc_tau    ! called by sbc_ice_lim
-
-   REAL(wp)  ::   rzero  = 0._wp    
-   REAL(wp)  ::   rone   = 1._wp
 
    REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:) ::   utau_oce, vtau_oce   ! air-ocean surface i- & j-stress     [N/m2]
@@ -97,107 +94,67 @@
       !!              - fr_i    : ice fraction
       !!              - tn_ice  : sea-ice surface temperature
-      !!              - alb_ice : sea-ice alberdo (lk_cpl=T)
+      !!              - alb_ice : sea-ice albedo (lk_cpl=T)
       !!
       !! References : Goosse, H. et al. 1996, Bul. Soc. Roy. Sc. Liege, 65, 87-90.
       !!              Tartinville et al. 2001 Ocean Modelling, 3, 95-108.
+      !!              These refs are now obsolete since everything has been revised
+      !!              The ref should be Rousset et al., 2015?
       !!---------------------------------------------------------------------
-      INTEGER, INTENT(in) ::   kt    ! number of iteration
-      !
-      INTEGER  ::   ji, jj, jl           ! dummy loop indices
-      INTEGER  ::   ierr, ifvt, i1mfr, idfr           ! local integer
-      INTEGER  ::   iflt, ial , iadv , ifral, ifrdv   !   -      -
-      REAL(wp) ::   zinda, zemp, zemp_snow, zfmm      ! local scalars
-      REAL(wp) ::   zemp_snw                          !   -      -
-      REAL(wp) ::   zfcm1 , zfcm2                     !   -      -
-      REAL(wp), POINTER, DIMENSION(:,:,:) ::   zalb, zalbp     ! 2D/3D workspace
-      REAL(wp) ::   zzfcm1, zfscmbq ! clem: for light penetration
+      INTEGER, INTENT(in) ::   kt                                   ! number of iteration
+      !
+      INTEGER  ::   ji, jj, jl, jk                                  ! dummy loop indices
+      !
+      REAL(wp) ::   zemp                                            !  local scalars
+      REAL(wp) ::   zf_mass                                         ! Heat flux associated with mass exchange ice->ocean (W.m-2)
+      REAL(wp) ::   zfcm1                                           ! New solar flux received by the ocean
+      !
+      REAL(wp), POINTER, DIMENSION(:,:,:) ::   zalb_cs, zalb_os     ! 2D/3D workspace
       !!---------------------------------------------------------------------
-      
-      IF( lk_cpl )   CALL wrk_alloc( jpi, jpj, jpl, zalb, zalbp )
-
-      !------------------------------------------!
-      !      heat flux at the ocean surface      !
-      !------------------------------------------!
+
+      ! make calls for heat fluxes before it is modified
+      IF( iom_use('qsr_oce') )   CALL iom_put( "qsr_oce" , qsr(:,:) * pfrld(:,:) )   !     solar flux at ocean surface
+      IF( iom_use('qns_oce') )   CALL iom_put( "qns_oce" , qns(:,:) * pfrld(:,:) )   ! non-solar flux at ocean surface
+      IF( iom_use('qsr_ice') )   CALL iom_put( "qsr_ice" , SUM( qsr_ice(:,:,:) * a_i_b(:,:,:), dim=3 ) ) !     solar flux at ice surface
+      IF( iom_use('qns_ice') )   CALL iom_put( "qns_ice" , SUM( qns_ice(:,:,:) * a_i_b(:,:,:), dim=3 ) ) ! non-solar flux at ice surface
+      IF( iom_use('qtr_ice') )   CALL iom_put( "qtr_ice" , SUM( ftr_ice(:,:,:) * a_i_b(:,:,:), dim=3 ) ) !     solar flux transmitted thru ice
+      IF( iom_use('qt_oce' ) )   CALL iom_put( "qt_oce"  , ( qsr(:,:) + qns(:,:) ) * pfrld(:,:) )  
+      IF( iom_use('qt_ice' ) )   CALL iom_put( "qt_ice"  , SUM( ( qns_ice(:,:,:) + qsr_ice(:,:,:) ) * a_i_b(:,:,:), dim=3 ) )
+
       ! pfrld is the lead fraction at the previous time step (actually between TRP and THD)
-      ! changed to old_frld and old ht_i
-
       DO jj = 1, jpj
          DO ji = 1, jpi
-            zinda   = 1.0 - MAX( rzero , SIGN( rone , - ( 1.0 - pfrld(ji,jj) ) ) )
-            ifvt    = zinda  *  MAX( rzero , SIGN( rone, - phicif(ji,jj) ) )  !subscripts are bad here
-            i1mfr   = 1.0 - MAX( rzero , SIGN( rone ,  - at_i(ji,jj) ) )
-            idfr    = 1.0 - MAX( rzero , SIGN( rone , ( 1.0 - at_i(ji,jj) ) - pfrld(ji,jj) ) )
-            iflt    = zinda  * (1 - i1mfr) * (1 - ifvt )
-            ial     = ifvt   * i1mfr + ( 1 - ifvt ) * idfr
-            iadv    = ( 1  - i1mfr ) * zinda
-            ifral   = ( 1  - i1mfr * ( 1 - ial ) )   
-            ifrdv   = ( 1  - ifral * ( 1 - ial ) ) * iadv 
-
-            ! switch --- 1.0 ---------------- 0.0 --------------------
-            ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-            ! zinda   | if pfrld = 1       | if pfrld < 1            |
-            !  -> ifvt| if pfrld old_ht_i
-            ! i1mfr   | if frld = 1        | if frld  < 1            |
-            ! idfr    | if frld <= pfrld    | if frld > pfrld        |
-            ! iflt    | 
-            ! ial     |
-            ! iadv    |
-            ! ifral
-            ! ifrdv
-
-            !   computation the solar flux at ocean surface
-            IF (lk_cpl) THEN ! be carfeful: not been tested yet
-               ! original line
-               !zfcm1 = qsr_tot(ji,jj) + fstric(ji,jj) * at_i(ji,jj)
-               ! new line to include solar penetration (not tested)
-               zfcm1 = qsr_tot(ji,jj) + fstric(ji,jj) * at_i(ji,jj) / ( 1.0 - zinda + zinda * iatte(ji,jj) )
+
+            !------------------------------------------!
+            !      heat flux at the ocean surface      !
+            !------------------------------------------!
+            ! Solar heat flux reaching the ocean = zfcm1 (W.m-2) 
+            !---------------------------------------------------
+            IF( lk_cpl ) THEN 
+               !!! LIM2 version zqsr = qsr_tot(ji,jj) + ( fstric(ji,jj) - qsr_ice(ji,jj,1) ) * ( 1.0 - pfrld(ji,jj) )
+               zfcm1 = qsr_tot(ji,jj)
                DO jl = 1, jpl
-                  zfcm1 = zfcm1 - qsr_ice(ji,jj,jl) * a_i(ji,jj,jl)
+                  zfcm1 = zfcm1 + ( ftr_ice(ji,jj,jl) - qsr_ice(ji,jj,jl) ) * a_i_b(ji,jj,jl)
                END DO
             ELSE
-               zfcm1   = pfrld(ji,jj) * qsr(ji,jj)  + &
-                    &    ( 1._wp - pfrld(ji,jj) ) * fstric(ji,jj) / ( 1.0 - zinda + zinda * iatte(ji,jj) )
+               !!! LIM2 version zqsr = pfrld(ji,jj) * qsr(ji,jj)  + ( 1.  - pfrld(ji,jj) ) * fstric(ji,jj)
+               zfcm1   = pfrld(ji,jj) * qsr(ji,jj)
+               DO jl = 1, jpl
+                  zfcm1   = zfcm1 + a_i_b(ji,jj,jl) * ftr_ice(ji,jj,jl)
+               END DO
             ENDIF
-            ! fstric     Solar flux transmitted trough the ice
-            ! qsr        Net short wave heat flux on free ocean
-            ! new line
-            fscmbq(ji,jj) = ( 1.0 - pfrld(ji,jj) ) * fstric(ji,jj) / ( 1.0 - zinda + zinda * iatte(ji,jj) )
-
-            ! solar flux and fscmbq with light penetration (clem)
-            zzfcm1  = pfrld(ji,jj) * qsr(ji,jj) * oatte(ji,jj) + ( 1. - pfrld(ji,jj) ) * fstric(ji,jj)
-            zfscmbq = ( 1.0 - pfrld(ji,jj) ) * fstric(ji,jj)
-
-            !  computation the non solar heat flux at ocean surface
-            zfcm2 = - zzfcm1                                                                    & !
-               &    + iflt    * zfscmbq                                                         & ! total ablation: heat given to the ocean
-               &    + ifral   * ( ial * qcmif(ji,jj) + (1 - ial) * qldif(ji,jj) ) * r1_rdtice   &
-               &    + ifrdv   * (       qfvbq(ji,jj) +             qdtcn(ji,jj) ) * r1_rdtice   &
-               &    + fhmec(ji,jj)                                                              & ! snow melt when ridging
-               &    + fheat_mec(ji,jj)                                                          & ! ridge formation
-               &    + fheat_res(ji,jj)                                                            ! residual heat flux
-            ! qcmif   Energy needed to bring the ocean surface layer until its freezing (ok)
-            ! qldif   heat balance of the lead (or of the open ocean)
-            ! qfvbq   latent heat uptake/release after accretion/ablation
-            ! qdtcn   Energy from the turbulent oceanic heat flux heat flux coming in the lead
-
-            IF( num_sal == 2 )   zfcm2 = zfcm2 + fhbri(ji,jj)    ! add contribution due to brine drainage 
-
-            ! bottom radiative component is sent to the computation of the oceanic heat flux
-            fsbbq(ji,jj) = ( 1._wp - ( ifvt + iflt ) ) * fscmbq(ji,jj)     
-
-            ! used to compute the oceanic heat flux at the next time step
-            qsr(ji,jj) = zfcm1                                       ! solar heat flux 
-            qns(ji,jj) = zfcm2 - fdtcn(ji,jj)                        ! non solar heat flux
-            !                           ! fdtcn : turbulent oceanic heat flux
-         END DO
-      END DO
-
-      !------------------------------------------!
-      !      mass flux at the ocean surface      !
-      !------------------------------------------!
-
-!!gm   optimisation: this loop have to be merged with the previous one
-      DO jj = 1, jpj
-         DO ji = 1, jpi
+
+            ! Total heat flux reaching the ocean = hfx_out (W.m-2) 
+            !---------------------------------------------------
+            zf_mass        = hfx_thd(ji,jj) + hfx_dyn(ji,jj) + hfx_res(ji,jj) ! heat flux from snow is 0 (T=0 degC)
+            hfx_out(ji,jj) = hfx_out(ji,jj) + zf_mass + zfcm1
+
+            ! New qsr and qns used to compute the oceanic heat flux at the next time step
+            !---------------------------------------------------
+            qsr(ji,jj) = zfcm1                                      
+            qns(ji,jj) = hfx_out(ji,jj) - zfcm1              
+
+            !------------------------------------------!
+            !      mass flux at the ocean surface      !
+            !------------------------------------------!
             !  case of realistic freshwater flux (Tartinville et al., 2001) (presently ACTIVATED)
             !  ------------------------------------------------------------------------------------- 
@@ -208,27 +165,22 @@
             !                     Even if i see Ice melting as a FW and SALT flux
             !        
-
             !  computing freshwater exchanges at the ice/ocean interface
-            IF (lk_cpl) THEN 
+            IF( lk_cpl ) THEN 
                zemp = - emp_tot(ji,jj) + emp_ice(ji,jj) * ( 1. - pfrld(ji,jj) )    &   !
-                  &   - rdm_snw(ji,jj) / rdt_ice
+                  &   + wfx_snw(ji,jj)
             ELSE
-               zemp =   emp(ji,jj)     * ( 1.0 - at_i(ji,jj)          )  &   ! evaporation over oceanic fraction
-                  &   - tprecip(ji,jj) *         at_i(ji,jj)             &   ! all precipitation reach the ocean
-                  &   + sprecip(ji,jj) * ( 1. - (pfrld(ji,jj)**betas) )  &   ! except solid precip intercepted by sea-ice
-                  &   - fmmec(ji,jj)                                         ! snow falling when ridging
+               zemp =   emp(ji,jj)     *           pfrld(ji,jj)            &   ! evaporation over oceanic fraction
+                  &   - tprecip(ji,jj) * ( 1._wp - pfrld(ji,jj) )          &   ! all precipitation reach the ocean
+                  &   + sprecip(ji,jj) * ( 1._wp - pfrld(ji,jj)**betas )       ! except solid precip intercepted by sea-ice
             ENDIF
 
-            ! mass flux at the ocean/ice interface (sea ice fraction)
-            zemp_snw = rdm_snw(ji,jj) * r1_rdtice                         ! snow melting = pure water that enters the ocean
-            zfmm     = rdm_ice(ji,jj) * r1_rdtice                         ! Freezing minus melting  
-
-            fmmflx(ji,jj) = zfmm                                     ! F/M mass flux save at least for biogeochemical model
-
-            emp(ji,jj) = zemp + zemp_snw + zfmm  ! mass flux + F/M mass flux (always ice/ocean mass exchange)
+            ! mass flux from ice/ocean
+            wfx_ice(ji,jj) = wfx_bog(ji,jj) + wfx_bom(ji,jj) + wfx_sum(ji,jj) + wfx_sni(ji,jj)   &
+                           + wfx_opw(ji,jj) + wfx_dyn(ji,jj) + wfx_res(ji,jj)
+
+            ! mass flux at the ocean/ice interface
+            fmmflx(ji,jj) = - wfx_ice(ji,jj) * r1_rdtice                    ! F/M mass flux save at least for biogeochemical model
+            emp(ji,jj)    = zemp - wfx_ice(ji,jj) - wfx_snw(ji,jj)       ! mass flux + F/M mass flux (always ice/ocean mass exchange)
             
-            !  correcting brine salt fluxes   (zinda = 1  if pfrld=1 , =0 otherwise)
-            zinda        = 1.0 - MAX( rzero , SIGN( rone , - ( 1.0 - pfrld(ji,jj) ) ) )
-            sfx_bri(ji,jj) = zinda * sfx_bri(ji,jj)
          END DO
       END DO
@@ -237,18 +189,16 @@
       !      salt flux at the ocean surface      !
       !------------------------------------------!
-
-      IF( num_sal == 2 ) THEN      ! variable ice salinity: brine drainage included in the salt flux
-         sfx(:,:) = sfx_thd(:,:) + sfx_res(:,:) + sfx_mec(:,:) + sfx_bri(:,:)
-      ELSE                         ! constant ice salinity:
-         sfx(:,:) = sfx_thd(:,:) + sfx_res(:,:) + sfx_mec(:,:)
-      ENDIF
-      !-----------------------------------------------!
-      !   mass of snow and ice per unit area          !
-      !-----------------------------------------------!
-      IF( nn_ice_embd /= 0 ) THEN                               ! embedded sea-ice (mass required)
-         snwice_mass_b(:,:) = snwice_mass(:,:)                  ! save mass from the previous ice time step
-         !                                                      ! new mass per unit area
+      sfx(:,:) = sfx_bog(:,:) + sfx_bom(:,:) + sfx_sum(:,:) + sfx_sni(:,:) + sfx_opw(:,:)   &
+         &     + sfx_res(:,:) + sfx_dyn(:,:) + sfx_bri(:,:)
+
+      !-------------------------------------------------------------!
+      !   mass of snow and ice per unit area for embedded sea-ice   !
+      !-------------------------------------------------------------!
+      IF( nn_ice_embd /= 0 ) THEN
+         ! save mass from the previous ice time step
+         snwice_mass_b(:,:) = snwice_mass(:,:)                  
+         ! new mass per unit area
          snwice_mass  (:,:) = tms(:,:) * ( rhosn * vt_s(:,:) + rhoic * vt_i(:,:)  ) 
-         !                                                      ! time evolution of snow+ice mass
+         ! time evolution of snow+ice mass
          snwice_fmass (:,:) = ( snwice_mass(:,:) - snwice_mass_b(:,:) ) * r1_rdtice
       ENDIF
@@ -261,11 +211,18 @@
 
       !------------------------------------------------!
-      !    Computation of snow/ice and ocean albedo    !
+      !    Snow/ice albedo (only if sent to coupler)   !
       !------------------------------------------------!
       IF( lk_cpl ) THEN          ! coupled case
-         CALL albedo_ice( t_su, ht_i, ht_s, zalbp, zalb )                  ! snow/ice albedo
-         !
-         alb_ice(:,:,:) =  0.5_wp * zalbp(:,:,:) + 0.5_wp * zalb (:,:,:)   ! Ice albedo (mean clear and overcast skys)
-      ENDIF
+
+            CALL wrk_alloc( jpi, jpj, jpl, zalb_cs, zalb_os )
+
+            CALL albedo_ice( t_su, ht_i, ht_s, zalb_cs, zalb_os )  ! cloud-sky and overcast-sky ice albedos
+
+            alb_ice(:,:,:) = ( 1. - cldf_ice ) * zalb_cs(:,:,:) + cldf_ice * zalb_os(:,:,:)
+
+            CALL wrk_dealloc( jpi, jpj, jpl, zalb_cs, zalb_os )
+
+      ENDIF
+
 
       IF(ln_ctl) THEN
@@ -275,7 +232,5 @@
          CALL prt_ctl( tab3d_1=tn_ice, clinfo1=' lim_sbc: tn_ice : ', kdim=jpl )
       ENDIF
-      !
-      IF( lk_cpl )   CALL wrk_dealloc( jpi, jpj, jpl, zalb, zalbp )
-      ! 
+
    END SUBROUTINE lim_sbc_flx
 
@@ -390,6 +345,5 @@
       ! clem modif
       IF( .NOT. ln_rstart ) THEN
-         iatte(:,:) = 1._wp
-         oatte(:,:) = 1._wp
+         fraqsr_1lev(:,:) = 1._wp
       ENDIF
       !
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd.F90	(revision 5038)
@@ -8,5 +8,5 @@
    !!            3.0  ! 2005-11 (M. Vancoppenolle)  LIM-3 : Multi-layer thermodynamics + salinity variations
    !!             -   ! 2007-04 (M. Vancoppenolle) add lim_thd_glohec, lim_thd_con_dh and lim_thd_con_dif
-   !!            3.2  ! 2009-07 (M. Vancoppenolle, Y. Aksenov, G. Madec) bug correction in rdm_snw
+   !!            3.2  ! 2009-07 (M. Vancoppenolle, Y. Aksenov, G. Madec) bug correction in wfx_snw
    !!            3.3  ! 2010-11 (G. Madec) corrected snow melting heat (due to factor betas)
    !!            4.0  ! 2011-02 (G. Madec) dynamical allocation
@@ -22,5 +22,5 @@
    USE phycst         ! physical constants
    USE dom_oce        ! ocean space and time domain variables
-   USE oce     , ONLY :  iatte, oatte
+   USE oce     , ONLY : fraqsr_1lev 
    USE ice            ! LIM: sea-ice variables
    USE par_ice        ! LIM: sea-ice parameters
@@ -43,4 +43,5 @@
    USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
    USE timing         ! Timing
+   USE limcons        ! conservation tests
 
    IMPLICIT NONE
@@ -49,8 +50,4 @@
    PUBLIC   lim_thd        ! called by limstp module
    PUBLIC   lim_thd_init   ! called by iceini module
-
-   REAL(wp) ::   epsi10 = 1.e-10_wp   !
-   REAL(wp) ::   zzero  = 0._wp      !
-   REAL(wp) ::   zone   = 1._wp      !
 
    !! * Substitutions
@@ -68,5 +65,5 @@
       !!                ***  ROUTINE lim_thd  ***       
       !!  
-      !! ** Purpose : This routine manages the ice thermodynamic.
+      !! ** Purpose : This routine manages ice thermodynamics
       !!         
       !! ** Action : - Initialisation of some variables
@@ -74,42 +71,39 @@
       !!               at the ice base, snow acc.,heat budget of the leads)
       !!             - selection of the icy points and put them in an array
-      !!             - call lim_vert_ther for vert ice thermodynamic
-      !!             - back to the geographic grid
-      !!             - selection of points for lateral accretion
-      !!             - call lim_lat_acc  for the ice accretion
+      !!             - call lim_thd_dif  for vertical heat diffusion
+      !!             - call lim_thd_dh   for vertical ice growth and melt
+      !!             - call lim_thd_ent  for enthalpy remapping
+      !!             - call lim_thd_sal  for ice desalination
+      !!             - call lim_thd_temp to  retrieve temperature from ice enthalpy
       !!             - back to the geographic grid
       !!     
-      !! ** References : H. Goosse et al. 1996, Bul. Soc. Roy. Sc. Liege, 65, 87-90
+      !! ** References : 
       !!---------------------------------------------------------------------
       INTEGER, INTENT(in) ::   kt    ! number of iteration
       !!
-      INTEGER  ::   ji, jj, jk, jl   ! dummy loop indices
-      INTEGER  ::   nbpb             ! nb of icy pts for thermo. cal.
-      REAL(wp) ::   zfric_umin = 5e-03_wp    ! lower bound for the friction velocity
-      REAL(wp) ::   zfric_umax = 2e-02_wp    ! upper bound for the friction velocity
-      REAL(wp) ::   zinda, zindb, zthsnice, zfric_u     ! local scalar
-      REAL(wp) ::   zfntlat, zpareff, zareamin, zcoef   !    -         -
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zqlbsbq   ! link with lead energy budget qldif
-      REAL(wp) :: zchk_v_i, zchk_smv, zchk_fs, zchk_fw, zchk_v_i_b, zchk_smv_b, zchk_fs_b, zchk_fw_b ! Check conservation (C Rousset)
-      REAL(wp) :: zchk_vmin, zchk_amin, zchk_amax ! Check errors (C Rousset)
+      INTEGER  :: ji, jj, jk, jl   ! dummy loop indices
+      INTEGER  :: nbpb             ! nb of icy pts for thermo. cal.
+      INTEGER  :: ii, ij           ! temporary dummy loop index
+      REAL(wp) :: zfric_umin = 0._wp        ! lower bound for the friction velocity (cice value=5.e-04)
+      REAL(wp) :: zch        = 0.0057_wp    ! heat transfer coefficient
+      REAL(wp) :: zareamin 
+      REAL(wp) :: zfric_u, zqld, zqfr
+      !
+      REAL(wp) :: zvi_b, zsmv_b, zei_b, zfs_b, zfw_b, zft_b 
+      !
+      REAL(wp), POINTER, DIMENSION(:,:) ::  zqsr, zqns
       !!-------------------------------------------------------------------
+      CALL wrk_alloc( jpi, jpj, zqsr, zqns )
+
       IF( nn_timing == 1 )  CALL timing_start('limthd')
 
-      CALL wrk_alloc( jpi, jpj, zqlbsbq )
-   
-      ! -------------------------------
-      !- check conservation (C Rousset)
-      IF (ln_limdiahsb) THEN
-         zchk_v_i_b = glob_sum( SUM(   v_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) )
-         zchk_smv_b = glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) )
-         zchk_fw_b  = glob_sum( rdm_ice(:,:) * area(:,:) * tms(:,:) )
-         zchk_fs_b  = glob_sum( ( sfx_bri(:,:) + sfx_thd(:,:) + sfx_res(:,:) + sfx_mec(:,:) ) * area(:,:) * tms(:,:) )
-      ENDIF
-      !- check conservation (C Rousset)
-      ! -------------------------------
-
-      !------------------------------------------------------------------------------!
-      ! 1) Initialization of diagnostic variables                                    !
-      !------------------------------------------------------------------------------!
+      ! conservation test
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(0, 'limthd', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
+
+      !------------------------------------------------------------------------!
+      ! 1) Initialization of some variables                                    !
+      !------------------------------------------------------------------------!
+      ftr_ice(:,:,:) = 0._wp  ! part of solar radiation transmitted through the ice
+
 
       !--------------------
@@ -121,10 +115,10 @@
             DO jj = 1, jpj
                DO ji = 1, jpi
+                  !0 if no ice and 1 if yes
+                  rswitch = 1.0 - MAX(  0.0 , SIGN( 1.0 , - v_i(ji,jj,jl) + epsi10 )  )
                   !Energy of melting q(S,T) [J.m-3]
-                  e_i(ji,jj,jk,jl) = e_i(ji,jj,jk,jl) / ( area(ji,jj) * MAX( v_i(ji,jj,jl) , epsi10 ) ) * REAL( nlay_i )
-                  !0 if no ice and 1 if yes
-                  zindb = 1.0 - MAX(  0.0 , SIGN( 1.0 , - v_i(ji,jj,jl) + epsi10 )  )
-                  !convert units ! very important that this line is here
-                  e_i(ji,jj,jk,jl) = e_i(ji,jj,jk,jl) * unit_fac * zindb 
+                  e_i(ji,jj,jk,jl) = rswitch * e_i(ji,jj,jk,jl) / ( area(ji,jj) * MAX( v_i(ji,jj,jl) , epsi10 ) ) * REAL( nlay_i )
+                  !convert units ! very important that this line is here        
+                  e_i(ji,jj,jk,jl) = e_i(ji,jj,jk,jl) * unit_fac 
                END DO
             END DO
@@ -133,27 +127,37 @@
             DO jj = 1, jpj
                DO ji = 1, jpi
+                  !0 if no ice and 1 if yes
+                  rswitch = 1.0 - MAX(  0.0 , SIGN( 1.0 , - v_s(ji,jj,jl) + epsi10 )  )
                   !Energy of melting q(S,T) [J.m-3]
-                  e_s(ji,jj,jk,jl) = e_s(ji,jj,jk,jl) / ( area(ji,jj) * MAX( v_s(ji,jj,jl) , epsi10 ) ) * REAL( nlay_s )
-                  !0 if no ice and 1 if yes
-                  zindb = 1.0 - MAX(  0.0 , SIGN( 1.0 , - v_s(ji,jj,jl) + epsi10 )  )
+                  e_s(ji,jj,jk,jl) = rswitch * e_s(ji,jj,jk,jl) / ( area(ji,jj) * MAX( v_s(ji,jj,jl) , epsi10 ) ) * REAL( nlay_s )
                   !convert units ! very important that this line is here
-                  e_s(ji,jj,jk,jl) = e_s(ji,jj,jk,jl) * unit_fac * zindb 
+                  e_s(ji,jj,jk,jl) = e_s(ji,jj,jk,jl) * unit_fac 
                END DO
             END DO
          END DO
       END DO
-
-      !-----------------------------------
-      ! 1.4) Compute global heat content
-      !-----------------------------------
-      qt_i_in  (:,:) = 0.e0
-      qt_s_in  (:,:) = 0.e0
-      qt_i_fin (:,:) = 0.e0
-      qt_s_fin (:,:) = 0.e0
-      sum_fluxq(:,:) = 0.e0
-      fatm     (:,:) = 0.e0
 
       ! 2) Partial computation of forcing for the thermodynamic sea ice model.      !
       !-----------------------------------------------------------------------------!
+
+      !--- Ocean solar and non solar fluxes to be used in zqld
+      IF ( .NOT. lk_cpl ) THEN   ! --- forced case, fluxes to the lead are the same as over the ocean
+         !
+         zqsr(:,:) = qsr(:,:)      ; zqns(:,:) = qns(:,:)
+         !
+      ELSE                       ! --- coupled case, fluxes to the lead are total - intercepted
+         !
+         zqsr(:,:) = qsr_tot(:,:)  ; zqns(:,:) = qns_tot(:,:)
+         !
+         DO jl = 1, jpl
+            DO jj = 1, jpj
+               DO ji = 1, jpi
+                  zqsr(ji,jj) = zqsr(ji,jj) - qsr_ice(ji,jj,jl) * a_i_b(ji,jj,jl)
+                  zqns(ji,jj) = zqns(ji,jj) - qns_ice(ji,jj,jl) * a_i_b(ji,jj,jl)
+               END DO
+            END DO
+         END DO
+         !
+      ENDIF
 
 !CDIR NOVERRCHK
@@ -161,5 +165,5 @@
 !CDIR NOVERRCHK
          DO ji = 1, jpi
-            zinda          = tms(ji,jj) * ( 1.0 - MAX( zzero , SIGN( zone , - at_i(ji,jj) + epsi10 ) ) )
+            rswitch          = tms(ji,jj) * ( 1._wp - MAX( 0._wp , SIGN( 1._wp , - at_i(ji,jj) + epsi10 ) ) ) ! 0 if no ice
             !
             !           !  solar irradiance transmission at the mixed layer bottom and used in the lead heat budget
@@ -168,39 +172,77 @@
             !           !  net downward heat flux from the ice to the ocean, expressed as a function of ocean 
             !           !  temperature and turbulent mixing (McPhee, 1992)
-            ! friction velocity
-            zfric_u        = MAX ( MIN( SQRT( ust2s(ji,jj) ) , zfric_umax ) , zfric_umin ) 
-
-            ! here the drag will depend on ice thickness and type (0.006)
-            fdtcn(ji,jj)  = zinda * rau0 * rcp * 0.006  * zfric_u * ( ( sst_m(ji,jj) + rt0 ) - t_bo(ji,jj) ) 
-            ! also category dependent
-            !           !-- Energy from the turbulent oceanic heat flux heat flux coming in the lead 
-            qdtcn(ji,jj)  = zinda * fdtcn(ji,jj) * ( 1.0 - at_i(ji,jj) ) * rdt_ice
-            !                       
-            !           !-- Lead heat budget, qldif (part 1, next one is in limthd_dh) 
-            !           !   caution: exponent betas used as more snow can fallinto leads
-            qldif(ji,jj) =  tms(ji,jj) * rdt_ice  * (                             &
-               &   pfrld(ji,jj)        * (  qsr(ji,jj) * oatte(ji,jj)             &   ! solar heat + clem modif
-               &                            + qns(ji,jj)                          &   ! non solar heat
-               &                            + fdtcn(ji,jj)                        &   ! turbulent ice-ocean heat
-               &                            + fsbbq(ji,jj) * ( 1.0 - zinda )  )   &   ! residual heat from previous step
-               & - pfrld(ji,jj)**betas * sprecip(ji,jj) * lfus                    )   ! latent heat of sprecip melting
             !
-            ! Positive heat budget is used for bottom ablation
-            zfntlat        = 1.0 - MAX( zzero , SIGN( zone ,  - qldif(ji,jj) ) )
-            != 1 if positive heat budget
-            zpareff        = 1.0 - zinda * zfntlat
-            != 0 if ice and positive heat budget and 1 if one of those two is false
-            zqlbsbq(ji,jj) = qldif(ji,jj) * ( 1.0 - zpareff ) / ( rdt_ice * MAX( at_i(ji,jj), epsi10 ) )
+
+            ! --- Energy received in the lead, zqld is defined everywhere (J.m-2) --- !
+            ! REMARK valid at least in forced mode from clem
+            ! precip is included in qns but not in qns_ice
+            IF ( lk_cpl ) THEN
+               zqld =  tms(ji,jj) * rdt_ice *  &
+                  &    (   zqsr(ji,jj) * fraqsr_1lev(ji,jj) + zqns(ji,jj)               &   ! pfrld already included in coupled mode
+                  &    + ( pfrld(ji,jj)**betas - pfrld(ji,jj) ) * sprecip(ji,jj)  *     &   ! heat content of precip
+                  &      ( cpic * ( MIN( tatm_ice(ji,jj), rt0_snow ) - rtt ) - lfus )   &
+                  &    + ( 1._wp - pfrld(ji,jj) ) * ( tprecip(ji,jj) - sprecip(ji,jj) ) * rcp * ( tatm_ice(ji,jj) - rtt ) )
+            ELSE
+               zqld =  tms(ji,jj) * rdt_ice *  &
+                  &      ( pfrld(ji,jj) * ( zqsr(ji,jj) * fraqsr_1lev(ji,jj) + zqns(ji,jj) )    &
+                  &    + ( pfrld(ji,jj)**betas - pfrld(ji,jj) ) * sprecip(ji,jj)  *             &  ! heat content of precip
+                  &      ( cpic * ( MIN( tatm_ice(ji,jj), rt0_snow ) - rtt ) - lfus )           &
+                  &    + ( 1._wp - pfrld(ji,jj) ) * ( tprecip(ji,jj) - sprecip(ji,jj) ) * rcp * ( tatm_ice(ji,jj) - rtt ) )
+            ENDIF
+
+            !-- Energy needed to bring ocean surface layer until its freezing (<0, J.m-2) --- !
+            zqfr = tms(ji,jj) * rau0 * rcp * fse3t_m(ji,jj) * ( t_bo(ji,jj) - ( sst_m(ji,jj) + rt0 ) )
+
+            !-- Energy Budget of the leads (J.m-2). Must be < 0 to form ice
+            qlead(ji,jj) = MIN( 0._wp , zqld - zqfr ) 
+
+            ! If there is ice and leads are warming, then transfer energy from the lead budget and use it for bottom melting 
+            IF( at_i(ji,jj) > epsi10 .AND. zqld > 0._wp ) THEN
+               fhld (ji,jj) = zqld * r1_rdtice / at_i(ji,jj) ! divided by at_i since this is (re)multiplied by a_i in limthd_dh.F90
+               qlead(ji,jj) = 0._wp
+            ELSE
+               fhld (ji,jj) = 0._wp
+            ENDIF
             !
-            ! Heat budget of the lead, energy transferred from ice to ocean
-            qldif  (ji,jj) = zpareff * qldif(ji,jj)
-            qdtcn  (ji,jj) = zpareff * qdtcn(ji,jj)
-            !
-            ! Energy needed to bring ocean surface layer until its freezing (qcmif, limflx)
-            qcmif  (ji,jj) =  rau0 * rcp * fse3t_m(ji,jj) * ( t_bo(ji,jj) - ( sst_m(ji,jj) + rt0 ) )
-            !
-            ! oceanic heat flux (limthd_dh)
-            fbif   (ji,jj) = zinda * (  fsbbq(ji,jj) / MAX( at_i(ji,jj) , epsi10 ) + fdtcn(ji,jj) )
-            !
+            !-- Energy from the turbulent oceanic heat flux --- !
+            !clem zfric_u        = MAX ( MIN( SQRT( ust2s(ji,jj) ) , zfric_umax ) , zfric_umin )
+            zfric_u      = MAX( SQRT( ust2s(ji,jj) ), zfric_umin ) 
+            fhtur(ji,jj) = MAX( 0._wp, rswitch * rau0 * rcp * zch  * zfric_u * ( ( sst_m(ji,jj) + rt0 ) - t_bo(ji,jj) ) ) ! W.m-2 
+            ! upper bound for fhtur: we do not want SST to drop below Tfreeze. 
+            ! So we say that the heat retrieved from the ocean (fhtur+fhld) must be < to the heat necessary to reach Tfreeze (zqfr)   
+            ! This is not a clean budget, so that should be corrected at some point
+            fhtur(ji,jj) = rswitch * MIN( fhtur(ji,jj), - fhld(ji,jj) - zqfr * r1_rdtice / MAX( at_i(ji,jj), epsi10 ) )
+
+            ! -----------------------------------------
+            ! Net heat flux on top of ice-ocean [W.m-2]
+            ! -----------------------------------------
+            !     First  step here      : heat flux at the ocean surface + precip
+            !     Second step below     : heat flux at the ice   surface (after limthd_dif) 
+            hfx_in(ji,jj) = hfx_in(ji,jj)                                                                                         & 
+               ! heat flux above the ocean
+               &    +             pfrld(ji,jj)   * ( zqns(ji,jj) + zqsr(ji,jj) )                                                  &
+               ! latent heat of precip (note that precip is included in qns but not in qns_ice)
+               &    +   ( 1._wp - pfrld(ji,jj) ) * sprecip(ji,jj) * ( cpic * ( MIN( tatm_ice(ji,jj), rt0_snow ) - rtt ) - lfus )  &
+               &    +   ( 1._wp - pfrld(ji,jj) ) * ( tprecip(ji,jj) - sprecip(ji,jj) ) * rcp * ( tatm_ice(ji,jj) - rtt )
+
+            ! -----------------------------------------------------------------------------
+            ! Net heat flux that is retroceded to the ocean or taken from the ocean [W.m-2]
+            ! -----------------------------------------------------------------------------
+            !     First  step here              :  non solar + precip - qlead - qturb
+            !     Second step in limthd_dh      :  heat remaining if total melt (zq_rema) 
+            !     Third  step in limsbc         :  heat from ice-ocean mass exchange (zf_mass) + solar
+            hfx_out(ji,jj) = hfx_out(ji,jj)                                                                                       & 
+               ! Non solar heat flux received by the ocean
+               &    +        pfrld(ji,jj) * qns(ji,jj)                                                                            &
+               ! latent heat of precip (note that precip is included in qns but not in qns_ice)
+               &    +      ( pfrld(ji,jj)**betas - pfrld(ji,jj) ) * sprecip(ji,jj)       &
+               &         * ( cpic * ( MIN( tatm_ice(ji,jj), rt0_snow ) - rtt ) - lfus )  &
+               &    +      ( 1._wp - pfrld(ji,jj) ) * ( tprecip(ji,jj) - sprecip(ji,jj) ) * rcp * ( tatm_ice(ji,jj) - rtt )       &
+               ! heat flux taken from the ocean where there is open water ice formation
+               &    -      qlead(ji,jj) * r1_rdtice                                                                               &
+               ! heat flux taken from the ocean during bottom growth/melt (fhld should be 0 while bott growth)
+               &    -      at_i(ji,jj) * fhtur(ji,jj)                                                                             &
+               &    -      at_i(ji,jj) *  fhld(ji,jj)
+
          END DO
       END DO
@@ -234,4 +276,5 @@
                DO jj = mj0(jjindx), mj1(jjindx)
                   jiindex_1d = (jj - 1) * jpi + ji
+                  WRITE(numout,*) ' lim_thd : Category no : ', jl 
                END DO
             END DO
@@ -250,19 +293,19 @@
             !-------------------------
 
-            CALL tab_2d_1d( nbpb, at_i_b     (1:nbpb), at_i            , jpi, jpj, npb(1:nbpb) )
-            CALL tab_2d_1d( nbpb, a_i_b      (1:nbpb), a_i(:,:,jl)     , jpi, jpj, npb(1:nbpb) )
-            CALL tab_2d_1d( nbpb, ht_i_b     (1:nbpb), ht_i(:,:,jl)    , jpi, jpj, npb(1:nbpb) )
-            CALL tab_2d_1d( nbpb, ht_s_b     (1:nbpb), ht_s(:,:,jl)    , jpi, jpj, npb(1:nbpb) )
-
-            CALL tab_2d_1d( nbpb, t_su_b     (1:nbpb), t_su(:,:,jl)    , jpi, jpj, npb(1:nbpb) )
-            CALL tab_2d_1d( nbpb, sm_i_b     (1:nbpb), sm_i(:,:,jl)    , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, at_i_1d     (1:nbpb), at_i            , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, a_i_1d      (1:nbpb), a_i(:,:,jl)     , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, ht_i_1d     (1:nbpb), ht_i(:,:,jl)    , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, ht_s_1d     (1:nbpb), ht_s(:,:,jl)    , jpi, jpj, npb(1:nbpb) )
+
+            CALL tab_2d_1d( nbpb, t_su_1d     (1:nbpb), t_su(:,:,jl)    , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, sm_i_1d     (1:nbpb), sm_i(:,:,jl)    , jpi, jpj, npb(1:nbpb) )
             DO jk = 1, nlay_s
-               CALL tab_2d_1d( nbpb, t_s_b(1:nbpb,jk), t_s(:,:,jk,jl)  , jpi, jpj, npb(1:nbpb) )
-               CALL tab_2d_1d( nbpb, q_s_b(1:nbpb,jk), e_s(:,:,jk,jl)  , jpi, jpj, npb(1:nbpb) )
+               CALL tab_2d_1d( nbpb, t_s_1d(1:nbpb,jk), t_s(:,:,jk,jl)  , jpi, jpj, npb(1:nbpb) )
+               CALL tab_2d_1d( nbpb, q_s_1d(1:nbpb,jk), e_s(:,:,jk,jl)  , jpi, jpj, npb(1:nbpb) )
             END DO
             DO jk = 1, nlay_i
-               CALL tab_2d_1d( nbpb, t_i_b(1:nbpb,jk), t_i(:,:,jk,jl)  , jpi, jpj, npb(1:nbpb) )
-               CALL tab_2d_1d( nbpb, q_i_b(1:nbpb,jk), e_i(:,:,jk,jl)  , jpi, jpj, npb(1:nbpb) )
-               CALL tab_2d_1d( nbpb, s_i_b(1:nbpb,jk), s_i(:,:,jk,jl)  , jpi, jpj, npb(1:nbpb) )
+               CALL tab_2d_1d( nbpb, t_i_1d(1:nbpb,jk), t_i(:,:,jk,jl)  , jpi, jpj, npb(1:nbpb) )
+               CALL tab_2d_1d( nbpb, q_i_1d(1:nbpb,jk), e_i(:,:,jk,jl)  , jpi, jpj, npb(1:nbpb) )
+               CALL tab_2d_1d( nbpb, s_i_1d(1:nbpb,jk), s_i(:,:,jk,jl)  , jpi, jpj, npb(1:nbpb) )
             END DO
 
@@ -271,58 +314,73 @@
             CALL tab_2d_1d( nbpb, fr1_i0_1d  (1:nbpb), fr1_i0          , jpi, jpj, npb(1:nbpb) )
             CALL tab_2d_1d( nbpb, fr2_i0_1d  (1:nbpb), fr2_i0          , jpi, jpj, npb(1:nbpb) )
-            CALL tab_2d_1d( nbpb, qnsr_ice_1d(1:nbpb), qns_ice(:,:,jl) , jpi, jpj, npb(1:nbpb) )
-#if ! defined key_coupled
-            CALL tab_2d_1d( nbpb, qla_ice_1d (1:nbpb), qla_ice(:,:,jl) , jpi, jpj, npb(1:nbpb) )
-            CALL tab_2d_1d( nbpb, dqla_ice_1d(1:nbpb), dqla_ice(:,:,jl), jpi, jpj, npb(1:nbpb) )
-#endif
+            CALL tab_2d_1d( nbpb, qns_ice_1d (1:nbpb), qns_ice(:,:,jl) , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, ftr_ice_1d (1:nbpb), ftr_ice(:,:,jl) , jpi, jpj, npb(1:nbpb) )
+            IF( .NOT. lk_cpl ) THEN
+               CALL tab_2d_1d( nbpb, qla_ice_1d (1:nbpb), qla_ice(:,:,jl) , jpi, jpj, npb(1:nbpb) )
+               CALL tab_2d_1d( nbpb, dqla_ice_1d(1:nbpb), dqla_ice(:,:,jl), jpi, jpj, npb(1:nbpb) )
+            ENDIF
             CALL tab_2d_1d( nbpb, dqns_ice_1d(1:nbpb), dqns_ice(:,:,jl), jpi, jpj, npb(1:nbpb) )
-            CALL tab_2d_1d( nbpb, t_bo_b     (1:nbpb), t_bo            , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, t_bo_1d     (1:nbpb), t_bo            , jpi, jpj, npb(1:nbpb) )
             CALL tab_2d_1d( nbpb, sprecip_1d (1:nbpb), sprecip         , jpi, jpj, npb(1:nbpb) ) 
-            CALL tab_2d_1d( nbpb, fbif_1d    (1:nbpb), fbif            , jpi, jpj, npb(1:nbpb) )
-            CALL tab_2d_1d( nbpb, qldif_1d   (1:nbpb), qldif           , jpi, jpj, npb(1:nbpb) )
-            CALL tab_2d_1d( nbpb, rdm_ice_1d (1:nbpb), rdm_ice         , jpi, jpj, npb(1:nbpb) )
-            CALL tab_2d_1d( nbpb, rdm_snw_1d (1:nbpb), rdm_snw         , jpi, jpj, npb(1:nbpb) )
-            CALL tab_2d_1d( nbpb, dmgwi_1d   (1:nbpb), dmgwi           , jpi, jpj, npb(1:nbpb) )
-            CALL tab_2d_1d( nbpb, qlbbq_1d   (1:nbpb), zqlbsbq         , jpi, jpj, npb(1:nbpb) )
-
-            CALL tab_2d_1d( nbpb, sfx_thd_1d (1:nbpb), sfx_thd         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, fhtur_1d   (1:nbpb), fhtur           , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, qlead_1d   (1:nbpb), qlead           , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, fhld_1d    (1:nbpb), fhld            , jpi, jpj, npb(1:nbpb) )
+
+            CALL tab_2d_1d( nbpb, wfx_snw_1d (1:nbpb), wfx_snw         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, wfx_sub_1d (1:nbpb), wfx_sub         , jpi, jpj, npb(1:nbpb) )
+
+            CALL tab_2d_1d( nbpb, wfx_bog_1d (1:nbpb), wfx_bog         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, wfx_bom_1d (1:nbpb), wfx_bom         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, wfx_sum_1d (1:nbpb), wfx_sum         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, wfx_sni_1d (1:nbpb), wfx_sni         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, wfx_res_1d (1:nbpb), wfx_res         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, wfx_spr_1d (1:nbpb), wfx_spr         , jpi, jpj, npb(1:nbpb) )
+
+            CALL tab_2d_1d( nbpb, sfx_bog_1d (1:nbpb), sfx_bog         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, sfx_bom_1d (1:nbpb), sfx_bom         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, sfx_sum_1d (1:nbpb), sfx_sum         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, sfx_sni_1d (1:nbpb), sfx_sni         , jpi, jpj, npb(1:nbpb) )
             CALL tab_2d_1d( nbpb, sfx_bri_1d (1:nbpb), sfx_bri         , jpi, jpj, npb(1:nbpb) )
-            CALL tab_2d_1d( nbpb, fhbri_1d   (1:nbpb), fhbri           , jpi, jpj, npb(1:nbpb) )
-            CALL tab_2d_1d( nbpb, fstbif_1d  (1:nbpb), fstric          , jpi, jpj, npb(1:nbpb) )
-            CALL tab_2d_1d( nbpb, qfvbq_1d   (1:nbpb), qfvbq           , jpi, jpj, npb(1:nbpb) )
-
-            CALL tab_2d_1d( nbpb, iatte_1d   (1:nbpb), iatte           , jpi, jpj, npb(1:nbpb) ) ! clem modif
-            CALL tab_2d_1d( nbpb, oatte_1d   (1:nbpb), oatte           , jpi, jpj, npb(1:nbpb) ) ! clem modif
+            CALL tab_2d_1d( nbpb, sfx_res_1d (1:nbpb), sfx_res         , jpi, jpj, npb(1:nbpb) )
+
+            CALL tab_2d_1d( nbpb, hfx_thd_1d (1:nbpb), hfx_thd         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, hfx_spr_1d (1:nbpb), hfx_spr         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, hfx_sum_1d (1:nbpb), hfx_sum         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, hfx_bom_1d (1:nbpb), hfx_bom         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, hfx_bog_1d (1:nbpb), hfx_bog         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, hfx_dif_1d (1:nbpb), hfx_dif         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, hfx_opw_1d (1:nbpb), hfx_opw         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, hfx_snw_1d (1:nbpb), hfx_snw         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, hfx_sub_1d (1:nbpb), hfx_sub         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, hfx_err_1d (1:nbpb), hfx_err         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, hfx_res_1d (1:nbpb), hfx_res         , jpi, jpj, npb(1:nbpb) )
+            CALL tab_2d_1d( nbpb, hfx_err_rem_1d (1:nbpb), hfx_err_rem , jpi, jpj, npb(1:nbpb) )
+
             !--------------------------------
             ! 4.3) Thermodynamic processes
             !--------------------------------
 
-            IF( con_i .AND. jiindex_1d > 0 )   CALL lim_thd_enmelt( 1, nbpb )   ! computes sea ice energy of melting
-            IF( con_i .AND. jiindex_1d > 0 )   CALL lim_thd_glohec( qt_i_in, qt_s_in, q_i_layer_in, 1, nbpb, jl )
-
-            !                                 !---------------------------------!
-            CALL lim_thd_dif( 1, nbpb, jl )   ! Ice/Snow Temperature profile    !
-            !                                 !---------------------------------!
-
-            CALL lim_thd_enmelt( 1, nbpb )    ! computes sea ice energy of melting compulsory for limthd_dh
-
-            IF( con_i .AND. jiindex_1d > 0 )   CALL lim_thd_glohec ( qt_i_fin, qt_s_fin, q_i_layer_fin, 1, nbpb, jl ) 
-            IF( con_i .AND. jiindex_1d > 0 )   CALL lim_thd_con_dif( 1 , nbpb , jl )
-
-            !                                 !---------------------------------!
-            CALL lim_thd_dh( 1, nbpb, jl )    ! Ice/Snow thickness              ! 
-            !                                 !---------------------------------!
-
-            !                                 !---------------------------------!
-            CALL lim_thd_ent( 1, nbpb, jl )   ! Ice/Snow enthalpy remapping     !
-            !                                 !---------------------------------!
-
-            !                                 !---------------------------------!
-            CALL lim_thd_sal( 1, nbpb )       ! Ice salinity computation        !
-            !                                 !---------------------------------!
-
-            !           CALL lim_thd_enmelt(1,nbpb)   ! computes sea ice energy of melting
-            IF( con_i .AND. jiindex_1d > 0 )   CALL lim_thd_glohec( qt_i_fin, qt_s_fin, q_i_layer_fin, 1, nbpb, jl ) 
-            IF( con_i .AND. jiindex_1d > 0 )   CALL lim_thd_con_dh ( 1 , nbpb , jl )
+            !---------------------------------!
+            ! Ice/Snow Temperature profile    !
+            !---------------------------------!
+            CALL lim_thd_dif( 1, nbpb )
+
+            !---------------------------------!
+            ! Ice/Snow thicnkess              !
+            !---------------------------------!
+            CALL lim_thd_dh( 1, nbpb )    
+
+            ! --- Ice enthalpy remapping --- !
+            CALL lim_thd_ent( 1, nbpb, q_i_1d(1:nbpb,:) ) 
+                                            
+            !---------------------------------!
+            ! --- Ice salinity --- !
+            !---------------------------------!
+            CALL lim_thd_sal( 1, nbpb )    
+
+            !---------------------------------!
+            ! --- temperature update --- !
+            !---------------------------------!
+            CALL lim_thd_temp( 1, nbpb )
 
             !--------------------------------
@@ -330,45 +388,53 @@
             !--------------------------------
 
-               CALL tab_1d_2d( nbpb, at_i          , npb, at_i_b    (1:nbpb)   , jpi, jpj )
-               CALL tab_1d_2d( nbpb, ht_i(:,:,jl)  , npb, ht_i_b    (1:nbpb)   , jpi, jpj )
-               CALL tab_1d_2d( nbpb, ht_s(:,:,jl)  , npb, ht_s_b    (1:nbpb)   , jpi, jpj )
-               CALL tab_1d_2d( nbpb, a_i (:,:,jl)  , npb, a_i_b     (1:nbpb)   , jpi, jpj )
-               CALL tab_1d_2d( nbpb, t_su(:,:,jl)  , npb, t_su_b    (1:nbpb)   , jpi, jpj )
-               CALL tab_1d_2d( nbpb, sm_i(:,:,jl)  , npb, sm_i_b    (1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, at_i          , npb, at_i_1d    (1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, ht_i(:,:,jl)  , npb, ht_i_1d    (1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, ht_s(:,:,jl)  , npb, ht_s_1d    (1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, a_i (:,:,jl)  , npb, a_i_1d     (1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, t_su(:,:,jl)  , npb, t_su_1d    (1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, sm_i(:,:,jl)  , npb, sm_i_1d    (1:nbpb)   , jpi, jpj )
             DO jk = 1, nlay_s
-               CALL tab_1d_2d( nbpb, t_s(:,:,jk,jl), npb, t_s_b     (1:nbpb,jk), jpi, jpj)
-               CALL tab_1d_2d( nbpb, e_s(:,:,jk,jl), npb, q_s_b     (1:nbpb,jk), jpi, jpj)
+               CALL tab_1d_2d( nbpb, t_s(:,:,jk,jl), npb, t_s_1d     (1:nbpb,jk), jpi, jpj)
+               CALL tab_1d_2d( nbpb, e_s(:,:,jk,jl), npb, q_s_1d     (1:nbpb,jk), jpi, jpj)
             END DO
             DO jk = 1, nlay_i
-               CALL tab_1d_2d( nbpb, t_i(:,:,jk,jl), npb, t_i_b     (1:nbpb,jk), jpi, jpj)
-               CALL tab_1d_2d( nbpb, e_i(:,:,jk,jl), npb, q_i_b     (1:nbpb,jk), jpi, jpj)
-               CALL tab_1d_2d( nbpb, s_i(:,:,jk,jl), npb, s_i_b     (1:nbpb,jk), jpi, jpj)
-            END DO
-               CALL tab_1d_2d( nbpb, fstric        , npb, fstbif_1d (1:nbpb)   , jpi, jpj )
-               CALL tab_1d_2d( nbpb, qldif         , npb, qldif_1d  (1:nbpb)   , jpi, jpj )
-               CALL tab_1d_2d( nbpb, qfvbq         , npb, qfvbq_1d  (1:nbpb)   , jpi, jpj )
-               CALL tab_1d_2d( nbpb, rdm_ice       , npb, rdm_ice_1d(1:nbpb)   , jpi, jpj )
-               CALL tab_1d_2d( nbpb, rdm_snw       , npb, rdm_snw_1d(1:nbpb)   , jpi, jpj )
-               CALL tab_1d_2d( nbpb, dmgwi         , npb, dmgwi_1d  (1:nbpb)   , jpi, jpj )
-               CALL tab_1d_2d( nbpb, rdvosif       , npb, dvsbq_1d  (1:nbpb)   , jpi, jpj )
-               CALL tab_1d_2d( nbpb, rdvobif       , npb, dvbbq_1d  (1:nbpb)   , jpi, jpj )
-               CALL tab_1d_2d( nbpb, fdvolif       , npb, dvlbq_1d  (1:nbpb)   , jpi, jpj )
-               CALL tab_1d_2d( nbpb, rdvonif       , npb, dvnbq_1d  (1:nbpb)   , jpi, jpj ) 
-               CALL tab_1d_2d( nbpb, sfx_thd       , npb, sfx_thd_1d(1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, t_i(:,:,jk,jl), npb, t_i_1d     (1:nbpb,jk), jpi, jpj)
+               CALL tab_1d_2d( nbpb, e_i(:,:,jk,jl), npb, q_i_1d     (1:nbpb,jk), jpi, jpj)
+               CALL tab_1d_2d( nbpb, s_i(:,:,jk,jl), npb, s_i_1d     (1:nbpb,jk), jpi, jpj)
+            END DO
+               CALL tab_1d_2d( nbpb, qlead         , npb, qlead_1d  (1:nbpb)   , jpi, jpj )
+
+               CALL tab_1d_2d( nbpb, wfx_snw       , npb, wfx_snw_1d(1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, wfx_sub       , npb, wfx_sub_1d(1:nbpb)   , jpi, jpj )
+
+               CALL tab_1d_2d( nbpb, wfx_bog       , npb, wfx_bog_1d(1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, wfx_bom       , npb, wfx_bom_1d(1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, wfx_sum       , npb, wfx_sum_1d(1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, wfx_sni       , npb, wfx_sni_1d(1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, wfx_res       , npb, wfx_res_1d(1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, wfx_spr       , npb, wfx_spr_1d(1:nbpb)   , jpi, jpj )
+
+               CALL tab_1d_2d( nbpb, sfx_bog       , npb, sfx_bog_1d(1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, sfx_bom       , npb, sfx_bom_1d(1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, sfx_sum       , npb, sfx_sum_1d(1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, sfx_sni       , npb, sfx_sni_1d(1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, sfx_res       , npb, sfx_res_1d(1:nbpb)   , jpi, jpj )
+               CALL tab_1d_2d( nbpb, sfx_bri       , npb, sfx_bri_1d(1:nbpb)   , jpi, jpj )
+
+              CALL tab_1d_2d( nbpb, hfx_thd       , npb, hfx_thd_1d(1:nbpb)   , jpi, jpj )
+              CALL tab_1d_2d( nbpb, hfx_spr       , npb, hfx_spr_1d(1:nbpb)   , jpi, jpj )
+              CALL tab_1d_2d( nbpb, hfx_sum       , npb, hfx_sum_1d(1:nbpb)   , jpi, jpj )
+              CALL tab_1d_2d( nbpb, hfx_bom       , npb, hfx_bom_1d(1:nbpb)   , jpi, jpj )
+              CALL tab_1d_2d( nbpb, hfx_bog       , npb, hfx_bog_1d(1:nbpb)   , jpi, jpj )
+              CALL tab_1d_2d( nbpb, hfx_dif       , npb, hfx_dif_1d(1:nbpb)   , jpi, jpj )
+              CALL tab_1d_2d( nbpb, hfx_opw       , npb, hfx_opw_1d(1:nbpb)   , jpi, jpj )
+              CALL tab_1d_2d( nbpb, hfx_snw       , npb, hfx_snw_1d(1:nbpb)   , jpi, jpj )
+              CALL tab_1d_2d( nbpb, hfx_sub       , npb, hfx_sub_1d(1:nbpb)   , jpi, jpj )
+              CALL tab_1d_2d( nbpb, hfx_err       , npb, hfx_err_1d(1:nbpb)   , jpi, jpj )
+              CALL tab_1d_2d( nbpb, hfx_res       , npb, hfx_res_1d(1:nbpb)   , jpi, jpj )
+              CALL tab_1d_2d( nbpb, hfx_err_rem   , npb, hfx_err_rem_1d(1:nbpb)   , jpi, jpj )
             !
-            IF( num_sal == 2 ) THEN
-               CALL tab_1d_2d( nbpb, sfx_bri       , npb, sfx_bri_1d(1:nbpb)   , jpi, jpj )
-               CALL tab_1d_2d( nbpb, fhbri         , npb, fhbri_1d  (1:nbpb)   , jpi, jpj )
-            ENDIF
-            !
-            !+++++       temporary stuff for a dummy version
-            CALL tab_1d_2d( nbpb, dh_i_surf2D, npb, dh_i_surf(1:nbpb)      , jpi, jpj )
-            CALL tab_1d_2d( nbpb, dh_i_bott2D, npb, dh_i_bott(1:nbpb)      , jpi, jpj )
-            CALL tab_1d_2d( nbpb, fsup2D     , npb, fsup     (1:nbpb)      , jpi, jpj )
-            CALL tab_1d_2d( nbpb, focea2D    , npb, focea    (1:nbpb)      , jpi, jpj )
-            CALL tab_1d_2d( nbpb, s_i_newice , npb, s_i_new  (1:nbpb)      , jpi, jpj )
-            CALL tab_1d_2d( nbpb, izero(:,:,jl) , npb, i0    (1:nbpb)      , jpi, jpj )
-            CALL tab_1d_2d( nbpb, qns_ice(:,:,jl), npb, qnsr_ice_1d(1:nbpb), jpi, jpj)
-            !+++++
+              CALL tab_1d_2d( nbpb, qns_ice(:,:,jl), npb, qns_ice_1d(1:nbpb) , jpi, jpj)
+              CALL tab_1d_2d( nbpb, ftr_ice(:,:,jl), npb, ftr_ice_1d(1:nbpb) , jpi, jpj )
             !
             IF( lk_mpp )   CALL mpp_comm_free( ncomm_ice ) !RB necessary ??
@@ -384,9 +450,8 @@
       ! 5.1) Ice heat content              
       !------------------------
-      ! Enthalpies are global variables we have to readjust the units (heat content in 10^9 Joules)
-      zcoef = 1._wp / ( unit_fac * REAL( nlay_i ) )
+      ! Enthalpies are global variables we have to readjust the units (heat content in Joules)
       DO jl = 1, jpl
          DO jk = 1, nlay_i
-            e_i(:,:,jk,jl) = e_i(:,:,jk,jl) * area(:,:) * a_i(:,:,jl) * ht_i(:,:,jl) * zcoef
+            e_i(:,:,jk,jl) = e_i(:,:,jk,jl) * area(:,:) * a_i(:,:,jl) * ht_i(:,:,jl) / ( unit_fac * REAL( nlay_i ) )
          END DO
       END DO
@@ -395,9 +460,8 @@
       ! 5.2) Snow heat content              
       !------------------------
-      ! Enthalpies are global variables we have to readjust the units (heat content in 10^9 Joules)
-      zcoef = 1._wp / ( unit_fac * REAL( nlay_s ) )
+      ! Enthalpies are global variables we have to readjust the units (heat content in Joules)
       DO jl = 1, jpl
          DO jk = 1, nlay_s
-            e_s(:,:,jk,jl) = e_s(:,:,jk,jl) * area(:,:) * a_i(:,:,jl) * ht_s(:,:,jl) * zcoef
+            e_s(:,:,jk,jl) = e_s(:,:,jk,jl) * area(:,:) * a_i(:,:,jl) * ht_s(:,:,jl) / ( unit_fac * REAL( nlay_s ) )
          END DO
       END DO
@@ -411,9 +475,4 @@
       ! 5.4) Diagnostic thermodynamic growth rates
       !--------------------------------------------
-!clem@useless      d_v_i_thd(:,:,:) = v_i      (:,:,:) - old_v_i(:,:,:)    ! ice volumes 
-!clem@mv-to-itd    dv_dt_thd(:,:,:) = d_v_i_thd(:,:,:) * r1_rdtice * rday
-
-      IF( con_i .AND. jiindex_1d > 0 )   fbif(:,:) = fbif(:,:) + zqlbsbq(:,:)
-
       IF(ln_ctl) THEN            ! Control print
          CALL prt_ctl_info(' ')
@@ -448,334 +507,20 @@
       ENDIF
       !
-      ! -------------------------------
-      !- check conservation (C Rousset)
-      IF (ln_limdiahsb) THEN
-         zchk_fs  = glob_sum( ( sfx_bri(:,:) + sfx_thd(:,:) + sfx_res(:,:) + sfx_mec(:,:) ) * area(:,:) * tms(:,:) ) - zchk_fs_b
-         zchk_fw  = glob_sum( rdm_ice(:,:) * area(:,:) * tms(:,:) ) - zchk_fw_b
- 
-         zchk_v_i = ( glob_sum( SUM(   v_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) ) - zchk_v_i_b - ( zchk_fw / rhoic ) ) * r1_rdtice
-         zchk_smv = ( glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) ) - zchk_smv_b ) * r1_rdtice + ( zchk_fs / rhoic )
-
-         zchk_vmin = glob_min(v_i)
-         zchk_amax = glob_max(SUM(a_i,dim=3))
-         zchk_amin = glob_min(a_i)
-       
-         IF(lwp) THEN
-            IF ( ABS( zchk_v_i   ) >  1.e-5 ) WRITE(numout,*) 'violation volume [m3/day]     (limthd) = ',(zchk_v_i * rday)
-            IF ( ABS( zchk_smv   ) >  1.e-4 ) WRITE(numout,*) 'violation saline [psu*m3/day] (limthd) = ',(zchk_smv * rday)
-            IF ( zchk_vmin <  0.            ) WRITE(numout,*) 'violation v_i<0  [mm]         (limthd) = ',(zchk_vmin * 1.e-3)
-            IF ( zchk_amax >  amax+epsi10   ) WRITE(numout,*) 'violation a_i>amax            (limthd) = ',zchk_amax
-            IF ( zchk_amin <  0.            ) WRITE(numout,*) 'violation a_i<0               (limthd) = ',zchk_amin
-         ENDIF
-      ENDIF
-      !- check conservation (C Rousset)
-      ! -------------------------------
-      !
-      CALL wrk_dealloc( jpi, jpj, zqlbsbq )
+      !
+      CALL wrk_dealloc( jpi, jpj, zqsr, zqns )
+
+      !
+      ! conservation test
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(1, 'limthd', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
       !
       IF( nn_timing == 1 )  CALL timing_stop('limthd')
-   END SUBROUTINE lim_thd
-
-
-   SUBROUTINE lim_thd_glohec( eti, ets, etilayer, kideb, kiut, jl )
+
+   END SUBROUTINE lim_thd 
+
+   SUBROUTINE lim_thd_temp( kideb, kiut )
       !!-----------------------------------------------------------------------
-      !!                   ***  ROUTINE lim_thd_glohec *** 
+      !!                   ***  ROUTINE lim_thd_temp *** 
       !!                 
-      !! ** Purpose :  Compute total heat content for each category
-      !!               Works with 1d vectors only
-      !!-----------------------------------------------------------------------
-      INTEGER , INTENT(in   )                         ::   kideb, kiut   ! bounds for the spatial loop
-      INTEGER , INTENT(in   )                         ::   jl            ! category number
-      REAL(wp), INTENT(  out), DIMENSION (jpij,jpl  ) ::   eti, ets      ! vertically-summed heat content for ice & snow
-      REAL(wp), INTENT(  out), DIMENSION (jpij,jkmax) ::   etilayer      ! heat content for ice layers
-      !!
-      INTEGER  ::   ji,jk   ! loop indices
-      !!-----------------------------------------------------------------------
-      eti(:,:) = 0._wp
-      ets(:,:) = 0._wp
-      !
-      DO jk = 1, nlay_i                ! total q over all layers, ice [J.m-2]
-         DO ji = kideb, kiut
-            etilayer(ji,jk) = q_i_b(ji,jk) * ht_i_b(ji) / REAL( nlay_i )
-            eti     (ji,jl) = eti(ji,jl) + etilayer(ji,jk) 
-         END DO
-      END DO
-      DO ji = kideb, kiut              ! total q over all layers, snow [J.m-2]
-         ets(ji,jl) = ets(ji,jl) + q_s_b(ji,1) * ht_s_b(ji) / REAL( nlay_s )
-      END DO
-      !
-      WRITE(numout,*) ' lim_thd_glohec '
-      WRITE(numout,*) ' qt_i_in : ', eti(jiindex_1d,jl) * r1_rdtice
-      WRITE(numout,*) ' qt_s_in : ', ets(jiindex_1d,jl) * r1_rdtice
-      WRITE(numout,*) ' qt_in   : ', ( eti(jiindex_1d,jl) + ets(jiindex_1d,jl) ) * r1_rdtice
-      !
-   END SUBROUTINE lim_thd_glohec
-
-
-   SUBROUTINE lim_thd_con_dif( kideb, kiut, jl )
-      !!-----------------------------------------------------------------------
-      !!                   ***  ROUTINE lim_thd_con_dif *** 
-      !!                 
-      !! ** Purpose :   Test energy conservation after heat diffusion
-      !!-------------------------------------------------------------------
-      INTEGER , INTENT(in   ) ::   kideb, kiut   ! bounds for the spatial loop
-      INTEGER , INTENT(in   ) ::   jl            ! category number
-
-      INTEGER  ::   ji, jk         ! loop indices
-      INTEGER  ::   ii, ij
-      INTEGER  ::   numce          ! number of points for which conservation is violated
-      REAL(wp) ::   meance         ! mean conservation error
-      REAL(wp) ::   max_cons_err, max_surf_err
-      !!---------------------------------------------------------------------
-
-      max_cons_err =  1.0_wp          ! maximum tolerated conservation error
-      max_surf_err =  0.001_wp        ! maximum tolerated surface error
-
-      !--------------------------
-      ! Increment of energy
-      !--------------------------
-      ! global
-      DO ji = kideb, kiut
-         dq_i(ji,jl) = qt_i_fin(ji,jl) - qt_i_in(ji,jl) + qt_s_fin(ji,jl) - qt_s_in(ji,jl)
-      END DO
-      ! layer by layer
-      dq_i_layer(:,:) = q_i_layer_fin(:,:) - q_i_layer_in(:,:)
-
-      !----------------------------------------
-      ! Atmospheric heat flux, ice heat budget
-      !----------------------------------------
-      DO ji = kideb, kiut
-         ii = MOD( npb(ji) - 1 , jpi ) + 1
-         ij =    ( npb(ji) - 1 ) / jpi + 1
-         fatm     (ji,jl) = qnsr_ice_1d(ji) + ( 1._wp - i0(ji) ) * qsr_ice_1d(ji)
-         sum_fluxq(ji,jl) = fc_su(ji) - fc_bo_i(ji) + qsr_ice_1d(ji) * i0(ji) - fstroc(ii,ij,jl)
-      END DO
-
-      !--------------------
-      ! Conservation error
-      !--------------------
-      DO ji = kideb, kiut
-         cons_error(ji,jl) = ABS( dq_i(ji,jl) * r1_rdtice + sum_fluxq(ji,jl) )
-      END DO
-
-      numce  = 0
-      meance = 0._wp
-      DO ji = kideb, kiut
-         IF ( cons_error(ji,jl) .GT. max_cons_err ) THEN
-            numce = numce + 1
-            meance = meance + cons_error(ji,jl)
-         ENDIF
-      END DO
-      IF( numce > 0 )   meance = meance / numce
-
-      WRITE(numout,*) ' Maximum tolerated conservation error : ', max_cons_err
-      WRITE(numout,*) ' After lim_thd_dif, category : ', jl
-      WRITE(numout,*) ' Mean conservation error on big error points ', meance, numit
-      WRITE(numout,*) ' Number of points where there is a cons err gt than c.e. : ', numce, numit
-
-      !-------------------------------------------------------
-      ! Surface error due to imbalance between Fatm and Fcsu
-      !-------------------------------------------------------
-      numce  = 0
-      meance = 0._wp
-
-      DO ji = kideb, kiut
-         surf_error(ji,jl) = ABS ( fatm(ji,jl) - fc_su(ji) )
-         IF( ( t_su_b(ji) .LT. rtt ) .AND. ( surf_error(ji,jl) .GT. max_surf_err ) ) THEN
-            numce = numce + 1 
-            meance = meance + surf_error(ji,jl)
-         ENDIF
-      ENDDO
-      IF( numce > 0 )   meance = meance / numce
-
-      WRITE(numout,*) ' Maximum tolerated surface error : ', max_surf_err
-      WRITE(numout,*) ' After lim_thd_dif, category : ', jl
-      WRITE(numout,*) ' Mean surface error on big error points ', meance, numit
-      WRITE(numout,*) ' Number of points where there is a surf err gt than surf_err : ', numce, numit
-
-      WRITE(numout,*) ' fc_su      : ', fc_su(jiindex_1d)
-      WRITE(numout,*) ' fatm       : ', fatm(jiindex_1d,jl)
-      WRITE(numout,*) ' t_su       : ', t_su_b(jiindex_1d)
-
-      !---------------------------------------
-      ! Write ice state in case of big errors
-      !---------------------------------------
-      DO ji = kideb, kiut
-         IF ( ( ( t_su_b(ji) .LT. rtt ) .AND. ( surf_error(ji,jl) .GT. max_surf_err ) ) .OR. &
-            ( cons_error(ji,jl) .GT. max_cons_err  ) ) THEN
-            ii                 = MOD( npb(ji) - 1, jpi ) + 1
-            ij                 = ( npb(ji) - 1 ) / jpi + 1
-            !
-            WRITE(numout,*) ' alerte 1     '
-            WRITE(numout,*) ' Untolerated conservation / surface error after '
-            WRITE(numout,*) ' heat diffusion in the ice '
-            WRITE(numout,*) ' Category   : ', jl
-            WRITE(numout,*) ' ii , ij  : ', ii, ij
-            WRITE(numout,*) ' lat, lon   : ', gphit(ii,ij), glamt(ii,ij)
-            WRITE(numout,*) ' cons_error : ', cons_error(ji,jl)
-            WRITE(numout,*) ' surf_error : ', surf_error(ji,jl)
-            WRITE(numout,*) ' dq_i       : ', - dq_i(ji,jl) * r1_rdtice
-            WRITE(numout,*) ' Fdt        : ', sum_fluxq(ji,jl)
-            WRITE(numout,*)
-            !        WRITE(numout,*) ' qt_i_in   : ', qt_i_in(ji,jl)
-            !        WRITE(numout,*) ' qt_s_in   : ', qt_s_in(ji,jl)
-            !        WRITE(numout,*) ' qt_i_fin  : ', qt_i_fin(ji,jl)
-            !        WRITE(numout,*) ' qt_s_fin  : ', qt_s_fin(ji,jl)
-            !        WRITE(numout,*) ' qt        : ', qt_i_fin(ji,jl) + qt_s_fin(ji,jl)
-            WRITE(numout,*) ' ht_i       : ', ht_i_b(ji)
-            WRITE(numout,*) ' ht_s       : ', ht_s_b(ji)
-            WRITE(numout,*) ' t_su       : ', t_su_b(ji)
-            WRITE(numout,*) ' t_s        : ', t_s_b(ji,1)
-            WRITE(numout,*) ' t_i        : ', t_i_b(ji,1:nlay_i)
-            WRITE(numout,*) ' t_bo       : ', t_bo_b(ji)
-            WRITE(numout,*) ' q_i        : ', q_i_b(ji,1:nlay_i)
-            WRITE(numout,*) ' s_i        : ', s_i_b(ji,1:nlay_i)
-            WRITE(numout,*) ' tmelts     : ', rtt - tmut*s_i_b(ji,1:nlay_i)
-            WRITE(numout,*)
-            WRITE(numout,*) ' Fluxes '
-            WRITE(numout,*) ' ~~~~~~ '
-            WRITE(numout,*) ' fatm       : ', fatm(ji,jl)
-            WRITE(numout,*) ' fc_su      : ', fc_su    (ji)
-            WRITE(numout,*) ' fstr_inice : ', qsr_ice_1d(ji)*i0(ji)
-            WRITE(numout,*) ' fc_bo      : ', - fc_bo_i  (ji)
-            WRITE(numout,*) ' foc        : ', fbif_1d(ji)
-            WRITE(numout,*) ' fstroc     : ', fstroc   (ii,ij,jl)
-            WRITE(numout,*) ' i0         : ', i0(ji)
-            WRITE(numout,*) ' qsr_ice    : ', (1.0-i0(ji))*qsr_ice_1d(ji)
-            WRITE(numout,*) ' qns_ice    : ', qnsr_ice_1d(ji)
-            WRITE(numout,*) ' Conduction fluxes : '
-            WRITE(numout,*) ' fc_s      : ', fc_s(ji,0:nlay_s)
-            WRITE(numout,*) ' fc_i      : ', fc_i(ji,0:nlay_i)
-            WRITE(numout,*)
-            WRITE(numout,*) ' Layer by layer ... '
-            WRITE(numout,*) ' dq_snow : ', ( qt_s_fin(ji,jl) - qt_s_in(ji,jl) ) * r1_rdtice
-            WRITE(numout,*) ' dfc_snow  : ', fc_s(ji,1) - fc_s(ji,0)
-            DO jk = 1, nlay_i
-               WRITE(numout,*) ' layer  : ', jk
-               WRITE(numout,*) ' dq_ice : ', dq_i_layer(ji,jk) * r1_rdtice  
-               WRITE(numout,*) ' radab  : ', radab(ji,jk)
-               WRITE(numout,*) ' dfc_i  : ', fc_i(ji,jk) - fc_i(ji,jk-1)
-               WRITE(numout,*) ' tot f  : ', fc_i(ji,jk) - fc_i(ji,jk-1) - radab(ji,jk)
-            END DO
-
-         ENDIF
-         !
-      END DO
-      !
-   END SUBROUTINE lim_thd_con_dif
-
-
-   SUBROUTINE lim_thd_con_dh( kideb, kiut, jl )
-      !!-----------------------------------------------------------------------
-      !!                   ***  ROUTINE lim_thd_con_dh  *** 
-      !!                 
-      !! ** Purpose :   Test energy conservation after enthalpy redistr.
-      !!-----------------------------------------------------------------------
-      INTEGER, INTENT(in) ::   kideb, kiut   ! bounds for the spatial loop
-      INTEGER, INTENT(in) ::   jl            ! category number
-      !
-      INTEGER  ::   ji                ! loop indices
-      INTEGER  ::   ii, ij, numce         ! local integers
-      REAL(wp) ::   meance, max_cons_err    !local scalar
-      !!---------------------------------------------------------------------
-
-      max_cons_err = 1._wp
-
-      !--------------------------
-      ! Increment of energy
-      !--------------------------
-      DO ji = kideb, kiut
-         dq_i(ji,jl) = qt_i_fin(ji,jl) - qt_i_in(ji,jl) + qt_s_fin(ji,jl) - qt_s_in(ji,jl)   ! global
-      END DO
-      dq_i_layer(:,:)    = q_i_layer_fin(:,:) - q_i_layer_in(:,:)                            ! layer by layer
-
-      !----------------------------------------
-      ! Atmospheric heat flux, ice heat budget
-      !----------------------------------------
-      DO ji = kideb, kiut
-         ii = MOD( npb(ji) - 1 , jpi ) + 1
-         ij =    ( npb(ji) - 1 ) / jpi + 1
-
-         fatm      (ji,jl) = qnsr_ice_1d(ji) + qsr_ice_1d(ji)                       ! total heat flux
-         sum_fluxq (ji,jl) = fatm(ji,jl) + fbif_1d(ji) - ftotal_fin(ji) - fstroc(ii,ij,jl) 
-         cons_error(ji,jl) = ABS( dq_i(ji,jl) * r1_rdtice + sum_fluxq(ji,jl) )
-      END DO
-
-      !--------------------
-      ! Conservation error
-      !--------------------
-      DO ji = kideb, kiut
-         cons_error(ji,jl) = ABS( dq_i(ji,jl) * r1_rdtice + sum_fluxq(ji,jl) )
-      END DO
-
-      numce = 0
-      meance = 0._wp
-      DO ji = kideb, kiut
-         IF( cons_error(ji,jl) .GT. max_cons_err ) THEN
-            numce = numce + 1
-            meance = meance + cons_error(ji,jl)
-         ENDIF
-      ENDDO
-      IF(numce > 0 ) meance = meance / numce
-
-      WRITE(numout,*) ' Error report - Category : ', jl
-      WRITE(numout,*) ' ~~~~~~~~~~~~ '
-      WRITE(numout,*) ' Maximum tolerated conservation error : ', max_cons_err
-      WRITE(numout,*) ' After lim_thd_ent, category : ', jl
-      WRITE(numout,*) ' Mean conservation error on big error points ', meance, numit
-      WRITE(numout,*) ' Number of points where there is a cons err gt than 0.1 W/m2 : ', numce, numit
-
-      !---------------------------------------
-      ! Write ice state in case of big errors
-      !---------------------------------------
-      DO ji = kideb, kiut
-         IF ( cons_error(ji,jl) .GT. max_cons_err  ) THEN
-            ii = MOD( npb(ji) - 1, jpi ) + 1
-            ij =    ( npb(ji) - 1 ) / jpi + 1
-            !
-            WRITE(numout,*) ' alerte 1 - category : ', jl
-            WRITE(numout,*) ' Untolerated conservation error after limthd_ent '
-            WRITE(numout,*) ' ii , ij  : ', ii, ij
-            WRITE(numout,*) ' lat, lon   : ', gphit(ii,ij), glamt(ii,ij)
-            WRITE(numout,*) ' * '
-            WRITE(numout,*) ' Ftotal     : ', sum_fluxq(ji,jl)
-            WRITE(numout,*) ' dq_t       : ', - dq_i(ji,jl) * r1_rdtice
-            WRITE(numout,*) ' dq_i       : ', - ( qt_i_fin(ji,jl) - qt_i_in(ji,jl) ) * r1_rdtice
-            WRITE(numout,*) ' dq_s       : ', - ( qt_s_fin(ji,jl) - qt_s_in(ji,jl) ) * r1_rdtice
-            WRITE(numout,*) ' cons_error : ', cons_error(ji,jl)
-            WRITE(numout,*) ' * '
-            WRITE(numout,*) ' Fluxes        --- : '
-            WRITE(numout,*) ' fatm       : ', fatm(ji,jl)
-            WRITE(numout,*) ' foce       : ', fbif_1d(ji)
-            WRITE(numout,*) ' fres       : ', ftotal_fin(ji)
-            WRITE(numout,*) ' fhbri      : ', fhbricat(ii,ij,jl)
-            WRITE(numout,*) ' * '
-            WRITE(numout,*) ' Heat contents --- : '
-            WRITE(numout,*) ' qt_s_in    : ', qt_s_in(ji,jl) * r1_rdtice
-            WRITE(numout,*) ' qt_i_in    : ', qt_i_in(ji,jl) * r1_rdtice
-            WRITE(numout,*) ' qt_in      : ', ( qt_i_in(ji,jl) + qt_s_in(ji,jl) ) * r1_rdtice
-            WRITE(numout,*) ' qt_s_fin   : ', qt_s_fin(ji,jl) * r1_rdtice
-            WRITE(numout,*) ' qt_i_fin   : ', qt_i_fin(ji,jl) * r1_rdtice
-            WRITE(numout,*) ' qt_fin     : ', ( qt_i_fin(ji,jl) + qt_s_fin(ji,jl) ) * r1_rdtice
-            WRITE(numout,*) ' * '
-            WRITE(numout,*) ' Ice variables --- : '
-            WRITE(numout,*) ' ht_i       : ', ht_i_b(ji)
-            WRITE(numout,*) ' ht_s       : ', ht_s_b(ji)
-            WRITE(numout,*) ' dh_s_tot  : ', dh_s_tot(ji)
-            WRITE(numout,*) ' dh_snowice: ', dh_snowice(ji)
-            WRITE(numout,*) ' dh_i_surf : ', dh_i_surf(ji)
-            WRITE(numout,*) ' dh_i_bott : ', dh_i_bott(ji)
-         ENDIF
-         !
-      END DO
-      !
-   END SUBROUTINE lim_thd_con_dh
-
-
-   SUBROUTINE lim_thd_enmelt( kideb, kiut )
-      !!-----------------------------------------------------------------------
-      !!                   ***  ROUTINE lim_thd_enmelt *** 
-      !!                 
-      !! ** Purpose :   Computes sea ice energy of melting q_i (J.m-3)
+      !! ** Purpose :   Computes sea ice temperature (Kelvin) from enthalpy
       !!
       !! ** Method  :   Formula (Bitz and Lipscomb, 1999)
@@ -784,23 +529,24 @@
       !!
       INTEGER  ::   ji, jk   ! dummy loop indices
-      REAL(wp) ::   ztmelts  ! local scalar 
+      REAL(wp) ::   ztmelts, zaaa, zbbb, zccc, zdiscrim  ! local scalar 
       !!-------------------------------------------------------------------
-      !
-      DO jk = 1, nlay_i             ! Sea ice energy of melting
+      ! Recover ice temperature
+      DO jk = 1, nlay_i
          DO ji = kideb, kiut
-            ztmelts      =  - tmut  * s_i_b(ji,jk) + rtt 
-            q_i_b(ji,jk) =    rhoic * ( cpic * ( ztmelts - t_i_b(ji,jk) )                                 &
-               &                      + lfus * ( 1.0 - (ztmelts-rtt) / MIN( t_i_b(ji,jk)-rtt, -epsi10 ) )   &
-               &                      - rcp  * ( ztmelts-rtt  )  ) 
-         END DO
-      END DO
-      DO jk = 1, nlay_s             ! Snow energy of melting
-         DO ji = kideb, kiut
-            q_s_b(ji,jk) = rhosn * ( cpic * ( rtt - t_s_b(ji,jk) ) + lfus )
-         END DO
-      END DO
-      !
-   END SUBROUTINE lim_thd_enmelt
-
+            ztmelts       =  -tmut * s_i_1d(ji,jk) + rtt
+            ! Conversion q(S,T) -> T (second order equation)
+            zaaa          =  cpic
+            zbbb          =  ( rcp - cpic ) * ( ztmelts - rtt ) + q_i_1d(ji,jk) / rhoic - lfus
+            zccc          =  lfus * ( ztmelts - rtt )
+            zdiscrim      =  SQRT( MAX( zbbb * zbbb - 4._wp * zaaa * zccc, 0._wp ) )
+            t_i_1d(ji,jk) =  rtt - ( zbbb + zdiscrim ) / ( 2._wp * zaaa )
+            
+            ! mask temperature
+            rswitch       =  1._wp - MAX( 0._wp , SIGN( 1._wp , - ht_i_1d(ji) ) ) 
+            t_i_1d(ji,jk) =  rswitch * t_i_1d(ji,jk) + ( 1._wp - rswitch ) * rtt
+         END DO 
+      END DO 
+
+   END SUBROUTINE lim_thd_temp
 
    SUBROUTINE lim_thd_init
@@ -818,7 +564,5 @@
       INTEGER  ::   ios                 ! Local integer output status for namelist read
       NAMELIST/namicethd/ hmelt , hiccrit, fraz_swi, maxfrazb, vfrazb, Cfrazb,   &
-         &                hicmin, hiclim,                                        &
-         &                sbeta  , parlat, hakspl, hibspl, exld,                 &
-         &                hakdif, hnzst  , thth  , parsub, alphs, betas,         & 
+         &                hiclim, hnzst, parsub, betas,                          & 
          &                kappa_i, nconv_i_thd, maxer_i_thd, thcon_i_swi
       !!-------------------------------------------------------------------
@@ -837,5 +581,7 @@
       READ  ( numnam_ice_cfg, namicethd, IOSTAT = ios, ERR = 902 )
 902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namicethd in configuration namelist', lwp )
-      WRITE ( numoni, namicethd )
+      IF(lwm) WRITE ( numoni, namicethd )
+
+      IF( lk_cpl .AND. parsub /= 0.0 )   CALL ctl_stop( 'In coupled mode, use parsub = 0. or send dqla' )
       !
       IF(lwp) THEN                          ! control print
@@ -843,22 +589,13 @@
          WRITE(numout,*)'   Namelist of ice parameters for ice thermodynamic computation '
          WRITE(numout,*)'      maximum melting at the bottom                           hmelt        = ', hmelt
-         WRITE(numout,*)'      ice thick. for lateral accretion in NH (SH)             hiccrit(1/2) = ', hiccrit
+         WRITE(numout,*)'      ice thick. for lateral accretion                        hiccrit      = ', hiccrit
          WRITE(numout,*)'      Frazil ice thickness as a function of wind or not       fraz_swi     = ', fraz_swi
          WRITE(numout,*)'      Maximum proportion of frazil ice collecting at bottom   maxfrazb     = ', maxfrazb
          WRITE(numout,*)'      Thresold relative drift speed for collection of frazil  vfrazb       = ', vfrazb
          WRITE(numout,*)'      Squeezing coefficient for collection of frazil          Cfrazb       = ', Cfrazb
-         WRITE(numout,*)'      ice thick. corr. to max. energy stored in brine pocket  hicmin       = ', hicmin  
          WRITE(numout,*)'      minimum ice thickness                                   hiclim       = ', hiclim 
          WRITE(numout,*)'      numerical carac. of the scheme for diffusion in ice '
-         WRITE(numout,*)'      Cranck-Nicholson (=0.5), implicit (=1), explicit (=0)   sbeta        = ', sbeta
-         WRITE(numout,*)'      percentage of energy used for lateral ablation          parlat       = ', parlat
-         WRITE(numout,*)'      slope of distr. for Hakkinen-Mellor lateral melting     hakspl       = ', hakspl  
-         WRITE(numout,*)'      slope of distribution for Hibler lateral melting        hibspl       = ', hibspl
-         WRITE(numout,*)'      exponent for leads-closure rate                         exld         = ', exld
-         WRITE(numout,*)'      coefficient for diffusions of ice and snow              hakdif       = ', hakdif
-         WRITE(numout,*)'      threshold thick. for comp. of eq. thermal conductivity  zhth         = ', thth 
          WRITE(numout,*)'      thickness of the surf. layer in temp. computation       hnzst        = ', hnzst
          WRITE(numout,*)'      switch for snow sublimation  (=1) or not (=0)           parsub       = ', parsub  
-         WRITE(numout,*)'      coefficient for snow density when snow ice formation    alphs        = ', alphs
          WRITE(numout,*)'      coefficient for ice-lead partition of snowfall          betas        = ', betas
          WRITE(numout,*)'      extinction radiation parameter in sea ice (1.0)         kappa_i      = ', kappa_i
@@ -866,8 +603,6 @@
          WRITE(numout,*)'      maximal err. on T for heat diffusion computation        maxer_i_thd  = ', maxer_i_thd
          WRITE(numout,*)'      switch for comp. of thermal conductivity in the ice     thcon_i_swi  = ', thcon_i_swi
+         WRITE(numout,*)'      check heat conservation in the ice/snow                 con_i        = ', con_i
       ENDIF
-      !
-      rcdsn = hakdif * rcdsn 
-      rcdic = hakdif * rcdic
       !
    END SUBROUTINE lim_thd_init
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd_dh.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd_dh.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd_dh.F90	(revision 5038)
@@ -6,5 +6,5 @@
    !! History :  LIM  ! 2003-05 (M. Vancoppenolle) Original code in 1D
    !!                 ! 2005-06 (M. Vancoppenolle) 3D version 
-   !!            3.2  ! 2009-07 (M. Vancoppenolle, Y. Aksenov, G. Madec) bug correction in rdm_snw & rdm_ice
+   !!            3.2  ! 2009-07 (M. Vancoppenolle, Y. Aksenov, G. Madec) bug correction in wfx_snw & wfx_ice
    !!            3.4  ! 2011-02 (G. Madec) dynamical allocation
    !!            3.5  ! 2012-10 (G. Madec & co) salt flux + bug fixes 
@@ -26,15 +26,9 @@
    USE wrk_nemo       ! work arrays
    USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
-
+   
    IMPLICIT NONE
    PRIVATE
 
    PUBLIC   lim_thd_dh   ! called by lim_thd
-
-   REAL(wp) ::   epsi20 = 1.e-20   ! constant values
-   REAL(wp) ::   epsi10 = 1.e-10   !
-   REAL(wp) ::   epsi13 = 1.e-13   !
-   REAL(wp) ::   zzero  = 0._wp    !
-   REAL(wp) ::   zone   = 1._wp    !
 
    !!----------------------------------------------------------------------
@@ -45,5 +39,5 @@
 CONTAINS
 
-   SUBROUTINE lim_thd_dh( kideb, kiut, jl )
+   SUBROUTINE lim_thd_dh( kideb, kiut )
       !!------------------------------------------------------------------
       !!                ***  ROUTINE lim_thd_dh  ***
@@ -70,36 +64,35 @@
       !!------------------------------------------------------------------
       INTEGER , INTENT(in) ::   kideb, kiut   ! Start/End point on which the  the computation is applied
-      INTEGER , INTENT(in) ::   jl            ! Thickness cateogry number
       !! 
       INTEGER  ::   ji , jk        ! dummy loop indices
       INTEGER  ::   ii, ij         ! 2D corresponding indices to ji
-      INTEGER  ::   isnow          ! switch for presence (1) or absence (0) of snow
-      INTEGER  ::   isnowic        ! snow ice formation not
-      INTEGER  ::   i_ice_switch   ! ice thickness above a certain treshold or not
       INTEGER  ::   iter
 
-      REAL(wp) ::   zzfmass_i, zihgnew                     ! local scalar
-      REAL(wp) ::   zzfmass_s, zhsnew, ztmelts             ! local scalar
-      REAL(wp) ::   zhn, zdhcf, zdhbf, zhni, zhnfi, zihg   !
-      REAL(wp) ::   zdhnm, zhnnew, zhisn, zihic, zzc       !
+      REAL(wp) ::   ztmelts             ! local scalar
+      REAL(wp) ::   zdh, zfdum  !
       REAL(wp) ::   zfracs       ! fractionation coefficient for bottom salt entrapment
       REAL(wp) ::   zcoeff       ! dummy argument for snowfall partitioning over ice and leads
-      REAL(wp) ::   zsm_snowice  ! snow-ice salinity
+      REAL(wp) ::   zs_snic  ! snow-ice salinity
       REAL(wp) ::   zswi1        ! switch for computation of bottom salinity
       REAL(wp) ::   zswi12       ! switch for computation of bottom salinity
       REAL(wp) ::   zswi2        ! switch for computation of bottom salinity
       REAL(wp) ::   zgrr         ! bottom growth rate
-      REAL(wp) ::   ztform       ! bottom formation temperature
-      !
-      REAL(wp), POINTER, DIMENSION(:) ::   zh_i        ! ice layer thickness
+      REAL(wp) ::   zt_i_new     ! bottom formation temperature
+
+      REAL(wp) ::   zQm          ! enthalpy exchanged with the ocean (J/m2), >0 towards the ocean
+      REAL(wp) ::   zEi          ! specific enthalpy of sea ice (J/kg)
+      REAL(wp) ::   zEw          ! specific enthalpy of exchanged water (J/kg)
+      REAL(wp) ::   zdE          ! specific enthalpy difference (J/kg)
+      REAL(wp) ::   zfmdt        ! exchange mass flux x time step (J/m2), >0 towards the ocean
+      REAL(wp) ::   zsstK        ! SST in Kelvin
+
       REAL(wp), POINTER, DIMENSION(:) ::   zh_s        ! snow layer thickness
-      REAL(wp), POINTER, DIMENSION(:) ::   ztfs        ! melting point
-      REAL(wp), POINTER, DIMENSION(:) ::   zhsold      ! old snow thickness
-      REAL(wp), POINTER, DIMENSION(:) ::   zqprec      ! energy of fallen snow
-      REAL(wp), POINTER, DIMENSION(:) ::   zqfont_su   ! incoming, remaining surface energy
-      REAL(wp), POINTER, DIMENSION(:) ::   zqfont_bo   ! incoming, bottom energy
-      REAL(wp), POINTER, DIMENSION(:) ::   z_f_surf    ! surface heat for ablation
-      REAL(wp), POINTER, DIMENSION(:) ::   zhgnew      ! new ice thickness
-      REAL(wp), POINTER, DIMENSION(:) ::   zfmass_i    ! 
+      REAL(wp), POINTER, DIMENSION(:) ::   zqprec      ! energy of fallen snow                       (J.m-3)
+      REAL(wp), POINTER, DIMENSION(:) ::   zq_su       ! heat for surface ablation                   (J.m-2)
+      REAL(wp), POINTER, DIMENSION(:) ::   zq_bo       ! heat for bottom ablation                    (J.m-2)
+      REAL(wp), POINTER, DIMENSION(:) ::   zq_1cat     ! corrected heat in case 1-cat and hmelt>15cm (J.m-2)
+      REAL(wp), POINTER, DIMENSION(:) ::   zq_rema     ! remaining heat at the end of the routine    (J.m-2)
+      REAL(wp), POINTER, DIMENSION(:) ::   zf_tt     ! Heat budget to determine melting or freezing(W.m-2)
+      INTEGER , POINTER, DIMENSION(:) ::   icount      ! number of layers vanished by melting 
 
       REAL(wp), POINTER, DIMENSION(:) ::   zdh_s_mel   ! snow melt 
@@ -108,87 +101,107 @@
 
       REAL(wp), POINTER, DIMENSION(:,:) ::   zdeltah
-
-      ! Pathological cases
-      REAL(wp), POINTER, DIMENSION(:) ::   zfdt_init   ! total incoming heat for ice melt
-      REAL(wp), POINTER, DIMENSION(:) ::   zfdt_final  ! total remaing heat for ice melt
-      REAL(wp), POINTER, DIMENSION(:) ::   zqt_i       ! total ice heat content
-      REAL(wp), POINTER, DIMENSION(:) ::   zqt_s       ! total snow heat content
-      REAL(wp), POINTER, DIMENSION(:) ::   zqt_dummy   ! dummy heat content
-
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zqt_i_lay   ! total ice heat content
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zh_i      ! ice layer thickness
+
+      REAL(wp), POINTER, DIMENSION(:) ::   zqh_i       ! total ice heat content  (J.m-2)
+      REAL(wp), POINTER, DIMENSION(:) ::   zqh_s       ! total snow heat content (J.m-2)
+      REAL(wp), POINTER, DIMENSION(:) ::   zq_s        ! total snow enthalpy     (J.m-3)
 
       ! mass and salt flux (clem)
-      REAL(wp) :: zdvres, zdvsur, zdvbot
-      REAL(wp), POINTER, DIMENSION(:) ::   zviold, zvsold   ! old ice volume...
+      REAL(wp) :: zdvres, zswitch_sal
 
       ! Heat conservation 
-      INTEGER  ::   num_iter_max, numce_dh
-      REAL(wp) ::   meance_dh
-      REAL(wp) ::   zinda 
-      REAL(wp), POINTER, DIMENSION(:) ::   zinnermelt
-      REAL(wp), POINTER, DIMENSION(:) ::   zfbase, zdq_i
+      INTEGER  ::   num_iter_max
+
       !!------------------------------------------------------------------
 
-      CALL wrk_alloc( jpij, zh_i, zh_s, ztfs, zhsold, zqprec, zqfont_su, zqfont_bo, z_f_surf, zhgnew, zfmass_i )
-      CALL wrk_alloc( jpij, zdh_s_mel, zdh_s_pre, zdh_s_sub, zfdt_init, zfdt_final, zqt_i, zqt_s, zqt_dummy )
-      CALL wrk_alloc( jpij, zinnermelt, zfbase, zdq_i )
-      CALL wrk_alloc( jpij, jkmax, zdeltah, zqt_i_lay )
-
-      CALL wrk_alloc( jpij, zviold, zvsold ) ! clem
+      ! Discriminate between varying salinity (num_sal=2) and prescribed cases (other values)
+      SELECT CASE( num_sal )                       ! varying salinity or not
+         CASE( 1, 3, 4 ) ;   zswitch_sal = 0       ! prescribed salinity profile
+         CASE( 2 )       ;   zswitch_sal = 1       ! varying salinity profile
+      END SELECT
+
+      CALL wrk_alloc( jpij, zh_s, zqprec, zq_su, zq_bo, zf_tt, zq_1cat, zq_rema )
+      CALL wrk_alloc( jpij, zdh_s_mel, zdh_s_pre, zdh_s_sub, zqh_i, zqh_s, zq_s )
+      CALL wrk_alloc( jpij, nlay_i+1, zdeltah, zh_i )
+      CALL wrk_alloc( jpij, icount )
       
-      ftotal_fin(:) = 0._wp
-      zfdt_init (:) = 0._wp
-      zfdt_final(:) = 0._wp
-
-      dh_i_surf (:) = 0._wp
-      dh_i_bott (:) = 0._wp
-      dh_snowice(:) = 0._wp
-
-      DO ji = kideb, kiut
-         old_ht_i_b(ji) = ht_i_b(ji)
-         old_ht_s_b(ji) = ht_s_b(ji)
-         zviold(ji) = a_i_b(ji) * ht_i_b(ji) ! clem
-         zvsold(ji) = a_i_b(ji) * ht_s_b(ji) ! clem
-      END DO
+      dh_i_surf  (:) = 0._wp ; dh_i_bott  (:) = 0._wp ; dh_snowice(:) = 0._wp
+      dsm_i_se_1d(:) = 0._wp ; dsm_i_si_1d(:) = 0._wp   
+ 
+      zqprec (:) = 0._wp ; zq_su  (:) = 0._wp ; zq_bo  (:) = 0._wp ; zf_tt  (:) = 0._wp
+      zq_1cat(:) = 0._wp ; zq_rema(:) = 0._wp
+
+      zh_s     (:) = 0._wp       
+      zdh_s_pre(:) = 0._wp
+      zdh_s_mel(:) = 0._wp
+      zdh_s_sub(:) = 0._wp
+      zqh_s    (:) = 0._wp      
+      zqh_i    (:) = 0._wp   
+
+      zh_i      (:,:) = 0._wp       
+      zdeltah   (:,:) = 0._wp       
+      icount    (:)   = 0
+
+      ! initialize layer thicknesses and enthalpies
+      h_i_old (:,0:nlay_i+1) = 0._wp
+      qh_i_old(:,0:nlay_i+1) = 0._wp
+      DO jk = 1, nlay_i
+         DO ji = kideb, kiut
+            h_i_old (ji,jk) = ht_i_1d(ji) / REAL( nlay_i )
+            qh_i_old(ji,jk) = q_i_1d(ji,jk) * h_i_old(ji,jk)
+         ENDDO
+      ENDDO
       !
       !------------------------------------------------------------------------------!
-      !  1) Calculate available heat for surface ablation                            !
+      !  1) Calculate available heat for surface and bottom ablation                 !
       !------------------------------------------------------------------------------!
       !
       DO ji = kideb, kiut
-         isnow         = INT(  1.0 - MAX(  0.0 , SIGN( 1.0 , - ht_s_b(ji) )  )  )
-         ztfs     (ji) = isnow * rtt + ( 1.0 - isnow ) * rtt
-         z_f_surf (ji) = qnsr_ice_1d(ji) + ( 1.0 - i0(ji) ) * qsr_ice_1d(ji) - fc_su(ji)
-         z_f_surf (ji) = MAX(  zzero , z_f_surf(ji)  ) * MAX(  zzero , SIGN( zone , t_su_b(ji) - ztfs(ji) )  )
-         zfdt_init(ji) = ( z_f_surf(ji) + MAX( fbif_1d(ji) + qlbbq_1d(ji) + fc_bo_i(ji),0.0 ) ) * rdt_ice
-      END DO ! ji
-
-      zqfont_su  (:) = 0._wp
-      zqfont_bo  (:) = 0._wp
-      dsm_i_se_1d(:) = 0._wp     
-      dsm_i_si_1d(:) = 0._wp   
+         rswitch       = 1._wp - MAX(  0._wp , SIGN( 1._wp , - ht_s_1d(ji) ) )
+         ztmelts       = rswitch * rtt + ( 1._wp - rswitch ) * rtt
+
+         zfdum      = qns_ice_1d(ji) + ( 1._wp - i0(ji) ) * qsr_ice_1d(ji) - fc_su(ji) 
+         zf_tt(ji)  = fc_bo_i(ji) + fhtur_1d(ji) + fhld_1d(ji) 
+
+         zq_su (ji) = MAX( 0._wp, zfdum     * rdt_ice ) * MAX( 0._wp , SIGN( 1._wp, t_su_1d(ji) - ztmelts ) )
+         zq_bo (ji) = MAX( 0._wp, zf_tt(ji) * rdt_ice )
+      END DO
+
       !
       !------------------------------------------------------------------------------!
-      !  2) Computing layer thicknesses and  snow and sea-ice enthalpies.            !
+      ! If snow temperature is above freezing point, then snow melts 
+      ! (should not happen but sometimes it does)
       !------------------------------------------------------------------------------!
-      !
-      DO ji = kideb, kiut     ! Layer thickness
-         zh_i(ji) = ht_i_b(ji) / REAL( nlay_i )
-         zh_s(ji) = ht_s_b(ji) / REAL( nlay_s )
-      END DO
-      !
-      zqt_s(:) = 0._wp        ! Total enthalpy of the snow
+      DO ji = kideb, kiut
+         IF( t_s_1d(ji,1) > rtt ) THEN !!! Internal melting
+            ! Contribution to heat flux to the ocean [W.m-2], < 0  
+            hfx_res_1d(ji) = hfx_res_1d(ji) + q_s_1d(ji,1) * ht_s_1d(ji) * a_i_1d(ji) * r1_rdtice
+            ! Contribution to mass flux
+            wfx_snw_1d(ji) = wfx_snw_1d(ji) + rhosn * ht_s_1d(ji) * a_i_1d(ji) * r1_rdtice
+            ! updates
+            ht_s_1d(ji)   = 0._wp
+            q_s_1d (ji,1) = 0._wp
+            t_s_1d (ji,1) = rtt
+         END IF
+      END DO
+
+      !------------------------------------------------------------!
+      !  2) Computing layer thicknesses and enthalpies.            !
+      !------------------------------------------------------------!
+      !
+      DO ji = kideb, kiut     
+         zh_s(ji) = ht_s_1d(ji) / REAL( nlay_s )
+      END DO
+      !
       DO jk = 1, nlay_s
          DO ji = kideb, kiut
-            zqt_s(ji) =  zqt_s(ji) + q_s_b(ji,jk) * ht_s_b(ji) / REAL( nlay_s )
+            zqh_s(ji) =  zqh_s(ji) + q_s_1d(ji,jk) * zh_s(ji)
          END DO
       END DO
       !
-      zqt_i(:) = 0._wp        ! Total enthalpy of the ice
       DO jk = 1, nlay_i
          DO ji = kideb, kiut
-            zzc = q_i_b(ji,jk) * ht_i_b(ji) / REAL( nlay_i )
-            zqt_i(ji)        =  zqt_i(ji) + zzc
-            zqt_i_lay(ji,jk) =              zzc
+            zh_i(ji,jk) = ht_i_1d(ji) / REAL( nlay_i )
+            zqh_i(ji)   = zqh_i(ji) + q_i_1d(ji,jk) * zh_i(ji,jk)
          END DO
       END DO
@@ -212,149 +225,166 @@
       ! Martin Vancoppenolle, December 2006
 
-      ! Snow fall
-      DO ji = kideb, kiut
-         zcoeff = ( 1.0 - ( 1.0 - at_i_b(ji) )**betas ) / at_i_b(ji) 
+      DO ji = kideb, kiut
+         !-----------
+         ! Snow fall
+         !-----------
+         ! thickness change
+         zcoeff = ( 1._wp - ( 1._wp - at_i_1d(ji) )**betas ) / at_i_1d(ji) 
          zdh_s_pre(ji) = zcoeff * sprecip_1d(ji) * rdt_ice / rhosn
-      END DO
-      zdh_s_mel(:) =  0._wp
-
-      ! Melt of fallen snow
-      DO ji = kideb, kiut
-         ! tatm_ice is now in K
-         zqprec   (ji)   =  rhosn * ( cpic * ( rtt - tatm_ice_1d(ji) ) + lfus )  
-         zqfont_su(ji)   =  z_f_surf(ji) * rdt_ice
-         zdeltah  (ji,1) =  MIN( 0.e0 , - zqfont_su(ji) / MAX( zqprec(ji) , epsi13 ) )
-         zqfont_su(ji)   =  MAX( 0.e0 , - zdh_s_pre(ji) - zdeltah(ji,1)              ) * zqprec(ji)
-         zdeltah  (ji,1) =  MAX( - zdh_s_pre(ji) , zdeltah(ji,1) )
-         zdh_s_mel(ji)   =  zdh_s_mel(ji) + zdeltah(ji,1)
-         ! heat conservation
-         qt_s_in(ji,jl)  =  qt_s_in(ji,jl) + zqprec(ji) * zdh_s_pre(ji)
-         zqt_s  (ji)     =  zqt_s  (ji)    + zqprec(ji) * zdh_s_pre(ji)
-         zqt_s  (ji)     =  MAX( zqt_s(ji) - zqfont_su(ji) , 0.e0 ) 
-      END DO
-
-
-      ! Snow melt due to surface heat imbalance
+         ! enthalpy of the precip (>0, J.m-3) (tatm_ice is now in K)
+         zqprec   (ji) = rhosn * ( cpic * ( rtt - MIN( tatm_ice_1d(ji), rt0_snow) ) + lfus )   
+         IF( sprecip_1d(ji) == 0._wp ) zqprec(ji) = 0._wp
+         ! heat flux from snow precip (>0, W.m-2)
+         hfx_spr_1d(ji) = hfx_spr_1d(ji) + zdh_s_pre(ji) * a_i_1d(ji) * zqprec(ji) * r1_rdtice
+         ! mass flux, <0
+         wfx_spr_1d(ji) = wfx_spr_1d(ji) - rhosn * a_i_1d(ji) * zdh_s_pre(ji) * r1_rdtice
+         ! update thickness
+         ht_s_1d    (ji) = MAX( 0._wp , ht_s_1d(ji) + zdh_s_pre(ji) )
+
+         !---------------------
+         ! Melt of falling snow
+         !---------------------
+         ! thickness change
+         IF( zdh_s_pre(ji) > 0._wp ) THEN
+         rswitch        = 1._wp - MAX( 0._wp , SIGN( 1._wp , - zqprec(ji) + epsi20 ) )
+         zdh_s_mel (ji) = - rswitch * zq_su(ji) / MAX( zqprec(ji) , epsi20 )
+         zdh_s_mel (ji) = MAX( - zdh_s_pre(ji), zdh_s_mel(ji) ) ! bound melting 
+         ! heat used to melt snow (W.m-2, >0)
+         hfx_snw_1d(ji) = hfx_snw_1d(ji) - zdh_s_mel(ji) * a_i_1d(ji) * zqprec(ji) * r1_rdtice
+         ! snow melting only = water into the ocean (then without snow precip), >0
+         wfx_snw_1d(ji) = wfx_snw_1d(ji) - rhosn * a_i_1d(ji) * zdh_s_mel(ji) * r1_rdtice
+         
+         ! updates available heat + thickness
+         zq_su (ji) = MAX( 0._wp , zq_su (ji) + zdh_s_mel(ji) * zqprec(ji) )      
+         ht_s_1d(ji) = MAX( 0._wp , ht_s_1d(ji) + zdh_s_mel(ji) )
+         zh_s  (ji) = ht_s_1d(ji) / REAL( nlay_s )
+
+         ENDIF
+      END DO
+
+      ! If heat still available, then melt more snow
+      zdeltah(:,:) = 0._wp ! important
       DO jk = 1, nlay_s
          DO ji = kideb, kiut
-            zdeltah  (ji,jk) = - zqfont_su(ji) / q_s_b(ji,jk)
-            zqfont_su(ji)    =  MAX( 0.0 , - zh_s(ji) - zdeltah(ji,jk) ) * q_s_b(ji,jk) 
-            zdeltah  (ji,jk) =  MAX( zdeltah(ji,jk) , - zh_s(ji) )
-            zdh_s_mel(ji)    =  zdh_s_mel(ji) + zdeltah(ji,jk)        ! resulting melt of snow    
+            ! thickness change
+            rswitch          = 1._wp - MAX( 0._wp, SIGN( 1._wp, - ht_s_1d(ji) ) ) 
+            rswitch          = rswitch * ( 1._wp - MAX( 0._wp, SIGN( 1._wp, - q_s_1d(ji,jk) + epsi20 ) ) ) 
+            zdeltah  (ji,jk) = - rswitch * zq_su(ji) / MAX( q_s_1d(ji,jk), epsi20 )
+            zdeltah  (ji,jk) = MAX( zdeltah(ji,jk) , - zh_s(ji) ) ! bound melting
+            zdh_s_mel(ji)    = zdh_s_mel(ji) + zdeltah(ji,jk)    
+            ! heat used to melt snow(W.m-2, >0)
+            hfx_snw_1d(ji)   = hfx_snw_1d(ji) - zdeltah(ji,jk) * a_i_1d(ji) * q_s_1d(ji,jk) * r1_rdtice 
+            ! snow melting only = water into the ocean (then without snow precip)
+            wfx_snw_1d(ji)   = wfx_snw_1d(ji) - rhosn * a_i_1d(ji) * zdeltah(ji,jk) * r1_rdtice
+
+            ! updates available heat + thickness
+            zq_su (ji) = MAX( 0._wp , zq_su (ji) + zdeltah(ji,jk) * q_s_1d(ji,jk) )
+            ht_s_1d(ji) = MAX( 0._wp , ht_s_1d(ji) + zdeltah(ji,jk) )
+
          END DO
       END DO
 
-      ! Apply snow melt to snow depth
-      DO ji = kideb, kiut
-         dh_s_tot(ji)   =  zdh_s_mel(ji) + zdh_s_pre(ji)
-         ! Old and new snow depths
-         zhsold(ji)     =  ht_s_b(ji)
-         zhsnew         =  ht_s_b(ji) + dh_s_tot(ji)
-         ! If snow is still present zhn = 1, else zhn = 0
-         zhn            =  1.0 - MAX(  zzero , SIGN( zone , - zhsnew )  )
-         ht_s_b(ji)     =  MAX( zzero , zhsnew )
-         ! we recompute dh_s_tot (clem) 
-         dh_s_tot (ji)  =  ht_s_b(ji) - zhsold(ji)
-         ! Volume and mass variations of snow
-         dvsbq_1d  (ji) =  a_i_b(ji) * ( ht_s_b(ji) - zhsold(ji) - zdh_s_pre(ji) )
-         dvsbq_1d  (ji) =  MIN( zzero, dvsbq_1d(ji) )
-         !clem rdm_snw_1d(ji) =  rdm_snw_1d(ji) + rhosn * dvsbq_1d(ji)
+      !----------------------
+      ! 3.2 Snow sublimation 
+      !----------------------
+      ! qla_ice is always >=0 (upwards), heat goes to the atmosphere, therefore snow sublimates
+      ! clem comment: not counted in mass exchange in limsbc since this is an exchange with atm. (not ocean)
+      ! clem comment: ice should also sublimate
+      IF( lk_cpl ) THEN
+         ! coupled mode: sublimation already included in emp_ice (to do in limsbc_ice)
+         zdh_s_sub(:)      =  0._wp 
+      ELSE
+         ! forced  mode: snow thickness change due to sublimation
+         DO ji = kideb, kiut
+            zdh_s_sub(ji)  =  MAX( - ht_s_1d(ji) , - parsub * qla_ice_1d(ji) / ( rhosn * lsub ) * rdt_ice )
+            ! Heat flux by sublimation [W.m-2], < 0
+            !      sublimate first snow that had fallen, then pre-existing snow
+            zcoeff         =      ( MAX( zdh_s_sub(ji), - MAX( 0._wp, zdh_s_pre(ji) + zdh_s_mel(ji) ) )   * zqprec(ji) +   &
+               &  ( zdh_s_sub(ji) - MAX( zdh_s_sub(ji), - MAX( 0._wp, zdh_s_pre(ji) + zdh_s_mel(ji) ) ) ) * q_s_1d(ji,1) )  &
+               &  * a_i_1d(ji) * r1_rdtice
+            hfx_sub_1d(ji) = hfx_sub_1d(ji) + zcoeff
+            ! Mass flux by sublimation
+            wfx_sub_1d(ji) =  wfx_sub_1d(ji) - rhosn * a_i_1d(ji) * zdh_s_sub(ji) * r1_rdtice
+            ! new snow thickness
+            ht_s_1d(ji)     =  MAX( 0._wp , ht_s_1d(ji) + zdh_s_sub(ji) )
+         END DO
+      ENDIF
+
+      ! --- Update snow diags --- !
+      DO ji = kideb, kiut
+         dh_s_tot(ji)   = zdh_s_mel(ji) + zdh_s_pre(ji) + zdh_s_sub(ji)
+         zh_s(ji)       = ht_s_1d(ji) / REAL( nlay_s )
       END DO ! ji
 
+      !-------------------------------------------
+      ! 3.3 Update temperature, energy
+      !-------------------------------------------
+      ! new temp and enthalpy of the snow (remaining snow precip + remaining pre-existing snow)
+      zq_s(:) = 0._wp 
+      DO jk = 1, nlay_s
+         DO ji = kideb,kiut
+            rswitch       =  MAX(  0._wp , SIGN( 1._wp, - ht_s_1d(ji) + epsi20 )  )
+            q_s_1d(ji,jk) = ( 1._wp - rswitch ) / MAX( ht_s_1d(ji), epsi20 ) *             &
+              &            ( (   MAX( 0._wp, dh_s_tot(ji) )              ) * zqprec(ji) +  &
+              &              ( - MAX( 0._wp, dh_s_tot(ji) ) + ht_s_1d(ji) ) * rhosn * ( cpic * ( rtt - t_s_1d(ji,jk) ) + lfus ) )
+            zq_s(ji)     =  zq_s(ji) + q_s_1d(ji,jk)
+         END DO
+      END DO
+
       !--------------------------
-      ! 3.2 Surface ice ablation 
+      ! 3.4 Surface ice ablation 
       !--------------------------
-      DO ji = kideb, kiut 
-         z_f_surf (ji) =  zqfont_su(ji) * r1_rdtice   ! heat conservation test
-         zdq_i    (ji) =  0._wp
-      END DO ! ji
-
+      zdeltah(:,:) = 0._wp ! important
       DO jk = 1, nlay_i
          DO ji = kideb, kiut 
-            !                                                    ! melt of layer jk
-            zdeltah  (ji,jk) = - zqfont_su(ji) / q_i_b(ji,jk)
-            !                                                    ! recompute heat available
-            zqfont_su(ji   ) = MAX( 0.0 , - zh_i(ji) - zdeltah(ji,jk) ) * q_i_b(ji,jk) 
-            !                                                    ! melt of layer jk cannot be higher than its thickness
-            zdeltah  (ji,jk) = MAX( zdeltah(ji,jk) , - zh_i(ji) )
-            !                                                    ! update surface melt
-            dh_i_surf(ji   ) = dh_i_surf(ji) + zdeltah(ji,jk) 
-            !                                                    ! for energy conservation
-            zdq_i    (ji   ) = zdq_i(ji) + zdeltah(ji,jk) * q_i_b(ji,jk) * r1_rdtice
-            !
-            ! clem
-            sfx_thd_1d(ji) = sfx_thd_1d(ji) - sm_i_b(ji) * a_i_b(ji)    &
-               &                              * MIN( zdeltah(ji,jk) , 0._wp ) * rhoic / rdt_ice
+            zEi            = - q_i_1d(ji,jk) / rhoic                ! Specific enthalpy of layer k [J/kg, <0]
+
+            ztmelts        = - tmut * s_i_1d(ji,jk) + rtt           ! Melting point of layer k [K]
+
+            zEw            =    rcp * ( ztmelts - rt0 )            ! Specific enthalpy of resulting meltwater [J/kg, <0]
+
+            zdE            =    zEi - zEw                          ! Specific enthalpy difference < 0
+
+            zfmdt          = - zq_su(ji) / zdE                     ! Mass flux to the ocean [kg/m2, >0]
+
+            zdeltah(ji,jk) = - zfmdt / rhoic                       ! Melt of layer jk [m, <0]
+
+            zdeltah(ji,jk) = MIN( 0._wp , MAX( zdeltah(ji,jk) , - zh_i(ji,jk) ) )    ! Melt of layer jk cannot exceed the layer thickness [m, <0]
+
+            zq_su(ji)      = MAX( 0._wp , zq_su(ji) - zdeltah(ji,jk) * rhoic * zdE ) ! update available heat
+
+            dh_i_surf(ji)  = dh_i_surf(ji) + zdeltah(ji,jk)        ! Cumulate surface melt
+
+            zfmdt          = - rhoic * zdeltah(ji,jk)              ! Recompute mass flux [kg/m2, >0]
+
+            zQm            = zfmdt * zEw                           ! Energy of the melt water sent to the ocean [J/m2, <0]
+
+            ! Contribution to salt flux (clem: using sm_i_1d and not s_i_1d(jk) is ok)
+            sfx_sum_1d(ji)   = sfx_sum_1d(ji) - sm_i_1d(ji) * a_i_1d(ji) * zdeltah(ji,jk) * rhoic * r1_rdtice
+
+            ! Contribution to heat flux [W.m-2], < 0
+            hfx_thd_1d(ji) = hfx_thd_1d(ji) + zfmdt * a_i_1d(ji) * zEw * r1_rdtice
+
+            ! Total heat flux used in this process [W.m-2], > 0  
+            hfx_sum_1d(ji) = hfx_sum_1d(ji) - zfmdt * a_i_1d(ji) * zdE * r1_rdtice
+
+            ! Contribution to mass flux
+            wfx_sum_1d(ji) =  wfx_sum_1d(ji) - rhoic * a_i_1d(ji) * zdeltah(ji,jk) * r1_rdtice
+           
+            ! record which layers have disappeared (for bottom melting) 
+            !    => icount=0 : no layer has vanished
+            !    => icount=5 : 5 layers have vanished
+            rswitch     = MAX( 0._wp , SIGN( 1._wp , - ( zh_i(ji,jk) + zdeltah(ji,jk) ) ) ) 
+            icount(ji)  = icount(ji) + NINT( rswitch )
+            zh_i(ji,jk) = MAX( 0._wp , zh_i(ji,jk) + zdeltah(ji,jk) )
+
+            ! update heat content (J.m-2) and layer thickness
+            qh_i_old(ji,jk) = qh_i_old(ji,jk) + zdeltah(ji,jk) * q_i_1d(ji,jk)
+            h_i_old (ji,jk) = h_i_old (ji,jk) + zdeltah(ji,jk)
          END DO
       END DO
-
-      !                                          !-------------------
-      IF( con_i .AND. jiindex_1d > 0 ) THEN      ! Conservation test
-         !                                       !-------------------
-         numce_dh  = 0
-         meance_dh = 0._wp
-         DO ji = kideb, kiut
-            IF ( ( z_f_surf(ji) + zdq_i(ji) ) .GE. 1.0e-3 ) THEN
-               numce_dh  = numce_dh + 1
-               meance_dh = meance_dh + z_f_surf(ji) + zdq_i(ji)
-            ENDIF
-            IF( z_f_surf(ji) + zdq_i(ji) .GE. 1.0e-3  ) THEN!
-               WRITE(numout,*) ' ALERTE heat loss for surface melt '
-               WRITE(numout,*) ' ii, ij, jl :', ii, ij, jl
-               WRITE(numout,*) ' ht_i_b       : ', ht_i_b(ji)
-               WRITE(numout,*) ' z_f_surf     : ', z_f_surf(ji)
-               WRITE(numout,*) ' zdq_i        : ', zdq_i(ji)
-               WRITE(numout,*) ' ht_i_b       : ', ht_i_b(ji)
-               WRITE(numout,*) ' fc_bo_i      : ', fc_bo_i(ji)
-               WRITE(numout,*) ' fbif_1d      : ', fbif_1d(ji)
-               WRITE(numout,*) ' qlbbq_1d     : ', qlbbq_1d(ji)
-               WRITE(numout,*) ' s_i_new      : ', s_i_new(ji)
-               WRITE(numout,*) ' sss_m        : ', sss_m(ii,ij)
-            ENDIF
-         END DO
-         !
-         IF( numce_dh > 0 )   meance_dh = meance_dh / numce_dh
-         WRITE(numout,*) ' Error report - Category : ', jl
-         WRITE(numout,*) ' ~~~~~~~~~~~~ '
-         WRITE(numout,*) ' Number of points where there is sur. me. error : ', numce_dh
-         WRITE(numout,*) ' Mean basal growth error on error points : ', meance_dh
-         !
-      ENDIF
-
-      !----------------------
-      ! 3.3 Snow sublimation
-      !----------------------
-
-      DO ji = kideb, kiut
-         ! qla_ice is always >=0 (upwards), heat goes to the atmosphere, therefore snow sublimates
-#if defined key_coupled
-         zdh_s_sub(ji)    =  0._wp      ! coupled mode: sublimation already included in emp_ice (to do in limsbc_ice)
-#else
-         !                              ! forced  mode: snow thickness change due to sublimation
-         zdh_s_sub(ji)    =  - parsub * qla_ice_1d(ji) / ( rhosn * lsub ) * rdt_ice
-#endif
-         dh_s_tot (ji)    =  dh_s_tot(ji) + zdh_s_sub(ji)
-         zdhcf            =  ht_s_b(ji) + zdh_s_sub(ji) 
-         ht_s_b   (ji)    =  MAX( zzero , zdhcf )
-         ! we recompute dh_s_tot 
-         dh_s_tot (ji)    =  ht_s_b(ji) - zhsold(ji)
-         qt_s_in  (ji,jl) =  qt_s_in(ji,jl) + zdh_s_sub(ji)*q_s_b(ji,1)
-      END DO
-
-      zqt_dummy(:) = 0.e0
-      DO jk = 1, nlay_s
-         DO ji = kideb,kiut
-            q_s_b    (ji,jk) = rhosn * ( cpic * ( rtt - t_s_b(ji,jk) ) + lfus )
-            zqt_dummy(ji)    =  zqt_dummy(ji) + q_s_b(ji,jk) * ht_s_b(ji) / REAL( nlay_s )            ! heat conservation
-         END DO
-      END DO
-
-      DO jk = 1, nlay_s
-         DO ji = kideb, kiut
-            ! In case of disparition of the snow, we have to update the snow temperatures
-            zhisn  =  MAX(  zzero , SIGN( zone, - ht_s_b(ji) )  )
-            t_s_b(ji,jk) = ( 1.0 - zhisn ) * t_s_b(ji,jk) + zhisn * rtt
-            q_s_b(ji,jk) = ( 1.0 - zhisn ) * q_s_b(ji,jk)
-         END DO
+      ! update ice thickness
+      DO ji = kideb, kiut
+         ht_i_1d(ji) =  MAX( 0._wp , ht_i_1d(ji) + dh_i_surf(ji) )
       END DO
 
@@ -364,302 +394,250 @@
       !------------------------------------------------------------------------------!
       !
-      ! Ice basal growth / melt is given by the ratio of heat budget over basal
-      ! ice heat content.  Basal heat budget is given by the difference between
-      ! the inner conductive flux  (fc_bo_i), from the open water heat flux 
-      ! (qlbbqb) and the turbulent ocean flux (fbif). 
-      ! fc_bo_i is positive downwards. fbif and qlbbq are positive to the ice 
-
-      !-----------------------------------------------------
-      ! 4.1 Basal growth - (a) salinity not varying in time 
-      !-----------------------------------------------------
-      IF(  num_sal /= 2  ) THEN   ! ice salinity constant in time
+      !------------------
+      ! 4.1 Basal growth 
+      !------------------
+      ! Basal growth is driven by heat imbalance at the ice-ocean interface,
+      ! between the inner conductive flux  (fc_bo_i), from the open water heat flux 
+      ! (fhld) and the turbulent ocean flux (fhtur). 
+      ! fc_bo_i is positive downwards. fhtur and fhld are positive to the ice 
+
+      ! If salinity varies in time, an iterative procedure is required, because
+      ! the involved quantities are inter-dependent.
+      ! Basal growth (dh_i_bott) depends upon new ice specific enthalpy (zEi),
+      ! which depends on forming ice salinity (s_i_new), which depends on dh/dt (dh_i_bott)
+      ! -> need for an iterative procedure, which converges quickly
+
+      IF ( num_sal == 2 ) THEN
+         num_iter_max = 5
+      ELSE
+         num_iter_max = 1
+      ENDIF
+
+      !clem debug. Just to be sure that enthalpy at nlay_i+1 is null
+      DO ji = kideb, kiut
+         q_i_1d(ji,nlay_i+1) = 0._wp
+      END DO
+
+      ! Iterative procedure
+      DO iter = 1, num_iter_max
          DO ji = kideb, kiut
-            IF(  ( fc_bo_i(ji) + fbif_1d(ji) + qlbbq_1d(ji) ) < 0._wp  ) THEN
-               s_i_new(ji)         =  sm_i_b(ji)
-               ! Melting point in K
-               ztmelts             =  - tmut * s_i_new(ji) + rtt 
-               ! New ice heat content (Bitz and Lipscomb, 1999)
-               ztform              =  t_i_b(ji,nlay_i)  ! t_bo_b crashes in the
-               ! Baltic
-               q_i_b(ji,nlay_i+1)  = rhoic * (  cpic * ( ztmelts - ztform )                                &
-                  &                           + lfus * (  1.0 - ( ztmelts - rtt ) / ( ztform - rtt )  )    &
-                  &                           - rcp  * ( ztmelts - rtt )                                 )
-               ! Basal growth rate = - F*dt / q
-               dh_i_bott(ji)       =  - rdt_ice * ( fc_bo_i(ji) + fbif_1d(ji) + qlbbq_1d(ji) ) / q_i_b(ji,nlay_i+1) 
-               sfx_thd_1d(ji) = sfx_thd_1d(ji) - s_i_new(ji) * a_i_b(ji) * dh_i_bott(ji) * rhoic * r1_rdtice
-            ENDIF
-         END DO
-      ENDIF
-
-      !-------------------------------------------------
-      ! 4.1 Basal growth - (b) salinity varying in time 
-      !-------------------------------------------------
-      IF(  num_sal == 2  ) THEN
-         ! the growth rate (dh_i_bott) is function of the new ice heat content (q_i_b(nlay_i+1)). 
-         ! q_i_b depends on the new ice salinity (snewice). 
-         ! snewice depends on dh_i_bott ; it converges quickly, so, no problem
-         ! See Vancoppenolle et al., OM08 for more info on this
-
-         ! Initial value (tested 1D, can be anything between 1 and 20)
-         num_iter_max = 4
-         s_i_new(:)   = 4.0
-
-         ! Iterative procedure
-         DO iter = 1, num_iter_max
-            DO ji = kideb, kiut
-               IF(  fc_bo_i(ji) + fbif_1d(ji) + qlbbq_1d(ji) < 0.e0  ) THEN
-                  ii = MOD( npb(ji) - 1, jpi ) + 1
-                  ij = ( npb(ji) - 1 ) / jpi + 1
-                  ! Melting point in K
-                  ztmelts             =   - tmut * s_i_new(ji) + rtt 
-                  ! New ice heat content (Bitz and Lipscomb, 1999)
-                  q_i_b(ji,nlay_i+1)  =  rhoic * (  cpic * ( ztmelts - t_bo_b(ji) )                             &
-                     &                            + lfus * ( 1.0 - ( ztmelts - rtt ) / ( t_bo_b(ji) - rtt ) )   &
-                     &                            - rcp * ( ztmelts-rtt )                                     )
-                  ! Bottom growth rate = - F*dt / q
-                  dh_i_bott(ji) =  - rdt_ice * ( fc_bo_i(ji) + fbif_1d(ji) + qlbbq_1d(ji) ) / q_i_b(ji,nlay_i+1)
-                  ! New ice salinity ( Cox and Weeks, JGR, 1988 )
-                  ! zswi2  (1) if dh_i_bott/rdt .GT. 3.6e-7
-                  ! zswi12 (1) if dh_i_bott/rdt .LT. 3.6e-7 and .GT. 2.0e-8
-                  ! zswi1  (1) if dh_i_bott/rdt .LT. 2.0e-8
-                  zgrr   = MIN( 1.0e-3, MAX ( dh_i_bott(ji) * r1_rdtice , epsi13 ) )
-                  zswi2  = MAX( zzero , SIGN( zone , zgrr - 3.6e-7 ) ) 
-                  zswi12 = MAX( zzero , SIGN( zone , zgrr - 2.0e-8 ) ) * ( 1.0 - zswi2 )
-                  zswi1  = 1. - zswi2 * zswi12 
-                  zfracs = zswi1  * 0.12 + zswi12 * ( 0.8925 + 0.0568 * LOG( 100.0 * zgrr ) )   &
-                     &                   + zswi2  * 0.26 / ( 0.26 + 0.74 * EXP ( - 724300.0 * zgrr ) ) 
-                  zfracs = MIN( 0.5 , zfracs )
-                  s_i_new(ji) = zfracs * sss_m(ii,ij)
-               ENDIF ! fc_bo_i
-            END DO ! ji
-         END DO ! iter
-
-         ! Final values
-         DO ji = kideb, kiut
-            IF( ( fc_bo_i(ji) + fbif_1d(ji) + qlbbq_1d(ji) ) .LT. 0.0  ) THEN
-               ! New ice salinity must not exceed 20 psu
-               s_i_new(ji) = MIN( s_i_new(ji), s_i_max )
-               ! Metling point in K
-               ztmelts     =   - tmut * s_i_new(ji) + rtt 
-               ! New ice heat content (Bitz and Lipscomb, 1999)
-               q_i_b(ji,nlay_i+1)  =  rhoic * (  cpic * ( ztmelts - t_bo_b(ji) )                              &
-                  &                            + lfus * ( 1.0 - ( ztmelts - rtt ) / ( t_bo_b(ji) - rtt ) )    &
-                  &                            - rcp * ( ztmelts - rtt )                                    )
-               ! Basal growth rate = - F*dt / q
-               dh_i_bott(ji)       =  - rdt_ice * ( fc_bo_i(ji) + fbif_1d(ji) + qlbbq_1d(ji) ) / q_i_b(ji,nlay_i+1)
-               ! Salinity update
-               ! entrapment during bottom growth
-               sfx_thd_1d(ji) = sfx_thd_1d(ji) - s_i_new(ji) * a_i_b(ji) * dh_i_bott(ji) * rhoic * r1_rdtice
-            ENDIF ! heat budget
-         END DO
-      ENDIF
+            IF(  zf_tt(ji) < 0._wp  ) THEN
+
+               ! New bottom ice salinity (Cox & Weeks, JGR88 )
+               !--- zswi1  if dh/dt < 2.0e-8
+               !--- zswi12 if 2.0e-8 < dh/dt < 3.6e-7 
+               !--- zswi2  if dh/dt > 3.6e-7
+               zgrr               = MIN( 1.0e-3, MAX ( dh_i_bott(ji) * r1_rdtice , epsi10 ) )
+               zswi2              = MAX( 0._wp , SIGN( 1._wp , zgrr - 3.6e-7 ) )
+               zswi12             = MAX( 0._wp , SIGN( 1._wp , zgrr - 2.0e-8 ) ) * ( 1.0 - zswi2 )
+               zswi1              = 1. - zswi2 * zswi12
+               zfracs             = MIN ( zswi1  * 0.12 + zswi12 * ( 0.8925 + 0.0568 * LOG( 100.0 * zgrr ) )   &
+                  &               + zswi2  * 0.26 / ( 0.26 + 0.74 * EXP ( - 724300.0 * zgrr ) )  , 0.5 )
+
+               ii = MOD( npb(ji) - 1, jpi ) + 1 ; ij = ( npb(ji) - 1 ) / jpi + 1
+
+               s_i_new(ji)        = zswitch_sal * zfracs * sss_m(ii,ij)  &  ! New ice salinity
+                                  + ( 1. - zswitch_sal ) * sm_i_1d(ji) 
+               ! New ice growth
+               ztmelts            = - tmut * s_i_new(ji) + rtt          ! New ice melting point (K)
+
+               zt_i_new           = zswitch_sal * t_bo_1d(ji) + ( 1. - zswitch_sal) * t_i_1d(ji, nlay_i)
+               
+               zEi                = cpic * ( zt_i_new - ztmelts ) &     ! Specific enthalpy of forming ice (J/kg, <0)      
+                  &               - lfus * ( 1.0 - ( ztmelts - rtt ) / ( zt_i_new - rtt ) )   &
+                  &               + rcp  * ( ztmelts-rtt )          
+
+               zEw                = rcp  * ( t_bo_1d(ji) - rt0 )         ! Specific enthalpy of seawater (J/kg, < 0)
+
+               zdE                = zEi - zEw                           ! Specific enthalpy difference (J/kg, <0)
+
+               dh_i_bott(ji)      = rdt_ice * MAX( 0._wp , zf_tt(ji) / ( zdE * rhoic ) )
+
+               q_i_1d(ji,nlay_i+1) = -zEi * rhoic                        ! New ice energy of melting (J/m3, >0)
+               
+            ENDIF ! fc_bo_i
+         END DO ! ji
+      END DO ! iter
+
+      ! Contribution to Energy and Salt Fluxes
+      DO ji = kideb, kiut
+         IF(  zf_tt(ji) < 0._wp  ) THEN
+            ! New ice growth
+                                    
+            zfmdt          = - rhoic * dh_i_bott(ji)             ! Mass flux x time step (kg/m2, < 0)
+
+            ztmelts        = - tmut * s_i_new(ji) + rtt          ! New ice melting point (K)
+            
+            zt_i_new       = zswitch_sal * t_bo_1d(ji) + ( 1. - zswitch_sal) * t_i_1d(ji, nlay_i)
+            
+            zEi            = cpic * ( zt_i_new - ztmelts ) &     ! Specific enthalpy of forming ice (J/kg, <0)      
+               &               - lfus * ( 1.0 - ( ztmelts - rtt ) / ( zt_i_new - rtt ) )   &
+               &               + rcp  * ( ztmelts-rtt )          
+            
+            zEw            = rcp  * ( t_bo_1d(ji) - rt0 )         ! Specific enthalpy of seawater (J/kg, < 0)
+            
+            zdE            = zEi - zEw                           ! Specific enthalpy difference (J/kg, <0)
+            
+            ! Contribution to heat flux to the ocean [W.m-2], >0  
+            hfx_thd_1d(ji) = hfx_thd_1d(ji) + zfmdt * a_i_1d(ji) * zEw * r1_rdtice
+
+            ! Total heat flux used in this process [W.m-2], <0  
+            hfx_bog_1d(ji) = hfx_bog_1d(ji) - zfmdt * a_i_1d(ji) * zdE * r1_rdtice
+            
+            ! Contribution to salt flux, <0
+            sfx_bog_1d(ji) = sfx_bog_1d(ji) + s_i_new(ji) * a_i_1d(ji) * zfmdt * r1_rdtice
+
+            ! Contribution to mass flux, <0
+            wfx_bog_1d(ji) =  wfx_bog_1d(ji) - rhoic * a_i_1d(ji) * dh_i_bott(ji) * r1_rdtice
+
+            ! update heat content (J.m-2) and layer thickness
+            qh_i_old(ji,nlay_i+1) = qh_i_old(ji,nlay_i+1) + dh_i_bott(ji) * q_i_1d(ji,nlay_i+1)
+            h_i_old (ji,nlay_i+1) = h_i_old (ji,nlay_i+1) + dh_i_bott(ji)
+         ENDIF
+      END DO
 
       !----------------
       ! 4.2 Basal melt
       !----------------
-      meance_dh = 0._wp
-      numce_dh  = 0
-      zinnermelt(:) = 0._wp
-
-      DO ji = kideb, kiut
-         ! heat convergence at the surface > 0
-         IF(  ( fc_bo_i(ji) + fbif_1d(ji) + qlbbq_1d(ji) ) >= 0._wp  ) THEN
-            s_i_new(ji)   =  s_i_b(ji,nlay_i)
-            zqfont_bo(ji) =  rdt_ice * ( fc_bo_i(ji) + fbif_1d(ji) + qlbbq_1d(ji) )
-            zfbase(ji)    =  zqfont_bo(ji) * r1_rdtice     ! heat conservation test
-            zdq_i(ji)     =  0._wp
-            dh_i_bott(ji) =  0._wp
-         ENDIF
-      END DO
-
+      zdeltah(:,:) = 0._wp ! important
       DO jk = nlay_i, 1, -1
          DO ji = kideb, kiut
-            IF(  fc_bo_i(ji) + fbif_1d(ji) + qlbbq_1d(ji)  >=  0._wp  ) THEN
-               ztmelts = - tmut * s_i_b(ji,jk) + rtt 
-               IF( t_i_b(ji,jk) >= ztmelts ) THEN   !!gm : a comment is needed
-                  zdeltah   (ji,jk) = - zh_i(ji)
-                  dh_i_bott (ji   ) = dh_i_bott(ji) + zdeltah(ji,jk)
-                  zinnermelt(ji   ) = 1._wp
-               ELSE                                  ! normal ablation
-                  zdeltah  (ji,jk) = - zqfont_bo(ji) / q_i_b(ji,jk)
-                  zqfont_bo(ji   ) = MAX( 0.0 , - zh_i(ji) - zdeltah(ji,jk) ) * q_i_b(ji,jk)
-                  zdeltah  (ji,jk) = MAX(zdeltah(ji,jk), - zh_i(ji) )
-                  dh_i_bott(ji   ) = dh_i_bott(ji) + zdeltah(ji,jk)
-                  zdq_i    (ji   ) = zdq_i(ji) + zdeltah(ji,jk) * q_i_b(ji,jk) * r1_rdtice
+            IF(  zf_tt(ji)  >=  0._wp  .AND. jk > icount(ji) ) THEN   ! do not calculate where layer has already disappeared from surface melting 
+
+               ztmelts = - tmut * s_i_1d(ji,jk) + rtt  ! Melting point of layer jk (K)
+
+               IF( t_i_1d(ji,jk) >= ztmelts ) THEN !!! Internal melting
+
+                  zEi               = - q_i_1d(ji,jk) / rhoic        ! Specific enthalpy of melting ice (J/kg, <0)
+
+                  !!zEw               = rcp * ( t_i_1d(ji,jk) - rtt )  ! Specific enthalpy of meltwater at T = t_i_1d (J/kg, <0)
+
+                  zdE               = 0._wp                         ! Specific enthalpy difference   (J/kg, <0)
+                                                                    ! set up at 0 since no energy is needed to melt water...(it is already melted)
+
+                  zdeltah   (ji,jk) = MIN( 0._wp , - zh_i(ji,jk) ) ! internal melting occurs when the internal temperature is above freezing     
+                                                                   ! this should normally not happen, but sometimes, heat diffusion leads to this
+
+                  dh_i_bott (ji)    = dh_i_bott(ji) + zdeltah(ji,jk)
+
+                  zfmdt             = - zdeltah(ji,jk) * rhoic          ! Mass flux x time step > 0
+
+                  ! Contribution to heat flux to the ocean [W.m-2], <0 (ice enthalpy zEi is "sent" to the ocean) 
+                  hfx_res_1d(ji) = hfx_res_1d(ji) + zfmdt * a_i_1d(ji) * zEi * r1_rdtice
+
+                  ! Contribution to salt flux (clem: using sm_i_1d and not s_i_1d(jk) is ok)
+                  sfx_res_1d(ji) = sfx_res_1d(ji) - sm_i_1d(ji) * a_i_1d(ji) * zdeltah(ji,jk) * rhoic * r1_rdtice
+                                    
+                  ! Contribution to mass flux
+                  wfx_res_1d(ji) =  wfx_res_1d(ji) - rhoic * a_i_1d(ji) * zdeltah(ji,jk) * r1_rdtice
+
+                  ! update heat content (J.m-2) and layer thickness
+                  qh_i_old(ji,jk) = qh_i_old(ji,jk) + zdeltah(ji,jk) * q_i_1d(ji,jk)
+                  h_i_old (ji,jk) = h_i_old (ji,jk) + zdeltah(ji,jk)
+
+               ELSE                               !!! Basal melting
+
+                  zEi               = - q_i_1d(ji,jk) / rhoic ! Specific enthalpy of melting ice (J/kg, <0)
+
+                  zEw               = rcp * ( ztmelts - rtt )! Specific enthalpy of meltwater (J/kg, <0)
+
+                  zdE               = zEi - zEw              ! Specific enthalpy difference   (J/kg, <0)
+
+                  zfmdt             = - zq_bo(ji) / zdE  ! Mass flux x time step (kg/m2, >0)
+
+                  zdeltah(ji,jk)    = - zfmdt / rhoic        ! Gross thickness change
+
+                  zdeltah(ji,jk)    = MIN( 0._wp , MAX( zdeltah(ji,jk), - zh_i(ji,jk) ) ) ! bound thickness change
+                  
+                  zq_bo(ji)         = MAX( 0._wp , zq_bo(ji) - zdeltah(ji,jk) * rhoic * zdE ) ! update available heat. MAX is necessary for roundup errors
+
+                  dh_i_bott(ji)     = dh_i_bott(ji) + zdeltah(ji,jk)    ! Update basal melt
+
+                  zfmdt             = - zdeltah(ji,jk) * rhoic          ! Mass flux x time step > 0
+
+                  zQm               = zfmdt * zEw         ! Heat exchanged with ocean
+
+                  ! Contribution to heat flux to the ocean [W.m-2], <0  
+                  hfx_thd_1d(ji) = hfx_thd_1d(ji) + zfmdt * a_i_1d(ji) * zEw * r1_rdtice
+
+                  ! Contribution to salt flux (clem: using sm_i_1d and not s_i_1d(jk) is ok)
+                  sfx_bom_1d(ji) = sfx_bom_1d(ji) - sm_i_1d(ji) * a_i_1d(ji) * zdeltah(ji,jk) * rhoic * r1_rdtice
+                  
+                  ! Total heat flux used in this process [W.m-2], >0  
+                  hfx_bom_1d(ji) = hfx_bom_1d(ji) - zfmdt * a_i_1d(ji) * zdE * r1_rdtice
+                  
+                  ! Contribution to mass flux
+                  wfx_bom_1d(ji) =  wfx_bom_1d(ji) - rhoic * a_i_1d(ji) * zdeltah(ji,jk) * r1_rdtice
+
+                  ! update heat content (J.m-2) and layer thickness
+                  qh_i_old(ji,jk) = qh_i_old(ji,jk) + zdeltah(ji,jk) * q_i_1d(ji,jk)
+                  h_i_old (ji,jk) = h_i_old (ji,jk) + zdeltah(ji,jk)
                ENDIF
-               ! clem: contribution to salt flux
-               sfx_thd_1d(ji) = sfx_thd_1d(ji) - sm_i_b(ji) * a_i_b(ji)    &
-                    &                              * MIN( zdeltah(ji,jk) , 0._wp ) * rhoic * r1_rdtice
+           
             ENDIF
          END DO ! ji
       END DO ! jk
 
-      !                                          !-------------------
-      IF( con_i .AND. jiindex_1d > 0 ) THEN      ! Conservation test
-      !                                          !-------------------
-         DO ji = kideb, kiut
-            IF(  ( fc_bo_i(ji) + fbif_1d(ji) + qlbbq_1d(ji) ) >= 0.e0  ) THEN
-               IF( ( zfbase(ji) + zdq_i(ji) ) >= 1.e-3 ) THEN
-                  numce_dh  = numce_dh + 1
-                  meance_dh = meance_dh + zfbase(ji) + zdq_i(ji)
-               ENDIF
-               IF ( zfbase(ji) + zdq_i(ji) .GE. 1.0e-3  ) THEN
-                  WRITE(numout,*) ' ALERTE heat loss for basal melt : ii, ij, jl :', ii, ij, jl
-                  WRITE(numout,*) ' ht_i_b    : ', ht_i_b(ji)
-                  WRITE(numout,*) ' zfbase    : ', zfbase(ji)
-                  WRITE(numout,*) ' zdq_i     : ', zdq_i(ji)
-                  WRITE(numout,*) ' ht_i_b    : ', ht_i_b(ji)
-                  WRITE(numout,*) ' fc_bo_i   : ', fc_bo_i(ji)
-                  WRITE(numout,*) ' fbif_1d   : ', fbif_1d(ji)
-                  WRITE(numout,*) ' qlbbq_1d  : ', qlbbq_1d(ji)
-                  WRITE(numout,*) ' s_i_new   : ', s_i_new(ji)
-                  WRITE(numout,*) ' sss_m     : ', sss_m(ii,ij)
-                  WRITE(numout,*) ' dh_i_bott : ', dh_i_bott(ji)
-                  WRITE(numout,*) ' innermelt : ', INT( zinnermelt(ji) )
-               ENDIF
-            ENDIF
-         END DO
-         IF( numce_dh > 0 )   meance_dh = meance_dh / numce_dh
-         WRITE(numout,*) ' Number of points where there is bas. me. error : ', numce_dh
-         WRITE(numout,*) ' Mean basal melt error on error points : ', meance_dh
-         WRITE(numout,*) ' Remaining bottom heat : ', zqfont_bo(jiindex_1d)
-         !
-      ENDIF
-
-      !
       !------------------------------------------------------------------------------!
-      !  5) Pathological cases                                                       !
+      ! Excessive ablation in a 1-category model
+      !     in a 1-category sea ice model, bottom ablation must not exceed hmelt (-0.15)
       !------------------------------------------------------------------------------!
-      !
-      !----------------------------------------------
-      ! 5.1 Excessive ablation in a 1-category model
-      !----------------------------------------------
-
-      DO ji = kideb, kiut
-         !                     ! in a 1-category sea ice model, bottom ablation must not exceed hmelt (-0.15)
-         IF( jpl == 1 ) THEN   ;   zdhbf = MAX( hmelt , dh_i_bott(ji) )
-         ELSE                  ;   zdhbf =              dh_i_bott(ji) 
-         ENDIF
-         zdvres        = zdhbf - dh_i_bott(ji)
-         dh_i_bott(ji) = zdhbf
-         sfx_thd_1d(ji)  = sfx_thd_1d(ji) - sm_i_b(ji) * a_i_b(ji) * zdvres * rhoic * r1_rdtice
-         !                     ! excessive energy is sent to lateral ablation
-         zinda = MAX( 0._wp, SIGN( 1._wp , 1.0 - at_i_b(ji) - epsi10 ) )
-         fsup(ji) =  zinda * rhoic * lfus * at_i_b(ji) / MAX( 1.0 - at_i_b(ji) , epsi10 ) * zdvres * r1_rdtice
-      END DO
-
-      !-----------------------------------
-      ! 5.2 More than available ice melts
-      !-----------------------------------
-      ! then heat applied minus heat content at previous time step should equal heat remaining 
-      !
-      DO ji = kideb, kiut
-         ! Adapt the remaining energy if too much ice melts
-         !--------------------------------------------------
-         zdvres     = MAX( 0._wp, - ht_i_b(ji) - dh_i_surf(ji) - dh_i_bott(ji) )
-         zdvsur     = MIN( 0._wp, dh_i_surf(ji) + zdvres ) - dh_i_surf(ji) ! fill the surface first
-         zdvbot     = MAX( 0._wp, zdvres - zdvsur ) ! then the bottom
-         dh_i_surf (ji) = dh_i_surf(ji) + zdvsur ! clem
-         dh_i_bott (ji) = dh_i_bott(ji) + zdvbot ! clem
-
-         ! new ice thickness (clem)
-         zhgnew(ji) = ht_i_b(ji) + dh_i_surf(ji) + dh_i_bott(ji)
-         zihgnew    = 1.0 - MAX( zzero , SIGN( zone , - zhgnew(ji) ) ) !1 if ice
-         zhgnew(ji) = zihgnew * zhgnew(ji)      ! ice thickness is put to 0
- 
-         !                     !since ice volume is only used for outputs, we keep it global for all categories
-         dvbbq_1d (ji) = a_i_b(ji) * dh_i_bott(ji)
-
-        ! remaining heat
-         zfdt_final(ji) = ( 1.0 - zihgnew ) * ( zqfont_su(ji) +  zqfont_bo(ji) ) 
-
-         ! If snow remains, energy is used to melt snow
-         zhni =  ht_s_b(ji)      ! snow depth at previous time step
-         zihg =  MAX(  zzero , SIGN ( zone , - ht_s_b(ji) )  )   ! =0 if snow 
-
-         ! energy of melting of remaining snow
-         zinda = MAX( 0._wp, SIGN( 1._wp , zhni - epsi10 ) )
-         zqt_s(ji) =    ( 1. - zihg ) * zqt_s(ji) / MAX( zhni, epsi10 ) * zinda
-         zdhnm     =  - ( 1. - zihg ) * ( 1. - zihgnew ) * zfdt_final(ji) / MAX( zqt_s(ji) , epsi13 )
-         zhnfi     =  zhni + zdhnm
-         zfdt_final(ji) =  MAX( zfdt_final(ji) + zqt_s(ji) * zdhnm , 0.0 )
-         ht_s_b(ji)     =  MAX( zzero , zhnfi )
-         zqt_s(ji)      =  zqt_s(ji) * ht_s_b(ji)
-         ! we recompute dh_s_tot (clem)
-         dh_s_tot (ji)  =  ht_s_b(ji) - zhsold(ji)
-
-         ! Mass variations of ice and snow
-         !---------------------------------
-         !                                              ! mass variation of the jl category
-         zzfmass_s = - a_i_b(ji) * ( zhni       - ht_s_b(ji) ) * rhosn   ! snow
-         zzfmass_i =   a_i_b(ji) * ( zhgnew(ji) - ht_i_b(ji) ) * rhoic   ! ice  
-         !
-         zfmass_i(ji) = zzfmass_i                       ! ice variation saved to compute salt flux (see below)
-         !
-         !                                              ! mass variation cumulated over category
-         !clem rdm_snw_1d(ji) = rdm_snw_1d(ji) + zzfmass_s                     ! snow 
-         !clem rdm_ice_1d(ji) = rdm_ice_1d(ji) + zzfmass_i                     ! ice 
-
-         ! Remaining heat to the ocean 
-         !---------------------------------
-         focea(ji)  = - zfdt_final(ji) * r1_rdtice         ! focea is in W.m-2 * dt
-
-         ! residual salt flux (clem)
-         !--------------------------
-         ! surface
-         sfx_thd_1d(ji)    = sfx_thd_1d(ji) - sm_i_b(ji)  * a_i_b(ji) * zdvsur * rhoic * r1_rdtice
-         ! bottom
-         IF ( fc_bo_i(ji) + fbif_1d(ji) + qlbbq_1d(ji) >= 0._wp ) THEN ! melting
-            sfx_thd_1d(ji) = sfx_thd_1d(ji) - sm_i_b(ji)  * a_i_b(ji) * zdvbot * rhoic * r1_rdtice
-         ELSE                                                          ! growth
-            sfx_thd_1d(ji) = sfx_thd_1d(ji) - s_i_new(ji) * a_i_b(ji) * zdvbot * rhoic * r1_rdtice
-         ENDIF
-         !
-         ! diagnostic 
-         ii = MOD( npb(ji) - 1, jpi ) + 1
-         ij = ( npb(ji) - 1 ) / jpi + 1
-         diag_bot_gr(ii,ij) = diag_bot_gr(ii,ij) + MAX(dh_i_bott(ji),0.0)*a_i_b(ji) * r1_rdtice
-         diag_sur_me(ii,ij) = diag_sur_me(ii,ij) + MIN(dh_i_surf(ji),0.0)*a_i_b(ji) * r1_rdtice
-         diag_bot_me(ii,ij) = diag_bot_me(ii,ij) + MIN(dh_i_bott(ji),0.0)*a_i_b(ji) * r1_rdtice
-      END DO
-
-      ftotal_fin (:) = zfdt_final(:)  * r1_rdtice
-
-      !---------------------------
-      ! heat fluxes                    
-      !---------------------------
-      DO ji = kideb, kiut
-         zihgnew    =  1.0 - MAX( zzero , SIGN( zone , - zhgnew(ji) ) )   ! =1 if ice
-         !
-         ! Heat flux
-         ! excessive bottom ablation energy (fsup) - 0 except if jpl = 1
-         ! excessive total  ablation energy (focea) sent to the ocean
-         qfvbq_1d(ji)  = qfvbq_1d(ji) + fsup(ji) + ( 1.0 - zihgnew ) * focea(ji) * a_i_b(ji) * rdt_ice
-
-         zihic   = 1.0 - MAX(  zzero , SIGN( zone , -ht_i_b(ji) )  )      ! equals 0 if ht_i = 0, 1 if ht_i gt 0
-         fscbq_1d(ji) =  a_i_b(ji) * fstbif_1d(ji)
-         qldif_1d(ji)  = qldif_1d(ji) + fsup(ji) + ( 1.0 - zihgnew ) * focea   (ji) * a_i_b(ji) * rdt_ice   &
-            &                                    + ( 1.0 - zihic   ) * fscbq_1d(ji)             * rdt_ice
-      END DO  ! ji
-
-      !-------------------------------------------
-      ! Correct temperature, energy and thickness
-      !-------------------------------------------
-      DO ji = kideb, kiut
-         zihgnew    =  1.0 - MAX( zzero , SIGN( zone , - zhgnew(ji) ) ) 
-         t_su_b(ji) =  zihgnew * t_su_b(ji) + ( 1.0 - zihgnew ) * rtt
-      END DO  ! ji
-
-      DO jk = 1, nlay_i
-         DO ji = kideb, kiut
-            zihgnew      =  1.0 - MAX( zzero , SIGN( zone , - zhgnew(ji) ) ) 
-            t_i_b(ji,jk) =  zihgnew * t_i_b(ji,jk) + ( 1.0 - zihgnew ) * rtt
-            q_i_b(ji,jk) =  zihgnew * q_i_b(ji,jk)
-         END DO
-      END DO  ! ji
-
-      DO ji = kideb, kiut
-         ht_i_b(ji) = zhgnew(ji)
-      END DO  ! ji
+      ! ??? keep ???
+      ! clem bug: I think this should be included above, so we would not have to 
+      !           track heat/salt/mass fluxes backwards
+!      IF( jpl == 1 ) THEN
+!         DO ji = kideb, kiut
+!            IF(  zf_tt(ji)  >=  0._wp  ) THEN
+!               zdh            = MAX( hmelt , dh_i_bott(ji) )
+!               zdvres         = zdh - dh_i_bott(ji) ! >=0
+!               dh_i_bott(ji)  = zdh
+!
+!               ! excessive energy is sent to lateral ablation
+!               rswitch = MAX( 0._wp, SIGN( 1._wp , 1._wp - at_i_1d(ji) - epsi20 ) )
+!               zq_1cat(ji) =  rswitch * rhoic * lfus * at_i_1d(ji) / MAX( 1._wp - at_i_1d(ji) , epsi20 ) * zdvres ! J.m-2 >=0
+!
+!               ! correct salt and mass fluxes
+!               sfx_bom_1d(ji) = sfx_bom_1d(ji) - sm_i_1d(ji) * a_i_1d(ji) * zdvres * rhoic * r1_rdtice ! this is only a raw approximation
+!               wfx_bom_1d(ji) = wfx_bom_1d(ji) - rhoic * a_i_1d(ji) * zdvres * r1_rdtice
+!            ENDIF
+!         END DO
+!      ENDIF
+
+      !-------------------------------------------
+      ! Update temperature, energy
+      !-------------------------------------------
+      DO ji = kideb, kiut
+         ht_i_1d(ji) =  MAX( 0._wp , ht_i_1d(ji) + dh_i_bott(ji) )
+      END DO  
+
+      !-------------------------------------------
+      ! 5. What to do with remaining energy
+      !-------------------------------------------
+      ! If heat still available for melting and snow remains, then melt more snow
+      !-------------------------------------------
+      zdeltah(:,:) = 0._wp ! important
+      DO ji = kideb, kiut
+         zq_rema(ji)     = zq_su(ji) + zq_bo(ji) 
+!         zindh           = 1._wp - MAX( 0._wp, SIGN( 1._wp, - ht_s_1d(ji) ) )   ! =1 if snow
+!         zindq           = 1._wp - MAX( 0._wp, SIGN( 1._wp, - zq_s(ji) + epsi20 ) )
+!         zdeltah  (ji,1) = - zindh * zindq * zq_rema(ji) / MAX( zq_s(ji), epsi20 )
+!         zdeltah  (ji,1) = MIN( 0._wp , MAX( zdeltah(ji,1) , - ht_s_1d(ji) ) ) ! bound melting
+!         zdh_s_mel(ji)   = zdh_s_mel(ji) + zdeltah(ji,1)    
+!         dh_s_tot (ji)   = dh_s_tot(ji) + zdeltah(ji,1)
+!         ht_s_1d   (ji)   = ht_s_1d(ji)   + zdeltah(ji,1)
+!        
+!         zq_rema(ji)     = zq_rema(ji) + zdeltah(ji,1) * zq_s(ji)                ! update available heat (J.m-2)
+!         ! heat used to melt snow
+!         hfx_snw_1d(ji)  = hfx_snw_1d(ji) - zdeltah(ji,1) * a_i_1d(ji) * zq_s(ji) * r1_rdtice ! W.m-2 (>0)
+!         ! Contribution to mass flux
+!         wfx_snw_1d(ji)  =  wfx_snw_1d(ji) - rhosn * a_i_1d(ji) * zdeltah(ji,1) * r1_rdtice
+!    
+         ii = MOD( npb(ji) - 1, jpi ) + 1 ; ij = ( npb(ji) - 1 ) / jpi + 1
+         ! Remaining heat flux (W.m-2) is sent to the ocean heat budget
+         hfx_out(ii,ij)  = hfx_out(ii,ij) + ( zq_1cat(ji) + zq_rema(ji) * a_i_1d(ji) ) * r1_rdtice
+
+         IF( ln_nicep .AND. zq_rema(ji) < 0. .AND. lwp ) WRITE(numout,*) 'ALERTE zq_rema <0 = ', zq_rema(ji)
+      END DO
+      
       !
       !------------------------------------------------------------------------------|
@@ -670,63 +648,77 @@
       DO ji = kideb, kiut
          !
-         dh_snowice(ji) = MAX(  zzero , ( rhosn * ht_s_b(ji) + (rhoic-rau0) * ht_i_b(ji) ) / ( rhosn+rau0-rhoic )  )
-         zhgnew(ji)     = MAX(  zhgnew(ji) , zhgnew(ji) + dh_snowice(ji)  )
-         zhnnew         = MIN(  ht_s_b(ji) , ht_s_b(ji) - dh_snowice(ji)  )
-
-         !  Changes in ice volume and ice mass.
-         dvnbq_1d  (ji) =                a_i_b(ji) * ( zhgnew(ji)-ht_i_b(ji) )
-         dmgwi_1d  (ji) = dmgwi_1d(ji) + a_i_b(ji) * ( ht_s_b(ji) - zhnnew ) * rhosn
-
-         !clem rdm_ice_1d(ji) = rdm_ice_1d(ji) + a_i_b(ji) * ( zhgnew(ji) - ht_i_b(ji) ) * rhoic 
-         !clem rdm_snw_1d(ji) = rdm_snw_1d(ji) + a_i_b(ji) * ( zhnnew     - ht_s_b(ji) ) * rhosn 
-
-         !        Equivalent salt flux (1) Snow-ice formation component
-         !        -----------------------------------------------------
-         ii = MOD( npb(ji) - 1, jpi ) + 1
-         ij =    ( npb(ji) - 1 ) / jpi + 1
-
-         IF( num_sal == 2 ) THEN   ;   zsm_snowice = sss_m(ii,ij) * ( rhoic - rhosn ) / rhoic
-         ELSE                      ;   zsm_snowice = sm_i_b(ji)   
-         ENDIF
+         dh_snowice(ji) = MAX(  0._wp , ( rhosn * ht_s_1d(ji) + (rhoic-rau0) * ht_i_1d(ji) ) / ( rhosn+rau0-rhoic )  )
+
+         ht_i_1d(ji)     = ht_i_1d(ji) + dh_snowice(ji)
+         ht_s_1d(ji)     = ht_s_1d(ji) - dh_snowice(ji)
+
+         ! Salinity of snow ice
+         ii = MOD( npb(ji) - 1, jpi ) + 1 ; ij = ( npb(ji) - 1 ) / jpi + 1
+         zs_snic = zswitch_sal * sss_m(ii,ij) * ( rhoic - rhosn ) / rhoic + ( 1. - zswitch_sal ) * sm_i_1d(ji)
+
          ! entrapment during snow ice formation
-         ! clem: new salinity difference stored (to be used in limthd_ent.F90)
+         ! new salinity difference stored (to be used in limthd_ent.F90)
          IF (  num_sal == 2  ) THEN
-            i_ice_switch = MAX( 0._wp , SIGN( 1._wp , zhgnew(ji) - epsi10 ) )
+            rswitch = MAX( 0._wp , SIGN( 1._wp , ht_i_1d(ji) - epsi10 ) )
             ! salinity dif due to snow-ice formation
-            dsm_i_si_1d(ji) = ( zsm_snowice - sm_i_b(ji) ) * dh_snowice(ji) / MAX( zhgnew(ji), epsi10 ) * i_ice_switch     
+            dsm_i_si_1d(ji) = ( zs_snic - sm_i_1d(ji) ) * dh_snowice(ji) / MAX( ht_i_1d(ji), epsi10 ) * rswitch     
             ! salinity dif due to bottom growth 
-            IF (  fc_bo_i(ji) + fbif_1d(ji) + qlbbq_1d(ji)  < 0._wp ) THEN
-               dsm_i_se_1d(ji) = ( s_i_new(ji) - sm_i_b(ji) ) * dh_i_bott(ji) / MAX( zhgnew(ji), epsi10 ) * i_ice_switch
+            IF (  zf_tt(ji)  < 0._wp ) THEN
+               dsm_i_se_1d(ji) = ( s_i_new(ji) - sm_i_1d(ji) ) * dh_i_bott(ji) / MAX( ht_i_1d(ji), epsi10 ) * rswitch
             ENDIF
          ENDIF
 
-         !  Actualize new snow and ice thickness.
-         ht_s_b(ji)  = zhnnew
-         ht_i_b(ji)  = zhgnew(ji)
-
-         ! Total ablation ! new lines added to debug
-         IF( ht_i_b(ji) <= 0._wp )   a_i_b(ji) = 0._wp
-
-         ! diagnostic ( snow ice growth )
-         ii = MOD( npb(ji) - 1, jpi ) + 1
-         ij =    ( npb(ji) - 1 ) / jpi + 1
-         diag_sni_gr(ii,ij)  = diag_sni_gr(ii,ij) + dh_snowice(ji)*a_i_b(ji) * r1_rdtice
-         !
-         ! salt flux
-         sfx_thd_1d(ji) = sfx_thd_1d(ji) - zsm_snowice * a_i_b(ji) * dh_snowice(ji) * rhoic * r1_rdtice
-         !--------------------------------
-         ! Update mass fluxes (clem)
-         !--------------------------------
-         rdm_ice_1d(ji) = rdm_ice_1d(ji) + ( a_i_b(ji) * ht_i_b(ji) - zviold(ji) ) * rhoic 
-         rdm_snw_1d(ji) = rdm_snw_1d(ji) + ( a_i_b(ji) * ht_s_b(ji) - zvsold(ji) ) * rhosn 
+         ! Contribution to energy flux to the ocean [J/m2], >0 (if sst<0)
+         ii = MOD( npb(ji) - 1, jpi ) + 1 ; ij = ( npb(ji) - 1 ) / jpi + 1
+         zfmdt          = ( rhosn - rhoic ) * MAX( dh_snowice(ji), 0._wp )    ! <0
+         zsstK          = sst_m(ii,ij) + rt0                                
+         zEw            = rcp * ( zsstK - rt0 )
+         zQm            = zfmdt * zEw 
+         
+         ! Contribution to heat flux
+         hfx_thd_1d(ji) = hfx_thd_1d(ji) + zfmdt * a_i_1d(ji) * zEw * r1_rdtice 
+
+         ! Contribution to salt flux
+         sfx_sni_1d(ji) = sfx_sni_1d(ji) + sss_m(ii,ij) * a_i_1d(ji) * zfmdt * r1_rdtice 
+          
+         ! Contribution to mass flux
+         ! All snow is thrown in the ocean, and seawater is taken to replace the volume
+         wfx_sni_1d(ji) = wfx_sni_1d(ji) - a_i_1d(ji) * dh_snowice(ji) * rhoic * r1_rdtice
+         wfx_snw_1d(ji) = wfx_snw_1d(ji) + a_i_1d(ji) * dh_snowice(ji) * rhosn * r1_rdtice
+
+         ! update heat content (J.m-2) and layer thickness
+         qh_i_old(ji,0) = qh_i_old(ji,0) + dh_snowice(ji) * q_s_1d(ji,1) + zfmdt * zEw
+         h_i_old (ji,0) = h_i_old (ji,0) + dh_snowice(ji)
+         
+         ! Total ablation (to debug)
+         IF( ht_i_1d(ji) <= 0._wp )   a_i_1d(ji) = 0._wp
 
       END DO !ji
-      !
-      CALL wrk_dealloc( jpij, zh_i, zh_s, ztfs, zhsold, zqprec, zqfont_su, zqfont_bo, z_f_surf, zhgnew, zfmass_i )
-      CALL wrk_dealloc( jpij, zdh_s_mel, zdh_s_pre, zdh_s_sub, zfdt_init, zfdt_final, zqt_i, zqt_s, zqt_dummy )
-      CALL wrk_dealloc( jpij, zinnermelt, zfbase, zdq_i )
-      CALL wrk_dealloc( jpij, jkmax, zdeltah, zqt_i_lay )
-      !
-      CALL wrk_dealloc( jpij, zviold, zvsold ) ! clem
+
+      !
+      !-------------------------------------------
+      ! Update temperature, energy
+      !-------------------------------------------
+      !clem bug: we should take snow into account here
+      DO ji = kideb, kiut
+         rswitch     =  1.0 - MAX( 0._wp , SIGN( 1._wp , - ht_i_1d(ji) ) ) 
+         t_su_1d(ji) =  rswitch * t_su_1d(ji) + ( 1.0 - rswitch ) * rtt
+      END DO  ! ji
+
+      DO jk = 1, nlay_s
+         DO ji = kideb,kiut
+            ! mask enthalpy
+            rswitch       =  MAX(  0._wp , SIGN( 1._wp, - ht_s_1d(ji) )  )
+            q_s_1d(ji,jk) = ( 1.0 - rswitch ) * q_s_1d(ji,jk)
+            ! recalculate t_s_1d from q_s_1d
+            t_s_1d(ji,jk) = rtt + ( 1._wp - rswitch ) * ( - q_s_1d(ji,jk) / ( rhosn * cpic ) + lfus / cpic )
+         END DO
+      END DO
+
+      CALL wrk_dealloc( jpij, zh_s, zqprec, zq_su, zq_bo, zf_tt, zq_1cat, zq_rema )
+      CALL wrk_dealloc( jpij, zdh_s_mel, zdh_s_pre, zdh_s_sub, zqh_i, zqh_s, zq_s )
+      CALL wrk_dealloc( jpij, nlay_i+1, zdeltah, zh_i )
+      CALL wrk_dealloc( jpij, icount )
+      !
       !
    END SUBROUTINE lim_thd_dh
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd_dif.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd_dif.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd_dif.F90	(revision 5038)
@@ -25,4 +25,5 @@
    USE wrk_nemo       ! work arrays
    USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
+   USE sbc_oce, ONLY : lk_cpl
 
    IMPLICIT NONE
@@ -31,5 +32,4 @@
    PUBLIC   lim_thd_dif   ! called by lim_thd
 
-   REAL(wp) ::   epsi10      =  1.e-10_wp    !
    !!----------------------------------------------------------------------
    !! NEMO/LIM3 4.0 , UCL - NEMO Consortium (2011)
@@ -39,5 +39,5 @@
 CONTAINS
 
-   SUBROUTINE lim_thd_dif( kideb , kiut , jl )
+   SUBROUTINE lim_thd_dif( kideb , kiut )
       !!------------------------------------------------------------------
       !!                ***  ROUTINE lim_thd_dif  ***
@@ -74,10 +74,10 @@
       !!
       !! ** Inputs / Ouputs : (global commons)
-      !!           surface temperature : t_su_b
-      !!           ice/snow temperatures   : t_i_b, t_s_b
-      !!           ice salinities          : s_i_b
+      !!           surface temperature : t_su_1d
+      !!           ice/snow temperatures   : t_i_1d, t_s_1d
+      !!           ice salinities          : s_i_1d
       !!           number of layers in the ice/snow: nlay_i, nlay_s
       !!           profile of the ice/snow layers : z_i, z_s
-      !!           total ice/snow thickness : ht_i_b, ht_s_b
+      !!           total ice/snow thickness : ht_i_1d, ht_s_1d
       !!
       !! ** External : 
@@ -91,7 +91,5 @@
       !!           (04-2007) Energy conservation tested by M. Vancoppenolle
       !!------------------------------------------------------------------
-      INTEGER , INTENT (in) ::   kideb   ! Start point on which the  the computation is applied
-      INTEGER , INTENT (in) ::   kiut    ! End point on which the  the computation is applied
-      INTEGER , INTENT (in) ::   jl      ! Category number
+      INTEGER , INTENT(in) ::   kideb, kiut   ! Start/End point on which the  the computation is applied
 
       !! * Local variables
@@ -99,65 +97,84 @@
       INTEGER ::   ii, ij      ! temporary dummy loop index
       INTEGER ::   numeq       ! current reference number of equation
-      INTEGER ::   layer       ! vertical dummy loop index 
+      INTEGER ::   jk       ! vertical dummy loop index 
       INTEGER ::   nconv       ! number of iterations in iterative procedure
       INTEGER ::   minnumeqmin, maxnumeqmax
-      INTEGER, DIMENSION(kiut) ::   numeqmin   ! reference number of top equation
-      INTEGER, DIMENSION(kiut) ::   numeqmax   ! reference number of bottom equation
-      INTEGER, DIMENSION(kiut) ::   isnow      ! switch for presence (1) or absence (0) of snow
+      INTEGER, POINTER, DIMENSION(:) ::   numeqmin   ! reference number of top equation
+      INTEGER, POINTER, DIMENSION(:) ::   numeqmax   ! reference number of bottom equation
+      INTEGER, POINTER, DIMENSION(:) ::   isnow      ! switch for presence (1) or absence (0) of snow
       REAL(wp) ::   zg1s      =  2._wp        ! for the tridiagonal system
       REAL(wp) ::   zg1       =  2._wp        !
       REAL(wp) ::   zgamma    =  18009._wp    ! for specific heat
       REAL(wp) ::   zbeta     =  0.117_wp     ! for thermal conductivity (could be 0.13)
-      REAL(wp) ::   zraext_s  =  1.e+8_wp     ! extinction coefficient of radiation in the snow
+      REAL(wp) ::   zraext_s  =  10._wp       ! extinction coefficient of radiation in the snow
       REAL(wp) ::   zkimin    =  0.10_wp      ! minimum ice thermal conductivity
+      REAL(wp) ::   ztsu_err  =  1.e-5_wp     ! range around which t_su is considered as 0°C 
       REAL(wp) ::   ztmelt_i    ! ice melting temperature
       REAL(wp) ::   zerritmax   ! current maximal error on temperature 
-      REAL(wp), DIMENSION(kiut) ::   ztfs        ! ice melting point
-      REAL(wp), DIMENSION(kiut) ::   ztsuold     ! old surface temperature (before the iterative procedure )
-      REAL(wp), DIMENSION(kiut) ::   ztsuoldit   ! surface temperature at previous iteration
-      REAL(wp), DIMENSION(kiut) ::   zh_i        ! ice layer thickness
-      REAL(wp), DIMENSION(kiut) ::   zh_s        ! snow layer thickness
-      REAL(wp), DIMENSION(kiut) ::   zfsw        ! solar radiation absorbed at the surface
-      REAL(wp), DIMENSION(kiut) ::   zf          ! surface flux function
-      REAL(wp), DIMENSION(kiut) ::   dzf         ! derivative of the surface flux function
-      REAL(wp), DIMENSION(kiut) ::   zerrit      ! current error on temperature
-      REAL(wp), DIMENSION(kiut) ::   zdifcase    ! case of the equation resolution (1->4)
-      REAL(wp), DIMENSION(kiut) ::   zftrice     ! solar radiation transmitted through the ice
-      REAL(wp), DIMENSION(kiut) ::   zihic, zhsu
-      REAL(wp), DIMENSION(kiut,0:nlay_i) ::   ztcond_i    ! Ice thermal conductivity
-      REAL(wp), DIMENSION(kiut,0:nlay_i) ::   zradtr_i    ! Radiation transmitted through the ice
-      REAL(wp), DIMENSION(kiut,0:nlay_i) ::   zradab_i    ! Radiation absorbed in the ice
-      REAL(wp), DIMENSION(kiut,0:nlay_i) ::   zkappa_i    ! Kappa factor in the ice
-      REAL(wp), DIMENSION(kiut,0:nlay_i) ::   ztiold      ! Old temperature in the ice
-      REAL(wp), DIMENSION(kiut,0:nlay_i) ::   zeta_i      ! Eta factor in the ice
-      REAL(wp), DIMENSION(kiut,0:nlay_i) ::   ztitemp     ! Temporary temperature in the ice to check the convergence
-      REAL(wp), DIMENSION(kiut,0:nlay_i) ::   zspeche_i   ! Ice specific heat
-      REAL(wp), DIMENSION(kiut,0:nlay_i) ::   z_i         ! Vertical cotes of the layers in the ice
-      REAL(wp), DIMENSION(kiut,0:nlay_s) ::   zradtr_s    ! Radiation transmited through the snow
-      REAL(wp), DIMENSION(kiut,0:nlay_s) ::   zradab_s    ! Radiation absorbed in the snow
-      REAL(wp), DIMENSION(kiut,0:nlay_s) ::   zkappa_s    ! Kappa factor in the snow
-      REAL(wp), DIMENSION(kiut,0:nlay_s) ::   zeta_s       ! Eta factor in the snow
-      REAL(wp), DIMENSION(kiut,0:nlay_s) ::   ztstemp      ! Temporary temperature in the snow to check the convergence
-      REAL(wp), DIMENSION(kiut,0:nlay_s) ::   ztsold       ! Temporary temperature in the snow
-      REAL(wp), DIMENSION(kiut,0:nlay_s) ::   z_s          ! Vertical cotes of the layers in the snow
-      REAL(wp), DIMENSION(kiut,jkmax+2) ::   zindterm   ! Independent term
-      REAL(wp), DIMENSION(kiut,jkmax+2) ::   zindtbis   ! temporary independent term
-      REAL(wp), DIMENSION(kiut,jkmax+2) ::   zdiagbis
-      REAL(wp), DIMENSION(kiut,jkmax+2,3) ::   ztrid   ! tridiagonal system terms
+      REAL(wp), POINTER, DIMENSION(:) ::   ztfs        ! ice melting point
+      REAL(wp), POINTER, DIMENSION(:) ::   ztsub       ! old surface temperature (before the iterative procedure )
+      REAL(wp), POINTER, DIMENSION(:) ::   ztsubit     ! surface temperature at previous iteration
+      REAL(wp), POINTER, DIMENSION(:) ::   zh_i        ! ice layer thickness
+      REAL(wp), POINTER, DIMENSION(:) ::   zh_s        ! snow layer thickness
+      REAL(wp), POINTER, DIMENSION(:) ::   zfsw        ! solar radiation absorbed at the surface
+      REAL(wp), POINTER, DIMENSION(:) ::   zf          ! surface flux function
+      REAL(wp), POINTER, DIMENSION(:) ::   dzf         ! derivative of the surface flux function
+      REAL(wp), POINTER, DIMENSION(:) ::   zerrit      ! current error on temperature
+      REAL(wp), POINTER, DIMENSION(:) ::   zdifcase    ! case of the equation resolution (1->4)
+      REAL(wp), POINTER, DIMENSION(:) ::   zftrice     ! solar radiation transmitted through the ice
+      REAL(wp), POINTER, DIMENSION(:) ::   zihic, zhsu
+      REAL(wp), POINTER, DIMENSION(:,:) ::   ztcond_i    ! Ice thermal conductivity
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zradtr_i    ! Radiation transmitted through the ice
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zradab_i    ! Radiation absorbed in the ice
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zkappa_i    ! Kappa factor in the ice
+      REAL(wp), POINTER, DIMENSION(:,:) ::   ztib        ! Old temperature in the ice
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zeta_i      ! Eta factor in the ice
+      REAL(wp), POINTER, DIMENSION(:,:) ::   ztitemp     ! Temporary temperature in the ice to check the convergence
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zspeche_i   ! Ice specific heat
+      REAL(wp), POINTER, DIMENSION(:,:) ::   z_i         ! Vertical cotes of the layers in the ice
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zradtr_s    ! Radiation transmited through the snow
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zradab_s    ! Radiation absorbed in the snow
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zkappa_s    ! Kappa factor in the snow
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zeta_s      ! Eta factor in the snow
+      REAL(wp), POINTER, DIMENSION(:,:) ::   ztstemp     ! Temporary temperature in the snow to check the convergence
+      REAL(wp), POINTER, DIMENSION(:,:) ::   ztsb        ! Temporary temperature in the snow
+      REAL(wp), POINTER, DIMENSION(:,:) ::   z_s         ! Vertical cotes of the layers in the snow
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zswiterm    ! Independent term
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zswitbis    ! temporary independent term
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zdiagbis
+      REAL(wp), POINTER, DIMENSION(:,:,:) ::   ztrid     ! tridiagonal system terms
+      ! diag errors on heat
+      REAL(wp), POINTER, DIMENSION(:) :: zdq, zq_ini, zhfx_err
       !!------------------------------------------------------------------     
       ! 
+      CALL wrk_alloc( jpij, numeqmin, numeqmax, isnow )
+      CALL wrk_alloc( jpij, ztfs, ztsub, ztsubit, zh_i, zh_s, zfsw )
+      CALL wrk_alloc( jpij, zf, dzf, zerrit, zdifcase, zftrice, zihic, zhsu )
+      CALL wrk_alloc( jpij, nlay_i+1, ztcond_i, zradtr_i, zradab_i, zkappa_i, ztib, zeta_i, ztitemp, z_i, zspeche_i, kjstart=0)
+      CALL wrk_alloc( jpij, nlay_s+1,           zradtr_s, zradab_s, zkappa_s, ztsb, zeta_s, ztstemp, z_s, kjstart=0)
+      CALL wrk_alloc( jpij, nlay_i+3, zswiterm, zswitbis, zdiagbis  )
+      CALL wrk_alloc( jpij, nlay_i+3, 3, ztrid )
+
+      CALL wrk_alloc( jpij, zdq, zq_ini, zhfx_err )
+
+      ! --- diag error on heat diffusion - PART 1 --- !
+      zdq(:) = 0._wp ; zq_ini(:) = 0._wp      
+      DO ji = kideb, kiut
+         zq_ini(ji) = ( SUM( q_i_1d(ji,1:nlay_i) ) * ht_i_1d(ji) / REAL( nlay_i ) +  &
+            &           SUM( q_s_1d(ji,1:nlay_s) ) * ht_s_1d(ji) / REAL( nlay_s ) ) 
+      END DO
+
       !------------------------------------------------------------------------------!
       ! 1) Initialization                                                            !
       !------------------------------------------------------------------------------!
-      !
+      ! clem clean: replace just ztfs by rtt
       DO ji = kideb , kiut
          ! is there snow or not
-         isnow(ji)= NINT(  1._wp - MAX( 0._wp , SIGN(1._wp, - ht_s_b(ji) ) )  )
+         isnow(ji)= NINT(  1._wp - MAX( 0._wp , SIGN(1._wp, - ht_s_1d(ji) ) )  )
          ! surface temperature of fusion
-!!gm ???  ztfs(ji) = rtt !!!????
          ztfs(ji) = REAL( isnow(ji) ) * rtt + REAL( 1 - isnow(ji) ) * rtt
          ! layer thickness
-         zh_i(ji) = ht_i_b(ji) / REAL( nlay_i )
-         zh_s(ji) = ht_s_b(ji) / REAL( nlay_s )
+         zh_i(ji) = ht_i_1d(ji) / REAL( nlay_i )
+         zh_s(ji) = ht_s_1d(ji) / REAL( nlay_s )
       END DO
 
@@ -169,13 +186,13 @@
       z_i(:,0) = 0._wp   ! vert. coord. of the up. lim. of the 1st ice layer
 
-      DO layer = 1, nlay_s            ! vert. coord of the up. lim. of the layer-th snow layer
-         DO ji = kideb , kiut
-            z_s(ji,layer) = z_s(ji,layer-1) + ht_s_b(ji) / REAL( nlay_s )
-         END DO
-      END DO
-
-      DO layer = 1, nlay_i            ! vert. coord of the up. lim. of the layer-th ice layer
-         DO ji = kideb , kiut
-            z_i(ji,layer) = z_i(ji,layer-1) + ht_i_b(ji) / REAL( nlay_i )
+      DO jk = 1, nlay_s            ! vert. coord of the up. lim. of the layer-th snow layer
+         DO ji = kideb , kiut
+            z_s(ji,jk) = z_s(ji,jk-1) + ht_s_1d(ji) / REAL( nlay_s )
+         END DO
+      END DO
+
+      DO jk = 1, nlay_i            ! vert. coord of the up. lim. of the layer-th ice layer
+         DO ji = kideb , kiut
+            z_i(ji,jk) = z_i(ji,jk-1) + ht_i_1d(ji) / REAL( nlay_i )
          END DO
       END DO
@@ -194,12 +211,12 @@
       ! zfsw    = (1-i0).qsr_ice   is absorbed at the surface 
       ! zftrice = io.qsr_ice       is below the surface 
-      ! fstbif  = io.qsr_ice.exp(-k(h_i)) transmitted below the ice 
+      ! ftr_ice = io.qsr_ice.exp(-k(h_i)) transmitted below the ice 
 
       DO ji = kideb , kiut
          ! switches
-         isnow(ji) = NINT(  1._wp - MAX( 0._wp , SIGN( 1._wp , - ht_s_b(ji) ) )  ) 
+         isnow(ji) = NINT(  1._wp - MAX( 0._wp , SIGN( 1._wp , - ht_s_1d(ji) ) )  ) 
          ! hs > 0, isnow = 1
          zhsu (ji) = hnzst  ! threshold for the computation of i0
-         zihic(ji) = MAX( 0._wp , 1._wp - ( ht_i_b(ji) / zhsu(ji) ) )     
+         zihic(ji) = MAX( 0._wp , 1._wp - ( ht_i_1d(ji) / zhsu(ji) ) )     
 
          i0(ji)    = REAL( 1 - isnow(ji) ) * ( fr1_i0_1d(ji) + zihic(ji) * fr2_i0_1d(ji) )
@@ -208,5 +225,5 @@
          !            a function of the cloud cover
          !
-         !i0(ji)     =  (1.0-FLOAT(isnow(ji)))*3.0/(100*ht_s_b(ji)+10.0)
+         !i0(ji)     =  (1.0-FLOAT(isnow(ji)))*3.0/(100*ht_s_1d(ji)+10.0)
          !formula used in Cice
       END DO
@@ -230,10 +247,10 @@
       END DO
 
-      DO layer = 1, nlay_s          ! Radiation through snow
+      DO jk = 1, nlay_s          ! Radiation through snow
          DO ji = kideb, kiut
             !                             ! radiation transmitted below the layer-th snow layer
-            zradtr_s(ji,layer) = zradtr_s(ji,0) * EXP( - zraext_s * ( MAX ( 0._wp , z_s(ji,layer) ) ) )
+            zradtr_s(ji,jk) = zradtr_s(ji,0) * EXP( - zraext_s * ( MAX ( 0._wp , z_s(ji,jk) ) ) )
             !                             ! radiation absorbed by the layer-th snow layer
-            zradab_s(ji,layer) = zradtr_s(ji,layer-1) - zradtr_s(ji,layer)
+            zradab_s(ji,jk) = zradtr_s(ji,jk-1) - zradtr_s(ji,jk)
          END DO
       END DO
@@ -243,30 +260,15 @@
       END DO
 
-      DO layer = 1, nlay_i          ! Radiation through ice
+      DO jk = 1, nlay_i          ! Radiation through ice
          DO ji = kideb, kiut
             !                             ! radiation transmitted below the layer-th ice layer
-            zradtr_i(ji,layer) = zradtr_i(ji,0) * EXP( - kappa_i * ( MAX ( 0._wp , z_i(ji,layer) ) ) )
+            zradtr_i(ji,jk) = zradtr_i(ji,0) * EXP( - kappa_i * ( MAX ( 0._wp , z_i(ji,jk) ) ) )
             !                             ! radiation absorbed by the layer-th ice layer
-            zradab_i(ji,layer) = zradtr_i(ji,layer-1) - zradtr_i(ji,layer)
+            zradab_i(ji,jk) = zradtr_i(ji,jk-1) - zradtr_i(ji,jk)
          END DO
       END DO
 
       DO ji = kideb, kiut           ! Radiation transmitted below the ice
-         fstbif_1d(ji) = fstbif_1d(ji) + iatte_1d(ji) * zradtr_i(ji,nlay_i) * a_i_b(ji) / at_i_b(ji) ! clem modif
-      END DO
-
-      ! +++++
-      ! just to check energy conservation
-      DO ji = kideb, kiut
-         ii = MOD( npb(ji) - 1 , jpi ) + 1
-         ij =    ( npb(ji) - 1 ) / jpi + 1
-         fstroc(ii,ij,jl) = iatte_1d(ji) * zradtr_i(ji,nlay_i) ! clem modif
-      END DO
-      ! +++++
-
-      DO layer = 1, nlay_i
-         DO ji = kideb, kiut
-            radab(ji,layer) = zradab_i(ji,layer)
-         END DO
+         ftr_ice_1d(ji) = zradtr_i(ji,nlay_i) 
       END DO
 
@@ -277,19 +279,19 @@
       !
       DO ji = kideb, kiut        ! Old surface temperature
-         ztsuold  (ji) =  t_su_b(ji)                              ! temperature at the beg of iter pr.
-         ztsuoldit(ji) =  t_su_b(ji)                              ! temperature at the previous iter
-         t_su_b   (ji) =  MIN( t_su_b(ji), ztfs(ji)-0.00001 )     ! necessary
+         ztsub  (ji) =  t_su_1d(ji)                              ! temperature at the beg of iter pr.
+         ztsubit(ji) =  t_su_1d(ji)                              ! temperature at the previous iter
+         t_su_1d   (ji) =  MIN( t_su_1d(ji), ztfs(ji) - ztsu_err )  ! necessary
          zerrit   (ji) =  1000._wp                                ! initial value of error
       END DO
 
-      DO layer = 1, nlay_s       ! Old snow temperature
-         DO ji = kideb , kiut
-            ztsold(ji,layer) =  t_s_b(ji,layer)
-         END DO
-      END DO
-
-      DO layer = 1, nlay_i       ! Old ice temperature
-         DO ji = kideb , kiut
-            ztiold(ji,layer) =  t_i_b(ji,layer)
+      DO jk = 1, nlay_s       ! Old snow temperature
+         DO ji = kideb , kiut
+            ztsb(ji,jk) =  t_s_1d(ji,jk)
+         END DO
+      END DO
+
+      DO jk = 1, nlay_i       ! Old ice temperature
+         DO ji = kideb , kiut
+            ztib(ji,jk) =  t_i_1d(ji,jk)
          END DO
       END DO
@@ -308,12 +310,12 @@
          IF( thcon_i_swi == 0 ) THEN      ! Untersteiner (1964) formula
             DO ji = kideb , kiut
-               ztcond_i(ji,0)        = rcdic + zbeta*s_i_b(ji,1) / MIN(-epsi10,t_i_b(ji,1)-rtt)
+               ztcond_i(ji,0)        = rcdic + zbeta*s_i_1d(ji,1) / MIN(-epsi10,t_i_1d(ji,1)-rtt)
                ztcond_i(ji,0)        = MAX(ztcond_i(ji,0),zkimin)
             END DO
-            DO layer = 1, nlay_i-1
+            DO jk = 1, nlay_i-1
                DO ji = kideb , kiut
-                  ztcond_i(ji,layer) = rcdic + zbeta*( s_i_b(ji,layer) + s_i_b(ji,layer+1) ) /  &
-                     MIN(-2.0_wp * epsi10, t_i_b(ji,layer)+t_i_b(ji,layer+1) - 2.0_wp * rtt)
-                  ztcond_i(ji,layer) = MAX(ztcond_i(ji,layer),zkimin)
+                  ztcond_i(ji,jk) = rcdic + zbeta*( s_i_1d(ji,jk) + s_i_1d(ji,jk+1) ) /  &
+                     MIN(-2.0_wp * epsi10, t_i_1d(ji,jk)+t_i_1d(ji,jk+1) - 2.0_wp * rtt)
+                  ztcond_i(ji,jk) = MAX(ztcond_i(ji,jk),zkimin)
                END DO
             END DO
@@ -322,19 +324,20 @@
          IF( thcon_i_swi == 1 ) THEN      ! Pringle et al formula included: 2.11 + 0.09 S/T - 0.011.T
             DO ji = kideb , kiut
-               ztcond_i(ji,0) = rcdic + 0.090_wp * s_i_b(ji,1) / MIN( -epsi10, t_i_b(ji,1)-rtt )   &
-                  &                   - 0.011_wp * ( t_i_b(ji,1) - rtt )  
+               ztcond_i(ji,0) = rcdic + 0.090_wp * s_i_1d(ji,1) / MIN( -epsi10, t_i_1d(ji,1)-rtt )   &
+                  &                   - 0.011_wp * ( t_i_1d(ji,1) - rtt )  
                ztcond_i(ji,0) = MAX( ztcond_i(ji,0), zkimin )
             END DO
-            DO layer = 1, nlay_i-1
+            DO jk = 1, nlay_i-1
                DO ji = kideb , kiut
-                  ztcond_i(ji,layer) = rcdic + 0.090_wp * ( s_i_b(ji,layer) + s_i_b(ji,layer+1) )   &
-                     &                                  / MIN(-2.0_wp * epsi10, t_i_b(ji,layer)+t_i_b(ji,layer+1) - 2.0_wp * rtt)   &
-                     &                       - 0.0055_wp* ( t_i_b(ji,layer) + t_i_b(ji,layer+1) - 2.0*rtt )  
-                  ztcond_i(ji,layer) = MAX( ztcond_i(ji,layer), zkimin )
+                  ztcond_i(ji,jk) = rcdic +                                                                     & 
+                     &                 0.090_wp * ( s_i_1d(ji,jk) + s_i_1d(ji,jk+1) )                          &
+                     &                 / MIN(-2.0_wp * epsi10, t_i_1d(ji,jk)+t_i_1d(ji,jk+1) - 2.0_wp * rtt)   &
+                     &               - 0.0055_wp* ( t_i_1d(ji,jk) + t_i_1d(ji,jk+1) - 2.0*rtt )  
+                  ztcond_i(ji,jk) = MAX( ztcond_i(ji,jk), zkimin )
                END DO
             END DO
             DO ji = kideb , kiut
-               ztcond_i(ji,nlay_i) = rcdic + 0.090_wp * s_i_b(ji,nlay_i) / MIN(-epsi10,t_bo_b(ji)-rtt)   &
-                  &                        - 0.011_wp * ( t_bo_b(ji) - rtt )  
+               ztcond_i(ji,nlay_i) = rcdic + 0.090_wp * s_i_1d(ji,nlay_i) / MIN(-epsi10,t_bo_1d(ji)-rtt)   &
+                  &                        - 0.011_wp * ( t_bo_1d(ji) - rtt )  
                ztcond_i(ji,nlay_i) = MAX( ztcond_i(ji,nlay_i), zkimin )
             END DO
@@ -352,15 +355,15 @@
          END DO
 
-         DO layer = 1, nlay_s-1
-            DO ji = kideb , kiut
-               zkappa_s(ji,layer)  = 2.0 * rcdsn / &
+         DO jk = 1, nlay_s-1
+            DO ji = kideb , kiut
+               zkappa_s(ji,jk)  = 2.0 * rcdsn / &
                   MAX(epsi10,2.0*zh_s(ji))
             END DO
          END DO
 
-         DO layer = 1, nlay_i-1
+         DO jk = 1, nlay_i-1
             DO ji = kideb , kiut
                !-- Ice kappa factors
-               zkappa_i(ji,layer)  = 2.0*ztcond_i(ji,layer)/ &
+               zkappa_i(ji,jk)  = 2.0*ztcond_i(ji,jk)/ &
                   MAX(epsi10,2.0*zh_i(ji)) 
             END DO
@@ -381,18 +384,18 @@
          !------------------------------------------------------------------------------|
          !
-         DO layer = 1, nlay_i
-            DO ji = kideb , kiut
-               ztitemp(ji,layer)   = t_i_b(ji,layer)
-               zspeche_i(ji,layer) = cpic + zgamma*s_i_b(ji,layer)/ &
-                  MAX((t_i_b(ji,layer)-rtt)*(ztiold(ji,layer)-rtt),epsi10)
-               zeta_i(ji,layer)    = rdt_ice / MAX(rhoic*zspeche_i(ji,layer)*zh_i(ji), &
+         DO jk = 1, nlay_i
+            DO ji = kideb , kiut
+               ztitemp(ji,jk)   = t_i_1d(ji,jk)
+               zspeche_i(ji,jk) = cpic + zgamma*s_i_1d(ji,jk)/ &
+                  MAX((t_i_1d(ji,jk)-rtt)*(ztib(ji,jk)-rtt),epsi10)
+               zeta_i(ji,jk)    = rdt_ice / MAX(rhoic*zspeche_i(ji,jk)*zh_i(ji), &
                   epsi10)
             END DO
          END DO
 
-         DO layer = 1, nlay_s
-            DO ji = kideb , kiut
-               ztstemp(ji,layer) = t_s_b(ji,layer)
-               zeta_s(ji,layer)  = rdt_ice / MAX(rhosn*cpic*zh_s(ji),epsi10)
+         DO jk = 1, nlay_s
+            DO ji = kideb , kiut
+               ztstemp(ji,jk) = t_s_1d(ji,jk)
+               zeta_s(ji,jk)  = rdt_ice / MAX(rhosn*cpic*zh_s(ji),epsi10)
             END DO
          END DO
@@ -402,15 +405,17 @@
          !------------------------------------------------------------------------------|
          !
-         DO ji = kideb , kiut
-
-            ! update of the non solar flux according to the update in T_su
-            qnsr_ice_1d(ji) = qnsr_ice_1d(ji) + dqns_ice_1d(ji) * & 
-               ( t_su_b(ji) - ztsuoldit(ji) )
-
+         IF( .NOT. lk_cpl ) THEN   !--- forced atmosphere case
+            DO ji = kideb , kiut
+               ! update of the non solar flux according to the update in T_su
+               qns_ice_1d(ji) = qns_ice_1d(ji) + dqns_ice_1d(ji) * ( t_su_1d(ji) - ztsubit(ji) )
+            END DO
+         ENDIF
+
+         ! Update incoming flux
+         DO ji = kideb , kiut
             ! update incoming flux
             zf(ji)    =   zfsw(ji)              & ! net absorbed solar radiation
-               + qnsr_ice_1d(ji)           ! non solar total flux 
+               + qns_ice_1d(ji)                   ! non solar total flux 
             ! (LWup, LWdw, SH, LH)
-
          END DO
 
@@ -427,11 +432,11 @@
          !!ice interior terms (top equation has the same form as the others)
 
-         DO numeq=1,jkmax+2
+         DO numeq=1,nlay_i+3
             DO ji = kideb , kiut
                ztrid(ji,numeq,1) = 0.
                ztrid(ji,numeq,2) = 0.
                ztrid(ji,numeq,3) = 0.
-               zindterm(ji,numeq)= 0.
-               zindtbis(ji,numeq)= 0.
+               zswiterm(ji,numeq)= 0.
+               zswitbis(ji,numeq)= 0.
                zdiagbis(ji,numeq)= 0.
             ENDDO
@@ -440,11 +445,11 @@
          DO numeq = nlay_s + 2, nlay_s + nlay_i 
             DO ji = kideb , kiut
-               layer              = numeq - nlay_s - 1
-               ztrid(ji,numeq,1)  =  - zeta_i(ji,layer)*zkappa_i(ji,layer-1)
-               ztrid(ji,numeq,2)  =  1.0 + zeta_i(ji,layer)*(zkappa_i(ji,layer-1) + &
-                  zkappa_i(ji,layer))
-               ztrid(ji,numeq,3)  =  - zeta_i(ji,layer)*zkappa_i(ji,layer)
-               zindterm(ji,numeq) =  ztiold(ji,layer) + zeta_i(ji,layer)* &
-                  zradab_i(ji,layer)
+               jk              = numeq - nlay_s - 1
+               ztrid(ji,numeq,1)  =  - zeta_i(ji,jk)*zkappa_i(ji,jk-1)
+               ztrid(ji,numeq,2)  =  1.0 + zeta_i(ji,jk)*(zkappa_i(ji,jk-1) + &
+                  zkappa_i(ji,jk))
+               ztrid(ji,numeq,3)  =  - zeta_i(ji,jk)*zkappa_i(ji,jk)
+               zswiterm(ji,numeq) =  ztib(ji,jk) + zeta_i(ji,jk)* &
+                  zradab_i(ji,jk)
             END DO
          ENDDO
@@ -457,12 +462,12 @@
                +  zkappa_i(ji,nlay_i-1) )
             ztrid(ji,numeq,3)  =  0.0
-            zindterm(ji,numeq) =  ztiold(ji,nlay_i) + zeta_i(ji,nlay_i)* &
+            zswiterm(ji,numeq) =  ztib(ji,nlay_i) + zeta_i(ji,nlay_i)* &
                ( zradab_i(ji,nlay_i) + zkappa_i(ji,nlay_i)*zg1 &
-               *  t_bo_b(ji) ) 
+               *  t_bo_1d(ji) ) 
          ENDDO
 
 
          DO ji = kideb , kiut
-            IF ( ht_s_b(ji).gt.0.0 ) THEN
+            IF ( ht_s_1d(ji).gt.0.0 ) THEN
                !
                !------------------------------------------------------------------------------|
@@ -472,11 +477,11 @@
                !!snow interior terms (bottom equation has the same form as the others)
                DO numeq = 3, nlay_s + 1
-                  layer =  numeq - 1
-                  ztrid(ji,numeq,1)   =  - zeta_s(ji,layer)*zkappa_s(ji,layer-1)
-                  ztrid(ji,numeq,2)   =  1.0 + zeta_s(ji,layer)*( zkappa_s(ji,layer-1) + &
-                     zkappa_s(ji,layer) )
-                  ztrid(ji,numeq,3)   =  - zeta_s(ji,layer)*zkappa_s(ji,layer)
-                  zindterm(ji,numeq)  =  ztsold(ji,layer) + zeta_s(ji,layer)* &
-                     zradab_s(ji,layer)
+                  jk =  numeq - 1
+                  ztrid(ji,numeq,1)   =  - zeta_s(ji,jk)*zkappa_s(ji,jk-1)
+                  ztrid(ji,numeq,2)   =  1.0 + zeta_s(ji,jk)*( zkappa_s(ji,jk-1) + &
+                     zkappa_s(ji,jk) )
+                  ztrid(ji,numeq,3)   =  - zeta_s(ji,jk)*zkappa_s(ji,jk)
+                  zswiterm(ji,numeq)  =  ztsb(ji,jk) + zeta_s(ji,jk)* &
+                     zradab_s(ji,jk)
                END DO
 
@@ -484,9 +489,9 @@
                IF ( nlay_i.eq.1 ) THEN
                   ztrid(ji,nlay_s+2,3)    =  0.0
-                  zindterm(ji,nlay_s+2)   =  zindterm(ji,nlay_s+2) + zkappa_i(ji,1)* &
-                     t_bo_b(ji) 
+                  zswiterm(ji,nlay_s+2)   =  zswiterm(ji,nlay_s+2) + zkappa_i(ji,1)* &
+                     t_bo_1d(ji) 
                ENDIF
 
-               IF ( t_su_b(ji) .LT. rtt ) THEN
+               IF ( t_su_1d(ji) .LT. rtt ) THEN
 
                   !------------------------------------------------------------------------------|
@@ -501,5 +506,5 @@
                   ztrid(ji,1,2) = dzf(ji) - zg1s*zkappa_s(ji,0)
                   ztrid(ji,1,3) = zg1s*zkappa_s(ji,0)
-                  zindterm(ji,1) = dzf(ji)*t_su_b(ji)   - zf(ji)
+                  zswiterm(ji,1) = dzf(ji)*t_su_1d(ji)   - zf(ji)
 
                   !!first layer of snow equation
@@ -507,5 +512,5 @@
                   ztrid(ji,2,2)  =  1.0 + zeta_s(ji,1)*(zkappa_s(ji,1) + zkappa_s(ji,0)*zg1s)
                   ztrid(ji,2,3)  =  - zeta_s(ji,1)* zkappa_s(ji,1)
-                  zindterm(ji,2) =  ztsold(ji,1) + zeta_s(ji,1)*zradab_s(ji,1)
+                  zswiterm(ji,2) =  ztsb(ji,1) + zeta_s(ji,1)*zradab_s(ji,1)
 
                ELSE 
@@ -524,7 +529,7 @@
                      zkappa_s(ji,0) * zg1s )
                   ztrid(ji,2,3)  =  - zeta_s(ji,1)*zkappa_s(ji,1) 
-                  zindterm(ji,2) = ztsold(ji,1) + zeta_s(ji,1) *            &
+                  zswiterm(ji,2) = ztsb(ji,1) + zeta_s(ji,1) *            &
                      ( zradab_s(ji,1) +                         &
-                     zkappa_s(ji,0) * zg1s * t_su_b(ji) ) 
+                     zkappa_s(ji,0) * zg1s * t_su_1d(ji) ) 
                ENDIF
             ELSE
@@ -534,5 +539,5 @@
                !------------------------------------------------------------------------------|
                !
-               IF (t_su_b(ji) .LT. rtt) THEN
+               IF (t_su_1d(ji) .LT. rtt) THEN
                   !
                   !------------------------------------------------------------------------------|
@@ -548,5 +553,5 @@
                   ztrid(ji,numeqmin(ji),2)   =  dzf(ji) - zkappa_i(ji,0)*zg1    
                   ztrid(ji,numeqmin(ji),3)   =  zkappa_i(ji,0)*zg1
-                  zindterm(ji,numeqmin(ji))  =  dzf(ji)*t_su_b(ji) - zf(ji)
+                  zswiterm(ji,numeqmin(ji))  =  dzf(ji)*t_su_1d(ji) - zf(ji)
 
                   !!first layer of ice equation
@@ -555,5 +560,5 @@
                      + zkappa_i(ji,0) * zg1 )
                   ztrid(ji,numeqmin(ji)+1,3) =  - zeta_i(ji,1)*zkappa_i(ji,1)  
-                  zindterm(ji,numeqmin(ji)+1)=  ztiold(ji,1) + zeta_i(ji,1)*zradab_i(ji,1)  
+                  zswiterm(ji,numeqmin(ji)+1)=  ztib(ji,1) + zeta_i(ji,1)*zradab_i(ji,1)  
 
                   !!case of only one layer in the ice (surface & ice equations are altered)
@@ -568,6 +573,6 @@
                      ztrid(ji,numeqmin(ji)+1,3)  =  0.0
 
-                     zindterm(ji,numeqmin(ji)+1) =  ztiold(ji,1) + zeta_i(ji,1)* &
-                        ( zradab_i(ji,1) + zkappa_i(ji,1)*t_bo_b(ji) )
+                     zswiterm(ji,numeqmin(ji)+1) =  ztib(ji,1) + zeta_i(ji,1)* &
+                        ( zradab_i(ji,1) + zkappa_i(ji,1)*t_bo_1d(ji) )
                   ENDIF
 
@@ -588,6 +593,6 @@
                      zg1)  
                   ztrid(ji,numeqmin(ji),3)      =  - zeta_i(ji,1) * zkappa_i(ji,1)
-                  zindterm(ji,numeqmin(ji))     =  ztiold(ji,1) + zeta_i(ji,1)*( zradab_i(ji,1) + &
-                     zkappa_i(ji,0) * zg1 * t_su_b(ji) ) 
+                  zswiterm(ji,numeqmin(ji))     =  ztib(ji,1) + zeta_i(ji,1)*( zradab_i(ji,1) + &
+                     zkappa_i(ji,0) * zg1 * t_su_1d(ji) ) 
 
                   !!case of only one layer in the ice (surface & ice equations are altered)
@@ -597,7 +602,7 @@
                         zkappa_i(ji,1))
                      ztrid(ji,numeqmin(ji),3)  =  0.0
-                     zindterm(ji,numeqmin(ji)) =  ztiold(ji,1) + zeta_i(ji,1)* &
-                        (zradab_i(ji,1) + zkappa_i(ji,1)*t_bo_b(ji)) &
-                        + t_su_b(ji)*zeta_i(ji,1)*zkappa_i(ji,0)*2.0
+                     zswiterm(ji,numeqmin(ji)) =  ztib(ji,1) + zeta_i(ji,1)* &
+                        (zradab_i(ji,1) + zkappa_i(ji,1)*t_bo_1d(ji)) &
+                        + t_su_1d(ji)*zeta_i(ji,1)*zkappa_i(ji,0)*2.0
                   ENDIF
 
@@ -618,8 +623,8 @@
 
          maxnumeqmax = 0
-         minnumeqmin = jkmax+4
-
-         DO ji = kideb , kiut
-            zindtbis(ji,numeqmin(ji)) =  zindterm(ji,numeqmin(ji))
+         minnumeqmin = nlay_i+5
+
+         DO ji = kideb , kiut
+            zswitbis(ji,numeqmin(ji)) =  zswiterm(ji,numeqmin(ji))
             zdiagbis(ji,numeqmin(ji)) =  ztrid(ji,numeqmin(ji),2)
             minnumeqmin               =  MIN(numeqmin(ji),minnumeqmin)
@@ -627,11 +632,11 @@
          END DO
 
-         DO layer = minnumeqmin+1, maxnumeqmax
-            DO ji = kideb , kiut
-               numeq               =  min(max(numeqmin(ji)+1,layer),numeqmax(ji))
+         DO jk = minnumeqmin+1, maxnumeqmax
+            DO ji = kideb , kiut
+               numeq               =  min(max(numeqmin(ji)+1,jk),numeqmax(ji))
                zdiagbis(ji,numeq)  =  ztrid(ji,numeq,2) - ztrid(ji,numeq,1)* &
                   ztrid(ji,numeq-1,3)/zdiagbis(ji,numeq-1)
-               zindtbis(ji,numeq)  =  zindterm(ji,numeq) - ztrid(ji,numeq,1)* &
-                  zindtbis(ji,numeq-1)/zdiagbis(ji,numeq-1)
+               zswitbis(ji,numeq)  =  zswiterm(ji,numeq) - ztrid(ji,numeq,1)* &
+                  zswitbis(ji,numeq-1)/zdiagbis(ji,numeq-1)
             END DO
          END DO
@@ -639,12 +644,12 @@
          DO ji = kideb , kiut
             ! ice temperatures
-            t_i_b(ji,nlay_i)    =  zindtbis(ji,numeqmax(ji))/zdiagbis(ji,numeqmax(ji))
+            t_i_1d(ji,nlay_i)    =  zswitbis(ji,numeqmax(ji))/zdiagbis(ji,numeqmax(ji))
          END DO
 
          DO numeq = nlay_i + nlay_s + 1, nlay_s + 2, -1
             DO ji = kideb , kiut
-               layer    =  numeq - nlay_s - 1
-               t_i_b(ji,layer)  =  (zindtbis(ji,numeq) - ztrid(ji,numeq,3)* &
-                  t_i_b(ji,layer+1))/zdiagbis(ji,numeq)
+               jk    =  numeq - nlay_s - 1
+               t_i_1d(ji,jk)  =  (zswitbis(ji,numeq) - ztrid(ji,numeq,3)* &
+                  t_i_1d(ji,jk+1))/zdiagbis(ji,numeq)
             END DO
          END DO
@@ -652,15 +657,15 @@
          DO ji = kideb , kiut
             ! snow temperatures      
-            IF (ht_s_b(ji).GT.0._wp) &
-               t_s_b(ji,nlay_s)     =  (zindtbis(ji,nlay_s+1) - ztrid(ji,nlay_s+1,3) &
-               *  t_i_b(ji,1))/zdiagbis(ji,nlay_s+1) &
-               *        MAX(0.0,SIGN(1.0,ht_s_b(ji))) 
+            IF (ht_s_1d(ji).GT.0._wp) &
+               t_s_1d(ji,nlay_s)     =  (zswitbis(ji,nlay_s+1) - ztrid(ji,nlay_s+1,3) &
+               *  t_i_1d(ji,1))/zdiagbis(ji,nlay_s+1) &
+               *        MAX(0.0,SIGN(1.0,ht_s_1d(ji))) 
 
             ! surface temperature
-            isnow(ji)     = NINT(  1.0 - MAX( 0.0 , SIGN( 1.0 , -ht_s_b(ji) )  )  )
-            ztsuoldit(ji) = t_su_b(ji)
-            IF( t_su_b(ji) < ztfs(ji) ) &
-               t_su_b(ji) = ( zindtbis(ji,numeqmin(ji)) - ztrid(ji,numeqmin(ji),3)* ( REAL( isnow(ji) )*t_s_b(ji,1)   &
-               &          + REAL( 1 - isnow(ji) )*t_i_b(ji,1) ) ) / zdiagbis(ji,numeqmin(ji))  
+            isnow(ji)     = NINT(  1.0 - MAX( 0.0 , SIGN( 1.0 , -ht_s_1d(ji) )  )  )
+            ztsubit(ji) = t_su_1d(ji)
+            IF( t_su_1d(ji) < ztfs(ji) ) &
+               t_su_1d(ji) = ( zswitbis(ji,numeqmin(ji)) - ztrid(ji,numeqmin(ji),3)* ( REAL( isnow(ji) )*t_s_1d(ji,1)   &
+               &          + REAL( 1 - isnow(ji) )*t_i_1d(ji,1) ) ) / zdiagbis(ji,numeqmin(ji))  
          END DO
          !
@@ -672,22 +677,20 @@
          ! zerrit(ji) is a measure of error, it has to be under maxer_i_thd
          DO ji = kideb , kiut
-            t_su_b(ji) =  MAX(  MIN( t_su_b(ji) , ztfs(ji) ) , 190._wp  )
-            zerrit(ji) =  ABS( t_su_b(ji) - ztsuoldit(ji) )     
-         END DO
-
-         DO layer  =  1, nlay_s
-            DO ji = kideb , kiut
-               ii = MOD( npb(ji) - 1, jpi ) + 1
-               ij = ( npb(ji) - 1 ) / jpi + 1
-               t_s_b(ji,layer) = MAX(  MIN( t_s_b(ji,layer), rtt ), 190._wp  )
-               zerrit(ji)      = MAX(zerrit(ji),ABS(t_s_b(ji,layer) - ztstemp(ji,layer)))
-            END DO
-         END DO
-
-         DO layer  =  1, nlay_i
-            DO ji = kideb , kiut
-               ztmelt_i        = -tmut * s_i_b(ji,layer) + rtt 
-               t_i_b(ji,layer) =  MAX(MIN(t_i_b(ji,layer),ztmelt_i), 190._wp)
-               zerrit(ji)      =  MAX(zerrit(ji),ABS(t_i_b(ji,layer) - ztitemp(ji,layer)))
+            t_su_1d(ji) =  MAX(  MIN( t_su_1d(ji) , ztfs(ji) ) , 190._wp  )
+            zerrit(ji) =  ABS( t_su_1d(ji) - ztsubit(ji) )     
+         END DO
+
+         DO jk  =  1, nlay_s
+            DO ji = kideb , kiut
+               t_s_1d(ji,jk) = MAX(  MIN( t_s_1d(ji,jk), rtt ), 190._wp  )
+               zerrit(ji)      = MAX(zerrit(ji),ABS(t_s_1d(ji,jk) - ztstemp(ji,jk)))
+            END DO
+         END DO
+
+         DO jk  =  1, nlay_i
+            DO ji = kideb , kiut
+               ztmelt_i        = -tmut * s_i_1d(ji,jk) + rtt 
+               t_i_1d(ji,jk) =  MAX(MIN(t_i_1d(ji,jk),ztmelt_i), 190._wp)
+               zerrit(ji)      =  MAX(zerrit(ji),ABS(t_i_1d(ji,jk) - ztitemp(ji,jk)))
             END DO
          END DO
@@ -713,45 +716,103 @@
       !-------------------------------------------------------------------------!
       DO ji = kideb, kiut
-#if ! defined key_coupled
          ! forced mode only : update of latent heat fluxes (sublimation) (always >=0, upward flux) 
-         qla_ice_1d (ji) = MAX( 0._wp, qla_ice_1d (ji) + dqla_ice_1d(ji) * ( t_su_b(ji) - ztsuold(ji) ) )
-#endif
+         IF( .NOT. lk_cpl) qla_ice_1d (ji) = MAX( 0._wp, qla_ice_1d (ji) + dqla_ice_1d(ji) * ( t_su_1d(ji) - ztsub(ji) ) )
          !                                ! surface ice conduction flux
-         isnow(ji)       = NINT(  1._wp - MAX( 0._wp, SIGN( 1._wp, -ht_s_b(ji) ) )  )
-         fc_su(ji)       =  -     REAL( isnow(ji) ) * zkappa_s(ji,0) * zg1s * (t_s_b(ji,1) - t_su_b(ji))   &
-            &               - REAL( 1 - isnow(ji) ) * zkappa_i(ji,0) * zg1  * (t_i_b(ji,1) - t_su_b(ji))
+         isnow(ji)       = NINT(  1._wp - MAX( 0._wp, SIGN( 1._wp, -ht_s_1d(ji) ) )  )
+         fc_su(ji)       =  -     REAL( isnow(ji) ) * zkappa_s(ji,0) * zg1s * (t_s_1d(ji,1) - t_su_1d(ji))   &
+            &               - REAL( 1 - isnow(ji) ) * zkappa_i(ji,0) * zg1  * (t_i_1d(ji,1) - t_su_1d(ji))
          !                                ! bottom ice conduction flux
-         fc_bo_i(ji)     =  - zkappa_i(ji,nlay_i) * ( zg1*(t_bo_b(ji) - t_i_b(ji,nlay_i)) )
-      END DO
-
-      !-------------------------!
-      ! Heat conservation       !
-      !-------------------------!
-      IF( con_i .AND. jiindex_1d > 0 ) THEN
+         fc_bo_i(ji)     =  - zkappa_i(ji,nlay_i) * ( zg1*(t_bo_1d(ji) - t_i_1d(ji,nlay_i)) )
+      END DO
+
+      !-----------------------------------------
+      ! Heat flux used to warm/cool ice in W.m-2
+      !-----------------------------------------
+      DO ji = kideb, kiut
+         IF( t_su_1d(ji) < rtt ) THEN  ! case T_su < 0degC
+            hfx_dif_1d(ji) = hfx_dif_1d(ji)  +   &
+               &            ( qns_ice_1d(ji) + qsr_ice_1d(ji) - zradtr_i(ji,nlay_i) - fc_bo_i(ji) ) * a_i_1d(ji)
+         ELSE                         ! case T_su = 0degC
+            hfx_dif_1d(ji) = hfx_dif_1d(ji) +    &
+               &             ( fc_su(ji) + i0(ji) * qsr_ice_1d(ji) - zradtr_i(ji,nlay_i) - fc_bo_i(ji) ) * a_i_1d(ji)
+         ENDIF
+      END DO
+
+      ! --- computes sea ice energy of melting compulsory for limthd_dh --- !
+      CALL lim_thd_enmelt( kideb, kiut )
+
+      ! --- diag conservation imbalance on heat diffusion - PART 2 --- !
+      DO ji = kideb, kiut
+         zdq(ji)        = - zq_ini(ji) + ( SUM( q_i_1d(ji,1:nlay_i) ) * ht_i_1d(ji) / REAL( nlay_i ) +  &
+            &                              SUM( q_s_1d(ji,1:nlay_s) ) * ht_s_1d(ji) / REAL( nlay_s ) )
+         zhfx_err(ji)   = ( fc_su(ji) + i0(ji) * qsr_ice_1d(ji) - zradtr_i(ji,nlay_i) - fc_bo_i(ji) + zdq(ji) * r1_rdtice ) 
+         hfx_err_1d(ji) = hfx_err_1d(ji) + zhfx_err(ji) * a_i_1d(ji)
+      END DO 
+
+      ! diagnose external surface (forced case) or bottom (forced case) from heat conservation
+      IF( .NOT. lk_cpl ) THEN   ! --- forced case: qns_ice and fc_su are diagnosed
+         !
          DO ji = kideb, kiut
-            ! Upper snow value
-            fc_s(ji,0) = - REAL( isnow(ji) ) * zkappa_s(ji,0) * zg1s * ( t_s_b(ji,1) - t_su_b(ji) ) 
-            ! Bott. snow value
-            fc_s(ji,1) = - REAL( isnow(ji) ) * zkappa_s(ji,1) * ( t_i_b(ji,1) - t_s_b(ji,1) ) 
-         END DO
-         DO ji = kideb, kiut         ! Upper ice layer
-            fc_i(ji,0) = - REAL( isnow(ji) ) * &  ! interface flux if there is snow
-               ( zkappa_i(ji,0)  * ( t_i_b(ji,1) - t_s_b(ji,nlay_s ) ) ) &
-               - REAL( 1 - isnow(ji) ) * ( zkappa_i(ji,0) * & 
-               zg1 * ( t_i_b(ji,1) - t_su_b(ji) ) ) ! upper flux if not
-         END DO
-         DO layer = 1, nlay_i - 1         ! Internal ice layers
-            DO ji = kideb, kiut
-               fc_i(ji,layer) = - zkappa_i(ji,layer) * ( t_i_b(ji,layer+1) - t_i_b(ji,layer) )
-               ii = MOD( npb(ji) - 1, jpi ) + 1
-               ij = ( npb(ji) - 1 ) / jpi + 1
-            END DO
-         END DO
-         DO ji = kideb, kiut         ! Bottom ice layers
-            fc_i(ji,nlay_i) = - zkappa_i(ji,nlay_i) * ( zg1*(t_bo_b(ji) - t_i_b(ji,nlay_i)) )
-         END DO
+            qns_ice_1d(ji) = qns_ice_1d(ji) - zhfx_err(ji)
+            fc_su     (ji) = fc_su(ji)      - zhfx_err(ji)
+         END DO
+         !
+      ELSE                      ! --- coupled case: ocean turbulent heat flux is diagnosed
+         !
+         DO ji = kideb, kiut
+            fhtur_1d  (ji) = fhtur_1d(ji)   - zhfx_err(ji)
+         END DO
+         !
       ENDIF
+
+      ! --- compute diagnostic net heat flux at the surface of the snow-ice system (W.m2)
+      DO ji = kideb, kiut
+         ii = MOD( npb(ji) - 1, jpi ) + 1 ; ij = ( npb(ji) - 1 ) / jpi + 1
+         hfx_in (ii,ij) = hfx_in (ii,ij) + a_i_1d(ji) * ( qsr_ice_1d(ji) + qns_ice_1d(ji) )
+      END DO
+   
       !
+      CALL wrk_dealloc( jpij, numeqmin, numeqmax, isnow )
+      CALL wrk_dealloc( jpij, ztfs, ztsub, ztsubit, zh_i, zh_s, zfsw )
+      CALL wrk_dealloc( jpij, zf, dzf, zerrit, zdifcase, zftrice, zihic, zhsu )
+      CALL wrk_dealloc( jpij, nlay_i+1, ztcond_i, zradtr_i, zradab_i, zkappa_i,   &
+         &              ztib, zeta_i, ztitemp, z_i, zspeche_i, kjstart = 0 )
+      CALL wrk_dealloc( jpij, nlay_s+1,           zradtr_s, zradab_s, zkappa_s, ztsb, zeta_s, ztstemp, z_s, kjstart = 0 )
+      CALL wrk_dealloc( jpij, nlay_i+3, zswiterm, zswitbis, zdiagbis )
+      CALL wrk_dealloc( jpij, nlay_i+3, 3, ztrid )
+      CALL wrk_dealloc( jpij, zdq, zq_ini, zhfx_err )
+
    END SUBROUTINE lim_thd_dif
+
+   SUBROUTINE lim_thd_enmelt( kideb, kiut )
+      !!-----------------------------------------------------------------------
+      !!                   ***  ROUTINE lim_thd_enmelt *** 
+      !!                 
+      !! ** Purpose :   Computes sea ice energy of melting q_i (J.m-3) from temperature
+      !!
+      !! ** Method  :   Formula (Bitz and Lipscomb, 1999)
+      !!-------------------------------------------------------------------
+      INTEGER, INTENT(in) ::   kideb, kiut   ! bounds for the spatial loop
+      !
+      INTEGER  ::   ji, jk   ! dummy loop indices
+      REAL(wp) ::   ztmelts  ! local scalar 
+      !!-------------------------------------------------------------------
+      !
+      DO jk = 1, nlay_i             ! Sea ice energy of melting
+         DO ji = kideb, kiut
+            ztmelts      = - tmut  * s_i_1d(ji,jk) + rtt 
+            rswitch      = MAX( 0._wp , SIGN( 1._wp , -(t_i_1d(ji,jk) - rtt) - epsi10 ) )
+            q_i_1d(ji,jk) = rhoic * ( cpic * ( ztmelts - t_i_1d(ji,jk) )                                             &
+               &                   + lfus * ( 1.0 - rswitch * ( ztmelts-rtt ) / MIN( t_i_1d(ji,jk)-rtt, -epsi10 ) )   &
+               &                   - rcp  *                 ( ztmelts-rtt )  ) 
+         END DO
+      END DO
+      DO jk = 1, nlay_s             ! Snow energy of melting
+         DO ji = kideb, kiut
+            q_s_1d(ji,jk) = rhosn * ( cpic * ( rtt - t_s_1d(ji,jk) ) + lfus )
+         END DO
+      END DO
+      !
+   END SUBROUTINE lim_thd_enmelt
 
 #else
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd_ent.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd_ent.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd_ent.F90	(revision 5038)
@@ -10,5 +10,6 @@
    !!                 ! 2006-11 (X. Fettweis) Vectorized 
    !!            3.0  ! 2008-03 (M. Vancoppenolle) Energy conservation and clean code
-   !!            4.0  ! 2011-02 (G. Madec) dynamical allocation
+   !!            3.4  ! 2011-02 (G. Madec) dynamical allocation
+   !!             -   ! 2014-05 (C. Rousset) complete rewriting
    !!----------------------------------------------------------------------
 #if defined key_lim3
@@ -22,4 +23,5 @@
    USE domain         !
    USE phycst         ! physical constants
+   USE sbc_oce        ! Surface boundary condition: ocean fields
    USE ice            ! LIM variables
    USE par_ice        ! LIM parameters
@@ -34,10 +36,5 @@
    PRIVATE
 
-   PUBLIC   lim_thd_ent         ! called by lim_thd
-
-   REAL(wp) ::   epsi20 = 1.e-20_wp   ! constant values
-   REAL(wp) ::   epsi10 = 1.e-10_wp   !
-   REAL(wp) ::   zzero  = 0._wp      !
-   REAL(wp) ::   zone   = 1._wp      !
+   PUBLIC   lim_thd_ent         ! called by limthd and limthd_lac
 
    !!----------------------------------------------------------------------
@@ -48,12 +45,11 @@
 CONTAINS
  
-   SUBROUTINE lim_thd_ent( kideb, kiut, jl )
+   SUBROUTINE lim_thd_ent( kideb, kiut, qnew )
       !!-------------------------------------------------------------------
       !!               ***   ROUTINE lim_thd_ent  ***
       !!
       !! ** Purpose :
-      !!           This routine computes new vertical grids 
-      !!           in the ice and in the snow, and consistently redistributes 
-      !!           temperatures in the snow / ice. 
+      !!           This routine computes new vertical grids in the ice, 
+      !!           and consistently redistributes temperatures. 
       !!           Redistribution is made so as to ensure to energy conservation
       !!
@@ -61,633 +57,97 @@
       !! ** Method  : linear conservative remapping
       !!           
-      !! ** Steps : 1) Grid
-      !!            2) Switches
-      !!            3) Snow redistribution
-      !!            4) Ice enthalpy redistribution
-      !!            5) Ice salinity, recover temperature
+      !! ** Steps : 1) cumulative integrals of old enthalpies/thicknesses
+      !!            2) linear remapping on the new layers
+      !!
+      !! ------------ cum0(0)                        ------------- cum1(0)
+      !!                                    NEW      -------------
+      !! ------------ cum0(1)               ==>      -------------
+      !!     ...                                     -------------
+      !! ------------                                -------------
+      !! ------------ cum0(nlay_i+2)                 ------------- cum1(nlay_i)
+      !!
       !!
       !! References : Bitz & Lipscomb, JGR 99; Vancoppenolle et al., GRL, 2005
       !!-------------------------------------------------------------------
       INTEGER , INTENT(in) ::   kideb, kiut   ! Start/End point on which the  the computation is applied
-      INTEGER , INTENT(in) ::   jl            ! Thickness cateogry number
 
-      INTEGER ::   ji,jk   !  dummy loop indices
-      INTEGER ::   ii, ij       ,   &  !  dummy indices
-         ntop0          ,   &  !  old layer top index
-         nbot1          ,   &  !  new layer bottom index
-         ntop1          ,   &  !  new layer top index
-         limsum         ,   &  !  temporary loop index
-         nlayi0,nlays0  ,   &  !  old number of layers
-         maxnbot0       ,   &  !  old layer bottom index
-         layer0, layer1        !  old/new layer indexes
+      REAL(wp), INTENT(inout), DIMENSION(:,:) :: qnew          ! new enthlapies (J.m-3, remapped)
 
-
-      REAL(wp) :: &
-         ztmelts        ,   &  ! ice melting point
-         zqsnic         ,   &  ! enthalpy of snow ice layer
-         zhsnow         ,   &  ! temporary snow thickness variable
-         zswitch        ,   &  ! dummy switch argument
-         zfac1          ,   &  ! dummy factor
-         zfac2          ,   &  ! dummy factor
-         ztform         ,   &  !: bottom formation temperature
-         zaaa           ,   &  !: dummy factor
-         zbbb           ,   &  !: dummy factor
-         zccc           ,   &  !: dummy factor
-         zdiscrim              !: dummy factor
-
-      INTEGER, POINTER, DIMENSION(:) ::   snswi     !  snow switch
-      INTEGER, POINTER, DIMENSION(:) ::   nbot0     !  old layer bottom index
-      INTEGER, POINTER, DIMENSION(:) ::   icsuind   !  ice surface index
-      INTEGER, POINTER, DIMENSION(:) ::   icsuswi   !  ice surface switch
-      INTEGER, POINTER, DIMENSION(:) ::   icboind   !  ice bottom index
-      INTEGER, POINTER, DIMENSION(:) ::   icboswi   !  ice bottom switch
-      INTEGER, POINTER, DIMENSION(:) ::   snicind   !  snow ice index
-      INTEGER, POINTER, DIMENSION(:) ::   snicswi   !  snow ice switch
-      INTEGER, POINTER, DIMENSION(:) ::   snind     !  snow index
+      INTEGER  :: ji         !  dummy loop indices
+      INTEGER  :: jk0, jk1   !  old/new layer indices
       !
-      REAL(wp), POINTER, DIMENSION(:) ::   zh_i   ! thickness of an ice layer
-      REAL(wp), POINTER, DIMENSION(:) ::   zh_s          ! thickness of a snow layer
-      REAL(wp), POINTER, DIMENSION(:) ::   zqsnow        ! enthalpy of the snow put in snow ice 	
-      REAL(wp), POINTER, DIMENSION(:) ::   zdeltah       ! temporary variable
-      REAL(wp), POINTER, DIMENSION(:) ::   zqti_in, zqts_in
-      REAL(wp), POINTER, DIMENSION(:) ::   zqti_fin, zqts_fin
-
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zm0       !  old layer-system vertical cotes 
-      REAL(wp), POINTER, DIMENSION(:,:) ::   qm0       !  old layer-system heat content 
-      REAL(wp), POINTER, DIMENSION(:,:) ::   z_s       !  new snow system vertical cotes 
-      REAL(wp), POINTER, DIMENSION(:,:) ::   z_i       !  new ice system vertical cotes 
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zthick0   !  old ice thickness 
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zhl0      ! old and new layer thicknesses 
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zrl01
-
-      REAL(wp) ::   zinda 
+      REAL(wp), POINTER, DIMENSION(:,:) :: zqh_cum0, zh_cum0   ! old cumulative enthlapies and layers interfaces
+      REAL(wp), POINTER, DIMENSION(:,:) :: zqh_cum1, zh_cum1   ! new cumulative enthlapies and layers interfaces
+      REAL(wp), POINTER, DIMENSION(:)   :: zhnew               ! new layers thicknesses
       !!-------------------------------------------------------------------
 
-      CALL wrk_alloc( jpij, snswi, nbot0, icsuind, icsuswi, icboind, icboswi, snicind, snicswi, snind )   ! integer
-      CALL wrk_alloc( jpij, zh_i, zh_s, zqsnow, zdeltah, zqti_in, zqts_in, zqti_fin, zqts_fin )           ! real
-      CALL wrk_alloc( jpij,jkmax+4, zm0, qm0, z_s, z_i, zthick0, zhl0, kjstart = 0 )
-      CALL wrk_alloc( jkmax+4,jkmax+4, zrl01, kistart = 0, kjstart = 0 )
+      CALL wrk_alloc( jpij, nlay_i+3, zqh_cum0, zh_cum0, kjstart = 0 )
+      CALL wrk_alloc( jpij, nlay_i+1, zqh_cum1, zh_cum1, kjstart = 0 )
+      CALL wrk_alloc( jpij, zhnew )
 
-      zthick0(:,:) = 0._wp
-      zm0    (:,:) = 0._wp
-      qm0    (:,:) = 0._wp
-      zrl01  (:,:) = 0._wp
-      zhl0   (:,:) = 0._wp
-      z_i    (:,:) = 0._wp
-      z_s    (:,:) = 0._wp
-
-      !
-      !------------------------------------------------------------------------------|
-      !  1) Grid                                                                     |
-      !------------------------------------------------------------------------------|
-      nlays0 = nlay_s
-      nlayi0 = nlay_i
-
-      DO ji = kideb, kiut
-         zh_i(ji) = old_ht_i_b(ji) / REAL( nlay_i ) 
-         zh_s(ji) = old_ht_s_b(ji) / REAL( nlay_s )
-      END DO
-
-      !
-      !------------------------------------------------------------------------------|
-      !  2) Switches                                                                 |
-      !------------------------------------------------------------------------------|
-      ! 2.1 snind(ji), snswi(ji)
-      ! snow surface behaviour : computation of snind(ji)-snswi(ji)
-      ! snind(ji) : index which equals 
-      ! 	 0 if snow is accumulating
-      ! 	 1 if 1st layer is melting
-      ! 	 2 if 2nd layer is melting ...
-      DO ji = kideb, kiut
-         snind  (ji) = 0
-         zdeltah(ji) = 0._wp
-      ENDDO !ji
-
-      DO jk = 1, nlays0
+      !--------------------------------------------------------------------------
+      !  1) Cumulative integral of old enthalpy * thicnkess and layers interfaces
+      !--------------------------------------------------------------------------
+      zqh_cum0(:,0:nlay_i+2) = 0._wp 
+      zh_cum0 (:,0:nlay_i+2) = 0._wp
+      DO jk0 = 1, nlay_i+2
          DO ji = kideb, kiut
-            snind(ji)  = jk        *      NINT(MAX(0.0,SIGN(1.0,-dh_s_tot(ji)-zdeltah(ji)))) &
-               + snind(ji) * (1 - NINT(MAX(0.0,SIGN(1.0,-dh_s_tot(ji)-zdeltah(ji)))))
-            zdeltah(ji)= zdeltah(ji) + zh_s(ji)
-         END DO ! ji
-      END DO ! jk
-
-      ! snswi(ji) : switch which value equals 1 if snow melts
-      !				   0 if not
-      DO ji = kideb, kiut
-         snswi(ji)     = MAX(0,NINT(-dh_s_tot(ji)/MAX(epsi20,ABS(dh_s_tot(ji)))))
-      END DO ! ji
-
-      ! 2.2 icsuind(ji), icsuswi(ji)
-      ! ice surface behaviour : computation of icsuind(ji)-icsuswi(ji)
-      ! icsuind(ji) : index which equals
-      !	   0 if nothing happens at the surface
-      !	   1 if first layer is melting
-      !	   2 if 2nd layer is reached by melt ...
-      DO ji = kideb, kiut
-         icsuind(ji) = 0
-         zdeltah(ji) = 0._wp
-      END DO !ji
-      DO jk = 1, nlayi0
-         DO ji = kideb, kiut
-            icsuind(ji) = jk          *      NINT(MAX(0.0,SIGN(1.0,-dh_i_surf(ji)-zdeltah(ji)))) &
-               + icsuind(ji) * (1 - NINT(MAX(0.0,SIGN(1.0,-dh_i_surf(ji)-zdeltah(ji)))))
-            zdeltah(ji) = zdeltah(ji) + zh_i(ji)
-         END DO ! ji
-      ENDDO !jk
-
-      ! icsuswi(ji) : switch which equals 
-      ! 	   1 if ice melts at the surface
-      ! 	   0 if not
-      DO ji = kideb, kiut
-         icsuswi(ji)  = MAX(0,NINT(-dh_i_surf(ji)/MAX(epsi20 , ABS(dh_i_surf(ji)) ) ) )
+            zqh_cum0(ji,jk0) = zqh_cum0(ji,jk0-1) + qh_i_old(ji,jk0-1)
+            zh_cum0 (ji,jk0) = zh_cum0 (ji,jk0-1) + h_i_old (ji,jk0-1)
+         ENDDO
       ENDDO
 
-      ! 2.3 icboind(ji), icboswi(ji)
-      ! ice bottom behaviour : computation of icboind(ji)-icboswi(ji)
-      ! icboind(ji) : index which equals
-      ! 	   0 if accretion is on the way
-      ! 	   1 if last layer has started to melt
-      ! 	   2 if penultiem layer is melting ... and so on
-      !            N+1 if all layers melt and that snow transforms into ice
-      DO ji = kideb, kiut 
-         icboind(ji) = 0
-         zdeltah(ji) = 0._wp
-      END DO
-      DO jk = nlayi0, 1, -1
-         DO ji = kideb, kiut
-            icboind(ji) = (nlayi0+1-jk) *      NINT(MAX(0.0,SIGN(1.0,-dh_i_bott(ji)-zdeltah(ji)))) &
-               &          + icboind(ji) * (1 - NINT(MAX(0.0,SIGN(1.0,-dh_i_bott(ji)-zdeltah(ji))))) 
-            zdeltah(ji) = zdeltah(ji) + zh_i(ji)
-         END DO
-      END DO
-
+      !------------------------------------
+      !  2) Interpolation on the new layers
+      !------------------------------------
+      ! new layer thickesses
       DO ji = kideb, kiut
-         ! case of total ablation with remaining snow
-         IF ( ( ht_i_b(ji) .GT. epsi20 ) .AND. &
-            ( ht_i_b(ji) - dh_snowice(ji) .LT. epsi20 ) ) icboind(ji) = nlay_i + 1
-      END DO
-
-      ! icboswi(ji) : switch which equals 
-      ! 	   1 if ice accretion is on the way
-      ! 	   0 if ablation is on the way
-      DO ji = kideb, kiut 
-         icboswi(ji) = MAX(0,NINT(dh_i_bott(ji) / MAX(epsi20,ABS(dh_i_bott(ji)))))
-      END DO
-
-      ! 2.4 snicind(ji), snicswi(ji)
-      ! snow ice formation : calcul de snicind(ji)-snicswi(ji)
-      ! snicind(ji) : index which equals 
-      ! 	   0 if no snow-ice forms
-      ! 	   1 if last layer of snow has started to melt
-      ! 	   2 if penultiem layer ...
-      DO ji = kideb, kiut
-         snicind(ji) = 0
-         zdeltah(ji) = 0._wp
-      END DO
-      DO jk = nlays0, 1, -1
-         DO ji = kideb, kiut
-            snicind(ji) = (nlays0+1-jk) &
-               *      NINT(MAX(0.0,SIGN(1.0,dh_snowice(ji)-zdeltah(ji)))) + snicind(ji)   &
-               * (1 - NINT(MAX(0.0,SIGN(1.0,dh_snowice(ji)-zdeltah(ji)))))
-            zdeltah(ji) = zdeltah(ji) + zh_s(ji)
-         END DO
-      END DO
-
-      ! snicswi(ji) : switch which equals 
-      ! 	   1 if snow-ice forms
-      ! 	   0 if not
-      DO ji = kideb, kiut
-         snicswi(ji)   = MAX(0,NINT(dh_snowice(ji)/MAX(epsi20,ABS(dh_snowice(ji)))))
+         zhnew(ji) = SUM( h_i_old(ji,0:nlay_i+1) ) / REAL( nlay_i )  
       ENDDO
 
-      !
-      !------------------------------------------------------------------------------|
-      !  3) Snow redistribution                                                      |
-      !------------------------------------------------------------------------------|
-      !
-      !-------------
-      ! Old profile
-      !-------------
-
-      ! by 'old', it is meant that layers coming from accretion are included, 
-      ! and that interfacial layers which were partly melted are reduced 
-
-      ! indexes of the vectors
-      !------------------------
-      ntop0    =  1
-      maxnbot0 =  0
-
-      DO ji = kideb, kiut
-         nbot0(ji) =  nlays0  + 1 - snind(ji) + ( 1 - snicind(ji) ) * snicswi(ji)
-         ! cotes of the top of the layers
-         zm0(ji,0) =  0._wp
-         maxnbot0 =  MAX ( maxnbot0 , nbot0(ji) )
-      END DO
-      IF( lk_mpp )   CALL mpp_max( maxnbot0, kcom=ncomm_ice )
-
-      DO jk = 1, maxnbot0
+      ! new layers interfaces
+      zh_cum1(:,0:nlay_i) = 0._wp
+      DO jk1 = 1, nlay_i
          DO ji = kideb, kiut
-            !change
-            limsum = ( 1 - snswi(ji) ) * ( jk - 1 ) + snswi(ji) * ( jk + snind(ji) - 1 )
-            limsum = MIN( limsum , nlay_s )
-            zm0(ji,jk) =  dh_s_tot(ji) + zh_s(ji) * REAL( limsum )
-         END DO
-      END DO
-
-      DO ji = kideb, kiut
-         zm0(ji,nbot0(ji)) =  dh_s_tot(ji) - REAL( snicswi(ji) ) * dh_snowice(ji) + zh_s(ji) * REAL( nlays0 )
-         zm0(ji,1)         =  dh_s_tot(ji) * REAL( 1 - snswi(ji) ) + REAL( snswi(ji) ) * zm0(ji,1)
-      END DO
-
-      DO jk = ntop0, maxnbot0
-         DO ji = kideb, kiut
-            zthick0(ji,jk)  =  zm0(ji,jk) - zm0(ji,jk-1)            ! layer thickness
-         END DO
-      END DO
-
-      zqts_in(:) = 0._wp
-
-      DO ji = kideb, kiut         ! layer heat content
-         qm0    (ji,1) =  rhosn * (  cpic * ( rtt - REAL( 1 - snswi(ji) ) * tatm_ice_1d(ji)        &
-            &                                         - REAL( snswi(ji) ) * t_s_b      (ji,1)  )   &
-            &                      + lfus  ) * zthick0(ji,1)
-         zqts_in(ji)   =  zqts_in(ji) + qm0(ji,1) 
-      END DO
-
-      DO jk = 2, maxnbot0
-         DO ji = kideb, kiut
-            limsum      = ( 1 - snswi(ji) ) * ( jk - 1 ) + snswi(ji) * ( jk + snind(ji) - 1 )
-            limsum      = MIN( limsum , nlay_s )
-            qm0(ji,jk)  = rhosn * ( cpic * ( rtt - t_s_b(ji,limsum) ) + lfus ) * zthick0(ji,jk)
-            zswitch = 1.0 - MAX (0.0, SIGN ( 1.0, - ht_s_b(ji) ) )
-            zqts_in(ji) = zqts_in(ji) + REAL( 1 - snswi(ji) ) * qm0(ji,jk) * zswitch
-         END DO ! jk
-      END DO ! ji
-
-      !------------------------------------------------
-      ! Energy given by the snow in snow-ice formation
-      !------------------------------------------------
-      ! zqsnow, enthalpy of the flooded snow
-      DO ji = kideb, kiut
-         zqsnow (ji) =  rhosn * lfus
-         zdeltah(ji) =  0._wp
-      END DO
-
-      DO jk =  nlays0, 1, -1
-         DO ji = kideb, kiut
-            zhsnow =  MAX( 0._wp , dh_snowice(ji)-zdeltah(ji) )
-            zqsnow (ji) =  zqsnow (ji) + rhosn*cpic*(rtt-t_s_b(ji,jk))
-            zdeltah(ji) =  zdeltah(ji) + zh_s(ji)
-         END DO
-      END DO
-
-      DO ji = kideb, kiut
-         zqsnow(ji) = zqsnow(ji) * dh_snowice(ji)
-      END DO
-
-      !------------------
-      ! new snow profile
-      !------------------
-
-      !--------------
-      ! Vector index   
-      !--------------
-      ntop1 =  1
-      nbot1 =  nlay_s
-
-      !-------------------
-      ! Layer coordinates 
-      !-------------------
-      DO ji = kideb, kiut
-         zh_s(ji)  = ht_s_b(ji) / REAL( nlay_s )
-         z_s(ji,0) =  0._wp
+            zh_cum1(ji,jk1) = zh_cum1(ji,jk1-1) + zhnew(ji)
+         ENDDO
       ENDDO
 
-      DO jk = 1, nlay_s
+      zqh_cum1(:,0:nlay_i) = 0._wp 
+      ! new cumulative q*h => linear interpolation
+      DO jk0 = 1, nlay_i+1
+         DO jk1 = 1, nlay_i-1
+            DO ji = kideb, kiut
+               IF( zh_cum1(ji,jk1) <= zh_cum0(ji,jk0) .AND. zh_cum1(ji,jk1) > zh_cum0(ji,jk0-1) ) THEN
+                  zqh_cum1(ji,jk1) = ( zqh_cum0(ji,jk0-1) * ( zh_cum0(ji,jk0) - zh_cum1(ji,jk1  ) ) +  &
+                     &                 zqh_cum0(ji,jk0  ) * ( zh_cum1(ji,jk1) - zh_cum0(ji,jk0-1) ) )  &
+                     &             / ( zh_cum0(ji,jk0) - zh_cum0(ji,jk0-1) )
+               ENDIF
+            ENDDO
+         ENDDO
+      ENDDO
+      ! to ensure that total heat content is strictly conserved, set:
+      zqh_cum1(:,nlay_i) = zqh_cum0(:,nlay_i+2) 
+
+      ! new enthalpies
+      DO jk1 = 1, nlay_i
          DO ji = kideb, kiut
-            z_s(ji,jk) =  zh_s(ji) * REAL( jk )
-         END DO
-      END DO
-
-      !-----------------
-      ! Layer thickness
-      !-----------------
-      DO layer0 = ntop0, maxnbot0
-         DO ji = kideb, kiut
-            zhl0(ji,layer0) = zm0(ji,layer0) - zm0(ji,layer0-1)
-         END DO
-      END DO
-
-      DO layer1 = ntop1, nbot1
-         DO ji = kideb, kiut
-            q_s_b(ji,layer1) = 0._wp
-         END DO
-      END DO
-
-      !----------------
-      ! Weight factors
-      !----------------
-      DO layer0 = ntop0, maxnbot0
-         DO layer1 = ntop1, nbot1
-            DO ji = kideb, kiut
-               zinda = MAX( 0._wp, SIGN( 1._wp , zhl0(ji,layer0) - epsi10 ) )
-               zrl01(layer1,layer0) = zinda * MAX(0.0,( MIN(zm0(ji,layer0),z_s(ji,layer1))   &
-                  &                 - MAX(zm0(ji,layer0-1), z_s(ji,layer1-1))) / MAX(zhl0(ji,layer0),epsi10)) 
-               q_s_b(ji,layer1) = q_s_b(ji,layer1) + zrl01(layer1,layer0)*qm0(ji,layer0)   &
-                  &                                * MAX(0.0,SIGN(1.0,REAL(nbot0(ji)-layer0)))
-            END DO
-         END DO
-      END DO
-
-      ! Heat conservation
-      zqts_fin(:) = 0._wp
-      DO jk = 1, nlay_s
-         DO ji = kideb, kiut
-            zqts_fin(ji) = zqts_fin(ji) + q_s_b(ji,jk)
-         END DO
-      END DO
-
-      IF ( con_i .AND. jiindex_1d > 0 ) THEN
-         DO ji = kideb, kiut
-            IF ( ABS ( zqts_in(ji) - zqts_fin(ji) ) * r1_rdtice  >  1.0e-6 ) THEN
-               ii                 = MOD( npb(ji) - 1, jpi ) + 1
-               ij                 = ( npb(ji) - 1 ) / jpi + 1
-               WRITE(numout,*) ' violation of heat conservation : ', ABS ( zqts_in(ji) - zqts_fin(ji) ) * r1_rdtice
-               WRITE(numout,*) ' ji, jj   : ', ii, ij
-               WRITE(numout,*) ' ht_s_b   : ', ht_s_b(ji)
-               WRITE(numout,*) ' zqts_in  : ', zqts_in (ji) * r1_rdtice
-               WRITE(numout,*) ' zqts_fin : ', zqts_fin(ji) * r1_rdtice
-               WRITE(numout,*) ' dh_snowice : ', dh_snowice(ji)
-               WRITE(numout,*) ' dh_s_tot : ', dh_s_tot(ji)
-               WRITE(numout,*) ' snswi    : ', snswi(ji)
-            ENDIF
-         END DO
-      ENDIF
-
-      !---------------------
-      ! Recover heat content
-      !---------------------
-      DO jk = 1, nlay_s
-         DO ji = kideb, kiut
-            zinda = MAX( 0._wp, SIGN( 1._wp , zh_s(ji) - epsi10 ) )        
-            q_s_b(ji,jk) = zinda * q_s_b(ji,jk) / MAX( zh_s(ji) , epsi10 )
-         END DO !ji
-      END DO !jk  
-
-      !---------------------
-      ! Recover temperature
-      !---------------------
-      zfac1 = 1. / ( rhosn * cpic )
-      zfac2 = lfus / cpic  
-      DO jk = 1, nlay_s
-         DO ji = kideb, kiut
-            zswitch = MAX ( 0.0 , SIGN ( 1.0, - ht_s_b(ji) ) )
-            t_s_b(ji,jk) = rtt + ( 1.0 - zswitch ) * ( - zfac1 * q_s_b(ji,jk) + zfac2 )
-         END DO
-      END DO
-      !
-      !------------------------------------------------------------------------------|
-      !  4) Ice redistribution                                                       |
-      !------------------------------------------------------------------------------|
-      !
-      !-------------
-      ! OLD PROFILE 
-      !-------------
-
-      !----------------
-      ! Vector indexes
-      !----------------
-      ntop0    =  1
-      maxnbot0 =  0
-
-      DO ji = kideb, kiut
-         ! reference number of the bottommost layer
-         nbot0(ji) =  MAX( 1 ,  MIN( nlayi0 + ( 1 - icboind(ji) ) +        &
-            &                           ( 1 - icsuind(ji) ) * icsuswi(ji) + snicswi(ji) , nlay_i + 2 ) )
-         ! maximum reference number of the bottommost layer over all domain
-         maxnbot0 =  MAX( maxnbot0 , nbot0(ji) )
-      END DO
-
-      !-------------------------
-      ! Cotes of old ice layers
-      !-------------------------
-      zm0(:,0) =  0._wp
-
-      DO jk = 1, maxnbot0
-         DO ji = kideb, kiut
-            ! jk goes from 1 to nbot0
-            ! the ice layer number goes from 1 to nlay_i
-            ! limsum is the real ice layer number corresponding to present jk
-            limsum    =  ( (icsuswi(ji)*(icsuind(ji)+jk-1) + & 
-               (1-icsuswi(ji))*jk))*(1-snicswi(ji)) + (jk-1)*snicswi(ji)
-            zm0(ji,jk)=  REAL(icsuswi(ji))*dh_i_surf(ji) + REAL(snicswi(ji))*dh_snowice(ji) &
-               +  REAL(limsum) * zh_i(ji)
-         END DO
-      END DO
-
-      DO ji = kideb, kiut
-         zm0(ji,nbot0(ji)) =  REAL(icsuswi(ji))*dh_i_surf(ji) + REAL(snicswi(ji))*dh_snowice(ji) + dh_i_bott(ji) &
-            +  zh_i(ji) * REAL(nlayi0)
-         zm0(ji,1)         =  REAL(snicswi(ji))*dh_snowice(ji) + REAL(1-snicswi(ji))*zm0(ji,1)
-      END DO
-
-      !-----------------------------
-      ! Thickness of old ice layers
-      !-----------------------------
-      DO jk = ntop0, maxnbot0
-         DO ji = kideb, kiut
-            zthick0(ji,jk) =  zm0(ji,jk) - zm0(ji,jk-1)
-         END DO
-      END DO
-
-      !---------------------------
-      ! Inner layers heat content
-      !---------------------------
-      qm0(:,:) =  0.0
-      zqti_in(:) = 0.0
-
-      DO jk = ntop0, maxnbot0
-         DO ji = kideb, kiut
-            limsum =  MAX(1,MIN(snicswi(ji)*(jk-1) + icsuswi(ji)*(jk-1+icsuind(ji)) + &
-               (1-icsuswi(ji))*(1-snicswi(ji))*jk,nlay_i))
-            ztmelts = -tmut * s_i_b(ji,limsum) + rtt
-            qm0(ji,jk) = rhoic * ( cpic * (ztmelts-t_i_b(ji,limsum)) + lfus * ( 1.0-(ztmelts-rtt)/ &
-               MIN((t_i_b(ji,limsum)-rtt),-epsi20) ) - rcp*(ztmelts-rtt) ) &
-               * zthick0(ji,jk)
-         END DO
-      END DO
-
-      !----------------------------
-      ! Bottom layers heat content
-      !----------------------------
-      DO ji = kideb, kiut        
-         ztmelts    = REAL( 1 - icboswi(ji) ) * (-tmut * s_i_b  (ji,nlayi0) )   &   ! case of melting ice
-            &       +     REAL( icboswi(ji) ) * (-tmut * s_i_new(ji)        )   &   ! case of forming ice
-            &       + rtt                                                         ! in Kelvin
-
-         ! bottom formation temperature
-         ztform = t_i_b(ji,nlay_i)
-         IF(  num_sal == 2  )   ztform = t_bo_b(ji)
-         qm0(ji,nbot0(ji)) = REAL( 1 - icboswi(ji) )*qm0(ji,nbot0(ji))             &   ! case of melting ice
-            &              + REAL( icboswi(ji) ) * rhoic * ( cpic*(ztmelts-ztform)       &   ! case of forming ice
-            + lfus *( 1.0-(ztmelts-rtt) / MIN ( (ztform-rtt) , - epsi10 ) )      & 
-            - rcp*(ztmelts-rtt) ) * zthick0(ji,nbot0(ji)  )
-      END DO
-
-      !-----------------------------
-      ! Snow ice layer heat content
-      !-----------------------------
-      DO ji = kideb, kiut
-         ! energy of the flooding seawater
-         zqsnic = rau0 * rcp * ( rtt - t_bo_b(ji) ) * dh_snowice(ji) * &
-            (rhoic - rhosn) / rhoic * REAL(snicswi(ji)) ! generally positive
-         ! Heat conservation diagnostic
-         qt_i_in(ji,jl) = qt_i_in(ji,jl) + zqsnic 
-
-         qldif_1d(ji)   = qldif_1d(ji) + zqsnic * a_i_b(ji)
-
-         ! enthalpy of the newly formed snow-ice layer
-         ! = enthalpy of snow + enthalpy of frozen water
-         zqsnic         =  zqsnow(ji) + zqsnic
-         qm0(ji,1)      =  REAL(snicswi(ji)) * zqsnic + REAL( 1 - snicswi(ji) ) * qm0(ji,1)
-
-      END DO ! ji
-
-      DO jk = ntop0, maxnbot0
-         DO ji = kideb, kiut
-            ! Heat conservation
-            zqti_in(ji) = zqti_in(ji) + qm0(ji,jk) * MAX( 0.0 , SIGN(1.0,ht_i_b(ji)-epsi10) ) &
-               &                                   * MAX( 0.0 , SIGN( 1. , REAL(nbot0(ji) - jk) ) )
-         END DO
-      END DO
-
-      !-------------
-      ! NEW PROFILE
-      !-------------
-
-      !---------------
-      ! Vectors index
-      !---------------
-      ntop1 =  1 
-      nbot1 =  nlay_i
-
-      !------------------
-      ! Layers thickness 
-      !------------------
-      DO ji = kideb, kiut
-         zh_i(ji) = ht_i_b(ji) / REAL( nlay_i )
+            rswitch      = 1._wp - MAX( 0._wp , SIGN( 1._wp , - zhnew(ji) + epsi10 ) ) 
+            qnew(ji,jk1) = rswitch * ( zqh_cum1(ji,jk1) - zqh_cum1(ji,jk1-1) ) / MAX( zhnew(ji), epsi10 )
+         ENDDO
       ENDDO
 
-      !-------------
-      ! Layer cotes      
-      !-------------
-      z_i(:,0) =  0._wp
-      DO jk = 1, nlay_i
-         DO ji = kideb, kiut
-            z_i(ji,jk) =  zh_i(ji) * jk
-         END DO
+      ! --- diag error on heat remapping --- !
+      ! comment: if input h_i_old and qh_i_old are already multiplied by a_i (as in limthd_lac), 
+      ! then we should not (* a_i) again but not important since this is just to check that remap error is ~0
+      DO ji = kideb, kiut
+         hfx_err_rem_1d(ji) = hfx_err_rem_1d(ji) + a_i_1d(ji) * r1_rdtice *  &
+            &               ( SUM( qnew(ji,1:nlay_i) ) * zhnew(ji) - SUM( qh_i_old(ji,0:nlay_i+1) ) ) 
       END DO
-
-      !--thicknesses of the layers
-      DO layer0 = ntop0, maxnbot0
-         DO ji = kideb, kiut
-            zhl0(ji,layer0) = zm0(ji,layer0) - zm0(ji,layer0-1)   ! thicknesses of the layers
-         END DO
-      END DO
-
-      !------------------------
-      ! Weights for relayering
-      !------------------------
-      q_i_b(:,:) = 0._wp
-      DO layer0 = ntop0, maxnbot0
-         DO layer1 = ntop1, nbot1
-            DO ji = kideb, kiut
-               zinda = MAX( 0._wp, SIGN( 1._wp , zhl0(ji,layer0) - epsi10 ) )
-               zrl01(layer1,layer0) = zinda * MAX(0.0,( MIN(zm0(ji,layer0),z_i(ji,layer1)) &
-                  - MAX(zm0(ji,layer0-1), z_i(ji,layer1-1)))/MAX(zhl0(ji,layer0),epsi10))
-               q_i_b(ji,layer1) = q_i_b(ji,layer1) & 
-                  + zrl01(layer1,layer0)*qm0(ji,layer0) &
-                  * MAX(0.0,SIGN(1.0,ht_i_b(ji)-epsi10)) &
-                  * MAX(0.0,SIGN(1.0,REAL(nbot0(ji)-layer0)))
-            END DO
-         END DO
-      END DO
-
-      !-------------------------
-      ! Heat conservation check
-      !-------------------------
-      zqti_fin(:) = 0._wp
-      DO jk = 1, nlay_i
-         DO ji = kideb, kiut
-            zqti_fin(ji) = zqti_fin(ji) + q_i_b(ji,jk)
-         END DO
-      END DO
+      
       !
-      IF ( con_i .AND. jiindex_1d > 0 ) THEN
-         DO ji = kideb, kiut
-            IF ( ABS ( zqti_in(ji) - zqti_fin(ji) ) * r1_rdtice  >  1.0e-6 ) THEN
-               ii                 = MOD( npb(ji) - 1, jpi ) + 1
-               ij                 = ( npb(ji) - 1 ) / jpi + 1
-               WRITE(numout,*) ' violation of heat conservation : ', ABS ( zqti_in(ji) - zqti_fin(ji) ) * r1_rdtice
-               WRITE(numout,*) ' ji, jj   : ', ii, ij
-               WRITE(numout,*) ' ht_i_b   : ', ht_i_b(ji)
-               WRITE(numout,*) ' zqti_in  : ', zqti_in (ji) * r1_rdtice
-               WRITE(numout,*) ' zqti_fin : ', zqti_fin(ji) * r1_rdtice
-               WRITE(numout,*) ' dh_i_bott: ', dh_i_bott(ji)
-               WRITE(numout,*) ' dh_i_surf: ', dh_i_surf(ji)
-               WRITE(numout,*) ' dh_snowice:', dh_snowice(ji)
-               WRITE(numout,*) ' icsuswi  : ', icsuswi(ji)
-               WRITE(numout,*) ' icboswi  : ', icboswi(ji)
-               WRITE(numout,*) ' snicswi  : ', snicswi(ji)
-            ENDIF
-         END DO
-      ENDIF
-
-      !----------------------
-      ! Recover heat content 
-      !----------------------
-      DO jk = 1, nlay_i
-         DO ji = kideb, kiut
-            zinda = MAX( 0._wp, SIGN( 1._wp , zh_i(ji) - epsi10 ) )
-            q_i_b(ji,jk) = zinda * q_i_b(ji,jk) / MAX( zh_i(ji) , epsi10 )
-         END DO !ji
-      END DO !jk  
-
-      ! Heat conservation
-      zqti_fin(:) = 0.0
-      DO jk = 1, nlay_i
-         DO ji = kideb, kiut
-            zqti_fin(ji) = zqti_fin(ji) + q_i_b(ji,jk) * zh_i(ji)
-         END DO
-      END DO
-
-      !
-      !------------------------------------------------------------------------------|
-      !  5) Update salinity and recover temperature                                  |
-      !------------------------------------------------------------------------------|
-      !
-      ! Update salinity (basal entrapment, snow ice formation)
-      DO ji = kideb, kiut
-         sm_i_b(ji) = sm_i_b(ji) + dsm_i_se_1d(ji) + dsm_i_si_1d(ji)
-      END DO !ji
-
-      ! Recover temperature
-      DO jk = 1, nlay_i
-         DO ji = kideb, kiut
-            ztmelts    =  -tmut*s_i_b(ji,jk) + rtt
-            !Conversion q(S,T) -> T (second order equation)
-            zaaa         =  cpic
-            zbbb         =  ( rcp - cpic ) * ( ztmelts - rtt ) + q_i_b(ji,jk) / rhoic - lfus
-            zccc         =  lfus * ( ztmelts - rtt )
-            zdiscrim     =  SQRT( MAX(zbbb*zbbb - 4.0*zaaa*zccc,0.0) )
-            t_i_b(ji,jk) =  rtt - ( zbbb + zdiscrim ) / ( 2.0 *zaaa )
-         END DO !ji
-
-      END DO !jk
-      !
-      CALL wrk_dealloc( jpij, snswi, nbot0, icsuind, icsuswi, icboind, icboswi, snicind, snicswi, snind )   ! integer
-      CALL wrk_dealloc( jpij, zh_i, zh_s, zqsnow, zdeltah, zqti_in, zqts_in, zqti_fin, zqts_fin )           ! real
-      CALL wrk_dealloc( jpij,jkmax+4, zm0, qm0, z_s, z_i, zthick0, zhl0, kjstart = 0 )
-      CALL wrk_dealloc( jkmax+4,jkmax+4, zrl01, kistart = 0, kjstart = 0 )
+      CALL wrk_dealloc( jpij, nlay_i+3, zqh_cum0, zh_cum0, kjstart = 0 )
+      CALL wrk_dealloc( jpij, nlay_i+1, zqh_cum1, zh_cum1, kjstart = 0 )
+      CALL wrk_dealloc( jpij, zhnew )
       !
    END SUBROUTINE lim_thd_ent
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd_lac.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd_lac.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd_lac.F90	(revision 5038)
@@ -29,5 +29,7 @@
    USE lib_mpp        ! MPP library
    USE wrk_nemo       ! work arrays
+   USE lbclnk         ! ocean lateral boundary conditions (or mpp link)
    USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
+   USE limthd_ent
 
    IMPLICIT NONE
@@ -35,8 +37,4 @@
 
    PUBLIC lim_thd_lac     ! called by lim_thd
-
-   REAL(wp) ::   epsi10 = 1.e-10_wp   !
-   REAL(wp) ::   zzero  = 0._wp      !
-   REAL(wp) ::   zone   = 1._wp      !
 
    !!----------------------------------------------------------------------
@@ -71,16 +69,23 @@
       !!             - Computation of variation of ice volume and mass
       !!             - Computation of frldb after lateral accretion and 
-      !!               update ht_s_b, ht_i_b and tbif_1d(:,:)      
+      !!               update ht_s_1d, ht_i_1d and tbif_1d(:,:)      
       !!------------------------------------------------------------------------
-      INTEGER ::   ji,jj,jk,jl,jm   ! dummy loop indices
-      INTEGER ::   layer, nbpac     ! local integers 
-      INTEGER ::   ii, ij, iter   !   -       -
-      REAL(wp)  ::   ztmelts, zdv, zqold, zfrazb, zweight, zalphai, zindb, zinda, zde  ! local scalars
+      INTEGER ::   ji,jj,jk,jl      ! dummy loop indices
+      INTEGER ::   nbpac            ! local integers 
+      INTEGER ::   ii, ij, iter     !   -       -
+      REAL(wp)  ::   ztmelts, zdv, zfrazb, zweight, zde                         ! local scalars
       REAL(wp) ::   zgamafr, zvfrx, zvgx, ztaux, ztwogp, zf , zhicol_new        !   -      -
       REAL(wp) ::   ztenagm, zvfry, zvgy, ztauy, zvrel2, zfp, zsqcd , zhicrit   !   -      -
       LOGICAL  ::   iterate_frazil   ! iterate frazil ice collection thickness
       CHARACTER (len = 15) :: fieldid
-      !
-      INTEGER , POINTER, DIMENSION(:) ::   zcatac      ! indexes of categories where new ice grows
+
+      REAL(wp) ::   zQm          ! enthalpy exchanged with the ocean (J/m2, >0 towards ocean)
+      REAL(wp) ::   zEi          ! sea ice specific enthalpy (J/kg)
+      REAL(wp) ::   zEw          ! seawater specific enthalpy (J/kg)
+      REAL(wp) ::   zfmdt        ! mass flux x time step (kg/m2, >0 towards ocean)
+     
+      REAL(wp) ::   zv_newfra
+  
+      INTEGER , POINTER, DIMENSION(:) ::   jcat        ! indexes of categories where new ice grows
       REAL(wp), POINTER, DIMENSION(:) ::   zswinew     ! switch for new ice or not
 
@@ -93,56 +98,26 @@
       REAL(wp), POINTER, DIMENSION(:) ::   zdv_res     ! residual volume in case of excessive heat budget
       REAL(wp), POINTER, DIMENSION(:) ::   zda_res     ! residual area in case of excessive heat budget
-      REAL(wp), POINTER, DIMENSION(:) ::   zat_i_ac    ! total ice fraction    
-      REAL(wp), POINTER, DIMENSION(:) ::   zat_i_lev   ! total ice fraction for level ice only (type 1)   
-      REAL(wp), POINTER, DIMENSION(:) ::   zdh_frazb   ! accretion of frazil ice at the ice bottom
-      REAL(wp), POINTER, DIMENSION(:) ::   zvrel_ac    ! relative ice / frazil velocity (1D vector)
-
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zhice_old   ! previous ice thickness
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zdummy      ! dummy thickness of new ice 
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zdhicbot    ! thickness of new ice which is accreted vertically
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zv_old      ! old volume of ice in category jl
-      REAL(wp), POINTER, DIMENSION(:,:) ::   za_old      ! old area of ice in category jl
-      REAL(wp), POINTER, DIMENSION(:,:) ::   za_i_ac     ! 1-D version of a_i
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zv_i_ac     ! 1-D version of v_i
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zoa_i_ac    ! 1-D version of oa_i
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zsmv_i_ac   ! 1-D version of smv_i
-
-      REAL(wp), POINTER, DIMENSION(:,:,:) ::   ze_i_ac   !: 1-D version of e_i
-
-      REAL(wp), POINTER, DIMENSION(:) ::   zqbgow    ! heat budget of the open water (negative)
-      REAL(wp), POINTER, DIMENSION(:) ::   zdhex     ! excessively thick accreted sea ice (hlead-hice)
-
-      REAL(wp), POINTER, DIMENSION(:,:,:) ::   zqm0      ! old layer-system heat content
-      REAL(wp), POINTER, DIMENSION(:,:,:) ::   zthick0   ! old ice thickness
-
-      REAL(wp), POINTER, DIMENSION(:,:) ::   vt_i_init, vt_i_final   ! ice volume summed over categories
-      REAL(wp), POINTER, DIMENSION(:,:) ::   vt_s_init, vt_s_final   !  snow volume summed over categories
-      REAL(wp), POINTER, DIMENSION(:,:) ::   et_i_init, et_i_final   !  ice energy summed over categories
-      REAL(wp), POINTER, DIMENSION(:,:) ::   et_s_init               !  snow energy summed over categories
+      REAL(wp), POINTER, DIMENSION(:) ::   zat_i_1d    ! total ice fraction    
+      REAL(wp), POINTER, DIMENSION(:) ::   zv_frazb    ! accretion of frazil ice at the ice bottom
+      REAL(wp), POINTER, DIMENSION(:) ::   zvrel_1d    ! relative ice / frazil velocity (1D vector)
+
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zv_b      ! old volume of ice in category jl
+      REAL(wp), POINTER, DIMENSION(:,:) ::   za_b      ! old area of ice in category jl
+      REAL(wp), POINTER, DIMENSION(:,:) ::   za_i_1d   ! 1-D version of a_i
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zv_i_1d   ! 1-D version of v_i
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zoa_i_1d  ! 1-D version of oa_i
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zsmv_i_1d ! 1-D version of smv_i
+
+      REAL(wp), POINTER, DIMENSION(:,:,:) ::   ze_i_1d   !: 1-D version of e_i
+
       REAL(wp), POINTER, DIMENSION(:,:) ::   zvrel                   ! relative ice / frazil velocity
       !!-----------------------------------------------------------------------!
 
-      CALL wrk_alloc( jpij, zcatac )   ! integer
+      CALL wrk_alloc( jpij, jcat )   ! integer
       CALL wrk_alloc( jpij, zswinew, zv_newice, za_newice, zh_newice, ze_newice, zs_newice, zo_newice )
-      CALL wrk_alloc( jpij, zdv_res, zda_res, zat_i_ac, zat_i_lev, zdh_frazb, zvrel_ac, zqbgow, zdhex )
-      CALL wrk_alloc( jpij,jpl, zhice_old, zdummy, zdhicbot, zv_old, za_old, za_i_ac, zv_i_ac, zoa_i_ac, zsmv_i_ac )
-      CALL wrk_alloc( jpij,jkmax,jpl, ze_i_ac )
-      CALL wrk_alloc( jpij,jkmax+1,jpl, zqm0, zthick0 )
-      CALL wrk_alloc( jpi,jpj, vt_i_init, vt_i_final, vt_s_init, vt_s_final, et_i_init, et_i_final, et_s_init, zvrel )
-
-      et_i_init(:,:) = 0._wp
-      et_s_init(:,:) = 0._wp
-      vt_i_init(:,:) = 0._wp
-      vt_s_init(:,:) = 0._wp
-
-      !------------------------------------------------------------------------------!
-      ! 1) Conservation check and changes in each ice category
-      !------------------------------------------------------------------------------!
-      IF( con_i ) THEN
-         CALL lim_column_sum        ( jpl, v_i          , vt_i_init)
-         CALL lim_column_sum        ( jpl, v_s          , vt_s_init)
-         CALL lim_column_sum_energy ( jpl, nlay_i , e_i , et_i_init)
-         CALL lim_column_sum        ( jpl, e_s(:,:,1,:) , et_s_init)
-      ENDIF
+      CALL wrk_alloc( jpij, zdv_res, zda_res, zat_i_1d, zv_frazb, zvrel_1d )
+      CALL wrk_alloc( jpij,jpl, zv_b, za_b, za_i_1d, zv_i_1d, zoa_i_1d, zsmv_i_1d )
+      CALL wrk_alloc( jpij,nlay_i+1,jpl, ze_i_1d )
+      CALL wrk_alloc( jpi,jpj, zvrel )
 
       !------------------------------------------------------------------------------|
@@ -154,7 +129,8 @@
                DO ji = 1, jpi
                   !Energy of melting q(S,T) [J.m-3]
-                  e_i(ji,jj,jk,jl) = e_i(ji,jj,jk,jl) / MAX( area(ji,jj) * v_i(ji,jj,jl) ,  epsi10 ) * REAL( nlay_i )
-                  zindb = 1._wp - MAX(  0._wp , SIGN( 1._wp , -v_i(ji,jj,jl) + epsi10 )  )   !0 if no ice and 1 if yes
-                  e_i(ji,jj,jk,jl) = e_i(ji,jj,jk,jl) * unit_fac * zindb
+                  rswitch          = 1._wp - MAX(  0._wp , SIGN( 1._wp , -v_i(ji,jj,jl) + epsi10 )  )   !0 if no ice and 1 if yes
+                  e_i(ji,jj,jk,jl) = rswitch * e_i(ji,jj,jk,jl) &
+                      &   / ( area(ji,jj) * MAX( v_i(ji,jj,jl) ,  epsi10 ) ) * REAL( nlay_i, wp )
+                  e_i(ji,jj,jk,jl) = e_i(ji,jj,jk,jl) * unit_fac
                END DO
             END DO
@@ -179,7 +155,7 @@
 
       ! Default new ice thickness 
-      hicol(:,:) = hiccrit(1)
-
-      IF( fraz_swi == 1._wp ) THEN
+      hicol(:,:) = hiccrit
+
+      IF( fraz_swi == 1 ) THEN
 
          !--------------------
@@ -193,8 +169,7 @@
          zgamafr = 0.03
 
-         DO jj = 1, jpj
-            DO ji = 1, jpi
-
-               IF ( tms(ji,jj) * ( qcmif(ji,jj) - qldif(ji,jj) ) > 0.e0 ) THEN
+         DO jj = 2, jpj
+            DO ji = 2, jpi
+               IF ( qlead(ji,jj) < 0._wp ) THEN
                   !-------------
                   ! Wind stress
@@ -206,11 +181,12 @@
                      &          +   vtau_ice(ji  ,jj  ) * tmv(ji  ,jj  ) ) * 0.5_wp
                   ! Square root of wind stress
-                  ztenagm       =  SQRT( SQRT( ztaux * ztaux + ztauy * ztauy ) )
+                  ztenagm       =  SQRT( SQRT( ztaux**2 + ztauy**2 ) )
 
                   !---------------------
                   ! Frazil ice velocity
                   !---------------------
-                  zvfrx         = zgamafr * zsqcd * ztaux / MAX(ztenagm,epsi10)
-                  zvfry         = zgamafr * zsqcd * ztauy / MAX(ztenagm,epsi10)
+                  rswitch = MAX( 0._wp, SIGN( 1._wp , ztenagm - epsi10 ) )
+                  zvfrx   = rswitch * zgamafr * zsqcd * ztaux / MAX( ztenagm, epsi10 )
+                  zvfry   = rswitch * zgamafr * zsqcd * ztauy / MAX( ztenagm, epsi10 )
 
                   !-------------------
@@ -218,9 +194,7 @@
                   !-------------------
                   ! C-grid ice velocity
-                  zindb = MAX(  0._wp, SIGN( 1._wp , at_i(ji,jj) )  )
-                  zvgx  = zindb * (  u_ice(ji-1,jj  ) * tmu(ji-1,jj  )    &
-                     &             + u_ice(ji,jj    ) * tmu(ji  ,jj  )  ) * 0.5_wp
-                  zvgy  = zindb * (  v_ice(ji  ,jj-1) * tmv(ji  ,jj-1)    &
-                     &             + v_ice(ji,jj    ) * tmv(ji  ,jj  )  ) * 0.5_wp
+                  rswitch = MAX(  0._wp, SIGN( 1._wp , at_i(ji,jj) )  )
+                  zvgx    = rswitch * ( u_ice(ji-1,jj  ) * tmu(ji-1,jj  )  + u_ice(ji,jj) * tmu(ji,jj) ) * 0.5_wp
+                  zvgy    = rswitch * ( v_ice(ji  ,jj-1) * tmv(ji  ,jj-1)  + v_ice(ji,jj) * tmv(ji,jj) ) * 0.5_wp
 
                   !-----------------------------------
@@ -264,4 +238,7 @@
             END DO ! loop on ji ends
          END DO ! loop on jj ends
+      ! 
+      CALL lbc_lnk( zvrel(:,:), 'T', 1. )
+      CALL lbc_lnk( hicol(:,:), 'T', 1. )
 
       ENDIF ! End of computation of frazil ice collection thickness
@@ -276,7 +253,9 @@
       ! This occurs if open water energy budget is negative
       nbpac = 0
+      npac(:) = 0
+      !
       DO jj = 1, jpj
          DO ji = 1, jpi
-            IF ( tms(ji,jj) * ( qcmif(ji,jj) - qldif(ji,jj) )  >  0._wp ) THEN
+            IF ( qlead(ji,jj)  <  0._wp ) THEN
                nbpac = nbpac + 1
                npac( nbpac ) = (jj - 1) * jpi + ji
@@ -290,5 +269,5 @@
          DO ji = mi0(jiindx), mi1(jiindx)
             DO jj = mj0(jjindx), mj1(jjindx)
-               IF ( tms(ji,jj) * ( qcmif(ji,jj) - qldif(ji,jj) )  >  0._wp ) THEN
+               IF ( qlead(ji,jj)  <  0._wp ) THEN
                   jiindex_1d = (jj - 1) * jpi + ji
                ENDIF
@@ -307,22 +286,24 @@
       IF ( nbpac > 0 ) THEN
 
-         CALL tab_2d_1d( nbpac, zat_i_ac  (1:nbpac)     , at_i         , jpi, jpj, npac(1:nbpac) )
+         CALL tab_2d_1d( nbpac, zat_i_1d  (1:nbpac)     , at_i         , jpi, jpj, npac(1:nbpac) )
          DO jl = 1, jpl
-            CALL tab_2d_1d( nbpac, za_i_ac  (1:nbpac,jl), a_i  (:,:,jl), jpi, jpj, npac(1:nbpac) )
-            CALL tab_2d_1d( nbpac, zv_i_ac  (1:nbpac,jl), v_i  (:,:,jl), jpi, jpj, npac(1:nbpac) )
-            CALL tab_2d_1d( nbpac, zoa_i_ac (1:nbpac,jl), oa_i (:,:,jl), jpi, jpj, npac(1:nbpac) )
-            CALL tab_2d_1d( nbpac, zsmv_i_ac(1:nbpac,jl), smv_i(:,:,jl), jpi, jpj, npac(1:nbpac) )
+            CALL tab_2d_1d( nbpac, za_i_1d  (1:nbpac,jl), a_i  (:,:,jl), jpi, jpj, npac(1:nbpac) )
+            CALL tab_2d_1d( nbpac, zv_i_1d  (1:nbpac,jl), v_i  (:,:,jl), jpi, jpj, npac(1:nbpac) )
+            CALL tab_2d_1d( nbpac, zoa_i_1d (1:nbpac,jl), oa_i (:,:,jl), jpi, jpj, npac(1:nbpac) )
+            CALL tab_2d_1d( nbpac, zsmv_i_1d(1:nbpac,jl), smv_i(:,:,jl), jpi, jpj, npac(1:nbpac) )
             DO jk = 1, nlay_i
-               CALL tab_2d_1d( nbpac, ze_i_ac(1:nbpac,jk,jl), e_i(:,:,jk,jl) , jpi, jpj, npac(1:nbpac) )
+               CALL tab_2d_1d( nbpac, ze_i_1d(1:nbpac,jk,jl), e_i(:,:,jk,jl) , jpi, jpj, npac(1:nbpac) )
             END DO ! jk
          END DO ! jl
 
-         CALL tab_2d_1d( nbpac, qldif_1d  (1:nbpac)     , qldif  , jpi, jpj, npac(1:nbpac) )
-         CALL tab_2d_1d( nbpac, qcmif_1d  (1:nbpac)     , qcmif  , jpi, jpj, npac(1:nbpac) )
-         CALL tab_2d_1d( nbpac, t_bo_b    (1:nbpac)     , t_bo   , jpi, jpj, npac(1:nbpac) )
-         CALL tab_2d_1d( nbpac, sfx_thd_1d(1:nbpac)     , sfx_thd, jpi, jpj, npac(1:nbpac) )
-         CALL tab_2d_1d( nbpac, rdm_ice_1d(1:nbpac)     , rdm_ice, jpi, jpj, npac(1:nbpac) )
-         CALL tab_2d_1d( nbpac, hicol_b   (1:nbpac)     , hicol  , jpi, jpj, npac(1:nbpac) )
-         CALL tab_2d_1d( nbpac, zvrel_ac  (1:nbpac)     , zvrel  , jpi, jpj, npac(1:nbpac) )
+         CALL tab_2d_1d( nbpac, qlead_1d  (1:nbpac)     , qlead  , jpi, jpj, npac(1:nbpac) )
+         CALL tab_2d_1d( nbpac, t_bo_1d   (1:nbpac)     , t_bo   , jpi, jpj, npac(1:nbpac) )
+         CALL tab_2d_1d( nbpac, sfx_opw_1d(1:nbpac)     , sfx_opw, jpi, jpj, npac(1:nbpac) )
+         CALL tab_2d_1d( nbpac, wfx_opw_1d(1:nbpac)     , wfx_opw, jpi, jpj, npac(1:nbpac) )
+         CALL tab_2d_1d( nbpac, hicol_1d  (1:nbpac)     , hicol  , jpi, jpj, npac(1:nbpac) )
+         CALL tab_2d_1d( nbpac, zvrel_1d  (1:nbpac)     , zvrel  , jpi, jpj, npac(1:nbpac) )
+
+         CALL tab_2d_1d( nbpac, hfx_thd_1d(1:nbpac)     , hfx_thd, jpi, jpj, npac(1:nbpac) )
+         CALL tab_2d_1d( nbpac, hfx_opw_1d(1:nbpac)     , hfx_opw, jpi, jpj, npac(1:nbpac) )
 
          !------------------------------------------------------------------------------!
@@ -330,19 +311,23 @@
          !------------------------------------------------------------------------------!
 
+         !-----------------------------------------
+         ! Keep old ice areas and volume in memory
+         !-----------------------------------------
+         zv_b(1:nbpac,:) = zv_i_1d(1:nbpac,:) 
+         za_b(1:nbpac,:) = za_i_1d(1:nbpac,:)
          !----------------------
          ! Thickness of new ice
          !----------------------
          DO ji = 1, nbpac
-            zh_newice(ji) = hiccrit(1)
-         END DO
-         IF( fraz_swi == 1.0 )   zh_newice(:) = hicol_b(:)
+            zh_newice(ji) = hiccrit
+         END DO
+         IF( fraz_swi == 1 ) zh_newice(1:nbpac) = hicol_1d(1:nbpac)
 
          !----------------------
          ! Salinity of new ice 
          !----------------------
-
          SELECT CASE ( num_sal )
          CASE ( 1 )                    ! Sice = constant 
-            zs_newice(:) = bulk_sal
+            zs_newice(1:nbpac) = bulk_sal
          CASE ( 2 )                    ! Sice = F(z,t) [Vancoppenolle et al (2005)]
             DO ji = 1, nbpac
@@ -352,7 +337,6 @@
             END DO
          CASE ( 3 )                    ! Sice = F(z) [multiyear ice]
-            zs_newice(:) =   2.3
+            zs_newice(1:nbpac) =   2.3
          END SELECT
-
 
          !-------------------------
@@ -362,10 +346,9 @@
          DO ji = 1, nbpac
             ztmelts       = - tmut * zs_newice(ji) + rtt                  ! Melting point (K)
-            ze_newice(ji) =   rhoic * (  cpic * ( ztmelts - t_bo_b(ji) )                             &
-               &                       + lfus * ( 1.0 - ( ztmelts - rtt ) / ( t_bo_b(ji) - rtt ) )   &
+            ze_newice(ji) =   rhoic * (  cpic * ( ztmelts - t_bo_1d(ji) )                             &
+               &                       + lfus * ( 1.0 - ( ztmelts - rtt ) / MIN( t_bo_1d(ji) - rtt, -epsi10 ) )   &
                &                       - rcp  *         ( ztmelts - rtt )  )
-            ze_newice(ji) =   MAX( ze_newice(ji) , 0._wp )    &
-               &          +   MAX(  0.0 , SIGN( 1.0 , - ze_newice(ji) )  ) * rhoic * lfus
          END DO ! ji
+
          !----------------
          ! Age of new ice
@@ -375,39 +358,37 @@
          END DO ! ji
 
-         !--------------------------
-         ! Open water energy budget 
-         !--------------------------
-         DO ji = 1, nbpac
-            zqbgow(ji) = qldif_1d(ji) - qcmif_1d(ji)     !<0
-         END DO ! ji
-
          !-------------------
          ! Volume of new ice
          !-------------------
          DO ji = 1, nbpac
-            zv_newice(ji) = - zqbgow(ji) / ze_newice(ji)
+
+            zEi           = - ze_newice(ji) / rhoic                ! specific enthalpy of forming ice [J/kg]
+
+            zEw           = rcp * ( t_bo_1d(ji) - rt0 )             ! specific enthalpy of seawater at t_bo_1d [J/kg]
+                                                                   ! clem: we suppose we are already at the freezing point (condition qlead<0 is satisfyied) 
+                                                                   
+            zdE           = zEi - zEw                              ! specific enthalpy difference [J/kg]
+                                              
+            zfmdt         = - qlead_1d(ji) / zdE                   ! Fm.dt [kg/m2] (<0) 
+                                                                   ! clem: we use qlead instead of zqld (limthd) because we suppose we are at the freezing point   
+            zv_newice(ji) = - zfmdt / rhoic
+
+            zQm           = zfmdt * zEw                            ! heat to the ocean >0 associated with mass flux  
+
+            ! Contribution to heat flux to the ocean [W.m-2], >0  
+            hfx_thd_1d(ji) = hfx_thd_1d(ji) + zfmdt * zEw * r1_rdtice
+            ! Total heat flux used in this process [W.m-2]  
+            hfx_opw_1d(ji) = hfx_opw_1d(ji) - zfmdt * zdE * r1_rdtice
+            ! mass flux
+            wfx_opw_1d(ji) = wfx_opw_1d(ji) - zv_newice(ji) * rhoic * r1_rdtice
+            ! salt flux
+            sfx_opw_1d(ji) = sfx_opw_1d(ji) - zv_newice(ji) * rhoic * zs_newice(ji) * r1_rdtice
 
             ! A fraction zfrazb of frazil ice is accreted at the ice bottom
-            zfrazb        = ( TANH ( Cfrazb * ( zvrel_ac(ji) - vfrazb ) ) + 1.0 ) * 0.5 * maxfrazb
-            zdh_frazb(ji) =         zfrazb   * zv_newice(ji)
+            rswitch       = 1._wp - MAX( 0._wp, SIGN( 1._wp , - zat_i_1d(ji) ) )
+            zfrazb        = rswitch * ( TANH ( Cfrazb * ( zvrel_1d(ji) - vfrazb ) ) + 1.0 ) * 0.5 * maxfrazb
+            zv_frazb(ji)  =         zfrazb   * zv_newice(ji)
             zv_newice(ji) = ( 1.0 - zfrazb ) * zv_newice(ji)
          END DO
-
-         !------------------------------------
-         ! Diags for energy conservation test
-         !------------------------------------
-         DO ji = 1, nbpac
-            ii = MOD( npac(ji) - 1 , jpi ) + 1
-            ij =    ( npac(ji) - 1 ) / jpi + 1
-            !
-            zde = ze_newice(ji) / unit_fac * area(ii,ij) * zv_newice(ji)
-            !
-            vt_i_init(ii,ij) = vt_i_init(ii,ij) + zv_newice(ji)             ! volume
-            et_i_init(ii,ij) = et_i_init(ii,ij) + zde                       ! Energy
-
-         END DO
-
-         ! keep new ice volume in memory
-         CALL tab_1d_2d( nbpac, v_newice , npac(1:nbpac), zv_newice(1:nbpac) , jpi, jpj )
 
          !-----------------
@@ -415,9 +396,6 @@
          !-----------------
          DO ji = 1, nbpac
-            ii = MOD( npac(ji) - 1 , jpi ) + 1
-            ij =    ( npac(ji) - 1 ) / jpi + 1
             za_newice(ji) = zv_newice(ji) / zh_newice(ji)
-            diag_lat_gr(ii,ij) = diag_lat_gr(ii,ij) + zv_newice(ji) * r1_rdtice ! clem
-         END DO !ji
+         END DO
 
          !------------------------------------------------------------------------------!
@@ -425,18 +403,12 @@
          !------------------------------------------------------------------------------!
 
-         !-----------------------------------------
-         ! Keep old ice areas and volume in memory
-         !-----------------------------------------
-         zv_old(:,:) = zv_i_ac(:,:) 
-         za_old(:,:) = za_i_ac(:,:)
-
-         !-------------------------------------------
-         ! Compute excessive new ice area and volume
-         !-------------------------------------------
+         !------------------------
+         ! 6.1) lateral ice growth
+         !------------------------
          ! If lateral ice growth gives an ice concentration gt 1, then
          ! we keep the excessive volume in memory and attribute it later to bottom accretion
          DO ji = 1, nbpac
-            IF ( za_newice(ji) >  ( amax - zat_i_ac(ji) ) ) THEN
-               zda_res(ji)   = za_newice(ji) - ( amax - zat_i_ac(ji) )
+            IF ( za_newice(ji) >  ( amax - zat_i_1d(ji) ) ) THEN
+               zda_res(ji)   = za_newice(ji) - ( amax - zat_i_1d(ji) )
                zdv_res(ji)   = zda_res  (ji) * zh_newice(ji) 
                za_newice(ji) = za_newice(ji) - zda_res  (ji)
@@ -446,130 +418,63 @@
                zdv_res(ji) = 0._wp
             ENDIF
-         END DO ! ji
-
-         !------------------------------------------------
-         ! Laterally redistribute new ice volume and area
-         !------------------------------------------------
-         zat_i_ac(:) = 0._wp
+         END DO
+
+         ! find which category to fill
+         zat_i_1d(:) = 0._wp
          DO jl = 1, jpl
             DO ji = 1, nbpac
-               IF(  hi_max   (jl-1)  <   zh_newice(ji)   .AND.   &
-                  & zh_newice(ji)    <=  hi_max   (jl)         ) THEN
-                  za_i_ac (ji,jl) = za_i_ac (ji,jl) + za_newice(ji)
-                  zv_i_ac (ji,jl) = zv_i_ac (ji,jl) + zv_newice(ji)
-                  zat_i_ac(ji)    = zat_i_ac(ji)    + za_i_ac  (ji,jl)
-                  zcatac  (ji)    = jl
+               IF( zh_newice(ji) > hi_max(jl-1) .AND. zh_newice(ji) <= hi_max(jl) ) THEN
+                  za_i_1d (ji,jl) = za_i_1d (ji,jl) + za_newice(ji)
+                  zv_i_1d (ji,jl) = zv_i_1d (ji,jl) + zv_newice(ji)
+                  jcat    (ji)    = jl
                ENDIF
-            END DO
-         END DO
-
-         !----------------------------------
-         ! Heat content - lateral accretion
-         !----------------------------------
-         DO ji = 1, nbpac
-            jl = zcatac(ji)                                                           ! categroy in which new ice is put
-            zindb = 1._wp - MAX( 0._wp , SIGN( 1._wp , -za_old(ji,jl) + epsi10 ) )             ! zindb=1 if ice =0 otherwise
-            zhice_old(ji,jl) = zv_old(ji,jl) / MAX( za_old(ji,jl) , epsi10 ) * zindb  ! old ice thickness
-            zdhex    (ji) = MAX( 0._wp , zh_newice(ji) - zhice_old(ji,jl) )           ! difference in thickness
-            zswinew  (ji) = MAX( 0._wp , SIGN( 1._wp , - za_old(ji,jl) + epsi10 ) )   ! ice totally new in jl category
+               zat_i_1d(ji) = zat_i_1d(ji) + za_i_1d  (ji,jl)
+            END DO
+         END DO
+
+         ! Heat content
+         DO ji = 1, nbpac
+            jl = jcat(ji)                                                    ! categroy in which new ice is put
+            zswinew  (ji) = MAX( 0._wp , SIGN( 1._wp , - za_b(ji,jl) ) )   ! 0 if old ice
          END DO
 
          DO jk = 1, nlay_i
             DO ji = 1, nbpac
-               jl = zcatac(ji)
-               zqold   = ze_i_ac(ji,jk,jl) ! [ J.m-3 ]
-               zalphai = MIN( zhice_old(ji,jl) * REAL( jk )     / REAL( nlay_i ), zh_newice(ji) )   &
-                  &    - MIN( zhice_old(ji,jl) * REAL( jk - 1 ) / REAL( nlay_i ), zh_newice(ji) )
-               ze_i_ac(ji,jk,jl) = zswinew(ji) * ze_newice(ji)                                     &
-                  + ( 1.0 - zswinew(ji) ) * ( za_old(ji,jl)  * zqold * zhice_old(ji,jl) / REAL( nlay_i )  &
-                  + za_newice(ji)  * ze_newice(ji) * zalphai                                       &
-                  + za_newice(ji)  * ze_newice(ji) * zdhex(ji) / REAL( nlay_i ) ) / ( ( zv_i_ac(ji,jl) ) / REAL( nlay_i ) )
-            END DO
-         END DO
-
-         !-----------------------------------------------
-         ! Add excessive volume of new ice at the bottom
-         !-----------------------------------------------
-         ! If the ice concentration exceeds 1, the remaining volume of new ice
-         ! is equally redistributed among all ice categories in which there is
-         ! ice
-
-         ! Fraction of level ice
-         jm = 1
-         zat_i_lev(:) = 0._wp
-
-         DO jl = ice_cat_bounds(jm,1), ice_cat_bounds(jm,2)
-            DO ji = 1, nbpac
-               zat_i_lev(ji) = zat_i_lev(ji) + za_i_ac(ji,jl) 
-            END DO
-         END DO
-
-         IF( ln_nicep .AND. jiindex_1d > 0 ) WRITE(numout,*) ' zv_i_ac : ', zv_i_ac(jiindex_1d, 1:jpl)
-         DO jl = ice_cat_bounds(jm,1), ice_cat_bounds(jm,2)
-            DO ji = 1, nbpac
-               zindb = MAX( 0._wp, SIGN( 1._wp , zdv_res(ji) ) )
-               zinda = MAX( 0._wp, SIGN( 1._wp , zat_i_lev(ji) - epsi10 ) )  ! clem
-               zv_i_ac(ji,jl) = zv_i_ac(ji,jl) + zindb * zinda * zdv_res(ji) * za_i_ac(ji,jl) / MAX( zat_i_lev(ji) , epsi10 )
-            END DO
-         END DO
-         IF( ln_nicep .AND. jiindex_1d > 0 )   WRITE(numout,*) ' zv_i_ac : ', zv_i_ac(jiindex_1d, 1:jpl)
-
-         !---------------------------------
-         ! Heat content - bottom accretion
-         !---------------------------------
-         jm = 1
-         DO jl = ice_cat_bounds(jm,1), ice_cat_bounds(jm,2)
-            DO ji = 1, nbpac
-               zindb =  1._wp - MAX( 0._wp , SIGN( 1._wp , - za_i_ac(ji,jl ) + epsi10 ) )       ! zindb=1 if ice =0 otherwise
-               zhice_old(ji,jl) = zv_i_ac(ji,jl) / MAX( za_i_ac(ji,jl) , epsi10 ) * zindb
-               zdhicbot (ji,jl) = zdv_res(ji)    / MAX( za_i_ac(ji,jl) , epsi10 ) * zindb    &
-                  &             +  zindb * zdh_frazb(ji)                               ! frazil ice may coalesce
-               zdummy(ji,jl)    = zv_i_ac(ji,jl) / MAX( za_i_ac(ji,jl) , epsi10 ) * zindb      ! thickness of residual ice
-            END DO
-         END DO
-
-         ! old layers thicknesses and enthalpies
-         DO jl = ice_cat_bounds(jm,1), ice_cat_bounds(jm,2)
+               jl = jcat(ji)
+               rswitch = MAX( 0._wp, SIGN( 1._wp , zv_i_1d(ji,jl) - epsi20 ) )
+               ze_i_1d(ji,jk,jl) = zswinew(ji)   *   ze_newice(ji) +                                                      &
+                  &        ( 1.0 - zswinew(ji) ) * ( ze_newice(ji) * zv_newice(ji) + ze_i_1d(ji,jk,jl) * zv_b(ji,jl) )  &
+                  &        * rswitch / MAX( zv_i_1d(ji,jl), epsi20 )
+            END DO
+         END DO
+
+         !------------------------------------------------
+         ! 6.2) bottom ice growth + ice enthalpy remapping
+         !------------------------------------------------
+         DO jl = 1, jpl
+
+            ! for remapping
+            h_i_old (1:nbpac,0:nlay_i+1) = 0._wp
+            qh_i_old(1:nbpac,0:nlay_i+1) = 0._wp
             DO jk = 1, nlay_i
                DO ji = 1, nbpac
-                  zthick0(ji,jk,jl) =  zhice_old(ji,jl) / REAL( nlay_i )
-                  zqm0   (ji,jk,jl) =  ze_i_ac(ji,jk,jl) * zthick0(ji,jk,jl)
+                  h_i_old (ji,jk) = zv_i_1d(ji,jl) / REAL( nlay_i )
+                  qh_i_old(ji,jk) = ze_i_1d(ji,jk,jl) * h_i_old(ji,jk)
                END DO
             END DO
-         END DO
-!!gm ???  why the previous do loop  if ocerwriten by the following one ?
-         DO jl = ice_cat_bounds(jm,1), ice_cat_bounds(jm,2)
+
+            ! new volumes including lateral/bottom accretion + residual
             DO ji = 1, nbpac
-               zthick0(ji,nlay_i+1,jl) =  zdhicbot(ji,jl)
-               zqm0   (ji,nlay_i+1,jl) =  ze_newice(ji) * zdhicbot(ji,jl)
-            END DO ! ji
-         END DO ! jl
-
-         ! Redistributing energy on the new grid
-         ze_i_ac(:,:,:) = 0._wp
-         DO jl = ice_cat_bounds(jm,1), ice_cat_bounds(jm,2)
-            DO jk = 1, nlay_i
-               DO layer = 1, nlay_i + 1
-                  DO ji = 1, nbpac
-                     zindb =  1._wp -  MAX( 0._wp , SIGN( 1._wp , - za_i_ac(ji,jl) + epsi10 ) ) 
-                     ! Redistributing energy on the new grid
-                     zweight = MAX (  MIN( zhice_old(ji,jl) * REAL( layer ), zdummy(ji,jl) * REAL( jk ) )   &
-                        &    - MAX( zhice_old(ji,jl) * REAL( layer - 1 ) , zdummy(ji,jl) * REAL( jk - 1 ) ) , 0._wp )   &
-                        &    /( MAX(REAL(nlay_i) * zthick0(ji,layer,jl),epsi10) ) * zindb
-                     ze_i_ac(ji,jk,jl) =  ze_i_ac(ji,jk,jl) + zweight * zqm0(ji,layer,jl)  
-                  END DO ! ji
-               END DO ! layer
-            END DO ! jk
-         END DO ! jl
-
-         DO jl = ice_cat_bounds(jm,1), ice_cat_bounds(jm,2)
-            DO jk = 1, nlay_i
-               DO ji = 1, nbpac
-                  zindb =  1._wp -  MAX( 0._wp , SIGN( 1._wp , - zv_i_ac(ji,jl) + epsi10 ) ) 
-                  ze_i_ac(ji,jk,jl) = ze_i_ac(ji,jk,jl)   &
-                     &              / MAX( zv_i_ac(ji,jl) , epsi10) * za_i_ac(ji,jl) * REAL( nlay_i ) * zindb
-               END DO
-            END DO
-         END DO
+               rswitch        = MAX( 0._wp, SIGN( 1._wp , zat_i_1d(ji) - epsi20 ) )
+               zv_newfra      = rswitch * ( zdv_res(ji) + zv_frazb(ji) ) * za_i_1d(ji,jl) / MAX( zat_i_1d(ji) , epsi20 )
+               za_i_1d(ji,jl) = rswitch * za_i_1d(ji,jl)               
+               zv_i_1d(ji,jl) = zv_i_1d(ji,jl) + zv_newfra
+               ! for remapping
+               h_i_old (ji,nlay_i+1) = zv_newfra
+               qh_i_old(ji,nlay_i+1) = ze_newice(ji) * zv_newfra
+            ENDDO
+            ! --- Ice enthalpy remapping --- !
+            CALL lim_thd_ent( 1, nbpac, ze_i_1d(1:nbpac,:,jl) ) 
+         ENDDO
 
          !------------
@@ -578,6 +483,6 @@
          DO jl = 1, jpl
             DO ji = 1, nbpac
-               zindb = 1._wp - MAX( 0._wp , SIGN( 1._wp , - za_i_ac(ji,jl) + epsi10 ) )  ! 0 if no ice and 1 if yes
-               zoa_i_ac(ji,jl)  = za_old(ji,jl) * zoa_i_ac(ji,jl) / MAX( za_i_ac(ji,jl) , epsi10 ) * zindb   
+               rswitch          = 1._wp - MAX( 0._wp , SIGN( 1._wp , - za_i_1d(ji,jl) + epsi20 ) )  ! 0 if no ice and 1 if yes
+               zoa_i_1d(ji,jl)  = za_b(ji,jl) * zoa_i_1d(ji,jl) / MAX( za_i_1d(ji,jl) , epsi20 ) * rswitch   
             END DO 
          END DO   
@@ -586,92 +491,52 @@
          ! Update salinity
          !-----------------
-         !clem IF(  num_sal == 2  ) THEN
-            DO jl = 1, jpl
-               DO ji = 1, nbpac
-                  zindb = 1._wp - MAX( 0._wp , SIGN( 1._wp , - zv_i_ac(ji,jl) + epsi10 ) )  ! 0 if no ice and 1 if yes
-                  zdv   = zv_i_ac(ji,jl) - zv_old(ji,jl)
-                  zsmv_i_ac(ji,jl) = zsmv_i_ac(ji,jl) + zdv * zs_newice(ji) * zindb ! clem modif
-               END DO
-            END DO   
-         !clem ENDIF
-
-         !--------------------------------
-         ! Update mass/salt fluxes (clem)
-         !--------------------------------
          DO jl = 1, jpl
             DO ji = 1, nbpac
-               zindb = 1._wp - MAX( 0._wp , SIGN( 1._wp , - zv_i_ac(ji,jl) + epsi10 ) )  ! 0 if no ice and 1 if yes
-               zdv   = zv_i_ac(ji,jl) - zv_old(ji,jl)
-               rdm_ice_1d(ji) = rdm_ice_1d(ji) + zdv * rhoic * zindb
-               sfx_thd_1d(ji)   =   sfx_thd_1d(ji) - zdv * rhoic * zs_newice(ji) * r1_rdtice * zindb
-           END DO
+               zdv   = zv_i_1d(ji,jl) - zv_b(ji,jl)
+               zsmv_i_1d(ji,jl) = zsmv_i_1d(ji,jl) + zdv * zs_newice(ji)
+            END DO
          END DO
 
          !------------------------------------------------------------------------------!
-         ! 8) Change 2D vectors to 1D vectors 
+         ! 7) Change 2D vectors to 1D vectors 
          !------------------------------------------------------------------------------!
          DO jl = 1, jpl
-            CALL tab_1d_2d( nbpac, a_i (:,:,jl), npac(1:nbpac), za_i_ac (1:nbpac,jl), jpi, jpj )
-            CALL tab_1d_2d( nbpac, v_i (:,:,jl), npac(1:nbpac), zv_i_ac (1:nbpac,jl), jpi, jpj )
-            CALL tab_1d_2d( nbpac, oa_i(:,:,jl), npac(1:nbpac), zoa_i_ac(1:nbpac,jl), jpi, jpj )
-            !clem IF (  num_sal == 2  )   &
-               CALL tab_1d_2d( nbpac, smv_i (:,:,jl), npac(1:nbpac), zsmv_i_ac(1:nbpac,jl) , jpi, jpj )
+            CALL tab_1d_2d( nbpac, a_i (:,:,jl), npac(1:nbpac), za_i_1d (1:nbpac,jl), jpi, jpj )
+            CALL tab_1d_2d( nbpac, v_i (:,:,jl), npac(1:nbpac), zv_i_1d (1:nbpac,jl), jpi, jpj )
+            CALL tab_1d_2d( nbpac, oa_i(:,:,jl), npac(1:nbpac), zoa_i_1d(1:nbpac,jl), jpi, jpj )
+            CALL tab_1d_2d( nbpac, smv_i (:,:,jl), npac(1:nbpac), zsmv_i_1d(1:nbpac,jl) , jpi, jpj )
             DO jk = 1, nlay_i
-               CALL tab_1d_2d( nbpac, e_i(:,:,jk,jl), npac(1:nbpac), ze_i_ac(1:nbpac,jk,jl), jpi, jpj )
-            END DO
-         END DO
-         CALL tab_1d_2d( nbpac, sfx_thd, npac(1:nbpac), sfx_thd_1d(1:nbpac), jpi, jpj )
-         CALL tab_1d_2d( nbpac, rdm_ice, npac(1:nbpac), rdm_ice_1d(1:nbpac), jpi, jpj )
+               CALL tab_1d_2d( nbpac, e_i(:,:,jk,jl), npac(1:nbpac), ze_i_1d(1:nbpac,jk,jl), jpi, jpj )
+            END DO
+         END DO
+         CALL tab_1d_2d( nbpac, sfx_opw, npac(1:nbpac), sfx_opw_1d(1:nbpac), jpi, jpj )
+         CALL tab_1d_2d( nbpac, wfx_opw, npac(1:nbpac), wfx_opw_1d(1:nbpac), jpi, jpj )
+
+         CALL tab_1d_2d( nbpac, hfx_thd, npac(1:nbpac), hfx_thd_1d(1:nbpac), jpi, jpj )
+         CALL tab_1d_2d( nbpac, hfx_opw, npac(1:nbpac), hfx_opw_1d(1:nbpac), jpi, jpj )
          !
       ENDIF ! nbpac > 0
 
       !------------------------------------------------------------------------------!
-      ! 9) Change units for e_i
+      ! 8) Change units for e_i
       !------------------------------------------------------------------------------!    
       DO jl = 1, jpl
-         DO jk = 1, nlay_i          ! heat content in 10^9 Joules
-            e_i(:,:,jk,jl) = e_i(:,:,jk,jl) * area(:,:) * v_i(:,:,jl) / REAL( nlay_i )  / unit_fac 
+         DO jk = 1, nlay_i
+            DO jj = 1, jpj
+               DO ji = 1, jpi
+                  ! heat content in Joules
+                  e_i(ji,jj,jk,jl) = e_i(ji,jj,jk,jl) * area(ji,jj) * v_i(ji,jj,jl) / ( REAL( nlay_i ,wp ) * unit_fac ) 
+               END DO
+            END DO
          END DO
       END DO
 
-      !------------------------------------------------------------------------------|
-      ! 10) Conservation check and changes in each ice category
-      !------------------------------------------------------------------------------|
-      IF( con_i ) THEN 
-         CALL lim_column_sum (jpl,   v_i, vt_i_final)
-         fieldid = 'v_i, limthd_lac'
-         CALL lim_cons_check (vt_i_init, vt_i_final, 1.0e-6, fieldid) 
-         !
-         CALL lim_column_sum_energy(jpl, nlay_i, e_i, et_i_final)
-         fieldid = 'e_i, limthd_lac'
-         CALL lim_cons_check (et_i_final, et_i_final, 1.0e-3, fieldid) 
-         !
-         CALL lim_column_sum (jpl,   v_s, vt_s_final)
-         fieldid = 'v_s, limthd_lac'
-         CALL lim_cons_check (vt_s_init, vt_s_final, 1.0e-6, fieldid) 
-         !
-         !     CALL lim_column_sum (jpl,   e_s(:,:,1,:) , et_s_init)
-         !     fieldid = 'e_s, limthd_lac'
-         !     CALL lim_cons_check (et_s_init, et_s_final, 1.0e-3, fieldid) 
-         IF( ln_nicep ) THEN
-            DO ji = mi0(jiindx), mi1(jiindx)
-               DO jj = mj0(jjindx), mj1(jjindx)
-                  WRITE(numout,*) ' vt_i_init : ', vt_i_init (ji,jj)
-                  WRITE(numout,*) ' vt_i_final: ', vt_i_final(ji,jj)
-                  WRITE(numout,*) ' et_i_init : ', et_i_init (ji,jj)
-                  WRITE(numout,*) ' et_i_final: ', et_i_final(ji,jj)
-               END DO
-            END DO
-         ENDIF
-         !
-      ENDIF
       !
-      CALL wrk_dealloc( jpij, zcatac )   ! integer
+      CALL wrk_dealloc( jpij, jcat )   ! integer
       CALL wrk_dealloc( jpij, zswinew, zv_newice, za_newice, zh_newice, ze_newice, zs_newice, zo_newice )
-      CALL wrk_dealloc( jpij, zdv_res, zda_res, zat_i_ac, zat_i_lev, zdh_frazb, zvrel_ac, zqbgow, zdhex )
-      CALL wrk_dealloc( jpij,jpl, zhice_old, zdummy, zdhicbot, zv_old, za_old, za_i_ac, zv_i_ac, zoa_i_ac, zsmv_i_ac )
-      CALL wrk_dealloc( jpij,jkmax,jpl, ze_i_ac )
-      CALL wrk_dealloc( jpij,jkmax+1,jpl, zqm0, zthick0 )
-      CALL wrk_dealloc( jpi,jpj, vt_i_init, vt_i_final, vt_s_init, vt_s_final, et_i_init, et_i_final, et_s_init, zvrel )
+      CALL wrk_dealloc( jpij, zdv_res, zda_res, zat_i_1d, zv_frazb, zvrel_1d )
+      CALL wrk_dealloc( jpij,jpl, zv_b, za_b, za_i_1d, zv_i_1d, zoa_i_1d, zsmv_i_1d )
+      CALL wrk_dealloc( jpij,nlay_i+1,jpl, ze_i_1d )
+      CALL wrk_dealloc( jpi,jpj, zvrel )
       !
    END SUBROUTINE lim_thd_lac
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd_sal.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd_sal.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limthd_sal.F90	(revision 5038)
@@ -53,19 +53,22 @@
       !
       INTEGER  ::   ji, jk     ! dummy loop indices 
-      REAL(wp) ::   zsold, iflush, iaccrbo, igravdr, isnowic, i_ice_switch,  ztmelts   ! local scalars
-      REAL(wp) ::   zaaa, zbbb, zccc, zdiscrim   ! local scalars
-      REAL(wp), POINTER, DIMENSION(:) ::   ze_init, zhiold, zsiold
+      REAL(wp) ::   iflush, igravdr   ! local scalars
       !!---------------------------------------------------------------------
 
-      CALL wrk_alloc( jpij, ze_init, zhiold, zsiold )
-
+      !---------------------------------------------------------
+      !  0) Update ice salinity from snow-ice and bottom growth
+      !---------------------------------------------------------
+      DO ji = kideb, kiut
+         sm_i_1d(ji) = sm_i_1d(ji) + dsm_i_se_1d(ji) + dsm_i_si_1d(ji)
+      END DO
+ 
       !------------------------------------------------------------------------------|
       ! 1) Constant salinity, constant in time                                       |
       !------------------------------------------------------------------------------|
-!!gm comment: if num_sal = 1 s_i_new, s_i_b and sm_i_b can be set to bulk_sal one for all in the initialisation phase !!
+!!gm comment: if num_sal = 1 s_i_new, s_i_1d and sm_i_1d can be set to bulk_sal one for all in the initialisation phase !!
 !!gm           ===>>>   simplification of almost all test on num_sal value
       IF(  num_sal == 1  ) THEN
-            s_i_b  (kideb:kiut,1:nlay_i) =  bulk_sal
-            sm_i_b (kideb:kiut)          =  bulk_sal 
+            s_i_1d (kideb:kiut,1:nlay_i) =  bulk_sal
+            sm_i_1d(kideb:kiut)          =  bulk_sal 
             s_i_new(kideb:kiut)          =  bulk_sal
       ENDIF
@@ -74,24 +77,5 @@
       !  Module 2 : Constant salinity varying in time                                |
       !------------------------------------------------------------------------------|
-
       IF(  num_sal == 2  ) THEN
-
-         !---------------------------------
-         ! Thickness at previous time step
-         !---------------------------------
-         DO ji = kideb, kiut
-            zhiold(ji) = ht_i_b(ji) - dh_i_bott(ji) - dh_snowice(ji) - dh_i_surf(ji)
-            zsiold(ji) = sm_i_b(ji)
-         END DO
-
-         !---------------------
-         ! Global heat content
-         !---------------------
-         ze_init(:)  =  0._wp
-         DO jk = 1, nlay_i
-            DO ji = kideb, kiut
-               ze_init(ji) = ze_init(ji) + q_i_b(ji,jk) * ht_i_b(ji) / REAL (nlay_i )
-            END DO
-         END DO
 
          DO ji = kideb, kiut
@@ -99,17 +83,14 @@
             ! Switches 
             !----------
-            iflush       =         MAX( 0._wp , SIGN( 1.0 , t_su_b(ji) - rtt )        )    ! =1 if summer 
-            igravdr      =         MAX( 0._wp , SIGN( 1.0 , t_bo_b(ji) - t_su_b(ji) ) )    ! =1 if t_su < t_bo
-            iaccrbo      =         MAX( 0._wp , SIGN( 1.0 , dh_i_bott(ji) )           )    ! =1 if bottom accretion
-            i_ice_switch = 1._wp - MAX ( 0._wp , SIGN( 1._wp , - ht_i_b(ji) + 1.e-2 ) )
-            isnowic      = 1._wp - MAX ( 0._wp , SIGN( 1._wp , - dh_snowice(ji) ) ) * i_ice_switch   ! =1 if snow ice formation
+            iflush  = MAX( 0._wp , SIGN( 1._wp , t_su_1d(ji) - rtt )        )     ! =1 if summer 
+            igravdr = MAX( 0._wp , SIGN( 1._wp , t_bo_1d(ji) - t_su_1d(ji) ) )    ! =1 if t_su < t_bo
 
             !---------------------
             ! Salinity tendencies
             !---------------------
-            !                                   ! drainage by gravity drainage
-            dsm_i_gd_1d(ji) = - igravdr * MAX( sm_i_b(ji) - sal_G , 0._wp ) / time_G * rdt_ice 
-            !                                   ! drainage by flushing  
-            dsm_i_fl_1d(ji) = - iflush  * MAX( sm_i_b(ji) - sal_F , 0._wp ) / time_F * rdt_ice
+            ! drainage by gravity drainage
+            dsm_i_gd_1d(ji) = - igravdr * MAX( sm_i_1d(ji) - sal_G , 0._wp ) / time_G * rdt_ice 
+            ! drainage by flushing  
+            dsm_i_fl_1d(ji) = - iflush  * MAX( sm_i_1d(ji) - sal_F , 0._wp ) / time_F * rdt_ice
 
             !-----------------
@@ -118,19 +99,10 @@
             ! only drainage terms ( gravity drainage and flushing )
             ! snow ice / bottom sources are added in lim_thd_ent to conserve energy
-            sm_i_b(ji) = sm_i_b(ji) + dsm_i_fl_1d(ji) + dsm_i_gd_1d(ji)
-
-            ! if no ice, salinity = 0.1
-            i_ice_switch = 1._wp - MAX ( 0._wp, SIGN( 1._wp , - ht_i_b(ji) ) )
-            sm_i_b(ji)   = i_ice_switch * sm_i_b(ji) + s_i_min * ( 1._wp - i_ice_switch )
-
-            !----------------------------
-            ! Heat flux - brine drainage
-            !----------------------------
-            fhbri_1d(ji) = 0._wp
+            sm_i_1d(ji) = sm_i_1d(ji) + dsm_i_fl_1d(ji) + dsm_i_gd_1d(ji)
 
             !----------------------------
             ! Salt flux - brine drainage
             !----------------------------
-            sfx_bri_1d(ji) = sfx_bri_1d(ji) - i_ice_switch * rhoic * a_i_b(ji) * ht_i_b(ji) * ( sm_i_b(ji) - zsiold(ji) ) * r1_rdtice
+            sfx_bri_1d(ji) = sfx_bri_1d(ji) - rhoic * a_i_1d(ji) * ht_i_1d(ji) * ( dsm_i_fl_1d(ji) + dsm_i_gd_1d(ji) ) * r1_rdtice
 
          END DO
@@ -138,21 +110,4 @@
          ! Salinity profile
          CALL lim_var_salprof1d( kideb, kiut )
-
-
-         ! Only necessary for conservation check since salinity is modified
-         !--------------------
-         ! Temperature update
-         !--------------------
-         DO jk = 1, nlay_i
-            DO ji = kideb, kiut
-               ztmelts    =  -tmut*s_i_b(ji,jk) + rtt
-               !Conversion q(S,T) -> T (second order equation)
-               zaaa         =  cpic
-               zbbb         =  ( rcp - cpic ) * ( ztmelts - rtt ) + q_i_b(ji,jk) / rhoic - lfus
-               zccc         =  lfus * ( ztmelts - rtt )
-               zdiscrim     =  SQRT(  MAX( zbbb*zbbb - 4.0*zaaa*zccc, 0._wp )  )
-               t_i_b(ji,jk) =  rtt - ( zbbb + zdiscrim ) / ( 2.0 *zaaa )
-            END DO
-         END DO
          !
       ENDIF 
@@ -161,9 +116,6 @@
       !  Module 3 : Profile of salinity, constant in time                            |
       !------------------------------------------------------------------------------|
-
       IF(  num_sal == 3  )   CALL lim_var_salprof1d( kideb, kiut )
 
-      !
-      CALL wrk_dealloc( jpij, ze_init, zhiold, zsiold )
       !
    END SUBROUTINE lim_thd_sal
@@ -193,5 +145,5 @@
       READ  ( numnam_ice_cfg, namicesal, IOSTAT = ios, ERR = 902 )
 902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namicesal in configuration namelist', lwp )
-      WRITE ( numoni, namicesal )
+      IF(lwm) WRITE ( numoni, namicesal )
       !
       IF(lwp) THEN                           ! control print
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limtrp.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limtrp.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limtrp.F90	(revision 5038)
@@ -30,4 +30,5 @@
    USE limvar          ! clem for ice thickness correction
    USE timing          ! Timing
+   USE limcons        ! conservation tests
 
    IMPLICIT NONE
@@ -35,8 +36,4 @@
 
    PUBLIC   lim_trp    ! called by ice_step
-
-   REAL(wp)  ::   epsi10 = 1.e-10_wp  
-   REAL(wp)  ::   rzero  = 0._wp   
-   REAL(wp)  ::   rone   = 1._wp
 
    !! * Substitution
@@ -63,42 +60,25 @@
       INTEGER, INTENT(in) ::   kt   ! number of iteration
       !
-      INTEGER  ::   ji, jj, jk, jl, layer   ! dummy loop indices
+      INTEGER  ::   ji, jj, jk, jl, jn      ! dummy loop indices
       INTEGER  ::   initad                  ! number of sub-timestep for the advection
-      INTEGER  ::   ierr                    ! error status
-      REAL(wp) ::   zindb  , zindsn , zindic, zindh, zinda      ! local scalar
-      REAL(wp) ::   zusvosn, zusvoic, zbigval     !   -      -
-      REAL(wp) ::   zcfl , zusnit                 !   -      -
-      REAL(wp) ::   ze   , zsal   , zage          !   -      -
+      REAL(wp) ::   zcfl , zusnit           !   -      -
       !
       REAL(wp), POINTER, DIMENSION(:,:)      ::   zui_u, zvi_v, zsm, zs0at, zs0ow
       REAL(wp), POINTER, DIMENSION(:,:,:)    ::   zs0ice, zs0sn, zs0a, zs0c0 , zs0sm , zs0oi
       REAL(wp), POINTER, DIMENSION(:,:,:,:)  ::   zs0e
-      REAL(wp) :: zchk_v_i, zchk_smv, zchk_fs, zchk_fw, zchk_v_i_b, zchk_smv_b, zchk_fs_b, zchk_fw_b ! Check conservation (C Rousset)
-      REAL(wp) :: zchk_vmin, zchk_amin, zchk_amax, zchk_umax ! Check errors (C Rousset)
-      ! mass and salt flux (clem)
-      REAL(wp), POINTER, DIMENSION(:,:,:) ::   zviold   ! old ice volume...
-      ! correct ice thickness (clem)
-      REAL(wp), POINTER, DIMENSION(:,:,:) ::   zaiold, zhimax   ! old ice concentration and thickness
-      REAL(wp) :: zdv, zda, zvi, zvs, zsmv
+      REAL(wp), POINTER, DIMENSION(:,:,:)    ::   zviold, zvsold   ! old ice volume...
+      REAL(wp), POINTER, DIMENSION(:,:,:)    ::   zaiold, zhimax   ! old ice concentration and thickness
+      REAL(wp), POINTER, DIMENSION(:,:)      ::   zeiold, zesold   ! old enthalpies
+      REAL(wp) :: zdv, zda, zvi, zvs, zsmv, zes, zei
+      !
+      REAL(wp) :: zvi_b, zsmv_b, zei_b, zfs_b, zfw_b, zft_b 
       !!---------------------------------------------------------------------
       IF( nn_timing == 1 )  CALL timing_start('limtrp')
 
-      CALL wrk_alloc( jpi, jpj, zui_u, zvi_v, zsm, zs0at, zs0ow )
+      CALL wrk_alloc( jpi, jpj, zui_u, zvi_v, zsm, zs0at, zs0ow, zeiold, zesold )
       CALL wrk_alloc( jpi, jpj, jpl, zs0ice, zs0sn, zs0a, zs0c0 , zs0sm , zs0oi )
-      CALL wrk_alloc( jpi, jpj, jkmax, jpl, zs0e )
-
-      CALL wrk_alloc( jpi,jpj,jpl,zviold )   ! clem
-      CALL wrk_alloc( jpi,jpj,jpl,zaiold, zhimax )   ! clem
-
-      ! -------------------------------
-      !- check conservation (C Rousset)
-      IF( ln_limdiahsb ) THEN
-         zchk_v_i_b = glob_sum( SUM(   v_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) )
-         zchk_smv_b = glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) )
-         zchk_fw_b  = glob_sum( rdm_ice(:,:) * area(:,:) * tms(:,:) )
-         zchk_fs_b  = glob_sum( ( sfx_bri(:,:) + sfx_thd(:,:) + sfx_res(:,:) + sfx_mec(:,:) ) * area(:,:) * tms(:,:) )
-      ENDIF
-      !- check conservation (C Rousset)
-      ! -------------------------------
+      CALL wrk_alloc( jpi, jpj, nlay_i+1, jpl, zs0e )
+
+      CALL wrk_alloc( jpi, jpj, jpl, zaiold, zhimax, zviold, zvsold )   ! clem
 
       IF( numit == nstart .AND. lwp ) THEN
@@ -115,6 +95,12 @@
       IF( ln_limdyn ) THEN          !   Advection of sea ice properties   !
          !                          !-------------------------------------!
+
+         ! conservation test
+         IF( ln_limdiahsb ) CALL lim_cons_hsm(0, 'limtrp', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
+
          ! mass and salt flux init (clem)
          zviold(:,:,:)  = v_i(:,:,:)
+         zeiold(:,:)  = SUM( SUM( e_i(:,:,1:nlay_i,:), dim=4 ), dim=3 ) 
+         zesold(:,:)  = SUM( SUM( e_s(:,:,1:nlay_s,:), dim=4 ), dim=3 ) 
 
          !--- Thickness correction init. (clem) -------------------------------
@@ -167,5 +153,5 @@
 !         ENDIF
 !!gm end
-         initad = 1 + NINT( MAX( rzero, SIGN( rone, zcfl-0.5 ) ) )
+         initad = 1 + NINT( MAX( 0._wp, SIGN( 1._wp, zcfl-0.5 ) ) )
          zusnit = 1.0 / REAL( initad ) 
          IF( zcfl > 0.5 .AND. lwp )   &
@@ -174,83 +160,83 @@
 
          IF( MOD( ( kt - 1) / nn_fsbc , 2 ) == 0 ) THEN       !==  odd ice time step:  adv_x then adv_y  ==!
-            DO jk = 1,initad
-               CALL lim_adv_x( zusnit, u_ice, rone , zsm, zs0ow (:,:), sxopw(:,:),   &             !--- ice open water area
+            DO jn = 1,initad
+               CALL lim_adv_x( zusnit, u_ice, 1._wp , zsm, zs0ow (:,:), sxopw(:,:),   &             !--- ice open water area
                   &                                       sxxopw(:,:), syopw(:,:), syyopw(:,:), sxyopw(:,:)  )
-               CALL lim_adv_y( zusnit, v_ice, rzero, zsm, zs0ow (:,:), sxopw(:,:),   &
+               CALL lim_adv_y( zusnit, v_ice, 0._wp, zsm, zs0ow (:,:), sxopw(:,:),   &
                   &                                       sxxopw(:,:), syopw(:,:), syyopw(:,:), sxyopw(:,:)  )
                DO jl = 1, jpl
-                  CALL lim_adv_x( zusnit, u_ice, rone , zsm, zs0ice(:,:,jl), sxice(:,:,jl),   &    !--- ice volume  ---
+                  CALL lim_adv_x( zusnit, u_ice, 1._wp , zsm, zs0ice(:,:,jl), sxice(:,:,jl),   &    !--- ice volume  ---
                      &                                       sxxice(:,:,jl), syice(:,:,jl), syyice(:,:,jl), sxyice(:,:,jl)  )
-                  CALL lim_adv_y( zusnit, v_ice, rzero, zsm, zs0ice(:,:,jl), sxice(:,:,jl),   &
+                  CALL lim_adv_y( zusnit, v_ice, 0._wp, zsm, zs0ice(:,:,jl), sxice(:,:,jl),   &
                      &                                       sxxice(:,:,jl), syice(:,:,jl), syyice(:,:,jl), sxyice(:,:,jl)  )
-                  CALL lim_adv_x( zusnit, u_ice, rone , zsm, zs0sn (:,:,jl), sxsn (:,:,jl),   &    !--- snow volume  ---
+                  CALL lim_adv_x( zusnit, u_ice, 1._wp , zsm, zs0sn (:,:,jl), sxsn (:,:,jl),   &    !--- snow volume  ---
                      &                                       sxxsn (:,:,jl), sysn (:,:,jl), syysn (:,:,jl), sxysn (:,:,jl)  )
-                  CALL lim_adv_y( zusnit, v_ice, rzero, zsm, zs0sn (:,:,jl), sxsn (:,:,jl),   &
+                  CALL lim_adv_y( zusnit, v_ice, 0._wp, zsm, zs0sn (:,:,jl), sxsn (:,:,jl),   &
                      &                                       sxxsn (:,:,jl), sysn (:,:,jl), syysn (:,:,jl), sxysn (:,:,jl)  )
-                  CALL lim_adv_x( zusnit, u_ice, rone , zsm, zs0sm (:,:,jl), sxsal(:,:,jl),   &    !--- ice salinity ---
+                  CALL lim_adv_x( zusnit, u_ice, 1._wp , zsm, zs0sm (:,:,jl), sxsal(:,:,jl),   &    !--- ice salinity ---
                      &                                       sxxsal(:,:,jl), sysal(:,:,jl), syysal(:,:,jl), sxysal(:,:,jl)  )
-                  CALL lim_adv_y( zusnit, v_ice, rzero, zsm, zs0sm (:,:,jl), sxsal(:,:,jl),   &
+                  CALL lim_adv_y( zusnit, v_ice, 0._wp, zsm, zs0sm (:,:,jl), sxsal(:,:,jl),   &
                      &                                       sxxsal(:,:,jl), sysal(:,:,jl), syysal(:,:,jl), sxysal(:,:,jl)  )
-                  CALL lim_adv_x( zusnit, u_ice, rone , zsm, zs0oi (:,:,jl), sxage(:,:,jl),   &   !--- ice age      ---     
+                  CALL lim_adv_x( zusnit, u_ice, 1._wp , zsm, zs0oi (:,:,jl), sxage(:,:,jl),   &   !--- ice age      ---     
                      &                                       sxxage(:,:,jl), syage(:,:,jl), syyage(:,:,jl), sxyage(:,:,jl)  )
-                  CALL lim_adv_y( zusnit, v_ice, rzero, zsm, zs0oi (:,:,jl), sxage(:,:,jl),   &
+                  CALL lim_adv_y( zusnit, v_ice, 0._wp, zsm, zs0oi (:,:,jl), sxage(:,:,jl),   &
                      &                                       sxxage(:,:,jl), syage(:,:,jl), syyage(:,:,jl), sxyage(:,:,jl)  )
-                  CALL lim_adv_x( zusnit, u_ice, rone , zsm, zs0a  (:,:,jl), sxa  (:,:,jl),   &   !--- ice concentrations ---
+                  CALL lim_adv_x( zusnit, u_ice, 1._wp , zsm, zs0a  (:,:,jl), sxa  (:,:,jl),   &   !--- ice concentrations ---
                      &                                       sxxa  (:,:,jl), sya  (:,:,jl), syya  (:,:,jl), sxya  (:,:,jl)  )
-                  CALL lim_adv_y( zusnit, v_ice, rzero, zsm, zs0a  (:,:,jl), sxa  (:,:,jl),   & 
+                  CALL lim_adv_y( zusnit, v_ice, 0._wp, zsm, zs0a  (:,:,jl), sxa  (:,:,jl),   & 
                      &                                       sxxa  (:,:,jl), sya  (:,:,jl), syya  (:,:,jl), sxya  (:,:,jl)  )
-                  CALL lim_adv_x( zusnit, u_ice, rone , zsm, zs0c0 (:,:,jl), sxc0 (:,:,jl),   &  !--- snow heat contents ---
+                  CALL lim_adv_x( zusnit, u_ice, 1._wp , zsm, zs0c0 (:,:,jl), sxc0 (:,:,jl),   &  !--- snow heat contents ---
                      &                                       sxxc0 (:,:,jl), syc0 (:,:,jl), syyc0 (:,:,jl), sxyc0 (:,:,jl)  )
-                  CALL lim_adv_y( zusnit, v_ice, rzero, zsm, zs0c0 (:,:,jl), sxc0 (:,:,jl),   &
+                  CALL lim_adv_y( zusnit, v_ice, 0._wp, zsm, zs0c0 (:,:,jl), sxc0 (:,:,jl),   &
                      &                                       sxxc0 (:,:,jl), syc0 (:,:,jl), syyc0 (:,:,jl), sxyc0 (:,:,jl)  )
-                  DO layer = 1, nlay_i                                                           !--- ice heat contents ---
-                     CALL lim_adv_x( zusnit, u_ice, rone , zsm, zs0e(:,:,layer,jl), sxe (:,:,layer,jl),   & 
-                        &                                       sxxe(:,:,layer,jl), sye (:,:,layer,jl),   &
-                        &                                       syye(:,:,layer,jl), sxye(:,:,layer,jl) )
-                     CALL lim_adv_y( zusnit, v_ice, rzero, zsm, zs0e(:,:,layer,jl), sxe (:,:,layer,jl),   & 
-                        &                                       sxxe(:,:,layer,jl), sye (:,:,layer,jl),   &
-                        &                                       syye(:,:,layer,jl), sxye(:,:,layer,jl) )
+                  DO jk = 1, nlay_i                                                           !--- ice heat contents ---
+                     CALL lim_adv_x( zusnit, u_ice, 1._wp , zsm, zs0e(:,:,jk,jl), sxe (:,:,jk,jl),   & 
+                        &                                       sxxe(:,:,jk,jl), sye (:,:,jk,jl),   &
+                        &                                       syye(:,:,jk,jl), sxye(:,:,jk,jl) )
+                     CALL lim_adv_y( zusnit, v_ice, 0._wp, zsm, zs0e(:,:,jk,jl), sxe (:,:,jk,jl),   & 
+                        &                                       sxxe(:,:,jk,jl), sye (:,:,jk,jl),   &
+                        &                                       syye(:,:,jk,jl), sxye(:,:,jk,jl) )
                   END DO
                END DO
             END DO
          ELSE
-            DO jk = 1, initad
-               CALL lim_adv_y( zusnit, v_ice, rone , zsm, zs0ow (:,:), sxopw(:,:),   &             !--- ice open water area
+            DO jn = 1, initad
+               CALL lim_adv_y( zusnit, v_ice, 1._wp , zsm, zs0ow (:,:), sxopw(:,:),   &             !--- ice open water area
                   &                                       sxxopw(:,:), syopw(:,:), syyopw(:,:), sxyopw(:,:)  )
-               CALL lim_adv_x( zusnit, u_ice, rzero, zsm, zs0ow (:,:), sxopw(:,:),   &
+               CALL lim_adv_x( zusnit, u_ice, 0._wp, zsm, zs0ow (:,:), sxopw(:,:),   &
                   &                                       sxxopw(:,:), syopw(:,:), syyopw(:,:), sxyopw(:,:)  )
                DO jl = 1, jpl
-                  CALL lim_adv_y( zusnit, v_ice, rone , zsm, zs0ice(:,:,jl), sxice(:,:,jl),   &    !--- ice volume  ---
+                  CALL lim_adv_y( zusnit, v_ice, 1._wp , zsm, zs0ice(:,:,jl), sxice(:,:,jl),   &    !--- ice volume  ---
                      &                                       sxxice(:,:,jl), syice(:,:,jl), syyice(:,:,jl), sxyice(:,:,jl)  )
-                  CALL lim_adv_x( zusnit, u_ice, rzero, zsm, zs0ice(:,:,jl), sxice(:,:,jl),   &
+                  CALL lim_adv_x( zusnit, u_ice, 0._wp, zsm, zs0ice(:,:,jl), sxice(:,:,jl),   &
                      &                                       sxxice(:,:,jl), syice(:,:,jl), syyice(:,:,jl), sxyice(:,:,jl)  )
-                  CALL lim_adv_y( zusnit, v_ice, rone , zsm, zs0sn (:,:,jl), sxsn (:,:,jl),   &    !--- snow volume  ---
+                  CALL lim_adv_y( zusnit, v_ice, 1._wp , zsm, zs0sn (:,:,jl), sxsn (:,:,jl),   &    !--- snow volume  ---
                      &                                       sxxsn (:,:,jl), sysn (:,:,jl), syysn (:,:,jl), sxysn (:,:,jl)  )
-                  CALL lim_adv_x( zusnit, u_ice, rzero, zsm, zs0sn (:,:,jl), sxsn (:,:,jl),   &
+                  CALL lim_adv_x( zusnit, u_ice, 0._wp, zsm, zs0sn (:,:,jl), sxsn (:,:,jl),   &
                      &                                       sxxsn (:,:,jl), sysn (:,:,jl), syysn (:,:,jl), sxysn (:,:,jl)  )
-                  CALL lim_adv_y( zusnit, v_ice, rone , zsm, zs0sm (:,:,jl), sxsal(:,:,jl),   &    !--- ice salinity ---
+                  CALL lim_adv_y( zusnit, v_ice, 1._wp , zsm, zs0sm (:,:,jl), sxsal(:,:,jl),   &    !--- ice salinity ---
                      &                                       sxxsal(:,:,jl), sysal(:,:,jl), syysal(:,:,jl), sxysal(:,:,jl)  )
-                  CALL lim_adv_x( zusnit, u_ice, rzero, zsm, zs0sm (:,:,jl), sxsal(:,:,jl),   &
+                  CALL lim_adv_x( zusnit, u_ice, 0._wp, zsm, zs0sm (:,:,jl), sxsal(:,:,jl),   &
                      &                                       sxxsal(:,:,jl), sysal(:,:,jl), syysal(:,:,jl), sxysal(:,:,jl)  )
 
-                  CALL lim_adv_y( zusnit, v_ice, rone , zsm, zs0oi (:,:,jl), sxage(:,:,jl),   &   !--- ice age      ---
+                  CALL lim_adv_y( zusnit, v_ice, 1._wp , zsm, zs0oi (:,:,jl), sxage(:,:,jl),   &   !--- ice age      ---
                      &                                       sxxage(:,:,jl), syage(:,:,jl), syyage(:,:,jl), sxyage(:,:,jl)  )
-                  CALL lim_adv_x( zusnit, u_ice, rzero, zsm, zs0oi (:,:,jl), sxage(:,:,jl),   &
+                  CALL lim_adv_x( zusnit, u_ice, 0._wp, zsm, zs0oi (:,:,jl), sxage(:,:,jl),   &
                      &                                       sxxage(:,:,jl), syage(:,:,jl), syyage(:,:,jl), sxyage(:,:,jl)  )
-                  CALL lim_adv_y( zusnit, v_ice, rone , zsm, zs0a  (:,:,jl), sxa  (:,:,jl),   &   !--- ice concentrations ---
+                  CALL lim_adv_y( zusnit, v_ice, 1._wp , zsm, zs0a  (:,:,jl), sxa  (:,:,jl),   &   !--- ice concentrations ---
                      &                                       sxxa  (:,:,jl), sya  (:,:,jl), syya  (:,:,jl), sxya  (:,:,jl)  )
-                  CALL lim_adv_x( zusnit, u_ice, rzero, zsm, zs0a  (:,:,jl), sxa  (:,:,jl),   &
+                  CALL lim_adv_x( zusnit, u_ice, 0._wp, zsm, zs0a  (:,:,jl), sxa  (:,:,jl),   &
                      &                                       sxxa  (:,:,jl), sya  (:,:,jl), syya  (:,:,jl), sxya  (:,:,jl)  )
-                  CALL lim_adv_y( zusnit, v_ice, rone , zsm, zs0c0 (:,:,jl), sxc0 (:,:,jl),   &  !--- snow heat contents ---
+                  CALL lim_adv_y( zusnit, v_ice, 1._wp , zsm, zs0c0 (:,:,jl), sxc0 (:,:,jl),   &  !--- snow heat contents ---
                      &                                       sxxc0 (:,:,jl), syc0 (:,:,jl), syyc0 (:,:,jl), sxyc0 (:,:,jl)  )
-                  CALL lim_adv_x( zusnit, u_ice, rzero, zsm, zs0c0 (:,:,jl), sxc0 (:,:,jl),   &
+                  CALL lim_adv_x( zusnit, u_ice, 0._wp, zsm, zs0c0 (:,:,jl), sxc0 (:,:,jl),   &
                      &                                       sxxc0 (:,:,jl), syc0 (:,:,jl), syyc0 (:,:,jl), sxyc0 (:,:,jl)  )
-                  DO layer = 1, nlay_i                                                           !--- ice heat contents ---
-                     CALL lim_adv_y( zusnit, v_ice, rone , zsm, zs0e(:,:,layer,jl), sxe (:,:,layer,jl),   & 
-                        &                                       sxxe(:,:,layer,jl), sye (:,:,layer,jl),   &
-                        &                                       syye(:,:,layer,jl), sxye(:,:,layer,jl) )
-                     CALL lim_adv_x( zusnit, u_ice, rzero, zsm, zs0e(:,:,layer,jl), sxe (:,:,layer,jl),   & 
-                        &                                       sxxe(:,:,layer,jl), sye (:,:,layer,jl),   &
-                        &                                       syye(:,:,layer,jl), sxye(:,:,layer,jl) )
+                  DO jk = 1, nlay_i                                                           !--- ice heat contents ---
+                     CALL lim_adv_y( zusnit, v_ice, 1._wp , zsm, zs0e(:,:,jk,jl), sxe (:,:,jk,jl),   & 
+                        &                                       sxxe(:,:,jk,jl), sye (:,:,jk,jl),   &
+                        &                                       syye(:,:,jk,jl), sxye(:,:,jk,jl) )
+                     CALL lim_adv_x( zusnit, u_ice, 0._wp, zsm, zs0e(:,:,jk,jl), sxe (:,:,jk,jl),   & 
+                        &                                       sxxe(:,:,jk,jl), sye (:,:,jk,jl),   &
+                        &                                       syye(:,:,jk,jl), sxye(:,:,jk,jl) )
                   END DO
                END DO
@@ -268,8 +254,5 @@
             zs0oi (:,:,jl) = zs0oi (:,:,jl) / area(:,:)
             zs0a  (:,:,jl) = zs0a  (:,:,jl) / area(:,:)
-            zs0c0 (:,:,jl) = zs0c0 (:,:,jl) / area(:,:)
-            DO jk = 1, nlay_i
-               zs0e(:,:,jk,jl) = zs0e(:,:,jk,jl) / area(:,:)
-            END DO
+            !
          END DO
 
@@ -289,8 +272,8 @@
          DO jj = 1, jpjm1                    ! NB: has not to be defined on jpj line and jpi row
             DO ji = 1 , fs_jpim1   ! vector opt.
-               pahu(ji,jj) = ( 1._wp - MAX( rzero, SIGN( rone, -zs0at(ji  ,jj) ) ) )   &
-                  &        * ( 1._wp - MAX( rzero, SIGN( rone, -zs0at(ji+1,jj) ) ) ) * ahiu(ji,jj)
-               pahv(ji,jj) = ( 1._wp - MAX( rzero, SIGN( rone, -zs0at(ji,jj  ) ) ) )   &
-                  &        * ( 1._wp - MAX( rzero, SIGN( rone,- zs0at(ji,jj+1) ) ) ) * ahiv(ji,jj)
+               pahu(ji,jj) = ( 1._wp - MAX( 0._wp, SIGN( 1._wp, -zs0at(ji  ,jj) ) ) )   &
+                  &        * ( 1._wp - MAX( 0._wp, SIGN( 1._wp, -zs0at(ji+1,jj) ) ) ) * ahiu(ji,jj)
+               pahv(ji,jj) = ( 1._wp - MAX( 0._wp, SIGN( 1._wp, -zs0at(ji,jj  ) ) ) )   &
+                  &        * ( 1._wp - MAX( 0._wp, SIGN( 1._wp,- zs0at(ji,jj+1) ) ) ) * ahiv(ji,jj)
             END DO
          END DO
@@ -305,8 +288,8 @@
             DO jj = 1, jpjm1                 ! NB: has not to be defined on jpj line and jpi row
                DO ji = 1 , fs_jpim1   ! vector opt.
-                  pahu(ji,jj) = ( 1._wp - MAX( rzero, SIGN( rone, -zs0a(ji  ,jj,jl) ) ) )   &
-                     &        * ( 1._wp - MAX( rzero, SIGN( rone, -zs0a(ji+1,jj,jl) ) ) ) * ahiu(ji,jj)
-                  pahv(ji,jj) = ( 1._wp - MAX( rzero, SIGN( rone, -zs0a(ji,jj  ,jl) ) ) )   &
-                     &        * ( 1._wp - MAX( rzero, SIGN( rone,- zs0a(ji,jj+1,jl) ) ) ) * ahiv(ji,jj)
+                  pahu(ji,jj) = ( 1._wp - MAX( 0._wp, SIGN( 1._wp, -zs0a(ji  ,jj,jl) ) ) )   &
+                     &        * ( 1._wp - MAX( 0._wp, SIGN( 1._wp, -zs0a(ji+1,jj,jl) ) ) ) * ahiu(ji,jj)
+                  pahv(ji,jj) = ( 1._wp - MAX( 0._wp, SIGN( 1._wp, -zs0a(ji,jj  ,jl) ) ) )   &
+                     &        * ( 1._wp - MAX( 0._wp, SIGN( 1._wp,- zs0a(ji,jj+1,jl) ) ) ) * ahiv(ji,jj)
                END DO
             END DO
@@ -334,10 +317,10 @@
             DO jj = 1, jpj
                DO ji = 1, jpi
-                  zs0sn (ji,jj,jl) = MAX( rzero, zs0sn (ji,jj,jl) )
-                  zs0ice(ji,jj,jl) = MAX( rzero, zs0ice(ji,jj,jl) )
-                  zs0sm (ji,jj,jl) = MAX( rzero, zs0sm (ji,jj,jl) )
-                  zs0oi (ji,jj,jl) = MAX( rzero, zs0oi (ji,jj,jl) )
-                  zs0a  (ji,jj,jl) = MAX( rzero, zs0a  (ji,jj,jl) )
-                  zs0c0 (ji,jj,jl) = MAX( rzero, zs0c0 (ji,jj,jl) )
+                  zs0sn (ji,jj,jl) = MAX( 0._wp, zs0sn (ji,jj,jl) )
+                  zs0ice(ji,jj,jl) = MAX( 0._wp, zs0ice(ji,jj,jl) )
+                  zs0sm (ji,jj,jl) = MAX( 0._wp, zs0sm (ji,jj,jl) )
+                  zs0oi (ji,jj,jl) = MAX( 0._wp, zs0oi (ji,jj,jl) )
+                  zs0a  (ji,jj,jl) = MAX( 0._wp, zs0a  (ji,jj,jl) )
+                  zs0c0 (ji,jj,jl) = MAX( 0._wp, zs0c0 (ji,jj,jl) )
                   zs0at (ji,jj)    = zs0at(ji,jj) + zs0a(ji,jj,jl)
                END DO
@@ -346,124 +329,35 @@
 
          !---------------------------------------------------------
-         ! 5.2) Snow thickness, Ice thickness, Ice concentrations
+         ! 5.2) Update and mask variables
          !---------------------------------------------------------
-         DO jj = 1, jpj
-            DO ji = 1, jpi
-               zindb        = MAX( 0._wp , SIGN( 1.0, zs0at(ji,jj) - epsi10) )
-               zs0ow(ji,jj) = ( 1._wp - zindb ) + zindb * MAX( zs0ow(ji,jj), 0._wp )
-               ato_i(ji,jj) = zs0ow(ji,jj)
-            END DO
-         END DO
-
-         DO jl = 1, jpl         ! Remove very small areas 
+         DO jl = 1, jpl          
             DO jj = 1, jpj
                DO ji = 1, jpi
-                  zvi = zs0ice(ji,jj,jl)
-                  zvs = zs0sn(ji,jj,jl)
+                  rswitch = MAX( 0._wp , SIGN( 1._wp, zs0a(ji,jj,jl) - epsi10 ) )
+
+                  zvi  = zs0ice(ji,jj,jl)
+                  zvs  = zs0sn (ji,jj,jl)
+                  zes  = zs0c0 (ji,jj,jl)      
+                  zsmv = zs0sm (ji,jj,jl)
                   !
-                  zindb         = MAX( 0.0 , SIGN( 1.0, zs0a(ji,jj,jl) - epsi10) )
-                  !
-                  v_s(ji,jj,jl)  = zindb * zs0sn (ji,jj,jl) 
-                  v_i(ji,jj,jl)  = zindb * zs0ice(ji,jj,jl)
-                  !
-                  zindsn         = MAX( rzero, SIGN( rone, v_s(ji,jj,jl) - epsi10 ) )
-                  zindic         = MAX( rzero, SIGN( rone, v_i(ji,jj,jl) - epsi10 ) )
-                  zindb          = MAX( zindsn, zindic )
-                  !
-                  zs0a(ji,jj,jl) = zindb  * zs0a(ji,jj,jl) !ice concentration
-                  a_i (ji,jj,jl) = zs0a(ji,jj,jl)
-                  v_s (ji,jj,jl) = zindsn * v_s(ji,jj,jl)
-                  v_i (ji,jj,jl) = zindic * v_i(ji,jj,jl)
-                  !
-                  ! Update mass fluxes (clem)
-                  rdm_ice(ji,jj) = rdm_ice(ji,jj) + ( v_i(ji,jj,jl) - zvi ) * rhoic 
-                  rdm_snw(ji,jj) = rdm_snw(ji,jj) + ( v_s(ji,jj,jl) - zvs ) * rhosn 
-              END DO
-            END DO
-         END DO
-
-         !--- Thickness correction in case too high (clem) --------------------------------------------------------
-         CALL lim_var_glo2eqv
-         DO jl = 1, jpl
-            DO jj = 1, jpj
-               DO ji = 1, jpi
-
-                  IF ( v_i(ji,jj,jl) > 0._wp ) THEN
-                     zvi = v_i(ji,jj,jl)
-                     zvs = v_s(ji,jj,jl)
-                     zdv = v_i(ji,jj,jl) - zviold(ji,jj,jl)   
-                     !zda = a_i(ji,jj,jl) - zaiold(ji,jj,jl)   
-                     
-                     zindh = 1._wp
-                     IF ( ( zdv > 0.0 .AND. ht_i(ji,jj,jl) > zhimax(ji,jj,jl) .AND. SUM( zaiold(ji,jj,1:jpl) ) < 0.80 ) .OR. &
-                        & ( zdv < 0.0 .AND. ht_i(ji,jj,jl) > zhimax(ji,jj,jl) ) ) THEN                                          
-                        ht_i(ji,jj,jl) = MIN( zhimax(ji,jj,jl), hi_max(jl) )
-                        zindh   =  MAX( rzero, SIGN( rone, ht_i(ji,jj,jl) - epsi10 ) )
-                        a_i(ji,jj,jl)  = zindh * v_i(ji,jj,jl) / MAX( ht_i(ji,jj,jl), epsi10 )
-                     ELSE
-                        ht_i(ji,jj,jl) = MAX( MIN( ht_i(ji,jj,jl), hi_max(jl) ), hi_max(jl-1) )
-                        zindh   =  MAX( rzero, SIGN( rone, ht_i(ji,jj,jl) - epsi10 ) )
-                        a_i(ji,jj,jl)  = zindh * v_i(ji,jj,jl) / MAX( ht_i(ji,jj,jl), epsi10 )
-                     ENDIF
-
-                     ! small correction due to *zindh for a_i
-                     v_i(ji,jj,jl) = zindh * v_i(ji,jj,jl)
-                     v_s(ji,jj,jl) = zindh * v_s(ji,jj,jl)
-
-                     ! Update mass fluxes
-                     rdm_ice(ji,jj) = rdm_ice(ji,jj) + ( v_i(ji,jj,jl) - zvi ) * rhoic
-                     rdm_snw(ji,jj) = rdm_snw(ji,jj) + ( v_s(ji,jj,jl) - zvs ) * rhosn
-
-                  ENDIF
-
-                  diag_trp_vi(ji,jj) = diag_trp_vi(ji,jj) + ( v_i(ji,jj,jl) - zviold(ji,jj,jl) ) * r1_rdtice
-
-               END DO
-            END DO
-         END DO
-
-         ! ---
-         DO jj = 1, jpj
-            DO ji = 1, jpi
-               zs0at(ji,jj) = SUM( zs0a(ji,jj,1:jpl) ) ! clem@useless??
-            END DO
-         END DO
-
-         !----------------------
-         ! 5.3) Ice properties
-         !----------------------
-
-         zbigval = 1.e+13
-
-         DO jl = 1, jpl
-            DO jj = 1, jpj
-               DO ji = 1, jpi
-                  zsmv = zs0sm(ji,jj,jl)
-
-                  ! Switches and dummy variables
-                  zusvosn         = 1.0/MAX( v_s(ji,jj,jl) , epsi10 )
-                  zusvoic         = 1.0/MAX( v_i(ji,jj,jl) , epsi10 )
-                  zindsn          = MAX( rzero, SIGN( rone, v_s(ji,jj,jl) - epsi10 ) )
-                  zindic          = MAX( rzero, SIGN( rone, v_i(ji,jj,jl) - epsi10 ) )
-                  zindb           = MAX( zindsn, zindic )
-
+                  ! Remove very small areas
+                  v_s(ji,jj,jl)   = rswitch * zs0sn (ji,jj,jl) 
+                  v_i(ji,jj,jl)   = rswitch * zs0ice(ji,jj,jl)
+                  a_i(ji,jj,jl)   = rswitch * zs0a  (ji,jj,jl)
+                  e_s(ji,jj,1,jl) = rswitch * zs0c0 (ji,jj,jl)      
                   ! Ice salinity and age
-                  !clem zsal = MAX( MIN( (rhoic-rhosn)/rhoic*sss_m(ji,jj), zusvoic * zs0sm(ji,jj,jl) ), s_i_min ) * v_i(ji,jj,jl)
                   IF(  num_sal == 2  ) THEN
                      smv_i(ji,jj,jl) = MAX( MIN( s_i_max * v_i(ji,jj,jl), zsmv ), s_i_min * v_i(ji,jj,jl) )
                   ENDIF
-
-                  zage = MAX( MIN( zbigval, zs0oi(ji,jj,jl) / MAX( a_i(ji,jj,jl), epsi10 ) ), 0._wp  ) * a_i(ji,jj,jl)
-                  oa_i (ji,jj,jl)  = zindic * zage 
-
-                  ! Snow heat content
-                  ze              =  MIN( MAX( 0.0, zs0c0(ji,jj,jl)*area(ji,jj) ), zbigval )
-                  e_s(ji,jj,1,jl) = zindsn * ze      
-
-                  ! Update salt fluxes (clem)
+                  oa_i(ji,jj,jl) = MAX( rswitch * zs0oi(ji,jj,jl) / MAX( a_i(ji,jj,jl), epsi10 ), 0._wp ) * a_i(ji,jj,jl)
+
+                 ! Update fluxes
+                  wfx_res(ji,jj) = wfx_res(ji,jj) - ( v_i(ji,jj,jl) - zvi ) * rhoic * r1_rdtice 
+                  wfx_snw(ji,jj) = wfx_snw(ji,jj) - ( v_s(ji,jj,jl) - zvs ) * rhosn * r1_rdtice
                   sfx_res(ji,jj) = sfx_res(ji,jj) - ( smv_i(ji,jj,jl) - zsmv ) * rhoic * r1_rdtice 
-               END DO !ji
-            END DO !jj
-         END DO ! jl
+                  hfx_res(ji,jj) = hfx_res(ji,jj) + ( e_s(ji,jj,1,jl) - zes ) * unit_fac / area(ji,jj) * r1_rdtice ! W.m-2 <0
+              END DO
+            END DO
+         END DO
 
          DO jl = 1, jpl
@@ -471,8 +365,9 @@
                DO jj = 1, jpj
                   DO ji = 1, jpi
-                     ! Ice heat content
-                     zindic          =  MAX( rzero, SIGN( rone, v_i(ji,jj,jl) - epsi10 ) )
-                     ze              =  MIN( MAX( 0.0, zs0e(ji,jj,jk,jl)*area(ji,jj) ), zbigval )
-                     e_i(ji,jj,jk,jl) = zindic * ze
+                     rswitch          = MAX( 0._wp , SIGN( 1._wp, zs0a(ji,jj,jl) - epsi10 ) )
+                     zei              = zs0e(ji,jj,jk,jl)      
+                     e_i(ji,jj,jk,jl) = rswitch * MAX( 0._wp, zs0e(ji,jj,jk,jl) )
+                     ! Update fluxes
+                     hfx_res(ji,jj) = hfx_res(ji,jj) + ( e_i(ji,jj,jk,jl) - zei ) * unit_fac / area(ji,jj) * r1_rdtice ! W.m-2 <0
                   END DO !ji
                END DO ! jj
@@ -480,26 +375,89 @@
          END DO ! jl
 
-
-      ! --- agglomerate variables (clem) -----------------
-      vt_i (:,:) = 0._wp
-      vt_s (:,:) = 0._wp
-      at_i (:,:) = 0._wp
-      !
-      DO jl = 1, jpl
+         !--- Thickness correction in case too high (clem) --------------------------------------------------------
+         CALL lim_var_glo2eqv
+         DO jl = 1, jpl
+            DO jj = 1, jpj
+               DO ji = 1, jpi
+
+                  IF ( v_i(ji,jj,jl) > 0._wp ) THEN
+                     zvi  = v_i  (ji,jj,jl)
+                     zvs  = v_s  (ji,jj,jl)
+                     zsmv = smv_i(ji,jj,jl)
+                     zes  = e_s  (ji,jj,1,jl)
+                     zei  = SUM( e_i(ji,jj,1:nlay_i,jl) )
+                     zdv  = v_i(ji,jj,jl) - zviold(ji,jj,jl)   
+                     !zda = a_i(ji,jj,jl) - zaiold(ji,jj,jl)   
+                     
+                     rswitch = 1._wp
+                     IF ( ( zdv > 0.0 .AND. ht_i(ji,jj,jl) > zhimax(ji,jj,jl) .AND. SUM( zaiold(ji,jj,1:jpl) ) < 0.80 ) .OR. &
+                        & ( zdv < 0.0 .AND. ht_i(ji,jj,jl) > zhimax(ji,jj,jl) ) ) THEN                                          
+                        ht_i(ji,jj,jl) = MIN( zhimax(ji,jj,jl), hi_max(jl) )
+                        rswitch        = MAX( 0._wp, SIGN( 1._wp, ht_i(ji,jj,jl) - epsi20 ) )
+                        a_i(ji,jj,jl)  = rswitch * v_i(ji,jj,jl) / MAX( ht_i(ji,jj,jl), epsi20 )
+                     ELSE
+                        ht_i(ji,jj,jl) = MAX( MIN( ht_i(ji,jj,jl), hi_max(jl) ), hi_max(jl-1) )
+                        rswitch        = MAX( 0._wp, SIGN( 1._wp, ht_i(ji,jj,jl) - epsi20 ) )
+                        a_i(ji,jj,jl)  = rswitch * v_i(ji,jj,jl) / MAX( ht_i(ji,jj,jl), epsi20 )
+                     ENDIF
+
+                     ! small correction due to *rswitch for a_i
+                     v_i  (ji,jj,jl) = rswitch * v_i  (ji,jj,jl)
+                     v_s  (ji,jj,jl) = rswitch * v_s  (ji,jj,jl)
+                     smv_i(ji,jj,jl) = rswitch * smv_i(ji,jj,jl)
+                     e_s(ji,jj,1,jl) = rswitch * e_s(ji,jj,1,jl)
+                     e_i(ji,jj,1:nlay_i,jl) = rswitch * e_i(ji,jj,1:nlay_i,jl)
+
+                     ! Update mass fluxes
+                     wfx_res(ji,jj) = wfx_res(ji,jj) - ( v_i(ji,jj,jl) - zvi ) * rhoic * r1_rdtice
+                     wfx_snw(ji,jj) = wfx_snw(ji,jj) - ( v_s(ji,jj,jl) - zvs ) * rhosn * r1_rdtice
+                     sfx_res(ji,jj) = sfx_res(ji,jj) - ( smv_i(ji,jj,jl) - zsmv ) * rhoic * r1_rdtice 
+                     hfx_res(ji,jj) = hfx_res(ji,jj) + ( e_s(ji,jj,1,jl) - zes ) * unit_fac / area(ji,jj) * r1_rdtice ! W.m-2 <0
+                     hfx_res(ji,jj) = hfx_res(ji,jj) + ( SUM( e_i(ji,jj,1:nlay_i,jl) ) - zei ) * unit_fac / area(ji,jj) * r1_rdtice ! W.m-2 <0
+                  ENDIF
+               END DO
+            END DO
+         END DO
+         ! -------------------------------------------------
+
+         ! --- diags ---
          DO jj = 1, jpj
             DO ji = 1, jpi
-               !
-               vt_i(ji,jj) = vt_i(ji,jj) + v_i(ji,jj,jl) ! ice volume
-               vt_s(ji,jj) = vt_s(ji,jj) + v_s(ji,jj,jl) ! snow volume
-               at_i(ji,jj) = at_i(ji,jj) + a_i(ji,jj,jl) ! ice concentration
-               !
-               zinda = MAX( rzero , SIGN( rone , at_i(ji,jj) - epsi10 ) )
-               icethi(ji,jj) = vt_i(ji,jj) / MAX( at_i(ji,jj) , epsi10 ) * zinda  ! ice thickness
-            END DO
-         END DO
-      END DO
-      ! -------------------------------------------------
-
-
+               diag_trp_ei(ji,jj) = ( SUM( e_i(ji,jj,1:nlay_i,:) ) - zeiold(ji,jj) ) / area(ji,jj) * unit_fac * r1_rdtice
+               diag_trp_es(ji,jj) = ( SUM( e_s(ji,jj,1:nlay_s,:) ) - zesold(ji,jj) ) / area(ji,jj) * unit_fac * r1_rdtice
+
+               diag_trp_vi(ji,jj) = SUM( v_i(ji,jj,:) - zviold(ji,jj,:) ) * r1_rdtice
+               diag_trp_vs(ji,jj) = SUM( v_s(ji,jj,:) - zvsold(ji,jj,:) ) * r1_rdtice
+            END DO
+         END DO
+
+         ! --- agglomerate variables -----------------
+         vt_i (:,:) = 0._wp
+         vt_s (:,:) = 0._wp
+         at_i (:,:) = 0._wp
+         !
+         DO jl = 1, jpl
+            DO jj = 1, jpj
+               DO ji = 1, jpi
+                  !
+                  vt_i(ji,jj) = vt_i(ji,jj) + v_i(ji,jj,jl) ! ice volume
+                  vt_s(ji,jj) = vt_s(ji,jj) + v_s(ji,jj,jl) ! snow volume
+                  at_i(ji,jj) = at_i(ji,jj) + a_i(ji,jj,jl) ! ice concentration
+               END DO
+            END DO
+         END DO
+         ! -------------------------------------------------
+
+         ! open water
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               ! open water = 1 if at_i=0
+               rswitch      = MAX( 0._wp , SIGN( 1._wp, - at_i(ji,jj) ) )
+               ato_i(ji,jj) = rswitch + (1._wp - rswitch ) * zs0ow(ji,jj)
+            END DO
+         END DO      
+
+         ! conservation test
+         IF( ln_limdiahsb ) CALL lim_cons_hsm(1, 'limtrp', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
 
       ENDIF
@@ -536,37 +494,10 @@
          END DO
       ENDIF
-      ! -------------------------------
-      !- check conservation (C Rousset)
-      IF( ln_limdiahsb ) THEN
-         zchk_fs  = glob_sum( ( sfx_bri(:,:) + sfx_thd(:,:) + sfx_res(:,:) + sfx_mec(:,:) ) * area(:,:) * tms(:,:) ) - zchk_fs_b
-         zchk_fw  = glob_sum( rdm_ice(:,:) * area(:,:) * tms(:,:) ) - zchk_fw_b
- 
-         zchk_v_i = ( glob_sum( SUM(   v_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) ) - zchk_v_i_b - ( zchk_fw / rhoic ) ) / rdt_ice
-         zchk_smv = ( glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) ) - zchk_smv_b ) / rdt_ice + ( zchk_fs / rhoic )
-
-         zchk_vmin = glob_min(v_i)
-         zchk_amax = glob_max(SUM(a_i,dim=3))
-         zchk_amin = glob_min(a_i)
-         zchk_umax = glob_max(SQRT(u_ice**2 + v_ice**2))
-
-         IF(lwp) THEN
-            IF ( ABS( zchk_v_i   ) >  1.e-5 ) THEN
-               WRITE(numout,*) 'violation volume [m3/day]     (limtrp) = ',(zchk_v_i * rday)
-               WRITE(numout,*) 'u_ice max [m/s]               (limtrp) = ',zchk_umax
-               WRITE(numout,*) 'number of time steps          (limtrp) =',kt
-            ENDIF
-            IF ( ABS( zchk_smv   ) >  1.e-4 ) WRITE(numout,*) 'violation saline [psu*m3/day] (limtrp) = ',(zchk_smv * rday)
-            IF ( zchk_vmin <  0.            ) WRITE(numout,*) 'violation v_i<0  [mm]         (limtrp) = ',(zchk_vmin * 1.e-3)
-            IF ( zchk_amin <  0.            ) WRITE(numout,*) 'violation a_i<0               (limtrp) = ',zchk_amin
-         ENDIF
-      ENDIF
-      !- check conservation (C Rousset)
-      ! -------------------------------
       !
-      CALL wrk_dealloc( jpi, jpj, zui_u, zvi_v, zsm, zs0at, zs0ow )
+      CALL wrk_dealloc( jpi, jpj, zui_u, zvi_v, zsm, zs0at, zs0ow, zeiold, zesold )
       CALL wrk_dealloc( jpi, jpj, jpl, zs0ice, zs0sn, zs0a, zs0c0 , zs0sm , zs0oi )
-      CALL wrk_dealloc( jpi, jpj, jkmax, jpl, zs0e )
-
-      CALL wrk_dealloc( jpi,jpj,jpl,zaiold, zhimax )   ! clem
+      CALL wrk_dealloc( jpi, jpj, nlay_i+1, jpl, zs0e )
+
+      CALL wrk_dealloc( jpi, jpj, jpl, zviold, zvsold, zaiold, zhimax )   ! clem
       !
       IF( nn_timing == 1 )  CALL timing_stop('limtrp')
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limupdate1.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limupdate1.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limupdate1.F90	(revision 5038)
@@ -5,4 +5,5 @@
    !!======================================================================
    !! History :  3.0  !  2006-04  (M. Vancoppenolle) Original code
+   !!            3.6  !  2014-06  (C. Rousset)       Complete rewriting/cleaning
    !!----------------------------------------------------------------------
 #if defined key_lim3
@@ -32,4 +33,5 @@
    USE par_ice
    USE limitd_th
+   USE limitd_me
    USE limvar
    USE prtctl           ! Print control
@@ -37,9 +39,9 @@
    USE wrk_nemo         ! work arrays
    USE lib_fortran     ! glob_sum
-   ! Check budget (Rousset)
    USE in_out_manager   ! I/O manager
    USE iom              ! I/O manager
    USE lib_mpp          ! MPP library
    USE timing          ! Timing
+   USE limcons        ! conservation tests
 
    IMPLICIT NONE
@@ -48,8 +50,4 @@
    PUBLIC   lim_update1   ! routine called by ice_step
 
-      REAL(wp)  ::   epsi10 = 1.e-10_wp   !    -       -
-      REAL(wp)  ::   rzero  = 0._wp       !    -       -
-      REAL(wp)  ::   rone   = 1._wp       !    -       -
-         
    !! * Substitutions
 #  include "vectopt_loop_substitute.h90"
@@ -66,122 +64,32 @@
       !!               
       !! ** Purpose :  Computes update of sea-ice global variables at 
-      !!               the end of the time step.
-      !!               Address pathological cases
-      !!               This place is very important
+      !!               the end of the dynamics.
       !!                
-      !! ** Method  :  
-      !!    Ice speed from ice dynamics
-      !!    Ice thickness, Snow thickness, Temperatures, Lead fraction
-      !!      from advection and ice thermodynamics 
-      !!
-      !! ** Action  : - 
       !!---------------------------------------------------------------------
-      INTEGER ::   ji, jj, jk, jl, jm    ! dummy loop indices
-      INTEGER ::   jbnd1, jbnd2
-      INTEGER ::   i_ice_switch
-      INTEGER ::   ind_im, layer      ! indices for internal melt
-      REAL(wp) ::   zweight, zesum, z_da_i, zhimax
-      REAL(wp) ::   zinda, zindb, zindsn, zindic
-      REAL(wp) ::   zindg, zh, zdvres, zviold2
-      REAL(wp) ::   zbigvalue, zvsold2, z_da_ex
-      REAL(wp) ::   z_prescr_hi, zat_i_old, ztmelts, ze_s
-
-      REAL(wp), POINTER, DIMENSION(:) ::   zthick0, zqm0      ! thickness of the layers and heat contents for
-      REAL(wp) :: zchk_v_i, zchk_smv, zchk_fs, zchk_fw, zchk_v_i_b, zchk_smv_b, zchk_fs_b, zchk_fw_b ! Check conservation (C Rousset)
-      REAL(wp) :: zchk_vmin, zchk_amin, zchk_amax ! Check errors (C Rousset)
-      ! mass and salt flux (clem)
-      REAL(wp), POINTER, DIMENSION(:,:,:) ::   zviold, zvsold, zsmvold   ! old ice volume...
+      INTEGER  ::   ji, jj, jk, jl   ! dummy loop indices
+      INTEGER  ::   i_ice_switch
+      REAL(wp) ::   zsal
+      !
+      REAL(wp) :: zvi_b, zsmv_b, zei_b, zfs_b, zfw_b, zft_b 
       !!-------------------------------------------------------------------
       IF( nn_timing == 1 )  CALL timing_start('limupdate1')
 
-      CALL wrk_alloc( jkmax, zthick0, zqm0 )
-
-      CALL wrk_alloc( jpi,jpj,jpl,zviold, zvsold, zsmvold )   ! clem
-
-      !------------------------------------------------------------------------------
-      ! 1. Update of Global variables                                               |
-      !------------------------------------------------------------------------------
-
-      !-----------------
-      !  Trend terms
-      !-----------------
-      d_u_ice_dyn(:,:)     = u_ice(:,:)     - old_u_ice(:,:)
-      d_v_ice_dyn(:,:)     = v_ice(:,:)     - old_v_ice(:,:)
-      d_a_i_trp  (:,:,:)   = a_i  (:,:,:)   - old_a_i  (:,:,:)
-      d_v_s_trp  (:,:,:)   = v_s  (:,:,:)   - old_v_s  (:,:,:)  
-      d_v_i_trp  (:,:,:)   = v_i  (:,:,:)   - old_v_i  (:,:,:)   
-      d_e_s_trp  (:,:,:,:) = e_s  (:,:,:,:) - old_e_s  (:,:,:,:)  
-      d_e_i_trp  (:,:,:,:) = e_i  (:,:,:,:) - old_e_i  (:,:,:,:)
-      d_oa_i_trp (:,:,:)   = oa_i (:,:,:)   - old_oa_i (:,:,:)
-      d_smv_i_trp(:,:,:)   = 0._wp
-      IF(  num_sal == 2  )   d_smv_i_trp(:,:,:)  = smv_i(:,:,:) - old_smv_i(:,:,:)
-
-      ! mass and salt flux init (clem)
-      zviold(:,:,:) = v_i(:,:,:)
-      zvsold(:,:,:) = v_s(:,:,:)
-      zsmvold(:,:,:) = smv_i(:,:,:)
-
-      ! -------------------------------
-      !- check conservation (C Rousset)
-      IF (ln_limdiahsb) THEN
-         zchk_v_i_b = glob_sum( SUM(   v_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) )
-         zchk_smv_b = glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) )
-         zchk_fw_b  = glob_sum( rdm_ice(:,:) * area(:,:) * tms(:,:) )
-         zchk_fs_b  = glob_sum( ( sfx_bri(:,:) + sfx_thd(:,:) + sfx_res(:,:) + sfx_mec(:,:) ) * area(:,:) * tms(:,:) )
-      ENDIF
-      !- check conservation (C Rousset)
-      ! -------------------------------
+      IF( ln_limdyn ) THEN 
+
+      ! conservation test
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(0, 'limupdate1', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
+
+      !-----------------
+      ! zap small values
+      !-----------------
+      CALL lim_itd_me_zapsmall
 
       CALL lim_var_glo2eqv
-
-      !--------------------------------------
-      ! 2. Review of all pathological cases
-      !--------------------------------------
-
-! clem: useless now
-      !-------------------------------------------
-      ! 2.1) Advection of ice in an ice-free cell
-      !-------------------------------------------
-      ! should be removed since it is treated after dynamics now
-!      zhimax = 5._wp
-!      ! first category
-!      DO jj = 1, jpj
-!         DO ji = 1, jpi
-!            !--- the thickness of such an ice is often out of bounds
-!            !--- thus we recompute a new area while conserving ice volume
-!            zat_i_old = SUM( old_a_i(ji,jj,:) )
-!            zindb     = MAX( 0._wp, SIGN( 1._wp, ABS( d_a_i_trp(ji,jj,1) ) - epsi10 ) ) 
-!            IF( ( ABS( d_v_i_trp(ji,jj,1) ) / MAX( ABS( d_a_i_trp(ji,jj,1) ), epsi10 ) * zindb .GT. zhimax ) &
-!              &   .AND.( ( v_i(ji,jj,1) / MAX( a_i(ji,jj,1), epsi10 ) * zindb ) .GT. zhimax ) &
-!              &   .AND.( zat_i_old .LT. 1.e-6 ) )  THEN ! new line
-!               ht_i(ji,jj,1) = hi_max(1) * 0.5_wp
-!               a_i (ji,jj,1) = v_i(ji,jj,1) / ht_i(ji,jj,1)
-!            ENDIF
-!         END DO
-!      END DO
-!
-!      zhimax = 20._wp
-!      ! other categories
-!      DO jl = 2, jpl
-!         jm = ice_types(jl)
-!         DO jj = 1, jpj
-!            DO ji = 1, jpi
-!               zindb =  MAX( rzero, SIGN( rone, ABS( d_a_i_trp(ji,jj,jl) ) - epsi10 ) ) 
-!               ! this correction is very tricky... sometimes, advection gets wrong i don't know why
-!               ! it makes problems when the advected volume and concentration do not seem to be 
-!               ! related with each other
-!               ! the new thickness is sometimes very big!
-!               ! and sometimes d_a_i_trp and d_v_i_trp have different sign
-!               ! which of course is plausible
-!               ! but fuck! it fucks everything up :)
-!               IF ( ( ABS( d_v_i_trp(ji,jj,jl) ) / MAX( ABS( d_a_i_trp(ji,jj,jl) ), epsi10 ) * zindb .GT. zhimax ) &
-!                  &  .AND. ( v_i(ji,jj,jl) / MAX( a_i(ji,jj,jl), epsi10 ) * zindb ) .GT. zhimax ) THEN
-!                  ht_i(ji,jj,jl) = ( hi_max_typ(jl-ice_cat_bounds(jm,1),jm) + hi_max_typ(jl-ice_cat_bounds(jm,1)+1,jm) ) * 0.5_wp
-!                  a_i (ji,jj,jl) = v_i(ji,jj,jl) / ht_i(ji,jj,jl)
-!               ENDIF
-!            END DO ! ji
-!         END DO !jj
-!      END DO !jl
      
+      !----------------------------------------------------
+      ! Rebin categories with thickness out of bounds
+      !----------------------------------------------------
+      IF ( jpl > 1 )   CALL lim_itd_th_reb(1, jpl)
+
       at_i(:,:) = 0._wp
       DO jl = 1, jpl
@@ -190,10 +98,15 @@
 
       !----------------------------------------------------
-      ! 2.2) Rebin categories with thickness out of bounds
-      !----------------------------------------------------
-      DO jm = 1, jpm
-         jbnd1 = ice_cat_bounds(jm,1)
-         jbnd2 = ice_cat_bounds(jm,2)
-         IF (ice_ncat_types(jm) .GT. 1 )   CALL lim_itd_th_reb(jbnd1, jbnd2, jm)
+      ! ice concentration should not exceed amax 
+      !-----------------------------------------------------
+      DO jl  = 1, jpl
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               IF( at_i(ji,jj) > amax .AND. a_i(ji,jj,jl) > 0._wp ) THEN
+                  a_i(ji,jj,jl)  = a_i(ji,jj,jl) * ( 1._wp - ( 1._wp - amax / at_i(ji,jj) ) )
+                  ht_i(ji,jj,jl) = v_i(ji,jj,jl) / a_i(ji,jj,jl)
+               ENDIF
+            END DO
+         END DO
       END DO
 
@@ -202,190 +115,50 @@
          at_i(:,:) = a_i(:,:,jl) + at_i(:,:)
       END DO
-
-      zbigvalue      = 1.0e+20
-
-      DO jl = 1, jpl
-         DO jj = 1, jpj 
-            DO ji = 1, jpi
-
-               !switches
-               zindb         = MAX( rzero, SIGN( rone, a_i(ji,jj,jl) - epsi10 ) ) 
-               !switch = 1 if a_i > 1e-06 and 0 if not
-               zindsn        = MAX( rzero, SIGN( rone, v_s(ji,jj,jl) - epsi10 ) ) !=1 if hs > 1e-10 and 0 if not
-               zindic        = MAX( rzero, SIGN( rone, v_i(ji,jj,jl) - epsi10 ) ) !=1 if hi > 1e-10 and 0 if not
-               ! bug fix 25 avril 2007
-               zindb         = zindb*zindic
-
-               !--- 2.3 Correction to ice age 
-               !------------------------------
-               !                IF ((o_i(ji,jj,jl)-1.0)*rday.gt.(rdt_ice*float(numit))) THEN
-               !                   o_i(ji,jj,jl) = rdt_ice*FLOAT(numit)/rday
-               !                ENDIF
-               IF ((oa_i(ji,jj,jl)-1.0)*rday.gt.(rdt_ice*numit*a_i(ji,jj,jl))) THEN
-                  oa_i(ji,jj,jl) = rdt_ice*numit/rday*a_i(ji,jj,jl)
-               ENDIF
-               oa_i(ji,jj,jl) = zindb*zindic*oa_i(ji,jj,jl)
-
-               !--- 2.4 Correction to snow thickness
-               !-------------------------------------
-               !          ! snow thickness has to be greater than 0, and if ice concentration smaller than 1e-6 then hs = 0
-               !             v_s(ji,jj,jl)  = MAX( zindb * v_s(ji,jj,jl), 0.0) 
-               ! snow thickness cannot be smaller than 1e-6
-               zdvres = (zindsn * zindb - 1._wp) * v_s(ji,jj,jl)
-               v_s(ji,jj,jl) = v_s(ji,jj,jl) + zdvres
-
-               !rdm_snw(ji,jj) = rdm_snw(ji,jj) + zdvres * rhosn
- 
-               !--- 2.5 Correction to ice thickness
-               !-------------------------------------
-               zdvres = (zindb - 1._wp) * v_i(ji,jj,jl)
-               v_i(ji,jj,jl) = v_i(ji,jj,jl) + zdvres
-
-               !rdm_ice(ji,jj) = rdm_ice(ji,jj) + zdvres * rhoic
-               !sfx_res(ji,jj)  = sfx_res(ji,jj) - sm_i(ji,jj,jl) * ( rhoic * zdvres / rdt_ice )
-
-               !--- 2.6 Snow is transformed into ice if the original ice cover disappears
-               !----------------------------------------------------------------------------
-               zindg          = tms(ji,jj) *  MAX( 0._wp, SIGN( 1._wp, -v_i(ji,jj,jl) ) )
-               zdvres         = zindg * rhosn * v_s(ji,jj,jl) / rau0
-               v_i(ji,jj,jl)  = v_i(ji,jj,jl)  + zdvres
-
-               zdvres         = zindsn*zindb * ( - zindg * v_s(ji,jj,jl) + zindg * v_i(ji,jj,jl) * ( rau0 - rhoic ) / rhosn )
-               v_s(ji,jj,jl)  = v_s(ji,jj,jl)  + zdvres
-
-               !--- 2.7 Correction to ice concentrations 
-               !--------------------------------------------
-               ! if greater than 0, ice concentration cannot be smaller than 1e-10
-               a_i(ji,jj,jl) = zindb * a_i(ji,jj,jl)
-
-               !-------------------------
-               ! 2.8) Snow heat content
-               !-------------------------
-               e_s(ji,jj,1,jl) = zindsn * ( MIN ( MAX ( 0._wp, e_s(ji,jj,1,jl) ), zbigvalue ) )
-
-            END DO ! ji
-         END DO ! jj
-      END DO ! jl
-
-      !------------------------
-      ! 2.9) Ice heat content 
-      !------------------------
-
-      DO jl = 1, jpl
-         DO jk = 1, nlay_i
+    
+      ! --------------------------------------
+      ! Final thickness distribution rebinning
+      ! --------------------------------------
+      IF ( jpl > 1 ) CALL lim_itd_th_reb(1, jpl)
+
+      !-----------------
+      ! zap small values
+      !-----------------
+      CALL lim_itd_me_zapsmall
+
+      !---------------------
+      ! Ice salinity bounds
+      !---------------------
+      IF (  num_sal == 2  ) THEN 
+         DO jl = 1, jpl
             DO jj = 1, jpj 
                DO ji = 1, jpi
-                  zindic        = MAX( rzero, SIGN( rone, v_i(ji,jj,jl) - epsi10 ) ) 
-                  e_i(ji,jj,jk,jl)= zindic * ( MIN ( MAX ( 0.0, e_i(ji,jj,jk,jl) ), zbigvalue ) )
-               END DO ! ji
-            END DO ! jj
-         END DO !jk
-      END DO !jl
- 
-      at_i(:,:) = 0._wp
-      DO jl = 1, jpl
-         at_i(:,:) = a_i(:,:,jl) + at_i(:,:)
-      END DO
-
-      !--- 2.13 ice concentration should not exceed amax 
-      !         (it should not be the case) 
-      !-----------------------------------------------------
-      DO jj = 1, jpj
-         DO ji = 1, jpi
-            z_da_ex =  MAX( at_i(ji,jj) - amax , 0.0 )        
-            zindb   =  MAX( rzero, SIGN( rone, at_i(ji,jj) - epsi10 ) ) 
-            DO jl  = 1, jpl
-               z_da_i = a_i(ji,jj,jl) * z_da_ex / MAX( at_i(ji,jj), epsi10 ) * zindb
-               a_i(ji,jj,jl) = MAX( 0._wp, a_i(ji,jj,jl) - z_da_i )
-               !
-               zinda   =  MAX( rzero, SIGN( rone, a_i(ji,jj,jl) - epsi10 ) ) 
-               ht_i(ji,jj,jl) = v_i(ji,jj,jl) / MAX( a_i(ji,jj,jl), epsi10 ) * zinda
-               !v_i(ji,jj,jl) = ht_i(ji,jj,jl) * a_i(ji,jj,jl) ! makes ice shrinken but should not be used
+                  zsal            = smv_i(ji,jj,jl)
+                  smv_i(ji,jj,jl) = sm_i(ji,jj,jl) * v_i(ji,jj,jl)
+                  ! salinity stays in bounds
+                  i_ice_switch    = 1._wp - MAX( 0._wp, SIGN( 1._wp, - v_i(ji,jj,jl) ) )
+                  smv_i(ji,jj,jl) = i_ice_switch * MAX( MIN( s_i_max * v_i(ji,jj,jl), smv_i(ji,jj,jl) ), s_i_min * v_i(ji,jj,jl) )
+                  ! associated salt flux
+                  sfx_res(ji,jj) = sfx_res(ji,jj) - ( smv_i(ji,jj,jl) - zsal ) * rhoic * r1_rdtice
+               END DO
             END DO
          END DO
-      END DO
-      at_i(:,:) = a_i(:,:,1)
-      DO jl = 2, jpl
-         at_i(:,:) = a_i(:,:,jl) + at_i(:,:)
-      END DO
-
-
-      ! Final thickness distribution rebinning
-      ! --------------------------------------
-      DO jm = 1, jpm
-         jbnd1 = ice_cat_bounds(jm,1)
-         jbnd2 = ice_cat_bounds(jm,2)
-         IF (ice_ncat_types(jm) .GT. 1 ) CALL lim_itd_th_reb(jbnd1, jbnd2, jm)
-         IF (ice_ncat_types(jm) .EQ. 1 ) THEN
-         ENDIF
-      END DO
-
-
-      !---------------------
-      ! 2.11) Ice salinity
-      !---------------------
-      ! clem correct bug on smv_i
-      smv_i(:,:,:) = sm_i(:,:,:) * v_i(:,:,:)
-
-      IF (  num_sal == 2  ) THEN ! general case
-         DO jl = 1, jpl
-            !DO jk = 1, nlay_i
-               DO jj = 1, jpj 
-                  DO ji = 1, jpi
-                     ! salinity stays in bounds
-                     !clem smv_i(ji,jj,jl)  =  MAX(MIN((rhoic-rhosn)/rhoic*sss_m(ji,jj),smv_i(ji,jj,jl)),0.1 * v_i(ji,jj,jl) )
-                     smv_i(ji,jj,jl) = MAX( MIN( s_i_max * v_i(ji,jj,jl), smv_i(ji,jj,jl) ), s_i_min * v_i(ji,jj,jl) )
-                     i_ice_switch    = 1._wp - MAX( 0._wp, SIGN( 1._wp, -v_i(ji,jj,jl) ) )
-                     smv_i(ji,jj,jl) = i_ice_switch * smv_i(ji,jj,jl) !+ s_i_min * ( 1._wp - i_ice_switch ) * v_i(ji,jj,jl)
-                  END DO ! ji
-               END DO ! jj
-            !END DO !jk
-         END DO !jl
       ENDIF
 
-      at_i(:,:) = a_i(:,:,1)
-      DO jl = 2, jpl
-         at_i(:,:) = a_i(:,:,jl) + at_i(:,:)
-      END DO
-
-
-      !--------------------------------
-      ! Update mass/salt fluxes (clem)
-      !--------------------------------
-      DO jl = 1, jpl
-         DO jj = 1, jpj 
-            DO ji = 1, jpi
-               diag_res_pr(ji,jj) = diag_res_pr(ji,jj) + ( v_i(ji,jj,jl) - zviold(ji,jj,jl) ) / rdt_ice 
-               rdm_ice(ji,jj) = rdm_ice(ji,jj) + ( v_i(ji,jj,jl) - zviold(ji,jj,jl) ) * rhoic 
-               rdm_snw(ji,jj) = rdm_snw(ji,jj) + ( v_s(ji,jj,jl) - zvsold(ji,jj,jl) ) * rhosn 
-               sfx_res(ji,jj) = sfx_res(ji,jj) - ( smv_i(ji,jj,jl) - zsmvold(ji,jj,jl) ) * rhoic / rdt_ice 
-            END DO
-         END DO
-      END DO
-  
-      ! -------------------------------
-      !- check conservation (C Rousset)
-      IF (ln_limdiahsb) THEN
-
-         zchk_fs  = glob_sum( ( sfx_bri(:,:) + sfx_thd(:,:) + sfx_res(:,:) + sfx_mec(:,:) ) * area(:,:) * tms(:,:) ) - zchk_fs_b
-         zchk_fw  = glob_sum( rdm_ice(:,:) * area(:,:) * tms(:,:) ) - zchk_fw_b
- 
-         zchk_v_i = ( glob_sum( SUM(   v_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) ) - zchk_v_i_b - ( zchk_fw / rhoic ) ) * r1_rdtice
-         zchk_smv = ( glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) ) - zchk_smv_b ) * r1_rdtice + ( zchk_fs / rhoic )
-
-         zchk_vmin = glob_min(v_i)
-         zchk_amax = glob_max(SUM(a_i,dim=3))
-         zchk_amin = glob_min(a_i)
-       
-         IF(lwp) THEN
-            IF ( ABS( zchk_v_i   ) >  1.e-5 ) WRITE(numout,*) 'violation volume [m3/day]     (limupdate1) = ',(zchk_v_i * rday)
-            IF ( ABS( zchk_smv   ) >  1.e-4 ) WRITE(numout,*) 'violation saline [psu*m3/day] (limupdate1) = ',(zchk_smv * rday)
-            IF ( zchk_vmin <  0.            ) WRITE(numout,*) 'violation v_i<0  [mm]         (limupdate1) = ',(zchk_vmin * 1.e-3)
-            IF ( zchk_amax >  amax+epsi10   ) WRITE(numout,*) 'violation a_i>amax            (limupdate1) = ',zchk_amax
-            IF ( zchk_amin <  0.            ) WRITE(numout,*) 'violation a_i<0               (limupdate1) = ',zchk_amin
-         ENDIF
-      ENDIF
-      !- check conservation (C Rousset)
-      ! -------------------------------
+      ! -------------------------------------------------
+      ! Diagnostics
+      ! -------------------------------------------------
+      d_u_ice_dyn(:,:)     = u_ice(:,:)     - u_ice_b(:,:)
+      d_v_ice_dyn(:,:)     = v_ice(:,:)     - v_ice_b(:,:)
+      d_a_i_trp  (:,:,:)   = a_i  (:,:,:)   - a_i_b  (:,:,:)
+      d_v_s_trp  (:,:,:)   = v_s  (:,:,:)   - v_s_b  (:,:,:)  
+      d_v_i_trp  (:,:,:)   = v_i  (:,:,:)   - v_i_b  (:,:,:)   
+      d_e_s_trp  (:,:,:,:) = e_s  (:,:,:,:) - e_s_b  (:,:,:,:)  
+      d_e_i_trp  (:,:,1:nlay_i,:) = e_i  (:,:,1:nlay_i,:) - e_i_b(:,:,1:nlay_i,:)
+      d_oa_i_trp (:,:,:)   = oa_i (:,:,:)   - oa_i_b (:,:,:)
+      d_smv_i_trp(:,:,:)   = 0._wp
+      IF(  num_sal == 2  ) d_smv_i_trp(:,:,:) = smv_i(:,:,:) - smv_i_b(:,:,:)
+
+      ! conservation test
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(1, 'limupdate1', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
 
       IF(ln_ctl) THEN   ! Control print
@@ -400,5 +173,5 @@
          CALL prt_ctl(tab2d_1=u_ice      , clinfo1=' lim_update1  : u_ice       :', tab2d_2=v_ice      , clinfo2=' v_ice       :')
          CALL prt_ctl(tab2d_1=d_u_ice_dyn, clinfo1=' lim_update1  : d_u_ice_dyn :', tab2d_2=d_v_ice_dyn, clinfo2=' d_v_ice_dyn :')
-         CALL prt_ctl(tab2d_1=old_u_ice  , clinfo1=' lim_update1  : old_u_ice   :', tab2d_2=old_v_ice  , clinfo2=' old_v_ice   :')
+         CALL prt_ctl(tab2d_1=u_ice_b    , clinfo1=' lim_update1  : u_ice_b     :', tab2d_2=v_ice_b    , clinfo2=' v_ice_b     :')
 
          DO jl = 1, jpl
@@ -413,26 +186,26 @@
             CALL prt_ctl(tab2d_1=o_i        (:,:,jl)        , clinfo1= ' lim_update1  : o_i         : ')
             CALL prt_ctl(tab2d_1=a_i        (:,:,jl)        , clinfo1= ' lim_update1  : a_i         : ')
-            CALL prt_ctl(tab2d_1=old_a_i    (:,:,jl)        , clinfo1= ' lim_update1  : old_a_i     : ')
+            CALL prt_ctl(tab2d_1=a_i_b      (:,:,jl)        , clinfo1= ' lim_update1  : a_i_b       : ')
             CALL prt_ctl(tab2d_1=d_a_i_trp  (:,:,jl)        , clinfo1= ' lim_update1  : d_a_i_trp   : ')
             CALL prt_ctl(tab2d_1=v_i        (:,:,jl)        , clinfo1= ' lim_update1  : v_i         : ')
-            CALL prt_ctl(tab2d_1=old_v_i    (:,:,jl)        , clinfo1= ' lim_update1  : old_v_i     : ')
+            CALL prt_ctl(tab2d_1=v_i_b      (:,:,jl)        , clinfo1= ' lim_update1  : v_i_b       : ')
             CALL prt_ctl(tab2d_1=d_v_i_trp  (:,:,jl)        , clinfo1= ' lim_update1  : d_v_i_trp   : ')
             CALL prt_ctl(tab2d_1=v_s        (:,:,jl)        , clinfo1= ' lim_update1  : v_s         : ')
-            CALL prt_ctl(tab2d_1=old_v_s    (:,:,jl)        , clinfo1= ' lim_update1  : old_v_s     : ')
+            CALL prt_ctl(tab2d_1=v_s_b      (:,:,jl)        , clinfo1= ' lim_update1  : v_s_b       : ')
             CALL prt_ctl(tab2d_1=d_v_s_trp  (:,:,jl)        , clinfo1= ' lim_update1  : d_v_s_trp   : ')
             CALL prt_ctl(tab2d_1=e_i        (:,:,1,jl)/1.0e9, clinfo1= ' lim_update1  : e_i1        : ')
-            CALL prt_ctl(tab2d_1=old_e_i    (:,:,1,jl)/1.0e9, clinfo1= ' lim_update1  : old_e_i1    : ')
+            CALL prt_ctl(tab2d_1=e_i_b      (:,:,1,jl)/1.0e9, clinfo1= ' lim_update1  : e_i1_b      : ')
             CALL prt_ctl(tab2d_1=d_e_i_trp  (:,:,1,jl)/1.0e9, clinfo1= ' lim_update1  : de_i1_trp   : ')
             CALL prt_ctl(tab2d_1=e_i        (:,:,2,jl)/1.0e9, clinfo1= ' lim_update1  : e_i2        : ')
-            CALL prt_ctl(tab2d_1=old_e_i    (:,:,2,jl)/1.0e9, clinfo1= ' lim_update1  : old_e_i2    : ')
+            CALL prt_ctl(tab2d_1=e_i_b      (:,:,2,jl)/1.0e9, clinfo1= ' lim_update1  : e_i2_b      : ')
             CALL prt_ctl(tab2d_1=d_e_i_trp  (:,:,2,jl)/1.0e9, clinfo1= ' lim_update1  : de_i2_trp   : ')
             CALL prt_ctl(tab2d_1=e_s        (:,:,1,jl)      , clinfo1= ' lim_update1  : e_snow      : ')
-            CALL prt_ctl(tab2d_1=old_e_s    (:,:,1,jl)      , clinfo1= ' lim_update1  : old_e_snow  : ')
+            CALL prt_ctl(tab2d_1=e_s_b      (:,:,1,jl)      , clinfo1= ' lim_update1  : e_snow_b    : ')
             CALL prt_ctl(tab2d_1=d_e_s_trp  (:,:,1,jl)/1.0e9, clinfo1= ' lim_update1  : d_e_s_trp   : ')
             CALL prt_ctl(tab2d_1=smv_i      (:,:,jl)        , clinfo1= ' lim_update1  : smv_i       : ')
-            CALL prt_ctl(tab2d_1=old_smv_i  (:,:,jl)        , clinfo1= ' lim_update1  : old_smv_i   : ')
+            CALL prt_ctl(tab2d_1=smv_i_b    (:,:,jl)        , clinfo1= ' lim_update1  : smv_i_b     : ')
             CALL prt_ctl(tab2d_1=d_smv_i_trp(:,:,jl)        , clinfo1= ' lim_update1  : d_smv_i_trp : ')
             CALL prt_ctl(tab2d_1=oa_i       (:,:,jl)        , clinfo1= ' lim_update1  : oa_i        : ')
-            CALL prt_ctl(tab2d_1=old_oa_i   (:,:,jl)        , clinfo1= ' lim_update1  : old_oa_i    : ')
+            CALL prt_ctl(tab2d_1=oa_i_b     (:,:,jl)        , clinfo1= ' lim_update1  : oa_i_b      : ')
             CALL prt_ctl(tab2d_1=d_oa_i_trp (:,:,jl)        , clinfo1= ' lim_update1  : d_oa_i_trp  : ')
 
@@ -446,7 +219,5 @@
          CALL prt_ctl_info(' - Heat / FW fluxes : ')
          CALL prt_ctl_info('   ~~~~~~~~~~~~~~~~~~ ')
-         CALL prt_ctl(tab2d_1=fmmec  , clinfo1= ' lim_update1 : fmmec : ', tab2d_2=fhmec     , clinfo2= ' fhmec     : ')
          CALL prt_ctl(tab2d_1=sst_m  , clinfo1= ' lim_update1 : sst   : ', tab2d_2=sss_m     , clinfo2= ' sss       : ')
-         CALL prt_ctl(tab2d_1=fhbri  , clinfo1= ' lim_update1 : fhbri : ', tab2d_2=fheat_mec , clinfo2= ' fheat_mec : ')
 
          CALL prt_ctl_info(' ')
@@ -458,8 +229,5 @@
       ENDIF
    
-
-      CALL wrk_dealloc( jkmax, zthick0, zqm0 )
-
-      CALL wrk_dealloc( jpi,jpj,jpl,zviold, zvsold, zsmvold )   ! clem
+      ENDIF ! ln_limdyn
 
       IF( nn_timing == 1 )  CALL timing_stop('limupdate1')
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limupdate2.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limupdate2.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limupdate2.F90	(revision 5038)
@@ -5,4 +5,5 @@
    !!======================================================================
    !! History :  3.0  !  2006-04  (M. Vancoppenolle) Original code
+   !!            3.6  !  2014-06  (C. Rousset)       Complete rewriting/cleaning
    !!----------------------------------------------------------------------
 #if defined key_lim3
@@ -39,4 +40,5 @@
    USE lib_fortran     ! glob_sum
    USE timing          ! Timing
+   USE limcons        ! conservation tests
 
    IMPLICIT NONE
@@ -45,8 +47,4 @@
    PUBLIC   lim_update2   ! routine called by ice_step
 
-      REAL(wp)  ::   epsi10 = 1.e-10_wp   !    -       -
-      REAL(wp)  ::   rzero  = 0._wp       !    -       -
-      REAL(wp)  ::   rone   = 1._wp       !    -       -
-         
    !! * Substitutions
 #  include "vectopt_loop_substitute.h90"
@@ -64,120 +62,46 @@
       !! ** Purpose :  Computes update of sea-ice global variables at 
       !!               the end of the time step.
-      !!               Address pathological cases
-      !!               This place is very important
-      !!                
-      !! ** Method  :  
-      !!    Ice speed from ice dynamics
-      !!    Ice thickness, Snow thickness, Temperatures, Lead fraction
-      !!      from advection and ice thermodynamics 
       !!
-      !! ** Action  : - 
       !!---------------------------------------------------------------------
-      INTEGER ::   ji, jj, jk, jl, jm    ! dummy loop indices
-      INTEGER ::   jbnd1, jbnd2
-      INTEGER ::   i_ice_switch
-      INTEGER ::   ind_im, layer      ! indices for internal melt
-      REAL(wp) ::   zweight, zesum, zhimax, z_da_i
-      REAL(wp) ::   zinda, zindb, zindsn, zindic
-      REAL(wp) ::   zindg, zh, zdvres, zviold2
-      REAL(wp) ::   zbigvalue, zvsold2, z_da_ex
-      REAL(wp) ::   z_prescr_hi, zat_i_old, ztmelts, ze_s
-
-      INTEGER , POINTER, DIMENSION(:,:,:) ::  internal_melt
-      REAL(wp), POINTER, DIMENSION(:) ::   zthick0, zqm0      ! thickness of the layers and heat contents for
-      REAL(wp) :: zchk_v_i, zchk_smv, zchk_fs, zchk_fw, zchk_v_i_b, zchk_smv_b, zchk_fs_b, zchk_fw_b ! Check conservation (C Rousset)
-      REAL(wp) :: zchk_vmin, zchk_amin, zchk_amax ! Check errors (C Rousset)
-      ! mass and salt flux (clem)
-      REAL(wp), POINTER, DIMENSION(:,:,:) ::   zviold, zvsold, zsmvold   ! old ice volume...
+      INTEGER  ::   ji, jj, jk, jl    ! dummy loop indices
+      INTEGER  ::   i_ice_switch
+      REAL(wp) ::   zh, zsal
+      !
+      REAL(wp) :: zvi_b, zsmv_b, zei_b, zfs_b, zfw_b, zft_b 
       !!-------------------------------------------------------------------
       IF( nn_timing == 1 )  CALL timing_start('limupdate2')
 
-      CALL wrk_alloc( jpi,jpj,jpl, internal_melt )   ! integer
-      CALL wrk_alloc( jkmax, zthick0, zqm0 )
-
-      CALL wrk_alloc( jpi,jpj,jpl,zviold, zvsold, zsmvold )   ! clem
-
-      !----------------------------------------------------------------------------------------
-      ! 1. Computation of trend terms      
-      !----------------------------------------------------------------------------------------
-      !- Trend terms
-      d_a_i_thd(:,:,:)   = a_i(:,:,:)   - old_a_i(:,:,:) 
-      d_v_s_thd(:,:,:)   = v_s(:,:,:)   - old_v_s(:,:,:)
-      d_v_i_thd(:,:,:)   = v_i(:,:,:)   - old_v_i(:,:,:)  
-      d_e_s_thd(:,:,:,:) = e_s(:,:,:,:) - old_e_s(:,:,:,:) 
-      d_e_i_thd(:,:,:,:) = e_i(:,:,:,:) - old_e_i(:,:,:,:)
-      !?? d_oa_i_thd(:,:,:)  = oa_i (:,:,:) - old_oa_i (:,:,:)
-      d_smv_i_thd(:,:,:) = 0._wp
-      IF( num_sal == 2 )   d_smv_i_thd(:,:,:) = smv_i(:,:,:) - old_smv_i(:,:,:)
-      ! diag only (clem)
-      dv_dt_thd(:,:,:) = d_v_i_thd(:,:,:) * r1_rdtice * rday
-
-      ! mass and salt flux init (clem)
-      zviold(:,:,:) = v_i(:,:,:)
-      zvsold(:,:,:) = v_s(:,:,:)
-      zsmvold(:,:,:) = smv_i(:,:,:)
-
-      ! -------------------------------
-      !- check conservation (C Rousset)
-      IF (ln_limdiahsb) THEN
-         zchk_v_i_b = glob_sum( SUM(   v_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) )
-         zchk_smv_b = glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) )
-         zchk_fw_b  = glob_sum( rdm_ice(:,:) * area(:,:) * tms(:,:) )
-         zchk_fs_b  = glob_sum( ( sfx_bri(:,:) + sfx_thd(:,:) + sfx_res(:,:) + sfx_mec(:,:) ) * area(:,:) * tms(:,:) )
-      ENDIF
-      !- check conservation (C Rousset)
-      ! -------------------------------
+      ! conservation test
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(0, 'limupdate2', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
+
+      !-----------------
+      ! zap small values
+      !-----------------
+      CALL lim_itd_me_zapsmall
 
       CALL lim_var_glo2eqv
 
-      !--------------------------------------
-      ! 2. Review of all pathological cases
-      !--------------------------------------
-
-! clem: useless now
-      !-------------------------------------------
-      ! 2.1) Advection of ice in an ice-free cell
-      !-------------------------------------------
-      ! should be removed since it is treated after dynamics now
-!      zhimax = 5._wp
-!      ! first category
-!      DO jj = 1, jpj
-!         DO ji = 1, jpi
-!            !--- the thickness of such an ice is often out of bounds
-!            !--- thus we recompute a new area while conserving ice volume
-!            zat_i_old = SUM( old_a_i(ji,jj,:) )
-!            zindb     = MAX( 0._wp, SIGN( 1._wp, ABS( d_a_i_thd(ji,jj,1) ) - epsi10 ) ) 
-!            IF ( ( ABS( d_v_i_thd(ji,jj,1) ) / MAX( ABS( d_a_i_thd(ji,jj,1) ),epsi10 ) * zindb .GT. zhimax ) &
-!               &  .AND. ( ( v_i(ji,jj,1) / MAX( a_i(ji,jj,1), epsi10 ) * zindb ) .GT. zhimax ) &
-!               &  .AND. ( zat_i_old .LT. 1.e-6 ) )  THEN ! new line
-!               ht_i(ji,jj,1) = hi_max(1) * 0.5_wp
-!               a_i (ji,jj,1) = v_i(ji,jj,1) / ht_i(ji,jj,1)
-!            ENDIF
-!         END DO
-!      END DO
-
-!      zhimax = 20._wp
-!      ! other categories
-!      DO jl = 2, jpl
-!         jm = ice_types(jl)
-!         DO jj = 1, jpj
-!            DO ji = 1, jpi
-!               zindb =  MAX( rzero, SIGN( rone, ABS( d_a_i_thd(ji,jj,jl)) - epsi10 ) ) 
-!              ! this correction is very tricky... sometimes, advection gets wrong i don't know why
-!               ! it makes problems when the advected volume and concentration do not seem to be 
-!               ! related with each other
-!               ! the new thickness is sometimes very big!
-!               ! and sometimes d_a_i_trp and d_v_i_trp have different sign
-!               ! which of course is plausible
-!               ! but fuck! it fucks everything up :)
-!               IF ( ( ABS( d_v_i_thd(ji,jj,jl) ) / MAX( ABS( d_a_i_thd(ji,jj,jl) ), epsi10 ) * zindb .GT. zhimax ) &
-!                  &  .AND. ( v_i(ji,jj,jl) / MAX( a_i(ji,jj,jl), epsi10 ) * zindb ) .GT. zhimax ) THEN
-!                  ht_i(ji,jj,jl) = ( hi_max_typ(jl-ice_cat_bounds(jm,1),jm) + hi_max_typ(jl-ice_cat_bounds(jm,1)+1,jm) ) * 0.5_wp
-!                  a_i (ji,jj,jl) = v_i(ji,jj,jl) / ht_i(ji,jj,jl)
-!               ENDIF
-!            END DO ! ji
-!         END DO !jj
-!      END DO !jl
-     
+      !----------------------------------------------------
+      ! Rebin categories with thickness out of bounds
+      !----------------------------------------------------
+      IF ( jpl > 1 )   CALL lim_itd_th_reb(1, jpl)
+
+      !----------------------------------------------------------------------
+      ! Constrain the thickness of the smallest category above hiclim
+      !----------------------------------------------------------------------
+      DO jj = 1, jpj 
+         DO ji = 1, jpi
+            IF( v_i(ji,jj,1) > 0._wp .AND. ht_i(ji,jj,1) < hiclim ) THEN
+               zh             = hiclim / ht_i(ji,jj,1)
+               ht_s(ji,jj,1) = ht_s(ji,jj,1) * zh
+               ht_i(ji,jj,1) = ht_i(ji,jj,1) * zh
+               a_i (ji,jj,1) = a_i(ji,jj,1)  / zh
+            ENDIF
+         END DO
+      END DO
+      
+      !-----------------------------------------------------
+      ! ice concentration should not exceed amax 
+      !-----------------------------------------------------
       at_i(:,:) = 0._wp
       DO jl = 1, jpl
@@ -185,232 +109,14 @@
       END DO
 
-      !----------------------------------------------------
-      ! 2.2) Rebin categories with thickness out of bounds
-      !----------------------------------------------------
-      DO jm = 1, jpm
-         jbnd1 = ice_cat_bounds(jm,1)
-         jbnd2 = ice_cat_bounds(jm,2)
-         IF (ice_ncat_types(jm) .GT. 1 )   CALL lim_itd_th_reb(jbnd1, jbnd2, jm)
-      END DO
-
-      !---------------------------------
-      ! 2.3) Melt of an internal layer
-      !---------------------------------
-      internal_melt(:,:,:) = 0
-
-      DO jl = 1, jpl
-         DO jk = 1, nlay_i
-            DO jj = 1, jpj 
-               DO ji = 1, jpi
-                  ztmelts = - tmut * s_i(ji,jj,jk,jl) + rtt
-                  IF ( ( ( e_i(ji,jj,jk,jl) .LE. 0.0 ) .OR. ( t_i(ji,jj,jk,jl) .GE. ztmelts ) ) &
-                    & .AND. ( v_i(ji,jj,jl) .GT. 0.0 ) .AND. ( a_i(ji,jj,jl) .GT. 0.0 ) ) THEN
-                     internal_melt(ji,jj,jl) = 1
-                  ENDIF
-               END DO ! ji
-            END DO ! jj
-         END DO !jk
-      END DO !jl
-
-      DO jl = 1, jpl
-         DO jj = 1, jpj 
+      DO jl  = 1, jpl
+         DO jj = 1, jpj
             DO ji = 1, jpi
-               IF( internal_melt(ji,jj,jl) == 1 ) THEN
-                  ! initial ice thickness
-                  !-----------------------
+               IF( at_i(ji,jj) > amax .AND. a_i(ji,jj,jl) > 0._wp ) THEN
+                  a_i(ji,jj,jl)  = a_i(ji,jj,jl) * ( 1._wp - ( 1._wp - amax / at_i(ji,jj) ) )
                   ht_i(ji,jj,jl) = v_i(ji,jj,jl) / a_i(ji,jj,jl)
-
-                  ! reduce ice thickness
-                  !-----------------------
-                  ind_im = 0
-                  zesum = 0.0
-                  DO jk = 1, nlay_i
-                     ztmelts = - tmut * s_i(ji,jj,jk,jl) + rtt
-                     IF ( ( e_i(ji,jj,jk,jl) .LE. 0.0 ) .OR. ( t_i(ji,jj,jk,jl) .GE. ztmelts ) ) ind_im = ind_im + 1
-                     zesum = zesum + e_i(ji,jj,jk,jl)
-                  END DO
-                  ht_i(ji,jj,jl) = ht_i(ji,jj,jl) - REAL(ind_im)*ht_i(ji,jj,jl) / REAL(nlay_i)
-                  v_i(ji,jj,jl)  = ht_i(ji,jj,jl) * a_i(ji,jj,jl)
-
-                  !CLEM
-                  zdvres = REAL(ind_im)*ht_i(ji,jj,jl) / REAL(nlay_i) * a_i(ji,jj,jl)
-                  !rdm_ice(ji,jj) = rdm_ice(ji,jj) - zdvres * rhoic
-                  !sfx_res(ji,jj)  = sfx_res(ji,jj) + sm_i(ji,jj,jl) * ( rhoic * zdvres / rdt_ice )
-
-                  ! redistribute heat
-                  !-----------------------
-                  ! old thicknesses and enthalpies
-                  ind_im = 0
-                  DO jk = 1, nlay_i
-                     ztmelts = - tmut * s_i(ji,jj,jk,jl) + rtt
-                     IF ( ( e_i(ji,jj,jk,jl) .GT. 0.0 ) .AND.  & 
-                        ( t_i(ji,jj,jk,jl) .LT. ztmelts ) ) THEN
-                        ind_im = ind_im + 1
-                        zthick0(ind_im) = ht_i(ji,jj,jl) * REAL(ind_im / nlay_i)
-                        zqm0   (ind_im) = MAX( e_i(ji,jj,jk,jl) , 0.0 )
-                     ENDIF
-                  END DO
-
-                  ! Redistributing energy on the new grid
-                  IF ( ind_im .GT. 0 ) THEN
-
-                     DO jk = 1, nlay_i
-                        e_i(ji,jj,jk,jl) = 0.0
-                        DO layer = 1, ind_im
-                           zweight         = MAX (  &
-                              MIN( ht_i(ji,jj,jl) * REAL(layer/ind_im) , ht_i(ji,jj,jl) * REAL(jk / nlay_i) ) -       &
-                              MAX( ht_i(ji,jj,jl) * REAL((layer-1)/ind_im) , ht_i(ji,jj,jl) * REAL((jk-1) / nlay_i) ) , 0.0 ) &
-                              /  ( ht_i(ji,jj,jl) / REAL(ind_im) )
-
-                           e_i(ji,jj,jk,jl) =  e_i(ji,jj,jk,jl) + zweight*zqm0(layer)
-                        END DO !layer
-                     END DO ! jk
-
-                     zesum = 0.0
-                     DO jk = 1, nlay_i
-                        zesum = zesum + e_i(ji,jj,jk,jl)
-                     END DO
-
-                  ELSE ! ind_im .EQ. 0, total melt
-                     e_i(ji,jj,jk,jl) = 0.0
-                  ENDIF
-
-               ENDIF ! internal_melt
-
-            END DO ! ji
-         END DO !jj
-      END DO !jl
-
-      internal_melt(:,:,:) = 0
-
-
-      ! Melt of snow
-      !--------------
-      DO jl = 1, jpl
-         DO jj = 1, jpj 
-            DO ji = 1, jpi
-               ! snow energy of melting
-               zinda   =  MAX( 0._wp, SIGN( 1._wp, v_s(ji,jj,jl) - epsi10 ) )
-               ze_s = zinda * e_s(ji,jj,1,jl) * unit_fac / area(ji,jj) / MAX( v_s(ji,jj,jl), epsi10 )  ! snow energy of melting
-
-               ! If snow energy of melting smaller then Lf
-               ! Then all snow melts and meltwater, heat go to the ocean
-               IF ( ze_s .LE. rhosn * lfus ) internal_melt(ji,jj,jl) = 1
-
+               ENDIF
             END DO
          END DO
       END DO
-
-      DO jl = 1, jpl
-         DO jj = 1, jpj 
-            DO ji = 1, jpi
-               IF ( internal_melt(ji,jj,jl) == 1 ) THEN
-                  zdvres = v_s(ji,jj,jl)
-                  ! release heat
-                  fheat_res(ji,jj) = fheat_res(ji,jj) + ze_s * zdvres / rdt_ice
-                  ! release mass
-                  !rdm_snw(ji,jj) =  rdm_snw(ji,jj) - zdvres * rhosn
-                  !
-                  v_s(ji,jj,jl)   = 0.0
-                  e_s(ji,jj,1,jl) = 0.0
-                 ENDIF
-            END DO
-         END DO
-      END DO
-
-      zbigvalue      = 1.0e+20
-      DO jl = 1, jpl
-         DO jj = 1, jpj 
-            DO ji = 1, jpi
-
-               !switches
-               zindb         = MAX( rzero, SIGN( rone, a_i(ji,jj,jl) - epsi10 ) ) 
-               !switch = 1 if a_i > 1e-06 and 0 if not
-               zindsn        = MAX( rzero, SIGN( rone, v_s(ji,jj,jl) - epsi10 ) ) !=1 if hs > 1e-10 and 0 if not
-               zindic        = MAX( rzero, SIGN( rone, v_i(ji,jj,jl) - epsi10 ) ) !=1 if hi > 1e-10 and 0 if not
-               ! bug fix 25 avril 2007
-               zindb         = zindb*zindic
-
-               !--- 2.3 Correction to ice age 
-               !------------------------------
-               !                IF ((o_i(ji,jj,jl)-1.0)*rday.gt.(rdt_ice*float(numit))) THEN
-               !                   o_i(ji,jj,jl) = rdt_ice*FLOAT(numit)/rday
-               !                ENDIF
-               IF ((oa_i(ji,jj,jl)-1.0)*rday.gt.(rdt_ice*numit*a_i(ji,jj,jl))) THEN
-                  oa_i(ji,jj,jl) = rdt_ice*numit/rday*a_i(ji,jj,jl)
-               ENDIF
-               oa_i(ji,jj,jl) = zindb*zindic*oa_i(ji,jj,jl)
-
-               !--- 2.4 Correction to snow thickness
-               !-------------------------------------
-               zdvres = (zindsn * zindb - 1._wp) * v_s(ji,jj,jl)
-               v_s(ji,jj,jl) = v_s(ji,jj,jl) + zdvres
-
-               !rdm_snw(ji,jj) = rdm_snw(ji,jj) + zdvres * rhosn
- 
-               !--- 2.5 Correction to ice thickness
-               !-------------------------------------
-               zdvres = (zindb - 1._wp) * v_i(ji,jj,jl)
-               v_i(ji,jj,jl) = v_i(ji,jj,jl) + zdvres
-
-               !rdm_ice(ji,jj) = rdm_ice(ji,jj) + zdvres * rhoic
-               !sfx_res(ji,jj)  = sfx_res(ji,jj) - sm_i(ji,jj,jl) * ( rhoic * zdvres / rdt_ice )
-
-               !--- 2.6 Snow is transformed into ice if the original ice cover disappears
-               !----------------------------------------------------------------------------
-               zindg          = tms(ji,jj) *  MAX( 0._wp, SIGN( 1._wp, -v_i(ji,jj,jl) ) )
-               zdvres         = zindg * rhosn * v_s(ji,jj,jl) / rau0
-               v_i(ji,jj,jl)  = v_i(ji,jj,jl)  + zdvres
-
-               zdvres         = zindsn*zindb * ( - zindg * v_s(ji,jj,jl) + zindg * v_i(ji,jj,jl) * ( rau0 - rhoic ) / rhosn )
-               v_s(ji,jj,jl)  = v_s(ji,jj,jl)  + zdvres
-
-               !--- 2.7 Correction to ice concentrations 
-               !--------------------------------------------
-               a_i(ji,jj,jl) = zindb * a_i(ji,jj,jl)
-
-               !-------------------------
-               ! 2.8) Snow heat content
-               !-------------------------
-               e_s(ji,jj,1,jl) = zindsn * ( MIN ( MAX ( 0.0, e_s(ji,jj,1,jl) ), zbigvalue ) )
-
-            END DO ! ji
-         END DO ! jj
-      END DO ! jl
-
-      !------------------------
-      ! 2.9) Ice heat content 
-      !------------------------
-
-      DO jl = 1, jpl
-         DO jk = 1, nlay_i
-            DO jj = 1, jpj 
-               DO ji = 1, jpi
-                  zindic        = MAX( rzero, SIGN( rone, v_i(ji,jj,jl) - epsi10 ) ) 
-                  e_i(ji,jj,jk,jl)= zindic * ( MIN ( MAX ( 0.0, e_i(ji,jj,jk,jl) ), zbigvalue ) )
-               END DO ! ji
-            END DO ! jj
-         END DO !jk
-      END DO !jl
-
-
-      DO jm = 1, jpm
-         DO jj = 1, jpj 
-            DO ji = 1, jpi
-               jl = ice_cat_bounds(jm,1)
-               !--- 2.12 Constrain the thickness of the smallest category above 5 cm
-               !----------------------------------------------------------------------
-               zindb         = MAX( rzero, SIGN( rone, a_i(ji,jj,jl) - epsi10 ) ) 
-               ht_i(ji,jj,jl) = zindb*v_i(ji,jj,jl)/MAX(a_i(ji,jj,jl), epsi10)
-               zh            = MAX( rone , zindb * hiclim  / MAX( ht_i(ji,jj,jl) , epsi10 ) )
-               ht_s(ji,jj,jl) = ht_s(ji,jj,jl)* zh
-               ht_i(ji,jj,jl) = ht_i(ji,jj,jl)* zh
-               a_i (ji,jj,jl) = a_i(ji,jj,jl) / zh
-               !CLEM
-               v_i (ji,jj,jl) = a_i(ji,jj,jl) * ht_i(ji,jj,jl)
-               v_s (ji,jj,jl) = a_i(ji,jj,jl) * ht_s(ji,jj,jl)
-            END DO !ji
-         END DO !jj
-      END DO !jm
 
       at_i(:,:) = 0.0
@@ -418,64 +124,33 @@
          at_i(:,:) = a_i(:,:,jl) + at_i(:,:)
       END DO
-      
-      !--- 2.13 ice concentration should not exceed amax 
-      !         (it should not be the case) 
-      !-----------------------------------------------------
-      DO jj = 1, jpj
-         DO ji = 1, jpi
-            z_da_ex =  MAX( at_i(ji,jj) - amax , 0.0 )        
-            zindb   =  MAX( rzero, SIGN( rone, at_i(ji,jj) - epsi10 ) ) 
-            DO jl  = 1, jpl
-               z_da_i = a_i(ji,jj,jl) * z_da_ex / MAX( at_i(ji,jj), epsi10 ) * zindb
-               a_i(ji,jj,jl) = MAX( 0._wp, a_i(ji,jj,jl) - z_da_i )
-               !
-               zinda   =  MAX( rzero, SIGN( rone, a_i(ji,jj,jl) - epsi10 ) ) 
-               ht_i(ji,jj,jl) = v_i(ji,jj,jl) / MAX( a_i(ji,jj,jl), epsi10 ) * zinda
-               !v_i(ji,jj,jl) = ht_i(ji,jj,jl) * a_i(ji,jj,jl) ! makes ice shrinken but should not be used
-            END DO
-         END DO
-      END DO
-      at_i(:,:) = 0.0
-      DO jl = 1, jpl
-         at_i(:,:) = a_i(:,:,jl) + at_i(:,:)
-      END DO
-
+
+      ! --------------------------------------
       ! Final thickness distribution rebinning
       ! --------------------------------------
-      DO jm = 1, jpm
-         jbnd1 = ice_cat_bounds(jm,1)
-         jbnd2 = ice_cat_bounds(jm,2)
-         IF (ice_ncat_types(jm) .GT. 1 ) CALL lim_itd_th_reb(jbnd1, jbnd2, jm)
-         IF (ice_ncat_types(jm) .EQ. 1 ) THEN
-         ENDIF
-      END DO
+      IF ( jpl > 1 ) CALL lim_itd_th_reb( 1, jpl )
+
+      !-----------------
+      ! zap small values
+      !-----------------
+      CALL lim_itd_me_zapsmall
 
       !---------------------
       ! 2.11) Ice salinity
       !---------------------
-      ! clem correct bug on smv_i
-      smv_i(:,:,:) = sm_i(:,:,:) * v_i(:,:,:)
-
-      IF (  num_sal == 2  ) THEN ! general case
+      IF (  num_sal == 2  ) THEN 
          DO jl = 1, jpl
-            !DO jk = 1, nlay_i
-               DO jj = 1, jpj 
-                  DO ji = 1, jpi
-                     ! salinity stays in bounds
-                     !clem smv_i(ji,jj,jl)  =  MAX(MIN((rhoic-rhosn)/rhoic*sss_m(ji,jj),smv_i(ji,jj,jl)),0.1 * v_i(ji,jj,jl) )
-                     smv_i(ji,jj,jl) = MAX( MIN( s_i_max * v_i(ji,jj,jl), smv_i(ji,jj,jl) ), s_i_min * v_i(ji,jj,jl) )
-                     i_ice_switch    = 1._wp - MAX( 0._wp, SIGN( 1._wp, -v_i(ji,jj,jl) ) )
-                     smv_i(ji,jj,jl) = i_ice_switch * smv_i(ji,jj,jl) !+ s_i_min * ( 1._wp - i_ice_switch ) * v_i(ji,jj,jl)
-                  END DO ! ji
-               END DO ! jj
-            !END DO !jk
+            DO jj = 1, jpj 
+               DO ji = 1, jpi
+                  zsal            = smv_i(ji,jj,jl)
+                  smv_i(ji,jj,jl) = sm_i(ji,jj,jl) * v_i(ji,jj,jl)
+                  ! salinity stays in bounds
+                  i_ice_switch    = 1._wp - MAX( 0._wp, SIGN( 1._wp, - v_i(ji,jj,jl) ) )
+                  smv_i(ji,jj,jl) = i_ice_switch * MAX( MIN( s_i_max * v_i(ji,jj,jl), smv_i(ji,jj,jl) ), s_i_min * v_i(ji,jj,jl) ) !+ s_i_min * ( 1._wp - i_ice_switch ) * v_i(ji,jj,jl)
+                  ! associated salt flux
+                  sfx_res(ji,jj) = sfx_res(ji,jj) - ( smv_i(ji,jj,jl) - zsal ) * rhoic * r1_rdtice
+               END DO ! ji
+            END DO ! jj
          END DO !jl
       ENDIF
-
-      ! -------------------
-      at_i(:,:) = a_i(:,:,1)
-      DO jl = 2, jpl
-         at_i(:,:) = a_i(:,:,jl) + at_i(:,:)
-      END DO
 
       !------------------------------------------------------------------------------
@@ -486,9 +161,9 @@
       DO jj = 2, jpjm1
          DO ji = 2, jpim1
-            IF ( at_i(ji,jj) .EQ. 0.0 ) THEN ! what to do if there is no ice
-               IF ( at_i(ji+1,jj) .EQ. 0.0 ) u_ice(ji,jj)   = 0.0 ! right side
-               IF ( at_i(ji-1,jj) .EQ. 0.0 ) u_ice(ji-1,jj) = 0.0 ! left side
-               IF ( at_i(ji,jj+1) .EQ. 0.0 ) v_ice(ji,jj)   = 0.0 ! upper side
-               IF ( at_i(ji,jj-1) .EQ. 0.0 ) v_ice(ji,jj-1) = 0.0 ! bottom side
+            IF ( at_i(ji,jj) == 0._wp ) THEN ! what to do if there is no ice
+               IF ( at_i(ji+1,jj) == 0._wp ) u_ice(ji,jj)   = 0._wp ! right side
+               IF ( at_i(ji-1,jj) == 0._wp ) u_ice(ji-1,jj) = 0._wp ! left side
+               IF ( at_i(ji,jj+1) == 0._wp ) v_ice(ji,jj)   = 0._wp ! upper side
+               IF ( at_i(ji,jj-1) == 0._wp ) v_ice(ji,jj-1) = 0._wp ! bottom side
             ENDIF
          END DO
@@ -501,42 +176,29 @@
       v_ice(:,:) = v_ice(:,:) * tmv(:,:)
  
-      !--------------------------------
-      ! Update mass/salt fluxes (clem)
-      !--------------------------------
-      DO jl = 1, jpl
-         DO jj = 1, jpj 
-            DO ji = 1, jpi
-               diag_res_pr(ji,jj) = diag_res_pr(ji,jj) + ( v_i(ji,jj,jl) - zviold(ji,jj,jl) ) / rdt_ice 
-               rdm_ice(ji,jj) = rdm_ice(ji,jj) + ( v_i(ji,jj,jl) - zviold(ji,jj,jl) ) * rhoic 
-               rdm_snw(ji,jj) = rdm_snw(ji,jj) + ( v_s(ji,jj,jl) - zvsold(ji,jj,jl) ) * rhosn 
-               sfx_res(ji,jj) = sfx_res(ji,jj) - ( smv_i(ji,jj,jl) - zsmvold(ji,jj,jl) ) * rhoic / rdt_ice 
-            END DO
-         END DO
-      END DO
-
-      ! -------------------------------
-      !- check conservation (C Rousset)
-      IF (ln_limdiahsb) THEN
-
-         zchk_fs  = glob_sum( ( sfx_bri(:,:) + sfx_thd(:,:) + sfx_res(:,:) + sfx_mec(:,:) ) * area(:,:) * tms(:,:) ) - zchk_fs_b
-         zchk_fw  = glob_sum( rdm_ice(:,:) * area(:,:) * tms(:,:) ) - zchk_fw_b
- 
-         zchk_v_i = ( glob_sum( SUM(   v_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) ) - zchk_v_i_b - ( zchk_fw / rhoic ) ) * r1_rdtice
-         zchk_smv = ( glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) ) - zchk_smv_b ) * r1_rdtice + ( zchk_fs / rhoic )
-
-         zchk_vmin = glob_min(v_i)
-         zchk_amax = glob_max(SUM(a_i,dim=3))
-         zchk_amin = glob_min(a_i)
-
-         IF(lwp) THEN
-            IF ( ABS( zchk_v_i   ) >  1.e-5 ) WRITE(numout,*) 'violation volume [m3/day]     (limupdate2) = ',(zchk_v_i * rday)
-            IF ( ABS( zchk_smv   ) >  1.e-4 ) WRITE(numout,*) 'violation saline [psu*m3/day] (limupdate2) = ',(zchk_smv * rday)
-            IF ( zchk_vmin <  0.            ) WRITE(numout,*) 'violation v_i<0  [mm]         (limupdate2) = ',(zchk_vmin * 1.e-3)
-            IF ( zchk_amax >  amax+epsi10   ) WRITE(numout,*) 'violation a_i>amax            (limupdate2) = ',zchk_amax
-            IF ( zchk_amin <  0.            ) WRITE(numout,*) 'violation a_i<0               (limupdate2) = ',zchk_amin
-         ENDIF
-      ENDIF
-      !- check conservation (C Rousset)
-      ! -------------------------------
+      ! -------------------------------------------------
+      ! Diagnostics
+      ! -------------------------------------------------
+      d_a_i_thd(:,:,:)   = a_i(:,:,:)   - a_i_b(:,:,:) 
+      d_v_s_thd(:,:,:)   = v_s(:,:,:)   - v_s_b(:,:,:)
+      d_v_i_thd(:,:,:)   = v_i(:,:,:)   - v_i_b(:,:,:)  
+      d_e_s_thd(:,:,:,:) = e_s(:,:,:,:) - e_s_b(:,:,:,:) 
+      d_e_i_thd(:,:,1:nlay_i,:) = e_i(:,:,1:nlay_i,:) - e_i_b(:,:,1:nlay_i,:)
+      !?? d_oa_i_thd(:,:,:)  = oa_i (:,:,:) - oa_i_b (:,:,:)
+      d_smv_i_thd(:,:,:) = 0._wp
+      IF( num_sal == 2 )   d_smv_i_thd(:,:,:) = smv_i(:,:,:) - smv_i_b(:,:,:)
+      ! diag only (clem)
+      dv_dt_thd(:,:,:) = d_v_i_thd(:,:,:) * r1_rdtice * rday
+
+      ! heat content variation (W.m-2)
+      DO jj = 1, jpj
+         DO ji = 1, jpi            
+            diag_heat_dhc(ji,jj) = ( SUM( d_e_i_trp(ji,jj,1:nlay_i,:) + d_e_i_thd(ji,jj,1:nlay_i,:) ) +  & 
+               &                     SUM( d_e_s_trp(ji,jj,1:nlay_s,:) + d_e_s_thd(ji,jj,1:nlay_s,:) )    &
+               &                   ) * unit_fac * r1_rdtice / area(ji,jj)   
+         END DO
+      END DO
+
+      ! conservation test
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(0, 'limupdate2', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
 
       IF(ln_ctl) THEN   ! Control print
@@ -550,5 +212,5 @@
          CALL prt_ctl(tab2d_1=strength   , clinfo1=' lim_update2  : strength    :')
          CALL prt_ctl(tab2d_1=u_ice      , clinfo1=' lim_update2  : u_ice       :', tab2d_2=v_ice      , clinfo2=' v_ice       :')
-         CALL prt_ctl(tab2d_1=old_u_ice  , clinfo1=' lim_update2  : old_u_ice   :', tab2d_2=old_v_ice  , clinfo2=' old_v_ice   :')
+         CALL prt_ctl(tab2d_1=u_ice_b    , clinfo1=' lim_update2  : u_ice_b     :', tab2d_2=v_ice_b    , clinfo2=' v_ice_b     :')
 
          DO jl = 1, jpl
@@ -563,26 +225,26 @@
             CALL prt_ctl(tab2d_1=o_i        (:,:,jl)        , clinfo1= ' lim_update2  : o_i         : ')
             CALL prt_ctl(tab2d_1=a_i        (:,:,jl)        , clinfo1= ' lim_update2  : a_i         : ')
-            CALL prt_ctl(tab2d_1=old_a_i    (:,:,jl)        , clinfo1= ' lim_update2  : old_a_i     : ')
+            CALL prt_ctl(tab2d_1=a_i_b      (:,:,jl)        , clinfo1= ' lim_update2  : a_i_b       : ')
             CALL prt_ctl(tab2d_1=d_a_i_thd  (:,:,jl)        , clinfo1= ' lim_update2  : d_a_i_thd   : ')
             CALL prt_ctl(tab2d_1=v_i        (:,:,jl)        , clinfo1= ' lim_update2  : v_i         : ')
-            CALL prt_ctl(tab2d_1=old_v_i    (:,:,jl)        , clinfo1= ' lim_update2  : old_v_i     : ')
+            CALL prt_ctl(tab2d_1=v_i_b      (:,:,jl)        , clinfo1= ' lim_update2  : v_i_b       : ')
             CALL prt_ctl(tab2d_1=d_v_i_thd  (:,:,jl)        , clinfo1= ' lim_update2  : d_v_i_thd   : ')
             CALL prt_ctl(tab2d_1=v_s        (:,:,jl)        , clinfo1= ' lim_update2  : v_s         : ')
-            CALL prt_ctl(tab2d_1=old_v_s    (:,:,jl)        , clinfo1= ' lim_update2  : old_v_s     : ')
+            CALL prt_ctl(tab2d_1=v_s_b      (:,:,jl)        , clinfo1= ' lim_update2  : v_s_b       : ')
             CALL prt_ctl(tab2d_1=d_v_s_thd  (:,:,jl)        , clinfo1= ' lim_update2  : d_v_s_thd   : ')
             CALL prt_ctl(tab2d_1=e_i        (:,:,1,jl)/1.0e9, clinfo1= ' lim_update2  : e_i1        : ')
-            CALL prt_ctl(tab2d_1=old_e_i    (:,:,1,jl)/1.0e9, clinfo1= ' lim_update2  : old_e_i1    : ')
+            CALL prt_ctl(tab2d_1=e_i_b      (:,:,1,jl)/1.0e9, clinfo1= ' lim_update2  : e_i1_b      : ')
             CALL prt_ctl(tab2d_1=d_e_i_thd  (:,:,1,jl)/1.0e9, clinfo1= ' lim_update2  : de_i1_thd   : ')
             CALL prt_ctl(tab2d_1=e_i        (:,:,2,jl)/1.0e9, clinfo1= ' lim_update2  : e_i2        : ')
-            CALL prt_ctl(tab2d_1=old_e_i    (:,:,2,jl)/1.0e9, clinfo1= ' lim_update2  : old_e_i2    : ')
+            CALL prt_ctl(tab2d_1=e_i_b      (:,:,2,jl)/1.0e9, clinfo1= ' lim_update2  : e_i2_b      : ')
             CALL prt_ctl(tab2d_1=d_e_i_thd  (:,:,2,jl)/1.0e9, clinfo1= ' lim_update2  : de_i2_thd   : ')
             CALL prt_ctl(tab2d_1=e_s        (:,:,1,jl)      , clinfo1= ' lim_update2  : e_snow      : ')
-            CALL prt_ctl(tab2d_1=old_e_s    (:,:,1,jl)      , clinfo1= ' lim_update2  : old_e_snow  : ')
+            CALL prt_ctl(tab2d_1=e_s_b      (:,:,1,jl)      , clinfo1= ' lim_update2  : e_snow_b    : ')
             CALL prt_ctl(tab2d_1=d_e_s_thd  (:,:,1,jl)/1.0e9, clinfo1= ' lim_update2  : d_e_s_thd   : ')
             CALL prt_ctl(tab2d_1=smv_i      (:,:,jl)        , clinfo1= ' lim_update2  : smv_i       : ')
-            CALL prt_ctl(tab2d_1=old_smv_i  (:,:,jl)        , clinfo1= ' lim_update2  : old_smv_i   : ')
+            CALL prt_ctl(tab2d_1=smv_i_b    (:,:,jl)        , clinfo1= ' lim_update2  : smv_i_b     : ')
             CALL prt_ctl(tab2d_1=d_smv_i_thd(:,:,jl)        , clinfo1= ' lim_update2  : d_smv_i_thd : ')
             CALL prt_ctl(tab2d_1=oa_i       (:,:,jl)        , clinfo1= ' lim_update2  : oa_i        : ')
-            CALL prt_ctl(tab2d_1=old_oa_i   (:,:,jl)        , clinfo1= ' lim_update2  : old_oa_i    : ')
+            CALL prt_ctl(tab2d_1=oa_i_b     (:,:,jl)        , clinfo1= ' lim_update2  : oa_i_b      : ')
             CALL prt_ctl(tab2d_1=d_oa_i_thd (:,:,jl)        , clinfo1= ' lim_update2  : d_oa_i_thd  : ')
 
@@ -596,7 +258,5 @@
          CALL prt_ctl_info(' - Heat / FW fluxes : ')
          CALL prt_ctl_info('   ~~~~~~~~~~~~~~~~~~ ')
-         CALL prt_ctl(tab2d_1=fmmec  , clinfo1= ' lim_update2 : fmmec : ', tab2d_2=fhmec     , clinfo2= ' fhmec     : ')
          CALL prt_ctl(tab2d_1=sst_m  , clinfo1= ' lim_update2 : sst   : ', tab2d_2=sss_m     , clinfo2= ' sss       : ')
-         CALL prt_ctl(tab2d_1=fhbri  , clinfo1= ' lim_update2 : fhbri : ', tab2d_2=fheat_mec , clinfo2= ' fheat_mec : ')
 
          CALL prt_ctl_info(' ')
@@ -608,10 +268,6 @@
       ENDIF
    
-      CALL wrk_dealloc( jpi,jpj,jpl, internal_melt )   ! integer
-      CALL wrk_dealloc( jkmax, zthick0, zqm0 )
-
-      CALL wrk_dealloc( jpi,jpj,jpl,zviold, zvsold, zsmvold )   ! clem
-
       IF( nn_timing == 1 )  CALL timing_stop('limupdate2')
+
    END SUBROUTINE lim_update2
 #else
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limvar.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limvar.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limvar.F90	(revision 5038)
@@ -66,8 +66,4 @@
    PUBLIC   lim_var_salprof1d    !
 
-   REAL(wp) ::   epsi10 = 1.e-10_wp   !    -       -
-   REAL(wp) ::   zzero = 0.e0        !    -       -
-   REAL(wp) ::   zone  = 1.e0        !    -       -
-
    !!----------------------------------------------------------------------
    !! NEMO/LIM3 4.0 , UCL - NEMO Consortium (2011)
@@ -94,5 +90,4 @@
       !
       INTEGER  ::   ji, jj, jk, jl   ! dummy loop indices
-      REAL(wp) ::   zinda, zindb
       !!------------------------------------------------------------------
 
@@ -113,6 +108,6 @@
                at_i(ji,jj) = at_i(ji,jj) + a_i(ji,jj,jl) ! ice concentration
                !
-               zinda = MAX( zzero , SIGN( zone , at_i(ji,jj) - epsi10 ) ) 
-               icethi(ji,jj) = vt_i(ji,jj) / MAX( at_i(ji,jj) , epsi10 ) * zinda  ! ice thickness
+               rswitch = MAX( 0._wp , SIGN( 1._wp , at_i(ji,jj) - epsi10 ) ) 
+               icethi(ji,jj) = vt_i(ji,jj) / MAX( at_i(ji,jj) , epsi10 ) * rswitch  ! ice thickness
             END DO
          END DO
@@ -134,9 +129,9 @@
             DO jj = 1, jpj
                DO ji = 1, jpi
-                  zinda = MAX( zzero , SIGN( zone , vt_i(ji,jj) - epsi10 ) ) 
-                  zindb = MAX( zzero , SIGN( zone , at_i(ji,jj) - epsi10 ) ) 
                   et_s(ji,jj)  = et_s(ji,jj)  + e_s(ji,jj,1,jl)                                       ! snow heat content
-                  smt_i(ji,jj) = smt_i(ji,jj) + smv_i(ji,jj,jl) / MAX( vt_i(ji,jj) , epsi10 ) * zinda   ! ice salinity
-                  ot_i(ji,jj)  = ot_i(ji,jj)  + oa_i(ji,jj,jl)  / MAX( at_i(ji,jj) , epsi10 ) * zindb   ! ice age
+                  rswitch = MAX( 0._wp , SIGN( 1._wp , vt_i(ji,jj) - epsi10 ) ) 
+                  smt_i(ji,jj) = smt_i(ji,jj) + smv_i(ji,jj,jl) / MAX( vt_i(ji,jj) , epsi10 ) * rswitch   ! ice salinity
+                  rswitch = MAX( 0._wp , SIGN( 1._wp , at_i(ji,jj) - epsi10 ) ) 
+                  ot_i(ji,jj)  = ot_i(ji,jj)  + oa_i(ji,jj,jl)  / MAX( at_i(ji,jj) , epsi10 ) * rswitch   ! ice age
                END DO
             END DO
@@ -163,5 +158,5 @@
       INTEGER  ::   ji, jj, jk, jl   ! dummy loop indices
       REAL(wp) ::   zq_i, zaaa, zbbb, zccc, zdiscrim     ! local scalars
-      REAL(wp) ::   ztmelts, zindb, zq_s, zfac1, zfac2   !   -      -
+      REAL(wp) ::   ztmelts, zq_s, zfac1, zfac2   !   -      -
       !!------------------------------------------------------------------
 
@@ -172,8 +167,8 @@
          DO jj = 1, jpj
             DO ji = 1, jpi
-               zindb = 1._wp - MAX( 0._wp , SIGN( 1._wp,- a_i(ji,jj,jl) + epsi10 ) )   !0 if no ice and 1 if yes
-               ht_i(ji,jj,jl) = v_i (ji,jj,jl) / MAX( a_i(ji,jj,jl) , epsi10 ) * zindb
-               ht_s(ji,jj,jl) = v_s (ji,jj,jl) / MAX( a_i(ji,jj,jl) , epsi10 ) * zindb
-               o_i(ji,jj,jl)  = oa_i(ji,jj,jl) / MAX( a_i(ji,jj,jl) , epsi10 ) * zindb
+               rswitch = 1._wp - MAX( 0._wp , SIGN( 1._wp,- a_i(ji,jj,jl) + epsi10 ) )   !0 if no ice and 1 if yes
+               ht_i(ji,jj,jl) = v_i (ji,jj,jl) / MAX( a_i(ji,jj,jl) , epsi10 ) * rswitch
+               ht_s(ji,jj,jl) = v_s (ji,jj,jl) / MAX( a_i(ji,jj,jl) , epsi10 ) * rswitch
+               o_i(ji,jj,jl)  = oa_i(ji,jj,jl) / MAX( a_i(ji,jj,jl) , epsi10 ) * rswitch
             END DO
          END DO
@@ -184,6 +179,6 @@
             DO jj = 1, jpj
                DO ji = 1, jpi
-                  zindb = 1._wp - MAX( 0._wp , SIGN( 1._wp,- a_i(ji,jj,jl) + epsi10 ) )   !0 if no ice and 1 if yes
-                  sm_i(ji,jj,jl) = smv_i(ji,jj,jl) / MAX( v_i(ji,jj,jl) , epsi10 ) * zindb
+                  rswitch = 1._wp - MAX( 0._wp , SIGN( 1._wp,- a_i(ji,jj,jl) + epsi10 ) )   !0 if no ice and 1 if yes
+                  sm_i(ji,jj,jl) = smv_i(ji,jj,jl) / MAX( v_i(ji,jj,jl) , epsi10 ) * rswitch
                END DO
             END DO
@@ -205,7 +200,7 @@
                DO ji = 1, jpi
                   !                                                              ! Energy of melting q(S,T) [J.m-3]
-                  zq_i    = e_i(ji,jj,jk,jl) / area(ji,jj) / MAX( v_i(ji,jj,jl) , epsi10 ) * REAL(nlay_i,wp) 
-                  zindb   = 1.0 - MAX( 0.0 , SIGN( 1.0 , - v_i(ji,jj,jl) + epsi10 ) )     ! zindb = 0 if no ice and 1 if yes
-                  zq_i    = zq_i * unit_fac * zindb                              !convert units
+                  rswitch   = 1.0 - MAX( 0.0 , SIGN( 1.0 , - v_i(ji,jj,jl) + epsi10 ) )     ! rswitch = 0 if no ice and 1 if yes
+                  zq_i    = rswitch * e_i(ji,jj,jk,jl) / area(ji,jj) / MAX( v_i(ji,jj,jl) , epsi10 ) * REAL(nlay_i,wp) 
+                  zq_i    = zq_i * unit_fac                             !convert units
                   ztmelts = -tmut * s_i(ji,jj,jk,jl) + rtt                       ! Ice layer melt temperature
                   !
@@ -214,5 +209,5 @@
                   zccc       =  lfus * (ztmelts-rtt)
                   zdiscrim   =  SQRT( MAX(zbbb*zbbb - 4._wp*zaaa*zccc , 0._wp) )
-                  t_i(ji,jj,jk,jl) = rtt + zindb *( - zbbb - zdiscrim ) / ( 2.0 *zaaa )
+                  t_i(ji,jj,jk,jl) = rtt + rswitch *( - zbbb - zdiscrim ) / ( 2.0 *zaaa )
                   t_i(ji,jj,jk,jl) = MIN( rtt, MAX( 173.15_wp, t_i(ji,jj,jk,jl) ) )       ! 100-rtt < t_i < rtt
                END DO
@@ -231,9 +226,9 @@
                DO ji = 1, jpi
                   !Energy of melting q(S,T) [J.m-3]
-                  zq_s  = e_s(ji,jj,jk,jl) / ( area(ji,jj) * MAX( v_s(ji,jj,jl) , epsi10 ) ) * REAL(nlay_s,wp)
-                  zindb = 1._wp - MAX( 0._wp , SIGN( 1._wp , - v_s(ji,jj,jl) + epsi10 ) )     ! zindb = 0 if no ice and 1 if yes
-                  zq_s  = zq_s * unit_fac * zindb                                    ! convert units
+                  rswitch = 1._wp - MAX( 0._wp , SIGN( 1._wp , - v_s(ji,jj,jl) + epsi10 ) )     ! rswitch = 0 if no ice and 1 if yes
+                  zq_s  = rswitch * e_s(ji,jj,jk,jl) / ( area(ji,jj) * MAX( v_s(ji,jj,jl) , epsi10 ) ) * REAL(nlay_s,wp)
+                  zq_s  = zq_s * unit_fac                                    ! convert units
                   !
-                  t_s(ji,jj,jk,jl) = rtt + zindb * ( - zfac1 * zq_s + zfac2 )
+                  t_s(ji,jj,jk,jl) = rtt + rswitch * ( - zfac1 * zq_s + zfac2 )
                   t_s(ji,jj,jk,jl) = MIN( rtt, MAX( 173.15, t_s(ji,jj,jk,jl) ) )     ! 100-rtt < t_i < rtt
                END DO
@@ -250,6 +245,6 @@
             DO jj = 1, jpj
                DO ji = 1, jpi
-                  zindb = (  1._wp - MAX( 0._wp , SIGN( 1._wp , - vt_i(ji,jj) + epsi10 ) )  )
-                  tm_i(ji,jj) = tm_i(ji,jj) + zindb * t_i(ji,jj,jk,jl) * v_i(ji,jj,jl)   &
+                  rswitch = (  1._wp - MAX( 0._wp , SIGN( 1._wp , - vt_i(ji,jj) + epsi10 ) )  )
+                  tm_i(ji,jj) = tm_i(ji,jj) + rswitch * t_i(ji,jj,jk,jl) * v_i(ji,jj,jl)   &
                      &                      / (  REAL(nlay_i,wp) * MAX( vt_i(ji,jj) , epsi10 )  )
                END DO
@@ -297,5 +292,5 @@
       INTEGER  ::   ji, jj, jk, jl   ! dummy loop index
       REAL(wp) ::   dummy_fac0, dummy_fac1, dummy_fac, zsal      ! local scalar
-      REAL(wp) ::   zind0, zind01, zindbal, zargtemp , zs_zero   !   -      -
+      REAL(wp) ::   zswi0, zswi01, zswibal, zargtemp , zs_zero   !   -      -
       REAL(wp), POINTER, DIMENSION(:,:,:) ::   z_slope_s, zalpha   ! 3D pointer
       !!------------------------------------------------------------------
@@ -320,5 +315,5 @@
             DO jj = 1, jpj
                DO ji = 1, jpi
-                  z_slope_s(ji,jj,jl) = 2._wp * sm_i(ji,jj,jl) / MAX( 0.01 , ht_i(ji,jj,jl) )
+                  z_slope_s(ji,jj,jl) = 2._wp * sm_i(ji,jj,jl) / MAX( epsi10 , ht_i(ji,jj,jl) )
                END DO
             END DO
@@ -332,13 +327,13 @@
             DO jj = 1, jpj
                DO ji = 1, jpi
-                  ! zind0 = 1 if sm_i le s_i_0 and 0 otherwise
-                  zind0  = MAX( 0._wp   , SIGN( 1._wp  , s_i_0 - sm_i(ji,jj,jl) ) ) 
-                  ! zind01 = 1 if sm_i is between s_i_0 and s_i_1 and 0 othws 
-                  zind01 = ( 1._wp - zind0 ) * MAX( 0._wp   , SIGN( 1._wp  , s_i_1 - sm_i(ji,jj,jl) ) ) 
-                  ! If 2.sm_i GE sss_m then zindbal = 1
+                  ! zswi0 = 1 if sm_i le s_i_0 and 0 otherwise
+                  zswi0  = MAX( 0._wp   , SIGN( 1._wp  , s_i_0 - sm_i(ji,jj,jl) ) ) 
+                  ! zswi01 = 1 if sm_i is between s_i_0 and s_i_1 and 0 othws 
+                  zswi01 = ( 1._wp - zswi0 ) * MAX( 0._wp   , SIGN( 1._wp  , s_i_1 - sm_i(ji,jj,jl) ) ) 
+                  ! If 2.sm_i GE sss_m then zswibal = 1
                   ! this is to force a constant salinity profile in the Baltic Sea
-                  zindbal = MAX( 0._wp , SIGN( 1._wp , 2._wp * sm_i(ji,jj,jl) - sss_m(ji,jj) ) )
-                  zalpha(ji,jj,jl) = zind0  + zind01 * ( sm_i(ji,jj,jl) * dummy_fac0 + dummy_fac1 )
-                  zalpha(ji,jj,jl) = zalpha(ji,jj,jl) * ( 1._wp - zindbal )
+                  zswibal = MAX( 0._wp , SIGN( 1._wp , 2._wp * sm_i(ji,jj,jl) - sss_m(ji,jj) ) )
+                  zalpha(ji,jj,jl) = zswi0  + zswi01 * ( sm_i(ji,jj,jl) * dummy_fac0 + dummy_fac1 )
+                  zalpha(ji,jj,jl) = zalpha(ji,jj,jl) * ( 1._wp - zswibal )
                END DO
             END DO
@@ -392,5 +387,4 @@
       !!------------------------------------------------------------------
       INTEGER  ::   ji, jj, jk, jl   ! dummy loop indices
-      REAL(wp) ::   zindb   !   -      -
       !!------------------------------------------------------------------
 
@@ -401,6 +395,6 @@
             DO jj = 1, jpj
                DO ji = 1, jpi
-                  zindb = (  1._wp - MAX( 0._wp , SIGN( 1._wp , - vt_i(ji,jj) + epsi10 ) )  )
-                  tm_i(ji,jj) = tm_i(ji,jj) + zindb * t_i(ji,jj,jk,jl) * v_i(ji,jj,jl)   &
+                  rswitch = (  1._wp - MAX( 0._wp , SIGN( 1._wp , - vt_i(ji,jj) + epsi10 ) )  )
+                  tm_i(ji,jj) = tm_i(ji,jj) + rswitch * t_i(ji,jj,jk,jl) * v_i(ji,jj,jl)   &
                      &                      / (  REAL(nlay_i,wp) * MAX( vt_i(ji,jj) , epsi10 )  )
                END DO
@@ -423,5 +417,5 @@
       !!------------------------------------------------------------------
       INTEGER  ::   ji, jj, jk, jl   ! dummy loop indices
-      REAL(wp) ::   zbvi, zinda, zindb      ! local scalars
+      REAL(wp) ::   zbvi             ! local scalars
       !!------------------------------------------------------------------
       !
@@ -431,9 +425,9 @@
             DO jj = 1, jpj
                DO ji = 1, jpi
-                  zinda = (  1._wp - MAX( 0._wp , SIGN( 1._wp , (t_i(ji,jj,jk,jl) - rtt) + epsi10 ) )  )
-                  zindb = (  1._wp - MAX( 0._wp , SIGN( 1._wp , - vt_i(ji,jj) + epsi10 ) )  )
-                  zbvi  = - zinda * tmut * s_i(ji,jj,jk,jl) / MIN( t_i(ji,jj,jk,jl) - rtt, - epsi10 )   &
+                  rswitch = (  1._wp - MAX( 0._wp , SIGN( 1._wp , (t_i(ji,jj,jk,jl) - rtt) + epsi10 ) )  )
+                  zbvi  = - rswitch * tmut * s_i(ji,jj,jk,jl) / MIN( t_i(ji,jj,jk,jl) - rtt, - epsi10 )   &
                      &                   * v_i(ji,jj,jl)    / REAL(nlay_i,wp)
-                  bv_i(ji,jj) = bv_i(ji,jj) + zindb * zbvi  / MAX( vt_i(ji,jj) , epsi10 )
+                  rswitch = (  1._wp - MAX( 0._wp , SIGN( 1._wp , - vt_i(ji,jj) + epsi10 ) )  )
+                  bv_i(ji,jj) = bv_i(ji,jj) + rswitch * zbvi  / MAX( vt_i(ji,jj) , epsi10 )
                END DO
             END DO
@@ -456,5 +450,5 @@
       INTEGER  ::   ii, ij  ! local integers
       REAL(wp) ::   dummy_fac0, dummy_fac1, dummy_fac2, zargtemp, zsal   ! local scalars
-      REAL(wp) ::   zalpha, zind0, zind01, zindbal, zs_zero              !   -      -
+      REAL(wp) ::   zalpha, zswi0, zswi01, zswibal, zs_zero              !   -      -
       !
       REAL(wp), POINTER, DIMENSION(:) ::   z_slope_s
@@ -466,5 +460,5 @@
       ! Vertically constant, constant in time
       !---------------------------------------
-      IF( num_sal == 1 )   s_i_b(:,:) = bulk_sal
+      IF( num_sal == 1 )   s_i_1d(:,:) = bulk_sal
 
       !------------------------------------------------------
@@ -475,5 +469,5 @@
          !
          DO ji = kideb, kiut          ! Slope of the linear profile zs_zero
-            z_slope_s(ji) = 2._wp * sm_i_b(ji) / MAX( 0.01 , ht_i_b(ji) )
+            z_slope_s(ji) = 2._wp * sm_i_1d(ji) / MAX( epsi10 , ht_i_1d(ji) )
          END DO
 
@@ -490,17 +484,17 @@
                ii =  MOD( npb(ji) - 1 , jpi ) + 1
                ij =     ( npb(ji) - 1 ) / jpi + 1
-               ! zind0 = 1 if sm_i le s_i_0 and 0 otherwise
-               zind0  = MAX( 0._wp , SIGN( 1._wp  , s_i_0 - sm_i_b(ji) ) ) 
-               ! zind01 = 1 if sm_i is between s_i_0 and s_i_1 and 0 othws 
-               zind01 = ( 1._wp - zind0 ) * MAX( 0._wp , SIGN( 1._wp , s_i_1 - sm_i_b(ji) ) ) 
-               ! if 2.sm_i GE sss_m then zindbal = 1
+               ! zswi0 = 1 if sm_i le s_i_0 and 0 otherwise
+               zswi0  = MAX( 0._wp , SIGN( 1._wp  , s_i_0 - sm_i_1d(ji) ) ) 
+               ! zswi01 = 1 if sm_i is between s_i_0 and s_i_1 and 0 othws 
+               zswi01 = ( 1._wp - zswi0 ) * MAX( 0._wp , SIGN( 1._wp , s_i_1 - sm_i_1d(ji) ) ) 
+               ! if 2.sm_i GE sss_m then zswibal = 1
                ! this is to force a constant salinity profile in the Baltic Sea
-               zindbal = MAX( 0._wp , SIGN( 1._wp , 2._wp * sm_i_b(ji) - sss_m(ii,ij) ) )
+               zswibal = MAX( 0._wp , SIGN( 1._wp , 2._wp * sm_i_1d(ji) - sss_m(ii,ij) ) )
                !
-               zalpha = (  zind0 + zind01 * ( sm_i_b(ji) * dummy_fac0 + dummy_fac1 )  ) * ( 1.0 - zindbal )
+               zalpha = (  zswi0 + zswi01 * ( sm_i_1d(ji) * dummy_fac0 + dummy_fac1 )  ) * ( 1.0 - zswibal )
                !
-               zs_zero = z_slope_s(ji) * ( REAL(jk,wp) - 0.5_wp ) * ht_i_b(ji) * dummy_fac2
+               zs_zero = z_slope_s(ji) * ( REAL(jk,wp) - 0.5_wp ) * ht_i_1d(ji) * dummy_fac2
                ! weighting the profile
-               s_i_b(ji,jk) = zalpha * zs_zero + ( 1._wp - zalpha ) * sm_i_b(ji)
+               s_i_1d(ji,jk) = zalpha * zs_zero + ( 1._wp - zalpha ) * sm_i_1d(ji)
             END DO ! ji
          END DO ! jk
@@ -514,5 +508,5 @@
       IF( num_sal == 3 ) THEN      ! Schwarzacher (1959) multiyear salinity profile (mean = 2.30)
          !
-         sm_i_b(:) = 2.30_wp
+         sm_i_1d(:) = 2.30_wp
          !
 !CDIR NOVERRCHK
@@ -521,5 +515,5 @@
             zsal =  1.6_wp * (  1._wp - COS( rpi * zargtemp**(0.407_wp/(0.573_wp+zargtemp)) )  )
             DO ji = kideb, kiut
-               s_i_b(ji,jk) = zsal
+               s_i_1d(ji,jk) = zsal
             END DO
          END DO
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limwri.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limwri.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limwri.F90	(revision 5038)
@@ -9,5 +9,4 @@
    !!----------------------------------------------------------------------
    !!   lim_wri      : write of the diagnostics variables in ouput file 
-   !!   lim_wri_init : initialization and namelist read
    !!   lim_wri_state : write for initial state or/and abandon
    !!----------------------------------------------------------------------
@@ -36,23 +35,4 @@
    PUBLIC lim_wri_state  ! called by dia_wri_state 
 
-   INTEGER, PARAMETER ::   jpnoumax = 43   !: maximum number of variable for ice output
-   
-   INTEGER  ::   noumef             ! number of fields
-   INTEGER  ::   noumefa            ! number of additional fields
-   INTEGER  ::   add_diag_swi       ! additional diagnostics
-   INTEGER  ::   nz                                         ! dimension for the itd field
-
-   REAL(wp) , DIMENSION(jpnoumax) ::   cmulti         ! multiplicative constant
-   REAL(wp) , DIMENSION(jpnoumax) ::   cadd           ! additive constant
-   REAL(wp) , DIMENSION(jpnoumax) ::   cmultia        ! multiplicative constant
-   REAL(wp) , DIMENSION(jpnoumax) ::   cadda          ! additive constant
-   CHARACTER(len = 35), DIMENSION(jpnoumax) ::   titn, titna   ! title of the field
-   CHARACTER(len = 8 ), DIMENSION(jpnoumax) ::   nam , nama    ! name of the field
-   CHARACTER(len = 8 ), DIMENSION(jpnoumax) ::   uni , unia    ! unit of the field
-   INTEGER            , DIMENSION(jpnoumax) ::   nc  , nca     ! switch for saving field ( = 1 ) or not ( = 0 )
-
-   REAL(wp)  ::   epsi06 = 1.e-6_wp
-   REAL(wp)  ::   zzero  = 0._wp
-   REAL(wp)  ::   zone   = 1._wp      
    !!----------------------------------------------------------------------
    !! NEMO/LIM3 4.0 , UCL - NEMO Consortium (2011)
@@ -78,300 +58,200 @@
       INTEGER, INTENT(in) ::   kindic   ! if kindic < 0 there has been an error somewhere
       !
-      INTEGER ::  ji, jj, jk, jl, jf, ipl ! dummy loop indices
-      INTEGER ::  ierr
-      REAL(wp),DIMENSION(1) ::   zdept
-      REAL(wp) ::  zsto, zjulian, zout, zindh, zinda, zindb, zindc
-      REAL(wp), POINTER, DIMENSION(:,:,:) ::   zcmo, zcmoa
-      REAL(wp), POINTER, DIMENSION(:,:  ) ::   zfield
-      REAL(wp), POINTER, DIMENSION(:,:,:) ::   zmaskitd, zoi, zei
-
-      CHARACTER(len = 60) ::   clhstnam, clop, clhstnama
-
-      INTEGER , SAVE ::   nice, nhorid, ndim, niter, ndepid
-      INTEGER , SAVE ::   nicea, nhorida, ndimitd
-      INTEGER , ALLOCATABLE, DIMENSION(:), SAVE ::   ndex51
-      INTEGER , ALLOCATABLE, DIMENSION(:), SAVE ::   ndexitd
+      INTEGER  ::  ji, jj, jk, jl  ! dummy loop indices
+      REAL(wp) ::  z1_365
+      REAL(wp) ::  ztmp
+      REAL(wp), POINTER, DIMENSION(:,:,:) ::  zoi, zei
+      REAL(wp), POINTER, DIMENSION(:,:)   ::  z2d, z2da, z2db, zswi    ! 2D workspace
       !!-------------------------------------------------------------------
 
       IF( nn_timing == 1 )  CALL timing_start('limwri')
 
-      CALL wrk_alloc( jpi, jpj, zfield )
-      CALL wrk_alloc( jpi, jpj, jpnoumax, zcmo, zcmoa )
-      CALL wrk_alloc( jpi, jpj, jpl, zmaskitd, zoi, zei )
-
-      ipl = jpl
-
-      IF( numit == nstart ) THEN 
-
-         ALLOCATE( ndex51(jpij), ndexitd(jpij*jpl), STAT=ierr )
-         IF( lk_mpp    )   CALL mpp_sum ( ierr )
-         IF( ierr /= 0 ) THEN
-            CALL ctl_stop( 'lim_wri : unable to allocate standard arrays' )   ;   RETURN
-         ENDIF
-
-         CALL lim_wri_init 
-
-         IF(lwp) WRITE(numout,*) ' lim_wri, first time step '
-         IF(lwp) WRITE(numout,*) ' add_diag_swi ', add_diag_swi
-
-         !--------------------
-         !  1) Initialization
-         !--------------------
-
-         !-------------
-         ! Normal file
-         !-------------
-         niter    = ( nit000 - 1 ) / nn_fsbc
-         CALL ymds2ju ( nyear, nmonth, nday, rdt, zjulian )
-         zjulian = zjulian - adatrj   !   set calendar origin to the beginning of the experiment
-!clem
-!         zsto     = rdt_ice
-!         IF( ln_mskland )   THEN   ;   clop = "ave(only(x))"   ! put 1.e+20 on land (very expensive!!)
-!         ELSE                      ;   clop = "ave(x)"         ! no use of the mask value (require less cpu time)
-!         ENDIF
-!         zout     = nwrite * rdt_ice / nn_fsbc
-!         zdept(1) = 0.
-!
-!         CALL dia_nam ( clhstnam, nwrite, 'icemod_old' )
-!         CALL histbeg ( clhstnam, jpi, glamt, jpj, gphit, 1, jpi, 1, jpj, niter, zjulian, rdt_ice,   &
-!            &           nhorid, nice, domain_id=nidom, snc4chunks=snc4set )
-!         CALL histvert( nice, "deptht", "Vertical T levels", "m", 1, zdept, ndepid, "down")
-!         CALL wheneq  ( jpij , tmask(:,:,1), 1, 1., ndex51, ndim)
-!
-!         DO jf = 1 , noumef
-!            IF(lwp) WRITE(numout,*) 'jf', jf
-!            IF ( nc(jf) == 1 ) THEN
-!               CALL histdef( nice, nam(jf), titn(jf), uni(jf), jpi, jpj &
-!                  , nhorid, 1, 1, 1, -99, 32, clop, zsto, zout )
-!               IF(lwp) WRITE(numout,*) 'nice, nam(jf), titn(jf), uni(jf), nhorid, clop, zsto, zout'
-!               IF(lwp) WRITE(numout,*)  nice, nam(jf), titn(jf), uni(jf), nhorid, clop, zsto, zout 
-!            ENDIF
-!         END DO
-!
-!         CALL histend(nice, snc4set)
-!clem
-         !
-         !-----------------
-         ! ITD file output
-         !-----------------
-         zsto     = rdt_ice
-         clop     = "ave(x)"
-         zout     = nwrite * rdt_ice / nn_fsbc
-         zdept(1) = 0.
-
-         CALL dia_nam ( clhstnama, nwrite, 'icemoa' )
-         CALL histbeg ( clhstnama, jpi, glamt, jpj, gphit,         &
-            1, jpi, 1, jpj,            & ! zoom
-            niter, zjulian, rdt_ice,   & ! time
-            nhorida,                   & ! ? linked with horizontal ...
-            nicea , domain_id=nidom, snc4chunks=snc4set)                  ! file 
-         CALL histvert( nicea, "icethi", "L levels","m", ipl , hi_mean , nz )
+      CALL wrk_alloc( jpi, jpj, jpl, zoi, zei )
+      CALL wrk_alloc( jpi, jpj     , z2d, z2da, z2db, zswi )
+
+      !-----------------------------
+      ! Mean category values
+      !-----------------------------
+
+      CALL lim_var_icetm      ! mean sea ice temperature
+
+      CALL lim_var_bv         ! brine volume
+
+      DO jj = 1, jpj          ! presence indicator of ice
+         DO ji = 1, jpi
+            zswi(ji,jj)  = MAX( 0._wp , SIGN( 1._wp , at_i(ji,jj) - epsi06 ) )
+         END DO
+      END DO
+      !
+      !
+      !                                             
+      IF ( iom_use( "icethic_cea" ) ) THEN                       ! mean ice thickness
+         DO jj = 1, jpj 
+            DO ji = 1, jpi
+               z2d(ji,jj)  = vt_i(ji,jj) / MAX( at_i(ji,jj), epsi06 ) * zswi(ji,jj)
+            END DO
+         END DO
+         CALL iom_put( "icethic_cea"  , z2d              )
+      ENDIF
+
+      IF ( iom_use( "snowthic_cea" ) ) THEN                      ! snow thickness = mean snow thickness over the cell 
+         DO jj = 1, jpj                                            
+            DO ji = 1, jpi
+               z2d(ji,jj)  = vt_s(ji,jj) / MAX( at_i(ji,jj), epsi06 ) * zswi(ji,jj)
+            END DO
+         END DO
+         CALL iom_put( "snowthic_cea" , z2d              )       
+      ENDIF
+      !
+      IF ( iom_use( "uice_ipa" ) .OR. iom_use( "vice_ipa" ) .OR. iom_use( "icevel" ) ) THEN 
+         DO jj = 2 , jpjm1
+            DO ji = 2 , jpim1
+               z2da(ji,jj)  = (  u_ice(ji,jj) * tmu(ji,jj) + u_ice(ji-1,jj) * tmu(ji-1,jj) ) * 0.5_wp
+               z2db(ji,jj)  = (  v_ice(ji,jj) * tmv(ji,jj) + v_ice(ji,jj-1) * tmv(ji,jj-1) ) * 0.5_wp
+           END DO
+         END DO
+         CALL lbc_lnk( z2da, 'T', -1. )
+         CALL lbc_lnk( z2db, 'T', -1. )
+         CALL iom_put( "uice_ipa"     , z2da                )       ! ice velocity u component
+         CALL iom_put( "vice_ipa"     , z2db                )       ! ice velocity v component
+         DO jj = 1, jpj                                 
+            DO ji = 1, jpi
+               z2d(ji,jj)  = SQRT( z2da(ji,jj) * z2da(ji,jj) + z2db(ji,jj) * z2db(ji,jj) ) 
+            END DO
+         END DO
+         CALL iom_put( "icevel"       , z2d                 )       ! ice velocity module
+      ENDIF
+      !
+      IF ( iom_use( "miceage" ) ) THEN 
+         z2d(:,:) = 0.e0
          DO jl = 1, jpl
-            zmaskitd(:,:,jl) = tmask(:,:,1)
-         END DO
-         CALL wheneq  ( jpij , tmask(:,:,1), 1, 1., ndex51, ndim)
-         CALL wheneq( jpi*jpj*jpl, zmaskitd, 1, 1., ndexitd, ndimitd  )  
-         CALL histdef( nicea, "iice_itd", "Ice area in categories"         , "-"    ,   &  
-            jpi, jpj, nhorida, jpl, 1, jpl, nz, 15, clop, zsto, zout )
-         CALL histdef( nicea, "iice_hid", "Ice thickness in categories"    , "m"    ,   &  
-            jpi, jpj, nhorida, jpl, 1, jpl, nz, 15, clop, zsto, zout )
-         CALL histdef( nicea, "iice_hsd", "Snow depth in in categories"    , "m"    ,   &  
-            jpi, jpj, nhorida, jpl, 1, jpl, nz, 15, clop, zsto, zout )
-         CALL histdef( nicea, "iice_std", "Ice salinity distribution"      , "ppt"  ,   &  
-            jpi, jpj, nhorida, jpl, 1, jpl, nz, 15, clop, zsto, zout )
-         CALL histdef( nicea, "iice_otd", "Ice age distribution"               , "days",   &  
-            jpi, jpj, nhorida, jpl, 1, jpl, nz, 15, clop, zsto, zout )
-         CALL histdef( nicea, "iice_etd", "Brine volume distr. "               , "%"    ,   &  
-            jpi, jpj, nhorida, jpl, 1, jpl, nz, 15, clop, zsto, zout )
-         CALL histend(nicea, snc4set)
-      ENDIF
-
-      !     !-----------------------------------------------------------------------!
-      !     !--2. Computation of instantaneous values                               ! 
-      !     !-----------------------------------------------------------------------!
-
-      !++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-      !IF( ln_nicep ) THEN
-      !   WRITE(numout,*)
-      !   WRITE(numout,*) 'lim_wri : write ice outputs in NetCDF files at time : ', nyear, nmonth, nday, numit
-      !   WRITE(numout,*) '~~~~~~~ '
-      !   WRITE(numout,*) ' kindic = ', kindic
-      !ENDIF
-      !++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
-      !-- calculs des valeurs instantanees
-      zcmo ( 1:jpi, 1:jpj, 1:jpnoumax ) = 0._wp
-      zcmoa( 1:jpi, 1:jpj, 1:jpnoumax ) = 0._wp
-
-      ! Ice surface temperature and some fluxes
-      DO jl = 1, jpl
+            DO jj = 1, jpj
+               DO ji = 1, jpi
+                  z2d(ji,jj) = z2d(ji,jj) + zswi(ji,jj) * oa_i(ji,jj,jl)
+               END DO
+            END DO
+         END DO
+         z1_365 = 1._wp / 365._wp
+         CALL iom_put( "miceage"     , z2d * z1_365         )        ! mean ice age
+      ENDIF
+
+      IF ( iom_use( "micet" ) ) THEN 
          DO jj = 1, jpj
             DO ji = 1, jpi
-               zinda  = MAX( zzero , SIGN( zone , at_i(ji,jj) - epsi06 ) )
-               zcmo(ji,jj,17) = zcmo(ji,jj,17) + a_i(ji,jj,jl)*qsr_ice (ji,jj,jl) 
-               zcmo(ji,jj,18) = zcmo(ji,jj,18) + a_i(ji,jj,jl)*qns_ice(ji,jj,jl) 
-               zcmo(ji,jj,27) = zcmo(ji,jj,27) + zinda*(t_su(ji,jj,jl)-rtt)*a_i(ji,jj,jl)/MAX(at_i(ji,jj),epsi06)
-               zcmo(ji,jj,21) = zcmo(ji,jj,21) + zinda*oa_i(ji,jj,jl)/MAX(at_i(ji,jj),epsi06) 
-            END DO
-         END DO
-      END DO
-
-      ! Mean sea ice temperature
-      CALL lim_var_icetm
-
-      ! Brine volume
-      CALL lim_var_bv
-
-      DO jj = 2 , jpjm1
-         DO ji = 2 , jpim1
-            zinda  = MAX( zzero , SIGN( zone , at_i(ji,jj) - epsi06 ) )
-            zindb  = MAX( zzero , SIGN( zone , at_i(ji,jj) ) )
-
-            zcmo(ji,jj,1)  = at_i(ji,jj)
-            zcmo(ji,jj,2)  = vt_i(ji,jj) / MAX( at_i(ji,jj), epsi06 ) * zinda
-            zcmo(ji,jj,3)  = vt_s(ji,jj) / MAX( at_i(ji,jj), epsi06 ) * zinda
-            zcmo(ji,jj,4)  = diag_bot_gr(ji,jj) * rday     ! Bottom thermodynamic ice production
-            zcmo(ji,jj,5)  = diag_dyn_gr(ji,jj) * rday     ! Dynamic ice production (rid/raft)
-            zcmo(ji,jj,22) = diag_lat_gr(ji,jj) * rday     ! Lateral thermodynamic ice production
-            zcmo(ji,jj,23) = diag_sni_gr(ji,jj) * rday     ! Snow ice production ice production
-            zcmo(ji,jj,24) = (tm_i(ji,jj) - rtt) * zinda
-
-            zcmo(ji,jj,6)  = fbif(ji,jj)*at_i(ji,jj)
-            zcmo(ji,jj,7)  = (  u_ice(ji,jj) * tmu(ji,jj) + u_ice(ji-1,jj) * tmu(ji-1,jj) ) * 0.5_wp
-            zcmo(ji,jj,8)  = (  v_ice(ji,jj) * tmv(ji,jj) + v_ice(ji,jj-1) * tmv(ji,jj-1) ) * 0.5_wp
-            zcmo(ji,jj,9)  = sst_m(ji,jj)
-            zcmo(ji,jj,10) = sss_m(ji,jj)
-
-            zcmo(ji,jj,11) = qns(ji,jj) + qsr(ji,jj)
-            zcmo(ji,jj,12) = qsr(ji,jj)
-            zcmo(ji,jj,13) = qns(ji,jj)
-            zcmo(ji,jj,14) = fhbri(ji,jj)
-            zcmo(ji,jj,15) = utau_ice(ji,jj)
-            zcmo(ji,jj,16) = vtau_ice(ji,jj)
-            zcmo(ji,jj,17) = zcmo(ji,jj,17) + ( 1._wp - at_i(ji,jj) ) * qsr(ji,jj)
-            zcmo(ji,jj,18) = zcmo(ji,jj,18) + ( 1._wp - at_i(ji,jj) ) * qns(ji,jj)
-            zcmo(ji,jj,19) = sprecip(ji,jj)
-            zcmo(ji,jj,20) = smt_i(ji,jj)
-            zcmo(ji,jj,25) = et_i(ji,jj)
-            zcmo(ji,jj,26) = et_s(ji,jj)
-            zcmo(ji,jj,28) = sfx_bri(ji,jj)
-            zcmo(ji,jj,29) = sfx_thd(ji,jj)
-
-            zcmo(ji,jj,30) = bv_i(ji,jj)
-            zcmo(ji,jj,31) = hicol(ji,jj) * zindb
-            zcmo(ji,jj,32) = strength(ji,jj)
-            zcmo(ji,jj,33) = SQRT(  zcmo(ji,jj,7)*zcmo(ji,jj,7) + zcmo(ji,jj,8)*zcmo(ji,jj,8)  )
-            zcmo(ji,jj,34) = diag_sur_me(ji,jj) * rday     ! Surface melt
-            zcmo(ji,jj,35) = diag_bot_me(ji,jj) * rday     ! Bottom melt
-            zcmo(ji,jj,36) = divu_i(ji,jj)
-            zcmo(ji,jj,37) = shear_i(ji,jj)
-            zcmo(ji,jj,38) = diag_res_pr(ji,jj) * rday     ! Bottom melt
-            zcmo(ji,jj,39) = vt_i(ji,jj)  ! ice volume
-            zcmo(ji,jj,40) = vt_s(ji,jj)  ! snow volume
-
-            zcmo(ji,jj,41) = sfx_mec(ji,jj)
-            zcmo(ji,jj,42) = sfx_res(ji,jj)
-
-            zcmo(ji,jj,43) = diag_trp_vi(ji,jj) * rday     ! transport of ice volume
-
-        END DO
-      END DO
-
-      !
-      ! ecriture d'un fichier netcdf
-      !
-      niter = niter + 1
-!clem
-!      DO jf = 1 , noumef
-!         !
-!         zfield(:,:) = zcmo(:,:,jf) * cmulti(jf) + cadd(jf)
-!         !
-!         IF( jf == 7  .OR. jf == 8  .OR. jf == 15 .OR. jf == 16 ) THEN   ;   CALL lbc_lnk( zfield, 'T', -1. )
-!         ELSE                                                            ;   CALL lbc_lnk( zfield, 'T',  1. )
-!         ENDIF
-!         !
-!         IF( ln_nicep ) THEN 
-!            WRITE(numout,*)
-!            WRITE(numout,*) 'nc(jf), nice, nam(jf), niter, ndim'
-!            WRITE(numout,*) nc(jf), nice, nam(jf), niter, ndim
-!         ENDIF
-!         IF( nc(jf) == 1 ) CALL histwrite( nice, nam(jf), niter, zfield, ndim, ndex51 )
-!         !
-!      END DO
-!
-!      IF( ( nn_fsbc * niter ) >= nitend .OR. kindic < 0 ) THEN
-!         IF( lwp) WRITE(numout,*) ' Closing the icemod file '
-!         CALL histclo( nice )
-!      ENDIF
-!clem
-      !
-       CALL iom_put ('iceconc', zcmo(:,:,1) )          ! field1: ice concentration
-       CALL iom_put ('icethic_cea', zcmo(:,:,2) )      ! field2: ice thickness (i.e. icethi(:,:))
-       CALL iom_put ('snowthic_cea', zcmo(:,:,3))      ! field3: snow thickness
-       CALL iom_put ('icebopr', zcmo(:,:,4) )   ! field4: daily bottom thermo ice production
-       CALL iom_put ('icedypr', zcmo(:,:,5) )   ! field5: daily dynamic ice production
-       CALL iom_put ('ioceflxb', zcmo(:,:,6) )         ! field6: Oceanic flux at the ice base
-       CALL iom_put ('uice_ipa', zcmo(:,:,7) )         ! field7: ice velocity u component
-       CALL iom_put ('vice_ipa', zcmo(:,:,8) )         ! field8: ice velocity v component
-       CALL iom_put ('isst', zcmo(:,:,9) )             ! field 9: sea surface temperature
-       CALL iom_put ('isss', zcmo(:,:,10) )            ! field 10: sea surface salinity
-       CALL iom_put ('qt_oce', zcmo(:,:,11) )           ! field 11: total flux at ocean surface
-       CALL iom_put ('qsr_oce', zcmo(:,:,12) )          ! field 12: solar flux at ocean surface
-       CALL iom_put ('qns_oce', zcmo(:,:,13) )          ! field 13: non-solar flux at ocean surface
-       !CALL iom_put ('hfbri', fhbri )                  ! field 14: heat flux due to brine release
-       CALL iom_put( 'utau_ice', zcmo(:,:,15)  )     ! Wind stress over ice along i-axis at I-point
-       CALL iom_put( 'vtau_ice', zcmo(:,:,16) )     ! Wind stress over ice along j-axis at I-point
-       CALL iom_put ('qsr_io', zcmo(:,:,17) )          ! field 17: solar flux at ice/ocean surface
-       CALL iom_put ('qns_io', zcmo(:,:,18) )          ! field 18: non-solar flux at ice/ocean surface
-       !CALL iom_put ('snowpre', zcmo(:,:,19) * rday ! field 19 :snow precip          
-       CALL iom_put ('micesalt', zcmo(:,:,20) )        ! field 20 :mean ice salinity
-       CALL iom_put ('miceage', zcmo(:,:,21) / 365)    ! field 21: mean ice age
-       CALL iom_put ('icelapr',zcmo(:,:,22) )   ! field 22: daily lateral thermo ice prod.
-       CALL iom_put ('icesipr',zcmo(:,:,23) )   ! field 23: daily snowice ice prod.
-       CALL iom_put ('micet', zcmo(:,:,24) )           ! field 24: mean ice temperature
-       CALL iom_put ('icehc', zcmo(:,:,25) )           ! field 25: ice total heat content
-       CALL iom_put ('isnowhc', zcmo(:,:,26) )         ! field 26: snow total heat content
-       CALL iom_put ('icest', zcmo(:,:,27) )           ! field 27: ice surface temperature
-       CALL iom_put ('sfxbri', zcmo(:,:,28) * rday )           ! field 28: brine salt flux
-       CALL iom_put ('sfxthd', zcmo(:,:,29) * rday )           ! field 29: equivalent FW salt flux
-       CALL iom_put ('ibrinv', zcmo(:,:,30) *100 )     ! field 30: brine volume
-       CALL iom_put ('icecolf', zcmo(:,:,31) )         ! field 31: frazil ice collection thickness
-       CALL iom_put ('icestr', zcmo(:,:,32) * 0.001 )  ! field 32: ice strength
-       CALL iom_put ('icevel', zcmo(:,:,33) )          ! field 33: ice velocity
-       CALL iom_put ('isume', zcmo(:,:,34) )    ! field 34: surface melt
-       CALL iom_put ('ibome', zcmo(:,:,35) )     ! field 35: bottom melt
-       CALL iom_put ('idive', zcmo(:,:,36) * 1.0e8)    ! field 36: divergence
-       CALL iom_put ('ishear', zcmo(:,:,37) * 1.0e8 )  ! field 37: shear
-       CALL iom_put ('icerepr', zcmo(:,:,38) ) ! field 38: daily prod./melting due to limupdate
-       CALL iom_put ('icevolu', zcmo(:,:,39) ) ! field 39: ice volume
-       CALL iom_put ('snowvol', zcmo(:,:,40) ) ! field 40: snow volume
-       CALL iom_put ('sfxmec', zcmo(:,:,41) * rday )           ! field 41: salt flux from ridging rafting
-       CALL iom_put ('sfxres', zcmo(:,:,42) * rday )           ! field 42: salt flux from limupdate (resultant)
-       CALL iom_put ('icetrp', zcmo(:,:,43) )    ! field 43: ice volume transport
-
-      !-----------------------------
-      ! Thickness distribution file
-      !-----------------------------
-      IF( add_diag_swi == 1 ) THEN
-
-         DO jl = 1, jpl 
-            CALL lbc_lnk( a_i(:,:,jl)  , 'T' ,  1. )
-            CALL lbc_lnk( sm_i(:,:,jl) , 'T' ,  1. )
-            CALL lbc_lnk( oa_i(:,:,jl) , 'T' ,  1. )
-            CALL lbc_lnk( ht_i(:,:,jl) , 'T' ,  1. )
-            CALL lbc_lnk( ht_s(:,:,jl) , 'T' ,  1. )
-         END DO
-
-         ! Compute ice age
+               z2d(ji,jj) = ( tm_i(ji,jj) - rtt ) * zswi(ji,jj)
+            END DO
+         END DO
+         CALL iom_put( "micet"       , z2d                  )        ! mean ice temperature
+      ENDIF
+      !
+      IF ( iom_use( "icest" ) ) THEN 
+         z2d(:,:) = 0.e0
+         DO jl = 1, jpl
+            DO jj = 1, jpj
+               DO ji = 1, jpi
+                  z2d(ji,jj) = z2d(ji,jj) + zswi(ji,jj) * ( t_su(ji,jj,jl) - rtt ) * a_i(ji,jj,jl) / MAX( at_i(ji,jj) , epsi06 )
+               END DO
+            END DO
+         END DO
+         CALL iom_put( "icest"       , z2d                 )        ! ice surface temperature
+      ENDIF
+
+      IF ( iom_use( "icecolf" ) ) THEN 
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               rswitch  = MAX( 0._wp , SIGN( 1._wp , at_i(ji,jj) ) )
+               z2d(ji,jj) = hicol(ji,jj) * rswitch
+            END DO
+         END DO
+         CALL iom_put( "icecolf"     , z2d                 )        ! frazil ice collection thickness
+      ENDIF
+
+      CALL iom_put( "isst"        , sst_m               )        ! sea surface temperature
+      CALL iom_put( "isss"        , sss_m               )        ! sea surface salinity
+      CALL iom_put( "iceconc"     , at_i                )        ! ice concentration
+      CALL iom_put( "icevolu"     , vt_i                )        ! ice volume = mean ice thickness over the cell
+      CALL iom_put( "icehc"       , et_i                )        ! ice total heat content
+      CALL iom_put( "isnowhc"     , et_s                )        ! snow total heat content
+      CALL iom_put( "ibrinv"      , bv_i * 100._wp      )        ! brine volume
+      CALL iom_put( "utau_ice"    , utau_ice            )        ! wind stress over ice along i-axis at I-point
+      CALL iom_put( "vtau_ice"    , vtau_ice            )        ! wind stress over ice along j-axis at I-point
+      CALL iom_put( "snowpre"     , sprecip             )        ! snow precipitation 
+      CALL iom_put( "micesalt"    , smt_i               )        ! mean ice salinity
+
+      CALL iom_put( "icestr"      , strength * 0.001    )        ! ice strength
+      CALL iom_put( "idive"       , divu_i * 1.0e8      )        ! divergence
+      CALL iom_put( "ishear"      , shear_i * 1.0e8     )        ! shear
+      CALL iom_put( "snowvol"     , vt_s                )        ! snow volume
+      
+      CALL iom_put( "icetrp"      , diag_trp_vi * rday  )        ! ice volume transport
+      CALL iom_put( "snwtrp"      , diag_trp_vs * rday  )        ! snw volume transport
+      CALL iom_put( "deitrp"      , diag_trp_ei         )        ! advected ice enthalpy (W/m2)
+      CALL iom_put( "destrp"      , diag_trp_es         )        ! advected snw enthalpy (W/m2)
+
+      CALL iom_put( "sfxbog"      , sfx_bog * rday      )        ! salt flux from brines
+      CALL iom_put( "sfxbom"      , sfx_bom * rday      )        ! salt flux from brines
+      CALL iom_put( "sfxsum"      , sfx_sum * rday      )        ! salt flux from brines
+      CALL iom_put( "sfxsni"      , sfx_sni * rday      )        ! salt flux from brines
+      CALL iom_put( "sfxopw"      , sfx_opw * rday      )        ! salt flux from brines
+      CALL iom_put( "sfxdyn"      , sfx_dyn * rday      )        ! salt flux from ridging rafting
+      CALL iom_put( "sfxres"      , sfx_res * rday      )        ! salt flux from limupdate (resultant)
+      CALL iom_put( "sfxbri"      , sfx_bri * rday      )        ! salt flux from brines
+      CALL iom_put( "sfx"         , sfx     * rday      )        ! total salt flux
+
+      ztmp = rday / rhoic
+      CALL iom_put( "vfxres"     , wfx_res * ztmp  )             ! daily prod./melting due to limupdate 
+      CALL iom_put( "vfxopw"     , wfx_opw * ztmp  )             ! daily lateral thermodynamic ice production
+      CALL iom_put( "vfxsni"     , wfx_sni * ztmp  )             ! daily snowice ice production
+      CALL iom_put( "vfxbog"     , wfx_bog * ztmp  )             ! daily bottom thermodynamic ice production
+      CALL iom_put( "vfxdyn"     , wfx_dyn * ztmp  )             ! daily dynamic ice production (rid/raft)
+      CALL iom_put( "vfxsum"     , wfx_sum * ztmp  )             ! surface melt 
+      CALL iom_put( "vfxbom"     , wfx_bom * ztmp  )             ! bottom melt 
+      CALL iom_put( "vfxice"     , wfx_ice * ztmp  )             ! total ice growth/melt 
+      CALL iom_put( "vfxsnw"     , wfx_snw * ztmp  )             ! total snw growth/melt 
+      CALL iom_put( "vfxsub"     , wfx_sub * ztmp  )             ! sublimation (snow) 
+      CALL iom_put( "vfxspr"     , wfx_spr * ztmp  )             ! precip (snow) 
+
+      CALL iom_put ('hfxthd', hfx_thd(:,:) )   !  
+      CALL iom_put ('hfxdyn', hfx_dyn(:,:) )   !  
+      CALL iom_put ('hfxres', hfx_res(:,:) )   !  
+      CALL iom_put ('hfxout', hfx_out(:,:) )   !  
+      CALL iom_put ('hfxin' , hfx_in(:,:) )   !  
+      CALL iom_put ('hfxsnw', hfx_snw(:,:) )   !  
+      CALL iom_put ('hfxsub', hfx_sub(:,:) )   !  
+      CALL iom_put ('hfxerr', hfx_err(:,:) )   !  
+      CALL iom_put ('hfxerr_rem', hfx_err_rem(:,:) )   !  
+      
+      CALL iom_put ('hfxsum', hfx_sum(:,:) )   !  
+      CALL iom_put ('hfxbom', hfx_bom(:,:) )   !  
+      CALL iom_put ('hfxbog', hfx_bog(:,:) )   !  
+      CALL iom_put ('hfxdif', hfx_dif(:,:) )   !  
+      CALL iom_put ('hfxopw', hfx_opw(:,:) )   !  
+      CALL iom_put ('hfxtur', fhtur(:,:) * at_i(:,:) )   ! turbulent heat flux at ice base 
+      CALL iom_put ('hfxdhc', diag_heat_dhc(:,:) )          ! Heat content variation in snow and ice 
+      CALL iom_put ('hfxspr', hfx_spr(:,:) )          ! Heat content of snow precip 
+      
+      !--------------------------------
+      ! Output values for each category
+      !--------------------------------
+      CALL iom_put( "iceconc_cat"      , a_i         )        ! area for categories
+      CALL iom_put( "icethic_cat"      , ht_i        )        ! thickness for categories
+      CALL iom_put( "snowthic_cat"     , ht_s        )        ! snow depth for categories
+      CALL iom_put( "salinity_cat"     , sm_i        )        ! salinity for categories
+
+      ! Compute ice age
+      IF ( iom_use( "iceage_cat" ) ) THEN 
          DO jl = 1, jpl 
             DO jj = 1, jpj
                DO ji = 1, jpi
-                  zinda = MAX( zzero , SIGN( zone , a_i(ji,jj,jl) - epsi06 ) )
-                  zoi(ji,jj,jl) = oa_i(ji,jj,jl)  / MAX( a_i(ji,jj,jl) , epsi06 ) * zinda
+                  rswitch = MAX( 0._wp , SIGN( 1._wp , a_i(ji,jj,jl) - epsi06 ) )
+                  zoi(ji,jj,jl) = oa_i(ji,jj,jl)  / MAX( a_i(ji,jj,jl) , epsi06 ) * rswitch
                END DO
             END DO
          END DO
-
-         ! Compute brine volume
+         CALL iom_put( "iceage_cat"     , zoi         )        ! ice age for categories
+      ENDIF
+
+      ! Compute brine volume
+      IF ( iom_use( "brinevol_cat" ) ) THEN 
          zei(:,:,:) = 0._wp
          DO jl = 1, jpl 
@@ -379,38 +259,21 @@
                DO jj = 1, jpj
                   DO ji = 1, jpi
-                     zinda = MAX( zzero , SIGN( zone , a_i(ji,jj,jl) - epsi06 ) )
+                     rswitch = MAX( 0._wp , SIGN( 1._wp , a_i(ji,jj,jl) - epsi06 ) )
                      zei(ji,jj,jl) = zei(ji,jj,jl) + 100.0* &
                         ( - tmut * s_i(ji,jj,jk,jl) / MIN( ( t_i(ji,jj,jk,jl) - rtt ), - epsi06 ) ) * &
-                        zinda / nlay_i
+                        rswitch / nlay_i
                   END DO
                END DO
             END DO
          END DO
-
-         DO jl = 1, jpl 
-            CALL lbc_lnk( zei(:,:,jl) , 'T' ,  1. )
-         END DO
-
-         CALL histwrite( nicea, "iice_itd", niter, a_i  , ndimitd , ndexitd  )   ! area
-         CALL histwrite( nicea, "iice_hid", niter, ht_i , ndimitd , ndexitd  )   ! thickness
-         CALL histwrite( nicea, "iice_hsd", niter, ht_s , ndimitd , ndexitd  )   ! snow depth
-         CALL histwrite( nicea, "iice_std", niter, sm_i , ndimitd , ndexitd  )   ! salinity
-         CALL histwrite( nicea, "iice_otd", niter, zoi  , ndimitd , ndexitd  )   ! age
-         CALL histwrite( nicea, "iice_etd", niter, zei  , ndimitd , ndexitd  )   ! brine volume
-
-         !     !  Create an output files (output.lim.abort.nc) if S < 0 or u > 20 m/s
-         !     IF( kindic < 0 )   CALL lim_wri_state( 'output.abort' )
-         !     not yet implemented
-
-         IF( ( nn_fsbc * niter ) >= nitend .OR. kindic < 0 ) THEN
-            IF(lwp) WRITE(numout,*) ' Closing the icemod file '
-            CALL histclo( nicea ) 
-         ENDIF
-         !
-      ENDIF
-
-      CALL wrk_dealloc( jpi, jpj, zfield )
-      CALL wrk_dealloc( jpi, jpj, jpnoumax, zcmo, zcmoa )
-      CALL wrk_dealloc( jpi, jpj, jpl, zmaskitd, zoi, zei )
+         CALL iom_put( "brinevol_cat"     , zei         )        ! brine volume for categories
+      ENDIF
+
+      !     !  Create an output files (output.lim.abort.nc) if S < 0 or u > 20 m/s
+      !     IF( kindic < 0 )   CALL lim_wri_state( 'output.abort' )
+      !     not yet implemented
+      
+      CALL wrk_dealloc( jpi, jpj, jpl, zoi, zei )
+      CALL wrk_dealloc( jpi, jpj     , z2d, zswi, z2da, z2db )
 
       IF( nn_timing == 1 )  CALL timing_stop('limwri')
@@ -419,124 +282,4 @@
 #endif
 
-   SUBROUTINE lim_wri_init
-      !!-------------------------------------------------------------------
-      !!                    ***   ROUTINE lim_wri_init  ***
-      !!                
-      !! ** Purpose :   ???
-      !!
-      !! ** Method  : Read the namicewri namelist and check the parameter 
-      !!       values called at the first timestep (nit000)
-      !!
-      !! ** input   :   Namelist namicewri
-      !!-------------------------------------------------------------------
-      INTEGER ::   nf      ! ???
-      INTEGER ::   ios     ! Local integer output status for namelist read
-
-      TYPE FIELD 
-         CHARACTER(len = 35) :: ztitle 
-         CHARACTER(len = 8 ) :: zname          
-         CHARACTER(len = 8 ) :: zunit
-         INTEGER             :: znc   
-         REAL                :: zcmulti 
-         REAL                :: zcadd        
-      END TYPE FIELD
-
-      TYPE(FIELD) ::  &
-         field_1 , field_2 , field_3 , field_4 , field_5 , field_6 ,   &
-         field_7 , field_8 , field_9 , field_10, field_11, field_12,   &
-         field_13, field_14, field_15, field_16, field_17, field_18,   &
-         field_19, field_20, field_21, field_22, field_23, field_24,   &
-         field_25, field_26, field_27, field_28, field_29, field_30,   &
-         field_31, field_32, field_33, field_34, field_35, field_36,   &
-         field_37, field_38, field_39, field_40, field_41, field_42, field_43
-
-      TYPE(FIELD) , DIMENSION(jpnoumax) :: zfield
-      !
-      NAMELIST/namiceout/ noumef, &
-         field_1 , field_2 , field_3 , field_4 , field_5 , field_6 ,   &
-         field_7 , field_8 , field_9 , field_10, field_11, field_12,   &
-         field_13, field_14, field_15, field_16, field_17, field_18,   &
-         field_19, field_20, field_21, field_22, field_23, field_24,   &
-         field_25, field_26, field_27, field_28, field_29, field_30,   &
-         field_31, field_32, field_33, field_34, field_35, field_36,   &
-         field_37, field_38, field_39, field_40, field_41, field_42, field_43, add_diag_swi
-      !!-------------------------------------------------------------------
-      REWIND( numnam_ice_ref )              ! Namelist namiceout in reference namelist : Ice outputs
-      READ  ( numnam_ice_ref, namiceout, IOSTAT = ios, ERR = 901)
-901   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namiceout in reference namelist', lwp )
-
-      REWIND( numnam_ice_cfg )              ! Namelist namiceout in configuration namelist : Ice outputs
-      READ  ( numnam_ice_cfg, namiceout, IOSTAT = ios, ERR = 902 )
-902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namiceout in configuration namelist', lwp )
-      WRITE ( numoni, namiceout )
-
-      zfield(1)  = field_1
-      zfield(2)  = field_2
-      zfield(3)  = field_3
-      zfield(4)  = field_4
-      zfield(5)  = field_5
-      zfield(6)  = field_6
-      zfield(7)  = field_7
-      zfield(8)  = field_8
-      zfield(9)  = field_9
-      zfield(10) = field_10
-      zfield(11) = field_11
-      zfield(12) = field_12
-      zfield(13) = field_13
-      zfield(14) = field_14
-      zfield(15) = field_15
-      zfield(16) = field_16
-      zfield(17) = field_17
-      zfield(18) = field_18
-      zfield(19) = field_19
-      zfield(20) = field_20
-      zfield(21) = field_21
-      zfield(22) = field_22
-      zfield(23) = field_23
-      zfield(24) = field_24
-      zfield(25) = field_25
-      zfield(26) = field_26
-      zfield(27) = field_27
-      zfield(28) = field_28
-      zfield(29) = field_29
-      zfield(30) = field_30
-      zfield(31) = field_31
-      zfield(32) = field_32
-      zfield(33) = field_33
-      zfield(34) = field_34
-      zfield(35) = field_35
-      zfield(36) = field_36
-      zfield(37) = field_37
-      zfield(38) = field_38
-      zfield(39) = field_39
-      zfield(40) = field_40
-      zfield(41) = field_41
-      zfield(42) = field_42
-      zfield(43) = field_43
-
-      DO nf = 1, noumef
-         titn  (nf) = zfield(nf)%ztitle
-         nam   (nf) = zfield(nf)%zname
-         uni   (nf) = zfield(nf)%zunit
-         nc    (nf) = zfield(nf)%znc
-         cmulti(nf) = zfield(nf)%zcmulti
-         cadd  (nf) = zfield(nf)%zcadd
-      END DO
-
-      IF(lwp) THEN                        ! control print
-         WRITE(numout,*)
-         WRITE(numout,*) 'lim_wri_init : Ice parameters for outputs'
-         WRITE(numout,*) '~~~~~~~~~~~~'
-         WRITE(numout,*) '    number of fields to be stored         noumef = ', noumef
-         WRITE(numout,*) '           title                            name     unit      Saving (1/0) ',   &
-            &            '    multiplicative constant       additive constant '
-         DO nf = 1 , noumef         
-            WRITE(numout,*) '   ', titn(nf), '   '    , nam   (nf), '      '  , uni (nf),   &
-               &            '  ' , nc  (nf),'        ', cmulti(nf), '        ', cadd(nf)
-         END DO
-         WRITE(numout,*) ' add_diag_swi ', add_diag_swi
-      ENDIF
-      !
-   END SUBROUTINE lim_wri_init
  
    SUBROUTINE lim_wri_state( kt, kid, kh_i )
@@ -555,35 +298,49 @@
       INTEGER, INTENT( in ) ::   kid , kh_i       
       !!----------------------------------------------------------------------
-      !CALL histvert( kid, "icethi", "L levels","m", jpl , hi_mean , nz )
-
-      CALL histdef( kid, "iicethic", "Ice thickness"           , "m"      , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
-      CALL histdef( kid, "iiceconc", "Ice concentration"       , "%"      , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
-      CALL histdef( kid, "iicetemp", "Ice temperature"         , "C"      , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
-      CALL histdef( kid, "iicevelu", "i-Ice speed (I-point)"   , "m/s"    , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
-      CALL histdef( kid, "iicevelv", "j-Ice speed (I-point)"   , "m/s"    , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt ) 
-      CALL histdef( kid, "iicestru", "i-Wind stress over ice (I-pt)", "Pa", jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
-      CALL histdef( kid, "iicestrv", "j-Wind stress over ice (I-pt)", "Pa", jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt ) 
-      CALL histdef( kid, "iicesflx", "Solar flux over ocean"     , "w/m2"   , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt ) 
-      CALL histdef( kid, "iicenflx", "Non-solar flux over ocean" , "w/m2"   , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
-      CALL histdef( kid, "isnowpre", "Snow precipitation"      , "kg/m2/s", jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt ) 
-      CALL histdef( kid, "iicesali", "Ice salinity"            , "PSU"    , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt ) 
-      CALL histdef( kid, "iicevolu", "Ice volume"              , "m"      , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt ) 
-      CALL histdef( kid, "iicedive", "Ice divergence"          , "10-8s-1", jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt ) 
-      CALL histdef( kid, "iicebopr", "Ice bottom production"   , "m/s"      , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
-      CALL histdef( kid, "iicedypr", "Ice dynamic production"  , "m/s"      , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
-      CALL histdef( kid, "iicelapr", "Ice open water prod"     , "m/s"      , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
-      CALL histdef( kid, "iicesipr", "Snow ice production "    , "m/s"      , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
-      CALL histdef( kid, "iicerepr", "Ice prod from limupdate" , "m/s"      , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
-      CALL histdef( kid, "iicebome", "Ice bottom melt"         , "m/s"      , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
-      CALL histdef( kid, "iicesume", "Ice surface melt"        , "m/s"      , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
-      CALL histdef( kid, "iisfxthd", "Salt flux from thermo"   , ""      , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
-      CALL histdef( kid, "iisfxmec", "Salt flux from dynmics"  , ""      , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
-      CALL histdef( kid, "iisfxres", "Salt flux from limupdate", ""      , jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
-
-
-      !CALL histdef( kid, "iice_itd", "Ice concentration by cat", "%"      , jpi, jpj, kh_i, jpl, 1, jpl, -99, 32, "inst(x)", rdt, rdt ) 
-      !CALL histdef( kid, "iice_hid", "Ice thickness by cat"    , "m"      , jpi, jpj, kh_i, jpl, 1, jpl, -99, 32, "inst(x)", rdt, rdt ) 
-      !CALL histdef( kid, "iice_hsd", "Snow thickness by cat"   , "m"      , jpi, jpj, kh_i, jpl, 1, jpl, -99, 32, "inst(x)", rdt, rdt ) 
-      !CALL histdef( kid, "iice_std", "Ice salinity by cat"     , "PSU"    , jpi, jpj, kh_i, jpl, 1, jpl, -99, 32, "inst(x)", rdt, rdt ) 
+
+      CALL histdef( kid, "iicethic", "Ice thickness"           , "m"      ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
+      CALL histdef( kid, "iiceconc", "Ice concentration"       , "%"      ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
+      CALL histdef( kid, "iicetemp", "Ice temperature"         , "C"      ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
+      CALL histdef( kid, "iicevelu", "i-Ice speed (I-point)"   , "m/s"    ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
+      CALL histdef( kid, "iicevelv", "j-Ice speed (I-point)"   , "m/s"    ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt ) 
+      CALL histdef( kid, "iicestru", "i-Wind stress over ice (I-pt)", "Pa",   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
+      CALL histdef( kid, "iicestrv", "j-Wind stress over ice (I-pt)", "Pa",   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt ) 
+      CALL histdef( kid, "iicesflx", "Solar flux over ocean"     , "w/m2" ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt ) 
+      CALL histdef( kid, "iicenflx", "Non-solar flux over ocean" , "w/m2" ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
+      CALL histdef( kid, "isnowpre", "Snow precipitation"      , "kg/m2/s",   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt ) 
+      CALL histdef( kid, "iicesali", "Ice salinity"            , "PSU"    ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt ) 
+      CALL histdef( kid, "iicevolu", "Ice volume"              , "m"      ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt ) 
+      CALL histdef( kid, "iicedive", "Ice divergence"          , "10-8s-1",   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt ) 
+      CALL histdef( kid, "iicebopr", "Ice bottom production"   , "m/s"    ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
+      CALL histdef( kid, "iicedypr", "Ice dynamic production"  , "m/s"    ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
+      CALL histdef( kid, "iicelapr", "Ice open water prod"     , "m/s"    ,   &
+      &       jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
+      CALL histdef( kid, "iicesipr", "Snow ice production "    , "m/s"    ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
+      CALL histdef( kid, "iicerepr", "Ice prod from limupdate" , "m/s"    ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
+      CALL histdef( kid, "iicebome", "Ice bottom melt"         , "m/s"    ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
+      CALL histdef( kid, "iicesume", "Ice surface melt"        , "m/s"    ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
+      CALL histdef( kid, "iisfxdyn", "Salt flux from dynmics"  , ""       ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
+      CALL histdef( kid, "iisfxres", "Salt flux from limupdate", ""       ,   &
+      &      jpi, jpj, kh_i, 1, 1, 1, -99, 32, "inst(x)", rdt, rdt )
 
       CALL histend( kid, snc4set )   ! end of the file definition
@@ -603,19 +360,17 @@
       CALL histwrite( kid, "iicedive", kt, divu_i*1.0e8   , jpi*jpj, (/1/) )
 
-      CALL histwrite( kid, "iicebopr", kt, diag_bot_gr        , jpi*jpj, (/1/) )
-      CALL histwrite( kid, "iicedypr", kt, diag_dyn_gr        , jpi*jpj, (/1/) )
-      CALL histwrite( kid, "iicelapr", kt, diag_lat_gr        , jpi*jpj, (/1/) )
-      CALL histwrite( kid, "iicesipr", kt, diag_sni_gr        , jpi*jpj, (/1/) )
-      CALL histwrite( kid, "iicerepr", kt, diag_res_pr        , jpi*jpj, (/1/) )
-      CALL histwrite( kid, "iicebome", kt, diag_bot_me        , jpi*jpj, (/1/) )
-      CALL histwrite( kid, "iicesume", kt, diag_sur_me        , jpi*jpj, (/1/) )
-      CALL histwrite( kid, "iisfxthd", kt, sfx_thd        , jpi*jpj, (/1/) )
-      CALL histwrite( kid, "iisfxmec", kt, sfx_mec        , jpi*jpj, (/1/) )
+      CALL histwrite( kid, "iicebopr", kt, wfx_bog        , jpi*jpj, (/1/) )
+      CALL histwrite( kid, "iicedypr", kt, wfx_dyn        , jpi*jpj, (/1/) )
+      CALL histwrite( kid, "iicelapr", kt, wfx_opw        , jpi*jpj, (/1/) )
+      CALL histwrite( kid, "iicesipr", kt, wfx_sni        , jpi*jpj, (/1/) )
+      CALL histwrite( kid, "iicerepr", kt, wfx_res        , jpi*jpj, (/1/) )
+      CALL histwrite( kid, "iicebome", kt, wfx_bom        , jpi*jpj, (/1/) )
+      CALL histwrite( kid, "iicesume", kt, wfx_sum        , jpi*jpj, (/1/) )
+      CALL histwrite( kid, "iisfxdyn", kt, sfx_dyn        , jpi*jpj, (/1/) )
       CALL histwrite( kid, "iisfxres", kt, sfx_res        , jpi*jpj, (/1/) )
 
-      !CALL histwrite( kid, "iice_itd", kt, a_i  , jpi*jpj*jpl, (/1/)  )   ! area
-      !CALL histwrite( kid, "iice_hid", kt, ht_i , jpi*jpj*jpl, (/1/)  )   ! thickness
-      !CALL histwrite( kid, "iice_hsd", kt, ht_s , jpi*jpj*jpl, (/1/)  )   ! snow depth
-      !CALL histwrite( kid, "iice_std", kt, sm_i , jpi*jpj*jpl, (/1/)  )   ! salinity
+      ! Close the file
+      ! -----------------
+      !CALL histclo( kid )
 
     END SUBROUTINE lim_wri_state
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limwri_dimg.h90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limwri_dimg.h90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/limwri_dimg.h90	(revision 5038)
@@ -89,14 +89,14 @@
    DO jj = 2 , jpjm1
       DO ji = 2 , jpim1   ! NO vector opt.
-         zindh  = MAX( zzero , SIGN( zone , ht_i(ji,jj,1) * (1.0 - frld(ji,jj) ) - 0.10 ) )
-         zinda  = MAX( zzero , SIGN( zone , ( 1.0 - frld(ji,jj) ) - 0.10 ) )
+         zindh  = MAX( 0._wp , SIGN( 1._wp , ht_i(ji,jj,1) * (1.0 - frld(ji,jj) ) - 0.10 ) )
+         zinda  = MAX( 0._wp , SIGN( 1._wp , ( 1.0 - frld(ji,jj) ) - 0.10 ) )
          zindb  = zindh * zinda
-         ztmu   = MAX( 0.5 * zone , ( tmu(ji,jj) + tmu(ji+1,jj) + tmu(ji,jj+1) + tmu(ji+1,jj+1) ) ) 
+         ztmu   = MAX( 0.5 * 1._wp , ( tmu(ji,jj) + tmu(ji+1,jj) + tmu(ji,jj+1) + tmu(ji+1,jj+1) ) ) 
          zcmo(ji,jj,1)  = ht_s (ji,jj,1)
          zcmo(ji,jj,2)  = ht_i (ji,jj,1)
-         zcmo(ji,jj,3)  = hicifp(ji,jj)
+         zcmo(ji,jj,3)  = 0.
          zcmo(ji,jj,4)  = frld  (ji,jj)
          zcmo(ji,jj,5)  = sist  (ji,jj)
-         zcmo(ji,jj,6)  = fbif  (ji,jj)
+         zcmo(ji,jj,6)  = fhtur  (ji,jj)
          zcmo(ji,jj,7)  = zindb * (  u_ice(ji,jj  ) * tmu(ji,jj  ) + u_ice(ji+1,jj  ) * tmu(ji+1,jj  )   &
             + u_ice(ji,jj+1) * tmu(ji,jj+1) + u_ice(ji+1,jj+1) * tmu(ji+1,jj+1) ) &
@@ -132,14 +132,14 @@
          DO jj = 2 , jpjm1
             DO ji = 2 , jpim1   ! NO vector opt.
-               zindh  = MAX( zzero , SIGN( zone , ht_i(ji,jj,1) * (1.0 - frld(ji,jj) ) - 0.10 ) )
-               zinda  = MAX( zzero , SIGN( zone , ( 1.0 - frld(ji,jj) ) - 0.10 ) )
+               zindh  = MAX( 0._wp , SIGN( 1._wp , ht_i(ji,jj,1) * (1.0 - frld(ji,jj) ) - 0.10 ) )
+               zinda  = MAX( 0._wp , SIGN( 1._wp , ( 1.0 - frld(ji,jj) ) - 0.10 ) )
                zindb  = zindh * zinda
-               ztmu   = MAX( 0.5 * zone , ( tmu(ji,jj) + tmu(ji+1,jj) + tmu(ji,jj+1) + tmu(ji+1,jj+1) ) )
+               ztmu   = MAX( 0.5 * 1._wp , ( tmu(ji,jj) + tmu(ji+1,jj) + tmu(ji,jj+1) + tmu(ji+1,jj+1) ) )
                rcmoy(ji,jj,1)  = ht_s (ji,jj,1)
                rcmoy(ji,jj,2)  = ht_i (ji,jj,1)
-               rcmoy(ji,jj,3)  = hicifp(ji,jj)
+               rcmoy(ji,jj,3)  = 0.
                rcmoy(ji,jj,4)  = frld  (ji,jj)
                rcmoy(ji,jj,5)  = sist  (ji,jj)
-               rcmoy(ji,jj,6)  = fbif  (ji,jj)
+               rcmoy(ji,jj,6)  = fhtur  (ji,jj)
                rcmoy(ji,jj,7)  = zindb * (  u_ice(ji,jj  ) * tmu(ji,jj  ) + u_ice(ji+1,jj  ) * tmu(ji+1,jj  )   &
                   + u_ice(ji,jj+1) * tmu(ji,jj+1) + u_ice(ji+1,jj+1) * tmu(ji+1,jj+1) ) &
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/par_ice.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/par_ice.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/par_ice.F90	(revision 5038)
@@ -12,10 +12,9 @@
 
    !                                             !!! ice thermodynamics
-   INTEGER, PUBLIC, PARAMETER ::   jkmax    = 6   !: maximum number of ice layers
+   INTEGER, PUBLIC, PARAMETER ::   nlay_i   = 5   !: number of ice layers
    INTEGER, PUBLIC, PARAMETER ::   nlay_s   = 1   !: number of snow layers
 
    !                                             !!! ice mechanical redistribution
    INTEGER, PUBLIC, PARAMETER ::   jpl      = 5   !: number of ice categories
-   INTEGER, PUBLIC, PARAMETER ::   jpm      = 1   !: number of ice types
 
    !!----------------------------------------------------------------------
Index: branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/thd_ice.F90
===================================================================
--- branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/thd_ice.F90	(revision 4622)
+++ branches/2014/dev_r4621_NOC4_BDY_VERT_INTERP/NEMOGCM/NEMO/LIM_SRC_3/thd_ice.F90	(revision 5038)
@@ -20,22 +20,13 @@
    !                               !!! ** ice-thermo namelist (namicethd) **
    REAL(wp), PUBLIC ::   hmelt       !: maximum melting at the bottom; active only for one category
-   REAL(wp), PUBLIC ::   hicmin      !: (REMOVE)
    REAL(wp), PUBLIC ::   hiclim      !: minimum ice thickness
-   REAL(wp), PUBLIC ::   sbeta       !: numerical scheme for diffusion in ice  (REMOVE)
-   REAL(wp), PUBLIC ::   parlat      !: (REMOVE)
-   REAL(wp), PUBLIC ::   hakspl      !: (REMOVE)
-   REAL(wp), PUBLIC ::   hibspl      !: (REMOVE)
-   REAL(wp), PUBLIC ::   exld        !: (REMOVE)
-   REAL(wp), PUBLIC ::   hakdif      !: (REMOVE)
-   REAL(wp), PUBLIC ::   thth        !: (REMOVE)
    REAL(wp), PUBLIC ::   hnzst       !: thick. of the surf. layer in temp. comp.
    REAL(wp), PUBLIC ::   parsub      !: switch for snow sublimation or not
-   REAL(wp), PUBLIC ::   alphs       !: coef. for snow density when snow-ice formation
-   REAL(wp), PUBLIC ::   fraz_swi    !: use of frazil ice collection in function of wind (1.0) or not (0.0)
    REAL(wp), PUBLIC ::   maxfrazb    !: maximum portion of frazil ice collecting at the ice bottom
    REAL(wp), PUBLIC ::   vfrazb      !: threshold drift speed for collection of bottom frazil ice
    REAL(wp), PUBLIC ::   Cfrazb      !: squeezing coefficient for collection of bottom frazil ice
+   REAL(wp), PUBLIC ::   hiccrit     !: ice th. for lateral accretion in the NH (SH) (m)
 
-   REAL(wp), PUBLIC, DIMENSION(2) ::   hiccrit   !: ice th. for lateral accretion in the NH (SH) (m)
+   INTEGER , PUBLIC ::   fraz_swi    !: use of frazil ice collection in function of wind (1) or not (0)
 
    !!-----------------------------
@@ -43,5 +34,5 @@
    !!-----------------------------
    !: In ice thermodynamics, to spare memory, the vectors are folded
-   !: from 1D to 2D vectors. The following variables, with ending _1d (or _b)
+   !: from 1D to 2D vectors. The following variables, with ending _1d
    !: are the variables corresponding to 2d vectors
 
@@ -49,28 +40,53 @@
    INTEGER , PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   npac   !: correspondance between points (lateral accretion)
 
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   qldif_1d      !: <==> the 2D  qldif
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   qcmif_1d      !: <==> the 2D  qcmif
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   fstbif_1d     !: <==> the 2D  fstric
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   fltbif_1d     !: <==> the 2D  ffltbif
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   fscbq_1d      !: <==> the 2D  fscmcbq
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   qsr_ice_1d    !: <==> the 2D  qsr_ice
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   fr1_i0_1d     !: <==> the 2D  fr1_i0
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   fr2_i0_1d     !: <==> the 2D  fr2_i0
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   qnsr_ice_1d   !: <==> the 2D  qns_ice
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   qfvbq_1d      !: <==> the 2D  qfvbq
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   t_bo_b        !: <==> the 2D  t_bo
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   qlead_1d     
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   ftr_ice_1d   
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   qsr_ice_1d  
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   fr1_i0_1d   
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   fr2_i0_1d   
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   qns_ice_1d  
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   t_bo_1d     
+
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   hfx_sum_1d
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   hfx_bom_1d
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   hfx_bog_1d
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   hfx_dif_1d
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   hfx_opw_1d
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   hfx_snw_1d
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   hfx_err_1d
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   hfx_err_rem_1d
+
+   ! heat flux associated with ice-atmosphere mass exchange
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   hfx_sub_1d
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   hfx_spr_1d
+
+   ! heat flux associated with ice-ocean mass exchange
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   hfx_thd_1d
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   hfx_res_1d
+
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   wfx_snw_1d 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   wfx_sub_1d
+
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   wfx_bog_1d    
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   wfx_bom_1d   
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   wfx_sum_1d  
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   wfx_sni_1d 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   wfx_opw_1d
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   wfx_res_1d 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   wfx_spr_1d
+
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   sfx_bri_1d
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   sfx_bog_1d    
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   sfx_bom_1d    
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   sfx_sum_1d    
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   sfx_sni_1d    
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   sfx_opw_1d   
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   sfx_res_1d  
 
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   sprecip_1d    !: <==> the 2D  sprecip
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   frld_1d       !: <==> the 2D  frld
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   at_i_b        !: <==> the 2D  frld
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   fbif_1d       !: <==> the 2D  fbif
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   rdm_ice_1d    !: <==> the 2D  rdm_ice
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   rdm_snw_1d    !: <==> the 2D  rdm_snw
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   qlbbq_1d      !: <==> the 2D  qlbsbq
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dmgwi_1d      !: <==> the 2D  dmgwi
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dvsbq_1d      !: <==> the 2D  rdvosif
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dvbbq_1d      !: <==> the 2D  rdvobif
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dvlbq_1d      !: <==> the 2D  rdvolif
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dvnbq_1d      !: <==> the 2D  rdvolif
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   at_i_1d        !: <==> the 2D  at_i
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   fhtur_1d      !: <==> the 2D  fhtur
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   fhld_1d       !: <==> the 2D  fhld
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dqns_ice_1d   !: <==> the 2D  dqns_ice
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   qla_ice_1d    !: <==> the 2D  qla_ice
@@ -78,63 +94,33 @@
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   tatm_ice_1d   !: <==> the 2D  tatm_ice
    !                                                     ! to reintegrate longwave flux inside the ice thermodynamics
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   fsup          !: Energy flux sent from bottom to lateral ablation if |dhb|> 0.15 m
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   focea         !: Remaining energy in case of total ablation
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   i0            !: fraction of radiation transmitted to the ice
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   old_ht_i_b    !: Ice thickness at the beginnning of the time step [m]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   old_ht_s_b    !: Snow thickness at the beginning of the time step [m]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   sfx_bri_1d    !: <==> the 2D sfx_bri
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   fhbri_1d      !: Heat flux due to brine drainage
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   sfx_thd_1d    !: <==> the 2D sfx_thd
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dsm_i_fl_1d   !: Ice salinity variations due to flushing
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dsm_i_gd_1d   !: Ice salinity variations due to gravity drainage
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dsm_i_se_1d   !: Ice salinity variations due to basal salt entrapment
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dsm_i_si_1d   !: Ice salinity variations due to lateral accretion    
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   hicol_b       !: Ice collection thickness accumulated in fleads
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   hicol_1d      !: Ice collection thickness accumulated in leads
 
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   t_su_b      !: <==> the 2D  t_su
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   a_i_b       !: <==> the 2D  a_i
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   ht_i_b      !: <==> the 2D  ht_s
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   ht_s_b      !: <==> the 2D  ht_i
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   fc_su       !: Surface Conduction flux 
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   fc_bo_i     !: Bottom  Conduction flux 
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dh_s_tot    !: Snow accretion/ablation        [m]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dh_i_surf   !: Ice surface accretion/ablation [m]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dh_i_bott   !: Ice bottom accretion/ablation  [m]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dh_snowice  !: Snow ice formation             [m of ice]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   sm_i_b      !: Ice bulk salinity [ppt]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   s_i_new     !: Salinity of new ice at the bottom
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   s_snowice   !: Salinity of new snow ice on top of the ice
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   o_i_b       !: Ice age                        [days]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   t_su_1d       !: <==> the 2D  t_su
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   a_i_1d        !: <==> the 2D  a_i
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   ht_i_1d       !: <==> the 2D  ht_s
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   ht_s_1d       !: <==> the 2D  ht_i
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   fc_su         !: Surface Conduction flux 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   fc_bo_i       !: Bottom  Conduction flux 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dh_s_tot      !: Snow accretion/ablation        [m]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dh_i_surf     !: Ice surface accretion/ablation [m]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dh_i_bott     !: Ice bottom accretion/ablation  [m]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dh_snowice    !: Snow ice formation             [m of ice]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   sm_i_1d       !: Ice bulk salinity [ppt]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   s_i_new       !: Salinity of new ice at the bottom
 
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   iatte_1d   !: clem attenuation coef of the input solar flux (unitless)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   oatte_1d   !: clem attenuation coef of the input solar flux (unitless)
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   t_s_1d   !: corresponding to the 2D var  t_s
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   t_i_1d   !: corresponding to the 2D var  t_i
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   s_i_1d   !: profiled ice salinity
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   q_i_1d   !:    Ice  enthalpy per unit volume
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   q_s_1d   !:    Snow enthalpy per unit volume
 
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   t_s_b   !: corresponding to the 2D var  t_s
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   t_i_b   !: corresponding to the 2D var  t_i
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   s_i_b   !: profiled ice salinity
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   q_i_b   !:    Ice  enthalpy per unit volume
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   q_s_b   !:    Snow enthalpy per unit volume
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   qh_i_old !: ice heat content (q*h, J.m-2)
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   h_i_old  !: ice thickness layer (m)
 
-   ! Clean the following ...
-   ! These variables are coded for conservation checks
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   qt_i_in                  !: ice energy summed over categories (initial)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   qt_i_fin                 !: ice energy summed over categories (final)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   qt_s_in, qt_s_fin        !: snow energy summed over categories
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   dq_i, sum_fluxq          !: increment of energy, sum of fluxes
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fatm, foce               !: atmospheric, oceanic, heat flux
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   cons_error, surf_error   !: conservation, surface error
-
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   q_i_layer_in        !: goes to trash
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   q_i_layer_fin       !: goes to trash
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   dq_i_layer, radab   !: goes to trash
-
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   ftotal_in    !: initial total heat flux
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   ftotal_fin   !: final total heat flux
-
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fc_s
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fc_i
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   de_s_lay
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   de_i_lay
-   
    INTEGER , PUBLIC ::   jiindex_1d   ! 1D index of debugging point
 
@@ -151,50 +137,40 @@
       !!---------------------------------------------------------------------!
       INTEGER ::   thd_ice_alloc   ! return value
-      INTEGER ::   ierr(4)
+      INTEGER ::   ierr(3)
       !!---------------------------------------------------------------------!
 
       ALLOCATE( npb      (jpij) , npac     (jpij),                          &
          !                                                                  !
-         &      qldif_1d (jpij) , qcmif_1d (jpij) , fstbif_1d  (jpij) ,     &
-         &      fltbif_1d(jpij) , fscbq_1d (jpij) , qsr_ice_1d (jpij) ,     &
-         &      fr1_i0_1d(jpij) , fr2_i0_1d(jpij) , qnsr_ice_1d(jpij) ,     &
-         &      qfvbq_1d (jpij) , t_bo_b   (jpij) , iatte_1d   (jpij) ,     &
-         &      oatte_1d (jpij)                                       , STAT=ierr(1) )
+         &      qlead_1d (jpij) , ftr_ice_1d  (jpij) ,     &
+         &      qsr_ice_1d (jpij) ,     &
+         &      fr1_i0_1d(jpij) , fr2_i0_1d(jpij) , qns_ice_1d(jpij) ,     &
+         &      t_bo_1d   (jpij) ,                                          &
+         &      hfx_sum_1d(jpij) , hfx_bom_1d(jpij) ,hfx_bog_1d(jpij) ,     & 
+         &      hfx_dif_1d(jpij) ,hfx_opw_1d(jpij) , &
+         &      hfx_thd_1d(jpij) , hfx_spr_1d(jpij) , &
+         &      hfx_snw_1d(jpij) , hfx_sub_1d(jpij) , hfx_err_1d(jpij) , &
+         &      hfx_res_1d(jpij) , hfx_err_rem_1d(jpij),       STAT=ierr(1) )
       !
-      ALLOCATE( sprecip_1d (jpij) , frld_1d    (jpij) , at_i_b     (jpij) ,     &
-         &      fbif_1d    (jpij) , rdm_ice_1d (jpij) , rdm_snw_1d (jpij) ,     &
-         &      qlbbq_1d   (jpij) , dmgwi_1d   (jpij) , dvsbq_1d   (jpij) ,     &
-         &      dvbbq_1d   (jpij) , dvlbq_1d   (jpij) , dvnbq_1d   (jpij) ,     &
+      ALLOCATE( sprecip_1d (jpij) , frld_1d    (jpij) , at_i_1d     (jpij) ,     &
+         &      fhtur_1d   (jpij) , wfx_snw_1d (jpij) , wfx_spr_1d (jpij) ,     &
+         &      fhld_1d    (jpij) , wfx_sub_1d (jpij) , wfx_bog_1d(jpij) , wfx_bom_1d(jpij) , &
+         &      wfx_sum_1d(jpij)  , wfx_sni_1d (jpij) , wfx_opw_1d (jpij) ,  wfx_res_1d (jpij) ,  &
          &      dqns_ice_1d(jpij) , qla_ice_1d (jpij) , dqla_ice_1d(jpij) ,     &
-         &      tatm_ice_1d(jpij) , fsup       (jpij) , focea      (jpij) ,     &   
-         &      i0         (jpij) , old_ht_i_b (jpij) , old_ht_s_b (jpij) ,     &  
-         &      sfx_bri_1d (jpij) , fhbri_1d   (jpij) , sfx_thd_1d (jpij) ,     &
+         &      tatm_ice_1d(jpij) ,      &   
+         &      i0         (jpij) ,     &  
+         &      sfx_bri_1d (jpij) , sfx_bog_1d (jpij) , sfx_bom_1d (jpij) ,sfx_sum_1d (jpij) ,   &
+         &      sfx_sni_1d (jpij) , sfx_opw_1d (jpij) , sfx_res_1d (jpij) , &
          &      dsm_i_fl_1d(jpij) , dsm_i_gd_1d(jpij) , dsm_i_se_1d(jpij) ,     &     
-         &      dsm_i_si_1d(jpij) , hicol_b    (jpij)                     , STAT=ierr(2) )
+         &      dsm_i_si_1d(jpij) , hicol_1d    (jpij)                     , STAT=ierr(2) )
       !
-      ALLOCATE( t_su_b    (jpij) , a_i_b    (jpij) , ht_i_b   (jpij) ,    &   
-         &      ht_s_b    (jpij) , fc_su    (jpij) , fc_bo_i  (jpij) ,    &    
+      ALLOCATE( t_su_1d    (jpij) , a_i_1d    (jpij) , ht_i_1d   (jpij) ,    &   
+         &      ht_s_1d    (jpij) , fc_su    (jpij) , fc_bo_i  (jpij) ,    &    
          &      dh_s_tot  (jpij) , dh_i_surf(jpij) , dh_i_bott(jpij) ,    &    
-         &      dh_snowice(jpij) , sm_i_b   (jpij) , s_i_new  (jpij) ,    &    
-         &      s_snowice (jpij) , o_i_b    (jpij)                   ,    &
-         !                                                                !
-         &      t_s_b(jpij,nlay_s),                                       &
-         !                                                                !
-         &      t_i_b(jpij,jkmax), s_i_b(jpij,jkmax)                ,     &            
-         &      q_i_b(jpij,jkmax), q_s_b(jpij,jkmax)                , STAT=ierr(3))
+         &      dh_snowice(jpij) , sm_i_1d   (jpij) , s_i_new  (jpij) ,    &
+         &      t_s_1d(jpij,nlay_s),                                       &
+         &      t_i_1d(jpij,nlay_i+1), s_i_1d(jpij,nlay_i+1)                ,     &            
+         &      q_i_1d(jpij,nlay_i+1), q_s_1d(jpij,nlay_i+1)                ,     &
+         &      qh_i_old(jpij,0:nlay_i+1), h_i_old(jpij,0:nlay_i+1) , STAT=ierr(3))
       !
-      ALLOCATE( qt_i_in   (jpij,jpl) , qt_i_fin(jpij,jpl) , qt_s_in   (jpij,jpl) ,     &
-         &      qt_s_fin  (jpij,jpl) , dq_i    (jpij,jpl) , sum_fluxq (jpij,jpl) ,     &
-         &      fatm      (jpij,jpl) , foce    (jpij,jpl) , cons_error(jpij,jpl) ,     &
-         &      surf_error(jpij,jpl)                                             ,     &
-         !                                                                             !
-         &      q_i_layer_in(jpij,jkmax) , q_i_layer_fin(jpij,jkmax)             ,     &
-         &      dq_i_layer  (jpij,jkmax) , radab        (jpij,jkmax)             ,     &
-         !                                                                             !
-         &      ftotal_in(jpij), ftotal_fin(jpij)                                ,     &
-         !                                                                             !
-         &      fc_s(jpij,0:nlay_s) , de_s_lay(jpij,nlay_s)                      ,     &
-         &      fc_i(jpij,0:jkmax)  , de_i_lay(jpij,jkmax)                       , STAT=ierr(4) )
-
       thd_ice_alloc = MAXVAL( ierr )
 
