
PRO eis_fit_correction, windata, fitdata, line=line, spline=spline

;+
; NAME
;
;      EIS_FIT_CORRECTION
;
; PROJECT
;
;      Hinode/EIS
;
; EXPLANATION
;
;      This routine takes the output fit structure from
;      eis_auto_fit_gen and performs the slit tilt and orbital
;      correction to the line centroids.
;
;      Note that eis_tilt_correction and eis_orbit_sline take as input
;      a structure that is in a different format to that output by
;      eis_auto_fit_gen, then I need to convert the eis_auto_fit_gen
;      structure to this format. The routine EIS_CONVERT_FITDATA
;      performs this conversion.
;
; INPUTS
;
;      WINDATA   A structure in the format produced by
;                EIS_GETWINDATA. This must be the same structure that
;                was used as input to EIS_AUTO_FIT_GEN.
;
;      FITDATA   The fit structure produced by EIS_AUTO_FIT_GEN.
;
; OPTIONAL INPUTS
;
;      LINE      If the fit structure includes fits for multiple
;                lines, then setting LINE chooses which line to
;                perform the correction for. Normally you will want to
;                choose the strongest line in the window. 
;
;      SPLINE    If an orbit correction has already been performed on
;                another line, then the same correction can be applied
;                to the lines in FITDATA by specifying SPLINE (which
;                is an output from EIS_ORBIT_SPLINE).
;
; OPTIONAL OUTPUTS
;
;      SPLINE    The spline fit used for the orbit correction and
;                generated by EIS_ORBIT_SPLINE. This can then be used
;                as input for a future call to EIS_FIT_CORRECTION (see
;                above). E.g., one may want to use the strong Fe XII
;                195 line to compute SPLINE, and then apply this
;                correction for another, weaker line in the spectrum. 
;
; OUTPUTS
;
;      The tag OFFSET in FITDATA is modified to include the wavelength
;      offsets produced by the tilt and orbital corrections. No other
;      tags are modified.
;
; CALLS
;
;      EIS_TILT_CORRECTION, EIS_ORBIT_SPLINE, EIS_CONVERT_FITDATA
;
; HISTORY
;
;      Beta 1, 6-Nov-2008, Peter Young
;-

IF n_elements(line) EQ 0 THEN BEGIN
  line=0
  print,'%EIS_FIT_CORRECTION: the structure contains fits for '+trim(fitdata.ngauss)+' emission lines.'
  print,'                     The emission line used for the corrections is:'
  print,'                     Line: '+trim(line)+'  '+trim(fitdata.refwvl[line])
  print,'                     Use the optional input LINE= to specify a different line'
ENDIF


;
; Make sure fitdata.offset is set to zero before doing anything
;
fitdata.offset=0.

outdata=eis_convert_fitdata(fitdata,line)

outdata2=eis_tilt_correction(windata,temporary(outdata))

outdata3=eis_orbit_spline(temporary(outdata2),/setzero, /quiet, spline=spline)

fitdata.offset=outdata3.offset

ngauss=fitdata.ngauss
FOR i=0,ngauss-1 DO BEGIN
  fitdata.aa[i*3+1,*,*]=fitdata.aa[i*3+1,*,*]-fitdata.offset
ENDFOR

END
