#!/usr/bin/perl
# -*- Perl -*-

#---------------------------------------------------------------------------
#  File:
#      get_weather_meteored
#  Description:
#      A perl script that gets the weather forecast for Spain using INM data
#  Author:
#      Ricardo Arroyo      ricardo.arroyo@ya.com
#    adapted from get_weather_ca, written by
#      Harald Koch     chk@pobox.com
#    based extensively on get_weather, written by
#      Bruce Winter    bruce@misterhouse.net   http://misterhouse.net
#  Latest version:
#      http://misterhouse.net/mh/bin
#
#  Copyright 2002 Bruce Winter
#
#---------------------------------------------------------------------------
#

use strict;

my ($Pgm_Path, $Pgm_Name);
BEGIN {
    ($Pgm_Path, $Pgm_Name) = $0 =~ /(.*)[\\\/](.+)\.?/;
    ($Pgm_Name) = $0 =~ /([^.]+)/, $Pgm_Path = '.' unless $Pgm_Name;
}

my ($Version) = q$Revision: 420 $ =~ /: (\S+)/; # Note: revision number is auto-updated by cvs

#print "Command: $Pgm_Name @ARGV\n";
#print "Version: $Version\n";

use Getopt::Long;
my %parms;
if (!&GetOptions(\%parms, "reget", "h", "help", "v", "debug", "city:s", "zone:s", "state:s", "file=s", "no_log") or
    @ARGV or
    ($parms{h} or $parms{help})) {
    print<<eof;

$Pgm_Name gets weather info

Usage:

  $Pgm_Name [options] 

    -h         => This help text
    -help      => This help text
    -v         => verbose
    -debug     => debug

    -reget     => force HTML fetch

    -city     xxx => xxx is the code for the city you want.
    -zone     xxx => xxx is the zone name. Not used. defined to have compatibility with get_weather (us) version
    -state    xxx => xxx is the state name. not used. defined to have compatibility with get_weather (us) version
    -file     xxx => xxx is the output filename

    -no_log       => Unless this option is used, the results also get filed 
                     into the data_dir/web directory

  Example:
    $Pgm_Name -city LEMD

eof
    exit;
  }

my ($conditions, $forecast, %data);
my %config_parms;


use vars qw(%Weather @Weather_Forecast);

my $caller = caller;
my $return_flag = ($caller and $caller ne 'main') ? 1 : 0;

#use my_lib "$Pgm_Path/../lib/site"; # See note in lib/mh_perl2exe.pl for lib -> my_lib explaination
BEGIN { eval "use lib '$Pgm_Path/../lib', '$Pgm_Path/../lib/site'" } # Use BEGIN eval to keep perl2exe happy

require 'handy_utilities.pl';       # For read_mh_opts funcion
&main::read_mh_opts(\%config_parms, $Pgm_Path);

use HTML::TableExtract;
use Date::Parse;
use Date::Format;

#set madrid (Retiro) as default
$parms{city}  = $config_parms{weather_meteored_city} unless $parms{city};
$parms{city}  = 'LEMD'                               unless $parms{city};

my $WeatherURL;
$WeatherURL = 'http://www.meteored.com/situacion_actual/hw3.php?icao=' . $parms{city};

my $f_weather_html = "$config_parms{data_dir}/../web/ia5/outside/weather_local.html";
my $f_weather_data = "$config_parms{data_dir}/weather_data";
my $f_weather_temp = "$config_parms{data_dir}/weather_temp";

my $debug = 1 if ($parms{debug});

###############
# get weather #
###############

    get_url_ua($WeatherURL, $f_weather_html);
    my $html = &file_read($f_weather_html);
    $html =~ s|<html>|<html>\n<BASE href='http://www.meteored.com/situacion_actual/'>|i;
    $html =~ s/<\/head>.*?<\!-- METAR METAR/<\/head>\n<\!-- METAR METAR/is;
    $html =~ s/END ZONE ROWS----------- \/\/-->.*?$/END ZONE ROWS----------- \/\/-->\n/is;
#    print "HTML:\n$html\n" if $debug;
    &file_write($f_weather_html, $html);

#################
# parse weather #
#################

    print "parsing weather data from $f_weather_html\n" if $parms{v};
    &parse_weather_meteored($f_weather_html);

################
# save weather #
################

print "saving weather data to $f_weather_data\n" if $parms{v};
&save_weather_sp($f_weather_data);

exit(0);

###############
# subroutines #
###############

# from get_url
sub get_url_ua {
    my $url = shift;
    my $file = shift;

    use LWP::UserAgent;

    my $ua = new LWP::UserAgent;
    $config_parms{proxy} = $ENV{HTTP_PROXY}           unless $config_parms{proxy};
    $ua -> proxy(['http', 'ftp'] => $config_parms{proxy}) if $config_parms{proxy};

    $ua->timeout([120]);         # Time out after 60 seconds 
    $ua->env_proxy(); 

    my $request = new HTTP::Request('GET', $url);
    my $response;

    print "Retrieving (with ua) $url into $file ...\n" unless $config_parms{quiet};
    if ($file eq '/dev/null') {
        $response = $ua->simple_request($request);
    }
    else {
        $response = $ua->simple_request($request, $file);
    }

    if ($response->is_error()) {
        printf "error: %s\n", $response->status_line;
    }
}


# There is an HTML weather page on disk. Parse the weather data out of it and
# save the results in a file so that the parent MH process can read them back
# in.
#
sub parse_weather_meteored {
    my $file = shift;
    my $html = &file_read($file);
    my $tmp;

    %Weather = ();
    @Weather_Forecast = ();

print STDERR $html if ($debug);

    # find the start of the actual data
    $html =~ s/.*BEGIN CURRENT CONDITIONS//s;
    $html =~ s/.*$//m;

    # remove font modificators
    $html =~ s/<strong>//gs;
    $html =~ s/<\/strong>//gs;
    $html =~ s/<small>//gs;
    $html =~ s/<\/small>//gs;
    $html =~ s/<b>//gs;
    $html =~ s/<\/b>//gs;
    $html =~ s/\&nbsp;/ /gs;
    $html =~ s/<td>//gs;
    $html =~ s/<\/td>//gs;
    $html =~ s/<font size=-1>//gs;
    $html =~ s/<\/font>//gs;
    $html =~ s/\&\#176;/\/gs;


print STDERR $html if ($debug);

    ($html =~ m/\<td valign="top" colspan="2" align="center"><font size="2">([^<]+)/i) &&
	($Weather{Conditions} = $1);
print STDERR "Conditions|$Weather{Conditions}|\n" if ($debug);

    $html =~ s/<.*?>//gs;
#    $html =~ s/\n//gs;
    
    ($html =~ m/Temperatura\:\s*(\-?\d+)/i) &&
        ($Weather{TempOutdoor} = $1);
print STDERR "TempOutdoor|$Weather{TempOutdoor}|\n" if ($debug);

    ($html =~ m/Humedad:\s*(\d+)\%/i) &&
        ($Weather{HumidOutdoor} = $1);
print STDERR "HumidOutdoor|$Weather{HumidOutdoor}|\n" if ($debug);

    if ($html =~ m/var cad=\"(\w*)\s+(\d+)\s+MPH/i) {
	my $direc = $1;
	my $wind = $2;
	$Weather{WindAvg} = $Weather{WindAvgSpeed} = $Weather{WindGustSpeed} = int($wind * 1.609344 + 0.5);
	$Weather{WindGustDir} = &convert_to_degrees($direc);
	$Weather{WindAvgDir} = &convert_to_degrees($direc);
	$Weather{Wind} = "$direc $Weather{WindAvg} km/h";
print STDERR "Wind|$Weather{Wind}|, " if ($debug);
print STDERR "WindGustDir|$Weather{WindGustDir}|, " if ($debug);
print STDERR "WindAvgDir|$Weather{WindAvgDir}|, " if ($debug);
print STDERR "WindAvgSpeed|$Weather{WindAvgSpeed}|, " if ($debug);
print STDERR "WindAvg|$Weather{WindAvg}|\n" if ($debug);
    }

    ($html =~ m/Presi&oacute;n:\s*(\d+)\s+mb\./i) &&
        ($Weather{Barom} = $1);
print STDERR "Barom|$Weather{Barom}|\n" if ($debug);

    ($html =~ m/Punto de roc\&iacute;o:\s*(\-?\d+)/i) &&
	($Weather{DewOutdoor} = $1);
print STDERR "DewOutdoor|$Weather{DewOutdoor}|\n" if ($debug);

    ($html =~ m/&Iacute;ndice de calor:\s*(\-?\d+)/i) &&
	($Weather{WindChill} = $1);
    my $WindChillHot;
    ($html =~ m/Aire Fr\&iacute;o:\s*(\-?\d+)/i) &&
	($WindChillHot = $1);
    $Weather{WindChill} = $WindChillHot if ($WindChillHot > $Weather{TempOutdoor});
print STDERR "WindChill|$Weather{WindChill}|\n" if ($debug);

### GET FORECAST
    
    my $forecast = $html;
#    @Weather_Forecast = @forecast;
}


sub save_weather_sp {
    my $file = shift;

    # save the data for import by parent
    my ($key, $perl, $line);
    
    $perl = '%Weather=(' . "\n";
    foreach $key (keys(%Weather)) {
	# cleanup the text
	my $data = $Weather{$key};
	$data =~ s/\s*(\xb0|&deg;)\s*[C]*//g;

	$data =~ s/\s*%\s*//g;
#	$data =~ s;\s*km/h\s*;;g;
	$data =~ s/\s*kPa\s*//g;
	$data =~ s/\s*\s*//g;

	$perl .= '    ' . $key . ' => ' . "'" . $data . "',\n";
    }
    $perl .= ');' . "\n";

    $perl .= '@Weather_Forecast=(';
    for (my $i = 0; $i <= $#Weather_Forecast; $i += 4) { 
	$perl .= "\n    '" . $Weather_Forecast[$i] . "',";
	$perl .= "    '" . $Weather_Forecast[$i+1] . "',";
	$perl .= "    '" . $Weather_Forecast[$i+2] . "',";
	$perl .= "    '" . $Weather_Forecast[$i+3] . "',";
    }
    $perl =~ s/,$//; # remove trailing comma
    $perl .= "\n" . ');' . "\n";

    main::file_write($file, $perl);
}

# convert text wind direction to degrees.
sub convert_to_degrees {
    my $text = shift;
    my $dir;

    ($text eq 'N')   && ($dir = 0);
    ($text eq 'NNE') && ($dir = 22);
    ($text eq 'NE')  && ($dir = 45);
    ($text eq 'ENE') && ($dir = 67);

    ($text eq 'E')   && ($dir = 90);
    ($text eq 'ESE') && ($dir = 112);
    ($text eq 'SE')  && ($dir = 135);
    ($text eq 'SSE') && ($dir = 157);

    ($text eq 'S')   && ($dir = 180);
    ($text eq 'SSW') && ($dir = 202);
    ($text eq 'SW')  && ($dir = 225);
    ($text eq 'WSW') && ($dir = 247);

    ($text eq 'W')   && ($dir = 270);
    ($text eq 'WNW') && ($dir = 292);
    ($text eq 'NW')  && ($dir = 315);
    ($text eq 'NNW') && ($dir = 337);

    return $dir;
}


###### v1.1	21/03/05
#Solved error in wind speed parse (eliminated search for \" after MPH)
#
