;+
; Project     : SOHO - CDS     
;                   
; Name        : PIX2WAVE
;               
; Purpose     : Calculate CDS wavelength given a detector pixel location.
;               
; Explanation : Uses dummy transformations to translate a pixel value to a
;               wavelength for any of the CDS's six spectral regions. To be
;               updated when the wavelength calibration is known.
;               
; Use         : IDL> lambda = pix2wave(spectrum [,pixel, slit=slit])
;    
; Inputs      : spectrum  - the spectrum identifier (string).  Only the first
;                           and last characters are used to identify the
;                           spectrum so inputs such as NIS1 and N1 are
;                           equivalent.  Valid entries are NIS1, NIS2, GIS1,
;                           GIS2, GIS3, GIS4 (and their abbreviations).
;               
; Opt. Inputs : pixel     - the pixel value to translate.  If not supplied the
;                           wavelengths for the extreme pixels are returned.
;                           The input can be an array.
;               
; Outputs     : The function value returned is the corresponding wavelength.
;               A value of -1 is returned for an error condition.
;               
; Opt. Outputs: None
;               
; Keywords    : Limit   - If input pixel is out of range, then return the
;                         appropriate wavelength limit.
;		NoLimit - If input pixel is out of range, then return a value
;                         anyway.
;               Slit    - specifies the slit to cater for slit dependence
;               Throwaway - do not retain input pixel numbers intact
;
; Calls       : None
;               
; Restrictions: Only dummy transformations at present
;               
; Side effects: None
;               
; Category    : Calibration, GDS, VDS, Wavelength
;               
; Prev. Hist. : None
;
; Written     : C D Pike, RAL, 28-May-1993
;               
; Modified    : Update VDS constants, CDP, 18-Nov-93
;               Include Limit keyword, CDP, 2-Feb-94
;               Updated wavelength ranges.  CDP, 30-Jan-95
;		Version 5, William Thompson, GSFC, 1 March 1995
;			Added keyword NOLIMIT
;               Version 6, Incorporate results of first calibration from
;                          s54r00 by BJIB.  27-Feb-96
;               Version 7, Rewrite to use common block communication with
;                          LOAD_WAVECAL.  CDP, 11-mar-96
;               Version 8, Make NIS quadratic.  CDP, 23-Jul-96
;               Version 9, Various updates suggested by SVVH. Float return
;                          and economise calculation.                 
;		Version 10, 19-Jan-2000, William Thompson, GSFC
;			Allow SPECTRUM to be an array
;
; Version     : Version 10, 19-Jan-2000
;-            


function pix2wave, spectrum, pixel_in, limit=limit, nolimit=nolimit,$
                   slit=slit, throwaway=throwaway

;
;  communication common block
;
common cds_wavecal, ncoff, gcoff


;
;  check parameters
;
if n_params() eq 0 then begin
   print,"Use: IDL> lam = pix2wave(spectrum_id [,pixel, /limit, /nolimit, $"
   print,'                         slit=slit]'
   print,"eg   IDL> lam = pix2wave('NIS1',[300,301,302])"
   return,0
endif

;
;  If spectrum was passed as an array, and matches the dimensions of pixel_in,
;  then call pix2wave individually for each point.
;
if n_elements(spectrum) gt 1 then begin
    if n_elements(spectrum) eq n_elements(pixel_in) then begin
	result = 0.*pixel_in
	for i=0,n_elements(result)-1 do result(i) = pix2wave(spectrum(i), $
		pixel_in(i), limit=limit, nolimit=nolimit, slit=slit,	  $
		throwaway=throwaway)
	return, result
    end else begin
	print, 'SPECTRUM_ID must be either a scalar'
	print, 'or have the same number of elements as PIXEL'
	return, 0
    endelse
endif

;
;  copy input pixels in case they are changed within
;
if keyword_set(throwaway) then pixel = temporary(pixel_in) $
  else pixel = pixel_in

;
;  parse the spectrum identifier
;
s = strtrim(spectrum,2)
sg = strupcase(strmid(s,0,1))
snum = fix(strmid(s,strlen(s)-1,1))

case sg of
  'N': begin
        if datatype(ncoff,1) ne 'Structure' then begin
           if load_wavecal('n') then begin
              print,'Loaded default NIS coefficients.'
           endif else begin
              print,'Error loading default NIS coefficients.'
              return, -1
           endelse
        endif
        c = float(ncoff.coeff)
        if n_elements(pixel) eq 0 then begin
           pixel = [0,1023]
        endif else begin
           small = where(pixel lt 0)
           big = where(pixel gt 1023)
           if big(0) ge 0 or small(0) ge 0 then begin
              if keyword_set(limit) then begin
                 if small(0) ge 0 then pixel(small) = 0
                 if big(0) ge 0 then pixel(big) = 1023
	      endif else if not keyword_set(nolimit) then begin
                 bell
                 print,'Pixel specification out of range for this spectrum.'
                 print,'Must be in the range 0 < pixel < 1023.'
                 return,-1
              endif
           endif
        endelse
        if snum eq 1 or snum eq 2 then begin
           temp = c(snum-1,1,0) + c(snum-1,2,0)*pixel
           lam  = c(snum-1,0,0) + temporary(temp)*temporary(pixel)
        endif else begin
                  bell
                  print,'Normal incidence spectrum number not recognised.'
                  print,'Value was ',snum
                  return,0
        endelse
       end
  'G': begin
        if datatype(gcoff,1) ne 'Structure' then begin
           if load_wavecal('g',gset_id=!def_gset_id) then begin
              print,'Loaded default (gset_id='+trim(!def_gset_id)+$
                     ') GIS coefficients.'
           endif else begin
              print,'Error loading default GIS coefficients.'
              return,-1
           endelse
        endif
        c = float(gcoff.coeff)
        if n_elements(pixel) eq 0 then begin
           pixel = [0,2047]
        endif else begin
           small = where(pixel lt 0)
           big = where(pixel gt 2047)
           if big(0) ge 0 or small(0) ge 0 then begin
              if keyword_set(limit) then begin
                 if small(0) ge 0 then pixel(small) = 0
                 if big(0) ge 0 then pixel(big) = 2047
              endif else if not keyword_set(nolimit) then begin
                 bell
                 print,'Pixel specification out of range for this spectrum.'
                 print,'Must be in the range 0 < pixel < 2047.'
                 return,-1
              endif
           endif
        endelse
        if snum ge 1 and snum le 4 then begin
           temp = c(snum-1,1,0) + c(snum-1,2,0)*pixel
           lam  = c(snum-1,0,0) + temporary(temp)*temporary(pixel)
        endif else begin
                  bell
                  print,'Grazing incidence spectrum number not recognised.'
                  print,'Value was ',snum
                  return,0
        endelse
       end
 else: begin
         bell
         print,'Spectrum identifier not recognised. Must be N or G.'
         print,'Character was ',sg
         return,0
       end
endcase

;
;  successful return
;
return, float(lam)

end
