#! /bin/csh -f
# Last edited on 2002-01-17 16:29:47 by stolfi

set usage = "$0 [ -freqs | -counts ] [-noylabels] [-binom N S] [-format eps|png] [-size NUM,NUM] AFILE ATITLE ASCALE ALT APT  BFILE BTITLE BSCALE BLT BPT ..." 

# Input files must have records of the form 
#   LENGTH COUNT FREQ ...
# Uses the "FREQ" column to plot.
#
# If "-binom N" is specified, plost also a binomial distribution for
# N even flips, shifted by S.

set ylabfmt = "%g"
set size = "1.00,1.00"
set relative = 1
set ymax = "0.30"
set xmax = "20"
set format = "eps"
set binN = "0"
set binS = "0"
set showbinom = 0

while ( ( $#argv > 0 ) && ( "/$1" =~ /-* ) )
  if ( ( $#argv >= 1 ) && ( "/$1" == "/-noylabels" ) ) then
    set ylabfmt = "     "; shift
  else if ( ( $#argv >= 2 ) && ( "/$1" == "/-format" ) ) then
    set format = "$2"; shift; shift
  else if ( ( $#argv >= 2 ) && ( "/$1" == "/-size" ) ) then
    set size = "$2"; shift; shift
  else if ( ( $#argv >= 1 ) && ( "/$1" == "/-freqs" ) ) then
    set ymax = "0.30"; set relative = 1; shift
  else if ( ( $#argv >= 1 ) && ( "/$1" == "/-counts" ) ) then
    set ymax = ""; set relative = 0; shift
  else if ( ( $#argv >= 3 ) && ( "/$1" == "/-binom" ) ) then
    set binN = "$2"; set binS = "$3"; shift; shift; shift
  else if ( ( $#argv >= 2 ) && ( "/$1" == "/-ymax" ) ) then
    set ymax = "$2"; shift; shift;
  else if ( ( $#argv >= 2 ) && ( "/$1" == "/-xmax" ) ) then
    set xmax = "$2"; shift; shift;
  else 
    echo "bad option"; echo "usage: ${usage}"; exit 1
  endif
end

if ( $#argv < 5 ) then
  echo "usage: ${usage}"; exit 1
endif

set pfile = "/tmp/$$.gnuplot"
set ofile = "/tmp/$$.${format}"

if ( "/${format}" == "/eps" ) then
  set fmtline = 'postscript eps mono "TimesRoman" 24'
  set ltype = ( 1 2 3 4 5 6 7 8 9 )
  set ptype = ( 7 1 2 3 4 6 5 8 9 )
else if ( "/${format}" == "/png" ) then
  set fmtline = 'png medium color; set size 0.75,0.75'
  set ltype = ( 3 1 5 7 8 9 4 2 6 )
  set ptype = ( 1 2 3 4 5 6 7 8 9 )
else
  echo "invalid plot file format" 
endif

if ( -x /n/image/bin/xv ) then
  set showpng = /n/image/bin/xv
else
  set showpng = display
endif

cat > ${pfile} <<EOF
# set term x11
set term ${fmtline}
set output "${ofile}"
set size ${size}
set xrange [0:${xmax}]
set yrange [0:${ymax}]
set xtics 1
set format y "${ylabfmt}"
rel = ${relative}
binN = ${binN}
binS = ${binS}
EOF

set sep = "plot"
@ i = 1
if ( "/$binN" != "/0" ) then
  printf '%s \\\n  (x<=binS-1 ? 0 : (x >= binN+binS+1 ? 0 : gamma(binN+1)/gamma(x-binS+1)/gamma(binN-x+binS+1)/2**binN))' \
    "${sep}" >> ${pfile}
  printf ' \\\n    title "binom(%s,k-%s)" with lines lt 5' \
    "${binN}" "${binS}" >> ${pfile}
  set sep = ","
endif
while ( $#argv > 0 ) 
  if ( $#argv < 3 ) then
    echo "usage: ${usage}"; exit 1
  endif
  set file = "$1"; shift
  set title = "$1"; shift
  set scale = "$1"; shift
  set ltx = "$1"; shift
  set ptx = "$1"; shift
  printf '%s \\\n  "%s" using 1:((rel ? $3:$2)*(%s)) title "%s"' \
    "${sep}" "${file}" "${scale}" "${title}" >> ${pfile}
  printf ' \\\n     with linespoints lt %d pt %d' \
    "${ltype[$ltx]}" "${ptype[$ptx]}" >> ${pfile}
  set sep = ","
  @ i = $i + 1
end
printf '\n' >> ${pfile}
printf 'quit\n' >> ${pfile}

/usr/bin/gnuplot < ${pfile}

cat ${ofile}
if ( "/${format}" == "/eps" ) then
  ghostview ${ofile}
else if ( "/${format}" == "/png" ) then
  ${showpng} ${ofile}
else
  echo "invalid plot file format" 
endif

/bin/rm -f ${ofile} ${pfile}
