      subroutine make_cat_list(list_ra,list_dec,list_mag,
     $     list_length,dec_center,ra_center,round_aper,
     $     focallength,pixelsize,ascending,sortfield,outfilename,
     $     radius,region_count,rahalf,dechalf,error)
C
C Description:
C
C This routine writes the list of "found" stars to file
C in USNO's catalog format. This format will allow subcatalogs
C to be created. A subcatalog will be an acceptable catalog to
C existing software (such as this program) if the stars are sorted
C in increaseing RA order. If they are arranged in another order,
C software will have to be changed in order to find the desired stars.
C
C Programming Interface:
C
C name         in/out type       structure description
C
C list_ra      in     integer     (maxlist) The list of RAs for the stars 
C                                           (arcsec*100)
C list_dec     in     integer     (maxlist) The list of decls for the stars
C                                           (arcsec*100)
C list_mag     in     integer     (maxlist) The list of magnitudes for the 
C                                           stars (format is complex - see
C                                           other documentation for details
C list_length  in     integer     scalar    The number of stars in the lists
C ra_center    in     real*8      scalar    The RA center position (rad)
C dec_center   in     real*8      scalar    The decl center position (rad)  
C round_aper   in     logical     scalar    TRUE if the aperture is round
C                                           Not used in this routine.
C focallength  in     real*8      scalar    Focal length of telescope. Any
C                                           unit is acceptable as long as
C                                           is the same unit used by 
C                                           pixelsize. Not used in this
C                                           subroutine
C pixelsize    in     real*8      scalar    Pixel size of CCD chip. Not used
C                                           in this routine. Same units as
C                                           focallength. Not used in this
C                                           subroutine
C ascending    in     logical     scalar    If TRUE sort order is ascending.
C sortfield    in     character*(1) scalar  Selects field on which sort is 
C                                           performed
C                                           'R' is RA
C                                           'D' is Decl.
C                                           'r' is red
C                                           'b' is blue
C                                           'd' is radius
C                                           'i' is color index
C outfilename  in     character*(64) scalar The prototype name of the output 
C                                           file
C radius       in     real*8      scalar    The radius of the round aperture
C                                           (rad). Not used in this subroutine.
C region_count in     integer     scalar    The number of regions that
C                                           have been examined so far.
C rahalf       in     real*8      scalar    The halfwidth of the CCD (rad)
C                                           Not used in this subroutine.
C dechalf      in     real*8      scalar    The halfheight of the CCD (rad)
C                                           Not used in this subroutine.
C error        out    logical     scalar    Set TRUE if an error occurs
C
C Errors:
C
C The usual I/O errors can occur in this routine. If such an error
C occurs, the routine will set 'error' TRUE and return immediately.
C
C Routines accessed:
C
C lnblnk
C left_justify
C int2_qsort
C dble_qsort
C int_qsort
C err_msg
C 
      implicit none
      include 'std_io.for'
      include 'include_.for'
      logical error,round_aper,ascending
      real*8 ra,dec,ra_center,dec_center,list_distance,
     $     diff,list_angle,focallength,pixelsize,radius,
     $     rahalf,dechalf
      integer list_ra,list_dec,list_mag,list_length,i,order,
     $     luout,ll,ul,step,region_count,lnblnk,mag
      integer*2 magr,magb,cindex
      character*(1) sortfield
      character*(4) digit
      character*(64) outfilename,name,left_justify
      dimension list_ra(maxlist),list_dec(maxlist),list_mag(maxlist),
     $     list_distance(maxlist),order(maxlist),list_angle(maxlist),
     $     magr(maxlist),magb(maxlist),cindex(maxlist)
      round_aper=.false.
      focallength=0.0d0
      pixelsize=0.0d0
      radius=0.0d0
      rahalf=0.0d0
      dechalf=0.0d0
      do i=1,list_length
         ra=pio180*(dble(list_ra(i))/360000.0d0)
         dec=pio180*(dble(list_dec(i))/360000.0d0)
         diff=ra_center-ra
         list_distance(i)=dacos(dcos(diff)*
     $        dsin(dec_center)*dsin(dec)+
     $        dcos(dec_center)*dcos(dec))
         if (dabs(list_distance(i)) .lt. 1.0d-8) then
            list_angle(i)=0.0d0
         elseif ((dabs(dec_center-pi) .lt. 1.0d-8) .or.
     $           (dabs(dec_center) .lt. 1.0d-8)) then
            list_angle(i)=diff
         else 
            list_angle(i)=datan2(dsin(diff)*dsin(dec),
     $           (dcos(dec)-dcos(dec_center)*dcos(list_distance(i))))
         end if
         mag=mod(iabs(list_mag(i)),1000000)
         magr(i)=mod(mag,1000)
         magb(i)=mod(mag/1000,1000)
         cindex(i)=magb(i)-magr(i)
      end do
      if (list_length .eq. 1) then
         order(1)=1
      elseif (list_length .gt. 1) then
         if (sortfield .eq. 'b') then
            call int2_qsort(magb,order,list_length)
         elseif (sortfield .eq. 'd') then
            call dble_qsort(list_distance,order,list_length)
         elseif (sortfield .eq. 'a') then
            call dble_qsort(list_angle,order,list_length)
         elseif (sortfield .eq. 'R') then
            call int_qsort(list_ra,order,list_length)
         elseif (sortfield .eq. 'D') then
            call int_qsort(list_dec,order,list_length)
         elseif (sortfield .eq. 'i') then
            call int2_qsort(cindex,order,list_length)
         elseif (sortfield .eq. 'r') then
            call int2_qsort(magr,order,list_length)
         end if
      end if
      if (list_length .gt. 0) then
         if (outfilename .ne. ' ') then
            luout=204
            write(digit,43,err=403) region_count
 43         format(i4)
            digit=left_justify(digit)
            name=outfilename(1:lnblnk(outfilename))//
     $           digit(1:lnblnk(digit))
            open(unit=luout,
     $           file=name,
     $           access='direct',
     $           recl=12,
     $           form='unformatted',
     $           status='new',
     $           err=401)
            if (ascending) then
               ll=1
               ul=list_length
               step=1
            else
               ll=list_length
               ul=1
               step=-1
            end if
            do i=ll,ul,step
               write(unit=luout,
     $              rec=i,
     $              err=404) list_ra(order(i)),
     $              list_dec(order(i)),list_mag(order(i))
            end do
            close(unit=luout,err=402)
         else
            error=.true.
            call err_msg('make_cat_list',
     $           'A prototype output file name must be named.')
         end if
      end if
      goto 400
 401  error=.true.
      call err_msg('make_cat_list','Error opening output file.')
      goto 400
 402  error=.true.
      call err_msg('make_cat_list','Error closing output file.')
      goto 400
 403  error=.true.
      call err_msg('make_cat_list','Error in format conversion.')
      goto 400
 404  error=.true.
      call err_msg('make_cat_list','Error writing to output file.')
 400  continue
      if (error) then
         call err_msg('make_cat_list','Traceback')
      end if
      return
      end
