C
C     ..................................................................
C
C        SUBROUTINE ATSG
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 ATSG (X,Z,F,WORK,IROW,ICOL,ARG,VAL,NDIM)
C
C        DESCRIPTION OF PARAMETERS
C           X      - THE SEARCH ARGUMENT.
C           Z      - THE VECTOR OF ARGUMENT VALUES (DIMENSION IROW).
C           F      - IN CASE ICOL=1, F IS THE VECTOR OF FUNCTION VALUES
C                    (DIMENSION IROW).
C                    IN CASE ICOL=2, F IS AN IROW BY 2 MATRIX. THE FIRST
C                    COLUMN SPECIFIES THE VECTOR OF FUNCTION VALUES AND
C                    THE SECOND THE VECTOR OF DERIVATIVES.
C           WORK   - A 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    - THE RESULTING VECTOR OF SELECTED AND ORDERED
C                    ARGUMENT VALUES (DIMENSION NDIM).
C           VAL    - THE RESULTING VECTOR OF SELECTED FUNCTION VALUES
C                    (DIMENSION NDIM) IN CASE ICOL=1. IN CASE ICOL=2,
C                    VAL IS THE VECTOR OF FUNCTION AND DERIVATIVE VALUES
C                    (DIMENSION 2*NDIM) WHICH ARE STORED IN PAIRS (I.E.
C                    EACH FUNCTION VALUE IS FOLLOWED BY ITS DERIVATIVE
C                    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 ATSG.
C           SUBROUTINE ATSG ESPECIALLY CAN BE USED FOR GENERATING THE
C           TABLE (ARG,VAL) NEEDED IN SUBROUTINES ALI, AHI, AND ACFI.
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 ATSG(X,Z,F,WORK,IROW,ICOL,ARG,VAL,NDIM)
C
C
      DIMENSION Z(1),F(1),WORK(1),ARG(1),VAL(1)
      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.
      DO 5 I=1,IROW
      DELTA=ABS(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.
      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
