pro table_dump, infil0, strec, year=year, dir=dir, rec1=rec1
;
;+
;NAME:
;	table_dump
;PURPOSE:
;	To write an SXT table to a text disk file and a binary
;	disk file.
;INPUT:
;	infil0	- Raw telemtry input file.
;	strec	- Starting record where the dump begins (the
;		  engineering program "table" or the reformatter
;		  has searched until it found a table dump)
;	year	- The year of the dump (since the raw telemetry
;		  does not have the year)
;	dir	- The output directory
;OPTIONAL INPUT:
;	rec1	- The telemetry major frame record with the dump
;		  If this is passed, the raw file is not read.
;HISTORY:
;	Written 21-Nov-91 by M.Morrison (taking TABLE1 as the starting
;			point.
;	18-Dec-91 (MDM) - Routine was only displaying one of 4 PFI/FFI
;			  sequence entry tables
;	25-Mar-92 (MDM) - Changed to use "table_print" routine (extracted
;			  the print portion and made that new routine)
;        4-Sep-94 (SLF) - replaced all the spawn,'rm' with file_delete calls
;-
;
if (n_elements(dir) eq 0) then begin
    dir_btb = '$DIR_SXT_TABLES/'
    dir_tab = '$DIR_SXT_ATABLES/'
end else begin
    dir_btb = dir
    dir_tab = dir
end
;
infil = findfile(infil0)
infil = infil(0)
break_file, infil0, dsk_log, dir0, fil, ext
fil = fil + ext
;
if (not keyword_set(rec1)) then rd_telem, infil, strec, rec1	;only read data file if the major frame was not passed
;
ttim2ex, rec1, tarr, year
tim_str = fmt_tim(tarr)
;
fileid = string(tarr(6), tarr(5), tarr(4), format='(3i2.2)') + '.'
for i=0,1 do fileid = fileid + string(tarr(i), '(i2.2)')	;this version only has HHMM
fileid0 = fileid						;old fileID, which did not have the seconds in the filename
fileid = fileid + string(tarr(2), format='(i2.2)')
;
;spawn, 'rm -f ' + dir_tab + 'tab' + fileid0		;remove old file
file_delete, dir_tab + 'tab' + fileid0 
;spawn, 'rm -f ' + dir_btb + 'btb' + fileid0
file_delete, dir_btb + 'btb' + fileid0
;
outfil = dir_tab + 'tab' + fileid
;spawn, 'rm -f ' + outfil				;remove it first since it might be owned by someone else and the
file_delete,outfil
							;program would bomb
print, 'Found a table.  Output file = ', outfil
;
table = rec1(16:*, *)			;drop first 16 bytes of each minor frame
table = byte(table,0, 16,8,64)		;16x8 by 64 minor frames
table = table(8:15, *, *)
table = reform(table, 64*64)
;
;---- also write out binary file
;
outfil2 = dir_btb + 'btb' + fileid
;spawn, 'rm -f ' + outfil2 				;remove it first since it might be owned by someone else and the
file_delete,outfil2                                                         ;program would bomb^P

openw, lun2, outfil2, /get_lun
writeu, lun2, table
free_lun, lun2
;
dummy = get_seq_tab(180, /qforcerd)	;update the table list in common blk
;
;--------------------------- Write out text info for sequence table
;
table_print, fileid, dir_tab
;
spawn, 'chmod g+w ' + outfil		;allow different person to delete the file to run the reformatter
spawn, 'chmod g+w ' + outfil2		;allow different person to delete the file to run the reformatter

end

