;+
; function get_tf_rec, fname, filnum, recnum
;
; Purpose: To return the a record from a ".tfr" file.
;          
; Arguments: 
;    1. fname - Name of the tfr file, not including the file number of the 
;               extension. (INPUT)
;    2. filnum - File Number
;    3. recnum - record number in file.
;
;  This procedure can be used to read a set of tfr files as though they were a 
; a single file. The procedure procedure updates filnum and recnum after
; reading the specified record, opening the next file in the series if 
; necessary.
;HISTORY:
;	Written 1994 by D.Mathur  
;       25-Apr-95 (MDM) - Added capability to get around FINDFILE
;                         limitations which were causing a false failure
;	19-Feb-96 (MDM) - Put a patch in for the case where there are more than
;			  100 extensions
;	 8-Aug-96 (MDM) - Added code to handle reading Stanford sci160k files
;-
function get_tf_rec, fname, filnum, recnum

common getrec, tfr_rec, lun_tfr, nfiles, tfr_files   ; Save these between calls
common lf_common,last_filnum

qstanford = getenv('MDI_STAN_SCI160K') ne ''

if (filnum ne last_filnum ) then begin 		; if need to open a new file
  if (last_filnum ne 0 ) then close, lun_tfr	; close the previous file
  file_search_str = fname + '*tfr'
  if (get_host() eq 'quake.Stanford.EDU') then file_search_str = fname + '/tfr'
  tfr_files = findfile (file_search_str, count=nfiles)
  if (nfiles eq 0) then begin     ;MDM added second chance 25-Apr-95
    break_file, fname, dsk_log, dir00, filnam00
    tfr_files = file_list(dir00, filnam00+'*tfr', /cd)
    if (tfr_files(0) eq '') then nfiles=0 else nfiles=n_elements(tfr_files)
  end
  if (nfiles eq 0) then return, -10
  ;
  if (nfiles ge 100) then begin			;MDM added 19-Feb-96
    break_file, tfr_files, dsk_log, dir000, filnam000
    tfr_extens = fix(strmid(filnam000, 14, 5))
    tfr_files = tfr_files(sort(tfr_extens))
  end
  ;
  tfr_name = tfr_files(filnum-1)
  openr, lun_tfr, tfr_name, get_lun = 1
  tfr_rec = assoc (lun_tfr, bytarr(1113))
  if (qstanford) then tfr_rec = assoc (lun_tfr, bytarr(1109))
endif

if (eof(lun_tfr)) then begin
  close, lun_tfr
  filnum = filnum + 1
  if (filnum gt nfiles) then begin
    filnum = filnum - 1
    free_lun, lun_tfr
    lun_tfr = -99
  endif else begin
    tfr_name = tfr_files(filnum-1)
    openr, lun_tfr, tfr_name
    recnum = 0
  endelse
endif

last_filnum = filnum
if (lun_tfr eq -99) then return, -1

tfr_blk = tfr_rec(recnum)
if (qstanford) then tfr_blk = [bytarr(4), tfr_blk]
recnum = recnum + 1

return, tfr_blk
end
