changeset 238:a1d6e2d8789e

Add new data from FMI service and change datafile format.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 19 Jan 2015 16:26:42 +0200
parents 56af82fb7a6d
children ae9d83ec50d5
files fetch_weather.pl weather.tcl
diffstat 2 files changed, 22 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- 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()).
-    "&parameters=temperature,humidity&maxlocations=100&bbox=19,59,32,75";
+    "&parameters=".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"}),
--- 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}."
 }