#!/usr/pkg/bin/perl -w
use strict;

# This perl-script was taken from the wmWeather package and modified for the
# use with bbweather.  All basical functionality was left as is, just some
# debugging and error-checking was added.

#
#    Grabs the latest local weather conditions from the National Weather
#    Service (NWS).  Uses the decoded METAR reports.
#
#    Need to give the 4-character METAR station code on the Command Line.
#    E.g.;
#
#           GrabWeather KLAM
#

require "ctime.pl";

if (!$ARGV[0]) { die "Usage: $0 StationID\n"; }

my $home = (getpwuid($<))[7];
chdir() || chdir($home);

if ( !(-e ".wmWeatherReports") )
{
		mkdir(".wmWeatherReports", 0775);
}
chdir(".wmWeatherReports");

my $StationID    = $ARGV[0];
my $HTMLFileName = "$StationID.TXT";
#my $URL          = "ftp://weather.noaa.gov/data/observations/metar/decoded/$HTMLFileName";
my $URL          = "ftp://tgftp.nws.noaa.gov/data/observations/metar/decoded/$HTMLFileName";
my $DataFileName = "$StationID.dat";

my $file = "$home/.wmWeatherReports/$StationID.TXT";
my $GrabCmd = "wget --proxy=off --passive-ftp --tries 0 -q -O $file $URL";

system "$GrabCmd" ;

if ((! (-e $file)) || (-z $file)) 
{
		print "Could not fetch data using:\n  \"$GrabCmd\"\n";
		print "Maybe non-existant Station ID?\n";
		exit(1);
}

my $Temp     = -999.0;
my $Chill    = -999.0;
my $DewPnt   = -999.0;
my $Pressure = -999.0;
my $Hum      = -999.0;
my $Flag 	 = 0;

open(TmpFile, "$HTMLFileName") or 
		die "Could not open \"$HTMLFileName\" for reading!\n";

my ($StationInfo, $UpdateTime, $SkyConditions, $CodedMETAR);

while (<TmpFile>)
{
		chop;
			
		if ($Flag == 0) { $StationInfo = $_; };
		if ($Flag == 1) { $UpdateTime = $_; };
		if (/^Sky conditions:/)
		{ 
				$SkyConditions = $_; 
				$SkyConditions =~ s/Sky conditions: (.*)/$1/; 
		}
		if (/^Temperature:/)
		{ 
				$Temp = $_; 
				$Temp =~ s/Temperature:\s*(\-{0,1}[0-9.]{1,}).*/$1/; 
		}
		if (/^Windchill:/)
		{ 
				$Chill = $_; 
				$Chill =~ s/Windchill:\s*(\-{0,1}[0-9.]{1,}).*/$1/; 
		}
		if (/^Dew Point:/)
		{ 
				$DewPnt = $_; 
				$DewPnt =~ s/Dew Point:\s*(\-{0,1}[0-9.]{1,}).*/$1/; 
		}
		if (/^Pressure \(.*\):/)
		{ 
				$Pressure = $_; 
				$Pressure =~ s/Pressure \(.*\):\s*([0-9.]{2,}).*/$1/; 
		}
		if (/^Relative Humidity:/)
		{ 
				$Hum = $_; 
				$Hum =~ s/Relative Humidity:\s*(\d{1,})\%.*/$1/; 
		}
		if (/^ob: /)
		{ 
				$CodedMETAR = $_; 
				$CodedMETAR =~ s/ob: (.*)/$1/; 
		}
		++$Flag;
}
close(TmpFile);


my $WindGroup = $CodedMETAR;
   $WindGroup =~ s/ RMK\s.*$//;
my $VarWindGroup = $WindGroup;
my $VarFlag;

if ($WindGroup =~ /.*\s\w{3}\d{2,3}KT\s\d{3}V\d{3}\s.*/ ) 
{
        $VarWindGroup =~ s/.*\s\w{3}\d{2,3}KT\s(\d{3}V\d{3})\s.*/$1/;
        $VarFlag = 1;
} 
elsif ($WindGroup =~ /.*\s\w{3}\d{2,3}MPS\s\d{3}V\d{3}\s.*/ ) 
{
        $VarWindGroup =~ s/.*\s\w{3}\d{2,3}MPS\s(\d{3}V\d{3})\s.*/$1/;
        $VarFlag = 1;
} 
elsif ($WindGroup =~ /.*\s\w{3}\d{2,3}G\d{2,3}KT\s(\d{2,3}V\d{2,3})\s.*/ ) 
{
        $VarWindGroup =~ s/.*\s\w{3}\d{2,3}G\d{2,3}KT\s(\d{2,3}V\d{2,3})\s.*/$1/;
        $VarFlag = 1;
} 
elsif ($WindGroup =~ /.*\s\w{3}\d{2,3}G\d{2,3}MPS\s(\d{2,3}V\d{2,3})\s.*/ ) 
{
        $VarWindGroup =~ s/.*\s\w{3}\d{2,3}G\d{2,3}MPS\s(\d{2,3}V\d{2,3})\s.*/$1/;
        $VarFlag = 1;
} 
else 
{
		$VarWindGroup = "";
		$VarFlag = 0;
}

my ($Direction, $AvgSpeed, $Speed, $Speed1, $Speed2);
my $Direction1 = $VarWindGroup; 
   $Direction1 =~ s/(\d{3})V\d{3}/$1/;
my $Direction2 = $VarWindGroup; 
   $Direction2 =~ s/\d{3}V(\d{3})/$1/;

my $GustFlag = 0;
if ($WindGroup =~ /.*\s\w{3}\d{2,3}KT\s.*/ ) 
{
		$WindGroup =~ s/.*\s(\w{3}\d{2,3}KT)\s.*/$1/;
		$Direction = $WindGroup; 
		$Direction =~ s/(\w{3})\d{2,3}KT/$1/;
		$Speed = $WindGroup; $Speed =~ s/\w{3}(\d{2,3})KT/$1/;
} 
elsif ($WindGroup =~ /.*\s\w{3}\d{2,3}MPS\s.*/ ) 
{
		$WindGroup =~ s/.*\s(\w{3}\d{2,3}MPS)\s.*/$1/;
		$Direction = $WindGroup; $Direction =~ s/(\w{3})\d{2,3}MPS/$1/;
		$Speed = $WindGroup; $Speed =~ s/\w{3}(\d{2,3})MPS/$1/;
		$Speed *= 1.942;
} 
elsif ($WindGroup =~ /.*\s\w{3}\d{2,3}G\d{2,3}KT\s.*/ ) 
{
		$WindGroup =~ s/.*\s(\w{3}\d{2,3}G\d{2,3}KT)\s.*/$1/;
		$Direction = $WindGroup; $Direction =~ s/(\w{3})\d{2,3}G\d{2,3}KT/$1/;
		$Speed1 = $WindGroup; $Speed1 =~ s/\w{3}(\d{2,3})G\d{2,3}KT/$1/;
		$Speed2 = $WindGroup; $Speed2 =~ s/\w{3}\d{2,3}G(\d{2,3})KT/$1/;
		$GustFlag = 1;
} 
elsif ($WindGroup =~ /.*\s\w{3}\d{2,3}G\d{2,3}MPS\s.*/ ) 
{
		$WindGroup =~ s/.*\s(\w{3}\d{2,3}G\d{2,3}MPS)\s.*/$1/;
		$Direction = $WindGroup; $Direction =~ s/(\w{3})\d{2,3}G\d{2,3}MPS/$1/;
		$Speed1 = $WindGroup; $Speed1 =~ s/\w{3}(\d{2,3})G\d{2,3}MPS/$1/;
		$Speed2 = $WindGroup; $Speed2 =~ s/\w{3}\d{2,3}G(\d{2,3})MPS/$1/;
		$Speed1 *= 1.942; $Speed2 *= 1.942;
		$GustFlag = 1;
} 
else 
{
        $WindGroup = "";
}


my $UniversalTime = $CodedMETAR;
if ($UniversalTime =~ /$StationID \d{1,2}\d{2}\d{2}Z/ )
{
		$UniversalTime =~ s/$StationID (\d{1,2})(\d{2})(\d{2})Z .*/$2:$3/;
} 
else 
{
        $UniversalTime = "99:99";
}

#  Write out the stuff we need to the Data File. This is the file that will
#  be read by.
if ($Flag > 0)
{
		open(TmpFile, ">$DataFileName") or 
				die "Could not open	\"$DataFileName\" for writing!\n";
		print TmpFile "$StationInfo\n";
		print TmpFile "$UpdateTime\n";
		print TmpFile "$SkyConditions\n";
		print TmpFile "$UniversalTime\n";
		print TmpFile "$Temp\n";
		print TmpFile "$DewPnt\n";
		print TmpFile "$Chill\n";
		print TmpFile "$Pressure\n";
		print TmpFile "$Hum\n";

		if ($Direction eq "" )
		{
				print TmpFile "-9999\n";
		} 
		elsif ($Direction =~ /VRB/ )
		{
				print TmpFile "999\n";
		} 
		elsif ($VarFlag) 
		{
				$Direction += 0;
				print TmpFile "-$Direction\n";
		} 
		else 
		{
				$Direction += 0;
				print TmpFile "$Direction\n";
		}

		if ($WindGroup eq "" ) 
		{
				print TmpFile "-9999\n";
		} 
		elsif ($GustFlag) 
		{
				$AvgSpeed = ($Speed1 + $Speed2)/2.0;
				$AvgSpeed *= 1.15155;
				print TmpFile "-$AvgSpeed\n";
		} 
		else 
		{
				$Speed += 0;
				$Speed *= 1.15155;
				print TmpFile "$Speed\n";
		}

		close(TmpFile);
}
