Index: /branches/2017/dev_merge_2017/NEMOGCM/NEMO/NST_SRC/agrif_opa_interp.F90
===================================================================
--- /branches/2017/dev_merge_2017/NEMOGCM/NEMO/NST_SRC/agrif_opa_interp.F90	(revision 9056)
+++ /branches/2017/dev_merge_2017/NEMOGCM/NEMO/NST_SRC/agrif_opa_interp.F90	(revision 9057)
@@ -37,11 +37,10 @@
    PRIVATE
 
-   PUBLIC   Agrif_tra, Agrif_dyn, Agrif_ssh, Agrif_dyn_ts, Agrif_ssh_ts, Agrif_dta_ts
+   PUBLIC   Agrif_dyn, Agrif_ssh, Agrif_dyn_ts, Agrif_ssh_ts, Agrif_dta_ts
+   PUBLIC   Agrif_tra, Agrif_avm
    PUBLIC   interpun , interpvn
-   PUBLIC   interptsn, interpsshn
+   PUBLIC   interptsn, interpsshn, interpavm
    PUBLIC   interpunb, interpvnb , interpub2b, interpvb2b
    PUBLIC   interpe3t, interpumsk, interpvmsk
-   PUBLIC   Agrif_avm, interpavm
->>>>>>> .merge-right.r9019
 
    INTEGER ::   bdy_tinterp = 0
@@ -80,4 +79,5 @@
       INTEGER ::   ji, jj, jk       ! dummy loop indices
       INTEGER ::   j1, j2, i1, i2
+      INTEGER ::   ibdy1, jbdy1, ibdy2, jbdy2
       REAL(wp), DIMENSION(jpi,jpj) ::   zub, zvb
       !!----------------------------------------------------------------------  
@@ -96,169 +96,134 @@
       i1 =  1   ;   i2 = jpi
       j1 =  1   ;   j2 = jpj
-      IF( nbondj == -1 .OR. nbondj == 2 )   j1 = 3
-      IF( nbondj == +1 .OR. nbondj == 2 )   j2 = nlcj-2
-      IF( nbondi == -1 .OR. nbondi == 2 )   i1 = 3
-      IF( nbondi == +1 .OR. nbondi == 2 )   i2 = nlci-2
-
+      IF( nbondj == -1 .OR. nbondj == 2 )   j1 = 2 + nbghostcells
+      IF( nbondj == +1 .OR. nbondj == 2 )   j2 = nlcj - nbghostcells - 1
+      IF( nbondi == -1 .OR. nbondi == 2 )   i1 = 2 + nbghostcells 
+      IF( nbondi == +1 .OR. nbondi == 2 )   i2 = nlci - nbghostcells - 1
+
+      ! --- West --- !
       IF( nbondi == -1 .OR. nbondi == 2 ) THEN
+         ibdy1 = 2
+         ibdy2 = 1+nbghostcells 
          !
          ! Smoothing
          ! ---------
          IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
-            ua_b(2:1+nbghostcells,:) = 0._wp
+            ua_b(ibdy1:ibdy2,:) = 0._wp
             DO jk = 1, jpkm1
                DO jj = 1, jpj
-                  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)
+                  ua_b(ibdy1:ibdy2,jj) = ua_b(ibdy1:ibdy2,jj) + e3u_a(ibdy1:ibdy2,jj,jk) * ua(ibdy1:ibdy2,jj,jk)
                END DO
             END DO
             DO jj = 1, jpj
-               ua_b(2:1+nbghostcells,jj) = ua_b(2:1+nbghostcells,jj) * r1_hu_a(2:1+nbghostcells,jj)
-            END DO
-         ENDIF
-         !
-         ! Smoothing if only 1 ghostcell
-         ! -----------------------------
-         IF( nbghostcells == 1 ) THEN
+               ua_b(ibdy1:ibdy2,jj) = ua_b(ibdy1:ibdy2,jj) * r1_hu_a(ibdy1:ibdy2,jj)
+            END DO
+         ENDIF
+         !
+         IF( .NOT.lk_agrif_clp ) THEN
             DO jk=1,jpkm1                 ! Smooth
                DO jj=j1,j2
-                  ua(2,jj,jk) = 0.25_wp*(ua(1,jj,jk)+2._wp*ua(2,jj,jk)+ua(3,jj,jk))
-                  ua(2,jj,jk) = ua(2,jj,jk) * umask(2,jj,jk)
-               END DO
-            END DO
-            !
-            zub(2,:) = 0._wp              ! Correct transport
+                  ua(ibdy2,jj,jk) = 0.25_wp*(ua(ibdy2-1,jj,jk)+2._wp*ua(ibdy2,jj,jk)+ua(ibdy2+1,jj,jk))
+                  ua(ibdy2,jj,jk) = ua(ibdy2,jj,jk) * umask(ibdy2,jj,jk)
+               END DO
+            END DO
+         ENDIF
+         !
+         zub(ibdy1:ibdy2,:) = 0._wp              ! Correct transport
+         DO jk = 1, jpkm1
+            DO jj = 1, jpj
+               zub(ibdy1:ibdy2,jj) = zub(ibdy1:ibdy2,jj) + e3u_a(ibdy1:ibdy2,jj,jk)  * ua(ibdy1:ibdy2,jj,jk)
+            END DO
+         END DO
+         DO jj=1,jpj
+            zub(ibdy1:ibdy2,jj) = zub(ibdy1:ibdy2,jj) * r1_hu_a(ibdy1:ibdy2,jj)
+         END DO
+            
+         DO jk = 1, jpkm1
+            DO jj = 1, jpj
+               ua(ibdy1:ibdy2,jj,jk) = (ua(ibdy1:ibdy2,jj,jk)+ua_b(ibdy1:ibdy2,jj)-zub(ibdy1:ibdy2,jj))*umask(ibdy1:ibdy2,jj,jk)
+            END DO
+         END DO
+            
+         IF( ln_dynspg_ts ) THEN       ! Set tangential velocities to time splitting estimate
+            zvb(ibdy1:ibdy2,:) = 0._wp
             DO jk = 1, jpkm1
                DO jj = 1, jpj
-                  zub(2,jj) = zub(2,jj) + e3u_a(2,jj,jk) * ua(2,jj,jk)
-               END DO
-            END DO
-            DO jj=1,jpj
-               zub(2,jj) = zub(2,jj) * r1_hu_a(2,jj)
-            END DO
-            
+                  zvb(ibdy1:ibdy2,jj) = zvb(ibdy1:ibdy2,jj) + e3v_a(ibdy1:ibdy2,jj,jk) * va(ibdy1:ibdy2,jj,jk)
+               END DO
+            END DO
+            DO jj = 1, jpj
+               zvb(ibdy1:ibdy2,jj) = zvb(ibdy1:ibdy2,jj) * r1_hv_a(ibdy1:ibdy2,jj)
+            END DO
             DO jk = 1, jpkm1
                DO jj = 1, jpj
-                  ua(2,jj,jk) = (ua(2,jj,jk)+ua_b(2,jj)-zub(2,jj))*umask(2,jj,jk)
-               END DO
-            END DO
-            
-            IF( ln_dynspg_ts ) THEN       ! Set tangential velocities to time splitting estimate
-               zvb(2,:) = 0._wp
-               DO jk = 1, jpkm1
-                  DO jj = 1, jpj
-                     zvb(2,jj) = zvb(2,jj) + e3v_a(2,jj,jk) * va(2,jj,jk)
-                  END DO
-               END DO
+                  va(ibdy1:ibdy2,jj,jk) = (va(ibdy1:ibdy2,jj,jk)+va_b(ibdy1:ibdy2,jj)-zvb(ibdy1:ibdy2,jj)) * vmask(ibdy1:ibdy2,jj,jk)
+               END DO
+            END DO
+         ENDIF
+         !
+      ENDIF
+
+      ! --- East --- !
+      IF( nbondi ==  1 .OR. nbondi == 2 ) THEN
+         ibdy1 = nlci-1-nbghostcells
+         ibdy2 = nlci-2 
+         !
+         ! Smoothing
+         ! ---------
+         IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
+            ua_b(ibdy1:ibdy2,:) = 0._wp
+            DO jk = 1, jpkm1
                DO jj = 1, jpj
-                  zvb(2,jj) = zvb(2,jj) * r1_hv_a(2,jj)
-               END DO
-               DO jk = 1, jpkm1
-                  DO jj = 1, jpj
-                     va(2,jj,jk) = (va(2,jj,jk)+va_b(2,jj)-zvb(2,jj)) * vmask(2,jj,jk)
-                  END DO
-               END DO
-            ENDIF
-            !
-         ENDIF
-         !
-         ! Mask domain edges:
-         !-------------------
-!         DO jk = 1, jpkm1
-!            DO jj = 1, jpj
-!               ua(1,jj,jk) = 0._wp
-!               va(1,jj,jk) = 0._wp
-!            END DO
-!         END DO
-         !
-      ENDIF
-
-      ! --- East --- !
-      IF( nbondi == 1 .OR. nbondi == 2 ) THEN
-
-         IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
-            ua_b(nlci-nbghostcells-1:nlci-2,:) = 0._wp
-            DO jk=1,jpkm1
-               DO jj=1,jpj
-                  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-2,jj) = ua_b(nlci-nbghostcells-1:nlci-2,jj) * r1_hu_a(nlci-nbghostcells-1:nlci-2,jj) 
-            END DO
-         ENDIF
-         !
-         ! Smoothing if only 1 ghostcell
-         ! -----------------------------
-         IF( nbghostcells == 1 ) THEN
-            DO jk = 1, jpkm1              ! Smooth
-               DO jj = j1, j2
-                  ua(nlci-2,jj,jk) = 0.25_wp * umask(nlci-2,jj,jk)      &
-                     &             * ( ua(nlci-3,jj,jk) + 2._wp*ua(nlci-2,jj,jk) + ua(nlci-1,jj,jk) )
-               END DO
-            END DO
-         ENDIF
-         zub(nlci-2,:) = 0._wp        ! Correct transport
+                  ua_b(ibdy1:ibdy2,jj) = ua_b(ibdy1:ibdy2,jj) + e3u_a(ibdy1:ibdy2,jj,jk) * ua(ibdy1:ibdy2,jj,jk)
+               END DO
+            END DO
+            DO jj = 1, jpj
+               ua_b(ibdy1:ibdy2,jj) = ua_b(ibdy1:ibdy2,jj) * r1_hu_a(ibdy1:ibdy2,jj)
+            END DO
+         ENDIF
+         !
+         IF( .NOT.lk_agrif_clp ) THEN
+            DO jk=1,jpkm1                 ! Smooth
+               DO jj=j1,j2
+                  ua(ibdy1,jj,jk) = 0.25_wp*(ua(ibdy1-1,jj,jk)+2._wp*ua(ibdy1,jj,jk)+ua(ibdy1+1,jj,jk))
+                  ua(ibdy1,jj,jk) = ua(ibdy1,jj,jk) * umask(ibdy1,jj,jk)
+               END DO
+            END DO
+         ENDIF
+         !
+         zub(ibdy1:ibdy2,:) = 0._wp              ! Correct transport
          DO jk = 1, jpkm1
             DO jj = 1, jpj
-               zub(nlci-2,jj) = zub(nlci-2,jj) + e3u_a(nlci-2,jj,jk) * ua(nlci-2,jj,jk)
-            END DO
-         END DO
-         DO jj = 1, jpj
-            zub(nlci-2,jj) = zub(nlci-2,jj) * r1_hu_a(nlci-2,jj)
-         END DO
-
+               zub(ibdy1:ibdy2,jj) = zub(ibdy1:ibdy2,jj) + e3u_a(ibdy1:ibdy2,jj,jk)  * ua(ibdy1:ibdy2,jj,jk)
+            END DO
+         END DO
+         DO jj=1,jpj
+            zub(ibdy1:ibdy2,jj) = zub(ibdy1:ibdy2,jj) * r1_hu_a(ibdy1:ibdy2,jj)
+         END DO
+            
          DO jk = 1, jpkm1
             DO jj = 1, jpj
-               ua(nlci-2,jj,jk) = ( ua(nlci-2,jj,jk) + ua_b(nlci-2,jj) - zub(nlci-2,jj) ) * umask(nlci-2,jj,jk)
-            END DO
-         END DO
-         !
-         ! Set tangential velocities to time splitting estimate
-         !-----------------------------------------------------
-         IF( ln_dynspg_ts ) THEN
-            zvb(nlci-1,:) = 0._wp
+               ua(ibdy1:ibdy2,jj,jk) = (ua(ibdy1:ibdy2,jj,jk)+ua_b(ibdy1:ibdy2,jj)-zub(ibdy1:ibdy2,jj))*umask(ibdy1:ibdy2,jj,jk)
+            END DO
+         END DO
+            
+         IF( ln_dynspg_ts ) THEN       ! Set tangential velocities to time splitting estimate
+            ibdy1 = ibdy1 + 1
+            ibdy2 = ibdy2 + 1 
+            zvb(ibdy1:ibdy2,:) = 0._wp
             DO jk = 1, jpkm1
                DO jj = 1, jpj
-                  zub(nlci-2,jj) = zub(nlci-2,jj) + e3u_a(nlci-2,jj,jk) * ua(nlci-2,jj,jk)
+                  zvb(ibdy1:ibdy2,jj) = zvb(ibdy1:ibdy2,jj) + e3v_a(ibdy1:ibdy2,jj,jk) * va(ibdy1:ibdy2,jj,jk)
                END DO
             END DO
             DO jj = 1, jpj
-               zub(nlci-2,jj) = zub(nlci-2,jj) * r1_hu_a(nlci-2,jj)
-            END DO
-            
+               zvb(ibdy1:ibdy2,jj) = zvb(ibdy1:ibdy2,jj) * r1_hv_a(ibdy1:ibdy2,jj)
+            END DO
             DO jk = 1, jpkm1
                DO jj = 1, jpj
-                  ua(nlci-2,jj,jk) = ( ua(nlci-2,jj,jk) + ua_b(nlci-2,jj) - zub(nlci-2,jj) ) * umask(nlci-2,jj,jk)
-               END DO
-            END DO
-            !
-            IF( ln_dynspg_ts ) THEN       ! Set tangential velocities to time splitting estimate
-               zvb(nlci-1,:) = 0._wp
-               DO jk = 1, jpkm1
-                  DO jj = 1, jpj
-                     zvb(nlci-1,jj) = zvb(nlci-1,jj) + e3v_a(nlci-1,jj,jk) * va(nlci-1,jj,jk)
-                  END DO
-               END DO
-               DO jj=1,jpj
-                  zvb(nlci-1,jj) = zvb(nlci-1,jj) * r1_hv_a(nlci-1,jj)
-               END DO
-               DO jk = 1, jpkm1
-                  DO jj = 1, jpj
-                     va(nlci-1,jj,jk) = ( va(nlci-1,jj,jk) + va_b(nlci-1,jj) - zvb(nlci-1,jj) ) * vmask(nlci-1,jj,jk)
-                  END DO
-               END DO
-            ENDIF
-            !
-         ENDIF
-         !
-         ! Mask domain edges:
-         !-------------------
-!         DO jk = 1, jpkm1
-!            DO jj = 1, jpj
-!               ua(nlci-1,jj,jk) = 0._wp
-!               va(nlci  ,jj,jk) = 0._wp
-!            END DO
-!         END DO
+                  va(ibdy1:ibdy2,jj,jk) = (va(ibdy1:ibdy2,jj,jk)+va_b(ibdy1:ibdy2,jj)-zvb(ibdy1:ibdy2,jj)) * vmask(ibdy1:ibdy2,jj,jk)
+               END DO
+            END DO
+         ENDIF
          !
       ENDIF
@@ -266,138 +231,127 @@
       ! --- South --- !
       IF( nbondj == -1 .OR. nbondj == 2 ) THEN
-
+         jbdy1 = 2
+         jbdy2 = 1+nbghostcells 
+         !
+         ! Smoothing
+         ! ---------
          IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
-            va_b(:,2:nbghostcells+1) = 0._wp
+            va_b(:,jbdy1:jbdy2) = 0._wp
             DO jk = 1, jpkm1
                DO ji = 1, jpi
-                  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)
+                  va_b(ji,jbdy1:jbdy2) = va_b(ji,jbdy1:jbdy2) + e3v_a(ji,jbdy1:jbdy2,jk) * va(ji,jbdy1:jbdy2,jk)
                END DO
             END DO
             DO ji=1,jpi
-               va_b(ji,2:nbghostcells+1) = va_b(ji,2:nbghostcells+1) * r1_hv_a(ji,2:nbghostcells+1)
-            END DO
-         ENDIF
-         !
-         IF (.NOT.lk_agrif_clp) THEN
+               va_b(ji,jbdy1:jbdy2) = va_b(ji,jbdy1:jbdy2) * r1_hv_a(ji,jbdy1:jbdy2)
+            END DO
+         ENDIF
+         !
+         IF ( .NOT.lk_agrif_clp ) THEN
             DO jk = 1, jpkm1              ! Smooth
                DO ji = i1, i2
-                  va(ji,2,jk) = 0.25_wp * vmask(ji,2,jk)    &
-                     &        * ( va(ji,1,jk) + 2._wp*va(ji,2,jk) + va(ji,3,jk) )
-               END DO
-            END DO
-         ENDIF
-         !
-         zvb(:,2) = 0._wp              ! Correct transport
+                  va(ji,jbdy2,jk) = 0.25_wp * vmask(ji,jbdy2,jk)    &
+                     &        * ( va(ji,jbdy2-1,jk) + 2._wp*va(ji,jbdy2,jk) + va(ji,jbdy2+1,jk) )
+               END DO
+            END DO
+         ENDIF
+         !
+         zvb(:,jbdy1:jbdy2) = 0._wp              ! Correct transport
          DO jk=1,jpkm1
             DO ji=1,jpi
-               zvb(ji,2) = zvb(ji,2) + e3v_a(ji,2,jk) * va(ji,2,jk) * vmask(ji,2,jk)
-            END DO
+               zvb(ji,jbdy1:jbdy2) = zvb(ji,jbdy1:jbdy2) + e3v_a(ji,jbdy1:jbdy2,jk) * va(ji,jbdy1:jbdy2,jk) * vmask(ji,jbdy1:jbdy2,jk)
+            END DO
+         END DO
+         DO ji = 1, jpi
+            zvb(ji,jbdy1:jbdy2) = zvb(ji,jbdy1:jbdy2) * r1_hv_a(ji,jbdy1:jbdy2)
+         END DO
+         DO jk = 1, jpkm1
             DO ji = 1, jpi
-               zvb(ji,2) = zvb(ji,2) * r1_hv_a(ji,2)
-            END DO
+               va(ji,jbdy1:jbdy2,jk) = ( va(ji,jbdy1:jbdy2,jk) + va_b(ji,jbdy1:jbdy2) - zvb(ji,jbdy1:jbdy2) ) * vmask(ji,jbdy1:jbdy2,jk)
+            END DO
+         END DO
+            
+         IF( ln_dynspg_ts ) THEN       ! Set tangential velocities to time splitting estimate
+            zub(:,2) = 0._wp
             DO jk = 1, jpkm1
                DO ji = 1, jpi
-                  va(ji,2,jk) = ( va(ji,2,jk) + va_b(ji,2) - zvb(ji,2) ) * vmask(ji,2,jk)
-               END DO
-            END DO
-            
-            IF( ln_dynspg_ts ) THEN       ! Set tangential velocities to time splitting estimate
-               zub(:,2) = 0._wp
-               DO jk = 1, jpkm1
-                  DO ji = 1, jpi
-                     zub(ji,2) = zub(ji,2) + e3u_a(ji,2,jk) * ua(ji,2,jk) * umask(ji,2,jk)
-                  END DO
-               END DO
-               DO ji = 1, jpi
-                  zub(ji,2) = zub(ji,2) * r1_hu_a(ji,2)
-               END DO
+                  zub(ji,jbdy1:jbdy2) = zub(ji,jbdy1:jbdy2) + e3u_a(ji,jbdy1:jbdy2,jk) * ua(ji,jbdy1:jbdy2,jk) * umask(ji,jbdy1:jbdy2,jk)
+               END DO
+            END DO
+            DO ji = 1, jpi
+               zub(ji,jbdy1:jbdy2) = zub(ji,jbdy1:jbdy2) * r1_hu_a(ji,jbdy1:jbdy2)
+            END DO
                
-               DO jk = 1, jpkm1
-                  DO ji = 1, jpi
-                     ua(ji,2,jk) = ( ua(ji,2,jk) + ua_b(ji,2) - zub(ji,2) ) * umask(ji,2,jk)
-                  END DO
-               END DO
-            ENDIF
-            !
-         ENDIF
-         !
-         ! Mask domain edges:
-         !-------------------
-!         DO jk = 1, jpkm1
-!            DO ji = 1, jpi
-!               ua(ji,1,jk) = 0._wp
-!               va(ji,1,jk) = 0._wp
-!            END DO
-!         END DO
-         !
-      ENDIF
-
-      ! --- North --- !
-      IF( nbondj == 1 .OR. nbondj == 2 ) THEN
-         !
-         IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
-            va_b(:,nlcj-nbghostcells-1:nlcj-2) = 0._wp
             DO jk = 1, jpkm1
                DO ji = 1, jpi
-                  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-2) = va_b(ji,nlcj-nbghostcells-1:nlcj-2) * r1_hv_a(ji,nlcj-nbghostcells-1:nlcj-2)
-            END DO
-         ENDIF
-         !
-         IF (.NOT.lk_agrif_clp) THEN
+                  ua(ji,jbdy1:jbdy2,jk) = ( ua(ji,jbdy1:jbdy2,jk) + ua_b(ji,jbdy1:jbdy2) - zub(ji,jbdy1:jbdy2) ) * umask(ji,jbdy1:jbdy2,jk)
+               END DO
+            END DO
+         ENDIF
+         !
+      ENDIF
+
+      ! --- North --- !
+      IF( nbondj ==  1 .OR. nbondj == 2 ) THEN
+         jbdy1 = nlcj-1-nbghostcells
+         jbdy2 = nlcj-2 
+         !
+         ! Smoothing
+         ! ---------
+         IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
+            va_b(:,jbdy1:jbdy2) = 0._wp
+            DO jk = 1, jpkm1
+               DO ji = 1, jpi
+                  va_b(ji,jbdy1:jbdy2) = va_b(ji,jbdy1:jbdy2) + e3v_a(ji,jbdy1:jbdy2,jk) * va(ji,jbdy1:jbdy2,jk)
+               END DO
+            END DO
+            DO ji=1,jpi
+               va_b(ji,jbdy1:jbdy2) = va_b(ji,jbdy1:jbdy2) * r1_hv_a(ji,jbdy1:jbdy2)
+            END DO
+         ENDIF
+         !
+         IF ( .NOT.lk_agrif_clp ) THEN
             DO jk = 1, jpkm1              ! Smooth
                DO ji = i1, i2
-                  va(ji,nlcj-2,jk) = 0.25_wp * vmask(ji,nlcj-2,jk)   &
-                     &             * ( va(ji,nlcj-3,jk) + 2._wp * va(ji,nlcj-2,jk) + va(ji,nlcj-1,jk) )
-               END DO
-            END DO
-         END IF
-         !
-         zvb(:,nlcj-2) = 0._wp         ! Correct transport
+                  va(ji,jbdy1,jk) = 0.25_wp * vmask(ji,jbdy1,jk)    &
+                     &        * ( va(ji,jbdy1-1,jk) + 2._wp*va(ji,jbdy1,jk) + va(ji,jbdy1+1,jk) )
+               END DO
+            END DO
+         ENDIF
+         !
+         zvb(:,jbdy1:jbdy2) = 0._wp              ! Correct transport
+         DO jk=1,jpkm1
+            DO ji=1,jpi
+               zvb(ji,jbdy1:jbdy2) = zvb(ji,jbdy1:jbdy2) + e3v_a(ji,jbdy1:jbdy2,jk) * va(ji,jbdy1:jbdy2,jk) * vmask(ji,jbdy1:jbdy2,jk)
+            END DO
+         END DO
+         DO ji = 1, jpi
+            zvb(ji,jbdy1:jbdy2) = zvb(ji,jbdy1:jbdy2) * r1_hv_a(ji,jbdy1:jbdy2)
+         END DO
          DO jk = 1, jpkm1
             DO ji = 1, jpi
-               zvb(ji,nlcj-2) = zvb(ji,nlcj-2) + e3v_a(ji,nlcj-2,jk) * va(ji,nlcj-2,jk) * vmask(ji,nlcj-2,jk)
-            END DO
-            DO ji = 1, jpi
-               zvb(ji,nlcj-2) = zvb(ji,nlcj-2) * r1_hv_a(ji,nlcj-2)
-            END DO
+               va(ji,jbdy1:jbdy2,jk) = ( va(ji,jbdy1:jbdy2,jk) + va_b(ji,jbdy1:jbdy2) - zvb(ji,jbdy1:jbdy2) ) * vmask(ji,jbdy1:jbdy2,jk)
+            END DO
+         END DO
+            
+         IF( ln_dynspg_ts ) THEN       ! Set tangential velocities to time splitting estimate
+            jbdy1 = jbdy1 + 1
+            jbdy2 = jbdy2 + 1 
+            zub(:,2) = 0._wp
             DO jk = 1, jpkm1
                DO ji = 1, jpi
-                  va(ji,nlcj-2,jk) = ( va(ji,nlcj-2,jk) + va_b(ji,nlcj-2) - zvb(ji,nlcj-2) ) * vmask(ji,nlcj-2,jk)
-               END DO
-            END DO
-            !
-            IF( ln_dynspg_ts ) THEN       ! Set tangential velocities to time splitting estimate
-               zub(:,nlcj-1) = 0._wp
-               DO jk = 1, jpkm1
-                  DO ji = 1, jpi
-                     zub(ji,nlcj-1) = zub(ji,nlcj-1) + e3u_a(ji,nlcj-1,jk) * ua(ji,nlcj-1,jk) * umask(ji,nlcj-1,jk)
-                  END DO
-               END DO
+                  zub(ji,jbdy1:jbdy2) = zub(ji,jbdy1:jbdy2) + e3u_a(ji,jbdy1:jbdy2,jk) * ua(ji,jbdy1:jbdy2,jk) * umask(ji,jbdy1:jbdy2,jk)
+               END DO
+            END DO
+            DO ji = 1, jpi
+               zub(ji,jbdy1:jbdy2) = zub(ji,jbdy1:jbdy2) * r1_hu_a(ji,jbdy1:jbdy2)
+            END DO
+               
+            DO jk = 1, jpkm1
                DO ji = 1, jpi
-                  zub(ji,nlcj-1) = zub(ji,nlcj-1) * r1_hu_a(ji,nlcj-1)
-               END DO
-               !
-               DO jk = 1, jpkm1
-                  DO ji = 1, jpi
-                     ua(ji,nlcj-1,jk) = ( ua(ji,nlcj-1,jk) + ua_b(ji,nlcj-1) - zub(ji,nlcj-1) ) * umask(ji,nlcj-1,jk)
-                  END DO
-               END DO
-            ENDIF
-            !
-         ENDIF
-         !
-         ! Mask domain edges:
-         !-------------------
-!         DO jk = 1, jpkm1
-!            DO ji = 1, jpi
-!               ua(ji,nlcj  ,jk) = 0._wp
-!               va(ji,nlcj-1,jk) = 0._wp
-!            END DO
-!         END DO
+                  ua(ji,jbdy1:jbdy2,jk) = ( ua(ji,jbdy1:jbdy2,jk) + ua_b(ji,jbdy1:jbdy2) - zub(ji,jbdy1:jbdy2) ) * umask(ji,jbdy1:jbdy2,jk)
+               END DO
+            END DO
+         ENDIF
          !
       ENDIF
@@ -416,5 +370,5 @@
       !
       IF( Agrif_Root() )   RETURN
-      !! clem ghost
+      !
       IF((nbondi == -1).OR.(nbondi == 2)) THEN
          DO jj=1,jpj
@@ -515,6 +469,5 @@
       INTEGER, INTENT(in) ::   kt
       !
-      INTEGER  :: ji, jj, indx
-      INTEGER :: ji, jj
+      INTEGER  :: ji, jj, indx, indy
       !!----------------------------------------------------------------------  
       !
@@ -549,8 +502,8 @@
       ! --- South --- !
       IF((nbondj == -1).OR.(nbondj == 2)) THEN
-         indx = 1+nbghostcells
-         DO jj = 2, indx
+         indy = 1+nbghostcells
+         DO jj = 2, indy
             DO ji = 1, jpi
-               ssha(ji,indx) = hbdy_s(ji)
+               ssha(ji,indy) = hbdy_s(ji)
             ENDDO
          ENDDO
@@ -559,8 +512,8 @@
       ! --- North --- !
       IF((nbondj == 1).OR.(nbondj == 2)) THEN
-         indx = nlcj-nbghostcells
+         indy = nlcj-nbghostcells
          DO jj = indx, nlcj-1
             DO ji = 1, jpi
-               ssha(ji,indx) = hbdy_n(ji)
+               ssha(ji,indy) = hbdy_n(ji)
             ENDDO
          ENDDO
@@ -709,5 +662,5 @@
 # endif
          !
-         IF( nbghostcells > 1 ) THEN  ! no smoothing
+         IF( lk_agrif_clp ) THEN  ! Clamped bcs
             tsa(i1:i2,j1:j2,k1:k2,n1:n2) = ptab_child(i1:i2,j1:j2,k1:k2,n1:n2)
          ELSE                         ! smoothing
@@ -727,8 +680,8 @@
             ! 
             ! Remove CORNERS
-            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        
+            IF((nbondj == -1).OR.(nbondj == 2)) jmin = 2 + nbghostcells
+            IF((nbondj == +1).OR.(nbondj == 2)) jmax = nlcj - nbghostcells - 1
+            IF((nbondi == -1).OR.(nbondi == 2)) imin = 1 + nbghostcells
+            IF((nbondi == +1).OR.(nbondi == 2)) imax = nlci - nbghostcells - 1      
             !
             IF( eastern_side ) THEN
@@ -856,6 +809,6 @@
          !! clem ghost
          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(eastern_side)  hbdy_e(j1:j2) = ptab(i1,j1:j2) * tmask(i1,j1:j2,1)
+         IF(southern_side) hbdy_s(i1:i2) = ptab(i1:i2,j2) * tmask(i1:i2,j2,1) 
          IF(northern_side) hbdy_n(i1:i2) = ptab(i1:i2,j1) * tmask(i1:i2,j1,1)
       ENDIF
@@ -1069,8 +1022,8 @@
             ztcoeff = 1
          ENDIF
-         !! clem ghost   
+         !   
          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(eastern_side)   ubdy_e(j1:j2) = ubdy_e(j1:j2) + ztcoeff * ptab(i1,j1:j2)  
+         IF(southern_side)  ubdy_s(i1:i2) = ubdy_s(i1:i2) + ztcoeff * ptab(i1:i2,j2)
          IF(northern_side)  ubdy_n(i1:i2) = ubdy_n(i1:i2) + ztcoeff * ptab(i1:i2,j1) 
          !            
@@ -1123,6 +1076,6 @@
          !! clem ghost
          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(eastern_side)   vbdy_e(j1:j2) = vbdy_e(j1:j2) + ztcoeff * ptab(i1,j1:j2)   
+         IF(southern_side)  vbdy_s(i1:i2) = vbdy_s(i1:i2) + ztcoeff * ptab(i1:i2,j2)
          IF(northern_side)  vbdy_n(i1:i2) = vbdy_n(i1:i2) + ztcoeff * ptab(i1:i2,j1) 
          !            
@@ -1171,6 +1124,6 @@
          !! clem ghost
          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(eastern_side ) ubdy_e(j1:j2) = zat * ptab(i1,j1:j2)  
+         IF(southern_side) ubdy_s(i1:i2) = zat * ptab(i1:i2,j2)
          IF(northern_side) ubdy_n(i1:i2) = zat * ptab(i1:i2,j1) 
       ENDIF
@@ -1213,6 +1166,6 @@
          !
          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(eastern_side )   vbdy_e(j1:j2) = zat * ptab(i1,j1:j2)  
+         IF(southern_side)   vbdy_s(i1:i2) = zat * ptab(i1:i2,j2)
          IF(northern_side)   vbdy_n(i1:i2) = zat * ptab(i1:i2,j1) 
       ENDIF
