14 UNIX-style options

If you want other arguments, or a UNIX-like set of options for specifying arguments with defaults, you’ll need to use switch and shift statements, rather than the simple while loop.

Suppose that we want to obtain photometric details of a galaxy, known to be the brightest object in a series of NDFs. Before that’s possible we must first measure the background in each image. To reduce contamination that could bias the determination of the sky we mask the region containing the galaxy. Here we obtain the values for the shape, orientation, and the names of the NDFs, and assign them to shell variables. There are also default values.

       #  Initialise some shell variables, including the default shape
       #  and orientation.
       set major = 82
       set minor = 44
       set orient = 152
       set args = ($argv[1-])
       set ndfs
  
       #  Check that there are some arguments present.
       if ( $#args == 0 ) then
          echo "Usage: galmask [-a semi-major] [-b semi-minor] " \
                              "[-o orientation] ndf1 [ndf2...]"
          exit
       endif
  
       #  Process each of the arguments to the script.
       while ( $#args > 0 )
          switch ($args[1])
          case -a:        #  Semi-major axis
             shift args
             set major = $args[1]
             shift args
             breaksw
          case -b:        #  Semi-minor axis
             shift args
             set minor = $args[1]
             shift args
             breaksw
          case -o:        #  Orientation
             shift args
             set orient = $args[1]
             shift args
             breaksw
          case *:         #  The NDFs
             set ndfs = ($ndfs[1-] $args[1])
             shift args
             breaksw
          endsw
       end
  
       # Loop through the remaining arguments, assuming that these are NDFs.
       foreach file ($ndfs[*])
          :      :      :

Some defaults are defined so that if the argument is not present, there is a suitable value. So for instance, the ellipse orientation is 152°  unless there is a -o orientation-value on the command line when invoking the script.

The switch looks for specific arguments. If the first argument matches one of the allowed cases: -a, -b, -o, or any string (*) in that order, the script jumps to that case, and follows the commands there until it encounters the breaksw, and then the script moves to the endsw statement. You may be wondering why only the first element of the arguments is tested against the cases. This is where the shift statement comes in. This decrements the element indices of an array by one, so that $argv[1] is lost, $argv[2] becomes $argv[1], $argv[3] becomes $argv[2] and so on. If we look at the -a case. The arguments are shifted, so that the first argument is now the value of the semi-major axis. This in turn is shifted out of the arguments. The * case does the equivalent of the earlier while loop (see Section 7.6) as it appends the NDF filenames. The [1-] means the first element until the last.

To actually perform the masking we could write an ARD ellipse shape using the variables, and pipe it into a file, in this case biggal.ard.

       echo ’ELLIPSE( ’$centre[1]’, ’$centre[2]’, ’$major’, ’$minor’, ’$orient’ )’ \
            > biggal.ard

On the command line we could enter

       % galmask -o 145 -a 78.4 myndf ’ccd*o’

if the script was called galmask. This would set the semi-major axis to 78.4 pixels, the orientation to 145° , leaving the semi-minor axis at its default of 44 pixels. It would operate on the NDF called myndf and those whose names began with ccd and ended with o.

There is a related example in Section 17.1 where the ellipse axis lengths and orientation are fixed for two galaxies. The above options code could replace that recipe’s first two lines, if we wanted to alter the spatial parameters for the brighter galaxy to see which gives best results.

In $KAPPA_DIR/multiplot.csh you can find a complete example. For a series of NDFs, this produces a grid of displayed images with axes and titles, prints them to a nominated device, once each page is full. You can modify it for other operations, where a single page of graphics output is produced by running several tasks, and joining them together with PSMERGE (SUN/164).