C
C************************** DETOUTQ *********************************
C 
C  OUTPUT OF THE DISCHARGE AT THE OUTLET OF THE BASIN AND IN THE OTHER
C  CELLS SUCH AS SELECTED IN PARM INPUT FILE 
C
C***********************************************************************
C
      SUBROUTINE DETOUTQ(NCELNL,NUM_QOUT,TIME,ID_QOUT,QOI_SN,
     1     Q_IN_KKP1_SN,Q_OUT_KKP1_SN_1,Q_OUT_KKP1_SN_2,TRANSP,
     1        QMASS_IN_KKP1_SN,QMASS_OUT_KKP1_SN_1,QMASS_OUT_KKP1_SN_2)

      IMPLICIT NONE   
      INCLUDE 'CATHY.H'
      LOGICAL TRANSP
      INTEGER I,NUM_QOUT,NCELNL
      INTEGER ID_QOUT(MAXQOUT),QOI_SN(MAXCEL)
      REAL*8  TIME
      REAL*8  Q_OUTPUT(MAXQOUT),QMASS_OUTPUT(MAXQOUT)
      REAL*8  Q_IN_KKP1_SN(MAXCEL)
      REAL*8  Q_OUT_KKP1_SN_1(MAXCEL)
      REAL*8  Q_OUT_KKP1_SN_2(MAXCEL)
      REAL*8  QMASS_IN_KKP1_SN(MAXCEL)
      REAL*8  QMASS_OUT_KKP1_SN_1(MAXCEL)
      REAL*8  QMASS_OUT_KKP1_SN_2(MAXCEL)

     
      INCLUDE 'IOUNITS.H'

C      WRITING STATEMENT DIFFERENT IF THE CONSIDERED CELL IS THE OUTLET OR NOT.
C      FOR THE INTERNAL CELLS OF THE BASIN WE'RE 'OBLIGED' TO CONSDER 
C      THE Q_IN_KKP1_SN IN CASE THERE IS DISPERSION IN THE FLUXES        

      DO I=1,NUM_QOUT
             Q_OUTPUT(I)=Q_IN_KKP1_SN(ID_QOUT(I))
      END DO
      IF (TRANSP) THEN
         DO I=1,NUM_QOUT
            QMASS_OUTPUT(I)=QMASS_IN_KKP1_SN(ID_QOUT(I))
         END DO
      END IF
      
      WRITE(IOUT41,1530) TIME,Q_OUT_KKP1_SN_1(QOI_SN(NCELNL)),
     1     Q_OUT_KKP1_SN_2(QOI_SN(NCELNL)),
     2     (Q_OUTPUT(I),I=1,NUM_QOUT),
     3     QMASS_OUT_KKP1_SN_1(QOI_SN(NCELNL)),
     4     QMASS_OUT_KKP1_SN_2(QOI_SN(NCELNL)),
     5     (QMASS_OUTPUT(I),I=1,NUM_QOUT)
      
      RETURN
C1530 FORMAT(1PE16.8,50(1PE16.7))
 1530 FORMAT(10(1PE16.8))
      END
