#!/bin/csh -f
#
# cvident - list the cv with each filename, sorted by cv
#    This goes with cvplot. See that for some explanation. This
# program pairs the cv with the filename, and sorts it, so it is easy
# to find out who has the high cv
# 
# usage:
#   cvident column file.data...
# where column is a column number in the data files (eg 3 is total time). 

if ($#argv < 2) then 
    echo 'usage: cvident column file.data...'
    exit 1
endif

set column=$1
shift

set tmp=/tmp/cv$$
set cv=$tmp.cv
set names=$tmp.names

onintr cleanup

echo>$cv
echo>$names

foreach i ($*)
    echo $i >> $names
    if ($i =~ *.data-a) then
        cat $i | cols $column | transpose | dm 'x1==0 ? 0 : x2/x1' >> $cv
    else
        cat $i | cols $column | avg c >> $cv
    endif
end

# sort -n won't take exponential notation so we force it back to %f format
abut $cv $names | awk '{printf "%.8f %s\n", $1, $2}' | sort -n

cleanup:
rm -f $tmp.*
