#! /bin/bash
# Last edited on 2004-12-22 19:10:03 by stolfi

name="$1"; shift;
num="$1"; shift;
seeds=( $@ )

tmp="/tmp/$$"

otfile="${tmp}.eps"

gpfile="${tmp}.gnuplot"

nmax=200

cat <<EOF > ${gpfile}
set term postscript eps color "TimesRoman" 14
set output "${otfile}"
num=(${num})
expt=((num-2.0)/2.0)
nmax=(${nmax})
ymin=0.5
ymax=(${nmax}*2)
set yrange [ymin:*]
set logscale x
set logscale y
set ylabel "T(Z)"
set xlabel "Z"
EOF

printf \
    'plot (8*(x**expt)) title "(Z-Z*)^(n/2-1)" with lines' \
  >> ${gpfile}
tpfiles=()
for seed in ${seeds[*]} ; do 
  printf ', \\'"\n" >> ${gpfile}
  infile="${name}-${seed}.trs"
  tpfile="${tmp}-${seed}.dat"
  printf "${infile} -> ${tpfile}\n" 1>&2 
  cat ${infile} \
    | gawk '/./{ print $1; }' \
    | sort -b +0 -1g \
    | head -${nmax} \
    > ${tpfile}
  tpfiles=( ${tpfiles[*]} ${tpfile} )
  parms=( `cat ${tpfile} | compute-parms -v n="${num}"` )
  fmin=${parms[0]}
  coef=${parms[1]}
  printf '  "'"${tpfile}"'" using (column(1)-('"${fmin}"')):(column(0)+1) title "'"${seed}"'" with linespoints' >> ${gpfile}
done

printf "\n" >> ${gpfile}
printf "quit\n" >> ${gpfile}

gnuplot < ${gpfile}

cat ${otfile}

/bin/rm ${tpfiles[*]} ${otfile} 
/bin/rm ${gpfile}


