public
Last active

Basic script for viewing forecasts for a specified zip-code; no axis labels or title.

  • Download Gist
forecast-view-simple.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#!/usr/bin/env perl
use strict;
use warnings;
 
#############################
# Getting the Forecast Data #
#############################
 
use PDL;
use Time::Piece;
my $current = localtime;
my $zip_code = shift @ARGV || '60601';
 
# pull the weather data from the web
my $dom_data = get_weather_data_for($zip_code);
 
# Pull out the temperatue data and convert to PDL
my $temp_data = $dom_data->at('temperature[type="hourly"]');
my @temps = $temp_data->find('value')
->pluck('text')
->each;
my $temp = pdl \@temps;
 
 
# get the times related to the temperature sequence, mapping them do days
# in the future, and convert to PDL
my $time_name = $temp_data->{'time-layout'};
my @times = $dom_data->find('time-layout')
->first(sub{$_->at('layout-key')->text eq $time_name})
->find('start-valid-time')
->pluck('text')
->map(\&convtime)
->each;
my $time = pdl \@times;
 
 
#################
# Plot the data #
#################
 
use PDL::Graphics::Prima::Simple;
line_plot $time, $temp;
 
#####################################
# Web retrieval and date conversion #
#####################################
 
sub convtime {
s/-\d{2}:\d{2}$//; # strip end time
my $delta = Time::Piece->strptime($_, '%Y-%m-%dT%T') - $current;
$delta->days;
}
 
use Mojo::UserAgent;
use Mojo::URL;
 
# Pull data for the requested zip code with a Mojo user agent
sub get_weather_data_for {
my $zip_code = shift;
my $ua = Mojo::UserAgent->new;
my $url = Mojo::URL->new(
'http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php'
)->query(
zipCodeList => $zip_code,
product => 'time-series'
);
return $ua->get($url)->res->dom;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.