#! /bin/bash
# Last edited on 2014-12-04 16:35:21 by stolfilocal

# Plots the traffic for one wallet.
# 
# Arguments: {DFILE} {CUM} {VAR} {INIDATE} {FINDATE} {WALLET} {OUTDIR}
# 
# Where 
#   
#   {DFILE}    is the input data file.
#   {CUM}      is 0 for daily counts and totals, 1 for cumulative totals.
#   {VAR}      is 0 for transfer counts, 1 for amount transferred.
#   {INIDATE}  first date present in file (for title and filenames).
#   {FINDATE}  last date present in file (for title and filenames).
#   {WALLET}   is the ID of the wallet (for titles).
#   {OUTDIR}   directory for output plots.
#  
# The input file (read from {stdin}) must contain one line per day in the 
# format "{DAY} { {DNUM} {DVAL} {CNUM} {CVAL} }.." where the four fields
# {DNUM}, {DVAL}, {CNUM}, and {CVAL} are repeated 3 times; 
# See {extract_wallet_daily_traffic.gawk} for details.
#
# The plots are called "{OUTDIR}/{INIDATE}--{FINDATE}-{num,val}-{day,cum}.{png,jpg}"
# 

dfile="$1"; shift   
cum="$1"; shift     
var="$1"; shift     
iniDate="$1"; shift # First date in file.
finDate="$1"; shift # Last date in file.
wallet="$1"; shift  
pdir="$1"; shift 

show="SHOW"

tmp="/tmp/$$"

if [[ ${cum} -eq 1 ]]; then
  titcum="cumulative"; xcum="cum"; 
  set_y_scale="unset logscale y; set yrange[(-ymin):]";
else
  titcum="daily"; xcum="day"; 
  set_y_scale="set logscale y; set yrange[(ymin):]";
fi
if [[ ${var} -eq 0 ]]; then
  titvar="Op count"; xvar="num";
  vsgn="+1"; ymin="1";
else
  titvar="Total btc"; xvar="btc";
  vsgn="-1"; ymin="0.01"
fi

title="Wallet ${wallet} -- ${iniDate}--${finDate} : ${titvar} (${titcum})"

oprefix="${pdir}/${iniDate}--${finDate}-${xvar}-${xcum}"

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

pfile="${oprefix}.png"
jfile="${oprefix}.jpg"

export GDFONTPATH=.:..

gnuplot <<EOF
set term png size 3000,1000 font "courbd,24"
set output "${pfile_full}"

# Input time format:
set timefmt "%Y-%m-%d"

set xdata time
set format x "%Y-%m-%d"

ymin = (${ymin})
${set_y_scale}

set title "${title}"

# Time (horiz axis):
tim(kcol) = (timecolumn(kcol))

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

set xzeroaxis lt 1 lw 3 lc rgb '#ffddaa'
set key left top

# Sign to multiply the output amounts: 
vsgn = (${vsgn})

# Columns of output, shuffle, input
kneg = 2 + 2*${cum} + ${var}
kzer = kneg + 4
kpos = kzer + 4

plot \
  "${dfile}" using (tim(1)):(column(kpos))      title "inputs"   with linespoints pt 7 ps 0.75 lt 1 lw 2.0 lc rgb '#008800', \
  "${dfile}" using (tim(1)):(vsgn*column(kneg)) title "outputs"  with linespoints pt 7 ps 0.75 lt 1 lw 2.0 lc rgb '#ff0000', \
  "${dfile}" using (tim(1)):(column(kzer))      title "shuffles" with linespoints pt 7 ps 0.75 lt 1 lw 2.0 lc rgb '#2244ff'
quit
EOF

if [[ -s ${pfile_full} ]]; then 
  convert ${pfile_full} -resize '50%' ${pfile}
  convert ${pfile} -quality 80 -resize '600x' ${jfile}
  if [[ "/${show}" == "/SHOW" ]]; then display ${pfile} ${jfile}; fi
fi

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