pro rd_sdb,sdb,spcnum,chnum,ut

on_error,1
sz=size(sdb) 
if sz(n_elements(sz)-2) ne 8 then $
  message,'no spectra are present in (or were extracted from) FIS file'

nspec=sz(1) & spec=strarr(nspec) & v=[6,5,4,0,1,2,3] 

;-- extract and insert spectral info into a string buffer for menu

mtitle='       TIME     NY  NZ   DY  DZ   DGI  MODE  STEP  NPTS   START    END'
for i=0,nspec-1 do begin
 temp=sdb(i).idx
 tb=strcompress(string(temp.time(v),'(i2)'))
 if i eq 0 then date=tb(0)+'/'+tb(1)+'/'+tb(2)
 mode=temp.modetype 
 eb=string(temp.dgi,'(i2)')
 rb=string(temp.ny,'(i2)')+'  '+string(temp.nz,'(i2)')
 db=string(temp.dy*5,'(i2)')+'  '+string(temp.dz*5,'(i2)')
 cb=string(temp.crystep,'(i2)')
 time=tb(3)+':'+tb(4)+':'+tb(5)
 sb=string(temp.nsamps,'(i5)')
 startb=string(sdb(i).c(0),'(i5)')
 endb=string(sdb(i).c(temp.nsamps-1),'(i5)')
 range=startb+'  '+endb
 spec(i)=time+'   '+rb+'   '+db+'   '+eb+'    '+mode+'  '+cb+'  '+sb+'   '+range
endfor
spec=strtrim(spec,2)

;-- select spectrum #: spcnum 

line='---------------------------------------' & line=line+line
quit='QUIT PROGRAM'

repeat begin
 spcnum=0 
 if nspec gt 1 then begin
  repeat begin
   if !d.name eq 'X' then begin
    quest='SELECT SPECTRUM FROM ABOVE CHOICES'
    spcnum=wmenu(['  '+strtrim(mtitle,2),spec,quit,quest],title=0,init=1)-1
    if spcnum eq nspec then message,'aborting'
   endif else begin
    print, '* following spectra are available on '+date+':'
    print,line & print,mtitle & print,line      
    for i=0,nspec-1 do print,'('+string(i,'(i2)')+') '+spec(i) 
    print,line
    spcnum='' &  read,'* enter spectrum number to extract [def = 0, q to quit]: ',spcnum
    if strupcase(strmid(spcnum,0,1)) eq 'Q' then message,'aborting...'
    spcnum=fix(spcnum)
   endelse
   good=(spcnum le (nspec-1)) and (spcnum ge 0)
   if not good then print,'% RD_SDB: INVALID SPECTRUM NO, TRY AGAIN PLEASE'
  endrep until good
 endif else begin
  print,'* following spectrum selected:' & print,mtitle & print,line
  print,'('+string(0,'(i2)')+') '+spec & print,line
 endelse

;-- select channel #: chnum

 chan=sdb(spcnum).idx.whichpara 
 on=where( (chan ge 1) and (chan le 7), nchan)
 if nchan eq 0  then print,'% RD_SDB: NO VALID CHANNELS, TRY AGAIN PLEASE'
endrep until nchan ne 0

chan_on=chan(on)

;-- deduce exact start time of spectrum

temp=sdb(spcnum).idx
tb=[string(temp.time(v(0:5)),'(i2)'),string(temp.time(v(6)),'(i3)')]
tb=strcompress(tb)
ut=tb(0)+'/'+tb(1)+'/'+tb(2)+' '+tb(3)+':'+tb(4)+':'+tb(5)+'.'+tb(6)

;-- print menu

if n_elements(chan_on) gt 1 then begin
 s=sort(chan_on) & chan_on=chan_on(s) 
endif
nchan=n_elements(chan_on) & schan=string(chan_on,'(i1)')
ctitle='  CHAN. NO.  ' & line  ='------------'

if nchan gt 1 then begin
 repeat begin
  if !d.name eq 'X' then begin
   quest='SELECT CHANNEL FROM ABOVE CHOICES' 
   cval=wmenu([ctitle,'                '+schan,quit,quest],title=0,init=1)-1
   if cval eq nchan then message,'aborting...'
  endif else begin
   print,'* following channels are available:'
   print,line & print,ctitle & print,line
   for i=0,nchan-1 do begin
    print,'     '+schan(i)
   endfor
   print,line
   chnum='' & read,'* enter channel number to extract [def = first; q to quit]: ',chnum
   if strupcase(strmid(chnum,0,1)) eq 'Q' then message,'aborting...'
   if chnum eq '' then chnum=fix(schan(0)) else chnum=fix(chnum)
  endelse

;-- check for valid channel entry

  if !d.name eq 'X' then begin
   good=(cval le (nchan-1))
   if good then chnum=fix(schan(cval)) 
  endif else begin
   cval=where(chnum eq fix(schan),count)
   good=(count gt 0)
  endelse
  if not good then print,'% RD_SDB: INVALID CHANNEL: TRY AGAIN PLEASE'
 endrep until good
endif else begin
 print,'* following channel selected:',schan
 chnum=chan_on 
endelse

cval=where(chnum eq chan_on) 
return & end