;+
; NAME:
;	BCS_NORM
; PURPOSE:
;	Normalizes data recorded by the BCS for time, and extracts the
;	Fast Queue data if present.
; CALLING SEQUENCE:  
;	dspec = bcs_norm(index,data)
; INPUTS:
;	index	the index to the data array
;	data	the decompressed data array
; OPTIONAL INPUTS:
;	notime	if present, only fast queue extraction is performed,
;		the data is not scalled for integration time.
;	nofq	if set, fast queue data not specially treated ???
; OUTPUTS:
;	The normalized array
; OPTIONAL OUTPUTS:
;	None
; RESTRICTIONS:
;	The data must have been decompressed prior to the call of this
;	routine.
;	If data has been "corrected", 100 should have been added to the
;	controltally field in the structure - such data is ignored by
;	this routine, and assumed to be normal.
; PROCEDURE:
;	Fast queue data is extracted and then the is normalized to
;	counts per second.
;
;	Note: Although BCS_NORM will run on all the spectra bins in a mode,
;	a better correction can be made if each channel is dealt with 
;	individually since frames with zero data can be handled better.
;
;	The call to correct a single channel is:
;	  dspec = bcs_norm(index,bcs_decomp(ext_bcschan(index,data,channel)))
;
; MODIFICATION HISTORY:
;	RDB  23-Oct-92	Written
;	RDB  25-Oct-92	Added nofq keyword, and check in incrementing of 
;			control tally
;	RDB  26-Oct-92	Only correct control tallies between 1 and 99
;	RDB  27-Oct-92  Only BlockID's of 1
;	RDB  14-Dec-92	Modified wz - only if zeros, then zero (n val.)
;-

function  bcs_norm,index,dspectra,notime=notime,nofq=nofq


idx_sz = size(index)
dat_sz = size(dspectra)

;		check index and data same dimension!!
if (idx_sz(0) ne 1) or (dat_sz(0) ne 2) or (idx_sz(1) ne dat_sz(2)) then begin
   print,'*** Index and Data arrays dimensions do not match ***'
   help,index,dspectra
   return,dspectra		;????????
endif

;	check dspectra real - i.e. decompressed
if dat_sz(3) ne 4 then begin
   print,'*** Supplied Data array is not Decompressed ***
   help,dspectra
   return,dspectra		;????????
endif


if not keyword_set(notime) then print,'Normalized by Integration Time'

;	data is affected, so work from saved array
ddspectra = dspectra

kdat = n_elements(index)-1

for k = 0,kdat do begin

;	TODO	data gaps...

if not keyword_set(nofq) then begin
;		Normalize the Fast Queue data
   if index(k).bcs.blockid eq 1 and  $
	(index(k).bcs.controltally gt 0 and index(k).bcs.controltally lt 100) and  $
	total(ddspectra(*,k)) gt 0 then begin
      if (k gt 0) then begin
         if index(k-1).bcs.blockid eq 1 and  $
            total(ddspectra(*,k-1)) gt 0 and  $
            (index(k).bcs.controltally - index(k-1).bcs.controltally eq 1) then begin
            dspectra(*,k) = ddspectra(*,k) - ddspectra(*,k-1)
         endif else begin
;               time needs adjusting???
            dspectra(*,k) = ddspectra(*,k)/(index(k).bcs.controltally+1)
         endelse
      endif else begin
;		time needs adjusting???
         if total(ddspectra(*,k)) gt 0 then $
            dspectra(*,k) = ddspectra(*,k)/(index(k).bcs.controltally+1)
      endelse

   endif
endif

;		Zero out any negative points ????
   wz = where(dspectra(*,k) le 0)
   if wz(0) ge 0 then dspectra(wz,k) = 0
;		Normalize for integration time unless inhinited
   if not keyword_set(notime) then dspectra (*,k) = dspectra(*,k)*(1./(index(k).bcs.dgi*0.125))

endfor

return,dspectra
end
