Thursday, January 13, 2011

Garmin FIT motorized segment filter in Perl

Finally, squeezing in work in my train commute between San Francisco and Mountain View (which unfortunately hasn't been a bike commute as much as I'd like due to the pressures of two big projects at work, but digression opportunities ate limited in the middle of a sentence), I've managed to finish a working draft of my "motorized segment filter" in Perl.

Like the fit_to_cols code I described recently, this code uses Kiyokazu Suto's Garmin::FIT package for Perl. Unlike fit_to_cols, this one needs to be able to write as well as read FIT data. The Garmin:FIT perl module allows this, but to figure out how you really need to dig into the provided example, fitsed. Fitsed is uncommented and does a lot more than just read and write FIT files: it also has a parser for selecting and/or changing fields in records. All good stuff, but a simple equivalent of hello.c, an example which minimally demonstrates writing FIT data, would have been useful. But I worked it all out, well most of it anyway, and my code seems to work.

So without further discussion, you can get it here (from Google Docs).

When downloading the code and placing it in your execution path, possibly typing "rehash" if you use a csh variant shell, the first thing to do is to type:

fit_filter_motor_segments -h

This prints help information.

Some examples, where "% " is the command prompt in these examples and is not typed:
  1. Check all files in the current directory for motorized segments. If any are found, then create a new FIT file for that file with ".fit" replaced with "_filtered.fit" :
    % fit_filter_motor_segments *.fit
  2. The same as the previous example, but create new FIT files with the same name but in directory /tmp with segments identified as motorized, if any, stripped:
    % fit_filter_motor_segments -a -dir /tmp *.fit
  3. From within Perl on a Linux or Unix based system, read an existing FIT file $ffit, filtering out the motorized segments (this is not a shell command, but rather Perl code):
    open FP, "fit_filter_motor_segments < $ffit |" or
         die("error opening FIT file $ffit: $!\n");
Next on my plate is a code to split FIT files at breaks which exceed a certain threshold, such as 8 hours. This is to correct the error of neglecting to reset a Garmin computer at the beginning of a ride. This turns out to be a bit tricker than I originally thought, since if a computer is left on the bike with the Garmin on, it has a tendency to generate "power up" and "power down" records at odd times. So how best to deal with these? Running Kiyokazu's fitdump is instructive. But I'm getting ahead of myself...

No comments: