#!/usr/bin/perl # # Assemble NOAA current and forecast weather data into an XHTML fragment. # See http://golem.ph.utexas.edu/~distler/blog/archives/000482.html # # Copyright, Jacques Distler, 12/2004. # Released under the GPL. use XML::Simple; use strict; #### Customize these ##### # my $numDays = 2; # number of days (2 forecast intervals/day) # my $location = 'Austin'; # my location # my $current_file = # the current conditions XML file '/Users/distler/Sites/weather/current.xml'; # my $forecast_file = # the NDFD 12-hour forecast file '/Users/distler/Sites/weather/forecast.xml'; # my $weather_file = # file to save the XHTML fragment '/Users/distler/Sites/weather/weather.html'; # my $unavail_icon = # icon for unavailable Forecasts (don't ask!) '/~distler/weather/icons/unavailable.gif'; # my $radar_url = # Doppler radar loop for nearest weather station 'http://weather.gov/radar/loop/DS.p20-r/si.kewx.shtml'; # ########################## my $current = XMLin($current_file); my $forecast = XMLin($forecast_file); my $params = $forecast->{'data'}->{'parameters'}; my $layout = $forecast->{'data'}->{'time-layout'}; my $title = $forecast->{'head'}->{'product'}->{'title'}; my $info_url = $forecast->{'head'}->{'source'}->{'more-information'}; my ($maxperiodkey, $minperiodkey, $condsperiodkey); # arrays of keys for the time-periods in the forecast LAYOUT: for my $i (0 .. $#$layout) { my $period_key = $layout->[$i]->{'start-valid-time'}; my $layout_key = $layout->[$i]->{'layout-key'}; $layout_key =~ /k-p\dd-n\d/ ? do {next LAYOUT;} : $layout_key =~ /k-p24h-n\d+-1/ ? do { $maxperiodkey=$period_key; next LAYOUT;} : $layout_key =~ /k-p24h-n\d+-2/ ? do { $minperiodkey=$period_key; next LAYOUT;} : $layout_key =~ /k-p12h-n\d+-3/ ? do {$condsperiodkey=$period_key; next LAYOUT;} : $layout_key =~ /k-p\d+h-n\d/ ? do {next LAYOUT;} : die "unknown layout key: $layout_key\n"; } # if there's less data in the forecast file than asked for, give only # as much as we've got. if ($numDays > $#$maxperiodkey + 1) { $numDays = $#$maxperiodkey + 1; } my ($conditions, $temperatures, $hilo, $times, $precip, $icons); # turn the forecast into hashes, keyed by time-period for my $i (0 .. 2*$numDays-1) { $times->[$i] = $condsperiodkey->[$i]->{'period-name'}; $conditions->{$times->[$i]} = $params->{'weather'}->{'weather-conditions'}->[$i]->{'weather-summary'}; $precip->{$times->[$i]} = $params->{'probability-of-precipitation'}->{'value'}->[$i]; # towards the end of a 12-hour period, NOAA doesn't deign to "forecast" the # weather for that period. So we hack around them. $icons->{$times->[$i]} = ($params->{'conditions-icon'}->{'icon-link'}->[$i] =~ /^HASH/ ) ? $unavail_icon : $params->{'conditions-icon'}->{'icon-link'}->[$i]; } for my $i (0 .. $numDays-1) { $temperatures->{$maxperiodkey->[$i]->{'period-name'}} = $params->{'temperature'}->{'Daily Maximum Temperature'}->{'value'}->[$i]; $temperatures->{$minperiodkey->[$i]->{'period-name'}} = $params->{'temperature'}->{'Daily Minimum Temperature'}->{'value'}->[$i]; $hilo->{$maxperiodkey->[$i]->{'period-name'}} = 'High'; $hilo->{$minperiodkey->[$i]->{'period-name'}} = 'Low'; } # now print out the resulting forecast as an XHTML fragment open(OUT,">$weather_file"); my $abbr_string = " title=\"Probability of Precipitation\""; print OUT "

$location Weather#

\n\n"; for my $i (@$times) { print OUT <


$i

$conditions->{$i}
$hilo->{$i}: $temperatures->{$i}\°F
PoP: $precip->{$i}\% FOO ; $abbr_string = ""; } print OUT <

Currently:

$current->{'weather'}, $current->{'temp_f'}\°F \($current->{'temp_c'}\°C\)
(Doppler Radar) BAR ; close(OUT);