diff fetch_weather.pl @ 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 ffd49b583e34
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"}),