;+
; NAME:
;       GET_BSC
; PURPOSE:
;       extract specific data products from BSC data
; CALLING SEQUENCE:
;       data=get_bsc(bsc_index,bsc_data)
; INPUTS:
;       bsc_data  = bsc_data structure
;       bsc_index = bsc_index structure
; KEYWORDS
;       chan      = channel to extract
;       tstart    = string start time to extract (def = start of data)
;       tend      = string end time to extract (def = start of data)
;       name of field to extract:
;       counts    = counts (per unit bin)
;       wave      = wavelength array (A)
;       bin       = physical bin positions
;       error     = uncertainties in counts
;       flux      = flux (ph cm-2 s-1 A-1)
;       eflux     = uncertainties in flux
;       accum     = accumulation time of spectrum
;       time      = spectrum time (secs since start of day)
;       rate      = count rate for selected channel
;       index     = usual index
; PROCEDURE:
;       Examines tags in BSC index and data structures.
;       e.g. to extract the FLUX field from channel 3:
;
;           IDL> FLUX=GET_BSC(BSC_INDEX,BSC_DATA,CHAN=3,/FLUX)
;  RESTRICTIONS:
;       Only 1-2 field keywords can be set at a time
; HISTORY:
;       Written Apr'93 by D. Zarro (ARC)
;       6-Oct-94, Zarro, fixed bug in call to SEL_BSC
;-

function get_bsc,bsc_index,bsc_data,counts=counts,wave=wave,bin=bin,fit=fit,time=time,$
 flux=flux,error=error,eflux=eflux,chan=chan,accum=accum,rate=rate,$
 tstart=tstart,tend=tend,index=index

on_error,1

keywords=['counts','wave','bin','flux','error','eflux','fit','accum',$
           'rate','time','index']

;-- check inputs

if (not bsc_check(bsc_index,bsc_data)) then begin
 chkarg,'get_bsc'
 return,0
endif

count=0
for i=0,n_elements(keywords)-1 do begin
 tag=keywords(i)
 stc='if keyword_set('+tag+') then count=count+1'
 s=execute(stc)
endfor


;if count gt 1 then message,'ONE and only ONE keyword can be set at a time'

if count eq 0 then begin
 print,'* following keyword fields are valid:'
 print,' '
 for i=0,n_elements(keywords)-1 do print,keywords(i)
 print,' '
 repeat begin
  key='' & read,'* enter a field ---> ',key
  look=where(strlowcase(key) eq keywords,count)
 endrep until (count ne 0)
 stc=keywords(look(0))+' = 1'
 s=execute(stc)
endif
  
;-- get channel data

sel_bsc,bsc_index,bsc_data,bindex,bdata,tstart=tstart,tend=tend,chan=chan

if n_elements(bindex) eq 0 then message,'no BSC data for specified times'
if keyword_set(index) then return,bindex

;-- get accumulation time from BSC_INDEX

sdgi=bindex.bsc.actim/1000.
if keyword_set(accum) then return,sdgi

;-- get spectrum times

if keyword_set(time) then return,gt_bsc_time(bindex)

;-- get lightcurve

if keyword_set(rate) then begin
 cps=gt_bsc_crate(bindex)
 if keyword_set(flux) then begin
  bincal=gt_bsc_bincal(bindex(0))
  cps=cps/bincal.efarea
 endif
 return,cps
endif

;-- wavelengths

if keyword_set(wave) then return,gt_bsc_wave(bindex,bdata)

;-- flux errors

if keyword_set(eflux) or keyword_set(flux) then begin
 if keyword_set(flux) then return,gt_bsc_flux(bindex,bdata)
 if keyword_set(eflux) then begin
  bflux=gt_bsc_flux(bindex,bdata,beflux)
  return,beflux
 endif
endif

;-- do rest of keywords

for i=0,n_elements(keywords)-1 do begin
 tag=keywords(i)
 stc='if keyword_set('+tag+') then out=bdata.'+tag
 status=execute(stc)
 if (status eq 1) and (n_elements(out) ne 0) then return,out
endfor
message,'BSC data does not contain requested field'
 
return,0 & end

