#! /bin/bash
# Last edited on 2011-06-23 12:32:21 by stolfi

title="$1"; shift

# Plots the 2011 Brazilian IDEB data.
# Input is assumed to have 8 columns separated by "|":
#   {uf}   

tmp="/tmp/$$"

datafile="${tmp}-d.txt"

cat \
  | sort -t'|' -b -k7,7gr -k1,1 \
  | gawk \
      ' BEGIN { FS= "|"; un = 1; }
        /^[A-Z][A-Z] [|]/ { 
          gsub(/[ ]*[|][ ]*/, "|", $0);
          gsub(/^[ ]+/, "", $0);
          gsub(/[ ]+$/, "", $0);
          uf = $1; 
          e2005 = $3; p2005 = $4;
          e2007 = $5; p2007 = $6;
          e2009 = $7; p2009 = $8;
          printf "%2d %+2d %s %4.1f %4.1f\n", un, -1, uf, e2005, p2005;
          printf "%2d %+2d %s %4.1f %4.1f\n", un, 00, uf, e2007, p2007;
          printf "%2d %+2d %s %4.1f %4.1f\n", un, +1, uf, e2009, p2009;
          printf "\n";
          un++;
        }
      ' \
  > ${datafile}
  
width=2400
height=600

tempplot="${tmp}-r.png"
fullplot="${tmp}-f.png"
    
export GDFONTPATH=.

gnuplot <<EOF

set term png font arial 18 size ${width},${height}
set xrange [0.0:28.0]
set yrange [0.0:9.2]
set output "${tempplot}" 
set format x ""
set title "IDEB 2005, 2007, 2009 - ${title}"

set grid ytics

xdot(i,j) = column(i)+0.20*column(j)
xlab(i,j) = (column(j) == 0 ? column(i) : 0/0)
ideb(i) = (column(i) > 0.0 ? column(i) : 0/0)
ufname(k) = stringcolumn(k)

plot \
  "${datafile}" using (xdot(1,2)):(ideb(4))         notitle           with impulses                lc rgb '#448844', \
  "${datafile}" using (xdot(1,2)):(ideb(5))         notitle           with impulses                lc rgb '#448844', \
  "${datafile}" using (xdot(1,2)):(ideb(4))         title "Estaduais" with linespoints pt 7 ps 2.0 lc rgb '#008800', \
  "${datafile}" using (xdot(1,2)):(ideb(5))         title "Privadas"  with linespoints pt 7 ps 2.0 lc rgb '#ff0000', \
  "${datafile}" using (xlab(1,2)):(7.7):(ufname(3)) notitle           with labels font "arial,18"  lc rgb '#0077ff'
  
quit
EOF

convert ${tempplot} -resize '50%' ${fullplot}

display ${fullplot}

cat ${fullplot}

rm -f ${tmp}-*


