#! /bin/bash
# Last edited on 2014-03-18 20:31:40 by stolfilocal

# Plots the slumber prices with dot sizes depending on weight.
# 
# Arguments: {TITLE} {PMIN} {PMAX} {MINWT} {FNAME}
# 
# Where 
#   
#   {TITLE} is the plot's title;
#   {PMIN},{PMAX} is the range of prices for the vertical scale of the plot; 
#   {MINWT} is the minimum weight to assign to each point.
#   {FNAME} is a data file with one line per slumber time in format
#      "{DATE} {TIME} | {NDAY} | {QL} | {HUBI.VH0} | {HUBI.VH1} | {HUBI.VH2} | {HUBI.VD}"

title="$1"; shift  # Plot title.
pmin="$1"; shift   # Min price for plot range.
pmax="$1"; shift   # Max price for plot range.
minwt="$1"; shift  # Minimum point weight.
fname="$1"; shift  # Input file name. 

show="SHOW"

tmp="/tmp/$$"

timg="${tmp}-full.png"

color=( '#0022ff' '#ff0000' '#008800' '#8800dd' '#dd4400' '#0066ff' )

export GDFONTPATH=.:..

gnuplot <<EOF
# ----------------------------------------------------------------------
# Common definitions:

set timefmt "%Y-%m-%d %H:%M:%S" # For {timecolumn}.
set lmargin 10
set rmargin 4
set bmargin 2.5
set tmargin 2.5

set xtics mirror
set xdata time
set grid xtics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mxtics
set format x "%b/%d"

# Minimum point weight:
minwt = ${minwt}  # Min weight.
minps = 1.0       # Min point size.
refps = 5.0       # Max point size.
refwt = 1.0       # Weight for point size = {refps}.

load "plot_slumber_funcs.gnuplot"

# Plottable price:
price(ip) = ((column(ip) == 0) ? 0/0 : column(ip))

# ----------------------------------------------------------------------
# Plot "pr" - prices
set term png size 2400,800 font "courbd,18"
set output "${timg}"
set title "${title} - slumber prices 03:00--03:59 CST"

pmin = ${pmin}
pmax = ${pmax}

unset logscale y
set yrange [pmin:pmax]
set grid ytics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mytics

set key top right

wpsVV(iw) = wps(column(iw))

plot \
  "< grep -e '^20' ${fname}" using (timecolumn(1)):(price(8)):(wpsVV(28)) title "Pr(03:30)" \
    with points pt 7 ps variable lt 1 lw 1.0 lc rgb '#0077dd'
    
quit
EOF

if [[ -s ${timg} ]]; then
  oimg="${tmp}-red.png"
  convert ${timg} -resize '50%' ${oimg}

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

cat ${oimg}

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