C
C
      SUBROUTINE  FWCT (NSFLOW,JRE)
C
C     -----------------------------------------------------------------
C
C       SUBROUTINE PURPOSES:
C       1) DATA REARRANGEMENT FOR AIRWAYS IN WHICH AIRFLOW REVERSAL
C          HAPPENED.
C       2) FORMATION OF AIRFLOW SCHEME IN THE NETWORK.
C       3) FIND AIRFLOW RECIRCULATION PATHS.
C
C     -----------------------------------------------------------------
C
      INCLUDE 'CMMNW.DAT'
C
      DIMENSION LREC(NMX),IINU(NMX)
      DATA      LREC,IINU / NMX*0, NMX*0 /
C
      IF (JRE.GE.1) GO TO 85
      DO 20 I=1,NB
         IF (Q(I).LT.0.0) THEN
            NSFLOW=0
            J=JS(I)
            JS(I)=JF(I)
            JF(I)=J
            Q(I)=-Q(I)
            DZRD(I)=-DZRD(I)
C
C     NNREV(K): NUMBER OF AIRFLOW DIRECTION CHANGES IN AIRWAY NO(K).
C
            IF (NWTYP(I).EQ.1) R(I)=-R(I)
            NNREV(I)=NNREV(I)+1
         ENDIF
20    CONTINUE
C
C
      L=0
      M=0
      N=1
      DO 60 I=1,MAXJ
         K=L
         DO 40 J=1,NB
            IF (JS(J).EQ.I) THEN
               L=L+1
               NGOUT(L)=J
            ENDIF
40       CONTINUE
         LOUT(N)=L
         MM=M
         DO 50 J=1,NB
            IF (JF(J).EQ.I) THEN
               M=M+1
               NGIN(M)=J
            ENDIF
50       CONTINUE
         MIN(N)=M
         IF ((MM.NE.M).OR.(K.NE.L)) THEN
            JNOL(N)=I
            N=N+1
         ENDIF
60    CONTINUE
      DO 80 I=1,NJ
C
C     JNOL(J): LIST OF JUNCTION NUMBERS IN INCREASING ORDER.
C     JLR(J): BRIDGE ARRAY RELATING JNO AND JNOL LISTS.
C
         DO 70 J=1,NJ
            IF (JNOL(I).EQ.JNO(J)) THEN
               JLR(I)=J
               GO TO 80
            ENDIF
70       CONTINUE
80    CONTINUE
C
C
85    IF (JRE.EQ.1) THEN
         DO 90 I=1,NMX
            LREC(I)=0
            KJF(I)=0
            KJS(I)=0
            KNO(I)=0
90       CONTINUE
         KREC=0
         DO 135 II0=1,NJ
            IF (JNOL(II0).EQ.JSTART.OR.(JNOL(II0).GE.900.AND.
     .      JNOL(II0).LE.950)) THEN
               JSTOUT=II0
               JSTA=JNOL(II0)
               N1=JSTOUT
               DO 95 I=1,NJ
                  IF (JNO(I).EQ.JSTA) THEN
                     JNO(I)=-JNO(I)
                     GO TO 100
                  ENDIF
95             CONTINUE
               GO TO 135
100            IF (N1.LE.1) THEN
                  N2=1
               ELSE
                  N2=LOUT(N1-1)+1
               ENDIF
               N3=LOUT(N1)
               DO 110 I1=N2,N3
                  I2=NGOUT(I1)
                  LREC(I2)=100
110            CONTINUE
115            JREC=0
               DO 130 I1=1,NJ
                  IF (JNO(I1).GT.0) THEN
                     DO 117 I9=1,NJ
                        IF (JNOL(I9).EQ.JNO(I1)) GO TO 118
117                  CONTINUE
                     GO TO 135
118                  IF (I9.LE.1) THEN
                        L1=1
                     ELSE
                        L1=MIN(I9-1)+1
                     ENDIF
                     L2=MIN(I9)
                     KREADY=0
                     DO 120 I3=L1,L2
                        I4=NGIN(I3)
                        IF (LREC(I4).LE.0) THEN
                           KREADY=1
                           JREC=I9
                        ENDIF
120                  CONTINUE
                     IF (KREADY.EQ.0) THEN
                        JNO(I1)=-JNO(I1)
                        N1=I9
                        GO TO 100
                     ENDIF
                  ENDIF
130            CONTINUE
               N1=JSTOUT
1000           IF (N1.LE.1) THEN
                  N2=1
               ELSE
                  N2=MIN(N1-1)+1
               ENDIF
               N3=MIN(N1)
               DO 1100 I1=N2,N3
                  I2=NGIN(I1)
                  LREC(I2)=100
1100           CONTINUE
1150           JREC=0
               DO 1300 I1=1,NJ
                  IF (JNO(I1).GT.0) THEN
                     DO 1170 I9=1,NJ
                        IF (JNOL(I9).EQ.JNO(I1)) GO TO 1180
1170                 CONTINUE
                     WRITE (6,*) 'SHOULD NOT OCCUR (1170)'
1180                 IF (I9.LE.1) THEN
                        L1=1
                     ELSE
                        L1=LOUT(I9-1)+1
                     ENDIF
                     L2=LOUT(I9)
                     KREADY=0
                     DO 1200 I3=L1,L2
                        I4=NGOUT(I3)
                        IF (LREC(I4).LE.0) THEN
                           KREADY=1
                           JREC=I9
                        ENDIF
1200                 CONTINUE
                     IF (KREADY.EQ.0) THEN
                        JNO(I1)=-JNO(I1)
                        N1=I9
                        GO TO 1000
                     ENDIF
                  ENDIF
1300           CONTINUE
            ENDIF
135      CONTINUE
         IF (JREC.GT.0) THEN
            KJSSX=0
            KJSSY=0
            KJSS=0
            N1=JREC
140         IF (N1.LE.1) THEN
               N2=1
            ELSE
               N2=LOUT(N1-1)+1
            ENDIF
            N3=LOUT(N1)
            NREADY=0
142         DO 170 I1=N2,N3
               I2=NGOUT(I1)
               IF (LREC(I2).EQ.NREADY) THEN
                  KJSS=KJSS+1
                  KJS(KJSS)=I2
                  IF (KJSS.GE.2) THEN
                  DO 160 I3=1,KJSS-1
                     J1=KJS(I3)
                     IF (JS(J1).EQ.JF(I2)) THEN
                        KREC=KREC+1
                        JUDGE=0
                        DO 143 I4=I3,KJSS
                           II1=KJS(I4)
                           IF (LREC(II1).LE.0) JUDGE=1
143                     CONTINUE
                        IF (JUDGE.EQ.0) KREC=KREC-1
                        DO 145 I4=I3,KJSS
                           IF (JUDGE.EQ.0) THEN
                              II1=KJS(I4)
                              LREC(II1)=100
                           ELSE
                              KJSSX=KJSSX+1
                              KJF(KJSSX)=KJS(I4)
                              II1=KJS(I4)
                              LREC(II1)=1
                           ENDIF
145                     CONTINUE
                        IF (JUDGE.GE.1) KNO(KREC)=KJSSX
                        DO 150 I4=1,KJSS
                           KJS(I4)=0
150                     CONTINUE
                        KJSS=0
                        DO 155 I4=1,NB
                           IF (KJSSY.GT.0) THEN
                              DO 154 II4=1,KJSSY
                                 IF (I4.EQ.IINU(II4)) GO TO 155
154                           CONTINUE
                           ENDIF
                           IF (LREC(I4).LE.0) THEN
                              JJ1=JS(I4)
                              DO 153 I5=1,NJ
                              IF (JJ1.EQ.JNO(I5)) THEN
                                 KJSSY=KJSSY+1
                                 IINU(KJSSY)=I4
                                 N1=I5
                                 GO TO 140
                              ENDIF
153                           CONTINUE
                           ENDIF
155                     CONTINUE
                        GO TO 175
                     ENDIF
160               CONTINUE
                  ENDIF
                  DO 165 N1=1,NJ
                     IF (JNOL(N1).EQ.JF(I2)) GO TO 140
165               CONTINUE
                  WRITE (6,*) 'SHOULD NOT OCCUR (165)'
               ENDIF
170         CONTINUE
            IF (NREADY.EQ.0) THEN
               NREADY=1
               GO TO 142
            ENDIF
         ENDIF
175      IF (KREC.GT.0) THEN
            WRITE (8,200)
            I2=1
            DO 190 I1=1,KREC
               I3=KNO(I1)
               K1=0
               DO 180 I4=I2,I3
                  I5=KJF(I4)
                  K1=K1+1
                  IINU(K1)=NO(I5)
                  K1=K1+1
                  IINU(K1)=JF(I5)
180            CONTINUE
               WRITE (8,210) I1,(IINU(K2),IINU(K2+1),K2=1,K1,2)
               I2=I3+1
190         CONTINUE
         ELSE
            WRITE (8,220)
         ENDIF
         DO 195 I=1,NMX
            KNO(I)=0
            KJS(I)=0
            KJF(I)=0
            IINU(I)=0
            LREC(I)=0
195      CONTINUE
         DO 198 I=1,NJ
            IF (JNO(I).LT.0) JNO(I)=-JNO(I)
198      CONTINUE
      ENDIF
C
200   FORMAT (//,1X,'THE FOLLOWING AIR RECIRCULATION PATH(S) WERE DETE',
     .       'CTED:',/)
210   FORMAT (/,1X,'RECIRCULATION PATH ',I3,'  "AIRWAY -> (JUNCTION)->"'
     .       ,50(/,1X,4(I5,'->(',I3,')->')))
220   FORMAT (//,1X,'NO AIR RECIRCULATION WAS DETECTED IN THE SYSTEM.')
      RETURN
C
      END

