Identifier   sws_rebin

 Purpose      Rebin an AAR onto a predefined grid or on a grid
              defined by a constant resolution or constant binsize

 Synopsis     aar = sws_rebin( aar_in [, ref]
                                      [, resolution=resol]
                                      [, binsize=binsize]
                                      [, oversample=oversample]
                                      [, xrange=xrange]
                                      [, aot_number=aot_number]
                                      [, aot_speed=aot_speed]
                                      [, aot_band=aot_band]
                                      [, lines=lines]
                                      [, scan=scan]
                                      [, det=det]
                                      [, /merge]
                                      [, method=method]
                                      [, minbin=minbin]
                                      [, /nosmooth]

 Arguments    name      I/O type      description
              --------------------------------------------------------------
              aar        O  struct    Rebinned spectrum in AAR format
              aar_in     I  struct    Input AAR
              ref        I  struct    Reference AAR containing an
                                      'oversampled' wavelength grid.
                                      SWS_REBIN checks on the definition
                                      of AOT-bands, LINE and SCAN
              resolution I  fltarr    Resolution to rebin the data to.
                                      The defaults are resp. for 
                                      AOT 1 speed 1/2 3 4, AOT2/AOT6 or AOT7
                                      default.
                                      [default: 300/500/1000/1500/20000] 
              binsize    I  fltarr    Constant binsize to which the AAR is
                                      rebinned. Not used when RESOLUTION
                                      has a nonzero value.
                                      [No default]
              oversample I  int       Oversample the spectrum.
                                      [default: 4]
              xrange     I  fltarr    Full wavelength ranges of aot-bands as
                                      in the reference aar or the data itself
              aot_number I  int       Read from the AAR header.
                                      On error AOT_NUMBER 1 is assumed.
                                      [Default: yes]
              aot_speed  I  int       Determined from the header.
                                      On error AOT_SPEED 4 is assumed
                                      for AOT 1 else AOT_SPEED 0.
                                      [Default: yes]
              aot_band   I  strarr    AOT-bands to rebin
                                      Specifing 'NONE' will rebin the
                                      full spectrum in one (usefull e.g.
                                      for AAR's in which the status and
                                      flag are undefined)
                                      [Default: all aot-bands]
              lines      I  intarr    Lines, as defined in the AAR line tag.
                                      LINES=0 combines all lines in a band
                                      and assumes SCAN=0
                                      [Default: all]
              scans      I  intarr    Scans, as defined in the AAR scnt tag.
                                      SCAN=0 combines all scans.
                                      [Default: all]
              det        I  intarr    Detectors to use.
              /merge     I  keyword   The data is first rebinned for 
                                      each AOT-band/line, after which
                                      ovelapping regions are averaged. 
              method     I  string    Method used to determine the
                                      values in the rebinned spectrum.
                                      'FLUXCON'  : flux conserving rebinning
                                      'MEAN'     : plain average rebinning
                                      'WMEAN'    : weighted mean rebinning
                                      'MEDIAN'   : median binning
                                      [default: 'FLUXCON']
              weight     I string     Defines how the weighting for the
                                      weighted mean rebinning should be done.
                                       's' standard deviations (1/s^2)
                                       't' integration time (sqrt(t_int))
                                       'g' gaussian distribution with distance
                                           of center of bin and fwhm of res
                                      When combining options enter e.g. 'st'
                                      [default: 's']
              minbin     I int        minimum number of points required
                                      [Default: 3]
              nosmooth   I keyword    Do not smooth the data with a 
                                      gaussian of FWHM of the RESOLUTION
                                      (only relevant with the FLUXCON option)
              noplot     I keyword    Do not plot the spectrum.

 Returns      Rebinned spectrum in the form of an AAR

 Description  SWS_REBIN will rebin the input data onto a grid either
              specified by the user or to a fixed resolution or binsize.
              Only data not flagged as NODATA is used.

              For resolution bins for which there is no data a warning
              is issued. The values for these points are replaced by
              interploated values and the FLAG is set to NODATA.

              Default all AOT-band's, lines and scans are treated seperately.
              When using /MERGE all AOT-bands/lines/scans are first
              rebinned seperately and then combined afterwards. 
               AOT_BAND = 'NONE' will rebin all data
               LINES    = 0      will combine all lines and scans
               SCANS    = 0      will combine all scans

              WMEAN gives a weighted average rebinned spectrum.
              The weighting can be a combination of three items;
                's' the standard deviation (1/(stdev^2))
                't' integration time (sqrt(t_int))
                'g' gaussian smoothing (with fwhm of the resolution)
              The applied weight is the multiplication of the
              individual weights. To use this enter a combination of
              the characters, e.g. WEIGHT='s' (default) or WEIGHT='stg'.
              Adding a character more times will add its weight 
              accordingly, e.g. WEIGHT='tt' or 'stt' will add the 
              weight for the integration time twice making the 
              effective weight for the integration time t_int instead 
              of sqrt(t_int).

              FLUXCON implies flux conserving rebinning. For each bin
              the flux value is calculated using the trapezoidal rule.
              When using FLUXCON, the data is smoothed with a gaussian 
              with a FWHM of the resolution. /NOSMOOTH disables this.

              Setting the IA error level to 'D' (debug) will give
              the user information on e.g. the wavelength of the bin
              and the number of points in the bin.

              Plotted is the input data for each AOT-band/line and 
              and the rebinned spectrum.

 Examples     
              It is possible to combine resolution and binsize
              to rebin one band to a constant resolution and
              another to a constant binsize, e.g:
                   aotbands=['1a','1b','1d','1e']
                   resol   =[2000,1500,0    ,1200]
                   binsize =[0   ,0   ,0.003,0   ]
                  
                   aar_reb=sws_rebin(aar, aot_band=aotbands, $
                                     resol=resol, binsize=binsize)


 Comments    

 Category     UTIL

 Filename     sws_rebin.pro (module: sws_rebin)

 Version      2.9

 History      1.0  08-02-1996 FL  document created
              1.1  06-03-1996 FL  error in auto range
              1.2  25-03-1996 FL  range->xrange, improved in speed
                   02-04-1996 FL  variable 'wave' replaced by 'waves'
              1.3  10-05-1996 FL  rebin per line/aotband, add sigma 
                                  clipping and flux conserving rebinning
              1.4  14-05-1996 FL  proper date in history for v1.3, 
                                  improved procedures
              1.5  14-05-1996 FL  ignore line tag, removed small bugs
              1.6  15-05-1996 FL  error in sigma clipping, problems with
                                  using INT_TABULATED -> call replaced
              1.7  17-05-1996 FL  checked plotrange, option 'none'
                                  and check on NODATA
              2.0  10-06-1996 FL  SIGCLIP routine extracted, Gaussian
                                  smoothing with FWHM of the res. bin.
              2.1  17-06-1996 DRB not_enoug(0) -> not_enough(0)
              2.2  04-07-1996 FL  return noise in stdev (stdev/sqrt(no))
              2.3  19-08-1996 FL  /NOSMOOTH did not work
              2.4  11-09-1996 FL  SPR_S0056 and SPR_S0057, better colors
                                  problems with AOT6s solved 
              2.5  15-09-1996 FL  SPR_S0059 inproper binsize init with RESOL
              2.6  28-11-1996 FL  SPR_S0131 new interface to CHK_MULTIX
              2.7  24-02-1997 FL  SPR_S0189 correct line numbers, 
                                            correct FWHM with Gaussian.
              2.8  05-09-1997 FL  SPR_S0268 retain reference grid, also
                                            with AOT-bands
                                  SPR_S0267 correct use of line and scnt
              2.9  25-09-1997 FL  SPR_S0281 more efficient data selection
                                            added WMEAN method
                                  SPR_S0282, use rgb colors