#! /bin/bash
# Last edited on 2009-12-12 00:21:11 by stolfi

# Plots reformatted new editor count raw data ("rer" format).
# Assumes that the first input file has the format 
# "{TIME} {YEAR} {MONTH} {DAY} {TOTEDS} {NEWEDS} {ACTEDS5} {ACTEDS100}"

pha="$1"; shift  # Which region of file to plot.
logscale="$1"; shift   # Use logscale for Y.
edtfile="$1"; shift  # File with editor data.
artfile="$1"; shift  # File with article data.

echo "${0##*/} pha = ${pha} edtfile = ${edtfile}" 1>&2

epsfile="/tmp/$$.eps"

if [[ ${pha} -eq 0 ]]; then
  xrange="set xrange [2000.8:2010.8]"
elif [[ ${pha} -eq 1 ]]; then
  xrange="set xrange [2000.8:2006.6]"
elif [[ ${pha} -eq 2 ]]; then
  xrange="set xrange [2005.3:2010.7]"
fi

if [[ ${logscale} -gt 0 ]]; then 
  ymax="100000*1.05"
  newmag=""
  a005mag=""
  a100mag=""
  ymin="(ymax/1.05*0.95/100000.0)"
  yscale="set logscale y"
  mytics="set mytics 2"
else
  if [[ ${pha} -eq 0 ]]; then
    ymax="4200*1.05"
    newmag="/4"
    a005mag="/14"
    a100mag="/2.3"
  elif [[ ${pha} -eq 1 ]]; then
    ymax="1000*1.05"
    newmag="/6"
    a005mag="/21"
    a100mag="/3.45"
  elif [[ ${pha} -eq 2 ]]; then
    ymax="1600*1.05"
    newmag="/10"
    a005mag="/40"
    a100mag="/4"
  else
    echo "invalid pha = [${pha}]" 1>&2
    exit 1
  fi
  ymin="(-ymax/1.05*0.05)"
  yscale=" "
  mytics="unset mytics"
fi

gnuplot <<EOF
set term postscript eps color solid "TimesRoman" 24
set size 1.5,1.2
set output "${epsfile}"
set key reverse left Left samplen 1.0

${xrange}
set grid xtics
set mxtics 12
set xtics 1.0 format "        %4.0f"
set xzeroaxis

ymax = ${ymax}
ymin = ${ymin}
set yrange [${ymin}:${ymax}]
${yscale}
${mytics}
set ytics format "%7.0f"
set grid ytics mytics

year(i)=(column(i)/365.25 + 2001)
plot \
  "${edtfile}" using (year(1)):(column(6)${newmag}) title "E0'(t)${newmag} (ed/mo)" with linespoints pt 6 ps 0.75 lc rgb '#ee00ee', \
  "${edtfile}" using (year(1)):(column(7)${a005mag}) title "E5(t)${a005mag} (ed)" with linespoints pt 7 ps 0.50 lc rgb '#0077ee', \
  "${edtfile}" using (year(1)):(column(8)${a100mag}) title "E100(t)${a100mag} (ed)" with linespoints pt 3 ps 0.50 lc rgb '#00aa77'
quit
EOF

cat ${epsfile}
gv ${epsfile}
rm -f ${epsfile}
