
FUNCTION eis_convert_fitdata, fitdata, line

;+
; NAME
;
;     EIS_CONVERT_FITDATA()
;
; PROJECT
;
;     Hinode/EIS
;
; EXPLANATION
;
;     This takes the fit structure produced by eis_auto_fit_gen and
;     converts it into the format of the fit structure produced by
;     eis_auto_fit for the specified emission line (LINE).
;
;     This allows software developed for use with eis_auto_fit to be
;     used on the eis_auto_fit_gen data.
;
; INPUTS
;
;     FITDATA   A fit structure produced by eis_auto_fit_gen.
;
;     LINE      The index of the emission line to be extracted from
;               FITDATA
;
; OUTPUTS
;
;     A structure of the same form as that produced by eis_auto_fit.
;
; CALLS
;
;     LAMB2V()
;
; HISTORY
;
;     Ver.1, 6-Nov-2008, Peter Young
;-

IF n_params() LT 2 THEN BEGIN
  print,'Use:  IDL> outdata = eis_convert_fitdata( fitdata, line)'
  return,-1.
ENDIF 

ngauss=fitdata.ngauss
nback=fitdata.nback
nx=fitdata.nx
ny=fitdata.ny

IF line GE ngauss THEN BEGIN
  print,'%EIS_CONVERT_FITDATA: The input LINE indicates which Gaussian should be chosen to'
  print,'                      extract to the output structure. The choices are:'
  FOR i=0,ngauss-1 DO BEGIN
    print,format='(i10,f10.3)',i,fitdata.refwvl[i]
  ENDFOR
  print,'Returning....'
  return,-1
ENDIF 

farr=fltarr(nx,ny)

i1=line*3     ; index in aa of line peak
i2=line*3+1   ; index in aa of line centroid
i3=line*3+2   ; index in aa of line width

IF nback EQ 2 THEN bg2=reform(fitdata.aa[ngauss*3+1,*,*])

outdata={int: reform(fitdata.int[line,*,*]), $
         interr: reform(fitdata.interr[line,*,*]), $
         cen: reform(fitdata.aa[i2,*,*]), $
         cenerr: reform(fitdata.sigmaa[i2,*,*]), $
         vel: lamb2v(reform(fitdata.aa[i2,*,*])-fitdata.refwvl[line],fitdata.refwvl[line]), $
         velerr: lamb2v(reform(fitdata.sigmaa[i2,*,*]),fitdata.refwvl[line]), $
         wid: reform(fitdata.aa[i3,*,*]), $
         widerr: reform(fitdata.sigmaa[i3,*,*]), $
         exp_start_times: fitdata.exp_start_times, $
         refwvl: fitdata.refwvl[line], $
         missing: fitdata.missing, $
         offset: fitdata.offset, $
         chi2: fitdata.chi2, $
         pix: fitdata.pix, $
         bg1: reform(fitdata.aa[ngauss*3,*,*]), $
         bg2: bg2, $
         xrange: fitdata.xrange, $
         yrange: fitdata.yrange}

return,outdata

END
