;+
; NAME:
;	bsc_bin2w
; PURPOSE:
;	convert BCS bin positions wavelength
; CALLING SEQUENCE:
;	wave=bsc_bin2w(bin,chan)
; INPUTS:
;       bins  = raw bin number [vector or scalar]
;       chan = BCS channel [must be scalar]
; OUTPUTS:
;       wave = wavelength corresponding to corrected bin position 
; OPTIONAL INPUT KEYWORDS:
;       modeid = necessary to identify grouping plan for each channel [def=1]
;       raw = if set, informs program that these are raw uncorrected bin positions
;             and, hence, need to be corrected.
;       dispc= dispersion correction factor (def=1)
; PROCEDURE::
;       Calls gt_bsc_bincal to determine mapping from bin to wavelength,
;       then interpolates input bins to new wavelengths.
; MODIFICATION HISTORY:
;       20 Aug 1994, written DMZ (ARC)
;       5  Jan 1995, modified DMZ (ARC) -- corrected for resonance line offset
;-

function bsc_bin2w,bins,chan,modeid=modeid,raw=raw,dispc=dispc

on_error,1

if (n_elements(chan) ne 1) or (n_elements(bins) eq 0) then $
 message,'syntax --> '+strupcase('wave = bsc_bin2w(bins,chan)')

;-- are these raw bins?

if keyword_set(raw) then cbins=bsc_bincor(bins,chan,modeid=modeid) else $
 cbins=bins 

bincal=gt_bsc_bincal(chan,modeid,/novalid)
if datatype(bincal) ne 'STC' then return,0

physpos=bincal.physpos
wave=bincal.wave

if n_elements(dispc) eq 0 then dispc=1.
if dispc ne 1. then begin
 wres=get_atomic(chan,/w)
 wave=wres*(1.-dispc)+dispc*wave
endif

cwave=interpol(wave,physpos,cbins)

if n_elements(cwave) eq 1 then cwave=cwave(0)


return,cwave

end

