#!/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 configdir layout'
#
# Part of
#              The STARFISH Parallel file-system simulator
#        (Simulation Tool for Advanced Research in File Systems)
# 
#                               David Kotz
#                           Dartmouth College
#                              Version 3.0
#                              October 1996
#                          dfk@cs.dartmouth.edu

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

if ($#argv != 3) then 
	echo $usage:q
	exit 1
endif

set patfile="$1"
if (! -r $patfile) then
    echo $patfile not found
    exit 1
endif
set pats=(`cat $patfile`)

set configdir="$2"
if (! -d $configdir) then
    echo $configdir not a directory
    exit 1
endif

set layout="$3"

set nio=16
set ncomp=16
set ndisk=16

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

# 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=$configdir/$rsize.1280.$fs.$sort.$layout.$ndisk.[0-9]*/results
        	if (! -r $dir) then
        	    echo missing $dir
    	    	    continue
    	    	endif

    	    	foreach pat ($pats)
    	    	    if ($rsize == 8 && ($pat =~ w?n || $pat == wrlw)) then
    	    	    	echo x x x yes >> $datafile
    	    	    else
    	    	    	set tfile=$dir/$pat.$nio.$ncomp.thruput
    	    	    	if (! -r $tfile) then
    	    	    	      echo missing $tfile
			      echo x x x yes >> $datafile
    	    	    	      continue
    	    	    	endif

			set datafiles=($dir/$pat.$nio.$ncomp.*.data)
			echo $#datafiles >> $trials

if ($#datafiles < 5) echo $#datafiles $dir/$pat.$nio.$ncomp

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

			if ("$signif" == "") then
			      echo Assuming not significant differences
			      set signif=no
			endif

    	    	    	# 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)

reawk: 

echo > table.tex
echo 'Computed table on' >> table.tex
date                     >> table.tex
echo '\\'                >> table.tex
echo ''                  >> table.tex
echo '{\bf Throughput in MB/s}\\' >> table.tex
echo ''                  >> table.tex
echo "Nio = $nio\\"      >> table.tex
echo "Ncomp = $ncomp\\"  >> table.tex
echo "Ndisk = $ndisk\\"  >> table.tex
echo '\bigskip'          >> table.tex
echo ''                  >> table.tex

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

set mintrials=`stats min < $trials`
set maxtrials=`stats max < $trials`
echo '' >> table.tex
echo '{\small Number of trials: max '$maxtrials', min '$mintrials'.}\\' >> table.tex

if ($maxtrials > 1) then
	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
endif

echo output is in table.tex

cleanup:
rm -fr $tmp
