  SUBROUTINE tssub_advdify_fifthorder(sal,temp   &
#ifdef key_substance
                                      ,c &
#endif
                                        )

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE tssub_advdify_fifthorder  ***
   !&E
   !&E ** Purpose : Estimate of meridional fluxes of temperature and salinity
   !&E              and other substances (advection and diffusion)
   !&E              Advection uses 5TH order scheme.
   !&E
   !&E ** Description : No horizontal diffusion at lower depths ( < hminkxky)
   !&E                 - case of cyclicity in y : correct for northward propagation,
   !&E                    uncorrect for southward propagation (stencil not long enough)
   !&E
   !&E ** Called by :
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays : hey,hy,fyeau,sal,temp,c(cv_wat),hzey,dy_v
   !&E                     fsaly,ftempy,fy
   !&E
   !&E ** Modified variables : fsaly,ftempy,fy
   !&E
   !&E ** Reference : Debreu, Duhaut (2011) contrat PREVIMER 09/2 211 097 
   !&E
   !&E ** History :
   !&E       !  2012-07 (V. Garnier) Original code (from tssubadvdifyultimatequickest.F90)
   !&E       !  2016-04  (B.Thouvenin) applied to all substances (dissolved and particulate, include sand)
   !&E       !  2016-12  (B.Thouvenin) version sedim_MUSTANG : only mixsed (mudsand not operationnal)
   !&E
   !&E---------------------------------------------------------------------

#include "toolcpp.h"

#ifdef key_tssub_adv_fifthorder

   !! * Modules used
   USE parameters
   USE comvars2d,   ONLY : jb,jh,hty,hey,hy,h0fond,ky,hminkxky,dy_v,halfdt,l_obc_cycl_y,dx_v
#if defined key_siggen || defined key_gencoord
   USE comsiggen,   ONLY : hzey
#endif
   USE comfluxdo,   ONLY : fsaly,fyeau
   USE cometemp,    ONLY : ftempy
#ifdef key_substance
   USE comsubstance,ONLY : nv_adv,nv_grav,fy
   USE comsubstance,ONLY : c5
#ifdef key_sedim_MUSTANG
   USE comMUSTANG,    ONLY : corfluy
#endif
#endif
   USE_MPI toolmpi, ONLY : ex_j_rsh,ex_j_rlg
   USE_MPI toolmpi, ONLY : ex_i_rsh
#ifdef key_tssub_adv_fifthorder
   USE comvarp2d, ONLY : sal5,temp5  !VALERIE PROVISOIRE
#endif

   IMPLICIT NONE

   !! * Arguments
   REAL(KIND=rsh),DIMENSION(kmax,liminm1:limaxp2,ljminm1:ljmaxp2),INTENT(in) :: sal,temp
#ifdef key_substance
   REAL(KIND=rsh),DIMENSION(nb_var,kmax,liminm1:limaxp2,ljminm1:ljmaxp2),INTENT(in) :: c
#endif

   !! * Local declarations
   INTEGER                   :: i,j,k
   REAL(KIND=rsh), PARAMETER :: dsmall = 1.0d-20
   REAL(KIND=rsh)            :: vv,da,db,dc,dd,de,vvv,cfl,     &
                                dygt,kyb,yas,yat,ybs,ybt,ycs,yct,yds,ydt, &
                                yes,yet,deltat,deltas
#ifdef key_substance
   INTEGER                              :: iv
   REAL(KIND=rsh), DIMENSION(nv_adv)    :: ya,yb,yc,yd,ye,delta
#endif

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

   PRINT_DBG*, 'enter TSSUB_ADVDIFY_FIFTHORDER.F90 routine'

!$OMP SINGLE
   sal5(:,liminm1:limaxp2,ljminm1:ljmaxp2) = sal(:,liminm1:limaxp2,ljminm1:ljmaxp2)
   temp5(:,liminm1:limaxp2,ljminm1:ljmaxp2) = temp(:,liminm1:limaxp2,ljminm1:ljmaxp2)
#ifdef key_substance
   c5(1:nv_adv,:,liminm1:limaxp2,ljminm1:ljmaxp2) = c(1:nv_adv,:,liminm1:limaxp2,ljminm1:ljmaxp2)
#endif
!$OMP END SINGLE
OMPMPI barrier
OMPMPI master
   CALL_MPI ex_i_rsh(-2,3,kmax,liminm2,limaxp3,ljminm2,ljmaxp3,sal5(:,liminm2:limaxp3,ljminm2:ljmaxp3))
   CALL_MPI ex_j_rsh(-2,3,kmax,liminm2,limaxp3,ljminm2,ljmaxp3,sal5(:,liminm2:limaxp3,ljminm2:ljmaxp3))
   CALL_MPI ex_i_rsh(-2,3,kmax,liminm2,limaxp3,ljminm2,ljmaxp3,temp5(:,liminm2:limaxp3,ljminm2:ljmaxp3))
   CALL_MPI ex_j_rsh(-2,3,kmax,liminm2,limaxp3,ljminm2,ljmaxp3,temp5(:,liminm2:limaxp3,ljminm2:ljmaxp3))
#ifdef key_substance
   CALL_MPI ex_i_rsh(-2,3,nv_adv*kmax,liminm2,limaxp3,ljminm2,ljmaxp3,c5(:,:,liminm2:limaxp3,ljminm2:ljmaxp3))
   CALL_MPI ex_j_rsh(-2,3,nv_adv*kmax,liminm2,limaxp3,ljminm2,ljmaxp3,c5(:,:,liminm2:limaxp3,ljminm2:ljmaxp3))
#endif
OMPMPI end master
OMPMPI barrier
#if ! defined key_offline
OMPMPI flush(sal5,temp5)
#endif
#ifdef key_substance
OMPMPI flush(c5)
#endif

!$OMP DO schedule(runtime),private(i,j,k    &
!$OMP&,kyb,yas,yat,ybs,ybt,ycs,yct,yds,ydt,yes,yet,deltat,deltas      &
#ifdef key_substance
!$OMP& ,iv,ya,yb,yc,yd,ye,delta                          &
#endif
!$OMP&,da,db,dc,dd,de,vv,vvv,cfl)

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

       IF (j >= jb(i) .AND. j <= jh(i)-1) THEN
         DO k=1,kmax
           IF (hty(i,j) <= 0.0_rsh .OR. hy(i,j)==-valmanq-h0fond) THEN
#if ! defined key_offline
             fsaly(k,i,j)=0.0_rlg
             ftempy(k,i,j)=0.0_rlg
#endif
#ifdef key_substance
             fy(nv_grav+1:nv_adv,k,i,j)=0.0_rsh
#endif
           ELSE
             kyb=ky
             IF (hty(i,j) < hminkxky) kyb=0.0_rsh ! no diffusion for small water depth
             vv=fyeau(k,i,j)
#if defined key_siggen || defined key_gencoord
             vvv = vv/(halfdt*hzey(k,i,j))
#else
             vvv = vv/(halfdt*hey(i,j))
#endif
             cfl = vvv*halfdt/dy_v(i,j)
                           
             IF (vv <= 0.0_rsh) THEN  ! southward advection

               de=(1.0_rsh/120.0_rsh)*(4.0_rsh-5.0_rsh*cfl**2+cfl**4)
               dd=(1.0_rsh/120.0_rsh)*(-26.0_rsh+5.0_rsh*cfl+30.0_rsh*cfl**2-5.0_rsh*cfl**3-4.0_rsh*cfl**4)
               dc=(1.0_rsh/120.0_rsh)*(94.0_rsh-75.0_rsh*cfl-40.0_rsh*cfl**2+15.0_rsh*cfl**3+6.0_rsh*cfl**4)
               db=(1.0_rsh/120.0_rsh)*(54.0_rsh+75.0_rsh*cfl+10.0_rsh*cfl**2-15.0_rsh*cfl**3-4.0_rsh*cfl**4)
               da=(1.0_rsh/120.0_rsh)*(-6.0_rsh-5.0_rsh*cfl+5.0_rsh*cfl**2+5.0_rsh*cfl**3+cfl**4)

#if ! defined key_offline
               ycs=sal5(k,i,j+1)
               ybs=sal5(k,i,j)
               yct=temp5(k,i,j+1)
               ybt=temp5(k,i,j)
#endif
#ifdef key_substance
               yc(nv_grav+1:nv_adv)=c5(nv_grav+1:nv_adv,k,i,j+1)
               yb(nv_grav+1:nv_adv)=c5(nv_grav+1:nv_adv,k,i,j)
#endif

               IF (hty(i,j+1) > 0.0_rsh .AND. (j+1 < jh(i) .OR. l_obc_cycl_y)) THEN
#if ! defined key_offline
                 yds=sal5(k,i,j+2)
                 ydt=temp5(k,i,j+2)
#endif
#ifdef key_substance
                 yd(nv_grav+1:nv_adv)=c5(nv_grav+1:nv_adv,k,i,j+2)
#endif
               ELSE
#if ! defined key_offline
                 yds=ycs
                 ydt=yct
#endif
#ifdef key_substance
                 yd(nv_grav+1:nv_adv)=yc(nv_grav+1:nv_adv)
#endif
               END IF
               IF (j+2<=jmax .AND. hty(i,MIN0(j+2,jmax)) > 0.0_rsh .AND. (j+2 < jh(i) .OR. l_obc_cycl_y)) THEN
#if ! defined key_offline
                 yes=sal5(k,i,j+3)
                 yet=temp5(k,i,j+3)
#endif
#ifdef key_substance
                 ye(nv_grav+1:nv_adv)=c5(nv_grav+1:nv_adv,k,i,j+3)
#endif
               ELSE
#if ! defined key_offline
                 yes=yds
                 yet=ydt
#endif
#ifdef key_substance
                 ye(nv_grav+1:nv_adv)=yd(nv_grav+1:nv_adv)
#endif
               END IF
               IF (hty(i,j-1) > 0.0_rsh .AND. (j > jb(i) .OR. l_obc_cycl_y)) THEN
#if ! defined key_offline
                 yas=sal5(k,i,j-1)
                 yat=temp5(k,i,j-1)
#endif
#ifdef key_substance
                 ya(nv_grav+1:nv_adv)=c5(nv_grav+1:nv_adv,k,i,j-1)
#endif
               ELSE
#if ! defined key_offline
                 yas=ybs
                 yat=ybt
#endif
#ifdef key_substance
                 ya(nv_grav+1:nv_adv)=yb(nv_grav+1:nv_adv)
#endif
               END IF
#if ! defined key_offline
               deltas=ycs-ybs
               deltat=yct-ybt
#endif
#ifdef key_substance
               delta(nv_grav+1:nv_adv)=yc(nv_grav+1:nv_adv)-yb(nv_grav+1:nv_adv)
#endif


             ELSE      ! northward advection

               da=(1.0_rsh/120.0_rsh)*(4.0_rsh-5.0_rsh*cfl**2+cfl**4)
               db=(1.0_rsh/120.0_rsh)*(-26.0_rsh-5.0_rsh*cfl+30.0_rsh*cfl**2+5.0_rsh*cfl**3-4.0_rsh*cfl**4)
               dc=(1.0_rsh/120.0_rsh)*(94.0_rsh+75.0_rsh*cfl-40.0_rsh*cfl**2-15.0_rsh*cfl**3+6.0_rsh*cfl**4)
               dd=(1.0_rsh/120.0_rsh)*(54.0_rsh-75.0_rsh*cfl+10.0_rsh*cfl**2+15.0_rsh*cfl**3-4.0_rsh*cfl**4)
               de=(1.0_rsh/120.0_rsh)*(-6.0_rsh+5.0_rsh*cfl+5.0_rsh*cfl**2-5.0_rsh*cfl**3+cfl**4)

#if ! defined key_offline
               ycs=sal5(k,i,j)
               yds=sal5(k,i,j+1)
               yct=temp5(k,i,j)
               ydt=temp5(k,i,j+1)
#endif
#ifdef key_substance
               yc(nv_grav+1:nv_adv)=c5(nv_grav+1:nv_adv,k,i,j)
               yd(nv_grav+1:nv_adv)=c5(nv_grav+1:nv_adv,k,i,j+1)
#endif
               IF (hty(i,j-1) > 0.0_rsh .AND. (j > jb(i) .OR. l_obc_cycl_y)) THEN
#if ! defined key_offline
                 ybs=sal5(k,i,j-1)
                 ybt=temp5(k,i,j-1)
#endif
#ifdef key_substance
                 yb(nv_grav+1:nv_adv)=c5(nv_grav+1:nv_adv,k,i,j-1)
#endif
               ELSE
#if ! defined key_offline
                 ybs=ycs
                 ybt=yct
#endif
#ifdef key_substance
                 yb(nv_grav+1:nv_adv)=yc(nv_grav+1:nv_adv)
#endif
               END IF
               IF (j-2>=jmin .AND. hty(i,MAX0(j-2,jmin)) > 0.0_rsh .AND. (j-1 > jb(i) .OR. l_obc_cycl_y)) THEN
#if ! defined key_offline
                 yas=sal5(k,i,j-2)
                 yat=temp5(k,i,j-2)
#endif
#ifdef key_substance
                 ya(nv_grav+1:nv_adv)=c5(nv_grav+1:nv_adv,k,i,j-2)
#endif
               ELSE
#if ! defined key_offline
                 yas=ybs
                 yat=ybt
#endif
#ifdef key_substance
                 ya(nv_grav+1:nv_adv)=yb(nv_grav+1:nv_adv)
#endif
               END IF
               IF (hty(i,j+1) > 0.0_rsh .AND. (j+1 < jh(i) .OR. l_obc_cycl_y)) THEN
#if ! defined key_offline
                 yes=sal5(k,i,j+2)
                 yet=temp5(k,i,j+2)
#endif
#ifdef key_substance
                 ye(nv_grav+1:nv_adv)=c5(nv_grav+1:nv_adv,k,i,j+2)
#endif
               ELSE
#if ! defined key_offline
                 yes=yds
                 yet=ydt
#endif
#ifdef key_substance
                 ye(nv_grav+1:nv_adv)=yd(nv_grav+1:nv_adv)
#endif
               END IF

#if ! defined key_offline
               deltas=yds-ycs
               deltat=ydt-yct
#endif
#ifdef key_substance
               delta(nv_grav+1:nv_adv)=yd(nv_grav+1:nv_adv)-yc(nv_grav+1:nv_adv)
#endif

             END IF       ! ends test on velocity sign


   ! advection flux of salinity/temperature/substances
   ! -------------------------------------------------

#if ! defined key_offline
             fsaly(k,i,j) = vv*(da*yas+db*ybs+dc*ycs+dd*yds+de*yes)
             ftempy(k,i,j) = vv*(da*yat+db*ybt+dc*yct+dd*ydt+de*yet)
#endif

#ifdef key_substance
             DO iv=nv_grav+1,nv_adv
               fy(iv,k,i,j) = vv*(da*ya(iv)+db*yb(iv)+dc*yc(iv)+dd*yd(iv)+de*ye(iv))
             END DO
#endif


#if ! defined key_offline
#if defined key_ts_diffh_geo || defined key_ts_diffh_rho
   ! Advection term alone
   ! --------------------
             ftempy(k,i,j)=ftempy(k,i,j)*dx_v(i,j)
             fsaly(k,i,j)=fsaly(k,i,j)*dx_v(i,j)
#else
   ! Advection/diffusion term (iso-sigma)
   ! ------------------------------------
#if defined key_siggen || defined key_gencoord
             ftempy(k,i,j)=ftempy(k,i,j)-hzey(k,i,j)*kyb*deltat/dy_v(i,j)*halfdt
             fsaly(k,i,j)=fsaly(k,i,j)-hzey(k,i,j)*kyb*deltas/dy_v(i,j)*halfdt              
#else
             ftempy(k,i,j)=ftempy(k,i,j)-hey(i,j)*kyb*deltat/dy_v(i,j)*halfdt
             fsaly(k,i,j)=fsaly(k,i,j)-hey(i,j)*kyb*deltas/dy_v(i,j)*halfdt              
#endif
#endif
#endif

#ifdef key_substance
             DO iv=nv_grav+1,nv_adv
#if defined key_ts_diffh_geo || defined key_ts_diffh_rho
   ! Advection term alone
   ! --------------------
               fy(iv,k,i,j) = fy(iv,k,i,j)*dx_v(i,j)
#else
   ! Advection/diffusion term (iso-sigma)
   ! ------------------------------------
#if defined key_siggen || defined key_gencoord
               fy(iv,k,i,j) = fy(iv,k,i,j) - hzey(k,i,j)*kyb*delta(iv)/dy_v(i,j)*halfdt
#else
               fy(iv,k,i,j) = fy(iv,k,i,j) - hey(i,j)*kyb*delta(iv)/dy_v(i,j)*halfdt
#endif
#endif
#ifdef key_sedim_MUSTANG
               IF (k.eq.1) fy(iv,k,i,j)=fy(iv,k,i,j)*corfluy(iv,i,j)
#endif
             END DO
#endif

           END IF ! ends test on water depth
         END DO ! ends loop on k
       END IF ! for selecting right j loop
     END DO ! ends loop on i
   END DO ! ends loop on j
!$OMP END DO

OMPMPI barrier
OMPMPI master
#if ! defined key_offline
   CALL_MPI ex_j_rlg(-1,0,kmax,limin,limax,ljminm1,ljmax,fsaly(:,limin:limax,ljminm1:ljmax))
   CALL_MPI ex_j_rlg(-1,0,kmax,limin,limax,ljminm1,ljmax,ftempy(:,limin:limax,ljminm1:ljmax))
#endif
#ifdef key_substance
   CALL_MPI ex_j_rsh(-1,0,nv_adv*kmax,limin,limax,ljminm1,ljmax,fy(:,:,limin:limax,ljminm1:ljmax))
#endif
OMPMPI end master
OMPMPI barrier
#if ! defined key_offline
OMPMPI flush(fsaly,ftempy)
#endif
#ifdef key_substance
OMPMPI flush(fy)
#endif

#else

   USE parameters, ONLY : rsh,kmax,liminm1,limaxp2,ljminm1,ljmaxp2
   REAL(KIND=rsh),DIMENSION(kmax,liminm1:limaxp2,ljminm1:ljmaxp2),INTENT(in) :: sal,temp
#ifdef key_substance
   REAL(KIND=rsh),DIMENSION(kmax,kmax,liminm1:limaxp2,ljminm1:ljmaxp2),INTENT(in) :: c
#endif

#endif

   PRINT_DBG*, 'exit TSSUB_ADVDIFY_FIFTHORDER.F90 routine'

  END SUBROUTINE tssub_advdify_fifthorder
