pro table_print, fileid, dir, fil, prefix=prefix
;
;+
;NAME:
;	table_print
;PURPOSE:
;	Take a binary (BTB) SXT table and write a text format to
;	disk.
;INPUT:
;	fileid	- The fileID of the BTB file which should have an
;		  ASCII file made.  It should be the exact time.
;	dir	- The output directory name
;OPTIONAL INPUT:
;	prefix	- The prefix of the output filename.  If not specified
;		  it will be 'TAB'.  
;HISTORY:
;	Written 25-Mar-92 by M.Morrison (taking TABLE_DUMP as the starting
;			point.
;	8-Jul-92 (MDM) - Adjusted to use headline - the 2 lines of 
;			 comments in the table dump
;-
;
if (n_elements(prefix) eq 0) then prefix = 'tab'
;
tim_str = fmt_tim( fid2ex(fileid) )
;
outfil = concat_dir(dir, prefix+fileid)
spawn, 'rm -f ' + outfil		;remove it first since it might be owned by someone else and then program would bomb
openw, lun, outfil, /get_lun
;
;--------------------------- Write out text info for sequence table
;
;---- Page 1 (Entry, ROI, and Default exposure)
out = gtab_entry(itab=itab, fid=fileid)
headline = out(0:2)
table_header, lun, fil, tim_str, 0, itab, headline
for i=3,n_elements(out)-1 do printf, lun, out(i)
printf, lun, '  '
printf, lun, '  '
;
out = gtab_roi(itab=itab, fid=fileid)
for i=3,n_elements(out)-1 do printf, lun, out(i)
printf, lun, '  '
printf, lun, '  '
;
out = gtab_exp(itab=itab, fid=fileid)
for i=3,n_elements(out)-1 do printf, lun, out(i)
;
;---- Page 2 (Common)
out = gtab_comm(itab=itab, fid=fileid)
table_header, lun, fil, tim_str, 1, itab, headline
for i=3,n_elements(out)-1 do printf, lun, out(i)
;
;---- Pages 3 and 4 (FFI)
for j=0,3 do begin
    out = gtab_ffi(0, j, itab=itab, fid=fileid)
    if ((j mod 2) eq 0) then table_header, lun, fil, tim_str, 1, itab, headline
    for i=3,n_elements(out)-1 do printf, lun, out(i)
    if ((j mod 2) eq 0) then printf, lun, '  '
end
;
;---- Pages 5 and 6 (PFI)
for j=0,3 do begin
    out = gtab_pfi(0, j, itab=itab, fid=fileid)
    if ((j mod 2) eq 0) then table_header, lun, fil, tim_str, 1, itab, headline
    for i=3,n_elements(out)-1 do printf, lun, out(i)
    if ((j mod 2) eq 0) then printf, lun, '  '
end
;
spawn, 'chmod g+w ' + outfil		;allow different person to delete the file to run the reformatter

free_lun, lun
end

