

PRO eis_debias, wd, xstart

;+
; NAME
;
;    EIS_DEBIAS
;
; PROJECT
;
;    HINODE/EIS
;
; EXPLANATION
;
;    Removes the CCD bias from an EIS data window.
;
; INPUTS
;
;    WD    An EIS window data array. The contents of WD are changed by
;          this routine.
;
;    XSTART The X start pixel on the detector for the data window. (This
;           places the data window into one of the four detector segments,
;           which each have different biases.)
;
; OUTPUT
;
;    WD    WD is returned with the bias value subtracted.
;
; HISTORY
;
;    0.1, 14-Dec-2006, Peter Young
;       using bias values from Harry Warren.
;    0.2, 10-Jan-2007, Peter Young
;       The bias is now estimated from the minimum of the data array to
;       avoid negative values.
;    0.3, 13-Jan-2007, Peter Young
;       Changed -5 to +5 (!).
;    0.4, 19-Feb-2007, Peter Young
;       Now works out median of the 2% of pixels with the lowest
;       intensities.
;    1.0, 11-Jan-2011, Peter Young
;       I now filter out any pixels with values equal to zero since
;       these are missing data.
;-


; CASE 1 OF 
;   xstart LT 1074: bias=548.8
;   xstart GE 1074 AND xstart LT 2098: bias=500.5
;   xstart GE 2198 AND xstart LT 3022: bias=556.6
;   xstart GE 3022: bias=547.7
;   ELSE: print,'% EIS_DEBIAS: outside of detector limits!!'
; ENDCASE


n=n_elements(wd)

k=where(wd NE 0)

i=sort(wd[k])

bias=median(wd[k[i[0:n/50]]])
bias=fix(bias)

wd=temporary(wd)-bias

END
