comparison 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
comparison
equal deleted inserted replaced
237:56af82fb7a6d 238:a1d6e2d8789e
442 for (my $n = 1; $n < scalar(@$oelems); $n++) 442 for (my $n = 1; $n < scalar(@$oelems); $n++)
443 { 443 {
444 my $fdata = @$oelems[$n]->{"nodes"}; 444 my $fdata = @$oelems[$n]->{"nodes"};
445 $weatherdata->{get_node($fdata, "text", 0)} = 445 $weatherdata->{get_node($fdata, "text", 0)} =
446 [ 446 [
447 # type, timestamp, temperature
447 0, 448 0,
448 parse_timestamp(get_node($fdata, "text", 1), $time_base), 449 parse_timestamp(get_node($fdata, "text", 1), $time_base),
449 get_node_lc($fdata, "text", 2), 450 get_node_lc($fdata, "text", 2),
451 # and the rest
450 get_node_lc($fdata, "text", 3), 452 get_node_lc($fdata, "text", 3),
451 translate_th_rain(get_node($fdata, "text", 4)), 453 translate_th_rain(get_node($fdata, "text", 4)),
452 "",
453 get_node_lc($fdata, "text", 5), 454 get_node_lc($fdata, "text", 5),
454 ]; 455 ];
455 } 456 }
456 } 457 }
457 } 458 }
463 ### Fetch FMI data 464 ### Fetch FMI data
464 ### 465 ###
465 if (opt_chk_bool("opt_fmi")) 466 if (opt_chk_bool("opt_fmi"))
466 { 467 {
467 die("FMI data scrape enabled, but no API key set.\n") unless opt_chk_valid("fmi_api_key", 10); 468 die("FMI data scrape enabled, but no API key set.\n") unless opt_chk_valid("fmi_api_key", 10);
469 my @fmitems = ("temperature", "humidity", "windspeedms", "totalcloudcover");
468 470
469 my $uri = "http://data.fmi.fi/fmi-apikey/".opt_get("fmi_api_key"). 471 my $uri = "http://data.fmi.fi/fmi-apikey/".opt_get("fmi_api_key").
470 "/wfs?request=getFeature&storedquery_id=fmi::observations::weather::". 472 "/wfs?request=getFeature&storedquery_id=fmi::observations::weather::".
471 "multipointcoverage". 473 "multipointcoverage".
472 # "timevaluepair". 474 # "timevaluepair".
473 "&starttime=".format_time_gmt(time() - 10*60)."&endtime=".format_time_gmt(time()). 475 "&starttime=".format_time_gmt(time() - 10*60)."&endtime=".format_time_gmt(time()).
474 "&parameters=temperature,humidity&maxlocations=100&bbox=19,59,32,75"; 476 "&parameters=".join(",", @fmitems)."&maxlocations=100&bbox=19,59,32,75";
475 477
476 print STDERR "FMI URI: ".$uri."\n" if opt_chk_bool("debug"); 478 print STDERR "FMI URI: ".$uri."\n" if opt_chk_bool("debug");
477 479
478 my $res = fetch_http($uri); 480 my $res = fetch_http($uri);
479 if ($res->code >= 200 && $res->code <= 201) 481 if ($res->code >= 200 && $res->code <= 201)
496 } 498 }
497 499
498 my $findex = 0; 500 my $findex = 0;
499 foreach my $fline (split(/\n/, $fshit->{"gml:rangeSet"}{"gml:DataBlock"}{"gml:doubleOrNilReasonTupleList"})) 501 foreach my $fline (split(/\n/, $fshit->{"gml:rangeSet"}{"gml:DataBlock"}{"gml:doubleOrNilReasonTupleList"}))
500 { 502 {
501 if ($fline =~ /^\s*([\+\-]?\d+\.\d*|NaN)\s+([\+\-]?\d+\.\d*|NaN)\s*$/) 503 my @fmatches = ($fline =~ /\s*([\+\-]?\d+\.\d*|NaN)\s*/ig);
504 if (scalar(@fmatches) > 0)
502 { 505 {
503 $farray[$findex]{"temp"} = $1 if ($1 ne "NaN"); 506 die("Not enough items in scalar line (".scalar(@fmatches). " vs ".scalar(@fmitems).
504 $farray[$findex]{"humidity"} = $2 if ($2 ne "NaN"); 507 "): ".$fline."\n") if (scalar(@fmatches) != scalar(@fmitems));
508 for (my $fni = 0; $fni < scalar(@fmitems); $fni++)
509 {
510 $farray[$findex]{$fmitems[$fni]} = $fmatches[$fni] if (lc($fmatches[$fni]) ne "nan");
511 }
505 $findex++; 512 $findex++;
506 } 513 }
507 } 514 }
508 # XXX Hashify the array into lat/long keys 515 # XXX Hashify the array into lat/long keys
509 516
523 $weatherdata->{$floc->{"gml:name"}} = 530 $weatherdata->{$floc->{"gml:name"}} =
524 [ 531 [
525 1, 532 1,
526 plonk_data($flol->{"time"}), 533 plonk_data($flol->{"time"}),
527 plonk_data($flol->{"temperature"}), 534 plonk_data($flol->{"temperature"}),
528 "", 535
529 "",
530 plonk_data($flol->{"humidity"}), 536 plonk_data($flol->{"humidity"}),
531 plonk_data($flol->{"windspeedms"}), 537 plonk_data($flol->{"windspeedms"}),
532 plonk_data($flol->{"totalcloudcover"}), 538 plonk_data($flol->{"totalcloudcover"}),
533 ]; 539 ];
534 } 540 }