;+
; PROJECT:
;	SDAC
; NAME: 
;       GAP_FINDER
;
; PURPOSE:
; GAP_FINDER finds the time intervals for which there is data within
; a BDB file.  Gaps of less than 1 minute are ignored.
;
;
; CATEGORY:
;       BATSE
;
; CALLING SEQUENCE:
;       gap_finder, bdbfile, fileend, goodtimes, count
;
; CALLS:
;	anytim, read_dd
;
; INPUTS:
;   bdbfile - input, bdb file name (without the BATSE_DATA part)
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;  fileend - output, end time of file in r*8 seconds relative to 79/1/1,0
;   goodtimes - output, array of r*8 seconds relative to 79/1/1,0 giving
;               the start and end of the good intervals.  Dimensioned
;               (2,count)
;   count - number of time intervals in array goodtimes
;
; OPTIONAL OUTPUTS:
;	none
;
; KEYWORDS:
;	none
; COMMON BLOCKS:
;	sec_array
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
; Kim Tolbert  5/18/92
; Version 2, richard.schwartz@gsfc.nasa.gov, 22-oct-1997.
;	now uses read_dd to read the bdb file.
; Version 3, richard.schwartz@gsfc.nasa.gov, 24-oct-1997.
;	adjusted start and end of good times to real acc. edges. and
;	fixed final record bug.
;-
pro gap_finder, bdbfile, fileend, goodtimes, count
;

count = 0

;
read_dd, file=bdbfile, seconds, /noyarray,dd_open=dd_open, error=error
if error then goto, errorexit

print, 'Number of records in BDB file ',bdbfile,' is ', dd_open.numrec
if dd_open.numrec le 0 then goto,errorexit
nrec = n_elements(seconds)



filestart = seconds(0)
fileend = seconds(nrec-1)
print,'Last time in file = ',anytim(fileend,/hxr)

diff = seconds(1:*) - seconds
gapind = where (diff gt 62., count)
if count gt 0 then begin
   count = count + 1
   goodtimes = reform ([seconds(0)-0.512, seconds(gapind+1)-0.512, $
                   seconds(gapind)+0.512, seconds(nrec-1)+0.512], count, 2)
   goodtimes = transpose (goodtimes)
;   print,'goodtimes = ',goodtimes
endif else begin
   print,'No gaps found'
   count = 1
   goodtimes = [seconds(0), seconds(nrec-1)]
endelse
;
print,form='(a)', anytim(goodtimes(0,*),/hxr)+' - '+ anytim(goodtimes(1,*),/hxr)  
;
return


errorexit:
print,'Error opening or reading bdb file: ', bdbfile

end
