Parsing and plotting OMNIC Specta SPA files with R and PHP

[This is a repost of an article that was originally published at DiscreteCosine.]

This is a quick “how-to” post to describe how to parse OMNIC Specta SPA files, in case anyone goes a-google’n for a similar solution in the future.

SPA files consist of some metadata, along with the data as little endian float32. The files contain a basic manifest right near the start, including the offset and runlength for the data. The start offset is at byte 386 (two byte integer), and the run length is at 390 (another two byte int). The actual data is strictly made up of the little endian floats – no start and stop, no control characters.

These files are pretty easy to parse and plot, at least to get a simple display. Here’s some R code to read and plot an SPA:

pathToSource <- "fill_in_your_path"; = file(pathToSource, "rb");

# Read the start offset
seek(, 386, origin="start");
startOffset # Read the length
seek(, 390, origin="start");

# seek to the start
seek(, startOffset, origin="start");

# we'll read four byte chunks

# read all our floats

floatDataFrame floatDataFrame$ID<
p.plot p.plot + geom_line() + theme_bw()

In my particular case, I need to plot them from PHP, and already have a pipeline that shells out to gnuplot to plot other types of data. So, in case it’s helpful to anyone, here’s the same plotting in PHP.

<!--?php function generatePlotForSPA($source, $targetFile) { $sourceFile = fopen($source, "rb"); fseek($sourceFile, 386); $targetOffset = current(unpack("v", fread($sourceFile, 2))); if($targetOffset > filesize($source)) {<br ?--> return false;
fseek($sourceFile, 390);
$dataLength = current(unpack("v", fread($sourceFile, 2)));
if($dataLength + $targetOffset > filesize($source)) {
return false;

fseek($sourceFile, $targetOffset);

$rawData = fread($sourceFile, $dataLength);
$rawDataOutputPath = $source . "_raw_data";
$outputFile = fopen($rawDataOutputPath, "w");
fwrite($outputFile, $rawData);
$gnuScript = "set terminal png size {width},{height};
set output '{output}';

unset key;
unset border;

plot '<cat' binary filetype=bin format='%float32' endian=little array=1:0 with lines lt rgb 'black';"; $targetScript = str_replace("{output}", $targetFile, $gnuScript); $targetScript = str_replace("{width}", 500, $targetScript); $targetScript = str_replace("{height}", 400, $targetScript); $gnuPath = "gnuplot"; $outputScript = "cat \"" . $rawDataOutputPath . "\" | " . $gnuPath . " -e \"" . $targetScript . "\""; exec($outputScript); if(!file_exists($targetFile)) { return false; } return true; } ?>

Leave a Reply

Your email address will not be published. Required fields are marked *