      SUBROUTINE LEAVE(IERSET,NF,MF,XV,TV,WV,ERROR,KERNEL,RHFCN,EP,
     *   IFLAG,X,T,NT,IER,EPS,ELINSY,TN,WN,TM,WM,XM,XMZ,KMM,KMN,KNM,
     *   RHS,IMKNN,LUFACT,R,RH,DELN,NUP,NHALF,XNORM)
C
C   THIS ROUTINE SETS ALL NECESSARY PARAMETERS FOR LEAVING IEGAUS.
C   IF NT .GT. 0, IT ALSO PERFORMS THE NECESSARY NYSTROM
C   INTERPOLATION AT THE NODES GIVEN IN T.
C
      REAL KERNEL,KMM,KMN,KNM,IMKNN,LUFACT,NORMK,NUMR1
      DIMENSION X(1),T(1),XV(MF),TV(MF),WV(MF),TN(NF),WN(NF),WM(MF),
     *   XM(MF),XMZ(MF),KMM(NUP,NUP),KMN(NUP,NHALF),KNM(NHALF,NUP),
     *   RHS(MF),IMKNN(NUP,NUP),LUFACT(NUP,NUP),R(MF),RH(NF),
     *   TM(MF),DELN(NF)
      COMMON/XXINFO/R1,R2,FINLEP,NORMK,NFINAL,MFINAL
      EXTERNAL KERNEL,RHFCN
C   SET ERROR PARAMETERS FOR RETURN.
      NORMK=0.0
      NFINAL=NF
      MFINAL=MF
      FINLEP=EPS
      IF((EPS .GT. EP) .AND. (ERROR .LE. EPS)) GO TO 10
      IER=IERSET
      EP=ERROR
      IF(NT .EQ. 0) GO TO 20
      GO TO 30
10    IER=3
C   SINCE EPS IS THE SMALLEST ERROR POSSIBLE, SET EP=EPS FOR THE
C   RETURN ERROR ESTIMATE.
      EP=EPS
      IF(NT .GT. 0) GO TO 30
C   NO NYSTROM INTERPOLATION IS DESIRED. RETURN THE VALUES AT THE
C   GAUSSIAN NODE POINTS.
20    DO 21 I=1,MF
      X(I)=XV(I)
21    T(I)=TV(I)
      NT=MF
      RETURN
C   CALCULATE NORM(K).
30    SAVEP=EP
      DO 31 I=1,NF
31    IMKNN(I,I)=IMKNN(I,I)-1.0
      NORMK=0.0
      DO 33 I=1,NF
      SUM=0.0
      DO 32 J=1,NF
32    SUM=SUM+ABS(IMKNN(I,J))
33    NORMK=AMAX1(NORMK,SUM)
      DO 34 I=1,NF
34    IMKNN(I,I)=IMKNN(I,I)+1.0
      IF(NF .EQ. MF) GO TO 50
C   ITERATE TO DECREASE THE NOISE LEVEL IN X. THIS SHOULD REDUCE POSSIBL
C   ERRORS IN NYSTROM INTERPOLATION.
40    DERROR=((1.0-R1)/R1)*EPS/NORMK
      IF(IFLAG .EQ. 1) DERROR=DERROR*XNORM
      ITLOOP=0
      DO 41 I=1,MF
41    XM(I)=XV(I)
42    DO 43 I=1,MF
43    XMZ(I)=XM(I)
      CALL ITERT(KERNEL,RHFCN,NF,TN,WN,MF,TM,WM,XM,XMZ,KMM,KMN,KNM,
     *   RHS,IMKNN,LUFACT,R,RH,DELN,NUP,NHALF,1)
      NUMR1=RNRM(XM,XMZ,MF,1)
      ITLOOP=ITLOOP+1
      IF((NUMR1 .GT. DERROR) .AND. (ITLOOP .LT. 5)) GO TO 42
      DO 44 I=1,MF
44    XV(I)=XM(I)
C   ESTIMATE NEW ERROR BOUND FOR NYSTROM INTERPOLATES.
45    TEMP=NORMK*(R1/(1.0-R1))*NUMR1
      IF(IFLAG .EQ. 1) TEMP=TEMP/XNORM
      EP=AMAX1(EP,TEMP)
      GO TO 60
C   NO ITERATION USED IN COMPUTING X. JUST COMPUTE ERROR ESTIMATE IN
C   NYSTROM INTERPOLATE.
50    TEMP=NORMK*ELINSY
      IF(IFLAG .EQ. 0) TEMP=TEMP*XNORM
      IF(IER .NE. 2) EP=AMAX1(EP,TEMP)
C   COMPUTE NYSTROM INTERPOLATES AT THE NODES IN T.
60    DO 62 I=1,NT
      SUM=0.0
      DO 61 J=1,MF
61    SUM=SUM+WV(J)*KERNEL(T(I),TV(J))*XV(J)
62    X(I)=RHFCN(T(I))+SUM
      IF((IER .EQ. 0) .AND. (EP .GT. EPS)) IER=4
      IF((IER .EQ. 1) .AND. (EP .GT. ERROR)) IER=5
      IF((IER .EQ. 3) .AND. (EP .GT. EPS)) IER=6
      EP=SAVEP
      RETURN
      END
