#! /bin/bash
# Last edited on 2012-10-18 22:15:58 by stolfilocal

# Plots mistreated reporters by candidate
# Source dataset named on the command line.

name="$1"; shift # Name of source
tmp="/tmp/$$"

pngfile="repo-${name}-big.png"

# Remove comments:
cat ${name}.tbl \
  | sed -e 's:[\#].*$::g' \
  | egrep -v -e '^[ ]*([\#]|$)' \
  | gawk \
     ' /[0-9]/{ 
         dt=$1; dy=$3; S=$5; H=$7;
         printf "%s | %3d | %5d | %5d \n", dt, dy, S, H; 
       } 
     ' \
  > ${tmp}-${name}.dat
  
export GDFONTPATH=.

gnuplot <<EOF

set term png font arialbd 18 size 800,480
set output "${tmp}.png" 
set xrange [270:305]
set yrange [-0.2:9.2]
set ytics 1
set grid ytics
set title "Prefeitura SP - Turno 2 - Reporteres Destratados"
set xtics ( \
  "01/10" 275, \
  "08/10" 282, \
  "15/10" 289, \
  "22/10" 296, \
  "29/10" 303 \
)
set mxtics 7
set grid xtics
set key left
B = 2.71828
d0 = 271.0
estS(x) = ((x < d0)|(x > 302) ? 0/0 : B**(((x-d0)/15)**2))
estH(x) = ((x < d0)|(x > 302) ? 0/0 : 0)
#test
plot \
   (estS(x)) notitle with lines lt 8 lc rgb '#0033ff', \
  "${tmp}-${name}.dat" using 3:5 title "Serra"  with linespoints pt 7 ps 2.0 lw 3.0 lc rgb '#0033ff', \
   (estH(x)) notitle with lines lt 9 lc rgb '#ff22ff', \
  "${tmp}-${name}.dat" using 3:7 title "Haddad" with linespoints pt 7 ps 2.0 lw 3.0 lc rgb '#ff2200'
quit
EOF

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

display ${pngfile}

rm -f ${tmp}-${name}.dat ${tmp}.png
