#!/bin/csh -f
#
# newextract-one - get a list of stats from one test case
#   This is very different from the old extract-one.  It only pulls
# out some interesting dependent variables, and does it in a way that
# makes the file more easily usable by other tools, and more easily 
# extendible.  It also incorporates the function of average directly.
#
#
# Part of
#              The STARFISH Parallel file-system simulator
#        (Simulation Tool for Advanced Research in File Systems)
# 
#                               David Kotz
#                           Dartmouth College
#                              Version 3.0
#                              January 1996
#                          dfk@cs.dartmouth.edu

if ($#argv < 1) then
	echo 'usage: newextract-one  pat.nio.ncomp  metric...'
	echo '  looks for files of the form pat.nio.ncomp.trial.sim*'
	echo '  produces  files of the form pat.nio.ncomp.data'
	echo "  'metric' is the short name for a metric" 
	exit 1
endif

set base=$1
shift
set metrics=($argv:q)

set trialfile=/tmp/newex$$.trials
onintr cleanup

# Extract those metrics from each of the sim files corresponding
# to the given base name.  
set ntrials=0
echo > $trialfile
foreach sim ($base.*.sim*)
    simex -sim $sim -noheader -shortname -sum $metrics >> $trialfile
    @ ntrials = $ntrials + 1
end

# now collapse the multiple trials into a file with format
#   metricname n mean sd cv
set data=$base.metrics
echo > $data
foreach metric ($metrics)
  # pull out the lines containing this metric, and then only the values
  # then compute some stats about them: n mean sd cv
  grep ^$metric $trialfile | colex 2 | stats n mean sd | \
	dm \"$metric\" s1 s2 s3 'x3 != 0 ? x3/x2 : 0' >> $data
end

# they should all have the same column 2, the number of trials
# (I don't need sort before uniq, because if they're not all the same,
# they're not all the same, and that's all I need to know)
set trials=`colex 2 < $data | uniq`
if ("$trials" != $ntrials) then
  echo $base': differing numbers of trials ('$trials') than files('$ntrials')'
  rm -f $data
endif

cleanup:
rm -f $trialfile
