      SUBROUTINE ADSET(XKNOTS, COEFS, KDIMEN, KMAX, NDIMEN, IERR)
C
C        ===============================================================
C
C **  THIS PROGRAM CHECKS THE INPUT DATA AND INITIALIZES THE COMPUTATION
C
      DOUBLE PRECISION A, ACCUR, B, BLEFT, BRIGHT, CHARF, DDTEMP,
     * DSCTOL, ERROR, ERRORI, FACTOR, FINTRP, FLEFT, FRIGHT, NORM,
     * XBREAK, XDD, XINTRP, XLEFT, XRIGHT, BUFFER
      DIMENSION XBREAK(20), DBREAK(20), BLEFT(20), BRIGHT(20)
      DIMENSION XLEFT(50), XRIGHT(50)
      DIMENSION DDTEMP(20,20), FACTOR(20), FINTRP(18), FLEFT(10),
     * FRIGHT(10), XDD(20), XINTRP(18)
      INTEGER BOTH, BREAK, DBREAK, DEGREE, EDIST, RIGHT, RIGHTX, SMOOTH
      LOGICAL DISCRD
      DOUBLE PRECISION XKNOTS(KDIMEN), COEFS(KMAX,NDIMEN)
      DOUBLE PRECISION AKMAX, EPS, EPS0, KM1, RATIO, ZERO, ONE, TWO,
     * THREE, C100
      DOUBLE PRECISION DPMPAR
C
      COMMON /INPUTZ/ A, B, ACCUR, NORM, CHARF, XBREAK, BLEFT, BRIGHT,
     * DBREAK, DEGREE, SMOOTH, LEVEL, EDIST, NBREAK, KNTDIM, NPARDM
      COMMON /RESULZ/ ERROR, KNOTS
      COMMON /KONTRL/ DSCTOL, ERRORI, XLEFT, XRIGHT, BREAK, BOTH,
     * FACTOR, IBREAK, INTERP, LEFT, MAXAUX, MAXKNT, MAXPAR, MAXSTK,
     * NPAR, NSTACK, RIGHT, DISCRD, BUFFER
      COMMON /COMDIF/ DDTEMP, FINTRP, FLEFT, FRIGHT, XDD, XINTRP,
     * LEFTX, NINTRP, RIGHTX
C
      DATA EPS0/1.D-30/
      DATA ZERO,ONE,TWO,THREE,C100/0.D0,1.D0,2.D0,3.D0,100.D0/
      DATA KLEFT, KRIGHT, KBOTH /1, 2, 3/
C
      EPS = DPMPAR(1)
      BUFFER = C100*DMAX1(EPS,EPS0)
C
C         PUT DATA STATEMENT ITEMS INTO COMMON VARIABLES
C
      LEFT = KLEFT
      RIGHT = KRIGHT
      BOTH = KBOTH
C
C -------- SET CURRENT VALUES OF LIMITS ON DIMENSIONS ------------------
C
      KNTDIM = KDIMEN
      NPARDM = NDIMEN
      MAXKNT = KNTDIM
      MAXSTK = 50
      MAXPAR = MIN0(20,NPARDM)
      MAXAUX = 20
C
C -------- CHECK INPUT DATA --------------------------------------------
C
      IERR = 0
      IF (A.GE.B .OR. ACCUR.LE.ZERO) GO TO 200
      IF (DEGREE.GE.MAXPAR .OR. 2*SMOOTH.GE.DEGREE) GO TO 200
      AKMAX = KMAX
      RATIO = (B-A)/(DABS(A)+DABS(B))
      IF (RATIO.LE.TWO*BUFFER*AKMAX) GO TO 210
      IF (CHARF.LT.(B-A)/AKMAX) GO TO 220
      IF (NORM.GE.ZERO .AND. (NORM-ONE)*(NORM-TWO)*(NORM-THREE).NE.ZERO)
     *         GO TO 200
      IF (EDIST*(EDIST-1)*(EDIST-2).NE.0) GO TO 200
      IF (NBREAK.LT.0 .OR. NBREAK.GT.MAXAUX) GO TO 200
      IF (NBREAK.EQ.0) GO TO 150
C
C         CHECK THE BREAK POINT DATA, MONOTONICITY AND DEGREE
C
      J = 1
      IF (XBREAK(1).LT.A .OR. XBREAK(NBREAK).GT.B) GO TO 230
      IF (NBREAK.EQ.1) GO TO 110
      DO 100 J=2,NBREAK
        IF (XBREAK(J-1).GE.XBREAK(J)) GO TO 230
  100 CONTINUE
  110 LIMSM = (DEGREE-1)/2
      DO 120 J=1,NBREAK
        IF (DBREAK(J).LT.0 .OR. DBREAK(J).GT.LIMSM) GO TO 240
  120 CONTINUE
C
C -------- INITIALIZATION OF VARIABLES ---------------------------------
C
C             ACTIVE INTERVAL STACK
C
  150 NSTACK = 1
      XLEFT(1) = A
      XRIGHT(1) = B
C
C             TERMINATION AND ERROR VALUES
C
      ERROR = ZERO
      DSCTOL = ACCUR**DABS(NORM)
      IF (EDIST.EQ.0) DSCTOL = DSCTOL/(B-A)
      IF (NORM.EQ.THREE) DSCTOL = ACCUR
C
C             MISCELLANEOUS VARIABLES AND POINTERS
C
      IBREAK = 1
      KNOTS = 1
      INTERP = DEGREE + 2 - 2*SMOOTH
      XKNOTS(1) = A
      NPAR = DEGREE + 1
C
C             COMPUTE ARRAY OF NPAR FACTORIALS
C
      FACTOR(1) = ONE
      FACTOR(2) = ONE
      DO 170 K=3,NPAR
        KM1 = K-1
        FACTOR(K) = KM1*FACTOR(K-1)
  170 CONTINUE
      RETURN
C
C -------- ERROR RETURN ------------------------------------------------
C
  200 IERR = -1
      RETURN
C
C         A AND B ARE TOO CLOSE
C
  210 IERR = -2
      RETURN
C
C         CHARF IS TOO SMALL
C
  220 IERR = -3
      RETURN
C
C         BREAK POINTS ARE NOT MONOTONIC
C
  230 IERR = -4
      RETURN
C
C         BAD VALUE IN DERIVATIVE BREAKS
C
  240 IERR = -5
      RETURN
      END
