;
; MODIFICATION HISTORY:
;       16-Feb-2008: A. Gardini, version 1.0
;       18-Mar-2008: A. Gardini- Changed the column's order of moments.
;       23-Sep-2008: A. Gardini- Added the errors on intensity and velocity.
;       24-Mar-2009: A. Gardini- New method to get windata, working also
;                                with data just calibrated.
;-
pro eis_moment::gauss_fit, data, iwin, init=init
  if n_elements(init) eq 1 then return
  missing = -100.
; get reference line  
  hdr=data->gethdr()
  line_id=strtrim((hdr->getline_id())[iwin],2)
  refwvl_id=(strsplit(line_id,' ',/extract))[2]
  refwvl=float(refwvl_id)
; ; get filename
;   filename = data->getfilename()
; ; get the wd structrure
;   wd = eis_getwindata(filename,refwvl)

; get the windata structrure
  errfile='errfile.tmp'
  data->saveerr,file=errfile
  data_er=obj_new('eis_data',errfile)
  windata=data->getwindata(iwin,data_er=data_er)
  file_delete,errfile

; run eis_auto_fit on data
  eis_auto_fit,windata,fit,refwvl=refwvl
; correction of velocities for slit tilt
  fit = eis_tilt_correction(windata,fit)

  int=rotate(fit.int,3)
  vel=rotate(fit.vel,3)
  wid=rotate(fit.wid,3)
  interr=rotate(fit.interr,3)
  velerr=rotate(fit.velerr,3)
  widerr=rotate(fit.widerr,3)
; Final computation of the line width: the factor 2.35 is introduced to get
; the FWHM ( = 2*sqrt(2ln2) * sigma for a Gaussian distribution).
  badindex = where(wid eq missing,ct)
  if ct gt 0 then wid[badindex]=max(wid)
  wid = wid*1000.*2.35
; put results into place in the instances of the eis_moment object:
  (*self.w[iwin])[0, *, *] = int[*, *]    ; intensity
  (*self.w[iwin])[1, *, *] = vel[*, *]    ; velocity
  (*self.w[iwin])[2, *, *] = wid[*, *]    ; width
  (*self.w[iwin])[3, *, *] = interr[*, *] ; intensity error 
  (*self.w[iwin])[4, *, *] = velerr[*, *] ; velocity error 
; correction of velocities for spacecraft motion
  bad = vel*0.
  badindex = where(vel eq missing,ct)
  if ct gt 0 then bad[badindex]=missing
  self->corrvel,iwin,bad
return
end
