#! /bin/bash
# Last edited on 2014-04-07 02:11:34 by stolfilocal

# Arguments:
#
#   {SHOW} {PTSIZE} PREFIX}
#
# where
#
#   {SHOW} "SHOW" to display, "NOSHOW" not to.
#   {PTSIZE} is the size of dots.
#   {TITLE} the plot title.
#   {PREFIX} is the in/out file name prefix.
#
# Reads {PREFIX}.txt, which is supposed to contain lines in the format
#   "{DATE[i]} {TIME[i]} {U[i]} {B[i]} {P[i]} {S[i]}"
# as produced by {compute_optimum_strategy.gawk}.
# 
# Plots the optimum strategy.
# Writes {PREFIX}.png

show="$1"; shift
ptSize="$1"; shift
title="$1"; shift
prefix="$1"; shift

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

tmp="/tmp/$$"

pngfile="${prefix}.png"

export GDFONTPATH=.

gnuplot <<EOF
set term png size 2400,1200 font "courbd,18"
set output "${tmp}.png"
set title "${title}"
set lmargin 8
set rmargin 8

# Horiz axis is time:
set xdata time
set timefmt "%Y-%m-%d %H:%M:%S"

set format x "%Y-%m-%d"
set grid xtics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'

# set yrange [0.001:1000000000.0]
set ytics ( \
  0.00000000001, 0.00000000002, 0.00000000004, \
  0.0000000001, 0.0000000002, 0.0000000004, \
  0.000000001, 0.000000002, 0.000000004, \
  0.00000001, 0.00000002, 0.00000004, \
  0.0000001, 0.0000002, 0.0000004, \
  0.000001, 0.000002, 0.000004, \
  0.00001, 0.00002, 0.00004, \
  0.0001, 0.0002, 0.0004, \
  0.001, 0.002, 0.004, \
  0.01, 0.02, 0.04, \
  0.1, 0.2, 0.4, \
  1.0, 2.0, 4.0, \
  10.0, 20.0, 40.0, \
  100.0, 200.0, 400.0, \
  1000.0, 2000.0, 4000.0, \
  10000.0, 20000.0, 40000.0, \
  100000.0, 200000.0, 400000.0, \
  1000000.0, 2000000.0, 4000000.0, \
  10000000.0, 20000000.0, 40000000.0, \
  100000000.0, 200000000.0, 400000000.0, \
  1000000000.0, 2000000000.0, 4000000000.0, \
  10000000000.0, 20000000000.0, 40000000000.0 \
) 
set logscale y

set key bottom right

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

nozercol(k) = (column(k) == 0 ? 0/0 : column(k))
ptSize = ${ptSize}

plot \
  "${infile}" using (timecolumn(1)):(nozercol(3)) title "USD"   with linespoints pt 7 ps (ptSize) lt 1 lw 2.0 lc rgb '#228800', \
  "${infile}" using (timecolumn(1)):(nozercol(4)) title "BTC"   with linespoints pt 7 ps (ptSize) lt 1 lw 2.0 lc rgb '#cc7700', \
  "${infile}" using (timecolumn(1)):(nozercol(5)) title "Price" with linespoints pt 7 ps (ptSize) lt 1 lw 2.0 lc rgb '#7799bb'

quit
EOF

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

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

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