;+
; NAME: RALICE_WAVECAL
;
;
; PURPOSE: Create the Rosetta Alice 2-D wavelength calibration image.
;
; This FITS file contains constants used by the IDL routine
; alice_wavecal.pro to determine the wavelength scale for the Rosetta
; Alice UV spectrometer. Changes in the temperature of the Alice
; detector electronics cause the column at which a particular
; wavelength appears in the Alice data to vary. To correct for this
; temperature dependent shift in apparent wavelength, a cubic
; polynomial was fit to the (gaussian) centroid locations of the Lyman
; beta, Lyman alpha, O I 1304, C I 1561, and C I 1657 emission lines
; present in the spectra of comet C/2002 LINEAR T7 observed in April
; and May 2004 and the Lyman beta, Lyman alpha and O I 1304 lines
; present in the spectra of comet 9/P Tempel 1 observed in June and
; July 2005.
;
; The wavelength scale for row 15 (zero-indexed) of the Alice data is
; given, by the following equation: 
;
; lambda = m*x + alpha + beta*T + gamma*T^2 + delta*T^3
;
; In addition to the temperature dependence of the wavelength scale,
; some field curvature is introduced by the Alice optics. This
; curvature is most pronounced in rows 5-12 of the detector, where
; lower wavelengths are shifted to lower column numbers, relative to
; the central row (row 15, zero-indexed). The 32-element vector
; contains offsets (relative to row 15 and in units of pixels) for
; each row of the Alice detector, as determined by fits to the
; centroid of the Lyman beta line in the Deep Impact obseravtions of
; comet 9/P Tempel 1.
;
; CATEGORY: Rosetta Alice calibration
;
; CALLING SEQUENCE: wave_image=ralice_wavecal(T)
;
; INPUTS:
;   T - variable containing the electronics temperature (T_DELECC)
;       during the observation.
;
; RETURNS: 
;   Returns the 2-dimensional wavelength array for the given
;   temperature.
;
; MODIFICATION HISTORY:
; Completely rewritten from prior versions written by Joel
; Parker to use new wavelength calibration from analysis of Deep
; Impact, C/2002 LINEAR T7, and sky observations. 

; $Id: ralice_wavecal.pro, v 4, 2007-01-12, AJS 
;  v 5 -- Modified to also use the stim position to determine
;         wavelength scale, 2007-02-24, AJS                         
;-

FUNCTION ralice_wavecal, T = t, stimpos = stimpos, row_offset = row_offset, hdr = hdr

IF n_elements(T) EQ 0 AND n_elements(stimpos) EQ 0 THEN BEGIN 
   mike_log, err = 2, 'RALICE_WAVECAL: No electronics temperature or stim position supplied.'
   mike_log, 'NOT creating wavelength calibration image'
   return, -1
ENDIF

IF NOT keyword_set(row_offset) THEN BEGIN 
   mike_log, err = 2, 'RALICE_WAVECAL: Error, no row offset vector.'
   mike_log, 'NOT creating wavelength calibration image'
   return, -1
ENDIF

IF NOT keyword_set(hdr) THEN BEGIN 
   mike_log, err = 2, 'RALICE_WAVECAL: Error, no wavelength header.'
   mike_log, 'NOT creating wavelength calibration image'
   return, -1
ENDIF

IF n_elements(stimpos) EQ 0 THEN stimpos = sxpar(hdr, 'M_T') + sxpar(hdr, 'ALPHA_T') + $
  sxpar(hdr, 'BETA_T') * T + sxpar(hdr, 'GAMMA_T') * T^2 + sxpar(hdr, 'DELTA_T') * T^3

m = sxpar(hdr, 'M_S')
alpha = sxpar(hdr, 'ALPHA_S')
beta = sxpar(hdr, 'BETA_S')
gamma = sxpar(hdr, 'GAMMA_S')
delta = sxpar(hdr, 'DELTA_S')

;refshift = (1215.67-(alpha + beta * 20. + gamma * 20.^2 + delta * 20.^3))/m

x = findgen(1024)
lambda = m * x + alpha + beta * stimpos + gamma * stimpos^2 + delta * stimpos^3
lambda_2d = lambda # (fltarr(32) + 1.)

; total image offset from wavelength image, in pixels, as a function
; of image row
xoffset = -row_offset
xoffsetarr = (fltarr(1024) + 1) # xoffset
yarr = (fltarr(1024)+1)#findgen(32)
xarr = findgen(1024)#(fltarr(32) + 1)

WaveImage = interpolate(lambda_2d, xarr - xoffsetarr, yarr)
WaveImage = float(WaveImage)

return, WaveImage
end