      subroutine make_ascii_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 an ASCII file/stdout
C in deg/hr min sec format.
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                                           (rad*100) 
C list_dec     in     integer     (maxlist) The list of decls for the stars
C                                           (rad*100) 
C list_mag     in     integer     (maxlist) The list of magnitudes for the 
C                                           stars (The 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 focallength  in     real*8      scalar    Focal length of telescope. Not used
C                                           in this routine
C pixelsize    in     real*8      scalar    Pixel size of CCD chip. Not used
C                                           in this routine
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                                           'a' is angle
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)
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 routine
C dechalf      in     real*8      scalar    The halfheight of the CCD (rad)
C                                           Not used in this routine
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 upcase_str
C int2_qsort
C dble_qsort
C int_qsort
C ascii_header
C err_msg
C
      implicit none
      include 'std_io.for'
      include 'include_.for'
      logical error,round_aper,ascending,done,finished
      real*8 ra,dec,ras,decs,ra_center,dec_center,list_distance,
     $     diff,list_angle,angle,focallength,pixelsize,radius,
     $     red,blue,rahalf,dechalf
      integer list_ra,list_dec,list_mag,list_length,i,order,rah,ram,
     $     decd,decm,luout,ll,ul,step,linecount,region_count,lnblnk,
     $     mag
      integer*2 magr,magb,cindex
      character*(1) sortfield
      character*(4) digit
      character*(64) outfilename,dummy,name,left_justify,upcase_str
      dimension list_ra(maxlist),list_dec(maxlist),list_mag(maxlist),
     $     list_distance(maxlist),order(maxlist),list_angle(maxlist),
     $     magr(maxlist),magb(maxlist),cindex(maxlist)
      focallength=0.0d0
      pixelsize=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,21,err=403) region_count
 21         format(i4)
            digit=left_justify(digit)
            name=outfilename(1:lnblnk(outfilename))//
     $           digit(1:lnblnk(digit))
            open(unit=luout,
     $           file=name,
     $           err=401)
         else
            luout=stdout
         end if
         call ascii_header(luout,error)
         if (error) goto 400
         if (ascending) then
            ll=1
            ul=list_length
            step=1
         else
            ll=list_length
            ul=1
            step=-1
         end if
         linecount=-1
         done=.false.
         do i=ll,ul,step
            if (.not. done) then
            if (((list_distance(order(i)) .lt. radius)
     $           .and. (round_aper)) .or. (.not. round_aper)) then
               if (outfilename .eq. ' ') then
                  linecount=linecount+1
                  if ((mod(linecount,19) .eq. 0) .and.
     $                 (linecount .gt. 1)) then
                     write(luout,6,err=405) 
 6                   format(' Depress ''<ret>'' to continue',
     $                    ' or ''q <ret>'' to quit.')
                     read(stdin,7,err=404) dummy
 7                   format(a)
                     if (index(upcase_str(dummy),'Q') .ne. 0) then
                        done=.true.
                     else
                        call ascii_header(luout,error)
                        if (error) goto 400
                     end if
                  end if
               end if
               if (.not. done) then
                  finished=.false.
                  ra=(dble(list_ra(order(i)))/360000.0d0)/15.0d0
                  do while (.not. finished)
                     rah=int(ra)
                     ram=int(60.0d0*(ra-dble(rah)))
                     ras=3600.0d0*(ra-dble(rah)-dble(ram)/60.0d0)
                     if (dmod(ras,1.0d0) .ge. 0.9995d0) then
                        ra=ra+1.0d-4/(15.0d0*3600.0d0)
                     else
                        finished=.true.
                     end if
                  end do
                  finished=.false.
                  dec=-90.0d0+dble(list_dec(order(i)))/360000.0d0
                  do while (.not. finished)
                     decd=iabs(int(dec))
                     decm=int(60.0d0*(dabs(dec)-dble(decd)))
                     decs=3600.0d0*(dabs(dec)-
     $                    dble(decd)-dble(decm)/60.0d0)
                     if (dmod(decs,1.0d0) .ge. 0.995d0) then
                        dec=dec+1.0d-3/(3600.0d0)
                     else
                        finished=.true.
                     end if
                  end do
                  diff=3600.0d0*list_distance(order(i))/pio180
                  angle=(pi+list_angle(order(i)))/pio180
                  red=dble(magr(order(i)))/10.0d0
                  blue=dble(magb(order(i)))/10.0d0
                  if (dec .ge. 0.0d0) then
                     write(luout,1,err=405) rah,ram,int(ras),
     $                    dmod(ras,1.0d0),
     $                    decd,decm,int(decs),dmod(decs,1.0d0),red,
     $                    blue,diff,angle
 1                   format(' ',i2.2,' ',i2.2,' ',
     $                    i2.2,f4.3,'  +',i2.2,
     $                    ' ',i2.2,' ',i2.2,f3.2,'   ',f4.1,'  ',f4.1,
     $                    '  ',f7.1,'     ',f6.1)
                  else
                     write(luout,2,err=405) rah,ram,int(ras),
     $                    dmod(ras,1.0d0),
     $                    decd,decm,int(decs),dmod(decs,1.0d0),red,
     $                    blue,diff,angle
 2                   format(' ',i2.2,' ',i2.2,' ',
     $                    i2.2,f4.3,'  -',i2.2,
     $                    ' ',i2.2,' ',i2.2,f3.2,'   ',f4.1,'  ',f4.1,
     $                    '  ',f7.1,'     ',f6.1)
                  end if
               end if
            end if
            end if
         end do
      else
         write(luout,*,err=405)
     $        'No stars found within the search region.'
      end if
      if (outfilename .ne. ' ') then
         close(unit=luout,err=402)
      end if
      goto 400
 401  error=.true.
      call err_msg('make_ascii_list','Error opening output file.')
      goto 400
 402  error=.true.
      call err_msg('make_ascii_list','Error closing output file.')
      goto 400
 403  error=.true.
      call err_msg('make_ascii_list','Error in format conversion.')
      goto 400
 404  error=.true.
      call err_msg('make_ascii_list','Error in format conversion.')
      goto 400
 405  error=.true.
      call err_msg('make_ascii_list','Error writing to output file.')
 400  continue
      if (error) then
         call err_msg('make_ascii_list','Traceback')
      end if
      return
      end
