C
C
      SUBROUTINE CHSFIT (NSTOP1)
C     ***************************
C
C     ----------------------------------------------------------------
C
C       SUBROUTINE PURPOSE:
C       1) SELECT SUITABLE FAN CURVE FITTING METHODS FOR EACH FAN.
C
C     ----------------------------------------------------------------
C
      INCLUDE 'CMMNW.DAT'
C
      DIMENSION HFN(IMX), QFN(IMX), NAUTO(IMX)
C
      DO 75 L=1,NFNUM
      IF (NSWT(L).LT.3) GO TO 75
         IF (NSWT(L).EQ.3) NAUTO(L)=NSWT(L)
         DFQ1=ABS(QF(L,2)-QF(L,1))
         DFP1=ABS(PF(L,2)-PF(L,1))
         DO 55 I=2,MPTS(L)-1
            DFQ2=ABS(QF(L,I)-QF(L,I-1))
            DFP2=ABS(PF(L,I)-PF(L,I-1))
            IF (DFQ1.LT.DFQ2) THEN
               DFQ1=DFQ2
            ELSEIF (DFP1.LT.DFP2) THEN
               DFP1=DFP2
            ENDIF
55       CONTINUE
         DQMAX=DFQ1
         DPMAX=DFP1
         DO 60 I=2,MPTS(L)-1
            DFQ2=ABS(QF(L,I+1)-QF(L,I))
            DFP2=ABS(PF(L,I+1)-PF(L,I))
            IF (DFQ1.GT.DFQ2) THEN
               DFQ1=DFQ2
            ENDIF
            IF (DFP1.GT.DFP2) THEN
               DFP1=DFP2
            ENDIF
60       CONTINUE
         DQMIN=DFQ1
         DPMIN=DFP1
         DIVQ=DQMAX/DQMIN
         DIVP=DPMAX/DPMIN
         IF (DIVQ.GT.4.AND.DIVP.GT.4) THEN
            NSWT(L)=1
            WRITE (8,975) L
            GO TO 75
         ENDIF
         INDEX=MPTS(L)
         IF (INDEX.LT.3) THEN
            WRITE (*,980) INDEX
             NSTOP1=1
            RETURN
         ELSE
            THSUM1=0.
            THSUM2=0.
            DO 444 K3=1,INDEX-1
               DQZZ=(QF(L,K3+1)-QF(L,K3))
               QFM=QF(L,K3)+DQZZ/2
               CALL SPLINE (L,QFM,HFM,TR,0)
               DIFH1=HFM-PF(L,K3)
               DIFH2=HFM-PF(L,K3+1)
               IF (PF(L,K3).GT.PF(L,K3+1)) THEN
                  IF (DIFH1.GT.0) THEN
                     THSUM1=THSUM1+DIFH1
                  ELSEIF (DIFH2.LT.0) THEN
                     THSUM1=THSUM1+ABS(DIFH2)
                  ENDIF
               ELSE
                  IF (DIFH2.GT.0) THEN
                     THSUM1=THSUM1+DIFH2
                  ELSEIF (DIFH1.LT.0) THEN
                     THSUM1=THSUM1+ABS(DIFH1)
                  ENDIF
               ENDIF
               CALL LSFAN (L,QFM,HFM,TR,0,0)
               DIFH3=HFM-PF(L,K3)
               DIFH4=HFM-PF(L,K3+1)
               IF (PF(L,K3).GT.PF(L,K3+1)) THEN
                  IF (DIFH3.GT.0) THEN
                     THSUM2=THSUM2+DIFH3
                  ELSEIF (DIFH4.LT.0) THEN
                     THSUM2=THSUM2+ABS(DIFH4)
                  ENDIF
               ELSE
                  IF (DIFH4.GT.0) THEN
                     THSUM2=THSUM2+DIFH4
                  ELSEIF (DIFH3.LT.0) THEN
                     THSUM2=THSUM2+ABS(DIFH3)
                  ENDIF
               ENDIF
444         CONTINUE
         ENDIF
         IF (ABS(THSUM1).GT.1.2*ABS(THSUM2)) THEN
            NSWT(L)=1
            WRITE (*,1000) L
         ELSE
            NSWT(L)=2
            WRITE (*,1005) L
         ENDIF
         IF (NAUTO(L).EQ.3) GO TO 75
         WRITE (*,1010) NOF(L)
         WRITE (*,1020)
         LL=MPTS(L)
         WRITE (*,1030) (QF(L,I)/1000.0,PF(L,I),I=1,LL)
         WRITE (*,1045) NOF(L)
            WRITE (*,1086)
            WRITE (*,1085)
            DO 441 K1=1,INDEX-1
               QZZ=(QF(L,K1+1)-QF(L,K1))/5.0
               DO 42 K2=1,5
                  QZZ1=QF(L,K1)+(K2-1)*QZZ
                  NFCWZ=NFCW(L)
                  CALL SPLINE (L,QZZ1,HZZ,TR,1)
                  NFCW(L)=NFCWZ
                  HFN(K2)=HZZ
                  QFN(K2)=QZZ1
42             CONTINUE
               WRITE (*,1090) (QFN(K4)/1000.,HFN(K4),K4=1,5)
441         CONTINUE
            WRITE (*,1087)
            WRITE (*,1085)
            DO 445 K1=1,INDEX-1
               QZZ=(QF(L,K1+1)-QF(L,K1))/5.0
               DO 443 K2=1,5
                  QZZ1=QF(L,K1)+(K2-1)*QZZ
                  NFCWZ=NFCW(L)
                  CALL LSFAN (L,QZZ1,HZZ,TR,1,0)
                  NFCW(L)=NFCWZ
                  HFN(K2)=HZZ
                  QFN(K2)=QZZ1
443            CONTINUE
               WRITE (*,1090) (QFN(K4)/1000.,HFN(K4),K4=1,5)
445         CONTINUE
         WRITE (*,1100)
         READ (*,*) NSWT(L)
75      CONTINUE
C
       RETURN
C
975   FORMAT (//,2X,'SOME DATA POINTS OF FAN',I5,2X,'CURVE ARE TOO CLOSE
     .',/,'SO THE LEAST SQUARE METHOD IS DESIRED')
980   FORMAT (//,2X,'THE DATA POINTS OF FAN CHARACTERISTIC ARE',I5,/,
     .'AT LEAST 3 DATA POINTS ARE DESIRED',/)
1000  FORMAT (//,2X,'RECOMMEND LEAST SQUARE METHOD FOR FAN',I5,/,
     .'SURVE FITTING',/)
1005  FORMAT (//,2X,'RECOMMEND SPLINE METHOD FOR FAN',I5,/,
     .'SURVE FITTING')
1010  FORMAT (//,'THESE CHARACTERISTICS WERE STORED FOR FAN IN AIRWAY'
     .,I5,/)
1020  FORMAT (//,5('  Q*1000   PF '),/)
1030  FORMAT (5(F8.1,F6.2)/,5(F8.1,F6.2),/)
1045  FORMAT (/,T5,'FAN',I5,/)
1085  FORMAT (/,2X,'INTERPOLATION:',/,5('  Q*1000   PF '),/)
1086  FORMAT (//,2X,'APPLYING SPLINE METHOD')
1087  FORMAT (//,2X,'APPLYING LEAST SQUARE METHOD')
1090  FORMAT (5(F8.1,F6.2))
1100  FORMAT (//,2X,'INPUT SELECT FAN CURVE FITTING METHOD',/,
     .'1-- FOR LEAST SQUARE METHOD, 2--FOR SPLINE METHOD')
C
      END
C


