#ifdef key_substance

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

  SUBROUTINE sub_obc3d_apply_agrif

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE sub_obc3d_apply_agrif  ***
   !&E
   !&E ** Purpose : open boundary conditions for tracers other than temperature and salinity
   !&E              For incoming conditions, used values are stored in the 
   !&E              obc_cv_wat_s, obc_cv_wat_n, obc_cv_wat_w and obc_cv_wat_e arrays,
   !&E              filled in the "subreaddat" routine.
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by :
   !&E    
   !&E ** External calls :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2005-10
   !&E       !  2006-11 (V. Garnier) module - mise en forme
   !&E
   !&E---------------------------------------------------------------------

#include "toolcpp.h"

   !! * Modules used
#ifdef key_agrif
   USE parameters
   USE comvarp2d
   USE toolmpi
   USE agrifcommonid
   USE comsubstance, ONLY : nvp,nv_adv
   USE comvars2d, ONLY : c_agrif_interp
   USE obccombine, ONLY : l_obc_char,l_obc_south,l_obc_north,l_obc_east,l_obc_west

   !! * Arguments

   !! * Local declarations
   INTEGER  :: l,i,j
   EXTERNAL :: interp3dsub

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

   Agrif_UseSpecialValue = .TRUE.
   Agrif_SpecialValue = valmanq

   c_agrif_interp='cv_wat_bc'
   CALL Agrif_Bc_Variable(cv_wat_id,procname=interp3dsub)
   Agrif_UseSpecialValue = .FALSE.

   IF (Agrif_GrandMother()) THEN
     IF ((l_obc_west .OR. l_obc_east)) THEN

       DO j=ljminp2,ljmaxm1

   ! Western boundary
         i=ig(j)
         IF_MPI (limin==i-1) THEN
           IF (hx(i,j) /= -valmanq-h0fond) THEN
              cv_wat(nvp+1:nv_adv,:,i,j) = cv_wat(nvp+1:nv_adv,:,i+1,j)
           ELSE
              cv_wat(nvp+1:nv_adv,:,i,j) = valmanq
           END IF
         ENDIF_MPI

   ! Eastern boundary
         i=id(j)
         IF_MPI (limax==i) THEN
           IF (hx(i-1,j) /= -valmanq-h0fond) THEN
              cv_wat(nvp+1:nv_adv,:,i,j) = cv_wat(nvp+1:nv_adv,:,i-1,j)
           ELSE
              cv_wat(nvp+1:nv_adv,:,i,j) = valmanq
           END IF
         ENDIF_MPI

       END DO

     END IF  ! ends test on .not.l_obc_cycl_x

     IF ((l_obc_north .OR. l_obc_south)) THEN

       DO j=ljmin,ljmax
       DO i=liminp2,limaxm1

   ! Southern boundary
         IF (j == jb(i))THEN
           IF (hy(i,j) /= -valmanq-h0fond) THEN
             cv_wat(nvp+1:nv_adv,:,i,j) = cv_wat(nvp+1:nv_adv,:,i,j+1)
           ELSE
             cv_wat(nvp+1:nv_adv,:,i,j) = valmanq
           END IF

   ! Northern boundary
         ELSE IF (j == jh(i))THEN
           IF (hy(i,j-1) /= -valmanq-h0fond) THEN
             cv_wat(nvp+1:nv_adv,:,i,j) = cv_wat(nvp+1:nv_adv,:,i,j-1)
           ELSE
             cv_wat(nvp+1:nv_adv,:,i,j) = valmanq
           END IF
         END IF

       END DO
       END DO

     END IF  ! ends test on .not.l_obc_cycl_y

   END IF

OMPMPI barrier
OMPMPI master
   CALL_MPI ex_i_rsh(-1,2,nb_var*kmax,liminm1,limaxp2,ljminm1,ljmaxp2,cv_wat(:,:,liminm1:limaxp2,ljminm1:ljmaxp2))
   CALL_MPI ex_j_rsh(-1,2,nb_var*kmax,liminm1,limaxp2,ljminm1,ljmaxp2,cv_wat(:,:,liminm1:limaxp2,ljminm1:ljmaxp2))
OMPMPI end master
OMPMPI barrier
OMPMPI flush(cv_wat)


#endif
  END SUBROUTINE sub_obc3d_apply_agrif

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

#ifdef key_agrif

  SUBROUTINE parsub_obc3d_apply_agrif(num_ws)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE parsub_obc3d_apply_agrif  ***
   !&E
   !&E ** Purpose : open boundary conditions for tracers other than temperature and salinity
   !&E              For incoming conditions, used values are stored in the 
   !&E              obc_cv_wat_s, obc_cv_wat_n, obc_cv_wat_w and obc_cv_wat_e arrays,
   !&E              filled in the "subreaddat" routine.
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by :
   !&E    
   !&E ** External calls :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2005-10
   !&E       !  2006-11 (V. Garnier) module - mise en forme
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   USE parameters
   USE comvarp2d
   USE toolmpi
   USE agrifcommonid
   USE comsubstance, ONLY : nvp,iv_ws_max,nv_ws_max
   USE comvars2d, ONLY : c_agrif_interp

   !! * Arguments
   INTEGER,                                                       INTENT(in) :: num_ws

   !! * Local declarations
   INTEGER  :: l,iv
   EXTERNAL :: interp3dsub

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

   Agrif_UseSpecialValue = .TRUE.
   Agrif_SpecialValue = valmanq
   
   c_agrif_interp='cv_wat_parsub_bc'
   agrif_indic = num_ws
   CALL Agrif_Bc_Variable(cv_wat_id,procname=interp3dsub)

   Agrif_UseSpecialValue = .FALSE.
   
OMPMPINOAGRIF barrier
OMPMPINOAGRIF master
   CALL_MPI ex_i_rsh(-1,2,nb_var*kmax,liminm1,limaxp2,ljminm1,ljmaxp2,cv_wat(:,:,liminm1:limaxp2,ljminm1:ljmaxp2))
   CALL_MPI ex_j_rsh(-1,2,nb_var*kmax,liminm1,limaxp2,ljminm1,ljmaxp2,cv_wat(:,:,liminm1:limaxp2,ljminm1:ljmaxp2))
OMPMPINOAGRIF end master
OMPMPINOAGRIF barrier
OMPMPINOAGRIF flush(cv_wat)


  END SUBROUTINE parsub_obc3d_apply_agrif
#endif

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

  SUBROUTINE sub_obc2d_apply_agrif

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE sub_obc2d_apply_agrif  ***
   !&E
   !&E ** Purpose : open boundary conditions for tracers other than temperature and salinity
   !&E              For incoming conditions, used values are stored in the 
   !&E              obc_cv_wat_s, obc_cv_wat_n, obc_cv_wat_w and obc_cv_wat_e arrays,
   !&E              filled in the "subreaddat" routine.
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by :
   !&E    
   !&E ** External calls :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2005-10
   !&E       !  2006-11 (V. Garnier) module - mise en forme
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
#ifdef key_agrif
   USE parameters
   USE comvarp2d
   USE toolmpi
   USE agrifcommonid
   USE comsubstance, ONLY : nvp,nv_adv
   USE comvars2d, ONLY : c_agrif_interp

   !! * Arguments

   !! * Local declarations
   INTEGER  :: l
   EXTERNAL :: interp3dsub

   !!----------------------------------------------------------------------
   !! * Executable part
   Agrif_UseSpecialValue = .TRUE.
   Agrif_SpecialValue = valmanq

   c_agrif_interp='cv_wat_bc'
   CALL Agrif_Bc_Variable(cv_wat_id,procname=interp3dsub)
   Agrif_UseSpecialValue = .FALSE.

   ! Warning  : faire une procname dediee ? interp2dsub

OMPMPI barrier
OMPMPI master
   CALL_MPI ex_i_rsh(-1,2,nb_var*kmax,liminm1,limaxp2,ljminm1,ljmaxp2,cv_wat(:,:,liminm1:limaxp2,ljminm1:ljmaxp2))
   CALL_MPI ex_j_rsh(-1,2,nb_var*kmax,liminm1,limaxp2,ljminm1,ljmaxp2,cv_wat(:,:,liminm1:limaxp2,ljminm1:ljmaxp2))
OMPMPI end master
OMPMPI barrier
OMPMPI flush(cv_wat)


#endif
  END SUBROUTINE sub_obc2d_apply_agrif

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

  SUBROUTINE parsub_obc2d_apply_agrif

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE parsub_obc2d_apply_agrif  ***
   !&E
   !&E ** Purpose : open boundary conditions for tracers other than temperature and salinity
   !&E              For incoming conditions, used values are stored in the 
   !&E              obc_cv_wat_s, obc_cv_wat_n, obc_cv_wat_w and obc_cv_wat_e arrays,
   !&E              filled in the "subreaddat" routine.
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by :
   !&E    
   !&E ** External calls :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2005-10
   !&E       !  2006-11 (V. Garnier) module - mise en forme
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
#ifdef key_agrif
   USE parameters
   USE comvarp2d
   USE toolmpi
   USE agrifcommonid
   USE comsubstance, ONLY : nvp
   USE comvars2d, ONLY : c_agrif_interp

   !! * Arguments

   !! * Local declarations
   INTEGER  :: l
   EXTERNAL :: interp3dsub

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

   Agrif_UseSpecialValue = .TRUE.
   Agrif_SpecialValue = valmanq
   c_agrif_interp='cv_wat_bc_2d'
   CALL Agrif_Bc_Variable(cv_wat_id,procname=interp3dsub)
   Agrif_UseSpecialValue = .FALSE.


OMPMPINOAGRIF barrier
OMPMPINOAGRIF master
   CALL_MPI ex_i_rsh(-1,2,nb_var*kmax,liminm1,limaxp2,ljminm1,ljmaxp2,cv_wat(:,:,liminm1:limaxp2,ljminm1:ljmaxp2))
   CALL_MPI ex_j_rsh(-1,2,nb_var*kmax,liminm1,limaxp2,ljminm1,ljmaxp2,cv_wat(:,:,liminm1:limaxp2,ljminm1:ljmaxp2))
OMPMPINOAGRIF end master
OMPMPINOAGRIF barrier
OMPMPINOAGRIF flush(cv_wat)

#endif
  END SUBROUTINE parsub_obc2d_apply_agrif

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

#ifdef key_agrif
      subroutine interp3dsub(tabres,l1,l2,k1,k2,i1,i2,j1,j2,before)
USE parameters
use comvarp3d
use comvars2d
use comvarp2d
USE comsubstance
USE agrifcommonid
implicit none

       integer i1,i2,j1,j2,l1,l2,k1,k2
       real tabres(l1:l2,k1:k2,i1:i2,j1:j2)
       integer i,j,l,k, iv
       logical :: before
       integer :: num_ws

if(before) then
       do l=l1,l2
        do j=j1,j2
         do i=i1,i2
          IF (h0(i,j)==-valmanq) THEN
           do k=k1,k2
             tabres(l,k,i,j) = Agrif_SpecialValue
           enddo
          ELSE
           do k=k1,k2
             tabres(l,k,i,j) =cv_wat(l,k,i,j)
           enddo
          ENDIF
         enddo
        enddo
       enddo
else
     select case (c_agrif_interp)
     case('cv_wat_init')
       do l=l1,l2
        do j=j1,j2
         do i=i1,i2
          IF (h0(i,j)==-valmanq) THEN
           do k=k1,k2
             cv_wat(l,k,i,j) = valmanq
           enddo
          ELSE
           do k=k1,k2
             cv_wat(l,k,i,j)=tabres(l,k,i,j)
           enddo
          ENDIF
         enddo
        enddo
       enddo
     case('cv_wat_bc')
       do l=l1,l2
        do j=j1,j2
         do i=i1,i2
          IF (h0(i,j)==-valmanq) THEN
           do k=k1,k2
             if(i==imin+1.or.i==imax.or.j==jmin+1.or.j==jmax) cv_wat(l,k,i,j) = valmanq
           enddo
          ELSE
           do k=k1,k2
             if(i==imin+1.or.i==imax.or.j==jmin+1.or.j==jmax) cv_wat(l,k,i,j)=tabres(l,k,i,j)
           enddo
          ENDIF
         enddo
        enddo
       enddo
     case('cv_wat_parsub_bc')
        num_ws = agrif_indic
        do j=j1,j2
         do i=i1,i2
          if(i==imin+1.or.i==imax.or.j==jmin+1.or.j==jmax) then
          IF (h0(i,j)+ssh(i,j) < 2.0_rsh*h0fond) THEN
           do k=k1,k2

               do l=1,nv_ws_max(num_ws)
                 iv=iv_ws_max(num_ws,l)
                 cv_wat(iv,k,i,j) = valmanq
               enddo
           enddo
          ELSE
           do k=k1,k2
               do l=1,nv_ws_max(num_ws)
                 iv=iv_ws_max(num_ws,l)
                 cv_wat(iv,k,i,j) =tabres(l,k,i,j)
               enddo
           enddo
          ENDIF
          endif
         enddo
        enddo

     case('cv_wat_bc_2d')
        num_ws = agrif_indic
        do j=j1,j2
         do i=i1,i2
          if(i==imin+1.or.i==imax.or.j==jmin+1.or.j==jmax) then
          IF (h0(i,j)+ssh(i,j) < 2.0_rsh*h0fond) THEN
           do k=k1,k2
               do l=1,nvp
                 cv_wat(l,k,i,j) = valmanq
               enddo
           enddo
          ELSE
           do k=k1,k2
               do l=1,nvp
                 cv_wat(l,k,i,j) =tabres(l,k,i,j)
               enddo
           enddo
          ENDIF
          endif
         enddo
        enddo

     end select
endif
      return
      end
#endif

   !!======================================================================
#endif

