  MODULE traobc3dapply

   !!======================================================================
   !!                   ***  MODULE  traobc3dapply  ***
   !! Ocean dynamics : apply open boundary conditions of variables
   !!                  temperature and salinity
   !!======================================================================

#include "toolcpp.h"

   !! * Modules used

   IMPLICIT NONE
   PRIVATE

   !! * Accessibility
   PUBLIC ts_obc3d_apply                ! routine called by stepu.F90 stepv.F90

   !! * Interface
   INTERFACE ts_obc3d_apply
#ifdef key_lagoon
     MODULE PROCEDURE ts_obc3d_apply_lagoon
#else
     MODULE PROCEDURE ts_obc3d_apply_ref
#endif
   END INTERFACE


   !! * Shared module variables

   !! * Private variables

  CONTAINS
   !!======================================================================

#if ! defined key_lagoon
  SUBROUTINE ts_obc3d_apply_ref(u,v,sal,temp)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE ts_obc3d_apply_ref  ***
   !&E
   !&E ** Purpose : apply open boundary conditions for temperature
   !&E              and salinity field
   !&E
   !&E ** Description : apply external value when water mass enters the domain
   !&E
   !&E ** Called by : stepu, stepv
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays : sal,temp,hx,u,obc_sw,obc_tw,dx_u,obc_se,obc_te,
   !&E                     v,obc_ss,obc_ts,dy_v,obc_sn,obc_tn
   !&E
   !&E ** Modified variables : sal,temp
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !                       Original code
   !&E       !  2007-05 (V. Garnier) Mise en forme - introduction de obc_* variables
   !&E       !  2007-09 (V. Garnier) homogeneneity of dx dy...
   !&E       !  2009-04 (A. Mano) MPI2D
   !&E       !  2009-07 (F. Vandermeirsch) MC 2nd order scheme
   !&E       !  2010-04  (T. Odaka) MPI2D cyclic
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   USE parameters, ONLY : imin,imax,jmin,jmax,kmax,                      &
                          ljminp2,ljmaxm1,ljmin,ljmax,ljminm1,ljmaxp1,   &
                          limin,limax,liminm1,limaxp2,liminp2,limaxm1,   &
                          ljmaxp2,liminm2,ljminm2,rsh,rlg,num_testcase,  &
                          ishift_obc_west,ishift_obc_east,jshift_obc_south,jshift_obc_north

   USE comvars2d,  ONLY : dt,ig,hx,h0fond,dx_u,id,jb,hy,dy_v,jh,l_obc_cycl_x,l_obc_cycl_y
   USE comvars3d,  ONLY : l_testcase,valmanq
   USE obcogcm,    ONLY : obc_tw,obc_se,obc_te,obc_ss,obc_ts,obc_sn,obc_tn,obc_sw,obc_coefrel
   USE obccombine, ONLY : l_obc_char,l_obc_south,l_obc_north,l_obc_east,l_obc_west
   USE_MPI toolmpi,ONLY : ex_i_rsh,ex_j_rsh,ex_peri_x_rsh,ex_peri_y_rsh

   !! * Arguments
   REAL(KIND=rsh),DIMENSION(kmax,liminm1:limaxp2,ljminm1:ljmaxp2),INTENT(inout) :: sal,temp
   REAL(KIND=rsh),DIMENSION(kmax,liminm2:limaxp2,ljminm2:ljmaxp2),INTENT(in) :: u,v

   !! * Local declarations
   INTEGER                   :: i,j,k
   REAL(KIND=rsh)            :: uu,vv,cou,dtrelax
   REAL(KIND=rlg)            :: dtm

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

   IF (l_testcase .AND. (num_testcase==1 &
       .OR. num_testcase==33 .OR. num_testcase == 37 .OR. num_testcase == 41 &
! cas kelvin a virer
       .OR. num_testcase==3 .OR. num_testcase==38 .OR. num_testcase==39) &
      ) THEN

     ! None

   ELSE ! general

     dtm=0.5_rlg*dt
     dtrelax=dtm*obc_coefrel  ! dt / time scale of relaxation
     IF (obc_coefrel==0.0_rsh) dtrelax=1.0_rsh

!$OMP SINGLE
   ! periodic conditions along x-axis
     IF (l_obc_cycl_x) THEN
#ifdef key_MPI_2D
!!!!!!#if defined key_tssub_adv_fourthorder_testts  || defined key_tssub_adv_fifthorder_testts
#ifdef key_shiftobc
       CALL_MPI ex_peri_x_rsh(3,kmax,liminm1,limaxp2,ljminm1,ljmaxp2,sal(:,liminm1:limaxp2,ljminm1:ljmaxp2))
       CALL_MPI ex_peri_x_rsh(3,kmax,liminm1,limaxp2,ljminm1,ljmaxp2,temp(:,liminm1:limaxp2,ljminm1:ljmaxp2))
#else
       CALL_MPI ex_peri_x_rsh(1,kmax,liminm1,limaxp2,ljminm1,ljmaxp2,sal(:,liminm1:limaxp2,ljminm1:ljmaxp2))
       CALL_MPI ex_peri_x_rsh(1,kmax,liminm1,limaxp2,ljminm1,ljmaxp2,temp(:,liminm1:limaxp2,ljminm1:ljmaxp2))
#endif
#else
       DO j=ljmin,ljmax
   ! western boundary
         sal(:,ig(j),j) = sal(:,id(j)-1,j)
         sal(:,ig(j)-1,j) = sal(:,id(j)-2,j)
         temp(:,ig(j),j) = temp(:,id(j)-1,j)
         temp(:,ig(j)-1,j) = temp(:,id(j)-2,j)
!!!!!!#if defined key_tssub_adv_fourthorder_testts  || defined key_tssub_adv_fifthorder_testts
#ifdef key_shiftobc
         sal(:,ig(j)-2,j) = sal(:,id(j)-3,j)
         temp(:,ig(j)-2,j) = temp(:,id(j)-3,j)
#endif
   ! eastern boundary
         sal(:,id(j),j) = sal(:,ig(j)+1,j)
         temp(:,id(j),j) = temp(:,ig(j)+1,j)
!!!!#if defined key_tssub_adv_fourthorder_testts  || defined key_tssub_adv_fifthorder_testts
#ifdef key_shiftobc
         sal(:,id(j)+1,j) = sal(:,ig(j)+2,j)
         sal(:,id(j)+2,j) = sal(:,ig(j)+3,j)
         temp(:,id(j)+1,j) = temp(:,ig(j)+2,j)
         temp(:,id(j)+2,j) = temp(:,ig(j)+3,j)
#endif
       END DO
#endif
     END IF  ! ends periodic conditions along x-axis

   ! periodic conditions along y-axis
     IF (l_obc_cycl_y) THEN

#ifdef key_MPI_2D
!!!!!#if defined key_tssub_adv_fourthorder_testts  || defined key_tssub_adv_fifthorder_testts
#ifdef key_shiftobc
       CALL_MPI ex_peri_y_rsh(3,kmax,liminm1,limaxp2,ljminm1,ljmaxp2,sal(:,liminm1:limaxp2,ljminm1:ljmaxp2))
       CALL_MPI ex_peri_y_rsh(3,kmax,liminm1,limaxp2,ljminm1,ljmaxp2,temp(:,liminm1:limaxp2,ljminm1:ljmaxp2))
#else
       CALL_MPI ex_peri_y_rsh(1,kmax,liminm1,limaxp2,ljminm1,ljmaxp2,sal(:,liminm1:limaxp2,ljminm1:ljmaxp2))
       CALL_MPI ex_peri_y_rsh(1,kmax,liminm1,limaxp2,ljminm1,ljmaxp2,temp(:,liminm1:limaxp2,ljminm1:ljmaxp2))
#endif
#else
       DO i=limin,limax
   ! southern boundary
         sal(:,i,jb(i)) = sal(:,i,jh(i)-1)
         sal(:,i,jb(i)-1) = sal(:,i,jh(i)-2)
         temp(:,i,jb(i)) = temp(:,i,jh(i)-1)
         temp(:,i,jb(i)-1) = temp(:,i,jh(i)-2)
!!!#if defined key_tssub_adv_fourthorder_testts  || defined key_tssub_adv_fifthorder_testts
#ifdef key_shiftobc
         sal(:,i,jb(i)-2) = sal(:,i,jh(i)-3)
         temp(:,i,jb(i)-2) = temp(:,i,jh(i)-3)
#endif
   ! northern boundary
         sal(:,i,jh(i)) = sal(:,i,jb(i)+1)
         temp(:,i,jh(i)) = temp(:,i,jb(i)+1)
!!!!#if defined key_tssub_adv_fourthorder_testts  || defined key_tssub_adv_fifthorder_testts
#ifdef key_shiftobc
         sal(:,i,jh(i)+1) = sal(:,i,jb(i)+2)
         sal(:,i,jh(i)+2) = sal(:,i,jb(i)+3)
         temp(:,i,jh(i)+1) = temp(:,i,jb(i)+2)
         temp(:,i,jh(i)+2) = temp(:,i,jb(i)+3)
#endif
       END DO
#endif

     END IF  ! ends periodic conditions along y-axis
!$OMP END SINGLE


     IF (.NOT.l_obc_cycl_x .AND. (l_obc_west .OR. l_obc_east)) THEN

       DO j=ljminp2,ljmaxm1

IF (j>=7 .and. j<=160) then
   ! Western boundary
         i=ig(j)
         !IF_MPI (i==imin+1+ishift_obc_west .AND. limin<=i .AND. i<=limax) THEN
         !IF_MPI (i==imin+31+ishift_obc_west .AND. limin<=i .AND. i<=limax) THEN
         IF_MPI (limin+30==i-1) THEN                                      !(limin==i-1) THEN
           IF (hx(i,j) /= -valmanq-h0fond) THEN
             DO k=1,kmax
               IF (l_obc_char) THEN
                 uu=u(k,i-1,j)+u(k,i,j)
               ELSE
                 uu=u(k,i,j)
               END IF
               IF (uu > 0.0_rsh) THEN
                 !sal(k,i,j)=sal(k,i,j)*(1.0_rsh-dtrelax)+obc_sw(k,1,j)*dtrelax
                 !temp(k,i,j)=temp(k,i,j)*(1.0_rsh-dtrelax)+obc_tw(k,1,j)*dtrelax
                 !IF (j==78) print*,"OGCM SW",i,j,k,obc_sw(k,31,j)
		 sal(k,i,j)=sal(k,i,j)*(1.0_rsh-dtrelax)+obc_sw(k,31,j)*dtrelax
                 temp(k,i,j)=temp(k,i,j)*(1.0_rsh-dtrelax)+obc_tw(k,31,j)*dtrelax
               ELSE
                 IF (l_obc_char) THEN
               ! Extrapolation d ordre 2
                   sal(k,i,j)=2.0_rsh*sal(k,i+1,j)-sal(k,i+2,j)
                   temp(k,i,j)=2.0_rsh*temp(k,i+1,j)-temp(k,i+2,j)
                 ELSE
                   cou=uu*dtm/dx_u(i,j)
                   sal(k,i,j)=sal(k,i,j)*(1.0_rsh+cou)-cou*sal(k,i+1,j)
                   temp(k,i,j)=temp(k,i,j)*(1.0_rsh+cou)-cou*temp(k,i+1,j)
                 END IF
               END IF
             END DO
           END IF
         ENDIF_MPI

   ! Eastern boundary
         i=id(j)
         IF_MPI (i==imax-ishift_obc_east .AND. limin<=i .AND. i<=limax) THEN
           IF (hx(i-1,j) /= -valmanq-h0fond) THEN
             DO k=1,kmax
               IF (l_obc_char) THEN
                 uu=u(k,i-1,j)+u(k,i,j)
               ELSE
                 uu=u(k,i-1,j)
               END IF
               IF (uu < 0.0_rsh) THEN
                 sal(k,i,j)=sal(k,i,j)*(1.0_rsh-dtrelax)+obc_se(k,imax,j)*dtrelax
                 temp(k,i,j)=temp(k,i,j)*(1.0_rsh-dtrelax)+obc_te(k,imax,j)*dtrelax
               ELSE
                 IF (l_obc_char) THEN
                   sal(k,i,j)=2.0_rsh*sal(k,i-1,j)-sal(k,i-2,j)
                   temp(k,i,j)=2.0_rsh*temp(k,i-1,j)-temp(k,i-2,j)
                 ELSE
                   cou=uu*dtm/dx_u(i-1,j)
                   sal(k,i,j)=sal(k,i,j)*(1.0_rsh-cou)+cou*sal(k,i-1,j)
                   temp(k,i,j)=temp(k,i,j)*(1.0_rsh-cou)+cou*temp(k,i-1,j)
                 END IF
               END IF
             END DO
           END IF
         ENDIF_MPI
ENDIF
       END DO

     END IF  ! ends test on .not.l_obc_cycl_x

     IF (.NOT.l_obc_cycl_y .AND. (l_obc_north .OR. l_obc_south)) THEN

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

   ! Southern boundary
         IF (j == jb(i) .AND. j==jmin+1+jshift_obc_south) THEN
           IF (hy(i,j) /= -valmanq-h0fond) THEN
             DO k=1,kmax
               IF (l_obc_char) THEN
                 vv=v(k,i,j-1)+v(k,i,j)
               ELSE
                 vv=v(k,i,j)
               END IF
               IF (vv > 0.0_rsh) THEN
                 sal(k,i,j)=sal(k,i,j)*(1.0_rsh-dtrelax)+obc_ss(k,i,1)*dtrelax
                 temp(k,i,j)=temp(k,i,j)*(1.0_rsh-dtrelax)+obc_ts(k,i,1)*dtrelax
               ELSE
                 IF (l_obc_char) THEN
                   sal(k,i,j)=2.0_rsh*sal(k,i,j+1)-sal(k,i,j+2)
                   temp(k,i,j)=2.0_rsh*temp(k,i,j+1)-temp(k,i,j+2)
                 ELSE
                   cou=vv*dtm/dy_v(i,j)
                   sal(k,i,j)=sal(k,i,j)*(1.0_rsh+cou)-cou*sal(k,i,j+1)
                   temp(k,i,j)=temp(k,i,j)*(1.0_rsh+cou)-cou*temp(k,i,j+1)
                 END IF
               END IF
             END DO
           END IF

   ! Northern boundary
         ELSE IF (j == jh(i) .AND. j==jmax-jshift_obc_north) THEN
           IF (hy(i,j-1) /= -valmanq-h0fond) THEN
             DO k=1,kmax
               IF (l_obc_char) THEN
                 vv=v(k,i,j-1)+v(k,i,j)
               ELSE
                 vv=v(k,i,j-1)
               END IF
               IF (vv < 0.0_rsh) THEN
                 sal(k,i,j)=sal(k,i,j)*(1.0_rsh-dtrelax)+obc_sn(k,i,jmax)*dtrelax
                 temp(k,i,j)=temp(k,i,j)*(1.0_rsh-dtrelax)+obc_tn(k,i,jmax)*dtrelax
               ELSE
                 IF (l_obc_char) THEN
                   sal(k,i,j)=2.0_rsh*sal(k,i,j-1)-sal(k,i,j-2)
                   temp(k,i,j)=2.0_rsh*temp(k,i,j-1)-temp(k,i,j-2)
                 ELSE
                   cou=vv*dtm/dy_v(i,j-1)
                   sal(k,i,j)=sal(k,i,j)*(1.0_rsh-cou)+cou*sal(k,i,j-1)
                   temp(k,i,j)=temp(k,i,j)*(1.0_rsh-cou)+cou*temp(k,i,j-1)
                 END IF
               END IF
             END DO
           END IF
         END IF

       END DO
       END DO

     END IF  ! ends test on .not.l_obc_cycl_y

   END IF   ! ends test on l_testcase

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

  END SUBROUTINE ts_obc3d_apply_ref
#endif

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

#ifdef key_lagoon
  SUBROUTINE ts_obc3d_apply_lagoon(u,v,sal,temp)

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE ts_obc3d_apply_lagoon  ***
   !&E
   !&E ** Purpose : apply open boundary conditions for temperature
   !&E              and salinity field only for LAGOONS
   !&E
   !&E ** Description : apply external value when water mass enters the domain
   !&E
   !&E ** Called by : stepu, stepv
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays : sal,temp,hx,u,obc_sw,obc_tw,dx_u,obc_se,obc_te,
   !&E                     v,obc_ss,obc_ts,dy_v,obc_sn,obc_tn
   !&E
   !&E ** Modified variables : sal,temp
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !                       Original code
   !&E       !  2007-05 (V. Garnier) Mise en forme - introduction de obc_* variables
   !&E       !  2007-09 (V. Garnier) homogeneneity of dx dy...
   !&E       !  2009-04 (A. Mano) MPI2D
   !&E       !  2009-07 (F. Vandermeirsch) MC 2nd order scheme
   !&E       !  2010-04  (T. Odaka) MPI2D cyclic
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used
   USE parameters, ONLY : imin,imax,jmin,jmax,kmax,                      &
                          ljminp2,ljmaxm1,ljmin,ljmax,ljminm1,ljmaxp1,   &
                          limin,limax,liminm1,limaxp2,liminp2,limaxm1,   &
                          ljmaxp2,liminm2,ljminm2,rsh,rlg,num_testcase,  &
                          ishift_obc_west,ishift_obc_east,jshift_obc_south,jshift_obc_north
   USE parameters, ONLY : obc_width
   USE comvars2d,  ONLY : igrau,jgrau,nbgrau,type_grau
   USE comvars2d,  ONLY : dt,ig,hx,h0fond,dx_u,id,jb,hy,dy_v,jh
   USE comvars2d,  ONLY : obc_sgrau,obc_tgrau
   USE comvars3d,  ONLY : valmanq
   USE obcogcm,    ONLY : obc_tw,obc_se,obc_te,obc_ss,obc_ts,obc_sn,obc_tn,obc_sw,obc_coefrel
   USE obccombine, ONLY : l_obc_char,l_obc_south,l_obc_north,l_obc_east,l_obc_west
   USE_MPI toolmpi,ONLY : ex_i_rsh,ex_j_rsh,ex_peri_x_rsh,ex_peri_y_rsh

   !! * Arguments
   REAL(KIND=rsh),DIMENSION(kmax,liminm1:limaxp2,ljminm1:ljmaxp2),INTENT(inout) :: sal,temp
   REAL(KIND=rsh),DIMENSION(kmax,liminm2:limaxp2,ljminm2:ljmaxp2),INTENT(in) :: u,v

   !! * Local declarations
   INTEGER                   :: i,j,k,ng
   REAL(KIND=rsh)            :: uu,vv,cou,dtrelax
   REAL(KIND=rlg)            :: dtm


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

     dtm=0.5_rlg*dt
     dtrelax=dtm*obc_coefrel  ! dt / time scale of relaxation
     IF (obc_coefrel==0.0_rsh) dtrelax=1.0_rsh

    ! key_lagoon : l_obc_cycl_x and l_obc_cycl_y=FALSE always

     DO ng=1,nbgrau
       i=igrau(ng)
       j=jgrau(ng)

      ! Western boundary
        IF (type_grau(ng)==4) THEN
        !! grau sur la frontiere Ouest
          IF_MPI ( (i >= limin) .AND. (i <= limax) .AND.(j >= ljmin) .AND. (j <= ljmax) ) THEN
             DO k=1,kmax
               IF (l_obc_char) THEN
                 uu=u(k,i-1,j)+u(k,i,j)
               ELSE
                 uu=u(k,i,j)
               END IF
               IF (uu > 0.0_rsh) THEN
                 sal(k,i,j)=sal(k,i,j)*(1.0_rsh-dtrelax)+obc_sgrau(ng)*dtrelax
                 temp(k,i,j)=temp(k,i,j)*(1.0_rsh-dtrelax)+obc_tgrau(ng)*dtrelax
               ELSE
                 IF (l_obc_char) THEN
               ! Extrapolation d ordre 2
                   sal(k,i,j)=2.0_rsh*sal(k,i+1,j)-sal(k,i+2,j)
                   temp(k,i,j)=2.0_rsh*temp(k,i+1,j)-temp(k,i+2,j)
                 ELSE
                   cou=uu*dtm/dx_u(i,j)
                   sal(k,i,j)=sal(k,i,j)*(1.0_rsh+cou)-cou*sal(k,i+1,j)
                   temp(k,i,j)=temp(k,i,j)*(1.0_rsh+cou)-cou*temp(k,i+1,j)
                 END IF
               END IF
             END DO
          ENDIF_MPI
        END IF

   ! Eastern boundary
        IF (type_grau(ng)==2) THEN
        !! grau sur la frontiere Est
          IF_MPI ( (i >= limin) .AND. (i <= limax) .AND.(j >= ljmin) .AND. (j <= ljmax) ) THEN
             DO k=1,kmax
               IF (l_obc_char) THEN
                 uu=u(k,i-1,j)+u(k,i,j)
               ELSE
                 uu=u(k,i-1,j)
               END IF
               IF (uu < 0.0_rsh) THEN
                 sal(k,i,j)=sal(k,i,j)*(1.0_rsh-dtrelax)+obc_sgrau(ng)*dtrelax
                 temp(k,i,j)=temp(k,i,j)*(1.0_rsh-dtrelax)+obc_tgrau(ng)*dtrelax
               ELSE
                 IF (l_obc_char) THEN
                   sal(k,i,j)=2.0_rsh*sal(k,i-1,j)-sal(k,i-2,j)
                   temp(k,i,j)=2.0_rsh*temp(k,i-1,j)-temp(k,i-2,j)
                 ELSE
                   cou=uu*dtm/dx_u(i-1,j)
                   sal(k,i,j)=sal(k,i,j)*(1.0_rsh-cou)+cou*sal(k,i-1,j)
                   temp(k,i,j)=temp(k,i,j)*(1.0_rsh-cou)+cou*temp(k,i-1,j)
                 END IF
               END IF
             END DO
          ENDIF_MPI
        ENDIF

     ! Southern boundary
        IF (type_grau(ng)==3) THEN
        !! grau sur la frontiere Sud
          IF_MPI ( (i >= limin) .AND. (i <= limax) .AND.(j >= ljmin) .AND. (j <= ljmax) ) THEN
             DO k=1,kmax
               IF (l_obc_char) THEN
                 vv=v(k,i,j-1)+v(k,i,j)
               ELSE
                 vv=v(k,i,j)
               END IF
               IF (vv > 0.0_rsh) THEN
                 sal(k,i,j)=sal(k,i,j)*(1.0_rsh-dtrelax)+obc_sgrau(ng)*dtrelax
                 temp(k,i,j)=temp(k,i,j)*(1.0_rsh-dtrelax)+obc_tgrau(ng)*dtrelax
               ELSE
                 IF (l_obc_char) THEN
                   sal(k,i,j)=2.0_rsh*sal(k,i,j+1)-sal(k,i,j+2)
                   temp(k,i,j)=2.0_rsh*temp(k,i,j+1)-temp(k,i,j+2)
                 ELSE
                   cou=vv*dtm/dy_v(i,j)
                   sal(k,i,j)=sal(k,i,j)*(1.0_rsh+cou)-cou*sal(k,i,j+1)
                   temp(k,i,j)=temp(k,i,j)*(1.0_rsh+cou)-cou*temp(k,i,j+1)
                 END IF
               END IF
             END DO
          ENDIF_MPI
        END IF

   ! Northern boundary
        IF (type_grau(ng)==1) THEN
        !! grau sur la frontiere Nord
          IF_MPI ( (i >= limin) .AND. (i <= limax) .AND.(j >= ljmin) .AND. (j <= ljmax) ) THEN
             DO k=1,kmax
               IF (l_obc_char) THEN
                 vv=v(k,i,j-1)+v(k,i,j)
               ELSE
                 vv=v(k,i,j-1)
               END IF
               IF (vv < 0.0_rsh) THEN
                 sal(k,i,j)=sal(k,i,j)*(1.0_rsh-dtrelax)+obc_sgrau(ng)*dtrelax
                 temp(k,i,j)=temp(k,i,j)*(1.0_rsh-dtrelax)+obc_tgrau(ng)*dtrelax
               ELSE
                 IF (l_obc_char) THEN
                   sal(k,i,j)=2.0_rsh*sal(k,i,j-1)-sal(k,i,j-2)
                   temp(k,i,j)=2.0_rsh*temp(k,i,j-1)-temp(k,i,j-2)
                 ELSE
                   cou=vv*dtm/dy_v(i,j-1)
                   sal(k,i,j)=sal(k,i,j)*(1.0_rsh-cou)+cou*sal(k,i,j-1)
                   temp(k,i,j)=temp(k,i,j)*(1.0_rsh-cou)+cou*temp(k,i,j-1)
                 END IF
               END IF
             END DO
           ENDIF_MPI
        END IF

     END DO



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

  END SUBROUTINE ts_obc3d_apply_lagoon
#endif

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

  END MODULE traobc3dapply

