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

  SUBROUTINE init_viscoef

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE init_viscoef  ***
   !&E
   !&E ** Purpose : Initialize the coefficients for horizontal viscosity.
   !&E              Set up sponge layers.
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by : main
   !&E
   !&E ** External calls : 
   !&E
   !&E ** Used ij-arrays : 
   !&E
   !&E ** Modified variables : vis2d, vish_xe, vish_phi
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E        !  2004-08-16
   !&E        !  2008-08  (T. Odaka) MPI
   !&E        !  2009-07  (N. Bruneau, F. Dumas) coupling with SWAN
   !&E
   !&E---------------------------------------------------------------------
   !! * Modules used

#include "toolcpp.h"

   USE parameters
   USE comvars2d, ONLY : fvisc, vish_xe, dy, vis2d, vish_phi,       &
                         jb,jh, id, ig, h0_g,sponge_nbcell, vismul, &
                         l_spongn, l_sponge, l_spongs, l_spongw,    &
                         l_modele2d
#ifdef key_waveforcing
   USE comvars3d,  ONLY : rhoref
   USE comvarp2d,  ONLY : ssh
   USE waveforcing,ONLY : M_battjes,Dissip
#endif
   USE_MPI toolmpi,   ONLY : ex_i_rsh,ex_j_rsh

   IMPLICIT NONE

   !! * Arguments

   !! * Local declarations
   INTEGER        :: i,j
   REAL(kind=rsh) :: nu0,vismuloc

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

!$OMP PARALLEL DEFAULT(none) PRIVATE(i,j,nu0,vismuloc) &
!$OMP& SHARED(fvisc,dy,vis2d,vish_xe,vish_phi,h0_g,    &
#ifdef key_agrif 
!$OMP & imin,imax,jmin,jmax ,                          &
#endif
!$OMP& ljmin,ljmax,ljminp1,l_modele2d,jb,jh,id,ig,     &
!$OMP& limin,limax,liminp1,                            &
#ifdef key_waveforcing
!$OMP& rhoref,ssh,M_battjes,Dissip,                    &
#endif
!$OMP& l_spongw,sponge_nbcell,vismul,l_sponge,l_spongn,l_spongs)

!$OMP DO SCHEDULE(static) 
   DO j=ljmin,ljmax
     DO i=limin,limax
       nu0=fvisc*0.01_rsh*dy(i,j)**1.15_rsh
#ifdef key_waveforcing
       nu0 = nu0 + M_battjes * (h0_g(i,j)+ssh(i,j)) * (Dissip(i,j)/rhoref)**(1.0_rsh/3.0_rsh)
#endif
       vis2d(i,j)=nu0
       IF (.NOT.l_modele2d) THEN
         vish_xe(:,i,j)=nu0
         vish_phi(:,i,j)=nu0
       END IF
     END DO
   END DO
!$OMP END DO

   !   a l ouest
   !------------
!$OMP DO SCHEDULE(static)
   DO j=ljminp1,ljmax
     IF (l_spongw) THEN
       IF (h0_g(ig(j),j) > -valmanq) THEN
         DO i=ig(j),MAX(MIN(ig(j)+sponge_nbcell,imax),imin)
           IF_MPI ((i >= limin).AND.(i <= limax)) THEN
!     vismuloc=(5.* exp(-real(i-1)/10.))
!     vismuloc=(i-(ig(j)+sponge_nbcell))*(1-vismul)/sponge_nbcell+1.0_rsh
           vismuloc=vismul
           nu0=fvisc*0.01_rsh*dy(i,j)**1.15_rsh
#ifdef key_waveforcing
           nu0 = nu0 + M_battjes * (h0_g(i,j)+ssh(i,j)) * (Dissip(i,j)/rhoref)**(1.0_rsh/3.0_rsh)
#endif
           vis2d(i,j)=MAX(nu0*vismuloc,vis2d(i,j))
           IF (.NOT.l_modele2d) THEN
             vish_phi(:,i,j)=MAX(nu0*vismuloc,vish_phi(:,i,j))
             vish_xe(:,i,j) =MAX(nu0*vismuloc,vish_xe(:,i,j))
           END IF
           ENDIF_MPI
         ENDDO
       ENDIF
     ENDIF

     !   a l est
     !----------
     IF (l_sponge) THEN
       IF (h0_g(id(j),j) > -valmanq) THEN
         DO i=MIN(MAX(id(j)-sponge_nbcell,imin),imax),id(j)
           IF_MPI ((i >= limin).AND.(i <= limax)) THEN
!     vismuloc=(5.* exp(-real(id(j)-i)/10.))
!     vismuloc=((id(j)-sponge_nbcell)-i)*(1-vismul)/sponge_nbcell+1.0_rsh
           vismuloc=vismul
           nu0=fvisc*0.01_rsh*dy(i,j)**1.15_rsh
#ifdef key_waveforcing
           nu0 = nu0 + M_battjes * (h0_g(i,j)+ssh(i,j)) * (Dissip(i,j)/rhoref)**(1.0_rsh/3.0_rsh)
#endif
           vis2d(i,j)=MAX(nu0*vismuloc,vis2d(i,j))
           IF (.NOT.l_modele2d) THEN
             vish_phi(:,i,j)=MAX(nu0*vismuloc,vish_phi(:,i,j))
             vish_xe(:,i,j) =MAX(nu0*vismuloc,vish_xe(:,i,j))
           END IF
           ENDIF_MPI
         END DO
       END IF
     END IF
   END DO
!$OMP END  DO

   !   au nord
   !----------
!$OMP DO SCHEDULE(static)
   DO i=liminp1,limax
     IF (l_spongn) THEN
       IF (h0_g(i,jh(i)) > -valmanq) THEN
         DO j=MIN(MAX(jh(i)-sponge_nbcell,jmin),jmax),jh(i)
           IF_MPI ((j >= ljmin).AND.(j <= ljmax)) THEN
!     vismuloc=(5.* exp(-real(jh(i)-j)/10.))
!     vismuloc=((jh(i)-sponge_nbcell)-j)*(1-vismul)/sponge_nbcell+1.0_rsh
             vismuloc=vismul
             nu0=fvisc*0.01_rsh*dy(i,j)**1.15_rsh
#ifdef key_waveforcing
             nu0 = nu0 + M_battjes * (h0_g(i,j)+ssh(i,j)) * (Dissip(i,j)/rhoref)**(1.0_rsh/3.0_rsh)
#endif
             vis2d(i,j)=MAX(nu0*vismuloc,vis2d(i,j))
             IF (.NOT.l_modele2d) THEN
               vish_phi(:,i,j)=MAX(nu0*vismuloc,vish_phi(:,i,j))
               vish_xe(:,i,j) =MAX(nu0*vismuloc,vish_xe(:,i,j))
             END IF
           ENDIF_MPI
         END DO
       END IF
     END IF

     !   au sud
     !----------
     IF (l_spongs) THEN                         ! couche eponge au sud si ouverte
       IF (h0_g(i,jb(i)) > -valmanq) THEN
         DO j=jb(i),MAX(MIN(jb(i)+sponge_nbcell,jmax),jmin)
           IF_MPI ((j >= ljmin).AND.(j <= ljmax)) THEN
!     vismuloc=(5.* exp(-real(j-1)/10.))
!     vismuloc=(j-(jb(i)+sponge_nbcell))*(1-vismul)/sponge_nbcell+1.0_rsh
             vismuloc=vismul
             nu0=fvisc*0.01_rsh*dy(i,j)**1.15_rsh
#ifdef key_waveforcing
             nu0 = nu0 + M_battjes * (h0_g(i,j)+ssh(i,j)) * (Dissip(i,j)/rhoref)**(1.0_rsh/3.0_rsh)
#endif
             vis2d(i,j)=MAX(nu0*vismuloc,vis2d(i,j))
             IF (.NOT.l_modele2d) THEN
               vish_phi(:,i,j)=MAX(nu0*vismuloc,vish_phi(:,i,j))
               vish_xe(:,i,j) =MAX(nu0*vismuloc,vish_xe(:,i,j))
             END IF
           ENDIF_MPI
         END DO
       END IF
     END IF

   END DO
!$OMP END DO

!$OMP Do schedule(static) 
   DO j=ljmin,ljmax
     DO i=limin,limax
       IF (h0_g(i,j)==-valmanq) THEN
         vis2d(i,j)=valmanq
         vish_xe(:,i,j)=valmanq
         vish_phi(:,i,j)=valmanq
       END IF
     END DO
   END DO
!$OMP end do

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!corrections Gironde FG(22/11/2018)

!$OMP Do schedule(static) 
   DO j=60,120
     IF_MPI ( ljmin <= j .AND. j <= ljmax) THEN
       DO i=200,imax
         IF_MPI ( limin <= i .AND. i <= limax) THEN
           vish_phi(1:kmax,i,j) = 0.1_rsh*vish_phi(1:kmax,i,j)
           vish_xe(1:kmax,i,j) = 0.1_rsh*vish_xe(1:kmax,i,j)
         ENDIF_MPI
       END DO
     ENDIF_MPI
   END DO
!$OMP end do

!fin corrections Gironde FG(22/11/2018)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!$OMP end parallel 

   CALL_MPI ex_i_rsh(-1,1,1,liminm1,limaxp1,ljminm1,ljmaxp1,vis2d(liminm1:limaxp1,ljminm1:ljmaxp1))
   CALL_MPI ex_j_rsh(-1,1,1,liminm1,limaxp1,ljminm1,ljmaxp1,vis2d(liminm1:limaxp1,ljminm1:ljmaxp1))
   IF (.NOT.l_modele2d) THEN
     CALL_MPI ex_i_rsh(-1,0,kmax,liminm1,limax,ljminm1,ljmax,vish_phi(:,liminm1:limax,ljminm1:ljmax))
     CALL_MPI ex_j_rsh(-1,0,kmax,liminm1,limax,ljminm1,ljmax,vish_phi(:,liminm1:limax,ljminm1:ljmax))
     CALL_MPI ex_i_rsh(0,1,kmax,limin,limaxp1,ljmin,ljmaxp1,vish_xe(:,limin:limaxp1,ljmin:ljmaxp1))
     CALL_MPI ex_j_rsh(0,1,kmax,limin,limaxp1,ljmin,ljmaxp1,vish_xe(:,limin:limaxp1,ljmin:ljmaxp1))
   END IF

END SUBROUTINE init_viscoef
