      SUBROUTINE SPLINE (J,QZZ1,RKI,TABF,NTW)
C
C     ----------------------------------------------------------------
C
C       SUBROUTINE PURPOSE:
C       1) FAN CURVE FITTING.
C       2) FAN PRESSURE INTERPOLATION.
C
C     ----------------------------------------------------------------
C
      INCLUDE 'CMMNW.DAT'
C
C     FKQ(J,I,1-4): COEFFS. OF 3RD ORDER POLYNOMIAL FOR FAN NOF(J),
C                   SEGMENT I.
C     NSKP(J)=1: MARKER INDICATING THAT FAN CURVE FITTING FOR FAN
C                NOF(J) HAS BEEN PROCESSED.
C
C         write (8,*) 'jqkiirkitabfntw',j,qzz1,rki,tabf,ntw
      MF = MPTS(J)
      IF (NTW.EQ.0) THEN
C
         DO 5 I=1,MF-1
            DK(I)=QF(J,I+1)-QF(J,I)
5        CONTINUE
C
         DO 10 I=2,MF-1
            FC3(I)=3.0*(PF(J,I+1)*DK(I-1)-PF(J,I)*(QF(J,I+1)-QF(J,I-1))
     .             +PF(J,I-1)*DK(I))/(DK(I-1)*DK(I))
10       CONTINUE
C
         FK(1)=1.0
         FC1(1)=0.0
         FC2(1)=0.0
C
         DO 15 I=2,MF-1
            FK(I)=2.0*(QF(J,I+1)-QF(J,I-1))-DK(I-1)*FC1(I-1)
            FC1(I)=DK(I)/FK(I)
            FC2(I)=(FC3(I)-DK(I-1)*FC2(I-1))/FK(I)
15       CONTINUE
C
         FK(MF-1)=1.0
         FC3(MF-1)=0.0
         FKQ(J,MF-1,3)=0.0
C
         DO 20 I=MF-1,1,-1
            FKQ(J,I,3)=FC2(I)-FC1(I)*FKQ(J,I+1,3)
            FKQ(J,I,2)=(PF(J,I+1)-PF(J,I))/DK(I)-
     .                 DK(I)*(FKQ(J,I+1,3)+2.0*FKQ(J,I,3))/3.0
            FKQ(J,I,4)=(FKQ(J,I+1,3)-FKQ(J,I,3))/(3.0*DK(I))
20       CONTINUE
C
         DO 25 I=1,MF-1
            FKQ(J,I,1)=PF(J,I)
25       CONTINUE
         NSKP(J)=1
      ENDIF
C
C
      QKI=ABS(QZZ1)
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
      IF (NPLOT.EQ.1) THEN
         IF (QKI.LE.QF(J,1).OR.NEGQ(J).GE.1) THEN
            DQKI=0.0
            DQKI1=QKI-QF(J,1)
            M=1
            RGRAD(J) = FKQ(J,M,2)+2*FKQ(J,M,3)*DQKI+
     .                 3*FKQ(J,M,4)*DQKI**2
            RKI=PF (J,1)+DQKI1*RGRAD(J)
            RKI=RKI*(TR+460.)/(TABF+460.)
            NFCW(J) = 1
            RETURN
         ELSE IF (QKI.GE.QF(J,MF)) THEN
            DQKI=QF(J,MF)-QF(J,MF-1)
            DQKI1=QKI-QF(J,MF)
            M=MF-1
            RGRAD(J) = FKQ(J,M,2)+2*FKQ(J,M,3)*DQKI+
     .                 3*FKQ(J,M,4)*DQKI**2
            RKI=PF (J,MF)+DQKI1*RGRAD(J)
            RKI=RKI*(TR+460.)/(TABF+460.)
            NFCW(J) = 1
            RETURN
         ENDIF
      ELSEIF (NPLOT.EQ.2) THEN
         IF (QKI.LE.QF(J,1).OR.NEGQ(J).GE.1) THEN
            DQKI=0.0
            DQKI1=QKI-QF(J,1)
            M=1
            RGRAD(J) = FKQ(J,M,2)+2*FKQ(J,M,3)*DQKI+
     .                 3*FKQ(J,M,4)*DQKI**2
            RKI=PF (J,1)+DQKI1*RGRAD(J)
            RKI=RKI*(TR+460.)/(TABF+460.)
            NFCW(J) = 1
            RETURN
         ELSE IF (QKI.GE.QF(J,MF)) THEN
            RKI = PF(J,MF)*(TR+460.)/(TABF+460.)
            RGRAD(J) = 0.0
            NFCW(J) = 1
            RETURN
         ENDIF
      ELSEIF (NPLOT.EQ.3) THEN
         IF (QKI.LE.QF(J,1).OR.NEGQ(J).GE.1) THEN
            RKI = PF(J,1)*(TR+460.)/(TABF+460.)
            RGRAD(J) = 0.0
            NFCW(J) = 1
C                   write (8,*) 'bround1',j,rgrad(j),qki,qf(j,1),rki
            RETURN
         ELSE IF (QKI.GE.QF(J,MF)) THEN
            RKI = PF(J,MF)*(TR+460.)/(TABF+460.)
            RGRAD(J) = 0.0
            NFCW(J) = 1
C               write (8,*) 'bround2',j,rgrad(j),qki,qf(j,mf),rki
            RETURN
         ENDIF
      ENDIF      
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
      IF (QF(J,1).GE.QKI) THEN
         M = 1
      ELSE
         DO 40 K = 2,MF
            IF (QF(J,K).GE.QKI) THEN
               M = K-1
               GO TO 45
            ENDIF
40      CONTINUE
      ENDIF
      M = MF-1
45    DQKI=QKI-QF(J,M)
      RKI = FKQ(J,M,1)+FKQ(J,M,2)*DQKI+FKQ(J,M,3)*DQKI**2
     .        +FKQ(J,M,4)*DQKI**3
      RKI=RKI*(TR+460.)/(TABF+460.)
C            write (8,*) 'dqkirki',j,m,dqki,fkq(j,m,1),fkq(j,m,2)
C      write (8,*) 'fkq34',fkq(j,m,3),fkq(j,m,4)
C      write (8,*) 'rkirkiiqki',rki,rkii,qki,tr,tabf
      RGRAD(J) = FKQ(J,M,2)+2*FKQ(J,M,3)*DQKI+
     .            3*FKQ(J,M,4)*DQKI**2
C        write (8,*) 'rgrad00',j,rgrad(j),DQKI,FKQ(J,M,2),FKQ(j,m,3)
C
      RETURN
      END
