      subroutine command_line(round_aper,density,focallength,infilename,
     $     himaglim,lomaglim,magtype,out_style,outfilename,batch,
     $     pixelsize,ascending,sortfield,catmap,error)
C
C Description:
C
C This routines gets all the parameters from the command line and
C interprets them for astronet. 
C
C System considerations:
C
C This routine is specific to UNIX systems. If you are not running a 
C UNIX system, then you will have to rewrite this routine to take
C advantage of your system's command line interface. If you are on
C a UNIX system but your FORTRAN does not have command line access
C routines (iargc and getarg) then your will have to rewrite this 
C routine. A quick and dirty fix for this routine is to hardcode values
C into all the output parameters and recompile each time you want to do 
C something differently.
C
C Programming interface:
C
C Name         In/out Type          Structure Meaning
C
C round_aper   out    logical       scalar    TRUE if a round aperture is
C                                             requested
C density      out    real*8        scalar    The maximum number of stars 
C                                             per square
C                                             degree allowed
C focallength  out    real*8        scalar    The focal length of the simulated
C                                             telescope requested for an IRAF 
C                                             star list    
C infilename   out    character*64  scalar    The name of the file containing 
C                                             the list of regions to be 
C                                             searched
C himaglim     out    real*8        scalar    The dim magnitude limit
C lomaglim     out    real*8        scalar    The bright magnitude limit
C magtype      out    character*1   scalar    The way magnitude is considered
C                                             'r' is red
C                                             'b' is blue
C                                             'd' is the difference between red
C                                             and blue
C out_style    out    character*1   scalar    The style of the list for stars
C                                             'a' is ASCII
C                                             'i' is IRAF
C                                             'c' is catalog
C outfilename  out    character*64  scalar    The name of the file containing
C                                             the list of found stars 
C batch        out    logical       scalar    TRUE if batch mode is selected
C                                             FALSE if interactive mode is 
C                                             selected
C pixelsize    out    real*8        scalar    The focal length of the simulated
C                                             telescope requested for IRAF     
C                                             star list                        
C ascending    out    logical       scalar    TRUE if the found star list is
C                                             to be sorted in ASCENDING order
C                                             FALSE for decending order
C sortfield    out    character*1   scalar    Selects the parameter that
C                                             will order the found stars
C                                             'R' is RA
C                                             'D' is Decl.
C                                             'r' is red magnitude
C                                             'b' is blue magnitude
C                                             'd' is radius from aperture cent.
C                                             'a' is aperture angle from North 
C                                             'i' is color index (b - r)
C catmap       out    character*64  scalar    Name of the catalog map file
C error        out    logical       scalar    TRUE if an error has occured
C
C User interface:
C
C The user interface is generally described in the 'man' page for astronet. 
C Even more esoteric details are available in the man page for the shell 
C you are using.
C
C Errors:
C
C This routine is disigned to verify that the user-supplied values in
C the command line are valid. Any switch that is not a supported switch
C will be considered an error. Any invalid argument to a switch will be
C condidered an error. Certain combinations of switches must be used
C together. It is considered an error if the switch combinations are
C not properly set. When switches are set that are irrelavant, a warning
C message is presented to the user.
C
C Subroutines called:
C
C iargc
C err_msg
C stat_msg
C iraf_warning
C find_arg
C is_fp_num
C is_valid_switch
C is_valid_switch_arg
C
      implicit none
      include 'std_io.for'
      include 'include_.for'
      real*8 density,focallength,himaglim,lomaglim,pixelsize
      integer count,i,nargs
      logical error,round_aper,batch,ascending,done,is_valid_switch,
     $     is_valid_switch_arg
      character*64 infilename,outfilename,arg,argos,find_arg,catmap
      character*1 magtype,out_style,sortfield
      count=nargs()
      count=count-1
C
C Check command line syntax. All switches require arguments. 
C Some arguments are numbers, some are filenames, and some are 
C a small set of characters.
C
      i=1
      done=.false.
      do while ((i .le. count) .and. (.not. done))
         if (is_valid_switch(i)) then
            i=i+1
            if (is_valid_switch_arg(i)) then
               i=i+1
            else
               done=.true.
               error=.true.
               call err_msg('command_line',
     $              'Encountered an invalid command line argument.')
            end if
         else
            done=.true.
            error=.true.
            call err_msg('command_line',
     $           'Encountered an invalid command line switch.')
         end if
      end do
      if (.not. error) then
C     
C Set all values without dependencies. Use defaults if command line 
C switch is not available. Since the syntax check was thorough, this
C code segment can be quick and dirty.
C
         arg=find_arg('-cm')
         if (arg .ne. ' ') then
            catmap=arg
         else
            catmap='catmap.dat'
         end if
         arg=find_arg('-so')
         if (arg .ne. ' ') then
            ascending=(arg .eq. 'a')
         else
            ascending=.true.
         end if
         arg=find_arg('-pr')
         if (arg .ne. ' ') then
            batch=(arg .eq. 'b')
         else
            batch=.false.
         end if
         arg=find_arg('-ot')
         outfilename=arg
         arg=find_arg('-mt')
         if (arg .ne. ' ') then
            magtype=arg
            if (arg .eq. 'rb') then
               magtype='d'
            end if
         else
            magtype='b'
         end if
         arg=find_arg('-mh')
         if (arg .ne. ' ') then
            call str2r8(arg,himaglim,error)
            if (error) goto 400
         else
            himaglim=30.0d0
         end if
         arg=find_arg('-ml')
         if (arg .ne. ' ') then
            call str2r8(arg,lomaglim,error)
            if (error) goto 400
         else
            lomaglim=-5.0d0
         end if
         arg=find_arg('-in')
         infilename=arg
         arg=find_arg('-dn')
         if (arg .ne. ' ') then
            call str2r8(arg,density,error)
            if (error) goto 400
         else
            density=1.0d9
         end if
C
C Now handle the part with dependencies.
C
         arg=find_arg('-ap')
         if (batch) then
            if (arg .eq. 'c') then
               call stat_msg('command_line',
     $              'a round aperture cannot be '//
     $              'used in the batch mode. '//
     $              'using a rectangular aperture.')
            end if
            round_aper=.false.
         else
            if (arg .ne. ' ') then
               round_aper=(arg .eq. 'c')
            else
               round_aper=.true.
            end if
         end if
         arg=find_arg('-st')
         if (arg .ne. ' ') then
            sortfield=arg(1:1)
            if (arg .eq. 'rad') then
               sortfield='d'
            end if
         else
            if (batch) then
               sortfield='r'
            else
               sortfield='d'
            end if
         end if
         argos=find_arg('-os')
         if (argos .eq. ' ') then
            call iraf_warning()
            out_style='a'
         else
            if (argos .eq. 'iraf') then
               arg=find_arg('-fl')
               if (arg .ne. ' ') then
                  call str2r8(arg,focallength,error)
                  if (error) goto 400
                  arg=find_arg('-ps')
                  if (arg .ne. ' ') then
                     call str2r8(arg,pixelsize,error)
                     if (error) goto 400
                     out_style='i'
                  else
                     error=.true.
                     call err_msg('command_line',
     $                    'the pixel size must be specified using '//
     $                    'the -ps switch when iraf output is '//
     $                    'selected.')
                  end if
               else
                  error=.true.
                  call err_msg('command_line',
     $                 'the focal length must be specified using '//
     $                 'the -fl switch when iraf output is selected.')
               end if
            else
               call iraf_warning()
               if (argos .eq. 'cat') then
                  if (outfilename .eq. ' ') then
                     call err_msg('command_line',
     $                    'the output file name must be specified '//
     $                    'using the -ot switch when catalog '//
     $                    'output is selected.')
                     error=.true.
                  else
                     out_style='c'
                  end if
               elseif (argos .eq. 'ascii') then
                  out_style='a'
               end if
            end if
         end if
      end if
      goto 400
 401  error=.true.
      call err_msg('command_line','Internal error in type conversion.')
 400  continue
      if (error) then
         call err_msg('command_line','Traceback')
      end if
      return
      end
