function get_seq_tab, timarr, filnam, fileid=fileid, fid=fid, filelist=filelist, qstop=qstop, qforcerd=qforcerd
;
;+
;Name:
;	get_seq_tab
;Purpose:
;	Given a time, return the sequence table serial number that was
;	used at that time.  Also return the filename of the table dump.
;Input:
;	timarr	- standard 7 element array with time of exposure
;		hh, mm, ss, msec, dd, mm, yy
;				(or)
;		  the sequence table number (a scalar)
;Optional Input:
;	filelist - If present, read the file "filelist" to get the list
;		  of SXT table dumps and the serial number associated
;		  with it.  If not present (which is most common), it
;		  looks at the files in the $DIR_SXT_TABLES directory
;		  and figures out the table number that way.
;	qforcerd - Force the "filelist" or the directory $DIR_SXT_TABLES
;		  to be read again (it holds information in a common block
;		  to avoid to have to read multiple times)
;	fid	- the fileID of the table to extract (string type) instead
;		  of a time or sequence table number
;Output:
;	returns	- table number that was used
;	filnam  - file name of table dump
;	fileid	- The fileID of the table dump
;HISTORY:
;	Written Fall '91 by M.Morrison
;	13-Mar-92 (MDM) Removed the technique of reading a data file to get
;			the list of table serial numbers.  Only use the
;			/ys/sxt/tables/tab* files kept on-line.
;	25-Mar-92 (MDM) Removed bug where the table selection was chosing
;			the wrong file if the exact time/FID was passed
;			(gt versus ge problem)
;	28-Aug-92 (MDM) Modified code to use new time routines available
;			Used "btb" files instead of "tab" files to generate
;			the list
;	 8-Mar-93 (MDM) Modified to use the new FILE_LIST routine because there
;			were too many files in the tables directory
;-
;
common blk1_get_seq_tab, timmat, sernum_arr, timstr
;
qdebug = 0
;
if ((n_elements(timmat) eq 0) or (keyword_set(qforcerd))) then begin
    dir = '$DIR_SXT_TABLES'
    ;ff = findfile(concat_dir(dir, 'btb*'))
    ff = file_list(dir, 'btb*', /cd)
    ff = ff(sort(file2time(ff)))	; Y2K fix - gls - 13-jan-2000
    n = n_elements(ff)

    timmat = intarr(7,n)
    sernum_arr = intarr(n)
    ;
    for i=0,n-1 do begin
	infil = ff(i)
	break_file, infil, dsk_log, dir0, filnam, ext
	p = 3
	timmat(0,i) = fix(strmid(ext, 1, 2))	;hours
	timmat(1,i) = fix(strmid(ext, 3, 2))	;minutes
	if (strlen(ext) eq 5) then timmat(2,i) = -1 $		;old file name convention
				else timmat(2,i) = fix(strmid(ext, 5, 2))	;sec
	timmat(4,i) = fix(strmid(filnam, p+4, 2))	;date
	timmat(5,i) = fix(strmid(filnam, p+2, 2))	;month
	timmat(6,i) = fix(strmid(filnam, p+0, 2))	;year
	sernum_arr(i) = 122 + i
    end
    timstr = anytim2ints(timmat)
end
;
n = n_elements(sernum_arr)
if (keyword_set(fid)) then begin
    timarr0 = fid2ex(fid)
end else begin
    timarr0 = timarr
end
nn = n_elements(timarr0)
;
fileid = ''
if (nn gt 1) then begin
    tim_ref = anytim2ints(timarr0)
    xxx = int2secarr(timstr, tim_ref)
    ii = where(xxx le 0, count)
    if (count gt 0) then ii = ii(count-1)
end else begin
    ii = timarr0-122	;-122 since that is the first seq table
end
;
case 1 of
   (ii lt 0): begin
        print, 'Trying to access before first available table', string(7b)
        print, 'Returning a zero'
        seq_tab_sernum = 0
        filnam = 'No File'
       end
   (ii gt n-1): begin 
        print, 'Trying to access after last available table', string(7b)
        print, 'Returning a zero'
        seq_tab_sernum = 0
        filnam = 'No File'
       end
    else: begin
        seq_tab_sernum = sernum_arr(ii)
        ;
        fileid = ''
        for j=6,4,-1 do fileid = fileid + string(timmat(j,ii), format='(i2.2)')
        fileid = fileid + '.'
        for j=0,1 do fileid = fileid + string(timmat(j,ii), format='(i2.2)')
	if (timmat(2,ii) ne -1) then fileid = fileid + string(timmat(2,ii), format='(i2.2)')
	filnam = 'tab' + fileid

	if (qdebug) then print, 'Requested ', fmt_tim(timarr0)
	if (qdebug) then print, 'Recieved  ', fmt_tim(timmat(*,ii))
       end
endcase
;
if (keyword_set(qstop)) then stop
return, seq_tab_sernum
;
end
