#!/bin/bash
#
# Creates a time scatterplot of MPEG activity, plotting frame arrival
# times against nominal bandwidth, coloring based on whether the frame
# could be decoded or not.  The input file is the trace output from the
# tracereceiver program.  The output is in jgraph format.

# Parameters for formatting the graph
XSZ="size 2.5"
YSZ="size 1.5"
GRAY=
LEGX=50
LEGY=300

proc_spec_file=../expevents.pl

if [ $# = 0 ]; then
  echo "usage: $0 <tracefile> [experimentfile]" 
  exit 1
fi

INFILE=$1
if [ $# -ge 2 ]; then
SPECFILE=$2
else
SPECFILE=
fi

# graph preamble

echo "newgraph"
echo "clip"
echo "xaxis $XSZ min 0 label : time (s)"
echo "yaxis $YSZ min 0 max 350 label : bandwidth (KB/s)"

# good frames

echo "newcurve"
echo "  label : decoded"
echo "  marktype circle"
if [ -n "$GRAY" ]; then
  echo "  gray .7"
else
  echo "  color 0 1 0"
fi
echo -n "pts "
grep OK $INFILE | awk '{ tm=$6; bw=$8 / 1000.0; printf("%0.2f %d ",tm,bw); }'

echo

# bad frames

echo "newcurve"
echo "  label : not decoded"
echo "  marktype x"
echo "  color 1 0 0"
echo -n "pts "
grep BAD $INFILE | awk '{ tm=$6; bw=$8 / 1000.0; printf("%0.2f %d ",tm,bw); }'

echo

# events

if [ -n "$SPECFILE" ]; then
  cat $SPECFILE | $proc_spec_file 
fi

# legend

if [ -n "$LEGX" -o -n "$LEGY" ]; then
  echo legend defaults x $LEGX y $LEGY
fi
