comparison fetch_weather.pl @ 208:c6bbf69afc57

Add some error handling and debugging.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 15 Aug 2014 10:18:23 +0300
parents 2ac661d551b3
children 5f04ec60e8d9
comparison
equal deleted inserted replaced
207:86bf1a0cd9e1 208:c6bbf69afc57
48 48
49 sub fetch_http($) 49 sub fetch_http($)
50 { 50 {
51 my $agent = LWP::UserAgent->new; 51 my $agent = LWP::UserAgent->new;
52 $agent->agent($settings{"http_user_agent"}); 52 $agent->agent($settings{"http_user_agent"});
53 $agent->timeout(10); 53 $agent->timeout(20);
54 54
55 my $req = HTTP::Request->new(GET => $_[0]); 55 my $req = HTTP::Request->new(GET => $_[0]);
56 56
57 return $agent->request($req); 57 return $agent->request($req);
58 } 58 }
416 my $res = fetch_http($uri); 416 my $res = fetch_http($uri);
417 if ($res->code >= 200 && $res->code <= 201) 417 if ($res->code >= 200 && $res->code <= 201)
418 { 418 {
419 my $xml = XMLin($res->decoded_content); 419 my $xml = XMLin($res->decoded_content);
420 my $time_base = time(); 420 my $time_base = time();
421 my $fdata = $xml->{"wfs:member"}{"omso:GridSeriesObservation"};
422 421
423 my $fshit = $fdata->{"om:result"}{"gmlcov:MultiPointCoverage"}; 422 if (defined($xml->{"wfs:member"}{"omso:GridSeriesObservation"}))
424 my @farray = ();
425
426 foreach my $fline (split(/\n/, $fshit->{"gml:domainSet"}{"gmlcov:SimpleMultiPoint"}{"gmlcov:positions"}))
427 { 423 {
428 if ($fline =~ /^\s*([\+\-]?\d+\.\d*)\s+([\+\-]?\d+\.\d*)\s+(\d+)\s*$/) 424 my $fdata = $xml->{"wfs:member"}{"omso:GridSeriesObservation"};
425 my $fshit = $fdata->{"om:result"}{"gmlcov:MultiPointCoverage"};
426 my @farray = ();
427
428 foreach my $fline (split(/\n/, $fshit->{"gml:domainSet"}{"gmlcov:SimpleMultiPoint"}{"gmlcov:positions"}))
429 { 429 {
430 push(@farray, {"lat" => $1, "long" => $2, "time" => $3}); 430 if ($fline =~ /^\s*([\+\-]?\d+\.\d*)\s+([\+\-]?\d+\.\d*)\s+(\d+)\s*$/)
431 } 431 {
432 } 432 push(@farray, {"lat" => $1, "long" => $2, "time" => $3});
433 433 }
434 my $findex = 0; 434 }
435 foreach my $fline (split(/\n/, $fshit->{"gml:rangeSet"}{"gml:DataBlock"}{"gml:doubleOrNilReasonTupleList"})) 435
436 { 436 my $findex = 0;
437 if ($fline =~ /^\s*([\+\-]?\d+\.\d*|NaN)\s+([\+\-]?\d+\.\d*|NaN)\s*$/) 437 foreach my $fline (split(/\n/, $fshit->{"gml:rangeSet"}{"gml:DataBlock"}{"gml:doubleOrNilReasonTupleList"}))
438 { 438 {
439 $farray[$findex]{"temp"} = $1 if ($1 ne "NaN"); 439 if ($fline =~ /^\s*([\+\-]?\d+\.\d*|NaN)\s+([\+\-]?\d+\.\d*|NaN)\s*$/)
440 $farray[$findex]{"humidity"} = $2 if ($2 ne "NaN"); 440 {
441 $findex++; 441 $farray[$findex]{"temp"} = $1 if ($1 ne "NaN");
442 } 442 $farray[$findex]{"humidity"} = $2 if ($2 ne "NaN");
443 } 443 $findex++;
444 # XXX Hashify the array into lat/long keys 444 }
445 445 }
446 # This is horrible :S 446 # XXX Hashify the array into lat/long keys
447 my $fcrap = $fdata->{"om:featureOfInterest"}{"sams:SF_SpatialSamplingFeature"}{"sams:shape"}{"gml:MultiPoint"}{"gml:pointMember"}; 447
448 foreach my $fnode (@{$fcrap}) 448 # This is horrible :S
449 { 449 my $fcrap = $fdata->{"om:featureOfInterest"}{"sams:SF_SpatialSamplingFeature"}{"sams:shape"}{"gml:MultiPoint"}{"gml:pointMember"};
450 my $floc = $fnode->{"gml:Point"}; 450 foreach my $fnode (@{$fcrap})
451 if ($floc->{"gml:pos"} =~ /^\s*([\+\-]?\d+\.\d*)\s+([\+\-]?\d+\.\d*)\s*$/)
452 { 451 {
453 my ($flat, $flong) = ($1, $2); 452 my $floc = $fnode->{"gml:Point"};
454 # Should use a hash - 453 if ($floc->{"gml:pos"} =~ /^\s*([\+\-]?\d+\.\d*)\s+([\+\-]?\d+\.\d*)\s*$/)
455 foreach my $flol (@farray)
456 { 454 {
457 if ($flol->{"lat"} == $flat && $flol->{"long"} == $flong) 455 my ($flat, $flong) = ($1, $2);
456 # Should use a hash -
457 foreach my $flol (@farray)
458 { 458 {
459 $weatherdata->{$floc->{"gml:name"}} = 459 if ($flol->{"lat"} == $flat && $flol->{"long"} == $flong)
460 [ 460 {
461 1, 461 $weatherdata->{$floc->{"gml:name"}} =
462 $flol->{"time"}, 462 [
463 $flol->{"temp"}, 463 1,
464 "", 464 $flol->{"time"},
465 "", 465 $flol->{"temp"},
466 defined($flol->{"humidity"}) ? $flol->{"humidity"} : "", 466 "",
467 ]; 467 "",
468 defined($flol->{"humidity"}) ? $flol->{"humidity"} : "",
469 ];
470 }
468 } 471 }
469 } 472 }
470 } 473 }
471 } 474 } else {
475 # defined
476 print STDERR "Invalid XML received:\n";
477 print STDERR $res->decoded_content."\n\n";
478 }
479 } else {
480 print STDERR "Error fetching FMI XML: ".$res->status_line."\n";
472 } 481 }
473 } 482 }
474 483
475 484
476 ### 485 ###