;+
; NAME:
;	BCS_MULTI
; PURPOSE:
;	Plots many BCS spectra of a selected channel on a page.
; CALLING SEQUENCE:  
;	BCS_MULTI,index,data
; INPUTS:
;	index	index structure corresponding to data
;	data	data array
; KEYWORD INPUTS:
;	chan	channel number to be plotted [Def = 3, Ca XIX]
;	range	start and stop elements of data array to be plotted
;	bins	limit of bin numbers to plot within the channel
;	decomp	if set, the data is decompressed - it is also scaled 
;		by the integration time (including fast-queue data!)
;	smooth	if set, the spectra is smoothed
;	page	if present, routine pauses ate end of each page when
;		plotting to X-windows display
; OUTPUT:
;
; OPTIONAL OUTPUT:
;
; PROCEDURE
;
; HISTORY
;	RDB	Nov, 1991	Written
;	RDB	18-Feb-92	DECOMPRESS -> BCS_DECOMP
;	RDB	 4-Apr-92	Some updates
;	RDB	25-Oct-92	Changed to procedure, major revamp
;	RDB	28-Feb-93	'q' kills if to screen; added clearplot
;-

pro	bcs_multi,index,data,chan=chan,smooth=smooth,decomp=decomp, $
	range=range,bins=bins,page=page

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

ns = size(dspectra)
print,ns

int2ex,index.gen.time,index.gen.day,tarr
tarr = fmt_tim(tarr)

ps_long

set_xy
;!fancy = 2
!p.font = -1
nx = [3,6]
!p.multi=[0,nx,0,1]
!xtitle = ''	;'Bins'
!ytitle = 'Counts/bin/sec'	;DGI'
!mtitle = ''

for j = 0,ns(2)-1 do  begin

   spect = dspectra(*,j)
   if keyword_set(smooth) then spect = smooth(spect,2)

   plot,indgen(bstp-bsta+1)+bsta,spect, $	;dspectra(*,j),  $
     xrange=[bsta,bstp],xstyle=1+8,ystyle=8,psym=10
   xyouts,0.75*(!x.crange(1)-!x.crange(0))+!x.crange(0), $
      0.8*(!y.crange(1)-!y.crange(0))+!y.crange(0), $
      strmid(tarr(j),11,8),siz=1.

;;   ans = ''	
   if !d.name eq 'X' and ((j+1) mod (nx(0)*nx(1))) eq 0 then begin
      if j lt nx(0)*nx(1) then print,'Next page? ',format='(a,$)'
      ans = get_kbrd(1)
      if strlowcase(strmid(ans,0,1)) eq 'q' then goto, endit
;;      read,string(j+1-nx(0)*nx(1)+dset_arr(0).dset,j+1+dset_arr(0).dset,format='(i4,1h-,i4)')+' :',ans
   endif
endfor

endit:
clearplot		;reset !p.multi, etc
ps_reset

end
