#!/bin/csh -f
#
# extract - extract one column of a set of data files 
#
# usage:
#   extract [-s] [-i | -ic] col file.data-a ...
# 
#  A list of the datum from that column from each file will appear on
# the standard output.
#  The -i flag will use the IMPROVEMENT in that column instead of the raw
# data. The file should be a prefetching file that also has a
# non-prefetched base. 
#  The -ic flag will use the improvement due to non-computation in that
# column instead of the raw data. The file should be a computation
# file that also has a non-computation base.
#  The -s flag will use the std.dev line from the data files instead.
#
# *** WARNING *** 
# The improvement used here is the negative of what is used everywhere
# else, so reductions are listed positive, increases are listed negative
# -- this fits the intuitive notion of most columns that smaller numbers
# are "better".

unset improve
unset improvecomp

if ($#argv < 2) then
    echo usage: 'extract [-s] [-i | -ic] col file.data-a ...'
    exit 1
endif

if ($1 == "-s") then
    set line=0
    shift
else
    set line=1
endif

if ($1 == "-i") then
	   set improve
	   shift
else if ($1 == "-ic") then
	   set improvecomp
	   shift
    endif
endif

set col=$1
shift

onintr cleanup
set base=/tmp/extract-base

if (! $?improve && ! $?improvecomp) then
	   awk 'NR%2=='$line $* | dm s$col
else
# note that for ic we do the % change backwards from the i case
    if ($?improve) then
    	   set files=`nonprefile $*`
	   awk 'NR%2=='$line $files | dm s$col > $base
	   awk 'NR%2=='$line $*     | dm s$col | abut $base - | \
	   		 dm 'if x1 == 0 then 0 else -(x2-x1)/x1*100'
    else
	   set files=`noncompfile $*`
	   awk 'NR%2=='$line $files | dm s$col > $base
	   awk 'NR%2=='$line $*     | dm s$col | abut - $base | \
	   		 dm 'if x1 == 0 then 0 else (x2-x1)/x1*100'
    endif
endif

cleanup:
rm -f $base 
