  SUBROUTINE tssub_advdifx_uq(sal,temp   &

                                      ,c &

                                        )

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE tssub_advdifx_ultimatequickest  ***
!&E
!&E ** Purpose : Estimate of zonal fluxes of temperature and salinity
!&E              and other substances (advection and diffusion)
!&E              Advection uses QUICKEST scheme with ULTIMATE limiter.
!&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 : Leonard B. P. (1991) : The ULTIMATE conservative
!&E                difference scheme applied to unsteady one-dimensional
!&E                advection. Computer Methods in Applied Mechanics and
!&E                Engineering, 88, 17-74
!&E
!&E ** History :
!&E           !  2008-09 (L. Debreu, T. Duhaut, M. Honnorat) original code
!&E           !  2008-09 (V. Garnier) generalized sigma, correction for western boundary
!&E           !  2008-10 (V. Garnier) introduction of substances other than
!&E                                   temperature and salinity
!&E           !  2009-04 (A. Mano) MPI2D
!&E           !  2010-10 (T. Duhaut) introduction of diffusion along isopycnes
!&E           !  2016-04 (B.Thouvenin) applied to all substances (dissolved and particulate)
!&E           !  2016-12  (B.Thouvenin) version sedim_MUSTANG : only mixsed (mudsand not operationnal)
!&E
!&E---------------------------------------------------------------------





























































!! * Modules used
   USE parameters
   USE comvars2d, ONLY : ig,id,htx,hex,hx,kx,h0fond,hminkxky,dx_u,halfdt,l_obc_cycl_x,dy_u

   USE comfluxdo, ONLY : fsalx,fxeau
   USE cometemp,  ONLY : ftempx

   USE comsubstance, ONLY : nv_adv,nv_grav,fx

   USE comMUSTANG,    ONLY : corflux


   USE toolmpi, ONLY : ex_i_rsh,ex_i_rlg

   IMPLICIT NONE

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

   REAL(KIND=rsh),DIMENSION(nb_var,kmax,liminm1:limaxp2,ljminm1:ljmaxp2),INTENT(in) :: c


!! * Local declarations
   INTEGER                   :: i,j,k
   REAL(KIND=rsh), PARAMETER :: dsmall = 1.0d-20
   REAL(KIND=rsh)            :: uu,dyds,dygs,dydt,d0,d1,uuu,courant,     &
                                dygt,kxb,yas,yat,ybs,ybt,ycs,yct,thetap, &
                                psip,deltat,deltas

   INTEGER                              :: iv
   REAL(KIND=rsh), DIMENSION(nv_adv)    :: dyd,dyg,ya,yb,yc,delta


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

   !PRINT*, 'enter TSSUB_ADVDIFX_ULTIMATEQUICKEST routine'

!$OMP DO schedule(runtime),private(i,j,k,kxb               &
!$OMP& ,dyds,dygs,dydt,dygt,yas,yat,ybs,ybt,ycs,yct        &

!$OMP& ,iv,dyd,dyg,ya,yb,yc,delta                          &

!$OMP& ,deltat,deltas,d0,d1,uu,uuu,courant,thetap,psip)

   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

           fsalx(k,i,j)=0.0_rlg
           ftempx(k,i,j)=0.0_rlg


!refonte subtances advection avril 2016
           fx(nv_grav+1:nv_adv,k,i,j)=0.0_rsh

         ELSE
           kxb=kx
           IF (htx(i,j) < hminkxky) kxb=0.0_rsh ! no diffusion for small water depth
           uu=fxeau(k,i,j)

           uuu = uu/(halfdt*hex(i,j))

           courant = ABS(uuu)*halfdt/dx_u(i,j)
           d0=(1.0_rsh/6.0_rsh)*(2.0_rsh-courant)*(1.0_rsh-courant)
           d1=(1.0_rsh/6.0_rsh)*(1.0_rsh-courant)*(1.0_rsh+courant)
             
           IF (uu <= 0.0_rsh) THEN  ! westward advection

             ybs=sal(k,i+1,j)
             ycs=sal(k,i,j)
             ybt=temp(k,i+1,j)
             yct=temp(k,i,j)


             yb(nv_grav+1:nv_adv)=c(nv_grav+1:nv_adv,k,i+1,j)
             yc(nv_grav+1:nv_adv)=c(nv_grav+1:nv_adv,k,i,j)

             IF (htx(i+1,j) > 0.0_rsh .AND. (i+1 < id(j) .OR. l_obc_cycl_x)) THEN

               yas=sal(k,i+2,j)
               yat=temp(k,i+2,j)


               ya(nv_grav+1:nv_adv)=c(nv_grav+1:nv_adv,k,i+2,j)

             ELSE

               yas=ybs
               yat=ybt


               ya(nv_grav+1:nv_adv)=yb(nv_grav+1:nv_adv)

             END IF

             dyds=ycs-ybs
             dygs=ybs-yas
             dydt=yct-ybt
             dygt=ybt-yat
             deltas=ybs-ycs
             deltat=ybt-yct


             dyd(nv_grav+1:nv_adv)=yc(nv_grav+1:nv_adv)-yb(nv_grav+1:nv_adv)
             dyg(nv_grav+1:nv_adv)=yb(nv_grav+1:nv_adv)-ya(nv_grav+1:nv_adv)
             delta(nv_grav+1:nv_adv)=yb(nv_grav+1:nv_adv)-yc(nv_grav+1:nv_adv)


           ELSE  ! eastward advection


             ybs=sal(k,i,j)
             ycs=sal(k,i+1,j)
             ybt=temp(k,i,j)
             yct=temp(k,i+1,j)


             yb(nv_grav+1:nv_adv)=c(nv_grav+1:nv_adv,k,i,j)
             yc(nv_grav+1:nv_adv)=c(nv_grav+1:nv_adv,k,i+1,j)

             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

               yas=sal(k,i-1,j)
               yat=temp(k,i-1,j)


               ya(nv_grav+1:nv_adv)=c(nv_grav+1:nv_adv,k,i-1,j)

             ELSE

               yas=ybs
               yat=ybt


               ya(nv_grav+1:nv_adv)=yb(nv_grav+1:nv_adv)

             END IF

             dyds=ycs-ybs
             dygs=ybs-yas
             dydt=yct-ybt
             dygt=ybt-yat
             deltas=ycs-ybs
             deltat=yct-ybt


             dyg(nv_grav+1:nv_adv)=yb(nv_grav+1:nv_adv)-ya(nv_grav+1:nv_adv)
             dyd(nv_grav+1:nv_adv)=yc(nv_grav+1:nv_adv)-yb(nv_grav+1:nv_adv)
             delta(nv_grav+1:nv_adv)=yc(nv_grav+1:nv_adv)-yb(nv_grav+1:nv_adv)


           END IF    ! ends test on velocity sign




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

           IF ((ABS(dyds).LE.dsmall).OR.(ABS(dygs).LE.dsmall)) THEN
! no horizontal gradient
             thetap = 0.0_rsh
             psip = 0.0_rsh
           ELSE
! ultimate limiter
             thetap=(dygs+dsmall)/(dyds+dsmall)
             psip = d0+d1*thetap
             psip=MAX(0.0_rsh,MIN( MIN(1.0_rsh,psip),((1.0_rsh-courant)/(courant+dsmall))*thetap) )
           END IF
           fsalx(k,i,j)  = uu*(ybs+psip*dyds)

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

           IF ((ABS(dydt).LE.dsmall).OR.(ABS(dygt).LE.dsmall)) THEN
! no horizontal gradient
             thetap = 0.0_rsh
             psip = 0.0_rsh
           ELSE
! ultimate limiter
             thetap=(dygt+dsmall)/(dydt+dsmall)
             psip = d0+d1*thetap
             psip=MAX(0.0_rsh,MIN(MIN(1.0_rsh,psip),((1.0_rsh-courant)/(courant+dsmall))*thetap))
           END IF
           ftempx(k,i,j)  = uu*(ybt+psip*dydt)

! key_offline




! 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
               thetap = 0.0_rsh
               psip = 0.0_rsh
             ELSE
! ultimate limiter
               thetap=(dyg(iv)+dsmall)/(dyd(iv)+dsmall)
               psip = d0+d1*thetap
               psip=MAX(0.0_rsh,MIN(MIN(1.0_rsh,psip),((1.0_rsh-courant)/(courant+dsmall))*thetap))
             END IF
             fx(iv,k,i,j) = uu*(yb(iv)+psip*dyd(iv))
           END DO




! Advection/diffusion term (iso-sigma)
! ----------------------------------

           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            





           DO iv=nv_grav+1,nv_adv

! Advection/diffusion term (iso-sigma)
! ----------------------------------

             fx(iv,k,i,j) = fx(iv,k,i,j) - hex(i,j)*kxb*delta(iv)/dx_u(i,j)*halfdt



             IF (k == 1) fx(iv,k,i,j)=fx(iv,k,i,j)*corflux(iv,i,j)

           END DO


         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

!$OMP barrier
!$OMP master

   CALL ex_i_rlg(-1,0,kmax,liminm1,limax,ljmin,ljmax,fsalx(:,liminm1:limax,ljmin:ljmax))
   CALL ex_i_rlg(-1,0,kmax,liminm1,limax,ljmin,ljmax,ftempx(:,liminm1:limax,ljmin:ljmax))


   CALL ex_i_rsh(-1,0,nv_adv*kmax,liminm1,limax,ljmin,ljmax,fx(:,:,liminm1:limax,ljmin:ljmax))

!$OMP end master
!$OMP barrier

!$OMP flush(fsalx,ftempx)


!$OMP flush(fx)


   !PRINT*, 'exit TSSUB_ADVDIFX_ULTIMATEQUICKEST routine'

  END SUBROUTINE tssub_advdifx_uq
