#! /bin/bash
# Last edited on 2016-02-04 18:12:26 by stolfilocal

# Arguments:
#
#   {SHOW} {WHICH} {CONFPCT} {TITLE} {DPREF} {OPREF}
#
# where
#
#   {SHOW}    "SHOW" to display, "NOSHOW" not to.
#   {WHICH}   name of independent variable ("time", "vbtc", or "vnat").
#   {CONFPCT} the fraction of volume included in the confidence interval.
#   {TITLE}   the plot title.
#   {DPREF}   is the input file name prefix.
#   {OPREF}   is the output file name prefix.
#
# Reads "{DPREF}_r.txt" and "{DPREF}_i.txt" as produced 
# by {compute_future_uncertainty.gawk}.
# Writes {OPREF}.png

show=$1; shift
which="$1"; shift
confpct="$1"; shift
title="$1"; shift
dpref="$1"; shift
opref="$1"; shift

ifile="${dpref}_i.txt"
if [[ ! ( -e ${ifile}) ]]; then
  echo "** no file \"${ifile}\"" 1>&2 ; exit 1
fi

rfile="${dpref}_r.txt"
if [[ ! ( -e ${rfile}) ]]; then
  echo "** no file \"${rfile}\"" 1>&2 ; exit 1
fi

tmp="/tmp/$$"

pngfile="${opref}.png"

export GDFONTPATH=.

gnuplot <<EOF
set term png size 3200,1600 font "courbd,24"
set output "${tmp}.png"
set title "Relative price changes after {r} ${which} steps - ${title}"

# set logscale y
# set ytics ( \
#   0.01, 0.012, 0.015, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, \
#   0.1, 0.12, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, \
#   1, 1.2, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, \
#   10, 12, 15, 20, 30, 40, 50, 60, 70, 80, 90, \
#   100, 120, 150, 200, 300, 400, 500, 600, 700, 800, 900 \
# )

set grid xtics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mxtics

set grid ytics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mytics

confpct = ${confpct}
lotitle = sprintf("low %.1f%%", 100*(1.0-confpct)/2)
hititle = sprintf("high %.1f%%", 100*(1.0-confpct)/2)
plhtitle = "2*sigma range"

# e10(k) = exp(log(10.0)*column(k))
# e10m(k,c) = exp(log(10.0)*(c*column(k)))

e10r(k,r) = column(k)/sqrt(column(r))
e10rm(k,r,c) = c*column(k)/sqrt(column(r))

set key left
plot \
  "${ifile}" using 3:(e10r(8,3))     title "rel.change/sqrt(r)"  with points      pt 2 ps 1.3 lt 1 lw 2.0 lc rgb '#bb9988', \
  "${rfile}" using 1:(e10r(2,1))     title "average/sqrt(r)"     with linespoints pt 7 ps 1.0 lt 1 lw 2.0 lc rgb '#227700', \
  ""         using 1:(e10r(3,1))     title "deviation/sqrt(r)"   with linespoints pt 7 ps 1.0 lt 1 lw 2.0 lc rgb '#cc3300', \
  ""         using 1:(e10r(4,1))     title lotitle               with histeps                 lt 1 lw 1.5 lc rgb '#0022ff', \
  ""         using 1:(e10r(5,1))     title hititle               with histeps                 lt 1 lw 1.5 lc rgb '#0022ff', \
  ""         using 1:(e10r(6,1))     title "model dev./sqrt(r)"  with lines                   lt 1 lw 1.5 lc rgb '#ff6600', \
  ""         using 1:(e10rm(6,1,2))  title plhtitle              with lines                   lt 1 lw 2.0 lc rgb '#7700ff', \
  ""         using 1:(e10rm(6,1,-2)) notitle                     with lines                   lt 1 lw 2.0 lc rgb '#7700ff'
quit
EOF

convert ${tmp}.png -resize '50%' ${pngfile}

if [[ "/${show}" == "/SHOW" ]]; then
  display ${pngfile}
fi

rm -fv ${tmp}{-*,}.*
