Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/dom_ice.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/dom_ice.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/dom_ice.F90	(revision 5832)
@@ -5,7 +5,6 @@
    !!======================================================================
    !! History :  3.0  ! 2003-08  (M. Vancoppenolle)  LIM-3 original code
-   !!            4.0  ! 2011-02  (G. Madec) dynamical allocation
+   !!            3.5  ! 2011-02  (G. Madec) dynamical allocation
    !!----------------------------------------------------------------------
-   USE par_ice        ! LIM-3 parameter
    USE in_out_manager ! I/O manager
    USE lib_mpp        ! MPP library
@@ -21,11 +20,5 @@
    INTEGER, PUBLIC ::   njeq , njeqm1        !: j-index of the equator if it is inside the domain
 
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   fcor       !: coriolis coefficient
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   covrai     !: sine of geographic latitude
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   area       !: surface of grid cell 
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   tms, tmi   !: temperature mask, mask for stress
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   tmu, tmv   !: mask at u and v velocity points
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   tmf        !: mask at f-point
-
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)     ::   fcor   !: coriolis coefficient
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::   wght   !: weight of the 4 neighbours to compute averages
 
@@ -44,10 +37,5 @@
       !!-------------------------------------------------------------------
       !
-      ALLOCATE( fcor(jpi,jpj)                   ,      &
-         &      covrai(jpi,jpj) , area(jpi,jpj) ,      &
-         &      tms   (jpi,jpj) , tmi (jpi,jpj) ,      &
-         &      tmu   (jpi,jpj) , tmv (jpi,jpj) ,      &
-         &      tmf   (jpi,jpj) ,                      &
-         &      wght(jpi,jpj,2,2)               , STAT = dom_ice_alloc )
+      ALLOCATE( fcor(jpi,jpj), wght(jpi,jpj,2,2), STAT = dom_ice_alloc )
       !
       IF( dom_ice_alloc /= 0 )   CALL ctl_warn( 'dom_ice_alloc: failed to allocate arrays.' )
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/ice.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/ice.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/ice.F90	(revision 5832)
@@ -11,5 +11,4 @@
    !!   'key_lim3'                                      LIM-3 sea-ice model
    !!----------------------------------------------------------------------
-   USE par_ice        ! LIM sea-ice parameters
    USE in_out_manager ! I/O manager
    USE lib_mpp        ! MPP library
@@ -18,5 +17,5 @@
    PRIVATE
 
-   PUBLIC    ice_alloc  !  Called in iceini.F90
+   PUBLIC    ice_alloc  !  Called in sbc_lim_init
 
    !!======================================================================
@@ -105,13 +104,13 @@
    !! ** 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     |
    !! smv_i       |      -      |    Sea ice salt content         | ppt.m |
    !! 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  | 
-   !! e_s         !      -      !    Snow enthalpy                | 10^9 J| 
-   !!      -      ! q_s_b       !    Snow enthalpy per unit vol.  | J/m3  | 
+   !! e_i         !      -      !    Ice enthalpy                 | J/m2  | 
+   !!      -      ! q_i_1d      !    Ice enthalpy per unit vol.   | J/m3  | 
+   !! e_s         !      -      !    Snow enthalpy                | J/m2  | 
+   !!      -      ! q_s_1d      !    Snow enthalpy per unit vol.  | J/m3  | 
    !!                                                                     |
    !!-------------|-------------|---------------------------------|-------|
@@ -120,12 +119,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 +141,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     |
@@ -148,6 +147,6 @@
    !! tm_i        |      -      |    Mean sea ice temperature     | K     |
    !! ot_i        !      -      !    Sea ice areal age content    | day   |
-   !! et_i        !      -      !    Total ice enthalpy           | 10^9 J| 
-   !! et_s        !      -      !    Total snow enthalpy          | 10^9 J| 
+   !! et_i        !      -      !    Total ice enthalpy           | J/m2  | 
+   !! et_s        !      -      !    Total snow enthalpy          | J/m2  | 
    !! bv_i        !      -      !    Mean relative brine volume   | ???   | 
    !!=====================================================================
@@ -165,76 +164,71 @@
    REAL(wp), PUBLIC ::   r1_rdtice        !: = 1. / rdt_ice
 
-   !                                     !!** 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
+   !                                     !!** ice-thickness distribution namelist (namiceitd) **
+   INTEGER , PUBLIC ::   nn_catbnd        !: categories distribution following: tanh function (1), or h^(-alpha) function (2)
+   REAL(wp), PUBLIC ::   rn_himean        !: mean thickness of the domain (used to compute the distribution, nn_itdshp = 2 only)
+
+   !                                     !!** ice-dynamics namelist (namicedyn) **
+   LOGICAL , PUBLIC ::   ln_icestr_bvf    !: use brine volume to diminish ice strength
+   INTEGER , PUBLIC ::   nn_icestr        !: ice strength parameterization (0=Hibler79 1=Rothrock75)
+   INTEGER , PUBLIC ::   nn_nevp          !: number of iterations for subcycling
+   INTEGER , PUBLIC ::   nn_ahi0          !: sea-ice hor. eddy diffusivity coeff. (3 ways of calculation)
+   REAL(wp), PUBLIC ::   rn_pe_rdg        !: ridging work divided by pot. energy change in ridging, nn_icestr = 1
+   REAL(wp), PUBLIC ::   rn_cio           !: drag coefficient for oceanic stress
+   REAL(wp), PUBLIC ::   rn_pstar         !: determines ice strength (N/M), Hibler JPO79
+   REAL(wp), PUBLIC ::   rn_crhg          !: determines changes in ice strength
+   REAL(wp), PUBLIC ::   rn_creepl        !: creep limit : has to be under 1.0e-9
+   REAL(wp), PUBLIC ::   rn_ecc           !: eccentricity of the elliptical yield curve
+   REAL(wp), PUBLIC ::   rn_ahi0_ref      !: sea-ice hor. eddy diffusivity coeff. (m2/s)
+   REAL(wp), PUBLIC ::   rn_relast        !: ratio => telast/rdt_ice (1/3 or 1/9 depending on nb of subcycling nevp) 
 
    !                                     !!** ice-salinity namelist (namicesal) **
-   REAL(wp), PUBLIC ::   s_i_max          !: maximum ice salinity [PSU]
-   REAL(wp), PUBLIC ::   s_i_min          !: minimum ice salinity [PSU]
-   REAL(wp), PUBLIC ::   s_i_0            !: 1st sal. value for the computation of sal .prof. [PSU]
-   REAL(wp), PUBLIC ::   s_i_1            !: 2nd sal. value for the computation of sal .prof. [PSU]
-   REAL(wp), PUBLIC ::   sal_G            !: restoring salinity for gravity drainage [PSU]
-   REAL(wp), PUBLIC ::   sal_F            !: restoring salinity for flushing [PSU]
-   REAL(wp), PUBLIC ::   time_G           !: restoring time constant for gravity drainage (= 20 days) [s]
-   REAL(wp), PUBLIC ::   time_F           !: restoring time constant for gravity drainage (= 10 days) [s]
-   REAL(wp), PUBLIC ::   bulk_sal         !: bulk salinity (ppt) in case of constant salinity
+   REAL(wp), PUBLIC ::   rn_simax         !: maximum ice salinity [PSU]
+   REAL(wp), PUBLIC ::   rn_simin         !: minimum ice salinity [PSU]
+   REAL(wp), PUBLIC ::   rn_sal_gd        !: restoring salinity for gravity drainage [PSU]
+   REAL(wp), PUBLIC ::   rn_sal_fl        !: restoring salinity for flushing [PSU]
+   REAL(wp), PUBLIC ::   rn_time_gd       !: restoring time constant for gravity drainage (= 20 days) [s]
+   REAL(wp), PUBLIC ::   rn_time_fl       !: restoring time constant for gravity drainage (= 10 days) [s]
+   REAL(wp), PUBLIC ::   rn_icesal        !: bulk salinity (ppt) in case of constant salinity
 
    !                                     !!** ice-salinity namelist (namicesal) **
-   INTEGER , PUBLIC ::   num_sal          !: salinity configuration used in the model
+   INTEGER , PUBLIC ::   nn_icesal           !: 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 ::   nn_ice_thcon        !: thermal conductivity: =0 Untersteiner (1964) ; =1 Pringle et al (2007)
+   INTEGER , PUBLIC ::   nn_monocat          !: virtual ITD mono-category parameterizations (1) or not (0)
+   LOGICAL , PUBLIC ::   ln_it_qnsice        !: iterate surface flux with changing surface temperature or not (F)
 
    !                                     !!** ice-mechanical redistribution namelist (namiceitdme)
-   REAL(wp), PUBLIC ::   Cs               !: fraction of shearing energy contributing to ridging            
-   REAL(wp), PUBLIC ::   Cf               !: ratio of ridging work to PE loss
-   REAL(wp), PUBLIC ::   fsnowrdg         !: fractional snow loss to the ocean during ridging
-   REAL(wp), PUBLIC ::   fsnowrft         !: fractional snow loss to the ocean during ridging
-   REAL(wp), PUBLIC ::   Gstar            !: fractional area of young ice contributing to ridging
-   REAL(wp), PUBLIC ::   astar            !: equivalent of G* for an exponential participation function
-   REAL(wp), PUBLIC ::   Hstar            !: thickness that determines the maximal thickness of ridged ice
-   REAL(wp), PUBLIC ::   hparmeter        !: threshold thickness (m) for rafting / ridging 
-   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]
-   REAL(wp), PUBLIC ::   nconv_i_thd      !: maximal number of iterations for heat diffusion
-   REAL(wp), PUBLIC ::   maxer_i_thd      !: maximal tolerated error (C) for heat diffusion
+   REAL(wp), PUBLIC ::   rn_cs            !: fraction of shearing energy contributing to ridging            
+   REAL(wp), PUBLIC ::   rn_fsnowrdg      !: fractional snow loss to the ocean during ridging
+   REAL(wp), PUBLIC ::   rn_fsnowrft      !: fractional snow loss to the ocean during ridging
+   REAL(wp), PUBLIC ::   rn_gstar         !: fractional area of young ice contributing to ridging
+   REAL(wp), PUBLIC ::   rn_astar         !: equivalent of G* for an exponential participation function
+   REAL(wp), PUBLIC ::   rn_hstar         !: thickness that determines the maximal thickness of ridged ice
+   REAL(wp), PUBLIC ::   rn_hraft         !: threshold thickness (m) for rafting / ridging 
+   REAL(wp), PUBLIC ::   rn_craft         !: coefficient for smoothness of the hyperbolic tangent in rafting
+   REAL(wp), PUBLIC ::   rn_por_rdg       !: initial porosity of ridges (0.3 regular value)
+   REAL(wp), PUBLIC ::   rn_betas         !: coef. for partitioning of snowfall between leads and sea ice
+   REAL(wp), PUBLIC ::   rn_kappa_i       !: coef. for the extinction of radiation Grenfell et al. (2006) [1/m]
+   REAL(wp), PUBLIC ::   nn_conv_dif      !: maximal number of iterations for heat diffusion
+   REAL(wp), PUBLIC ::   rn_terr_dif      !: maximal tolerated error (C) for heat diffusion
 
    !                                     !!** 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 ::   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
+   LOGICAL , PUBLIC ::   ln_rafting      !: rafting of ice or not                        
+   INTEGER , PUBLIC ::   nn_partfun      !: participation function: =0 Thorndike et al. (1975), =1 Lipscomb et al. (2007)
+
+   REAL(wp), PUBLIC ::   usecc2           !:  = 1.0 / ( rn_ecc * rn_ecc )
+   REAL(wp), PUBLIC ::   rhoco            !: = rau0 * cio
+   REAL(wp), PUBLIC ::   r1_nlay_i        !: 1 / nlay_i
+   REAL(wp), PUBLIC ::   r1_nlay_s        !: 1 / nlay_s 
+   !
+   !                                     !!** switch for presence of ice or not 
+   REAL(wp), PUBLIC ::   rswitch
+   !
+   !                                     !!** define some parameters 
+   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,62 @@
    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(:,:) ::   afx_tot     !: ice concentration tendency (total) [s-1]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   afx_thd     !: ice concentration tendency (thermodynamics) [s-1]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:) ::   afx_dyn     !: ice concentration tendency (dynamics) [s-1]
+
+   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
-
-   ! 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(:,:) ::   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_dif !: heat flux remaining due to change in non-solar flux
+   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
 
    !!--------------------------------------------------------------------------
@@ -321,14 +331,9 @@
    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]
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::   e_i        !: ice thermal contents [Giga J]
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::   e_i        !: ice thermal contents    [J/m2]
    REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::   s_i        !: ice salinities          [PSU]
 
@@ -348,73 +353,52 @@
    !! * 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)
-      
-
-   !!--------------------------------------------------------------------------
-   !! * Increment of global variables
-   !!--------------------------------------------------------------------------
+   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
+            
+   !!--------------------------------------------------------------------------
+   !! * Ice thickness distribution variables
+   !!--------------------------------------------------------------------------
+   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 
+
+   !!--------------------------------------------------------------------------
+   !! * Ice Run
+   !!--------------------------------------------------------------------------
+   !                                                  !!: ** Namelist namicerun read in sbc_lim_init **
+   INTEGER          , PUBLIC ::   jpl             !: number of ice  categories 
+   INTEGER          , PUBLIC ::   nlay_i          !: number of ice  layers 
+   INTEGER          , PUBLIC ::   nlay_s          !: number of snow layers 
+   CHARACTER(len=32), PUBLIC ::   cn_icerst_in    !: suffix of ice restart name (input)
+   CHARACTER(len=256), PUBLIC ::   cn_icerst_indir !: ice restart input directory
+   CHARACTER(len=32), PUBLIC ::   cn_icerst_out   !: suffix of ice restart name (output)
+   CHARACTER(len=256), PUBLIC ::   cn_icerst_outdir!: ice restart output directory
+   LOGICAL          , PUBLIC ::   ln_limdyn       !: flag for ice dynamics (T) or not (F)
+   LOGICAL          , PUBLIC ::   ln_icectl       !: flag for sea-ice points output (T) or not (F)
+   REAL(wp)         , PUBLIC ::   rn_amax         !: maximum ice concentration
+   INTEGER          , PUBLIC ::   iiceprt         !: debug i-point
+   INTEGER          , PUBLIC ::   jiceprt         !: debug j-point
+   !
+   !!--------------------------------------------------------------------------
+   !! * Ice diagnostics
+   !!--------------------------------------------------------------------------
+   ! Increment of global variables
    ! thd refers to changes induced by thermodynamics
    ! trp   ''         ''     ''       advection (transport of ice)
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   d_a_i_thd  , d_a_i_trp                 !: icefractions                  
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   d_v_s_thd  , d_v_s_trp                 !: snow volume
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   d_v_i_thd  , d_v_i_trp                 !: ice  volume
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:) ::   d_smv_i_thd, d_smv_i_trp               !:     
-   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_e_s_thd  , d_e_s_trp     !:
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:,:,:) ::   d_e_i_thd  , d_e_i_trp     !:
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)     ::   d_u_ice_dyn, d_v_ice_dyn   !: ice velocity 
-      
-   !!--------------------------------------------------------------------------
-   !! * 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
-
-   !!--------------------------------------------------------------------------
-   !! * Ice Run
-   !!--------------------------------------------------------------------------
-   !                                                  !!: ** Namelist namicerun read in iceini **
-   CHARACTER(len=32)     , PUBLIC ::   cn_icerst_in    !: suffix of ice restart name (input)
-   CHARACTER(len=32)     , PUBLIC ::   cn_icerst_out   !: suffix of ice restart name (output)
-   LOGICAL               , PUBLIC ::   ln_limdyn       !: flag for ice dynamics (T) or not (F)
-   LOGICAL               , PUBLIC ::   ln_nicep        !: flag for sea-ice points output (T) or not (F)
-   REAL(wp)              , PUBLIC ::   cai             !: atmospheric drag over sea ice
-   REAL(wp)              , PUBLIC ::   cao             !: atmospheric drag over ocean
-   REAL(wp)              , PUBLIC ::   amax            !: maximum ice concentration
+   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(:,:)     ::   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_trp_smv  !: transport of salt content
    !
-   !!--------------------------------------------------------------------------
-   !! * Ice diagnostics
-   !!--------------------------------------------------------------------------
-   !! Check if everything down here is necessary
-   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
-   INTEGER , PUBLIC ::   jiindx, jjindx        !: indexes of the debugging point
-
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)     ::   diag_heat     !: snw/ice heat content variation   [W/m2] 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)     ::   diag_smvi     !: ice salt content variation   [] 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)     ::   diag_vice     !: ice volume variation   [m/s] 
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:,:)     ::   diag_vsnw     !: snw volume variation   [m/s] 
+   !
    !!----------------------------------------------------------------------
    !! NEMO/LIM3 4.0 , UCL - NEMO Consortium (2010)
@@ -430,5 +414,5 @@
       INTEGER :: ice_alloc
       !
-      INTEGER :: ierr(20), ii
+      INTEGER :: ierr(17), ii
       !!-----------------------------------------------------------------
 
@@ -447,19 +431,18 @@
 
       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) ,     &
+         &      afx_tot(jpi,jpj) , afx_thd(jpi,jpj),  afx_dyn(jpi,jpj) ,                        &
+         &      fhtur  (jpi,jpj) , ftr_ice(jpi,jpj,jpl), qlead  (jpi,jpj) ,                     &
+         &      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_dif(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 +458,7 @@
          &      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,jpl) , e_i(jpi,jpj,nlay_i,jpl) , s_i(jpi,jpj,nlay_i,jpl) , STAT=ierr(ii) )
 
       ! * Moments for advection
@@ -494,36 +476,22 @@
          &      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,jpl) , sye (jpi,jpj,nlay_i,jpl) , sxxe(jpi,jpj,nlay_i,jpl) ,     &
+         &      syye(jpi,jpj,nlay_i,jpl) , sxye(jpi,jpj,nlay_i,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) )
-
-      ! * Increment of global variables
-      ii = ii + 1
-      ALLOCATE( d_a_i_thd(jpi,jpj,jpl) , d_a_i_trp (jpi,jpj,jpl) , d_v_s_thd  (jpi,jpj,jpl) , d_v_s_trp  (jpi,jpj,jpl) ,   &
-         &      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) ,   &
-         &     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( 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,jpl) ,     &
+         &      oa_i_b (jpi,jpj,jpl) , u_ice_b(jpi,jpj)     , v_ice_b(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( diag_trp_vi(jpi,jpj), diag_trp_vs (jpi,jpj), diag_trp_ei(jpi,jpj),   & 
+         &      diag_trp_es(jpi,jpj), diag_trp_smv(jpi,jpj), diag_heat  (jpi,jpj),   &
+         &      diag_smvi  (jpi,jpj), diag_vice   (jpi,jpj), diag_vsnw  (jpi,jpj), STAT=ierr(ii) )
 
       ice_alloc = MAXVAL( ierr(:) )
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/iceini.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/iceini.F90	(revision 4712)
+++ 	(revision )
@@ -1,267 +1,0 @@
-MODULE iceini
-   !!======================================================================
-   !!                       ***  MODULE iceini   ***
-   !!   Sea-ice model : LIM Sea ice model Initialization
-   !!======================================================================
-   !! History :  3.0  ! 2008-03  (M. Vancoppenolle) LIM-3 original code
-   !!            3.3  ! 2010-12  (G. Madec) add call to lim_thd_init and lim_thd_sal_init
-   !!            4.0  ! 2011-02  (G. Madec) dynamical allocation
-   !!----------------------------------------------------------------------
-#if defined key_lim3
-   !!----------------------------------------------------------------------
-   !!   'key_lim3'                                     LIM sea-ice model
-   !!----------------------------------------------------------------------
-   !!   ice_init      : sea-ice model initialization
-   !!----------------------------------------------------------------------
-   USE phycst         ! physical constants
-   USE dom_oce        ! ocean domain
-   USE sbc_oce        ! Surface boundary condition: ocean fields
-   USE sbc_ice        ! Surface boundary condition: ice   fields
-   USE ice            ! LIM variables
-   USE par_ice        ! LIM parameters
-   USE dom_ice        ! LIM domain
-   USE thd_ice        ! LIM thermodynamical variables
-   USE limitd_me      ! LIM ice thickness distribution
-   USE limmsh         ! LIM mesh
-   USE limistate      ! LIM initial state
-   USE limrst         ! LIM restart
-   USE limthd         ! LIM ice thermodynamics
-   USE limthd_sal     ! LIM ice thermodynamics: salinity
-   USE limvar         ! LIM variables
-   USE limsbc         ! LIM surface boundary condition
-   USE in_out_manager ! I/O manager
-   USE lib_mpp        ! MPP library
-   USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
-
-   IMPLICIT NONE
-   PRIVATE
-
-   PUBLIC   ice_init   ! called by sbcice_lim.F90
-
-   !!----------------------------------------------------------------------
-   !! NEMO/LIM3 4.0 , UCL - NEMO Consortium (2011)
-   !! $Id$
-   !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
-   !!----------------------------------------------------------------------
-CONTAINS
-
-   SUBROUTINE ice_init
-      !!----------------------------------------------------------------------
-      !!                  ***  ROUTINE ice_init  ***
-      !!
-      !! ** purpose :   Allocate all the dynamic arrays of the LIM-3 modules
-      !!----------------------------------------------------------------------
-      INTEGER :: ierr
-      !!----------------------------------------------------------------------
-
-      !                                ! Allocate the ice arrays
-      ierr =        ice_alloc        ()      ! ice variables
-      ierr = ierr + dom_ice_alloc    ()      ! domain
-      ierr = ierr + sbc_ice_alloc    ()      ! surface forcing
-      ierr = ierr + thd_ice_alloc    ()      ! thermodynamics
-      ierr = ierr + lim_itd_me_alloc ()      ! ice thickness distribution - mechanics
-      !
-      IF( lk_mpp    )   CALL mpp_sum( ierr )
-      IF( ierr /= 0 )   CALL ctl_stop('STOP', 'ice_init : unable to allocate ice arrays')
-      !
-      !                                ! adequation jpk versus ice/snow layers/categories
-      IF( jpl   > jpk  .OR.  jpm    > jpk .OR.                                    &
-          jkmax > 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.' )
-
-                                       ! Open the reference and configuration namelist files and namelist output file 
-      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 )
-      IF(lwm) CALL ctl_opn( numoni, 'output.namelist.ice', 'UNKNOWN', 'FORMATTED', 'SEQUENTIAL', -1, numout, lwp, 1 )
-      !
-      CALL ice_run                     ! set some ice run parameters
-      !
-      CALL lim_thd_init                ! set ice thermodynics parameters
-      !
-      CALL lim_thd_sal_init            ! set ice salinity parameters
-      !
-      rdt_ice   = nn_fsbc * rdttra(1)  ! sea-ice timestep
-      r1_rdtice = 1._wp / rdt_ice      ! sea-ice timestep inverse
-      !
-      CALL lim_msh                     ! ice mesh initialization
-      !
-      CALL lim_itd_ini                 ! ice thickness distribution initialization
-      !
-      !                                ! Initial sea-ice state
-      IF( .NOT. ln_rstart ) THEN              ! start from rest
-         numit = 0
-         numit = nit000 - 1
-         CALL lim_istate                        ! start from rest: sea-ice deduced from sst
-         CALL lim_var_agg(1)                    ! aggregate category variables in bulk variables
-         CALL lim_var_glo2eqv                   ! convert global variables in equivalent variables
-      ELSE                                   ! start from a restart file
-         CALL lim_rst_read                      ! read the restart file
-         numit = nit000 - 1
-         CALL lim_var_agg(1)                    ! aggregate ice variables
-         CALL lim_var_glo2eqv                   ! convert global var in equivalent variables
-      ENDIF
-      !
-      hi_max(jpl) = 99._wp  ! Set hi_max(jpl) to a big value to ensure that all ice is thinner than hi_max(jpl)
-      !
-      CALL lim_sbc_init                 ! ice surface boundary condition   
-      !
-      fr_i(:,:) = at_i(:,:)             ! initialisation of sea-ice fraction
-      tn_ice(:,:,:) = t_su(:,:,:)       ! initialisation of surface temp for coupled simu
-      !
-      nstart = numit  + nn_fsbc      
-      nitrun = nitend - nit000 + 1 
-      nlast  = numit  + nitrun 
-      !
-      IF( nstock == 0  )   nstock = nlast + 1
-      !
-   END SUBROUTINE ice_init
-
-
-   SUBROUTINE ice_run
-      !!-------------------------------------------------------------------
-      !!                  ***  ROUTINE ice_run ***
-      !!                 
-      !! ** Purpose :   Definition some run parameter for ice model
-      !!
-      !! ** Method  :   Read the namicerun namelist and check the parameter 
-      !!              values called at the first timestep (nit000)
-      !!
-      !! ** input   :   Namelist namicerun
-      !!-------------------------------------------------------------------
-      NAMELIST/namicerun/ cn_icerst_in, cn_icerst_out, ln_limdyn, amax, cai, cao, ln_nicep, ln_limdiahsb, ln_limdiaout
-      INTEGER  ::   ios                 ! Local integer output status for namelist read
-      !!-------------------------------------------------------------------
-      !                    
-      REWIND( numnam_ice_ref )              ! Namelist namicerun in reference namelist : Parameters for ice
-      READ  ( numnam_ice_ref, namicerun, IOSTAT = ios, ERR = 901)
-901   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namicerun in reference namelist', lwp )
-
-      REWIND( numnam_ice_cfg )              ! Namelist namicerun in configuration namelist : Parameters for ice
-      READ  ( numnam_ice_cfg, namicerun, IOSTAT = ios, ERR = 902 )
-902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namicerun in configuration namelist', lwp )
-      IF(lwm) WRITE ( numoni, namicerun )
-      !
-      !IF( lk_mpp .AND. ln_nicep ) THEN
-      !   ln_nicep = .FALSE.
-      !   CALL ctl_warn( 'ice_run : specific control print for LIM3 desactivated with MPI' )
-      !ENDIF
-      !
-      IF(lwp) THEN                        ! control print
-         WRITE(numout,*)
-         WRITE(numout,*) 'ice_run : ice share parameters for dynamics/advection/thermo of sea-ice'
-         WRITE(numout,*) ' ~~~~~~'
-         WRITE(numout,*) '   switch for ice dynamics (1) or not (0)      ln_limdyn   = ', ln_limdyn
-         WRITE(numout,*) '   maximum ice concentration                               = ', amax 
-         WRITE(numout,*) '   atmospheric drag over sea ice                           = ', cai
-         WRITE(numout,*) '   atmospheric drag over ocean                             = ', cao
-         WRITE(numout,*) '   Several ice points in the ice or not in ocean.output    = ', ln_nicep
-         WRITE(numout,*) '   Diagnose heat/salt budget or not          ln_limdiahsb  = ', ln_limdiahsb
-         WRITE(numout,*) '   Output   heat/salt budget or not          ln_limdiaout  = ', ln_limdiaout
-      ENDIF
-      !
-   END SUBROUTINE ice_run
-
-
-   SUBROUTINE lim_itd_ini
-      !!------------------------------------------------------------------
-      !!                ***  ROUTINE lim_itd_ini ***
-      !!
-      !! ** Purpose :   Initializes the ice thickness distribution
-      !! ** Method  :   ...
-      !!    Note    : hi_max(jpl) is here set up to a value close to 7 m for
-      !!              limistate (only) and is changed to 99 m in ice_init
-      !!------------------------------------------------------------------
-      INTEGER  ::   jl, jm               ! dummy loop index
-      REAL(wp) ::   zc1, zc2, zc3, zx1   ! local scalars
-      !!------------------------------------------------------------------
-
-      IF(lwp) WRITE(numout,*)
-      IF(lwp) WRITE(numout,*) 'lim_itd_ini : Initialization of ice thickness distribution '
-      IF(lwp) WRITE(numout,*) '~~~~~~~~~~~~'
-
-      !------------------------------------------------------------------------------!
-      ! 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
-
-      !- Thickness categories boundaries 
-      !----------------------------------
-      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 )
-      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 )
-         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,*) ' 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
-         hi_mean(jl) = ( hi_max(jl) + hi_max(jl-1) ) * 0.5_wp
-      END DO
-      !
-      !
-   END SUBROUTINE lim_itd_ini
-
-#else
-   !!----------------------------------------------------------------------
-   !!   Default option :        Empty module           NO LIM sea-ice model
-   !!----------------------------------------------------------------------
-CONTAINS
-   SUBROUTINE ice_init        ! Empty routine
-   END SUBROUTINE ice_init
-#endif
-
-   !!======================================================================
-END MODULE iceini
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limadv.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limadv.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limadv.F90	(revision 5832)
@@ -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
@@ -67,5 +63,5 @@
       !! 
       INTEGER  ::   ji, jj                               ! dummy loop indices
-      REAL(wp) ::   zs1max, zrdt, zslpmax, ztemp, zin0   ! local scalars
+      REAL(wp) ::   zs1max, zrdt, zslpmax, ztemp         ! local scalars
       REAL(wp) ::   zs1new, zalf , zalfq , zbt           !   -      -
       REAL(wp) ::   zs2new, zalf1, zalf1q, zbt1          !   -      -
@@ -84,28 +80,28 @@
       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
+            rswitch = ( 1.0 - MAX( 0._wp, SIGN( 1._wp, -zslpmax) ) ) * tmask(ji,jj,1)   ! Case of empty boxes & Apply mask
 
             ps0 (ji,jj) = zslpmax  
-            psx (ji,jj) = zs1new      * zin0
-            psxx(ji,jj) = zs2new      * zin0
-            psy (ji,jj) = psy (ji,jj) * zin0
-            psyy(ji,jj) = psyy(ji,jj) * zin0
-            psxy(ji,jj) = MIN( zslpmax, MAX( -zslpmax, psxy(ji,jj) ) ) * zin0
+            psx (ji,jj) = zs1new      * rswitch
+            psxx(ji,jj) = zs2new      * rswitch
+            psy (ji,jj) = psy (ji,jj) * rswitch
+            psyy(ji,jj) = psyy(ji,jj) * rswitch
+            psxy(ji,jj) = MIN( zslpmax, MAX( -zslpmax, psxy(ji,jj) ) ) * rswitch
          END DO
       END DO
 
       !  Initialize volumes of boxes  (=area if adv_x first called, =psm otherwise)                                     
-      psm (:,:)  = MAX( pcrh * area(:,:) + ( 1.0 - pcrh ) * psm(:,:) , epsi20 )
+      psm (:,:)  = MAX( pcrh * e12t(:,:) + ( 1.0 - pcrh ) * psm(:,:) , epsi20 )
 
       !  Calculate fluxes and moments between boxes i<-->i+1              
       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
@@ -211,8 +207,8 @@
 
       !-- Lateral boundary conditions
-      CALL lbc_lnk( psm , 'T',  1. )   ;   CALL lbc_lnk( ps0 , 'T',  1. )
-      CALL lbc_lnk( psx , 'T', -1. )   ;   CALL lbc_lnk( psy , 'T', -1. )      ! caution gradient ==> the sign changes
-      CALL lbc_lnk( psxx, 'T',  1. )   ;   CALL lbc_lnk( psyy, 'T',  1. )
-      CALL lbc_lnk( psxy, 'T',  1. )
+      CALL lbc_lnk_multi( psm , 'T',  1., ps0 , 'T',  1.   &
+         &              , psx , 'T', -1., psy , 'T', -1.   &   ! caution gradient ==> the sign changes
+         &              , psxx, 'T',  1., psyy, 'T',  1.   &
+         &              , psxy, 'T',  1. )
 
       IF(ln_ctl) THEN
@@ -252,5 +248,5 @@
       !!
       INTEGER  ::   ji, jj                               ! dummy loop indices
-      REAL(wp) ::   zs1max, zrdt, zslpmax, ztemp, zin0   ! temporary scalars
+      REAL(wp) ::   zs1max, zrdt, zslpmax, ztemp         ! temporary scalars
       REAL(wp) ::   zs1new, zalf , zalfq , zbt           !    -         -
       REAL(wp) ::   zs2new, zalf1, zalf1q, zbt1          !    -         -
@@ -269,28 +265,28 @@
       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
+            rswitch = ( 1.0 - MAX( 0._wp, SIGN( 1._wp, -zslpmax) ) ) * tmask(ji,jj,1)   ! Case of empty boxes & Apply mask
             !
             ps0 (ji,jj) = zslpmax  
-            psx (ji,jj) = psx (ji,jj) * zin0
-            psxx(ji,jj) = psxx(ji,jj) * zin0
-            psy (ji,jj) = zs1new * zin0
-            psyy(ji,jj) = zs2new * zin0
-            psxy(ji,jj) = MIN( zslpmax, MAX( -zslpmax, psxy(ji,jj) ) ) * zin0
+            psx (ji,jj) = psx (ji,jj) * rswitch
+            psxx(ji,jj) = psxx(ji,jj) * rswitch
+            psy (ji,jj) = zs1new * rswitch
+            psyy(ji,jj) = zs2new * rswitch
+            psxy(ji,jj) = MIN( zslpmax, MAX( -zslpmax, psxy(ji,jj) ) ) * rswitch
          END DO
       END DO
 
       !  Initialize volumes of boxes (=area if adv_x first called, =psm otherwise)
-      psm(:,:)  = MAX(  pcrh * area(:,:) + ( 1.0 - pcrh ) * psm(:,:) , epsi20  )
+      psm(:,:)  = MAX(  pcrh * e12t(:,:) + ( 1.0 - pcrh ) * psm(:,:) , epsi20  )
 
       !  Calculate fluxes and moments between boxes j<-->j+1              
       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
@@ -397,8 +393,8 @@
 
       !-- Lateral boundary conditions
-      CALL lbc_lnk( psm , 'T',  1. )   ;   CALL lbc_lnk( ps0 , 'T',  1. )
-      CALL lbc_lnk( psx , 'T', -1. )   ;   CALL lbc_lnk( psy , 'T', -1. )      ! caution gradient ==> the sign changes
-      CALL lbc_lnk( psxx, 'T',  1. )   ;   CALL lbc_lnk( psyy, 'T',  1. )
-      CALL lbc_lnk( psxy, 'T',  1. )
+      CALL lbc_lnk_multi( psm , 'T',  1.,  ps0 , 'T',  1.   &
+         &              , psx , 'T', -1.,  psy , 'T', -1.   &   ! caution gradient ==> the sign changes
+         &              , psxx, 'T',  1.,  psyy, 'T',  1.   &
+         &              , psxy, 'T',  1. )
 
       IF(ln_ctl) THEN
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limcat_1D.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limcat_1D.F90	(revision 4712)
+++ 	(revision )
@@ -1,215 +1,0 @@
-MODULE limcat_1D
-   !!======================================================================
-   !!                     ***  MODULE  limcat_1D  ***
-   !!  Used for LIM3 to convert cell averages of ice thickness, snow thickness
-   !!  and ice cover into a prescribed distribution over the cell. 
-   !!  (Example of application: BDY forcings when input are cell averaged)  
-   !!======================================================================
-   !! History :   -   ! Original code from M. Vancoppenolle (?)
-   !!                 ! 2011-12 (C. Rousset) rewritten for clarity
-   !!----------------------------------------------------------------------
-#if defined key_lim3
-   !!----------------------------------------------------------------------
-   !!   'key_lim3' :                                    LIM3 sea-ice model
-   !!----------------------------------------------------------------------
-   !!   lim_cat_1D      :  main subroutine
-   !!----------------------------------------------------------------------
-   !! Modules used
-   USE phycst
-   USE oce             ! dynamics and tracers variables
-   USE dom_oce
-   USE sbc_oce         ! Surface boundary condition: ocean fields
-   USE par_ice         ! ice parameters
-   USE ice             ! ice variables
-   USE eosbn2          ! equation of state
-   USE in_out_manager
-   USE dom_ice
-   USE ice
-   USE lbclnk
-   USE timing          ! Timing
-
-   IMPLICIT NONE
-   PRIVATE
-
-   !! Accessibility
-   PUBLIC lim_cat_1D      
-
-CONTAINS
-
-   SUBROUTINE lim_cat_1D(zhti,zhts,zai,zht_i,zht_s,za_i)
-      !! Local variables
-      INTEGER  :: ji, jk, jl             ! dummy loop indices
-      INTEGER  :: ijpij, i_fill, jl0, ztest_1, ztest_2, ztest_3, ztest_4, ztests  
-      REAL(wp) :: zarg, zV, zconv
-      REAL(wp), DIMENSION(:),   INTENT(in)    ::   zhti, zhts, zai    ! input ice/snow variables
-      REAL(wp), DIMENSION(:,:), INTENT(inout) ::   zht_i, zht_s, za_i ! output ice/snow variables
-      REAL(wp) :: epsi06 = 1.0e-6
-      REAL(wp) :: zc1, zc2, zc3, zx1, zdh   ! local scalars
-      REAL(wp), DIMENSION(0:jpl)   ::   zhi_max         !:Boundary of ice thickness categories in thickness space
- 
-      IF( nn_timing == 1 )  CALL timing_start('limcat_1D')
-      !--------------------------------------------------------------------
-      ! initialisation of variables
-      !--------------------------------------------------------------------
-      ijpij = SIZE(zhti,1)
-      zht_i(1:ijpij,1:jpl) = 0._wp
-      zht_s(1:ijpij,1:jpl) = 0._wp
-      za_i (1:ijpij,1:jpl) = 0._wp
-
-      !------------------------------------------------------------------------------------
-      ! Distribute ice concentration and thickness into the categories
-      !------------------------------------------------------------------------------------
-      ! Method: we first try to fill the jpl ice categories bounded by thicknesses
-      !         hmax(0:jpl) with a gaussian distribution, and check whether the distribution
-      !         fulfills volume and area conservation, positivity and ice categories bounds.
-      !         In other words, if ice input is too thin, the last category (jpl)
-      !         cannot be filled, so we try to fill jpl-1 categories...
-      !         And so forth iteratively until the number of categories filled
-      !         fulfills ice volume concervation between input and output (ztests=4) 
-      !--------------------------------------------------------------------------------------
-
-      !- Thickness categories boundaries
-      !    hi_max is calculated in iceini.F90 but since limcat_1D.F90 routine 
-      !    is called before (in bdydta.F90), one must recalculate it.   
-      !    Note clem: there may be a way of doing things cleaner
-      !----------------------------------
-      zhi_max(:) = 0._wp
-      zc1 =  3._wp / REAL( jpl , wp ) ; zc2 = 10._wp * zc1 ; zc3 =  3._wp
-      DO jl = 1, jpl
-         zx1 = REAL( jl-1 , wp ) / REAL( jpl , wp )
-         zhi_max(jl) = zhi_max(jl-1) + zc1 + zc2 * ( 1._wp + TANH( zc3 * ( zx1 - 1._wp ) ) )
-      END DO
- 
-      ! ----------------------------------------
-      ! distribution over the jpl ice categories
-      ! ----------------------------------------
-      DO ji = 1, ijpij
-         
-         IF( zhti(ji) > 0._wp ) THEN
-
-         ! initialisation of tests
-         ztest_1 = 0
-         ztest_2 = 0
-         ztest_3 = 0
-         ztest_4 = 0
-         ztests  = 0
-         
-         i_fill = jpl + 1                                    !====================================
-         DO WHILE ( ( ztests /= 4 ) .AND. ( i_fill >= 2 ) )  ! iterative loop on i_fill categories  
-            ! iteration                                      !====================================
-            i_fill = i_fill - 1
-            
-            ! initialisation of ice variables for each try
-            zht_i(ji,1:jpl) = 0._wp
-            za_i (ji,1:jpl) = 0._wp
-            
-            ! *** case very thin ice: fill only category 1
-            IF ( i_fill == 1 ) THEN
-               zht_i(ji,1) = zhti(ji)
-               za_i (ji,1) = zai (ji)
-               ! *** case ice is thicker: fill categories >1
-            ELSE
-
-               ! Fill ice thicknesses except the last one (i_fill) by (hmax-hmin)/2 
-               DO jl = 1, i_fill - 1
-                  zht_i(ji,jl) = ( zhi_max(jl) + zhi_max(jl-1) ) * 0.5_wp
-               END DO
-               
-               ! find which category (jl0) the input ice thickness falls into
-               jl0 = i_fill
-               DO jl = 1, i_fill
-                  IF ( ( zhti(ji) >= zhi_max(jl-1) ) .AND. ( zhti(ji) < zhi_max(jl) ) ) THEN
-                     jl0 = jl
-		     CYCLE
-                  ENDIF
-               END DO
-               
-               ! Concentrations in the (i_fill-1) categories 
-               za_i(ji,jl0) = zai(ji) / SQRT(REAL(jpl))
-               DO jl = 1, i_fill - 1
-                  IF ( jl == jl0 ) CYCLE
-                  zarg           = ( zht_i(ji,jl) - zhti(ji) ) / ( zhti(ji) * 0.5_wp )
-                  za_i(ji,jl) =   za_i (ji,jl0) * EXP(-zarg**2)
-               END DO
-               
-               ! Concentration in the last (i_fill) category
-               za_i(ji,i_fill) = zai(ji) - SUM( za_i(ji,1:i_fill-1) )
-               
-               ! Ice thickness in the last (i_fill) category
-               zV = SUM( za_i(ji,1:i_fill-1) * zht_i(ji,1:i_fill-1) )
-               zht_i(ji,i_fill) = ( zhti(ji)*zai(ji) -  zV ) / za_i(ji,i_fill) 
-               
-            ENDIF ! case ice is thick or thin
-            
-            !---------------------
-            ! Compatibility tests
-            !--------------------- 
-            ! Test 1: area conservation
-            zconv = ABS( zai(ji) - SUM( za_i(ji,1:jpl) ) )
-            IF ( zconv < epsi06 ) ztest_1 = 1
-            
-            ! Test 2: volume conservation
-            zconv = ABS( zhti(ji)*zai(ji) - SUM( za_i(ji,1:jpl)*zht_i(ji,1:jpl) ) )
-            IF ( zconv < epsi06 ) ztest_2 = 1
-            
-            ! Test 3: thickness of the last category is in-bounds ?
-            IF ( zht_i(ji,i_fill) >= zhi_max(i_fill-1) ) ztest_3 = 1
-            
-            ! Test 4: positivity of ice concentrations
-            ztest_4 = 1
-            DO jl = 1, i_fill
-               IF ( za_i(ji,jl) < 0._wp ) ztest_4 = 0
-            END DO
-            
-            ztests = ztest_1 + ztest_2 + ztest_3 + ztest_4
-                                                           !============================
-         END DO                                            ! end iteration on categories
-                                                           !============================
-         ! Check if tests have passed (i.e. volume conservation...)
-         !IF ( ztests /= 4 ) THEN
-         !   WRITE(numout,*) ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! '
-         !   WRITE(numout,*) ' !! ALERT categories distribution !!'
-         !   WRITE(numout,*) ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! '
-         !   WRITE(numout,*) ' *** ztests is not equal to 4 '
-         !   WRITE(numout,*) ' *** ztest (1:4) = ', ztest_1, ztest_2, ztest_3, ztest_4
-         !   WRITE(numout,*) 'i_fill=',i_fill
-         !   WRITE(numout,*) 'zai(ji)=',zai(ji)
-         !   WRITE(numout,*) 'za_i(ji,jpl)=',za_i(ji,:)
-         !ENDIF
-         
-         ENDIF ! if zhti > 0
-
-      END DO ! i loop
-
-      ! ------------------------------------------------
-      ! Adding Snow in each category where za_i is not 0
-      ! ------------------------------------------------ 
-      DO jl = 1, jpl
-         DO ji = 1, ijpij
-            IF( za_i(ji,jl) > 0._wp ) THEN
-               zht_s(ji,jl) = zht_i(ji,jl) * ( zhts(ji) / zhti(ji) )
-               ! In case snow load is in excess that would lead to transformation from snow to ice
-               ! Then, transfer the snow excess into the ice (different from limthd_dh)
-               zdh = MAX( 0._wp, ( rhosn * zht_s(ji,jl) + ( rhoic - rau0 ) * zht_i(ji,jl) ) * r1_rau0 ) 
-               ! recompute ht_i, ht_s avoiding out of bounds values
-               zht_i(ji,jl) = MIN( zhi_max(jl), zht_i(ji,jl) + zdh )
-               zht_s(ji,jl) = MAX( 0._wp, zht_s(ji,jl) - zdh * rhoic / rhosn )
-            ENDIF
-         ENDDO
-      ENDDO
-
-      IF( nn_timing == 1 )  CALL timing_stop('limcat_1D')
-     
-    END SUBROUTINE lim_cat_1D
-    
-#else
-   !!----------------------------------------------------------------------
-   !!   Default option :         Empty module          NO LIM sea-ice model
-   !!----------------------------------------------------------------------
-CONTAINS
-   SUBROUTINE lim_cat_1D          ! Empty routine
-   END SUBROUTINE lim_cat_1D
-#endif
-
-   !!======================================================================
-END MODULE limcat_1D
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limcons.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limcons.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limcons.F90	(revision 5832)
@@ -6,5 +6,7 @@
    !! History :   -   ! Original code from William H. Lipscomb, LANL
    !!            3.0  ! 2004-06  (M. Vancoppenolle)   Energy Conservation 
-   !!            4.0  ! 2011-02  (G. Madec)  add mpp considerations
+   !!            3.5  ! 2011-02  (G. Madec)  add mpp considerations
+   !!             -   ! 2014-05  (C. Rousset) add lim_cons_hsm
+   !!             -   ! 2015-03  (C. Rousset) add lim_cons_final
    !!----------------------------------------------------------------------
 #if defined key_lim3
@@ -14,5 +16,5 @@
    !!    lim_cons     :   checks whether energy, mass and salt are conserved 
    !!----------------------------------------------------------------------
-   USE par_ice        ! LIM-3 parameter
+   USE phycst         ! physical constants
    USE ice            ! LIM-3 variables
    USE dom_ice        ! LIM-3 domain
@@ -21,4 +23,5 @@
    USE lib_mpp        ! MPP library
    USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
+   USE sbc_oce , ONLY : sfx  ! Surface boundary condition: ocean fields
 
    IMPLICIT NONE
@@ -28,4 +31,6 @@
    PUBLIC   lim_column_sum_energy
    PUBLIC   lim_cons_check
+   PUBLIC   lim_cons_hsm
+   PUBLIC   lim_cons_final
 
    !!----------------------------------------------------------------------
@@ -70,8 +75,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,jpl), INTENT(in   ) ::   pin    !: input field
+      REAL(wp), DIMENSION(jpi,jpj)           , INTENT(  out) ::   pout   !: output field
       !
       INTEGER ::   jk, jl   ! dummy loop indices
@@ -151,4 +156,153 @@
    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 ice routine
+      !!                     + test if ice concentration and volume are > 0
+      !!
+      !! ** Method  : This is an online diagnostics which can be activated with ln_limdiahsb=true
+      !!              It prints in ocean.output if there is a violation of conservation at each time-step
+      !!              The thresholds (zv_sill, zs_sill, zh_sill) which determine violations are set to
+      !!              a minimum of 1 mm of ice (over the ice area) that is lost/gained spuriously during 100 years.
+      !!              For salt and heat thresholds, ice is considered to have a salinity of 10 
+      !!              and a heat content of 3e5 J/kg (=latent heat of fusion) 
+      !!--------------------------------------------------------------------------------------------------------
+      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 
+      REAL(wp)                        :: zvtrp, zetrp
+      REAL(wp)                        :: zarea, zv_sill, zs_sill, zh_sill
+      REAL(wp), PARAMETER             :: zconv = 1.e-9 ! convert W to GW and kg to Mt
+
+      IF( icount == 0 ) THEN
+
+         ! salt flux
+         zfs_b  = glob_sum(  ( sfx_bri(:,:) + sfx_bog(:,:) + sfx_bom(:,:) + sfx_sum(:,:) + sfx_sni(:,:) +  &
+            &                  sfx_opw(:,:) + sfx_res(:,:) + sfx_dyn(:,:)                                  &
+            &                ) *  e12t(:,:) * tmask(:,:,1) * zconv )
+
+         ! water flux
+         zfw_b  = glob_sum( -( wfx_bog(:,:) + wfx_bom(:,:) + wfx_sum(:,:) + wfx_sni(:,:) + wfx_opw(:,:) +  &
+            &                  wfx_res(:,:) + wfx_dyn(:,:) + wfx_snw(:,:) + wfx_sub(:,:) + wfx_spr(:,:)    &
+            &                ) *  e12t(:,:) * tmask(:,:,1) * zconv )
+
+         ! heat flux
+         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(:,:)   &
+            &                ) *  e12t(:,:) * tmask(:,:,1) * zconv )
+
+         zvi_b  = glob_sum( SUM( v_i * rhoic + v_s * rhosn, dim=3 ) * e12t * tmask(:,:,1) * zconv )
+
+         zsmv_b = glob_sum( SUM( smv_i * rhoic            , dim=3 ) * e12t * tmask(:,:,1) * zconv )
+
+         zei_b  = glob_sum( ( SUM( SUM( e_i(:,:,1:nlay_i,:), dim=4 ), dim=3 ) +  &
+            &                 SUM( SUM( e_s(:,:,1:nlay_s,:), dim=4 ), dim=3 )    &
+                            ) * e12t * tmask(:,:,1) * zconv )
+
+      ELSEIF( icount == 1 ) THEN
+
+         ! salt flux
+         zfs  = glob_sum(  ( sfx_bri(:,:) + sfx_bog(:,:) + sfx_bom(:,:) + sfx_sum(:,:) + sfx_sni(:,:) +  &
+            &                sfx_opw(:,:) + sfx_res(:,:) + sfx_dyn(:,:)                                  & 
+            &              ) * e12t(:,:) * tmask(:,:,1) * zconv ) - zfs_b
+
+         ! water flux
+         zfw  = glob_sum( -( wfx_bog(:,:) + wfx_bom(:,:) + wfx_sum(:,:) + wfx_sni(:,:) + wfx_opw(:,:) +  &
+            &                wfx_res(:,:) + wfx_dyn(:,:) + wfx_snw(:,:) + wfx_sub(:,:) + wfx_spr(:,:)    &
+            &              ) * e12t(:,:) * tmask(:,:,1) * zconv ) - zfw_b
+
+         ! heat flux
+         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(:,:)   &
+            &              ) * e12t(:,:) * tmask(:,:,1) * zconv ) - zft_b
+ 
+         ! outputs
+         zvi  = ( ( glob_sum( SUM( v_i * rhoic + v_s * rhosn, dim=3 )  &
+            &                    * e12t * tmask(:,:,1) * zconv ) - zvi_b ) * r1_rdtice - zfw ) * rday
+
+         zsmv = ( ( glob_sum( SUM( smv_i * rhoic            , dim=3 )  &
+            &                    * e12t * tmask(:,:,1) * zconv ) - zsmv_b ) * r1_rdtice + zfs ) * rday
+
+         zei  =   glob_sum( ( SUM( SUM( e_i(:,:,1:nlay_i,:), dim=4 ), dim=3 ) +  &
+            &                 SUM( SUM( e_s(:,:,1:nlay_s,:), dim=4 ), dim=3 )    &
+            &                ) * e12t * tmask(:,:,1) * zconv ) * r1_rdtice - zei_b * r1_rdtice + zft
+
+         ! zvtrp and zetrp must be close to 0 if the advection scheme is conservative
+         zvtrp = glob_sum( ( diag_trp_vi * rhoic + diag_trp_vs * rhosn ) * e12t * tmask(:,:,1) * zconv ) * rday 
+         zetrp = glob_sum( ( diag_trp_ei         + diag_trp_es         ) * e12t * tmask(:,:,1) * zconv )
+
+         zvmin = glob_min( v_i )
+         zamax = glob_max( SUM( a_i, dim=3 ) )
+         zamin = glob_min( a_i )
+
+         ! set threshold values and calculate the ice area (+epsi10 to set a threshold > 0 when there is no ice) 
+         zarea   = glob_sum( SUM( a_i + epsi10, dim=3 ) * e12t * zconv ) ! in 1.e9 m2
+         zv_sill = zarea * 2.5e-5
+         zs_sill = zarea * 25.e-5
+         zh_sill = zarea * 10.e-5
+
+         IF(lwp) THEN
+            IF ( ABS( zvi  ) > zv_sill ) WRITE(numout,*) 'violation volume [Mt/day]     (',cd_routine,') = ',zvi
+            IF ( ABS( zsmv ) > zs_sill ) WRITE(numout,*) 'violation saline [psu*Mt/day] (',cd_routine,') = ',zsmv
+            IF ( ABS( zei  ) > zh_sill ) WRITE(numout,*) 'violation enthalpy [GW]       (',cd_routine,') = ',zei
+            IF ( ABS(zvtrp ) > zv_sill .AND. cd_routine == 'limtrp' ) THEN
+                                         WRITE(numout,*) 'violation vtrp [Mt/day]       (',cd_routine,') = ',zvtrp
+                                         WRITE(numout,*) 'violation etrp [GW]           (',cd_routine,') = ',zetrp
+            ENDIF
+            IF (     zvmin   < -epsi10 ) WRITE(numout,*) 'violation v_i<0  [m]          (',cd_routine,') = ',zvmin
+            IF (     zamax   > rn_amax+epsi10 .AND. cd_routine /= 'limtrp' .AND. cd_routine /= 'limitd_me' ) THEN
+                                         WRITE(numout,*) 'violation a_i>amax            (',cd_routine,') = ',zamax
+            ENDIF
+            IF (      zamin  < -epsi10 ) WRITE(numout,*) 'violation a_i<0               (',cd_routine,') = ',zamin
+         ENDIF
+
+      ENDIF
+
+   END SUBROUTINE lim_cons_hsm
+
+   SUBROUTINE lim_cons_final( cd_routine )
+      !!---------------------------------------------------------------------------------------------------------
+      !!                                   ***  ROUTINE lim_cons_final ***
+      !!
+      !! ** Purpose : Test the conservation of heat, salt and mass at the end of each ice time-step
+      !!
+      !! ** Method  : This is an online diagnostics which can be activated with ln_limdiahsb=true
+      !!              It prints in ocean.output if there is a violation of conservation at each time-step
+      !!              The thresholds (zv_sill, zs_sill, zh_sill) which determine the violation are set to
+      !!              a minimum of 1 mm of ice (over the ice area) that is lost/gained spuriously during 100 years.
+      !!              For salt and heat thresholds, ice is considered to have a salinity of 10 
+      !!              and a heat content of 3e5 J/kg (=latent heat of fusion) 
+      !!--------------------------------------------------------------------------------------------------------
+      CHARACTER(len=*), INTENT(in)    :: cd_routine    ! name of the routine
+      REAL(wp)                        :: zhfx, zsfx, zvfx
+      REAL(wp)                        :: zarea, zv_sill, zs_sill, zh_sill
+      REAL(wp), PARAMETER             :: zconv = 1.e-9 ! convert W to GW and kg to Mt
+
+#if ! defined key_bdy
+      ! heat flux
+      zhfx  = glob_sum( ( hfx_in - hfx_out - diag_heat - diag_trp_ei - diag_trp_es - hfx_sub ) * e12t * tmask(:,:,1) * zconv ) 
+      ! salt flux
+      zsfx  = glob_sum( ( sfx + diag_smvi ) * e12t * tmask(:,:,1) * zconv ) * rday
+      ! water flux
+      zvfx  = glob_sum( ( wfx_ice + wfx_snw + wfx_spr + wfx_sub + diag_vice + diag_vsnw ) * e12t * tmask(:,:,1) * zconv ) * rday
+
+      ! set threshold values and calculate the ice area (+epsi10 to set a threshold > 0 when there is no ice) 
+      zarea   = glob_sum( SUM( a_i + epsi10, dim=3 ) * e12t * zconv ) ! in 1.e9 m2
+      zv_sill = zarea * 2.5e-5
+      zs_sill = zarea * 25.e-5
+      zh_sill = zarea * 10.e-5
+
+      IF( ABS( zvfx ) > zv_sill ) WRITE(numout,*) 'violation vfx    [Mt/day]       (',cd_routine,')  = ',(zvfx)
+      IF( ABS( zsfx ) > zs_sill ) WRITE(numout,*) 'violation sfx    [psu*Mt/day]   (',cd_routine,')  = ',(zsfx)
+      IF( ABS( zhfx ) > zh_sill ) WRITE(numout,*) 'violation hfx    [GW]           (',cd_routine,')  = ',(zhfx)
+#endif
+
+   END SUBROUTINE lim_cons_final
+
 #else
    !!----------------------------------------------------------------------
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limctl.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limctl.F90	(revision 5832)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limctl.F90	(revision 5832)
@@ -0,0 +1,458 @@
+MODULE limctl
+   !!======================================================================
+   !!                     ***  MODULE  limctl  ***
+   !!   LIM-3 : control and printing
+   !!======================================================================
+   !! History :  3.5  !  2015-01  (M. Vancoppenolle) Original code
+   !!----------------------------------------------------------------------
+#if defined key_lim3
+   !!----------------------------------------------------------------------
+   !!   'key_lim3'                                      LIM3 sea-ice model
+   !!----------------------------------------------------------------------
+   !!    lim_ctl   : control prints in case of crash
+   !!    lim_prt   : ice control print at a given grid point
+   !!----------------------------------------------------------------------
+   USE oce             ! ocean dynamics and tracers
+   USE dom_oce         ! ocean space and time domain
+   USE ice             ! LIM-3: ice variables
+   USE thd_ice         ! LIM-3: thermodynamical variables
+   USE dom_ice         ! LIM-3: ice domain
+   USE sbc_oce         ! Surface boundary condition: ocean fields
+   USE sbc_ice         ! Surface boundary condition: ice   fields
+
+   USE phycst          ! Define parameters for the routines
+
+   USE lib_mpp         ! MPP library
+   USE wrk_nemo        ! work arrays
+   USE timing          ! Timing
+   USE in_out_manager  ! I/O manager
+   USE prtctl          ! Print control
+   USE lib_fortran     ! 
+
+   IMPLICIT NONE
+   PRIVATE
+
+   PUBLIC   lim_ctl
+   PUBLIC   lim_prt
+
+   !! * Substitutions
+#  include "vectopt_loop_substitute.h90"
+   !!----------------------------------------------------------------------
+   !! NEMO/LIM3 4.0 , UCL - NEMO Consortium (2011)
+   !! $Id: limctl.F90 5043 2015-01-28 16:44:18Z clem $
+   !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
+   !!----------------------------------------------------------------------
+
+CONTAINS
+
+   SUBROUTINE lim_ctl( kt )
+      !!-----------------------------------------------------------------------
+      !!                   ***  ROUTINE lim_ctl *** 
+      !!                 
+      !! ** Purpose :   Alerts in case of model crash
+      !!-------------------------------------------------------------------
+      INTEGER, INTENT(in) ::   kt      ! ocean time step
+      INTEGER  ::   ji, jj, jk,  jl   ! dummy loop indices
+      INTEGER  ::   inb_altests       ! number of alert tests (max 20)
+      INTEGER  ::   ialert_id         ! number of the current alert
+      REAL(wp) ::   ztmelts           ! ice layer melting point
+      CHARACTER (len=30), DIMENSION(20)      ::   cl_alname   ! name of alert
+      INTEGER           , DIMENSION(20)      ::   inb_alp     ! number of alerts positive
+      !!-------------------------------------------------------------------
+
+      inb_altests = 10
+      inb_alp(:)  =  0
+
+      ! Alert if incompatible volume and concentration
+      ialert_id = 2 ! reference number of this alert
+      cl_alname(ialert_id) = ' Incompat vol and con         '    ! name of the alert
+
+      DO jl = 1, jpl
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               IF(  v_i(ji,jj,jl) /= 0._wp   .AND.   a_i(ji,jj,jl) == 0._wp   ) THEN
+                  !WRITE(numout,*) ' ALERTE 2 :   Incompatible volume and concentration '
+                  !WRITE(numout,*) ' at_i     ', at_i(ji,jj)
+                  !WRITE(numout,*) ' Point - category', ji, jj, jl
+                  !WRITE(numout,*) ' a_i *** a_i_b   ', a_i      (ji,jj,jl), a_i_b  (ji,jj,jl)
+                  !WRITE(numout,*) ' v_i *** v_i_b   ', v_i      (ji,jj,jl), v_i_b  (ji,jj,jl)
+                  inb_alp(ialert_id) = inb_alp(ialert_id) + 1
+               ENDIF
+            END DO
+         END DO
+      END DO
+
+      ! Alerte if very thick ice
+      ialert_id = 3 ! reference number of this alert
+      cl_alname(ialert_id) = ' Very thick ice               ' ! name of the alert
+      jl = jpl 
+      DO jj = 1, jpj
+         DO ji = 1, jpi
+            IF(   ht_i(ji,jj,jl)  >  50._wp   ) THEN
+               !CALL lim_prt( kt, ji, jj, 2, ' ALERTE 3 :   Very thick ice ' )
+               inb_alp(ialert_id) = inb_alp(ialert_id) + 1
+            ENDIF
+         END DO
+      END DO
+
+      ! Alert if very fast ice
+      ialert_id = 4 ! reference number of this alert
+      cl_alname(ialert_id) = ' Very fast ice               ' ! name of the alert
+      DO jj = 1, jpj
+         DO ji = 1, jpi
+            IF(   MAX( ABS( u_ice(ji,jj) ), ABS( v_ice(ji,jj) ) ) > 1.5  .AND.  &
+               &  at_i(ji,jj) > 0._wp   ) THEN
+               !CALL lim_prt( kt, ji, jj, 1, ' ALERTE 4 :   Very fast ice ' )
+               !WRITE(numout,*) ' ice strength             : ', strength(ji,jj)
+               !WRITE(numout,*) ' oceanic stress utau      : ', utau(ji,jj) 
+               !WRITE(numout,*) ' oceanic stress vtau      : ', vtau(ji,jj)
+               !WRITE(numout,*) ' sea-ice stress utau_ice  : ', utau_ice(ji,jj) 
+               !WRITE(numout,*) ' sea-ice stress vtau_ice  : ', vtau_ice(ji,jj)
+               !WRITE(numout,*) ' oceanic speed u          : ', u_oce(ji,jj)
+               !WRITE(numout,*) ' oceanic speed v          : ', v_oce(ji,jj)
+               !WRITE(numout,*) ' sst                      : ', sst_m(ji,jj)
+               !WRITE(numout,*) ' sss                      : ', sss_m(ji,jj)
+               !WRITE(numout,*) 
+               inb_alp(ialert_id) = inb_alp(ialert_id) + 1
+            ENDIF
+         END DO
+      END DO
+
+      ! Alert if there is ice on continents
+      ialert_id = 6 ! reference number of this alert
+      cl_alname(ialert_id) = ' Ice on continents           ' ! name of the alert
+      DO jj = 1, jpj
+         DO ji = 1, jpi
+            IF(   tmask(ji,jj,1) <= 0._wp   .AND.   at_i(ji,jj) > 0._wp   ) THEN 
+               !CALL lim_prt( kt, ji, jj, 1, ' ALERTE 6 :   Ice on continents ' )
+               !WRITE(numout,*) ' masks s, u, v        : ', tmask(ji,jj,1), umask(ji,jj,1), vmask(ji,jj,1) 
+               !WRITE(numout,*) ' sst                  : ', sst_m(ji,jj)
+               !WRITE(numout,*) ' sss                  : ', sss_m(ji,jj)
+               !WRITE(numout,*) ' at_i(ji,jj)          : ', at_i(ji,jj)
+               !WRITE(numout,*) ' v_ice(ji,jj)         : ', v_ice(ji,jj)
+               !WRITE(numout,*) ' v_ice(ji,jj-1)       : ', v_ice(ji,jj-1)
+               !WRITE(numout,*) ' u_ice(ji-1,jj)       : ', u_ice(ji-1,jj)
+               !WRITE(numout,*) ' u_ice(ji,jj)         : ', v_ice(ji,jj)
+               !
+               inb_alp(ialert_id) = inb_alp(ialert_id) + 1
+            ENDIF
+         END DO
+      END DO
+
+!
+!     ! Alert if very fresh ice
+      ialert_id = 7 ! reference number of this alert
+      cl_alname(ialert_id) = ' Very fresh ice               ' ! name of the alert
+      DO jl = 1, jpl
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               IF( sm_i(ji,jj,jl) < 0.1 .AND. a_i(ji,jj,jl) > 0._wp ) THEN
+!                 CALL lim_prt(kt,ji,jj,1, ' ALERTE 7 :   Very fresh ice ' )
+!                 WRITE(numout,*) ' sst                  : ', sst_m(ji,jj)
+!                 WRITE(numout,*) ' sss                  : ', sss_m(ji,jj)
+!                 WRITE(numout,*) 
+                  inb_alp(ialert_id) = inb_alp(ialert_id) + 1
+               ENDIF
+            END DO
+         END DO
+      END DO
+!
+
+!     ! Alert if too old ice
+      ialert_id = 9 ! reference number of this alert
+      cl_alname(ialert_id) = ' Very old   ice               ' ! name of the alert
+      DO jl = 1, jpl
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               IF ( ( ( ABS( o_i(ji,jj,jl) ) > rdt_ice ) .OR. &
+                      ( ABS( o_i(ji,jj,jl) ) < 0._wp) ) .AND. &
+                             ( a_i(ji,jj,jl) > 0._wp ) ) THEN
+                  !CALL lim_prt( kt, ji, jj, 1, ' ALERTE 9 :   Wrong ice age ')
+                  inb_alp(ialert_id) = inb_alp(ialert_id) + 1
+               ENDIF
+            END DO
+         END DO
+      END DO
+ 
+      ! Alert on salt flux
+      ialert_id = 5 ! reference number of this alert
+      cl_alname(ialert_id) = ' High salt flux               ' ! name of the alert
+      DO jj = 1, jpj
+         DO ji = 1, jpi
+            IF( ABS( sfx (ji,jj) ) > 1.0e-2 ) THEN  ! = 1 psu/day for 1m ocean depth
+               !CALL lim_prt( kt, ji, jj, 3, ' ALERTE 5 :   High salt flux ' )
+               !DO jl = 1, jpl
+                  !WRITE(numout,*) ' Category no: ', jl
+                  !WRITE(numout,*) ' a_i        : ', a_i      (ji,jj,jl) , ' a_i_b      : ', a_i_b  (ji,jj,jl)   
+                  !WRITE(numout,*) ' v_i        : ', v_i      (ji,jj,jl) , ' v_i_b      : ', v_i_b  (ji,jj,jl)   
+                  !WRITE(numout,*) ' '
+               !END DO
+               inb_alp(ialert_id) = inb_alp(ialert_id) + 1
+            ENDIF
+         END DO
+      END DO
+
+      ! Alert if qns very big
+      ialert_id = 8 ! reference number of this alert
+      cl_alname(ialert_id) = ' fnsolar very big             ' ! name of the alert
+      DO jj = 1, jpj
+         DO ji = 1, jpi
+            IF( ABS( qns(ji,jj) ) > 1500._wp .AND. at_i(ji,jj) > 0._wp ) THEN
+               !
+               !WRITE(numout,*) ' ALERTE 8 :   Very high non-solar heat flux'
+               !WRITE(numout,*) ' ji, jj    : ', ji, jj
+               !WRITE(numout,*) ' qns       : ', qns(ji,jj)
+               !WRITE(numout,*) ' sst       : ', sst_m(ji,jj)
+               !WRITE(numout,*) ' sss       : ', sss_m(ji,jj)
+               !
+               !CALL lim_prt( kt, ji, jj, 2, '   ')
+               inb_alp(ialert_id) = inb_alp(ialert_id) + 1
+               !
+            ENDIF
+         END DO
+      END DO
+      !+++++
+ 
+      ! Alert if very warm ice
+      ialert_id = 10 ! reference number of this alert
+      cl_alname(ialert_id) = ' Very warm ice                ' ! name of the alert
+      inb_alp(ialert_id) = 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) + rt0
+                  IF( t_i(ji,jj,jk,jl) >= ztmelts  .AND.  v_i(ji,jj,jl) > 1.e-10   &
+                     &                             .AND.  a_i(ji,jj,jl) > 0._wp   ) THEN
+                     !WRITE(numout,*) ' ALERTE 10 :   Very warm ice'
+                     !WRITE(numout,*) ' ji, jj, jk, jl : ', ji, jj, jk, jl
+                     !WRITE(numout,*) ' t_i : ', t_i(ji,jj,jk,jl)
+                     !WRITE(numout,*) ' e_i : ', e_i(ji,jj,jk,jl)
+                     !WRITE(numout,*) ' s_i : ', s_i(ji,jj,jk,jl)
+                     !WRITE(numout,*) ' ztmelts : ', ztmelts
+                     inb_alp(ialert_id) = inb_alp(ialert_id) + 1
+                  ENDIF
+               END DO
+            END DO
+         END DO
+      END DO
+
+      ! sum of the alerts on all processors
+      IF( lk_mpp ) THEN
+         DO ialert_id = 1, inb_altests
+            CALL mpp_sum(inb_alp(ialert_id))
+         END DO
+      ENDIF
+
+      ! print alerts
+      IF( lwp ) THEN
+         ialert_id = 1                                 ! reference number of this alert
+         cl_alname(ialert_id) = ' NO alerte 1      '   ! name of the alert
+         WRITE(numout,*) ' time step ',kt
+         WRITE(numout,*) ' All alerts at the end of ice model '
+         DO ialert_id = 1, inb_altests
+            WRITE(numout,*) ialert_id, cl_alname(ialert_id)//' : ', inb_alp(ialert_id), ' times ! '
+         END DO
+      ENDIF
+     !
+   END SUBROUTINE lim_ctl
+ 
+   
+   SUBROUTINE lim_prt( kt, ki, kj, kn, cd1 )
+      !!-----------------------------------------------------------------------
+      !!                   ***  ROUTINE lim_prt *** 
+      !!                 
+      !! ** Purpose :   Writes global ice state on the (i,j) point 
+      !!                in ocean.ouput 
+      !!                3 possibilities exist 
+      !!                n = 1/-1 -> simple ice state (plus Mechanical Check if -1)
+      !!                n = 2    -> exhaustive state
+      !!                n = 3    -> ice/ocean salt fluxes
+      !!
+      !! ** input   :   point coordinates (i,j) 
+      !!                n : number of the option
+      !!-------------------------------------------------------------------
+      INTEGER         , INTENT(in) ::   kt            ! ocean time step
+      INTEGER         , INTENT(in) ::   ki, kj, kn    ! ocean gridpoint indices
+      CHARACTER(len=*), INTENT(in) ::   cd1           !
+      !!
+      INTEGER :: jl, ji, jj
+      !!-------------------------------------------------------------------
+
+      DO ji = mi0(ki), mi1(ki)
+         DO jj = mj0(kj), mj1(kj)
+
+            WRITE(numout,*) ' time step ',kt,' ',cd1             ! print title
+
+            !----------------
+            !  Simple state
+            !----------------
+            
+            IF ( kn == 1 .OR. kn == -1 ) THEN
+               WRITE(numout,*) ' lim_prt - Point : ',ji,jj
+               WRITE(numout,*) ' ~~~~~~~~~~~~~~ '
+               WRITE(numout,*) ' Simple state '
+               WRITE(numout,*) ' masks s,u,v   : ', tmask(ji,jj,1), umask(ji,jj,1), vmask(ji,jj,1)
+               WRITE(numout,*) ' lat - long    : ', gphit(ji,jj), glamt(ji,jj)
+               WRITE(numout,*) ' Time step     : ', numit
+               WRITE(numout,*) ' - Ice drift   '
+               WRITE(numout,*) '   ~~~~~~~~~~~ '
+               WRITE(numout,*) ' u_ice(i-1,j)  : ', u_ice(ji-1,jj)
+               WRITE(numout,*) ' u_ice(i  ,j)  : ', u_ice(ji,jj)
+               WRITE(numout,*) ' v_ice(i  ,j-1): ', v_ice(ji,jj-1)
+               WRITE(numout,*) ' v_ice(i  ,j)  : ', v_ice(ji,jj)
+               WRITE(numout,*) ' strength      : ', strength(ji,jj)
+               WRITE(numout,*)
+               WRITE(numout,*) ' - Cell values '
+               WRITE(numout,*) '   ~~~~~~~~~~~ '
+               WRITE(numout,*) ' cell area     : ', e12t(ji,jj)
+               WRITE(numout,*) ' at_i          : ', at_i(ji,jj)       
+               WRITE(numout,*) ' vt_i          : ', vt_i(ji,jj)       
+               WRITE(numout,*) ' vt_s          : ', vt_s(ji,jj)       
+               DO jl = 1, jpl
+                  WRITE(numout,*) ' - Category (', jl,')'
+                  WRITE(numout,*) ' a_i           : ', a_i(ji,jj,jl)
+                  WRITE(numout,*) ' ht_i          : ', ht_i(ji,jj,jl)
+                  WRITE(numout,*) ' ht_s          : ', ht_s(ji,jj,jl)
+                  WRITE(numout,*) ' v_i           : ', v_i(ji,jj,jl)
+                  WRITE(numout,*) ' v_s           : ', v_s(ji,jj,jl)
+                  WRITE(numout,*) ' e_s           : ', e_s(ji,jj,1,jl)
+                  WRITE(numout,*) ' e_i           : ', e_i(ji,jj,1:nlay_i,jl)
+                  WRITE(numout,*) ' t_su          : ', t_su(ji,jj,jl)
+                  WRITE(numout,*) ' t_snow        : ', t_s(ji,jj,1,jl)
+                  WRITE(numout,*) ' t_i           : ', t_i(ji,jj,1:nlay_i,jl)
+                  WRITE(numout,*) ' sm_i          : ', sm_i(ji,jj,jl)
+                  WRITE(numout,*) ' smv_i         : ', smv_i(ji,jj,jl)
+                  WRITE(numout,*)
+               END DO
+            ENDIF
+            IF( kn == -1 ) THEN
+               WRITE(numout,*) ' Mechanical Check ************** '
+               WRITE(numout,*) ' Check what means ice divergence '
+               WRITE(numout,*) ' Total ice concentration ', at_i (ji,jj)
+               WRITE(numout,*) ' Total lead fraction     ', ato_i(ji,jj)
+               WRITE(numout,*) ' Sum of both             ', ato_i(ji,jj) + at_i(ji,jj)
+               WRITE(numout,*) ' Sum of both minus 1     ', ato_i(ji,jj) + at_i(ji,jj) - 1.00
+            ENDIF
+            
+
+            !--------------------
+            !  Exhaustive state
+            !--------------------
+            
+            IF ( kn .EQ. 2 ) THEN
+               WRITE(numout,*) ' lim_prt - Point : ',ji,jj
+               WRITE(numout,*) ' ~~~~~~~~~~~~~~ '
+               WRITE(numout,*) ' Exhaustive state '
+               WRITE(numout,*) ' lat - long ', gphit(ji,jj), glamt(ji,jj)
+               WRITE(numout,*) ' Time step ', numit
+               WRITE(numout,*) 
+               WRITE(numout,*) ' - Cell values '
+               WRITE(numout,*) '   ~~~~~~~~~~~ '
+               WRITE(numout,*) ' cell area     : ', e12t(ji,jj)
+               WRITE(numout,*) ' at_i          : ', at_i(ji,jj)       
+               WRITE(numout,*) ' vt_i          : ', vt_i(ji,jj)       
+               WRITE(numout,*) ' vt_s          : ', vt_s(ji,jj)       
+               WRITE(numout,*) ' u_ice(i-1,j)  : ', u_ice(ji-1,jj)
+               WRITE(numout,*) ' u_ice(i  ,j)  : ', u_ice(ji,jj)
+               WRITE(numout,*) ' v_ice(i  ,j-1): ', v_ice(ji,jj-1)
+               WRITE(numout,*) ' v_ice(i  ,j)  : ', v_ice(ji,jj)
+               WRITE(numout,*) ' strength      : ', strength(ji,jj)
+               WRITE(numout,*) ' u_ice_b       : ', u_ice_b(ji,jj)    , ' v_ice_b       : ', v_ice_b(ji,jj)  
+               WRITE(numout,*)
+               
+               DO jl = 1, jpl
+                  WRITE(numout,*) ' - Category (',jl,')'
+                  WRITE(numout,*) '   ~~~~~~~~         ' 
+                  WRITE(numout,*) ' ht_i       : ', ht_i(ji,jj,jl)             , ' ht_s       : ', ht_s(ji,jj,jl)
+                  WRITE(numout,*) ' t_i        : ', t_i(ji,jj,1:nlay_i,jl)
+                  WRITE(numout,*) ' t_su       : ', t_su(ji,jj,jl)             , ' t_s        : ', t_s(ji,jj,1,jl)
+                  WRITE(numout,*) ' sm_i       : ', sm_i(ji,jj,jl)             , ' o_i        : ', o_i(ji,jj,jl)
+                  WRITE(numout,*) ' a_i        : ', a_i(ji,jj,jl)              , ' a_i_b      : ', a_i_b(ji,jj,jl)   
+                  WRITE(numout,*) ' v_i        : ', v_i(ji,jj,jl)              , ' v_i_b      : ', v_i_b(ji,jj,jl)   
+                  WRITE(numout,*) ' v_s        : ', v_s(ji,jj,jl)              , ' v_s_b      : ', v_s_b(ji,jj,jl)  
+                  WRITE(numout,*) ' e_i1       : ', e_i(ji,jj,1,jl)            , ' ei1        : ', e_i_b(ji,jj,1,jl) 
+                  WRITE(numout,*) ' e_i2       : ', e_i(ji,jj,2,jl)            , ' ei2_b      : ', e_i_b(ji,jj,2,jl)  
+                  WRITE(numout,*) ' e_snow     : ', e_s(ji,jj,1,jl)            , ' e_snow_b   : ', e_s_b(ji,jj,1,jl) 
+                  WRITE(numout,*) ' smv_i      : ', smv_i(ji,jj,jl)            , ' smv_i_b    : ', smv_i_b(ji,jj,jl)   
+                  WRITE(numout,*) ' oa_i       : ', oa_i(ji,jj,jl)             , ' oa_i_b     : ', oa_i_b(ji,jj,jl)
+               END DO !jl
+               
+               WRITE(numout,*)
+               WRITE(numout,*) ' - Heat / FW fluxes '
+               WRITE(numout,*) '   ~~~~~~~~~~~~~~~~ '
+               WRITE(numout,*) ' - Heat fluxes in and out the ice ***'
+               WRITE(numout,*) ' qsr_ini       : ', pfrld(ji,jj) * qsr(ji,jj) + SUM( a_i_b(ji,jj,:) * qsr_ice(ji,jj,:) )
+               WRITE(numout,*) ' qns_ini       : ', pfrld(ji,jj) * qns(ji,jj) + SUM( a_i_b(ji,jj,:) * qns_ice(ji,jj,:) )
+               WRITE(numout,*)
+               WRITE(numout,*) 
+               WRITE(numout,*) ' sst        : ', sst_m(ji,jj)  
+               WRITE(numout,*) ' sss        : ', sss_m(ji,jj)  
+               WRITE(numout,*) 
+               WRITE(numout,*) ' - Stresses '
+               WRITE(numout,*) '   ~~~~~~~~ '
+               WRITE(numout,*) ' utau_ice   : ', utau_ice(ji,jj) 
+               WRITE(numout,*) ' vtau_ice   : ', vtau_ice(ji,jj)
+               WRITE(numout,*) ' utau       : ', utau    (ji,jj) 
+               WRITE(numout,*) ' vtau       : ', vtau    (ji,jj)
+               WRITE(numout,*) ' oc. vel. u : ', u_oce   (ji,jj)
+               WRITE(numout,*) ' oc. vel. v : ', v_oce   (ji,jj)
+            ENDIF
+            
+            !---------------------
+            ! Salt / heat fluxes
+            !---------------------
+            
+            IF ( kn .EQ. 3 ) THEN
+               WRITE(numout,*) ' lim_prt - Point : ',ji,jj
+               WRITE(numout,*) ' ~~~~~~~~~~~~~~ '
+               WRITE(numout,*) ' - Salt / Heat Fluxes '
+               WRITE(numout,*) '   ~~~~~~~~~~~~~~~~ '
+               WRITE(numout,*) ' lat - long ', gphit(ji,jj), glamt(ji,jj)
+               WRITE(numout,*) ' Time step ', numit
+               WRITE(numout,*)
+               WRITE(numout,*) ' - Heat fluxes at bottom interface ***'
+               WRITE(numout,*) ' qsr       : ', qsr(ji,jj)
+               WRITE(numout,*) ' qns       : ', qns(ji,jj)
+               WRITE(numout,*)
+               WRITE(numout,*) ' hfx_mass     : ', hfx_thd(ji,jj) + hfx_dyn(ji,jj) + hfx_snw(ji,jj) + hfx_res(ji,jj)
+               WRITE(numout,*) ' hfx_in       : ', hfx_in(ji,jj)
+               WRITE(numout,*) ' hfx_out      : ', hfx_out(ji,jj)
+               WRITE(numout,*) ' dhc          : ', diag_heat(ji,jj)              
+               WRITE(numout,*)
+               WRITE(numout,*) ' hfx_dyn      : ', hfx_dyn(ji,jj)
+               WRITE(numout,*) ' hfx_thd      : ', hfx_thd(ji,jj)
+               WRITE(numout,*) ' hfx_res      : ', hfx_res(ji,jj)
+               WRITE(numout,*) ' fhtur        : ', fhtur(ji,jj) 
+               WRITE(numout,*) ' qlead        : ', qlead(ji,jj) * r1_rdtice
+               WRITE(numout,*)
+               WRITE(numout,*) ' - Salt fluxes at bottom interface ***'
+               WRITE(numout,*) ' emp       : ', emp    (ji,jj)
+               WRITE(numout,*) ' sfx       : ', sfx    (ji,jj)
+               WRITE(numout,*) ' sfx_res   : ', sfx_res(ji,jj)
+               WRITE(numout,*) ' sfx_bri   : ', sfx_bri(ji,jj)
+               WRITE(numout,*) ' sfx_dyn   : ', sfx_dyn(ji,jj)
+               WRITE(numout,*)
+               WRITE(numout,*) ' - Momentum fluxes '
+               WRITE(numout,*) ' utau      : ', utau(ji,jj) 
+               WRITE(numout,*) ' vtau      : ', vtau(ji,jj)
+            ENDIF 
+            WRITE(numout,*) ' '
+            !
+         END DO
+      END DO
+      !
+   END SUBROUTINE lim_prt
+
+#else
+   !!--------------------------------------------------------------------------
+   !!   Default option         Empty Module               No LIM3 sea-ice model
+   !!--------------------------------------------------------------------------
+CONTAINS
+   SUBROUTINE lim_ctl     ! Empty routine
+   END SUBROUTINE lim_ctl
+   SUBROUTINE lim_prt     ! Empty routine
+   END SUBROUTINE lim_prt
+#endif
+   !!======================================================================
+END MODULE limctl
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limdiahsb.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limdiahsb.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limdiahsb.F90	(revision 5832)
@@ -14,8 +14,8 @@
    !!----------------------------------------------------------------------
    USE ice             ! LIM-3: sea-ice variable
-   USE par_ice         ! LIM-3: ice parameters
    USE dom_ice         ! LIM-3: sea-ice domain
    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
@@ -31,12 +31,7 @@
 
    PUBLIC   lim_diahsb        ! routine called by ice_step.F90
-   !!PUBLIC   lim_diahsb_init   ! routine called by ice_init.F90
-   !!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
@@ -45,5 +40,5 @@
    !!----------------------------------------------------------------------
    !! NEMO/OPA 3.4 , NEMO Consortium (2012)
-   !! $Id: limdiahsb.F90 3294 2012-10-18 16:44:18Z rblod $
+   !! $Id$
    !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
    !!----------------------------------------------------------------------
@@ -59,10 +54,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,44 +71,72 @@
 
       ! 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( e12t(:,:) * tmask(:,:,1) ), epsi06 )
+
+      rswitch = MAX( 0._wp , SIGN( 1._wp , glob_sum( e12t(:,:) * tmask(:,:,1) ) - epsi06 ) )
       ! ----------------------- !
       ! 1 -  Content variations !
       ! ----------------------- !
-      zbg_ivo = glob_sum( vt_i(:,:) * area(:,:) * tms(:,:) ) ! volume ice 
-      zbg_svo = glob_sum( vt_s(:,:) * area(:,:) * tms(:,:) ) ! volume snow
-      zbg_are = glob_sum( at_i(:,:) * area(:,:) * tms(:,:) ) ! area
-      zbg_sal = glob_sum( SUM( smv_i(:,:,:), dim=3 ) * area(:,:) * tms(:,:) )       ! mean salt content
-      zbg_tem = glob_sum( ( tm_i(:,:) - rtt ) * vt_i(:,:) * area(:,:) * tms(:,:) )  ! mean temp content
-
-      !zbg_ihc = glob_sum( et_i(:,:) * area(:,:) * tms(:,:) ) / MAX( zbg_ivo,epsi06 ) ! ice heat content
-      !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
-      
+      zbg_ivo = glob_sum( vt_i(:,:) * e12t(:,:) * tmask(:,:,1) ) ! volume ice 
+      zbg_svo = glob_sum( vt_s(:,:) * e12t(:,:) * tmask(:,:,1) ) ! volume snow
+      zbg_are = glob_sum( at_i(:,:) * e12t(:,:) * tmask(:,:,1) ) ! area
+      zbg_sal = glob_sum( SUM( smv_i(:,:,:), dim=3 ) * e12t(:,:) * tmask(:,:,1) )       ! mean salt content
+      zbg_tem = glob_sum( ( tm_i(:,:) - rt0 ) * vt_i(:,:) * e12t(:,:) * tmask(:,:,1) )  ! mean temp content
+
+      !zbg_ihc = glob_sum( et_i(:,:) * e12t(:,:) * tmask(:,:,1) ) / MAX( zbg_ivo,epsi06 ) ! ice heat content
+      !zbg_shc = glob_sum( et_s(:,:) * e12t(:,:) * tmask(:,:,1) ) / MAX( zbg_svo,epsi06 ) ! snow heat content
+
+      ! Volume
+      ztmp = rswitch * z1_area * r1_rau0 * rday
+      zbg_vfx     = ztmp * glob_sum(     emp(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_vfx_bog = ztmp * glob_sum( wfx_bog(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_vfx_opw = ztmp * glob_sum( wfx_opw(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_vfx_sni = ztmp * glob_sum( wfx_sni(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_vfx_dyn = ztmp * glob_sum( wfx_dyn(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_vfx_bom = ztmp * glob_sum( wfx_bom(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_vfx_sum = ztmp * glob_sum( wfx_sum(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_vfx_res = ztmp * glob_sum( wfx_res(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_vfx_spr = ztmp * glob_sum( wfx_spr(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_vfx_snw = ztmp * glob_sum( wfx_snw(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_vfx_sub = ztmp * glob_sum( wfx_sub(:,:) * e12t(:,:) * tmask(:,:,1) )
+
+      ! Salt
+      zbg_sfx     = ztmp * glob_sum(     sfx(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_sfx_bri = ztmp * glob_sum( sfx_bri(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_sfx_res = ztmp * glob_sum( sfx_res(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_sfx_dyn = ztmp * glob_sum( sfx_dyn(:,:) * e12t(:,:) * tmask(:,:,1) )
+
+      zbg_sfx_bog = ztmp * glob_sum( sfx_bog(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_sfx_opw = ztmp * glob_sum( sfx_opw(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_sfx_sni = ztmp * glob_sum( sfx_sni(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_sfx_bom = ztmp * glob_sum( sfx_bom(:,:) * e12t(:,:) * tmask(:,:,1) )
+      zbg_sfx_sum = ztmp * glob_sum( sfx_sum(:,:) * e12t(:,:) * tmask(:,:,1) )
+
+      ! Heat budget
+      zbg_ihc      = glob_sum( et_i(:,:) * e12t(:,:) * 1.e-20 ) ! ice heat content  [1.e20 J]
+      zbg_shc      = glob_sum( et_s(:,:) * e12t(:,:) * 1.e-20 ) ! snow heat content [1.e20 J]
+      zbg_hfx_dhc  = glob_sum( diag_heat(:,:) * e12t(:,:) * tmask(:,:,1) ) ! [in W]
+      zbg_hfx_spr  = glob_sum( hfx_spr(:,:) * e12t(:,:) * tmask(:,:,1) ) ! [in W]
+
+      zbg_hfx_thd  = glob_sum( hfx_thd(:,:) * e12t(:,:) * tmask(:,:,1) ) ! [in W]
+      zbg_hfx_dyn  = glob_sum( hfx_dyn(:,:) * e12t(:,:) * tmask(:,:,1) ) ! [in W]
+      zbg_hfx_res  = glob_sum( hfx_res(:,:) * e12t(:,:) * tmask(:,:,1) ) ! [in W]
+      zbg_hfx_sub  = glob_sum( hfx_sub(:,:) * e12t(:,:) * tmask(:,:,1) ) ! [in W]
+      zbg_hfx_snw  = glob_sum( hfx_snw(:,:) * e12t(:,:) * tmask(:,:,1) ) ! [in W]
+      zbg_hfx_sum  = glob_sum( hfx_sum(:,:) * e12t(:,:) * tmask(:,:,1) ) ! [in W]
+      zbg_hfx_bom  = glob_sum( hfx_bom(:,:) * e12t(:,:) * tmask(:,:,1) ) ! [in W]
+      zbg_hfx_bog  = glob_sum( hfx_bog(:,:) * e12t(:,:) * tmask(:,:,1) ) ! [in W]
+      zbg_hfx_dif  = glob_sum( hfx_dif(:,:) * e12t(:,:) * tmask(:,:,1) ) ! [in W]
+      zbg_hfx_opw  = glob_sum( hfx_opw(:,:) * e12t(:,:) * tmask(:,:,1) ) ! [in W]
+      zbg_hfx_out  = glob_sum( hfx_out(:,:) * e12t(:,:) * tmask(:,:,1) ) ! [in W]
+      zbg_hfx_in   = glob_sum(  hfx_in(:,:) * e12t(:,:) * tmask(:,:,1) ) ! [in W]
+    
       ! --------------------------------------------- !
       ! 2 - Trends due to forcing and ice growth/melt !
       ! --------------------------------------------- !
-      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_frc_vol = r1_rau0 * glob_sum( - emp(:,:) * e12t(:,:) * tmask(:,:,1) ) ! volume fluxes
+      z_frc_sal = r1_rau0 * glob_sum(   sfx(:,:) * e12t(:,:) * tmask(:,:,1) ) ! salt fluxes
+      z_bg_grme = glob_sum( - ( wfx_bog(:,:) + wfx_opw(:,:) + wfx_sni(:,:) + wfx_dyn(:,:) + &
+                          &     wfx_bom(:,:) + wfx_sum(:,:) + wfx_res(:,:) + wfx_snw(:,:) + &
+                          &     wfx_sub(:,:) ) * e12t(:,:) * tmask(:,:,1) ) ! volume fluxes
       !
       frc_vol  = frc_vol  + z_frc_vol  * rdt_ice
@@ -123,33 +151,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' )
@@ -186,11 +245,4 @@
          WRITE(numout,*) '~~~~~~~~~~~~'
       ENDIF
-
-      ! ---------------------------------- !
-      ! 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
       !
       CALL lim_diahsb_rst( nstart, 'READ' )  !* read or initialize all required files
@@ -226,8 +278,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_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limdyn.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limdyn.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limdyn.F90	(revision 5832)
@@ -6,5 +6,5 @@
    !! history :  1.0  ! 2002-08  (C. Ethe, G. Madec)  original VP code 
    !!            3.0  ! 2007-03  (MA Morales Maqueda, S. Bouillon, M. Vancoppenolle)  LIM3: EVP-Cgrid
-   !!            4.0  ! 2011-02  (G. Madec) dynamical allocation
+   !!            3.5  ! 2011-02  (G. Madec) dynamical allocation
    !!----------------------------------------------------------------------
 #if defined key_lim3
@@ -20,5 +20,4 @@
    USE sbc_ice          ! Surface boundary condition: ice   fields
    USE ice              ! LIM-3 variables
-   USE par_ice          ! LIM-3 parameters
    USE dom_ice          ! LIM-3 domain
    USE limrhg           ! LIM-3 rheology
@@ -30,4 +29,6 @@
    USE lib_fortran      ! glob_sum
    USE timing          ! Timing
+   USE limcons        ! conservation tests
+   USE limvar
 
    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,7 @@
 
       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 )
+
+      CALL lim_var_agg(1)             ! aggregate ice categories
 
       IF( kt == nit000 )   CALL lim_dyn_init   ! Initialization (first time-step only)
@@ -90,6 +82,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(:,:) * umask(:,:,1)
+         v_ice_b(:,:) = v_ice(:,:) * vmask(:,:,1)
 
          ! Rheology (ice dynamics)
@@ -107,6 +102,6 @@
             !
             DO jj = 1, jpj
-               zind(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
+               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 +112,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 +122,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 +134,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 +140,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
@@ -164,19 +159,22 @@
          zv_io(:,:) = v_ice(:,:) - ssv_m(:,:)
          ! frictional velocity at T-point
-         zcoef = 0.5_wp * cw
+         zcoef = 0.5_wp * rn_cio
          DO jj = 2, jpjm1 
             DO ji = fs_2, fs_jpim1   ! vector opt.
                ust2s(ji,jj) = zcoef * (  zu_io(ji,jj) * zu_io(ji,jj) + zu_io(ji-1,jj) * zu_io(ji-1,jj)   &
-                  &                    + zv_io(ji,jj) * zv_io(ji,jj) + zv_io(ji,jj-1) * zv_io(ji,jj-1)   ) * tms(ji,jj)
+                  &                    + zv_io(ji,jj) * zv_io(ji,jj) + zv_io(ji,jj-1) * zv_io(ji,jj-1) ) * tmask(ji,jj,1)
             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
          !
-         zcoef = SQRT( 0.5_wp ) / rau0
+         zcoef = SQRT( 0.5_wp ) * r1_rau0
          DO jj = 2, jpjm1
             DO ji = fs_2, fs_jpim1   ! vector opt.
                ust2s(ji,jj) = zcoef * SQRT(  utau(ji,jj) * utau(ji,jj) + utau(ji-1,jj) * utau(ji-1,jj)   &
-                  &                        + vtau(ji,jj) * vtau(ji,jj) + vtau(ji,jj-1) * vtau(ji,jj-1)   ) * tms(ji,jj)
+                  &                        + vtau(ji,jj) * vtau(ji,jj) + vtau(ji,jj-1) * vtau(ji,jj-1) ) * tmask(ji,jj,1)
             END DO
          END DO
@@ -193,5 +191,5 @@
          CALL prt_ctl(tab2d_1=delta_i   , clinfo1=' lim_dyn  : delta_i   :')
          CALL prt_ctl(tab2d_1=strength  , clinfo1=' lim_dyn  : strength  :')
-         CALL prt_ctl(tab2d_1=area      , clinfo1=' lim_dyn  : cell area :')
+         CALL prt_ctl(tab2d_1=e12t      , clinfo1=' lim_dyn  : cell area :')
          CALL prt_ctl(tab2d_1=at_i      , clinfo1=' lim_dyn  : at_i      :')
          CALL prt_ctl(tab2d_1=vt_i      , clinfo1=' lim_dyn  : vt_i      :')
@@ -224,30 +222,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 +243,8 @@
       !!-------------------------------------------------------------------
       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/ nn_icestr, ln_icestr_bvf, rn_pe_rdg, rn_pstar, rn_crhg, rn_cio,  rn_creepl, rn_ecc, &
+         &                nn_nevp, rn_relast, nn_ahi0, rn_ahi0_ref
+      INTEGER  ::   ji, jj
+      REAL(wp) ::   za00, zd_max
       !!-------------------------------------------------------------------
 
@@ -288,42 +262,64 @@
          WRITE(numout,*) 'lim_dyn_init : ice parameters for ice dynamics '
          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,*) '   coefficient for the solution of int. stresses  alphaevp = ', alphaevp
-         WRITE(numout,*) '   min ice thickness for rheology calculations     hminrhg = ', hminrhg
+         WRITE(numout,*)'    ice strength parameterization (0=Hibler 1=Rothrock)  nn_icestr     = ', nn_icestr 
+         WRITE(numout,*)'    Including brine volume in ice strength comp.         ln_icestr_bvf = ', ln_icestr_bvf
+         WRITE(numout,*)'   Ratio of ridging work to PotEner change in ridging    rn_pe_rdg     = ', rn_pe_rdg 
+         WRITE(numout,*) '   drag coefficient for oceanic stress                  rn_cio        = ', rn_cio
+         WRITE(numout,*) '   first bulk-rheology parameter                        rn_pstar      = ', rn_pstar
+         WRITE(numout,*) '   second bulk-rhelogy parameter                        rn_crhg       = ', rn_crhg
+         WRITE(numout,*) '   creep limit                                          rn_creepl     = ', rn_creepl
+         WRITE(numout,*) '   eccentricity of the elliptical yield curve           rn_ecc        = ', rn_ecc
+         WRITE(numout,*) '   number of iterations for subcycling                  nn_nevp       = ', nn_nevp
+         WRITE(numout,*) '   ratio of elastic timescale over ice time step        rn_relast     = ', rn_relast
+         WRITE(numout,*) '   horizontal diffusivity calculation                   nn_ahi0       = ', nn_ahi0
+         WRITE(numout,*) '   horizontal diffusivity coeff. (orca2 grid)           rn_ahi0_ref   = ', rn_ahi0_ref
       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
-
-      !  Diffusion coefficients.
-      ahiu(:,:) = ahi0 * umask(:,:,1)
-      ahiv(:,:) = ahi0 * vmask(:,:,1)
-      !
+      usecc2 = 1._wp / ( rn_ecc * rn_ecc )
+      rhoco  = rau0  * rn_cio
+      !
+      !  Diffusion coefficients
+      SELECT CASE( nn_ahi0 )
+
+      CASE( 0 )
+         ahiu(:,:) = rn_ahi0_ref
+         ahiv(:,:) = rn_ahi0_ref
+
+         IF(lwp) WRITE(numout,*) ''
+         IF(lwp) WRITE(numout,*) '   laplacian operator: ahim constant = rn_ahi0_ref'
+
+      CASE( 1 ) 
+
+         zd_max = MAX( MAXVAL( e1t(:,:) ), MAXVAL( e2t(:,:) ) )
+         IF( lk_mpp )   CALL mpp_max( zd_max )          ! max over the global domain
+         
+         ahiu(:,:) = rn_ahi0_ref * zd_max * 1.e-05_wp   ! 1.e05 = 100km = max grid space at 60° latitude in orca2
+                                                        !                    (60° = min latitude for ice cover)  
+         ahiv(:,:) = rn_ahi0_ref * zd_max * 1.e-05_wp
+
+         IF(lwp) WRITE(numout,*) ''
+         IF(lwp) WRITE(numout,*) '   laplacian operator: ahim proportional to max of e1 e2 over the domain (', zd_max, ')'
+         IF(lwp) WRITE(numout,*) '   value for ahim = ', rn_ahi0_ref * zd_max * 1.e-05_wp 
+         
+      CASE( 2 ) 
+
+         zd_max = MAX( MAXVAL( e1t(:,:) ), MAXVAL( e2t(:,:) ) )
+         IF( lk_mpp )   CALL mpp_max( zd_max )   ! max over the global domain
+         
+         za00 = rn_ahi0_ref * 1.e-05_wp          ! 1.e05 = 100km = max grid space at 60° latitude in orca2
+                                                 !                    (60° = min latitude for ice cover)  
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               ahiu(ji,jj) = za00 * MAX( e1t(ji,jj), e2t(ji,jj) ) * umask(ji,jj,1)
+               ahiv(ji,jj) = za00 * MAX( e1f(ji,jj), e2f(ji,jj) ) * vmask(ji,jj,1)
+            END DO
+         END DO
+         !
+         IF(lwp) WRITE(numout,*) ''
+         IF(lwp) WRITE(numout,*) '   laplacian operator: ahim proportional to e1'
+         IF(lwp) WRITE(numout,*) '   maximum grid-spacing = ', zd_max, ' maximum value for ahim = ', za00*zd_max
+         
+      END SELECT
+
    END SUBROUTINE lim_dyn_init
 
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limhdf.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limhdf.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limhdf.F90	(revision 5832)
@@ -13,4 +13,5 @@
    !!----------------------------------------------------------------------
    !!   lim_hdf       : diffusion trend on sea-ice variable
+   !!   lim_hdf_init  : initialisation of diffusion trend on sea-ice variable
    !!----------------------------------------------------------------------
    USE dom_oce        ! ocean domain
@@ -26,8 +27,9 @@
    PRIVATE
 
-   PUBLIC   lim_hdf     ! called by lim_tra
-
-   LOGICAL  ::   linit = .TRUE.              ! initialization flag (set to flase after the 1st call)
-   REAL(wp) ::   epsi04 = 1.e-04              ! constant
+   PUBLIC   lim_hdf         ! called by lim_trp
+   PUBLIC   lim_hdf_init    ! called by sbc_lim_init
+
+   LOGICAL  ::   linit = .TRUE.                             ! initialization flag (set to flase after the 1st call)
+   INTEGER  ::   nn_convfrq                                 !:  convergence check frequency of the Crant-Nicholson scheme
    REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:) ::   efact   ! metric coefficient
 
@@ -54,9 +56,12 @@
       REAL(wp), DIMENSION(jpi,jpj), INTENT( inout ) ::   ptab    ! Field on which the diffusion is applied
       !
-      INTEGER  ::  ji, jj                   ! dummy loop indices
-      INTEGER  ::  its, iter, ierr          ! local integers
-      REAL(wp) ::   zalfa, zrlxint, zconv   ! local scalars
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zrlx, zflu, zflv, zdiv0, zdiv, ztab0
-      CHARACTER(lc) ::   charout   ! local character
+      INTEGER                           ::  ji, jj                    ! dummy loop indices
+      INTEGER                           ::  iter, ierr           ! local integers
+      REAL(wp)                          ::  zrlxint, zconv     ! local scalars
+      REAL(wp), POINTER, DIMENSION(:,:) ::  zrlx, zflu, zflv, zdiv0, zdiv, ztab0
+      CHARACTER(lc)                     ::  charout                   ! local character
+      REAL(wp), PARAMETER               ::  zrelax = 0.5_wp           ! relaxation constant for iterative procedure
+      REAL(wp), PARAMETER               ::  zalfa  = 0.5_wp           ! =1.0/0.5/0.0 = implicit/Cranck-Nicholson/explicit
+      INTEGER , PARAMETER               ::  its    = 100              ! Maximum number of iteration
       !!-------------------------------------------------------------------
       
@@ -71,5 +76,5 @@
          DO jj = 2, jpjm1  
             DO ji = fs_2 , fs_jpim1   ! vector opt.
-               efact(ji,jj) = ( e2u(ji,jj) + e2u(ji-1,jj) + e1v(ji,jj) + e1v(ji,jj-1) ) / ( e1t(ji,jj) * e2t(ji,jj) )
+               efact(ji,jj) = ( e2u(ji,jj) + e2u(ji-1,jj) + e1v(ji,jj) + e1v(ji,jj-1) ) * r1_e12t(ji,jj)
             END DO
          END DO
@@ -77,21 +82,17 @@
       ENDIF
       !                             ! Time integration parameters
-      zalfa = 0.5_wp                      ! =1.0/0.5/0.0 = implicit/Cranck-Nicholson/explicit
-      its   = 100                         ! Maximum number of iteration
       !
       ztab0(:, : ) = ptab(:,:)      ! Arrays initialization
       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  ==!
       iter  = 0
       !
-      DO WHILE( zconv > ( 2._wp * epsi04 ) .AND. iter <= its )   ! Sub-time step loop
+      DO WHILE( zconv > ( 2._wp * 1.e-04 ) .AND. iter <= its )   ! Sub-time step loop
          !
          iter = iter + 1                                 ! incrementation of the sub-time step number
@@ -99,6 +100,6 @@
          DO jj = 1, jpjm1                                ! diffusive fluxes in U- and V- direction
             DO ji = 1 , fs_jpim1   ! vector opt.
-               zflu(ji,jj) = pahu(ji,jj) * e2u(ji,jj) / e1u(ji,jj) * ( ptab(ji+1,jj) - ptab(ji,jj) )
-               zflv(ji,jj) = pahv(ji,jj) * e1v(ji,jj) / e2v(ji,jj) * ( ptab(ji,jj+1) - ptab(ji,jj) )
+               zflu(ji,jj) = pahu(ji,jj) * e2u(ji,jj) * r1_e1u(ji,jj) * ( ptab(ji+1,jj) - ptab(ji,jj) )
+               zflv(ji,jj) = pahv(ji,jj) * e1v(ji,jj) * r1_e2v(ji,jj) * ( ptab(ji,jj+1) - ptab(ji,jj) )
             END DO
          END DO
@@ -106,6 +107,5 @@
          DO jj= 2, jpjm1                                 ! diffusive trend : divergence of the fluxes
             DO ji = fs_2 , fs_jpim1   ! vector opt. 
-               zdiv (ji,jj) = (  zflu(ji,jj) - zflu(ji-1,jj  )   &
-                  &            + zflv(ji,jj) - zflv(ji  ,jj-1)  ) / ( e1t (ji,jj) * e2t (ji,jj) )
+               zdiv(ji,jj) = ( zflu(ji,jj) - zflu(ji-1,jj) + zflv(ji,jj) - zflv(ji,jj-1) ) * r1_e12t(ji,jj)
             END DO
          END DO
@@ -117,18 +117,20 @@
                zrlxint = (   ztab0(ji,jj)    &
                   &       +  rdt_ice * (           zalfa   * ( zdiv(ji,jj) + efact(ji,jj) * ptab(ji,jj) )   &
-                  &                      + ( 1.0 - zalfa ) *   zdiv0(ji,jj) )  )                             & 
-                  &    / ( 1.0 + zalfa * rdt_ice * efact(ji,jj) )
-               zrlx(ji,jj) = ptab(ji,jj) + om * ( zrlxint - ptab(ji,jj) )
+                  &                      + ( 1.0 - zalfa ) *   zdiv0(ji,jj) )                               & 
+                  &      ) / ( 1.0 + zalfa * rdt_ice * efact(ji,jj) )
+               zrlx(ji,jj) = ptab(ji,jj) + zrelax * ( zrlxint - ptab(ji,jj) )
             END DO
          END DO
          CALL lbc_lnk( zrlx, 'T', 1. )                   ! lateral boundary condition
          !
-         zconv = 0._wp                                   ! convergence test
-         DO jj = 2, jpjm1
-            DO ji = fs_2, fs_jpim1
-               zconv = MAX( zconv, ABS( zrlx(ji,jj) - ptab(ji,jj) )  )
-            END DO
-         END DO
-         IF( lk_mpp )   CALL mpp_max( zconv )            ! max over the global domain
+         IF ( MOD( iter, nn_convfrq ) == 0 )  THEN    ! convergence test every nn_convfrq iterations (perf. optimization)
+            zconv = 0._wp
+            DO jj = 2, jpjm1
+               DO ji = fs_2, fs_jpim1
+                  zconv = MAX( zconv, ABS( zrlx(ji,jj) - ptab(ji,jj) )  )
+               END DO
+            END DO
+            IF( lk_mpp )   CALL mpp_max( zconv )      ! max over the global domain
+         ENDIF
          !
          ptab(:,:) = zrlx(:,:)
@@ -140,6 +142,6 @@
       DO jj = 1, jpjm1                                ! diffusive fluxes in U- and V- direction
          DO ji = 1 , fs_jpim1   ! vector opt.
-            zflu(ji,jj) = pahu(ji,jj) * e2u(ji,jj) / e1u(ji,jj) * ( ptab(ji+1,jj) - ptab(ji,jj) )
-            zflv(ji,jj) = pahv(ji,jj) * e1v(ji,jj) / e2v(ji,jj) * ( ptab(ji,jj+1) - ptab(ji,jj) )
+            zflu(ji,jj) = pahu(ji,jj) * e2u(ji,jj) * r1_e1u(ji,jj) * ( ptab(ji+1,jj) - ptab(ji,jj) )
+            zflv(ji,jj) = pahv(ji,jj) * e1v(ji,jj) * r1_e2v(ji,jj) * ( ptab(ji,jj+1) - ptab(ji,jj) )
          END DO
       END DO
@@ -147,6 +149,5 @@
       DO jj= 2, jpjm1                                 ! diffusive trend : divergence of the fluxes
          DO ji = fs_2 , fs_jpim1   ! vector opt. 
-            zdiv (ji,jj) = (  zflu(ji,jj) - zflu(ji-1,jj  )   &
-                 &            + zflv(ji,jj) - zflv(ji  ,jj-1)  ) / ( e1t (ji,jj) * e2t (ji,jj) )
+            zdiv(ji,jj) = ( zflu(ji,jj) - zflu(ji-1,jj) + zflv(ji,jj) - zflv(ji,jj-1) ) * r1_e12t(ji,jj)
             ptab(ji,jj) = ztab0(ji,jj) + 0.5 * ( zdiv(ji,jj) + zdiv0(ji,jj) )
          END DO
@@ -166,11 +167,45 @@
    END SUBROUTINE lim_hdf
 
+   
+   SUBROUTINE lim_hdf_init
+      !!-------------------------------------------------------------------
+      !!                  ***  ROUTINE lim_hdf_init  ***
+      !!
+      !! ** Purpose : Initialisation of horizontal diffusion of sea-ice 
+      !!
+      !! ** Method  : Read the namicehdf namelist
+      !!
+      !! ** input   : Namelist namicehdf
+      !!-------------------------------------------------------------------
+      INTEGER  ::   ios                 ! Local integer output status for namelist read
+      NAMELIST/namicehdf/ nn_convfrq
+      !!-------------------------------------------------------------------
+      !
+      IF(lwp) THEN
+         WRITE(numout,*)
+         WRITE(numout,*) 'lim_hdf : Ice horizontal diffusion'
+         WRITE(numout,*) '~~~~~~~'
+      ENDIF
+      !
+      REWIND( numnam_ice_ref )              ! Namelist namicehdf in reference namelist : Ice horizontal diffusion
+      READ  ( numnam_ice_ref, namicehdf, IOSTAT = ios, ERR = 901)
+901   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namicehdf in reference namelist', lwp )
+
+      REWIND( numnam_ice_cfg )              ! Namelist namicehdf in configuration namelist : Ice horizontal diffusion
+      READ  ( numnam_ice_cfg, namicehdf, IOSTAT = ios, ERR = 902 )
+902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namicehdf in configuration namelist', lwp )
+      IF(lwm) WRITE ( numoni, namicehdf )
+      !
+      IF(lwp) THEN                          ! control print
+         WRITE(numout,*)
+         WRITE(numout,*)'   Namelist of ice parameters for ice horizontal diffusion computation '
+         WRITE(numout,*)'      convergence check frequency of the Crant-Nicholson scheme   nn_convfrq   = ', nn_convfrq
+      ENDIF
+      !
+   END SUBROUTINE lim_hdf_init
 #else
    !!----------------------------------------------------------------------
    !!   Default option          Dummy module           NO LIM sea-ice model
    !!----------------------------------------------------------------------
-CONTAINS
-   SUBROUTINE lim_hdf         ! Empty routine
-   END SUBROUTINE lim_hdf
 #endif
 
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limistate.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limistate.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limistate.F90	(revision 5832)
@@ -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
@@ -22,9 +22,7 @@
    USE eosbn2           ! equation of state
    USE ice              ! sea-ice variables
-   USE par_ice          ! ice parameters
    USE par_oce          ! ocean parameters
    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 +34,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) ::   rn_thres_sst   ! threshold water temperature for initial sea ice
+   REAL(wp) ::   rn_hts_ini_n   ! initial snow thickness in the north
+   REAL(wp) ::   rn_hts_ini_s   ! initial snow thickness in the south
+   REAL(wp) ::   rn_hti_ini_n   ! initial ice thickness in the north
+   REAL(wp) ::   rn_hti_ini_s   ! initial ice thickness in the south
+   REAL(wp) ::   rn_ati_ini_n   ! initial leads area in the north
+   REAL(wp) ::   rn_ati_ini_s   ! initial leads area in the south
+   REAL(wp) ::   rn_smi_ini_n   ! initial salinity 
+   REAL(wp) ::   rn_smi_ini_s   ! initial salinity
+   REAL(wp) ::   rn_tmi_ini_n   ! initial temperature
+   REAL(wp) ::   rn_tmi_ini_s   ! initial temperature
+
+   LOGICAL  ::  ln_iceini    ! initialization or not
    !!----------------------------------------------------------------------
    !!   LIM 3.0,  UCL-LOCEAN-IPSL (2008)
@@ -53,5 +53,4 @@
    !! Software governed by the CeCILL licence (modipsl/doc/NEMO_CeCILL.txt)
    !!----------------------------------------------------------------------
-
 CONTAINS
 
@@ -77,5 +76,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 :
@@ -87,19 +86,18 @@
       !! * Local variables
       INTEGER    :: ji, jj, jk, jl             ! dummy loop indices
-      REAL(wp)   :: epsi20, ztmelts, zdh
+      REAL(wp)   :: ztmelts, zdh
       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 )
+
       IF(lwp) WRITE(numout,*)
       IF(lwp) WRITE(numout,*) 'lim_istate : Ice initialization '
@@ -112,27 +110,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) = rt0 * tmask(:,:,1)
+         tn_ice(:,:,jl) = rt0 * tmask(:,:,1)
+      END DO
+
+      ! basal temperature (considered at freezing point)
+      t_bo(:,:) = ( eos_fzp( sss_m(:,:) ) + rt0 ) * tmask(:,:,1) 
+
+      IF( ln_iceini ) 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( ( sst_m(ji,jj)  - ( t_bo(ji,jj) - rt0 ) ) * tmask(ji,jj,1) >= rn_thres_sst ) THEN 
+               zswitch(ji,jj) = 0._wp * tmask(ji,jj,1)    ! no ice
+            ELSE                                                                                   
+               zswitch(ji,jj) = 1._wp * tmask(ji,jj,1)    !    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 +146,4 @@
          END DO
       END DO
-      ! END MV 2011 new initialization
 
       !--------------------------------------------------------------------
@@ -153,11 +154,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) = rn_hti_ini_n ; zht_i_ini(2) = rn_hti_ini_s  ! ice thickness
+      zht_s_ini(1) = rn_hts_ini_n ; zht_s_ini(2) = rn_hts_ini_s  ! snow depth
+      zat_i_ini(1) = rn_ati_ini_n ; zat_i_ini(2) = rn_ati_ini_s  ! ice concentration
+      zsm_i_ini(1) = rn_smi_ini_n ; zsm_i_ini(2) = rn_smi_ini_s  ! bulk ice salinity
+      ztm_i_ini(1) = rn_tmi_ini_n ; ztm_i_ini(2) = rn_tmi_ini_s  ! temperature (ice and snow)
+
+      zvt_i_ini(:) = zht_i_ini(:) * zat_i_ini(:)   ! ice volume
 
       !---------------------------------------------------------------------
@@ -183,35 +185,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) )
-               END DO
-
-            !--- jl0: most likely index where cc will be maximum
+                  zh_i_ini(jl,i_hemis) = hi_mean(jl)
+               END DO
+               
+               !--- 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) >  hi_max(jl-1) ) .AND. &
+                     & ( zht_i_ini(i_hemis) <= 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 +223,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)
-               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)
+                  zV = zV + za_i_ini(jl,i_hemis)*zh_i_ini(jl,i_hemis)
+               END DO
+               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 +264,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) > 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 +290,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 +313,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))     ! salinity
+               o_i(ji,jj,jl)   = zswitch(ji,jj) * 1._wp                        ! age (1 day)
+               t_su(ji,jj,jl)  = zswitch(ji,jj) * ztm_i_ini(zhemis(ji,jj)) + ( 1._wp - zswitch(ji,jj) ) * rt0 ! surf temp
 
                ! This case below should not be used if (ht_s/ht_i) is ok in namelist
@@ -327,5 +326,5 @@
                ! recompute ht_i, ht_s avoiding out of bounds values
                ht_i(ji,jj,jl) = MIN( hi_max(jl), ht_i(ji,jj,jl) + zdh )
-               ht_s(ji,jj,jl) = MAX( 0._wp, ht_s(ji,jj,jl) - zdh * rhoic / rhosn )
+               ht_s(ji,jj,jl) = MAX( 0._wp, ht_s(ji,jj,jl) - zdh * rhoic * r1_rhosn )
 
                ! ice volume, salt content, age content
@@ -334,7 +333,7 @@
                smv_i(ji,jj,jl) = MIN( sm_i(ji,jj,jl) , sss_m(ji,jj) ) * v_i(ji,jj,jl) ! salt content
                oa_i(ji,jj,jl)  = o_i(ji,jj,jl) * a_i(ji,jj,jl)               ! age content
-            END DO ! ji
-         END DO ! jj
-      END DO ! jl
+            END DO
+         END DO
+      END DO
 
       ! Snow temperature and heat content
@@ -343,15 +342,14 @@
             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) ) * rt0
                    ! Snow energy of melting
-                   e_s(ji,jj,jk,jl) = zidto(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
-                   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
-            END DO ! jj
-         END DO ! jl
-      END DO ! jk
+                   e_s(ji,jj,jk,jl) = zswitch(ji,jj) * rhosn * ( cpic * ( rt0 - t_s(ji,jj,jk,jl) ) + lfus )
+
+                   ! Mutliply by volume, and divide by number of layers to get heat content in J/m2
+                   e_s(ji,jj,jk,jl) = e_s(ji,jj,jk,jl) * v_s(ji,jj,jl) * r1_nlay_s
+               END DO
+            END DO
+         END DO
+      END DO
 
       ! Ice salinity, temperature and heat content
@@ -360,28 +358,56 @@
             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
-                   ztmelts          = - tmut * s_i(ji,jj,jk,jl) + rtt !Melting temperature in K
+                   t_i(ji,jj,jk,jl) = zswitch(ji,jj) * ztm_i_ini(zhemis(ji,jj)) + ( 1._wp - zswitch(ji,jj) ) * rt0 
+                   s_i(ji,jj,jk,jl) = zswitch(ji,jj) * zsm_i_ini(zhemis(ji,jj)) !+ ( 1._wp - zswitch(ji,jj) ) * rn_simin
+                   ztmelts          = - tmut * s_i(ji,jj,jk,jl) + rt0 !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) ) &
-                      +   lfus    * ( 1._wp - (ztmelts-rtt) / MIN((t_i(ji,jj,jk,jl)-rtt),-epsi20) ) &
-                      -   rcp     * ( ztmelts - rtt ) )
-
-                   ! Correct dimensions to avoid big values
-                   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
-                   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
-            END DO ! jj
-         END DO ! jl
-      END DO ! jk
-
+                   e_i(ji,jj,jk,jl) = zswitch(ji,jj) * rhoic * (   cpic    * ( ztmelts - t_i(ji,jj,jk,jl) ) &
+                      +   lfus    * ( 1._wp - (ztmelts-rt0) / MIN((t_i(ji,jj,jk,jl)-rt0),-epsi20) ) &
+                      -   rcp     * ( ztmelts - rt0 ) )
+
+                   ! Mutliply by ice volume, and divide by number of layers to get heat content in J/m2
+                   e_i(ji,jj,jk,jl) = e_i(ji,jj,jk,jl) * v_i(ji,jj,jl) * r1_nlay_i
+               END DO
+            END DO
+         END DO
+      END DO
+
+      tn_ice (:,:,:) = t_su (:,:,:)
+
+      ELSE 
+         ! if ln_iceini=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) = rt0 * tmask(:,:,1)
+            END DO
+            DO jk = 1, nlay_s
+               t_s(:,:,jk,jl) = rt0 * tmask(:,:,1)
+            END DO
+         END DO
+      
+      ENDIF ! ln_iceini
+      
+      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 +416,4 @@
       stress12_i(:,:) = 0._wp
 
-# if defined key_coupled
-      albege(:,:)   = 0.8 * tms(:,:)
-# endif
-
       !--------------------------------------------------------------------
       ! 5) Moments for advection
@@ -428,52 +450,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 +474,6 @@
       !!  8.5  ! 07-11 (M. Vancoppenolle) rewritten initialization
       !!-----------------------------------------------------------------------------
-      NAMELIST/namiceini/ ttest, hninn, hnins, hginn, hgins, aginn, agins, sinn, sins
-      !
+      NAMELIST/namiceini/ ln_iceini, rn_thres_sst, rn_hts_ini_n, rn_hts_ini_s, rn_hti_ini_n, rn_hti_ini_s,  &
+         &                                      rn_ati_ini_n, rn_ati_ini_s, rn_smi_ini_n, rn_smi_ini_s, rn_tmi_ini_n, rn_tmi_ini_s
       INTEGER :: ios                 ! Local integer output status for namelist read
       !!-----------------------------------------------------------------------------
@@ -516,13 +495,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_iceini     = ', ln_iceini
+         WRITE(numout,*) '   threshold water temp. for initial sea-ice    rn_thres_sst  = ', rn_thres_sst
+         WRITE(numout,*) '   initial snow thickness in the north          rn_hts_ini_n  = ', rn_hts_ini_n
+         WRITE(numout,*) '   initial snow thickness in the south          rn_hts_ini_s  = ', rn_hts_ini_s 
+         WRITE(numout,*) '   initial ice thickness  in the north          rn_hti_ini_n  = ', rn_hti_ini_n
+         WRITE(numout,*) '   initial ice thickness  in the south          rn_hti_ini_s  = ', rn_hti_ini_s
+         WRITE(numout,*) '   initial ice concentr.  in the north          rn_ati_ini_n  = ', rn_ati_ini_n
+         WRITE(numout,*) '   initial ice concentr.  in the north          rn_ati_ini_s  = ', rn_ati_ini_s
+         WRITE(numout,*) '   initial  ice salinity  in the north          rn_smi_ini_n  = ', rn_smi_ini_n
+         WRITE(numout,*) '   initial  ice salinity  in the south          rn_smi_ini_s  = ', rn_smi_ini_s
+         WRITE(numout,*) '   initial  ice/snw temp  in the north          rn_tmi_ini_n  = ', rn_tmi_ini_n
+         WRITE(numout,*) '   initial  ice/snw temp  in the south          rn_tmi_ini_s  = ', rn_tmi_ini_s
       ENDIF
 
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limitd_me.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limitd_me.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limitd_me.F90	(revision 5832)
@@ -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
    !!----------------------------------------------------------------------
@@ -18,19 +18,17 @@
    USE thd_ice          ! LIM thermodynamics
    USE ice              ! LIM variables
-   USE par_ice          ! LIM parameters
    USE dom_ice          ! LIM domain
-   USE limthd_lac       ! LIM
    USE limvar           ! LIM
-   USE limcons          ! LIM
-   USE in_out_manager   ! I/O manager
    USE lbclnk           ! lateral boundary condition - MPP exchanges
    USE lib_mpp          ! MPP library
    USE wrk_nemo         ! work arrays
    USE prtctl           ! Print control
-  ! Check budget (Rousset)
+
+   USE in_out_manager   ! I/O manager
    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
@@ -40,10 +38,5 @@
    PUBLIC   lim_itd_me_icestrength
    PUBLIC   lim_itd_me_init
-   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
+   PUBLIC   lim_itd_me_alloc        ! called by sbc_lim_init 
 
    !-----------------------------------------------------------------------
@@ -129,30 +122,25 @@
       !!  and Elizabeth C. Hunke, LANL are gratefully acknowledged
       !!--------------------------------------------------------------------!
-      INTEGER ::   ji, jj, jk, jl   ! dummy loop index
-      INTEGER ::   niter, nitermax = 20   ! local integer 
-      LOGICAL  ::   asum_error            ! flag for asum .ne. 1
+      INTEGER  ::   ji, jj, jk, jl        ! dummy loop index
+      INTEGER  ::   niter                 ! local integer 
       INTEGER  ::   iterate_ridging       ! if true, repeat the ridging
-      REAL(wp) ::   w1, tmpfac            ! local scalar
+      REAL(wp) ::   za, zfac              ! local scalar
       CHARACTER (len = 15) ::   fieldid
-      REAL(wp), POINTER, DIMENSION(:,:) ::   closing_net     ! net rate at which area is removed    (1/s)
-                                                             ! (ridging ice area - area of new ridges) / dt
-      REAL(wp), POINTER, DIMENSION(:,:) ::   divu_adv        ! divu as implied by transport scheme  (1/s)
-      REAL(wp), POINTER, DIMENSION(:,:) ::   opning          ! rate of opening due to divergence/shear
-      REAL(wp), POINTER, DIMENSION(:,:) ::   closing_gross   ! rate at which area removed, not counting area of new ridges
-      REAL(wp), POINTER, DIMENSION(:,:) ::   msnow_mlt       ! mass of snow added to ocean (kg m-2)
-      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), POINTER, DIMENSION(:,:)   ::   closing_net     ! net rate at which area is removed    (1/s)
+                                                               ! (ridging ice area - area of new ridges) / dt
+      REAL(wp), POINTER, DIMENSION(:,:)   ::   divu_adv        ! divu as implied by transport scheme  (1/s)
+      REAL(wp), POINTER, DIMENSION(:,:)   ::   opning          ! rate of opening due to divergence/shear
+      REAL(wp), POINTER, DIMENSION(:,:)   ::   closing_gross   ! rate at which area removed, not counting area of new ridges
+      REAL(wp), POINTER, DIMENSION(:,:)   ::   msnow_mlt       ! mass of snow added to ocean (kg m-2)
+      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
+      !
+      INTEGER, PARAMETER ::   nitermax = 20    
+      !
+      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)
+      CALL wrk_alloc( jpi,jpj, closing_net, divu_adv, opning, closing_gross, msnow_mlt, esnow_mlt, vt_i_init, vt_i_final )
 
       IF(ln_ctl) THEN
@@ -162,24 +150,15 @@
 
       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)
+
+      CALL lim_var_zapsmall
+      CALL lim_var_glo2eqv            ! equivalent variables, requested for rafting
 
       !-----------------------------------------------------------------------------!
       ! 1) Thickness categories boundaries, ice / o.w. concentrations, init_ons
       !-----------------------------------------------------------------------------!
-      Cp = 0.5 * grav * (rau0-rhoic) * rhoic / rau0                ! proport const for PE
+      Cp = 0.5 * grav * (rau0-rhoic) * rhoic * r1_rau0             ! proport const for PE
       !
       CALL lim_itd_me_ridgeprep                                    ! prepare ridging
@@ -215,5 +194,5 @@
             !  (thick, newly ridged ice).
 
-            closing_net(ji,jj) = Cs * 0.5 * ( Delta_i(ji,jj) - ABS( divu_i(ji,jj) ) ) - MIN( divu_i(ji,jj), 0._wp )
+            closing_net(ji,jj) = rn_cs * 0.5 * ( delta_i(ji,jj) - ABS( divu_i(ji,jj) ) ) - MIN( divu_i(ji,jj), 0._wp )
 
             ! 2.2 divu_adv
@@ -259,15 +238,13 @@
                ! Reduce the closing rate if more than 100% of the open water 
                ! would be removed.  Reduce the opening rate proportionately.
-               IF ( ato_i(ji,jj) .GT. epsi10 .AND. athorn(ji,jj,0) .GT. 0.0 ) THEN
-                  w1 = athorn(ji,jj,0) * closing_gross(ji,jj) * rdt_ice
-                  IF ( w1 .GT. ato_i(ji,jj)) THEN
-                     tmpfac = ato_i(ji,jj) / w1
-                     closing_gross(ji,jj) = closing_gross(ji,jj) * tmpfac
-                     opning(ji,jj) = opning(ji,jj) * tmpfac
-                  ENDIF !w1
-               ENDIF !at0i and athorn
-
-            END DO ! ji
-         END DO ! jj
+               za   = athorn(ji,jj,0) * closing_gross(ji,jj) * rdt_ice
+               IF( za > epsi20 ) THEN
+                  zfac = MIN( 1._wp, ato_i(ji,jj) / za )
+                  closing_gross(ji,jj) = closing_gross(ji,jj) * zfac
+                  opning       (ji,jj) = opning       (ji,jj) * zfac
+               ENDIF
+
+            END DO
+         END DO
 
          ! correction to closing rate / opening if excessive ice removal
@@ -275,19 +252,16 @@
          ! Reduce the closing rate if more than 100% of any ice category 
          ! would be removed.  Reduce the opening rate proportionately.
-
          DO jl = 1, jpl
             DO jj = 1, jpj
                DO ji = 1, jpi
-                  IF ( a_i(ji,jj,jl) > epsi10 .AND. athorn(ji,jj,jl) > 0._wp )THEN
-                     w1 = athorn(ji,jj,jl) * closing_gross(ji,jj) * rdt_ice
-                     IF ( w1  >  a_i(ji,jj,jl) ) THEN
-                        tmpfac = a_i(ji,jj,jl) / w1
-                        closing_gross(ji,jj) = closing_gross(ji,jj) * tmpfac
-                        opning       (ji,jj) = opning       (ji,jj) * tmpfac
-                     ENDIF
+                  za = athorn(ji,jj,jl) * closing_gross(ji,jj) * rdt_ice
+                  IF( za  >  epsi20 ) THEN
+                     zfac = MIN( 1._wp, a_i(ji,jj,jl) / za )
+                     closing_gross(ji,jj) = closing_gross(ji,jj) * zfac
+                     opning       (ji,jj) = opning       (ji,jj) * zfac
                   ENDIF
-               END DO !ji
-            END DO ! jj
-         END DO !jl
+               END DO
+            END DO
+         END DO
 
          ! 3.3 Redistribute area, volume, and energy.
@@ -298,6 +272,9 @@
          ! 3.4 Compute total area of ice plus open water after ridging.
          !-----------------------------------------------------------------------------!
-
-         CALL lim_itd_me_asumr
+         ! This is in general not equal to one because of divergence during transport
+         asum(:,:) = ato_i(:,:)
+         DO jl = 1, jpl
+            asum(:,:) = asum(:,:) + a_i(:,:,jl)
+         END DO
 
          ! 3.5 Do we keep on iterating ???
@@ -310,5 +287,5 @@
          DO jj = 1, jpj
             DO ji = 1, jpi
-               IF (ABS(asum(ji,jj) - kamax ) .LT. epsi10) THEN
+               IF (ABS(asum(ji,jj) - kamax ) < epsi10) THEN
                   closing_net(ji,jj) = 0._wp
                   opning     (ji,jj) = 0._wp
@@ -346,11 +323,6 @@
       ! Convert ridging rate diagnostics to correct units.
       ! Update fresh water and heat fluxes due to snow melt.
-
-      asum_error = .false. 
-
       DO jj = 1, jpj
          DO ji = 1, jpi
-
-            IF(ABS(asum(ji,jj) - kamax) > epsi10 ) asum_error = .true.
 
             dardg1dt(ji,jj) = dardg1dt(ji,jj) * r1_rdtice
@@ -362,6 +334,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) * r1_rdtice     ! heat sink for ocean (<0, W.m-2)
 
          END DO
@@ -369,20 +341,21 @@
 
       ! Check if there is a ridging error
-      DO jj = 1, jpj
-         DO ji = 1, jpi
-            IF( ABS( asum(ji,jj) - kamax)  >  epsi10 ) THEN   ! there is a bug
-               WRITE(numout,*) ' '
-               WRITE(numout,*) ' ALERTE : Ridging error: total area = ', asum(ji,jj)
-               WRITE(numout,*) ' limitd_me '
-               WRITE(numout,*) ' POINT : ', ji, jj
-               WRITE(numout,*) ' jpl, a_i, athorn '
-               WRITE(numout,*) 0, ato_i(ji,jj), athorn(ji,jj,0)
-               DO jl = 1, jpl
-                  WRITE(numout,*) jl, a_i(ji,jj,jl), athorn(ji,jj,jl)
-               END DO
-            ENDIF  ! asum
-
-         END DO !ji
-      END DO !jj
+      IF( lwp ) THEN
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               IF( ABS( asum(ji,jj) - kamax)  >  epsi10 ) THEN   ! there is a bug
+                  WRITE(numout,*) ' '
+                  WRITE(numout,*) ' ALERTE : Ridging error: total area = ', asum(ji,jj)
+                  WRITE(numout,*) ' limitd_me '
+                  WRITE(numout,*) ' POINT : ', ji, jj
+                  WRITE(numout,*) ' jpl, a_i, athorn '
+                  WRITE(numout,*) 0, ato_i(ji,jj), athorn(ji,jj,0)
+                  DO jl = 1, jpl
+                     WRITE(numout,*) jl, a_i(ji,jj,jl), athorn(ji,jj,jl)
+                  END DO
+               ENDIF
+            END DO
+         END DO
+      END IF
 
       ! Conservation check
@@ -393,29 +366,16 @@
       ENDIF
 
+      CALL lim_var_agg( 1 ) 
+
       !-----------------------------------------------------------------------------!
-      ! 6) Updating state variables and trend terms (done in limupdate)
+      ! control prints
       !-----------------------------------------------------------------------------!
-      CALL lim_var_glo2eqv
-      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
+      IF(ln_ctl) THEN 
+         CALL lim_var_glo2eqv
+
          CALL prt_ctl_info(' ')
          CALL prt_ctl_info(' - Cell values : ')
          CALL prt_ctl_info('   ~~~~~~~~~~~~~ ')
-         CALL prt_ctl(tab2d_1=area , clinfo1=' lim_itd_me  : cell area :')
+         CALL prt_ctl(tab2d_1=e12t , clinfo1=' lim_itd_me  : cell area :')
          CALL prt_ctl(tab2d_1=at_i , clinfo1=' lim_itd_me  : at_i      :')
          CALL prt_ctl(tab2d_1=vt_i , clinfo1=' lim_itd_me  : vt_i      :')
@@ -445,33 +405,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')
@@ -494,9 +431,8 @@
       !!----------------------------------------------------------------------
       INTEGER, INTENT(in) ::   kstrngth    ! = 1 for Rothrock formulation, 0 for Hibler (1979)
-
-      INTEGER ::   ji,jj, jl   ! dummy loop indices
-      INTEGER ::   ksmooth     ! smoothing the resistance to deformation
-      INTEGER ::   numts_rm    ! number of time steps for the P smoothing
-      REAL(wp) ::   hi, zw1, zp, zdummy, zzc, z1_3   ! local scalars
+      INTEGER             ::   ji,jj, jl   ! dummy loop indices
+      INTEGER             ::   ksmooth     ! smoothing the resistance to deformation
+      INTEGER             ::   numts_rm    ! number of time steps for the P smoothing
+      REAL(wp)            ::   zhi, zp, z1_3  ! local scalars
       REAL(wp), POINTER, DIMENSION(:,:) ::   zworka   ! temporary array used here
       !!----------------------------------------------------------------------
@@ -524,30 +460,29 @@
                   !
                   IF( a_i(ji,jj,jl) > epsi10 .AND. athorn(ji,jj,jl) > 0._wp ) THEN
-                     hi = v_i(ji,jj,jl) / a_i(ji,jj,jl)
+                     zhi = v_i(ji,jj,jl) / a_i(ji,jj,jl)
                      !----------------------------
                      ! PE loss from deforming ice
                      !----------------------------
-                     strength(ji,jj) = strength(ji,jj) - athorn(ji,jj,jl) * hi * hi
+                     strength(ji,jj) = strength(ji,jj) - athorn(ji,jj,jl) * zhi * zhi
 
                      !--------------------------
                      ! PE gain from rafting ice
                      !--------------------------
-                     strength(ji,jj) = strength(ji,jj) + 2._wp * araft(ji,jj,jl) * hi * hi
+                     strength(ji,jj) = strength(ji,jj) + 2._wp * araft(ji,jj,jl) * zhi * zhi
 
                      !----------------------------
                      ! PE gain from ridging ice
                      !----------------------------
-                     strength(ji,jj) = strength(ji,jj) + aridge(ji,jj,jl)/krdg(ji,jj,jl)     &
-                        * z1_3 * (hrmax(ji,jj,jl)**3 - hrmin(ji,jj,jl)**3) / ( hrmax(ji,jj,jl)-hrmin(ji,jj,jl) )   
-!!gm Optimization:  (a**3-b**3)/(a-b) = a*a+ab+b*b   ==> less costly operations even if a**3 is replaced by a*a*a...                    
-                  ENDIF            ! aicen > epsi10
+                     strength(ji,jj) = strength(ji,jj) + aridge(ji,jj,jl) / krdg(ji,jj,jl)     &
+                        * z1_3 * ( hrmax(ji,jj,jl)**2 + hrmin(ji,jj,jl)**2 +  hrmax(ji,jj,jl) * hrmin(ji,jj,jl) )  
+                        !!(a**3-b**3)/(a-b) = a*a+ab+b*b                      
+                  ENDIF
                   !
-               END DO ! ji
-            END DO !jj
-         END DO !jl
-
-         zzc = Cf * Cp     ! where Cp = (g/2)*(rhow-rhoi)*(rhoi/rhow) and Cf accounts for frictional dissipation
-         strength(:,:) = zzc * strength(:,:) / aksum(:,:)
-
+               END DO
+            END DO
+         END DO
+   
+         strength(:,:) = rn_pe_rdg * Cp * strength(:,:) / aksum(:,:)
+                         ! where Cp = (g/2)*(rhow-rhoi)*(rhoi/rhow) and rn_pe_rdg accounts for frictional dissipation
          ksmooth = 1
 
@@ -557,5 +492,5 @@
       ELSE                      ! kstrngth ne 1:  Hibler (1979) form
          !
-         strength(:,:) = Pstar * vt_i(:,:) * EXP( - C_rhg * ( 1._wp - at_i(:,:) )  )
+         strength(:,:) = rn_pstar * vt_i(:,:) * EXP( - rn_crhg * ( 1._wp - at_i(:,:) )  )
          !
          ksmooth = 1
@@ -569,16 +504,11 @@
       ! CAN BE REMOVED
       !
-      IF ( brinstren_swi == 1 ) THEN
+      IF( ln_icestr_bvf ) THEN
 
          DO jj = 1, jpj
             DO ji = 1, jpi
-               IF ( bv_i(ji,jj) .GT. 0.0 ) THEN
-                  zdummy = MIN ( bv_i(ji,jj), 0.10 ) * MIN( bv_i(ji,jj), 0.10 )
-               ELSE
-                  zdummy = 0.0
-               ENDIF
                strength(ji,jj) = strength(ji,jj) * exp(-5.88*SQRT(MAX(bv_i(ji,jj),0.0)))
-            END DO              ! j
-         END DO                 ! i
+            END DO
+         END DO
 
       ENDIF
@@ -596,16 +526,11 @@
          CALL lbc_lnk( strength, 'T', 1. )
 
-         DO jj = 2, jpj - 1
-            DO ji = 2, jpi - 1
-               IF ( ( asum(ji,jj) - ato_i(ji,jj) ) .GT. epsi10) THEN ! ice is
-                  ! present
-                  zworka(ji,jj) = 4.0 * strength(ji,jj)              &
-                     &          + strength(ji-1,jj) * tms(ji-1,jj) &  
-                     &          + strength(ji+1,jj) * tms(ji+1,jj) &  
-                     &          + strength(ji,jj-1) * tms(ji,jj-1) &  
-                     &          + strength(ji,jj+1) * tms(ji,jj+1)    
-
-                  zw1 = 4.0 + tms(ji-1,jj) + tms(ji+1,jj) + tms(ji,jj-1) + tms(ji,jj+1)
-                  zworka(ji,jj) = zworka(ji,jj) / zw1
+         DO jj = 2, jpjm1
+            DO ji = 2, jpim1
+               IF ( ( asum(ji,jj) - ato_i(ji,jj) ) > 0._wp) THEN 
+                  zworka(ji,jj) = ( 4.0 * strength(ji,jj)              &
+                     &                  + strength(ji-1,jj) * tmask(ji-1,jj,1) + strength(ji+1,jj) * tmask(ji+1,jj,1) &  
+                     &                  + strength(ji,jj-1) * tmask(ji,jj-1,1) + strength(ji,jj+1) * tmask(ji,jj+1,1) &
+                     &            ) / ( 4.0 + tmask(ji-1,jj,1) + tmask(ji+1,jj,1) + tmask(ji,jj-1,1) + tmask(ji,jj+1,1) )
                ELSE
                   zworka(ji,jj) = 0._wp
@@ -614,6 +539,6 @@
          END DO
 
-         DO jj = 2, jpj - 1
-            DO ji = 2, jpi - 1
+         DO jj = 2, jpjm1
+            DO ji = 2, jpim1
                strength(ji,jj) = zworka(ji,jj)
             END DO
@@ -621,5 +546,5 @@
          CALL lbc_lnk( strength, 'T', 1. )
 
-      ENDIF ! ksmooth
+      ENDIF
 
       !--------------------
@@ -638,8 +563,8 @@
          DO jj = 1, jpj - 1
             DO ji = 1, jpi - 1
-               IF ( ( asum(ji,jj) - ato_i(ji,jj) ) .GT. epsi10) THEN       ! ice is present
+               IF ( ( asum(ji,jj) - ato_i(ji,jj) ) > 0._wp) THEN 
                   numts_rm = 1 ! number of time steps for the running mean
-                  IF ( strp1(ji,jj) .GT. 0.0 ) numts_rm = numts_rm + 1
-                  IF ( strp2(ji,jj) .GT. 0.0 ) numts_rm = numts_rm + 1
+                  IF ( strp1(ji,jj) > 0.0 ) numts_rm = numts_rm + 1
+                  IF ( strp2(ji,jj) > 0.0 ) numts_rm = numts_rm + 1
                   zp = ( strength(ji,jj) + strp1(ji,jj) + strp2(ji,jj) ) / numts_rm
                   strp2(ji,jj) = strp1(ji,jj)
@@ -670,6 +595,5 @@
       !!---------------------------------------------------------------------!
       INTEGER ::   ji,jj, jl    ! dummy loop indices
-      INTEGER ::   krdg_index   ! 
-      REAL(wp) ::   Gstari, astari, hi, hrmean, zdummy   ! local scalar
+      REAL(wp) ::   Gstari, astari, zhi, hrmean, zdummy   ! local scalar
       REAL(wp), POINTER, DIMENSION(:,:)   ::   zworka    ! temporary array used here
       REAL(wp), POINTER, DIMENSION(:,:,:) ::   Gsum      ! Gsum(n) = sum of areas in categories 0 to n
@@ -679,6 +603,6 @@
       CALL wrk_alloc( jpi,jpj,jpl+2, Gsum, kkstart = -1 )
 
-      Gstari     = 1.0/Gstar    
-      astari     = 1.0/astar    
+      Gstari     = 1.0/rn_gstar    
+      astari     = 1.0/rn_astar    
       aksum(:,:)    = 0.0
       athorn(:,:,:) = 0.0
@@ -691,5 +615,5 @@
 
       !     ! Zero out categories with very small areas
-      CALL lim_itd_me_zapsmall
+      CALL lim_var_zapsmall
 
       !------------------------------------------------------------------------------!
@@ -698,7 +622,9 @@
 
       ! Compute total area of ice plus open water.
-      CALL lim_itd_me_asumr
-      ! This is in general not equal to one 
-      ! because of divergence during transport
+      ! This is in general not equal to one because of divergence during transport
+      asum(:,:) = ato_i(:,:)
+      DO jl = 1, jpl
+         asum(:,:) = asum(:,:) + a_i(:,:,jl)
+      END DO
 
       ! Compute cumulative thickness distribution function
@@ -708,22 +634,9 @@
 
       Gsum(:,:,-1) = 0._wp
-
-      DO jj = 1, jpj
-         DO ji = 1, jpi
-            IF( ato_i(ji,jj) > epsi10 ) THEN   ;   Gsum(ji,jj,0) = ato_i(ji,jj)
-            ELSE                               ;   Gsum(ji,jj,0) = 0._wp
-            ENDIF
-         END DO
-      END DO
+      Gsum(:,:,0 ) = ato_i(:,:)
 
       ! for each value of h, you have to add ice concentration then
       DO jl = 1, jpl
-         DO jj = 1, jpj 
-            DO ji = 1, jpi
-               IF( a_i(ji,jj,jl) .GT. epsi10 ) THEN   ;   Gsum(ji,jj,jl) = Gsum(ji,jj,jl-1) + a_i(ji,jj,jl)
-               ELSE                                   ;   Gsum(ji,jj,jl) = Gsum(ji,jj,jl-1)
-               ENDIF
-            END DO
-         END DO
+         Gsum(:,:,jl) = Gsum(:,:,jl-1) + a_i(:,:,jl)
       END DO
 
@@ -746,53 +659,50 @@
       !-----------------------------------------------------------------
 
-      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( nn_partfun == 0 ) THEN       !--- Linear formulation (Thorndike et al., 1975)
+         DO jl = 0, jpl    
             DO jj = 1, jpj 
                DO ji = 1, jpi
-                  IF( Gsum(ji,jj,jl) < Gstar) THEN
-                     athorn(ji,jj,jl) = Gstari * (Gsum(ji,jj,jl)-Gsum(ji,jj,jl-1)) * &
-                        (2.0 - (Gsum(ji,jj,jl-1)+Gsum(ji,jj,jl))*Gstari)
-                  ELSEIF (Gsum(ji,jj,jl-1) < Gstar) THEN
-                     athorn(ji,jj,jl) = Gstari * (Gstar-Gsum(ji,jj,jl-1)) *  &
-                        (2.0 - (Gsum(ji,jj,jl-1)+Gstar)*Gstari)
+                  IF( Gsum(ji,jj,jl) < rn_gstar) THEN
+                     athorn(ji,jj,jl) = Gstari * ( Gsum(ji,jj,jl) - Gsum(ji,jj,jl-1) ) * &
+                        &                        ( 2.0 - (Gsum(ji,jj,jl-1) + Gsum(ji,jj,jl) ) * Gstari )
+                  ELSEIF (Gsum(ji,jj,jl-1) < rn_gstar) THEN
+                     athorn(ji,jj,jl) = Gstari * ( rn_gstar - Gsum(ji,jj,jl-1) ) *  &
+                        &                        ( 2.0 - ( Gsum(ji,jj,jl-1) + rn_gstar ) * Gstari )
                   ELSE
                      athorn(ji,jj,jl) = 0.0
                   ENDIF
-               END DO ! ji
-            END DO ! jj
-         END DO ! jl 
+               END DO
+            END DO
+         END DO
 
       ELSE                             !--- Exponential, more stable formulation (Lipscomb et al, 2007)
          !                        
          zdummy = 1._wp / ( 1._wp - EXP(-astari) )        ! precompute exponential terms using Gsum as a work array
-
          DO jl = -1, jpl
             Gsum(:,:,jl) = EXP( -Gsum(:,:,jl) * astari ) * zdummy
-         END DO !jl
-         DO jl = 0, ice_cat_bounds(1,2)
+         END DO
+         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
+
+      IF( ln_rafting ) THEN      ! Ridging and rafting ice participation functions
          !
          DO jl = 1, jpl
             DO jj = 1, jpj 
                DO ji = 1, jpi
-                  IF ( athorn(ji,jj,jl) .GT. 0._wp ) THEN
+                  IF ( athorn(ji,jj,jl) > 0._wp ) THEN
 !!gm  TANH( -X ) = - TANH( X )  so can be computed only 1 time....
-                     aridge(ji,jj,jl) = ( TANH (  Craft * ( ht_i(ji,jj,jl) - hparmeter ) ) + 1.0 ) * 0.5 * athorn(ji,jj,jl)
-                     araft (ji,jj,jl) = ( TANH ( -Craft * ( ht_i(ji,jj,jl) - hparmeter ) ) + 1.0 ) * 0.5 * athorn(ji,jj,jl)
+                     aridge(ji,jj,jl) = ( TANH (  rn_craft * ( ht_i(ji,jj,jl) - rn_hraft ) ) + 1.0 ) * 0.5 * athorn(ji,jj,jl)
+                     araft (ji,jj,jl) = ( TANH ( -rn_craft * ( ht_i(ji,jj,jl) - rn_hraft ) ) + 1.0 ) * 0.5 * athorn(ji,jj,jl)
                      IF ( araft(ji,jj,jl) < epsi06 )   araft(ji,jj,jl)  = 0._wp
                      aridge(ji,jj,jl) = MAX( athorn(ji,jj,jl) - araft(ji,jj,jl), 0.0 )
-                  ENDIF ! athorn
-               END DO ! ji
-            END DO ! jj
-         END DO ! jl
-
-      ELSE  ! raftswi = 0
+                  ENDIF
+               END DO
+            END DO
+         END DO
+
+      ELSE
          !
          DO jl = 1, jpl
@@ -802,11 +712,11 @@
       ENDIF
 
-      IF ( raftswi == 1 ) THEN
-
-         IF( MAXVAL(aridge + araft - athorn(:,:,1:jpl)) .GT. epsi10 ) THEN
+      IF( ln_rafting ) THEN
+
+         IF( MAXVAL(aridge + araft - athorn(:,:,1:jpl)) > epsi10 .AND. lwp ) THEN
             DO jl = 1, jpl
                DO jj = 1, jpj
                   DO ji = 1, jpi
-                     IF ( aridge(ji,jj,jl) + araft(ji,jj,jl) - athorn(ji,jj,jl) .GT. epsi10 ) THEN
+                     IF ( aridge(ji,jj,jl) + araft(ji,jj,jl) - athorn(ji,jj,jl) > epsi10 ) THEN
                         WRITE(numout,*) ' ALERTE 96 : wrong participation function ... '
                         WRITE(numout,*) ' ji, jj, jl : ', ji, jj, jl
@@ -854,11 +764,11 @@
             DO ji = 1, jpi
 
-               IF (a_i(ji,jj,jl) .GT. epsi10 .AND. athorn(ji,jj,jl) .GT. 0.0 ) THEN
-                  hi = v_i(ji,jj,jl) / a_i(ji,jj,jl)
-                  hrmean          = MAX(SQRT(Hstar*hi), hi*krdgmin)
-                  hrmin(ji,jj,jl) = MIN(2.0*hi, 0.5*(hrmean + hi))
+               IF (a_i(ji,jj,jl) > epsi10 .AND. athorn(ji,jj,jl) > 0.0 ) THEN
+                  zhi = v_i(ji,jj,jl) / a_i(ji,jj,jl)
+                  hrmean          = MAX(SQRT(rn_hstar*zhi), zhi*krdgmin)
+                  hrmin(ji,jj,jl) = MIN(2.0*zhi, 0.5*(hrmean + zhi))
                   hrmax(ji,jj,jl) = 2.0*hrmean - hrmin(ji,jj,jl)
-                  hraft(ji,jj,jl) = kraft*hi
-                  krdg(ji,jj,jl)  = hrmean / hi
+                  hraft(ji,jj,jl) = kraft*zhi
+                  krdg(ji,jj,jl)  = hrmean / zhi
                ELSE
                   hraft(ji,jj,jl) = 0.0
@@ -868,7 +778,7 @@
                ENDIF
 
-            END DO ! ji
-         END DO ! jj
-      END DO ! jl
+            END DO
+         END DO
+      END DO
 
       ! Normalization factor : aksum, ensures mass conservation
@@ -902,12 +812,8 @@
       LOGICAL, PARAMETER ::   l_conservation_check = .true.  ! if true, check conservation (useful for debugging)
       !
-      LOGICAL ::   neg_ato_i      ! flag for ato_i(i,j) < -puny
-      LOGICAL ::   large_afrac    ! flag for afrac > 1
-      LOGICAL ::   large_afrft    ! flag for afrac > 1
       INTEGER ::   ji, jj, jl, jl1, jl2, jk   ! dummy loop indices
       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) ::   hL, hR, farea, ztmelts    ! left and right limits of integration
 
       INTEGER , POINTER, DIMENSION(:) ::   indxi, indxj   ! compressed indices
@@ -917,5 +823,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
 
@@ -925,5 +831,4 @@
       REAL(wp), POINTER, DIMENSION(:,:) ::   ardg1 , ardg2    ! area of ice ridged & new ridges
       REAL(wp), POINTER, DIMENSION(:,:) ::   vsrdg , esrdg    ! snow volume & energy of ridging ice
-      REAL(wp), POINTER, DIMENSION(:,:) ::   oirdg1, oirdg2   ! areal age content of ridged & rifging ice
       REAL(wp), POINTER, DIMENSION(:,:) ::   dhr   , dhr2     ! hrmax - hrmin  &  hrmax^2 - hrmin^2
 
@@ -934,4 +839,5 @@
       REAL(wp), POINTER, DIMENSION(:,:) ::   srdg2   ! sal*volume of new ridges
       REAL(wp), POINTER, DIMENSION(:,:) ::   smsw    ! sal*volume of water trapped into ridges
+      REAL(wp), POINTER, DIMENSION(:,:) ::   oirdg1, oirdg2   ! ice age of ice ridged
 
       REAL(wp), POINTER, DIMENSION(:,:) ::   afrft            ! fraction of category area rafted
@@ -939,5 +845,5 @@
       REAL(wp), POINTER, DIMENSION(:,:) ::   virft , vsrft    ! ice & snow volume of rafting ice
       REAL(wp), POINTER, DIMENSION(:,:) ::   esrft , smrft    ! snow energy & salinity of rafting ice
-      REAL(wp), POINTER, DIMENSION(:,:) ::   oirft1, oirft2   ! areal age content of rafted ice & rafting ice
+      REAL(wp), POINTER, DIMENSION(:,:) ::   oirft1, oirft2   ! ice age of ice rafted
 
       REAL(wp), POINTER, DIMENSION(:,:,:) ::   eirft      ! ice energy of rafting ice
@@ -947,12 +853,12 @@
       !!----------------------------------------------------------------------
 
-      CALL wrk_alloc( (jpi+1)*(jpj+1),      indxi, indxj )
-      CALL wrk_alloc( jpi, jpj,             vice_init, vice_final, eice_init, eice_final )
-      CALL wrk_alloc( jpi, jpj,             afrac, fvol , ardg1, ardg2, vsrdg, esrdg, oirdg1, oirdg2, dhr, dhr2 )
-      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+1)*(jpj+1),       indxi, indxj )
+      CALL wrk_alloc( jpi, jpj,              vice_init, vice_final, eice_init, eice_final )
+      CALL wrk_alloc( jpi, jpj,              afrac, fvol , ardg1, ardg2, vsrdg, esrdg, dhr, dhr2 )
+      CALL wrk_alloc( jpi, jpj,              vrdg1, vrdg2, vsw  , srdg1, srdg2, smsw, oirdg1, oirdg2 )
+      CALL wrk_alloc( jpi, jpj,              afrft, arft1, arft2, virft, vsrft, esrft, smrft, oirft1, oirft2 )
+      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,      eirft, erdg1, erdg2, ersw )
+      CALL wrk_alloc( jpi, jpj, nlay_i, jpl, eicen_init )
 
       ! Conservation check
@@ -962,6 +868,6 @@
          CALL lim_column_sum        (jpl,    v_i,       vice_init )
          CALL lim_column_sum_energy (jpl, nlay_i,  e_i, eice_init )
-         DO ji = mi0(jiindx), mi1(jiindx)
-            DO jj = mj0(jjindx), mj1(jjindx)
+         DO ji = mi0(iiceprt), mi1(iiceprt)
+            DO jj = mj0(jiceprt), mj1(jiceprt)
                WRITE(numout,*) ' vice_init  : ', vice_init(ji,jj)
                WRITE(numout,*) ' eice_init  : ', eice_init(ji,jj)
@@ -973,52 +879,26 @@
       ! 1) Compute change in open water area due to closing and opening.
       !-------------------------------------------------------------------------------
-
-      neg_ato_i = .false.
-
       DO jj = 1, jpj
          DO ji = 1, jpi
             ato_i(ji,jj) = ato_i(ji,jj) - athorn(ji,jj,0) * closing_gross(ji,jj) * rdt_ice        &
                &                        + opning(ji,jj)                          * rdt_ice
-            IF( ato_i(ji,jj) < -epsi10 ) THEN
-               neg_ato_i = .TRUE.
-            ELSEIF( ato_i(ji,jj) < 0._wp ) THEN    ! roundoff error
+            IF    ( ato_i(ji,jj) < -epsi10 ) THEN    ! there is a bug
+               IF(lwp)   WRITE(numout,*) 'Ridging error: ato_i < 0 -- ato_i : ',ato_i(ji,jj)
+            ELSEIF( ato_i(ji,jj) < 0._wp   ) THEN    ! roundoff error
                ato_i(ji,jj) = 0._wp
             ENDIF
-         END DO !jj
-      END DO !ji
-
-      ! if negative open water area alert it
-      IF( neg_ato_i ) THEN       ! there is a bug
-         DO jj = 1, jpj 
-            DO ji = 1, jpi
-               IF( ato_i(ji,jj) < -epsi10 ) THEN 
-                  WRITE(numout,*) ''  
-                  WRITE(numout,*) 'Ridging error: ato_i < 0'
-                  WRITE(numout,*) 'ato_i : ', ato_i(ji,jj)
-               ENDIF               ! ato_i < -epsi10
-            END DO
-         END DO
-      ENDIF
+         END DO
+      END DO
 
       !-----------------------------------------------------------------
       ! 2) Save initial state variables
       !-----------------------------------------------------------------
-
-      DO jl = 1, jpl
-         aicen_init(:,:,jl) = a_i(:,:,jl)
-         vicen_init(:,:,jl) = v_i(:,:,jl)
-         vsnon_init(:,:,jl) = v_s(:,:,jl)
-         !
-         smv_i_init(:,:,jl) = smv_i(:,:,jl)
-         oa_i_init (:,:,jl) = oa_i (:,:,jl)
-      END DO !jl
-
-      esnon_init(:,:,:) = e_s(:,:,1,:)
-
-      DO jl = 1, jpl  
-         DO jk = 1, nlay_i
-            eicen_init(:,:,jk,jl) = e_i(:,:,jk,jl)
-         END DO
-      END DO
+      aicen_init(:,:,:)   = a_i  (:,:,:)
+      vicen_init(:,:,:)   = v_i  (:,:,:)
+      vsnwn_init(:,:,:)   = v_s  (:,:,:)
+      smv_i_init(:,:,:)   = smv_i(:,:,:)
+      esnwn_init(:,:,:)   = e_s  (:,:,1,:)
+      eicen_init(:,:,:,:) = e_i  (:,:,:,:)
+      oa_i_init (:,:,:)   = oa_i (:,:,:)
 
       !
@@ -1043,12 +923,8 @@
                   indxi(icells) = ji
                   indxj(icells) = jj
-               ENDIF ! test on a_icen_init 
-            END DO ! ji
-         END DO ! jj
-
-         large_afrac = .false.
-         large_afrft = .false.
-
-!CDIR NODEP
+               ENDIF
+            END DO
+         END DO
+
          DO ij = 1, icells
             ji = indxi(ij)
@@ -1064,9 +940,4 @@
             arft2(ji,jj) = arft1(ji,jj) / kraft
 
-            oirdg1(ji,jj)= aridge(ji,jj,jl1)*closing_gross(ji,jj)*rdt_ice
-            oirft1(ji,jj)= araft (ji,jj,jl1)*closing_gross(ji,jj)*rdt_ice
-            oirdg2(ji,jj)= oirdg1(ji,jj) / krdg(ji,jj,jl1)
-            oirft2(ji,jj)= oirft1(ji,jj) / kraft
-
             !---------------------------------------------------------------
             ! 3.3) Compute ridging /rafting fractions, make sure afrac <=1 
@@ -1076,12 +947,13 @@
             afrft(ji,jj) = arft1(ji,jj) / aicen_init(ji,jj,jl1) !rafting
 
-            IF (afrac(ji,jj) > kamax + epsi10) THEN  !riging
-               large_afrac = .true.
-            ELSEIF (afrac(ji,jj) > kamax) THEN  ! roundoff error
+            IF( afrac(ji,jj) > kamax + epsi10 ) THEN  ! there is a bug
+               IF(lwp)   WRITE(numout,*) ' ardg > a_i -- ardg, aicen_init : ', ardg1(ji,jj), aicen_init(ji,jj,jl1)
+            ELSEIF( afrac(ji,jj) > kamax ) THEN       ! roundoff error
                afrac(ji,jj) = kamax
             ENDIF
-            IF (afrft(ji,jj) > kamax + epsi10) THEN !rafting
-               large_afrft = .true.
-            ELSEIF (afrft(ji,jj) > kamax) THEN  ! roundoff error
+
+            IF( afrft(ji,jj) > kamax + epsi10 ) THEN ! there is a bug
+               IF(lwp)   WRITE(numout,*) ' arft > a_i -- arft, aicen_init : ', arft1(ji,jj), aicen_init(ji,jj,jl1) 
+            ELSEIF( afrft(ji,jj) > kamax) THEN       ! roundoff error
                afrft(ji,jj) = kamax
             ENDIF
@@ -1091,43 +963,43 @@
             !     / rafting category n1.
             !--------------------------------------------------------------------------
-            vrdg1(ji,jj) = vicen_init(ji,jj,jl1) * afrac(ji,jj) / ( 1._wp + ridge_por )
-            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)
+            vrdg1(ji,jj) = vicen_init(ji,jj,jl1) * afrac(ji,jj)
+            vrdg2(ji,jj) = vrdg1(ji,jj) * ( 1. + rn_por_rdg )
+            vsw  (ji,jj) = vrdg1(ji,jj) * rn_por_rdg
+
+            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)
+            oirdg1(ji,jj) = oa_i_init (ji,jj,jl1) * afrac(ji,jj)
+            oirdg2(ji,jj) = oa_i_init (ji,jj,jl1) * afrac(ji,jj) / krdg(ji,jj,jl1) 
 
             ! 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)
-            smrft(ji,jj) = smv_i_init(ji,jj,jl1) * afrft(ji,jj) 
+            virft (ji,jj) = vicen_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) 
+            oirft1(ji,jj) = oa_i_init (ji,jj,jl1) * afrft(ji,jj) 
+            oirft2(ji,jj) = oa_i_init (ji,jj,jl1) * afrft(ji,jj) / kraft 
 
             ! substract everything
-            a_i(ji,jj,jl1)   = a_i(ji,jj,jl1)   - ardg1(ji,jj)  - arft1(ji,jj)
-            v_i(ji,jj,jl1)   = v_i(ji,jj,jl1)   - vrdg1(ji,jj)  - virft(ji,jj)
-            v_s(ji,jj,jl1)   = v_s(ji,jj,jl1)   - vsrdg(ji,jj)  - vsrft(ji,jj)
-            e_s(ji,jj,1,jl1) = e_s(ji,jj,1,jl1) - esrdg(ji,jj)  - esrft(ji,jj)
+            a_i(ji,jj,jl1)   = a_i(ji,jj,jl1)   - ardg1 (ji,jj) - arft1 (ji,jj)
+            v_i(ji,jj,jl1)   = v_i(ji,jj,jl1)   - vrdg1 (ji,jj) - virft (ji,jj)
+            v_s(ji,jj,jl1)   = v_s(ji,jj,jl1)   - vsrdg (ji,jj) - vsrft (ji,jj)
+            e_s(ji,jj,1,jl1) = e_s(ji,jj,1,jl1) - esrdg (ji,jj) - esrft (ji,jj)
+            smv_i(ji,jj,jl1) = smv_i(ji,jj,jl1) - srdg1 (ji,jj) - smrft (ji,jj)
             oa_i(ji,jj,jl1)  = oa_i(ji,jj,jl1)  - oirdg1(ji,jj) - oirft1(ji,jj)
-            smv_i(ji,jj,jl1) = smv_i(ji,jj,jl1) - srdg1(ji,jj)  - smrft(ji,jj)
 
             !-----------------------------------------------------------------
             ! 3.5) Compute properties of new ridges
             !-----------------------------------------------------------------
-            !-------------
+            !---------
             ! 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( rn_simax * 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   ! increase in ice volume du to seawater frozen in voids             
 
             !------------------------------------            
@@ -1155,9 +1027,10 @@
             !           ij looping 1-icells
 
-            msnow_mlt(ji,jj) = msnow_mlt(ji,jj) + rhosn*vsrdg(ji,jj)*(1.0-fsnowrdg)   &   ! rafting included
-               &                                + 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)          
+            msnow_mlt(ji,jj) = msnow_mlt(ji,jj) + rhosn*vsrdg(ji,jj)*(1.0-rn_fsnowrdg)   &   ! rafting included
+               &                                + rhosn*vsrft(ji,jj)*(1.0-rn_fsnowrft)
+
+            ! in J/m2 (same as e_s)
+            esnow_mlt(ji,jj) = esnow_mlt(ji,jj) - esrdg(ji,jj)*(1.0-rn_fsnowrdg)         &   !rafting included
+               &                                - esrft(ji,jj)*(1.0-rn_fsnowrft)          
 
             !-----------------------------------------------------------------
@@ -1172,5 +1045,5 @@
             dhr2(ji,jj) = hrmax(ji,jj,jl1) * hrmax(ji,jj,jl1) - hrmin(ji,jj,jl1) * hrmin(ji,jj,jl1)
 
-         END DO                 ! ij
+         END DO
 
          !--------------------------------------------------------------------
@@ -1179,71 +1052,34 @@
          !--------------------------------------------------------------------
          DO jk = 1, nlay_i
-!CDIR NODEP
             DO ij = 1, icells
                ji = indxi(ij)
                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
-
-               ! 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 )
-
+               
+               
+               ! enthalpy of the trapped seawater (J/m2, >0)
+               ! clem: if sst>0, then ersw <0 (is that possible?)
+               ersw(ji,jj,jk)   = - rhoic * vsw(ji,jj) * rcp * sst_m(ji,jj) * r1_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 
+
+               ! it is added to sea ice because the sign convention is the opposite of the sign convention for the ocean
                erdg2(ji,jj,jk)  = erdg1(ji,jj,jk) + ersw(ji,jj,jk)
-            END DO ! ij
-         END DO !jk
+
+            END DO
+         END DO
 
 
          IF( con_i ) THEN
             DO jk = 1, nlay_i
-!CDIR NODEP
                DO ij = 1, icells
                   ji = indxi(ij)
                   jj = indxj(ij)
                   eice_init(ji,jj) = eice_init(ji,jj) + erdg2(ji,jj,jk) - erdg1(ji,jj,jk)
-               END DO ! ij
-            END DO !jk
-         ENDIF
-
-         IF( large_afrac ) THEN   ! there is a bug
-!CDIR NODEP
-            DO ij = 1, icells
-               ji = indxi(ij)
-               jj = indxj(ij)
-               IF( afrac(ji,jj) > kamax + epsi10 ) THEN 
-                  WRITE(numout,*) ''
-                  WRITE(numout,*) ' ardg > a_i'
-                  WRITE(numout,*) ' ardg, aicen_init : ', ardg1(ji,jj), aicen_init(ji,jj,jl1)
-               ENDIF
-            END DO
-         ENDIF
-         IF( large_afrft ) THEN  ! there is a bug
-!CDIR NODEP
-            DO ij = 1, icells
-               ji = indxi(ij)
-               jj = indxj(ij)
-               IF( afrft(ji,jj) > kamax + epsi10 ) THEN 
-                  WRITE(numout,*) ''
-                  WRITE(numout,*) ' arft > a_i'
-                  WRITE(numout,*) ' arft, aicen_init : ', arft1(ji,jj), aicen_init(ji,jj,jl1)
-               ENDIF
+               END DO
             END DO
          ENDIF
@@ -1253,7 +1089,6 @@
          !-------------------------------------------------------------------------------
          !        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
             DO ij = 1, icells
                ji = indxi(ij)
@@ -1264,6 +1099,5 @@
                ! Transfer area, volume, and energy accordingly.
 
-               IF( hrmin(ji,jj,jl1) >= hi_max(jl2)  .OR.        &
-                   hrmax(ji,jj,jl1) <= hi_max(jl2-1) ) THEN
+               IF( hrmin(ji,jj,jl1) >= hi_max(jl2) .OR. hrmax(ji,jj,jl1) <= hi_max(jl2-1) ) THEN
                   hL = 0._wp
                   hR = 0._wp
@@ -1279,18 +1113,17 @@
                a_i  (ji,jj  ,jl2) = a_i  (ji,jj  ,jl2) + ardg2 (ji,jj) * farea
                v_i  (ji,jj  ,jl2) = v_i  (ji,jj  ,jl2) + vrdg2 (ji,jj) * fvol(ji,jj)
-               v_s  (ji,jj  ,jl2) = v_s  (ji,jj  ,jl2) + vsrdg (ji,jj) * fvol(ji,jj) * fsnowrdg
-               e_s  (ji,jj,1,jl2) = e_s  (ji,jj,1,jl2) + esrdg (ji,jj) * fvol(ji,jj) * fsnowrdg
+               v_s  (ji,jj  ,jl2) = v_s  (ji,jj  ,jl2) + vsrdg (ji,jj) * fvol(ji,jj) * rn_fsnowrdg
+               e_s  (ji,jj,1,jl2) = e_s  (ji,jj,1,jl2) + esrdg (ji,jj) * fvol(ji,jj) * rn_fsnowrdg
                smv_i(ji,jj  ,jl2) = smv_i(ji,jj  ,jl2) + srdg2 (ji,jj) * fvol(ji,jj)
                oa_i (ji,jj  ,jl2) = oa_i (ji,jj  ,jl2) + oirdg2(ji,jj) * farea
 
-            END DO ! ij
+            END DO
 
             ! Transfer ice energy to category jl2 by ridging
             DO jk = 1, nlay_i
-!CDIR NODEP
                DO ij = 1, icells
                   ji = indxi(ij)
                   jj = indxj(ij)
-                  e_i(ji,jj,jk,jl2) = e_i(ji,jj,jk,jl2) + fvol(ji,jj)*erdg2(ji,jj,jk)
+                  e_i(ji,jj,jk,jl2) = e_i(ji,jj,jk,jl2) + fvol(ji,jj) * erdg2(ji,jj,jk)
                END DO
             END DO
@@ -1298,7 +1131,6 @@
          END DO                 ! jl2 (new ridges)            
 
-         DO jl2 = ice_cat_bounds(1,1), ice_cat_bounds(1,2) 
-
-!CDIR NODEP
+         DO jl2 = 1, jpl 
+
             DO ij = 1, icells
                ji = indxi(ij)
@@ -1307,30 +1139,27 @@
                ! thickness category jl2, transfer area, volume, and energy accordingly.
                !
-               IF( hraft(ji,jj,jl1) <= hi_max(jl2)  .AND.        &
-                   hraft(ji,jj,jl1) >  hi_max(jl2-1) ) THEN
+               IF( hraft(ji,jj,jl1) <= hi_max(jl2) .AND. hraft(ji,jj,jl1) >  hi_max(jl2-1) ) THEN
                   a_i  (ji,jj  ,jl2) = a_i  (ji,jj  ,jl2) + arft2 (ji,jj)
                   v_i  (ji,jj  ,jl2) = v_i  (ji,jj  ,jl2) + virft (ji,jj)
-                  v_s  (ji,jj  ,jl2) = v_s  (ji,jj  ,jl2) + vsrft (ji,jj) * fsnowrft
-                  e_s  (ji,jj,1,jl2) = e_s  (ji,jj,1,jl2) + esrft (ji,jj) * fsnowrft
+                  v_s  (ji,jj  ,jl2) = v_s  (ji,jj  ,jl2) + vsrft (ji,jj) * rn_fsnowrft
+                  e_s  (ji,jj,1,jl2) = e_s  (ji,jj,1,jl2) + esrft (ji,jj) * rn_fsnowrft
                   smv_i(ji,jj  ,jl2) = smv_i(ji,jj  ,jl2) + smrft (ji,jj)    
-                  oa_i (ji,jj  ,jl2) = oa_i (ji,jj  ,jl2) + oirft2(ji,jj)    
-               ENDIF ! hraft
+                  oa_i (ji,jj  ,jl2) = oa_i (ji,jj  ,jl2) + oirft2(ji,jj)
+               ENDIF
                !
-            END DO ! ij
+            END DO
 
             ! Transfer rafted ice energy to category jl2 
             DO jk = 1, nlay_i
-!CDIR NODEP
                DO ij = 1, icells
                   ji = indxi(ij)
                   jj = indxj(ij)
-                  IF(  hraft(ji,jj,jl1)  <=  hi_max(jl2)   .AND.        &
-                       hraft(ji,jj,jl1)  >   hi_max(jl2-1)  ) THEN
+                  IF( hraft(ji,jj,jl1) <= hi_max(jl2) .AND. hraft(ji,jj,jl1) > hi_max(jl2-1)  ) THEN
                      e_i(ji,jj,jk,jl2) = e_i(ji,jj,jk,jl2) + eirft(ji,jj,jk)
                   ENDIF
-               END DO           ! ij
-            END DO !jk
-
-         END DO ! jl2
+               END DO
+            END DO
+
+         END DO
 
       END DO ! jl1 (deforming categories)
@@ -1346,6 +1175,6 @@
          CALL lim_cons_check (eice_init, eice_final, 1.0e-2, fieldid) 
 
-         DO ji = mi0(jiindx), mi1(jiindx)
-            DO jj = mj0(jjindx), mj1(jjindx)
+         DO ji = mi0(iiceprt), mi1(iiceprt)
+            DO jj = mj0(jiceprt), mj1(jiceprt)
                WRITE(numout,*) ' vice_init  : ', vice_init (ji,jj)
                WRITE(numout,*) ' vice_final : ', vice_final(ji,jj)
@@ -1356,37 +1185,14 @@
       ENDIF
       !
-      CALL wrk_dealloc( (jpi+1)*(jpj+1),      indxi, indxj )
-      CALL wrk_dealloc( jpi, jpj,             vice_init, vice_final, eice_init, eice_final )
-      CALL wrk_dealloc( jpi, jpj,             afrac, fvol , ardg1, ardg2, vsrdg, esrdg, oirdg1, oirdg2, dhr, dhr2 )
-      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+1)*(jpj+1),        indxi, indxj )
+      CALL wrk_dealloc( jpi, jpj,               vice_init, vice_final, eice_init, eice_final )
+      CALL wrk_dealloc( jpi, jpj,               afrac, fvol , ardg1, ardg2, vsrdg, esrdg, dhr, dhr2 )
+      CALL wrk_dealloc( jpi, jpj,               vrdg1, vrdg2, vsw  , srdg1, srdg2, smsw, oirdg1, oirdg2 )
+      CALL wrk_dealloc( jpi, jpj,               afrft, arft1, arft2, virft, vsrft, esrft, smrft, oirft1, oirft2 )
+      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,       eirft, erdg1, erdg2, ersw )
+      CALL wrk_dealloc( jpi, jpj, nlay_i, jpl,  eicen_init )
       !
    END SUBROUTINE lim_itd_me_ridgeshift
-
-
-   SUBROUTINE lim_itd_me_asumr
-      !!-----------------------------------------------------------------------------
-      !!                ***  ROUTINE lim_itd_me_asumr ***
-      !!
-      !! ** Purpose :   finds total fractional area
-      !!
-      !! ** Method  :   Find the total area of ice plus open water in each grid cell.
-      !!              This is similar to the aggregate_area subroutine except that the
-      !!              total area can be greater than 1, so the open water area is 
-      !!              included in the sum instead of being computed as a residual. 
-      !!-----------------------------------------------------------------------------
-      INTEGER ::   jl   ! dummy loop index
-      !!-----------------------------------------------------------------------------
-      !
-      asum(:,:) = ato_i(:,:)                    ! open water
-      DO jl = 1, jpl                            ! ice categories
-         asum(:,:) = asum(:,:) + a_i(:,:,jl)
-      END DO
-      !
-   END SUBROUTINE lim_itd_me_asumr
-
 
    SUBROUTINE lim_itd_me_init
@@ -1404,9 +1210,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/ rn_cs, rn_fsnowrdg, rn_fsnowrft,              & 
+        &                   rn_gstar, rn_astar, rn_hstar, ln_rafting, rn_hraft, rn_craft, rn_por_rdg, &
+        &                   nn_partfun
       !!-------------------------------------------------------------------
       !
@@ -1424,128 +1228,18 @@
          WRITE(numout,*)' lim_itd_me_init : ice parameters for mechanical ice redistribution '
          WRITE(numout,*)' ~~~~~~~~~~~~~~~'
-         WRITE(numout,*)'   Switch choosing the ice redistribution scheme           ridge_scheme_swi', ridge_scheme_swi 
-         WRITE(numout,*)'   Fraction of shear energy contributing to ridging        Cs              ', Cs 
-         WRITE(numout,*)'   Ratio of ridging work to PotEner change in ridging      Cf              ', Cf 
-         WRITE(numout,*)'   Fraction of snow volume conserved during ridging        fsnowrdg        ', fsnowrdg 
-         WRITE(numout,*)'   Fraction of snow volume conserved during ridging        fsnowrft        ', fsnowrft 
-         WRITE(numout,*)'   Fraction of total ice coverage contributing to ridging  Gstar           ', Gstar
-         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,*)'   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
+         WRITE(numout,*)'   Fraction of shear energy contributing to ridging        rn_cs       = ', rn_cs 
+         WRITE(numout,*)'   Fraction of snow volume conserved during ridging        rn_fsnowrdg = ', rn_fsnowrdg 
+         WRITE(numout,*)'   Fraction of snow volume conserved during ridging        rn_fsnowrft = ', rn_fsnowrft 
+         WRITE(numout,*)'   Fraction of total ice coverage contributing to ridging  rn_gstar    = ', rn_gstar
+         WRITE(numout,*)'   Equivalent to G* for an exponential part function       rn_astar    = ', rn_astar
+         WRITE(numout,*)'   Quantity playing a role in max ridged ice thickness     rn_hstar    = ', rn_hstar
+         WRITE(numout,*)'   Rafting of ice sheets or not                            ln_rafting  = ', ln_rafting
+         WRITE(numout,*)'   Parmeter thickness (threshold between ridge-raft)       rn_hraft    = ', rn_hraft
+         WRITE(numout,*)'   Rafting hyperbolic tangent coefficient                  rn_craft    = ', rn_craft  
+         WRITE(numout,*)'   Initial porosity of ridges                              rn_por_rdg  = ', rn_por_rdg
+         WRITE(numout,*)'   Switch for part. function (0) linear (1) exponential    nn_partfun  = ', nn_partfun
       ENDIF
       !
    END SUBROUTINE lim_itd_me_init
-
-
-   SUBROUTINE lim_itd_me_zapsmall
-      !!-------------------------------------------------------------------
-      !!                   ***  ROUTINE lim_itd_me_zapsmall ***
-      !!
-      !! ** Purpose :   Remove too small sea ice areas and correct salt fluxes
-      !!
-      !! history :
-      !! author: William H. Lipscomb, LANL
-      !! Nov 2003:  Modified by Julie Schramm to conserve volume and energy
-      !! Sept 2004: Modified by William Lipscomb; replaced normalize_state with
-      !!            additions to local freshwater, salt, and heat fluxes
-      !!  9.0, LIM3.0 - 02-2006 (M. Vancoppenolle) original code
-      !!-------------------------------------------------------------------
-      INTEGER ::   ji, jj, jl, jk   ! dummy loop indices
-      INTEGER ::   icells           ! number of cells with ice to zap
-
-      REAL(wp), POINTER, DIMENSION(:,:) ::   zmask   ! 2D workspace
-      REAL(wp)                          ::   zmask_glo
-!!gm      REAL(wp) ::   xtmp      ! temporary variable
-      !!-------------------------------------------------------------------
-
-      CALL wrk_alloc( jpi, jpj, zmask )
-
-      DO jl = 1, jpl
-
-         !-----------------------------------------------------------------
-         ! Count categories to be zapped.
-         ! Abort model in case of negative area.
-         !-----------------------------------------------------------------
-         icells = 0
-         zmask(:,:)  = 0._wp
-         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
-            END DO
-         END DO
-         !zmask_glo = glob_sum(zmask)
-         !IF( ln_nicep .AND. lwp ) WRITE(numout,*) zmask_glo, ' cells of ice zapped in the ocean '
-
-         !-----------------------------------------------------------------
-         ! Zap ice energy and use ocean heat to melt ice
-         !-----------------------------------------------------------------
-
-         DO jk = 1, nlay_i
-            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
-                  e_i(ji,jj,jk,jl) = e_i(ji,jj,jk,jl) * ( 1._wp - zmask(ji,jj) )
-               END DO
-            END DO
-         END DO
-
-         DO jj = 1 , jpj
-            DO ji = 1 , jpi
-
-               !-----------------------------------------------------------------
-               ! Zap snow energy and use ocean heat to melt snow
-               !-----------------------------------------------------------------
-               !           xtmp = esnon(i,j,n) / dt ! < 0
-               !           fhnet(i,j)      = fhnet(i,j)      + xtmp
-               !           fhnet_hist(i,j) = fhnet_hist(i,j) + xtmp
-               ! xtmp is greater than 0
-               ! 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) )
-
-               !-----------------------------------------------------------------
-               ! 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)
-               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) )
-               v_s  (ji,jj,jl) = v_s  (ji,jj,jl) * ( 1._wp - zmask(ji,jj) )
-               t_su (ji,jj,jl) = t_su (ji,jj,jl) * ( 1._wp - zmask(ji,jj) ) + t_bo(ji,jj) * zmask(ji,jj)
-               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) )
-               !
-            END DO
-         END DO
-         !
-      END DO                 ! jl 
-      !
-      CALL wrk_dealloc( jpi, jpj, zmask )
-      !
-   END SUBROUTINE lim_itd_me_zapsmall
 
 #else
@@ -1558,10 +1252,6 @@
    SUBROUTINE lim_itd_me_icestrength
    END SUBROUTINE lim_itd_me_icestrength
-   SUBROUTINE lim_itd_me_sort
-   END SUBROUTINE lim_itd_me_sort
    SUBROUTINE lim_itd_me_init
    END SUBROUTINE lim_itd_me_init
-   SUBROUTINE lim_itd_me_zapsmall
-   END SUBROUTINE lim_itd_me_zapsmall
 #endif
    !!======================================================================
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limitd_th.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limitd_th.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limitd_th.F90	(revision 5832)
@@ -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
    !!----------------------------------------------------------------------
@@ -13,5 +13,4 @@
    !!   'key_lim3' :                                   LIM3 sea-ice model
    !!----------------------------------------------------------------------
-   !!   lim_itd_th       : thermodynamics of ice thickness distribution
    !!   lim_itd_th_rem   :
    !!   lim_itd_th_reb   :
@@ -25,8 +24,5 @@
    USE thd_ice          ! LIM-3 thermodynamic variables
    USE ice              ! LIM-3 variables
-   USE par_ice          ! LIM-3 parameters
-   USE limthd_lac       ! LIM-3 lateral accretion
    USE limvar           ! LIM-3 variables
-   USE limcons          ! LIM-3 conservation
    USE prtctl           ! Print control
    USE in_out_manager   ! I/O manager
@@ -34,17 +30,11 @@
    USE wrk_nemo         ! work arrays
    USE lib_fortran      ! to use key_nosignedzero
-   USE timing          ! Timing
+   USE limcons          ! conservation tests
 
    IMPLICIT NONE
    PRIVATE
 
-   PUBLIC   lim_itd_th         ! called by ice_stp
    PUBLIC   lim_itd_th_rem
    PUBLIC   lim_itd_th_reb
-   PUBLIC   lim_itd_fitline
-   PUBLIC   lim_itd_shiftice
-
-   REAL(wp) ::   epsi10 = 1.e-10_wp   !
-   REAL(wp) ::   epsi06 = 1.e-6_wp   !
 
    !!----------------------------------------------------------------------
@@ -55,119 +45,5 @@
 CONTAINS
 
-   SUBROUTINE lim_itd_th( kt )
-      !!------------------------------------------------------------------
-      !!                ***  ROUTINE lim_itd_th ***
-      !!
-      !! ** Purpose :   computes the thermodynamics of ice thickness distribution
-      !!
-      !! ** Method  :
-      !!------------------------------------------------------------------
-      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)
-      !!------------------------------------------------------------------
-      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)
-      ! -------------------------------
-
-      IF( kt == nit000 .AND. lwp ) THEN
-         WRITE(numout,*)
-         WRITE(numout,*) 'lim_itd_th  : Thermodynamics of the ice thickness distribution'
-         WRITE(numout,*) '~~~~~~~~~~~'
-      ENDIF
-
-      !------------------------------------------------------------------------------|
-      !  1) Transport of ice between thickness categories.                           |
-      !------------------------------------------------------------------------------|
-      ! 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
-      !
-      CALL lim_var_glo2eqv    ! only for info
-      CALL lim_var_agg(1)
-
-      !------------------------------------------------------------------------------|
-      !  3) Add frazil ice growing in leads.
-      !------------------------------------------------------------------------------|
-
-      CALL lim_thd_lac
-      CALL lim_var_glo2eqv    ! only for info
-
-     IF(ln_ctl) THEN   ! Control print
-         CALL prt_ctl_info(' ')
-         CALL prt_ctl_info(' - Cell values : ')
-         CALL prt_ctl_info('   ~~~~~~~~~~~~~ ')
-         CALL prt_ctl(tab2d_1=area , clinfo1=' lim_itd_th  : cell area :')
-         CALL prt_ctl(tab2d_1=at_i , clinfo1=' lim_itd_th  : at_i      :')
-         CALL prt_ctl(tab2d_1=vt_i , clinfo1=' lim_itd_th  : vt_i      :')
-         CALL prt_ctl(tab2d_1=vt_s , clinfo1=' lim_itd_th  : vt_s      :')
-         DO jl = 1, jpl
-            CALL prt_ctl_info(' ')
-            CALL prt_ctl_info(' - Category : ', ivar1=jl)
-            CALL prt_ctl_info('   ~~~~~~~~~~')
-            CALL prt_ctl(tab2d_1=a_i   (:,:,jl)   , clinfo1= ' lim_itd_th  : a_i      : ')
-            CALL prt_ctl(tab2d_1=ht_i  (:,:,jl)   , clinfo1= ' lim_itd_th  : ht_i     : ')
-            CALL prt_ctl(tab2d_1=ht_s  (:,:,jl)   , clinfo1= ' lim_itd_th  : ht_s     : ')
-            CALL prt_ctl(tab2d_1=v_i   (:,:,jl)   , clinfo1= ' lim_itd_th  : v_i      : ')
-            CALL prt_ctl(tab2d_1=v_s   (:,:,jl)   , clinfo1= ' lim_itd_th  : v_s      : ')
-            CALL prt_ctl(tab2d_1=e_s   (:,:,1,jl) , clinfo1= ' lim_itd_th  : e_s      : ')
-            CALL prt_ctl(tab2d_1=t_su  (:,:,jl)   , clinfo1= ' lim_itd_th  : t_su     : ')
-            CALL prt_ctl(tab2d_1=t_s   (:,:,1,jl) , clinfo1= ' lim_itd_th  : t_snow   : ')
-            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
-               CALL prt_ctl_info(' ')
-               CALL prt_ctl_info(' - Layer : ', ivar1=ja)
-               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      : ')
-            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)
-      ! -------------------------------
-      !
-     IF( nn_timing == 1 )  CALL timing_stop('limitd_th')
-   END SUBROUTINE lim_itd_th
-   !
-
-   SUBROUTINE lim_itd_th_rem( klbnd, kubnd, ntyp, kt )
+   SUBROUTINE lim_itd_th_rem( klbnd, kubnd, kt )
       !!------------------------------------------------------------------
       !!                ***  ROUTINE lim_itd_th_rem ***
@@ -182,5 +58,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 +65,5 @@
       REAL(wp) ::   zx1, zwk1, zdh0, zetamin, zdamax   ! local scalars
       REAL(wp) ::   zx2, zwk2, zda0, zetamax           !   -      -
-      REAL(wp) ::   zx3,             zareamin, zindb   !   -      -
+      REAL(wp) ::   zx3        
       CHARACTER (len = 15) :: fieldid
 
@@ -200,5 +75,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
@@ -216,14 +91,12 @@
       !!------------------------------------------------------------------
 
-      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, zremap_flag )
+      CALL wrk_alloc( jpi,jpj,jpl-1, zdonor )
+      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 )   
       CALL wrk_alloc( (jpi+1)*(jpj+1), zvetamin, zvetamax )   
-      CALL wrk_alloc( (jpi+1)*(jpj+1), nind_i, nind_j )   ! integer 
+      CALL wrk_alloc( (jpi+1)*(jpj+1), nind_i, nind_j ) 
       CALL wrk_alloc( jpi,jpj, zhb0,zhb1,vt_i_init,vt_i_final,vt_s_init,vt_s_final,et_i_init,et_i_final,et_s_init,et_s_final )
-
-      zareamin = epsi10   !minimum area in thickness categories tolerated by the conceptors of the model
 
       !!----------------------------------------------------------------------------------------------
@@ -247,5 +120,4 @@
          WRITE(numout,*) ' klbnd :       ', klbnd
          WRITE(numout,*) ' kubnd :       ', kubnd
-         WRITE(numout,*) ' ntyp  :       ', ntyp 
       ENDIF
 
@@ -254,9 +126,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           = 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           = MAX( 0.0, SIGN( 1.0, a_i_b(ji,jj,jl) - epsi10) )
+               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) ! clem: useless IF statement? 
             END DO
          END DO
@@ -277,5 +149,5 @@
       DO jj = 1, jpj
          DO ji = 1, jpi
-            IF ( at_i(ji,jj) .gt. zareamin ) THEN
+            IF ( at_i(ji,jj) > epsi10 ) THEN
                nbrem         = nbrem + 1
                nind_i(nbrem) = ji
@@ -285,6 +157,6 @@
                zremap_flag(ji,jj) = 0
             ENDIF
-         END DO !ji
-      END DO !jj
+         END DO
+      END DO
 
       !-----------------------------------------------------------------------------------------------
@@ -292,7 +164,4 @@
       !-----------------------------------------------------------------------------------------------
       !- 4.1 Compute category boundaries
-      ! Tricky trick see limitd_me.F90
-      ! will be soon removed, CT
-      ! hi_max(kubnd) = 99.
       zhbnew(:,:,:) = 0._wp
 
@@ -302,17 +171,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,31 +185,25 @@
          !- 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
+
+            ! clem: we do not want ht_i to be too close to either HR or HL otherwise a division by nearly 0 is possible 
+            ! in lim_itd_fitline in the case (HR-HL) = 3(Hice - HL) or = 3(HR - Hice)
+            IF    ( a_i(ii,ij,jl  ) > epsi10 .AND. ht_i(ii,ij,jl  ) > ( zhbnew(ii,ij,jl) - epsi10 ) ) 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) + epsi10 ) ) 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
-      END DO !jl
+            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
+            ! clem bug: why is not the following instead?
+            !!IF( zhbnew(ii,ij,jl) < hi_max(jl-1) ) zremap_flag(ii,ij) = 0
+            !!IF( zhbnew(ii,ij,jl) > hi_max(jl  ) ) zremap_flag(ii,ij) = 0
+ 
+         END DO
+
+      END DO
 
       !-----------------------------------------------------------------------------------------------
@@ -354,11 +213,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,21 +226,24 @@
       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
-
-            zhbnew(ji,jj,klbnd-1) = 0._wp
+            zhb0(ji,jj) = hi_max(0)
+            zhb1(ji,jj) = hi_max(1)
 
             IF( a_i(ji,jj,kubnd) > epsi10 ) THEN
-               zhbnew(ji,jj,kubnd) = 3._wp * ht_i(ji,jj,kubnd) - 2._wp * zhbnew(ji,jj,kubnd-1)
+               zhbnew(ji,jj,kubnd) = MAX( hi_max(kubnd-1), 3._wp * ht_i(ji,jj,kubnd) - 2._wp * zhbnew(ji,jj,kubnd-1) )
             ELSE
-               zhbnew(ji,jj,kubnd) = hi_max(kubnd)  
-               !!? clem bug: since hi_max(jpl)=99, this limit is very high 
-               !!? but I think it is erased in fitline subroutine 
-            ENDIF
-
-            IF( zhbnew(ji,jj,kubnd) < hi_max(kubnd-1) )   zhbnew(ji,jj,kubnd) = hi_max(kubnd-1)
-
-         END DO !jj
-      END DO !jj
+!clem bug               zhbnew(ji,jj,kubnd) = hi_max(kubnd)  
+               zhbnew(ji,jj,kubnd) = hi_max(kubnd-1) ! not used anyway
+            ENDIF
+
+            ! clem: we do not want ht_i_b to be too close to either HR or HL otherwise a division by nearly 0 is possible 
+            ! in lim_itd_fitline in the case (HR-HL) = 3(Hice - HL) or = 3(HR - Hice)
+            IF    ( zht_i_b(ji,jj,klbnd) < ( zhb0(ji,jj) + epsi10 ) )  THEN
+               zremap_flag(ji,jj) = 0
+            ELSEIF( zht_i_b(ji,jj,klbnd) > ( zhb1(ji,jj) - epsi10 ) )  THEN
+               zremap_flag(ji,jj) = 0
+            ENDIF
+
+         END DO
+      END DO
 
       !-----------------------------------------------------------------------------------------------
@@ -389,6 +251,5 @@
       !-----------------------------------------------------------------------------------------------
       !- 7.1 g(h) for category 1 at start of time step
-      CALL lim_itd_fitline( klbnd, zhb0, zhb1, zht_i_o(:,:,klbnd),         &
-         &                  g0(:,:,klbnd), g1(:,:,klbnd), hL(:,:,klbnd),   &
+      CALL lim_itd_fitline( klbnd, zhb0, zhb1, zht_i_b(:,:,klbnd), g0(:,:,klbnd), g1(:,:,klbnd), hL(:,:,klbnd),   &
          &                  hR(:,:,klbnd), zremap_flag )
 
@@ -398,52 +259,39 @@
          ij = nind_j(ji) 
 
-         !ji
-         IF (a_i(ii,ij,klbnd) .gt. epsi10) THEN
+         IF( a_i(ii,ij,klbnd) > epsi10 ) THEN
+
             zdh0 = zdhice(ii,ij,klbnd) !decrease of ice thickness in the lower category
-            ! ji, a_i > epsi10
-            IF (zdh0 .lt. 0.0) THEN !remove area from category 1
-               ! ji, a_i > epsi10; zdh0 < 0
-               zdh0 = MIN(-zdh0,hi_max(klbnd))
-
+
+            IF( zdh0 < 0.0 ) THEN      !remove area from category 1
+               zdh0 = MIN( -zdh0, hi_max(klbnd) )
                !Integrate g(1) from 0 to dh0 to estimate area melted
-               zetamax = MIN(zdh0,hR(ii,ij,klbnd)) - hL(ii,ij,klbnd)
-               IF (zetamax.gt.0.0) THEN
-                  zx1  = zetamax
-                  zx2  = 0.5 * zetamax*zetamax 
-                  zda0 = g1(ii,ij,klbnd) * zx2 + g0(ii,ij,klbnd) * zx1 !ice area removed
-                  ! 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
-                  !ice area lost due to melting of thin ice
-                  zda0   = MIN(zda0, zdamax)
-
+               zetamax = MIN( zdh0, hR(ii,ij,klbnd) ) - hL(ii,ij,klbnd)
+
+               IF( zetamax > 0.0 ) THEN
+                  zx1    = zetamax
+                  zx2    = 0.5 * zetamax * zetamax 
+                  zda0   = g1(ii,ij,klbnd) * zx2 + g0(ii,ij,klbnd) * zx1                        ! ice area removed
+                  zdamax = a_i(ii,ij,klbnd) * (1.0 - ht_i(ii,ij,klbnd) / zht_i_b(ii,ij,klbnd) ) ! Constrain new thickness <= ht_i                
+                  zda0   = MIN( zda0, zdamax )                                                  ! ice area lost due to melting 
+                                                                                                !     of thin ice (zdamax > 0)
                   ! Remove area, conserving volume
-                  ht_i(ii,ij,klbnd) = ht_i(ii,ij,klbnd) & 
-                     * a_i(ii,ij,klbnd) / ( a_i(ii,ij,klbnd) - zda0 )
+                  ht_i(ii,ij,klbnd) = ht_i(ii,ij,klbnd) * a_i(ii,ij,klbnd) / ( a_i(ii,ij,klbnd) - zda0 )
                   a_i(ii,ij,klbnd)  = a_i(ii,ij,klbnd) - zda0
-                  v_i(ii,ij,klbnd)  = a_i(ii,ij,klbnd)*ht_i(ii,ij,klbnd) ! clem-useless ?
-               ENDIF     ! zetamax > 0
-               ! ji, a_i > epsi10
-
-            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 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 
-
-            ! a_i > epsi10
-         ENDIF ! a_i > epsi10
-
-      END DO ! ji
+                  v_i(ii,ij,klbnd)  = a_i(ii,ij,klbnd) * ht_i(ii,ij,klbnd) ! clem-useless ?
+               ENDIF
+
+            ELSE ! if ice accretion zdh0 > 0
+               ! zhbnew was 0, and is shifted to the right to account for thin ice growth in openwater (F0 = f1)
+               zhbnew(ii,ij,klbnd-1) = MIN( zdh0, hi_max(klbnd) ) 
+            ENDIF
+
+         ENDIF
+
+      END DO
 
       !- 7.3 g(h) for each thickness category  
       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)
+         CALL lim_itd_fitline( jl, zhbnew(:,:,jl-1), zhbnew(:,:,jl), ht_i(:,:,jl),  &
+            &                  g0(:,:,jl), g1(:,:,jl), hL(:,:,jl), hR(:,:,jl), zremap_flag )
       END DO
 
@@ -465,37 +313,34 @@
             ij = nind_j(ji)
 
-            IF (zhbnew(ii,ij,jl) .gt. hi_max(jl)) THEN ! transfer from jl to jl+1
-
+            IF (zhbnew(ii,ij,jl) > hi_max(jl)) THEN ! transfer from jl to jl+1
                ! left and right integration limits in eta space
-               zvetamin(ji) = MAX(hi_max(jl), hL(ii,ij,jl)) - hL(ii,ij,jl)
-               zvetamax(ji) = MIN(zhbnew(ii,ij,jl), hR(ii,ij,jl)) - hL(ii,ij,jl)
+               zvetamin(ji) = MAX( hi_max(jl), hL(ii,ij,jl) ) - hL(ii,ij,jl)
+               zvetamax(ji) = MIN( zhbnew(ii,ij,jl), hR(ii,ij,jl) ) - hL(ii,ij,jl)
                zdonor(ii,ij,jl) = jl
 
-            ELSE  ! zhbnew(jl) <= hi_max(jl) ; transfer from jl+1 to jl
-
+            ELSE                                    ! zhbnew(jl) <= hi_max(jl) ; transfer from jl+1 to jl
                ! left and right integration limits in eta space
                zvetamin(ji) = 0.0
-               zvetamax(ji) = MIN(hi_max(jl), hR(ii,ij,jl+1)) - hL(ii,ij,jl+1)
+               zvetamax(ji) = MIN( hi_max(jl), hR(ii,ij,jl+1) ) - hL(ii,ij,jl+1)
                zdonor(ii,ij,jl) = jl + 1
 
-            ENDIF  ! zhbnew(jl) > hi_max(jl)
-
-            zetamax = MAX(zvetamax(ji), zvetamin(ji)) ! no transfer if etamax < etamin
+            ENDIF
+
+            zetamax = MAX( zvetamax(ji), zvetamin(ji) ) ! no transfer if etamax < etamin
             zetamin = zvetamin(ji)
 
             zx1  = zetamax - zetamin
-            zwk1 = zetamin*zetamin
-            zwk2 = zetamax*zetamax
-            zx2  = 0.5 * (zwk2 - zwk1)
+            zwk1 = zetamin * zetamin
+            zwk2 = zetamax * zetamax
+            zx2  = 0.5 * ( zwk2 - zwk1 )
             zwk1 = zwk1 * zetamin
             zwk2 = zwk2 * zetamax
-            zx3  = 1.0/3.0 * (zwk2 - zwk1)
+            zx3  = 1.0 / 3.0 * ( zwk2 - zwk1 )
             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)
-
-         END DO ! ji
-      END DO ! jl klbnd -> kubnd - 1
+            zdvice(ii,ij,jl) = g1(ii,ij,nd)*zx3 + g0(ii,ij,nd)*zx2 + zdaice(ii,ij,jl)*hL(ii,ij,nd)
+
+         END DO
+      END DO
 
       !!----------------------------------------------------------------------------------------------
@@ -511,10 +356,9 @@
          ii = nind_i(ji)
          ij = nind_j(ji)
-         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
+         IF ( a_i(ii,ij,1) > epsi10 .AND. ht_i(ii,ij,1) < rn_himin ) THEN
+            a_i (ii,ij,1) = a_i(ii,ij,1) * ht_i(ii,ij,1) / rn_himin 
+            ht_i(ii,ij,1) = rn_himin
          ENDIF
-      END DO !ji
+      END DO
 
       !!----------------------------------------------------------------------------------------------
@@ -540,11 +384,11 @@
       ENDIF
 
-      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, zremap_flag )
+      CALL wrk_dealloc( jpi,jpj,jpl-1, zdonor )
+      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 )   
       CALL wrk_dealloc( (jpi+1)*(jpj+1), zvetamin, zvetamax )   
-      CALL wrk_dealloc( (jpi+1)*(jpj+1), nind_i, nind_j )   ! integer 
+      CALL wrk_dealloc( (jpi+1)*(jpj+1), nind_i, nind_j ) 
       CALL wrk_dealloc( jpi,jpj, zhb0,zhb1,vt_i_init,vt_i_final,vt_s_init,vt_s_final,et_i_init,et_i_final,et_s_init,et_s_final )
 
@@ -552,6 +396,5 @@
 
 
-   SUBROUTINE lim_itd_fitline( num_cat, HbL, Hbr, hice,   &
-      &                        g0, g1, hL, hR, zremap_flag )
+   SUBROUTINE lim_itd_fitline( num_cat, HbL, Hbr, hice, g0, g1, hL, hR, zremap_flag )
       !!------------------------------------------------------------------
       !!                ***  ROUTINE lim_itd_fitline ***
@@ -572,5 +415,5 @@
       INTEGER , DIMENSION(jpi,jpj), INTENT(in   ) ::   zremap_flag  !
       !
-      INTEGER ::   ji,jj           ! horizontal indices
+      INTEGER  ::   ji,jj        ! horizontal indices
       REAL(wp) ::   zh13         ! HbL + 1/3 * (HbR - HbL)
       REAL(wp) ::   zh23         ! HbL + 2/3 * (HbR - HbL)
@@ -579,20 +422,17 @@
       !!------------------------------------------------------------------
       !
-      !
       DO jj = 1, jpj
          DO ji = 1, jpi
             !
             IF( zremap_flag(ji,jj) == 1 .AND. a_i(ji,jj,num_cat) > epsi10   &
-               &                        .AND. hice(ji,jj)        > 0._wp     ) THEN
+               &                        .AND. hice(ji,jj)        > 0._wp )  THEN
 
                ! Initialize hL and hR
-
                hL(ji,jj) = HbL(ji,jj)
                hR(ji,jj) = HbR(ji,jj)
 
                ! Change hL or hR if hice falls outside central third of range
-
-               zh13 = 1.0/3.0 * (2.0*hL(ji,jj) + hR(ji,jj))
-               zh23 = 1.0/3.0 * (hL(ji,jj) + 2.0*hR(ji,jj))
+               zh13 = 1.0 / 3.0 * ( 2.0 * hL(ji,jj) + hR(ji,jj) )
+               zh23 = 1.0 / 3.0 * ( hL(ji,jj) + 2.0 * hR(ji,jj) )
 
                IF    ( hice(ji,jj) < zh13 ) THEN   ;   hR(ji,jj) = 3._wp * hice(ji,jj) - 2._wp * hL(ji,jj)
@@ -601,17 +441,16 @@
 
                ! Compute coefficients of g(eta) = g0 + g1*eta
-
                zdhr = 1._wp / (hR(ji,jj) - hL(ji,jj))
                zwk1 = 6._wp * a_i(ji,jj,num_cat) * zdhr
                zwk2 = ( hice(ji,jj) - hL(ji,jj) ) * zdhr
-               g0(ji,jj) = zwk1 * ( 2._wp/3._wp - zwk2 )
-               g1(ji,jj) = 2._wp * zdhr * zwk1 * (zwk2 - 0.5)
+               g0(ji,jj) = zwk1 * ( 2._wp / 3._wp - zwk2 )
+               g1(ji,jj) = 2._wp * zdhr * zwk1 * ( zwk2 - 0.5 )
                !
-            ELSE                   ! remap_flag = .false. or a_i < epsi10 
+            ELSE  ! remap_flag = .false. or a_i < epsi10 
                hL(ji,jj) = 0._wp
                hR(ji,jj) = 0._wp
                g0(ji,jj) = 0._wp
                g1(ji,jj) = 0._wp
-            ENDIF                  ! a_i > epsi10
+            ENDIF
             !
          END DO
@@ -637,5 +476,5 @@
 
       INTEGER ::   ji, jj, jl, jl2, jl1, jk   ! dummy loop indices
-      INTEGER ::   ii, ij          ! indices when changing from 2D-1D is done
+      INTEGER ::   ii, ij                     ! indices when changing from 2D-1D is done
 
       REAL(wp), POINTER, DIMENSION(:,:,:) ::   zaTsfn
@@ -647,20 +486,13 @@
       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
 
-      INTEGER ::   nbrem             ! number of cells with ice to transfer
-
-      LOGICAL ::   zdaice_negative         ! true if daice < -puny
-      LOGICAL ::   zdvice_negative         ! true if dvice < -puny
-      LOGICAL ::   zdaice_greater_aicen    ! true if daice > aicen
-      LOGICAL ::   zdvice_greater_vicen    ! true if dvice > vicen
+      INTEGER  ::   nbrem             ! number of cells with ice to transfer
       !!------------------------------------------------------------------
 
       CALL wrk_alloc( jpi,jpj,jpl, zaTsfn )
       CALL wrk_alloc( jpi,jpj, zworka )
-      CALL wrk_alloc( (jpi+1)*(jpj+1), nind_i, nind_j )   ! integer
+      CALL wrk_alloc( (jpi+1)*(jpj+1), nind_i, nind_j )
 
       !----------------------------------------------------------------------------------------------
@@ -669,91 +501,9 @@
 
       DO jl = klbnd, kubnd
-         zaTsfn(:,:,jl) = a_i(:,:,jl)*t_su(:,:,jl)
-      END DO
-
-      !----------------------------------------------------------------------------------------------
-      ! 2) Check for daice or dvice out of range, allowing for roundoff error
-      !----------------------------------------------------------------------------------------------
-      ! Note: zdaice < 0 or zdvice < 0 usually happens when category jl
-      ! has a small area, with h(n) very close to a boundary.  Then
-      ! the coefficients of g(h) are large, and the computed daice and
-      ! dvice can be in error. If this happens, it is best to transfer
-      ! either the entire category or nothing at all, depending on which
-      ! side of the boundary hice(n) lies.
-      !-----------------------------------------------------------------
-      DO jl = klbnd, kubnd-1
-
-         zdaice_negative = .false.
-         zdvice_negative = .false.
-         zdaice_greater_aicen = .false.
-         zdvice_greater_vicen = .false.
-
-         DO jj = 1, jpj
-            DO ji = 1, jpi
-
-               IF (zdonor(ji,jj,jl) .GT. 0) THEN
-                  jl1 = zdonor(ji,jj,jl)
-
-                  IF (zdaice(ji,jj,jl) .LT. 0.0) THEN
-                     IF (zdaice(ji,jj,jl) .GT. -epsi10) THEN
-                        IF ( ( jl1.EQ.jl   .AND. ht_i(ji,jj,jl1) .GT. hi_max(jl) )           &
-                           .OR.                                      &
-                           ( jl1.EQ.jl+1 .AND. ht_i(ji,jj,jl1) .LE. hi_max(jl) )           &  
-                           ) THEN                                                             
-                           zdaice(ji,jj,jl) = a_i(ji,jj,jl1)  ! shift entire category
-                           zdvice(ji,jj,jl) = v_i(ji,jj,jl1)
-                        ELSE
-                           zdaice(ji,jj,jl) = 0.0 ! shift no ice
-                           zdvice(ji,jj,jl) = 0.0
-                        ENDIF
-                     ELSE
-                        zdaice_negative = .true.
-                     ENDIF
-                  ENDIF
-
-                  IF (zdvice(ji,jj,jl) .LT. 0.0) THEN
-                     IF (zdvice(ji,jj,jl) .GT. -epsi10 ) THEN
-                        IF ( ( jl1.EQ.jl .AND. ht_i(ji,jj,jl1).GT.hi_max(jl) )     &
-                           .OR.                                     &
-                           ( jl1.EQ.jl+1 .AND. ht_i(ji,jj,jl1) .LE. hi_max(jl) ) &
-                           ) THEN
-                           zdaice(ji,jj,jl) = a_i(ji,jj,jl1) ! shift entire category
-                           zdvice(ji,jj,jl) = v_i(ji,jj,jl1) 
-                        ELSE
-                           zdaice(ji,jj,jl) = 0.0    ! shift no ice
-                           zdvice(ji,jj,jl) = 0.0
-                        ENDIF
-                     ELSE
-                        zdvice_negative = .true.
-                     ENDIF
-                  ENDIF
-
-                  ! If daice is close to aicen, set daice = aicen.
-                  IF (zdaice(ji,jj,jl) .GT. a_i(ji,jj,jl1) - epsi10 ) THEN
-                     IF (zdaice(ji,jj,jl) .LT. a_i(ji,jj,jl1)+epsi10) THEN
-                        zdaice(ji,jj,jl) = a_i(ji,jj,jl1)
-                        zdvice(ji,jj,jl) = v_i(ji,jj,jl1) 
-                     ELSE
-                        zdaice_greater_aicen = .true.
-                     ENDIF
-                  ENDIF
-
-                  IF (zdvice(ji,jj,jl) .GT. v_i(ji,jj,jl1)-epsi10) THEN
-                     IF (zdvice(ji,jj,jl) .LT. v_i(ji,jj,jl1)+epsi10) THEN
-                        zdaice(ji,jj,jl) = a_i(ji,jj,jl1)
-                        zdvice(ji,jj,jl) = v_i(ji,jj,jl1) 
-                     ELSE
-                        zdvice_greater_vicen = .true.
-                     ENDIF
-                  ENDIF
-
-               ENDIF               ! donor > 0
-            END DO                   ! i
-         END DO                 ! j
-
-      END DO !jl
+         zaTsfn(:,:,jl) = a_i(:,:,jl) * t_su(:,:,jl)
+      END DO
 
       !-------------------------------------------------------------------------------
-      ! 3) Transfer volume and energy between categories
+      ! 2) Transfer volume and energy between categories
       !-------------------------------------------------------------------------------
 
@@ -762,9 +512,9 @@
          DO jj = 1, jpj
             DO ji = 1, jpi
-               IF (zdaice(ji,jj,jl) .GT. 0.0 ) THEN ! daice(n) can be < puny
+               IF (zdaice(ji,jj,jl) > 0.0 ) THEN ! daice(n) can be < puny
                   nbrem = nbrem + 1
                   nind_i(nbrem) = ji
                   nind_j(nbrem) = jj
-               ENDIF ! tmask
+               ENDIF
             END DO
          END DO
@@ -775,8 +525,8 @@
 
             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._wp , SIGN( 1._wp , 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 
+            ELSE                  ;   jl2 = jl 
             ENDIF
 
@@ -784,5 +534,4 @@
             ! Ice areas
             !--------------
-
             a_i(ii,ij,jl1) = a_i(ii,ij,jl1) - zdaice(ii,ij,jl)
             a_i(ii,ij,jl2) = a_i(ii,ij,jl2) + zdaice(ii,ij,jl)
@@ -791,5 +540,4 @@
             ! Ice volumes
             !--------------
-
             v_i(ii,ij,jl1) = v_i(ii,ij,jl1) - zdvice(ii,ij,jl) 
             v_i(ii,ij,jl2) = v_i(ii,ij,jl2) + zdvice(ii,ij,jl)
@@ -798,6 +546,5 @@
             ! Snow volumes
             !--------------
-
-            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 
@@ -806,6 +553,5 @@
             ! Snow heat content  
             !--------------------
-
-            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
@@ -814,6 +560,5 @@
             ! Ice age 
             !--------------
-
-            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
@@ -822,6 +567,5 @@
             ! Ice salinity
             !--------------
-
-            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
@@ -830,10 +574,9 @@
             ! Surface temperature
             !---------------------
-
-            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 
 
-         END DO                 ! ji
+         END DO
 
          !------------------
@@ -842,5 +585,4 @@
 
          DO jk = 1, nlay_i
-!CDIR NODEP
             DO ji = 1, nbrem
                ii = nind_i(ji)
@@ -848,5 +590,5 @@
 
                jl1 = zdonor(ii,ij,jl)
-               IF (jl1 .EQ. jl) THEN
+               IF (jl1 == jl) THEN
                   jl2 = jl+1
                ELSE             ! n1 = n+1
@@ -857,6 +599,6 @@
                e_i(ii,ij,jk,jl1) =  e_i(ii,ij,jk,jl1) - zdeice
                e_i(ii,ij,jk,jl2) =  e_i(ii,ij,jk,jl2) + zdeice 
-            END DO              ! ji
-         END DO                 ! jk
+            END DO
+         END DO
 
       END DO                   ! boundaries, 1 to ncat-1
@@ -872,21 +614,20 @@
                   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
                ELSE
                   ht_i(ji,jj,jl)  = 0._wp
-                  t_su(ji,jj,jl)  = rtt
+                  t_su(ji,jj,jl)  = rt0
                ENDIF
-            END DO                 ! ji
-         END DO                 ! jj
-      END DO                    ! jl
+            END DO
+         END DO
+      END DO
       !
       CALL wrk_dealloc( jpi,jpj,jpl, zaTsfn )
       CALL wrk_dealloc( jpi,jpj, zworka )
-      CALL wrk_dealloc( (jpi+1)*(jpj+1), nind_i, nind_j )   ! integer
+      CALL wrk_dealloc( (jpi+1)*(jpj+1), nind_i, nind_j )
       !
    END SUBROUTINE lim_itd_shiftice
    
 
-   SUBROUTINE lim_itd_th_reb( klbnd, kubnd, ntyp )
+   SUBROUTINE lim_itd_th_reb( klbnd, kubnd )
       !!------------------------------------------------------------------
       !!                ***  ROUTINE lim_itd_th_reb ***
@@ -898,5 +639,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
@@ -927,9 +667,6 @@
          DO jj = 1, jpj
             DO ji = 1, jpi 
-               IF( a_i(ji,jj,jl) > epsi10 ) THEN 
-                  ht_i(ji,jj,jl) = v_i(ji,jj,jl) / a_i(ji,jj,jl)
-               ELSE
-                  ht_i(ji,jj,jl) = 0._wp
-               ENDIF
+               rswitch = MAX( 0._wp , SIGN( 1._wp, a_i(ji,jj,jl) - epsi10 ) )
+               ht_i(ji,jj,jl) = v_i (ji,jj,jl) / MAX( a_i(ji,jj,jl) , epsi10 ) * rswitch
             END DO
          END DO
@@ -937,19 +674,5 @@
 
       !------------------------------------------------------------------------------
-      ! 2) Make sure thickness of cat klbnd is at least hi_max_typ(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)
-               ENDIF
-            ENDIF
-         END DO
-      END DO
-
-      !------------------------------------------------------------------------------
-      ! 3) If a category thickness is not in bounds, shift the
+      ! 2) If a category thickness is not in bounds, shift the
       ! entire area, volume, and energy to the neighboring category
       !------------------------------------------------------------------------------
@@ -980,15 +703,13 @@
                   zdonor(ji,jj,jl)  = jl 
                   ! begin TECLIM change
-                  !zdaice(ji,jj,jl)  = a_i(ji,jj,jl)
-                  !zdvice(ji,jj,jl)  = v_i(ji,jj,jl)
                   !zdaice(ji,jj,jl)  = a_i(ji,jj,jl) * 0.5_wp
                   !zdvice(ji,jj,jl)  = v_i(ji,jj,jl)-zdaice(ji,jj,jl)*(hi_max(jl)+hi_max(jl-1)) * 0.5_wp
                   ! end TECLIM change 
                   ! clem: how much of a_i you send in cat sup is somewhat arbitrary
-                  zdaice(ji,jj,jl)  = a_i(ji,jj,jl) * ( ht_i(ji,jj,jl) - hi_max(jl) + epsi10 ) / ht_i(ji,jj,jl)  
-                  zdvice(ji,jj,jl)  = v_i(ji,jj,jl) - ( a_i(ji,jj,jl) - zdaice(ji,jj,jl) ) * ( hi_max(jl) - epsi10 )
+                  zdaice(ji,jj,jl)  = a_i(ji,jj,jl) * ( ht_i(ji,jj,jl) - hi_max(jl) + epsi20 ) / ht_i(ji,jj,jl)  
+                  zdvice(ji,jj,jl)  = v_i(ji,jj,jl) - ( a_i(ji,jj,jl) - zdaice(ji,jj,jl) ) * ( hi_max(jl) - epsi20 )
                ENDIF
-            END DO                 ! ji
-         END DO                 ! jj
+            END DO
+         END DO
          IF(lk_mpp)   CALL mpp_max( zshiftflag )
 
@@ -1001,5 +722,5 @@
          ENDIF
          !
-      END DO                    ! jl
+      END DO
 
       !----------------------------
@@ -1014,45 +735,30 @@
          zshiftflag = 0
 
-!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
-                  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) 
+               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
-         ! clem-change end
-
-      END DO                    ! jl
+            END DO
+         END DO
+
+         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
+
+      END DO
 
       !------------------------------------------------------------------------------
-      ! 4) Conservation check
+      ! 3) Conservation check
       !------------------------------------------------------------------------------
 
@@ -1067,5 +773,5 @@
       ENDIF
       !
-      CALL wrk_dealloc( jpi,jpj,jpl, zdonor )   ! interger
+      CALL wrk_dealloc( jpi,jpj,jpl, zdonor )
       CALL wrk_dealloc( jpi,jpj,jpl, zdaice, zdvice )
       CALL wrk_dealloc( jpi,jpj, vt_i_init, vt_i_final, vt_s_init, vt_s_final )
@@ -1078,8 +784,4 @@
    !!----------------------------------------------------------------------
 CONTAINS
-   SUBROUTINE lim_itd_th           ! Empty routines
-   END SUBROUTINE lim_itd_th
-   SUBROUTINE lim_itd_th_ini
-   END SUBROUTINE lim_itd_th_ini
    SUBROUTINE lim_itd_th_rem
    END SUBROUTINE lim_itd_th_rem
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limmsh.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limmsh.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limmsh.F90	(revision 5832)
@@ -23,5 +23,5 @@
    PRIVATE
 
-   PUBLIC   lim_msh   ! routine called by ice_ini.F90
+   PUBLIC   lim_msh   ! routine called by sbcice_lim.F90
 
    !!----------------------------------------------------------------------
@@ -41,5 +41,4 @@
       !!              - Definition of some constants linked with the grid
       !!              - Definition of the metric coef. for the sea/ice
-      !!              - Initialization of the ice masks (tmsk, umsk)
       !! 
       !! Reference  : Deleersnijder et al. Ocean Modelling 100, 7-10 
@@ -103,17 +102,4 @@
 !!gm end
 
-      !                           !==  ice masks  ==!
-      tms(:,:) = tmask(:,:,1)             ! ice T-point  : use surface tmask
-      tmu(:,:) = umask(:,:,1)             ! ice U-point  : use surface umask  (C-grid EVP)
-      tmv(:,:) = vmask(:,:,1)             ! ice V-point  : use surface vmask  (C-grid EVP)
-      DO jj = 1, jpjm1                    ! ice F-point  : recompute fmask (due to nn_shlat)
-         DO ji = 1 , jpim1   ! NO vector opt.
-            tmf(ji,jj) =  tms(ji,jj) * tms(ji+1,jj) * tms(ji,jj+1) * tms(ji+1,jj+1)
-         END DO
-      END DO
-      CALL lbc_lnk( tmf(:,:), 'F', 1. )           ! lateral boundary conditions
-
-      !                           !==  unmasked and masked area of T-grid cell
-      area(:,:) = e1t(:,:) * e2t(:,:)
       !
    END SUBROUTINE lim_msh
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limrhg.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limrhg.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limrhg.F90	(revision 5832)
@@ -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"
@@ -106,5 +102,5 @@
       !!                 and charge ellipse.
       !!                 The user should make sure that the parameters
-      !!                 nevp, telast and creepl maintain stress state
+      !!                 nn_nevp, elastic time scale and rn_creepl maintain stress state
       !!                 on the charge ellipse for plastic flow
       !!                 e.g. in the Canadian Archipelago
@@ -112,5 +108,4 @@
       !! References : Hunke and Dukowicz, JPO97
       !!              Bouillon et al., Ocean Modelling 2009
-      !!              Vancoppenolle et al., Ocean Modelling 2008
       !!-------------------------------------------------------------------
       INTEGER, INTENT(in) ::   k_j1    ! southern j-index for ice computation
@@ -121,17 +116,17 @@
       CHARACTER (len=50) ::   charout
       REAL(wp) ::   zt11, zt12, zt21, zt22, ztagnx, ztagny, delta                         !
-      REAL(wp) ::   za, zstms, zsang, zmask   ! local scalars
-
-      REAL(wp) ::   dtevp              ! time step for subcycling
-      REAL(wp) ::   dtotel, ecc2, ecci ! square of yield ellipse eccenticity
-      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) ::   zdsshx, zdsshy     ! term for the gradient of ocean surface
-      REAL(wp) ::   sigma1, sigma2     ! internal ice stress
+      REAL(wp) ::   za, zstms          ! local scalars
+      REAL(wp) ::   zc1, zc2, zc3      ! ice mass
+
+      REAL(wp) ::   dtevp , z1_dtevp              ! time step for subcycling
+      REAL(wp) ::   dtotel, z1_dtotel, ecc2, ecci ! square of yield ellipse eccenticity
+      REAL(wp) ::   z0, zr, zcca, zccb            ! temporary scalars
+      REAL(wp) ::   zu_ice2, zv_ice1              !
+      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,15 +137,12 @@
       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
+      REAL(wp), POINTER, DIMENSION(:,:) ::   v_oce1           ! ocean u/v component on U points                           
+      REAL(wp), POINTER, DIMENSION(:,:) ::   u_oce2           ! ocean u/v component on V points
       REAL(wp), POINTER, DIMENSION(:,:) ::   u_ice2, v_ice1   ! ice u/v component on V/U point
       REAL(wp), POINTER, DIMENSION(:,:) ::   zf1   , zf2      ! arrays for internal stresses
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zmask            ! mask ocean grid points
       
-      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
@@ -159,52 +151,54 @@
                                                               !   ocean surface (ssh_m) if ice is not embedded
                                                               !   ice top surface if ice is embedded   
+
+      REAL(wp), PARAMETER               ::   zepsi = 1.0e-20_wp ! tolerance parameter
+      REAL(wp), PARAMETER               ::   zvmin = 1.0e-03_wp ! ice volume below which ice velocity equals ocean velocity
       !!-------------------------------------------------------------------
 
       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_oce2, u_ice2, v_oce1 , v_ice1 , zmask               )
+      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
 # if defined key_agrif
-     USE ice_2, vt_s => hsnm
-     USE ice_2, vt_i => hicm
+      USE ice_2, vt_s => hsnm
+      USE ice_2, vt_i => hicm
 # else
-     vt_s => hsnm
-     vt_i => hicm
+      vt_s => hsnm
+      vt_i => hicm
 # endif
-     at_i(:,:) = 1. - frld(:,:)
+      at_i(:,:) = 1. - frld(:,:)
 #endif
 #if defined key_agrif && defined key_lim2 
-    CALL agrif_rhg_lim2_load      ! First interpolation of coarse values
-#endif
-      !
-      !------------------------------------------------------------------------------!
-      ! 1) Ice-Snow mass (zc1), ice strength (zpresh)                                !
+      CALL agrif_rhg_lim2_load      ! First interpolation of coarse values
+#endif
+      !
+      !------------------------------------------------------------------------------!
+      ! 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
-      CALL lim_itd_me_icestrength( ridge_scheme_swi )      ! LIM-3: Ice strength on T-points
-#endif
-
-!CDIR NOVERRCHK
+      CALL lim_itd_me_icestrength( nn_icestr )      ! LIM-3: Ice strength on T-points
+#endif
+
       DO jj = k_j1 , k_jpj       ! Ice mass and temp variables
-!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)
+            zpresh(ji,jj) = tmask(ji,jj,1) *  strength(ji,jj)
 #endif
 #if defined key_lim2
-            zpresh(ji,jj) = tms(ji,jj) *  pstar * vt_i(ji,jj) * EXP( -c_rhg * (1. - at_i(ji,jj) ) )
-#endif
-            ! tmi = 1 where there is ice or on land
-            tmi(ji,jj)    = 1._wp - ( 1._wp - MAX( 0._wp , SIGN ( 1._wp , vt_i(ji,jj) - epsd ) ) ) * tms(ji,jj)
+            zpresh(ji,jj) = tmask(ji,jj,1) *  pstar * vt_i(ji,jj) * EXP( -c_rhg * (1. - at_i(ji,jj) ) )
+#endif
+            ! zmask = 1 where there is ice or on land
+            zmask(ji,jj)    = 1._wp - ( 1._wp - MAX( 0._wp , SIGN ( 1._wp , vt_i(ji,jj) - zepsi ) ) ) * tmask(ji,jj,1)
          END DO
       END DO
@@ -212,21 +206,13 @@
       ! Ice strength on grid cell corners (zpreshc)
       ! needed for calculation of shear stress 
-!CDIR NOVERRCHK
       DO jj = k_j1+1, k_jpj-1
-!CDIR NOVERRCHK
          DO ji = 2, jpim1 !RB caution no fs_ (ji+1,jj+1)
-            zstms          =  tms(ji+1,jj+1) * wght(ji+1,jj+1,2,2) + &
-               &              tms(ji,jj+1)   * wght(ji+1,jj+1,1,2) + &
-               &              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)
+            zstms          =  tmask(ji+1,jj+1,1) * wght(ji+1,jj+1,2,2) + tmask(ji,jj+1,1) * wght(ji+1,jj+1,1,2) +   &
+               &              tmask(ji+1,jj,1)   * wght(ji+1,jj+1,2,1) + tmask(ji,jj,1)   * wght(ji+1,jj+1,1,1)
+            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)     &
+               &             ) / MAX( zstms, zepsi )
          END DO
       END DO
-
       CALL lbc_lnk( zpreshc(:,:), 'F', 1. )
       !
@@ -243,21 +229,19 @@
       !  zcorl2: Coriolis parameter on V-points                            
       !  (ztagnx,ztagny): wind stress on U/V points                       
-      !  u_oce1: ocean u component on u points                           
       !  v_oce1: ocean v component on u points                          
       !  u_oce2: ocean u component on v points                         
-      !  v_oce2: ocean v component on v points                        
 
       IF( nn_ice_embd == 2 ) THEN             !== embedded sea ice: compute representative ice top surface ==!
-          !                                            
-          ! average interpolation coeff as used in dynspg = (1/nn_fsbc) * {SUM[n/nn_fsbc], n=0,nn_fsbc-1}
-          !                                               = (1/nn_fsbc)^2 * {SUM[n], n=0,nn_fsbc-1}
+         !                                            
+         ! average interpolation coeff as used in dynspg = (1/nn_fsbc) * {SUM[n/nn_fsbc], n=0,nn_fsbc-1}
+         !                                               = (1/nn_fsbc)^2 * {SUM[n], n=0,nn_fsbc-1}
          zintn = REAL( nn_fsbc - 1 ) / REAL( nn_fsbc ) * 0.5_wp     
-          !
-          ! average interpolation coeff as used in dynspg = (1/nn_fsbc) * {SUM[1-n/nn_fsbc], n=0,nn_fsbc-1}
-          !                                               = (1/nn_fsbc)^2 * (nn_fsbc^2 - {SUM[n], n=0,nn_fsbc-1})
+         !
+         ! average interpolation coeff as used in dynspg = (1/nn_fsbc) * {SUM[1-n/nn_fsbc], n=0,nn_fsbc-1}
+         !                                               = (1/nn_fsbc)^2 * (nn_fsbc^2 - {SUM[n], n=0,nn_fsbc-1})
          zintb = REAL( nn_fsbc + 1 ) / REAL( nn_fsbc ) * 0.5_wp
-          !
+         !
          zpice(:,:) = ssh_m(:,:) + (  zintn * snwice_mass(:,:) +  zintb * snwice_mass_b(:,:)  ) * r1_rau0
-          !
+         !
       ELSE                                    !== non-embedded sea ice: use ocean surface for slope calculation ==!
          zpice(:,:) = ssh_m(:,:)
@@ -267,32 +251,33 @@
          DO ji = fs_2, fs_jpim1
 
-            zt11 = tms(ji  ,jj) * e1t(ji  ,jj)
-            zt12 = tms(ji+1,jj) * e1t(ji+1,jj)
-            zt21 = tms(ji,jj  ) * e2t(ji,jj  )
-            zt22 = tms(ji,jj+1) * e2t(ji,jj+1)
+            zc1 = tmask(ji  ,jj  ,1) * ( rhosn * vt_s(ji  ,jj  ) + rhoic * vt_i(ji  ,jj  ) )
+            zc2 = tmask(ji+1,jj  ,1) * ( rhosn * vt_s(ji+1,jj  ) + rhoic * vt_i(ji+1,jj  ) )
+            zc3 = tmask(ji  ,jj+1,1) * ( rhosn * vt_s(ji  ,jj+1) + rhoic * vt_i(ji  ,jj+1) )
+
+            zt11 = tmask(ji  ,jj,1) * e1t(ji  ,jj)
+            zt12 = tmask(ji+1,jj,1) * e1t(ji+1,jj)
+            zt21 = tmask(ji,jj  ,1) * e2t(ji,jj  )
+            zt22 = tmask(ji,jj+1,1) * e2t(ji,jj+1)
 
             ! Leads area.
-            zfrld1(ji,jj) = ( zt12 * ( 1.0 - at_i(ji,jj) ) + zt11 * ( 1.0 - at_i(ji+1,jj) ) ) / ( zt11 + zt12 + epsd )
-            zfrld2(ji,jj) = ( zt22 * ( 1.0 - at_i(ji,jj) ) + zt21 * ( 1.0 - at_i(ji,jj+1) ) ) / ( zt21 + zt22 + epsd )
+            zfrld1(ji,jj) = ( zt12 * ( 1.0 - at_i(ji,jj) ) + zt11 * ( 1.0 - at_i(ji+1,jj) ) ) / ( zt11 + zt12 + zepsi )
+            zfrld2(ji,jj) = ( zt22 * ( 1.0 - at_i(ji,jj) ) + zt21 * ( 1.0 - at_i(ji,jj+1) ) ) / ( zt21 + zt22 + zepsi )
 
             ! 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)
-            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 )
-            zcorl2(ji,jj) = zmass2(ji,jj) * ( e2t(ji,jj+1)*fcor(ji,jj) + e2t(ji,jj)*fcor(ji,jj+1) )   &
-               &                          / ( e2t(ji,jj+1) + e2t(ji,jj) + epsd )
+            zmass1(ji,jj) = ( zt12 * zc1 + zt11 * zc2 ) / ( zt11 + zt12 + zepsi )
+            zmass2(ji,jj) = ( zt22 * zc1 + zt21 * zc3 ) / ( zt21 + zt22 + zepsi )
+            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) + zepsi )
+            zcorl2(ji,jj) = zmass2(ji,jj) * ( e2t(ji,jj+1) * fcor(ji,jj) + e2t(ji,jj) * fcor(ji,jj+1) )   &
+               &                          / ( e2t(ji,jj+1) + e2t(ji,jj) + zepsi )
             !
-            u_oce1(ji,jj)  = u_oce(ji,jj)
-            v_oce2(ji,jj)  = v_oce(ji,jj)
-
             ! Ocean has no slip boundary condition
-            v_oce1(ji,jj)  = 0.5*( (v_oce(ji,jj)+v_oce(ji,jj-1))*e1t(ji,jj)    &
-               &                 +(v_oce(ji+1,jj)+v_oce(ji+1,jj-1))*e1t(ji+1,jj)) &
-               &               /(e1t(ji+1,jj)+e1t(ji,jj)) * tmu(ji,jj)  
-
-            u_oce2(ji,jj)  = 0.5*((u_oce(ji,jj)+u_oce(ji-1,jj))*e2t(ji,jj)     &
-               &                 +(u_oce(ji,jj+1)+u_oce(ji-1,jj+1))*e2t(ji,jj+1)) &
-               &                / (e2t(ji,jj+1)+e2t(ji,jj)) * tmv(ji,jj)
+            v_oce1(ji,jj)  = 0.5 * ( ( v_oce(ji  ,jj) + v_oce(ji  ,jj-1) ) * e1t(ji,jj)      &
+               &                   + ( v_oce(ji+1,jj) + v_oce(ji+1,jj-1) ) * e1t(ji+1,jj) )  &
+               &                   / ( e1t(ji+1,jj) + e1t(ji,jj) ) * umask(ji,jj,1)  
+
+            u_oce2(ji,jj)  = 0.5 * ( ( u_oce(ji,jj  ) + u_oce(ji-1,jj  ) ) * e2t(ji,jj)      &
+               &                   + ( u_oce(ji,jj+1) + u_oce(ji-1,jj+1) ) * e2t(ji,jj+1) )  &
+               &                   / ( e2t(ji,jj+1) + e2t(ji,jj) ) * vmask(ji,jj,1)
 
             ! Wind stress at U,V-point
@@ -306,6 +291,6 @@
             ! include it later
 
-            zdsshx =  ( zpice(ji+1,jj) - zpice(ji,jj) ) / e1u(ji,jj)
-            zdsshy =  ( zpice(ji,jj+1) - zpice(ji,jj) ) / e2v(ji,jj)
+            zdsshx =  ( zpice(ji+1,jj) - zpice(ji,jj) ) * r1_e1u(ji,jj)
+            zdsshy =  ( zpice(ji,jj+1) - zpice(ji,jj) ) * r1_e2v(ji,jj)
 
             za1ct(ji,jj) = ztagnx - zmass1(ji,jj) * grav * zdsshx
@@ -321,9 +306,14 @@
       !
       ! Time step for subcycling
-      dtevp  = rdt_ice / nevp
+      dtevp  = rdt_ice / nn_nevp
+#if defined key_lim3
+      dtotel = dtevp / ( 2._wp * rn_relast * rdt_ice )
+#else
       dtotel = dtevp / ( 2._wp * telast )
-
+#endif
+      z1_dtotel = 1._wp / ( 1._wp + dtotel )
+      z1_dtevp  = 1._wp / dtevp
       !-ecc2: square of yield ellipse eccenticrity (reminder: must become a namelist parameter)
-      ecc2 = ecc * ecc
+      ecc2 = rn_ecc * rn_ecc
       ecci = 1. / ecc2
 
@@ -334,5 +324,5 @@
 
       !                                               !----------------------!
-      DO jter = 1 , nevp                              !    loop over jter    !
+      DO jter = 1 , nn_nevp                           !    loop over jter    !
          !                                            !----------------------!        
          DO jj = k_j1, k_jpj-1
@@ -342,9 +332,9 @@
 
          DO jj = k_j1+1, k_jpj-1
-            DO ji = fs_2, jpim1   !RB bug no vect opt due to tmi
+            DO ji = fs_2, fs_jpim1   !RB bug no vect opt due to zmask
 
                !  
                !- 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 +345,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,142 +353,80 @@
                !
                !
-               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)
-
-               zdt(ji,jj) = ( ( u_ice(ji,jj)/e2u(ji,jj)                    &
-                  &            -u_ice(ji-1,jj)/e2u(ji-1,jj)                &
-                  &           )*e2t(ji,jj)*e2t(ji,jj)                      &
-                  &          -( v_ice(ji,jj)/e1v(ji,jj)                    &
-                  &            -v_ice(ji,jj-1)/e1v(ji,jj-1)                &
-                  &           )*e1t(ji,jj)*e1t(ji,jj)                      &
-                  &         )                                              &
-                  &        / 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)   &
+                  &            ) * r1_e12t(ji,jj)
+
+               zdt(ji,jj) = ( ( u_ice(ji,jj) * r1_e2u(ji,jj) - u_ice(ji-1,jj) * r1_e2u(ji-1,jj) ) * e2t(ji,jj) * e2t(ji,jj)   &
+                  &         - ( v_ice(ji,jj) * r1_e1v(ji,jj) - v_ice(ji,jj-1) * r1_e1v(ji,jj-1) ) * e1t(ji,jj) * e1t(ji,jj)   &
+                  &         ) * r1_e12t(ji,jj)
 
                !
-               zds(ji,jj) = ( ( u_ice(ji,jj+1)/e1u(ji,jj+1)                &
-                  &            -u_ice(ji,jj)/e1u(ji,jj)                    &
-                  &           )*e1f(ji,jj)*e1f(ji,jj)                      &
-                  &          +( v_ice(ji+1,jj)/e2v(ji+1,jj)                &
-                  &            -v_ice(ji,jj)/e2v(ji,jj)                    &
-                  &           )*e2f(ji,jj)*e2f(ji,jj)                      &
-                  &         )                                              &
-                  &        / ( e1f(ji,jj) * e2f(ji,jj) ) * ( 2.0 - tmf(ji,jj) ) &
-                  &        * tmi(ji,jj) * tmi(ji,jj+1)                     &
-                  &        * tmi(ji+1,jj) * tmi(ji+1,jj+1)
-
-
-               v_ice1(ji,jj)  = 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) 
-
-               u_ice2(ji,jj)  = 0.5*( (u_ice(ji,jj)+u_ice(ji-1,jj))*e2t(ji,jj+1)   &
-                  &                 +(u_ice(ji,jj+1)+u_ice(ji-1,jj+1))*e2t(ji,jj)) &
-                  &               /(e2t(ji,jj+1)+e2t(ji,jj)) * tmv(ji,jj)
-
-            END DO
-         END DO
-         CALL lbc_lnk( v_ice1, 'U', -1. )   ;   CALL lbc_lnk( u_ice2, 'V', -1. )      ! lateral boundary cond.
-
-!CDIR NOVERRCHK
+               zds(ji,jj) = ( ( u_ice(ji,jj+1) * r1_e1u(ji,jj+1) - u_ice(ji,jj) * r1_e1u(ji,jj) ) * e1f(ji,jj) * e1f(ji,jj)   &
+                  &         + ( v_ice(ji+1,jj) * r1_e2v(ji+1,jj) - v_ice(ji,jj) * r1_e2v(ji,jj) ) * e2f(ji,jj) * e2f(ji,jj)   &
+                  &         ) * r1_e12f(ji,jj) * ( 2._wp - fmask(ji,jj,1) )   &
+                  &         * zmask(ji,jj) * zmask(ji,jj+1) * zmask(ji+1,jj) * zmask(ji+1,jj+1)
+
+
+               v_ice1(ji,jj)  = 0.5_wp * ( ( 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) ) * umask(ji,jj,1) 
+
+               u_ice2(ji,jj)  = 0.5_wp * ( ( u_ice(ji,jj  ) + u_ice(ji-1,jj  ) ) * e2t(ji,jj+1)     &
+                  &                      + ( u_ice(ji,jj+1) + u_ice(ji-1,jj+1) ) * e2t(ji,jj  ) )   &
+                  &                      / ( e2t(ji,jj+1) + e2t(ji,jj) ) * vmask(ji,jj,1)
+            END DO
+         END DO
+
+         CALL lbc_lnk_multi( v_ice1, 'U', -1., u_ice2, 'V', -1. )      ! lateral boundary cond.
+         
          DO jj = k_j1+1, k_jpj-1
-!CDIR NOVERRCHK
             DO ji = fs_2, fs_jpim1
 
                !- Calculate Delta at centre of grid cells
-               zzdst      = (  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)
-
-               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
-               !-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) )  &
-                  &         * zpresh(ji,jj) ) ) / ( 1._wp + dtotel )
-               zs2(ji,jj) = ( zs2(ji,jj) + dtotel * ( ecci * zdt(ji,jj) / deltat(ji,jj) * zpresh(ji,jj) ) )  &
-                  &         / ( 1._wp + dtotel )
-
-            END DO
-         END DO
-
-         CALL lbc_lnk( zs1(:,:), 'T', 1. )
-         CALL lbc_lnk( zs2(:,:), 'T', 1. )
-
-!CDIR NOVERRCHK
-         DO jj = k_j1+1, k_jpj-1
-!CDIR NOVERRCHK
-            DO ji = fs_2, fs_jpim1
+               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)   &
+                  &            ) * r1_e12t(ji,jj)
+
+               delta          = SQRT( divu_i(ji,jj)**2 + ( zdt(ji,jj)**2 + zdst**2 ) * usecc2 )  
+               delta_i(ji,jj) = delta + rn_creepl
+
                !- Calculate Delta on corners
-               zddc  =      ( ( v_ice1(ji,jj+1)/e1u(ji,jj+1)                &
-                  &            -v_ice1(ji,jj)/e1u(ji,jj)                    &
-                  &           )*e1f(ji,jj)*e1f(ji,jj)                       &
-                  &          +( u_ice2(ji+1,jj)/e2v(ji+1,jj)                &
-                  &            -u_ice2(ji,jj)/e2v(ji,jj)                    &
-                  &           )*e2f(ji,jj)*e2f(ji,jj)                       &
-                  &         )                                               &
-                  &        / ( e1f(ji,jj) * e2f(ji,jj) )
-
-               zdtc  =      (-( v_ice1(ji,jj+1)/e1u(ji,jj+1)                &
-                  &            -v_ice1(ji,jj)/e1u(ji,jj)                    &
-                  &           )*e1f(ji,jj)*e1f(ji,jj)                       &
-                  &          +( u_ice2(ji+1,jj)/e2v(ji+1,jj)                &
-                  &            -u_ice2(ji,jj)/e2v(ji,jj)                    &
-                  &           )*e2f(ji,jj)*e2f(ji,jj)                       &
-                  &         )                                               &
-                  &        / ( e1f(ji,jj) * e2f(ji,jj) )
-
-               deltac(ji,jj) = 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) ) )  &
-                  &          / ( 1.0 + dtotel ) 
-
-            END DO ! ji
-         END DO ! jj
-
-         CALL lbc_lnk( zs12(:,:), 'F', 1. )
-
+               zddc  = (  ( v_ice1(ji,jj+1) * r1_e1u(ji,jj+1) - v_ice1(ji,jj) * r1_e1u(ji,jj) ) * e1f(ji,jj) * e1f(ji,jj)  &
+                  &     + ( u_ice2(ji+1,jj) * r1_e2v(ji+1,jj) - u_ice2(ji,jj) * r1_e2v(ji,jj) ) * e2f(ji,jj) * e2f(ji,jj)  &
+                  &    ) * r1_e12f(ji,jj)
+
+               zdtc  = (- ( v_ice1(ji,jj+1) * r1_e1u(ji,jj+1) - v_ice1(ji,jj) * r1_e1u(ji,jj) ) * e1f(ji,jj) * e1f(ji,jj)  &
+                  &     + ( u_ice2(ji+1,jj) * r1_e2v(ji+1,jj) - u_ice2(ji,jj) * r1_e2v(ji,jj) ) * e2f(ji,jj) * e2f(ji,jj)  &
+                  &    ) * r1_e12f(ji,jj)
+
+               zddc = SQRT( zddc**2 + ( zdtc**2 + zds(ji,jj)**2 ) * usecc2 ) + rn_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 * ( divu_i(ji,jj) - delta ) / delta_i(ji,jj)   * zpresh(ji,jj)   &
+                  &          ) * z1_dtotel
+               zs2(ji,jj)  = ( zs2 (ji,jj) + dtotel *         ecci * zdt(ji,jj) / delta_i(ji,jj)   * zpresh(ji,jj)   &
+                  &          ) * z1_dtotel
+               !-Calculate stress tensor component zs12 at corners
+               zs12(ji,jj) = ( zs12(ji,jj) + dtotel *         ecci * zds(ji,jj) / ( 2._wp * zddc ) * zpreshc(ji,jj)  &
+                  &          ) * z1_dtotel 
+
+            END DO
+         END DO
+
+         CALL lbc_lnk_multi( zs1 , 'T', 1., zs2, 'T', 1., zs12, 'F', 1. )
+ 
          ! Ice internal stresses (Appendix C of Hunke and Dukowicz, 2002)
          DO jj = k_j1+1, k_jpj-1
             DO ji = fs_2, fs_jpim1
                !- contribution of zs1, zs2 and zs12 to zf1
-               zf1(ji,jj) = 0.5*( (zs1(ji+1,jj)-zs1(ji,jj))*e2u(ji,jj) &
-                  &              +(zs2(ji+1,jj)*e2t(ji+1,jj)**2-zs2(ji,jj)*e2t(ji,jj)**2)/e2u(ji,jj) &
-                  &              +2.0*(zs12(ji,jj)*e1f(ji,jj)**2-zs12(ji,jj-1)*e1f(ji,jj-1)**2)/e1u(ji,jj) &
-                  &             ) / ( e1u(ji,jj)*e2u(ji,jj) )
+               zf1(ji,jj) = 0.5 * ( ( zs1(ji+1,jj) - zs1(ji,jj) ) * e2u(ji,jj)  &
+                  &             + ( zs2(ji+1,jj) * e2t(ji+1,jj)**2 - zs2(ji,jj) * e2t(ji,jj)**2 ) * r1_e2u(ji,jj)          &
+                  &             + 2.0 * ( zs12(ji,jj) * e1f(ji,jj)**2 - zs12(ji,jj-1) * e1f(ji,jj-1)**2 ) * r1_e1u(ji,jj)  &
+                  &                ) * r1_e12u(ji,jj)
                ! contribution of zs1, zs2 and zs12 to zf2
-               zf2(ji,jj) = 0.5*( (zs1(ji,jj+1)-zs1(ji,jj))*e1v(ji,jj) &
-                  &              -(zs2(ji,jj+1)*e1t(ji,jj+1)**2 - zs2(ji,jj)*e1t(ji,jj)**2)/e1v(ji,jj) &
-                  &              + 2.0*(zs12(ji,jj)*e2f(ji,jj)**2 -    &
-                  zs12(ji-1,jj)*e2f(ji-1,jj)**2)/e2v(ji,jj) &
-                  &             ) / ( e1v(ji,jj)*e2v(ji,jj) )
+               zf2(ji,jj) = 0.5 * ( ( zs1(ji,jj+1) - zs1(ji,jj) ) * e1v(ji,jj)  &
+                  &             - ( zs2(ji,jj+1) * e1t(ji,jj+1)**2 - zs2(ji,jj) * e1t(ji,jj)**2 ) * r1_e1v(ji,jj)          &
+                  &             + 2.0 * ( zs12(ji,jj) * e2f(ji,jj)**2 - zs12(ji-1,jj) * e2f(ji-1,jj)**2 ) * r1_e2v(ji,jj)  &
+                  &               )  * r1_e12v(ji,jj)
             END DO
          END DO
@@ -510,24 +438,19 @@
          IF (MOD(jter,2).eq.0) THEN 
 
-!CDIR NOVERRCHK
             DO jj = k_j1+1, k_jpj-1
-!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
-                  z0           = zmass1(ji,jj)/dtevp
+                  rswitch      = ( 1.0 - MAX( 0._wp, SIGN( 1._wp, -zmass1(ji,jj) ) ) ) * umask(ji,jj,1)
+                  z0           = zmass1(ji,jj) * z1_dtevp
 
                   ! SB modif because ocean has no slip boundary condition
-                  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))   &
-                     &               /(e1t(ji+1,jj)+e1t(ji,jj)) * tmu(ji,jj)
-                  za           = rhoco*SQRT((u_ice(ji,jj)-u_oce1(ji,jj))**2 + &
-                     (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
-                  u_ice(ji,jj) = (zr+zccb*zv_ice1)/(zcca+epsd)*zmask 
-
+                  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) )   &
+                     &                 / ( e1t(ji+1,jj) + e1t(ji,jj) ) * umask(ji,jj,1)
+                  za           = rhoco * SQRT( ( u_ice(ji,jj) - u_oce(ji,jj) )**2 +  &
+                     &                         ( 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 * u_oce(ji,jj)
+                  zcca         = z0 + za
+                  zccb         = zcorl1(ji,jj)
+                  u_ice(ji,jj) = ( zr + zccb * zv_ice1 ) / ( zcca + zepsi ) * rswitch 
                END DO
             END DO
@@ -535,31 +458,25 @@
             CALL lbc_lnk( u_ice(:,:), 'U', -1. )
 #if defined key_agrif && defined key_lim2
-            CALL agrif_rhg_lim2( jter, nevp, 'U' )
+            CALL agrif_rhg_lim2( jter, nn_nevp, 'U' )
 #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         
 
-!CDIR NOVERRCHK
             DO jj = k_j1+1, k_jpj-1
-!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
-                  z0           = zmass2(ji,jj)/dtevp
+                  rswitch      = ( 1.0 - MAX( 0._wp, SIGN( 1._wp, -zmass2(ji,jj) ) ) ) * vmask(ji,jj,1)
+                  z0           = zmass2(ji,jj) * z1_dtevp
                   ! SB modif because ocean has no slip boundary condition
-                  zu_ice2       = 0.5*( (u_ice(ji,jj)+u_ice(ji-1,jj))*e2t(ji,jj)     &
-                     &                 + (u_ice(ji,jj+1)+u_ice(ji-1,jj+1))*e2t(ji,jj+1))   &
-                     &               /(e2t(ji,jj+1)+e2t(ji,jj)) * tmv(ji,jj)
-                  za           = rhoco*SQRT((zu_ice2-u_oce2(ji,jj))**2 + & 
-                     (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
-                  v_ice(ji,jj) = (zr-zccb*zu_ice2)/(zcca+epsd)*zmask
-
+                  zu_ice2      = 0.5 * ( ( u_ice(ji,jj  ) + u_ice(ji-1,jj  ) ) * e2t(ji,jj)       &
+                     &                 + ( u_ice(ji,jj+1) + u_ice(ji-1,jj+1) ) * e2t(ji,jj+1) )   &
+                     &                 / ( e2t(ji,jj+1) + e2t(ji,jj) ) * vmask(ji,jj,1)
+                  za           = rhoco * SQRT( ( zu_ice2 - u_oce2(ji,jj) )**2 +  & 
+                     &                         ( v_ice(ji,jj) - v_oce(ji,jj))**2 ) * ( 1.0 - zfrld2(ji,jj) )
+                  zr           = z0 * v_ice(ji,jj) + zf2(ji,jj) + za2ct(ji,jj) + za * v_oce(ji,jj)
+                  zcca         = z0 + za
+                  zccb         = zcorl2(ji,jj)
+                  v_ice(ji,jj) = ( zr - zccb * zu_ice2 ) / ( zcca + zepsi ) * rswitch
                END DO
             END DO
@@ -567,32 +484,26 @@
             CALL lbc_lnk( v_ice(:,:), 'V', -1. )
 #if defined key_agrif && defined key_lim2
-            CALL agrif_rhg_lim2( jter, nevp, 'V' )
+            CALL agrif_rhg_lim2( jter, nn_nevp, 'V' )
 #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         
 
          ELSE 
-!CDIR NOVERRCHK
             DO jj = k_j1+1, k_jpj-1
-!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
-                  z0           = zmass2(ji,jj)/dtevp
+                  rswitch      = ( 1.0 - MAX( 0._wp, SIGN( 1._wp, -zmass2(ji,jj) ) ) ) * vmask(ji,jj,1)
+                  z0           = zmass2(ji,jj) * z1_dtevp
                   ! SB modif because ocean has no slip boundary condition
-                  zu_ice2       = 0.5*( (u_ice(ji,jj)+u_ice(ji-1,jj))*e2t(ji,jj)      &
-                     &                 +(u_ice(ji,jj+1)+u_ice(ji-1,jj+1))*e2t(ji,jj+1))   &
-                     &               /(e2t(ji,jj+1)+e2t(ji,jj)) * tmv(ji,jj)   
-
-                  za           = rhoco*SQRT((zu_ice2-u_oce2(ji,jj))**2 + &
-                     (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
-                  v_ice(ji,jj) = (zr-zccb*zu_ice2)/(zcca+epsd)*zmask
-
+                  zu_ice2      = 0.5 * ( ( u_ice(ji,jj  ) + u_ice(ji-1,jj  ) ) * e2t(ji,jj)       &
+                     &                  +( u_ice(ji,jj+1) + u_ice(ji-1,jj+1) ) * e2t(ji,jj+1) )   &
+                     &                 / ( e2t(ji,jj+1) + e2t(ji,jj) ) * vmask(ji,jj,1)   
+
+                  za           = rhoco * SQRT( ( zu_ice2 - u_oce2(ji,jj) )**2 +  &
+                     &                         ( v_ice(ji,jj) - v_oce(ji,jj) )**2 ) * ( 1.0 - zfrld2(ji,jj) )
+                  zr           = z0 * v_ice(ji,jj) + zf2(ji,jj) + za2ct(ji,jj) + za * v_oce(ji,jj)
+                  zcca         = z0 + za
+                  zccb         = zcorl2(ji,jj)
+                  v_ice(ji,jj) = ( zr - zccb * zu_ice2 ) / ( zcca + zepsi ) * rswitch
                END DO
             END DO
@@ -600,43 +511,32 @@
             CALL lbc_lnk( v_ice(:,:), 'V', -1. )
 #if defined key_agrif && defined key_lim2
-            CALL agrif_rhg_lim2( jter, nevp, 'V' )
+            CALL agrif_rhg_lim2( jter, nn_nevp, 'V' )
 #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         
 
-!CDIR NOVERRCHK
             DO jj = k_j1+1, k_jpj-1
-!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
-                  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))   &
-                     &               /(e1t(ji+1,jj)+e1t(ji,jj)) * tmu(ji,jj)
-
-                  za           = rhoco*SQRT((u_ice(ji,jj)-u_oce1(ji,jj))**2 + &
-                     (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
-                  u_ice(ji,jj) = (zr+zccb*zv_ice1)/(zcca+epsd)*zmask 
-               END DO ! ji
-            END DO ! jj
+                  rswitch      = ( 1.0 - MAX( 0._wp, SIGN( 1._wp, -zmass1(ji,jj) ) ) ) * umask(ji,jj,1)
+                  z0           = zmass1(ji,jj) * z1_dtevp
+                  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) )   &
+                     &                 / ( e1t(ji+1,jj) + e1t(ji,jj) ) * umask(ji,jj,1)
+
+                  za           = rhoco * SQRT( ( u_ice(ji,jj) - u_oce(ji,jj) )**2 +  &
+                     &                         ( 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 * u_oce(ji,jj)
+                  zcca         = z0 + za
+                  zccb         = zcorl1(ji,jj)
+                  u_ice(ji,jj) = ( zr + zccb * zv_ice1 ) / ( zcca + zepsi ) * rswitch 
+               END DO
+            END DO
 
             CALL lbc_lnk( u_ice(:,:), 'U', -1. )
 #if defined key_agrif && defined key_lim2
-            CALL agrif_rhg_lim2( jter, nevp, 'U' )
+            CALL agrif_rhg_lim2( jter, nn_nevp, 'U' )
 #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         
@@ -647,8 +547,7 @@
             !---  Convergence test.
             DO jj = k_j1+1 , k_jpj-1
-               zresr(:,jj) = MAX( ABS( u_ice(:,jj) - zu_ice(:,jj) ) ,           &
-                  ABS( v_ice(:,jj) - zv_ice(:,jj) ) )
-            END DO
-            zresm = MAXVAL( zresr( 1:jpi , k_j1+1:k_jpj-1 ) )
+               zresr(:,jj) = MAX( ABS( u_ice(:,jj) - zu_ice(:,jj) ), ABS( v_ice(:,jj) - zv_ice(:,jj) ) )
+            END DO
+            zresm = MAXVAL( zresr( 1:jpi, k_j1+1:k_jpj-1 ) )
             IF( lk_mpp )   CALL mpp_max( zresm )   ! max over the global domain
          ENDIF
@@ -661,31 +560,22 @@
       ! 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, 
-      ! This prevents high velocity when ice is thin
-!CDIR NOVERRCHK
+      ! If the ice volume is below zvmin then ice velocity should equal the
+      ! ocean velocity. This prevents high velocity when ice is thin
       DO jj = k_j1+1, k_jpj-1
-!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
+            IF ( vt_i(ji,jj) <= zvmin ) THEN
                u_ice(ji,jj) = u_oce(ji,jj)
                v_ice(ji,jj) = v_oce(ji,jj)
-            ENDIF ! zdummy
+            ENDIF
          END DO
       END DO
 
-      CALL lbc_lnk( u_ice(:,:), 'U', -1. ) 
-      CALL lbc_lnk( v_ice(:,:), 'V', -1. ) 
+      CALL lbc_lnk_multi( u_ice(:,:), 'U', -1., v_ice(:,:), 'V', -1. )
+
 #if defined key_agrif && defined key_lim2
-      CALL agrif_rhg_lim2( nevp , nevp, 'U' )
-      CALL agrif_rhg_lim2( nevp , nevp, 'V' )
+      CALL agrif_rhg_lim2( nn_nevp , nn_nevp, 'U' )
+      CALL agrif_rhg_lim2( nn_nevp , nn_nevp, 'V' )
 #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,104 +584,64 @@
       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
-               v_ice1(ji,jj)  = 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)
-
-               u_ice2(ji,jj)  = 0.5*( (u_ice(ji,jj)+u_ice(ji-1,jj))*e2t(ji,jj+1)   &
-                  &                 +(u_ice(ji,jj+1)+u_ice(ji-1,jj+1))*e2t(ji,jj)) &
-                  &               /(e2t(ji,jj+1)+e2t(ji,jj)) * tmv(ji,jj)
-            ENDIF ! zdummy
+            IF ( vt_i(ji,jj) <= zvmin ) THEN
+               v_ice1(ji,jj)  = 0.5_wp * ( ( 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) ) * umask(ji,jj,1)
+
+               u_ice2(ji,jj)  = 0.5_wp * ( ( u_ice(ji,jj  ) + u_ice(ji-1,jj  ) ) * e2t(ji,jj+1)     &
+                  &                      + ( u_ice(ji,jj+1) + u_ice(ji-1,jj+1) ) * e2t(ji,jj  ) )   &
+                  &                      / ( e2t(ji,jj+1) + e2t(ji,jj) ) * vmask(ji,jj,1)
+            ENDIF 
          END DO
       END DO
 
-      CALL lbc_lnk( u_ice2(:,:), 'V', -1. ) 
-      CALL lbc_lnk( v_ice1(:,:), 'U', -1. )
+      CALL lbc_lnk_multi( u_ice2(:,:), 'V', -1., v_ice1(:,:), 'U', -1. )
 
       ! Recompute delta, shear and div, inputs for mechanical redistribution 
-!CDIR NOVERRCHK
       DO jj = k_j1+1, k_jpj-1
-!CDIR NOVERRCHK
-         DO ji = fs_2, jpim1   !RB bug no vect opt due to tmi
-            !- zdd(:,:), zdt(:,:): divergence and tension at centre 
+         DO ji = fs_2, jpim1   !RB bug no vect opt due to zmask
+            !- 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)
-
-               zdt(ji,jj) = ( ( u_ice(ji,jj)/e2u(ji,jj)                    &
-                  &            -u_ice(ji-1,jj)/e2u(ji-1,jj)                &
-                  &           )*e2t(ji,jj)*e2t(ji,jj)                      &
-                  &          -( v_ice(ji,jj)/e1v(ji,jj)                    &
-                  &            -v_ice(ji,jj-1)/e1v(ji,jj-1)                &
-                  &           )*e1t(ji,jj)*e1t(ji,jj)                      &
-                  &         )                                              &
-                  &        / area(ji,jj)
+            IF ( vt_i(ji,jj) <= zvmin ) THEN
+
+               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)   &
+                  &            ) * r1_e12t(ji,jj)
+
+               zdt(ji,jj) = ( ( u_ice(ji,jj) * r1_e2u(ji,jj) - u_ice(ji-1,jj) * r1_e2u(ji-1,jj) ) * e2t(ji,jj) * e2t(ji,jj)  &
+                  &          -( v_ice(ji,jj) * r1_e1v(ji,jj) - v_ice(ji,jj-1) * r1_e1v(ji,jj-1) ) * e1t(ji,jj) * e1t(ji,jj)  &
+                  &         ) * r1_e12t(ji,jj)
                !
                ! SB modif because ocean has no slip boundary condition 
-               zds(ji,jj) = ( ( u_ice(ji,jj+1) / e1u(ji,jj+1)              &
-                  &           - u_ice(ji,jj)   / e1u(ji,jj) )              &
-                  &           * e1f(ji,jj) * e1f(ji,jj)                    &
-                  &          + ( v_ice(ji+1,jj) / e2v(ji+1,jj)             &
-                  &            - v_ice(ji,jj)  / e2v(ji,jj) )              &
-                  &           * e2f(ji,jj) * e2f(ji,jj) )                  &
-                  &        / ( e1f(ji,jj) * e2f(ji,jj) ) * ( 2.0 - tmf(ji,jj) ) &
-                  &        * tmi(ji,jj) * tmi(ji,jj+1)                     &
-                  &        * tmi(ji+1,jj) * tmi(ji+1,jj+1)
-
-               zdst(ji,jj) = (  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
+               zds(ji,jj) = ( ( u_ice(ji,jj+1) * r1_e1u(ji,jj+1) - u_ice(ji,jj) * r1_e1u(ji,jj) ) * e1f(ji,jj) * e1f(ji,jj)  &
+                  &          +( v_ice(ji+1,jj) * r1_e2v(ji+1,jj) - v_ice(ji,jj) * r1_e2v(ji,jj) ) * e2f(ji,jj) * e2f(ji,jj)  &
+                  &         ) * r1_e12f(ji,jj) * ( 2.0 - fmask(ji,jj,1) )                                     &
+                  &         * zmask(ji,jj) * zmask(ji,jj+1) * zmask(ji+1,jj) * zmask(ji+1,jj+1)
+
+               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) ) * r1_e12t(ji,jj)
+
+               delta = SQRT( divu_i(ji,jj)**2 + ( zdt(ji,jj)**2 + zdst**2 ) * usecc2 )  
+               delta_i(ji,jj) = delta + rn_creepl
             
-            ENDIF ! zdummy
-
-         END DO !jj
-      END DO !ji
+            ENDIF
+         END DO
+      END DO
       !
       !------------------------------------------------------------------------------!
       ! 5) Store stress tensor and its invariants
       !------------------------------------------------------------------------------!
-      !
       ! * Invariants of the stress tensor are required for limitd_me
       !   (accelerates convergence and improves stability)
       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)           &   
-               &          - 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) )
-            ! end TECLIM change
+            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) ) * r1_e12t(ji,jj) 
+            shear_i(ji,jj) = SQRT( zdt(ji,jj) * zdt(ji,jj) + zdst * zdst )
          END DO
       END DO
 
       ! Lateral boundary condition
-      CALL lbc_lnk( divu_i (:,:), 'T', 1. )
-      CALL lbc_lnk( delta_i(:,:), 'T', 1. )
-      ! CALL lbc_lnk( shear_i(:,:), 'F', 1. )
-      CALL lbc_lnk( shear_i(:,:), 'T', 1. )
+      CALL lbc_lnk_multi( divu_i (:,:), 'T', 1., delta_i(:,:), 'T', 1.,  shear_i(:,:), 'T', 1. )
 
       ! * Store the stress tensor for the next time step
@@ -824,5 +674,5 @@
             DO jj = k_j1+1, k_jpj-1
                DO ji = 2, jpim1
-                  IF (zpresh(ji,jj) .GT. 1.0) THEN
+                  IF (zpresh(ji,jj) > 1.0) THEN
                      sigma1 = ( zs1(ji,jj) + (zs2(ji,jj)**2 + 4*zs12(ji,jj)**2 )**0.5 ) / ( 2*zpresh(ji,jj) ) 
                      sigma2 = ( zs1(ji,jj) - (zs2(ji,jj)**2 + 4*zs12(ji,jj)**2 )**0.5 ) / ( 2*zpresh(ji,jj) )
@@ -838,7 +688,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_oce2, u_ice2, v_oce1 , v_ice1 , zmask               )
+      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_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limrst.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limrst.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limrst.F90	(revision 5832)
@@ -18,5 +18,4 @@
    USE ice            ! sea-ice variables
    USE oce     , ONLY :  snwice_mass, snwice_mass_b
-   USE par_ice        ! sea-ice parameters
    USE dom_oce        ! ocean domain
    USE sbc_oce        ! Surface boundary condition: ocean fields
@@ -27,4 +26,5 @@
    USE wrk_nemo       ! work arrays
    USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
+   USE limctl
 
    IMPLICIT NONE
@@ -33,5 +33,5 @@
    PUBLIC   lim_rst_opn    ! routine called by icestep.F90
    PUBLIC   lim_rst_write  ! routine called by icestep.F90
-   PUBLIC   lim_rst_read   ! routine called by iceini.F90
+   PUBLIC   lim_rst_read   ! routine called by sbc_lim_init
 
    LOGICAL, PUBLIC ::   lrst_ice         !: logical to control the ice restart write 
@@ -55,4 +55,5 @@
       CHARACTER(LEN=20)   ::   clkt     ! ocean time-step define as a character
       CHARACTER(LEN=50)   ::   clname   ! ice output restart file name
+      CHARACTER(len=256)  ::   clpath   ! full path to ice output restart file 
       !!----------------------------------------------------------------------
       !
@@ -64,26 +65,33 @@
       IF( kt == nitrst - 2*nn_fsbc + 1 .OR. nstock == nn_fsbc    &
          &                             .OR. ( kt == nitend - nn_fsbc + 1 .AND. .NOT. lrst_ice ) ) THEN
-         ! beware of the format used to write kt (default is i8.8, that should be large enough...)
-         IF( nitrst > 99999999 ) THEN   ;   WRITE(clkt, *       ) nitrst
-         ELSE                           ;   WRITE(clkt, '(i8.8)') nitrst
+         IF( nitrst <= nitend .AND. nitrst > 0 ) THEN
+            ! beware of the format used to write kt (default is i8.8, that should be large enough...)
+            IF( nitrst > 99999999 ) THEN   ;   WRITE(clkt, *       ) nitrst
+            ELSE                           ;   WRITE(clkt, '(i8.8)') nitrst
+            ENDIF
+            ! create the file
+            clname = TRIM(cexper)//"_"//TRIM(ADJUSTL(clkt))//"_"//TRIM(cn_icerst_out)
+            clpath = TRIM(cn_icerst_outdir) 
+            IF( clpath(LEN_TRIM(clpath):) /= '/' ) clpath = TRIM(clpath)//'/'
+            IF(lwp) THEN
+               WRITE(numout,*)
+               SELECT CASE ( jprstlib )
+               CASE ( jprstdimg )
+                  WRITE(numout,*) '             open ice restart binary file: ',TRIM(clpath)//clname
+               CASE DEFAULT
+                  WRITE(numout,*) '             open ice restart NetCDF file: ',TRIM(clpath)//clname
+               END SELECT
+               IF( kt == nitrst - 2*nn_fsbc + 1 ) THEN   
+                  WRITE(numout,*)         '             kt = nitrst - 2*nn_fsbc + 1 = ', kt,' date= ', ndastp
+               ELSE   ;   WRITE(numout,*) '             kt = '                         , kt,' date= ', ndastp
+               ENDIF
+            ENDIF
+            !
+            CALL iom_open( TRIM(clpath)//TRIM(clname), numriw, ldwrt = .TRUE., kiolib = jprstlib )
+            lrst_ice = .TRUE.
          ENDIF
-         ! create the file
-         clname = TRIM(cexper)//"_"//TRIM(ADJUSTL(clkt))//"_"//TRIM(cn_icerst_out)
-         IF(lwp) THEN
-            WRITE(numout,*)
-            SELECT CASE ( jprstlib )
-            CASE ( jprstdimg )   ;   WRITE(numout,*) '             open ice restart binary file: '//clname
-            CASE DEFAULT         ;   WRITE(numout,*) '             open ice restart NetCDF file: '//clname
-            END SELECT
-            IF( kt == nitrst - 2*nn_fsbc + 1 ) THEN   
-               WRITE(numout,*)         '             kt = nitrst - 2*nn_fsbc + 1 = ', kt,' date= ', ndastp
-            ELSE   ;   WRITE(numout,*) '             kt = '                         , kt,' date= ', ndastp
-            ENDIF
-         ENDIF
-         !
-         CALL iom_open( clname, numriw, ldwrt = .TRUE., kiolib = jprstlib )
-         lrst_ice = .TRUE.
       ENDIF
       !
+      IF( ln_icectl )   CALL lim_prt( kt, iiceprt, jiceprt, 1, ' - Beginning the time step - ' )   ! control print
    END SUBROUTINE lim_rst_opn
 
@@ -162,10 +170,9 @@
       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  )
       CALL iom_rstput( iter, nitrst, numriw, 'stress12_i'   , stress12_i )
-      CALL iom_rstput( iter, nitrst, numriw, 'snwice_mass'  , snwice_mass )   !clem modif
-      CALL iom_rstput( iter, nitrst, numriw, 'snwice_mass_b', snwice_mass_b ) !clem modif
+      CALL iom_rstput( iter, nitrst, numriw, 'snwice_mass'  , snwice_mass )
+      CALL iom_rstput( iter, nitrst, numriw, 'snwice_mass_b', snwice_mass_b )
 
       DO jl = 1, jpl 
@@ -307,8 +314,6 @@
       !! ** purpose  :   read of sea-ice variable restart in a netcdf file
       !!----------------------------------------------------------------------
-      INTEGER :: ji, jj, jk, jl, indx
+      INTEGER :: ji, jj, jk, jl
       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), POINTER, DIMENSION(:,:) ::   z2d
       CHARACTER(len=15) ::   znam
@@ -318,5 +323,4 @@
       !!----------------------------------------------------------------------
 
-      CALL wrk_alloc( nlay_i, zs_zero )
       CALL wrk_alloc( jpi, jpj, z2d )
 
@@ -330,9 +334,9 @@
         ! eventually read netcdf file (monobloc)  for restarting on different number of processors
         ! if {cn_icerst_in}.nc exists, then set jlibalt to jpnf90
-        INQUIRE( FILE = TRIM(cn_icerst_in)//'.nc', EXIST = llok )
+        INQUIRE( FILE = TRIM(cn_icerst_indir)//'/'//TRIM(cn_icerst_in)//'.nc', EXIST = llok )
         IF ( llok ) THEN ; jlibalt = jpnf90  ; ELSE ; jlibalt = jprstlib ; ENDIF
       ENDIF
 
-      CALL iom_open ( cn_icerst_in, numrir, kiolib = jprstlib )
+      CALL iom_open ( TRIM(cn_icerst_indir)//'/'//cn_icerst_in, numrir, kiolib = jprstlib )
 
       CALL iom_get( numrir, 'nn_fsbc', zfice )
@@ -393,10 +397,9 @@
       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  )
       CALL iom_get( numrir, jpdom_autoglo, 'stress12_i', stress12_i )
-      CALL iom_get( numrir, jpdom_autoglo, 'snwice_mass'  , snwice_mass )   !clem modif
-      CALL iom_get( numrir, jpdom_autoglo, 'snwice_mass_b', snwice_mass_b ) !clem modif
+      CALL iom_get( numrir, jpdom_autoglo, 'snwice_mass'  , snwice_mass )
+      CALL iom_get( numrir, jpdom_autoglo, 'snwice_mass_b', snwice_mass_b )
 
       DO jl = 1, jpl 
@@ -522,7 +525,17 @@
       END DO
       !
+      ! clem: I do not understand why the following IF is needed
+      !       I suspect something inconsistent in the main code with option nn_icesal=1
+      IF( nn_icesal == 1 ) THEN
+         DO jl = 1, jpl 
+            sm_i(:,:,jl) = rn_icesal
+            DO jk = 1, nlay_i 
+               s_i(:,:,jk,jl) = rn_icesal
+            END DO
+         END DO
+      ENDIF
+      !
       !CALL iom_close( numrir ) !clem: closed in sbcice_lim.F90
       !
-      CALL wrk_dealloc( nlay_i, zs_zero )
       CALL wrk_dealloc( jpi, jpj, z2d )
       !
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limsbc.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limsbc.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limsbc.F90	(revision 5832)
@@ -25,14 +25,10 @@
    USE par_oce          ! ocean parameters
    USE phycst           ! physical constants
-   USE par_ice          ! ice parameters
    USE dom_oce          ! ocean domain
-   USE domvvl           ! ocean vertical scale factors
-   USE dom_ice,    ONLY : tms
    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 : sshn, sshb, snwice_mass, snwice_mass_b, snwice_fmass
    USE albedo           ! albedo parameters
    USE lbclnk           ! ocean lateral boundary condition - MPP exchanges
@@ -42,15 +38,16 @@
    USE prtctl           ! Print control
    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 traqsr           ! add penetration of solar flux in the calculation of heat budget
+   USE iom
+   USE domvvl           ! Variable volume
+   USE limctl
+   USE limcons
 
    IMPLICIT NONE
    PRIVATE
 
-   PUBLIC   lim_sbc_init   ! called by ice_init
+   PUBLIC   lim_sbc_init   ! called by sbc_lim_init
    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,62 @@
       !!              - fr_i    : ice fraction
       !!              - tn_ice  : sea-ice surface temperature
-      !!              - alb_ice : sea-ice alberdo (lk_cpl=T)
+      !!              - alb_ice : sea-ice albedo (only useful in coupled mode)
       !!
       !! 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) ::   zqmass                                         ! Heat flux associated with mass exchange ice->ocean (W.m-2)
+      REAL(wp) ::   zqsr                                           ! 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_oce(:,:) * pfrld(:,:) )                                   !     solar flux at ocean surface
+      IF( iom_use('qns_oce') )   CALL iom_put( "qns_oce" , qns_oce(:,:) * pfrld(:,:) + qemp_oce(:,:) )                   ! 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 ) + qemp_ice(:,:) ) ! 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_oce(:,:) + qns_oce(:,:) ) * pfrld(:,:) + qemp_oce(:,:) )  
+      IF( iom_use('qt_ice' ) )   CALL iom_put( "qt_ice"  , SUM( ( qns_ice(:,:,:) + qsr_ice(:,:,:) )   &
+         &                                                      * a_i_b(:,:,:), dim=3 ) + qemp_ice(:,:) )
+      IF( iom_use('qemp_oce' ) )   CALL iom_put( "qemp_oce"  , qemp_oce(:,:) )  
+      IF( iom_use('qemp_ice' ) )   CALL iom_put( "qemp_ice"  , qemp_ice(:,:) )  
+
       ! 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) )
-               DO jl = 1, jpl
-                  zfcm1 = zfcm1 - qsr_ice(ji,jj,jl) * a_i(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) )
-            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
+
+            !------------------------------------------!
+            !      heat flux at the ocean surface      !
+            !------------------------------------------!
+            ! Solar heat flux reaching the ocean = zqsr (W.m-2) 
+            !---------------------------------------------------
+            zqsr = qsr_tot(ji,jj)
+            DO jl = 1, jpl
+               zqsr = zqsr - a_i_b(ji,jj,jl) * (  qsr_ice(ji,jj,jl) - ftr_ice(ji,jj,jl) ) 
+            END DO
+
+            ! Total heat flux reaching the ocean = hfx_out (W.m-2) 
+            !---------------------------------------------------
+            zqmass         = 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) + zqmass + zqsr
+
+            ! Add the residual from heat diffusion equation (W.m-2)
+            !-------------------------------------------------------
+            hfx_out(ji,jj) = hfx_out(ji,jj) + hfx_err_dif(ji,jj)
+
+            ! New qsr and qns used to compute the oceanic heat flux at the next time step
+            !---------------------------------------------------
+            qsr(ji,jj) = zqsr                                      
+            qns(ji,jj) = hfx_out(ji,jj) - zqsr              
+
+            !------------------------------------------!
+            !      mass flux at the ocean surface      !
+            !------------------------------------------!
             !  case of realistic freshwater flux (Tartinville et al., 2001) (presently ACTIVATED)
             !  ------------------------------------------------------------------------------------- 
@@ -208,27 +160,12 @@
             !                     Even if i see Ice melting as a FW and SALT flux
             !        
-
-            !  computing freshwater exchanges at the ice/ocean interface
-            IF (lk_cpl) THEN 
-               zemp = - emp_tot(ji,jj) + emp_ice(ji,jj) * ( 1. - pfrld(ji,jj) )    &   !
-                  &   - rdm_snw(ji,jj) / rdt_ice
-            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
-            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) + wfx_snw(ji,jj) ) * r1_rdtice  ! F/M mass flux save at least for biogeochemical model
+            emp(ji,jj)    = emp_oce(ji,jj) - 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 +174,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
-         snwice_mass  (:,:) = tms(:,:) * ( rhosn * vt_s(:,:) + rhoic * vt_i(:,:)  ) 
-         !                                                      ! time evolution of snow+ice mass
+      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  (:,:) = tmask(:,:,1) * ( rhosn * vt_s(:,:) + rhoic * vt_i(:,:)  ) 
+         ! time evolution of snow+ice mass
          snwice_fmass (:,:) = ( snwice_mass(:,:) - snwice_mass_b(:,:) ) * r1_rdtice
       ENDIF
@@ -260,12 +195,17 @@
       tn_ice(:,:,:) = t_su(:,:,:)           ! Ice surface temperature                      
 
-      !------------------------------------------------!
-      !    Computation of snow/ice and ocean albedo    !
-      !------------------------------------------------!
-      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
+      !------------------------------------------------------------------------!
+      !    Snow/ice albedo (only if sent to coupler, useless in forced mode)   !
+      !------------------------------------------------------------------------!
+      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 )
+
+      ! conservation test
+      IF( ln_limdiahsb ) CALL lim_cons_final( 'limsbc' )
+
+      ! control prints
+      IF( ln_icectl )   CALL lim_prt( kt, iiceprt, jiceprt, 3, ' - Final state lim_sbc - ' )
 
       IF(ln_ctl) THEN
@@ -275,7 +215,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
 
@@ -315,7 +253,5 @@
       !
       IF( MOD( kt-1, nn_fsbc ) == 0 ) THEN     !==  Ice time-step only  ==!   (i.e. surface module time-step)
-!CDIR NOVERRCHK
          DO jj = 2, jpjm1                             !* update the modulus of stress at ocean surface (T-point)
-!CDIR NOVERRCHK
             DO ji = fs_2, fs_jpim1
                !                                               ! 2*(U_ice-U_oce) at T-point
@@ -367,6 +303,4 @@
       !! ** input   : Namelist namicedia
       !!-------------------------------------------------------------------
-      REAL(wp) :: zsum, zarea
-      !
       INTEGER  ::   ji, jj, jk                       ! dummy loop indices
       REAL(wp) ::   zcoefu, zcoefv, zcoeff          ! local scalar
@@ -388,15 +322,9 @@
          END WHERE
       ENDIF
-      ! clem modif
-      IF( .NOT. ln_rstart ) THEN
-         iatte(:,:) = 1._wp
-         oatte(:,:) = 1._wp
-      ENDIF
-      !
-      ! clem: snwice_mass in the restart file now
+      !
       IF( .NOT. ln_rstart ) THEN
          !                                      ! embedded sea ice
          IF( nn_ice_embd /= 0 ) THEN            ! mass exchanges between ice and ocean (case 1 or 2) set the snow+ice mass
-            snwice_mass  (:,:) = tms(:,:) * ( rhosn * vt_s(:,:) + rhoic * vt_i(:,:)  )
+            snwice_mass  (:,:) = tmask(:,:,1) * ( rhosn * vt_s(:,:) + rhoic * vt_i(:,:)  )
             snwice_mass_b(:,:) = snwice_mass(:,:)
          ELSE
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd.F90	(revision 5832)
@@ -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,16 +22,15 @@
    USE phycst         ! physical constants
    USE dom_oce        ! ocean space and time domain variables
-   USE oce     , ONLY :  iatte, oatte
    USE ice            ! LIM: sea-ice variables
-   USE par_ice        ! LIM: sea-ice parameters
    USE sbc_oce        ! Surface boundary condition: ocean fields
    USE sbc_ice        ! Surface boundary condition: ice fields
    USE thd_ice        ! LIM thermodynamic sea-ice variables
    USE dom_ice        ! LIM sea-ice domain
-   USE domvvl         ! domain: variable volume level
    USE limthd_dif     ! LIM: thermodynamics, vertical diffusion
    USE limthd_dh      ! LIM: thermodynamics, ice and snow thickness variation
    USE limthd_sal     ! LIM: thermodynamics, ice salinity
    USE limthd_ent     ! LIM: thermodynamics, ice enthalpy redistribution
+   USE limthd_lac     ! LIM-3 lateral accretion
+   USE limitd_th      ! remapping thickness distribution
    USE limtab         ! LIM: 1D <==> 2D transformation
    USE limvar         ! LIM: sea-ice variables
@@ -43,14 +42,12 @@
    USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
    USE timing         ! Timing
+   USE limcons        ! conservation tests
+   USE limctl
 
    IMPLICIT NONE
    PRIVATE
 
-   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      !
+   PUBLIC   lim_thd         ! called by limstp module
+   PUBLIC   lim_thd_init    ! called by sbc_lim_init
 
    !! * 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,57 +71,48 @@
       !!               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, 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 vertical thermo calculations
+      INTEGER  :: ii, ij           ! temporary dummy loop index
+      REAL(wp) :: zfric_u, zqld, zqfr
+      REAL(wp) :: zvi_b, zsmv_b, zei_b, zfs_b, zfw_b, zft_b 
+      REAL(wp), PARAMETER :: zfric_umin = 0._wp           ! lower bound for the friction velocity (cice value=5.e-04)
+      REAL(wp), PARAMETER :: zch        = 0.0057_wp       ! heat transfer coefficient
+      !
       !!-------------------------------------------------------------------
+
       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)
+
+      CALL lim_var_glo2eqv
+      !------------------------------------------------------------------------!
+      ! 1) Initialization of some variables                                    !
+      !------------------------------------------------------------------------!
+      ftr_ice(:,:,:) = 0._wp  ! part of solar radiation transmitted through the ice
 
       !--------------------
       ! 1.2) Heat content    
       !--------------------
-      ! Change the units of heat content; from global units to J.m3
+      ! Change the units of heat content; from J/m2 to J/m3
       DO jl = 1, jpl
          DO jk = 1, nlay_i
             DO jj = 1, jpj
                DO ji = 1, jpi
+                  !0 if no ice and 1 if yes
+                  rswitch = MAX(  0._wp , SIGN( 1._wp , v_i(ji,jj,jl) - epsi20 )  )
                   !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) / MAX( v_i(ji,jj,jl) , epsi20 ) * REAL( nlay_i )
                END DO
             END DO
@@ -133,10 +121,8 @@
             DO jj = 1, jpj
                DO ji = 1, jpi
+                  !0 if no ice and 1 if yes
+                  rswitch = MAX(  0._wp , SIGN( 1._wp , v_s(ji,jj,jl) - epsi20 )  )
                   !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 )  )
-                  !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) = rswitch * e_s(ji,jj,jk,jl) / MAX( v_s(ji,jj,jl) , epsi20 ) * REAL( nlay_s )
                END DO
             END DO
@@ -144,22 +130,9 @@
       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.      !
       !-----------------------------------------------------------------------------!
-
-!CDIR NOVERRCHK
       DO jj = 1, jpj
-!CDIR NOVERRCHK
          DO ji = 1, jpi
-            zinda          = tms(ji,jj) * ( 1.0 - MAX( zzero , SIGN( zone , - at_i(ji,jj) + epsi10 ) ) )
+            rswitch  = tmask(ji,jj,1) * 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 +141,47 @@
             !           !  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) --- !
+            zqld =  tmask(ji,jj,1) * rdt_ice *  &
+               &    ( pfrld(ji,jj) * qsr_oce(ji,jj) * frq_m(ji,jj) + pfrld(ji,jj) * qns_oce(ji,jj) + qemp_oce(ji,jj) )
+
+            ! --- Energy needed to bring ocean surface layer until its freezing (<0, J.m-2) --- !
+            zqfr = tmask(ji,jj,1) * rau0 * rcp * fse3t_m(ji,jj) * ( t_bo(ji,jj) - ( sst_m(ji,jj) + rt0 ) )
+
+            ! --- Energy from the turbulent oceanic heat flux (W/m2) --- !
+            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
+            fhtur(ji,jj) = rswitch * MIN( fhtur(ji,jj), - zqfr * r1_rdtice / MAX( at_i(ji,jj), epsi10 ) )
+            ! upper bound for fhtur: the heat retrieved from the ocean must be smaller than the heat necessary to reach 
+            !                        the freezing point, so that we do not have SST < T_freeze
+            !                        This implies: - ( fhtur(ji,jj) * at_i(ji,jj) * rtdice ) - zqfr >= 0
+
+            !-- Energy Budget of the leads (J.m-2). Must be < 0 to form ice
+            qlead(ji,jj) = MIN( 0._wp , zqld - ( fhtur(ji,jj) * at_i(ji,jj) * rdt_ice ) - zqfr )
+
+            ! If there is ice and leads are warming, then transfer energy from the lead budget and use it for bottom melting 
+            IF( zqld > 0._wp ) THEN
+               fhld (ji,jj) = rswitch * zqld * r1_rdtice / MAX( at_i(ji,jj), epsi10 ) ! 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) )
-            !
+            ! -----------------------------------------
+            ! Net heat flux on top of ice-ocean [W.m-2]
+            ! -----------------------------------------
+            hfx_in(ji,jj) = qns_tot(ji,jj) + qsr_tot(ji,jj) 
+
+            ! -----------------------------------------------------------------------------
+            ! Net heat flux on top of the ocean after ice thermo (1st step) [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) =   pfrld(ji,jj) * qns_oce(ji,jj) + qemp_oce(ji,jj)  &  ! Non solar heat flux received by the ocean               
+               &             - qlead(ji,jj) * r1_rdtice                         &  ! heat flux taken from the ocean where there is open water ice formation
+               &             - at_i(ji,jj) * fhtur(ji,jj)                       &  ! heat flux taken by turbulence
+               &             - at_i(ji,jj) *  fhld(ji,jj)                          ! heat flux taken during bottom growth/melt 
+                                                                                   !    (fhld should be 0 while bott growth)
          END DO
       END DO
@@ -217,9 +198,8 @@
          ENDIF
 
-         zareamin = epsi10
          nbpb = 0
          DO jj = 1, jpj
             DO ji = 1, jpi
-               IF ( a_i(ji,jj,jl) .gt. zareamin ) THEN     
+               IF ( a_i(ji,jj,jl) > epsi10 ) THEN     
                   nbpb      = nbpb  + 1
                   npb(nbpb) = (jj - 1) * jpi + ji
@@ -230,8 +210,9 @@
          ! debug point to follow
          jiindex_1d = 0
-         IF( ln_nicep ) THEN
-            DO ji = mi0(jiindx), mi1(jiindx)
-               DO jj = mj0(jjindx), mj1(jjindx)
+         IF( ln_icectl ) THEN
+            DO ji = mi0(iiceprt), mi1(iiceprt)
+               DO jj = mj0(jiceprt), mj1(jiceprt)
                   jiindex_1d = (jj - 1) * jpi + ji
+                  WRITE(numout,*) ' lim_thd : Category no : ', jl 
                END DO
             END DO
@@ -246,134 +227,49 @@
          IF( nbpb > 0 ) THEN  ! If there is no ice, do nothing.
 
-            !-------------------------
-            ! 4.1 Move to 1D arrays
-            !-------------------------
-
-            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) )
-            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) )
-            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) )
-            END DO
-
-            CALL tab_2d_1d( nbpb, tatm_ice_1d(1:nbpb), tatm_ice(:,:)   , jpi, jpj, npb(1:nbpb) )
-            CALL tab_2d_1d( nbpb, qsr_ice_1d (1:nbpb), qsr_ice(:,:,jl) , jpi, jpj, npb(1:nbpb) )
-            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, 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, 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, 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
-            !--------------------------------
-            ! 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 )
-
-            !--------------------------------
-            ! 4.4) Move 1D to 2D vectors
-            !--------------------------------
-
-               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 )
-            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)
-            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 )
-            !
-            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)
-            !+++++
+            !-------------------------!
+            ! --- Move to 1D arrays ---
+            !-------------------------!
+            CALL lim_thd_1d2d( nbpb, jl, 1 )
+
+            !--------------------------------------!
+            ! --- Ice/Snow Temperature profile --- !
+            !--------------------------------------!
+            CALL lim_thd_dif( 1, nbpb )
+
+            !---------------------------------!
+            ! --- Ice/Snow thickness ---      !
+            !---------------------------------!
+            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 )
+
+            !------------------------------------!
+            ! --- lateral melting if monocat --- !
+            !------------------------------------!
+            IF ( ( nn_monocat == 1 .OR. nn_monocat == 4 ) .AND. jpl == 1 ) THEN
+               CALL lim_thd_lam( 1, nbpb )
+            END IF
+
+            !-------------------------!
+            ! --- Move to 2D arrays ---
+            !-------------------------!
+            CALL lim_thd_1d2d( nbpb, jl, 2 )
+
             !
             IF( lk_mpp )   CALL mpp_comm_free( ncomm_ice ) !RB necessary ??
          ENDIF
          !
-      END DO
+      END DO !jl
 
       !------------------------------------------------------------------------------!
@@ -382,37 +278,46 @@
 
       !------------------------
-      ! 5.1) Ice heat content              
+      ! 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 J/m2)
       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) * a_i(:,:,jl) * ht_i(:,:,jl) * r1_nlay_i
          END DO
       END DO
 
       !------------------------
-      ! 5.2) Snow heat content              
+      ! 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 J/m2)
       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) * a_i(:,:,jl) * ht_s(:,:,jl) * r1_nlay_s
          END DO
       END DO
-
+ 
       !----------------------------------
-      ! 5.3) Change thickness to volume
+      ! Change thickness to volume
       !----------------------------------
-      CALL lim_var_eqv2glo
+      v_i(:,:,:)   = ht_i(:,:,:) * a_i(:,:,:)
+      v_s(:,:,:)   = ht_s(:,:,:) * a_i(:,:,:)
+      smv_i(:,:,:) = sm_i(:,:,:) * v_i(:,:,:)
+
+      ! update ice age (in case a_i changed, i.e. becomes 0 or lateral melting in monocat)
+      DO jl  = 1, jpl
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               rswitch = MAX( 0._wp , SIGN( 1._wp, a_i_b(ji,jj,jl) - epsi10 ) )
+               oa_i(ji,jj,jl) = rswitch * oa_i(ji,jj,jl) * a_i(ji,jj,jl) / MAX( a_i_b(ji,jj,jl), epsi10 )
+            END DO
+         END DO
+      END DO
+
+      CALL lim_var_zapsmall
 
       !--------------------------------------------
-      ! 5.4) Diagnostic thermodynamic growth rates
+      ! 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_icectl )   CALL lim_prt( kt, iiceprt, jiceprt, 1, ' - ice thermodyn. - ' )   ! control print
 
       IF(ln_ctl) THEN            ! Control print
@@ -420,5 +325,5 @@
          CALL prt_ctl_info(' - Cell values : ')
          CALL prt_ctl_info('   ~~~~~~~~~~~~~ ')
-         CALL prt_ctl(tab2d_1=area , clinfo1=' lim_thd  : cell area :')
+         CALL prt_ctl(tab2d_1=e12t , clinfo1=' lim_thd  : cell area :')
          CALL prt_ctl(tab2d_1=at_i , clinfo1=' lim_thd  : at_i      :')
          CALL prt_ctl(tab2d_1=vt_i , clinfo1=' lim_thd  : vt_i      :')
@@ -448,334 +353,71 @@
       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
+      !
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(1, 'limthd', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
+
+      !------------------------------------------------------------------------------|
+      !  6) Transport of ice between thickness categories.                           |
+      !------------------------------------------------------------------------------|
+      ! Given thermodynamic growth rates, transport ice between thickness categories.
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(0, 'limitd_th_rem', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
+
+      IF( jpl > 1 )      CALL lim_itd_th_rem( 1, jpl, kt )
+
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(1, 'limitd_th_rem', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
+
+      !------------------------------------------------------------------------------|
+      !  7) Add frazil ice growing in leads.
+      !------------------------------------------------------------------------------|
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(0, 'limthd_lac', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
+
+      CALL lim_thd_lac
+      
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(1, 'limthd_lac', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
+
+      ! Control print
+      IF(ln_ctl) THEN
+         CALL lim_var_glo2eqv
+
+         CALL prt_ctl_info(' ')
+         CALL prt_ctl_info(' - Cell values : ')
+         CALL prt_ctl_info('   ~~~~~~~~~~~~~ ')
+         CALL prt_ctl(tab2d_1=e12t , clinfo1=' lim_itd_th  : cell area :')
+         CALL prt_ctl(tab2d_1=at_i , clinfo1=' lim_itd_th  : at_i      :')
+         CALL prt_ctl(tab2d_1=vt_i , clinfo1=' lim_itd_th  : vt_i      :')
+         CALL prt_ctl(tab2d_1=vt_s , clinfo1=' lim_itd_th  : vt_s      :')
+         DO jl = 1, jpl
+            CALL prt_ctl_info(' ')
+            CALL prt_ctl_info(' - Category : ', ivar1=jl)
+            CALL prt_ctl_info('   ~~~~~~~~~~')
+            CALL prt_ctl(tab2d_1=a_i   (:,:,jl)   , clinfo1= ' lim_itd_th  : a_i      : ')
+            CALL prt_ctl(tab2d_1=ht_i  (:,:,jl)   , clinfo1= ' lim_itd_th  : ht_i     : ')
+            CALL prt_ctl(tab2d_1=ht_s  (:,:,jl)   , clinfo1= ' lim_itd_th  : ht_s     : ')
+            CALL prt_ctl(tab2d_1=v_i   (:,:,jl)   , clinfo1= ' lim_itd_th  : v_i      : ')
+            CALL prt_ctl(tab2d_1=v_s   (:,:,jl)   , clinfo1= ' lim_itd_th  : v_s      : ')
+            CALL prt_ctl(tab2d_1=e_s   (:,:,1,jl) , clinfo1= ' lim_itd_th  : e_s      : ')
+            CALL prt_ctl(tab2d_1=t_su  (:,:,jl)   , clinfo1= ' lim_itd_th  : t_su     : ')
+            CALL prt_ctl(tab2d_1=t_s   (:,:,1,jl) , clinfo1= ' lim_itd_th  : t_snow   : ')
+            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 jk = 1, nlay_i
+               CALL prt_ctl_info(' ')
+               CALL prt_ctl_info(' - Layer : ', ivar1=jk)
+               CALL prt_ctl_info('   ~~~~~~~')
+               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
+      !
+      IF( nn_timing == 1 )  CALL timing_stop('limthd')
+
+   END SUBROUTINE lim_thd 
+
  
-         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 )
-      !
-      IF( nn_timing == 1 )  CALL timing_stop('limthd')
-   END SUBROUTINE lim_thd
-
-
-   SUBROUTINE lim_thd_glohec( eti, ets, etilayer, kideb, kiut, jl )
+   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,22 +426,192 @@
       !!
       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
+            ztmelts       =  -tmut * s_i_1d(ji,jk) + rt0
+            ! Conversion q(S,T) -> T (second order equation)
+            zaaa          =  cpic
+            zbbb          =  ( rcp - cpic ) * ( ztmelts - rt0 ) + q_i_1d(ji,jk) * r1_rhoic - lfus
+            zccc          =  lfus * ( ztmelts - rt0 )
+            zdiscrim      =  SQRT( MAX( zbbb * zbbb - 4._wp * zaaa * zccc, 0._wp ) )
+            t_i_1d(ji,jk) =  rt0 - ( 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 ) * rt0
+         END DO 
+      END DO 
+
+   END SUBROUTINE lim_thd_temp
+
+   SUBROUTINE lim_thd_lam( kideb, kiut )
+      !!-----------------------------------------------------------------------
+      !!                   ***  ROUTINE lim_thd_lam *** 
+      !!                 
+      !! ** Purpose :   Lateral melting in case monocategory
+      !!                          ( dA = A/2h dh )
+      !!-----------------------------------------------------------------------
+      INTEGER, INTENT(in) ::   kideb, kiut        ! bounds for the spatial loop
+      INTEGER             ::   ji                 ! dummy loop indices
+      REAL(wp)            ::   zhi_bef            ! ice thickness before thermo
+      REAL(wp)            ::   zdh_mel, zda_mel   ! net melting
+      REAL(wp)            ::   zvi, zvs           ! ice/snow volumes 
+
+      DO ji = kideb, kiut
+         zdh_mel = MIN( 0._wp, dh_i_surf(ji) + dh_i_bott(ji) + dh_snowice(ji) )
+         IF( zdh_mel < 0._wp .AND. a_i_1d(ji) > 0._wp )  THEN
+            zvi          = a_i_1d(ji) * ht_i_1d(ji)
+            zvs          = a_i_1d(ji) * ht_s_1d(ji)
+            ! lateral melting = concentration change
+            zhi_bef     = ht_i_1d(ji) - zdh_mel
+            rswitch     = MAX( 0._wp , SIGN( 1._wp , zhi_bef - epsi20 ) )
+            zda_mel     = rswitch * a_i_1d(ji) * zdh_mel / ( 2._wp * MAX( zhi_bef, epsi20 ) )
+            a_i_1d(ji)  = MAX( epsi20, a_i_1d(ji) + zda_mel ) 
+             ! adjust thickness
+            ht_i_1d(ji) = zvi / a_i_1d(ji)            
+            ht_s_1d(ji) = zvs / a_i_1d(ji)            
+            ! retrieve total concentration
+            at_i_1d(ji) = a_i_1d(ji)
+         END IF
       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
+      
+   END SUBROUTINE lim_thd_lam
+
+   SUBROUTINE lim_thd_1d2d( nbpb, jl, kn )
+      !!-----------------------------------------------------------------------
+      !!                   ***  ROUTINE lim_thd_1d2d *** 
+      !!                 
+      !! ** Purpose :   move arrays from 1d to 2d and the reverse
+      !!-----------------------------------------------------------------------
+      INTEGER, INTENT(in) ::   kn       ! 1= from 2D to 1D
+                                        ! 2= from 1D to 2D
+      INTEGER, INTENT(in) ::   nbpb     ! size of 1D arrays
+      INTEGER, INTENT(in) ::   jl       ! ice cat
+      INTEGER             ::   jk       ! dummy loop indices
+
+      SELECT CASE( kn )
+
+      CASE( 1 )
+
+         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_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_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
+         
+         CALL tab_2d_1d( nbpb, qprec_ice_1d(1:nbpb), qprec_ice(:,:) , jpi, jpj, npb(1:nbpb) )
+         CALL tab_2d_1d( nbpb, qsr_ice_1d (1:nbpb), qsr_ice(:,:,jl) , jpi, jpj, npb(1:nbpb) )
+         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, 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) )
+         CALL tab_2d_1d( nbpb, evap_ice_1d (1:nbpb), evap_ice(:,:,jl), jpi, jpj, npb(1:nbpb) )
+         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_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, 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, 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_dif_1d (1:nbpb), hfx_err_dif , jpi, jpj, npb(1:nbpb) )
+         CALL tab_2d_1d( nbpb, hfx_err_rem_1d (1:nbpb), hfx_err_rem , jpi, jpj, npb(1:nbpb) )
+
+      CASE( 2 )
+
+         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_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_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 )
+         CALL tab_1d_2d( nbpb, hfx_err_dif   , npb, hfx_err_dif_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 )
+         !         
+      END SELECT
+
+   END SUBROUTINE lim_thd_1d2d
 
 
@@ -817,9 +629,7 @@
       !!-------------------------------------------------------------------
       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,         & 
-         &                kappa_i, nconv_i_thd, maxer_i_thd, thcon_i_swi
+      NAMELIST/namicethd/ rn_hnewice, ln_frazil, rn_maxfrazb, rn_vfrazb, rn_Cfrazb,                       &
+         &                rn_himin, rn_betas, rn_kappa_i, nn_conv_dif, rn_terr_dif, nn_ice_thcon, &
+         &                nn_monocat, ln_it_qnsice
       !!-------------------------------------------------------------------
       !
@@ -839,35 +649,29 @@
       IF(lwm) WRITE ( numoni, namicethd )
       !
+      IF ( ( jpl > 1 ) .AND. ( nn_monocat == 1 ) ) THEN 
+         nn_monocat = 0
+         IF(lwp) WRITE(numout, *) '   nn_monocat must be 0 in multi-category case '
+      ENDIF
+
+      !
       IF(lwp) THEN                          ! control print
          WRITE(numout,*)
          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,*)'      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,*)'      ice thick. for lateral accretion                        rn_hnewice   = ', rn_hnewice
+         WRITE(numout,*)'      Frazil ice thickness as a function of wind or not       ln_frazil    = ', ln_frazil
+         WRITE(numout,*)'      Maximum proportion of frazil ice collecting at bottom   rn_maxfrazb  = ', rn_maxfrazb
+         WRITE(numout,*)'      Thresold relative drift speed for collection of frazil  rn_vfrazb    = ', rn_vfrazb
+         WRITE(numout,*)'      Squeezing coefficient for collection of frazil          rn_Cfrazb    = ', rn_Cfrazb
+         WRITE(numout,*)'      minimum ice thickness                                   rn_himin     = ', rn_himin 
          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
-         WRITE(numout,*)'      maximal n. of iter. for heat diffusion computation      nconv_i_thd  = ', nconv_i_thd
-         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,*)'      coefficient for ice-lead partition of snowfall          rn_betas     = ', rn_betas
+         WRITE(numout,*)'      extinction radiation parameter in sea ice               rn_kappa_i   = ', rn_kappa_i
+         WRITE(numout,*)'      maximal n. of iter. for heat diffusion computation      nn_conv_dif  = ', nn_conv_dif
+         WRITE(numout,*)'      maximal err. on T for heat diffusion computation        rn_terr_dif  = ', rn_terr_dif
+         WRITE(numout,*)'      switch for comp. of thermal conductivity in the ice     nn_ice_thcon = ', nn_ice_thcon
+         WRITE(numout,*)'      check heat conservation in the ice/snow                 con_i        = ', con_i
+         WRITE(numout,*)'      virtual ITD mono-category parameterizations (1) or not  nn_monocat   = ', nn_monocat
+         WRITE(numout,*)'      iterate the surface non-solar flux (T) or not (F)       ln_it_qnsice = ', ln_it_qnsice
       ENDIF
-      !
-      rcdsn = hakdif * rcdsn 
-      rcdic = hakdif * rcdic
       !
    END SUBROUTINE lim_thd_init
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd_dh.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd_dh.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd_dh.F90	(revision 5832)
@@ -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 
@@ -20,5 +20,4 @@
    USE sbc_oce        ! Surface boundary condition: ocean fields
    USE ice            ! LIM variables
-   USE par_ice        ! LIM parameters
    USE thd_ice        ! LIM thermodynamics
    USE in_out_manager ! I/O manager
@@ -26,15 +25,14 @@
    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    !
+   PUBLIC   lim_thd_dh      ! called by lim_thd
+   PUBLIC   lim_thd_snwblow ! called in sbcblk/sbcclio/sbccpl and here
+
+   INTERFACE lim_thd_snwblow
+      MODULE PROCEDURE lim_thd_snwblow_1d, lim_thd_snwblow_2d
+   END INTERFACE
 
    !!----------------------------------------------------------------------
@@ -45,5 +43,5 @@
 CONTAINS
 
-   SUBROUTINE lim_thd_dh( kideb, kiut, jl )
+   SUBROUTINE lim_thd_dh( kideb, kiut )
       !!------------------------------------------------------------------
       !!                ***  ROUTINE lim_thd_dh  ***
@@ -70,36 +68,31 @@
       !!------------------------------------------------------------------
       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) ::   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), 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) ::   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(:) ::   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_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)
 
       REAL(wp), POINTER, DIMENSION(:) ::   zdh_s_mel   ! snow melt 
@@ -108,87 +101,101 @@
 
       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
-
-      ! mass and salt flux (clem)
-      REAL(wp) :: zdvres, zdvsur, zdvbot
-      REAL(wp), POINTER, DIMENSION(:) ::   zviold, zvsold   ! old ice volume...
+      REAL(wp), POINTER, DIMENSION(:,:) ::   zh_i      ! ice layer thickness
+      INTEGER , POINTER, DIMENSION(:,:) ::   icount    ! number of layers vanished by melting 
+
+      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)
+      REAL(wp), POINTER, DIMENSION(:) ::   zsnw        ! distribution of snow after wind blowing
+
+      REAL(wp) :: 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
-      
-      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
+      ! Discriminate between varying salinity (nn_icesal=2) and prescribed cases (other values)
+      SELECT CASE( nn_icesal )                       ! 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, zqprec, zq_su, zq_bo, zf_tt, zq_rema, zsnw )
+      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, zdeltah, zh_i )
+      CALL wrk_alloc( jpij, nlay_i, icount )
+       
+      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_rema  (:) = 0._wp ; zsnw     (:) = 0._wp
+      zdh_s_mel(:) = 0._wp ; zdh_s_pre(:) = 0._wp ; zdh_s_sub(:) = 0._wp ; zqh_i(:) = 0._wp
+      zqh_s    (:) = 0._wp ; zq_s     (:) = 0._wp     
+
+      zdeltah(:,:) = 0._wp ; zh_i(:,:) = 0._wp       
+      icount (:,:) = 0
+
+
+      ! Initialize enthalpy at nlay_i+1
+      DO ji = kideb, kiut
+         q_i_1d(ji,nlay_i+1) = 0._wp
+      END DO
+
+      ! 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) * r1_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   
+         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) - rt0 ) )
+         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) > rt0 ) 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) = rt0
+         END IF
+      END DO
+
+      !------------------------------------------------------------!
+      !  2) Computing layer thicknesses and enthalpies.            !
+      !------------------------------------------------------------!
+      !
       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) * ht_s_1d(ji) * r1_nlay_s
          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) * r1_nlay_i
+            zqh_i(ji)   = zqh_i(ji) + q_i_1d(ji,jk) * zh_i(ji,jk)
          END DO
       END DO
@@ -212,149 +219,183 @@
       ! Martin Vancoppenolle, December 2006
 
-      ! Snow fall
-      DO ji = kideb, kiut
-         zcoeff = ( 1.0 - ( 1.0 - at_i_b(ji) )**betas ) / at_i_b(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
+      CALL lim_thd_snwblow( 1. - at_i_1d(kideb:kiut), zsnw(kideb:kiut) ) ! snow distribution over ice after wind blowing
+
+      zdeltah(:,:) = 0._wp
+      DO ji = kideb, kiut
+         !-----------
+         ! Snow fall
+         !-----------
+         ! thickness change
+         zdh_s_pre(ji) = zsnw(ji) * sprecip_1d(ji) * rdt_ice * r1_rhosn / at_i_1d(ji)
+         ! enthalpy of the precip (>0, J.m-3)
+         zqprec   (ji) = - qprec_ice_1d(ji)   
+         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
+
+         !---------------------
+         ! Melt of falling snow
+         !---------------------
+         ! thickness change
+         rswitch        = MAX( 0._wp , SIGN( 1._wp , zqprec(ji) - epsi20 ) )
+         zdeltah (ji,1) = - rswitch * zq_su(ji) / MAX( zqprec(ji) , epsi20 )
+         zdeltah (ji,1) = MAX( - zdh_s_pre(ji), zdeltah(ji,1) ) ! bound melting 
+         ! heat used to melt snow (W.m-2, >0)
+         hfx_snw_1d(ji) = hfx_snw_1d(ji) - zdeltah(ji,1) * 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) * zdeltah(ji,1) * r1_rdtice    
+         ! updates available heat + precipitations after melting
+         zq_su     (ji) = MAX( 0._wp , zq_su (ji) + zdeltah(ji,1) * zqprec(ji) )      
+         zdh_s_pre (ji) = zdh_s_pre(ji) + zdeltah(ji,1)
+
+         ! update thickness
+         ht_s_1d(ji) = MAX( 0._wp , ht_s_1d(ji) + zdh_s_pre(ji) )
+      END DO
+
+      ! If heat still available (zq_su > 0), then melt more snow
+      zdeltah(:,:) = 0._wp
       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 * ( 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) , - ht_s_1d(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)
-      END DO ! ji
-
-      !--------------------------
-      ! 3.2 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
-
-      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
-         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
+      ! 3.2 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
+      ! qla_ice is always >=0 (upwards), heat goes to the atmosphere, therefore snow sublimates
+      ! clem comment: not counted in mass/heat exchange in limsbc since this is an exchange with atm. (not ocean)
+      ! clem comment: ice should also sublimate
+      zdeltah(:,:) = 0._wp
+      ! coupled mode: sublimation is set to 0 (evap_ice = 0) until further notice
+      ! forced  mode: snow thickness change due to sublimation
+      DO ji = kideb, kiut
+         zdh_s_sub(ji)  =  MAX( - ht_s_1d(ji) , - evap_ice_1d(ji) * r1_rhosn * rdt_ice )
+         ! Heat flux by sublimation [W.m-2], < 0
+         !      sublimate first snow that had fallen, then pre-existing snow
+         zdeltah(ji,1)  = MAX( zdh_s_sub(ji), - zdh_s_pre(ji) )
+         hfx_sub_1d(ji) = hfx_sub_1d(ji) + ( zdeltah(ji,1) * zqprec(ji) + ( zdh_s_sub(ji) - zdeltah(ji,1) ) * q_s_1d(ji,1)  &
+            &                              ) * a_i_1d(ji) * r1_rdtice
+         ! 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) )
+         ! update precipitations after sublimation and correct sublimation
+         zdh_s_pre(ji) = zdh_s_pre(ji) + zdeltah(ji,1)
+         zdh_s_sub(ji) = zdh_s_sub(ji) - zdeltah(ji,1)
+      END DO
+      
+      ! --- Update snow diags --- !
+      DO ji = kideb, kiut
+         dh_s_tot(ji) = zdh_s_mel(ji) + zdh_s_pre(ji) + zdh_s_sub(ji)
+      END DO
+
+      !-------------------------------------------
+      ! 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
-            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
+            rswitch       = MAX(  0._wp , SIGN( 1._wp, ht_s_1d(ji) - epsi20 )  )
+            q_s_1d(ji,jk) = rswitch / MAX( ht_s_1d(ji), epsi20 ) *                          &
+              &            ( (   zdh_s_pre(ji)             ) * zqprec(ji) +  &
+              &              (   ht_s_1d(ji) - zdh_s_pre(ji) ) * rhosn * ( cpic * ( rt0 - t_s_1d(ji,jk) ) + lfus ) )
+            zq_s(ji)     =  zq_s(ji) + q_s_1d(ji,jk)
          END DO
       END DO
 
-      DO jk = 1, nlay_s
+      !--------------------------
+      ! 3.4 Surface ice ablation 
+      !--------------------------
+      zdeltah(:,:) = 0._wp ! important
+      DO jk = 1, nlay_i
          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)
+            ztmelts           = - tmut * s_i_1d(ji,jk) + rt0          ! Melting point of layer k [K]
+            
+            IF( t_i_1d(ji,jk) >= ztmelts ) THEN !!! Internal melting
+
+               zEi            = - q_i_1d(ji,jk) * r1_rhoic            ! Specific enthalpy of layer k [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
+               zfmdt          = - zdeltah(ji,jk) * rhoic              ! Mass flux x time step > 0
+                         
+               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]
+
+               ! 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) - rhoic * a_i_1d(ji) * zdeltah(ji,jk) * sm_i_1d(ji) * r1_rdtice
+               
+               ! Contribution to mass flux
+               wfx_res_1d(ji) =  wfx_res_1d(ji) - rhoic * a_i_1d(ji) * zdeltah(ji,jk) * r1_rdtice
+
+            ELSE                                !!! Surface melting
+               
+               zEi            = - q_i_1d(ji,jk) * r1_rhoic            ! Specific enthalpy of layer k [J/kg, <0]
+               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 * r1_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) - rhoic * a_i_1d(ji) * zdeltah(ji,jk) * sm_i_1d(ji) * 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
+               
+            END IF
+            ! 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,jk) = 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
+      ! 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 +405,210 @@
       !------------------------------------------------------------------------------!
       !
-      ! 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
+
+      num_iter_max = 1
+      IF( nn_icesal == 2 ) num_iter_max = 5
+
+      ! 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
+            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) + rt0          ! 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 - rt0 ) / ( zt_i_new - rt0 ) )   &
+                  &               + rcp  * ( ztmelts-rt0 )          
+
+               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
          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
+      END DO
+
+      ! 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) + rt0          ! 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 - rt0 ) / ( zt_i_new - rt0 ) )   &
+               &               + rcp  * ( ztmelts-rt0 )          
+            
+            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) - rhoic * a_i_1d(ji) * dh_i_bott(ji) * s_i_new(ji) * 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,jk) ) THEN   ! do not calculate where layer has already disappeared by surface melting 
+
+               ztmelts = - tmut * s_i_1d(ji,jk) + rt0  ! Melting point of layer jk (K)
+
+               IF( t_i_1d(ji,jk) >= ztmelts ) THEN !!! Internal melting
+
+                  zEi               = - q_i_1d(ji,jk) * r1_rhoic    ! Specific enthalpy of melting ice (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) - rhoic * a_i_1d(ji) * zdeltah(ji,jk) * sm_i_1d(ji) * 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) * r1_rhoic ! Specific enthalpy of melting ice (J/kg, <0)
+                  zEw             = rcp * ( ztmelts - rt0 )    ! 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 * r1_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) - rhoic *  a_i_1d(ji) * zdeltah(ji,jk) * sm_i_1d(ji) * 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                                                       !
-      !------------------------------------------------------------------------------!
-      !
-      !----------------------------------------------
-      ! 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
+      END DO
+
+      !-------------------------------------------
+      ! 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) 
+         rswitch         = 1._wp - MAX( 0._wp, SIGN( 1._wp, - ht_s_1d(ji) ) )   ! =1 if snow
+         rswitch         = rswitch * MAX( 0._wp, SIGN( 1._wp, q_s_1d(ji,1) - epsi20 ) )
+         zdeltah  (ji,1) = - rswitch * zq_rema(ji) / MAX( q_s_1d(ji,1), epsi20 )
+         zdeltah  (ji,1) = MIN( 0._wp , MAX( zdeltah(ji,1) , - ht_s_1d(ji) ) ) ! bound melting
+         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) * q_s_1d(ji,1)                ! 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) * q_s_1d(ji,1) * 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_rema(ji) * a_i_1d(ji) ) * r1_rdtice
+
+         IF( ln_icectl .AND. zq_rema(ji) < 0. .AND. lwp ) WRITE(numout,*) 'ALERTE zq_rema <0 = ', zq_rema(ji)
+      END DO
+      
       !
       !------------------------------------------------------------------------------|
@@ -670,65 +619,96 @@
       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 ) * r1_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)
-         IF (  num_sal == 2  ) THEN
-            i_ice_switch = MAX( 0._wp , SIGN( 1._wp , zhgnew(ji) - epsi10 ) )
+         ! new salinity difference stored (to be used in limthd_sal.F90)
+         IF (  nn_icesal == 2  ) THEN
+            rswitch = MAX( 0._wp , SIGN( 1._wp , ht_i_1d(ji) - epsi20 ) )
             ! 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), epsi20 ) * 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), epsi20 ) * 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 
-
-      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
+         ! 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)
+         
+      END DO
+
+      !
+      !-------------------------------------------
+      ! Update temperature, energy
+      !-------------------------------------------
+      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 ) * rt0
+      END DO
+
+      DO jk = 1, nlay_s
+         DO ji = kideb,kiut
+            ! mask enthalpy
+            rswitch       = 1._wp - MAX(  0._wp , SIGN( 1._wp, - ht_s_1d(ji) )  )
+            q_s_1d(ji,jk) = rswitch * q_s_1d(ji,jk)
+            ! recalculate t_s_1d from q_s_1d
+            t_s_1d(ji,jk) = rt0 + rswitch * ( - q_s_1d(ji,jk) / ( rhosn * cpic ) + lfus / cpic )
+         END DO
+      END DO
+
+      ! --- ensure that a_i = 0 where ht_i = 0 ---
+      WHERE( ht_i_1d == 0._wp ) a_i_1d = 0._wp
+      
+      CALL wrk_dealloc( jpij, zqprec, zq_su, zq_bo, zf_tt, zq_rema, zsnw )
+      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, zdeltah, zh_i )
+      CALL wrk_dealloc( jpij, nlay_i, icount )
+      !
       !
    END SUBROUTINE lim_thd_dh
+
+
+   !!--------------------------------------------------------------------------
+   !! INTERFACE lim_thd_snwblow
+   !! ** Purpose :   Compute distribution of precip over the ice
+   !!--------------------------------------------------------------------------
+   SUBROUTINE lim_thd_snwblow_2d( pin, pout )
+      REAL(wp), DIMENSION(:,:), INTENT(in   ) :: pin   ! previous fraction lead ( pfrld or (1. - a_i_b) )
+      REAL(wp), DIMENSION(:,:), INTENT(inout) :: pout
+      pout = ( 1._wp - ( pin )**rn_betas )
+   END SUBROUTINE lim_thd_snwblow_2d
+
+   SUBROUTINE lim_thd_snwblow_1d( pin, pout )
+      REAL(wp), DIMENSION(:), INTENT(in   ) :: pin
+      REAL(wp), DIMENSION(:), INTENT(inout) :: pout
+      pout = ( 1._wp - ( pin )**rn_betas )
+   END SUBROUTINE lim_thd_snwblow_1d
+
    
 #else
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd_dif.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd_dif.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd_dif.F90	(revision 5832)
@@ -19,5 +19,4 @@
    USE phycst         ! physical constants (ocean directory) 
    USE ice            ! LIM-3 variables
-   USE par_ice        ! LIM-3 parameters
    USE thd_ice        ! LIM-3: thermodynamics
    USE in_out_manager ! I/O manager
@@ -31,5 +30,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 +37,5 @@
 CONTAINS
 
-   SUBROUTINE lim_thd_dif( kideb , kiut , jl )
+   SUBROUTINE lim_thd_dif( kideb , kiut )
       !!------------------------------------------------------------------
       !!                ***  ROUTINE lim_thd_dif  ***
@@ -74,10 +72,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 +89,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 +95,100 @@
       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
+      
       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) ::   zhsu
+      
+      REAL(wp), POINTER, DIMENSION(:)     ::   isnow       ! switch for presence (1) or absence (0) of snow
+      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(:)     ::   zqns_ice_b  ! 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
+      
+      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(:,:)   ::   zindterm    ! 'Ind'ependent term
+      REAL(wp), POINTER, DIMENSION(:,:)   ::   zindtbis    ! Temporary 'ind'ependent term
+      REAL(wp), POINTER, DIMENSION(:,:)   ::   zdiagbis    ! Temporary 'dia'gonal term
+      REAL(wp), POINTER, DIMENSION(:,:,:) ::   ztrid       ! Tridiagonal system terms
+      
+      ! diag errors on heat
+      REAL(wp), POINTER, DIMENSION(:)     :: zdq, zq_ini, zhfx_err
+      
+      ! Mono-category
+      REAL(wp)                            :: zepsilon      ! determines thres. above which computation of G(h) is done
+      REAL(wp)                            :: zratio_s      ! dummy factor
+      REAL(wp)                            :: zratio_i      ! dummy factor
+      REAL(wp)                            :: zh_thres      ! thickness thres. for G(h) computation
+      REAL(wp)                            :: zhe           ! dummy factor
+      REAL(wp)                            :: zkimean       ! mean sea ice thermal conductivity
+      REAL(wp)                            :: zfac          ! dummy factor
+      REAL(wp)                            :: zihe          ! dummy factor
+      REAL(wp)                            :: zheshth       ! dummy factor
+      
+      REAL(wp), POINTER, DIMENSION(:)     :: zghe          ! G(he), th. conduct enhancement factor, mono-cat
+      
       !!------------------------------------------------------------------     
       ! 
+      CALL wrk_alloc( jpij, numeqmin, numeqmax )
+      CALL wrk_alloc( jpij, isnow, ztsub, ztsubit, zh_i, zh_s, zfsw )
+      CALL wrk_alloc( jpij, zf, dzf, zqns_ice_b, zerrit, zdifcase, zftrice, zihic, zghe )
+      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, zindterm, zindtbis, 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) * r1_nlay_i +  &
+            &           SUM( q_s_1d(ji,1:nlay_s) ) * ht_s_1d(ji) * r1_nlay_s ) 
+      END DO
+
       !------------------------------------------------------------------------------!
       ! 1) Initialization                                                            !
       !------------------------------------------------------------------------------!
-      !
       DO ji = kideb , kiut
-         ! is there snow or not
-         isnow(ji)= NINT(  1._wp - MAX( 0._wp , SIGN(1._wp, - ht_s_b(ji) ) )  )
-         ! surface temperature of fusion
-!!gm ???  ztfs(ji) = rtt !!!????
-         ztfs(ji) = REAL( isnow(ji) ) * rtt + REAL( 1 - isnow(ji) ) * rtt
+         isnow(ji)= 1._wp - MAX( 0._wp , SIGN(1._wp, - ht_s_1d(ji) ) )  ! is there snow or not
          ! 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) * r1_nlay_i
+         zh_s(ji) = ht_s_1d(ji) * r1_nlay_s
       END DO
 
@@ -169,18 +200,18 @@
       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) * r1_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) * r1_nlay_i
          END DO
       END DO
       !
       !------------------------------------------------------------------------------|
-      ! 2) Radiations                                                                |
+      ! 2) Radiation								                                 |
       !------------------------------------------------------------------------------|
       !
@@ -194,20 +225,14 @@
       ! 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 
+      ! fr1_i0_1d = i0 for a thin ice cover, fr1_i0_2d = i0 for a thick ice cover
+      zhsu = 0.1_wp ! threshold for the computation of i0
       DO ji = kideb , kiut
          ! switches
-         isnow(ji) = NINT(  1._wp - MAX( 0._wp , SIGN( 1._wp , - ht_s_b(ji) ) )  ) 
+         isnow(ji) = 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) ) )     
-
-         i0(ji)    = REAL( 1 - isnow(ji) ) * ( fr1_i0_1d(ji) + zihic(ji) * fr2_i0_1d(ji) )
-         !fr1_i0_1d = i0 for a thin ice surface
-         !fr1_i0_2d = i0 for a thick ice surface
-         !            a function of the cloud cover
-         !
-         !i0(ji)     =  (1.0-FLOAT(isnow(ji)))*3.0/(100*ht_s_b(ji)+10.0)
-         !formula used in Cice
+         zihic(ji) = MAX( 0._wp , 1._wp - ( ht_i_1d(ji) / zhsu ) )     
+
+         i0(ji)    = ( 1._wp - isnow(ji) ) * ( fr1_i0_1d(ji) + zihic(ji) * fr2_i0_1d(ji) )
       END DO
 
@@ -217,7 +242,8 @@
       !-------------------------------------------------------
       DO ji = kideb , kiut
-         zfsw   (ji) =  qsr_ice_1d(ji) * ( 1 - i0(ji) )   ! Shortwave radiation absorbed at surface
-         zftrice(ji) =  qsr_ice_1d(ji) *       i0(ji)     ! Solar radiation transmitted below the surface layer
-         dzf    (ji) = dqns_ice_1d(ji)                    ! derivative of incoming nonsolar flux 
+         zfsw   (ji)    =  qsr_ice_1d(ji) * ( 1 - i0(ji) )   ! Shortwave radiation absorbed at surface
+         zftrice(ji)    =  qsr_ice_1d(ji) *       i0(ji)     ! Solar radiation transmitted below the surface layer
+         dzf    (ji)    = dqns_ice_1d(ji)                    ! derivative of incoming nonsolar flux 
+         zqns_ice_b(ji) = qns_ice_1d(ji)                     ! store previous qns_ice_1d value
       END DO
 
@@ -230,46 +256,30 @@
       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
 
       DO ji = kideb, kiut           ! ice initialization
-         zradtr_i(ji,0) = zradtr_s(ji,nlay_s) * REAL( isnow(ji) ) + zftrice(ji) * REAL( 1 - isnow(ji) )
-      END DO
-
-      DO layer = 1, nlay_i          ! Radiation through ice
+         zradtr_i(ji,0) = zradtr_s(ji,nlay_s) * isnow(ji) + zftrice(ji) * ( 1._wp - isnow(ji) )
+      END DO
+
+      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( - rn_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
-      END DO
-
-      !
+         ftr_ice_1d(ji) = zradtr_i(ji,nlay_i) 
+      END DO
+
       !------------------------------------------------------------------------------|
       !  3) Iterative procedure begins                                               |
@@ -277,19 +287,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
-         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)
+         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), rt0 - ztsu_err )       ! necessary
+         zerrit (ji) =  1000._wp                                 ! initial value of error
+      END DO
+
+      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
@@ -298,5 +308,5 @@
       zerritmax =  1000._wp    ! maximal value of error on all points
 
-      DO WHILE ( zerritmax > maxer_i_thd .AND. nconv < nconv_i_thd )
+      DO WHILE ( zerritmax > rn_terr_dif .AND. nconv < nn_conv_dif )
          !
          nconv = nconv + 1
@@ -306,116 +316,157 @@
          !------------------------------------------------------------------------------|
          !
-         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)        = MAX(ztcond_i(ji,0),zkimin)
-            END DO
-            DO layer = 1, nlay_i-1
+         IF( nn_ice_thcon == 0 ) THEN      ! Untersteiner (1964) formula
+            DO ji = kideb , kiut
+               ztcond_i(ji,0) = rcdic + zbeta * s_i_1d(ji,1) / MIN( -epsi10, t_i_1d(ji,1) - rt0 )
+               ztcond_i(ji,0) = MAX( ztcond_i(ji,0), zkimin )
+            END DO
+            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 * rt0)
+                  ztcond_i(ji,jk) = MAX( ztcond_i(ji,jk), zkimin )
                END DO
             END DO
          ENDIF
 
-         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 )  
+         IF( nn_ice_thcon == 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_1d(ji,1) / MIN( -epsi10, t_i_1d(ji,1) - rt0 )   &
+                  &                   - 0.011_wp * ( t_i_1d(ji,1) - rt0 )  
                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.09_wp * ( s_i_1d(ji,jk) + s_i_1d(ji,jk+1) )                              &
+                     &                 / MIN( -2._wp * epsi10, t_i_1d(ji,jk) + t_i_1d(ji,jk+1) - 2.0_wp * rt0 )   &
+                     &               - 0.0055_wp * ( t_i_1d(ji,jk) + t_i_1d(ji,jk+1) - 2.0 * rt0 )  
+                  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) - rt0 )   &
+                  &                        - 0.011_wp * ( t_bo_1d(ji) - rt0 )  
                ztcond_i(ji,nlay_i) = MAX( ztcond_i(ji,nlay_i), zkimin )
             END DO
          ENDIF
-         !
-         !------------------------------------------------------------------------------|
-         !  5) kappa factors                                                            |
-         !------------------------------------------------------------------------------|
-         !
+         
+         !
+         !------------------------------------------------------------------------------|
+         !  5) G(he) - enhancement of thermal conductivity in mono-category case        |
+         !------------------------------------------------------------------------------|
+         !
+         ! Computation of effective thermal conductivity G(h)
+         ! Used in mono-category case only to simulate an ITD implicitly
+         ! Fichefet and Morales Maqueda, JGR 1997
+
+         zghe(:) = 1._wp
+
+         SELECT CASE ( nn_monocat )
+
+         CASE (1,3) ! LIM3
+
+            zepsilon = 0.1_wp
+            zh_thres = EXP( 1._wp ) * zepsilon * 0.5_wp
+
+            DO ji = kideb, kiut
+   
+               ! Mean sea ice thermal conductivity
+               zkimean  = SUM( ztcond_i(ji,0:nlay_i) ) / REAL( nlay_i+1, wp )
+
+               ! Effective thickness he (zhe)
+               zfac     = 1._wp / ( rcdsn + zkimean )
+               zratio_s = rcdsn   * zfac
+               zratio_i = zkimean * zfac
+               zhe      = zratio_s * ht_i_1d(ji) + zratio_i * ht_s_1d(ji)
+
+               ! G(he)
+               rswitch  = MAX( 0._wp , SIGN( 1._wp , zhe - zh_thres ) )  ! =0 if zhe < zh_thres, if >
+               zghe(ji) = ( 1._wp - rswitch ) + rswitch * 0.5_wp * ( 1._wp + LOG( 2._wp * zhe / zepsilon ) )
+   
+               ! Impose G(he) < 2.
+               zghe(ji) = MIN( zghe(ji), 2._wp )
+
+            END DO
+
+         END SELECT
+
+         !
+         !------------------------------------------------------------------------------|
+         !  6) kappa factors                                                            |
+         !------------------------------------------------------------------------------|
+         !
+         !--- Snow
          DO ji = kideb, kiut
-
-            !-- Snow kappa factors
-            zkappa_s(ji,0)         = rcdsn / MAX(epsi10,zh_s(ji))
-            zkappa_s(ji,nlay_s)    = rcdsn / MAX(epsi10,zh_s(ji))
-         END DO
-
-         DO layer = 1, nlay_s-1
-            DO ji = kideb , kiut
-               zkappa_s(ji,layer)  = 2.0 * rcdsn / &
-                  MAX(epsi10,2.0*zh_s(ji))
-            END DO
-         END DO
-
-         DO layer = 1, nlay_i-1
-            DO ji = kideb , kiut
-               !-- Ice kappa factors
-               zkappa_i(ji,layer)  = 2.0*ztcond_i(ji,layer)/ &
-                  MAX(epsi10,2.0*zh_i(ji)) 
-            END DO
-         END DO
-
-         DO ji = kideb , kiut
-            zkappa_i(ji,0)        = ztcond_i(ji,0)/MAX(epsi10,zh_i(ji))
-            zkappa_i(ji,nlay_i)   = ztcond_i(ji,nlay_i) / MAX(epsi10,zh_i(ji))
-            !-- Interface
-            zkappa_s(ji,nlay_s)   = 2.0*rcdsn*ztcond_i(ji,0)/MAX(epsi10, &
-               (ztcond_i(ji,0)*zh_s(ji) + rcdsn*zh_i(ji)))
-            zkappa_i(ji,0)        = zkappa_s(ji,nlay_s)*REAL( isnow(ji) ) &
-               + zkappa_i(ji,0)*REAL( 1 - isnow(ji) )
-         END DO
-         !
-         !------------------------------------------------------------------------------|
-         ! 6) Sea ice specific heat, eta factors                                        |
-         !------------------------------------------------------------------------------|
-         !
-         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), &
-                  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)
-            END DO
-         END DO
-         !
-         !------------------------------------------------------------------------------|
-         ! 7) surface flux computation                                                  |
-         !------------------------------------------------------------------------------|
-         !
-         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) )
-
+            zfac                  =  1. / MAX( epsi10 , zh_s(ji) )
+            zkappa_s(ji,0)        = zghe(ji) * rcdsn * zfac
+            zkappa_s(ji,nlay_s)   = zghe(ji) * rcdsn * zfac
+         END DO
+
+         DO jk = 1, nlay_s-1
+            DO ji = kideb , kiut
+               zkappa_s(ji,jk)    = zghe(ji) * 2.0 * rcdsn / MAX( epsi10, 2.0 * zh_s(ji) )
+            END DO
+         END DO
+
+         !--- Ice
+         DO jk = 1, nlay_i-1
+            DO ji = kideb , kiut
+               zkappa_i(ji,jk)    = zghe(ji) * 2.0 * ztcond_i(ji,jk) / MAX( epsi10 , 2.0 * zh_i(ji) )
+            END DO
+         END DO
+
+         !--- Snow-ice interface
+         DO ji = kideb , kiut
+            zfac                  = 1./ MAX( epsi10 , zh_i(ji) )
+            zkappa_i(ji,0)        = zghe(ji) * ztcond_i(ji,0) * zfac
+            zkappa_i(ji,nlay_i)   = zghe(ji) * ztcond_i(ji,nlay_i) * zfac
+            zkappa_s(ji,nlay_s)   = zghe(ji) * zghe(ji) * 2.0 * rcdsn * ztcond_i(ji,0) / & 
+           &                        MAX( epsi10, ( zghe(ji) * ztcond_i(ji,0) * zh_s(ji) + zghe(ji) * rcdsn * zh_i(ji) ) )
+            zkappa_i(ji,0)        = zkappa_s(ji,nlay_s) * isnow(ji) + zkappa_i(ji,0) * ( 1._wp - isnow(ji) )
+         END DO
+
+         !
+         !------------------------------------------------------------------------------|
+         ! 7) Sea ice specific heat, eta factors                                        |
+         !------------------------------------------------------------------------------|
+         !
+         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) - rt0 ) * ( ztib(ji,jk) - rt0 ), epsi10 )
+               zeta_i(ji,jk)    = rdt_ice / MAX( rhoic * zspeche_i(ji,jk) * zh_i(ji), epsi10 )
+            END DO
+         END DO
+
+         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
+
+         !
+         !------------------------------------------------------------------------------|
+         ! 8) surface flux computation                                                  |
+         !------------------------------------------------------------------------------|
+         !
+         IF ( ln_it_qnsice ) THEN 
+            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 
-            ! (LWup, LWdw, SH, LH)
-
-         END DO
-
-         !
-         !------------------------------------------------------------------------------|
-         ! 8) tridiagonal system terms                                                  |
+            zf(ji)    =          zfsw(ji)              & ! net absorbed solar radiation
+               &         + qns_ice_1d(ji)                ! non solar total flux (LWup, LWdw, SH, LH)
+         END DO
+
+         !
+         !------------------------------------------------------------------------------|
+         ! 9) tridiagonal system terms                                                  |
          !------------------------------------------------------------------------------|
          !
@@ -427,5 +478,5 @@
          !!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.
@@ -440,11 +491,9 @@
          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)
+               zindterm(ji,numeq) =  ztib(ji,jk) + zeta_i(ji,jk) * zradab_i(ji,jk)
             END DO
          ENDDO
@@ -454,15 +503,13 @@
             !!ice bottom term
             ztrid(ji,numeq,1)  =  - zeta_i(ji,nlay_i)*zkappa_i(ji,nlay_i-1)   
-            ztrid(ji,numeq,2)  =  1.0 + zeta_i(ji,nlay_i)*( zkappa_i(ji,nlay_i)*zg1 &
-               +  zkappa_i(ji,nlay_i-1) )
+            ztrid(ji,numeq,2)  =  1.0 + zeta_i(ji,nlay_i) * ( zkappa_i(ji,nlay_i) * zg1 + zkappa_i(ji,nlay_i-1) )
             ztrid(ji,numeq,3)  =  0.0
-            zindterm(ji,numeq) =  ztiold(ji,nlay_i) + zeta_i(ji,nlay_i)* &
-               ( zradab_i(ji,nlay_i) + zkappa_i(ji,nlay_i)*zg1 &
-               *  t_bo_b(ji) ) 
+            zindterm(ji,numeq) =  ztib(ji,nlay_i) + zeta_i(ji,nlay_i) *  &
+               &                  ( zradab_i(ji,nlay_i) + zkappa_i(ji,nlay_i) * zg1 *  t_bo_1d(ji) ) 
          ENDDO
 
 
          DO ji = kideb , kiut
-            IF ( ht_s_b(ji).gt.0.0 ) THEN
+            IF ( ht_s_1d(ji) > 0.0 ) THEN
                !
                !------------------------------------------------------------------------------|
@@ -472,11 +519,9 @@
                !!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)
+                  zindterm(ji,numeq)  =  ztsb(ji,jk) + zeta_s(ji,jk) * zradab_s(ji,jk)
                END DO
 
@@ -484,9 +529,8 @@
                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) 
+                  zindterm(ji,nlay_s+2)   =  zindterm(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) < rt0 ) THEN
 
                   !------------------------------------------------------------------------------|
@@ -498,14 +542,14 @@
 
                   !!surface equation
-                  ztrid(ji,1,1) = 0.0
-                  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)
+                  ztrid(ji,1,1)  = 0.0
+                  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_1d(ji) - zf(ji)
 
                   !!first layer of snow equation
-                  ztrid(ji,2,1)  =  - zkappa_s(ji,0)*zg1s*zeta_s(ji,1)
-                  ztrid(ji,2,2)  =  1.0 + zeta_s(ji,1)*(zkappa_s(ji,1) + zkappa_s(ji,0)*zg1s)
+                  ztrid(ji,2,1)  =  - zkappa_s(ji,0) * zg1s * zeta_s(ji,1)
+                  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)
+                  zindterm(ji,2) =  ztsb(ji,1) + zeta_s(ji,1) * zradab_s(ji,1)
 
                ELSE 
@@ -521,10 +565,8 @@
                   !!first layer of snow equation
                   ztrid(ji,2,1)  =  0.0
-                  ztrid(ji,2,2)  =  1.0 + zeta_s(ji,1) * ( zkappa_s(ji,1) + &
-                     zkappa_s(ji,0) * zg1s )
+                  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) +                         &
-                     zkappa_s(ji,0) * zg1s * t_su_b(ji) ) 
+                  zindterm(ji,2) = ztsb(ji,1) + zeta_s(ji,1) *  &
+                     &             ( zradab_s(ji,1) + zkappa_s(ji,0) * zg1s * t_su_1d(ji) ) 
                ENDIF
             ELSE
@@ -534,5 +576,5 @@
                !------------------------------------------------------------------------------|
                !
-               IF (t_su_b(ji) .LT. rtt) THEN
+               IF ( t_su_1d(ji) < rt0 ) THEN
                   !
                   !------------------------------------------------------------------------------|
@@ -548,26 +590,24 @@
                   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)
+                  zindterm(ji,numeqmin(ji))  =  dzf(ji)*t_su_1d(ji) - zf(ji)
 
                   !!first layer of ice equation
                   ztrid(ji,numeqmin(ji)+1,1) =  - zkappa_i(ji,0) * zg1 * zeta_i(ji,1)
-                  ztrid(ji,numeqmin(ji)+1,2) =  1.0 + zeta_i(ji,1) * ( zkappa_i(ji,1) & 
-                     + 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)  
+                  ztrid(ji,numeqmin(ji)+1,2) =  1.0 + zeta_i(ji,1) * ( zkappa_i(ji,1) + zkappa_i(ji,0) * zg1 )
+                  ztrid(ji,numeqmin(ji)+1,3) =  - zeta_i(ji,1) * zkappa_i(ji,1)  
+                  zindterm(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)
 
-                  IF (nlay_i.eq.1) THEN
+                  IF ( nlay_i == 1 ) THEN
                      ztrid(ji,numeqmin(ji),1)    =  0.0
-                     ztrid(ji,numeqmin(ji),2)    =  dzf(ji) - zkappa_i(ji,0)*2.0
-                     ztrid(ji,numeqmin(ji),3)    =  zkappa_i(ji,0)*2.0
-                     ztrid(ji,numeqmin(ji)+1,1)  =  -zkappa_i(ji,0)*2.0*zeta_i(ji,1)
-                     ztrid(ji,numeqmin(ji)+1,2)  =  1.0 + zeta_i(ji,1)*(zkappa_i(ji,0)*2.0 + &
-                        zkappa_i(ji,1))
+                     ztrid(ji,numeqmin(ji),2)    =  dzf(ji) - zkappa_i(ji,0) * 2.0
+                     ztrid(ji,numeqmin(ji),3)    =  zkappa_i(ji,0) * 2.0
+                     ztrid(ji,numeqmin(ji)+1,1)  =  -zkappa_i(ji,0) * 2.0 * zeta_i(ji,1)
+                     ztrid(ji,numeqmin(ji)+1,2)  =  1.0 + zeta_i(ji,1) * ( zkappa_i(ji,0) * 2.0 + zkappa_i(ji,1) )
                      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) )
+                     zindterm(ji,numeqmin(ji)+1) =  ztib(ji,1) + zeta_i(ji,1) *  &
+                        &                          ( zradab_i(ji,1) + zkappa_i(ji,1) * t_bo_1d(ji) )
                   ENDIF
 
@@ -585,19 +625,16 @@
                   !!first layer of ice equation
                   ztrid(ji,numeqmin(ji),1)      =  0.0
-                  ztrid(ji,numeqmin(ji),2)      =  1.0 + zeta_i(ji,1)*(zkappa_i(ji,1) + zkappa_i(ji,0)* &
-                     zg1)  
+                  ztrid(ji,numeqmin(ji),2)      =  1.0 + zeta_i(ji,1) * ( zkappa_i(ji,1) + zkappa_i(ji,0) * 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) ) 
+                  zindterm(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)
-                  IF (nlay_i.eq.1) THEN
+                  IF ( nlay_i == 1 ) THEN
                      ztrid(ji,numeqmin(ji),1)  =  0.0
-                     ztrid(ji,numeqmin(ji),2)  =  1.0 + zeta_i(ji,1)*(zkappa_i(ji,0)*2.0 + &
-                        zkappa_i(ji,1))
+                     ztrid(ji,numeqmin(ji),2)  =  1.0 + zeta_i(ji,1) * ( zkappa_i(ji,0) * 2.0 + 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
+                     zindterm(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
 
@@ -609,5 +646,5 @@
          !
          !------------------------------------------------------------------------------|
-         ! 9) tridiagonal system solving                                                |
+         ! 10) tridiagonal system solving                                               |
          !------------------------------------------------------------------------------|
          !
@@ -618,5 +655,5 @@
 
          maxnumeqmax = 0
-         minnumeqmin = jkmax+4
+         minnumeqmin = nlay_i+5
 
          DO ji = kideb , kiut
@@ -627,11 +664,9 @@
          END DO
 
-         DO layer = minnumeqmin+1, maxnumeqmax
-            DO ji = kideb , kiut
-               numeq               =  min(max(numeqmin(ji)+1,layer),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)
+         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)
             END DO
          END DO
@@ -639,12 +674,11 @@
          DO ji = kideb , kiut
             ! ice temperatures
-            t_i_b(ji,nlay_i)    =  zindtbis(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)
+            t_i_1d(ji,nlay_i)    =  zindtbis(ji,numeqmax(ji)) / zdiagbis(ji,numeqmax(ji))
+         END DO
+
+         DO numeq = nlay_i + nlay_s, nlay_s + 2, -1
+            DO ji = kideb , kiut
+               jk    =  numeq - nlay_s - 1
+               t_i_1d(ji,jk)  =  ( zindtbis(ji,numeq) - ztrid(ji,numeq,3) * t_i_1d(ji,jk+1) ) / zdiagbis(ji,numeq)
             END DO
          END DO
@@ -652,42 +686,39 @@
          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) > 0._wp) &
+               t_s_1d(ji,nlay_s)     =  ( zindtbis(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)   = 1._wp - MAX( 0._wp , SIGN( 1._wp , -ht_s_1d(ji) ) )
+            ztsubit(ji) = t_su_1d(ji)
+            IF( t_su_1d(ji) < rt0 ) &
+               t_su_1d(ji) = ( zindtbis(ji,numeqmin(ji)) - ztrid(ji,numeqmin(ji),3) *  &
+               &             ( isnow(ji) * t_s_1d(ji,1) + ( 1._wp - isnow(ji) ) * t_i_1d(ji,1) ) ) / zdiagbis(ji,numeqmin(ji))  
          END DO
          !
          !--------------------------------------------------------------------------
-         !  10) Has the scheme converged ?, end of the iterative procedure         |
+         !  11) Has the scheme converged ?, end of the iterative procedure         |
          !--------------------------------------------------------------------------
          !
          ! check that nowhere it has started to melt
-         ! 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)))
+         ! zerrit(ji) is a measure of error, it has to be under terr_dif
+         DO ji = kideb , kiut
+            t_su_1d(ji) =  MAX(  MIN( t_su_1d(ji) , rt0 ) , 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), rt0 ), 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) + rt0 
+               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
@@ -703,5 +734,5 @@
       END DO  ! End of the do while iterative procedure
 
-      IF( ln_nicep .AND. lwp ) THEN
+      IF( ln_icectl .AND. lwp ) THEN
          WRITE(numout,*) ' zerritmax : ', zerritmax
          WRITE(numout,*) ' nconv     : ', nconv
@@ -710,48 +741,97 @@
       !
       !-------------------------------------------------------------------------!
-      !   11) Fluxes at the interfaces                                          !
+      !   12) Fluxes at the interfaces                                          !
       !-------------------------------------------------------------------------!
       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
          !                                ! 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)       = 1._wp - MAX( 0._wp, SIGN( 1._wp, -ht_s_1d(ji) ) )
+         fc_su(ji)       =  -           isnow(ji)   * zkappa_s(ji,0) * zg1s * (t_s_1d(ji,1) - t_su_1d(ji))   &
+            &               - ( 1._wp - 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
+
+      ! --- computes sea ice energy of melting compulsory for limthd_dh --- !
+      CALL lim_thd_enmelt( kideb, kiut )
+
+      ! --- diagnose the change in non-solar flux due to surface temperature change --- !
+      IF ( ln_it_qnsice ) THEN
          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
-      ENDIF
+            hfx_err_dif_1d(ji) = hfx_err_dif_1d(ji) - ( qns_ice_1d(ji)  - zqns_ice_b(ji) ) * a_i_1d(ji) 
+         END DO
+      END IF
+
+      ! --- 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) * r1_nlay_i +  &
+            &                              SUM( q_s_1d(ji,1:nlay_s) ) * ht_s_1d(ji) * r1_nlay_s )
+         IF( t_su_1d(ji) < rt0 ) THEN  ! case T_su < 0degC
+            zhfx_err(ji) = qns_ice_1d(ji) + qsr_ice_1d(ji) - zradtr_i(ji,nlay_i) - fc_bo_i(ji) + zdq(ji) * r1_rdtice 
+         ELSE                          ! case T_su = 0degC
+            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 
+         ENDIF
+         hfx_err_1d(ji) = hfx_err_1d(ji) + zhfx_err(ji) * a_i_1d(ji)
+
+         ! total heat that is sent to the ocean (i.e. not used in the heat diffusion equation)
+         hfx_err_dif_1d(ji) = hfx_err_dif_1d(ji) + zhfx_err(ji) * a_i_1d(ji)
+      END DO 
+
+      !-----------------------------------------
+      ! Heat flux used to warm/cool ice in W.m-2
+      !-----------------------------------------
+      DO ji = kideb, kiut
+         IF( t_su_1d(ji) < rt0 ) 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
+         ! correction on the diagnosed heat flux due to non-convergence of the algorithm used to solve heat equation
+         hfx_dif_1d(ji) = hfx_dif_1d(ji) - zhfx_err(ji) * a_i_1d(ji)
+      END DO   
       !
+      CALL wrk_dealloc( jpij, numeqmin, numeqmax )
+      CALL wrk_dealloc( jpij, isnow, ztsub, ztsubit, zh_i, zh_s, zfsw )
+      CALL wrk_dealloc( jpij, zf, dzf, zqns_ice_b, zerrit, zdifcase, zftrice, zihic, zghe )
+      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, zindterm, zindtbis, 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) + rt0
+            t_i_1d(ji,jk) = MIN( t_i_1d(ji,jk), ztmelts ) ! Force t_i_1d to be lower than melting point
+                                                          !   (sometimes dif scheme produces abnormally high temperatures)   
+            q_i_1d(ji,jk) = rhoic * ( cpic * ( ztmelts - t_i_1d(ji,jk) )                           &
+               &                    + lfus * ( 1.0 - ( ztmelts-rt0 ) / ( t_i_1d(ji,jk) - rt0 ) )   &
+               &                    - rcp  *         ( ztmelts-rt0 )  ) 
+         END DO
+      END DO
+      DO jk = 1, nlay_s             ! Snow energy of melting
+         DO ji = kideb, kiut
+            q_s_1d(ji,jk) = rhosn * ( cpic * ( rt0 - t_s_1d(ji,jk) ) + lfus )
+         END DO
+      END DO
+      !
+   END SUBROUTINE lim_thd_enmelt
 
 #else
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd_ent.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd_ent.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd_ent.F90	(revision 5832)
@@ -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,6 +23,6 @@
    USE domain         !
    USE phycst         ! physical constants
+   USE sbc_oce        ! Surface boundary condition: ocean fields
    USE ice            ! LIM variables
-   USE par_ice        ! LIM parameters
    USE thd_ice        ! LIM thermodynamics
    USE limvar         ! LIM variables
@@ -34,10 +35,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 +44,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 +56,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 * thickness 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) ) * r1_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      = MAX( 0._wp , SIGN( 1._wp , zhnew(ji) - epsi20 ) ) 
+            qnew(ji,jk1) = rswitch * ( zqh_cum1(ji,jk1) - zqh_cum1(ji,jk1-1) ) / MAX( zhnew(ji), epsi20 )
+         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_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd_lac.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd_lac.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd_lac.F90	(revision 5832)
@@ -22,5 +22,4 @@
    USE thd_ice        ! LIM thermodynamics
    USE dom_ice        ! LIM domain
-   USE par_ice        ! LIM parameters
    USE ice            ! LIM variables
    USE limtab         ! LIM 2D <==> 1D
@@ -29,5 +28,8 @@
    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
+   USE limvar
 
    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,57 +98,30 @@
       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(:,:) ::   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
+
+      REAL(wp) :: zcai = 1.4e-3_wp
       !!-----------------------------------------------------------------------!
 
-      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, zsmv_i_1d )
+      CALL wrk_alloc( jpij,nlay_i,jpl, ze_i_1d )
+      CALL wrk_alloc( jpi,jpj, zvrel )
+
+      CALL lim_var_agg(1)
+      CALL lim_var_glo2eqv
       !------------------------------------------------------------------------------|
       ! 2) Convert units for ice internal energy
@@ -154,7 +132,6 @@
                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          = MAX(  0._wp , SIGN( 1._wp , v_i(ji,jj,jl) - epsi20 )  )   !0 if no ice
+                  e_i(ji,jj,jk,jl) = rswitch * e_i(ji,jj,jk,jl) / MAX( v_i(ji,jj,jl), epsi20 ) * REAL( nlay_i, wp )
                END DO
             END DO
@@ -179,7 +156,7 @@
 
       ! Default new ice thickness 
-      hicol(:,:) = hiccrit(1)
-
-      IF( fraz_swi == 1._wp ) THEN
+      hicol(:,:) = rn_hnewice
+
+      IF( ln_frazil ) THEN
 
          !--------------------
@@ -190,27 +167,27 @@
          zhicrit = 0.04 ! frazil ice thickness
          ztwogp  = 2. * rau0 / ( grav * 0.3 * ( rau0 - rhoic ) ) ! reduced grav
-         zsqcd   = 1.0 / SQRT( 1.3 * cai ) ! 1/SQRT(airdensity*drag)
+         zsqcd   = 1.0 / SQRT( 1.3 * zcai ) ! 1/SQRT(airdensity*drag)
          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
                   !-------------
                   ! C-grid wind stress components
-                  ztaux         = ( utau_ice(ji-1,jj  ) * tmu(ji-1,jj  )   &
-                     &          +   utau_ice(ji  ,jj  ) * tmu(ji  ,jj  ) ) * 0.5_wp
-                  ztauy         = ( vtau_ice(ji  ,jj-1) * tmv(ji  ,jj-1)   &
-                     &          +   vtau_ice(ji  ,jj  ) * tmv(ji  ,jj  ) ) * 0.5_wp
+                  ztaux         = ( utau_ice(ji-1,jj  ) * umask(ji-1,jj  ,1)   &
+                     &          +   utau_ice(ji  ,jj  ) * umask(ji  ,jj  ,1) ) * 0.5_wp
+                  ztauy         = ( vtau_ice(ji  ,jj-1) * vmask(ji  ,jj-1,1)   &
+                     &          +   vtau_ice(ji  ,jj  ) * vmask(ji  ,jj  ,1) ) * 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 +195,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  ) * umask(ji-1,jj  ,1)  + u_ice(ji,jj) * umask(ji,jj,1) ) * 0.5_wp
+                  zvgy    = rswitch * ( v_ice(ji  ,jj-1) * vmask(ji  ,jj-1,1)  + v_ice(ji,jj) * vmask(ji,jj,1) ) * 0.5_wp
 
                   !-----------------------------------
@@ -248,5 +223,5 @@
                   iterate_frazil = .true.
 
-                  DO WHILE ( iter .LT. 100 .AND. iterate_frazil ) 
+                  DO WHILE ( iter < 100 .AND. iterate_frazil ) 
                      zf = ( hicol(ji,jj) - zhicrit ) * ( hicol(ji,jj)**2 - zhicrit**2 ) &
                         - hicol(ji,jj) * zhicrit * ztwogp * zvrel2
@@ -264,4 +239,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 +254,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
@@ -287,8 +267,8 @@
       ! debug point to follow
       jiindex_1d = 0
-      IF( ln_nicep ) THEN
-         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( ln_icectl ) THEN
+         DO ji = mi0(iiceprt), mi1(iiceprt)
+            DO jj = mj0(jiceprt), mj1(jiceprt)
+               IF ( qlead(ji,jj)  <  0._wp ) THEN
                   jiindex_1d = (jj - 1) * jpi + ji
                ENDIF
@@ -297,5 +277,5 @@
       ENDIF
    
-      IF( ln_nicep ) WRITE(numout,*) 'lim_thd_lac : nbpac = ', nbpac
+      IF( ln_icectl ) WRITE(numout,*) 'lim_thd_lac : nbpac = ', nbpac
 
       !------------------------------
@@ -307,22 +287,23 @@
       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, 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) )
-            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, ze_i_1d(1:nbpac,jk,jl), e_i(:,:,jk,jl) , jpi, jpj, npac(1:nbpac) )
+            END DO
+         END DO
+
+         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,29 +311,32 @@
          !------------------------------------------------------------------------------!
 
+         !-----------------------------------------
+         ! 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) = rn_hnewice
+         END DO
+         IF( ln_frazil ) zh_newice(1:nbpac) = hicol_1d(1:nbpac)
 
          !----------------------
          ! Salinity of new ice 
          !----------------------
-
-         SELECT CASE ( num_sal )
+         SELECT CASE ( nn_icesal )
          CASE ( 1 )                    ! Sice = constant 
-            zs_newice(:) = bulk_sal
+            zs_newice(1:nbpac) = rn_icesal
          CASE ( 2 )                    ! Sice = F(z,t) [Vancoppenolle et al (2005)]
             DO ji = 1, nbpac
                ii =   MOD( npac(ji) - 1 , jpi ) + 1
                ij =      ( npac(ji) - 1 ) / jpi + 1
-               zs_newice(ji) = MIN(  4.606 + 0.91 / zh_newice(ji) , s_i_max , 0.5 * sss_m(ii,ij)  )
+               zs_newice(ji) = MIN(  4.606 + 0.91 / zh_newice(ji) , rn_simax , 0.5 * sss_m(ii,ij)  )
             END DO
          CASE ( 3 )                    ! Sice = F(z) [multiyear ice]
-            zs_newice(:) =   2.3
+            zs_newice(1:nbpac) =   2.3
          END SELECT
-
 
          !-------------------------
@@ -361,11 +345,10 @@
          ! We assume that new ice is formed at the seawater freezing point
          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 ) )   &
-               &                       - 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
+            ztmelts       = - tmut * zs_newice(ji) + rt0                  ! Melting point (K)
+            ze_newice(ji) =   rhoic * (  cpic * ( ztmelts - t_bo_1d(ji) )                             &
+               &                       + lfus * ( 1.0 - ( ztmelts - rt0 ) / MIN( t_bo_1d(ji) - rt0, -epsi10 ) )   &
+               &                       - rcp  *         ( ztmelts - rt0 )  )
+         END DO
+
          !----------------
          ! Age of new ice
@@ -373,12 +356,5 @@
          DO ji = 1, nbpac
             zo_newice(ji) = 0._wp
-         END DO ! ji
-
-         !--------------------------
-         ! Open water energy budget 
-         !--------------------------
-         DO ji = 1, nbpac
-            zqbgow(ji) = qldif_1d(ji) - qcmif_1d(ji)     !<0
-         END DO ! ji
+         END DO
 
          !-------------------
@@ -386,28 +362,33 @@
          !-------------------
          DO ji = 1, nbpac
-            zv_newice(ji) = - zqbgow(ji) / ze_newice(ji)
+
+            zEi           = - ze_newice(ji) * r1_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 * r1_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 ( rn_Cfrazb * ( zvrel_1d(ji) - rn_vfrazb ) ) + 1.0 ) * 0.5 * rn_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) >  ( rn_amax - zat_i_1d(ji) ) ) THEN
+               zda_res(ji)   = za_newice(ji) - ( rn_amax - zat_i_1d(ji) )
                zdv_res(ji)   = zda_res  (ji) * zh_newice(ji) 
                za_newice(ji) = za_newice(ji) - zda_res  (ji)
@@ -446,232 +418,114 @@
                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) * r1_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
-
-         !------------
-         ! Update age 
-         !------------
-         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   
-            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
 
          !-----------------
          ! 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, 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 J/m2
+                  e_i(ji,jj,jk,jl) = e_i(ji,jj,jk,jl) * v_i(ji,jj,jl) * r1_nlay_i 
+               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, zsmv_i_1d )
+      CALL wrk_dealloc( jpij,nlay_i,jpl, ze_i_1d )
+      CALL wrk_dealloc( jpi,jpj, zvrel )
       !
    END SUBROUTINE lim_thd_lac
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd_sal.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd_sal.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limthd_sal.F90	(revision 5832)
@@ -18,5 +18,4 @@
    USE sbc_oce        ! Surface boundary condition: ocean fields
    USE ice            ! LIM variables
-   USE par_ice        ! LIM parameters
    USE thd_ice        ! LIM thermodynamics
    USE limvar         ! LIM variables
@@ -30,5 +29,5 @@
 
    PUBLIC   lim_thd_sal        ! called by limthd module
-   PUBLIC   lim_thd_sal_init   ! called by iceini module
+   PUBLIC   lim_thd_sal_init   ! called by sbc_lim_init
 
    !!----------------------------------------------------------------------
@@ -46,27 +45,30 @@
       !!
       !! ** Method  :  3 possibilities
-      !!               -> num_sal = 1 -> Sice = cst    [ice salinity constant in both time & space] 
-      !!               -> num_sal = 2 -> Sice = S(z,t) [Vancoppenolle et al. 2005]
-      !!               -> num_sal = 3 -> Sice = S(z)   [multiyear ice]
+      !!               -> nn_icesal = 1 -> Sice = cst    [ice salinity constant in both time & space] 
+      !!               -> nn_icesal = 2 -> Sice = S(z,t) [Vancoppenolle et al. 2005]
+      !!               -> nn_icesal = 3 -> Sice = S(z)   [multiyear ice]
       !!---------------------------------------------------------------------
       INTEGER, INTENT(in) ::   kideb, kiut   ! thickness category index
       !
       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           ===>>>   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_new(kideb:kiut)          =  bulk_sal
+!!gm comment: if nn_icesal = 1 s_i_new, s_i_1d and sm_i_1d can be set to rn_icesal one for all in the initialisation phase !!
+!!gm           ===>>>   simplification of almost all test on nn_icesal value
+      IF(  nn_icesal == 1  ) THEN
+            s_i_1d (kideb:kiut,1:nlay_i) =  rn_icesal
+            sm_i_1d(kideb:kiut)          =  rn_icesal 
+            s_i_new(kideb:kiut)          =  rn_icesal
       ENDIF
 
@@ -74,24 +76,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
+      IF(  nn_icesal == 2  ) THEN
 
          DO ji = kideb, kiut
@@ -99,17 +82,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) - rt0 )        )     ! =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) - rn_sal_gd , 0._wp ) / rn_time_gd * rdt_ice 
+            ! drainage by flushing  
+            dsm_i_fl_1d(ji) = - iflush  * MAX( sm_i_1d(ji) - rn_sal_fl , 0._wp ) / rn_time_fl * rdt_ice
 
             !-----------------
@@ -118,19 +98,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 +109,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 +115,6 @@
       !  Module 3 : Profile of salinity, constant in time                            |
       !------------------------------------------------------------------------------|
+      IF(  nn_icesal == 3  )   CALL lim_var_salprof1d( kideb, kiut )
 
-      IF(  num_sal == 3  )   CALL lim_var_salprof1d( kideb, kiut )
-
-      !
-      CALL wrk_dealloc( jpij, ze_init, zhiold, zsiold )
       !
    END SUBROUTINE lim_thd_sal
@@ -182,6 +133,6 @@
       !!-------------------------------------------------------------------
       INTEGER  ::   ios                 ! Local integer output status for namelist read
-      NAMELIST/namicesal/ num_sal, bulk_sal, sal_G, time_G, sal_F, time_F,   &
-         &                s_i_max, s_i_min, s_i_0, s_i_1
+      NAMELIST/namicesal/ nn_icesal, rn_icesal, rn_sal_gd, rn_time_gd, rn_sal_fl, rn_time_fl,   &
+         &                rn_simax, rn_simin 
       !!-------------------------------------------------------------------
       !
@@ -199,14 +150,12 @@
          WRITE(numout,*) 'lim_thd_sal_init : Ice parameters for salinity '
          WRITE(numout,*) '~~~~~~~~~~~~~~~~'
-         WRITE(numout,*) ' switch for salinity num_sal        : ', num_sal
-         WRITE(numout,*) ' bulk salinity value if num_sal = 1 : ', bulk_sal
-         WRITE(numout,*) ' restoring salinity for GD          : ', sal_G
-         WRITE(numout,*) ' restoring time for GD              : ', time_G
-         WRITE(numout,*) ' restoring salinity for flushing    : ', sal_F
-         WRITE(numout,*) ' restoring time for flushing        : ', time_F
-         WRITE(numout,*) ' Maximum tolerated ice salinity     : ', s_i_max
-         WRITE(numout,*) ' Minimum tolerated ice salinity     : ', s_i_min
-         WRITE(numout,*) ' 1st salinity for salinity profile  : ', s_i_0
-         WRITE(numout,*) ' 2nd salinity for salinity profile  : ', s_i_1
+         WRITE(numout,*) '   switch for salinity nn_icesal        = ', nn_icesal
+         WRITE(numout,*) '   bulk salinity value if nn_icesal = 1 = ', rn_icesal
+         WRITE(numout,*) '   restoring salinity for GD            = ', rn_sal_gd
+         WRITE(numout,*) '   restoring time for GD                = ', rn_time_gd
+         WRITE(numout,*) '   restoring salinity for flushing      = ', rn_sal_fl
+         WRITE(numout,*) '   restoring time for flushing          = ', rn_time_fl
+         WRITE(numout,*) '   Maximum tolerated ice salinity       = ', rn_simax
+         WRITE(numout,*) '   Minimum tolerated ice salinity       = ', rn_simin
       ENDIF
       !
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limtrp.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limtrp.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limtrp.F90	(revision 5832)
@@ -17,9 +17,10 @@
    USE dom_oce        ! ocean domain
    USE sbc_oce        ! ocean surface boundary condition
-   USE par_ice        ! ice parameter
    USE dom_ice        ! ice domain
    USE ice            ! ice variables
    USE limadv         ! ice advection
    USE limhdf         ! ice horizontal diffusion
+   USE limvar         ! 
+   !
    USE in_out_manager ! I/O manager
    USE lbclnk         ! lateral boundary conditions -- MPP exchanges
@@ -28,15 +29,14 @@
    USE prtctl         ! Print control
    USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
-   USE limvar          ! clem for ice thickness correction
-   USE timing          ! Timing
+   USE timing         ! Timing
+   USE limcons        ! conservation tests
+   USE limctl         ! control prints
 
    IMPLICIT NONE
    PRIVATE
 
-   PUBLIC   lim_trp    ! called by ice_step
-
-   REAL(wp)  ::   epsi10 = 1.e-10_wp  
-   REAL(wp)  ::   rzero  = 0._wp   
-   REAL(wp)  ::   rone   = 1._wp
+   PUBLIC   lim_trp    ! called by sbcice_lim
+
+   INTEGER  ::   ncfl                 ! number of ice time step with CFL>1/2  
 
    !! * Substitution
@@ -61,44 +61,28 @@
       !! ** action :
       !!---------------------------------------------------------------------
-      INTEGER, INTENT(in) ::   kt   ! number of iteration
+      INTEGER, INTENT(in) ::   kt           ! number of iteration
       !
-      INTEGER  ::   ji, jj, jk, jl, layer   ! dummy loop indices
+      INTEGER  ::   ji, jj, jk, jl, jt      ! 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           !   -      -
+      CHARACTER(len=80) ::   cltmp
       !
-      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(:,:)      ::   zsm
+      REAL(wp), POINTER, DIMENSION(:,:,:)    ::   z0ice, z0snw, z0ai, z0es , z0smi , z0oi
+      REAL(wp), POINTER, DIMENSION(:,:,:)    ::   z0opw
+      REAL(wp), POINTER, DIMENSION(:,:,:,:)  ::   z0ei
+      REAL(wp), POINTER, DIMENSION(:,:,:)    ::   zviold, zvsold, zsmvold  ! old ice volume...
+      REAL(wp), POINTER, DIMENSION(:,:,:)    ::   zhimax                   ! old ice thickness
+      REAL(wp), POINTER, DIMENSION(:,:)      ::   zatold, zeiold, zesold   ! old concentration, enthalpies
+      REAL(wp) ::    zdv, 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, 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,            zsm, zatold, zeiold, zesold )
+      CALL wrk_alloc( jpi,jpj,jpl,        z0ice, z0snw, z0ai, z0es , z0smi , z0oi )
+      CALL wrk_alloc( jpi,jpj,1,          z0opw )
+      CALL wrk_alloc( jpi,jpj,nlay_i,jpl, z0ei )
+      CALL wrk_alloc( jpi,jpj,jpl,        zhimax, zviold, zvsold, zsmvold )
 
       IF( numit == nstart .AND. lwp ) THEN
@@ -108,30 +92,43 @@
          ENDIF
          WRITE(numout,*) '~~~~~~~~~~~~'
+         ncfl = 0                ! nb of time step with CFL > 1/2
       ENDIF
+
+      zsm(:,:) = e12t(:,:)
       
-      zsm(:,:) = area(:,:)
-
       !                             !-------------------------------------!
       IF( ln_limdyn ) THEN          !   Advection of sea ice properties   !
          !                          !-------------------------------------!
-         ! mass and salt flux init (clem)
+
+         ! 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
          zviold(:,:,:)  = v_i(:,:,:)
-
-         !--- Thickness correction init. (clem) -------------------------------
-         CALL lim_var_glo2eqv
-         zaiold(:,:,:) = a_i(:,:,:)
+         zvsold(:,:,:)  = v_s(:,:,:)
+         zsmvold(:,:,:) = smv_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. -------------------------------
+         zatold(:,:) = SUM( a_i(:,:,:), dim=3 )
+         DO jl = 1, jpl
+            DO jj = 1, jpj
+               DO ji = 1, jpi
+                  rswitch          = MAX( 0._wp , SIGN( 1._wp, a_i(ji,jj,jl) - epsi20 ) )
+                  ht_i  (ji,jj,jl) = v_i (ji,jj,jl) / MAX( a_i(ji,jj,jl) , epsi20 ) * rswitch
+                  ht_s  (ji,jj,jl) = v_s (ji,jj,jl) / MAX( a_i(ji,jj,jl) , epsi20 ) * rswitch
+               END DO
+            END DO
+         END DO
          !---------------------------------------------------------------------
-         ! Record max of the surrounding ice thicknesses for correction in limupdate
+         ! Record max of the surrounding ice thicknesses for correction
          ! in case advection creates ice too thick.
          !---------------------------------------------------------------------
-         zhimax(:,:,:) = ht_i(:,:,:)
+         zhimax(:,:,:) = ht_i(:,:,:) + ht_s(:,:,:)
          DO jl = 1, jpl
             DO jj = 2, jpjm1
                DO ji = 2, jpim1
-                  zhimax(ji,jj,jl) = MAXVAL( ht_i(ji-1:ji+1,jj-1:jj+1,jl) )
-                  !zhimax(ji,jj,jl) = ( ht_i(ji  ,jj  ,jl) * tmask(ji,  jj  ,1) + ht_i(ji-1,jj-1,jl) * tmask(ji-1,jj-1,1) + ht_i(ji+1,jj+1,jl) * tmask(ji+1,jj+1,1) &
-                  !     &             + ht_i(ji-1,jj  ,jl) * tmask(ji-1,jj  ,1) + ht_i(ji  ,jj-1,jl) * tmask(ji  ,jj-1,1) &
-                  !     &             + ht_i(ji+1,jj  ,jl) * tmask(ji+1,jj  ,1) + ht_i(ji  ,jj+1,jl) * tmask(ji  ,jj+1,1) &
-                  !     &             + ht_i(ji-1,jj+1,jl) * tmask(ji-1,jj+1,1) + ht_i(ji+1,jj-1,jl) * tmask(ji+1,jj-1,1) )
+                  zhimax(ji,jj,jl) = MAXVAL( ht_i(ji-1:ji+1,jj-1:jj+1,jl) + ht_s(ji-1:ji+1,jj-1:jj+1,jl) )
                END DO
             END DO
@@ -139,118 +136,123 @@
          END DO
          
+         !=============================!
+         !==      Prather scheme     ==!
+         !=============================!
+
+         ! If ice drift field is too fast, use an appropriate time step for advection.         
+         zcfl  =            MAXVAL( ABS( u_ice(:,:) ) * rdt_ice * r1_e1u(:,:) )         ! CFL test for stability
+         zcfl  = MAX( zcfl, MAXVAL( ABS( v_ice(:,:) ) * rdt_ice * r1_e2v(:,:) ) )
+         IF(lk_mpp )   CALL mpp_max( zcfl )
+
+         IF( zcfl > 0.5 ) THEN   ;   initad = 2   ;   zusnit = 0.5_wp
+         ELSE                    ;   initad = 1   ;   zusnit = 1.0_wp
+         ENDIF
+
+         IF( zcfl > 0.5_wp .AND. lwp )   ncfl = ncfl + 1
+!!         IF( lwp ) THEN
+!!            IF( ncfl > 0 ) THEN   
+!!               WRITE(cltmp,'(i6.1)') ncfl
+!!               CALL ctl_warn( 'lim_trp: ncfl= ', TRIM(cltmp), 'advective ice time-step using a split in sub-time-step ')
+!!            ELSE
+!!            !  WRITE(numout,*) 'lim_trp : CFL criterion for ice advection is always smaller than 1/2 '
+!!            ENDIF
+!!         ENDIF
+
          !-------------------------
          ! transported fields                                        
          !-------------------------
-         ! Snow vol, ice vol, salt and age contents, area
-         zs0ow(:,:) = ato_i(:,:) * area(:,:)               ! Open water area 
-         DO jl = 1, jpl
-            zs0sn (:,:,jl)   = v_s  (:,:,jl) * area(:,:)    ! Snow volume
-            zs0ice(:,:,jl)   = v_i  (:,:,jl) * area(:,:)    ! Ice  volume
-            zs0a  (:,:,jl)   = a_i  (:,:,jl) * area(:,:)    ! Ice area
-            zs0sm (:,:,jl)   = smv_i(:,:,jl) * area(:,:)    ! Salt content
-            zs0oi (:,:,jl)   = oa_i (:,:,jl) * area(:,:)    ! Age content
-            zs0c0 (:,:,jl)   = e_s  (:,:,1,jl)              ! Snow heat content
-            zs0e  (:,:,:,jl) = e_i  (:,:,:,jl)              ! Ice  heat content
-         END DO
-
-         !--------------------------
-         ! Advection of Ice fields  (Prather scheme)                                            
-         !--------------------------
-         ! If ice drift field is too fast, use an appropriate time step for advection.         
-         ! CFL test for stability
-         zcfl  =            MAXVAL( ABS( u_ice(:,:) ) * rdt_ice / e1u(:,:) )
-         zcfl  = MAX( zcfl, MAXVAL( ABS( v_ice(:,:) ) * rdt_ice / e2v(:,:) ) )
-         IF(lk_mpp )   CALL mpp_max( zcfl )
-!!gm more readability:
-!         IF( zcfl > 0.5 ) THEN   ;   initad = 2   ;   zusnit = 0.5_wp
-!         ELSE                    ;   initad = 1   ;   zusnit = 1.0_wp
-!         ENDIF
-!!gm end
-         initad = 1 + NINT( MAX( rzero, SIGN( rone, zcfl-0.5 ) ) )
-         zusnit = 1.0 / REAL( initad ) 
-         IF( zcfl > 0.5 .AND. lwp )   &
-            WRITE(numout,*) 'lim_trp   : CFL violation at day ', nday, ', cfl = ', zcfl,   &
-               &                        ': the ice time stepping is split in two'
+         z0opw(:,:,1) = ato_i(:,:) * e12t(:,:)             ! Open water area 
+         DO jl = 1, jpl
+            z0snw (:,:,jl)  = v_s  (:,:,jl) * e12t(:,:)    ! Snow volume
+            z0ice(:,:,jl)   = v_i  (:,:,jl) * e12t(:,:)    ! Ice  volume
+            z0ai  (:,:,jl)  = a_i  (:,:,jl) * e12t(:,:)    ! Ice area
+            z0smi (:,:,jl)  = smv_i(:,:,jl) * e12t(:,:)    ! Salt content
+            z0oi (:,:,jl)   = oa_i (:,:,jl) * e12t(:,:)    ! Age content
+            z0es (:,:,jl)   = e_s  (:,:,1,jl) * e12t(:,:)  ! Snow heat content
+            DO jk = 1, nlay_i
+               z0ei  (:,:,jk,jl) = e_i  (:,:,jk,jl) * e12t(:,:) ! Ice  heat content
+            END DO
+         END DO
+
 
          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
-                  &                                       sxxopw(:,:), syopw(:,:), syyopw(:,:), sxyopw(:,:)  )
-               CALL lim_adv_y( zusnit, v_ice, rzero, zsm, zs0ow (:,:), sxopw(:,:),   &
-                  &                                       sxxopw(:,:), syopw(:,:), syyopw(:,:), sxyopw(:,:)  )
+            DO jt = 1, initad
+               CALL lim_adv_x( zusnit, u_ice, 1._wp, zsm, z0opw (:,:,1), sxopw(:,:),   &             !--- ice open water area
+                  &                                       sxxopw(:,:)  , syopw(:,:), syyopw(:,:), sxyopw(:,:)  )
+               CALL lim_adv_y( zusnit, v_ice, 0._wp, zsm, z0opw (:,:,1), 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, z0ice (:,:,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, z0ice (:,:,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, z0snw (:,:,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, z0snw (:,:,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, z0smi (:,:,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, z0smi (:,:,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, z0oi  (:,:,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, z0oi  (:,:,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, z0ai  (:,:,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, z0ai  (:,:,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, z0es  (:,:,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, z0es  (:,:,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, z0ei(:,:,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, z0ei(:,:,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
-                  &                                       sxxopw(:,:), syopw(:,:), syyopw(:,:), sxyopw(:,:)  )
-               CALL lim_adv_x( zusnit, u_ice, rzero, zsm, zs0ow (:,:), sxopw(:,:),   &
-                  &                                       sxxopw(:,:), syopw(:,:), syyopw(:,:), sxyopw(:,:)  )
+            DO jt = 1, initad
+               CALL lim_adv_y( zusnit, v_ice, 1._wp, zsm, z0opw (:,:,1), sxopw(:,:),   &             !--- ice open water area
+                  &                                       sxxopw(:,:)  , syopw(:,:), syyopw(:,:), sxyopw(:,:)  )
+               CALL lim_adv_x( zusnit, u_ice, 0._wp, zsm, z0opw (:,:,1), 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, z0ice (:,:,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, z0ice (:,:,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, z0snw (:,:,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, z0snw (:,:,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, z0smi (:,:,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, z0smi (:,:,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, z0oi  (:,:,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, z0oi  (:,:,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, z0ai  (:,:,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, z0ai  (:,:,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, z0es  (:,:,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, z0es  (:,:,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, z0ei(:,:,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, z0ei(:,:,jk,jl), sxe (:,:,jk,jl),   & 
+                        &                                       sxxe(:,:,jk,jl), sye (:,:,jk,jl),   &
+                        &                                       syye(:,:,jk,jl), sxye(:,:,jk,jl) )
                   END DO
                END DO
@@ -261,40 +263,41 @@
          ! Recover the properties from their contents
          !-------------------------------------------
-         zs0ow(:,:) = zs0ow(:,:) / area(:,:)
-         DO jl = 1, jpl
-            zs0ice(:,:,jl) = zs0ice(:,:,jl) / area(:,:)
-            zs0sn (:,:,jl) = zs0sn (:,:,jl) / area(:,:)
-            zs0sm (:,:,jl) = zs0sm (:,:,jl) / area(:,:)
-            zs0oi (:,:,jl) = zs0oi (:,:,jl) / area(:,:)
-            zs0a  (:,:,jl) = zs0a  (:,:,jl) / area(:,:)
-            zs0c0 (:,:,jl) = zs0c0 (:,:,jl) / area(:,:)
+         ato_i(:,:) = z0opw(:,:,1) * r1_e12t(:,:)
+         DO jl = 1, jpl
+            v_i  (:,:,jl)   = z0ice(:,:,jl) * r1_e12t(:,:)
+            v_s  (:,:,jl)   = z0snw(:,:,jl) * r1_e12t(:,:)
+            smv_i(:,:,jl)   = z0smi(:,:,jl) * r1_e12t(:,:)
+            oa_i (:,:,jl)   = z0oi (:,:,jl) * r1_e12t(:,:)
+            a_i  (:,:,jl)   = z0ai (:,:,jl) * r1_e12t(:,:)
+            e_s  (:,:,1,jl) = z0es (:,:,jl) * r1_e12t(:,:)
             DO jk = 1, nlay_i
-               zs0e(:,:,jk,jl) = zs0e(:,:,jk,jl) / area(:,:)
-            END DO
+               e_i(:,:,jk,jl) = z0ei(:,:,jk,jl) * r1_e12t(:,:)
+            END DO
+         END DO
+
+         at_i(:,:) = a_i(:,:,1)      ! total ice fraction
+         DO jl = 2, jpl
+            at_i(:,:) = at_i(:,:) + a_i(:,:,jl)
          END DO
 
          !------------------------------------------------------------------------------!
-         ! 4) Diffusion of Ice fields                  
+         ! Diffusion of Ice fields                  
          !------------------------------------------------------------------------------!
 
+         !
          !--------------------------------
          !  diffusion of open water area
          !--------------------------------
-         zs0at(:,:) = zs0a(:,:,1)      ! total ice fraction
-         DO jl = 2, jpl
-            zs0at(:,:) = zs0at(:,:) + zs0a(:,:,jl)
-         END DO
-         !
          !                             ! Masked eddy diffusivity coefficient at ocean U- and V-points
          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, -at_i(ji  ,jj) ) ) )   &
+                  &        * ( 1._wp - MAX( 0._wp, SIGN( 1._wp, -at_i(ji+1,jj) ) ) ) * ahiu(ji,jj)
+               pahv(ji,jj) = ( 1._wp - MAX( 0._wp, SIGN( 1._wp, -at_i(ji,jj  ) ) ) )   &
+                  &        * ( 1._wp - MAX( 0._wp, SIGN( 1._wp,- at_i(ji,jj+1) ) ) ) * ahiv(ji,jj)
             END DO
          END DO
          !
-         CALL lim_hdf( zs0ow (:,:) )   ! Diffusion
+         CALL lim_hdf( ato_i (:,:) )
 
          !------------------------------------
@@ -305,270 +308,163 @@
             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)
-               END DO
-            END DO
-
-            CALL lim_hdf( zs0ice (:,:,jl) )
-            CALL lim_hdf( zs0sn  (:,:,jl) )
-            CALL lim_hdf( zs0sm  (:,:,jl) )
-            CALL lim_hdf( zs0oi  (:,:,jl) )
-            CALL lim_hdf( zs0a   (:,:,jl) )
-            CALL lim_hdf( zs0c0  (:,:,jl) )
+                  pahu(ji,jj) = ( 1._wp - MAX( 0._wp, SIGN( 1._wp, -a_i(ji  ,jj,jl) ) ) )   &
+                     &        * ( 1._wp - MAX( 0._wp, SIGN( 1._wp, -a_i(ji+1,jj,jl) ) ) ) * ahiu(ji,jj)
+                  pahv(ji,jj) = ( 1._wp - MAX( 0._wp, SIGN( 1._wp, -a_i(ji,jj  ,jl) ) ) )   &
+                     &        * ( 1._wp - MAX( 0._wp, SIGN( 1._wp,- a_i(ji,jj+1,jl) ) ) ) * ahiv(ji,jj)
+               END DO
+            END DO
+
+            CALL lim_hdf( v_i  (:,:,  jl) )
+            CALL lim_hdf( v_s  (:,:,  jl) )
+            CALL lim_hdf( smv_i(:,:,  jl) )
+            CALL lim_hdf( oa_i (:,:,  jl) )
+            CALL lim_hdf( a_i  (:,:,  jl) )
+            CALL lim_hdf( e_s  (:,:,1,jl) )
             DO jk = 1, nlay_i
-               CALL lim_hdf( zs0e (:,:,jk,jl) )
+               CALL lim_hdf( e_i(:,:,jk,jl) )
             END DO
          END DO
 
          !------------------------------------------------------------------------------!
-         ! 5) Update and limit ice properties after transport                           
+         ! limit ice properties after transport                           
          !------------------------------------------------------------------------------!
-
-         !--------------------------------------------------
-         ! 5.1) Recover mean values over the grid squares.
-         !--------------------------------------------------
-         zs0at(:,:) = 0._wp
+!!gm & cr   :  MAX should not be active if adv scheme is positive !
          DO jl = 1, jpl
             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) )
-                  zs0at (ji,jj)    = zs0at(ji,jj) + zs0a(ji,jj,jl)
-               END DO
-            END DO
-         END DO
-
-         !---------------------------------------------------------
-         ! 5.2) Snow thickness, Ice thickness, Ice concentrations
-         !---------------------------------------------------------
-         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 jj = 1, jpj
-               DO ji = 1, jpi
-                  zvi = zs0ice(ji,jj,jl)
-                  zvs = zs0sn(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 )
-
-                  ! 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)
-                  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
-
-         DO jl = 1, jpl
+                  v_s  (ji,jj,jl)   = MAX( 0._wp, v_s  (ji,jj,jl) )
+                  v_i  (ji,jj,jl)   = MAX( 0._wp, v_i  (ji,jj,jl) )
+                  smv_i(ji,jj,jl)   = MAX( 0._wp, smv_i(ji,jj,jl) )
+                  oa_i (ji,jj,jl)   = MAX( 0._wp, oa_i (ji,jj,jl) )
+                  a_i  (ji,jj,jl)   = MAX( 0._wp, a_i  (ji,jj,jl) )
+                  e_s  (ji,jj,1,jl) = MAX( 0._wp, e_s  (ji,jj,1,jl) )
+               END DO
+            END DO
+
             DO jk = 1, nlay_i
                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
-                  END DO !ji
-               END DO ! jj
-            END DO ! jk
-         END DO ! jl
-
-
-      ! --- agglomerate variables (clem) -----------------
-      vt_i (:,:) = 0._wp
-      vt_s (:,:) = 0._wp
-      at_i (:,:) = 0._wp
-      !
-      DO jl = 1, jpl
+                     e_i(ji,jj,jk,jl) = MAX( 0._wp, e_i(ji,jj,jk,jl) )
+                  END DO
+               END DO
+            END DO
+         END DO
+!!gm & cr 
+
+         ! --- 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) ) * r1_rdtice
+               diag_trp_es(ji,jj) = ( SUM( e_s(ji,jj,1:nlay_s,:) ) - zesold(ji,jj) ) * 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
+               diag_trp_smv(ji,jj) = SUM( smv_i(ji,jj,:) - zsmvold(ji,jj,:) ) * r1_rdtice
+            END DO
+         END DO
+
+         ! zap small areas
+         CALL lim_var_zapsmall
+
+         !--- Thickness correction in case too high --------------------------------------------------------
+         DO jl = 1, jpl
+            DO jj = 1, jpj
+               DO ji = 1, jpi
+
+                  IF ( v_i(ji,jj,jl) > 0._wp ) THEN
+
+                     rswitch          = MAX( 0._wp , SIGN( 1._wp, a_i(ji,jj,jl) - epsi20 ) )
+                     ht_i  (ji,jj,jl) = v_i (ji,jj,jl) / MAX( a_i(ji,jj,jl) , epsi20 ) * rswitch
+                     ht_s  (ji,jj,jl) = v_s (ji,jj,jl) / MAX( a_i(ji,jj,jl) , epsi20 ) * rswitch
+                     
+                     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) + v_s(ji,jj,jl) - zviold(ji,jj,jl) - zvsold(ji,jj,jl)  
+
+                     IF ( ( zdv >  0.0 .AND. (ht_i(ji,jj,jl)+ht_s(ji,jj,jl)) > zhimax(ji,jj,jl) .AND. zatold(ji,jj) < 0.80 ) .OR. &
+                        & ( zdv <= 0.0 .AND. (ht_i(ji,jj,jl)+ht_s(ji,jj,jl)) > zhimax(ji,jj,jl) ) ) THEN
+
+                        rswitch        = MAX( 0._wp, SIGN( 1._wp, zhimax(ji,jj,jl) - epsi20 ) )
+                        a_i(ji,jj,jl)  = rswitch * ( v_i(ji,jj,jl) + v_s(ji,jj,jl) ) / MAX( zhimax(ji,jj,jl), epsi20 )
+
+                        ! 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 ) * r1_rdtice ! W.m-2 <0
+                        hfx_res(ji,jj) = hfx_res(ji,jj) + ( SUM( e_i(ji,jj,1:nlay_i,jl) ) - zei ) * r1_rdtice ! W.m-2 <0
+
+                     ENDIF
+
+                  ENDIF
+
+               END DO
+            END DO
+         END DO
+         ! -------------------------------------------------
+         
+         !--------------------------------------
+         ! Impose a_i < amax in mono-category
+         !--------------------------------------
+         !
+         IF ( ( nn_monocat == 2 ) .AND. ( jpl == 1 ) ) THEN ! simple conservative piling, comparable with LIM2
+            DO jj = 1, jpj
+               DO ji = 1, jpi
+                  a_i(ji,jj,1)  = MIN( a_i(ji,jj,1), rn_amax )
+               END DO
+            END DO
+         ENDIF
+
+         ! --- 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)
+                  vt_s(ji,jj) = vt_s(ji,jj) + v_s(ji,jj,jl)
+                  at_i(ji,jj) = at_i(ji,jj) + a_i(ji,jj,jl)
+               END DO
+            END DO
+         END DO
+
+         ! --- open water = 1 if at_i=0 --------------------------------
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               rswitch      = MAX( 0._wp , SIGN( 1._wp, - at_i(ji,jj) ) )
+               ato_i(ji,jj) = rswitch + (1._wp - rswitch ) * ato_i(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
+
       ! -------------------------------------------------
-
-
-
-      ENDIF
-
-      IF(ln_ctl) THEN   ! Control print
-         CALL prt_ctl_info(' ')
-         CALL prt_ctl_info(' - Cell values : ')
-         CALL prt_ctl_info('   ~~~~~~~~~~~~~ ')
-         CALL prt_ctl(tab2d_1=area , clinfo1=' lim_trp  : cell area :')
-         CALL prt_ctl(tab2d_1=at_i , clinfo1=' lim_trp  : at_i      :')
-         CALL prt_ctl(tab2d_1=vt_i , clinfo1=' lim_trp  : vt_i      :')
-         CALL prt_ctl(tab2d_1=vt_s , clinfo1=' lim_trp  : vt_s      :')
-         DO jl = 1, jpl
-            CALL prt_ctl_info(' ')
-            CALL prt_ctl_info(' - Category : ', ivar1=jl)
-            CALL prt_ctl_info('   ~~~~~~~~~~')
-            CALL prt_ctl(tab2d_1=a_i   (:,:,jl)   , clinfo1= ' lim_trp  : a_i      : ')
-            CALL prt_ctl(tab2d_1=ht_i  (:,:,jl)   , clinfo1= ' lim_trp  : ht_i     : ')
-            CALL prt_ctl(tab2d_1=ht_s  (:,:,jl)   , clinfo1= ' lim_trp  : ht_s     : ')
-            CALL prt_ctl(tab2d_1=v_i   (:,:,jl)   , clinfo1= ' lim_trp  : v_i      : ')
-            CALL prt_ctl(tab2d_1=v_s   (:,:,jl)   , clinfo1= ' lim_trp  : v_s      : ')
-            CALL prt_ctl(tab2d_1=e_s   (:,:,1,jl) , clinfo1= ' lim_trp  : e_s      : ')
-            CALL prt_ctl(tab2d_1=t_su  (:,:,jl)   , clinfo1= ' lim_trp  : t_su     : ')
-            CALL prt_ctl(tab2d_1=t_s   (:,:,1,jl) , clinfo1= ' lim_trp  : t_snow   : ')
-            CALL prt_ctl(tab2d_1=sm_i  (:,:,jl)   , clinfo1= ' lim_trp  : sm_i     : ')
-            CALL prt_ctl(tab2d_1=smv_i (:,:,jl)   , clinfo1= ' lim_trp  : smv_i    : ')
-            DO jk = 1, nlay_i
-               CALL prt_ctl_info(' ')
-               CALL prt_ctl_info(' - Layer : ', ivar1=jk)
-               CALL prt_ctl_info('   ~~~~~~~')
-               CALL prt_ctl(tab2d_1=t_i(:,:,jk,jl) , clinfo1= ' lim_trp  : t_i      : ')
-               CALL prt_ctl(tab2d_1=e_i(:,:,jk,jl) , clinfo1= ' lim_trp  : 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 ) ) / 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)
-      ! -------------------------------
+      ! control prints
+      ! -------------------------------------------------
+      IF( ln_icectl )   CALL lim_prt( kt, iiceprt, jiceprt,-1, ' - ice dyn & trp - ' )
       !
-      CALL wrk_dealloc( jpi, jpj, zui_u, zvi_v, zsm, zs0at, zs0ow )
-      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,            zsm, zatold, zeiold, zesold )
+      CALL wrk_dealloc( jpi,jpj,jpl,        z0ice, z0snw, z0ai, z0es , z0smi , z0oi )
+      CALL wrk_dealloc( jpi,jpj,1,          z0opw )
+      CALL wrk_dealloc( jpi,jpj,nlay_i,jpl, z0ei )
+      CALL wrk_dealloc( jpi,jpj,jpl,        zviold, zvsold, zhimax, zsmvold )
       !
       IF( nn_timing == 1 )  CALL timing_stop('limtrp')
+
    END SUBROUTINE lim_trp
 
@@ -581,5 +477,4 @@
    END SUBROUTINE lim_trp
 #endif
-
    !!======================================================================
 END MODULE limtrp
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limupdate1.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limupdate1.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limupdate1.F90	(revision 5832)
@@ -5,4 +5,5 @@
    !!======================================================================
    !! History :  3.0  !  2006-04  (M. Vancoppenolle) Original code
+   !!            3.5  !  2014-06  (C. Rousset)       Complete rewriting/cleaning
    !!----------------------------------------------------------------------
 #if defined key_lim3
@@ -12,176 +13,63 @@
    !!    lim_update1   : computes update of sea-ice global variables from trend terms
    !!----------------------------------------------------------------------
-   USE limrhg          ! ice rheology
-
-   USE dom_oce
-   USE oce             ! dynamics and tracers variables
-   USE in_out_manager
    USE sbc_oce         ! Surface boundary condition: ocean fields
    USE sbc_ice         ! Surface boundary condition: ice fields
    USE dom_ice
+   USE dom_oce
    USE phycst          ! physical constants
    USE ice
-   USE limdyn
-   USE limtrp
-   USE limthd
-   USE limsbc
-   USE limdiahsb
-   USE limwri
-   USE limrst
    USE thd_ice         ! LIM thermodynamic sea-ice variables
-   USE par_ice
    USE limitd_th
    USE limvar
-   USE prtctl           ! Print control
-   USE lbclnk           ! lateral boundary condition - MPP exchanges
-   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 prtctl          ! Print control
+   USE wrk_nemo        ! work arrays
    USE timing          ! Timing
+   USE limcons         ! conservation tests
+   USE lib_mpp         ! MPP library
+   USE lib_fortran     ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
+   USE in_out_manager  ! I/O manager
 
    IMPLICIT NONE
    PRIVATE
 
-   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       !    -       -
-         
+   PUBLIC   lim_update1
+
    !! * Substitutions
 #  include "vectopt_loop_substitute.h90"
    !!----------------------------------------------------------------------
    !! NEMO/LIM3 4.0 , UCL - NEMO Consortium (2011)
-   !! $Id: limupdate.F90 3294 2012-01-28 16:44:18Z rblod $
+   !! $Id$
    !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
    !!----------------------------------------------------------------------
 CONTAINS
 
-   SUBROUTINE lim_update1
+   SUBROUTINE lim_update1( kt )
       !!-------------------------------------------------------------------
       !!               ***  ROUTINE lim_update1  ***
       !!               
       !! ** 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, INTENT(in) ::   kt    ! number of iteration
+      INTEGER  ::   ji, jj, jk, jl   ! dummy loop indices
+      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(:,:) )
+      IF( ln_limdyn ) THEN 
+
+      IF( kt == nit000 .AND. lwp ) THEN
+         WRITE(numout,*) ' lim_update1 ' 
+         WRITE(numout,*) ' ~~~~~~~~~~~ '
       ENDIF
-      !- check conservation (C Rousset)
-      ! -------------------------------
-
-      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
-     
+
+      ! conservation test
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(0, 'limupdate1', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
+
+      !----------------------------------------------------
+      ! ice concentration should not exceed amax 
+      !-----------------------------------------------------
       at_i(:,:) = 0._wp
       DO jl = 1, jpl
@@ -189,209 +77,74 @@
       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
-
-      at_i(:,:) = 0._wp
-      DO jl = 1, jpl
-         at_i(:,:) = a_i(:,:,jl) + at_i(:,:)
-      END DO
-
-      zbigvalue      = 1.0e+20
-
-      DO jl = 1, jpl
-         DO jj = 1, jpj 
+      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)
+               IF( at_i(ji,jj) > rn_amax .AND. a_i(ji,jj,jl) > 0._wp ) THEN
+                  a_i (ji,jj,jl) = a_i (ji,jj,jl) * ( 1._wp - ( 1._wp - rn_amax / at_i(ji,jj) ) )
+                  oa_i(ji,jj,jl) = oa_i(ji,jj,jl) * ( 1._wp - ( 1._wp - rn_amax / at_i(ji,jj) ) )
                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
+            END DO
+         END DO
+      END DO
+    
+      !---------------------
+      ! Ice salinity bounds
+      !---------------------
+      IF (  nn_icesal == 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) 
-      !-----------------------------------------------------
+                  zsal            = smv_i(ji,jj,jl)
+                  ! salinity stays in bounds
+                  rswitch         = 1._wp - MAX( 0._wp, SIGN( 1._wp, - v_i(ji,jj,jl) ) )
+                  smv_i(ji,jj,jl) = rswitch * MAX( MIN( rn_simax * v_i(ji,jj,jl), smv_i(ji,jj,jl) ), rn_simin * 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
+      ENDIF
+
+      !----------------------------------------------------
+      ! Rebin categories with thickness out of bounds
+      !----------------------------------------------------
+      IF ( jpl > 1 ) CALL lim_itd_th_reb(1, jpl)
+
+      !-----------------
+      ! zap small values
+      !-----------------
+      CALL lim_var_zapsmall
+
+      ! -------------------------------------------------
+      ! Diagnostics
+      ! -------------------------------------------------
+      DO jl  = 1, jpl
+         afx_dyn(:,:) = afx_dyn(:,:) + ( a_i(:,:,jl) - a_i_b(:,:,jl) ) * r1_rdtice
+      END DO
+
       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(:,:) = 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)
-      ! -------------------------------
-
+         DO ji = 1, jpi            
+            ! heat content variation (W.m-2)
+            diag_heat(ji,jj) = - ( SUM( e_i(ji,jj,1:nlay_i,:) - e_i_b(ji,jj,1:nlay_i,:) ) +  & 
+               &                   SUM( e_s(ji,jj,1:nlay_s,:) - e_s_b(ji,jj,1:nlay_s,:) )    &
+               &                 ) * r1_rdtice
+            ! salt, volume
+            diag_smvi(ji,jj) = SUM( smv_i(ji,jj,:) - smv_i_b(ji,jj,:) ) * rhoic * r1_rdtice
+            diag_vice(ji,jj) = SUM( v_i  (ji,jj,:) - v_i_b  (ji,jj,:) ) * rhoic * r1_rdtice
+            diag_vsnw(ji,jj) = SUM( v_s  (ji,jj,:) - v_s_b  (ji,jj,:) ) * rhosn * r1_rdtice
+         END DO
+      END DO
+
+      ! conservation test
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(1, 'limupdate1', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
+
+      ! -------------------------------------------------
+      ! control prints
+      ! -------------------------------------------------
       IF(ln_ctl) THEN   ! Control print
          CALL prt_ctl_info(' ')
          CALL prt_ctl_info(' - Cell values : ')
          CALL prt_ctl_info('   ~~~~~~~~~~~~~ ')
-         CALL prt_ctl(tab2d_1=area       , clinfo1=' lim_update1  : cell area   :')
+         CALL prt_ctl(tab2d_1=e12t       , clinfo1=' lim_update1  : cell area   :')
          CALL prt_ctl(tab2d_1=at_i       , clinfo1=' lim_update1  : at_i        :')
          CALL prt_ctl(tab2d_1=vt_i       , clinfo1=' lim_update1  : vt_i        :')
@@ -399,6 +152,5 @@
          CALL prt_ctl(tab2d_1=strength   , clinfo1=' lim_update1  : strength    :')
          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,27 +165,19 @@
             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=d_a_i_trp  (:,:,jl)        , clinfo1= ' lim_update1  : d_a_i_trp   : ')
+            CALL prt_ctl(tab2d_1=a_i_b      (:,:,jl)        , clinfo1= ' lim_update1  : a_i_b       : ')
             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=d_v_i_trp  (:,:,jl)        , clinfo1= ' lim_update1  : d_v_i_trp   : ')
+            CALL prt_ctl(tab2d_1=v_i_b      (:,:,jl)        , clinfo1= ' lim_update1  : v_i_b       : ')
             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=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=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=d_e_i_trp  (:,:,2,jl)/1.0e9, clinfo1= ' lim_update1  : de_i2_trp   : ')
+            CALL prt_ctl(tab2d_1=v_s_b      (:,:,jl)        , clinfo1= ' lim_update1  : v_s_b       : ')
+            CALL prt_ctl(tab2d_1=e_i        (:,:,1,jl)      , clinfo1= ' lim_update1  : e_i1        : ')
+            CALL prt_ctl(tab2d_1=e_i_b      (:,:,1,jl)      , clinfo1= ' lim_update1  : e_i1_b      : ')
+            CALL prt_ctl(tab2d_1=e_i        (:,:,2,jl)      , clinfo1= ' lim_update1  : e_i2        : ')
+            CALL prt_ctl(tab2d_1=e_i_b      (:,:,2,jl)      , clinfo1= ' lim_update1  : e_i2_b      : ')
             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=d_e_s_trp  (:,:,1,jl)/1.0e9, clinfo1= ' lim_update1  : d_e_s_trp   : ')
+            CALL prt_ctl(tab2d_1=e_s_b      (:,:,1,jl)      , clinfo1= ' lim_update1  : e_snow_b    : ')
             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=d_smv_i_trp(:,:,jl)        , clinfo1= ' lim_update1  : d_smv_i_trp : ')
+            CALL prt_ctl(tab2d_1=smv_i_b    (:,:,jl)        , clinfo1= ' lim_update1  : smv_i_b     : ')
             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=d_oa_i_trp (:,:,jl)        , clinfo1= ' lim_update1  : d_oa_i_trp  : ')
+            CALL prt_ctl(tab2d_1=oa_i_b     (:,:,jl)        , clinfo1= ' lim_update1  : oa_i_b      : ')
 
             DO jk = 1, nlay_i
@@ -446,7 +190,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 +200,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_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limupdate2.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limupdate2.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limupdate2.F90	(revision 5832)
@@ -5,4 +5,5 @@
    !!======================================================================
    !! History :  3.0  !  2006-04  (M. Vancoppenolle) Original code
+   !!            3.5  !  2014-06  (C. Rousset)       Complete rewriting/cleaning
    !!----------------------------------------------------------------------
 #if defined key_lim3
@@ -12,31 +13,22 @@
    !!    lim_update2   : computes update of sea-ice global variables from trend terms
    !!----------------------------------------------------------------------
-   USE limrhg          ! ice rheology
-
-   USE dom_oce
-   USE oce             ! dynamics and tracers variables
-   USE in_out_manager
    USE sbc_oce         ! Surface boundary condition: ocean fields
    USE sbc_ice         ! Surface boundary condition: ice fields
    USE dom_ice
+   USE dom_oce
    USE phycst          ! physical constants
    USE ice
-   USE limdyn
-   USE limtrp
-   USE limthd
-   USE limsbc
-   USE limdiahsb
-   USE limwri
-   USE limrst
    USE thd_ice         ! LIM thermodynamic sea-ice variables
-   USE par_ice
    USE limitd_th
-   USE limitd_me
    USE limvar
-   USE prtctl           ! Print control
-   USE lbclnk           ! lateral boundary condition - MPP exchanges
-   USE wrk_nemo         ! work arrays
-   USE lib_fortran     ! glob_sum
+   USE prtctl          ! Print control
+   USE lbclnk          ! lateral boundary condition - MPP exchanges
+   USE wrk_nemo        ! work arrays
    USE timing          ! Timing
+   USE limcons         ! conservation tests
+   USE limctl
+   USE lib_mpp         ! MPP library
+   USE lib_fortran     ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
+   USE in_out_manager
 
    IMPLICIT NONE
@@ -45,18 +37,14 @@
    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"
    !!----------------------------------------------------------------------
    !! NEMO/LIM3 4.0 , UCL - NEMO Consortium (2011)
-   !! $Id: limupdate.F90 3294 2012-01-28 16:44:18Z rblod $
+   !! $Id$
    !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
    !!----------------------------------------------------------------------
 CONTAINS
 
-   SUBROUTINE lim_update2
+   SUBROUTINE lim_update2( kt )
       !!-------------------------------------------------------------------
       !!               ***  ROUTINE lim_update2  ***
@@ -64,120 +52,38 @@
       !! ** 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, INTENT(in) ::   kt    ! number of iteration
+      INTEGER  ::   ji, jj, jk, jl   ! dummy loop indices
+      REAL(wp) ::   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(:,:) )
+      IF( kt == nit000 .AND. lwp ) THEN
+         WRITE(numout,*) ' lim_update2 '
+         WRITE(numout,*) ' ~~~~~~~~~~~ '
       ENDIF
-      !- check conservation (C Rousset)
-      ! -------------------------------
-
-      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
-     
+
+      ! conservation test
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(0, 'limupdate2', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
+
+      !----------------------------------------------------------------------
+      ! Constrain the thickness of the smallest category above himin
+      !----------------------------------------------------------------------
+      DO jj = 1, jpj 
+         DO ji = 1, jpi
+            rswitch = MAX( 0._wp , SIGN( 1._wp, a_i(ji,jj,1) - epsi20 ) )   !0 if no ice and 1 if yes
+            ht_i(ji,jj,1) = v_i (ji,jj,1) / MAX( a_i(ji,jj,1) , epsi20 ) * rswitch
+            IF( v_i(ji,jj,1) > 0._wp .AND. ht_i(ji,jj,1) < rn_himin ) THEN
+               a_i (ji,jj,1) = a_i (ji,jj,1) * ht_i(ji,jj,1) / rn_himin
+               oa_i(ji,jj,1) = oa_i(ji,jj,1) * ht_i(ji,jj,1) / rn_himin
+            ENDIF
+         END DO
+      END DO
+      
+      !-----------------------------------------------------
+      ! ice concentration should not exceed amax 
+      !-----------------------------------------------------
       at_i(:,:) = 0._wp
       DO jl = 1, jpl
@@ -185,300 +91,45 @@
       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 jl  = 1, jpl
+         DO jj = 1, jpj
+            DO ji = 1, jpi
+               IF( at_i(ji,jj) > rn_amax .AND. a_i(ji,jj,jl) > 0._wp ) THEN
+                  a_i (ji,jj,jl) = a_i (ji,jj,jl) * ( 1._wp - ( 1._wp - rn_amax / at_i(ji,jj) ) )
+                  oa_i(ji,jj,jl) = oa_i(ji,jj,jl) * ( 1._wp - ( 1._wp - rn_amax / at_i(ji,jj) ) )
+               ENDIF
+            END DO
+         END DO
+      END DO
+
+      !---------------------
+      ! Ice salinity
+      !---------------------
+      IF (  nn_icesal == 2  ) THEN 
+         DO jl = 1, jpl
             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 ji = 1, jpi
-               IF( internal_melt(ji,jj,jl) == 1 ) THEN
-                  ! initial ice thickness
-                  !-----------------------
-                  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
-
+                  zsal            = smv_i(ji,jj,jl)
+                  ! salinity stays in bounds
+                  rswitch         = 1._wp - MAX( 0._wp, SIGN( 1._wp, - v_i(ji,jj,jl) ) )
+                  smv_i(ji,jj,jl) = rswitch * MAX( MIN( rn_simax * v_i(ji,jj,jl), smv_i(ji,jj,jl) ), rn_simin * 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
-
-      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
-      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
-            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
-
-      !---------------------
-      ! 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
+      !----------------------------------------------------
+      ! Rebin categories with thickness out of bounds
+      !----------------------------------------------------
+      IF ( jpl > 1 ) CALL lim_itd_th_reb( 1, jpl )
+
+      !-----------------
+      ! zap small values
+      !-----------------
+      CALL lim_var_zapsmall
 
       !------------------------------------------------------------------------------
-      ! 2) Corrections to avoid wrong values                                        |
+      ! Corrections to avoid wrong values                                        |
       !------------------------------------------------------------------------------
       ! Ice drift
@@ -486,9 +137,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
@@ -498,45 +149,41 @@
       CALL lbc_lnk( v_ice(:,:), 'V', -1. )
       !mask velocities
-      u_ice(:,:) = u_ice(:,:) * tmu(:,:)
-      v_ice(:,:) = v_ice(:,:) * tmv(:,:)
+      u_ice(:,:) = u_ice(:,:) * umask(:,:,1)
+      v_ice(:,:) = v_ice(:,:) * vmask(:,:,1)
  
-      !--------------------------------
-      ! 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
+      ! -------------------------------------------------
+      DO jl  = 1, jpl
+         oa_i(:,:,jl) = oa_i(:,:,jl) + a_i(:,:,jl) * rdt_ice / rday   ! ice natural aging
+         afx_thd(:,:) = afx_thd(:,:) + ( a_i(:,:,jl) - a_i_b(:,:,jl) ) * r1_rdtice
+      END DO
+      afx_tot = afx_thd + afx_dyn
+
+      DO jj = 1, jpj
+         DO ji = 1, jpi            
+            ! heat content variation (W.m-2)
+            diag_heat(ji,jj) = diag_heat(ji,jj) -  &
+               &               ( SUM( e_i(ji,jj,1:nlay_i,:) - e_i_b(ji,jj,1:nlay_i,:) ) +  & 
+               &                 SUM( e_s(ji,jj,1:nlay_s,:) - e_s_b(ji,jj,1:nlay_s,:) )    &
+               &               ) * r1_rdtice   
+            ! salt, volume
+            diag_smvi(ji,jj) = diag_smvi(ji,jj) + SUM( smv_i(ji,jj,:) - smv_i_b(ji,jj,:) ) * rhoic * r1_rdtice
+            diag_vice(ji,jj) = diag_vice(ji,jj) + SUM( v_i  (ji,jj,:) - v_i_b  (ji,jj,:) ) * rhoic * r1_rdtice
+            diag_vsnw(ji,jj) = diag_vsnw(ji,jj) + SUM( v_s  (ji,jj,:) - v_s_b  (ji,jj,:) ) * rhosn * r1_rdtice
+         END DO
+      END DO
+
+      ! conservation test
+      IF( ln_limdiahsb ) CALL lim_cons_hsm(1, 'limupdate2', zvi_b, zsmv_b, zei_b, zfw_b, zfs_b, zft_b)
+
+      ! necessary calls (at least for coupling)
+      CALL lim_var_glo2eqv
+      CALL lim_var_agg(2)
+
+      ! -------------------------------------------------
+      ! control prints
+      ! -------------------------------------------------
+      IF( ln_icectl )   CALL lim_prt( kt, iiceprt, jiceprt, 2, ' - Final state - ' )   ! control print
 
       IF(ln_ctl) THEN   ! Control print
@@ -544,5 +191,5 @@
          CALL prt_ctl_info(' - Cell values : ')
          CALL prt_ctl_info('   ~~~~~~~~~~~~~ ')
-         CALL prt_ctl(tab2d_1=area       , clinfo1=' lim_update2  : cell area   :')
+         CALL prt_ctl(tab2d_1=e12t       , clinfo1=' lim_update2  : cell area   :')
          CALL prt_ctl(tab2d_1=at_i       , clinfo1=' lim_update2  : at_i        :')
          CALL prt_ctl(tab2d_1=vt_i       , clinfo1=' lim_update2  : vt_i        :')
@@ -550,5 +197,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,27 +210,19 @@
             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=d_a_i_thd  (:,:,jl)        , clinfo1= ' lim_update2  : d_a_i_thd   : ')
+            CALL prt_ctl(tab2d_1=a_i_b      (:,:,jl)        , clinfo1= ' lim_update2  : a_i_b       : ')
             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=d_v_i_thd  (:,:,jl)        , clinfo1= ' lim_update2  : d_v_i_thd   : ')
+            CALL prt_ctl(tab2d_1=v_i_b      (:,:,jl)        , clinfo1= ' lim_update2  : v_i_b       : ')
             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=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=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=d_e_i_thd  (:,:,2,jl)/1.0e9, clinfo1= ' lim_update2  : de_i2_thd   : ')
+            CALL prt_ctl(tab2d_1=v_s_b      (:,:,jl)        , clinfo1= ' lim_update2  : v_s_b       : ')
+            CALL prt_ctl(tab2d_1=e_i        (:,:,1,jl)      , clinfo1= ' lim_update2  : e_i1        : ')
+            CALL prt_ctl(tab2d_1=e_i_b      (:,:,1,jl)      , clinfo1= ' lim_update2  : e_i1_b      : ')
+            CALL prt_ctl(tab2d_1=e_i        (:,:,2,jl)      , clinfo1= ' lim_update2  : e_i2        : ')
+            CALL prt_ctl(tab2d_1=e_i_b      (:,:,2,jl)      , clinfo1= ' lim_update2  : e_i2_b      : ')
             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=d_e_s_thd  (:,:,1,jl)/1.0e9, clinfo1= ' lim_update2  : d_e_s_thd   : ')
+            CALL prt_ctl(tab2d_1=e_s_b      (:,:,1,jl)      , clinfo1= ' lim_update2  : e_snow_b    : ')
             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=d_smv_i_thd(:,:,jl)        , clinfo1= ' lim_update2  : d_smv_i_thd : ')
+            CALL prt_ctl(tab2d_1=smv_i_b    (:,:,jl)        , clinfo1= ' lim_update2  : smv_i_b     : ')
             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=d_oa_i_thd (:,:,jl)        , clinfo1= ' lim_update2  : d_oa_i_thd  : ')
+            CALL prt_ctl(tab2d_1=oa_i_b     (:,:,jl)        , clinfo1= ' lim_update2  : oa_i_b      : ')
 
             DO jk = 1, nlay_i
@@ -596,7 +235,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 +245,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_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limvar.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limvar.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limvar.F90	(revision 5832)
@@ -30,5 +30,5 @@
    !!======================================================================
    !! History :   -   ! 2006-01 (M. Vancoppenolle) Original code
-   !!            4.0  ! 2011-02 (G. Madec) dynamical allocation
+   !!            3.4  ! 2011-02 (G. Madec) dynamical allocation
    !!----------------------------------------------------------------------
 #if defined key_lim3
@@ -36,16 +36,8 @@
    !!   'key_lim3'                                      LIM3 sea-ice model
    !!----------------------------------------------------------------------
-   !!   lim_var_agg       : 
-   !!   lim_var_glo2eqv   :
-   !!   lim_var_eqv2glo   :
-   !!   lim_var_salprof   : 
-   !!   lim_var_salprof1d :
-   !!   lim_var_bv        :
-   !!----------------------------------------------------------------------
    USE par_oce        ! ocean parameters
    USE phycst         ! physical constants (ocean directory) 
    USE sbc_oce        ! Surface boundary condition: ocean fields
    USE ice            ! ice variables
-   USE par_ice        ! ice parameters
    USE thd_ice        ! ice variables (thermodynamics)
    USE dom_ice        ! ice domain
@@ -58,18 +50,16 @@
    PRIVATE
 
-   PUBLIC   lim_var_agg          !
-   PUBLIC   lim_var_glo2eqv      !
-   PUBLIC   lim_var_eqv2glo      !
-   PUBLIC   lim_var_salprof      !
-   PUBLIC   lim_var_icetm        !
-   PUBLIC   lim_var_bv           !
-   PUBLIC   lim_var_salprof1d    !
-
-   REAL(wp) ::   epsi10 = 1.e-10_wp   !    -       -
-   REAL(wp) ::   zzero = 0.e0        !    -       -
-   REAL(wp) ::   zone  = 1.e0        !    -       -
+   PUBLIC   lim_var_agg          
+   PUBLIC   lim_var_glo2eqv      
+   PUBLIC   lim_var_eqv2glo      
+   PUBLIC   lim_var_salprof      
+   PUBLIC   lim_var_icetm        
+   PUBLIC   lim_var_bv           
+   PUBLIC   lim_var_salprof1d    
+   PUBLIC   lim_var_zapsmall
+   PUBLIC   lim_var_itd
 
    !!----------------------------------------------------------------------
-   !! NEMO/LIM3 4.0 , UCL - NEMO Consortium (2011)
+   !! NEMO/LIM3 3.5 , UCL - NEMO Consortium (2011)
    !! $Id$
    !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
@@ -94,5 +84,4 @@
       !
       INTEGER  ::   ji, jj, jk, jl   ! dummy loop indices
-      REAL(wp) ::   zinda, zindb
       !!------------------------------------------------------------------
 
@@ -113,6 +102,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 +123,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
+                  et_s(ji,jj)  = et_s(ji,jj)  + e_s(ji,jj,1,jl)                                           ! snow heat content
+                  rswitch = MAX( 0._wp , SIGN( 1._wp , vt_i(ji,jj) - epsi20 ) ) 
+                  smt_i(ji,jj) = smt_i(ji,jj) + smv_i(ji,jj,jl) / MAX( vt_i(ji,jj) , epsi20 ) * rswitch   ! ice salinity
+                  rswitch = MAX( 0._wp , SIGN( 1._wp , at_i(ji,jj) - epsi20 ) ) 
+                  ot_i(ji,jj)  = ot_i(ji,jj)  + oa_i(ji,jj,jl)  / MAX( at_i(ji,jj) , epsi20 ) * rswitch   ! ice age
                END DO
             END DO
@@ -163,5 +152,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,18 +161,20 @@
          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
-            END DO
-         END DO
-      END DO
-
-      IF(  num_sal == 2  )THEN
+               rswitch = MAX( 0._wp , SIGN( 1._wp, a_i(ji,jj,jl) - epsi20 ) )   !0 if no ice and 1 if yes
+               ht_i(ji,jj,jl) = v_i (ji,jj,jl) / MAX( a_i(ji,jj,jl) , epsi20 ) * rswitch
+               ht_s(ji,jj,jl) = v_s (ji,jj,jl) / MAX( a_i(ji,jj,jl) , epsi20 ) * rswitch
+               o_i(ji,jj,jl)  = oa_i(ji,jj,jl) / MAX( a_i(ji,jj,jl) , epsi20 ) * rswitch
+            END DO
+         END DO
+      END DO
+
+      IF(  nn_icesal == 2  )THEN
          DO jl = 1, jpl
             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 = MAX( 0._wp , SIGN( 1._wp, v_i(ji,jj,jl) - epsi20 ) )   !0 if no ice and 1 if yes
+                  sm_i(ji,jj,jl) = smv_i(ji,jj,jl) / MAX( v_i(ji,jj,jl) , epsi20 ) * rswitch
+                  !                                      ! bounding salinity
+                  sm_i(ji,jj,jl) = MAX( sm_i(ji,jj,jl), rn_simin )
                END DO
             END DO
@@ -196,24 +187,19 @@
       ! Ice temperatures
       !-------------------
-!CDIR NOVERRCHK
-      DO jl = 1, jpl
-!CDIR NOVERRCHK
+      DO jl = 1, jpl
          DO jk = 1, nlay_i
-!CDIR NOVERRCHK
             DO jj = 1, jpj
-!CDIR NOVERRCHK
                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
-                  ztmelts = -tmut * s_i(ji,jj,jk,jl) + rtt                       ! Ice layer melt temperature
+                  rswitch = MAX( 0.0 , SIGN( 1.0 , v_i(ji,jj,jl) - epsi20 ) )     ! rswitch = 0 if no ice and 1 if yes
+                  zq_i    = rswitch * e_i(ji,jj,jk,jl) / MAX( v_i(ji,jj,jl) , epsi20 ) * REAL(nlay_i,wp) 
+                  ztmelts = -tmut * s_i(ji,jj,jk,jl) + rt0              ! Ice layer melt temperature
                   !
                   zaaa       =  cpic                  ! Conversion q(S,T) -> T (second order equation)
-                  zbbb       =  ( rcp - cpic ) * ( ztmelts - rtt ) + zq_i / rhoic - lfus
-                  zccc       =  lfus * (ztmelts-rtt)
+                  zbbb       =  ( rcp - cpic ) * ( ztmelts - rt0 ) + zq_i * r1_rhoic - lfus
+                  zccc       =  lfus * (ztmelts-rt0)
                   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) = MIN( rtt, MAX( 173.15_wp, t_i(ji,jj,jk,jl) ) )       ! 100-rtt < t_i < rtt
+                  t_i(ji,jj,jk,jl) = rt0 + rswitch *( - zbbb - zdiscrim ) / ( 2.0 *zaaa )
+                  t_i(ji,jj,jk,jl) = MIN( ztmelts, MAX( rt0 - 100._wp, t_i(ji,jj,jk,jl) ) )  ! -100 < t_i < ztmelts
                END DO
             END DO
@@ -231,10 +217,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 = MAX( 0._wp , SIGN( 1._wp , v_s(ji,jj,jl) - epsi20 ) )     ! rswitch = 0 if no ice and 1 if yes
+                  zq_s  = rswitch * e_s(ji,jj,jk,jl) / MAX( v_s(ji,jj,jl) , epsi20 ) * REAL(nlay_s,wp)
                   !
-                  t_s(ji,jj,jk,jl) = rtt + zindb * ( - 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
+                  t_s(ji,jj,jk,jl) = rt0 + rswitch * ( - zfac1 * zq_s + zfac2 )
+                  t_s(ji,jj,jk,jl) = MIN( rt0, MAX( rt0 - 100._wp , t_s(ji,jj,jk,jl) ) )     ! -100 < t_s < rt0
                END DO
             END DO
@@ -245,4 +230,9 @@
       ! Mean temperature
       !-------------------
+      vt_i (:,:) = 0._wp
+      DO jl = 1, jpl
+         vt_i(:,:) = vt_i(:,:) + v_i(:,:,jl)
+      END DO
+
       tm_i(:,:) = 0._wp
       DO jl = 1, jpl
@@ -250,11 +240,12 @@
             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)   &
-                     &                      / (  REAL(nlay_i,wp) * MAX( vt_i(ji,jj) , epsi10 )  )
-               END DO
-            END DO
-         END DO
-      END DO
+                  rswitch = MAX( 0._wp , SIGN( 1._wp , vt_i(ji,jj) - epsi10 ) )
+                  tm_i(ji,jj) = tm_i(ji,jj) + r1_nlay_i * rswitch * ( t_i(ji,jj,jk,jl) - rt0 ) * v_i(ji,jj,jl)  &
+                     &            / MAX( vt_i(ji,jj) , epsi10 )
+               END DO
+            END DO
+         END DO
+      END DO
+      tm_i = tm_i + rt0
       !
    END SUBROUTINE lim_var_glo2eqv
@@ -275,5 +266,4 @@
       v_s(:,:,:)   = ht_s(:,:,:) * a_i(:,:,:)
       smv_i(:,:,:) = sm_i(:,:,:) * v_i(:,:,:)
-      oa_i (:,:,:) = o_i (:,:,:) * a_i(:,:,:)
       !
    END SUBROUTINE lim_var_eqv2glo
@@ -286,17 +276,19 @@
       !! ** Purpose :   computes salinity profile in function of bulk salinity     
       !!
-      !! ** Method  : If bulk salinity greater than s_i_1, 
+      !! ** Method  : If bulk salinity greater than zsi1, 
       !!              the profile is assumed to be constant (S_inf)
-      !!              If bulk salinity lower than s_i_0,
+      !!              If bulk salinity lower than zsi0,
       !!              the profile is linear with 0 at the surface (S_zero)
-      !!              If it is between s_i_0 and s_i_1, it is a
+      !!              If it is between zsi0 and zsi1, it is a
       !!              alpha-weighted linear combination of s_inf and s_zero
       !!
-      !! ** References : Vancoppenolle et al., 2007 (in preparation)
+      !! ** References : Vancoppenolle et al., 2007
       !!------------------------------------------------------------------
       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), POINTER, DIMENSION(:,:,:) ::   z_slope_s, zalpha   ! 3D pointer
+      REAL(wp) ::   zfac0, zfac1, zsal
+      REAL(wp) ::   zswi0, zswi01, zargtemp , zs_zero   
+      REAL(wp), POINTER, DIMENSION(:,:,:) ::   z_slope_s, zalpha
+      REAL(wp), PARAMETER :: zsi0 = 3.5_wp
+      REAL(wp), PARAMETER :: zsi1 = 4.5_wp
       !!------------------------------------------------------------------
 
@@ -306,10 +298,10 @@
       ! Vertically constant, constant in time
       !---------------------------------------
-      IF(  num_sal == 1  )   s_i(:,:,:,:) = bulk_sal
+      IF(  nn_icesal == 1  )   s_i(:,:,:,:) = rn_icesal
 
       !-----------------------------------
       ! Salinity profile, varying in time
       !-----------------------------------
-      IF(  num_sal == 2  ) THEN
+      IF(  nn_icesal == 2  ) THEN
          !
          DO jk = 1, nlay_i
@@ -320,11 +312,12 @@
             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) )
-               END DO
-            END DO
-         END DO
-         !
-         dummy_fac0 = 1._wp / ( s_i_0 - s_i_1 )       ! Weighting factor between zs_zero and zs_inf
-         dummy_fac1 = s_i_1 / ( s_i_1 - s_i_0 )
+                  rswitch = MAX( 0._wp , SIGN( 1._wp , ht_i(ji,jj,jl) - epsi20 ) )
+                  z_slope_s(ji,jj,jl) = rswitch * 2._wp * sm_i(ji,jj,jl) / MAX( epsi20 , ht_i(ji,jj,jl) )
+               END DO
+            END DO
+         END DO
+         !
+         zfac0 = 1._wp / ( zsi0 - zsi1 )       ! Weighting factor between zs_zero and zs_inf
+         zfac1 = zsi1  / ( zsi1 - zsi0 )
          !
          zalpha(:,:,:) = 0._wp
@@ -332,18 +325,18 @@
             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 zsi0 and 0 otherwise
+                  zswi0  = MAX( 0._wp   , SIGN( 1._wp  , zsi0 - sm_i(ji,jj,jl) ) ) 
+                  ! zswi01 = 1 if sm_i is between zsi0 and zsi1 and 0 othws 
+                  zswi01 = ( 1._wp - zswi0 ) * MAX( 0._wp   , SIGN( 1._wp  , zsi1 - sm_i(ji,jj,jl) ) ) 
+                  ! If 2.sm_i GE sss_m then rswitch = 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 )
-               END DO
-            END DO
-         END DO
-
-         dummy_fac = 1._wp / REAL( nlay_i )                   ! Computation of the profile
+                  rswitch = 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) * zfac0 + zfac1 )
+                  zalpha(ji,jj,jl) = zalpha(ji,jj,jl) * ( 1._wp - rswitch )
+               END DO
+            END DO
+         END DO
+
+         ! Computation of the profile
          DO jl = 1, jpl
             DO jk = 1, nlay_i
@@ -351,13 +344,15 @@
                   DO ji = 1, jpi
                      !                                      ! linear profile with 0 at the surface
-                     zs_zero = z_slope_s(ji,jj,jl) * ( REAL(jk,wp) - 0.5_wp ) * ht_i(ji,jj,jl) * dummy_fac
+                     zs_zero = z_slope_s(ji,jj,jl) * ( REAL(jk,wp) - 0.5_wp ) * ht_i(ji,jj,jl) * r1_nlay_i
                      !                                      ! weighting the profile
                      s_i(ji,jj,jk,jl) = zalpha(ji,jj,jl) * zs_zero + ( 1._wp - zalpha(ji,jj,jl) ) * sm_i(ji,jj,jl)
-                  END DO ! ji
-               END DO ! jj
-            END DO ! jk
-         END DO ! jl
-         !
-      ENDIF ! num_sal
+                     !                                      ! bounding salinity
+                     s_i(ji,jj,jk,jl) = MIN( rn_simax, MAX( s_i(ji,jj,jk,jl), rn_simin ) )
+                  END DO
+               END DO
+            END DO
+         END DO
+         !
+      ENDIF ! nn_icesal
 
       !-------------------------------------------------------
@@ -365,12 +360,11 @@
       !-------------------------------------------------------
 
-      IF(  num_sal == 3  ) THEN      ! Schwarzacher (1959) multiyear salinity profile (mean = 2.30)
+      IF(  nn_icesal == 3  ) THEN      ! Schwarzacher (1959) multiyear salinity profile (mean = 2.30)
          !
          sm_i(:,:,:) = 2.30_wp
          !
          DO jl = 1, jpl
-!CDIR NOVERRCHK
             DO jk = 1, nlay_i
-               zargtemp  = ( REAL(jk,wp) - 0.5_wp ) / REAL(nlay_i,wp)
+               zargtemp  = ( REAL(jk,wp) - 0.5_wp ) * r1_nlay_i
                zsal =  1.6_wp * (  1._wp - COS( rpi * zargtemp**(0.407_wp/(0.573_wp+zargtemp)) )  )
                s_i(:,:,jk,jl) =  zsal
@@ -378,5 +372,5 @@
          END DO
          !
-      ENDIF ! num_sal
+      ENDIF ! nn_icesal
       !
       CALL wrk_dealloc( jpi, jpj, jpl, z_slope_s, zalpha )
@@ -392,8 +386,12 @@
       !!------------------------------------------------------------------
       INTEGER  ::   ji, jj, jk, jl   ! dummy loop indices
-      REAL(wp) ::   zindb   !   -      -
       !!------------------------------------------------------------------
 
       ! Mean sea ice temperature
+      vt_i (:,:) = 0._wp
+      DO jl = 1, jpl
+         vt_i(:,:) = vt_i(:,:) + v_i(:,:,jl)
+      END DO
+
       tm_i(:,:) = 0._wp
       DO jl = 1, jpl
@@ -401,11 +399,12 @@
             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)   &
-                     &                      / (  REAL(nlay_i,wp) * MAX( vt_i(ji,jj) , epsi10 )  )
-               END DO
-            END DO
-         END DO
-      END DO
+                  rswitch = MAX( 0._wp , SIGN( 1._wp , vt_i(ji,jj) - epsi10 ) )
+                  tm_i(ji,jj) = tm_i(ji,jj) + r1_nlay_i * rswitch * ( t_i(ji,jj,jk,jl) - rt0 ) * v_i(ji,jj,jl)  &
+                     &            / MAX( vt_i(ji,jj) , epsi10 )
+               END DO
+            END DO
+         END DO
+      END DO
+      tm_i = tm_i + rt0
 
    END SUBROUTINE lim_var_icetm
@@ -423,7 +422,12 @@
       !!------------------------------------------------------------------
       INTEGER  ::   ji, jj, jk, jl   ! dummy loop indices
-      REAL(wp) ::   zbvi, zinda, zindb      ! local scalars
-      !!------------------------------------------------------------------
-      !
+      REAL(wp) ::   zbvi             ! local scalars
+      !!------------------------------------------------------------------
+      !
+      vt_i (:,:) = 0._wp
+      DO jl = 1, jpl
+         vt_i(:,:) = vt_i(:,:) + v_i(:,:,jl)
+      END DO
+
       bv_i(:,:) = 0._wp
       DO jl = 1, jpl
@@ -431,9 +435,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 )   &
-                     &                   * 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 , (t_i(ji,jj,jk,jl) - rt0) + epsi10 ) )  )
+                  zbvi  = - rswitch * tmut * s_i(ji,jj,jk,jl) / MIN( t_i(ji,jj,jk,jl) - rt0, - epsi10 )   &
+                     &                   * v_i(ji,jj,jl) * r1_nlay_i
+                  rswitch = (  1._wp - MAX( 0._wp , SIGN( 1._wp , - vt_i(ji,jj) + epsi20 ) )  )
+                  bv_i(ji,jj) = bv_i(ji,jj) + rswitch * zbvi  / MAX( vt_i(ji,jj) , epsi20 )
                END DO
             END DO
@@ -454,9 +458,11 @@
       !
       INTEGER  ::   ji, jk    ! dummy loop indices
-      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              !   -      -
+      INTEGER  ::   ii, ij    ! local integers
+      REAL(wp) ::   zfac0, zfac1, zargtemp, zsal   ! local scalars
+      REAL(wp) ::   zalpha, zswi0, zswi01, zs_zero              !   -      -
       !
       REAL(wp), POINTER, DIMENSION(:) ::   z_slope_s
+      REAL(wp), PARAMETER :: zsi0 = 3.5_wp
+      REAL(wp), PARAMETER :: zsi1 = 4.5_wp
       !!---------------------------------------------------------------------
 
@@ -466,5 +472,5 @@
       ! Vertically constant, constant in time
       !---------------------------------------
-      IF( num_sal == 1 )   s_i_b(:,:) = bulk_sal
+      IF( nn_icesal == 1 )   s_i_1d(:,:) = rn_icesal
 
       !------------------------------------------------------
@@ -472,39 +478,38 @@
       !------------------------------------------------------
 
-      IF(  num_sal == 2  ) THEN
+      IF(  nn_icesal == 2  ) THEN
          !
          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) )
+            rswitch = MAX( 0._wp , SIGN( 1._wp , ht_i_1d(ji) - epsi20 ) )
+            z_slope_s(ji) = rswitch * 2._wp * sm_i_1d(ji) / MAX( epsi20 , ht_i_1d(ji) )
          END DO
 
          ! Weighting factor between zs_zero and zs_inf
          !---------------------------------------------
-         dummy_fac0 = 1._wp / ( s_i_0 - s_i_1 )
-         dummy_fac1 = s_i_1 / ( s_i_1 - s_i_0 )
-         dummy_fac2 = 1._wp / REAL(nlay_i,wp)
-
-!CDIR NOVERRCHK
+         zfac0 = 1._wp / ( zsi0 - zsi1 )
+         zfac1 = zsi1 / ( zsi1 - zsi0 )
          DO jk = 1, nlay_i
-!CDIR NOVERRCHK
             DO ji = kideb, kiut
                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 zsi0 and 0 otherwise
+               zswi0  = MAX( 0._wp , SIGN( 1._wp  , zsi0 - sm_i_1d(ji) ) ) 
+               ! zswi01 = 1 if sm_i is between zsi0 and zsi1 and 0 othws 
+               zswi01 = ( 1._wp - zswi0 ) * MAX( 0._wp , SIGN( 1._wp , zsi1 - sm_i_1d(ji) ) ) 
+               ! if 2.sm_i GE sss_m then rswitch = 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) ) )
+               rswitch = 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) * zfac0 + zfac1 )  ) * ( 1._wp - rswitch )
                !
-               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) * r1_nlay_i
                ! weighting the profile
-               s_i_b(ji,jk) = zalpha * zs_zero + ( 1._wp - zalpha ) * sm_i_b(ji)
-            END DO ! ji
-         END DO ! jk
-
-      ENDIF ! num_sal
+               s_i_1d(ji,jk) = zalpha * zs_zero + ( 1._wp - zalpha ) * sm_i_1d(ji)
+               ! bounding salinity
+               s_i_1d(ji,jk) = MIN( rn_simax, MAX( s_i_1d(ji,jk), rn_simin ) )
+            END DO 
+         END DO 
+
+      ENDIF 
 
       !-------------------------------------------------------
@@ -512,14 +517,13 @@
       !-------------------------------------------------------
 
-      IF( num_sal == 3 ) THEN      ! Schwarzacher (1959) multiyear salinity profile (mean = 2.30)
-         !
-         sm_i_b(:) = 2.30_wp
-         !
-!CDIR NOVERRCHK
+      IF( nn_icesal == 3 ) THEN      ! Schwarzacher (1959) multiyear salinity profile (mean = 2.30)
+         !
+         sm_i_1d(:) = 2.30_wp
+         !
          DO jk = 1, nlay_i
-            zargtemp  = ( REAL(jk,wp) - 0.5_wp ) / REAL(nlay_i,wp)
-            zsal =  1.6_wp * (  1._wp - COS( rpi * zargtemp**(0.407_wp/(0.573_wp+zargtemp)) )  )
+            zargtemp  = ( REAL(jk,wp) - 0.5_wp ) * r1_nlay_i
+            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
@@ -530,4 +534,252 @@
       !
    END SUBROUTINE lim_var_salprof1d
+
+   SUBROUTINE lim_var_zapsmall
+      !!-------------------------------------------------------------------
+      !!                   ***  ROUTINE lim_var_zapsmall ***
+      !!
+      !! ** Purpose :   Remove too small sea ice areas and correct fluxes
+      !!
+      !! history : LIM3.5 - 01-2014 (C. Rousset) original code
+      !!-------------------------------------------------------------------
+      INTEGER  ::   ji, jj, jl, jk   ! dummy loop indices
+      REAL(wp) ::   zsal, zvi, zvs, zei, zes
+      !!-------------------------------------------------------------------
+      at_i (:,:) = 0._wp
+      DO jl = 1, jpl
+         at_i(:,:) = at_i(:,:) + a_i(:,:,jl)
+      END DO
+
+      DO jl = 1, jpl
+
+         !-----------------------------------------------------------------
+         ! Zap ice energy and use ocean heat to melt ice
+         !-----------------------------------------------------------------
+         DO jk = 1, nlay_i
+            DO jj = 1 , jpj
+               DO ji = 1 , jpi
+                  rswitch          = MAX( 0._wp , SIGN( 1._wp, a_i(ji,jj,jl) - epsi10 ) )
+                  rswitch          = MAX( 0._wp , SIGN( 1._wp, at_i(ji,jj  ) - epsi10 ) ) * rswitch
+                  rswitch          = MAX( 0._wp , SIGN( 1._wp, v_i(ji,jj,jl) - epsi10 ) ) * rswitch
+                  rswitch          = MAX( 0._wp , SIGN( 1._wp, v_i(ji,jj,jl) * rswitch  &
+                     &                                       / MAX( a_i(ji,jj,jl), epsi10 ) - epsi10 ) ) * rswitch
+                  zei              = e_i(ji,jj,jk,jl)
+                  e_i(ji,jj,jk,jl) = e_i(ji,jj,jk,jl) * rswitch
+                  t_i(ji,jj,jk,jl) = t_i(ji,jj,jk,jl) * rswitch + rt0 * ( 1._wp - rswitch )
+                  ! update exchanges with ocean
+                  hfx_res(ji,jj)   = hfx_res(ji,jj) + ( e_i(ji,jj,jk,jl) - zei ) * r1_rdtice ! W.m-2 <0
+               END DO
+            END DO
+         END DO
+
+         DO jj = 1 , jpj
+            DO ji = 1 , jpi
+               rswitch = MAX( 0._wp , SIGN( 1._wp, a_i(ji,jj,jl) - epsi10 ) )
+               rswitch = MAX( 0._wp , SIGN( 1._wp, at_i(ji,jj  ) - epsi10 ) ) * rswitch
+               rswitch = MAX( 0._wp , SIGN( 1._wp, v_i(ji,jj,jl) - epsi10 ) ) * rswitch
+               rswitch = MAX( 0._wp , SIGN( 1._wp, v_i(ji,jj,jl) * rswitch  &
+                  &                              / MAX( a_i(ji,jj,jl), epsi10 ) - epsi10 ) ) * rswitch
+               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 
+               !-----------------------------------------------------------------
+               t_s(ji,jj,1,jl) = t_s(ji,jj,1,jl) * rswitch + rt0 * ( 1._wp - rswitch )
+               e_s(ji,jj,1,jl) = e_s(ji,jj,1,jl) * rswitch
+
+               !-----------------------------------------------------------------
+               ! zap ice and snow volume, add water and salt to ocean
+               !-----------------------------------------------------------------
+               ato_i(ji,jj)    = a_i  (ji,jj,jl) * ( 1._wp - rswitch ) + ato_i(ji,jj)
+               a_i  (ji,jj,jl) = a_i  (ji,jj,jl) * rswitch
+               v_i  (ji,jj,jl) = v_i  (ji,jj,jl) * rswitch
+               v_s  (ji,jj,jl) = v_s  (ji,jj,jl) * rswitch
+               t_su (ji,jj,jl) = t_su (ji,jj,jl) * rswitch + t_bo(ji,jj) * ( 1._wp - rswitch )
+               oa_i (ji,jj,jl) = oa_i (ji,jj,jl) * rswitch
+               smv_i(ji,jj,jl) = smv_i(ji,jj,jl) * rswitch
+
+               ! 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 ) * r1_rdtice ! W.m-2 <0
+            END DO
+         END DO
+      END DO 
+
+      ! to be sure that at_i is the sum of a_i(jl)
+      at_i (:,:) = 0._wp
+      DO jl = 1, jpl
+         at_i(:,:) = at_i(:,:) + a_i(:,:,jl)
+      END DO
+
+      ! open water = 1 if at_i=0
+      DO jj = 1, jpj
+         DO ji = 1, jpi
+            rswitch      = MAX( 0._wp , SIGN( 1._wp, - at_i(ji,jj) ) )
+            ato_i(ji,jj) = rswitch + (1._wp - rswitch ) * ato_i(ji,jj)
+         END DO
+      END DO
+
+      !
+   END SUBROUTINE lim_var_zapsmall
+
+   SUBROUTINE lim_var_itd( zhti, zhts, zai, zht_i, zht_s, za_i )
+      !!------------------------------------------------------------------
+      !!                ***  ROUTINE lim_var_itd   ***
+      !!
+      !! ** Purpose :  converting 1-cat ice to multiple ice categories
+      !!
+      !!                  ice thickness distribution follows a gaussian law
+      !!               around the concentration of the most likely ice thickness
+      !!                           (similar as limistate.F90)
+      !!
+      !! ** Method:   Iterative procedure
+      !!                
+      !!               1) Try to fill the jpl ice categories (bounds hi_max(0:jpl)) with a gaussian
+      !!
+      !!               2) Check whether the distribution conserves area and volume, positivity and
+      !!                  category boundaries
+      !!              
+      !!               3) If not (input ice is too thin), the last category is empty and
+      !!                  the number of categories is reduced (jpl-1)
+      !!
+      !!               4) Iterate until ok (SUM(itest(:) = 4)
+      !!
+      !! ** Arguments : zhti: 1-cat ice thickness
+      !!                zhts: 1-cat snow depth
+      !!                zai : 1-cat ice concentration
+      !!
+      !! ** Output    : jpl-cat 
+      !!
+      !!  (Example of application: BDY forcings when input are cell averaged)  
+      !!
+      !!-------------------------------------------------------------------
+      !! History : LIM3.5 - 2012    (M. Vancoppenolle)  Original code
+      !!                    2014    (C. Rousset)        Rewriting
+      !!-------------------------------------------------------------------
+      !! Local variables
+      INTEGER  :: ji, jk, jl             ! dummy loop indices
+      INTEGER  :: ijpij, i_fill, jl0  
+      REAL(wp) :: zarg, zV, zconv, zdh
+      REAL(wp), DIMENSION(:),   INTENT(in)    ::   zhti, zhts, zai    ! input ice/snow variables
+      REAL(wp), DIMENSION(:,:), INTENT(inout) ::   zht_i, zht_s, za_i ! output ice/snow variables
+      INTEGER , POINTER, DIMENSION(:)         ::   itest
+ 
+      CALL wrk_alloc( 4, itest )
+      !--------------------------------------------------------------------
+      ! initialisation of variables
+      !--------------------------------------------------------------------
+      ijpij = SIZE(zhti,1)
+      zht_i(1:ijpij,1:jpl) = 0._wp
+      zht_s(1:ijpij,1:jpl) = 0._wp
+      za_i (1:ijpij,1:jpl) = 0._wp
+
+      ! ----------------------------------------
+      ! distribution over the jpl ice categories
+      ! ----------------------------------------
+      DO ji = 1, ijpij
+         
+         IF( zhti(ji) > 0._wp ) THEN
+
+         ! initialisation of tests
+         itest(:)  = 0
+         
+         i_fill = jpl + 1                                             !====================================
+         DO WHILE ( ( SUM( itest(:) ) /= 4 ) .AND. ( i_fill >= 2 ) )  ! iterative loop on i_fill categories  
+            ! iteration                                               !====================================
+            i_fill = i_fill - 1
+            
+            ! initialisation of ice variables for each try
+            zht_i(ji,1:jpl) = 0._wp
+            za_i (ji,1:jpl) = 0._wp
+            
+            ! *** case very thin ice: fill only category 1
+            IF ( i_fill == 1 ) THEN
+               zht_i(ji,1) = zhti(ji)
+               za_i (ji,1) = zai (ji)
+
+            ! *** case ice is thicker: fill categories >1
+            ELSE
+
+               ! Fill ice thicknesses except the last one (i_fill) by hmean 
+               DO jl = 1, i_fill - 1
+                  zht_i(ji,jl) = hi_mean(jl)
+               END DO
+               
+               ! find which category (jl0) the input ice thickness falls into
+               jl0 = i_fill
+               DO jl = 1, i_fill
+                  IF ( ( zhti(ji) >= hi_max(jl-1) ) .AND. ( zhti(ji) < hi_max(jl) ) ) THEN
+                     jl0 = jl
+		     CYCLE
+                  ENDIF
+               END DO
+               
+               ! Concentrations in the (i_fill-1) categories 
+               za_i(ji,jl0) = zai(ji) / SQRT(REAL(jpl))
+               DO jl = 1, i_fill - 1
+                  IF ( jl == jl0 ) CYCLE
+                  zarg        = ( zht_i(ji,jl) - zhti(ji) ) / ( zhti(ji) * 0.5_wp )
+                  za_i(ji,jl) =   za_i (ji,jl0) * EXP(-zarg**2)
+               END DO
+               
+               ! Concentration in the last (i_fill) category
+               za_i(ji,i_fill) = zai(ji) - SUM( za_i(ji,1:i_fill-1) )
+               
+               ! Ice thickness in the last (i_fill) category
+               zV = SUM( za_i(ji,1:i_fill-1) * zht_i(ji,1:i_fill-1) )
+               zht_i(ji,i_fill) = ( zhti(ji) * zai(ji) - zV ) / za_i(ji,i_fill) 
+               
+            ENDIF ! case ice is thick or thin
+            
+            !---------------------
+            ! Compatibility tests
+            !--------------------- 
+            ! Test 1: area conservation
+            zconv = ABS( zai(ji) - SUM( za_i(ji,1:jpl) ) )
+            IF ( zconv < epsi06 ) itest(1) = 1
+            
+            ! Test 2: volume conservation
+            zconv = ABS( zhti(ji)*zai(ji) - SUM( za_i(ji,1:jpl)*zht_i(ji,1:jpl) ) )
+            IF ( zconv < epsi06 ) itest(2) = 1
+            
+            ! Test 3: thickness of the last category is in-bounds ?
+            IF ( zht_i(ji,i_fill) >= hi_max(i_fill-1) ) itest(3) = 1
+            
+            ! Test 4: positivity of ice concentrations
+            itest(4) = 1
+            DO jl = 1, i_fill
+               IF ( za_i(ji,jl) < 0._wp ) itest(4) = 0
+            END DO            
+                                                           !============================
+         END DO                                            ! end iteration on categories
+                                                           !============================
+         ENDIF ! if zhti > 0
+      END DO ! i loop
+
+      ! ------------------------------------------------
+      ! Adding Snow in each category where za_i is not 0
+      ! ------------------------------------------------ 
+      DO jl = 1, jpl
+         DO ji = 1, ijpij
+            IF( za_i(ji,jl) > 0._wp ) THEN
+               zht_s(ji,jl) = zht_i(ji,jl) * ( zhts(ji) / zhti(ji) )
+               ! In case snow load is in excess that would lead to transformation from snow to ice
+               ! Then, transfer the snow excess into the ice (different from limthd_dh)
+               zdh = MAX( 0._wp, ( rhosn * zht_s(ji,jl) + ( rhoic - rau0 ) * zht_i(ji,jl) ) * r1_rau0 ) 
+               ! recompute ht_i, ht_s avoiding out of bounds values
+               zht_i(ji,jl) = MIN( hi_max(jl), zht_i(ji,jl) + zdh )
+               zht_s(ji,jl) = MAX( 0._wp, zht_s(ji,jl) - zdh * rhoic * r1_rhosn )
+            ENDIF
+         ENDDO
+      ENDDO
+
+      CALL wrk_dealloc( 4, itest )
+      !
+    END SUBROUTINE lim_var_itd
+
 
 #else
@@ -548,4 +800,8 @@
    SUBROUTINE lim_var_salprof1d    ! Emtpy routines
    END SUBROUTINE lim_var_salprof1d
+   SUBROUTINE lim_var_zapsmall
+   END SUBROUTINE lim_var_zapsmall
+   SUBROUTINE lim_var_itd
+   END SUBROUTINE lim_var_itd
 #endif
 
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limwri.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limwri.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limwri.F90	(revision 5832)
@@ -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
    !!----------------------------------------------------------------------
@@ -25,5 +24,4 @@
    USE lib_mpp         ! MPP library
    USE wrk_nemo        ! work arrays
-   USE par_ice
    USE iom
    USE timing          ! Timing
@@ -36,23 +34,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 +57,219 @@
       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, zt_i, zt_s
+      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, zt_i, zt_s )
+      CALL wrk_alloc( jpi, jpj     , z2d, z2da, z2db, zswi )
+
+      !-----------------------------
+      ! Mean category values
+      !-----------------------------
+      z1_365 = 1._wp / 365._wp
+
+      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) * umask(ji,jj,1) + u_ice(ji-1,jj) * umask(ji-1,jj,1) ) * 0.5_wp
+               z2db(ji,jj)  = (  v_ice(ji,jj) * vmask(ji,jj,1) + v_ice(ji,jj-1) * vmask(ji,jj-1,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
+                  rswitch    = MAX( 0._wp , SIGN( 1._wp , at_i(ji,jj) - 0.1 ) )
+                  z2d(ji,jj) = z2d(ji,jj) + rswitch * oa_i(ji,jj,jl) / MAX( at_i(ji,jj), 0.1 )
+               END DO
+            END DO
+         END DO
+         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) - rt0 ) * 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) - rt0 ) * 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 * 86400.    )        ! 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( "saltrp"      , diag_trp_smv * rday * rhoic ) ! salt content 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( "afxtot"     , afx_tot * rday       )        ! concentration tendency (total)
+      CALL iom_put( "afxdyn"     , afx_dyn * rday       )        ! concentration tendency (dynamics)
+      CALL iom_put( "afxthd"     , afx_thd * rday       )        ! concentration tendency (thermo)
+
+      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(:,:) * SUM(a_i_b(:,:,:), dim=3) ) ! turbulent heat flux at ice base 
+      CALL iom_put ('hfxdhc'     , diag_heat(:,:)       )   ! 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
+
+      ! ice temperature
+      IF ( iom_use( "icetemp_cat" ) ) THEN 
+         zt_i(:,:,:) = SUM( t_i(:,:,:,:), dim=3 ) * r1_nlay_i
+         CALL iom_put( "icetemp_cat"   , zt_i - rt0  )
+      ENDIF
+      
+      ! snow temperature
+      IF ( iom_use( "snwtemp_cat" ) ) THEN 
+         zt_s(:,:,:) = SUM( t_s(:,:,:,:), dim=3 ) * r1_nlay_s
+         CALL iom_put( "snwtemp_cat"   , zt_s - rt0  )
+      ENDIF
+
+      ! 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 , at_i(ji,jj) - 0.1 ) )
+                  rswitch = rswitch * MAX( 0._wp , SIGN( 1._wp , a_i(ji,jj,jl) - 0.1 ) )
+                  zoi(ji,jj,jl) = oa_i(ji,jj,jl)  / MAX( a_i(ji,jj,jl) , 0.1 ) * rswitch
                END DO
             END DO
          END DO
-
-         ! Compute brine volume
+         CALL iom_put( "iceage_cat"   , zoi * z1_365 )        ! ice age for categories
+      ENDIF
+
+      ! Compute brine volume
+      IF ( iom_use( "brinevol_cat" ) ) THEN 
          zei(:,:,:) = 0._wp
          DO jl = 1, jpl 
@@ -379,38 +277,21 @@
                DO jj = 1, jpj
                   DO ji = 1, jpi
-                     zinda = MAX( zzero , SIGN( zone , 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 = 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) - rt0 ), - epsi06 ) ) * &
+                        rswitch * r1_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, zt_i, zt_s )
+      CALL wrk_dealloc( jpi, jpj     , z2d, zswi, z2da, z2db )
 
       IF( nn_timing == 1 )  CALL timing_stop('limwri')
@@ -419,124 +300,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 )
-      IF(lwm) 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 +316,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
@@ -591,5 +366,5 @@
       CALL histwrite( kid, "iicethic", kt, icethi        , jpi*jpj, (/1/) )    
       CALL histwrite( kid, "iiceconc", kt, at_i          , jpi*jpj, (/1/) )
-      CALL histwrite( kid, "iicetemp", kt, tm_i - rtt    , jpi*jpj, (/1/) )
+      CALL histwrite( kid, "iicetemp", kt, tm_i - rt0    , jpi*jpj, (/1/) )
       CALL histwrite( kid, "iicevelu", kt, u_ice          , jpi*jpj, (/1/) )
       CALL histwrite( kid, "iicevelv", kt, v_ice          , jpi*jpj, (/1/) )
@@ -603,19 +378,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_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limwri_dimg.h90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limwri_dimg.h90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/limwri_dimg.h90	(revision 5832)
@@ -89,20 +89,20 @@
    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 , ( umask(ji,jj,1) + umask(ji+1,jj,1) + umask(ji,jj+1,1) + umask(ji+1,jj+1,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,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) ) &
+         zcmo(ji,jj,6)  = fhtur  (ji,jj)
+         zcmo(ji,jj,7)  = zindb * (  u_ice(ji,jj  ) * umask(ji,jj,1) + u_ice(ji+1,jj  ) * umask(ji+1,jj,1)   &
+            + u_ice(ji,jj+1) * umask(ji,jj+1,1) + u_ice(ji+1,jj+1) * umask(ji+1,jj+1,1) ) &
             / ztmu 
 
-         zcmo(ji,jj,8)  = zindb * (  v_ice(ji,jj  ) * tmu(ji,jj  ) + v_ice(ji+1,jj  ) * tmu(ji+1,jj  )   &
-            + v_ice(ji,jj+1) * tmu(ji,jj+1) + v_ice(ji+1,jj+1) * tmu(ji+1,jj+1) ) &
+         zcmo(ji,jj,8)  = zindb * (  v_ice(ji,jj  ) * umask(ji,jj,1) + v_ice(ji+1,jj  ) * umask(ji+1,jj,1)   &
+            + v_ice(ji,jj+1) * umask(ji,jj+1,1) + v_ice(ji+1,jj+1) * umask(ji+1,jj+1,1) ) &
             / ztmu
          zcmo(ji,jj,9)  = sst_m(ji,jj)
@@ -132,20 +132,20 @@
          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 , ( umask(ji,jj,1) + umask(ji+1,jj,1) + umask(ji,jj+1,1) + umask(ji+1,jj+1,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,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) ) &
+               rcmoy(ji,jj,6)  = fhtur  (ji,jj)
+               rcmoy(ji,jj,7)  = zindb * (  u_ice(ji,jj  ) * umask(ji,jj,1) + u_ice(ji+1,jj  ) * umask(ji+1,jj,1)   &
+                  + u_ice(ji,jj+1) * umask(ji,jj+1,1) + u_ice(ji+1,jj+1) * umask(ji+1,jj+1,1) ) &
                   / ztmu
 
-               rcmoy(ji,jj,8)  = zindb * (  v_ice(ji,jj  ) * tmu(ji,jj  ) + v_ice(ji+1,jj  ) * tmu(ji+1,jj  )   &
-                  + v_ice(ji,jj+1) * tmu(ji,jj+1) + v_ice(ji+1,jj+1) * tmu(ji+1,jj+1) ) &
+               rcmoy(ji,jj,8)  = zindb * (  v_ice(ji,jj  ) * umask(ji,jj,1) + v_ice(ji+1,jj  ) * umask(ji+1,jj,1)   &
+                  + v_ice(ji,jj+1) * umask(ji,jj+1,1) + v_ice(ji+1,jj+1) * umask(ji+1,jj+1,1) ) &
                   / ztmu
                rcmoy(ji,jj,9)  = sst_m(ji,jj)
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/par_ice.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/par_ice.F90	(revision 4712)
+++ 	(revision )
@@ -1,26 +1,0 @@
-MODULE par_ice
-   !!======================================================================
-   !!                       ***  MODULE par_ice   ***
-   !! LIM-3 Sea Ice :   definition of parameters
-   !!======================================================================
-   !! History :  3.0  ! 2003-08  (M. Vancoppenolle)  LIM-3
-   !!----------------------------------------------------------------------
-   USE par_oce
-
-   IMPLICIT NONE
-   PUBLIC          
-
-   !                                             !!! ice thermodynamics
-   INTEGER, PUBLIC, PARAMETER ::   jkmax    = 6   !: maximum 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
-
-   !!----------------------------------------------------------------------
-   !! NEMO/LIM3 3.3 , UCL - NEMO Consortium (2010)
-   !! $Id$
-   !! Software governed by the CeCILL licence (NEMOGCM/NEMO_CeCILL.txt)
-   !!======================================================================
-END MODULE par_ice
Index: branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/thd_ice.F90
===================================================================
--- branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/thd_ice.F90	(revision 4712)
+++ branches/2014/dev_r4650_UKMO12_CFL_diags_take2/NEMOGCM/NEMO/LIM_SRC_3/thd_ice.F90	(revision 5832)
@@ -6,7 +6,7 @@
    !! History :  3.0  !  2002-11  (C. Ethe)  F90: Free form and module
    !!----------------------------------------------------------------------
-   USE par_ice        ! LIM-3 parameters
    USE in_out_manager ! I/O manager
    USE lib_mpp        ! MPP library
+   USE ice, ONLY :   nlay_i, nlay_s
 
    IMPLICIT NONE
@@ -19,23 +19,11 @@
    !!---------------------------
    !                               !!! ** 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 ::   rn_himin    !: minimum ice thickness
+   REAL(wp), PUBLIC ::   rn_maxfrazb !: maximum portion of frazil ice collecting at the ice bottom
+   REAL(wp), PUBLIC ::   rn_vfrazb   !: threshold drift speed for collection of bottom frazil ice
+   REAL(wp), PUBLIC ::   rn_Cfrazb   !: squeezing coefficient for collection of bottom frazil ice
+   REAL(wp), PUBLIC ::   rn_hnewice  !: thickness for new ice formation (m)
 
-   REAL(wp), PUBLIC, DIMENSION(2) ::   hiccrit   !: ice th. for lateral accretion in the NH (SH) (m)
+   LOGICAL , PUBLIC ::   ln_frazil   !: use of frazil ice collection as function of wind (T) or not (F)
 
    !!-----------------------------
@@ -43,98 +31,94 @@
    !!-----------------------------
    !: 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
 
-   INTEGER , PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   npb    !: number of points where computations has to be done
-   INTEGER , PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   npac   !: correspondance between points (lateral accretion)
+   INTEGER , PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   npb    !: address vector for 1d vertical thermo computations
+   INTEGER , PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   nplm   !: address vector for mono-category lateral melting
+   INTEGER , PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   npac   !: address vector for new ice formation
 
-   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
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   hfx_err_dif_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
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   dqla_ice_1d   !: <==> the 2D  dqla_ice
-   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   tatm_ice_1d   !: <==> the 2D  tatm_ice
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   evap_ice_1d   !: <==> the 2D  evap_ice
+   REAL(wp), PUBLIC, ALLOCATABLE, SAVE, DIMENSION(:) ::   qprec_ice_1d  !: <==> the 2D  qprec_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 +135,36 @@
       !!---------------------------------------------------------------------!
       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) )
+      ALLOCATE( npb      (jpij) , nplm      (jpij) , npac       (jpij) ,   &
+         &      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) , hfx_err_dif_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) ,     &
-         &      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) ,     &
-         &      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) )
+      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) , evap_ice_1d (jpij),                                         &
+         &      qprec_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_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) , s_i_1d(jpij,nlay_i) ,  &            
+         &      q_i_1d(jpij,nlay_i+1) , q_s_1d(jpij,nlay_s) ,                        &
+         &      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 )
 
