      subroutine find_stars(ra,spd,rahalf,spdhalf,
     $     lomaglim,himaglim,magtype,density,
     $     list_ra,list_dec,list_mag,list_length,catmap,error)
C
C Description:
C
C This routine determines the region of the catalog is to be searched
C to find stars in the region requested by the user.
C
C Programming interface:
C
C name         in/out  type     structure  meaning
C
C ra           in      real*8   scalar     Right ascension of the center of the
C                                          scan region
C spd          in      real*8   scalar     SPD of the center of the 
C                                          scan region
C spdhalf      in      real*8   scalar     SPD half-width of the 
C                                          scan region
C rahalf       in      real*8   scalar     RA half-width of the scan region
C lomaglim     in      real*8   scalar     Low magnitude limit
C himaglim     in      real*8   scalar     High magnitude limit
C magtype      in      character*1 scalar  'r'=red 'b'=blue 'd'=difference
C density      in      real*8   scalar     Density of stars (per sq. deg.)
C list_ra      out     integer  (maxlist)  The right ascension of the 
C                                          stars found
C list_dec     out     integer  (maxlist)  The declination of the stars found
C list_mag     out     integer  (maxlist)  The magnitude of the stars found
C list_length  out     integer  scalar     The number of stars found
C catmap       in      character*64 scalar The name of the catalog map file
C error        out     logical  scalar     Returned TRUE if an error occurs.
C
C Errors
C
C All local I/O errors are trapped. If such an error occurs, the routine
C immediately returns. All errors from called routines force this routine
C to immediately return. Only I/O errors are trapped locally. No other 
C error should occur.
C
C Routines accessed:
C
C find_constants
C find_ra_limits
C star_list
C err_msg
C
      implicit none
      include 'std_io.for'
      include 'include_.for'
      integer looplim,list_length,ndx,catalog,k,cat_start,
     $     cat_end,stat,i,list_ra,list_dec,list_mag,lnblnk,
     $     fnblnk,status
      real*8 ra,spd,rahalf,spdhalf,lomaglim,himaglim,density,
     $     spd_start,spd_end,ra_local,
     $     ra_low_lim,ra_high_lim
      character*64 path,msg,catmap
      character*8 response,name
      character*1 magtype
      logical error,firsttime,file_is_present,done
      dimension path(0:dec_regions-1),list_ra(maxlist),
     $     list_dec(maxlist),list_mag(maxlist),msg(0:dec_regions),
     $     name(0:dec_regions-1)
      save firsttime,path,msg
      data firsttime/.true./
      data name /
     $     'zone0000',
     $     'zone0075',
     $     'zone0150',
     $     'zone0225',
     $     'zone0300',
     $     'zone0375',
     $     'zone0450',
     $     'zone0525',
     $     'zone0600',
     $     'zone0675',
     $     'zone0750',
     $     'zone0825',
     $     'zone0900',
     $     'zone0975',
     $     'zone1050',
     $     'zone1125',
     $     'zone1200',
     $     'zone1275',
     $     'zone1350',
     $     'zone1425',
     $     'zone1500',
     $     'zone1575',
     $     'zone1650',
     $     'zone1725'/
      if (firsttime) then
         firsttime=.false.
         call read_catmap(catmap,path,msg,error)
         if (error) goto 400
      end if
      ndx=201
      catalog=202
C
C If the specified search region crosses the pole, it will
C create two regions. So search the catalog in two regions
C and concatinate the results. Note that the limits
C on spdhalf preclude the possibility of covering both poles.
C
      looplim=1
      list_length=0
      If ((spd+spdhalf .gt. pi) .or. (spd-spdhalf .lt. 0.0d0)) then
         looplim=2
      end if
      do k=1,looplim
C
C Since pole crossing is a possibility, calculate new values
C for spd_start, and spd_end for each time through the loop.
C
         if (looplim .eq. 2) then
            if (k .eq. 1) then
               if (spd+spdhalf .gt. pi) then
                  spd_start=spd-spdhalf
                  spd_end=pi
               elseif (spd-spdhalf .lt. 0.0d0) then
                  spd_start=0.0d0
                  spd_end=spd+spdhalf
               end if
               ra_local=ra
            elseif (k .eq. 2) then
               if (spd+spdhalf .gt. pi) then
                  spd_start=twopi-(spd+spdhalf)
                  spd_end=pi
               elseif (spd-spdhalf .lt. 0.0d0) then
                  spd_start=0.0d0
                  spd_end=-(spd-spdhalf)
               end if
               ra_local=mod(ra+pi,twopi)
            end if
         elseif (looplim .eq. 1) then
            spd_start=spd-spdhalf
            spd_end=spd+spdhalf
            ra_local=ra
         end if
         call find_constants(spd_start,spd_end,cat_start,cat_end)
         do i=cat_start,cat_end
            file_is_present=.false.
            done=.false.
            do while ((.not. file_is_present) .and. (.not. done))
               open(unit=ndx,
     $              file=path(i)(1:lnblnk(path(i)))//
     $              name(i)//'.acc',
     $              iostat=status,
     $              mode='read',
     $              status='old')
               if (status .eq. 6416) then
                   file_is_present=.false.
               elseif (status .eq. 0) then
                   close(unit=ndx,iostat=status)
                   file_is_present=.true.
               else
                   goto 401
               end if
               if (file_is_present) then
                  done=.true.
                  open(unit=ndx,
     $                 file=path(i)(1:lnblnk(path(i)))//
     $                 name(i)//'.acc',
     $                 err=401,
     $                 iostat=stat,
     $                 access='SEQUENTIAL',
     $                 status='old')
                  open(unit=catalog,
     $                 file=path(i)(1:lnblnk(path(i)))//
     $                 name(i)//'.cat',
     $                 err=402,
     $                 iostat=stat,
     $                 access='DIRECT',
     $                 recl=12,
     $                 status='old')
                  call find_ra_limits(ra_local,i,
     $                 rahalf,spd_start,spd_end,
     $                 ra_low_lim,ra_high_lim)
                  if (ra_low_lim .lt. 0.0d0) then
                          call star_list(ndx,catalog,spd_start,
     $                    spd_end,spdhalf,
     $                    rahalf,ra_local,ra_low_lim+twopi,twopi,
     $                    lomaglim,himaglim,magtype,density,
     $                    list_ra,list_dec,list_mag,
     $                    list_length,error)
                     if (error) goto 400
                     call star_list(ndx,catalog,spd_start,
     $                    spd_end,spdhalf,
     $                    rahalf,ra_local,0.0d0,ra_high_lim,
     $                    lomaglim,himaglim,magtype,density,
     $                    list_ra,list_dec,list_mag,
     $                    list_length,error)
                     if (error) goto 400
                  else if (ra_high_lim .gt. twopi) then
                     call star_list(ndx,catalog,spd_start,
     $                    spd_end,spdhalf,
     $                    rahalf,ra_local,ra_low_lim,twopi,
     $                    lomaglim,himaglim,magtype,density,
     $                    list_ra,list_dec,list_mag,
     $                    list_length,error)
                     if (error) goto 400
                     call star_list(ndx,catalog,spd_start,
     $                    spd_end,spdhalf,
     $                    rahalf,ra_local,0.0d0,ra_high_lim-twopi,
     $                    lomaglim,himaglim,magtype,density,
     $                    list_ra,list_dec,list_mag,
     $                    list_length,error)
                     if (error) goto 400
                  else 
                     call star_list(ndx,catalog,spd_start,
     $                    spd_end,spdhalf,
     $                    rahalf,ra_local,ra_low_lim,ra_high_lim,
     $                    lomaglim,himaglim,magtype,density,
     $                    list_ra,list_dec,list_mag,
     $                    list_length,error)
                     if (error) goto 400
                  end if
                  close(unit=catalog,err=403,iostat=stat)
                  close(unit=ndx,err=404,iostat=stat)
               else
                  call stat_msg('find_stars',
     $                 'The required catalog file is not '//
     $                 'on the disk.')
                  write(stderr,1) msg(i)
                  write(debug,1) msg(i)
 1                format(' ',a64)
                  write(stderr,2)
                  write(debug,2)
                  write(stderr,4)
                  write(debug,4)
 2                format(' Load the catalog file in the proper ',
     $                 'location and depress ''<ret>''.')
 4                format(' To quit, depress ''q <ret>''.')
                  read(stdin,3) response
 3                format(a8)
                  if (fnblnk(response) .ne. 0) then
                     done=.true.
                  end if
               end if
            end do
         end do
      end do
      goto 400
 401  call err_msg('find_stars','Open error on catalog index.')
      error=.true.
      goto 400
 402  call err_msg('find_stars','Open error on catalog.')
      error=.true.
      goto 400
 403  call err_msg('find_stars','Close error on catalog.')
      error=.true.
      goto 400
 404  call err_msg('find_stars','Close error on catalog index.')
      error=.true.
      goto 400
 405  call err_msg('find_stars','Error using inquire function.')
      error=.true.
 400  continue
      if (error) then
         call err_msg('find_stars','Traceback')
      end if
      return
      end
