* Last processed by NICE on 12-Jun-2000 15:51:00
* Customized for :  IEEE, LINUX, UNIX, MOTIF, F77
      SUBROUTINE COPY_GOOD(NGOOD,GOOD,INBUF,OUTBUF)
C----------------------------------------------------------------------
C PSC	Copies appropriate information from one column of input table
C	to output table.
C----------------------------------------------------------------------
      INTEGER NGOOD, GOOD(NGOOD)
      INTEGER OUTBUF(NGOOD), INBUF(1)
      INTEGER I
*
      DO I=1,NGOOD
         OUTBUF(I) = INBUF(GOOD(I))
      ENDDO
      END
*<FF>
      SUBROUTINE GET_RANGE(NLINE,ADDR,NRANGE,NMAX,NMIN)
C----------------------------------------------------------------------
C PSC	Determines source range(s) from box limits in RA.
C
C Arguments
C	NLINE	I	Total number of sources			Input
C	ADDR 	Addr	Pointer to first right ascension	Input
C	NRANGE 	I	Number of source ranges			Output
C	NMAX	I(2)	Maximum source number in each range	Output
C	NMIN	I(2)	Minimum source number in each range	Output
C----------------------------------------------------------------------
      INTEGER NMAX(2), NMIN(2), NLINE, NRANGE, ADDR
*
      INCLUDE 'psc.inc'
      INCLUDE 'inc:memory.inc'
      INTEGER N
      INTEGER*4 IP, POINTER
*
      IP = POINTER(ADDR,MEMORY)
      IF (RMAX.GT.RMIN) THEN
         NRANGE = 1
         CALL FIND_RANGE (NLINE,MEMORY(IP),RMIN,N)
         NMIN(1) = MAX(1,N-1)
         CALL FIND_RANGE (NLINE,MEMORY(IP),RMAX,N)
         NMAX(1) = MIN(NLINE,N+1)
      ELSE
         NRANGE = 2
         CALL FIND_RANGE (NLINE,MEMORY(IP),RMIN,N)
         NMIN(1) = MAX(1,N-1)
         NMAX(1) = NLINE
         NMIN(2) = 1
         CALL FIND_RANGE (NLINE,MEMORY(IP),RMAX,N)
         NMAX(2) = MIN(NLINE,N+1)
      ENDIF
      END
*<FF>
      SUBROUTINE FIND_RANGE (NP,X,XLIM,NLIM)
C----------------------------------------------------------------------
C GILDAS	Internal routine
C	Find NLIM such as
C	 	X(NLIM-1) < XLIM < X(NLIM)
C	for input data ordered.
C	Use a dichotomic search for that
C Arguments:
C	NP	I	Number of values in input array		Input
C	X	R*4	Ordered input vector			Input
C	XLIM	R*4	Search value				Input
C	NLIM	I	Index of found element.			Output
C----------------------------------------------------------------------
      INTEGER NP, NINF, NSUP, NMID, NLIM
      REAL*8 XLIM
      REAL X(NP)
*
      IF (X(1).GT.XLIM) THEN
         NLIM = 1
         RETURN
      ENDIF
      NINF = 1
      NSUP = NP
*
      DO WHILE (NSUP.GT.NINF+1)
         NMID = (NSUP + NINF)/2
         IF (X(NMID).LT.XLIM) THEN
            NINF = NMID
         ELSE
            NSUP = NMID
         ENDIF
      ENDDO
      NLIM = NSUP
      END
