Index: NEMO/branches/2020/test_12905_xios_restart/src/NST/agrif_oce_interp.F90
===================================================================
--- NEMO/branches/2020/test_12905_xios_restart/src/NST/agrif_oce_interp.F90	(revision 12987)
+++ NEMO/branches/2020/test_12905_xios_restart/src/NST/agrif_oce_interp.F90	(revision 13751)
@@ -34,4 +34,5 @@
    USE lib_mpp
    USE vremap
+   USE lbclnk
  
    IMPLICIT NONE
@@ -43,8 +44,8 @@
    PUBLIC   interptsn, interpsshn, interpavm
    PUBLIC   interpunb, interpvnb , interpub2b, interpvb2b
-   PUBLIC   interpe3t
-#if defined key_vertical
+   PUBLIC   interpe3t, interpglamt, interpgphit
    PUBLIC   interpht0, interpmbkt
-# endif
+   PUBLIC   agrif_initts, agrif_initssh
+
    INTEGER ::   bdy_tinterp = 0
 
@@ -86,254 +87,245 @@
       IF( Agrif_Root() )   RETURN
       !
-      Agrif_SpecialValue    = 0._wp
+      Agrif_SpecialValue    = 0.0_wp
       Agrif_UseSpecialValue = ln_spc_dyn
       !
+      use_sign_north = .TRUE.
+      sign_north = -1.0_wp
       CALL Agrif_Bc_variable( un_interp_id, procname=interpun )
       CALL Agrif_Bc_variable( vn_interp_id, procname=interpvn )
+      use_sign_north = .FALSE.
       !
       Agrif_UseSpecialValue = .FALSE.
       !
       ! --- West --- !
-      ibdy1 = 2
-      ibdy2 = 1+nbghostcells 
-      !
-      IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
+      IF( lk_west ) THEN
+         ibdy1 = nn_hls + 2                  ! halo + land + 1
+         ibdy2 = nn_hls + 1 + nbghostcells   ! halo + land + nbghostcells
+         !
+         IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
+            DO ji = mi0(ibdy1), mi1(ibdy2)
+               uu_b(ji,:,Krhs_a) = 0._wp
+               DO jk = 1, jpkm1
+                  DO jj = 1, jpj
+                     uu_b(ji,jj,Krhs_a) = uu_b(ji,jj,Krhs_a) + e3u(ji,jj,jk,Krhs_a) * uu(ji,jj,jk,Krhs_a) * umask(ji,jj,jk)
+                  END DO
+               END DO
+               DO jj = 1, jpj
+                  uu_b(ji,jj,Krhs_a) = uu_b(ji,jj,Krhs_a) * r1_hu(ji,jj,Krhs_a)
+               END DO
+            END DO
+         ENDIF
+         !
          DO ji = mi0(ibdy1), mi1(ibdy2)
-            uu_b(ji,:,Krhs_a) = 0._wp
-
+            zub(ji,:) = 0._wp    ! Correct transport
             DO jk = 1, jpkm1
                DO jj = 1, jpj
-                  uu_b(ji,jj,Krhs_a) = uu_b(ji,jj,Krhs_a) + e3u(ji,jj,jk,Krhs_a) * uu(ji,jj,jk,Krhs_a) * umask(ji,jj,jk)
-               END DO
-            END DO
-
-            DO jj = 1, jpj
-               uu_b(ji,jj,Krhs_a) = uu_b(ji,jj,Krhs_a) * r1_hu(ji,jj,Krhs_a)
-            END DO
-         END DO
-      ENDIF
-      !
-      DO ji = mi0(ibdy1), mi1(ibdy2)
-         zub(ji,:) = 0._wp    ! Correct transport
-         DO jk = 1, jpkm1
-            DO jj = 1, jpj
-               zub(ji,jj) = zub(ji,jj) & 
-                  & + e3u(ji,jj,jk,Krhs_a)  * uu(ji,jj,jk,Krhs_a)*umask(ji,jj,jk)
-            END DO
-         END DO
-         DO jj=1,jpj
-            zub(ji,jj) = zub(ji,jj) * r1_hu(ji,jj,Krhs_a)
-         END DO
-            
-         DO jk = 1, jpkm1
-            DO jj = 1, jpj
-               uu(ji,jj,jk,Krhs_a) = ( uu(ji,jj,jk,Krhs_a) + uu_b(ji,jj,Krhs_a)-zub(ji,jj)) * umask(ji,jj,jk)
-            END DO
-         END DO
-      END DO
-            
-      IF( ln_dynspg_ts ) THEN       ! Set tangential velocities to time splitting estimate
-         DO ji = mi0(ibdy1), mi1(ibdy2)
-            zvb(ji,:) = 0._wp
+                  zub(ji,jj) = zub(ji,jj) + e3u(ji,jj,jk,Krhs_a)  * uu(ji,jj,jk,Krhs_a) * umask(ji,jj,jk)
+               END DO
+            END DO
+            DO jj=1,jpj
+               zub(ji,jj) = zub(ji,jj) * r1_hu(ji,jj,Krhs_a)
+            END DO 
             DO jk = 1, jpkm1
                DO jj = 1, jpj
-                  zvb(ji,jj) = zvb(ji,jj) + e3v(ji,jj,jk,Krhs_a) * vv(ji,jj,jk,Krhs_a) * vmask(ji,jj,jk)
-               END DO
-            END DO
-            DO jj = 1, jpj
-               zvb(ji,jj) = zvb(ji,jj) * r1_hv(ji,jj,Krhs_a)
-            END DO
+                  uu(ji,jj,jk,Krhs_a) = ( uu(ji,jj,jk,Krhs_a) + uu_b(ji,jj,Krhs_a) - zub(ji,jj) ) * umask(ji,jj,jk)
+               END DO
+            END DO
+         END DO
+         !   
+         IF( ln_dynspg_ts ) THEN       ! Set tangential velocities to time splitting estimate
+            DO ji = mi0(ibdy1), mi1(ibdy2)
+               zvb(ji,:) = 0._wp
+               DO jk = 1, jpkm1
+                  DO jj = 1, jpj
+                     zvb(ji,jj) = zvb(ji,jj) + e3v(ji,jj,jk,Krhs_a) * vv(ji,jj,jk,Krhs_a) * vmask(ji,jj,jk)
+                  END DO
+               END DO
+               DO jj = 1, jpj
+                  zvb(ji,jj) = zvb(ji,jj) * r1_hv(ji,jj,Krhs_a)
+               END DO
+               DO jk = 1, jpkm1
+                  DO jj = 1, jpj
+                     vv(ji,jj,jk,Krhs_a) = ( vv(ji,jj,jk,Krhs_a) + vv_b(ji,jj,Krhs_a) - zvb(ji,jj) )*vmask(ji,jj,jk)
+                  END DO
+               END DO
+            END DO
+         ENDIF
+         !
+      ENDIF
+
+      ! --- East --- !
+      IF( lk_east) THEN
+         ibdy1 = jpiglo - ( nn_hls + nbghostcells + 1)   ! halo + land + nbghostcells
+         ibdy2 = jpiglo - ( nn_hls + 2 )                 ! halo + land + 1
+         !
+         IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
+            DO ji = mi0(ibdy1), mi1(ibdy2)
+               uu_b(ji,:,Krhs_a) = 0._wp
+               DO jk = 1, jpkm1
+                  DO jj = 1, jpj
+                     uu_b(ji,jj,Krhs_a) = uu_b(ji,jj,Krhs_a) + e3u(ji,jj,jk,Krhs_a) * uu(ji,jj,jk,Krhs_a) * umask(ji,jj,jk)
+                  END DO
+               END DO
+               DO jj = 1, jpj
+                  uu_b(ji,jj,Krhs_a) = uu_b(ji,jj,Krhs_a) * r1_hu(ji,jj,Krhs_a)
+               END DO
+            END DO
+         ENDIF
+         !
+         DO ji = mi0(ibdy1), mi1(ibdy2)
+            zub(ji,:) = 0._wp    ! Correct transport
             DO jk = 1, jpkm1
                DO jj = 1, jpj
-                  vv(ji,jj,jk,Krhs_a) = ( vv(ji,jj,jk,Krhs_a) + vv_b(ji,jj,Krhs_a)-zvb(ji,jj))*vmask(ji,jj,jk)
-               END DO
-            END DO
-         END DO
-      ENDIF
-
-      ! --- East --- !
-      ibdy1 = jpiglo-1-nbghostcells
-      ibdy2 = jpiglo-2 
-      !
-      IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
-         DO ji = mi0(ibdy1), mi1(ibdy2)
-            uu_b(ji,:,Krhs_a) = 0._wp
+                  zub(ji,jj) = zub(ji,jj) + e3u(ji,jj,jk,Krhs_a)  * uu(ji,jj,jk,Krhs_a) * umask(ji,jj,jk)
+               END DO
+            END DO
+            DO jj=1,jpj
+               zub(ji,jj) = zub(ji,jj) * r1_hu(ji,jj,Krhs_a)
+            END DO
             DO jk = 1, jpkm1
                DO jj = 1, jpj
-                  uu_b(ji,jj,Krhs_a) = uu_b(ji,jj,Krhs_a) & 
-                      & + e3u(ji,jj,jk,Krhs_a) * uu(ji,jj,jk,Krhs_a) * umask(ji,jj,jk)
-               END DO
-            END DO
-            DO jj = 1, jpj
-               uu_b(ji,jj,Krhs_a) = uu_b(ji,jj,Krhs_a) * r1_hu(ji,jj,Krhs_a)
-            END DO
-         END DO
-      ENDIF
-      !
-      DO ji = mi0(ibdy1), mi1(ibdy2)
-         zub(ji,:) = 0._wp    ! Correct transport
-         DO jk = 1, jpkm1
-            DO jj = 1, jpj
-               zub(ji,jj) = zub(ji,jj) & 
-                  & + e3u(ji,jj,jk,Krhs_a)  * uu(ji,jj,jk,Krhs_a) * umask(ji,jj,jk)
-            END DO
-         END DO
-         DO jj=1,jpj
-            zub(ji,jj) = zub(ji,jj) * r1_hu(ji,jj,Krhs_a)
-         END DO
-            
-         DO jk = 1, jpkm1
-            DO jj = 1, jpj
-               uu(ji,jj,jk,Krhs_a) = ( uu(ji,jj,jk,Krhs_a) & 
-                 & + uu_b(ji,jj,Krhs_a)-zub(ji,jj))*umask(ji,jj,jk)
-            END DO
-         END DO
-      END DO
-            
-      IF( ln_dynspg_ts ) THEN       ! Set tangential velocities to time splitting estimate
-         ibdy1 = jpiglo-nbghostcells
-         ibdy2 = jpiglo-1 
-         DO ji = mi0(ibdy1), mi1(ibdy2)
-            zvb(ji,:) = 0._wp
-            DO jk = 1, jpkm1
+                  uu(ji,jj,jk,Krhs_a) = ( uu(ji,jj,jk,Krhs_a) + uu_b(ji,jj,Krhs_a) - zub(ji,jj) ) * umask(ji,jj,jk)
+               END DO
+            END DO
+         END DO
+         !
+         IF( ln_dynspg_ts ) THEN       ! Set tangential velocities to time splitting estimate
+            ibdy1 = jpiglo - ( nn_hls + nbghostcells )   ! halo + land + nbghostcells - 1
+            ibdy2 = jpiglo - ( nn_hls + 1 )              ! halo + land + 1            - 1
+            DO ji = mi0(ibdy1), mi1(ibdy2)
+               zvb(ji,:) = 0._wp
+               DO jk = 1, jpkm1
+                  DO jj = 1, jpj
+                     zvb(ji,jj) = zvb(ji,jj) + e3v(ji,jj,jk,Krhs_a) * vv(ji,jj,jk,Krhs_a) * vmask(ji,jj,jk)
+                  END DO
+               END DO
                DO jj = 1, jpj
-                  zvb(ji,jj) = zvb(ji,jj) &
-                     & + e3v(ji,jj,jk,Krhs_a) * vv(ji,jj,jk,Krhs_a) * vmask(ji,jj,jk)
-               END DO
-            END DO
-            DO jj = 1, jpj
+                  zvb(ji,jj) = zvb(ji,jj) * r1_hv(ji,jj,Krhs_a)
+               END DO
+               DO jk = 1, jpkm1
+                  DO jj = 1, jpj
+                     vv(ji,jj,jk,Krhs_a) = ( vv(ji,jj,jk,Krhs_a) + vv_b(ji,jj,Krhs_a) - zvb(ji,jj) ) * vmask(ji,jj,jk)
+                  END DO
+               END DO
+            END DO
+         ENDIF
+         !
+      ENDIF
+
+      ! --- South --- !
+      IF( lk_south ) THEN
+         jbdy1 = nn_hls + 2                  ! halo + land + 1
+         jbdy2 = nn_hls + 1 + nbghostcells   ! halo + land + nbghostcells
+         !
+         IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
+            DO jj = mj0(jbdy1), mj1(jbdy2)
+               vv_b(:,jj,Krhs_a) = 0._wp
+               DO jk = 1, jpkm1
+                  DO ji = 1, jpi
+                     vv_b(ji,jj,Krhs_a) = vv_b(ji,jj,Krhs_a) + e3v(ji,jj,jk,Krhs_a) * vv(ji,jj,jk,Krhs_a) * vmask(ji,jj,jk)
+                  END DO
+               END DO
+               DO ji=1,jpi
+                  vv_b(ji,jj,Krhs_a) = vv_b(ji,jj,Krhs_a) * r1_hv(ji,jj,Krhs_a)     
+               END DO
+            END DO
+         ENDIF
+         !
+         DO jj = mj0(jbdy1), mj1(jbdy2)
+            zvb(:,jj) = 0._wp    ! Correct transport
+            DO jk=1,jpkm1
+               DO ji=1,jpi
+                  zvb(ji,jj) = zvb(ji,jj) + e3v(ji,jj,jk,Krhs_a) * vv(ji,jj,jk,Krhs_a) * vmask(ji,jj,jk)
+               END DO
+            END DO
+            DO ji = 1, jpi
                zvb(ji,jj) = zvb(ji,jj) * r1_hv(ji,jj,Krhs_a)
             END DO
-            DO jk = 1, jpkm1
-               DO jj = 1, jpj
-                  vv(ji,jj,jk,Krhs_a) = ( vv(ji,jj,jk,Krhs_a) & 
-                      & + vv_b(ji,jj,Krhs_a)-zvb(ji,jj)) * vmask(ji,jj,jk)
-               END DO
-            END DO
-         END DO
-      ENDIF
-
-      ! --- South --- !
-      jbdy1 = 2
-      jbdy2 = 1+nbghostcells 
-      !
-      IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
-         DO jj = mj0(jbdy1), mj1(jbdy2)
-            vv_b(:,jj,Krhs_a) = 0._wp
             DO jk = 1, jpkm1
                DO ji = 1, jpi
-                  vv_b(ji,jj,Krhs_a) = vv_b(ji,jj,Krhs_a) & 
-                      & + e3v(ji,jj,jk,Krhs_a) * vv(ji,jj,jk,Krhs_a) * vmask(ji,jj,jk)
-               END DO
-            END DO
-            DO ji=1,jpi
-               vv_b(ji,jj,Krhs_a) = vv_b(ji,jj,Krhs_a) * r1_hv(ji,jj,Krhs_a)     
-            END DO
-         END DO
-      ENDIF
-      !
-      DO jj = mj0(jbdy1), mj1(jbdy2)
-         zvb(:,jj) = 0._wp    ! Correct transport
-         DO jk=1,jpkm1
-            DO ji=1,jpi
-               zvb(ji,jj) = zvb(ji,jj) & 
-                  & + e3v(ji,jj,jk,Krhs_a) * vv(ji,jj,jk,Krhs_a) * vmask(ji,jj,jk)
-            END DO
-         END DO
-         DO ji = 1, jpi
-            zvb(ji,jj) = zvb(ji,jj) * r1_hv(ji,jj,Krhs_a)
-         END DO
-
-         DO jk = 1, jpkm1
+                  vv(ji,jj,jk,Krhs_a) = ( vv(ji,jj,jk,Krhs_a) + vv_b(ji,jj,Krhs_a) - zvb(ji,jj) ) * vmask(ji,jj,jk)
+               END DO
+            END DO
+         END DO
+         !
+         IF( ln_dynspg_ts ) THEN       ! Set tangential velocities to time splitting estimate
+            DO jj = mj0(jbdy1), mj1(jbdy2)
+               zub(:,jj) = 0._wp
+               DO jk = 1, jpkm1
+                  DO ji = 1, jpi
+                     zub(ji,jj) = zub(ji,jj) + e3u(ji,jj,jk,Krhs_a) * uu(ji,jj,jk,Krhs_a) * umask(ji,jj,jk)
+                  END DO
+               END DO
+               DO ji = 1, jpi
+                  zub(ji,jj) = zub(ji,jj) * r1_hu(ji,jj,Krhs_a)
+               END DO
+               DO jk = 1, jpkm1
+                  DO ji = 1, jpi
+                     uu(ji,jj,jk,Krhs_a) = ( uu(ji,jj,jk,Krhs_a) + uu_b(ji,jj,Krhs_a) - zub(ji,jj) ) * umask(ji,jj,jk)
+                  END DO
+               END DO
+            END DO
+         ENDIF
+         !
+      ENDIF
+
+      ! --- North --- !
+      IF( lk_north ) THEN
+         jbdy1 = jpjglo - ( nn_hls + nbghostcells + 1)   ! halo + land + nbghostcells
+         jbdy2 = jpjglo - ( nn_hls + 2 )                 ! halo + land + 1
+         !
+         IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
+            DO jj = mj0(jbdy1), mj1(jbdy2)
+               vv_b(:,jj,Krhs_a) = 0._wp
+               DO jk = 1, jpkm1
+                  DO ji = 1, jpi
+                     vv_b(ji,jj,Krhs_a) = vv_b(ji,jj,Krhs_a) + e3v(ji,jj,jk,Krhs_a) * vv(ji,jj,jk,Krhs_a) * vmask(ji,jj,jk)
+                  END DO
+               END DO
+               DO ji=1,jpi
+                  vv_b(ji,jj,Krhs_a) = vv_b(ji,jj,Krhs_a) * r1_hv(ji,jj,Krhs_a)
+               END DO
+            END DO
+         ENDIF
+         !
+         DO jj = mj0(jbdy1), mj1(jbdy2)
+            zvb(:,jj) = 0._wp    ! Correct transport
+            DO jk=1,jpkm1
+               DO ji=1,jpi
+                  zvb(ji,jj) = zvb(ji,jj) + e3v(ji,jj,jk,Krhs_a) * vv(ji,jj,jk,Krhs_a) * vmask(ji,jj,jk)
+               END DO
+            END DO
             DO ji = 1, jpi
-               vv(ji,jj,jk,Krhs_a) = ( vv(ji,jj,jk,Krhs_a) & 
-                 & + vv_b(ji,jj,Krhs_a) - zvb(ji,jj) ) * vmask(ji,jj,jk)
-            END DO
-         END DO
-      END DO
-            
-      IF( ln_dynspg_ts ) THEN       ! Set tangential velocities to time splitting estimate
-         DO jj = mj0(jbdy1), mj1(jbdy2)
-            zub(:,jj) = 0._wp
+               zvb(ji,jj) = zvb(ji,jj) * r1_hv(ji,jj,Krhs_a)
+            END DO
             DO jk = 1, jpkm1
                DO ji = 1, jpi
-                  zub(ji,jj) = zub(ji,jj) & 
-                     & + e3u(ji,jj,jk,Krhs_a) * uu(ji,jj,jk,Krhs_a) * umask(ji,jj,jk)
-               END DO
-            END DO
-            DO ji = 1, jpi
-               zub(ji,jj) = zub(ji,jj) * r1_hu(ji,jj,Krhs_a)
-            END DO
-               
-            DO jk = 1, jpkm1
+                  vv(ji,jj,jk,Krhs_a) = ( vv(ji,jj,jk,Krhs_a) + vv_b(ji,jj,Krhs_a) - zvb(ji,jj) ) * vmask(ji,jj,jk)
+               END DO
+            END DO
+         END DO
+         !
+         IF( ln_dynspg_ts ) THEN       ! Set tangential velocities to time splitting estimate
+            jbdy1 = jpjglo - ( nn_hls + nbghostcells )   ! halo + land + nbghostcells - 1
+            jbdy2 = jpjglo - ( nn_hls + 1 )              ! halo + land + 1            - 1
+            DO jj = mj0(jbdy1), mj1(jbdy2)
+               zub(:,jj) = 0._wp
+               DO jk = 1, jpkm1
+                  DO ji = 1, jpi
+                     zub(ji,jj) = zub(ji,jj) + e3u(ji,jj,jk,Krhs_a) * uu(ji,jj,jk,Krhs_a) * umask(ji,jj,jk)
+                  END DO
+               END DO
                DO ji = 1, jpi
-                  uu(ji,jj,jk,Krhs_a) = ( uu(ji,jj,jk,Krhs_a) & 
-                    & + uu_b(ji,jj,Krhs_a) - zub(ji,jj) ) * umask(ji,jj,jk)
-               END DO
-            END DO
-         END DO
-      ENDIF
-
-      ! --- North --- !
-      jbdy1 = jpjglo-1-nbghostcells
-      jbdy2 = jpjglo-2 
-      !
-      IF( .NOT.ln_dynspg_ts ) THEN  ! Store transport
-         DO jj = mj0(jbdy1), mj1(jbdy2)
-            vv_b(:,jj,Krhs_a) = 0._wp
-            DO jk = 1, jpkm1
-               DO ji = 1, jpi
-                  vv_b(ji,jj,Krhs_a) = vv_b(ji,jj,Krhs_a) & 
-                      & + e3v(ji,jj,jk,Krhs_a) * vv(ji,jj,jk,Krhs_a) * vmask(ji,jj,jk)
-               END DO
-            END DO
-            DO ji=1,jpi
-               vv_b(ji,jj,Krhs_a) = vv_b(ji,jj,Krhs_a) * r1_hv(ji,jj,Krhs_a)
-            END DO
-         END DO
-      ENDIF
-      !
-      DO jj = mj0(jbdy1), mj1(jbdy2)
-         zvb(:,jj) = 0._wp    ! Correct transport
-         DO jk=1,jpkm1
-            DO ji=1,jpi
-               zvb(ji,jj) = zvb(ji,jj) & 
-                  & + e3v(ji,jj,jk,Krhs_a) * vv(ji,jj,jk,Krhs_a) * vmask(ji,jj,jk)
-            END DO
-         END DO
-         DO ji = 1, jpi
-            zvb(ji,jj) = zvb(ji,jj) * r1_hv(ji,jj,Krhs_a)
-         END DO
-
-         DO jk = 1, jpkm1
-            DO ji = 1, jpi
-               vv(ji,jj,jk,Krhs_a) = ( vv(ji,jj,jk,Krhs_a) & 
-                 & + vv_b(ji,jj,Krhs_a) - zvb(ji,jj) ) * vmask(ji,jj,jk)
-            END DO
-         END DO
-      END DO
-            
-      IF( ln_dynspg_ts ) THEN       ! Set tangential velocities to time splitting estimate
-         jbdy1 = jpjglo-nbghostcells
-         jbdy2 = jpjglo-1
-         DO jj = mj0(jbdy1), mj1(jbdy2)
-            zub(:,jj) = 0._wp
-            DO jk = 1, jpkm1
-               DO ji = 1, jpi
-                  zub(ji,jj) = zub(ji,jj) & 
-                     & + e3u(ji,jj,jk,Krhs_a) * uu(ji,jj,jk,Krhs_a) * umask(ji,jj,jk)
-               END DO
-            END DO
-            DO ji = 1, jpi
-               zub(ji,jj) = zub(ji,jj) * r1_hu(ji,jj,Krhs_a)
-            END DO
-               
-            DO jk = 1, jpkm1
-               DO ji = 1, jpi
-                  uu(ji,jj,jk,Krhs_a) = ( uu(ji,jj,jk,Krhs_a) & 
-                    & + uu_b(ji,jj,Krhs_a) - zub(ji,jj) ) * umask(ji,jj,jk)
-               END DO
-            END DO
-         END DO
+                  zub(ji,jj) = zub(ji,jj) * r1_hu(ji,jj,Krhs_a)
+               END DO
+               DO jk = 1, jpkm1
+                  DO ji = 1, jpi
+                     uu(ji,jj,jk,Krhs_a) = ( uu(ji,jj,jk,Krhs_a) + uu_b(ji,jj,Krhs_a) - zub(ji,jj) ) * umask(ji,jj,jk)
+                  END DO
+               END DO
+            END DO
+         ENDIF
+         !
       ENDIF
       !
@@ -354,57 +346,68 @@
       !
       !--- West ---!
-      istart = 2
-      iend   = nbghostcells+1
-      DO ji = mi0(istart), mi1(iend)
-         DO jj=1,jpj
-            va_e(ji,jj) = vbdy(ji,jj) * hvr_e(ji,jj)
-            ua_e(ji,jj) = ubdy(ji,jj) * hur_e(ji,jj)
-         END DO
-      END DO
+      IF( lk_west ) THEN
+         istart = nn_hls + 2                              ! halo + land + 1
+         iend   = nn_hls + 1 + nbghostcells               ! halo + land + nbghostcells
+         DO ji = mi0(istart), mi1(iend)
+            DO jj=1,jpj
+               va_e(ji,jj) = vbdy(ji,jj) * hvr_e(ji,jj)
+               ua_e(ji,jj) = ubdy(ji,jj) * hur_e(ji,jj)
+            END DO
+         END DO
+      ENDIF
       !
       !--- East ---!
-      istart = jpiglo-nbghostcells
-      iend   = jpiglo-1
-      DO ji = mi0(istart), mi1(iend)
-         DO jj=1,jpj
-            va_e(ji,jj) = vbdy(ji,jj) * hvr_e(ji,jj)
-         END DO
-      END DO
-      istart = jpiglo-nbghostcells-1
-      iend   = jpiglo-2
-      DO ji = mi0(istart), mi1(iend)
-         DO jj=1,jpj
-            ua_e(ji,jj) = ubdy(ji,jj) * hur_e(ji,jj)
-         END DO
-      END DO
+      IF( lk_east ) THEN
+         istart = jpiglo - ( nn_hls + nbghostcells )      ! halo + land + nbghostcells - 1
+         iend   = jpiglo - ( nn_hls + 1 )                 ! halo + land + 1            - 1
+         DO ji = mi0(istart), mi1(iend)
+
+            DO jj=1,jpj
+               va_e(ji,jj) = vbdy(ji,jj) * hvr_e(ji,jj)
+            END DO
+         END DO
+         istart = jpiglo - ( nn_hls + nbghostcells + 1)   ! halo + land + nbghostcells
+         iend   = jpiglo - ( nn_hls + 2 )                 ! halo + land + 1
+         DO ji = mi0(istart), mi1(iend)
+            DO jj=1,jpj
+               ua_e(ji,jj) = ubdy(ji,jj) * hur_e(ji,jj)
+            END DO
+         END DO
+      ENDIF 
       !
       !--- South ---!
-      jstart = 2
-      jend   = nbghostcells+1
-      DO jj = mj0(jstart), mj1(jend)
-         DO ji=1,jpi
-            ua_e(ji,jj) = ubdy(ji,jj) * hur_e(ji,jj)
-            va_e(ji,jj) = vbdy(ji,jj) * hvr_e(ji,jj)
-         END DO
-      END DO
+      IF( lk_south ) THEN
+         jstart = nn_hls + 2                              ! halo + land + 1
+         jend   = nn_hls + 1 + nbghostcells               ! halo + land + nbghostcells
+         DO jj = mj0(jstart), mj1(jend)
+
+            DO ji=1,jpi
+               ua_e(ji,jj) = ubdy(ji,jj) * hur_e(ji,jj)
+               va_e(ji,jj) = vbdy(ji,jj) * hvr_e(ji,jj)
+            END DO
+         END DO
+      ENDIF       
       !
       !--- North ---!
-      jstart = jpjglo-nbghostcells
-      jend   = jpjglo-1
-      DO jj = mj0(jstart), mj1(jend)
-         DO ji=1,jpi
-            ua_e(ji,jj) = ubdy(ji,jj) * hur_e(ji,jj)
-         END DO
-      END DO
-      jstart = jpjglo-nbghostcells-1
-      jend   = jpjglo-2
-      DO jj = mj0(jstart), mj1(jend)
-         DO ji=1,jpi
-            va_e(ji,jj) = vbdy(ji,jj) * hvr_e(ji,jj)
-         END DO
-      END DO
+      IF( lk_north ) THEN
+         jstart = jpjglo - ( nn_hls + nbghostcells )      ! halo + land + nbghostcells - 1
+         jend   = jpjglo - ( nn_hls + 1 )                 ! halo + land + 1            - 1
+         DO jj = mj0(jstart), mj1(jend)
+            DO ji=1,jpi
+               ua_e(ji,jj) = ubdy(ji,jj) * hur_e(ji,jj)
+            END DO
+         END DO
+         jstart = jpjglo - ( nn_hls + nbghostcells + 1)   ! halo + land + nbghostcells
+         jend   = jpjglo - ( nn_hls + 2 )                 ! halo + land + 1
+         DO jj = mj0(jstart), mj1(jend)
+            DO ji=1,jpi
+               va_e(ji,jj) = vbdy(ji,jj) * hvr_e(ji,jj)
+            END DO
+         END DO
+      ENDIF 
       !
    END SUBROUTINE Agrif_dyn_ts
 
+   
    SUBROUTINE Agrif_dyn_ts_flux( jn, zu, zv )
       !!----------------------------------------------------------------------
@@ -421,57 +424,66 @@
       !
       !--- West ---!
-      istart = 2
-      iend   = nbghostcells+1
-      DO ji = mi0(istart), mi1(iend)
-         DO jj=1,jpj
-            zv(ji,jj) = vbdy(ji,jj) * e1v(ji,jj)
-            zu(ji,jj) = ubdy(ji,jj) * e2u(ji,jj)
-         END DO
-      END DO
+      IF( lk_west ) THEN
+         istart = nn_hls + 2                              ! halo + land + 1
+         iend   = nn_hls + 1 + nbghostcells               ! halo + land + nbghostcells
+         DO ji = mi0(istart), mi1(iend)
+            DO jj=1,jpj
+               zv(ji,jj) = vbdy(ji,jj) * e1v(ji,jj)
+               zu(ji,jj) = ubdy(ji,jj) * e2u(ji,jj)
+            END DO
+         END DO
+      ENDIF
       !
       !--- East ---!
-      istart = jpiglo-nbghostcells
-      iend   = jpiglo-1
-      DO ji = mi0(istart), mi1(iend)
-         DO jj=1,jpj
-            zv(ji,jj) = vbdy(ji,jj) * e1v(ji,jj)
-         END DO
-      END DO
-      istart = jpiglo-nbghostcells-1
-      iend   = jpiglo-2
-      DO ji = mi0(istart), mi1(iend)
-         DO jj=1,jpj
-            zu(ji,jj) = ubdy(ji,jj) * e2u(ji,jj)
-         END DO
-      END DO
+      IF( lk_east ) THEN
+         istart = jpiglo - ( nn_hls + nbghostcells )      ! halo + land + nbghostcells - 1
+         iend   = jpiglo - ( nn_hls + 1 )                 ! halo + land + 1            - 1
+         DO ji = mi0(istart), mi1(iend)
+            DO jj=1,jpj
+               zv(ji,jj) = vbdy(ji,jj) * e1v(ji,jj)
+            END DO
+         END DO
+         istart = jpiglo - ( nn_hls + nbghostcells + 1)   ! halo + land + nbghostcells
+         iend   = jpiglo - ( nn_hls + 2 )                 ! halo + land + 1
+         DO ji = mi0(istart), mi1(iend)
+            DO jj=1,jpj
+               zu(ji,jj) = ubdy(ji,jj) * e2u(ji,jj)
+            END DO
+         END DO
+      ENDIF
       !
       !--- South ---!
-      jstart = 2
-      jend   = nbghostcells+1
-      DO jj = mj0(jstart), mj1(jend)
-         DO ji=1,jpi
-            zu(ji,jj) = ubdy(ji,jj) * e2u(ji,jj)
-            zv(ji,jj) = vbdy(ji,jj) * e1v(ji,jj)
-         END DO
-      END DO
+      IF( lk_south ) THEN
+         jstart = nn_hls + 2                              ! halo + land + 1
+         jend   = nn_hls + 1 + nbghostcells               ! halo + land + nbghostcells
+         DO jj = mj0(jstart), mj1(jend)
+            DO ji=1,jpi
+               zu(ji,jj) = ubdy(ji,jj) * e2u(ji,jj)
+               zv(ji,jj) = vbdy(ji,jj) * e1v(ji,jj)
+            END DO
+         END DO
+      ENDIF
       !
       !--- North ---!
-      jstart = jpjglo-nbghostcells
-      jend   = jpjglo-1
-      DO jj = mj0(jstart), mj1(jend)
-         DO ji=1,jpi
-            zu(ji,jj) = ubdy(ji,jj) * e2u(ji,jj)
-         END DO
-      END DO
-      jstart = jpjglo-nbghostcells-1
-      jend   = jpjglo-2
-      DO jj = mj0(jstart), mj1(jend)
-         DO ji=1,jpi
-            zv(ji,jj) = vbdy(ji,jj) * e1v(ji,jj)
-         END DO
-      END DO
+      IF( lk_north ) THEN
+         jstart = jpjglo - ( nn_hls + nbghostcells )      ! halo + land + nbghostcells - 1
+         jend   = jpjglo - ( nn_hls + 1 )                 ! halo + land + 1            - 1
+         DO jj = mj0(jstart), mj1(jend)
+            DO ji=1,jpi
+               zu(ji,jj) = ubdy(ji,jj) * e2u(ji,jj)
+            END DO
+         END DO
+         jstart = jpjglo - ( nn_hls + nbghostcells + 1)   ! halo + land + nbghostcells
+         jend   = jpjglo - ( nn_hls + 2 )                 ! halo + land + 1
+         DO jj = mj0(jstart), mj1(jend)
+            DO ji=1,jpi
+               zv(ji,jj) = vbdy(ji,jj) * e1v(ji,jj)
+            END DO
+         END DO
+      ENDIF
       !
    END SUBROUTINE Agrif_dyn_ts_flux
 
+   
    SUBROUTINE Agrif_dta_ts( kt )
       !!----------------------------------------------------------------------
@@ -494,4 +506,8 @@
       Agrif_SpecialValue = 0._wp
       Agrif_UseSpecialValue = ln_spc_dyn
+
+      use_sign_north = .TRUE.
+      sign_north = -1.
+
       !
       ! Set bdy time interpolation stage to 0 (latter incremented locally do deal with corners)
@@ -518,4 +534,5 @@
       ENDIF
       Agrif_UseSpecialValue = .FALSE.
+      use_sign_north = .FALSE.
       ! 
    END SUBROUTINE Agrif_dta_ts
@@ -542,38 +559,46 @@
       !
       ! --- West --- !
-      istart = 2
-      iend   = 1 + nbghostcells
-      DO ji = mi0(istart), mi1(iend)
-         DO jj = 1, jpj
-            ssh(ji,jj,Krhs_a) = hbdy(ji,jj)
-         ENDDO
-      ENDDO
+      IF(lk_west) THEN
+         istart = nn_hls + 2                              ! halo + land + 1
+         iend   = nn_hls + 1 + nbghostcells               ! halo + land + nbghostcells
+         DO ji = mi0(istart), mi1(iend)
+            DO jj = 1, jpj
+               ssh(ji,jj,Krhs_a) = hbdy(ji,jj)
+            END DO
+         END DO
+      ENDIF
       !
       ! --- East --- !
-      istart = jpiglo - nbghostcells
-      iend   = jpiglo - 1
-      DO ji = mi0(istart), mi1(iend)
-         DO jj = 1, jpj
-            ssh(ji,jj,Krhs_a) = hbdy(ji,jj)
-         ENDDO
-      ENDDO
+      IF(lk_east) THEN
+         istart = jpiglo - ( nn_hls + nbghostcells )      ! halo + land + nbghostcells - 1
+         iend   = jpiglo - ( nn_hls + 1 )                 ! halo + land + 1            - 1
+         DO ji = mi0(istart), mi1(iend)
+            DO jj = 1, jpj
+               ssh(ji,jj,Krhs_a) = hbdy(ji,jj)
+            END DO
+         END DO
+      ENDIF
       !
       ! --- South --- !
-      jstart = 2
-      jend   = 1 + nbghostcells
-      DO jj = mj0(jstart), mj1(jend)
-         DO ji = 1, jpi
-            ssh(ji,jj,Krhs_a) = hbdy(ji,jj)
-         ENDDO
-      ENDDO
+      IF(lk_south) THEN
+         jstart = nn_hls + 2                              ! halo + land + 1
+         jend   = nn_hls + 1 + nbghostcells               ! halo + land + nbghostcells
+         DO jj = mj0(jstart), mj1(jend)
+            DO ji = 1, jpi
+               ssh(ji,jj,Krhs_a) = hbdy(ji,jj)
+            END DO
+         END DO
+      ENDIF
       !
       ! --- North --- !
-      jstart = jpjglo - nbghostcells
-      jend   = jpjglo - 1
-      DO jj = mj0(jstart), mj1(jend)
-         DO ji = 1, jpi
-            ssh(ji,jj,Krhs_a) = hbdy(ji,jj)
-         ENDDO
-      ENDDO
+      IF(lk_north) THEN
+         jstart = jpjglo - ( nn_hls + nbghostcells )      ! halo + land + nbghostcells - 1
+         jend   = jpjglo - ( nn_hls + 1 )                 ! halo + land + 1            - 1
+         DO jj = mj0(jstart), mj1(jend)
+            DO ji = 1, jpi
+               ssh(ji,jj,Krhs_a) = hbdy(ji,jj)
+            END DO
+         END DO
+      ENDIF
       !
    END SUBROUTINE Agrif_ssh
@@ -593,41 +618,50 @@
       !
       ! --- West --- !
-      istart = 2
-      iend   = 1+nbghostcells
-      DO ji = mi0(istart), mi1(iend)
-         DO jj = 1, jpj
-            ssha_e(ji,jj) = hbdy(ji,jj)
-         ENDDO
-      ENDDO
+      IF(lk_west) THEN
+         istart = nn_hls + 2                              ! halo + land + 1
+         iend   = nn_hls + 1 + nbghostcells               ! halo + land + nbghostcells
+         DO ji = mi0(istart), mi1(iend)
+            DO jj = 1, jpj
+               ssha_e(ji,jj) = hbdy(ji,jj)
+            END DO
+         END DO
+      ENDIF
       !
       ! --- East --- !
-      istart = jpiglo - nbghostcells
-      iend   = jpiglo - 1
-      DO ji = mi0(istart), mi1(iend)
-         DO jj = 1, jpj
-            ssha_e(ji,jj) = hbdy(ji,jj)
-         ENDDO
-      ENDDO
+      IF(lk_east) THEN
+         istart = jpiglo - ( nn_hls + nbghostcells )      ! halo + land + nbghostcells - 1
+         iend   = jpiglo - ( nn_hls + 1 )                 ! halo + land + 1            - 1
+         DO ji = mi0(istart), mi1(iend)
+            DO jj = 1, jpj
+               ssha_e(ji,jj) = hbdy(ji,jj)
+            END DO
+         END DO
+      ENDIF
       !
       ! --- South --- !
-      jstart = 2
-      jend   = 1+nbghostcells
-      DO jj = mj0(jstart), mj1(jend)
-         DO ji = 1, jpi
-            ssha_e(ji,jj) = hbdy(ji,jj)
-         ENDDO
-      ENDDO
+      IF(lk_south) THEN
+         jstart = nn_hls + 2                              ! halo + land + 1
+         jend   = nn_hls + 1 + nbghostcells               ! halo + land + nbghostcells
+         DO jj = mj0(jstart), mj1(jend)
+            DO ji = 1, jpi
+               ssha_e(ji,jj) = hbdy(ji,jj)
+            END DO
+         END DO
+      ENDIF
       !
       ! --- North --- !
-      jstart = jpjglo - nbghostcells
-      jend   = jpjglo - 1
-      DO jj = mj0(jstart), mj1(jend)
-         DO ji = 1, jpi
-            ssha_e(ji,jj) = hbdy(ji,jj)
-         ENDDO
-      ENDDO
+      IF(lk_north) THEN
+         jstart = jpjglo - ( nn_hls + nbghostcells )      ! halo + land + nbghostcells - 1
+         jend   = jpjglo - ( nn_hls + 1 )                 ! halo + land + 1            - 1
+         DO jj = mj0(jstart), mj1(jend)
+            DO ji = 1, jpi
+               ssha_e(ji,jj) = hbdy(ji,jj)
+            END DO
+         END DO
+      ENDIF
       !
    END SUBROUTINE Agrif_ssh_ts
 
+   
    SUBROUTINE Agrif_avm
       !!----------------------------------------------------------------------
@@ -650,5 +684,5 @@
       !
    END SUBROUTINE Agrif_avm
-   
+
 
    SUBROUTINE interptsn( ptab, i1, i2, j1, j2, k1, k2, n1, n2, before )
@@ -662,12 +696,17 @@
       INTEGER  ::   ji, jj, jk, jn  ! dummy loop indices
       INTEGER  ::   N_in, N_out
+      INTEGER  :: item
       ! vertical interpolation:
       REAL(wp) :: zhtot
       REAL(wp), DIMENSION(k1:k2,1:jpts) :: tabin
-      REAL(wp), DIMENSION(k1:k2) :: h_in
-      REAL(wp), DIMENSION(1:jpk) :: h_out
-      !!----------------------------------------------------------------------
-
-      IF( before ) THEN         
+      REAL(wp), DIMENSION(k1:k2) :: h_in, z_in
+      REAL(wp), DIMENSION(1:jpk) :: h_out, z_out
+      !!----------------------------------------------------------------------
+
+      IF( before ) THEN
+
+         item = Kmm_a
+         IF( l_ini_child )   Kmm_a = Kbb_a  
+
          DO jn = 1,jpts
             DO jk=k1,k2
@@ -678,71 +717,96 @@
               END DO
            END DO
-        END DO
-
-# if defined key_vertical
-        ! Interpolate thicknesses
-        ! Warning: these are masked, hence extrapolated prior interpolation.
-        DO jk=k1,k2
-           DO jj=j1,j2
-              DO ji=i1,i2
-                  ptab(ji,jj,jk,jpts+1) = tmask(ji,jj,jk) * e3t(ji,jj,jk,Kmm_a)
-              END DO
-           END DO
-        END DO
-
-        ! Extrapolate thicknesses in partial bottom cells:
-        ! Set them to Agrif_SpecialValue (0.). Correct bottom thicknesses are retrieved later on
-        IF (ln_zps) THEN
-           DO jj=j1,j2
-              DO ji=i1,i2
-                  jk = mbkt(ji,jj)
-                  ptab(ji,jj,jk,jpts+1) = 0._wp
-              END DO
-           END DO           
-        END IF
-     
-        ! Save ssh at last level:
-        IF (.NOT.ln_linssh) THEN
-           ptab(i1:i2,j1:j2,k2,jpts+1) = ssh(i1:i2,j1:j2,Kmm_a)*tmask(i1:i2,j1:j2,1) 
-        ELSE
-           ptab(i1:i2,j1:j2,k2,jpts+1) = 0._wp
-        END IF      
-# endif
+         END DO
+
+         IF( l_vremap .OR. l_ini_child) THEN
+            ! Interpolate thicknesses
+            ! Warning: these are masked, hence extrapolated prior interpolation.
+            DO jk=k1,k2
+               DO jj=j1,j2
+                  DO ji=i1,i2
+                      ptab(ji,jj,jk,jpts+1) = tmask(ji,jj,jk) * e3t(ji,jj,jk,Kmm_a)
+
+                  END DO
+               END DO
+            END DO
+
+            ! Extrapolate thicknesses in partial bottom cells:
+            ! Set them to Agrif_SpecialValue (0.). Correct bottom thicknesses are retrieved later on
+            IF (ln_zps) THEN
+               DO jj=j1,j2
+                  DO ji=i1,i2
+                      jk = mbkt(ji,jj)
+                      ptab(ji,jj,jk,jpts+1) = 0._wp
+                  END DO
+               END DO           
+            END IF
+        
+            ! Save ssh at last level:
+            IF (.NOT.ln_linssh) THEN
+               ptab(i1:i2,j1:j2,k2,jpts+1) = ssh(i1:i2,j1:j2,Kmm_a)*tmask(i1:i2,j1:j2,1) 
+            ELSE
+               ptab(i1:i2,j1:j2,k2,jpts+1) = 0._wp
+            END IF      
+         ENDIF
+         Kmm_a = item
+
       ELSE 
-
-# if defined key_vertical 
-         IF (ln_linssh) ptab(i1:i2,j1:j2,k2,n2) = 0._wp 
-            
-         DO jj=j1,j2
-            DO ji=i1,i2
-               ts(ji,jj,:,:,Krhs_a) = 0._wp
-               N_in = mbkt_parent(ji,jj)
-               zhtot = 0._wp
-               DO jk=1,N_in !k2 = jpk of parent grid
-                  IF (jk==N_in) THEN
-                     h_in(jk) = ht0_parent(ji,jj) + ptab(ji,jj,k2,n2) - zhtot
-                  ELSE
-                     h_in(jk) = ptab(ji,jj,jk,n2)
+         item = Krhs_a
+         IF( l_ini_child )   Krhs_a = Kbb_a  
+
+         IF( l_vremap .OR. l_ini_child ) THEN
+            IF (ln_linssh) ptab(i1:i2,j1:j2,k2,n2) = 0._wp 
+               
+            DO jj=j1,j2
+               DO ji=i1,i2
+                  ts(ji,jj,:,:,Krhs_a) = 0.                  
+               !   IF( l_ini_child) ts(ji,jj,:,:,Krhs_a) = ptab(ji,jj,:,1:jpts)
+                  N_in = mbkt_parent(ji,jj)
+                  zhtot = 0._wp
+                  DO jk=1,N_in !k2 = jpk of parent grid
+                     IF (jk==N_in) THEN
+                        h_in(jk) = ht0_parent(ji,jj) + ptab(ji,jj,k2,n2) - zhtot
+                     ELSE
+                        h_in(jk) = ptab(ji,jj,jk,n2)
+                     ENDIF
+                     zhtot = zhtot + h_in(jk)
+                     tabin(jk,:) = ptab(ji,jj,jk,n1:n2-1)
+                  END DO
+                  z_in(1) = 0.5_wp * h_in(1) - zhtot + ht0_parent(ji,jj)
+                  DO jk=2,N_in
+                     z_in(jk) = z_in(jk-1) + 0.5_wp * h_in(jk)
+                  END DO
+
+                  N_out = 0
+                  DO jk=1,jpk ! jpk of child grid
+                     IF (tmask(ji,jj,jk) == 0._wp) EXIT 
+                     N_out = N_out + 1
+                     h_out(jk) = e3t(ji,jj,jk,Krhs_a)
+                  END DO
+
+                  z_out(1) = 0.5_wp * h_out(1) - SUM(h_out(1:N_out)) + ht_0(ji,jj)
+                  DO jk=2,N_out
+                     z_out(jk) = z_out(jk-1) + 0.5_wp * h_out(jk)
+                  END DO
+
+                  IF (N_in*N_out > 0) THEN
+                     IF( l_ini_child ) THEN
+                        CALL remap_linear(tabin(1:N_in,1:jpts),z_in(1:N_in),ts(ji,jj,1:N_out,1:jpts,Krhs_a),          &
+                                      &   z_out(1:N_out),N_in,N_out,jpts)  
+                     ELSE 
+                        CALL reconstructandremap(tabin(1:N_in,1:jpts),h_in(1:N_in),ts(ji,jj,1:N_out,1:jpts,Krhs_a),   &
+                                      &   h_out(1:N_out),N_in,N_out,jpts)  
+                     ENDIF
                   ENDIF
-                  zhtot = zhtot + h_in(jk)
-                  tabin(jk,:) = ptab(ji,jj,jk,n1:n2-1)
-               END DO
-               N_out = 0
-               DO jk=1,jpk ! jpk of child grid
-                  IF (tmask(ji,jj,jk) == 0._wp) EXIT 
-                  N_out = N_out + 1
-                  h_out(jk) = e3t(ji,jj,jk,Krhs_a)
-               ENDDO
-               IF (N_in*N_out > 0) THEN
-                  CALL reconstructandremap(tabin(1:N_in,1:jpts),h_in(1:N_in),ts(ji,jj,1:N_out,1:jpts,Krhs_a),h_out(1:N_out),N_in,N_out,jpts)
-               ENDIF
-            ENDDO
-         ENDDO
-# else
-         !
-         DO jn=1, jpts
-            ts(i1:i2,j1:j2,1:jpk,jn,Krhs_a)=ptab(i1:i2,j1:j2,1:jpk,jn)*tmask(i1:i2,j1:j2,1:jpk) 
-         END DO
-# endif
+               END DO
+            END DO
+            Krhs_a = item
+ 
+         ELSE
+         
+            DO jn=1, jpts
+                ts(i1:i2,j1:j2,1:jpk,jn,Krhs_a)=ptab(i1:i2,j1:j2,1:jpk,jn)*tmask(i1:i2,j1:j2,1:jpk) 
+            END DO
+         ENDIF
 
       ENDIF
@@ -750,4 +814,5 @@
    END SUBROUTINE interptsn
 
+   
    SUBROUTINE interpsshn( ptab, i1, i2, j1, j2, before )
       !!----------------------------------------------------------------------
@@ -768,4 +833,5 @@
    END SUBROUTINE interpsshn
 
+   
    SUBROUTINE interpun( ptab, i1, i2, j1, j2, k1, k2, m1, m2, before )
       !!----------------------------------------------------------------------
@@ -780,85 +846,111 @@
       REAL(wp) :: zrhoy, zhtot
       ! vertical interpolation:
-      REAL(wp), DIMENSION(k1:k2) :: tabin, h_in
-      REAL(wp), DIMENSION(1:jpk) :: h_out
-      INTEGER  :: N_in, N_out
+      REAL(wp), DIMENSION(k1:k2) :: tabin, h_in, z_in
+      REAL(wp), DIMENSION(1:jpk) :: h_out, z_out
+      INTEGER  :: N_in, N_out,item
       REAL(wp) :: h_diff
       !!---------------------------------------------    
       !
       IF (before) THEN 
+
+         item = Kmm_a
+         IF( l_ini_child )   Kmm_a = Kbb_a     
+
          DO jk=1,jpk
             DO jj=j1,j2
                DO ji=i1,i2
                   ptab(ji,jj,jk,1) = (e2u(ji,jj) * e3u(ji,jj,jk,Kmm_a) * uu(ji,jj,jk,Kmm_a)*umask(ji,jj,jk)) 
-# if defined key_vertical
-                  ! Interpolate thicknesses (masked for subsequent extrapolation)
-                  ptab(ji,jj,jk,2) = umask(ji,jj,jk) * e2u(ji,jj) * e3u(ji,jj,jk,Kmm_a)
-# endif
-               END DO
-            END DO
-         END DO
-# if defined key_vertical
+                  IF( l_vremap .OR. l_ini_child) THEN
+                     ! Interpolate thicknesses (masked for subsequent extrapolation)
+                     ptab(ji,jj,jk,2) = umask(ji,jj,jk) * e2u(ji,jj) * e3u(ji,jj,jk,Kmm_a)
+                  ENDIF
+               END DO
+            END DO
+         END DO
+
+        IF( l_vremap .OR. l_ini_child) THEN
          ! Extrapolate thicknesses in partial bottom cells:
          ! Set them to Agrif_SpecialValue (0.). Correct bottom thicknesses are retrieved later on
-         IF (ln_zps) THEN
-            DO jj=j1,j2
-               DO ji=i1,i2
-                  jk = mbku(ji,jj)
-                  ptab(ji,jj,jk,2) = 0._wp
-               END DO
-            END DO           
-         END IF
-        ! Save ssh at last level:
-        ptab(i1:i2,j1:j2,k2,2) = 0._wp
-        IF (.NOT.ln_linssh) THEN
-           ! This vertical sum below should be replaced by the sea-level at U-points (optimization):
-           DO jk=1,jpk
-              ptab(i1:i2,j1:j2,k2,2) = ptab(i1:i2,j1:j2,k2,2) + e3u(i1:i2,j1:j2,jk,Kmm_a) * umask(i1:i2,j1:j2,jk)
-           END DO
-           ptab(i1:i2,j1:j2,k2,2) = ptab(i1:i2,j1:j2,k2,2) - hu_0(i1:i2,j1:j2)
-        END IF 
-# endif
+            IF (ln_zps) THEN
+               DO jj=j1,j2
+                  DO ji=i1,i2
+                     jk = mbku(ji,jj)
+                     ptab(ji,jj,jk,2) = 0._wp
+                  END DO
+               END DO           
+            END IF
+
+           ! Save ssh at last level:
+           ptab(i1:i2,j1:j2,k2,2) = 0._wp
+           IF (.NOT.ln_linssh) THEN
+              ! This vertical sum below should be replaced by the sea-level at U-points (optimization):
+              DO jk=1,jpk
+                 ptab(i1:i2,j1:j2,k2,2) = ptab(i1:i2,j1:j2,k2,2) + e3u(i1:i2,j1:j2,jk,Kmm_a) * umask(i1:i2,j1:j2,jk)
+              END DO
+              ptab(i1:i2,j1:j2,k2,2) = ptab(i1:i2,j1:j2,k2,2) - hu_0(i1:i2,j1:j2)
+           END IF
+        ENDIF
+
+         Kmm_a = item
          !
       ELSE
          zrhoy = Agrif_rhoy()
-# if defined key_vertical
+
+        IF( l_vremap .OR. l_ini_child) THEN
 ! VERTICAL REFINEMENT BEGIN
 
-         IF (ln_linssh) ptab(i1:i2,j1:j2,k2,2) = 0._wp 
-
-         DO ji=i1,i2
-            DO jj=j1,j2
-               uu(ji,jj,:,Krhs_a) = 0._wp
-               N_in = mbku_parent(ji,jj)
-               zhtot = 0._wp
-               DO jk=1,N_in
-                  IF (jk==N_in) THEN
-                     h_in(jk) = hu0_parent(ji,jj) + ptab(ji,jj,k2,2) - zhtot
-                  ELSE
-                     h_in(jk) = ptab(ji,jj,jk,2)/(e2u(ji,jj)*zrhoy) 
-                  ENDIF
-                  zhtot = zhtot + h_in(jk)
-                  tabin(jk) = ptab(ji,jj,jk,1)/(e2u(ji,jj)*zrhoy*h_in(jk))
-              ENDDO
-                  
-              N_out = 0
-              DO jk=1,jpk
-                 if (umask(ji,jj,jk) == 0) EXIT
-                 N_out = N_out + 1
-                 h_out(N_out) = e3u(ji,jj,jk,Krhs_a)
-              ENDDO
-              IF (N_in*N_out > 0) THEN
-                 CALL reconstructandremap(tabin(1:N_in),h_in(1:N_in),uu(ji,jj,1:N_out,Krhs_a),h_out(1:N_out),N_in,N_out,1)
-              ENDIF
-            ENDDO
-         ENDDO
-
-# else
-         DO jk = 1, jpkm1
-            DO jj=j1,j2
-               uu(i1:i2,jj,jk,Krhs_a) = ptab(i1:i2,jj,jk,1) / ( zrhoy * e2u(i1:i2,jj) * e3u(i1:i2,jj,jk,Krhs_a) )
-            END DO
-         END DO
-# endif
+            IF (ln_linssh) ptab(i1:i2,j1:j2,k2,2) = 0._wp 
+
+            DO ji=i1,i2
+               DO jj=j1,j2
+                  uu(ji,jj,:,Krhs_a) = 0._wp
+                  N_in = mbku_parent(ji,jj)
+                  zhtot = 0._wp
+                  DO jk=1,N_in
+                     IF (jk==N_in) THEN
+                        h_in(jk) = hu0_parent(ji,jj) + ptab(ji,jj,k2,2) - zhtot
+                     ELSE
+                        h_in(jk) = ptab(ji,jj,jk,2)/(e2u(ji,jj)*zrhoy) 
+                     ENDIF
+                     zhtot = zhtot + h_in(jk)
+                     IF( h_in(jk) .GT. 0. ) THEN
+                     tabin(jk) = ptab(ji,jj,jk,1)/(e2u(ji,jj)*zrhoy*h_in(jk))
+                     ELSE
+                     tabin(jk) = 0.
+                     ENDIF
+                 END DO
+                 z_in(1) = 0.5_wp * h_in(1) - zhtot + hu0_parent(ji,jj) 
+                 DO jk=2,N_in
+                    z_in(jk) = z_in(jk-1) + 0.5_wp * h_in(jk)
+                 END DO
+                     
+                 N_out = 0
+                 DO jk=1,jpk
+                    IF (umask(ji,jj,jk) == 0) EXIT
+                    N_out = N_out + 1
+                    h_out(N_out) = e3u(ji,jj,jk,Krhs_a)
+                 END DO
+
+                 z_out(1) = 0.5_wp * h_out(1) - SUM(h_out(1:N_out)) + hu_0(ji,jj)
+                 DO jk=2,N_out
+                    z_out(jk) = z_out(jk-1) + 0.5_wp * h_out(jk) 
+                 END DO  
+
+                 IF (N_in*N_out > 0) THEN
+                     IF( l_ini_child ) THEN
+                        CALL remap_linear       (tabin(1:N_in),z_in(1:N_in),uu(ji,jj,1:N_out,Krhs_a),z_out(1:N_out),N_in,N_out,1)
+                     ELSE
+                        CALL reconstructandremap(tabin(1:N_in),h_in(1:N_in),uu(ji,jj,1:N_out,Krhs_a),h_out(1:N_out),N_in,N_out,1)
+                     ENDIF   
+                 ENDIF
+               END DO
+            END DO
+         ELSE
+            DO jk = 1, jpkm1
+               DO jj=j1,j2
+                  uu(i1:i2,jj,jk,Krhs_a) = ptab(i1:i2,jj,jk,1) / ( zrhoy * e2u(i1:i2,jj) * e3u(i1:i2,jj,jk,Krhs_a) )
+               END DO
+            END DO
+         ENDIF
 
       ENDIF
@@ -866,4 +958,5 @@
    END SUBROUTINE interpun
 
+   
    SUBROUTINE interpvn( ptab, i1, i2, j1, j2, k1, k2, m1, m2, before )
       !!----------------------------------------------------------------------
@@ -878,80 +971,107 @@
       REAL(wp) :: zrhox
       ! vertical interpolation:
-      REAL(wp), DIMENSION(k1:k2) :: tabin, h_in
-      REAL(wp), DIMENSION(1:jpk) :: h_out
-      INTEGER  :: N_in, N_out
+      REAL(wp), DIMENSION(k1:k2) :: tabin, h_in, z_in
+      REAL(wp), DIMENSION(1:jpk) :: h_out, z_out
+      INTEGER  :: N_in, N_out, item
       REAL(wp) :: h_diff, zhtot
       !!---------------------------------------------    
       !      
-      IF (before) THEN          
+      IF (before) THEN   
+
+         item = Kmm_a
+         IF( l_ini_child )   Kmm_a = Kbb_a     
+       
          DO jk=k1,k2
             DO jj=j1,j2
                DO ji=i1,i2
                   ptab(ji,jj,jk,1) = (e1v(ji,jj) * e3v(ji,jj,jk,Kmm_a) * vv(ji,jj,jk,Kmm_a)*vmask(ji,jj,jk))
-# if defined key_vertical
-                  ! Interpolate thicknesses (masked for subsequent extrapolation)
-                  ptab(ji,jj,jk,2) = vmask(ji,jj,jk) * e1v(ji,jj) * e3v(ji,jj,jk,Kmm_a)
-# endif
-               END DO
-            END DO
-         END DO
-# if defined key_vertical
+                  IF( l_vremap .OR. l_ini_child) THEN
+                     ! Interpolate thicknesses (masked for subsequent extrapolation)
+                     ptab(ji,jj,jk,2) = vmask(ji,jj,jk) * e1v(ji,jj) * e3v(ji,jj,jk,Kmm_a)
+                  ENDIF
+               END DO
+            END DO
+         END DO
+
+         IF( l_vremap .OR. l_ini_child) THEN
          ! Extrapolate thicknesses in partial bottom cells:
          ! Set them to Agrif_SpecialValue (0.). Correct bottom thicknesses are retrieved later on
-         IF (ln_zps) THEN
+            IF (ln_zps) THEN
+               DO jj=j1,j2
+                  DO ji=i1,i2
+                     jk = mbkv(ji,jj)
+                     ptab(ji,jj,jk,2) = 0._wp
+                  END DO
+               END DO           
+            END IF
+            ! Save ssh at last level:
+            ptab(i1:i2,j1:j2,k2,2) = 0._wp
+            IF (.NOT.ln_linssh) THEN
+               ! This vertical sum below should be replaced by the sea-level at V-points (optimization):
+               DO jk=1,jpk
+                  ptab(i1:i2,j1:j2,k2,2) = ptab(i1:i2,j1:j2,k2,2) + e3v(i1:i2,j1:j2,jk,Kmm_a) * vmask(i1:i2,j1:j2,jk)
+               END DO
+               ptab(i1:i2,j1:j2,k2,2) = ptab(i1:i2,j1:j2,k2,2) - hv_0(i1:i2,j1:j2)
+            END IF 
+         ENDIF
+         item = Kmm_a
+
+      ELSE       
+         zrhox = Agrif_rhox()
+
+         IF( l_vremap .OR. l_ini_child ) THEN
+
+            IF (ln_linssh) ptab(i1:i2,j1:j2,k2,2) = 0._wp 
+
             DO jj=j1,j2
                DO ji=i1,i2
-                  jk = mbkv(ji,jj)
-                  ptab(ji,jj,jk,2) = 0._wp
-               END DO
-            END DO           
-         END IF
-        ! Save ssh at last level:
-        ptab(i1:i2,j1:j2,k2,2) = 0._wp
-        IF (.NOT.ln_linssh) THEN
-           ! This vertical sum below should be replaced by the sea-level at V-points (optimization):
-           DO jk=1,jpk
-              ptab(i1:i2,j1:j2,k2,2) = ptab(i1:i2,j1:j2,k2,2) + e3v(i1:i2,j1:j2,jk,Kmm_a) * vmask(i1:i2,j1:j2,jk)
-           END DO
-           ptab(i1:i2,j1:j2,k2,2) = ptab(i1:i2,j1:j2,k2,2) - hv_0(i1:i2,j1:j2)
-        END IF 
-# endif
-      ELSE       
-         zrhox = Agrif_rhox()
-# if defined key_vertical
-
-         IF (ln_linssh) ptab(i1:i2,j1:j2,k2,2) = 0._wp 
-
-         DO jj=j1,j2
-            DO ji=i1,i2
-               vv(ji,jj,:,Krhs_a) = 0._wp
-               N_in = mbkv_parent(ji,jj)
-               zhtot = 0._wp
-               DO jk=1,N_in
-                  IF (jk==N_in) THEN
-                     h_in(jk) = hv0_parent(ji,jj) + ptab(ji,jj,k2,2) - zhtot
-                  ELSE
-                     h_in(jk) = ptab(ji,jj,jk,2)/(e1v(ji,jj)*zrhox) 
+                  vv(ji,jj,:,Krhs_a) = 0._wp
+                  N_in = mbkv_parent(ji,jj)
+                  zhtot = 0._wp
+                  DO jk=1,N_in
+                     IF (jk==N_in) THEN
+                        h_in(jk) = hv0_parent(ji,jj) + ptab(ji,jj,k2,2) - zhtot
+                     ELSE
+                        h_in(jk) = ptab(ji,jj,jk,2)/(e1v(ji,jj)*zrhox) 
+                     ENDIF
+                     zhtot = zhtot + h_in(jk)
+                     IF( h_in(jk) .GT. 0. ) THEN
+                       tabin(jk) = ptab(ji,jj,jk,1)/(e1v(ji,jj)*zrhox*h_in(jk))
+                     ELSE
+                       tabin(jk)  = 0.
+                     ENDIF 
+                  END DO
+
+                  z_in(1) = 0.5_wp * h_in(1) - zhtot + hv0_parent(ji,jj)
+                  DO jk=2,N_in
+                     z_in(jk) = z_in(jk-1) + 0.5_wp * h_in(jk)
+                  END DO
+
+                  N_out = 0
+                  DO jk=1,jpk
+                     IF (vmask(ji,jj,jk) == 0) EXIT
+                     N_out = N_out + 1
+                     h_out(N_out) = e3v(ji,jj,jk,Krhs_a)
+                  END DO
+
+                  z_out(1) = 0.5_wp * h_out(1) - SUM(h_out(1:N_out)) + hv_0(ji,jj)
+                  DO jk=2,N_out
+                     z_out(jk) = z_out(jk-1) + 0.5_wp * h_out(jk)
+                  END DO
+ 
+                  IF (N_in*N_out > 0) THEN
+                     IF( l_ini_child ) THEN
+                        CALL remap_linear       (tabin(1:N_in),z_in(1:N_in),vv(ji,jj,1:N_out,Krhs_a),z_out(1:N_out),N_in,N_out,1)
+                     ELSE
+                        CALL reconstructandremap(tabin(1:N_in),h_in(1:N_in),vv(ji,jj,1:N_out,Krhs_a),h_out(1:N_out),N_in,N_out,1)
+                     ENDIF   
                   ENDIF
-                  zhtot = zhtot + h_in(jk)
-                  tabin(jk) = ptab(ji,jj,jk,1)/(e1v(ji,jj)*zrhox*h_in(jk))
-              ENDDO
-         
-               N_out = 0
-               DO jk=1,jpk
-                  if (vmask(ji,jj,jk) == 0) EXIT
-                  N_out = N_out + 1
-                  h_out(N_out) = e3v(ji,jj,jk,Krhs_a)
-               END DO
-               IF (N_in*N_out > 0) THEN
-                  call reconstructandremap(tabin(1:N_in),h_in(1:N_in),vv(ji,jj,1:N_out,Krhs_a),h_out(1:N_out),N_in,N_out,1)
-               ENDIF
-            END DO
-         END DO
-# else
-         DO jk = 1, jpkm1
-            vv(i1:i2,j1:j2,jk,Krhs_a) = ptab(i1:i2,j1:j2,jk,1) / ( zrhox * e1v(i1:i2,j1:j2) * e3v(i1:i2,j1:j2,jk,Krhs_a) )
-         END DO
-# endif
+               END DO
+            END DO
+         ELSE
+            DO jk = 1, jpkm1
+               vv(i1:i2,j1:j2,jk,Krhs_a) = ptab(i1:i2,j1:j2,jk,1) / ( zrhox * e1v(i1:i2,j1:j2) * e3v(i1:i2,j1:j2,jk,Krhs_a) )
+            END DO
+         ENDIF
       ENDIF
       !        
@@ -1152,6 +1272,6 @@
                      WRITE(numout,*) ' Agrif error for e3t_0: parent , child, i, j, k ',  & 
                      &                 ptab(ji,jj,jk), tmask(ji,jj,jk) * e3t_0(ji,jj,jk), &
-                     &                 ji+nimpp-1, jj+njmpp-1, jk
-                     kindic_agr = kindic_agr + 1
+                     &                 mig0(ji), mig0(jj), jk
+                !     kindic_agr = kindic_agr + 1
                   ENDIF
                END DO
@@ -1162,4 +1282,61 @@
       ! 
    END SUBROUTINE interpe3t
+
+   SUBROUTINE interpglamt( ptab, i1, i2, j1, j2, before )
+      !!----------------------------------------------------------------------
+      !!                  ***  ROUTINE interpglamt  ***
+      !!----------------------------------------------------------------------  
+      INTEGER                        , INTENT(in   ) :: i1, i2, j1, j2
+      REAL(wp),DIMENSION(i1:i2,j1:j2), INTENT(inout) :: ptab
+      LOGICAL                        , INTENT(in   ) :: before
+      !
+      INTEGER :: ji, jj, jk
+      REAL(wp):: ztst
+      !!----------------------------------------------------------------------  
+      !    
+      IF( before ) THEN
+         ptab(i1:i2,j1:j2) = glamt(i1:i2,j1:j2)
+      ELSE
+         ztst = MAXVAL(ABS(glamt(i1:i2,j1:j2)))*1.e-4
+         DO jj = j1, j2
+            DO ji = i1, i2
+               IF( ABS( ptab(ji,jj) - glamt(ji,jj) ) > ztst ) THEN
+                  WRITE(numout,*) ' Agrif error for glamt: parent, child, i, j ', ptab(ji,jj), glamt(ji,jj), mig0(ji), mig0(jj)
+!                  kindic_agr = kindic_agr + 1
+               ENDIF
+            END DO
+         END DO
+      ENDIF
+      ! 
+   END SUBROUTINE interpglamt
+
+
+   SUBROUTINE interpgphit( ptab, i1, i2, j1, j2, before )
+      !!----------------------------------------------------------------------
+      !!                  ***  ROUTINE interpgphit  ***
+      !!----------------------------------------------------------------------  
+      INTEGER                        , INTENT(in   ) :: i1, i2, j1, j2
+      REAL(wp),DIMENSION(i1:i2,j1:j2), INTENT(inout) :: ptab
+      LOGICAL                        , INTENT(in   ) :: before
+      !
+      INTEGER :: ji, jj, jk
+      REAL(wp):: ztst
+      !!----------------------------------------------------------------------  
+      !    
+      IF( before ) THEN
+         ptab(i1:i2,j1:j2) = gphit(i1:i2,j1:j2)
+      ELSE
+         ztst = MAXVAL(ABS(gphit(i1:i2,j1:j2)))*1.e-4
+         DO jj = j1, j2
+            DO ji = i1, i2
+               IF( ABS( ptab(ji,jj) - gphit(ji,jj) ) > ztst ) THEN
+                  WRITE(numout,*) ' Agrif error for gphit: parent, child, i, j ', ptab(ji,jj), gphit(ji,jj), mig0(ji), mig0(jj)
+!                  kindic_agr = kindic_agr + 1
+               ENDIF
+            END DO
+         END DO
+      ENDIF
+      ! 
+   END SUBROUTINE interpgphit
 
 
@@ -1185,66 +1362,68 @@
               END DO
            END DO
-        END DO
-
-# if defined key_vertical
-        ! Interpolate thicknesses
-        ! Warning: these are masked, hence extrapolated prior interpolation.
-        DO jk=k1,k2
-           DO jj=j1,j2
-              DO ji=i1,i2
-                  ptab(ji,jj,jk,2) = tmask(ji,jj,jk) * e3t(ji,jj,jk,Kmm_a)
-              END DO
-           END DO
-        END DO
-
-        ! Extrapolate thicknesses in partial bottom cells:
-        ! Set them to Agrif_SpecialValue (0.). Correct bottom thicknesses are retrieved later on
-        IF (ln_zps) THEN
-           DO jj=j1,j2
-              DO ji=i1,i2
-                  jk = mbkt(ji,jj)
-                  ptab(ji,jj,jk,2) = 0._wp
-              END DO
-           END DO           
-        END IF
-     
-        ! Save ssh at last level:
-        IF (.NOT.ln_linssh) THEN
-           ptab(i1:i2,j1:j2,k2,2) = ssh(i1:i2,j1:j2,Kmm_a)*tmask(i1:i2,j1:j2,1) 
-        ELSE
-           ptab(i1:i2,j1:j2,k2,2) = 0._wp
-        END IF      
-# endif
+         END DO
+
+         IF( l_vremap ) THEN
+            ! Interpolate thicknesses
+            ! Warning: these are masked, hence extrapolated prior interpolation.
+            DO jk=k1,k2
+               DO jj=j1,j2
+                  DO ji=i1,i2
+                      ptab(ji,jj,jk,2) = tmask(ji,jj,jk) * e3t(ji,jj,jk,Kmm_a)
+                  END DO
+               END DO
+            END DO
+
+            ! Extrapolate thicknesses in partial bottom cells:
+            ! Set them to Agrif_SpecialValue (0.). Correct bottom thicknesses are retrieved later on
+            IF (ln_zps) THEN
+               DO jj=j1,j2
+                  DO ji=i1,i2
+                      jk = mbkt(ji,jj)
+                      ptab(ji,jj,jk,2) = 0._wp
+                  END DO
+               END DO           
+            END IF
+        
+           ! Save ssh at last level:
+            IF (.NOT.ln_linssh) THEN
+               ptab(i1:i2,j1:j2,k2,2) = ssh(i1:i2,j1:j2,Kmm_a)*tmask(i1:i2,j1:j2,1) 
+            ELSE
+               ptab(i1:i2,j1:j2,k2,2) = 0._wp
+            END IF      
+          ENDIF
+
       ELSE 
-#ifdef key_vertical         
-         IF (ln_linssh) ptab(i1:i2,j1:j2,k2,2) = 0._wp 
-         avm_k(i1:i2,j1:j2,k1:k2) = 0._wp
-            
-         DO jj = j1, j2
-            DO ji =i1, i2
-               N_in = mbkt_parent(ji,jj)
-               IF ( tmask(ji,jj,1) == 0._wp) N_in = 0
-               z_in(N_in+1) = ht0_parent(ji,jj) + ptab(ji,jj,k2,2)
-               DO jk = N_in, 1, -1  ! Parent vertical grid               
-                     z_in(jk) = z_in(jk+1) - ptab(ji,jj,jk,2)
-                    tabin(jk) = ptab(ji,jj,jk,1)
-               END DO
-               N_out = mbkt(ji,jj) 
-               DO jk = 1, N_out        ! Child vertical grid
-                  z_out(jk) = gdepw(ji,jj,jk,Kmm_a)
-               ENDDO
-               IF (N_in*N_out > 0) THEN
-                  CALL remap_linear(tabin(1:N_in),z_in(1:N_in),avm_k(ji,jj,1:N_out),z_out(1:N_out),N_in,N_out,1)
-               ENDIF
-            ENDDO
-         ENDDO
-#else
-         avm_k(i1:i2,j1:j2,k1:k2) = ptab (i1:i2,j1:j2,k1:k2,1)
-#endif
+
+         IF( l_vremap ) THEN
+            IF (ln_linssh) ptab(i1:i2,j1:j2,k2,2) = 0._wp 
+            avm_k(i1:i2,j1:j2,k1:k2) = 0._wp
+               
+            DO jj = j1, j2
+               DO ji =i1, i2
+                  N_in = mbkt_parent(ji,jj)
+                  IF ( tmask(ji,jj,1) == 0._wp) N_in = 0
+                  z_in(N_in+1) = ht0_parent(ji,jj) + ptab(ji,jj,k2,2)
+                  DO jk = N_in, 1, -1  ! Parent vertical grid               
+                        z_in(jk) = z_in(jk+1) - ptab(ji,jj,jk,2)
+                       tabin(jk) = ptab(ji,jj,jk,1)
+                  END DO
+                  N_out = mbkt(ji,jj) 
+                  DO jk = 1, N_out        ! Child vertical grid
+                     z_out(jk) = gdepw(ji,jj,jk,Kmm_a)
+                  END DO
+                  IF (N_in*N_out > 0) THEN
+                     CALL remap_linear(tabin(1:N_in),z_in(1:N_in),avm_k(ji,jj,1:N_out),z_out(1:N_out),N_in,N_out,1)
+                  ENDIF
+               END DO
+            END DO
+         ELSE
+            avm_k(i1:i2,j1:j2,k1:k2) = ptab (i1:i2,j1:j2,k1:k2,1)
+         ENDIF
       ENDIF
       !
    END SUBROUTINE interpavm
 
-# if defined key_vertical
+   
    SUBROUTINE interpmbkt( ptab, i1, i2, j1, j2, before )
       !!----------------------------------------------------------------------
@@ -1265,4 +1444,5 @@
    END SUBROUTINE interpmbkt
 
+   
    SUBROUTINE interpht0( ptab, i1, i2, j1, j2, before )
       !!----------------------------------------------------------------------
@@ -1282,6 +1462,43 @@
       !
    END SUBROUTINE interpht0
-#endif
-
+
+   
+   SUBROUTINE agrif_initts(tabres,i1,i2,j1,j2,k1,k2,m1,m2,before)
+       INTEGER :: i1, i2, j1, j2, k1, k2, m1, m2
+       REAL(wp):: tabres(i1:i2,j1:j2,k1:k2,m1:m2)
+       LOGICAL :: before
+
+       INTEGER :: jm
+
+       IF (before) THEN
+         DO jm=1,jpts
+             tabres(i1:i2,j1:j2,k1:k2,jm) = ts(i1:i2,j1:j2,k1:k2,jm,Kbb_a)
+         END DO
+       ELSE
+         DO jm=1,jpts
+             ts(i1:i2,j1:j2,k1:k2,jm,Kbb_a)=tabres(i1:i2,j1:j2,k1:k2,jm)
+         END DO
+       ENDIF
+   END SUBROUTINE agrif_initts 
+
+   
+   SUBROUTINE agrif_initssh( ptab, i1, i2, j1, j2, before )
+      !!----------------------------------------------------------------------
+      !!                  ***  ROUTINE interpsshn  ***
+      !!----------------------------------------------------------------------  
+      INTEGER                         , INTENT(in   ) ::   i1, i2, j1, j2
+      REAL(wp), DIMENSION(i1:i2,j1:j2), INTENT(inout) ::   ptab
+      LOGICAL                         , INTENT(in   ) ::   before
+      !
+      !!----------------------------------------------------------------------  
+      !
+      IF( before) THEN
+         ptab(i1:i2,j1:j2) = ssh(i1:i2,j1:j2,Kbb_a)
+      ELSE
+         ssh(i1:i2,j1:j2,Kbb_a) = ptab(i1:i2,j1:j2)*tmask(i1:i2,j1:j2,1)
+      ENDIF
+      !
+   END SUBROUTINE agrif_initssh
+   
 #else
    !!----------------------------------------------------------------------
