NAME: 
  cavgspec
 PURPOSE: 
  Robust average of a set of 1-D spectra.
 DESCRIPTION:
 CATEGORY:
  Spectroscopy
 CALLING SEQUENCE:
  cavgspec,specset,bad,avgspec,avgspec_sig,badavg
 INPUTS:
  specset - a 2-D array of spectra [pixel number,spectrum number]
  bad     - a 2-D array of badflags [pixel number,spectrum number]
 OPTIONAL INPUT PARAMETERS:
 KEYWORD INPUT PARAMETERS:
  SCALE - range of pixels to use for scaling all spectra to each other.
          The default is to use all pixels for scaling.
  REF   - Spectrum number to use as the scaling reference.  The scale
          factors are determined relative to this one.

 OUTPUTS:
  avgspec - Final averaged spectrum.
  avgspec_sig - Uncertainties of the averaged spectrum.
  badavg  - Bad flags for final average spectrum.
 KEYWORD OUTPUT PARAMETERS:
  SCFACTOR  - Vector of relative scaling factors for each spectrum.
 COMMON BLOCKS:
 SIDE EFFECTS:
 RESTRICTIONS:
 PROCEDURE:
 MODIFICATION HISTORY:
  98/07/17 - Written by Chris Dalla Piazza, Lycoming College.  Extracted from
             the original avgspec program
  98/07/17 - Added denominator filter for the sf part.  Indices were also
             backwards for the badflags in that section.
  98/08/17, MWB, added return of bad flags on the average.
  2000/10/1, MWB, added avgspec_sig return