function tim2file, tim_in, dir_out, fileid, keyfile=keyfile, dirs=dirs, qstop=qstop
;
;+
;Name:
;       tim2file
;Purpose:
;       Given a time (or array of times) return the file that
;	the data should be in.
;Input:
;       tim_in - Can be a structure with the .TIME and .DAY
;                fields
;                       (OR)
;                The "standard" 7 element external representation
;                of time (HH,MM,SS,MSEC,DD,MM,YY)
;Optional Input:
;	keyfile	- The file prefix to use for the directory search
;		  If not present, it uses ADA.
;	dirs	- An array of the directories to search.  If not
;		  present, it calls "DATA_PATH".
;		  The path does NOT include a trailing '/'
;	qstop	- Stop at the bottom of the program for debuggin
;		  purposes.
;Output:
;       Returns the whole data path to the file in question
;
;	dir_out	- An array of the directory path.  The path
;		  DOES include a trailing '/'
;	fileid	- An array of only the fileIDs
;Method:
;	Perform a directory listing (with FINDFILE) on all of
;	the directories, sort, and find where the input time
;	falls.
;
;	TODO - Later?
;	What it should do is look at the observing log and
;	get a list of FileIDs
;
;       day_str - Optional) just the date part of the string
;       time_str - (Optional) just the time part of the string
;History:
;       19-Oct-91 by M.Morrison
;-
;
siz = size(tim_in)
if (siz(siz(0)+1) eq 8) then begin
    int2ex, tim_in.time, tim_in.day, daytim
end else begin
    daytim = tim_in
end
;
siz = size(daytim)
nout = 1           ;array dimension (# of different times/dates)
if (siz(0) eq 2) then nout = siz(2)
;
files = strarr(nout)
dir_out = strarr(nout)
fileid = strarr(nout)
;
if (not keyword_set(keyfile)) then keyfile='ada'
if (not keyword_set(dirs)) then dirs = data_paths()
;
for idir=0,n_elements(dirs)-1 do begin
    ff0 = findfile(dirs(idir) + '/' + keyfile + '*')
    if (ff0(0) ne '') then if (n_elements(ff) eq 0) then ff=ff0 else ff=[ff,ff0]
end
;
if (ff(0) eq '') then begin
    print, 'No ', keyfile, ' files in ', dirs
    print, 'Returning
    return, ''
end
;
n = n_elements(ff)
ff2 = strarr(n)
for i=0,n_elements(ff)-1 do begin                           ;strip off directory part of file name
    break_file, ff(i), dsk_log, dir0, filnam, ext
    ff2(i) = filnam + ext
end
;
for j=0,nout-1 do begin
    fileID0 = string(daytim(6,j), daytim(5,j), daytim(4,j), '.', daytim(0,j), daytim(1,j), FORMAT = '(3i2.2,a,2i2.2)')
    temp = keyfile + fileID0
    fff = [ff2, temp]
    ss = sort(fff)
    fff = fff(ss)
    i = where(fff eq temp)
    if (i(0) eq 0) then begin
        print, 'Selecting a time before first available data'
        print, 'Files available are:'
        for i=0,n-1 do print, ff(i)
        return, ''
    end else begin
	outfil = ff(i(0)-1)		;get the file right before the time in question
	break_file, outfil, dsk_log, dir0, filnam, ext
        files(j) = outfil
	fileid(j) = fileid0
	dir_out(j) = dir0
    end
end
;
if (nout eq 1) then begin	;convert to scalers
    files = files(0)
    fileid = fileid(0)
    dir_out = dir_out(0)
end
;
if (keyword_set(qstop)) then stop
return, files
end
