;+
; PROJECT:
;	SDAC
; NAME: 
;       FIND_PACKET
;
; PURPOSE:
;
;
; 	This procedure returns the record number in a BATSE Daily Dataset (DD) stream
;	which contains the given time or the following record number if the time
;	occurs in a gap.
;
;
;
; CATEGORY:
;       BATSE
;
; CALLING SEQUENCE:
;
;
; CALLS:
;	ANYTIM, CONV_VAX_UNIX, OS_FAMILY, SC_SECONDS_ED
;
; INPUTS:
; 	Sec_find - time to search for. Format readable by ANYTIM.
; 	Dd_open -  structure containing info about open file including number 
;            of data records in file
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
; rec_found  -  returns record number containing start time (or first after)
;
; OPTIONAL OUTPUTS:
;	none
;
; KEYWORDS:
; 	error -    0/1 means no error/error
;	none
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	After an initial guess, a binary search is performed to find the record.
;
; MODIFICATION HISTORY:
;
; Kim Tolbert 4/28/91
;
; ras, 17-apr-95, make it compatible with stream files for vms/unix
; reconcile with read_dd, using conv_vax_unix identically
;	Version 3, richard.schwartz@gsfc.nasa.gov, 12-aug-1997, use conv_vax_unix
;	Version 4, richard.schwartz@gsfc.nasa.gov, 4-sep-1997, reconcile new conv_vax_unix
;	add check for pad_byte
; ras, 4-dec-1997, protect against dd_open without filename tag.
;-
pro find_packet, sec_find,  dd_open, rec_found, error=error

;
; Structures for BATSE data base file for the FDR (header) record and data
; records.
;
error = 0

sec_find = anytim (sec_find, /seconds)
;
;print, 'Number of records in file = ',dd_open.numrec
;
; If sec_find is before start of file, or is 0., then return rec_found=1
;
first_datarec = 1 + dd_open.num_audit_rec
;print, 'first_datarec = ',first_datarec
fstat = fstat(dd_open.lun)


if tag_exist( dd_open, 'FILENAME') then pad_byte = getenv('pad_byte_'+dd_open.filename) $
	else pad_byte = ''
if pad_byte eq '' then pad_byte = 0 else pad_byte=fix(pad_byte)
point_lun, dd_open.lun, first_datarec * (size_struct(dd_open.data)+pad_byte)

rec = dd_open.data
readu, dd_open.lun, rec

sec = sc_seconds_ed (conv_vax_unix(rec.sctime))

rec_start = sec(0)
if (sec_find lt rec_start) or (sec_find eq 0.d0) then begin
   rec_found = first_datarec
   goto,finish
endif
;
;
; If no records are missing, then the number of records to the start time is
; equal to difference in seconds between the file start and the requested start 
; divided by 2.048. (plus the number of audit records)
diff = sec_find - rec_start
rec_found = long(diff/2.048d0 + first_datarec)
;print,'sec_find =' ,sec_find,'  rec_start=',rec_start,'  diff=',diff
;print,'rec_found = ',rec_found
if rec_found lt first_datarec then rec_found = first_datarec
if rec_found gt dd_open.numrec then rec_found = dd_open.numrec
;
;rec = rec_ass (rec_found)  ; akt 9/27/93

point_lun, dd_open.lun, rec_found * (size_struct( dd_open.data)+pad_byte)

readu, dd_open.lun, rec

sec = sc_seconds_ed (conv_vax_unix(rec.sctime))

sec = sec(0)
;print,'Record # =', rec_found,  'time=', atime(sec)
if (sec le sec_find) and (sec_find lt sec+2.048) then goto,finish
if (sec_find ge sec+2.048) and (rec_found eq dd_open.numrec) then goto,bad
;
r1 = first_datarec  &  r2 = rec_found
sec1 = rec_start & sec2 = sec
;
retry:
; there must have been gaps. Do binary search for record containing start time.
rec_found = (r1 + r2) / 2

;rec = rec_ass (rec_found)   ; akt 9/27/93
point_lun, dd_open.lun, rec_found * (size_struct( dd_open.data)+pad_byte)

readu, dd_open.lun, rec

sec = sc_seconds_ed (conv_vax_unix(rec.sctime)) 

sec = sec(0)
;print,'record =', rec_found,  'time=', atime(sec)
;
if sec_find lt sec then begin
   r2 = rec_found & sec2 = sec
endif else if sec_find gt sec+2.048 then begin
   r1 = rec_found & sec1 = sec
endif else goto,finish
if (r2 - r1) le 1 then goto,gotit
goto,retry
;
gotit:     ; record we want is either r1 or r2 - determine which one
rec_found = r2
if sec_find lt (sec1 + 2.048) then rec_found = r1
;
finish:
;print,'Returning, record = ',rec_found
goto,getout
;
bad:
error = 1
print,'Requested time is beyond end time of input file.'
goto,getout
;
getout:
end
