pro tfr_summary2, infil, redo=redo
;+
;NAME:
;	tfr_summary2
;PURPOSE:
;	To display information on a per-image basis for a tfr file.
;SAMPLE CALLING SEQUENCE:
;	tfr_summary2, file_list('/md65/log/sci160k','950921_222120*.tfr')
;	tfr_summary2, file_list('/md65/log/sci160k','95092*.tfr', /bydate)
;HISTORY:
;	Written 25-Sep-95 by M.Morrison
;-
;
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

n = n_elements(infil)
if (infil(0) eq '') then return
;
for ifil=0,n-1 do begin
    infil0 = infil(ifil)
    break_file, infil0, dsk_log, outdir, filnam, ext
    infil0_notfr = outdir + filnam
    outfil = outdir + strmid(filnam, 0, 13) + '.tfrs2'
    ext_num = fix(strmid(filnam, 14, 4))
    ;
    qneed_head = (not file_exist(outfil)) or (keyword_set(redo) and (ifil eq 0))
    if (qneed_head) then begin
        openw, lun, outfil, /get_lun
	printf, lun, 'TFR_SUMMARY2 Program Initially Run: ' + !stime
	printf, lun, '      File: ' + outfil
	printf, lun, ' '
	printf, lun, ' Line# Exten# Img#  St  End    DPC     SeqID  #rec  Comp      EGSE Date/Time               Image
	printf, lun, '                   Rec  Rec                    DPC Factor                         Min     Max     Avg     Dev
                     ;     2    1   2  1432  2320 cfffff25 c43f0fff  889  62.1   21-SEP-95  22:23:34 -1690.0   617.0  -230.7   279.8
 	linn = 0L
    end else begin
	linn = 0L
	spawn, ['tail', '-1', outfil], result, /noshell
	on_ioerror, skip		;protect against case where the last line does not have a number
	reads, result, linn
	skip:
	on_ioerror, NULL
	linn = linn + 1
	openu, lun, outfil, /get_lun, /append
    end
    ;
    list_tfi, infil0_notfr, info, /quiet		;no extension
    openr, lun2, infil0_notfr+'.tfr', /get_lun		;read the _01 file type .tfrx file
    a = assoc(lun2, bytarr(1113))
    nimg = n_elements(info)
    if (data_type(info) ne 8) then begin
	nimg = 0
	printf, lun, ' '
	printf, lun, ' ********** No data products in file with extension ' + strtrim(ext_num,2) + ' *************'
    end
    ;
    for i=0,nimg-1 do begin
	info0 = info(i)
	irec = info0.en_recnum
	rec = a(irec)
        dummy = long (rec, 9)
        if (qswap) then dec2sun, dummy
        ba = dummy and '3fffff'x
        block_addr = dummy and '3fffff'x
        proc_id = (dummy and 'c00000'x) / 2L^22
        case proc_id of
	    0: type = 'FILL'
	    1: type = 'COMP'
	    2: type = 'PACK'
	    3: type = 'RAW'
	endcase

        if (proc_id eq 1) or (proc_id eq 2) then begin
            decom_rec = tfr_decom(rec)
        end else begin
            decom_rec = fix(rec, 14, 549)
        end
        ndecom = n_elements(decom_rec)
	;
        read_mdi, infil0_notfr, i, img, head, tfr_head=tfr_head		;/nodata
        masked = tfr_head(2:3) and 65535
        nword_dpc = masked(1)*2L^16 + masked(0)
        addr = (rec(10) and '1F'x)*2L^16 + rec(11)*2L^8 + rec(12)
	nword_left = (nword_dpc-addr-ndecom)>0
	ntfr_dpc = info0.en_recnum - info0.recnum + 1
	if (i eq 0) then ntfr_fill = (info0.recnum-1) >0	$
		    else ntfr_fill = info0.recnum - info(i-1).en_recnum - 1
	comp_eff = (ntfr_dpc*549.) / nword_dpc * 100.

	imin = min(img)
	imax = max(img)
	idev = stdev(img, iavg)

	tim = fmt_tim( anytim2ints('1-jan-70', off=double(info0.time)))
	fmt = '(i6, i5, i4, 2i6, ' + $
			'1x, z8.8, 1x, z8.8, i5, f6.1, 3x, a, 4f8.1)'
	str = string(linn, ext_num, i, info0.recnum, info0.en_recnum, $
			info0.dpc, info0.seqid, ntfr_dpc, comp_eff, $
			tim, imin, imax, iavg, idev, format=fmt)
	print, str
	printf, lun, str
	linn = linn + 1
    end
    free_lun, lun
    free_lun, lun2
end
;
end