# HG changeset patch # User Matti Hamalainen # Date 1421677602 -7200 # Node ID a1d6e2d8789eaa088e752fb3c4f94b1235292164 # Parent 56af82fb7a6def2b7a5738b6e8ecc772d6445fe1 Add new data from FMI service and change datafile format. diff -r 56af82fb7a6d -r a1d6e2d8789e fetch_weather.pl --- a/fetch_weather.pl Mon Jan 19 11:34:44 2015 +0200 +++ b/fetch_weather.pl Mon Jan 19 16:26:42 2015 +0200 @@ -444,12 +444,13 @@ my $fdata = @$oelems[$n]->{"nodes"}; $weatherdata->{get_node($fdata, "text", 0)} = [ + # type, timestamp, temperature 0, parse_timestamp(get_node($fdata, "text", 1), $time_base), get_node_lc($fdata, "text", 2), + # and the rest get_node_lc($fdata, "text", 3), translate_th_rain(get_node($fdata, "text", 4)), - "", get_node_lc($fdata, "text", 5), ]; } @@ -465,13 +466,14 @@ if (opt_chk_bool("opt_fmi")) { die("FMI data scrape enabled, but no API key set.\n") unless opt_chk_valid("fmi_api_key", 10); + my @fmitems = ("temperature", "humidity", "windspeedms", "totalcloudcover"); my $uri = "http://data.fmi.fi/fmi-apikey/".opt_get("fmi_api_key"). "/wfs?request=getFeature&storedquery_id=fmi::observations::weather::". "multipointcoverage". # "timevaluepair". "&starttime=".format_time_gmt(time() - 10*60)."&endtime=".format_time_gmt(time()). - "¶meters=temperature,humidity&maxlocations=100&bbox=19,59,32,75"; + "¶meters=".join(",", @fmitems)."&maxlocations=100&bbox=19,59,32,75"; print STDERR "FMI URI: ".$uri."\n" if opt_chk_bool("debug"); @@ -498,10 +500,15 @@ my $findex = 0; foreach my $fline (split(/\n/, $fshit->{"gml:rangeSet"}{"gml:DataBlock"}{"gml:doubleOrNilReasonTupleList"})) { - if ($fline =~ /^\s*([\+\-]?\d+\.\d*|NaN)\s+([\+\-]?\d+\.\d*|NaN)\s*$/) + my @fmatches = ($fline =~ /\s*([\+\-]?\d+\.\d*|NaN)\s*/ig); + if (scalar(@fmatches) > 0) { - $farray[$findex]{"temp"} = $1 if ($1 ne "NaN"); - $farray[$findex]{"humidity"} = $2 if ($2 ne "NaN"); + die("Not enough items in scalar line (".scalar(@fmatches). " vs ".scalar(@fmitems). + "): ".$fline."\n") if (scalar(@fmatches) != scalar(@fmitems)); + for (my $fni = 0; $fni < scalar(@fmitems); $fni++) + { + $farray[$findex]{$fmitems[$fni]} = $fmatches[$fni] if (lc($fmatches[$fni]) ne "nan"); + } $findex++; } } @@ -525,8 +532,7 @@ 1, plonk_data($flol->{"time"}), plonk_data($flol->{"temperature"}), - "", - "", + plonk_data($flol->{"humidity"}), plonk_data($flol->{"windspeedms"}), plonk_data($flol->{"totalcloudcover"}), diff -r 56af82fb7a6d -r a1d6e2d8789e weather.tcl --- a/weather.tcl Mon Jan 19 11:34:44 2015 +0200 +++ b/weather.tcl Mon Jan 19 16:26:42 2015 +0200 @@ -207,10 +207,15 @@ # Produce one location of weather data as a string proc weather_get_data {ukey udata} { set str "\002[lindex $udata 0]\002, mitattu klo [weather_ctime [lindex $udata 2]]: \002[lindex $udata 3]°C\002" - append str [weather_item $udata 5 ", @@"] - append str [weather_item $udata 6 ", kosteus \002@@%\002"] - append str [weather_item $udata 7 ", keli @@"] - append str [weather_item $udata 4 ", tien pinta @@°C"] + if {[lindex $udata 1] == 0} { + append str [weather_item $udata 4 ", @@"] + append str [weather_item $udata 5 ", keli @@"] + append str [weather_item $udata 6 ", tien pinta @@°C"] + } else { + append str [weather_item $udata 4 ", kosteus \002@@%\002"] + append str [weather_item $udata 5 ", tuuli \002@@\002 m/s"] + append str [weather_item $udata 6 ", pilvipeite \002@@\002"] + } return "${str}." }