GeekTool desktop weather fix

Today I noticed that my GeekTool weather information was goofed up. Fortunately, the fix was pretty simple.

Here’s what the lower left corner of my Desktop looked like.

Why was the temperature missing? The GeekTool shell command that controls the display is this command

/opt/local/bin/lynx -dump -width 100 http://weather.noaa.gov/weather/current/KARR.html | /Users/drang/bin/noaanow

This gets a big chunk of data from the local NOAA weather station and dumps it to a Perl script that extracts just the parts I want to display. I first thought that NOAA had changed the format of the data, which would screw up my extraction script, but after running

/opt/local/bin/lynx -dump -width 100 http://weather.noaa.gov/weather/current/KARR.html

in the Terminal, I saw what the problem was. Here’s chunk of the output that has the current conditions

Conditions at [Dec 18, 2008 - 09:52 AM EST]
2008.12.18 1452 UTC
Wind Calm
Visibility 4 mile(s)
Sky conditions clear
Weather Mist
Temperature -0.0 F (-17.8 C)
Dew Point -0.9 F (-18.3 C)
Relative Humidity 95%
Pressure (altimeter) 30.34 in. Hg (1027 hPa)
Pressure tendency 0.03 inches (1.1 hPa) higher than three hours ago
ob KARR 181452Z 00000KT 4SM BR CLR M18/M18 A3034 RMK AO2 SLP291 T11781183 51011

and here’s the line in the Perl script that extracts the data I want

@t = grep /^ +(Temperature \d|Wind|Relative)/, @w;

where @w is the list of all the lines in the data. Many lines in the data start with the word “Temperature,” but only the line with the current temperature is immediately followed by a number, hence the Temperature \d portion of the regular expression.

The problem was that today’s temperature didn’t start with a digit(\d), it started with a minus sign (actually a hyphen). So the regular expression didn’t collect the temperature line. I couldn’t believe I hadn’t run into this bug earlier; maybe I just wasn’t paying attention on below-zero days before today. In any event, the fix was easy. This is what the noaanow script looks like now:

 1:  #!/usr/bin/perl
 2:  
 3:  # Grab all the lines and put in an array.
 4:  @w = <>;
 5:  
 6:  # Keep only certain lines for the current conditions.
 7:  @t = grep /^ +(Temperature -?\d|Wind|Relative)/, @w;
 8:  
 9:  # Erase the leading spaces and parenthetical (metric) values.
10:  for (@t){
11:    s/^ +//;
12:    s/ \([^)]+\)//g
13:  };
14:  
15:  # I want the temperature line to print on the bottom to make it
16:  # easy to see on the desktop. The temperature is always on the
17:  # second line, so exchange it with the last line.
18:  ($t[$#t], $t[1]) = ($t[1], $t[$#t]);
19:  
20:  # Sometimes there's a windchill line, and sometimes there isn't.
21:  # Add a blank line to the front of the array if there isn't.
22:  unshift @t, "\n" if $#t == 2;
23:  
24:  # Print the lines of interest in the order I want.
25:  print join "", @t;

Line 7 has the new regular expression. The change is the optional hyphen (-?) before the digit. Now my Desktop looks like this.

and all is right with the world.

Tags: