  SUBROUTINE tssub_advdif_macho3d(xe,sal,temp  &

                                 ,cv_wat        &

                                 )

!&E---------------------------------------------------------------------
!&E                 ***  ROUTINE tssub_advdif_macho3d  ***
!&E
!&E ** Purpose : call the good advection scheme depending of the cpp key
!&E
!&E ** Description : use macho splitting in 3 directions X,Y,Z
!&E                  The tracer advection is computed in a different order at each time step:
!&E                  along directions XYZ - XZY - YXZ - YZX - ZXY - ZYX
!&E
!&E ** Called by :
!&E
!&E ** External calls :
!&E
!&E ** Used ij-arrays : temp,sal,cv_wat
!&E
!&E ** Modified variables :
!&E
!&E ** Reference : Leonard B. P., Lock A. P. and M. K. MacVean (1996) :
!&E                Conservative Explicit Unrestricted-Time-Step
!&E                Multidimensional COnstancy-Preserving Advections Schemes.
!&E                Monthly Weather Review, 124, 2588:2605
!&E
!&E                Debreu, Duhaut (2011) - Contrat PREVIMER 09/2 211 097
!&E
!&E ** History :
!&E       !  2010-12  (V. Garnier) Original code
!&E
!&E---------------------------------------------------------------------





























































!! Modules used
   USE parameters, ONLY : rsh,imin,imax,jmin,ljminm2,ljmaxp2,jmax,kmax,ljmin,ljmax, &
                          liminm2,liminm1,limaxp1,limaxp2,limin,limax,ljminm1,ljmaxp1

   USE parameters, ONLY : nb_var

   USE comvars2d,  ONLY : nbouc,ierrorlog,nbouc_macho
   USE comvarp2d,  ONLY : temp2,sal2,temp3,sal3

   USE comsubstance, ONLY : c2,c3


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

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


!! * Local declarations
   INTEGER :: nxyz

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

   !PRINT*, 'enter TSSUB_ADVDIF_MACHO3D.F90 routine'


   nxyz=MOD(nbouc+nbouc_macho,6)
   SELECT CASE (nxyz)

     CASE(1) ! XYZ

     CALL tssub_advx_macho(sal,temp,sal2,temp2   &

                                   ,cv_wat,c2    &

                                           )
     CALL tssub_advy_macho(sal2,temp2,sal3,temp3  &

                                      ,c2,c3      &

                                           )
! Advection along x

     CALL tssub_advdifx_uq(sal,temp             &


                                   ,cv_wat      &

                                        )

! Advection along y

     CALL tssub_advdify_uq(sal2,temp2           &


                                   ,c2          &

                                        )

! Advection along z


     CALL ts_advz_compact(xe,sal3,temp3)


     CALL sub_advz_compact(xe,c3)




   CASE(2) ! XZY

     CALL tssub_advx_macho(sal,temp,sal2,temp2   &

                                   ,cv_wat,c2    &

                                           )
     CALL tssub_advz_macho(xe,sal2,temp2,sal3,temp3 &

                                      ,c2,c3     &

                                           )

! Advection along x

     CALL tssub_advdifx_uq(sal,temp              &


                                   ,cv_wat       &

                                        )

! Advection along y

     CALL tssub_advdify_uq(sal3,temp3            &


                                   ,c3           &

                                        )

! Advection along z


     CALL ts_advz_compact(xe,sal2,temp2)


     CALL sub_advz_compact(xe,c2)




   CASE(3) ! YXZ

     CALL tssub_advy_macho(sal,temp,sal2,temp2   &

                                   ,cv_wat,c2    &

                                           )
     CALL tssub_advx_macho(sal2,temp2,sal3,temp3 &

                                      ,c2,c3     &

                                           )

! Advection along x

     CALL tssub_advdifx_uq(sal2,temp2            &


                                   ,c2           &

                                        )

! Advection along y

     CALL tssub_advdify_uq(sal,temp              &


                                   ,cv_wat       &

                                        )

! Advection along z


     CALL ts_advz_compact(xe,sal3,temp3)


     CALL sub_advz_compact(xe,c3)




   CASE(4) ! YZX

     CALL tssub_advy_macho(sal,temp,sal2,temp2   &

                                   ,cv_wat,c2    &

                                           )
     CALL tssub_advz_macho(xe,sal2,temp2,sal3,temp3 &

                                      ,c2,c3     &

                                           )

! Advection along x

     CALL tssub_advdifx_uq(sal3,temp3            &


                                   ,c3           &

                                        )

! Advection along y

     CALL tssub_advdify_uq(sal,temp              &


                                   ,cv_wat       &

                                        )

! Advection along z


     CALL ts_advz_compact(xe,sal2,temp2)


     CALL sub_advz_compact(xe,c2)



   CASE(5) ! ZXY

     CALL tssub_advz_macho(xe,sal,temp,sal2,temp2   &

                                      ,cv_wat,c2    &

                                           )
     CALL tssub_advx_macho(sal2,temp2,sal3,temp3 &

                                      ,c2,c3     &

                                           )

! Advection along x

     CALL tssub_advdifx_uq(sal2,temp2            &


                                   ,c2           &

                                        )

! Advection along y

     CALL tssub_advdify_uq(sal3,temp3            &


                                   ,c3           &

                                        )

! Advection along z


     CALL ts_advz_compact(xe,sal,temp)


     CALL sub_advz_compact(xe,cv_wat)



   CASE(0) ! ZYX

     CALL tssub_advz_macho(xe,sal,temp,sal2,temp2   &

                                      ,cv_wat,c2    &

                                           )
     CALL tssub_advy_macho(sal2,temp2,sal3,temp3 &

                                      ,c2,c3     &

                                           )

! Advection along x

     CALL tssub_advdifx_uq(sal3,temp3            &


                                   ,c3           &

                                        )

! Advection along y

     CALL tssub_advdify_uq(sal2,temp2            &


                                   ,c2           &

                                        )

! Advection along z


     CALL ts_advz_compact(xe,sal,temp)


     CALL sub_advz_compact(xe,cv_wat)



   CASE DEFAULT
     WRITE(ierrorlog,*) ' '
     WRITE(ierrorlog,*) ' routine TSSUB_ADVDIF_MACHO3D '
     WRITE(ierrorlog,*) ' you should never see this line '
     WRITE(ierrorlog,*) ' '
   END SELECT

   !PRINT*, 'exit TSSUB_ADVDIF_MACHO3D.F90 routine'

  END SUBROUTINE tssub_advdif_macho3d
