Skip to content

Instantly share code, notes, and snippets.

@jberger
Forked from run4flat/forecast-view-simple.pl
Last active December 11, 2015 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jberger/4592048 to your computer and use it in GitHub Desktop.
Save jberger/4592048 to your computer and use it in GitHub Desktop.
#!/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