#! /bin/bash
# Last edited on 2017-05-14 22:14:08 by stolfilocal

# Plots some ref price and a polynomial fit to it
# 
# Arguments: {TITLE} {DMIN} {DMAX} {VMIN} {VMAX} {FNAME}
# 
# Where 
#   
#   {TITLE} is the plot's title;
#   {DMIN,DMAX} is the range of dates to plot (format "%Y-%m-%d");
#   {VMIN},{VMAX} is the range of prices for the vertical scale of the plot; 
#   {FNAME} is a data file with lines "{DATE} {REFPRICE} {FITPRICE}".
#   

title="$1"; shift  # Plot title.
dmin="$1"; shift   # Min date to plot.
dmax="$1"; shift   # Max date to plot.
vmin="$1"; shift   # Min price to plot.
vmax="$1"; shift   # Max price to plot.
fname="$1"; shift  # Data file name.

show="YES";      # "YES" to display plot.

tmp="/tmp/$$"

fullplotFile="${tmp}-full.png"
plotFile="${tmp}.png"

export GDFONTPATH=.:..

gnuplot <<EOF
set term png size 2400,1400 font "courbd,24"
set output "${fullplotFile}"

tticdays = 25*7              # Number of days per time tic interval.
tticsecs = 60*60*24*tticdays # Number of seconds per time tic interval.
tmtics =   5                 # Number of minor time tic intervals per major time tic interval.

set xdata time
xmin = strptime("%Y-%m-%d","${dmin}")
xmax = strptime("%Y-%m-%d","${dmax}")

xminplt = xmin-24*3600
xmaxplt = xmax+24*3600
set xrange [(xminplt):(xmaxplt)]

# X-axis tics
unset xtics
unset mxtics 
set xtics ( 0 )
xtimefmt = "%Y-%m-%d"
mticsecs = tticsecs/tmtics
xtime = xminplt
k = 0
while (xtime <= xmaxplt) {
    minor = ((k % tmtics) != 0)
    set xtics rotate by -60 add ( strftime(xtimefmt, xtime) xtime minor )
    k = k + 1
    xtime = xtime + mticsecs
}
set xtics mirror

set title "${title}"

# Input time:
set timefmt "%Y-%m-%d %H:%M:%S"
tim(kcol) = (timecolumn(kcol))

# Price field, adjusted by given rate:
pmd(kpmd) = (column(kpmd) == 0 ? 0/0 : column(kpmd))

vmin = ${vmin}
vmax = ${vmax}

set yrange [vmin:vmax]
set logscale y

if (vmax < 1000000) {
set ytics ( \
                       0.050, 0.070,            \
  0.10,  0.20,  0.30,  0.50,  0.70,             \
  1,       2,       3,       5,       7,        \
  10,      20,      30,      50,      70,       \
  100,     200,     300,     500,     700,      \
  1000,    2000,    3000,    5000,    7000,     \
  10000,   20000,   30000,   50000,   70000,    \
  100000,  200000,  300000,  500000,  700000,   \
  1000000, 2000000, 3000000, 5000000, 7000000   \
)
} else {
set ytics ( \
                                0.050,         \
  0.10,          0.20,          0.50,          \
  1.0,           2.0,           5.0,           \
  10.0,          20.0,          50.0,          \
  100.0,         200.0,         500.0,         \
  1000.0,        2000.0,        5000.0,        \
  10000.0,       20000.0,       50000.0,       \
  100000.0,      200000.0,      500000.0,      \
  1000000.0,     2000000.0,     5000000.0,     \
  10000000.0,    20000000.0,    50000000.0,    \
  100000000.0,   200000000.0,   500000000.0,   \
  1000000000.0,  2000000000.0,  5000000000.0,  \
  10000000000.0, 20000000000.0, 50000000000.0  \
)
}
set format y "%.2f"

set grid xtics  lt 1 lw 3 lc rgb '#eeeedd', lt 1 lw 2 lc rgb '#eeeedd'
set grid mxtics
set grid ytics  lt 1 lw 3 lc rgb '#eeeedd', lt 1 lw 2 lc rgb '#eeeedd'

set xzeroaxis lt 1 lw 3 lc rgb '#eeeedd'

set key left top

plot \
  "${fname}" using (tim(1)):(pmd(3)) title "fitted" with lines lt 1 lw 3.0 lc rgb '#ff2200', \
  "${fname}" using (tim(1)):(pmd(2)) title "actual" with lines lt 1 lw 2.0 lc rgb '#2244ff'
quit
EOF

if [[ -s ${fullplotFile} ]]; then 
  convert ${fullplotFile} -resize '50%' ${plotFile}
  if [[ "/${show}" == "/YES" ]]; then display ${plotFile}; fi
  cat ${plotFile}
fi

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