NAME:
  lclxtrem

 PURPOSE:
  Find local minima or maxima in a 1-d vector.

 DESCRIPTION:
  Scan a vector and identify all maxima or minima separated by more than
    WIDTH values.

 CATEGORY:
  Spectroscopy

 CALLING SEQUENCE:
  idx=lclxtrem(vec,width,[/MAXIMA])

 INPUTS:
  vec - Input vector of data points.

 OPTIONAL INPUT PARAMETERS:
  width - size of zone to search, minima (or maxima) separated by less than
            width are never returned.  (Default = 5)

 KEYWORD INPUT PARAMETERS:
  MAXIMA  - Flag, if set, causes program to search for local maxima, the default
            is to search for local minima

 OUTPUTS:
  Returns indicies into vec that give local extrema.  This returned vector
    is also sorted by decreasing absolute value of vec.

 KEYWORD OUTPUT PARAMETERS:
  COUNT  - Number of extrema returned.  If 0, there was an error.

 COMMON BLOCKS:

 SIDE EFFECTS:

 RESTRICTIONS:
  The first and last points in the array will NEVER be flagged as extrema
    since there is no information to decide if the endpoint is an inflection
    or not.  There is one exception to this condition.  If the data are
    smooth and there are no inflection points, one or the other of the
    endpoints will be the absolute extreme.  In this case only, will an
    endpoint be returned.

 PROCEDURE:
  This procedure scans the vector and looks for local maxima or local minima.
  In the first pass, all such extrema are flagged, if you want all of them
  Just set width to 0 if you want _all_ local extrema.  The second pass
  filters our extrema that are too close together.  The width of the filter
  can be provided, the default is 5.  The extrema are scanned in decreasing
  absolute value.  For each extremum, all other extrema that are fainter and
  are closer than width away are removed from the list.  The returned vector
  of indicies is sorted by decreasing size of the peak/trough.

 MODIFICATION HISTORY:
  97/12/5, Written by Marc W. Buie, Lowell Observatory
  99/11/24, MWB, total rewrite to speed it up and make it work properly.