#!/bin/csh -f
#
# latextable - collect the basic data files into a latexable table
#    -- one row for each pattern
#    -- columns for rsectors(cache, gen nosort, gen sort), contig (cache, gen)
#    -- columns for ratios
set usage='usage: latextable patfile layout'
#
# Part of
#              The STARFISH Parallel file-system simulator
#        (Simulation Tool for Advanced Research in File Systems)
# 
#                               David Kotz
#                           Dartmouth College
#                              Version 2.0
#                              January 1995
#                          dfk@cs.dartmouth.edu

set P=`runbin`	# where to find awk scripts

if ($#argv != 2) then 
	echo "$usage"
	exit 1
endif

set patfile="$1"
set layout="$2"

set tmp=/tmp/latextable
set rawdata=$tmp/rawdata
set cvfile=$tmp/cvfile

# goto reawk

rm -rf $tmp
onintr cleanup
mkdir $tmp
set files=()

foreach rsize (8 8192)
    foreach sort (nosort sort)
	if ($layout == contig && $sort == sort) continue

	foreach fs (general)  		# cache is implied, in .thruput files
		set datafile=$tmp/$rsize.$fs.$sort.$layout
    	    	echo > $datafile
    	    	set files=($files $datafile)

    	    	echo Data for $rsize.$fs.$sort.$layout

            	set dir=configs/$rsize.1280.$fs.$sort.$layout.16/results
        	if (! -r $dir) then
        	    echo missing $dir
    	    	    continue
    	    	endif

    	    	foreach pat (`cat $patfile`)
    	    	    if ($rsize == 8 && ($pat =~ w?n || $pat == wrlw)) then
    	    	    	echo 0 0 0 yes >> $datafile
    	    	    else
    	    	    	set tfile=$dir/$pat.16.16.thruput
    	    	    	if (! -r $tfile) then
    	    	    	      echo missing $tfile
    	    	    	      continue
    	    	    	endif

			# find out if ratio is statistically significant
			# either "yes" or "no"
			set signif=`grep $pat.16.16 $dir/significance | cols 1`

    	    	    	# grab average thruput in MB/s, and average ratio
			# columns in .thruput file: general cache general/cache
    	    	    	avg a < $tfile \
			   | dm x2/1024. x1/1024. x3 \"$signif\" >> $datafile

    	    	    	colex 3 < $tfile | avg c >> $cvfile
    	    	    endif
    	    	end
    	end
    end
end

# Make the left-hand side, a list of patterns and record sizes
set datafile=$tmp/pats
sed '/^$/d' $patfile > $datafile
set files=($datafile $files)

# Now collect all the columns into one big file
abut $files > $rawdata

reawk: 

# Now, run it through awk to get it formatted right
nawk -f $P/latextable.awk "layout=$layout" $rawdata > table.tex

set maxcv=`stats max < $cvfile`
echo '' >> table.tex
echo '{\small Maximum coefficient of variation on average of ratios was '$maxcv'.}\\' >> table.tex
echo '{\small Ratios in {\em italics\/} do not represent a statistically significant difference at the 95\% confidence level; all others do.}\\' >> table.tex

echo output is in table.tex

cleanup:
# rm -fr $tmp
