Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Geo::TAF to aviation module #2

Merged
merged 2 commits into from Feb 22, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 31 additions & 8 deletions modules/aviation.pm
Expand Up @@ -33,13 +33,16 @@
# to support more airports
# - Initial heading is now calculated internally with standard
# algorithm from http://williams.best.vwh.net/avform.htm.
# 2012/12/19 matt@knm.org.uk
# - Added support for Geo::TAF module to parse METAR and TAF
# results into English
#
# $Id: aviation.pm,v 1.5 2005/08/31 13:39:08 rich_lafferty Exp $
#------------------------------------------------------------------------

package aviation;

my ( $no_aviation, $no_entities, $no_posix );
my ( $no_aviation, $no_entities, $no_posix, $no_parsing );
my $pi = 3.1415926535;

BEGIN {
Expand All @@ -51,6 +54,8 @@ BEGIN {
if ($@) { $no_posix++ }
eval "use HTML::TokeParser";
if ($@) { $no_aviation++ }
eval "use Geo::TAF";
if ($@) { $no_parsing++ }
}

# Set the following to 1 if you want the forecast separators in
Expand All @@ -75,7 +80,9 @@ sub aviation::scan(&$$) {
defined( ::getparam('aviation') )
or defined( ::getparam('metar') )
and $message =~ /^(metar |
conditions |
taf |
forecast |
great[-\s]?circle |
zulutime |
tsd |
Expand Down Expand Up @@ -105,6 +112,8 @@ sub aviation::get {
return 'NOREPLY' if $pid; # parent does nothing
if ( $line =~ /^metar\s+/i ) { $callback->( metar($line) ) }
elsif ( $line =~ /^taf\s+/i ) { $callback->( taf($line) ) }
elsif ( $line =~ /^conditions\s+/i ) { $callback->( metar($line,1) ) }
elsif ( $line =~ /^forecast\s+/i ) { $callback->( taf($line,1) ) }
elsif ( $line =~ /^great[-\s]?circle\s+/i ) {
$callback->( greatcircle($line) );
} elsif ( $line =~ /^tsd\s+/i ) {
Expand All @@ -131,15 +140,16 @@ sub aviation::get {
# aviation - list available aviation functions
#
sub aviation {
return "My aviation-related functions are metar, taf, great-circle, tsd, zulutime, rh, and airport. For help with any, ask me about '<function name> help'.";
return "My aviation-related functions are metar, conditions, taf, forecast, great-circle, tsd, zulutime, rh, and airport. For help with any, ask me about '<function name> help'.";
}

#
# METAR - current weather observation
#
sub metar {
my $line = shift;
if ( $line =~ /^metar\s+(for\s+)?(.*)/i ) {
my $parse = shift;
if ( $line =~ /metar\s+(for\s+)?(.*)/i or $line =~ /conditions\s+(for\s+)?(.*)/i) {

# ICAO airport codes *can* contain numbers, despite earlier claims.
# Americans tend to use old FAA three-letter codes; luckily we can
Expand All @@ -155,7 +165,7 @@ sub metar {
$site_id = "K" . $site_id if length($site_id) == 3;

# HELP isn't an airport, so we use it for a reference work.
return "For observations, ask me 'metar <code>'. For information on decoding Aerodrome Weather Observations (METAR), see http://www.avweb.com/weather/metartaf.html"
return "For observations, ask me 'metar <code>'. For information on decoding Aerodrome Weather Observations (METAR), see http://www.avweb.com/weather/metartaf.html - or use 'conditions <code>'"
if $site_id eq 'HELP';

my $metar_url =
Expand Down Expand Up @@ -186,7 +196,13 @@ sub metar {
return "I can't find any observations for $site_id."
if length($metar) < 10;

return $metar;
if($no_parsing or not $parse){
return $metar;
} else {
my $t = new Geo::TAF;
$t->metar($metar);
return $t->as_string;
}
} else {

# malformed
Expand All @@ -199,7 +215,8 @@ sub metar {
#
sub taf {
my $line = shift;
if ( $line =~ /^taf\s+(for\s+)?(.*)/i ) {
my $parse = shift;
if ( $line =~ /^taf+\s+(for\s+)?(.*)/i or $line =~ /^forecast+\s+(for\s+)?(.*)/i ) {

# ICAO airport codes *can* contain numbers, despite earlier claims.
# Americans tend to use old FAA three-letter codes; luckily we can
Expand All @@ -215,7 +232,7 @@ sub taf {
$site_id = "K" . $site_id if length($site_id) == 3;

# HELP isn't an airport, so we use it for a reference work.
return "For a forecast, ask me 'taf <ICAO code>'. For information on decoding Terminal Area Forecasts, see http://www.avweb.com/toc/metartaf.html"
return "For a forecast, ask me 'taf <ICAO code>'. For information on decoding Terminal Area Forecasts, see http://www.avweb.com/toc/metartaf.html or use 'forecast <ICAO code>'"
if $site_id eq 'HELP';

my $taf_url =
Expand Down Expand Up @@ -254,7 +271,13 @@ sub taf {
return "I can't find any forecast for $site_id."
if length($taf) < 10;

return $taf;
if($no_parsing or not $parse){
return $taf;
} else {
my $t = new Geo::TAF;
$t->taf($taf);
return $t->as_string;
}
} else {

# malformed
Expand Down