C
C     ..................................................................
C
C        SUBROUTINE DATSG
C
C        PURPOSE
C           NDIM POINTS OF A GIVEN GENERAL TABLE ARE SELECTED AND
C           ORDERED SUCH THAT ABS(ARG(I)-X).GE.ABS(ARG(J)-X) IF I.GT.J.
C
C        USAGE
C           CALL DATSG (X,Z,F,WORK,IROW,ICOL,ARG,VAL,NDIM)
C
C        DESCRIPTION OF PARAMETERS
C           X      - DOUBLE PRECISION SEARCH ARGUMENT.
C           Z      - DOUBLE PRECISION VECTOR OD ARGUMENT VALUES
C                    (DIMENSION IROW).
C           F      - IN CASE ICOL=1, F IS THE DOUBLE PRECISION VECTOR
C                    OF FUNCTION VALUES (DIMENSION IROW).
C                    IN CASE ICOL=2, F IS A DOUBLE PRECISION IROW BY 2
C                    MATRIX. THE FIRST COLUMN SPECIFIES VECTOR OF FUNC-
C                    TION VALUES AND THE SECOND VECTOR OF DERIVATIVES.
C           WORK   - DOUBLE PRECISION WORKING STORAGE (DIMENSION IROW).
C           IROW   - THE DIMENSION OF VECTORS Z AND WORK AND OF EACH
C                    COLUMN IN MATRIX F.
C           ICOL   - THE NUMBER OF COLUMNS IN F (I.E. 1 OR 2).
C           ARG    - RESULTING DOUBLE PRECISION VECTOR OF SELECTED AND
C                    ORDERED ARGUMENT VALUES (DIMENSION NDIM).
C           VAL    - RESULTING DOUBLE PRECISION VECTOR OF SELECTED
C                    FUNCTION VALUES (DIMENSION NDIM) IN CASE ICOL=1.
C                    IN CASE ICOL=2, VAL IS THE DOUBLE PRECISION VECTOR
C                    OF FUNCTION AND DERIVATIVE VALUES (DIMENSION
C                    2*NDIM) WHICH ARE STORED IN PAIRS (I.E. EACH FUNC-
C                    TION VALUE IS FOLLOWED BY ITS DERIVATIVE VALUE).
C           NDIM   - THE NUMBER OF POINTS WHICH MUST BE SELECTED OUT OF
C                    THE GIVEN TABLE (Z,F).
C
C        REMARKS
C           NO ACTION IN CASE IROW LESS THAN 1.
C           IF INPUT VALUE NDIM IS GREATER THAN IROW, THE PROGRAM
C           SELECTS ONLY A MAXIMUM TABLE OF IROW POINTS.  THEREFORE THE
C           USER OUGHT TO CHECK CORRESPONDENCE BETWEEN TABLE (ARG,VAL)
C           AND ITS DIMENSION BY COMPARISON OF NDIM AND IROW, IN ORDER
C           TO GET CORRECT RESULTS IN FURTHER WORK WITH TABLE (ARG,VAL).
C           THIS TEST MAY BE DONE BEFORE OR AFTER CALLING
C           SUBROUTINE DATSG.
C           SUBROUTINE DATSG ESPECIALLY CAN BE USED FOR GENERATING THE
C           TABLE (ARG,VAL) NEEDED IN SUBROUTINES DALI, DAHI, AND DACFI.
C
C        SUBROUTINES AND FUNCTION SUBPROGRAMS REQUIRED
C           NONE
C
C        METHOD
C           SELECTION IS DONE BY GENERATING THE VECTOR WORK WITH
C           COMPONENTS WORK(I)=ABS(Z(I)-X) AND AT EACH OF THE NDIM STEPS
C           (OR IROW STEPS IF NDIM IS GREATER THAN IROW)
C           SEARCHING FOR THE SUBSCRIPT OF THE SMALLEST COMPONENT, WHICH
C           IS AFTERWARDS REPLACED BY A NUMBER GREATER THAN
C           MAX(WORK(I)).
C
C     ..................................................................
C
      SUBROUTINE DATSG(X,Z,F,WORK,IROW,ICOL,ARG,VAL,NDIM)
C
C
      DIMENSION Z(1),F(1),WORK(1),ARG(1),VAL(1)
      DOUBLE PRECISION X,Z,F,WORK,ARG,VAL,B,DELTA
      IF(IROW)11,11,1
    1 N=NDIM
C     IF N IS GREATER THAN IROW, N IS SET EQUAL TO IROW.
      IF(N-IROW)3,3,2
    2 N=IROW
C
C     GENERATION OF VECTOR WORK AND COMPUTATION OF ITS GREATEST ELEMENT.
    3 B=0.D0
      DO 5 I=1,IROW
      DELTA=DABS(Z(I)-X)
      IF(DELTA-B)5,5,4
    4 B=DELTA
    5 WORK(I)=DELTA
C
C     GENERATION OF TABLE (ARG,VAL)
      B=B+1.D0
      DO 10 J=1,N
      DELTA=B
      DO 7 I=1,IROW
      IF(WORK(I)-DELTA)6,7,7
    6 II=I
      DELTA=WORK(I)
    7 CONTINUE
      ARG(J)=Z(II)
      IF(ICOL-1)8,9,8
    8 VAL(2*J-1)=F(II)
      III=II+IROW
      VAL(2*J)=F(III)
      GOTO 10
    9 VAL(J)=F(II)
   10 WORK(II)=B
   11 RETURN
      END
