;+
; Project     : SDAC
;
; Name        : LAD_PHA_EDGES
;
; Purpose     : This function returns the LAD PHA edges in keV for the indicated detector.
;
; Category    : BATSE
;
; Explanation : The nominal edges are calculated using the LESTRADE coefficients and
;	the offsets computed by Rob Preece.
;
; Syntax      : edges = lad_pha_edges( det_id, ut)
;
; Examples    :
;
; Inputs      : Det_id- detector id, 0-7
;		Ut    - time in anytim format, used to obtain coefficients.
;		Xvec - 2x128 linear channel edges
;
; Opt. Inputs :
;
; Outputs     :
;		This function returns a 2X128 array of edges in keV.
;	
; Opt. Outputs:
;
; Keyword Inputs: 
;		NO_OFF- Return nominal LESTRADE edges without PREECE(MOD BY SCHWARTZ) offsets.
;		PATH  - Find the "bin_offsets_det#.dat" files here.
; Keyword Outputs: 
;		
; Common      :
;
; Restrictions:
;
; Side effects:
;
; History     : Adapted from SHERS_LOAD on 23-jul-1997, integrated with PREECE offsets.
;		Version 2, richard.schwartz, 8-feb-1998, added LLD cutoff channel and energy.
;		Version 2.1, 6-oct-1998, added protection on file search for bin offsets files.
;		There may be two versions in the database for a detector, and that was causing
;		problems that adding the wildcard should have fixed in the call to loc_file.
; Contact     : Richard.Schwartz@gsfc.nasa.gov
;-
function lad_pha_edges, det_id, ut, xvec, no_off=no_off, path=path


files = loc_file('bin_offsets_det'+strtrim(det_id,2)+'.dat*', $
	path=fcheck(path, [curdir(),'SSWDB_BATSE']),/all, count=nfiles)
if nfiles eq 0 then print, 'Cannot find bin_offsets_det file for this detector',det_id

read_seqfile, buffer, files(nfiles-1)
offset=fltarr(128)+1.
if not keyword_set(no_off) then if get_pendleton() eq 2 then reads, buffer(1:*), offset




xvec= [[findgen(65), findgen(31)*2+65, findgen(32)*8+127]*offset,1900]
coefs = get_lad_gain( ut)
evec  = coefs(0,det_id) + xvec *coefs(1,det_id) + xvec^0.5 * coefs(2,det_id)

xvec  = [xvec(0:n_elements(xvec)-2), 380] ;last channel number for purposes of deadtime
evec  = evec > 10.0
edge_products, xvec, edges_2=xvec
edge_products, evec, edges_2=evec

return, evec
end
