Last active
December 11, 2015 10:29
-
-
Save run4flat/4587417 to your computer and use it in GitHub Desktop.
Basic script for viewing forecasts for a specified zip-code; no axis labels or title.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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