;+
pro list_tfi, fnam, info, str_info, quiet=quiet, outfil=outfil, blks=blks,$
	human=human	
;
; Procedure to list an index file
;HISTORY:
;	20-Sep-94 (MDM) - Added INFO output
;			- Changed format statement
;	 6-Feb-95 (MDM) - Changed to print out start time of data
;			  product based on packet time
;	 6-Feb-95 (MDM) - Added common block to save host name so
;			  don't have to spawn with call to GET_HOST
;			- Added FREE_LUN call
;			- Added check for non zero number of datasets
;	31-Mar-95 (MDM) - Changed format statement to display SeqID
;			- Print out a title line
;	 3-Apr-95 (MDM) - Added /QUIET keyword
;			- Added end file and frame to the output structure
;	 3-Apr-95 (MDM) - Close the unit if no data
;	15-Sep-95 (MDM) - Added OUTFIL option
;	22-Sep-95 (MDM) - Added STR_INFO output option
;	13-Nov-95 (MDM) - Modified to display the RefTime and Frame #
;	16-Nov-95 (MDM) - Modifed to run faster - Read all at once
;	11-Dec-95 (MDM) - Added the name of the file to the header of the
;			  output file
;	18-Dec-95 (MDM) - Added protection for .tfr files with no images
;	15-Jan-97 (CED) - Added human-readable DPC labels ("/human" switch)
;	24-Feb-97 (MDM) - Added file delete before open
;	19-Nov-99 (RIB) - When no data products are in a file, a warning message
;                         is placed in the file instead of deleting it
;-
common list_tfi_blk, host

qprint = (1-keyword_set(quiet))
if (keyword_set(outfil)) then begin
    file_delete, outfil
    openw, lun, outfil, /get_lun
    printf, lun, 'Input File: ' + fnam
    printf, lun, ' '
end

;---- MDM added 4-Apr-94
swap_os=['vms','ultrix','OSF']                  ; list of swap machines
chk=where(!version.os eq swap_os,nscount)       ; current OS in noswap list?
qswap = nscount ne 0                            ; assign boolean
;---- MDM added 4-Apr-94

;Check if the index files exists. If not create it.
tfi_name = fnam + '.tfrx'
if (n_elements(host) eq 0) then host = get_host()
if (host eq 'quake.Stanford.EDU') then tfi_name = fnam + '/tfrx'
tfi_file = findfile (tfi_name, count=ntfi)
if (ntfi eq 0) then mk_tfi, fnam

tfi_file = findfile (tfi_name, count=ntfi)
if (ntfi eq 0) then begin		;MDM added 7-Apr-94
    print, 'RDTFR: Telemetry file ' + fnam + ' NOT available'
    return
end

openr, lun_tfi, tfi_name, /get_lun
blk = lonarr(16)
readu, lun_tfi, blk	& if (qswap) then dec2sun, blk
nimg = blk(0)
if (nimg eq 0) then begin
    print, 'No data products in file:' + fnam
    info=0b
    free_lun, lun_tfi
    if (keyword_set(outfil)) then begin
        printf, lun, 'No data products in file!'
	free_lun, lun
;	file_delete, outfil
    end
    return
end
blks = lonarr(16, nimg)
readu, lun_tfi, blks
free_lun, lun_tfi	& if (qswap) then dec2sun, blks

;;rec = assoc(lun_tfi, lonarr(16))
;;blk = rec(0)	& if (qswap) then dec2sun, blk


openr, lun_tfi, tfi_name, /get_lun
blk = lonarr(16)
readu, lun_tfi, blk	& if (qswap) then dec2sun, blk
info = replicate({imgnum: 0L, filnum: 0L, recnum: 0L, en_filnam: 0L, en_recnum: 0L, junk1: 0L, dpc: 0L, time: 0L, $
		seqid: 0L, reftime: 0L, framen: 0L, spare: lonarr(5)}, nimg)
readu, lun_tfi, info		& if (qswap) then dec2sun, info
free_lun, lun_tfi
;
str_info = strarr(nimg)

if(not keyword_set(human)) then begin 
    tit = '    #  StFilRec EnFilRec ?   DPC       EGSE Time  SeqID   RefTime Frm   RefTime      EGSE Date/Time
	      ;    68  1  2077  1  2107 0  ff021003   30a7bcf8 c6888000 473a1b54 11   21:47:00    13-NOV-95  21:47:36
    if (qprint) then print, tit
    if (keyword_set(outfil)) then printf, lun, tit
    ;
    tim    = fmt_tim( anytim2ints('1-jan-70', off=double(info.time)) )
    reftim = gt_time( anytim2ints('1-jan-70', off=double(info.reftime)), /str )
    fmt = '(i6, 2(i3,i6), i2, 2x, z8, 3x, z8, 1x, z8, 1x, z8, i3, 3x, a, 4x, a)'
    ;
    for i=0,nimg-1 do begin
        str = string(blks(0:10,i), reftim(i), tim(i), format = fmt)
        if (qprint)              then print, str
        if (keyword_set(outfil)) then printf, lun, str
        str_info(i)       = str
    end
end else begin
    tit = '    #  StFilRec EnFilRec ?   Data Product                 SeqID   Frm   RefTime      EGSE Date/Time
    if (qprint)              then print,tit
    if (keyword_set(outfil)) then printf,lun,tit
    ;
    tim    = fmt_tim(anytim2ints('1-jan-70',off=double(info.time)) )
    reftim = gt_time(anytim2ints('1-jan-70',off=double(info.reftime)),/str)
    fmt = '(i6, 2(i3,i6), i2, 2x, z8, ": ", a,a, z3, 1x, a, 4x, a)'
    for i=0,nimg-1 do begin
	stra = strmid(dpcstr(blks(6,i),'obs'),0,33)
        str = string(blks(0:5,i),blks(6,i),stra,strmid("                                     ",0,33 - strlen(stra)),blks(10,i),reftim(i),tim(i),format=fmt)
        if (qprint)              then print, str
	if (keyword_set(outfil)) then printf,lun,str
	str_info(i)       = str
    end
end
		

;for i = 1, nimg do begin
;    blk = rec(i)	& if (qswap) then dec2sun, blk
;    tim = fmt_tim( anytim2ints('1-jan-70', off=double(blk(7))) )
;    reftim = gt_time( anytim2ints('1-jan-70', off=double(blk(9))), /str )
;    str = string(blk(0:10), reftim, tim, format = '(i6, 2(i3,i6), i2, 2x, z8, 3x, z8, 1x, z8, 1x, z8, i3, 3x, a, 4x, a)')
;    if (qprint)              then print, str
;    if (keyword_set(outfil)) then printf, lun, str
;    info(i-1).imgnum    = blk(0)
;    info(i-1).filnum    = blk(1)
;    info(i-1).recnum    = blk(2)
;    info(i-1).en_filnam = blk(3)
;    info(i-1).en_recnum = blk(4)
;    info(i-1).dpc       = blk(6)
;    info(i-1).time      = blk(7)
;    info(i-1).seqid     = blk(8)
;    info(i-1).reftime	= blk(9)
;    info(i-1).framen	= blk(10)
;    str_info(i-1)       = str
;endfor

if (keyword_set(outfil)) then free_lun, lun
end
