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

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE tssub_advdifx_fourthorder  ***
   !&E
   !&E ** Purpose : Estimate of zonal fluxes of temperature and salinity
   !&E              and other substances (advection and diffusion)
   !&E              Advection uses a fourth order centered scheme equivalent 
   !&E
   !&E ** Description : No horizontal diffusion at lower depths ( < hminkxky)
   !&E                  - case of cyclicity in y : correct for eastward propagation,
   !&E                    uncorrect for westward propagation (stencil not long enough)
   !&E
   !&E ** Called by :
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays : htx,hex,hx,fxeau,sal,temp,c(cv_wat),hzex,dx_u 
   !&E                     fsalx,ftempx,fx 
   !&E  
   !&E ** Modified variables : fsalx,ftempx,fx
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E           !  2010-10 (L. Debreu, T. Duhaut) original code
   !&E           !  2016-04 (B.Thouvenin) applied to all substances (dissolved and particulate)
   !&E
   !&E---------------------------------------------------------------------

#include "toolcpp.h"

#ifdef key_tssub_adv_fourthorder

   !! * Modules used
   USE parameters
   USE comvars2d,   ONLY : ig,id,htx,hex,hx,kx,h0fond,hminkxky,dx_u,halfdt,l_obc_cycl_x,dy_u
#if defined key_siggen || defined key_gencoord
   USE comsiggen,   ONLY : hzex
#endif
   USE comfluxdo,   ONLY : fsalx,fxeau
   USE cometemp,    ONLY : ftempx
#ifdef key_substance
   USE comsubstance, ONLY : nv_adv,nv_grav,fx
   USE comsubstance,ONLY : c5
#ifdef key_sedim_MUSTANG
   USE comMUSTANG,    ONLY : corflux
#endif
#endif
   USE_MPI toolmpi, ONLY : ex_i_rsh,ex_i_rlg
#ifdef key_tssub_adv_fourthorder
   USE_MPI toolmpi, ONLY : ex_j_rsh
   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)            :: uu,uuu,cfl,d1,d2,d3,d4,kxb,  &
                                yas,yat,ybs,ybt,ycs,yct,yds,ydt, &
                                deltat,deltas
#ifdef key_substance
   INTEGER                              :: iv
   REAL(KIND=rsh), DIMENSION(nv_adv)    :: ya,yb,yc,yd,delta
#endif

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

   PRINT_DBG*, 'enter TSSUB_ADVDIFX_FOURTHORDER.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,kxb   &
!$OMP& ,yas,yat,ybs,ybt,ycs,yct,yds,ydt        &
#ifdef key_substance
!$OMP& ,iv,ya,yb,yc,yd,delta                   &
#endif
!$OMP& ,deltat,deltas,d1,d2,d3,d4,uu,uuu,cfl)

   DO j=ljminp2,ljmaxm1
     DO i=MAX0(limin,ig(j)),MIN0(limax,id(j)-1)
       DO k=1,kmax
         IF (htx(i,j) <= 0.0_rsh .OR. hx(i,j) == -valmanq-h0fond) THEN
#if ! defined key_offline
           fsalx(k,i,j)=0.0_rlg
           ftempx(k,i,j)=0.0_rlg
#endif
#ifdef key_substance
           fx(nv_grav+1:nv_adv,k,i,j)=0.0_rsh
#endif
         ELSE
           kxb=kx
           IF (htx(i,j) < hminkxky) kxb=0.0_rsh ! no diffusion for small water depth
           uu=fxeau(k,i,j)
#if defined key_siggen || defined key_gencoord
           uuu = uu/(halfdt*hzex(k,i,j))
#else
           uuu = uu/(halfdt*hex(i,j))
#endif
           cfl = uuu*halfdt/dx_u(i,j)
           d1 = (1.0_rsh/24.0_rsh)*(-2.0_rlg+cfl+2.0_rlg*cfl**2-cfl**3)
           d2 = (1.0_rsh/24.0_rsh)*(14.0_rlg-15.0_rlg*cfl-2.0_rlg*cfl**2+3.0_rlg*cfl**3)
           d3 = (1.0_rsh/24.0_rsh)*(14.0_rlg+15.0_rlg*cfl-2.0_rlg*cfl**2-3.0_rlg*cfl**3)
           d4 = (1.0_rsh/24.0_rsh)*(-2.0_rlg-cfl+2.0_rlg*cfl**2+cfl**3)

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

           IF (htx(i+1,j) > 0.0_rsh .AND. (i+1 < id(j) .OR. l_obc_cycl_x)) THEN
#if ! defined key_offline
             yas=sal5(k,i+2,j)
             yat=temp5(k,i+2,j)
#endif
#ifdef key_substance
             ya(nv_grav+1:nv_adv)=c5(nv_grav+1:nv_adv,k,i+2,j)
#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 (htx(i-1,j) > 0.0_rsh .AND. (i > ig(j) .OR. l_obc_cycl_x)) THEN
!              IF (hex(i-1,j) > 0.0_rsh) THEN
#if ! defined key_offline
             yds=sal5(k,i-1,j)
             ydt=temp5(k,i-1,j)
#endif
#ifdef key_substance
             yd(nv_grav+1:nv_adv)=c5(nv_grav+1:nv_adv,k,i-1,j)
#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 ! defined key_offline

   ! advection flux of salinity
   ! --------------------------

!           IF ((ABS(dyds).LE.dsmall).OR.(ABS(dygs).LE.dsmall)) THEN
!             ! no horizontal gradient
!           ELSE
!           END IF
           fsalx(k,i,j)  = uu*(d1*yas+d2*ybs+d3*ycs+d4*yds)

   ! flux of temperature advection
   ! -----------------------------

!           IF ((ABS(dydt).LE.dsmall).OR.(ABS(dygt).LE.dsmall)) THEN
!             ! no horizontal gradient
!           ELSE
!           END IF
           ftempx(k,i,j)  = uu*(d1*yat+d2*ybt+d3*yct+d4*ydt)

   ! key_offline
#endif

#ifdef key_substance

   ! advection flux of other substances
   ! ----------------------------------

           DO iv=nv_grav+1,nv_adv
!             IF ((ABS(dyd(iv)).LE.dsmall).OR.(ABS(dyg(iv)).LE.dsmall)) THEN
!               ! no horizontal gradient
!             ELSE
!             END IF
             fx(iv,k,i,j) = uu*(d1*ya(iv)+d2*yb(iv)+d3*yc(iv)+d4*yd(iv))
           END DO
#endif

#if ! defined key_offline
#if defined key_ts_diffh_geo || defined key_ts_diffh_rho
   ! Advection term alone
   ! --------------------
           ftempx(k,i,j) = ftempx(k,i,j)*dy_u(i,j)
           fsalx(k,i,j)  = fsalx(k,i,j)*dy_u(i,j)
#else
   ! Advection/diffusion term (iso-sigma)
   ! ------------------------------------
#if defined key_siggen || defined key_gencoord
           ftempx(k,i,j) = ftempx(k,i,j)-hzex(k,i,j)*kxb*deltat/dx_u(i,j)*halfdt
           fsalx(k,i,j) = fsalx(k,i,j)-hzex(k,i,j)*kxb*deltas/dx_u(i,j)*halfdt            
#else
           ftempx(k,i,j) = ftempx(k,i,j)-hex(i,j)*kxb*deltat/dx_u(i,j)*halfdt
           fsalx(k,i,j) = fsalx(k,i,j)-hex(i,j)*kxb*deltas/dx_u(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 (iso-sigma) alone
   ! --------------------------------
             fx(iv,k,i,j) = fx(iv,k,i,j)*dy_u(i,j)
#else
   ! Advection/diffusion term (iso-sigma)
   ! ----------------------------------
#if defined key_siggen || defined key_gencoord
             fx(iv,k,i,j) = fx(iv,k,i,j) - hzex(k,i,j)*kxb*delta(iv)/dx_u(i,j)*halfdt
#else
             fx(iv,k,i,j) = fx(iv,k,i,j) - hex(i,j)*kxb*delta(iv)/dx_u(i,j)*halfdt
#endif
#endif
#ifdef key_sedim_MUSTANG
             IF (k == 1) fx(iv,k,i,j)=fx(iv,k,i,j)*corflux(iv,i,j)
#endif
           END DO
#endif

         END IF ! ends test on water depth
       END DO ! ends loop on k
     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_i_rlg(-1,0,kmax,liminm1,limax,ljmin,ljmax,fsalx(:,liminm1:limax,ljmin:ljmax))
   CALL_MPI ex_i_rlg(-1,0,kmax,liminm1,limax,ljmin,ljmax,ftempx(:,liminm1:limax,ljmin:ljmax))
#endif
#ifdef key_substance
   CALL_MPI ex_i_rsh(-1,0,nv_adv*kmax,liminm1,limax,ljmin,ljmax,fx(:,:,liminm1:limax,ljmin:ljmax))
#endif
OMPMPI end master
OMPMPI barrier
#if ! defined key_offline
OMPMPI flush(fsalx,ftempx)
#endif
#ifdef key_substance
OMPMPI flush(fx)
#endif

   PRINT_DBG*, 'exit TSSUB_ADVDIFX_FOURTHORDER.F90 routine'

#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

  END SUBROUTINE tssub_advdifx_fourthorder
