
function eis_tilt_correction, wd, fitstr

;+
; NAME
;
;    EIS_TILT_CORRECTION()
;
; PROJECT
;
;    Hinode/EIS
;
; EXPLANATION
;
;    Corrects the eis_auto_fit fit structure for the tilt of the EIS slit.
;    Both the 1" and 2" slits are accounted for.
;
; INPUTS
;
;    WD    The window structure produced by eis_getwindata.
;
;    FITSTR The fit structure produced by eis_auto_fit.
;
; OUTPUT
;
;    The returned structure is identical to FITSTR, but the .cen and .vel
;    tags are modified for the EIS tilt correction.
;
; CALLS
;
;    LAMB2V()
;
; HISTORY
;
;    Ver.1, 9-Nov-2007, Peter Young
;    Ver.2, 5-Nov-2008, Peter Young
;     -The offset (in angstroms) is now put in the offset tag of the
;      output. If the tag does not exist, then it is created.
;     -The tilt is now corrected if the data have been binned in the
;      Y-direction. 
;    Ver.3, 16-Dec-2008, Peter Young
;     -changed way nx and ny are computed.
;-

IF n_params() LT 2 THEN BEGIN
  print,'Use:  IDL> outfit=eis_tilt_correction( wd, fitstr )'
  return,-1
ENDIF

;
; The array tilt_values gives the tilt values for the different EIS slits.
;   0 - 1"
;   1 - 266"
;   2 - 2"
;   3 - 40"
;
tilt_values=[1.22e-5,0.,1.14e-4,0]

slit_ind=wd.hdr.slit_ind

tilt=tilt_values[slit_ind]

scale=wd.scale

ny=fitstr.yrange[1]-fitstr.yrange[0]+1
nx=fitstr.xrange[1]-fitstr.xrange[0]+1

corr=findgen(ny)*scale[1]*tilt
velcorr=lamb2v(corr,fitstr.refwvl)

corr=(fltarr(nx)+1.)#temporary(corr)
velcorr=(fltarr(nx)+1.)#temporary(velcorr)

i=where(fitstr.vel NE fitstr.missing)

outfit=fitstr
outfit.cen[i]=fitstr.cen[i]-corr[i]
outfit.vel[i]=fitstr.vel[i]-velcorr[i]

IF NOT tag_exist(outfit,'offset') THEN outfit=add_tag(outfit,fltarr(nx,ny),'offset')
outfit.offset=corr

return,outfit

END

