Skip to content

@run4flat /forecast-view-simple.pl
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Basic script for viewing forecasts for a specified zip-code; no axis labels or title.
#!/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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.