Identifier   sws_flatfield

 Purpose      Flatfield all separate det scans per AOT band and line
              with respect to eachother or a Reference spectrum.

 Synopsis     struct_out = sws_flatfield( struct [, degree]
                                                 [, /fitmethod]
                                                 [, aot_band=aotband]
                                                 [, det=det]
                                                 [, lines=lines]
                                                 [, scans=scans]
                                                 [, ref_spec=ref_spec]
                                                 [, /offset]
                                                 [, /scaling]
                                                 [, clip=clip]
                                                 [, minfit=minfit]
                                                 [, /noupdown]
                                                 [, /select]
                                                 [, /noplot]
                                                 [, logfile=logfile]

 Arguments    name     I/O  type     description
              -------------------------------------------------------------
              struct_out O  struct   Output struct of the same size 
                                     as the input struct.
              struct     I  struct   Input struct AAR or SPD
              degree     I  int      Degree of polynomial
                                     [default: 0]
              fitmethod  I  keyword  Method to use for the fits.
                                      'POLY'nomial
                                      'ORTHO'normal polynomial
                                     [default: 'ORTHO']
              aot_band   I  strarr   AOT bands (e.g '1a','1b'...)
                                     [default: all aot_bands]
              det        I  intarr   Detectors to flatfield.
                                     Only these detectors are flatfielded.
                                     Note however that the reference 
                                     is still the data of all detectors
                                     or the reference spectrum
                                     [Default: all detectors]
              lines      I  intarr   Individual lines. LINES = 0 will 
                                     results in all lines being treated 
                                     as one and the flatfielding is
                                     done over the whole AOT-band.
                                     [default: all]
              scans      I  intarr   Scans, as defined in the AAR
                                     scnt tag, to use.
                                     SCANS=0 combines all scans.
                                     [Default: all]
              ref_spec   I  struct   Reference spectrum to which the data
                                     will be aligned (as an AAR struct).
              offset     I  keyword  Treat the differences as offset errors.
                                     [default]
              scaling    I  keyword  Treat the differences as scaling errors.
              clip       I  float    Clip all the data above or below the
                                     spectrum before doing the fit. 
                                     Can be useful in the presence of lines.
                                     [default: 5]
              minfit     I  integer  Minimum number of distinct wavelength
                                     points required to make a 'proper' fit.
                                     If there are not enough points in a
                                     scan, this is removed from the data.
                                     [default: 10]
              noupdown   I  keyword  Do not fit the up and down scans
                                     separately
              select     I  keyword  Select a xrange in the plot which
                                     is to be used in flatfielding.
                                     The selection is done by drawing
                                     a box in the plot covering the 
                                     desired wavelength range.
              noplot     I  keyword  Do not plot the data and fits
              logfile    I  string   Log some diagnostic messages
                                     [Default: 'sws_flatfield.log']

 Returns      SPD or AAR structure depending on input structure

 Description  FLATFIELD_SWS will will flatfield the data in an AAR or SPD.
              It will do so by fitting a median, line or an orthonormal
              polynomial of the same order to:
               1. - all grating down data (or all data if NOUPDOWN 
                    is specified) of the same AOT-band, line and scan 
                    as the scans in 2 over the same wavelength region. 
                  or
                  - a reference spectrum (specified with REF) over
                    a wavelength range similar to the scans in 2.
                    If in the reference AAR the status and flag are
                    filled with AOT-band information, this will
                    be used to select the reference data of the 
                    same AOT-band (but NOT LINE of SCNT) as the data.
               2. all separate detector scans (all up and down scans (unless
                  NOUPDOWN is specified) for all specified detectors in
                  each specified AOT-band, line and scan).
              These two fits are then used to correct the scans as either
              an offset or a scaling correction (/OFFSET or /SCALING)

              When not using a reference spectrum (i.e. using the 
              data itself as a reference) one extra correction is 
              applied to the data after all detectors have been 
              corrected. The total flux in both the up and the down 
              scan (in the selected range) will be made equal 
              to that of the reference data, being either the down 
              data or all data.

              It is possible to do a one step sigma clipping procedure
              in the polynomial fitting (keyword CLIP). Note that this
              has only impacts on the fit, no data will be removed.
              This is done by first making a fit of the specified 
              order, determining the rms with respect to this and then 
              replacing the data CLIP*rms above or below the fit by the
              values of the fit before doing a second fit.

              All data will be corrected, so also the data poits which 
              are flagged as NODATA.

              In an SPD all scans separated by a DARK current block
              are treated as separate lines.
 
              Scans which are shorter than MINFIT will be flagged as 
              NODATA.

 Examples
              To align all detectors within the separate AOT bands. 
                  flat_aar = sws_flatfield( aar )

              To align all the data onto a reference spectrum, e.g.
              to the model spectrum of HR6705 by Cohen.
                  HR6705_M_COHEN = read_faar('IAS_SED:HR6705_M_COHEN.FITS')
                  flat_aar = sws_flatfield( aar, ref=HR6705_M_COHEN )

              Default all lines in e.g. an AOT2 spectrum will be handled 
              separately. This means that all scans in each separate
              line is FLATFIELDED TO THE DOWN SCAN OF THAT LINE.
              To flatfield all lines covering the same wavelength
              range so the same level one can supply a reference
              spectrum. This can be e.g. the data itself, a rebinned
              spectrum or a model. So e.g.

                  ref = sws_rebin(aar,aot_band='3d')
                  flat_aar = sws_flatfield(aar,ref=ref,aot_band='3d')

 Comments    
              V2.9 Also in the reference spectrum (when provided)
                   a selection is done on AOT-band. This means that
                   e.g. the aar itself or a fully rebinnen AAR
                   can be provided without problems.
              V4.0 option SCANS and DET added.

 Category     UTIL

 Filename     sws_flatfield.pro (module: sws_flatfield)

 Dependencies calls: ORTHO_POLY, CHK_MULTIX

 Version      4.3

 History      1.0  06-02-1996 FL  document created
              1.1  25-02-1996 FL  mask removed, data clipping added
              1.2  26-02-1996 FL  minfit added
                   01-04-1996 BV  variable 'wave' replaced by 'waves'
              1.3  06-05-1996 FL  error with 0th order fit
                                  also sigma clipping of ref. spectrum
                                  doc. improved.
              1.4  15-05-1996 FL  default degree of fit set to 0
              1.5  10-07-1996 FL  optional flatfielding per line
                                  OFFSET as default correction
              1.6  20-08-1996 FL  rewritten, no interpolation errors,
                                  improved fitting and speed
              1.7  11-09-1996 FL  SPR_S0058, tried to 'really' improve speed
                                  Per default lines are treated separately.
                                  proper colors and plot titles
                                  /SELECT added as option
              1.8  15-09-1996 FL  SPR_S0060 define xrange_fit 
              1.9  26-06-1996 FL  SPR_S0071 proper plot of fit with /SELECT
                                  another heroic attempt to make it faster
              2.0  28-11-1996 FL  SPR_S0131 new interface to CHK_MULTIX
              2.1  24-01-1997 FL  SPR_S0170, occasional strange fits
                                             improved efficiency
              2.2  24-01-1997 FL  SPR_S0175, option LINE=0 did not work
              2.3  24-01-1997 FL  SPR_S0176, scalar/array(1) conflict
              2.4  18-02-1997 FL  SPR_S0187, better higher order fits
              2.5  25-02-1997 FL  SPR_S0190, better check validity ref scan,
                                             plot only VALID data
              2.6  05-03-1997 FL  SPR_S0197, undefined variable, no of ref pts
              2.7  15-04-1997 FL  SPR_S0218, Shifts in flux level
              2.8  22-04-1997 FL  SPR_S0226, High order fits using ref spectrum
              2.9  19-05-1997 FL  SPR_S0231, interpolation error for order 0
                                             slow array initialization
              3.0  23-05-1997 FL  SPR_S0232, Incorrect offsets with /SELECT
              3.1  12-06-1997 FL  SPR_S0238, Not use VER, non-standard IA
                                  SPR_S0239, Check on width of REF SCAN
              4.0  05-09-1997 FL  SPR_S0267, use scnt, add DETS option
              4.1  25-09-1997 FL  SPR_S0281, more efficient selection of data
                                  SPR_S0282, use rgb colors
              4.2  21-10-1997 FL  SPR_S0283, occasionaly wrong reference fits
              4.3  19-01-1998 FL  SPR_S0310, fits in double precision
                                  SPR_S0311, replaced orthopoly by ortho_poly