Index: /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/NST_SRC/agrif_lim3_interp.F90
===================================================================
--- /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/NST_SRC/agrif_lim3_interp.F90	(revision 8188)
+++ /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/NST_SRC/agrif_lim3_interp.F90	(revision 8189)
@@ -90,5 +90,5 @@
       !! i1 i2 j1 j2 are the index of the boundaries parent(when before) and child (when after)
       !! To solve issues when parent grid is "land" masked but not all the corresponding child grid points,
-      !! put -9999 WHERE the parent grid is masked. The child solution will be found in the 9(?) points around
+      !! put -999 WHERE the parent grid is masked. The child solution will be found in the 9(?) points around
       !!-----------------------------------------------------------------------
       INTEGER , INTENT(in) :: i1, i2, j1, j2
@@ -101,8 +101,8 @@
       IF( before ) THEN  ! parent grid
          ptab(:,:) = e2u(i1:i2,j1:j2) * u_ice_b(i1:i2,j1:j2)
-         WHERE( umask(i1:i2,j1:j2,1) == 0. )  ptab(:,:) = -9999.
+         WHERE( umask(i1:i2,j1:j2,1) == 0. )   ptab(i1:i2,j1:j2) = Agrif_SpecialValue
       ELSE               ! child grid
          zrhoy = Agrif_Rhoy()
-         u_ice(i1:i2,j1:j2) = ptab(:,:) / ( e2u(i1:i2,j1:j2) * zrhoy ) * umask(i1:i2,j1:j2,1)
+         u_ice(i1:i2,j1:j2) = ptab(i1:i2,j1:j2) / ( e2u(i1:i2,j1:j2) * zrhoy ) * umask(i1:i2,j1:j2,1)
       ENDIF
       !
@@ -116,5 +116,5 @@
       !! i1 i2 j1 j2 are the index of the boundaries parent(when before) and child (when after)
       !! To solve issues when parent grid is "land" masked but not all the corresponding child grid points,
-      !! put -9999 WHERE the parent grid is masked. The child solution will be found in the 9(?) points around
+      !! put -999 WHERE the parent grid is masked. The child solution will be found in the 9(?) points around
       !!-----------------------------------------------------------------------      
       INTEGER , INTENT(in) :: i1, i2, j1, j2
@@ -127,8 +127,8 @@
       IF( before ) THEN  ! parent grid
          ptab(:,:) = e1v(i1:i2,j1:j2) * v_ice_b(i1:i2,j1:j2)
-         WHERE( vmask(i1:i2,j1:j2,1) == 0. )  ptab(:,:) = -9999.
+         WHERE( vmask(i1:i2,j1:j2,1) == 0. )  ptab(i1:i2,j1:j2) = Agrif_SpecialValue
       ELSE               ! child grid
          zrhox = Agrif_Rhox()
-         v_ice(i1:i2,j1:j2) = ptab(:,:) / ( e1v(i1:i2,j1:j2) * zrhox ) * vmask(i1:i2,j1:j2,1)
+         v_ice(i1:i2,j1:j2) = ptab(i1:i2,j1:j2) / ( e1v(i1:i2,j1:j2) * zrhox ) * vmask(i1:i2,j1:j2,1)
       ENDIF
       !
@@ -142,5 +142,5 @@
       !! i1 i2 j1 j2 are the index of the boundaries parent(when before) and child (when after)
       !! To solve issues when parent grid is "land" masked but not all the corresponding child grid points,
-      !! put -9999 WHERE the parent grid is masked. The child solution will be found in the 9(?) points around
+      !! put -999 WHERE the parent grid is masked. The child solution will be found in the 9(?) points around
       !!-----------------------------------------------------------------------
       REAL(wp), DIMENSION(i1:i2,j1:j2,k1:k2), INTENT(inout) :: ptab
@@ -154,19 +154,19 @@
       REAL(wp) ::   zrhox, z1, z2, z3, z4, z5, z6, z7
       LOGICAL  ::   western_side, eastern_side, northern_side, southern_side
-      INTEGER  ::   ind1, ind2, ind3
 
       !!-----------------------------------------------------------------------
       ! tracers are not multiplied by grid cell here => before: * e12t ; after: * r1_e12t / rhox / rhoy
       ! and it is ok since we conserve tracers (same as in the ocean).
-      ALLOCATE( ztab(SIZE(a_i_b,1),SIZE(a_i_b,2),SIZE(ptab,3)) )
+      ALLOCATE( ztab(SIZE(a_i,1),SIZE(a_i,2),SIZE(ptab,3)) )
      
       IF( before ) THEN  ! parent grid
          jm = 1
          DO jl = 1, jpl
-            ptab(i1:i2,j1:j2,jm) = a_i_b  (i1:i2,j1:j2,jl) ; jm = jm + 1
-            ptab(i1:i2,j1:j2,jm) = v_i_b  (i1:i2,j1:j2,jl) ; jm = jm + 1
-            ptab(i1:i2,j1:j2,jm) = v_s_b  (i1:i2,j1:j2,jl) ; jm = jm + 1
-            ptab(i1:i2,j1:j2,jm) = smv_i_b(i1:i2,j1:j2,jl) ; jm = jm + 1
-            ptab(i1:i2,j1:j2,jm) = oa_i_b (i1:i2,j1:j2,jl) ; jm = jm + 1
+            ptab(i1:i2,j1:j2,jm  ) = a_i_b  (i1:i2,j1:j2,jl)
+            ptab(i1:i2,j1:j2,jm+1) = v_i_b  (i1:i2,j1:j2,jl)
+            ptab(i1:i2,j1:j2,jm+2) = v_s_b  (i1:i2,j1:j2,jl)
+            ptab(i1:i2,j1:j2,jm+3) = smv_i_b(i1:i2,j1:j2,jl)
+            ptab(i1:i2,j1:j2,jm+4) = oa_i_b (i1:i2,j1:j2,jl)
+            jm = jm + 5
             DO jk = 1, nlay_s
                ptab(i1:i2,j1:j2,jm) = e_s_b(i1:i2,j1:j2,jk,jl) ; jm = jm + 1
@@ -178,145 +178,173 @@
          
          DO jk = k1, k2
-            WHERE( tmask(i1:i2,j1:j2,1) == 0. )  ptab(i1:i2,j1:j2,jk) = -9999.
+            WHERE( tmask(i1:i2,j1:j2,1) == 0. )  ptab(i1:i2,j1:j2,jk) = Agrif_SpecialValue
          ENDDO
          
       ELSE               ! child grid
-!! ==> The easiest interpolation is the following commented lines
-         jm = 1
-         DO jl = 1, jpl
-            a_i  (i1:i2,j1:j2,jl) = ptab(i1:i2,j1:j2,jm) * tmask(i1:i2,j1:j2,1) ; jm = jm + 1
-            v_i  (i1:i2,j1:j2,jl) = ptab(i1:i2,j1:j2,jm) * tmask(i1:i2,j1:j2,1) ; jm = jm + 1
-            v_s  (i1:i2,j1:j2,jl) = ptab(i1:i2,j1:j2,jm) * tmask(i1:i2,j1:j2,1) ; jm = jm + 1
-            smv_i(i1:i2,j1:j2,jl) = ptab(i1:i2,j1:j2,jm) * tmask(i1:i2,j1:j2,1) ; jm = jm + 1
-            oa_i (i1:i2,j1:j2,jl) = ptab(i1:i2,j1:j2,jm) * tmask(i1:i2,j1:j2,1) ; jm = jm + 1
-            DO jk = 1, nlay_s
-               e_s(i1:i2,j1:j2,jk,jl) = ptab(i1:i2,j1:j2,jm) * tmask(i1:i2,j1:j2,1) ; jm = jm + 1
-            ENDDO
-            DO jk = 1, nlay_i
-               e_i(i1:i2,j1:j2,jk,jl) = ptab(i1:i2,j1:j2,jm) * tmask(i1:i2,j1:j2,1) ; jm = jm + 1
-            ENDDO
-         ENDDO
-
-!! ==> this is a more complex interpolation since we mix solutions over a couple of grid points
-!!     it is advised to use it for fields modified by high order schemes (e.g. advection UM5...)
-!!        clem: for some reason (I don't know why), the following lines do not work 
-!!              with mpp (or in realistic configurations?). It makes the model crash
-!         ! record ztab
-!         jm = 1
-!         DO jl = 1, jpl
-!            ztab(:,:,jm) = a_i  (:,:,jl) ; jm = jm + 1
-!            ztab(:,:,jm) = v_i  (:,:,jl) ; jm = jm + 1
-!            ztab(:,:,jm) = v_s  (:,:,jl) ; jm = jm + 1
-!            ztab(:,:,jm) = smv_i(:,:,jl) ; jm = jm + 1
-!            ztab(:,:,jm) = oa_i (:,:,jl) ; jm = jm + 1
-!            DO jk = 1, nlay_s
-!               ztab(:,:,jm) = e_s(:,:,jk,jl) ; jm = jm + 1
-!            ENDDO
-!            DO jk = 1, nlay_i
-!               ztab(:,:,jm) = e_i(:,:,jk,jl) ; jm = jm + 1
-!            ENDDO
-!         ENDDO
-!         !
-!         ! borders of the domain
-!         western_side  = (nb == 1).AND.(ndir == 1)  ;  eastern_side  = (nb == 1).AND.(ndir == 2)
-!         southern_side = (nb == 2).AND.(ndir == 1)  ;  northern_side = (nb == 2).AND.(ndir == 2)
-!         !
-!         ! spatial smoothing
-!         zrhox = Agrif_Rhox()
-!         z1 =      ( zrhox - 1. ) * 0.5 
-!         z3 =      ( zrhox - 1. ) / ( zrhox + 1. )
-!         z6 = 2. * ( zrhox - 1. ) / ( zrhox + 1. )
-!         z7 =    - ( zrhox - 1. ) / ( zrhox + 3. )
-!         z2 = 1. - z1
-!         z4 = 1. - z3
-!         z5 = 1. - z6 - z7
-!         !
-!         ! Remove corners
-!         imin = i1  ;  imax = i2  ;  jmin = j1  ;  jmax = j2
-!         !!clem2017 ghost
-!         ind1 =     nbghostcells
-!         ind2 = 1 + nbghostcells
-!         ind3 = 2 + nbghostcells
-!         IF( (nbondj == -1) .OR. (nbondj == 2) )   jmin = ind3
-!         IF( (nbondj == +1) .OR. (nbondj == 2) )   jmax = nlcj-ind2
-!         IF( (nbondi == -1) .OR. (nbondi == 2) )   imin = ind3
-!         IF( (nbondi == +1) .OR. (nbondi == 2) )   imax = nlci-ind2
-!         !!clem2017 ghost
-!
-!         ! smoothed fields
-!         IF( eastern_side ) THEN
-!            ztab(nlci,j1:j2,:) = z1 * ptab(nlci,j1:j2,:) + z2 * ptab(nlci-1,j1:j2,:)
-!            DO jj = jmin, jmax
-!               rswitch = 0.
-!               IF( u_ice(nlci-2,jj) > 0._wp ) rswitch = 1.
-!               ztab(nlci-1,jj,:) = ( 1. - umask(nlci-2,jj,1) ) * ztab(nlci,jj,:)  &
-!                  &                +      umask(nlci-2,jj,1)   *  &
-!                  &                ( ( 1. - rswitch ) * ( z4 * ztab(nlci,jj,:)   + z3 * ztab(nlci-2,jj,:) )  &
-!                  &                  +      rswitch   * ( z6 * ztab(nlci-2,jj,:) + z5 * ztab(nlci,jj,:) + z7 * ztab(nlci-3,jj,:) ) )
-!               ztab(nlci-1,jj,:) = ztab(nlci-1,jj,:) * tmask(nlci-1,jj,1)
-!            END DO
-!         ENDIF
-!         ! 
-!         IF( northern_side ) THEN
-!            ztab(i1:i2,nlcj,:) = z1 * ptab(i1:i2,nlcj,:) + z2 * ptab(i1:i2,nlcj-1,:)
-!            DO ji = imin, imax
-!               rswitch = 0.
-!               IF( v_ice(ji,nlcj-2) > 0._wp ) rswitch = 1.
-!               ztab(ji,nlcj-1,:) = ( 1. - vmask(ji,nlcj-2,1) ) * ztab(ji,nlcj,:)  &
-!                  &                +      vmask(ji,nlcj-2,1)   *  &
-!                  &                ( ( 1. - rswitch ) * ( z4 * ztab(ji,nlcj,:)   + z3 * ztab(ji,nlcj-2,:) ) &
-!                  &                  +      rswitch   * ( z6 * ztab(ji,nlcj-2,:) + z5 * ztab(ji,nlcj,:) + z7 * ztab(ji,nlcj-3,:) ) )
-!               ztab(ji,nlcj-1,:) = ztab(ji,nlcj-1,:) * tmask(ji,nlcj-1,1)
-!            END DO
-!         END IF
-!         !
-!         IF( western_side) THEN
-!            ztab(1,j1:j2,:) = z1 * ptab(1,j1:j2,:) + z2 * ptab(2,j1:j2,:)
-!            DO jj = jmin, jmax
-!               rswitch = 0.
-!               IF( u_ice(2,jj) < 0._wp ) rswitch = 1.
-!               ztab(2,jj,:) = ( 1. - umask(2,jj,1) ) * ztab(1,jj,:)  &
-!                  &           +      umask(2,jj,1)   *   &
-!                  &           ( ( 1. - rswitch ) * ( z4 * ztab(1,jj,:) + z3 * ztab(3,jj,:) ) &
-!                  &             +      rswitch   * ( z6 * ztab(3,jj,:) + z5 * ztab(1,jj,:) + z7 * ztab(4,jj,:) ) )
-!               ztab(2,jj,:) = ztab(2,jj,:) * tmask(2,jj,1)
-!            END DO
-!         ENDIF
-!         !
-!         IF( southern_side ) THEN
-!            ztab(i1:i2,1,:) = z1 * ptab(i1:i2,1,:) + z2 * ptab(i1:i2,2,:)
-!            DO ji = imin, imax
-!               rswitch = 0.
-!               IF( v_ice(ji,2) < 0._wp ) rswitch = 1.
-!               ztab(ji,2,:) = ( 1. - vmask(ji,2,1) ) * ztab(ji,1,:)  &
-!                  &           +      vmask(ji,2,1)   *  &
-!                  &           ( ( 1. - rswitch ) * ( z4 * ztab(ji,1,:) + z3 * ztab(ji,3,:) ) &
-!                  &             +      rswitch   * ( z6 * ztab(ji,3,:) + z5 * ztab(ji,1,:) + z7 * ztab(ji,4,:) ) )
-!               ztab(ji,2,:) = ztab(ji,2,:) * tmask(ji,2,1)
-!            END DO
-!         END IF
-!         !
-!         ! Treatment of corners
-!         IF( (eastern_side) .AND. ((nbondj == -1).OR.(nbondj == 2)) )  ztab(nlci-1,2,:)      = ptab(nlci-1,2,:)      ! East south
-!         IF( (eastern_side) .AND. ((nbondj ==  1).OR.(nbondj == 2)) )  ztab(nlci-1,nlcj-1,:) = ptab(nlci-1,nlcj-1,:) ! East north
-!         IF( (western_side) .AND. ((nbondj == -1).OR.(nbondj == 2)) )  ztab(2,2,:)           = ptab(2,2,:)           ! West south
-!         IF( (western_side) .AND. ((nbondj ==  1).OR.(nbondj == 2)) )  ztab(2,nlcj-1,:)      = ptab(2,nlcj-1,:)      ! West north
-!
-!         ! retrieve ice tracers
-!         jm = 1
-!         DO jl = 1, jpl
-!            a_i  (i1:i2,j1:j2,jl) = ztab(i1:i2,j1:j2,jm) ; jm = jm + 1
-!            v_i  (i1:i2,j1:j2,jl) = ztab(i1:i2,j1:j2,jm) ; jm = jm + 1
-!            v_s  (i1:i2,j1:j2,jl) = ztab(i1:i2,j1:j2,jm) ; jm = jm + 1
-!            smv_i(i1:i2,j1:j2,jl) = ztab(i1:i2,j1:j2,jm) ; jm = jm + 1
-!            oa_i (i1:i2,j1:j2,jl) = ztab(i1:i2,j1:j2,jm) ; jm = jm + 1
-!            DO jk = 1, nlay_s
-!               e_s(i1:i2,j1:j2,jk,jl) = ztab(i1:i2,j1:j2,jm) ; jm = jm + 1
-!            ENDDO
-!            DO jk = 1, nlay_i
-!               e_i(i1:i2,j1:j2,jk,jl) = ztab(i1:i2,j1:j2,jm) ; jm = jm + 1
-!            ENDDO
-!         ENDDO
-       
+
+         IF( nbghostcells > 1 ) THEN
+            !! ==> The easiest interpolation is the following lines
+
+            jm = 1
+            DO jl = 1, jpl
+               !
+               DO jj = j1, j2
+                  DO ji = i1, i2
+                     a_i  (ji,jj,jl) = ptab(ji,jj,jm  ) * tmask(ji,jj,1)
+                     v_i  (ji,jj,jl) = ptab(ji,jj,jm+1) * tmask(ji,jj,1)
+                     v_s  (ji,jj,jl) = ptab(ji,jj,jm+2) * tmask(ji,jj,1)
+                     smv_i(ji,jj,jl) = ptab(ji,jj,jm+3) * tmask(ji,jj,1)
+                     oa_i (ji,jj,jl) = ptab(ji,jj,jm+4) * tmask(ji,jj,1)
+                  ENDDO
+               ENDDO
+               jm = jm + 5
+               !
+               DO jk = 1, nlay_s
+                  e_s(i1:i2,j1:j2,jk,jl) = ptab(:,:,jm) * tmask(i1:i2,j1:j2,1)
+                  jm = jm + 1
+               ENDDO
+               !
+               DO jk = 1, nlay_i
+                  e_i(i1:i2,j1:j2,jk,jl) = ptab(:,:,jm) * tmask(i1:i2,j1:j2,1)
+                  jm = jm + 1
+               ENDDO
+               !
+            ENDDO
+            
+         ELSE
+            !! ==> this is a more complex interpolation since we mix solutions over a couple of grid points
+            !!     it is advised to use it for fields modified by high order schemes (e.g. advection UM5...)
+            !!        clem: for some reason (I don't know why), the following lines do not work 
+            !!              with mpp (or in realistic configurations?). It makes the model crash
+            !      I think there is an issue with Agrif_SpecialValue here (not taken into account properly)
+            ! record ztab
+            jm = 1
+            DO jl = 1, jpl
+               ztab(:,:,jm  ) = a_i  (:,:,jl)
+               ztab(:,:,jm+1) = v_i  (:,:,jl)
+               ztab(:,:,jm+2) = v_s  (:,:,jl)
+               ztab(:,:,jm+3) = smv_i(:,:,jl)
+               ztab(:,:,jm+4) = oa_i (:,:,jl)
+               jm = jm + 5
+               DO jk = 1, nlay_s
+                  ztab(:,:,jm) = e_s(:,:,jk,jl)
+                  jm = jm + 1
+               ENDDO
+               DO jk = 1, nlay_i
+                  ztab(:,:,jm) = e_i(:,:,jk,jl)
+                  jm = jm + 1
+               ENDDO
+               !
+            ENDDO
+            !
+            ! borders of the domain
+            western_side  = (nb == 1).AND.(ndir == 1)  ;  eastern_side  = (nb == 1).AND.(ndir == 2)
+            southern_side = (nb == 2).AND.(ndir == 1)  ;  northern_side = (nb == 2).AND.(ndir == 2)
+            !
+            ! spatial smoothing
+            zrhox = Agrif_Rhox()
+            z1 =      ( zrhox - 1. ) * 0.5 
+            z3 =      ( zrhox - 1. ) / ( zrhox + 1. )
+            z6 = 2. * ( zrhox - 1. ) / ( zrhox + 1. )
+            z7 =    - ( zrhox - 1. ) / ( zrhox + 3. )
+            z2 = 1. - z1
+            z4 = 1. - z3
+            z5 = 1. - z6 - z7
+            !
+            ! Remove corners
+            imin = i1  ;  imax = i2  ;  jmin = j1  ;  jmax = j2
+            IF( (nbondj == -1) .OR. (nbondj == 2) )   jmin = 3
+            IF( (nbondj == +1) .OR. (nbondj == 2) )   jmax = nlcj-2
+            IF( (nbondi == -1) .OR. (nbondi == 2) )   imin = 3
+            IF( (nbondi == +1) .OR. (nbondi == 2) )   imax = nlci-2
+
+            ! smoothed fields
+            IF( eastern_side ) THEN
+               ztab(nlci,j1:j2,:) = z1 * ptab(nlci,j1:j2,:) + z2 * ptab(nlci-1,j1:j2,:)
+               DO jj = jmin, jmax
+                  rswitch = 0.
+                  IF( u_ice(nlci-2,jj) > 0._wp ) rswitch = 1.
+                  ztab(nlci-1,jj,:) = ( 1. - umask(nlci-2,jj,1) ) * ztab(nlci,jj,:)  &
+                     &                +      umask(nlci-2,jj,1)   *  &
+                     &                ( ( 1. - rswitch ) * ( z4 * ztab(nlci,jj,:)   + z3 * ztab(nlci-2,jj,:) )  &
+                     &                  +      rswitch   * ( z6 * ztab(nlci-2,jj,:) + z5 * ztab(nlci,jj,:) + z7 * ztab(nlci-3,jj,:) ) )
+                  ztab(nlci-1,jj,:) = ztab(nlci-1,jj,:) * tmask(nlci-1,jj,1)
+               END DO
+            ENDIF
+            ! 
+            IF( northern_side ) THEN
+               ztab(i1:i2,nlcj,:) = z1 * ptab(i1:i2,nlcj,:) + z2 * ptab(i1:i2,nlcj-1,:)
+               DO ji = imin, imax
+                  rswitch = 0.
+                  IF( v_ice(ji,nlcj-2) > 0._wp ) rswitch = 1.
+                  ztab(ji,nlcj-1,:) = ( 1. - vmask(ji,nlcj-2,1) ) * ztab(ji,nlcj,:)  &
+                     &                +      vmask(ji,nlcj-2,1)   *  &
+                     &                ( ( 1. - rswitch ) * ( z4 * ztab(ji,nlcj,:)   + z3 * ztab(ji,nlcj-2,:) ) &
+                     &                  +      rswitch   * ( z6 * ztab(ji,nlcj-2,:) + z5 * ztab(ji,nlcj,:) + z7 * ztab(ji,nlcj-3,:) ) )
+                  ztab(ji,nlcj-1,:) = ztab(ji,nlcj-1,:) * tmask(ji,nlcj-1,1)
+               END DO
+            END IF
+            !
+            IF( western_side) THEN
+               ztab(1,j1:j2,:) = z1 * ptab(1,j1:j2,:) + z2 * ptab(2,j1:j2,:)
+               DO jj = jmin, jmax
+                  rswitch = 0.
+                  IF( u_ice(2,jj) < 0._wp ) rswitch = 1.
+                  ztab(2,jj,:) = ( 1. - umask(2,jj,1) ) * ztab(1,jj,:)  &
+                     &           +      umask(2,jj,1)   *   &
+                     &           ( ( 1. - rswitch ) * ( z4 * ztab(1,jj,:) + z3 * ztab(3,jj,:) ) &
+                     &             +      rswitch   * ( z6 * ztab(3,jj,:) + z5 * ztab(1,jj,:) + z7 * ztab(4,jj,:) ) )
+                  ztab(2,jj,:) = ztab(2,jj,:) * tmask(2,jj,1)
+               END DO
+            ENDIF
+            !
+            IF( southern_side ) THEN
+               ztab(i1:i2,1,:) = z1 * ptab(i1:i2,1,:) + z2 * ptab(i1:i2,2,:)
+               DO ji = imin, imax
+                  rswitch = 0.
+                  IF( v_ice(ji,2) < 0._wp ) rswitch = 1.
+                  ztab(ji,2,:) = ( 1. - vmask(ji,2,1) ) * ztab(ji,1,:)  &
+                     &           +      vmask(ji,2,1)   *  &
+                     &           ( ( 1. - rswitch ) * ( z4 * ztab(ji,1,:) + z3 * ztab(ji,3,:) ) &
+                     &             +      rswitch   * ( z6 * ztab(ji,3,:) + z5 * ztab(ji,1,:) + z7 * ztab(ji,4,:) ) )
+                  ztab(ji,2,:) = ztab(ji,2,:) * tmask(ji,2,1)
+               END DO
+            END IF
+            !
+            ! Treatment of corners
+            IF( (eastern_side) .AND. ((nbondj == -1).OR.(nbondj == 2)) )  ztab(nlci-1,2,:)      = ptab(nlci-1,2,:)      ! East south
+            IF( (eastern_side) .AND. ((nbondj ==  1).OR.(nbondj == 2)) )  ztab(nlci-1,nlcj-1,:) = ptab(nlci-1,nlcj-1,:) ! East north
+            IF( (western_side) .AND. ((nbondj == -1).OR.(nbondj == 2)) )  ztab(2,2,:)           = ptab(2,2,:)           ! West south
+            IF( (western_side) .AND. ((nbondj ==  1).OR.(nbondj == 2)) )  ztab(2,nlcj-1,:)      = ptab(2,nlcj-1,:)      ! West north
+            
+            ! retrieve ice tracers
+            jm = 1
+            DO jl = 1, jpl
+               !
+               DO jj = j1, j2
+                  DO ji = i1, i2
+                     a_i  (ji,jj,jl) = ztab(ji,jj,jm  ) * tmask(ji,jj,1)
+                     v_i  (ji,jj,jl) = ztab(ji,jj,jm+1) * tmask(ji,jj,1)
+                     v_s  (ji,jj,jl) = ztab(ji,jj,jm+2) * tmask(ji,jj,1)
+                     smv_i(ji,jj,jl) = ztab(ji,jj,jm+3) * tmask(ji,jj,1)
+                     oa_i (ji,jj,jl) = ztab(ji,jj,jm+4) * tmask(ji,jj,1)
+                  ENDDO
+               ENDDO
+               jm = jm + 5
+               !
+               DO jk = 1, nlay_s
+                  e_s(i1:i2,j1:j2,jk,jl) = ztab(i1:i2,j1:j2,jm) * tmask(i1:i2,j1:j2,1)
+                  jm = jm + 1
+               ENDDO
+               !
+               DO jk = 1, nlay_i
+                  e_i(i1:i2,j1:j2,jk,jl) = ztab(i1:i2,j1:j2,jm) * tmask(i1:i2,j1:j2,1)
+                  jm = jm + 1
+               ENDDO
+               !
+            ENDDO
+          
+         ENDIF  ! nbghostcells=1
+         
          ! integrated values
          vt_i (i1:i2,j1:j2) = SUM( v_i(i1:i2,j1:j2,:), dim=3 )
@@ -325,8 +353,9 @@
          et_s(i1:i2,j1:j2)  = SUM( SUM( e_s(i1:i2,j1:j2,:,:), dim=4 ), dim=3 )
          et_i(i1:i2,j1:j2)  = SUM( SUM( e_i(i1:i2,j1:j2,:,:), dim=4 ), dim=3 )
-
+         
       ENDIF
+
+      DEALLOCATE( ztab )
       
-      DEALLOCATE( ztab )
       !
    END SUBROUTINE interp_tra_ice
Index: /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/NST_SRC/agrif_lim3_update.F90
===================================================================
--- /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/NST_SRC/agrif_lim3_update.F90	(revision 8188)
+++ /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/NST_SRC/agrif_lim3_update.F90	(revision 8189)
@@ -58,6 +58,6 @@
       IF( nn_ice == 0 ) RETURN   ! clem2017: do not update if child domain does not have ice
       !
+      Agrif_SpecialValueFineGrid = -9999.
       Agrif_UseSpecialValueInUpdate = .TRUE.
-      Agrif_SpecialValueFineGrid = -9999.
 # if defined TWO_WAY
       IF( MOD(nbcline,nbclineupdate) == 0) THEN ! update the whole basin at each nbclineupdate (=nn_cln_update) baroclinic parent time steps
@@ -72,4 +72,5 @@
       ENDIF
 # endif
+      Agrif_SpecialValueFineGrid = 0.
       Agrif_UseSpecialValueInUpdate = .FALSE.
       !
@@ -90,5 +91,5 @@
       LOGICAL , INTENT(in) :: before
       !!
-      INTEGER  :: jk, jl, jm
+      INTEGER  :: ji, jj, jk, jl, jm
       !!-----------------------------------------------------------------------
       ! it is ok not to multiply by e1*e2 since we conserve tracers here (same as in the ocean).
@@ -96,37 +97,56 @@
          jm = 1
          DO jl = 1, jpl
-            ptab(:,:,jm) = a_i  (i1:i2,j1:j2,jl) ; jm = jm + 1
-            ptab(:,:,jm) = v_i  (i1:i2,j1:j2,jl) ; jm = jm + 1
-            ptab(:,:,jm) = v_s  (i1:i2,j1:j2,jl) ; jm = jm + 1
-            ptab(:,:,jm) = smv_i(i1:i2,j1:j2,jl) ; jm = jm + 1
-            ptab(:,:,jm) = oa_i (i1:i2,j1:j2,jl) ; jm = jm + 1
+            ptab(i1:i2,j1:j2,jm  ) = a_i  (i1:i2,j1:j2,jl)
+            ptab(i1:i2,j1:j2,jm+1) = v_i  (i1:i2,j1:j2,jl)
+            ptab(i1:i2,j1:j2,jm+2) = v_s  (i1:i2,j1:j2,jl)
+            ptab(i1:i2,j1:j2,jm+3) = smv_i(i1:i2,j1:j2,jl)
+            ptab(i1:i2,j1:j2,jm+4) = oa_i (i1:i2,j1:j2,jl)
+            jm = jm + 5
             DO jk = 1, nlay_s
-               ptab(:,:,jm) = e_s(i1:i2,j1:j2,jk,jl) ; jm = jm + 1
+               ptab(i1:i2,j1:j2,jm) = e_s(i1:i2,j1:j2,jk,jl) ; jm = jm + 1
             ENDDO
             DO jk = 1, nlay_i
-               ptab(:,:,jm) = e_i(i1:i2,j1:j2,jk,jl) ; jm = jm + 1
+               ptab(i1:i2,j1:j2,jm) = e_i(i1:i2,j1:j2,jk,jl) ; jm = jm + 1
             ENDDO
          ENDDO
-
+         !
          DO jk = k1, k2
-            WHERE( tmask(i1:i2,j1:j2,1) == 0. )  ptab(:,:,jk) = -9999.
+            WHERE( tmask(i1:i2,j1:j2,1) == 0. )  ptab(i1:i2,j1:j2,jk) = Agrif_SpecialValueFineGrid 
          ENDDO
-                  
+         !
       ELSE
+         !
          jm = 1
          DO jl = 1, jpl
-            a_i  (i1:i2,j1:j2,jl) = ptab(:,:,jm) * tmask(i1:i2,j1:j2,1) ; jm = jm + 1
-            v_i  (i1:i2,j1:j2,jl) = ptab(:,:,jm) * tmask(i1:i2,j1:j2,1) ; jm = jm + 1
-            v_s  (i1:i2,j1:j2,jl) = ptab(:,:,jm) * tmask(i1:i2,j1:j2,1) ; jm = jm + 1
-            smv_i(i1:i2,j1:j2,jl) = ptab(:,:,jm) * tmask(i1:i2,j1:j2,1) ; jm = jm + 1
-            oa_i (i1:i2,j1:j2,jl) = ptab(:,:,jm) * tmask(i1:i2,j1:j2,1) ; jm = jm + 1
+            !
+            DO jj = j1, j2
+               DO ji = i1, i2
+                  IF( ptab(ji,jj,jm) /= Agrif_SpecialValueFineGrid ) THEN
+                     a_i  (ji,jj,jl) = ptab(ji,jj,jm  ) * tmask(ji,jj,1)
+                     v_i  (ji,jj,jl) = ptab(ji,jj,jm+1) * tmask(ji,jj,1)
+                     v_s  (ji,jj,jl) = ptab(ji,jj,jm+2) * tmask(ji,jj,1)
+                     smv_i(ji,jj,jl) = ptab(ji,jj,jm+3) * tmask(ji,jj,1)
+                     oa_i (ji,jj,jl) = ptab(ji,jj,jm+4) * tmask(ji,jj,1)
+                  ENDIF
+               ENDDO
+            ENDDO
+            jm = jm + 5
+            !
             DO jk = 1, nlay_s
-               e_s(i1:i2,j1:j2,jk,jl) = ptab(:,:,jm) * tmask(i1:i2,j1:j2,1) ; jm = jm + 1
-            ENDDO
+               WHERE( ptab(i1:i2,j1:j2,jm) /= Agrif_SpecialValueFineGrid )
+                  e_s(i1:i2,j1:j2,jk,jl) = ptab(i1:i2,j1:j2,jm) * tmask(i1:i2,j1:j2,1)
+               ENDWHERE
+               jm = jm + 1
+            ENDDO
+            !
             DO jk = 1, nlay_i
-               e_i(i1:i2,j1:j2,jk,jl) = ptab(:,:,jm) * tmask(i1:i2,j1:j2,1) ; jm = jm + 1
-            ENDDO
+               WHERE( ptab(i1:i2,j1:j2,jm) /= Agrif_SpecialValueFineGrid )
+                  e_i(i1:i2,j1:j2,jk,jl) = ptab(i1:i2,j1:j2,jm) * tmask(i1:i2,j1:j2,1)
+               ENDWHERE
+               jm = jm + 1
+            ENDDO
+            !
          ENDDO
-
+         !
          ! integrated values
          vt_i (i1:i2,j1:j2) = SUM( v_i(i1:i2,j1:j2,:), dim=3 )
@@ -156,7 +176,9 @@
          zrhoy = Agrif_Rhoy()
          ptab(:,:) = e2u(i1:i2,j1:j2) * u_ice(i1:i2,j1:j2) * zrhoy
-         WHERE( umask(i1:i2,j1:j2,1) == 0. )  ptab(:,:) = -9999.
+         WHERE( umask(i1:i2,j1:j2,1) == 0. )  ptab(:,:) = Agrif_SpecialValueFineGrid
       ELSE
-         u_ice(i1:i2,j1:j2) = ptab(:,:) / e2u(i1:i2,j1:j2) * umask(i1:i2,j1:j2,1)
+         WHERE( ptab(i1:i2,j1:j2) /= Agrif_SpecialValueFineGrid )
+            u_ice(i1:i2,j1:j2) = ptab(i1:i2,j1:j2) / e2u(i1:i2,j1:j2) * umask(i1:i2,j1:j2,1)
+         ENDWHERE
       ENDIF
       ! 
@@ -179,7 +201,9 @@
          zrhox = Agrif_Rhox()
          ptab(:,:) = e1v(i1:i2,j1:j2) * v_ice(i1:i2,j1:j2) * zrhox
-         WHERE( vmask(i1:i2,j1:j2,1) == 0. )  ptab(:,:) = -9999.
+         WHERE( vmask(i1:i2,j1:j2,1) == 0. )  ptab(:,:) = Agrif_SpecialValueFineGrid
       ELSE
-         v_ice(i1:i2,j1:j2) = ptab(:,:) / e1v(i1:i2,j1:j2) * vmask(i1:i2,j1:j2,1)
+         WHERE( ptab(i1:i2,j1:j2) /= Agrif_SpecialValueFineGrid )
+            v_ice(i1:i2,j1:j2) = ptab(i1:i2,j1:j2) / e1v(i1:i2,j1:j2) * vmask(i1:i2,j1:j2,1)
+         ENDWHERE
       ENDIF
       !
Index: /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/NST_SRC/agrif_opa_interp.F90
===================================================================
--- /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/NST_SRC/agrif_opa_interp.F90	(revision 8188)
+++ /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/NST_SRC/agrif_opa_interp.F90	(revision 8189)
@@ -104,12 +104,12 @@
          !
          IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
-            ua_b(1:1+nbghostcells,:) = 0._wp
+            ua_b(2:1+nbghostcells,:) = 0._wp
             DO jk = 1, jpkm1
                DO jj = 1, jpj
-                  ua_b(1:1+nbghostcells,jj) = ua_b(1:1+nbghostcells,jj) + e3u_a(1:1+nbghostcells,jj,jk) * ua(1:1+nbghostcells,jj,jk)
+                  ua_b(2:1+nbghostcells,jj) = ua_b(2:1+nbghostcells,jj) + e3u_a(2:1+nbghostcells,jj,jk) * ua(2:1+nbghostcells,jj,jk)
                END DO
             END DO
             DO jj = 1, jpj
-               ua_b(1:1+nbghostcells,jj) = ua_b(1:1+nbghostcells,jj) * r1_hu_a(1:1+nbghostcells,jj)            
+               ua_b(2:1+nbghostcells,jj) = ua_b(2:1+nbghostcells,jj) * r1_hu_a(2:1+nbghostcells,jj)
             END DO
          ENDIF
@@ -166,13 +166,13 @@
 
          IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
-            ua_b(nlci-nbghostcells-1:nlci,:) = 0._wp
+            ua_b(nlci-nbghostcells-1:nlci-2,:) = 0._wp
             DO jk=1,jpkm1
                DO jj=1,jpj
-                  ua_b(nlci-nbghostcells-1:nlci,jj) = ua_b(nlci-nbghostcells-1:nlci,jj) + e3u_a(nlci-nbghostcells-1:nlci,jj,jk)  &
-                     &                                                                     * ua(nlci-nbghostcells-1:nlci,jj,jk)
+                  ua_b(nlci-nbghostcells-1:nlci-2,jj) = ua_b(nlci-nbghostcells-1:nlci-2,jj) + e3u_a(nlci-nbghostcells-1:nlci-2,jj,jk)  &
+                     &                                                                         * ua(nlci-nbghostcells-1:nlci-2,jj,jk)
                END DO
             END DO
             DO jj=1,jpj
-               ua_b(nlci-nbghostcells-1:nlci,jj) = ua_b(nlci-nbghostcells-1:nlci,jj) * r1_hu_a(nlci-nbghostcells-1:nlci,jj)            
+               ua_b(nlci-nbghostcells-1:nlci-2,jj) = ua_b(nlci-nbghostcells-1:nlci-2,jj) * r1_hu_a(nlci-nbghostcells-1:nlci-2,jj) 
             END DO
          ENDIF
@@ -229,12 +229,12 @@
 
          IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
-            va_b(:,1:nbghostcells+1) = 0._wp
+            va_b(:,2:nbghostcells+1) = 0._wp
             DO jk = 1, jpkm1
                DO ji = 1, jpi
-                  va_b(ji,1:nbghostcells+1) = va_b(ji,1:nbghostcells+1) + e3v_a(ji,1:nbghostcells+1,jk) * va(ji,1:nbghostcells+1,jk)
+                  va_b(ji,2:nbghostcells+1) = va_b(ji,2:nbghostcells+1) + e3v_a(ji,2:nbghostcells+1,jk) * va(ji,2:nbghostcells+1,jk)
                END DO
             END DO
             DO ji=1,jpi
-               va_b(ji,1:nbghostcells+1) = va_b(ji,1:nbghostcells+1) * r1_hv_a(ji,1:nbghostcells+1)            
+               va_b(ji,2:nbghostcells+1) = va_b(ji,2:nbghostcells+1) * r1_hv_a(ji,2:nbghostcells+1)
             END DO
          ENDIF
@@ -291,13 +291,13 @@
          !
          IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
-            va_b(:,nlcj-nbghostcells-1:nlcj) = 0._wp
+            va_b(:,nlcj-nbghostcells-1:nlcj-2) = 0._wp
             DO jk = 1, jpkm1
                DO ji = 1, jpi
-                  va_b(ji,nlcj-nbghostcells-1:nlcj) = va_b(ji,nlcj-nbghostcells-1:nlcj) + e3v_a(ji,nlcj-nbghostcells-1:nlcj,jk)  &
-                     &                                                                     * va(ji,nlcj-nbghostcells-1:nlcj,jk)
+                  va_b(ji,nlcj-nbghostcells-1:nlcj-2) = va_b(ji,nlcj-nbghostcells-1:nlcj-2) + e3v_a(ji,nlcj-nbghostcells-1:nlcj-2,jk)  &
+                     &                                                                         * va(ji,nlcj-nbghostcells-1:nlcj-2,jk)
                END DO
             END DO
             DO ji = 1, jpi
-               va_b(ji,nlcj-nbghostcells-1:nlcj) = va_b(ji,nlcj-nbghostcells-1:nlcj) * r1_hv_a(ji,nlcj-nbghostcells-1:nlcj)            
+               va_b(ji,nlcj-nbghostcells-1:nlcj-2) = va_b(ji,nlcj-nbghostcells-1:nlcj-2) * r1_hv_a(ji,nlcj-nbghostcells-1:nlcj-2)
             END DO
          ENDIF
@@ -369,10 +369,12 @@
       IF((nbondi == -1).OR.(nbondi == 2)) THEN
          DO jj=1,jpj
-            va_e(1:nbghostcells+1,jj) = vbdy_w(jj) * hvr_e(1:nbghostcells+1,jj)
-            ! Specified fluxes:
-            ua_e(1:nbghostcells+1,jj) = ubdy_w(jj) * hur_e(1:nbghostcells+1,jj)
-            ! Characteristics method (only if ghostcells=1):
-            !alt            ua_e(2,jj) = 0.5_wp * ( ubdy_w(jj) * hur_e(2,jj) + ua_e(3,jj) &
-            !alt                       &           - sqrt(grav * hur_e(2,jj)) * (sshn_e(3,jj) - hbdy_w(jj)) )
+            IF( vmask(2,jj,1) == 1._wp ) THEN !clem: avoid interpolation if coast at the boundary
+               va_e(2:nbghostcells+1,jj) = vbdy_w(jj) * hvr_e(2:nbghostcells+1,jj)
+               ! Specified fluxes:
+               ua_e(2:nbghostcells+1,jj) = ubdy_w(jj) * hur_e(2:nbghostcells+1,jj)
+               ! Characteristics method (only if ghostcells=1):
+               !alt            ua_e(2,jj) = 0.5_wp * ( ubdy_w(jj) * hur_e(2,jj) + ua_e(3,jj) &
+               !alt                       &           - sqrt(grav * hur_e(2,jj)) * (sshn_e(3,jj) - hbdy_w(jj)) )
+            ENDIF
          END DO
       ENDIF
@@ -380,10 +382,12 @@
       IF((nbondi == 1).OR.(nbondi == 2)) THEN
          DO jj=1,jpj
-            va_e(nlci-nbghostcells:nlci,jj)     = vbdy_e(jj) * hvr_e(nlci-nbghostcells:nlci,jj)
-            ! Specified fluxes:
-            ua_e(nlci-nbghostcells-1:nlci-1,jj) = ubdy_e(jj) * hur_e(nlci-nbghostcells-1:nlci-1,jj)
-            ! Characteristics method (only if ghostcells=1):
-            !alt            ua_e(nlci-2,jj) = 0.5_wp * ( ubdy_e(jj) * hur_e(nlci-2,jj) + ua_e(nlci-3,jj) &
-            !alt                            &           + sqrt(grav * hur_e(nlci-2,jj)) * (sshn_e(nlci-2,jj) - hbdy_e(jj)) )
+            IF( vmask(nlci-1,jj,1) == 1._wp ) THEN !clem: avoid interpolation if coast at the boundary
+               va_e(nlci-nbghostcells:nlci-1,jj)   = vbdy_e(jj) * hvr_e(nlci-nbghostcells:nlci-1,jj)
+               ! Specified fluxes:
+               ua_e(nlci-nbghostcells-1:nlci-2,jj) = ubdy_e(jj) * hur_e(nlci-nbghostcells-1:nlci-2,jj)
+               ! Characteristics method (only if ghostcells=1):
+               !alt            ua_e(nlci-2,jj) = 0.5_wp * ( ubdy_e(jj) * hur_e(nlci-2,jj) + ua_e(nlci-3,jj) &
+               !alt                            &           + sqrt(grav * hur_e(nlci-2,jj)) * (sshn_e(nlci-2,jj) - hbdy_e(jj)) )
+            ENDIF
          END DO
       ENDIF
@@ -391,10 +395,12 @@
       IF((nbondj == -1).OR.(nbondj == 2)) THEN
          DO ji=1,jpi
-            ua_e(ji,1:nbghostcells+1) = ubdy_s(ji) * hur_e(ji,1:nbghostcells+1)
-            ! Specified fluxes:
-            va_e(ji,1:nbghostcells+1) = vbdy_s(ji) * hvr_e(ji,1:nbghostcells+1)
-            ! Characteristics method (only if ghostcells=1):
-            !alt            va_e(ji,2) = 0.5_wp * ( vbdy_s(ji) * hvr_e(ji,2) + va_e(ji,3) &
-            !alt                       &           - sqrt(grav * hvr_e(ji,2)) * (sshn_e(ji,3) - hbdy_s(ji)) )
+            IF( umask(ji,2,1) == 1._wp ) THEN !clem: avoid interpolation if coast at the boundary
+               ua_e(ji,2:nbghostcells+1) = ubdy_s(ji) * hur_e(ji,2:nbghostcells+1)
+               ! Specified fluxes:
+               va_e(ji,2:nbghostcells+1) = vbdy_s(ji) * hvr_e(ji,2:nbghostcells+1)
+               ! Characteristics method (only if ghostcells=1):
+               !alt            va_e(ji,2) = 0.5_wp * ( vbdy_s(ji) * hvr_e(ji,2) + va_e(ji,3) &
+               !alt                       &           - sqrt(grav * hvr_e(ji,2)) * (sshn_e(ji,3) - hbdy_s(ji)) )
+            ENDIF
          END DO
       ENDIF
@@ -402,10 +408,12 @@
       IF((nbondj == 1).OR.(nbondj == 2)) THEN
          DO ji=1,jpi
-            ua_e(ji,nlcj-nbghostcells:nlcj)     = ubdy_n(ji) * hur_e(ji,nlcj-nbghostcells:nlcj)
-            ! Specified fluxes:
-            va_e(ji,nlcj-nbghostcells-1:nlcj-1) = vbdy_n(ji) * hvr_e(ji,nlcj-nbghostcells-1:nlcj-1)
-            ! Characteristics method (only if ghostcells=1):
-            !alt            va_e(ji,nlcj-2) = 0.5_wp * ( vbdy_n(ji) * hvr_e(ji,nlcj-2)  + va_e(ji,nlcj-3) &
-            !alt                            &           + sqrt(grav * hvr_e(ji,nlcj-2)) * (sshn_e(ji,nlcj-2) - hbdy_n(ji)) )
+            IF( umask(ji,nlcj-1,1) == 1._wp ) THEN !clem: avoid interpolation if coast at the boundary
+               ua_e(ji,nlcj-nbghostcells:nlcj-1)   = ubdy_n(ji) * hur_e(ji,nlcj-nbghostcells:nlcj-1)
+               ! Specified fluxes:
+               va_e(ji,nlcj-nbghostcells-1:nlcj-2) = vbdy_n(ji) * hvr_e(ji,nlcj-nbghostcells-1:nlcj-2)
+               ! Characteristics method (only if ghostcells=1):
+               !alt            va_e(ji,nlcj-2) = 0.5_wp * ( vbdy_n(ji) * hvr_e(ji,nlcj-2)  + va_e(ji,nlcj-3) &
+               !alt                            &           + sqrt(grav * hvr_e(ji,nlcj-2)) * (sshn_e(ji,nlcj-2) - hbdy_n(ji)) )
+            ENDIF
          END DO
       ENDIF
@@ -488,7 +496,9 @@
          indx = 1+nbghostcells
          DO jj = 1, jpj
-            DO ji = 1, indx
-               ssha(ji,jj)=ssha(indx+1,jj)
-               sshn(ji,jj)=sshn(indx+1,jj)
+            DO ji = 2, indx
+               IF( tmask(2,jj,1) == 1._wp ) THEN !clem: avoid interpolation if coast at the boundary
+                  ssha(ji,jj)=ssha(indx+1,jj)
+                  sshn(ji,jj)=sshn(indx+1,jj)
+               ENDIF
             ENDDO
          ENDDO
@@ -499,7 +509,9 @@
          indx = nlci-nbghostcells
          DO jj = 1, jpj
-            DO ji = indx, nlci
-               ssha(ji,jj)=ssha(indx-1,jj)
-               sshn(ji,jj)=sshn(indx-1,jj)
+            DO ji = indx, nlci-1
+               IF( tmask(nlci-1,jj,1) == 1._wp ) THEN !clem: avoid interpolation if coast at the boundary
+                  ssha(ji,jj)=ssha(indx-1,jj)
+                  sshn(ji,jj)=sshn(indx-1,jj)
+               ENDIF
             ENDDO
          ENDDO
@@ -509,8 +521,10 @@
       IF((nbondj == -1).OR.(nbondj == 2)) THEN
          indx = 1+nbghostcells
-         DO jj = 1, indx
+         DO jj = 2, indx
             DO ji = 1, jpi
-               ssha(ji,jj)=ssha(ji,indx+1)
-               sshn(ji,jj)=sshn(ji,indx+1)
+               IF( tmask(ji,2,1) == 1._wp ) THEN !clem: avoid interpolation if coast at the boundary
+                  ssha(ji,jj)=ssha(ji,indx+1)
+                  sshn(ji,jj)=sshn(ji,indx+1)
+               ENDIF
             ENDDO
          ENDDO
@@ -520,8 +534,10 @@
       IF((nbondj == 1).OR.(nbondj == 2)) THEN
          indx = nlcj-nbghostcells
-         DO jj = indx, nlcj
+         DO jj = indx, nlcj-1
             DO ji = 1, jpi
-               ssha(ji,jj)=ssha(ji,indx-1)
-               sshn(ji,jj)=sshn(ji,indx-1)
+               IF( tmask(ji,nlcj-1,1) == 1._wp ) THEN !clem: avoid interpolation if coast at the boundary
+                  ssha(ji,jj)=ssha(ji,indx-1)
+                  sshn(ji,jj)=sshn(ji,indx-1)
+               ENDIF
             ENDDO
          ENDDO
@@ -542,5 +558,7 @@
       IF((nbondi == -1).OR.(nbondi == 2)) THEN
          DO jj = 1, jpj
-            ssha_e(1:nbghostcells+1,jj) = hbdy_w(jj)
+            IF( tmask(2,jj,1) == 1._wp ) THEN !clem: avoid interpolation if coast at the boundary
+               ssha_e(2:nbghostcells+1,jj) = hbdy_w(jj)
+            ENDIF
          END DO
       ENDIF
@@ -548,5 +566,7 @@
       IF((nbondi == 1).OR.(nbondi == 2)) THEN
          DO jj = 1, jpj
-            ssha_e(nlci-nbghostcells:nlci,jj) = hbdy_e(jj)
+            IF( tmask(nlci-1,jj,1) == 1._wp ) THEN !clem: avoid interpolation if coast at the boundary
+               ssha_e(nlci-nbghostcells:nlci-1,jj) = hbdy_e(jj)
+            ENDIF
          END DO
       ENDIF
@@ -554,5 +574,7 @@
       IF((nbondj == -1).OR.(nbondj == 2)) THEN
          DO ji = 1, jpi
-            ssha_e(ji,1:nbghostcells+1) = hbdy_s(ji)
+            IF( tmask(ji,2,1) == 1._wp ) THEN !clem: avoid interpolation if coast at the boundary
+               ssha_e(ji,2:nbghostcells+1) = hbdy_s(ji)
+            ENDIF
          END DO
       ENDIF
@@ -560,5 +582,7 @@
       IF((nbondj == 1).OR.(nbondj == 2)) THEN
          DO ji = 1, jpi
-            ssha_e(ji,nlcj-nbghostcells:nlcj) = hbdy_n(ji)
+            IF( tmask(ji,nlcj-1,1) == 1._wp ) THEN !clem: avoid interpolation if coast at the boundary
+               ssha_e(ji,nlcj-nbghostcells:nlcj-1) = hbdy_n(ji)
+            ENDIF
          END DO
       ENDIF
@@ -608,10 +632,10 @@
       ELSE
          !
+         western_side  = (nb == 1).AND.(ndir == 1)  ;  eastern_side  = (nb == 1).AND.(ndir == 2)
+         southern_side = (nb == 2).AND.(ndir == 1)  ;  northern_side = (nb == 2).AND.(ndir == 2)
+         !
          IF( nbghostcells > 1 ) THEN  ! no smoothing
             tsa(i1:i2,j1:j2,k1:k2,n1:n2) = ptab(i1:i2,j1:j2,k1:k2,n1:n2)
          ELSE                         ! smoothing
-            !
-            western_side  = (nb == 1).AND.(ndir == 1)  ;  eastern_side  = (nb == 1).AND.(ndir == 2)
-            southern_side = (nb == 2).AND.(ndir == 1)  ;  northern_side = (nb == 2).AND.(ndir == 2)
             !
             zrhox = Agrif_Rhox()
@@ -744,8 +768,8 @@
          northern_side = (nb == 2).AND.(ndir == 2)
          !! clem ghost
-         IF(western_side)  hbdy_w(j1:j2) = ptab(i2,j1:j2) * tmask(i1,j1:j2,1)
-         IF(eastern_side)  hbdy_e(j1:j2) = ptab(i2,j1:j2) * tmask(i2,j1:j2,1) !clem previously i1
+         IF(western_side)  hbdy_w(j1:j2) = ptab(i2,j1:j2) * tmask(i2,j1:j2,1)
+         IF(eastern_side)  hbdy_e(j1:j2) = ptab(i1,j1:j2) * tmask(i1,j1:j2,1) !clem previously i1
          IF(southern_side) hbdy_s(i1:i2) = ptab(i1:i2,j2) * tmask(i1:i2,j2,1) !clem previously j1
-         IF(northern_side) hbdy_n(i1:i2) = ptab(i1:i2,j2) * tmask(i1:i2,j1,1)
+         IF(northern_side) hbdy_n(i1:i2) = ptab(i1:i2,j1) * tmask(i1:i2,j1,1)
       ENDIF
       !
@@ -844,12 +868,12 @@
          ENDIF
          !! clem ghost   
-         IF(western_side)   ubdy_w(j1:j2) = ubdy_w(j1:j2) + ztcoeff * ptab(i1,j1:j2)  
-         IF(eastern_side)   ubdy_e(j1:j2) = ubdy_e(j1:j2) + ztcoeff * ptab(i2,j1:j2) !clem previously i1  
+         IF(western_side)   ubdy_w(j1:j2) = ubdy_w(j1:j2) + ztcoeff * ptab(i2,j1:j2)  
+         IF(eastern_side)   ubdy_e(j1:j2) = ubdy_e(j1:j2) + ztcoeff * ptab(i1,j1:j2) !clem previously i1  
          IF(southern_side)  ubdy_s(i1:i2) = ubdy_s(i1:i2) + ztcoeff * ptab(i1:i2,j2) !clem previously j1
          IF(northern_side)  ubdy_n(i1:i2) = ubdy_n(i1:i2) + ztcoeff * ptab(i1:i2,j1) 
          !            
          IF( bdy_tinterp == 0 .OR. bdy_tinterp == 2) THEN
-            IF(western_side)   ubdy_w(j1:j2) = ubdy_w(j1:j2) / (zrhoy*e2u(i1,j1:j2)) * umask(i1,j1:j2,1)
-            IF(eastern_side)   ubdy_e(j1:j2) = ubdy_e(j1:j2) / (zrhoy*e2u(i2,j1:j2)) * umask(i2,j1:j2,1)
+            IF(western_side)   ubdy_w(j1:j2) = ubdy_w(j1:j2) / (zrhoy*e2u(i2,j1:j2)) * umask(i2,j1:j2,1)
+            IF(eastern_side)   ubdy_e(j1:j2) = ubdy_e(j1:j2) / (zrhoy*e2u(i1,j1:j2)) * umask(i1,j1:j2,1)
             IF(southern_side)  ubdy_s(i1:i2) = ubdy_s(i1:i2) / (zrhoy*e2u(i1:i2,j2)) * umask(i1:i2,j2,1)
             IF(northern_side)  ubdy_n(i1:i2) = ubdy_n(i1:i2) / (zrhoy*e2u(i1:i2,j1)) * umask(i1:i2,j1,1)
@@ -896,12 +920,12 @@
          ENDIF
          !! clem ghost
-         IF(western_side)   vbdy_w(j1:j2) = vbdy_w(j1:j2) + ztcoeff * ptab(i1,j1:j2)  
-         IF(eastern_side)   vbdy_e(j1:j2) = vbdy_e(j1:j2) + ztcoeff * ptab(i2,j1:j2) !clem previously i1  
+         IF(western_side)   vbdy_w(j1:j2) = vbdy_w(j1:j2) + ztcoeff * ptab(i2,j1:j2)  
+         IF(eastern_side)   vbdy_e(j1:j2) = vbdy_e(j1:j2) + ztcoeff * ptab(i1,j1:j2) !clem previously i1  
          IF(southern_side)  vbdy_s(i1:i2) = vbdy_s(i1:i2) + ztcoeff * ptab(i1:i2,j2) !clem previously j1
          IF(northern_side)  vbdy_n(i1:i2) = vbdy_n(i1:i2) + ztcoeff * ptab(i1:i2,j1) 
          !            
          IF( bdy_tinterp == 0 .OR. bdy_tinterp == 2) THEN
-            IF(western_side)   vbdy_w(j1:j2) = vbdy_w(j1:j2) / (zrhox*e1v(i1,j1:j2)) * vmask(i1,j1:j2,1)
-            IF(eastern_side)   vbdy_e(j1:j2) = vbdy_e(j1:j2) / (zrhox*e1v(i2,j1:j2)) * vmask(i2,j1:j2,1)
+            IF(western_side)   vbdy_w(j1:j2) = vbdy_w(j1:j2) / (zrhox*e1v(i2,j1:j2)) * vmask(i2,j1:j2,1)
+            IF(eastern_side)   vbdy_e(j1:j2) = vbdy_e(j1:j2) / (zrhox*e1v(i1,j1:j2)) * vmask(i1,j1:j2,1)
             IF(southern_side)  vbdy_s(i1:i2) = vbdy_s(i1:i2) / (zrhox*e1v(i1:i2,j2)) * vmask(i1:i2,j2,1)
             IF(northern_side)  vbdy_n(i1:i2) = vbdy_n(i1:i2) / (zrhox*e1v(i1:i2,j1)) * vmask(i1:i2,j1,1)
@@ -940,6 +964,6 @@
             &           - zt0**2._wp * (-2._wp*zt0 + 3._wp)    ) 
          !! clem ghost
-         IF(western_side ) ubdy_w(j1:j2) = zat * ptab(i1,j1:j2)  
-         IF(eastern_side ) ubdy_e(j1:j2) = zat * ptab(i2,j1:j2) !clem previously i1  
+         IF(western_side ) ubdy_w(j1:j2) = zat * ptab(i2,j1:j2)  
+         IF(eastern_side ) ubdy_e(j1:j2) = zat * ptab(i1,j1:j2) !clem previously i1  
          IF(southern_side) ubdy_s(i1:i2) = zat * ptab(i1:i2,j2) !clem previously j1
          IF(northern_side) ubdy_n(i1:i2) = zat * ptab(i1:i2,j1) 
@@ -978,6 +1002,6 @@
             &           - zt0**2._wp * (-2._wp*zt0 + 3._wp)    ) 
          !
-         IF(western_side )   vbdy_w(j1:j2) = zat * ptab(i1,j1:j2)  
-         IF(eastern_side )   vbdy_e(j1:j2) = zat * ptab(i2,j1:j2) !clem previously i1  
+         IF(western_side )   vbdy_w(j1:j2) = zat * ptab(i2,j1:j2)  
+         IF(eastern_side )   vbdy_e(j1:j2) = zat * ptab(i1,j1:j2) !clem previously i1  
          IF(southern_side)   vbdy_s(i1:i2) = zat * ptab(i1:i2,j2) !clem previously j1 
          IF(northern_side)   vbdy_n(i1:i2) = zat * ptab(i1:i2,j1) 
Index: /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/NST_SRC/agrif_user.F90
===================================================================
--- /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/NST_SRC/agrif_user.F90	(revision 8188)
+++ /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/NST_SRC/agrif_user.F90	(revision 8189)
@@ -149,6 +149,6 @@
    !-----------------------------
    !!clem ghost (previously set to /0,0/)
-   CALL Agrif_Set_bc(e1u_id,(/0,ind1/))
-   CALL Agrif_Set_bc(e2v_id,(/0,ind1/))
+   CALL Agrif_Set_bc(e1u_id,(/0,ind1-1/))
+   CALL Agrif_Set_bc(e2v_id,(/0,ind1-1/))
    !!clem ghost
 
@@ -433,13 +433,13 @@
    CALL Agrif_Set_bc(vn_sponge_id ,(/-nn_sponge_len*Agrif_irhox()-1,0/))
 
-   CALL Agrif_Set_bc(sshn_id,(/0,ind1/))
-   CALL Agrif_Set_bc(unb_id ,(/0,ind1/))
-   CALL Agrif_Set_bc(vnb_id ,(/0,ind1/))
-   CALL Agrif_Set_bc(ub2b_interp_id,(/0,ind1/))
-   CALL Agrif_Set_bc(vb2b_interp_id,(/0,ind1/))
-
-   CALL Agrif_Set_bc(e3t_id,(/-2*Agrif_irhox()-1,ind1/))   ! if west and rhox=3 and ghost=1: column 1 to 9
-   CALL Agrif_Set_bc(umsk_id,(/0,ind1/))
-   CALL Agrif_Set_bc(vmsk_id,(/0,ind1/))
+   CALL Agrif_Set_bc(sshn_id,(/0,ind1-1/))
+   CALL Agrif_Set_bc(unb_id ,(/0,ind1-1/))
+   CALL Agrif_Set_bc(vnb_id ,(/0,ind1-1/))
+   CALL Agrif_Set_bc(ub2b_interp_id,(/0,ind1-1/))
+   CALL Agrif_Set_bc(vb2b_interp_id,(/0,ind1-1/))
+
+   CALL Agrif_Set_bc(e3t_id,(/-2*Agrif_irhox()-1,ind1-1/))   ! if west and rhox=3 and ghost=1: column 2 to 9
+   CALL Agrif_Set_bc(umsk_id,(/0,ind1-1/))
+   CALL Agrif_Set_bc(vmsk_id,(/0,ind1-1/))
 
    ! clem: previously set to /0,1/
@@ -827,5 +827,5 @@
    CALL Agrif_Set_bc(trn_id,(/0,ind1/))
    !clem: previously set to /-,0/
-   CALL Agrif_Set_bc(trn_sponge_id,(/-nn_sponge_len*Agrif_irhox()-1,ind1/))
+   CALL Agrif_Set_bc(trn_sponge_id,(/-nn_sponge_len*Agrif_irhox()-1,0/))
 
    ! 5. Update type
Index: /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/OPA_SRC/DYN/divhor.F90
===================================================================
--- /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/OPA_SRC/DYN/divhor.F90	(revision 8188)
+++ /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/OPA_SRC/DYN/divhor.F90	(revision 8189)
@@ -82,11 +82,11 @@
             END DO  
          END DO  
-         IF( .NOT. AGRIF_Root() ) THEN
-            IF( nbondi ==  1 .OR. nbondi == 2 )   hdivn(nlci-1,   :  ,jk) = 0._wp      ! east
-            IF( nbondi == -1 .OR. nbondi == 2 )   hdivn(  2   ,   :  ,jk) = 0._wp      ! west
-            IF( nbondj ==  1 .OR. nbondj == 2 )   hdivn(  :   ,nlcj-1,jk) = 0._wp      ! north
-            IF( nbondj == -1 .OR. nbondj == 2 )   hdivn(  :   ,  2   ,jk) = 0._wp      ! south
-         ENDIF
       END DO
+      IF( .NOT. Agrif_Root() ) THEN
+         IF( nbondi == -1 .OR. nbondi == 2 )   hdivn( 2:nbghostcells+1,:        ,:) = 0._wp      ! west
+         IF( nbondi ==  1 .OR. nbondi == 2 )   hdivn( nlci-nbghostcells:nlci-1,:,:) = 0._wp      ! east
+         IF( nbondj == -1 .OR. nbondj == 2 )   hdivn( :,2:nbghostcells+1        ,:) = 0._wp      ! south
+         IF( nbondj ==  1 .OR. nbondj == 2 )   hdivn( :,nlcj-nbghostcells:nlcj-1,:) = 0._wp      ! north
+      ENDIF
       !
       IF( ln_rnf )   CALL sbc_rnf_div( hdivn )      !==  runoffs    ==!   (update hdivn field)
Index: /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90
===================================================================
--- /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90	(revision 8188)
+++ /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/OPA_SRC/DYN/dynspg_ts.F90	(revision 8189)
@@ -686,20 +686,20 @@
             IF((nbondi == -1).OR.(nbondi == 2)) THEN
                DO jj=1,jpj
-                  zwx(2,jj) = ubdy_w(jj) * e2u(2,jj)
+                  zwx(2:nbghostcells+1,jj) = ubdy_w(jj) * e2u(2:nbghostcells+1,jj)
                END DO
             ENDIF
             IF((nbondi ==  1).OR.(nbondi == 2)) THEN
                DO jj=1,jpj
-                  zwx(nlci-2,jj) = ubdy_e(jj) * e2u(nlci-2,jj)
+                  zwx(nlci-nbghostcells-1:nlci-2,jj) = ubdy_e(jj) * e2u(nlci-nbghostcells-1:nlci-2,jj)
                END DO
             ENDIF
             IF((nbondj == -1).OR.(nbondj == 2)) THEN
                DO ji=1,jpi
-                  zwy(ji,2) = vbdy_s(ji) * e1v(ji,2)
+                  zwy(ji,2:nbghostcells+1) = vbdy_s(ji) * e1v(ji,2:nbghostcells+1)
                END DO
             ENDIF
             IF((nbondj ==  1).OR.(nbondj == 2)) THEN
                DO ji=1,jpi
-                  zwy(ji,nlcj-2) = vbdy_n(ji) * e1v(ji,nlcj-2)
+                  zwy(ji,nlcj-nbghostcells-1:nlcj-2) = vbdy_n(ji) * e1v(ji,nlcj-nbghostcells-1:nlcj-2)
                END DO
             ENDIF
Index: /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/OPA_SRC/SBC/sbcwave.F90
===================================================================
--- /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/OPA_SRC/SBC/sbcwave.F90	(revision 8188)
+++ /branches/2017/dev_r8127_AGRIF_LIM3_GHOST/NEMOGCM/NEMO/OPA_SRC/SBC/sbcwave.F90	(revision 8189)
@@ -152,9 +152,9 @@
       END DO
       !
-      IF( .NOT. AGRIF_Root() ) THEN
-         IF( nbondi ==  1 .OR. nbondi == 2 )   ze3divh(nlci-1,   :  ,:) = 0._wp      ! east
-         IF( nbondi == -1 .OR. nbondi == 2 )   ze3divh(  2   ,   :  ,:) = 0._wp      ! west
-         IF( nbondj ==  1 .OR. nbondj == 2 )   ze3divh(  :   ,nlcj-1,:) = 0._wp      ! north
-         IF( nbondj == -1 .OR. nbondj == 2 )   ze3divh(  :   ,  2   ,:) = 0._wp      ! south
+      IF( .NOT. Agrif_Root() ) THEN
+         IF( nbondi == -1 .OR. nbondi == 2 )   ze3divh( 2:nbghostcells+1,:        ,:) = 0._wp      ! west
+         IF( nbondi ==  1 .OR. nbondi == 2 )   ze3divh( nlci-nbghostcells:nlci-1,:,:) = 0._wp      ! east
+         IF( nbondj == -1 .OR. nbondj == 2 )   ze3divh( :,2:nbghostcells+1        ,:) = 0._wp      ! south
+         IF( nbondj ==  1 .OR. nbondj == 2 )   ze3divh( :,nlcj-nbghostcells:nlcj-1,:) = 0._wp      ! north
       ENDIF
       !
