;+
; NAME:
;	BCS_DT_INTERP
; PURPOSE:
;	interpolate out zero deadtime correction factors
; CALLING SEQUENCE:
;	DTFAC=BCS_DT_INTERP(DTFAC,BDA_INDEX)
; INPUTS:
;       DTFAC        - [4,*] deadtime correction factors from BCS_DTFAC
;       BDA_INDEX    - BDA index structure
; OUTPUTS:
;       DT_FAC       - interpolated correction factors for all BSC channels
; PROCEDURE:
;                      Uses INTERPOL
; HISTORY:
;                      Written Apr'93 (DMZ, ARC)
;-

function bcs_dt_interp,dtfac,bda_index
                       
on_error,1

ndsets=n_elements(bda_index)
if ndsets eq 1 then begin
 message,'cannot interpolate single point spectra',/contin
 return,dtfac
endif

sz=size(dtfac)
if (datatype(bda_index) ne 'STC') or (sz(2) ne ndsets) then $
 message,'invalid inputs'

;-- get BDA times

msec_day=24l*3600l*1000l
time=gt_time(bda_index)
day=gt_day(bda_index)
tarray=time+(day-day(0))*msec_day

;-- now interpolate

message,'interpolating zero deadtime correction factors',/contin

ndtfac=transpose(dtfac)
for i=0,3 do begin
 tdata=dtfac(i,*)
 good=where(tdata gt 0, ngood)
 bad=where(tdata le 0,nbad)
 if (nbad gt 0) and (ngood gt 0) then begin
  if nbad gt ngood then begin
   message,'too many bad dead-time correction factors',/cont
   dtfac=0
   return,dtfac
  endif
  if ngood gt 1 then ndata=interpol(tdata(good),tarray(good),tarray) else ndata=tdata
  ndata(good)=tdata(good)
  ndtfac(i*ndsets)=ndata 
 endif
endfor
ndtfac=transpose(ndtfac)
 
return,ndtfac & end
            

