pro rdbda,file,chan,accum,times,ltc,utbase,spectra,bins,sdgi,ion,w0,dw,rock,ierr

sq2=2.*sqrt(alog(2.))


ions = ['Ca XVIII-XIX','Fe K-alpha  ','Fe XVIII-XXI', $
       'Fe XXII-XXV ','Fe XXIII     ','Fe XXIV     ','Fe XXV (R)  ', $
       'Fe XXVI K-al']

adw=[3.050e-4,9.4007e-5,2.6822e-4,2.682e-4,1.2526e-4,1.2526e-4,1.2526e-4,1.]

aw0=[3.2343,1.94815,1.950,1.9019,1.8840,1.8840,1.8840,0.]

arock=[6.12e-4,6.1e-5,6.2e-5,6.6e-5,6.6e-5,6.6e-5,6.7e-5,7.1e-5]

chans=1+indgen(8)

;-- BDA file?

if n_elements(file) eq 0 then begin
 fetch,file,'bda'
endif

;-- where is it?

find=findfile(file,count=fc)
if fc eq 0 then begin
 ierr=1 & message,'cannot find '+file,/contin & return 
endif

;-- channel? 

if n_elements(chan) eq 0 then chan=0
if chan(0) eq 0 then begin
 repeat begin
  chan='' & read,'* enter channel to process (1-8) [def=1]: ',chan
  if chan eq '' then chan=1 else chan=fix(chan)
  cfind=where(chan eq chans,count)
 endrep until (count ne 0)
endif

nchn=n_elements(chan) & ion=ions(chan-1) & w0=aw0(chan-1) & dw=adw(chan-1)
rock=arock(chan-1)
 
;-- accumulation time?

if n_elements(accum) eq 0 then accum=0
if accum(0) eq 0 then begin
 accum='' & read,'* enter accumulation time [def=10 secs]: ',accum
 if accum eq '' then accum=10. else accum=float(accum)
endif

;-- start reading

rdbda_smm,file,index,data,chan=chan,accum=accum,ierr=ierr

if (ierr gt 0) then message,'BDA read error',/contin else begin

;-- make times compatible with UTPLOT 

 utbase=atime((index(0).gen.day-1)*24.*3600.)
 times=index.gen.time/1000.
 sdgi=index.bsc.actim/1000.
 ltc=index.bsc.total_cnts/sdgi
 spectra=data.counts
 bins=data.bin

endelse

return & end