C
C
C
      SUBROUTINE  DTTR (MARKL,MARKP)
C
C     ------------------------------------------------------------------
C
C       SUBROUTINE PURPOSE:
C       REARRANGING DETAILED DATA RECORDS FOR THE AIRWAY WHOSE AIRFLOW
C     REVERSED.
C
C     ------------------------------------------------------------------
C
      INCLUDE 'CMMNW.DAT'
C
      DO 90 I=1,NB
         DO 1 J=1,NJ
            IF (JNO(J).EQ.JS(I)) JU=J
1        CONTINUE
C
C     MARKP=0: A THERMALLY BALANCED SOLUTION HAS NOT BEEN REACHED YET
C              WITHIN A TIME INTERVAL.
C     MARKP=1: THE CALCULATION OF THE PRESENT TIME INTERVAL IS DONE.
C     Q(I): AIRFLOW RATE IN AIRWAY NO(I) AT THE END OF THE PRESENT
C           INTERVAL.
C     QQ(I): TIME AVERAGED AIRFLOW RATE.
C
         IF (MARKP.EQ.0) QRS=QQ(I)
         IF (MARKP.EQ.1) QRS=Q(I)
         IF (QRS.LT.0.0) THEN
            MARKL=1
            IQ=IBTN(I)
            IF (IQ.GT.0) THEN
               IF (MARKP.EQ.1) THEN
                  IS=NSAC(I,2)
                  IF (IS.GT.0) THEN
                     DO 2 J=1,IS
                        DO 2 K=1,5
                           GBTN(J,K)=DPPA(IQ,J,K)
2                    CONTINUE
                     DO 6 J=1,IS
                        JP=IS-J+1
                        DO 4 K=1,5
                           DPPA(IQ,JP,K)=GBTN(J,K)
4                       CONTINUE
                        DPPA(IQ,JP,1)=LA(I)-DPPA(IQ,JP,1)
                        IF (J.EQ.1) THEN
                           DPPA(IQ,JP,2)=TAUXC(IQ)
                        ELSE
                           DPPA(IQ,JP,2)=TAUXA(IQ,J-1)
                        ENDIF
6                    CONTINUE
                     DO 7 J=1,IS
                        JP=IS-J+1
                        IF (J.EQ.1) THEN
                           TAUXA(IQ,JP)=TRD(I)
                        ELSE
                           TAUXA(IQ,JP)=GBTN(J-1,2)
                           IF (J.EQ.IS) TAUXC(IQ)=GBTN(J,2)
                        ENDIF
7                    CONTINUE
                     FMASS1=DPPA(IQ,1,5)
                     IF (IS.GE.2) THEN
                        DO 8 J=1,IS-1
                           DPPA(IQ,J,5)=DPPA(IQ,J+1,5)
8                       CONTINUE
                     ENDIF
                     IF (FMASS(IQ).LT.0.0) THEN
                        FKKX=535.0/(460.0+(DPPA(IQ,IS,2)+TRD(I))/2.0)
                        FMASS(IQ)=A(I)*DPPA(IQ,IS,1)*0.075*FKKX
                     ENDIF
                     DPPA(IQ,IS,5)=FMASS(IQ)
                     FMASS(IQ)=FMASS1
                     RDPROP(I)=DPPA(IQ,1,3)
                     RDCH4(I)=DPPA(IQ,1,4)
                     RDOP(I)=DPPA(IQ,1,3)
                     RCH4(I)=DPPA(IQ,1,4)
                  ELSE
                     IF (NWTYP(I).NE.10) THEN
                        RDPROP(I)=PROP(JU)
                        RDCH4(I)=PRCH4(JU)
                        RDH2O(I)=PRH2O(JU)
                        RDOP(I)=PROP(JU)
                        RCH4(I)=PRCH4(JU)
                        RH2O(I)=PRH2O(JU)
                     ENDIF
                  ENDIF
               ELSE
                  IS=NSACB(I,2)
                  IF (IS.GT.0) THEN
                     DO 10 J=1,IS
                        DO 10 K=1,5
                           GBTN(J,K)=DPPB(IQ,J,K)
10                   CONTINUE
                     DO 30 J=1,IS
                        JP=IS-J+1
                        DO 20 K=1,5
                           DPPB(IQ,JP,K)=GBTN(J,K)
20                      CONTINUE
                        DPPB(IQ,JP,1)=LA(I)-DPPB(IQ,JP,1)
                        IF (J.EQ.1) THEN
                           DPPB(IQ,JP,2)=TAUXD(IQ)
                        ELSE
                           DPPB(IQ,JP,2)=TAUXB(IQ,J-1)
                        ENDIF
30                   CONTINUE
                     DO 32 J=1,IS
                        JP=IS-J+1
                        IF (J.EQ.1) THEN
                           TAUXB(IQ,JP)=TTRD(I)
                        ELSE
                           TAUXB(IQ,JP)=GBTN(J-1,2)
                           IF (J.EQ.IS) TAUXD(IQ)=GBTN(J,2)
                        ENDIF
32                   CONTINUE
                     FMASS1=DPPB(IQ,1,5)
                     IF (IS.GE.2) THEN
                        DO 35 J=1,IS-1
                           DPPB(IQ,J,5)=DPPB(IQ,J+1,5)
35                      CONTINUE
                     ENDIF
                     IF (FMASS(IQ).LT.0.0) THEN
                        FKKX=535.0/(460.0+(DPPB(IQ,IS,2)+TTRD(I))/2.0)
                        FMASS(IQ)=A(I)*DPPB(IQ,IS,1)*0.075*FKKX
                     ENDIF
                     DPPB(IQ,IS,5)=FMASS(IQ)
                     FMASS(IQ)=FMASS1
                     DO 40 J=1,IS
                        DPPA(IQ,J,5)=DPPB(IQ,J,5)
40                   CONTINUE
                     RDPROP(I)=DPPB(IQ,1,3)
                     RDCH4(I)=DPPB(IQ,1,4)
                     RDOP(I)=DPPB(IQ,1,3)
                     RCH4(I)=DPPB(IQ,1,4)
                  ELSE
                     IF (NWTYP(I).NE.10) THEN
                        RDPROP(I)=PROPS(JU)
                        RDCH4(I)=PRCH4S(JU)
                        RDH2O(I)=PRH2OS(JU)
                        RDOP(I)=PROPS(JU)
                        RCH4(I)=PRCH4S(JU)
                        RH2O(I)=PRH2OS(JU)
                     ENDIF
                  ENDIF
               ENDIF
               IF (NHM.GT.0) THEN
                  JLE=NSP(I)
                  DO 50 J=1,JLE
                     GBTN(J,1)=TDA(IQ,J)
                     GBTN(J,2)=STV(IQ,J)
                     GBTN(J,3)=WMTR(IQ,J)
                     GBTN(J,4)=WTCORR(IQ,J)
                     GBTN(J,5)=HABS(IQ,J)
50                CONTINUE
                  DO 60 J=1,JLE
                     JP=JLE-J+1
                     TDA(IQ,J)=GBTN(JP,1)
                     STV(IQ,J)=GBTN(JP,2)
                     WMTR(IQ,J)=GBTN(JP,3)
                     WTCORR(IQ,J)=GBTN(JP,4)
                     HABS(IQ,J)=GBTN(JP,5)
60                CONTINUE
               ENDIF
            ENDIF
            TRDA=TRD(I)
            TTRDA=TTRD(I)
            TRD(I)=TJS(I)
            TTRD(I)=TTJS(I)
            TJS(I)=TRDA
            TTJS(I)=TTRDA
            JE=JS(I)
            JS(I)=JF(I)
            JF(I)=JE
            DZRD(I)=-DZRD(I)
            FRNVP(I)=-FRNVP(I)
            QTP(I)=-QTP(I)
            QQ(I)=-QQ(I)
            Q(I)=-Q(I)
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
            NUMCT=0
            DO 65 J=1,INFLOW
               IF (NO(I).EQ.NCENT(J)) THEN
                  DO 64 JJ=1,NB
                     IF (JF(JJ).EQ.JNO(JU)) THEN
                        NUMCT=NUMCT+1
                        SFUME=SFUME+RDPROP(JJ)
                     ENDIF
64                CONTINUE
                  PROP(JU)=SFUME/NUMCT
               ENDIF
65          CONTINUE
C            DO 70 J=1,JAJ
C               IF (JS(I).EQ.JSTAR(J)) THEN
C                  PROP(JU)=0.
C               ENDIF
C70          CONTINUE
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C     NREV(I): NUMBER OF CHANGES IN AIRFLOW DIRECTION (QQ) IN AIRWAY
C              NO(I) IN THE PRESENT TIME INTERVAL.
C     NNREV(I): NUMBER OF CHANGES IN AIRFLOW DIRECTION (Q) IN AIRWAY
C               NO(I) SINCE TIME ZERO.
C
            NREV(I)=NREV(I)+1
         ENDIF
         IF (MARKP.EQ.1.AND.Q(I)*QTP(I).LT.0.0) NNREV(I)=NNREV(I)+1
         IF (ABS(QQ(I)).LT.1.E-5) QQ(I)=1.E-5
         IF (ABS(Q(I)).LT.1.E-5) Q(I)=1.E-5
90    CONTINUE
C
C       WHEN THERE IS AIRFLOW REVERSAL HAPPENED (MARKL>0), NETWORK
C     SCHEME ARRAYS WILL BE RE-STRUCTURED.
C
      IF (MARKL.NE.0) THEN
         L=0
         M=0
         N=1
         DO 120 I=1,MAXJ
            K=L
            DO 100 J=1,NB
               IF (JS(J).EQ.I) THEN
                  L=L+1
                  NGOUT(L)=J
               ENDIF
100         CONTINUE
            LOUT(N)=L
            MM=M
            DO 110 J=1,NB
               IF (JF(J).EQ.I) THEN
                  M=M+1
                  NGIN(M)=J
               ENDIF
110         CONTINUE
            MIN(N)=M
            IF (MM.NE.M.OR.K.NE.L) THEN
               JNOL(N)=I
               N=N+1
            ENDIF
120      CONTINUE
         DO 140 I=1,NJ
            DO 130 J=1,NJ
               IF (JNOL(I).EQ.JNO(J)) THEN
                  JLR(I)=J
                  GO TO 140
               ENDIF
130         CONTINUE
140      CONTINUE
      ENDIF
C
C
      RETURN
      END

