pro plot_ref,index,data,channel,dset_arr,title=title,decomp=decomp,line=line, $
     scale=scale,noextract=noextract,bsd=bsd,atp=atp
;+
; atp 27-mar-1992 
; 
; name:    plot_ref.pro
;
; purpose: plots and over plots a series of spectra for direct comparison
;
; inputs: data  data structure
;         index structure
;         channel - data channel you are interested in 0,1,2,3
;         dset_arr - array of dataset numbers - the first is plotted
;                    the rest are overplotted.
;
; optional inputs: title - the plot title
;			   if not specified the channel and date from the
;                          first index are used
;		   decomp - do bcs decompression on the data
;                          default is to not decompress 
;                  line - put a different line style on each plot
;			   default is to use solid for each
;                  scale - vector of same length as dset_arr containing
;                          the scale factors associated with each index.
;                          default is to not scale.
;                  noextract - will not extract the data using the bcs_grp_plan
;                          (useful if you have already extracted by channel)
;                          (or are after a sub array )
;                           This may allow use of bsd files in this program
;		   bsd     - use bsd file structures
;
; side effects: a plot is produced on the current graphics device.
;-
;
; first do sanity checks and setup flags etc....
;
;stop

if (n_elements(dset_arr) lt 0) then begin
    print,'Less than two data sets selected'
    print,'usage: plotca,index,data,dset_arr,title=title,decomp=decomp,line=line,scale=scale' 
    return
    endif 

ndset = n_elements(dset_arr)

if (keyword_set(scale)) then begin
    if (n_elements(scale) ne n_elements(dset_arr)) then begin
     print,'Number of elements in scale array must match that in dataset array'
     return
     endif
     sscale = scale
    endif else begin 
    sscale = intarr(ndset) + 1
    endelse


if (not keyword_set(title)) then begin
    ist = dset_arr(0)
    if (keyword_set(bsd)) then begin
      time = fmt_tim(anytim2ex(index(channel-1,ist).time(*)))
      schannel  = channel-1
      endif else begin
      time = fmt_tim(intextime(gt_day(index(ist)),gt_time(index(ist))))
	schannel = channel
      endelse
   
    case schannel of 
     0: ion = 'FeXXVI'
     1: ion = 'FeXXV '
     2: ion = 'CaXIX '
     3: ion = 'SXV   '
     else: begin
       print,'Channel ',string(format='(i4)',channel),' not in range 0-3'
       return
       end
    endcase
    ptitle = ion+' '+strmid(time,0,9)
    endif else ptitle = title

if (not keyword_set(line)) then begin
	lflag = 0 
	histflag = 0
	endif else begin
	if (line eq 10) then begin 
		histflag = 1	
		lflag = 0
		endif else begin
		histflag = 0
		lflag = 1
		endelse
	endelse

if (not keyword_set(decomp)) then decoflag = 0 else decoflag = 1

if (keyword_set(noextract)) then noextrflag = 1 else noextrflag = 0

if (keyword_set(bsd)) then begin
	noextrflag = 1
        decoflag=0
	endif 
;
; do plotsetup
;
!p.multi = 0
xtitle  = "Bin Number"
if (keyword_set(bsd)) then ytitle = "Counts/bin/sec" else ytitle = "Counts"
;

for i = 0,ndset-1 do begin
 if (lflag) then !p.linestyle = (i mod 6) else !p.linestyle = 0
	
 kkkk = dset_arr(i)
 ;
 ; read data array, decompress and scale
 ;
 ; extract channel
 ;
 if (noextrflag) then begin
  if (keyword_set(bsd)) then begin
	tdata = data(channel-1,kkkk).counts(*)
	endif else begin
        tdata = data(*,kkkk)
	endelse
   endif else begin
   bcs_grp_plan,index(kkkk).bcs.modeid,ngroup,group,nsamp
   koffset = total(nsamp(0:channel)) - nsamp(channel)
   tdata=data(koffset:koffset+nsamp(channel)-2,kkkk)   
   endelse
 ;
 if (decoflag) then tdata = bcs_decomp(tdata)
 tdata = tdata*sscale(i)
 ntdata = n_elements(tdata)
 ;
 ; plot
 ;
 xarr = (findgen(ntdata)*256)/ntdata
 if (i eq 0) then plot,xarr,tdata,xtitle=xtitle,ytitle=ytitle,title=ptitle,charsize=1.6 else begin 

 if (histflag eq 1) then !psym = 10 else !psym=0;histogram mode 
 oplot,xarr,tdata

 endelse
 if (keyword_set(bsd)) then begin
      time = fmt_tim(anytim2ex(index(channel-1,kkkk).time(*)),kdays,ktims,/msec)
      endif else begin
      time=fmt_tim(intextime(gt_day(index(kkkk)),gt_time(index(kkkk))),kdays,ktims,/msec)
      endelse

 if (keyword_set(atp)) then begin
    pstring = string(format='(i5)',dset_arr(i))+' sf'+ $
     string(format='(f4.1)',sscale(i))+'  '+ktims
    endif else begin
    pstring = ' X'+ $
     string(format='(f4.1)',sscale(i))+'  '+ktims
    endelse

 if (channel ge 3) then xstrpos=0.6 else xstrpos=0.2  
 xyouts,xstrpos,.87-(i*.03),/normal,pstring

 endfor

end

 
