NAME:
  xdspec
 PURPOSE:
  End-to-end reduction tool for OSIRIS XD spectral data.
 DESCRIPTION:

  This program handles all steps of processing raw OSIRIS XD spectral data
    to create final averaged output spectra.  This will work quite well for
    data on single point sources.  If the objects are extended or if there
    are more than one object, you need to do the reductions by hand.

 CATEGORY:
  Spectroscopy

 CALLING SEQUENCE:
  xdspec

 INPUTS:

 OPTIONAL INPUT PARAMETERS:

 KEYWORD INPUT PARAMETERS:
  Unless otherwise indicated, these options are all flags.

  ABSPOSCAL  - Run through the absolute position calibration step.

  ADDPATH    - String of directory name to prepend to the DIRLIST for a
                  location to look for the raw data.  The same restriction
                  applies to this as to DIRLIST.

  ALLCAL     - Run through all the calibration steps.

  ALLSPEC    - (Re)compute all spectral extractions.

  AVGSPEC    - Force (re)computation of final average spectra.

  DIRLIST    - String array list of directories for where to search for
                  raw data.  Do NOT include the final directory that is
                  the same as the current directory and the root of the
                  raw data files.

  FINAL      - Enable final spectral extraction, this step combines any
                  bad pixel masks and creates final mask (if not already
                  present) and then extracts all spectra.

  FLUSH_INFO - Flush all remembered information.

  GAUSSCOR- Correction factor from the directly computed image FWHM (per order)
               See OPTSPEC for a more detailed explanation.

  GROUPCHECK - Flag, generate a summary plot of all spectra against their
                  group leader.

  MASKTHRESH - When using a final mask image, you can adjust the mask
                  threshold with this keyword.  The mask image records the
                  fraction of the time that a given pixel is recorded as
                  being "bad".  This fraction varies between 0.0 and 1.0.
                  The default is to mark pixels bad that are seen to be bad
                  more than 4% of the time.  In other words, MASKTHRESH=0.04
                  is the default.

  NOSPEC     - Inhibit any spectral extraction processing.

  PLOTRANGE  - Two element vector, [start,stop], frame numbers to generate
                  postscript plots for.

  PLOTS      - How many and how much to plot.  (only for optspec plots)
                  0 - All plots generated.
                  1 - No plots at all.
                  2 - Show strip image, average profile, order profiles (default)

  QUEUE      - (String) Print queue to send postscript plots to.  If this
                  is not provided, then the idl.ps file is not spooled to
                  a printer and is left behind when xdspec finishes.  If you
                  give a queue name, the idl.ps file is deleted after the
                  file is spooled for printing.

  RELPOSCAL  - Run through the relative position calibration step.

  SLOPECAL   - Run through centerline slope calibration step.

  WAVECAL    - Run the wavelength calibration steps on each order.

 OUTPUTS:

  All output is either to the various plot screens or to files.

 KEYWORD OUTPUT PARAMETERS:

 COMMON BLOCKS:
  COM_XDSPEC - Used to store information from one invocation of xdspec to
                 the next.

 SIDE EFFECTS:

 RESTRICTIONS:

  If you use an 's' flag to eliminate an image and its spectrum from the
    output products, you will not necessarily get this image as a mate in
    cases where you might like it.  You will almost always need to tweak
    the mate in the .tbl file by hand in these cases.

 PROCEDURE:

  This program assumes (and requires) that the current directory is NOT where
    the raw frames are to be found.  The raw frames are in some other location
    and have no other files with them.  This allows you to have the raw data
    in a location where you do not have write permission (ie., CDROM).  All
    of the resulting files from running xdspec are written to the current
    directory and directories beneath there.

  Also, for simplicity of bookkeeping, I have assumed that the root of the
    raw data file names is the same as the directory where you are running
    xdspec and where the data live.  For example:
       Data files of the form:   980115.001
       Current directory:     /usr/results/980115/
       Data directory:        /usr/data/980115/
    In this case '/usr/data' should be in the DIRLIST or added with ADDPATH
    (if it isn't already present).

  If there are no flat field calibration frames on this night, you must use
    calibration specifications and flat fields from another night in that
    run.  In this case, as you start XDSPEC you get a flat field selection
    popup window.  Use this tool to browse through other nights until you
    find an appropriate calibration file.  In selecting the flat you are
    actually selecting that flat and its associated files.cal specification
    file.  In this case you probably shouldn't fiddle with _anything_ in
    the files.cal and if the original ever changes, you should probably start
    all over from scratch on any other nights that use the same stuff.
    You _can_ re-run the wavelength calibration steps in this case but it's
    probably not a good idea.  Once you select the flat field, processing
    continues and there's very little that needs to be done on the calibration
    steps (actually nothing at all).

  During the wavelength calibration steps, here are the mouse operation
   options:          (1st:sky spectrum)                 (2nd:synthetic)
          left      measure nearest line           grab nearest strong line
          middle    delete nearest point                   nothing
          right           done                         abort this line

  Here's the typical "flow" of running this program:
   xdspec        <-- Initial call, sets up calibration information, and
                        extracts initial spectra.  Run this as many times
                        as needed to get a complete set of initial spectra.

   xdspec,plotrange=[1,NNN],queue='printer name'
                     this will generate the summary plots for all the
                     spectra.  Do only when all are done.  You then review
                     the plots with an eye toward spectra that should be
                     removed from the averages.  You mark one to be skipped
                     by replacing the second column flag character
                     by an 'x' (to exclude).  After all have
                     been marked, proceed to the next step ...

   xdspec,/final  <-- Final processing, combines bad pixel maps, reextracts
                      the spectra and generates final averages.  You are done
                      with this program at this point.

   Note, you can ignore any spectrum in the tbl file by marking it with an
     'x' in the second column in the table.  It will do no good to delete the
     line from the table as the line will come back.  'x' marks it to be
     ignored.  If any extraction/reduction products exist for such a spectrum
     they will be deleted.

 MODIFICATION HISTORY:
  97/12/11, Written by Marc W. Buie, Lowell Observatory
  98/03/09, MWB, cleaned up logic needed when data are added to raw directory.
  98/04/01, MWB, added some generalization items, and ADDPATH, DIRLIST
  98/06, MWB, numerous changes to incorporate a numerical image profile for
            the optimal extraction on the FINAL pass.  Also added support
            for those cases where more than one data directory can be found.
  98/06/30, MWB, added user selection if data could be found in more than one
            directory.  Also, some logic was cleaned up that dealt with
            spectra marked to be ignored.
  98/08/25, MWB, changed the default plotting option, PLOTS=2 so that only
            three windows are automatically created and used.
  98/09/17, MWB, fixed a minor bookkeeping problem with 's' flagged frames.
            The output files from these frames were still hanging around and
            this has been changed so the output products are deleted.
  99/11/24, MWB, changed lclxtrem call to accomodate new version.
  2000/11/08, MWB, removed use of obsolete code constructs
  2002/09/03, MWB, changed Str_sep call to strsplit
  2003/10/01, MWB, converted to IDL file_mkdir calls