   !!======================================================================

  SUBROUTINE agrif_obc_vz(uvz,uv,xeuv)
!
!********************************************************************
!
!     objet    :  conditions aux limites ouvertes en vitesse.
!                 On remplit le cadre de v.
!
!
!
!     appelant         : main, dyn3dy
!                      :
!     appeles          :
!                      :
!     canal d ecriture :
!                      :
!     canal de lecture :
!                      :
!     cle cpp          :
!                      :
!     variables in     : jh, jb, ig, id
!                      :
!     variables out    : u
!                      :
!     variables inout  : v, uvz
!                      :
!     dernieres modifs : 13/10/2005
!                      : 12/12/2007 F. Dumas
   !&E       !  2009-05  (F. Dumas)  AGRIF pour nouveau schema temporel
!                      :
!***********************************************************

#include "toolcpp.h"

#ifdef key_agrif
   USE parameters
   USE comvarp3d
   USE comvars2d
   USE comvarp2d
   USE agrifcommonid
!   USE_MPI toolmpi, ONLY : ex_i_rsh,ex_j_rsh

   IMPLICIT NONE
!
   INTEGER        :: i,j,k
   REAL(KIND=rsh),DIMENSION(kmax,liminm2:limaxp2,ljminm2:ljmaxp2),INTENT(inout) :: uvz
   REAL(KIND=rsh),DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2),INTENT(in) :: uv
   REAL(KIND=rsh),DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2),INTENT(in) :: xeuv
!
   EXTERNAL :: interpvz_stepu
!
!************************************************************
!     Executable statements
!************************************************************
!
   PRINT_DBG*, 'enter AGRIF_OBC_VZ routine',Agrif_Root()

   ! Initialization
   agrif_SpecialValue=0.
   c_agrif_interp='vz_bc'

   ! Initialization


!$OMP DO SCHEDULE(STATIC)
   DO j = ljminm2,ljmaxp2
     DO i = liminm2,limaxp2
       nesting_tabtemp_3d(:,i,j) = uvz(:,i,j)
       nesting_tabtemp_2d(i,j,1) = uv(i,j)
       nesting_tabtemp_2d(i,j,2) = xeuv(i,j)
     END DO
   END DO
!$OMP END DO

   ! Agrif on OBC
OMPAGRIF MASTER
   CALL Agrif_Bc_variable(vz_id,procname=interpvz_stepu)
OMPAGRIF END MASTER
OMPAGRIF BARRIER

   ! Set up

!$OMP DO SCHEDULE(STATIC)
   DO j = ljminm2,ljmaxp2
     DO i = liminm2,limaxp2
       uvz(:,i,j) = nesting_tabtemp_3d(:,i,j)
     END DO
   END DO
!$OMP END DO

   agrif_SpecialValue=valmanq

   ! Coarsening
   IF (Agrif_Grandmother()) THEN

     DO j=ljminp1,ljmaxm1

       ! Western boundary
       i=ig(j)
       IF_MPI (limin==imin .AND. limin==i-1) THEN
         IF (hy(i,j) /= -valmanq-h0fond) uvz(:,i,j)=uvz(:,i+1,j)
       ENDIF_MPI

       ! Eastern boundary
       i=id(j)
       IF_MPI (limax==imax .AND. limax==i) THEN
         IF (hy(i,j) /= -valmanq-h0fond) uvz(:,i,j)=uvz(:,i-1,j)
       ENDIF_MPI

     END DO


     DO i=liminp2,limaxm1

       ! Southern boundary
       j=jb(i)-1
       IF_MPI (ljmin==jmin .AND. ljmin==j) THEN
         IF (hy(i,j) /= -valmanq-h0fond) uvz(:,i,j)=uvz(:,i,j+1)
       ENDIF_MPI

       ! Northern open boundary
       j=jh(i)
       IF_MPI (ljmax==jmax .AND. ljmax==j) THEN
         IF (hy(i,j) /= -valmanq-h0fond) uvz(:,i,j)=uvz(:,i,j-1)
       ENDIF_MPI

     END DO

   END IF    ! end Grand Mother

!!!!!!!!!OMPMPINOAGRIF barrier
!!!!!!!!!OMPMPINOAGRIF master
!!!!!!!!!   CALL_MPI ex_i_rsh(-2,2,kmax,liminm2,limaxp2,ljminm2,ljmaxp2,uvz(:,liminm2:limaxp2,ljminm2:ljmaxp2))
!part really usefull CALL ex_j_rsh(-2,2,kmax,limin,limaxp1,ljminm2,ljmaxp2,uvz(:,limin:limaxp1,ljminm2:ljmaxp2))
!larger exchanges to make transfer in step2d* and step3d* easier
!!!!!!!!   CALL_MPI ex_j_rsh(-2,2,kmax,liminm2,limaxp2,ljminm2,ljmaxp2,uvz(:,liminm2:limaxp2,ljminm2:ljmaxp2))
!!!!!!!!OMPMPINOAGRIF end master
!!!!!!!!OMPMPINOAGRIF barrier
!!!!!!!!OMPMPINOAGRIF flush(uvz)

   PRINT_DBG*, 'exit AGRIF_OBC_VZ routine',Agrif_Root()

   RETURN

#else

   USE parameters, ONLY : rsh,liminm2,limaxp2,ljminm2,ljmaxp2,kmax
   REAL(KIND=rsh),DIMENSION(kmax,liminm2:limaxp2,ljminm2:ljmaxp2),INTENT(inout) :: uvz
   REAL(KIND=rsh),DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2),INTENT(in) :: uv
   REAL(KIND=rsh),DIMENSION(liminm2:limaxp2,ljminm2:ljmaxp2),INTENT(in) :: xeuv

#endif

  END SUBROUTINE agrif_obc_vz

   !!======================================================================

#ifdef key_agrif

  SUBROUTINE interpvz_stepu(tabres,k1,k2,i1,i2,j1,j2,before)

   !&E       !
   !&E       !  2015-09  (M. Honnorat, V. Garnier) MPI
   !&E       !

   USE parameters
   USE comvarp3d
   USE comvars2d
   USE comvarp2d
   USE comcolig2d
   USE agrifcommonid
#if defined key_siggen || defined key_gencoord
   USE comsiggen, ONLY : hzey,dcusds
   USE toolgeom,  ONLY : f_hzu_y
#endif
   USE comvars3d, ONLY : dsigu
   IMPLICIT NONE

   INTEGER, INTENT(IN)      :: k1,k2,i1,i2,j1,j2
   REAL,    INTENT(INOUT)   :: tabres(k1:k2,i1:i2,j1:j2)
   LOGICAL, INTENT(IN)      :: before

   REAL(KIND=rsh),DIMENSION(k1:k2,i1:i2,j1:j2)  :: Fx, Fy, uvdiff
   REAL(KIND=rsh),DIMENSION(      i1:i2,j1:j2)  :: coeff
   REAL(KIND=rsh),DIMENSION(1:kmax,i1:i2,j1:j2) :: hzey_loc
   REAL(KIND=rsh),DIMENSION(       i1:i2,j1:j2) :: hey_loc
   REAL(KIND=rsh)                               :: visc_sponge, vmoy
   REAL(KIND=rsh)                               :: Agrif_Irhox_OMP
   INTEGER,       DIMENSION(       i1:i2,j1:j2) :: igdv_loc
   REAL :: htxy_loc
   INTEGER :: i,j,k,dist,test,jp0,jp1

   !!----------------------------------------------------------------------
   !! * Executable part

   PRINT_DBG*, 'enter INTERPVZ_STEPU routine',Agrif_Root()

   IF (before) THEN     ! mother

!!$OMP  PARALLEL DEFAULT(none) PRIVATE(i,j,k) &
!!$OMP& SHARED(j1,j2,i1,i2,k1,k2,tabres,vz)
!!$OMP DO SCHEDULE(RUNTIME)
     DO j=j1,j2
       DO i=i1,i2
         DO k=k1,k2
           tabres(k,i,j) = vz(k,i,j)
         END DO
       END DO
     END DO
!!$OMP END DO
!!$OMP END PARALLEL

   ELSE                 ! child

     SELECT CASE(c_agrif_interp)

       CASE('vz_init')

         test = 2
         CALL agrif_dyn3d_height_mask(ssh,igdv_loc,hey_loc, &
#if defined key_siggen || defined key_gencoord
                                      hzey_loc, &
#endif
                                      i1,i2,j1,j2,test)

!$OMP  PARALLEL DEFAULT(none) PRIVATE(i,j,vmoy) &
!$OMP& SHARED(j1,j2,i1,i2,ljmin,ljmax,limin,limax,igdv_loc,nesting_tabtemp_3d,tabres,v,dsigu,hzey_loc,hey_loc)
!$OMP DO SCHEDULE(RUNTIME)
         DO j=MAX(j1,ljmin),MIN(j2,ljmax)
           DO i=MAX(i1,limin),MIN(i2,limax)
             IF (igdv_loc(i,j) == 1) THEN
               nesting_tabtemp_3d(:,i,j) = tabres(1:kmax,i,j)
#ifdef key_siggen
               vmoy = SUM(nesting_tabtemp_3d(:,i,j)*hzey_loc(:,i,j)*dsigu(:))/hey_loc(i,j)
#else
#endif
               nesting_tabtemp_3d(:,i,j) = nesting_tabtemp_3d(:,i,j)-vmoy+v(i,j)
             ELSE IF (igdv_loc(i,j) == 5) THEN
               nesting_tabtemp_3d(:,i,j) = v(i,j)
             ELSE
               nesting_tabtemp_3d(:,i,j) = 0.0_rsh
             END IF
           END DO
         END DO
!$OMP END DO
!$OMP END PARALLEL

       CASE('vz_bc')


         Agrif_Irhox_OMP = Agrif_Irhox()

!!$OMP  PARALLEL  DEFAULT(none) PRIVATE(i,j,jp0,jp1,htxy_loc) &
#ifdef key_agrif_obc_sponge_uv
!!$OMP& PRIVATE(visc_sponge,dist) &
#endif
!!$OMP& SHARED(j1,j2,i1,i2,imin,imax,jmin,jmax,nesting_tabtemp_3d,nesting_tabtemp_2d,tabres,hm,hy) &
#ifdef key_agrif_obc_sponge_uv
!!$OMP& SHARED(ljminp1,liminp2,ljmaxm1,limaxm1,ljminp1m1,liminp1m1,ljminm1) &
!!$OMP& SHARED(uvdiff,hey_loc,coeff,Fx,Fy,halfdt,Agrif_Irhox_OMP,me,dx_u,dy_v,dx_v,dy,dx_f) &
#endif
!!$OMP& SHARED(igdv_loc)


         ! Initialize uvdiff : uz (fine grid) - uz (interpolated from mother to child grid)
!cval j1=ljminm2 if inside cpu domain DO j=MAX(j1,ljminm2),MIN(j2,ljmaxp2)
!cval j2=ljmaxp2 if inside cpu domain DO i=MAX(i1,liminm2),MIN(i2,limaxp2)

!!$OMP DO SCHEDULE(RUNTIME)
         DO j=j1,j2
           DO i=i1,i2
             ! at open boundary of the child grid, copy mother grid field
             IF ((i==imin+1.or.i==imax.or.j==jmin.or.j==jmax)) THEN

               jp1 = j + 1
               jp0 = j
               IF (j==jmax) jp1 = j
               IF (j==jmin) jp0 = j + 1

               htxy_loc = hy(i,j)+0.5_rsh*(nesting_tabtemp_2d(i,jp0,2)+nesting_tabtemp_2d(i,jp1,2))
               IF (htxy_loc <=0.0_rsh .AND. (hy(i,j)+nesting_tabtemp_2d(i,jp0,2) > 0.0_rsh .OR. &
                                             hy(i,j)+nesting_tabtemp_2d(i,jp1,2) > 0.0_rsh)) &
                     htxy_loc = hy(i,j)+MAX(nesting_tabtemp_2d(i,jp0,2),nesting_tabtemp_2d(i,jp1,2))
               IF (htxy_loc > 0.0_rsh) THEN
                 igdv_loc(i,j) = 1
                 IF (htxy_loc < hm) igdv_loc(i,j) = 5
                 IF (htxy_loc < MIN(epsilon,REAL(hm,rlg))) igdv_loc(i,j) = 4
               ELSE
                  igdv_loc(i,j) = 4
               ENDIF

               IF (igdv_loc(i,j) == 1) THEN
                 nesting_tabtemp_3d(:,i,j) = tabres(1:kmax,i,j)
               ELSE IF (igdv_loc(i,j) == 5) THEN
                 nesting_tabtemp_3d(:,i,j) = nesting_tabtemp_2d(i,j,1) ! v(i,j)
               ELSE
                 nesting_tabtemp_3d(:,i,j) = 0.0_rsh
               END IF
             END IF

#ifdef key_agrif_obc_sponge_uv
             jp1 = j + 1
             jp0 = j
             IF (j==jmax) jp1 = j
             IF (j==jmin) jp0 = j + 1

             htxy_loc = hy(i,j)+0.5_rsh*(nesting_tabtemp_2d(i,jp0,2)+nesting_tabtemp_2d(i,jp1,2))
             IF (htxy_loc <=0.0_rsh .AND. (hy(i,j)+nesting_tabtemp_2d(i,jp0,2) > 0.0_rsh .OR. &
                                           hy(i,j)+nesting_tabtemp_2d(i,jp1,2) > 0.0_rsh)) &
                     htxy_loc = hy(i,j)+MAX(nesting_tabtemp_2d(i,jp0,2),nesting_tabtemp_2d(i,jp1,2))
             IF (htxy_loc > 0.0_rsh) THEN
               igdv_loc(i,j) = 1
               IF (htxy_loc < hm) igdv_loc(i,j) = 5
               IF (htxy_loc < MIN(epsilon,REAL(hm,rlg))) igdv_loc(i,j) = 4
#ifdef key_dyn_adv_linear
               hey_loc(i,j)=MAX(0.0_rsh,hy(i,j))*govlet(i,j)
#else
               hey_loc(i,j)=(htxy_loc+dhy(i,j))*govlet(i,j)
#endif
             ELSE
               igdv_loc(i,j) = 4
               hey_loc(i,j) = 0.0_rsh
             ENDIF

             uvdiff(:,i,j) = 0.0_rsh
             IF (igdv_loc(i,j) /=4) THEN
               uvdiff(:,i,j) = nesting_tabtemp_3d(:,i,j)-tabres(:,i,j)
             ELSE
               uvdiff(:,i,j) = 0.0_rsh
             END IF
#endif

           END DO
         END DO
!!$OMP END DO

#if ! defined key_agrif_obc_sponge_uv
!!$OMP END PARALLEL
         RETURN
#else

         ! Sponge layer for the child grid (over nb_connect*Agrif_Irhox)

         ! Estimate of the coefficient of diffusion (m2/s)

!cval WARNING AGRIF en dur !!!! depends on the choice of the scheme ?
         visc_sponge=1.0_rsh/8.0_rsh

         !cval DO j=MAX(j1,ljminm2),MIN(j2,ljmaxp2)
         !cval  DO i=MAX(i1,liminm2),MIN(i2,limaxp2)
         !cval i1=liminm2 ... when required. Test is useless.

!!$OMP DO SCHEDULE(RUNTIME)
         DO j=j1,j2
           DO i=i1,i2
             coeff(i,j) = 0.0_rsh
             IF (igdv_loc(i,j)/=4) THEN
               dist = nb_connect*Agrif_Irhox_OMP
               dist = min(dist,i-(imin+1))
               dist = min(dist,imax-i)
               dist = min(dist,j-jmin)
               dist = min(dist,jmax-j)
               coeff(i,j) = (1.-REAL(dist)/real(nb_connect*Agrif_Irhox_OMP))*visc_sponge &
                            * (dx_u(i,j)*dy_v(i,j))**2 / (halfdt*(dx_u(i,j)**2+dy_v(i,j)**2))
             END IF
           END DO
         END DO
!!$OMP END DO

         ! nu d uvdiff / dx
         !Fx(:,:,:) = 0.0_rsh
!!$OMP DO SCHEDULE(RUNTIME)
         DO j=MAX(j1,ljminp1m1),MIN(j2,ljmaxm1)
           DO i=MAX(i1,liminp1m1),MIN(i2-1,limaxm1)
             Fx(:,i,j) = me(i,j)*coeff(i,j)*(uvdiff(:,i+1,j)-uvdiff(:,i,j))/dx_f(i,j)
           END DO
         END DO
!!$OMP END DO

         ! nu d uvdiff / dy
         !Fy(:,:,:) = 0.0_rsh
!!$OMP DO SCHEDULE(RUNTIME)
         DO j=MAX(j1,ljminm1),MIN(j2-1,ljmaxm1)
           DO i=MAX(i1,liminp2),MIN(i2,limaxm1)
             Fy(:,i,j) = coeff(i,j)*(uvdiff(:,i,j+1)-uvdiff(:,i,j))/dy(i,j+1)
           END DO
         END DO
!!$OMP END DO

         ! Add d / dx ( nu d uvdiff/dx ) and  d / dx ( nu d uvdiff/dy )
         ! inside the domain of calculation (Mother field at jmin,jmax,imax,imin+1)
!!$OMP DO SCHEDULE(RUNTIME)
         DO j=MAX(j1+1,ljminp1),MIN(j2-1,ljmaxm1)
           DO i=MAX(i1+1,liminp2),MIN(i2-1,limaxm1)
             IF ((igdv_loc(i,j)/=4).AND.(hey_loc(i,j)/=0.)) THEN
               nesting_tabtemp_3d(:,i,j) = nesting_tabtemp_3d(:,i,j) + &
                 ((1./dx_v(i,j))*(Fx(:,i,j)-Fx(:,i-1,j))  &
                 +(1./dy_v(i,j))*(Fy(:,i,j)-Fy(:,i,j-1)))*halfdt
             END IF
           END DO
         END DO
!!$OMP END DO

!!$OMP END PARALLEL

#endif /* key_agrif_obc_sponge_uv */

     END SELECT

   END IF

   PRINT_DBG*, 'exit INTERPVZ_STEPU routine',Agrif_Root()

   RETURN

  END SUBROUTINE interpvz_stepu
#endif
