#!/bin/sh

#
#  Take the output of tsim on stdin and generate V/I distribution charts in
#  postscript to stdout.
#

TMP=/tmp/viplot.$$

[ $# -ne 1 ] &&
{
   echo "viplot: usage: ./viplot frequency" >&2
   exit 1
}

grep "VI $1 " | sort -nu +2.0 > $TMP.in

[ `wc -l < $TMP.in` -eq 0 ] &&
{
   echo "viplot: no data for frequency $1" >&2
   exit 1
}

opts='
    set data style lines
    set size 1.8,0.5
    set terminal pbm small
    set border
    set rmargin 3
    set lmargin 10
    set nokey
    set angles degrees
    set xlabel "Position"
    '

echo "$opts" '
    set title "Voltage Distribution"
    set ylabel "|V|"
    plot "'$TMP.in'" using ($3):(sqrt($4*$4+$5*$5))
    ' | gnuplot 2> $TMP.err  > $TMP.1.pbm

echo "$opts" '
    set title "Current Distribution"
    set ylabel "|I|"
    plot "'$TMP.in'" using ($3):(sqrt($6*$6+$7*$7))
    ' | gnuplot 2> $TMP.err  > $TMP.2.pbm

montage -geometry '1151x240+0+0~' -tile 1x2 \
         +frame +shadow +label \
        $TMP.1.pbm $TMP.2.pbm ps:-

rm -f $TMP.*

