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

  SUBROUTINE outflow

#ifdef key_substance

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE outflow  ***
   !&E
   !&E ** Purpose : calculation of substance fluxes for each outflow
   !&E
   !&E ** Description :
   !&E
   !&E ** Called by : main.F90 step2dxy.F90 step2dyx.F90
   !&E                step3dxy.F90 step3dyx.F90 Agrif_User.F90
   !&E
   !&E ** Channel : 29, 39
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays :
   !&E
   !&E ** Modified variables : phicon, f90, valrej, xrej, yrej, krej, nucon, l_rejflux
   !&E
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !                         Original code
   !&E       !  2008-10  (M. Jouan) no numriv initialization for flux discharge
   !&E       !  2009-04  (A. Mano) MPI2D
   !&E       !  2009-09  (B. Thouvenin) Global initialization of phicon
   !&E       !  2009-11  (S. Michel, HOCER) check consistency between river and outflow positions
   !&E       !  2010-10  (B. Thouvenin) Distribution of the river discharge throughout a thickness
   !&E       !  2010-10  (B. Thouvenin) Distribution of the river discharge throughout a thickness
   !&E       !  2013-09 (B.Thouvenin) integration of module tracer_N with key_biolo (A. Menesguen)
   !&E       !  2014-04 (B.Thouvenin) integration of module tracer_P with key_biolo (A. Menesguen) 
   !&E
   !&E---------------------------------------------------------------------

#include "toolcpp.h"

   !! * Modules used
   USE parameters,   ONLY : rsh,rlg,lchain,valmanq,limin,limax,ljmin,ljmax, &
                            l_testcase,kmax
   USE comvars2d,    ONLY : nbouc,nvalmax,h0_g,tchrono,t,dt,indchrono, &
                            iscreenlog,ierrorlog,iwarnlog
   USE comfluxdo,    ONLY : file_river,nbfl,phiriver,nomriviere,iriv,jriv,kriv
   USE comsubstance, ONLY : f90,t90
   USE comsubstance, ONLY : file_outflow,nbrej,xrej,yrej,krej,l_rejflux,valrej, &
                            nv_adv,nv_state,irk_fil,name_var,numriv,nucon, &
                            phicon,unit_modif_mudbio_N2dw
   USE comsubstance, ONLY : phicon_drycell
   USE comvars2d,    ONLY : h0,h0fond
   USE comvarp2d,    ONLY : ssh  
#if defined key_sedim_MUSTANG && defined key_sand2D
   USE comsubstance,ONLY : isand1,isand2,l_subs2D
#endif
#if defined key_biolo && defined key_N_tracer
   USE parameters, ONLY : nb_var_tracerN 
   USE bioloinit,    ONLY : nb_source_river_tracerN,nb_source_tracerN,p_marque_tracerN,iv_tracer_N,iv_signed_N,name_source_tracerN      
#endif
#if defined key_biolo && defined key_P_tracer
   USE parameters, ONLY : nb_var_tracerP 
   USE bioloinit,    ONLY : nb_source_river_tracerP,nb_source_tracerP,p_marque_tracerP,iv_tracer_P,iv_signed_P,name_source_tracerP      
#endif

   USE_MPI toolmpi,  ONLY : MASTER

   IMPLICIT NONE

   !! * Arguments

   !! * Local declarations
   LOGICAL                             :: flag,ex,l_posit,lrejflux
   CHARACTER(LEN=lchain), DIMENSION(:), ALLOCATABLE ::                 &
                                          nom,nomriv,nomcorres
   CHARACTER(LEN=lchain)               :: rec,lnomriv,lnomrej
   CHARACTER(LEN=5)                    :: comment
   CHARACTER(LEN=19)                   :: tool_sectodat,date
   INTEGER                             :: i,j,k,kf,kk,ii,jj,nu,eof,ir, &
                                          nbrejfile,lirej,ljrej,lkrej, &
                                          lnumriv,IERR_MPI
   INTEGER,DIMENSION(:), ALLOCATABLE   :: irej,jrej
   REAL(KIND=rsh)                      :: val1,val2,tool_latlon2i,tool_latlon2j,conatt
   REAL(KIND=rlg)                      :: dt1,tint1,tint2,tool_datosec,g,phi,t1
   REAL(KIND=rlg)                      :: lxrej,lyrej
#if defined key_N_tracer || defined key_P_tracer
   INTEGER                             :: is,ivtra,nbrej_tracer,iiv,ip,ichaindeb,ichainfin,nbrej_sanstracer,kkr
   INTEGER,DIMENSION(:),ALLOCATABLE     :: nrivsource
   REAL(KIND=rsh)                      :: somm_pmarque
   CHARACTER(LEN=lchain)     :: namerivsource
   CHARACTER(LEN=lchain),DIMENSION(:,:),ALLOCATABLE     :: namesourceriv
#endif
   !!----------------------------------------------------------------------
   !! * Executable part

   ! ****  Initialization  ****
   !---------------------------

   IF (nbouc == 0 .AND. .NOT.l_testcase) THEN

   ! save into simu.log
   !-------------------

     IF_MPI (MASTER) THEN
     WRITE(iscreenlog,*) ' '
     WRITE(iscreenlog,*) ' '
     WRITE(iscreenlog,*) ' '
     WRITE(iscreenlog,*) '**************************************************'
     WRITE(iscreenlog,*) '**************** OUTFLOW.F90 *********************'
     WRITE(iscreenlog,*) '**************************************************'
     WRITE(iscreenlog,*) ' '
     WRITE(iscreenlog,*) 'DISCHARGE FILE :',TRIM(file_outflow)
     ENDIF_MPI

     ! check the number of outflows
     ! ----------------------------

     nbrejfile=0
     nbrej=0
     eof=0

     INQUIRE(file=file_outflow,exist=ex)
     IF (ex) THEN

       OPEN(39,file = file_outflow,form='formatted')
       comment='debut'
       DO WHILE (comment /= '*****')
         READ(39,'(a)',iostat=eof) comment
       END DO
       DO WHILE(eof==0)
         READ(39,'(a)',iostat=eof)
         READ(39,*,iostat=eof)
         READ(39,*,iostat=eof)
         READ(39,*,iostat=eof)
         READ(39,'(a)',iostat=eof)
         comment='debut'
         READ(39,'(a)',iostat=eof) comment
         DO WHILE (comment /= '*****' .AND. eof==0)
           READ(39,'(a)',iostat=eof)
           READ(39,'(a)',iostat=eof)
           nbrej=nbrej+1
           READ(39,'(a)',iostat=eof) comment
         END DO
         nbrejfile=nbrejfile+1
       END DO
       CLOSE(39)
       nbrejfile=nbrejfile-1

   ! save into simu.log
   !-------------------

       IF(nbrej<nbfl) THEN
         IF_MPI (MASTER) THEN
         WRITE(iscreenlog,*)'Number of discharges, ',nbrej
         WRITE(iscreenlog,*)'lower than number of rivers',nbfl
         ENDIF_MPI
       ELSE IF(nbrej>nbfl) THEN
         IF_MPI (MASTER) THEN
         WRITE(iscreenlog,*)'Number of discharges, ',nbrej
         WRITE(iscreenlog,*)'larger than number of rivers',nbfl
         ENDIF_MPI
       ELSE IF(nbrej==nbfl) THEN
         IF_MPI (MASTER) &
         WRITE(iscreenlog,*)'Number of discharges = number of rivers = ',nbrej
       END IF
       IF_MPI (MASTER) THEN
       WRITE(iscreenlog,*) ' '
       WRITE(iscreenlog,*) 'Number of discharges = ',nbrej
       ENDIF_MPI

     ! dynamic allocation
     ! ------------------
#if defined key_N_tracer || defined key_P_tracer
       nbrej_tracer=0
#if defined key_N_tracer 
       nbrej_tracer= nbrej
       ALLOCATE(nrivsource(nb_source_river_tracerN))
       ALLOCATE(namesourceriv(nb_source_river_tracerN,5))
       DO is=1,nb_source_river_tracerN
         namerivsource=TRIM(name_source_tracerN(is))//';'
         ! recherche du nombre de riviere dans cette source
         ichaindeb=1
         ichainfin=LEN_TRIM(namerivsource)
         ip=0
         somm_pmarque=0.0_rsh
         DO ivtra=1,nb_var_tracerN
           somm_pmarque=somm_pmarque+p_marque_tracerN(ivtra,is)
         ENDDO
         DO
           ii=INDEX(namerivsource(ichaindeb:ichainfin),';')+ichaindeb-1
           ip=ip+1
           namesourceriv(is,ip)=namerivsource(ichaindeb:ii-1)
           ichaindeb=ii+1
           if(ichaindeb >= ichainfin) exit
         ENDDO
         nrivsource(is)=ip
         nbrej_tracer= nbrej_tracer +  somm_pmarque*nrivsource(is)
       ENDDO 
       if(limin==0 .and.ljmin==0)write(*,*) 'nbrej_tracer=', nbrej_tracer,&
               ((TRIM(ADJUSTL(ADJUSTR(namesourceriv(is,ip)))),ip=1,nrivsource(is)),is=1,nb_source_river_tracerN)
#if ! defined key_P_tracer
       ALLOCATE(nomcorres(nbrej_tracer),nomriv(nbrej_tracer),nom(nbrej_tracer))
       ALLOCATE(xrej(nbrej_tracer),yrej(nbrej_tracer),irej(nbrej_tracer),jrej(nbrej_tracer),krej(nbrej_tracer))
       ALLOCATE(nucon(nbrej_tracer),numriv(nbrej_tracer))
       ALLOCATE(l_rejflux(nbrej_tracer),valrej(nvalmax,nbrej_tracer))
#endif
#endif
#if defined  key_P_tracer 
       nbrej_tracer= nbrej+nbrej_tracer
       ALLOCATE(nrivsource(nb_source_river_tracerP))
       ALLOCATE(namesourceriv(nb_source_river_tracerP,5))
       DO is=1,nb_source_river_tracerP
         namerivsource=TRIM(name_source_tracerP(is))//';'
         ! recherche du nombre de riviere dans cette source
         ichaindeb=1
         ichainfin=LEN_TRIM(namerivsource)
         ip=0
         somm_pmarque=0.0_rsh
         DO ivtra=1,nb_var_tracerP
           somm_pmarque=somm_pmarque+p_marque_tracerP(ivtra,is)
         ENDDO
         DO
           ii=INDEX(namerivsource(ichaindeb:ichainfin),';')+ichaindeb-1
           ip=ip+1
           namesourceriv(is,ip)=namerivsource(ichaindeb:ii-1)
           ichaindeb=ii+1
           if(ichaindeb >= ichainfin) exit
         ENDDO
         nrivsource(is)=ip
         nbrej_tracer= nbrej_tracer +  somm_pmarque*nrivsource(is)
       ENDDO 
       if(limin==0 .and.ljmin==0)write(*,*) 'nbrej_tracer=', nbrej_tracer,&
               ((TRIM(ADJUSTL(ADJUSTR(namesourceriv(is,ip)))),ip=1,nrivsource(is)),is=1,nb_source_river_tracerP)
       ALLOCATE(nomcorres(nbrej_tracer),nomriv(nbrej_tracer),nom(nbrej_tracer))
       ALLOCATE(xrej(nbrej_tracer),yrej(nbrej_tracer),irej(nbrej_tracer),jrej(nbrej_tracer),krej(nbrej_tracer))
       ALLOCATE(nucon(nbrej_tracer),numriv(nbrej_tracer))
       ALLOCATE(l_rejflux(nbrej_tracer),valrej(nvalmax,nbrej_tracer))
#endif
#else
       ALLOCATE(nomcorres(nbrej),nomriv(nbrej),nom(nbrej))
       ALLOCATE(xrej(nbrej),yrej(nbrej),irej(nbrej),jrej(nbrej),krej(nbrej))
       ALLOCATE(nucon(nbrej),numriv(nbrej))
       ALLOCATE(l_rejflux(nbrej),valrej(nvalmax,nbrej))
#endif

     ! read data file for initialization
     ! ---------------------------------

       OPEN(39,file = file_outflow,form='formatted')
       comment='debut'
       DO WHILE (comment /= '*****')
         READ(39,'(a)',iostat=eof) comment
       END DO

       k = 0
       DO kf = 1,nbrejfile  ! loop on different locations of discharges

   ! outflow name
         READ(39,*) lnomrej

   ! location of river discharges
         READ(39,*) l_posit
         IF (.NOT.l_posit) THEN
           READ(39,*) g,phi,lkrej
           lxrej=tool_latlon2i(g,phi)
           lyrej=tool_latlon2j(g,phi)
           lirej=NINT(lxrej)
           ljrej=NINT(lyrej)
         ELSE
           READ(39,*) lirej,ljrej,lkrej
           lxrej=lirej
           lyrej=ljrej
         END IF
         ir=0
         DO WHILE(h0_g(lirej,ljrej)==-valmanq)  ! check the closest wet cell
           ir=ir+1
           IF_MPI(MASTER) THEN
             IF (ir==1) WRITE(iscreenlog,*) 'The outflow ',TRIM(lnomrej), ' is shifted '
           ENDIF_MPI
           DO j=ljrej-ir,ljrej+ir
             DO i=lirej-ir,lirej+ir
               IF (h0_g(i,j) /=-valmanq) THEN
                 lirej=i
                 ljrej=j
               END IF
             END DO
           END DO
         END DO
         IF (ir/=0) THEN
           lxrej=lirej
           lyrej=ljrej
           IF_MPI(MASTER) THEN
           WRITE(iscreenlog,*) 'into the cell (i,j) : ',NINT(lxrej),NINT(lyrej)
           ENDIF_MPI
         END IF

   ! unit ? flux (.true.) or concentration
         READ(39,*) lrejflux

   ! river name
         READ(39,*) lnomriv

         IF (.NOT.lrejflux) THEN
           flag=.FALSE.
           DO kk=1,nbfl
             IF (TRIM(ADJUSTL(ADJUSTR(lnomriv)))== &
               TRIM(ADJUSTL(ADJUSTR(nomriviere(kk)))) ) THEN
               lnumriv=kk
               flag=.TRUE.
             END IF
           END DO
           IF (.NOT.flag) THEN
             WRITE(ierrorlog,*) ' '
             WRITE(ierrorlog,*) 'ERROR: The river name relative to the outflow ',k
             WRITE(ierrorlog,*) 'has not been found.'
             WRITE(ierrorlog,*) 'Make sure the river "',TRIM(lnomriv),'"'
             WRITE(ierrorlog,*) 'does exist in the river list "river.dat".'
           END IF
           IF ((lirej/=iriv(lnumriv)).OR.(ljrej/=jriv(lnumriv)).OR.(lkrej/=kriv(lnumriv))) THEN
             WRITE(ierrorlog,*) ' '
             WRITE(ierrorlog,*) 'ERROR: The river position relative to the outflow ',k
             WRITE(ierrorlog,*) 'is not located at the same i,j,k indices as the outflow :'
             WRITE(ierrorlog,*) '(i,j,k)river   = ',iriv(lnumriv),jriv(lnumriv),kriv(lnumriv)
             WRITE(ierrorlog,*) '(i,j,k)outflow = ',lirej,ljrej,lkrej
             WRITE(ierrorlog,*) 'Make sure the river "',TRIM(lnomriv),'"'
             WRITE(ierrorlog,*) 'has the same position in the river list "river.dat"'
             WRITE(ierrorlog,*) 'and in the outflow list "outflow.dat".'
             flag=.FALSE.
           ENDIF
           IF (.NOT.flag) THEN
             WRITE(ierrorlog,*) 'Simulation stopped'
             STOP
           ENDIF
         END IF

         comment='debut'
         READ(39,'(a)',iostat=eof) comment
         DO WHILE (comment /= '*****' .AND. eof==0)
           k=k+1

   ! substance name
           READ(39,*) nomcorres(k)
           flag=.FALSE.
           DO kk=1,nv_adv
             IF (TRIM(ADJUSTL(ADJUSTR(nomcorres(k))))== &
               TRIM(ADJUSTL(ADJUSTR(name_var(irk_fil(kk))))) ) THEN
               nucon(k)=kk
               flag=.TRUE.
             END IF
           END DO
           IF (.NOT.flag) THEN
             WRITE(ierrorlog,*) ' '
             WRITE(ierrorlog,*) 'ERROR: A substance name relative to the outflow ',k
             WRITE(ierrorlog,*) 'has not been found.'
             WRITE(ierrorlog,*) 'Make sure the substance "',TRIM(nomcorres(k)),'"'
             WRITE(ierrorlog,*) 'does exist in the substance list "variable.dat".'
             WRITE(ierrorlog,*) 'Simulation stopped'
             STOP
           END IF

   ! discharges file
           READ(39,'(a)') nom(k)

   ! river name
           nomriv(k)=lnomriv

   ! location of river discharges
           xrej(k)=lxrej
           yrej(k)=lyrej
           irej(k)=lirej
           jrej(k)=ljrej
           krej(k)=lkrej

   ! unit ? flux (.true.) or concentration
           l_rejflux(k)=lrejflux
           IF (.NOT.lrejflux) numriv(k)=lnumriv

           READ(39,'(a)',iostat=eof) comment
         END DO ! ends test on advected variables

       END DO ! ends loop on different locations of discharges

       CLOSE(39)

   ! save into simu.log and initialize the discharge amount
   ! ------------------------------------------------------

       DO k = 1,nbrej
         IF_MPI (MASTER) THEN
         WRITE(iscreenlog,*) ' '
         WRITE(iscreenlog,*) ' '
         WRITE(iscreenlog,*) 'RIVER NAME        : ', TRIM(nomriv(k))
         WRITE(iscreenlog,*) 'LOCATION i,j,k    : ',irej(k),jrej(k),krej(k)

         WRITE(iscreenlog,*) ' '
         WRITE(iscreenlog,*) 'Substance name    : ',TRIM(nomcorres(k))
         WRITE(iscreenlog,*) 'Data file         : ',TRIM(nom(k))
         ENDIF_MPI

   ! time interpolation of discharges
   ! --------------------------------

         OPEN(29,file = nom(k),form='formatted')
         i=1
         READ(29,*)
         READ(29,*)
         READ(29,*)
         READ(29,*)
         READ(29,'(a)',err=999,END=999) rec
         kk=INDEX(rec,'/')-2
         READ(rec(kk+19:),*) val1
         tint1 = tool_datosec(rec(kk:kk+18))

   ! check the first available date in river discharge file
   ! ------------------------------------------------------

         t1=tchrono(i)
         DO WHILE(t1 < tint1.AND.i<=nvalmax)
           valrej(i,k)=val1
           i=i+1
           IF (i<=nvalmax) t1=tchrono(i)
         END DO

         ! If the simulation starts before the first data record,
         ! the river discharge is set to the first available data.
         IF (i > 1) THEN
           WRITE(iwarnlog,*) ' '
           WRITE(iwarnlog,*) 'WARNING: The discharge data of outflow number ',k
           WRITE(iwarnlog,*) 'is not available from the beginning of the simulation.'
           WRITE(iwarnlog,*) 'The first available value from the file : ',TRIM(nom(k))
           WRITE(iwarnlog,*) 'corresponds to the date : ',rec(kk:kk+18)
           WRITE(iwarnlog,*) 'It is equal to : ',val1,' u.s.i.'
           WRITE(iwarnlog,*) 'and will be used at the beginning of the simulation.'
         END IF

         ! time interpolation of discharge data
         IF (i < nvalmax) THEN
           eof=0
           DO WHILE(eof==0)
             READ(29,'(a)',iostat=eof) rec
             IF (eof==0) THEN
               kk=INDEX(rec,'/')-2
               READ(rec(kk+19:),*) val2
               tint2 = tool_datosec(rec(kk:kk+18))
               DO WHILE((tchrono(i)-tint1)*(tchrono(i)-tint2)<=0.0_rlg) 
                 dt1=(tchrono(i)-tint1)/(tint2-tint1)
                 valrej(i,k)=val1*(1.0_rlg-dt1)+val2*dt1
                 IF (valrej(i,k) < 0._rsh ) THEN
                   WRITE(ierrorlog,*) 'Negative concentrations in OUTFLOW',TRIM(nomcorres(k)),TRIM(nomriv(k)),TRIM(nom(k))
                   WRITE(ierrorlog,*) 'The run is stopped'
                   CALL_MPI MPI_FINALIZE(IERR_MPI)
                   STOP
                 END IF
                 i=i+1
                 IF (i > nvalmax) GOTO 999
               END DO
               tint1=tint2
               val1=val2
             END IF
           END DO
         END IF

  999 CONTINUE

         ! If the simulation ends after the last data record,
         ! set the river discharge to the last available data.
         IF (i < nvalmax) THEN
           WRITE(iwarnlog,*) ' '
           WRITE(iwarnlog,*) 'WARNING: The discharge data of outflow number ',k
           WRITE(iwarnlog,*) 'is not available to the end of the simulation.'
           WRITE(iwarnlog,*) 'The last available value from the file : ',TRIM(nom(k))
           WRITE(iwarnlog,*) 'corresponds to the date : ',rec(kk:kk+18)
           WRITE(iwarnlog,*) 'It is equal to :',val2,' u.s.i.'
           WRITE(iwarnlog,*) 'and will be used until the end of the simulation.'
           valrej(i:nvalmax,k)=val2
         END IF

         CLOSE(29)
       END DO

#if defined key_N_tracer || defined key_P_tracer
     ! definition of tracer sources points
     nbrej_sanstracer=nbrej
#if defined key_N_tracer
     DO is=1,nb_source_river_tracerN
#else
     DO is=1,nb_source_river_tracerP
#endif
       DO ip=1,nrivsource(is)
        DO kkr = 1,nbfl
           IF (TRIM(nomriviere(kkr)) == TRIM(namesourceriv(is,ip)) ) THEN
             DO k=1,nbrej_sanstracer
               IF(TRIM(nomriviere(kkr)) == TRIM(nomriv(k)) ) THEN
#if defined key_N_tracer
                 DO ivtra=1,nb_var_tracerN
                   IF(p_marque_tracerN(ivtra,is) .NE. 0.0_rsh .AND. iv_signed_N(ivtra,is)== nucon(k)) THEN
                      nbrej=nbrej+1
                      nucon(nbrej)=iv_tracer_N(ivtra,is)
                      xrej(nbrej)=iriv(kkr)
                      yrej(nbrej)=jriv(kkr)
                      krej(nbrej)=kriv(kkr)
                      valrej(:,nbrej)=p_marque_tracerN(ivtra,is)*valrej(:,k)
                      l_rejflux(nbrej)=l_rejflux(k)
                      numriv(nbrej)=kkr
                   ENDIF
                 ENDDO
#endif
#if defined key_P_tracer
                 DO ivtra=1,nb_var_tracerP
                   IF(p_marque_tracerP(ivtra,is) .NE. 0.0_rsh .AND. iv_signed_P(ivtra,is)== nucon(k)) THEN
                      nbrej=nbrej+1
                      nucon(nbrej)=iv_tracer_P(ivtra,is)
                      xrej(nbrej)=iriv(kkr)
                      yrej(nbrej)=jriv(kkr)
                      krej(nbrej)=kriv(kkr)
                      valrej(:,nbrej)=p_marque_tracerP(ivtra,is)*valrej(:,k)
                      l_rejflux(nbrej)=l_rejflux(k)
                      numriv(nbrej)=kkr
                   ENDIF
                 ENDDO
#endif
               ENDIF
             ENDDO
           ENDIF
        ENDDO    
      ENDDO    
    ENDDO    
#endif

      DEALLOCATE(nomcorres,nomriv,nom,irej,jrej)
     END IF   ! ends test on file availability

   END IF   ! ends test on nbouc

   ! **** End of initialization part ****
   ! ------------------------------------



   ! ****  Current progress  ****
   ! ----------------------------

   f90(1:nv_state) = 1.0_rsh
   DO k = 1,nv_state
     IF (t90(k) /= 0.0_rsh) f90(k) = EXP(-0.5_rsh*REAL(dt,rsh)*2.3026_rsh/(t90(k)*3600.0_rsh))
   END DO



   ! prescribe each discharges of substance
   ! (phicon has been assigned to zero or
   ! phicon_drycell in initflux.F90)
   ! --------------------------------------

   DO k = 1,nbrej
     ii=NINT(xrej(k))
     jj=NINT(yrej(k))
     IF_MPI (jj >= ljmin .AND. jj <= ljmax .AND.  ii >= limin .AND. ii <= limax) THEN
       kk=krej(k)
       nu=nucon(k)
       dt1=(t-tchrono(indchrono))/(tchrono(indchrono+1)-tchrono(indchrono))
       IF (l_rejflux(k)) THEN
         phicon(nu,kk,ii,jj)=phicon(nu,kk,ii,jj)+&
              0.5_rlg*dt*(dt1*valrej(indchrono+1,k)+(1.0_rlg-dt1)*valrej(indchrono,k))
       ELSE
       ! concentration at time t
         conatt=(dt1*valrej(indchrono+1,k)+(1.0_rlg-dt1)*valrej(indchrono,k))
         IF (conatt < 0._rsh ) THEN
           WRITE(ierrorlog,*) 'Concentrations negatives dans outflow',TRIM(nomcorres(k)),TRIM(nomriv(k)),TRIM(nom(k))
           CALL_MPI MPI_FINALIZE(IERR_MPI)
           STOP
         END IF
#if defined key_sedim_MUSTANG && defined key_sand2D
       ! for sand, the outflow is into the bottom cell (average concentration on depth )
             IF(l_subs2D(nu)) THEN     
               phicon(nu,2:kmax,ii,jj)=0.0_rsh
               phicon(nu,1,ii,jj)=phicon(nu,1,ii,jj)+0.5_rlg*dt*conatt*SUM(phiriver(numriv(k),:))
             ELSE
               ! the outflow and the river discharge is distributed throughout the river ! thickness
               phicon(nu,:,ii,jj)=phicon(nu,:,ii,jj)+0.5_rlg*dt*conatt*phiriver(numriv(k),:)
             ENDIF
#else
       ! the outflow and the river discharge is distributed throughout the river ! thickness
         phicon(nu,:,ii,jj)=phicon(nu,:,ii,jj)+0.5_rlg*dt*conatt*phiriver(numriv(k),:)
#endif
       ! conversion for detrital biological matter considered as constitutive particulate variable
         phicon(nu,:,ii,jj)=phicon(nu,:,ii,jj)*unit_modif_mudbio_N2dw(irk_fil(nu))
       END IF
       ! test cell without water
       IF ((h0(ii,jj)+ssh(ii,jj)) <= h0fond ) THEN
              phicon_drycell(nu,:,ii,jj)=phicon(nu,:,ii,jj)+phicon_drycell(nu,:,ii,jj)
              phicon(nu,:,ii,jj)=0.0_rsh
       ENDIF
     ENDIF_MPI
   END DO

   PRINT_DBG*, 'exit OUTFLOW routine'

#else
   USE comvars2d, ONLY : ierrorlog
   WRITE(ierrorlog,*) ' '
   WRITE(ierrorlog,*) 'ERROR: OUTFLOW.F90 is called without the CPP key "key_substance".'
   WRITE(ierrorlog,*) 'Simulation stopped'
   STOP
   RETURN
#endif

  END SUBROUTINE outflow
