# HG changeset patch # User Matti Hamalainen # Date 1454860449 -7200 # Node ID b706e8ebecf8628c73e1c76e8812c51ac7d8702e # Parent 0a6d9f0f0a57602ec8a0ecef7f97d097e0b04f37 fetch_weather: Possibly improve FMI XML mess parsing. diff -r 0a6d9f0f0a57 -r b706e8ebecf8 fetch_weather.pl --- 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