changeset 378:b706e8ebecf8

fetch_weather: Possibly improve FMI XML mess parsing.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 07 Feb 2016 17:54:09 +0200
parents 0a6d9f0f0a57
children 3715df1cd9aa
files fetch_weather.pl
diffstat 1 files changed, 36 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/fetch_weather.pl	Sun Feb 07 17:53:33 2016 +0200
+++ b/fetch_weather.pl	Sun Feb 07 17:54:09 2016 +0200
@@ -419,34 +419,49 @@
     {
       my $fdata = $xml->{"wfs:member"}{"omso:GridSeriesObservation"};
       my $fshit = $fdata->{"om:result"}{"gmlcov:MultiPointCoverage"};
+
+      my @position_lines = split(/\n/, $fshit->{"gml:domainSet"}{"gmlcov:SimpleMultiPoint"}{"gmlcov:positions"});
+      my @data_lines = split(/\n/, $fshit->{"gml:rangeSet"}{"gml:DataBlock"}{"gml:doubleOrNilReasonTupleList"});
       my @farray = ();
 
-      foreach my $fline (split(/\n/, $fshit->{"gml:domainSet"}{"gmlcov:SimpleMultiPoint"}{"gmlcov:positions"}))
+      if (scalar(@position_lines) == scalar(@data_lines))
       {
-        if ($fline =~ /^\s*([\+\-]?\d+\.\d*)\s+([\+\-]?\d+\.\d*)\s+(\d+)\s*$/)
+        for (my $nline = 0; $nline < scalar(@position_lines); $nline++)
         {
-          push(@farray, {"lat" => $1, "long" => $2, "time" => $3});
+          my $dline = $data_lines[$nline];
+          my $pline = $position_lines[$nline];
+
+          my @fmatches = ($dline =~ /\s*([\+\-]?\d+\.\d*|NaN)\s*/ig);
+          if (scalar(@fmatches) != scalar(@fmitems))
+          {
+            print STDERR "Not enough items in scalar line #".$nline." (".
+              scalar(@fmatches). " vs ".scalar(@fmitems)."): ".$dline."\n";
+          }
+          else
+          {
+            my $vtmp = {};
+            for (my $fni = 0; $fni < scalar(@fmitems); $fni++)
+            {
+              $$vtmp{$fmitems[$fni]} = $fmatches[$fni] if (lc($fmatches[$fni]) ne "nan");
+            }
+            if ($pline =~ /^\s*([\+\-]?\d+\.\d*)\s+([\+\-]?\d+\.\d*)\s+(\d+)\s*$/)
+            {
+              $$vtmp{"lat"} = $1;
+              $$vtmp{"long"} = $2;
+              $$vtmp{"time"} = $3;
+              push(@farray, $vtmp);
+            }
+            else
+            {
+              print STDERR "Data mismatch #".$nline.": ".$pline."\n";
+            }
+          }
         }
       }
-
-      my $findex = 0;
-      foreach my $fline (split(/\n/, $fshit->{"gml:rangeSet"}{"gml:DataBlock"}{"gml:doubleOrNilReasonTupleList"}))
+      else
       {
-        my @fmatches = ($fline =~ /\s*([\+\-]?\d+\.\d*|NaN)\s*/ig);
-        if (scalar(@fmatches) > 0)
-        {
-          if (scalar(@fmatches) != scalar(@fmitems))
-          {
-            print STDERR "Not enough items in scalar line (".
-              scalar(@fmatches). " vs ".scalar(@fmitems)."): ".$fline."\n";
-            goto skip_it;
-          }
-          for (my $fni = 0; $fni < scalar(@fmitems); $fni++)
-          {
-            $farray[$findex]{$fmitems[$fni]} = $fmatches[$fni] if (lc($fmatches[$fni]) ne "nan");
-          }
-          $findex++;
-        }
+        print STDERR "Position and data lines do not match.\n";
+        goto skip_it;
       }
       # XXX Hashify the array into lat/long keys