Perl checks for rain and issues umbrella warning
Rain Check
A Perl script that retrieves the current weather forecast at dawn helps the Perlmeister decide whether to run the risk or take an umbrella just in case.
In the wet season, the question often arises as to whether rain or snow will fall from the sky during the day and therefore whether it might be advisable to take an umbrella with me to work.
Underground Weather
Ultimately, however, even occasional rain showers around noon would be irrelevant, as long as you could navigate the commute to and from work without getting wet. Luckily, the Weather Underground [1] weather service offers free forecasts for the day. Although they are certainly not 100 percent accurate, they do beat human intuition by a mile.
On CPAN, Perl programmers will find the WWW::Wunderground::API module, which uses a script to fetch the weather data from the server thanks to a free API developer key [2] procured especially for that purpose. The simple script in Listing 1 uses the module; it iterates over the hourly entries in the bulk of JSON data returned and outputs the time and the probability of rain for each hour sent in the pop
(probability of precipitation) [3] entry.
Figure 1 shows less than a 50 percent probability of rain in the afternoon; a practicing mathematician might be inclined to leave that umbrella at home.
More Than Just Rain
That said, the Underground server returns far more than just the probability of rain. The hourly()
method called in line 12 of Listing 1 returns an array of hourly weather elements for the day. As the mass of data in Figure 2 shows, each element points to a number of keys such as humidity
or sky
(cloud coverage). Finally, the date and time of the forecast are provided under FCTTIME
.
Listing 1
chance-of-rain.pl
01 #!/usr/local/bin/perl -w 02 use strict; 03 use WWW::Wunderground::API; 04 use DateTime; 05 06 my $w = WWW::Wunderground::API->new( 07 location => "San Francisco, CA", 08 api_key => "0123456789abcdef", 09 auto_api => 1, 10 ); 11 12 for my $e (@{ $w->hourly }) { 13 my $dt = DateTime->from_epoch( 14 epoch => $e->{ FCTTIME }->{ epoch } ); 15 16 print "$dt: $e->{ pop }%\n"; 17 }
Listing 1 grabs the epoch
entry from the date and converts the Unix seconds since 1970, using the DateTime
Perl module, to a readable and easily machine manipulable format for use later.
API Key for an Email Address
The Wunderground server does not give you the data unconditionally, but if you register on the developer page by providing a valid email address [2], you will receive a 16-digit hexadecimal API key in return that enables a limited number of daily queries to the service. If you select the free "Cumulus" plan and also assure the provider that the data will not be used for commercial purposes (Figure 3), you do not need to specify any form of payment. In Listings 1 and 2, you need to replace the string defined by api_key
with the API key you are assigned (Figure 4) before use.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.