#! /bin/bash
# Last edited on 2004-09-26 21:48:57 by stolfi

cmd=${0##*/}
usage="${cmd} {STEP} {FMT} {LABS} {TITLE} < {INFILE}.flo > {OUTFILE}.{FMT}"

# Reads from standard input a list of records of the form
#   
#   {COUNT} {VAL1} {VAL2} {WORD}
#   
# writes two superimposed plots in format {FMT} ("eps" or "png") 
# showing the number of times each value of {VAL1} and {VAL2}
# falls within each bucket of width {STEP}. The {TITLE} is 
# displayed in the plot. The {LABS} argument is a boolean
# that tells whether the H axis tics should be labelled.

function error()
{ 
  echo "${cmd}: $1" >&2; 
  echo "usage: ${usage}" >&2; 
  echo "(unknown)";
  exit 1;
}

if [ $# != 4 ]; then
  error "wrong number of parameters";
fi

step="$1"; shift;
fmt="$1"; shift;
labs="$1"; shift;
title="$1"; shift;

# Working files:
diffile="/tmp/$$.df2"
nfile="/tmp/$$-1.his"
rfile="/tmp/$$-2.his"
pltfile="/tmp/$$.${fmt}"

# Save input data
cat > ${diffile}

# Compute histograms of each column
for n in 1 2; do
  cat ${diffile} \
    | gawk -v n=${n} -v s=${step} \
        ' BEGIN{ split("",a); im=0; } \
          /./ { \
            d=$(n+1); i=int((d-1)/s); \
            a[i]++; if(i>im){im=i;} \
          } \
          END { \
            for (i=0; i<=im; i++) \
              { printf "%9.1f %7d\n", (i+0.5)*s+1, a[i]; } \
          } \
        ' \
    > "/tmp/$$-${n}.his"
done

# Chose X tic format
if [ $labs -ne 0 ]; then
  xfmt="%.0f"
else
  xfmt=""
fi

# Choose plotting options appropriate for format
if [ ".${fmt}" == ".eps" ]; then
  term='postscript eps mono "TimesRoman" 14'
  size="0.5,((0.31+${labs}*0.04)*5/7)"
elif [ ".${fmt}" == ".png" ]; then
  term='png small color'
  size="0.5,((0.31+${labs}*0.04)*0.666667)"
else 
  error "bad plot format ${fmt}"
fi

gnuplot <<EOF
set term ${term}
set size ${size}
set output "${pltfile}"
set title "${title}" +13.7,-4.7
set xrange [(-0.1*${step}):(57000+0.1*${step})]
set yrange [-0.1:*]
set format x "${xfmt}"
plot \
  "${nfile}" using (column(1)):(column(2)+0.01) \
    title "n" with histeps linetype 1, \
  "${rfile}" using (column(1)+0.1*${step}):(column(2)+0.01) \
    title "r" with histeps linetype 3
quit
EOF

cat ${pltfile} 

/bin/rm -f ${diffile} ${nfile} ${rfile} ${pltfile}