;   Note: 1st time wrong if first modes are not 0/1 ?????


pro bcs_cont,index,data,chan=chan,smooth=smooth,decomp=decomp, $
    range=range,bins=bins

nx_data = size(data)
ista = 0  &  istp = nx_data(2)-1
if keyword_set(range) then begin
;   print,range
   ista = max([ista,range(0)])
   istp = min([istp,range(1)])
endif
;;print,ista,istp

channel = 3     ;Ca XIX
if keyword_set(chan) then channel = chan

dspectra = ext_bcschan(index(ista:istp),data(*,ista:istp),channel,nbins)
;   if desired, decompress and scale by integration time
if keyword_set(decomp) then begin
   dspectra = bcs_decomp(dspectra)  ;   !! check array type ???
   dspectra = bcs_norm(index(ista:istp),dspectra)
endif

nx_data = size(dspectra)        ;array dimensions have changed!!

;       Bin range within selected channel
bsta = 0 & bstp = nx_data(1)-1
if keyword_set(bins) then begin
;   print,bins
   bsta = max([bsta,bins(0)])
   bstp = min([bstp,bins(1)])
   dspectra = dspectra(bsta:bstp,*)
endif
;;print,bsta,bstp,nbins


!p.multi = 0


;   annotate if index structure is supplied
if n_elements(index) gt 0 then begin

   tt = anytim2ints(index)
   tstart = tt(ista)
   tstart.time = tstart.time/20000L*20000L  ;round to nearest 20 sec below

   ttstart = tstart.time + (tstart.day - tt(0).day)*86400L*1000L
   ttime = tt.time + (tt.day - tt(0).day)*86400L*1000L
   yvec = (ttime(ista:istp) - ttstart)/1000.    ;secs   ;/3600.

;;   yvec = index(ista:istp).gen.time - index(ista).gen.time    ;!!!!
;;   yvec = yvec/3000                       ;!!!!

   ww = where(index(ista:istp).bcs.blockid le 1 and total(dspectra,1) gt 0)
   if keyword_set(smooth) then dspectra = smooth(dspectra(*,ww),3) $
   else dspectra = dspectra(*,ww)

;       setup vector of contour levels
   yymax = max(dspectra)
   clevels = indgen(20)*10
   if yymax lt 30 then clevels = indgen(20)*2

;       do the plot
   xvec = indgen(bstp-bsta+1) + bsta
   int2ex,tstart.time,tstart.day,tarr
   tarr = fmt_tim(tarr)
   mtitle = string('BCS Channel ',channel, $
    ' Spectra starting at:  '+tarr,format='(a,i1,a)')
   contour,dspectra,xvec,yvec(ww),levels=clevels,  $
     xstyle=1,xtitle='Bin Number',title=mtitle,  $
     ystyle=1,ytitle='Time from start (secs)',  $
     yrange=[0,(fix(max(yvec))/20+1)*20]        ;,/noclip

;       and annotate it
   xp = 0.60*nx_data(1)

   yp0 = 0.05*(!y.crange(1)-!y.crange(0))+!y.crange(0)
   int2ex,index(ista).gen.time,index(ista).gen.day,tarr
   tarr = fmt_tim(tarr)
   xyouts,xp,yp0,tarr + '   Start

   yp1 = 0.10*(!y.crange(1)-!y.crange(0))+!y.crange(0)
   int2ex,index(istp).gen.time,index(istp).gen.day,tarr
   tarr = fmt_tim(tarr)
   xyouts,xp,yp1,tarr + '   Stop

   yp2 = 0.15*(!y.crange(1)-!y.crange(0))+!y.crange(0)
   if keyword_set(smooth) then xyouts,xp,yp2,'Smoothed'

endif else begin

   if keyword_set(smooth) then dspectra = smooth(dspectra,3)
   contour,dspectra,levels=indgen(20)*10,  $
     xstyle=1,xtitle='Bin Number',  $
     ytitle='Spectra Number'

endelse


;;ans = ''
;;read,ans
return
end