Utility: Black box plotter (for Linux + Gnuplot)

View previous topic View next topic Go down

Utility: Black box plotter (for Linux + Gnuplot)

Post by jsiren on Thu Jul 23, 2015 11:59 pm

Hi all,

I've been away for a while for personal reasons, but got back to driving the Network West Midlands route after a long pause. I started thinking about the black box, and wrote up a shell script to graph the data with Gnuplot. It's only 82 lines, so if you don't mind, I'll just paste the code here. Requirements:
 - a Bourne-type shell (bash, dash...)
 - Gnuplot (mine is version 4.6) http://www.gnuplot.info/
The output is PDF or PNG, and the plot is based on time or distance (selectable).

I wrote this for myself, but if somebody else finds it useful or interesting - even better.

Code:
#!/bin/sh
TD=1
TLABEL="Time (seconds from midnight)"
OUTFILE=""
RANGE=""
FORMAT="pdf"
SIZE="29.7cm,21cm"
usage="$0 [-t|-d] [-r min:max] -o outfile infile"
OPTTEMP=`getopt -o tdho:f:r: -n 'openbve-plot' -- "$@"`
eval set -- $OPTTEMP
while true ; do
  case $1 in
    -h) echo $usage >&2 ; exit 1 ;;
    -t) shift ;;
    -d) TD=2 ; TLABEL="Position (m)" ; shift ;;
    -f) case $2 in
      pdf) FORMAT="pdf" ;;
      PDF) FORMAT="pdf" ;;
      png) FORMAT="png" ; SIZE="1280,800" ;;
      PNG) FORMAT="png" ; SIZE="1280,800" ;;
      *) echo $usage >&2 ; exit 1 ;;
      esac ; shift 2 ;;
    -s) SIZE=$2 ; shift 2 ;;
    -o) OUTFILE=$2 ; shift 2 ;;
    -r) RANGE="set xrange [$2]" ; shift 2 ;;
    --) shift ; break ;;
    *) echo $usage >&2 ; exit 1 ;;
  esac
done
GNUPLOT=`which gnuplot`
PLOT_ARGS=""
if [ -n "$OUTFILE" ] ; then exec 1>$OUTFILE ; fi
DATAFILE=$1
usage="Usage: $0 [OPTION] ... <FILE>
Takes a CSV file exported from the OpenBVE black box (Review last game - Black box -
Format: Comma separated value - Export...) and plots an A4 sized PDF chart out of it.
Options:
 -t       time-based plot (default)
 -d       distance-based plot
 -f pdf|png   PDF plot (default) or PNG plot
 -s x,y   dimensions of plot (default: 29.7cm,21cm for PDF, 1280,800 for PNG)
 -o outfile     output file name (stdout if omitted)
 -r min:max   range of time or distance values (as in the first or second column
      of the file) to plot; if omitted, plots entire file
 -h      this help"
$GNUPLOT $PLOT_ARGS <<EOF
set datafile separator ','
set term $FORMAT size $SIZE
set yrange [-10:100]
set y2range [-1:10]
$RANGE
set xtics rotate out
set xzeroaxis
set ytics out
set y2tics out
set xlabel "$TLABEL"
set ylabel "Speed m/s"
set y2label "Acceleration m/s² | Step"
set mytics 5
set my2tics 0
set style fill transparent solid 0.4
set linetype  1 lc rgb "#FF8C00" lw 1
set linetype  2 lc rgb "#FF4500" lw 3
set linetype  3 lc rgb "#1E90FF" lw 1
set linetype  4 lc rgb "#0000FF" lw 3
set linetype  5 lc rgb "#00FA9A" lw 1
set linetype  6 lc rgb "#32CD32" lw 3
set linetype  7 lc rgb "#FF8C00" lw 3
set linetype  8 lc rgb "black"  lw 1
set linetype  9 lc rgb "gray50"  lw 1
set linetype cycle  9

plot "$DATAFILE" using $TD:5 title 'Reverser driver' axes x1y2 with fillsteps, \
'' using $TD:6 title 'Reverser security' axes x1y2 with steps, \
'' using $TD:7 title 'Power driver' axes x1y2 with steps, \
'' using $TD:8 title 'Power security' axes x1y2 with steps, \
'' using $TD:9 title 'Brake driver' axes x1y2 with steps, \
'' using $TD:10 title 'Brake security' axes x1y2 with steps, \
'' using $TD:3 title 'Speed m/s' axes x1y1 with lines, \
'' using $TD:4 title 'Acceleration m/s²' axes x1y2 with lines, \
'' using $TD:11 title 'Event' axes x1y2 with steps
EOF

jsiren

Posts : 106
Join date : 2012-09-16

Back to top Go down

Re: Utility: Black box plotter (for Linux + Gnuplot)

Post by jsiren on Fri Jul 24, 2015 3:56 pm

And here's another version, as an attachment to this message, and a sample plot. This is from a deliberately horrible 323 ECS run, I was trying to trigger all the safety devices (ignoring AWS, opening doors at speed etc.) just to see how it looks in the black box.

How this was plotted:
openbve-plot -i -f png -o 323test.png 323test.csv

where
openbve-plot = how I named this script on my computer
-i = plots speed in mph (as the speed limits are on the NWM route)
-f png = produces output in PNG format
-o 323test.png = name of the output file to be produced
323test.csv = name of my input file (the black box file I saved from OpenBVE)

This script requires a Bourne-style shell (sh, bash, dash...), the getopt command, and gnuplot, all of which can be installed on pretty much any computer, including Windows.
Attachments
openbve-plot.zip A zip file containing the scriptYou don't have permission to download attachments.(2 Kb) Downloaded 0 times

jsiren

Posts : 106
Join date : 2012-09-16

Back to top Go down

Re: Utility: Black box plotter (for Linux + Gnuplot)

Post by jsiren on Fri Jul 24, 2015 11:48 pm

P.S. mod(s): if you think this is post is more appropriate somewhere else, e.g. Support for Developers, feel free to move it.

jsiren

Posts : 106
Join date : 2012-09-16

Back to top Go down

Re: Utility: Black box plotter (for Linux + Gnuplot)

Post by Sponsored content Today at 2:34 pm


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum