Let me introduce you today into a tool that a lot of people might evaluate as useless: Jeremy Stanley’s weather-util. Whith this tiny python script, which finally found its way into Debian Etch and Ubuntu repositories, you can retrieve weather information from weather stations worldwide directly from the command line.
After installing it by running „aptitude install weather-util“ or synaptec, call „weather“:
$ weather Current conditions at Raleigh-Durham International Airport (KRDU) Last updated Jun 04, 2008 - 01:51 AM EDT / 2008.06.04 0551 UTC Wind: from the S (180 degrees) at 10 MPH (9 KT) Sky conditions: mostly cloudy Temperature: 72.0 F (22.2 C) Relative Humidity: 73%
Pretty impressive, isn’t it? Weather just makes an http call to a weather server for a preset station (where the heck is Raleigh-Durham International Airport?) and returns the current weather information. Of course you can also retrieve the forecast for the next days by running „weather -f“:
$ weather -f Current conditions at Raleigh-Durham International Airport (KRDU) Last updated Jun 04, 2008 - 01:51 AM EDT / 2008.06.04 0551 UTC Wind: from the S (180 degrees) at 10 MPH (9 KT) Sky conditions: mostly cloudy Temperature: 72.0 F (22.2 C) Relative Humidity: 73% City Forecast for Raleigh Durham, NC Issued Wednesday morning - Jun 4, 2008 Wednesday... Partly cloudy, high 67, 20% chance of precipitation. Wednesday night... Low 96, 20% chance of precipitation. Thursday... Partly cloudy, high 71, 10% chance of precipitation. Thursday night... Low 97. Friday... High 72.
Sadfully the forecast only displays Fahrenheit, but that way we have enough space for patching the package
Retrieving local weather information
Now we are, of course, we are interested in the weather in our area. The easiest way is getting the ID for a weather station. Just go to http://weather.noaa.gov/ and choose your country/city/station by using the drop down menus for US and international stations. When you found a station close to your point of interest you can see a four letter id in round brackets. See the example above – the airport has KRDU. I am using EDDI most of the times which is Berlin Tempelhof – an airport in the city center of Berlin.
So you are ready to ask politely for weather again by giving the id with „weather –id=ID“, in my case „–id=EDDI“. (note: you can also make it short with „-iEDDI“:
$ weather --id=EDDI Current conditions at Germany (EDDI) 52-28N 013-24E 49M (EDDI) Last updated Jun 04, 2008 - 01:50 AM EDT / 2008.06.04 0550 UTC Wind: from the E (080 degrees) at 13 MPH (11 KT) Temperature: 62 F (17 C) Relative Humidity: 59%
Please note: Not all weather stations support forecasts (-f) and drop a 404 http error. You just have to try this. You can also switch on „verbose“ mode (-v) which gives you even more details.
Weather on the command line without weather-util?
Works like a charm, doesn’t it? For the curious people around who want to understand where weather-util pulls the information from: See
http://weather.noaa.gov/pub/data/observations/metar/stations/
for reference. Just text files on a web server regularly updated. Click around and go to there parent dir – you’ll find even more interesting information. So using weather-util without weather-util should be not a big deal.
Screen integration
Now for the console lovers: You are using screen with a pimped status bar, don’t you? And in your wildest dreams you imagined the status bar showing the weather report, so you even don’t have to look outside the window because as a console guy you don’t even like your real „window“? No problem anymore by using screens backticks and weather-util.
As I noticed that weather-util runs into trouble from time to time when not being able to send it’s http request, I decided for a indirect weather pull by writing the information I need to a flat file by a cronjob. We just call weather-util and use awk to grab the snippet we need. I am interested in the temperature in Celsius. weather-util shows this line:
Temperature: 62 F (17 C)
So I use the following very quick and very dirty awk to get the „17“ out:
$ weather -iEDDI | awk '/Temperature/ {print $4}' | \ awk -F "(" '{print $2}' |
Feel free to brush this up and report back. I am sure you can improve to use only one awk call instead of two.
You save this line to a shell script that is scheduled to run every five minutes and direct it via „>“ to write it’s output to a flat txt file. Within you .screenrc you read this file and display the contents in you status bar.
~/.screenrc:
startup_message off defscrollback 1024 hardstatus on hardstatus alwayslastline backtick 1 0 300 cat /path/to/weather-text-file.txt # remove line breaks made with "\"on the following lines caption always "%{+b rk}$USER@%{wk}%H | %{yk}(Last: %l) %{gk} \ Weather: %1`C %-21=%{wk}%D %d.%m.%Y %0c" hardstatus alwayslastline "%?%-Lw%?%{wb}%n*%f %t%?(%u)\ %?%{kw}%?%+Lw%? %{wk}"
Make sure that have the file /path/to/weather-text-file.txt with the temperature in it. Now run screen and enjoy you shiny new status bar. See the green area in the screenshot below:
So that’s all for now. You should be able to play around with weather-util and screen to get the information you need (or let’s say „want“ :).
[update]
The incredible mnemonikk updated my awk | awk to a onetime sed within seconds:
$ weather -iEDDI | sed -n 's/.*Temperature:.*(\(.*\))/\1/p' |
Thank you!