function livetime, counts, tau, det_id=det_id
on_error,2
;+
;
;  Name:
;       LIVETIME
;
; PURPOSE: Return BATSE discriminator livetime fraction
;
; CATEGORY: Data reduction, analysis, time histories, spectroscopy
;       BATSE
; CALLING SEQUENCE: fraction = livetime( countrate [,tau] )
;
; CALLS TO: Interpol
;
; INPUTS:
;	Counts - Array of counts in first four DISCLA channels by time
;		 organized nchan x nbin 
;
; OPTIONAL INPUTS:
;	Tau       - gate recovery time per event
;		  - default is value for .68e-6 seconds
;                 - must be scalar or same number of elements as countrate
;        det_id   - detector id (0-7)
; OUTPUTS:  livetime fraction
;
; PROCEDURE: Use BATSE_DEADTM to compute the livetime fraction for a
;		counter with paralyzable and non-paralyzable components.
;
; MODIFICATION HISTORY: ras, 3 Sep 93
;	fixed at low count rate, 26 Sep 93, ras
;	Complete overhaul using BATSE_DEADTM and PARA_LIVETIME, 4 Dec 93, ras
;       AES 94/07/12 changed comment about omni data - channel numbers have
;       all been moved down, so old channel 1 is now channel 0
;-

edges = discla_edges()
checkvar, det_id, 0
checkvar, tau, ([.579,.515,.627,.562,.44,.56,.496,.67]*1e-6)(det_id)
edges = edges(*,det_id) < 400. ;suppress the upper edge of channel 3
edge_products, edges, edges_2=channel_edges


;look for OMNI data, channel 2 has data and 3 is set to zero
qomni = where( counts(2,*) gt 0 and counts(3,*) eq 0, nomni)
if nomni ge 1 then counts(3,*) = 500

delta_t = 1.024
pha_dead = 1.722e-9
amp_dead = 1.33e-6

high_rate = where( total(counts(0:3,*),1) gt 2.0e5, nhigh)
if nhigh ge 2 then begin
	lims = limits(high_rate)
	high_rate = lims(0) + indgen(lims(1)-lims(0)+1)
	pileup = where( counts(1,high_rate) gt counts(0,high_rate) $
		or counts(2,high_rate) gt counts(1,high_rate), npile)
	if npile ge 1 then high_rate = high_rate(pileup)
endif

result= batse_deadtm(counts, channel_edges=channel_edges, delta_t=delta_t, $
	pha_dead=pha_dead, amp_dead=amp_dead, high_rate=high_rate,gate=tau )

if nomni ge 1 then counts(3,*) = 0
return, result
end
