#! /bin/csh -f 
# Last edited on 2008-02-04 20:49:46 by stolfi

set usage = "$0 [-eps EPS] < INFILE.pr2 > OUTFILE.gif"

# Reads a file with fields of the form PROB1 PROB2 WORD.
# Plots PROB1 aginst PROB2 in log scale, fudging zeros.

set eps = "0.00001"
set title = "word probabilities"

while ( ( $#argv > 0 ) && ( "/$1" =~ /-* ) )
  if ( ( $#argv >= 2  ) && ( "/$1" == "/-eps" ) ) then
    set eps = "$2"; shift; shift; 
  else if ( ( $#argv >= 2  ) && ( "/$1" == "/-title" ) ) then
    set title = "$2"; shift; shift; 
  else
    echo "invalid option $1"
    echo "usage: ${usage}"; exit 1
  endif
end

if ( $#argv != 0) then
  echo "usage: ${usage}"; exit 1
endif

set tmp = "/tmp/$$"

set prfile = "${tmp}.pr2"
set bmfile = "${tmp}.ppm"
set gffile = "${tmp}.gif"

cat > ${tmp}.pr2

gnuplot \
  -bg black \
  -pointsize 2 \
<<EOF 
set terminal pbm color medium
set output "${bmfile}"
set xlabel "observed"
set ylabel "computed"
set nokey
set title "${title}"
set logscale xy
eps = ${eps}
pmin = eps*0.49999;
pmax = 1.50001;
set xrange [pmin:pmax]
set yrange [pmin:pmax]
set size 1.0,1.2
plot \
  (x<eps?0/0:(x>1?0/0:x)) with lines lt 1, \
  (x<eps/2?0/0:(x>1?0/0:2*x)) with lines lt 2, \
  (x<eps?0/0:(x>2?0/0:x/2)) with lines lt 2, \
  "${prfile}" using \
    (sqrt(\$1**2 + eps**2)):(sqrt(\$2**2 + eps**2)) : \
    (0.4 * eps):(0.4 * eps) \
    title "words" with xyerrorbars lt 3 pt 1
quit
EOF

if ( ( -r ${bmfile} ) && ( ! ( -z ${bmfile} ) ) ) then
  ppmtogif < ${bmfile} > ${gffile}
  cat ${gffile}
  ( xv ${gffile}; /bin/rm -f  ${gffile} ) &
endif

/bin/rm -f ${prfile} ${bmfile}