Tuesday, March 20, 2012

Installing xgraph on Mac OS/X Lion

After years on Thinkpads running Linux, I decided to take the plunge on an Apple Mac Air. Macs have always had a premium price for the hardware, and I never considered the cost justified, since you're buying a lot of software with that money and I have all the software I need with Linux. But the deal with the Macs is they simply work, and the Air is a beautifully designed machine.

After searching for a quick data plotting application, I was frustrated to find I still couldn't find anything which beat the old tried-and-true: xgraph, written at Berkeley in the 1980's It produces nice plots, data sets clearly differentiated in nice colors against a neutral grey background, with a super-simple data format trivially processed with scripting. The code's beauty is its simplicity and it's excellent defaults: no widgets or pull-down menus to deal with. At work I can tell people think I'm a bit strange for using such ancient code, but I get the last laugh when I toss well-scaled plots up on my screen in a fraction of the time it would take them using their tools.

The primary competition is almost as venerable: gnuplot. Gnuplot is even better, but I've never wrapped my head around the syntax. So I stick with xgraph. For publication-ready stuff I still use xmgrace, descendent of xmgr, another very old code. I'd love to hear options which do a better job in less time than either xmgrace or xgraph.

Anyway, I had to compile the xgraph source in OS/X. This was especially important to me because I've done a few custom modifications to the C source for myself (in particular, providing a "-aspect" command line option which keeps sets the autoscale ranges of the x and y axes to the same span, and also changing the tick-labels to a "%g" format).

First issue was getting it to compile. "dialog.c" contains a procedure called "getline". This is in conflict with a procedure in the standard library, so I changed the two references in "getline" in dialog.c (the definition and call) to "getline_xgraph". Now it compiled.

But then I faced a tougher problem: I was getting a persistent segmentation fault when I ran it. gdb wasn't turning up the problem, either.

The clue was in the compiler warnings: xgraph plays fast and loose with casting between addresses and integers. Sometimes that works, sometimes... So in desperation I checked the gcc man page and started reading, looking for an option which treated all integers as longs. I didn't think this would work, because external libraries might not cooperate.

I didn't find it. But what I found instead was the "-m32" option to compile the code in 32-bit mode even on a 64-bit machine. Hmm... seemed promising. I added this to the head of the "CFLAGS" definition in Makefile (Makefile having been generated with "configure", so this isn't the best approach). I tried it and, ding-ding-ding-ding, the code ran. My little victory shout was probably out of place, since I was on Caltrain at the time...

So now it works.

There's still an issue that when the window is resized the plot can become corrupted. But that's quickly fixed by minimizing then restoring the window.

Here's a screen grab, comparing speed from a ride to smoothed speed as processed with my Ruby script: