      subroutine bmain(round_aper,density,focallength,infilename,
     $     himaglim,lomaglim,magtype,outstyle,outfilename,
     $     pixelsize,ascending,sortfield,catmap,error)
C     
C Description:
C     
C This program is the main routine controlling the batch mode
C of the asternet program.
C
C Programming interface:
C
C Name         In/out  Type Structure Meaning
C     
C round_aper   in      logical        TRUE if the search aperture is circular
C                                     FALSE if rectangular
C density      in      real*8         Maximum number of stars per square deg.
C focallength  in      real*8         Focal length of telescope
C infilename   in      character*64   Name of region specification file.
C himaglim     in      real*8         Dim star limit
C lomaglim     in      real*8         Bright star limit
C magtype      in      character*1    Magnitude type. 
C                                     Uses preceeding mag limits.
C                                     'r' is red
C                                     'b' is blue
C                                     'd' is difference (blue - red)
C outstyle     in      character*1    Format for results
C                                     'a' is ASCII
C                                     'i' is IRAF
C                                     'c' is catalog
C outfilename  in      character*64   Prototype name of results file
C pixelsize    in      real*8         Pixel size of CCD. (Same units as
C                                     'focallength'
C ascending    in      logcial        TRUE if sort order is ascending
C sortfield    in      character*1    Selects which result parameter is to
C                                     be sorted
C                                     'R' is RA
C                                     'D' is Decl.
C                                     'd' is distance from aperture center
C                                     'b' is blue magnitude
C                                     'r' is red magnitude
C                                     'a' is aperture angle from North 
C                                     'i' is color index (blue - red) 
C capmap       in      character*64   Name of the catalog map file.
C error        out     logical        TRUE if an error occurs
C
C Errors:
C
C This routine acts as the interface to the region specification file.
C As such, it detects errors in the user supplied region specification
C list. There can be a variety of errors. This routine attempts to 
C detect them all but the variety of user supplied errors is limitless
C so, no doubt, some have been missed. In any case, when an error is
C detected, an "informative" error message is supplied, the routine
C sets 'error' TRUE, and then the routine immediately terminates.
C Also, if the subroutines that find the requested stars or the output
C subroutines return an error, this routine terminates immediately.
C
C Routines accessed
C
C get_next_token
C err_msg
C find_stars
C make_ascii_list
C make_iraf_list
C make_cat_list
C downcase_str
C left_justify
C
      implicit none
      include 'std_io.for'
      include 'include_.for'
      real*8 density,focallength,himaglim,lomaglim,pixelsize,
     $     rasign,rah,ram,ras,decsign,decd,decm,decs,rahalf,
     $     dechalf,ra,dec,radius
      integer inlu,lnblnk,bptr,limit,ptr,fnblnk,region_count,
     $     list_ra,list_dec,list_mag,list_length
      logical error,round_aper,ascending,done
      character*64 infilename,outfilename,token,downcase_str,
     $     left_justify,delimiters,catmap
      character*80 instr
      character*1 magtype,outstyle,sortfield
      dimension list_ra(maxlist),list_mag(maxlist),list_dec(maxlist)
      if (infilename .eq. ' ') then
         inlu=stdin
      else
         inlu=206
         open(unit=inlu,
     $        file=infilename,
     $        access='sequential',
     $        err=401,
     $        status='old')
      end if
      done=.false.
      do while (.not. done)
         read(unit=inlu,fmt=1,end=501,err=402) instr
 1       format(a)
         delimiters=' '
         bptr=1
         limit=lnblnk(instr)+1
         call get_next_token(instr,bptr,limit,token,delimiters)
         if (index(token,'.') .eq. 0) then
            ptr=lnblnk(token)
            token(ptr+1:ptr+1)='.'
         end if
         ptr=fnblnk(token)
         if (token(ptr:ptr) .eq. '-') then
            rasign=-1.0d0
            token(ptr:ptr)=' '
         else
            rasign=1.0d0
         end if
         call str2r8(token,rah,error)
         if (error) goto 400
         call get_next_token(instr,bptr,limit,token,delimiters)
         if (index(token,'.') .eq. 0) then
            ptr=lnblnk(token)
            token(ptr+1:ptr+1)='.'
         end if
         if (index(token,'-') .ne. 0) then
            error=.true.
            call err_msg('bmain',
     $           'A sign is not allowed on RA minutes.')
         end if
         call str2r8(token,ram,error)
         if (error) goto 400
         call get_next_token(instr,bptr,limit,token,delimiters)
         if (index(token,'.') .eq. 0) then
            ptr=lnblnk(token)
            token(ptr+1:ptr+1)='.'
         end if
         if (index(token,'-') .ne. 0) then
            error=.true.
            call err_msg('bmain',
     $           'A sign is not allowed on RA seconds.')
         end if
         call str2r8(token,ras,error)
         if (error) goto 400
         call get_next_token(instr,bptr,limit,token,delimiters)
         if (index(token,'.') .eq. 0) then
            ptr=lnblnk(token)
            token(ptr+1:ptr+1)='.'
         end if
         ptr=fnblnk(token)
         if (token(ptr:ptr) .eq. '-') then
            decsign=-1.0d0
            token(ptr:ptr)=' '
         else
            decsign=1.0d0
         end if
         call str2r8(token,decd,error)
         if (error) goto 400
         call get_next_token(instr,bptr,limit,token,delimiters)
         if (index(token,'.') .eq. 0) then
            ptr=lnblnk(token)
            token(ptr+1:ptr+1)='.'
         end if
         if (index(token,'-') .ne. 0) then
            error=.true.
            call err_msg('bmain',
     $           'A sign is not allowed on Decl. minutes.')
         end if
         call str2r8(token,decm,error)
         if (error) goto 400
         call get_next_token(instr,bptr,limit,token,delimiters)
         if (index(token,'.') .eq. 0) then
            ptr=lnblnk(token)
            token(ptr+1:ptr+1)='.'
         end if
         if (index(token,'-') .ne. 0) then
            error=.true.
            call err_msg('bmain',
     $           'A sign is not allowed on Decl. seconds.')
         end if
         call str2r8(token,decs,error)
         if (error) goto 400
         call get_next_token(instr,bptr,limit,token,delimiters)
         if (index(token,'.') .eq. 0) then
            ptr=lnblnk(token)
            token(ptr+1:ptr+1)='.'
         end if
         call str2r8(token,rahalf,error)
         if (error) goto 400
         rahalf=pio180*rahalf/3600.0d0
         if ((rahalf .lt. 0.0d0) .or. (rahalf .gt. pi/4.0d0)) then
            error=.true.
            call err_msg('lmain',
     $           'The RA half-width must be between 0 and 45 deg.')
         end if
         call get_next_token(instr,bptr,limit,token,delimiters)
         if (index(token,'.') .eq. 0) then
            ptr=lnblnk(token)
            token(ptr+1:ptr+1)='.'
         end if
         call str2r8(token,dechalf,error)
         if (error) goto 400
         dechalf=pio180*dechalf/3600.0d0
         if ((dechalf .lt. 0.0d0) .or. (dechalf .gt. pi/4.0d0)) then
            error=.true.
            call err_msg('lmain',
     $           'The Decl. half-width must be between 0 and 45 deg.')
         end if
         call get_next_token(instr,bptr,limit,token,delimiters)
         if (index(token,'.') .eq. 0) then
            ptr=lnblnk(token)
            token(ptr+1:ptr+1)='.'
         end if
         call str2r8(token,lomaglim,error)
         if (error) goto 400
         if ((lomaglim .lt. -5.0d0) .or. (lomaglim .gt. 30.0d0)) then
            call err_msg('bmain',
     $           'The low magnitude limit must be between -5 and 30.')
         endif
         call get_next_token(instr,bptr,limit,token,delimiters)
         if (index(token,'.') .eq. 0) then
            ptr=lnblnk(token)
            token(ptr+1:ptr+1)='.'
         end if
         call str2r8(token,himaglim,error)
         if (error) goto 400
         if ((himaglim .lt. -5.0d0) .or. (himaglim .gt. 30.0d0) .or.
     $        (lomaglim .gt. himaglim)) then
            call err_msg('bmain',
     $           'The high magnitude limit must be between'//
     $           ' -5 and 30 and greater than the low mag limit.')
         endif
         call get_next_token(instr,bptr,limit,token,delimiters)
         if (index(token,'.') .eq. 0) then
            ptr=lnblnk(token)
            token(ptr+1:ptr+1)='.'
         end if
         magtype=downcase_str(left_justify(token))
         if (lnblnk(magtype) .eq. 1) then
            if ((magtype .eq. 'b') .or. (magtype .eq. 'r') .or.
     $           (magtype .eq. 'd')) then
            else
               error=.true.
               call err_msg('bmain','Unknown magnitude type.')
            end if
         else
            error=.true.
            call err_msg('bmain','Unknown magnitude type. Too long.')
         end if
         call get_next_token(instr,bptr,limit,token,delimiters)
         if (index(token,'.') .eq. 0) then
            ptr=lnblnk(token)
            token(ptr+1:ptr+1)='.'
         end if
         call str2r8(token,density,error)
         if (error) goto 400
         if (density .lt. 0.0d0) then
            call err_msg('bmain','Density must be positive.')
            error=.true.
         end if
         ra=rasign*(rah+ram/60.0d0+ras/3600.0d0)*15.0d0*pio180
         if ((ra .lt. 0.0d0) .or. (ra .gt. twopi)) then
            error=.true.
            call err_msg('bmain','RA must lie between 0 and 24 hr.')
         end if
         dec=decsign*(decd+decm/60.0d0+decs/3600.0d0)*pio180+pio2
         if ((dec .lt. 0.0d0) .or. (dec .gt. pi)) then
            error=.true.
            call err_msg('bmain',
     $           'Decl. must lie between -90 and 90 deg.')
         end if
         if (.not. error) then
            region_count=region_count+1
            call find_stars(ra,dec,rahalf,dechalf,
     $           lomaglim,himaglim,magtype,density,
     $           list_ra,list_dec,list_mag,list_length,catmap,error)
            if (error) goto 400
            if (outstyle .eq. 'a') then
               call make_ascii_list(list_ra,list_dec,list_mag,
     $              list_length,dec,ra,round_aper,focallength,
     $              pixelsize,ascending,sortfield,outfilename,
     $              radius,region_count,rahalf,dechalf,error)
               if (error) goto 400
            elseif (outstyle .eq. 'i') then
               call make_iraf_list(list_ra,list_dec,list_mag,
     $              list_length,dec,ra,round_aper,focallength,
     $              pixelsize,ascending,sortfield,outfilename,
     $              radius,region_count,rahalf,dechalf,error)
               if (error) goto 400
            elseif (outstyle .eq. 'c') then
               call make_cat_list(list_ra,list_dec,list_mag,
     $              list_length,dec,ra,round_aper,focallength,
     $              pixelsize,ascending,sortfield,outfilename,
     $              radius,region_count,rahalf,dechalf,error)
               if (error) goto 400
            end if
         end if
      end do
 501  if (infilename .ne. ' ') then
         close(unit=inlu,err=403)
      end if
      goto 400
 401  error=.true.
      call err_msg('imain','Error opening region specification file.')
      goto 400
 402  error=.true.
      call err_msg('imain','Error reading region specification.')
      goto 400
 403  error=.true.
      call err_msg('imain','Error closing region specification file.')
      goto 400
 404  error=.true.
      call err_msg('imain','Error reading RA hours.')
      goto 400
 405  error=.true.
      call err_msg('imain','Error reading RA minutes')
      goto 400
 406  error=.true.
      call err_msg('imain','Error reading RA seconds')
      goto 400
 407  error=.true.
      call err_msg('imain','Error reading Decl. degrees.')
      goto 400
 408  error=.true.
      call err_msg('imain','Error reading Decl. minutes')
      goto 400
 409  error=.true.
      call err_msg('imain','Error reading Decl. seconds')
      goto 400
 410  error=.true.
      call err_msg('imain','Error reading RA half-width')
      goto 400
 411  error=.true.
      call err_msg('imain','Error reading Decl. half-width')
      goto 400
 412  error=.true.
      call err_msg('imain','Error reading low magnitude limit')
      goto 400
 413  error=.true.
      call err_msg('imain','Error reading high magnitude limit')
      goto 400
 414  error=.true.
      call err_msg('imain','Error reading density')
      goto 400
 400  continue
      if (error) then
         call err_msg('bmain','Traceback')
      end if
      return
      end
