#! /bin/csh -f
# Last edited on 2004-08-20 22:21:43 by stolfi

set usage = "$0 INFILE.data  QUIZ LABEL > IMAGEFILE.png"

# Plots an histogram of grades for quiz number QUIZ.
# Assumes that the input INFILE.data has one record per student, 
# containing the student's RA number followed by the grades for 
# each quiz.

if ( $#argv != 3 ) then
  echo "usage: ${usage}"; exit 1
endif

set infile = "$1"; shift;
set qz     = "$1"; shift;
set lab    = "$1"; shift;

set tmpfile = "/tmp/$$.hdat"

cat ${infile} \
  | gawk -v qz="${qz}" \
      ' BEGIN { \
          split("", h); split("", c); \
          n = 10; s = 100.0/n; \
        } \
        /./{ \
          g = $(qz+1); if (g == "---") { next; } \
          i = int(g/s); \
          h[(i < 0 ? 0 : (i >= n ? n-1 : i))]++; \
        } \
        END { \
          for (i=0; i<n; i++) { printf "%5.1f %6.1f\n", s/10*(i + 0.5), h[i]; } \
        } \
      ' \
  > ${tmpfile}

gnuplot <<EOF
set terminal pbm small color;
set size 0.35,0.5
set xlabel "${lab}"
set ylabel "alunos"
set xrange [-0.2:10.2]
set yrange [0:15]
set xzeroaxis
set xtics 1
set nokey
plot "${tmpfile}" using 1:2 with histeps
quit
EOF

# rm -f ${tmpfile}

