Get the current weather

I made a little Perl script that will get the current weather from the National Weather Service and display it in nice, readible form. It uses the Geo::WeatherNWS Perl package to do the dirty work, so you'll need to edit that. You can just supply it with an ICAO designation code. (Hint, they're 4 letters long, so prefix your local U.S. airport with "K" to make one.) To find a local ICAO code, go here:

http://weather.gov/tg/siteloc.shtml

This is still very much version 0.1, so hopefully I'll make some additions.


#!/usr/bin/perl
# Returns some weather information. By default it returns just the temp and some
# current conditions.
#
# Note that you need to give it the ICAO station code.
#
use Geo::WeatherNWS;
use Time::Local;
use strict;

my $report=Geo::WeatherNWS::new();

# Set anonymous ftp login info

# Check the first argument
my $arg = $ARGV[0];
usage() if ($arg =~ /-h/i or $arg =~ /--help/i);

# Otherwise, assume it's a station name, or default to KMSN if nothing
my $station = $arg || 'KMSN';

$report->setusername("anonymous");
$report->setpassword('ben@moozer.com');

# Set the station code in ICAO format (note the leading K)
$report->getreport($station);

# Check for errors.
if ($report->{error})
{
    print "Error: \n     $report->{errortext}\n";
    usage();
}


# Get time of report, subtract the offset, and get it in localtime
my ($year,$month,$day,$hour,$min);
($year,$month,$day,$hour,$min) = ($report->{obs} =~ /(\d+)\/(\d+)\/(\d+) (\d+):(\d+)/);

#                               0 seconds         months from 0-11
my $obs_time = localtime(timegm(0,$min,$hour,$day,$month-1,$year));
print scalar($obs_time),"\n";

# Current temp string
my $temp = $report->{temperature_c};
my $temp_f = $report->{temperature_f};

# Windchill
my $windchill = $report->{windchill_c};

# Heat index
my $heatindex = $report->{heat_index_c};

# Conditions
my $conditions = lc($report->{conditionstext});

# Clouds
my $clouds = lc($report->{cloudcover});

# Wind direction
my $winddir = lc($report->{winddirtext});

# Wind speed
my $windspeed = $report->{windspeedmph};

# Wind gust speed
my $windgust = $report->{windgustmph};

# Dewpoint
my $dewpoint = $report->{dewpoint_c};

# Barometer (inHg)
my $barometer = $report->{slp_inhg};

# print "Current temp: $report->{temperature_c} C\n";
# print "All data follows...\n";
# foreach my $stat ( keys(%$report) ) {
#     if ($stat =~ /arrayref/) {
#        # use a for loop to make sure we go in order
#        for (0 .. $#{ $report->{$stat} }) {
#           print "$stat: $report->{$stat}[$_]\n";
#        }
#    }
#    else {
#           print "$stat: $report->{$stat}\n";
#    }
# }

print "Current conditions as of ", scalar($obs_time),":\n";

print "It is currently $temp C ($temp_f F).";
  print " with a windchill of $windchill C." if ($windchill < 0);
  print " with a heat index of $heatindex C." if ($heatindex > 26);
print "\n";

print "It is $conditions";
  print " and $clouds" if ($conditions ne $clouds);
  print ". Winds are out of the $winddir at $windspeed MPH";
  print " gusting to $windgust" if ($windgust);
print ".\n";

print "The dewpoint is $dewpoint C and the barometer is at $barometer\n.";

exit 0;

sub usage {

   # Match just the file name and not the path
   $0 =~ /([\w\.]*$)/;
   my $prog_name = $1;

print << EOF;
$prog_name -- Prints the current weather observations from a National
              Weather Service reporting station

Usage:

      $prog_name [ICAO station name]

      If no station name is given, it will default to KMSN (Madison, WI)

EOF
   exit 0;
}