;+
;
; NAME: 
;	GET_SLLD
;
; PURPOSE:
;	Find the SPEC LLD values from the database closest to the input time.
;
; CATEGORY:
;	BATSE
;
; CALLING SEQUENCE:
;        sllds = GET_SLLD( Ut )
;
; CALLED BY:
;
;
; CALLS:
;	READ_SEQFILE, ANYTIM, UTIME, CONCAT_DIR, UNIQ, PRINTX
;
; INPUTS:
;       Ut - time in ANYTIM format
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       The result is 
;	Sllds - The SPEC LLD for each of the 8 BATSE SPECs
;
; OPTIONAL OUTPUTS:
;	Ut_slld - returns the time (anytim format) and slld's in a structure
;	with tags UT and SLLD
;
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;
;	for a date ut return the 8 spec lld values
;	for the event.  Extracted from perm_data:batse_slld.dat,
;	the values are chosen for the event immediately preceding the
;	time passed in ut.
;
; MODIFICATION HISTORY:
;	fully revised, ras, 27-dec-1995
;	ras, 2-jan-1996, revised to use batse_slld2.dat
;	ras, 2-dec-1996, added ut_slld output structure
;               RAS, 7-Jul-1997, changed PERM_DATA to SSWDB_BATSE
;-


function get_slld, ut, ut_slld
common get_slld_com, event_time, slld

if n_elements(event_time) eq 0 then begin

;	ras, 2-jan-1996

	;dfile = concat_dir(chklog('SSWDB_BATSE'),'batse_slld.dat')
	;dfile = concat_dir(chklog('SSWDB_BATSE'),'batse_slld2.dat')  
	dfile = loc_file( 'batse_slld2.dat',path='SSWDB_BATSE')
;	ras, 2-jan-1996


	read_seqfile, buff, dfile(0) 
        buff = buff(where(buff ne ''))
	buff = buff(1:*)
	start = strpos(buff(0),'|') + 1
	ymd = strmid(buff,start,2)+'/'+strmid(buff,start+2,2) $
		+'/'+strmid(buff,start+4,2)
	sec10 = fix( strmid(buff,8,4 )) * 10L; 10's of seconds
	event_time = utime(ymd) + sec10

	slld = intarr(8,n_elements(ymd))

	reads, strmid(buff,15,60), slld
	s = uniq( event_time, sort(event_time))
	event_time = event_time(s)
	slld = slld(*,s)
	dslld= total( abs(slld(*,1:*)-slld),1)
	wdf  = where( dslld ne 0)
	wdf  = [0,wdf+1]
	event_time = event_time(wdf)
	slld = slld(*,wdf)
endif

; Calibrate for slld to within an hour of the event
ipick = where( (anytim(ut,/sec))(0) ge (event_time-300.), npick)
ipick = ipick(npick-1) > 0

;if total( (slld(*,ipick)-slld(*, ipick-1>0 ))^2 ) ne 0. then begin
;	printx,'SPEC LLD values different for previous event'
;	printx,'For event at '+atime(event_time( ipick-1>0 ))
;	printx,string(/print,'SPEC LLD = ',slld(*,ipick-1>0))
;	printx,'For event at '+atime(event_time( ipick ))
;	printx,string(/print,'SPEC LLD = ',slld(*,ipick))
;endif
if n_params() eq 2 then begin
	ut_slld = replicate( {ut_slld, ut: event_time(0), slld:slld(*,0)},n_elements(event_time))
	ut_slld.ut = event_time
	ut_slld.slld = slld
endif

return,slld(*,ipick)
end
