Get Weather Info

This Perl program queries the weather conditions at KDAY (Dayton, Ohio).  It parses the result, creating an HCA-friendly file that can be read into HCA and recorded or otherwise acted upon.

use warnings;
use strict;
use LWP::Simple;  # supplies ‘get’ function
use XML::Simple;  # supplies xml parsing functions
use Data::Dumper;  # used for diag

# major definitions
my $outfile = “noaa-wx.txt”;
my $url = “http://www.weather.gov/xml/current_obs/KDAY.xml”;
my $page = “”;
my $xmldata = “”;
my $outln = “”;
# definintions for history file
my $histfile = “”;
my $histln = “”;
my $yearnumber = “”;
my $monthnumber = “”;
my $visibility_xx = “”;

# define simple fields that go into csv record
my $year_2 = “2008”;
my $month_3 = “01”;
my $date_4 = “01”;
my $hour_5 = “12”;
my $min_6 = “00”;
my $sec_7 = “00”;
my $wind_sp_8 = “0”;
my $wind_gust_9 = “0”;
my $wind_dir_10 = “000”;
my $rel_humid_12 = “00”;
my $temp_f_14 = “0”;
my $pressure_15 = “0”;
my $weather_19 = “0”;
my $wind_chill_29 = “0”;
my $heat_index_31 = “0”;
my $dewpoint_32 = “0”;

# define working areas for more complicated fields
my $work_date = “”;
my $work_weather = “”;
my @date_toks;
my $work_month;
my @work_time;

$page = get ($url);   # get xml record from NOAA

$xmldata = XMLin($page);  # give record to xml parser

# print Dumper($xmldata);  # for diags

# pull the easy values out of the xml record
$wind_sp_8 = $xmldata->{wind_mph};
$wind_gust_9 = 0;  #no longer part of metar
$wind_dir_10 = $xmldata->{wind_degrees};
$rel_humid_12 = $xmldata->{relative_humidity};
$temp_f_14 = $xmldata->{temp_f};
$pressure_15 = $xmldata->{pressure_in};
$wind_chill_29 = 0; #no longer part of metar
$heat_index_31 = 0;  #no longer part of metar
$dewpoint_32 = $xmldata->{dewpoint_f};
$visibility_xx = $xmldata->{visibility_mi};

# split out the date
$work_date = $xmldata->{observation_time_rfc822};
@date_toks = split(/\s+/, $work_date);   # tokenize on white space
$year_2 = $date_toks[3];
$work_month = $date_toks[2];  # assign number to month name, crudely done
if ($work_month eq “Jan”) {$month_3 = “1”};
if ($work_month eq “Feb”) {$month_3 = “2”};
if ($work_month eq “Mar”) {$month_3 = “3”};
if ($work_month eq “Apr”) {$month_3 = “4”};
if ($work_month eq “May”) {$month_3 = “5”};
if ($work_month eq “Jun”) {$month_3 = “6”};
if ($work_month eq “Jul”) {$month_3 = “7”};
if ($work_month eq “Aug”) {$month_3 = “8”};
if ($work_month eq “Sep”) {$month_3 = “9”};
if ($work_month eq “Oct”) {$month_3 = “10”};
if ($work_month eq “Nov”) {$month_3 = “11”};
if ($work_month eq “Dec”) {$month_3 = “12”};

$date_4 = $date_toks[1];

# split out time
@work_time = split(/:/, $date_toks[4]);  # get and split time field by colons
$hour_5 = $work_time[0];
$min_6 = $work_time[1];
$sec_7 = $work_time[2];

$weather_19 = $xmldata->{weather};  # for now just grab field

# now that the fields are assembled, write them out to disk
# first write out the hca import data file
open OUTPUT, “> $outfile” or die “Output file open error: $!”;

# create each line in hca file import format
# including whatever you want hca to see
$outln = “noaaouttemp” . “=” . $temp_f_14 . “\n”;
print OUTPUT $outln;
$outln = “noaaoutrh” . “=” . $rel_humid_12 . “\n”;
print OUTPUT $outln;

# create and write the separate history file
$yearnumber = $year_2;    # grab current year from noaa
$monthnumber = $month_3;   # grab current month from noaa
if ((length $monthnumber) == 1) {$monthnumber = “0” . $monthnumber};
$histfile = “yswxhist” . $yearnumber . $monthnumber . “\.” . “csv”;  # use noaa year and month to create file name
open HIST, “>> $histfile” or die “History file open error: $!”;
# if first hour of the day, put put a separator line
if ($hour_5 eq “00”)
{
$histln = “yy,mm,dd,hh,mm,tf,wc,hi,weather,ws,wg,wd,ba,rh,dp,vs\n”;
print HIST $histln;
}

# create record in csv format, my choice of fields
$histln = “$year_2,$month_3,$date_4,$hour_5,$min_6,$temp_f_14,$wind_chill_29,$heat_index_31,$weather_19,$wind_sp_8,$wind_gust_9,$wind_dir_10,$pressure_15,$rel_humid_12,$dewpoint_32,$visibility_xx \n” ;
# put record out to disk, appending the record

print HIST $histln;

As In Various and Sundry