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

  SUBROUTINE agrif_obc_uz(uvz,uv,xeuv)
!
!********************************************************************
!
!     objet    :  conditions aux limites ouvertes en vitesse.
!                 On remplit le cadre de u.
!
!
!
!     appelant         : dyn3dx, stepu
!                      :
!     appeles          :
!                      :
!     canal d ecriture :
!                      :
!     canal de lecture :
!                      :
!     cle cpp          :
!                      :
!     variables in     : jh, jb, ig, id, valmanq, h0fond, hx
!                      :
!     variables out    :
!                      :
!     variables inout  : uz
!                      :
!     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 :: interpuz_stepu
!
!************************************************************
!     Executable statements
!************************************************************
!
   PRINT_DBG*, 'enter AGRIF_OBC_UZ routine',Agrif_Root()

   ! Initialization
   agrif_SpecialValue=0.
   c_agrif_interp='uz_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(uz_id,procname=interpuz_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=ljminp2,ljmaxm1

       ! Western boundary
       i=ig(j)-1
       IF_MPI (limin==imin .AND. limin==i) THEN
         IF (hx(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 (hx(i,j) /= -valmanq-h0fond) uvz(:,i,j)=uvz(:,i-1,j)
       ENDIF_MPI

     END DO


     DO i=liminp1,limaxm1

       ! Southern open boundary
       j=jb(i)
       IF_MPI (ljmin==jmin .AND. ljmin==j-1) THEN
         IF (hx(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 (hx(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,liminm1,limax,ljminm2,ljmaxp2,uvz(:,liminm1:limax,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_UZ 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_uz

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

#ifdef key_agrif

  SUBROUTINE interpuz_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 : hzex,dcusds
   USE toolgeom,  ONLY : f_hzu_x
#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) :: hzex_loc
   REAL(KIND=rsh),DIMENSION(       i1:i2,j1:j2) :: hex_loc
   REAL(KIND=rsh)                               :: visc_sponge, umoy
   REAL(KIND=rsh)                               :: Agrif_Irhox_OMP
   INTEGER,       DIMENSION(       i1:i2,j1:j2) :: igdu_loc
   REAL :: htxy_loc
   INTEGER :: i,j,k,dist,test,ip0,ip1


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

   PRINT_DBG*, 'enter INTERPUZ_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,uz)
!!$OMP DO SCHEDULE(RUNTIME)
     DO j=j1,j2
       DO i=i1,i2
         DO k=k1,k2
           tabres(k,i,j) = uz(k,i,j)
         END DO
       END DO
     END DO
!!$OMP END DO
!!$OMP END PARALLEL

   ELSE                 ! child

     SELECT CASE(c_agrif_interp)

       CASE('uz_init')

         test = 1
         CALL agrif_dyn3d_height_mask(ssh,igdu_loc,hex_loc, &
#if defined key_siggen || defined key_gencoord
                                      hzex_loc, &
#endif
                                      i1,i2,j1,j2,test)

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


       CASE('uz_bc')


         Agrif_Irhox_OMP = Agrif_Irhox()

!!$OMP  PARALLEL  DEFAULT(none) PRIVATE(i,j,ip0,ip1,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,hx) &
#ifdef key_agrif_obc_sponge_uv
!!$OMP& SHARED(ljminp2,ljmaxm1,liminm1,limaxm1,ljminp1m1,liminp1m1,liminp1) &
!!$OMP& SHARED(uvdiff,hex_loc,coeff,Fx,Fy,halfdt,Agrif_Irhox_OMP,mn,dx_u,dy_v,dy_u,dx,dy_f) &
#endif
!!$OMP& SHARED(igdu_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.or.i==imax.or.j==jmax.or.j==jmin+1)) THEN
               ip1 = i + 1
               ip0 = i
               IF (i==imax) ip1 = i
               IF (i==imin) ip0 = i + 1

               htxy_loc = hx(i,j)+0.5_rsh*(nesting_tabtemp_2d(ip0,j,2)+nesting_tabtemp_2d(ip1,j,2))
               IF (htxy_loc <=0.0_rsh .AND. (hx(i,j)+nesting_tabtemp_2d(ip0,j,2) > 0.0_rsh .OR. &
                                             hx(i,j)+nesting_tabtemp_2d(ip1,j,2) > 0.0_rsh)) &
                     htxy_loc = hx(i,j)+MAX(nesting_tabtemp_2d(ip0,j,2),nesting_tabtemp_2d(ip1,j,2))
               IF (htxy_loc > 0.0_rsh) THEN
                 igdu_loc(i,j) = 1
                 IF (htxy_loc < hm) igdu_loc(i,j) = 5
                 IF (htxy_loc < MIN(epsilon,REAL(hm,rlg))) igdu_loc(i,j) = 4
               ELSE
                 igdu_loc(i,j) = 4
               ENDIF
               IF (igdu_loc(i,j) == 1) THEN
                 nesting_tabtemp_3d(:,i,j) = tabres(1:kmax,i,j)
               ELSE IF (igdu_loc(i,j) == 5) THEN
                 nesting_tabtemp_3d(:,i,j) = nesting_tabtemp_2d(i,j,1) ! u(i,j)
               ELSE
                 nesting_tabtemp_3d(:,i,j) = 0.0_rsh
               END IF
             END IF

#ifdef key_agrif_obc_sponge_uv
             ip1 = i + 1
             ip0 = i
             IF (i==imax) ip1 = i
             IF (i==imin) ip0 = i + 1

             htxy_loc = hx(i,j)+0.5_rsh*(nesting_tabtemp_2d(ip0,j,2)+nesting_tabtemp_2d(ip1,j,2))
             IF (htxy_loc <=0.0_rsh .AND. (hx(i,j)+nesting_tabtemp_2d(ip0,j,2) > 0.0_rsh .OR. &
                                           hx(i,j)+nesting_tabtemp_2d(ip1,j,2) > 0.0_rsh)) &
                     htxy_loc = hx(i,j)+MAX(nesting_tabtemp_2d(ip0,j,2),nesting_tabtemp_2d(ip1,j,2))
             IF (htxy_loc > 0.0_rsh) THEN
               igdu_loc(i,j) = 1
               IF (htxy_loc < hm) igdu_loc(i,j) = 5
               IF (htxy_loc < MIN(epsilon,REAL(hm,rlg))) igdu_loc(i,j) = 4
#ifdef key_dyn_adv_linear
               hex_loc(i,j)=MAX(0.0_rsh,hx(i,j))*goulet(i,j)
#else
               hex_loc(i,j)=(htxy_loc+dhx(i,j))*goulet(i,j)
#endif
             ELSE
               igdu_loc(i,j) = 4
               hex_loc(i,j) = 0.0_rsh
             ENDIF
             uvdiff(:,i,j) = 0.0_rsh
             IF (igdu_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 PARALLEL NUM_THREADS(2)

!!$OMP DO SCHEDULE(RUNTIME)
         DO j=j1,j2
           DO i=i1,i2
             coeff(i,j) = 0.0_rsh
             IF (igdu_loc(i,j)/=4) THEN
               dist = nb_connect*Agrif_Irhox_OMP
               dist = min(dist,i-imin)
               dist = min(dist,imax-i)
               dist = min(dist,j-(jmin+1))
               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  pour simplifier OMP mais a verifier
!!$OMP DO SCHEDULE(RUNTIME)
         DO j=MAX(j1,ljminp2),MIN(j2,ljmaxm1)
           DO i=MAX(i1,liminm1),MIN(i2-1,limaxm1)
             Fx(:,i,j) = coeff(i,j)*(uvdiff(:,i+1,j)-uvdiff(:,i,j))/dx(i+1,j)
           END DO
         END DO
!!$OMP END DO NOWAIT

         ! nu d uvdiff / dy
         !Fy(:,:,:) = 0.0_rsh
!!$OMP DO SCHEDULE(RUNTIME)
         DO j=MAX(j1,ljminp1m1),MIN(j2-1,ljmaxm1)
           DO i=MAX(i1,liminp1m1),MIN(i2,limaxm1)
             Fy(:,i,j) = mn(i,j)*coeff(i,j)*(uvdiff(:,i,j+1)-uvdiff(:,i,j))/dy_f(i,j)
           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+1,jmax,imax,imin)
!!$OMP DO SCHEDULE(RUNTIME)
         DO j=MAX(j1+1,ljminp2),MIN(j2-1,ljmaxm1)
           DO i=MAX(i1+1,liminp1),MIN(i2-1,limaxm1)
             IF ((igdu_loc(i,j)/=4).AND.(hex_loc(i,j)/=0.)) THEN
               nesting_tabtemp_3d(:,i,j) = nesting_tabtemp_3d(:,i,j) + &
                 ((1./dx_u(i,j))*(Fx(:,i,j)-Fx(:,i-1,j))  &
                 +(1./dy_u(i,j))*(Fy(:,i,j)-Fy(:,i,j-1)))*halfdt
             END IF
           END DO
         END DO
!!$OMP END DO NOWAIT


!!$OMP END PARALLEL

#endif /* key_agrif_obc_sponge_uv */

     END SELECT

   END IF

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

   RETURN

  END SUBROUTINE interpuz_stepu
#endif
