Index: branches/2017/dev_r8183_ICEMODEL/NEMOGCM/NEMO/LIM_SRC_3/icevar.F90
===================================================================
--- branches/2017/dev_r8183_ICEMODEL/NEMOGCM/NEMO/LIM_SRC_3/icevar.F90	(revision 8786)
+++ branches/2017/dev_r8183_ICEMODEL/NEMOGCM/NEMO/LIM_SRC_3/icevar.F90	(revision 8787)
@@ -44,5 +44,6 @@
    !!   ice_var_salprof1d : salinity profile in the ice 1D
    !!   ice_var_zapsmall  : remove very small area and volume
-   !!   ice_var_itd       : convert 1-cat to multiple cat
+   !!   ice_var_itd       : convert 1-cat to jpl-cat
+   !!   ice_var_itd2      : convert N-cat to jpl-cat
    !!   ice_var_bv        : brine volume
    !!----------------------------------------------------------------------
@@ -67,4 +68,5 @@
    PUBLIC   ice_var_zapsmall
    PUBLIC   ice_var_itd
+   PUBLIC   ice_var_itd2
    PUBLIC   ice_var_bv           
 
@@ -549,5 +551,5 @@
       !!-------------------------------------------------------------------
       INTEGER  :: ji, jk, jl             ! dummy loop indices
-      INTEGER  :: ijpij, i_fill, jl0  
+      INTEGER  :: ndim, i_fill, jl0  
       REAL(wp) :: zarg, zV, zconv, zdh, zdv
       REAL(wp), DIMENSION(:),   INTENT(in)    ::   zhti, zhts, zati    ! input ice/snow variables
@@ -562,10 +564,10 @@
       ! then we check whether the distribution fullfills
       ! volume and area conservation, positivity and ice categories bounds
-      ijpij = SIZE( zhti , 1 )
-      zh_i(1:ijpij,1:jpl) = 0._wp
-      zh_s(1:ijpij,1:jpl) = 0._wp
-      za_i (1:ijpij,1:jpl) = 0._wp
-
-      DO ji = 1, ijpij
+      ndim = SIZE( zhti , 1 )
+      zh_i(1:ndim,1:jpl) = 0._wp
+      zh_s(1:ndim,1:jpl) = 0._wp
+      za_i(1:ndim,1:jpl) = 0._wp
+
+      DO ji = 1, ndim
          
          IF( zhti(ji) > 0._wp ) THEN
@@ -649,5 +651,5 @@
       ! Add Snow in each category where za_i is not 0
       DO jl = 1, jpl
-         DO ji = 1, ijpij
+         DO ji = 1, ndim
             IF( za_i(ji,jl) > 0._wp ) THEN
                zh_s(ji,jl) = zh_i(ji,jl) * ( zhts(ji) / zhti(ji) )
@@ -662,8 +664,137 @@
       END DO
       !
-    END SUBROUTINE ice_var_itd
-
-
-    SUBROUTINE ice_var_bv
+   END SUBROUTINE ice_var_itd
+
+   SUBROUTINE ice_var_itd2( zhti, zhts, zati, zh_i, zh_s, za_i )
+      !!-------------------------------------------------------------------
+      !!                ***  ROUTINE ice_var_itd2   ***
+      !!
+      !! ** Purpose :  converting N-cat ice to jpl ice categories
+      !!
+      !!                  ice thickness distribution follows a gaussian law
+      !!               around the concentration of the most likely ice thickness
+      !!                           (similar as iceistate.F90)
+      !!
+      !! ** Method:   Iterative procedure
+      !!                
+      !!               1) Fill ice cat that correspond to input thicknesses
+      !!                  Find the lowest(jlmin) and highest(jlmax) cat that are filled
+      !!
+      !!               2) Expand the filling to the cat jlmin-1 and jlmax+1
+      !!                   by removing 25% ice area from jlmin and jlmax (resp.) 
+      !!              
+      !!               3) Expand the filling to the empty cat between jlmin and jlmax 
+      !!                   by a) removing 25% ice area from the lower cat (ascendant loop jlmin=>jlmax)
+      !!                      b) removing 25% ice area from the higher cat (descendant loop jlmax=>jlmin)
+      !!
+      !! ** Arguments : zhti: N-cat ice thickness
+      !!                zhts: N-cat snow depth
+      !!                zati: N-cat ice concentration
+      !!
+      !! ** Output    : jpl-cat 
+      !!
+      !!  (Example of application: BDY forcings when inputs have N-cat /= jpl)  
+      !!-------------------------------------------------------------------
+      INTEGER  ::   ji, jl, jl1, jl2             ! dummy loop indices
+      INTEGER  ::   ndim, ncat  
+      INTEGER, PARAMETER ::   ztrans = 0.25_wp
+      REAL(wp), DIMENSION(:,:), INTENT(in)    ::   zhti, zhts, zati    ! input ice/snow variables
+      REAL(wp), DIMENSION(:,:), INTENT(inout) ::   zh_i, zh_s, za_i    ! output ice/snow variables
+      INTEGER , DIMENSION(:,:), ALLOCATABLE   ::   jlfil, jlfil2
+      INTEGER , DIMENSION(:)  , ALLOCATABLE   ::   jlmax, jlmin
+      !!-------------------------------------------------------------------
+      !
+      ndim = SIZE( zhti, 1 )
+      ncat = SIZE( zhti, 2 )
+
+      ! allocate arrays
+      ALLOCATE( jlfil(ndim,jpl), jlfil2(ndim,jpl) ) 
+      ALLOCATE( jlmin(ndim), jlmax(ndim) )
+
+      ! --- initialize output fields to 0 --- !
+      zh_i(1:ndim,1:jpl) = 0._wp
+      zh_s(1:ndim,1:jpl) = 0._wp
+      za_i(1:ndim,1:jpl) = 0._wp
+      !
+      ! --- fill the categories --- !
+      !     find where cat-input = cat-output and fill cat-output fields  
+      jlmax(:) = 0
+      jlmin(:) = 999
+      jlfil(:,:) = 0
+      DO jl1 = 1, jpl
+         DO jl2 = 1, ncat
+            DO ji = 1, ndim
+               IF( hi_max(jl1-1) <= zhti(ji,jl2) .AND. hi_max(jl1) > zhti(ji,jl2) ) THEN
+                  ! fill the right category
+                  zh_i(ji,jl1) = zhti(ji,jl2)
+                  zh_s(ji,jl1) = zhts(ji,jl2)
+                  za_i(ji,jl1) = zati(ji,jl2)
+                  ! record categories that are filled
+                  jlmax(ji) = MAX( jlmax(ji), jl1 )
+                  jlmin(ji) = MIN( jlmin(ji), jl1 )
+                  jlfil(ji,jl1) = jl1
+               ENDIF
+            END DO
+         END DO
+      END DO
+      !
+      ! --- fill the gaps between categories --- !  
+      !     transfer from categories filled at the previous step to the empty ones in between
+      DO ji = 1, ndim
+         jl1 = jlmin(ji)
+         jl2 = jlmax(ji)
+         IF( jl1 > 1 ) THEN
+            ! fill the lower cat (jl1-1)
+            za_i(ji,jl1-1) = ztrans * za_i(ji,jl1)
+            zh_i(ji,jl1-1) = hi_mean(jl1-1)
+            ! remove from cat jl1
+            za_i(ji,jl1  ) = ( 1._wp - ztrans ) * za_i(ji,jl1)
+         ENDIF
+         IF( jl2 < jpl ) THEN
+            ! fill the upper cat (jl2+1)
+            za_i(ji,jl2+1) = ztrans * za_i(ji,jl2)
+            zh_i(ji,jl2+1) = hi_mean(jl2+1)
+            ! remove from cat jl2
+            za_i(ji,jl2  ) = ( 1._wp - ztrans ) * za_i(ji,jl2)
+         ENDIF
+      END DO
+     
+      jlfil2(:,:) = jlfil(:,:) 
+      ! fill categories from low to high
+      DO jl = 2, jpl-1
+         DO ji = 1, ndim
+            IF( jlfil(ji,jl-1) /= 0 .AND. jlfil(ji,jl) == 0 ) THEN
+               ! fill high
+               za_i(ji,jl) = ztrans * za_i(ji,jl-1)
+               zh_i(ji,jl) = hi_mean(jl)
+               jlfil(ji,jl) = jl
+               ! remove low
+               za_i(ji,jl-1) = ( 1._wp - ztrans ) * za_i(ji,jl-1)
+            ENDIF
+         END DO
+      END DO
+
+      ! fill categories from high to low
+      DO jl = jpl-1, 2, -1
+         DO ji = 1, ndim
+            IF( jlfil2(ji,jl+1) /= 0 .AND. jlfil2(ji,jl) == 0 ) THEN
+               ! fill low
+               za_i(ji,jl) = za_i(ji,jl) + ztrans * za_i(ji,jl+1)
+               zh_i(ji,jl) = hi_mean(jl) 
+               jlfil2(ji,jl) = jl
+               ! remove high
+               za_i(ji,jl+1) = ( 1._wp - ztrans ) * za_i(ji,jl+1)
+            ENDIF
+         END DO
+      END DO
+      
+      ! deallocate arrays
+      DEALLOCATE( jlfil, jlfil2 )
+      DEALLOCATE( jlmin, jlmax )
+
+   END SUBROUTINE ice_var_itd2
+
+
+   SUBROUTINE ice_var_bv
       !!-------------------------------------------------------------------
       !!                ***  ROUTINE ice_var_bv ***
Index: branches/2017/dev_r8183_ICEMODEL/NEMOGCM/NEMO/OPA_SRC/BDY/bdydta.F90
===================================================================
--- branches/2017/dev_r8183_ICEMODEL/NEMOGCM/NEMO/OPA_SRC/BDY/bdydta.F90	(revision 8786)
+++ branches/2017/dev_r8183_ICEMODEL/NEMOGCM/NEMO/OPA_SRC/BDY/bdydta.F90	(revision 8787)
@@ -48,5 +48,5 @@
 
 #if defined key_lim3
-   LOGICAL :: ll_bdylim3                  ! determine whether ice input is 1cat (F) or Xcat (T) type
+   INTEGER :: nice_cat                    ! number of categories in the input file
    INTEGER :: jfld_hti, jfld_hts, jfld_ai ! indices of ice thickness, snow thickness and concentration in bf structure
 #endif
@@ -175,5 +175,5 @@
 
 #if defined key_lim3
-            IF( nn_ice_lim_dta(ib_bdy) == 0 ) THEN 
+            IF( nn_ice_lim_dta(ib_bdy) == 0 ) THEN ! set ice to initial values
                ilen1(:) = nblen(:)
                IF( dta%ll_a_i ) THEN
@@ -343,7 +343,12 @@
                ENDIF
 #if defined key_lim3
-               IF( .NOT. ll_bdylim3 .AND. cn_ice_lim(ib_bdy) /= 'none' .AND. nn_ice_lim_dta(ib_bdy) == 1 ) THEN ! bdy ice input (case input is 1cat)
-     	          CALL ice_var_itd ( bf(jfld_hti)%fnow(:,1,1), bf(jfld_hts)%fnow(:,1,1), bf(jfld_ai)%fnow(:,1,1), &
-                                  & dta_bdy(ib_bdy)%h_i,     dta_bdy(ib_bdy)%h_s,     dta_bdy(ib_bdy)%a_i     )
+               IF( cn_ice_lim(ib_bdy) /= 'none' .AND. nn_ice_lim_dta(ib_bdy) == 1 ) THEN
+                  IF( nice_cat == 1 ) THEN ! case input cat = 1
+                     CALL ice_var_itd ( bf(jfld_hti)%fnow(:,1,1), bf(jfld_hts)%fnow(:,1,1), bf(jfld_ai)%fnow(:,1,1), &
+                        &               dta_bdy(ib_bdy)%h_i     , dta_bdy(ib_bdy)%h_s     , dta_bdy(ib_bdy)%a_i    )
+                  ELSEIF( nice_cat /= 1 .AND. nice_cat /= jpl ) THEN ! case input cat /=1 and /=jpl
+                     CALL ice_var_itd2( bf(jfld_hti)%fnow(:,1,:), bf(jfld_hts)%fnow(:,1,:), bf(jfld_ai)%fnow(:,1,:), &
+                        &               dta_bdy(ib_bdy)%h_i     , dta_bdy(ib_bdy)%h_s     , dta_bdy(ib_bdy)%a_i    )
+                  ENDIF
                ENDIF
 #endif
@@ -413,5 +418,6 @@
       TYPE(OBC_DATA), POINTER                ::   dta           ! short cut
 #if defined key_lim3
-      INTEGER               ::   zndims   ! number of dimensions in an array (i.e. 3 = wo ice cat; 4 = w ice cat)
+      INTEGER               ::   kndims   ! number of dimensions in an array (i.e. 3 = wo ice cat; 4 = w ice cat)
+      INTEGER, DIMENSION(4) ::   kdimsz   ! size   of dimensions
       INTEGER               ::   inum,id1 ! local integer
 #endif
@@ -619,11 +625,11 @@
                !
                CALL iom_open  ( clname, inum )
-               id1 = iom_varid( inum, bn_a_i%clvar, kndims=zndims, ldstop = .FALSE. )
+               id1 = iom_varid( inum, bn_a_i%clvar, kdimsz=kdimsz, kndims=kndims, ldstop = .FALSE. )
                CALL iom_close ( inum )
 
-      	       IF ( zndims == 4 ) THEN
-                 ll_bdylim3 = .TRUE.   ! Xcat input
+      	       IF ( kndims == 4 ) THEN
+                 nice_cat = kdimsz(4)   ! Xcat input
                ELSE
-                 ll_bdylim3 = .FALSE.  ! 1cat input      
+                 nice_cat = 1           ! 1cat input      
                ENDIF
                ! End test
@@ -632,8 +638,8 @@
                   jfld = jfld + 1
                   blf_i(jfld) = bn_a_i
-                  ibdy(jfld) = ib_bdy
+                  ibdy(jfld)  = ib_bdy
                   igrid(jfld) = 1
                   ilen1(jfld) = nblen(igrid(jfld))
-                  IF ( ll_bdylim3 ) THEN ; ilen3(jfld)=jpl ; ELSE ; ilen3(jfld)=1 ; ENDIF
+                  ilen3(jfld) = nice_cat
                ENDIF
 
@@ -641,17 +647,17 @@
                   jfld = jfld + 1
                   blf_i(jfld) = bn_h_i
-                  ibdy(jfld) = ib_bdy
+                  ibdy(jfld)  = ib_bdy
                   igrid(jfld) = 1
                   ilen1(jfld) = nblen(igrid(jfld))
-                  IF ( ll_bdylim3 ) THEN ; ilen3(jfld)=jpl ; ELSE ; ilen3(jfld)=1 ; ENDIF
+                  ilen3(jfld) = nice_cat
                ENDIF
 
                IF( dta%ll_h_s ) THEN
                   jfld = jfld + 1
-                   blf_i(jfld) = bn_h_s
-                  ibdy(jfld) = ib_bdy
+                  blf_i(jfld) = bn_h_s
+                  ibdy(jfld)  = ib_bdy
                   igrid(jfld) = 1
                   ilen1(jfld) = nblen(igrid(jfld))
-                  IF ( ll_bdylim3 ) THEN ; ilen3(jfld)=jpl ; ELSE ; ilen3(jfld)=1 ; ENDIF
+                  ilen3(jfld) = nice_cat
                ENDIF
 
@@ -789,5 +795,5 @@
                ALLOCATE( dta_bdy(ib_bdy)%h_s(nblen(1),jpl) )
             ELSE
-               IF ( ll_bdylim3 ) THEN ! case input is Xcat
+               IF ( nice_cat == jpl ) THEN ! case input cat = jpl
                   jfld = jfld + 1
                   dta_bdy(ib_bdy)%a_i => bf(jfld)%fnow(:,1,:)
@@ -796,5 +802,5 @@
                   jfld = jfld + 1
                   dta_bdy(ib_bdy)%h_s => bf(jfld)%fnow(:,1,:)
-               ELSE ! case input is 1cat
+               ELSE                        ! case input cat = 1 OR (/=1 and /=jpl)
                   jfld_ai  = jfld + 1
                   jfld_hti = jfld + 2
