Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: creating a webpage to fetch/calculate weather data

by Aldebaran (Curate)
on Jun 23, 2020 at 22:58 UTC ( [id://11118397]=note: print w/replies, xml ) Need Help??


in reply to Re: creating a webpage to fetch/calculate weather data
in thread creating a webpage to fetch/calculate weather data

1. One of the Atlanta.pm members was raving at the last meeting about how useful Mojo is for web munging (scraping, parsing, and re-publishing as an internal "API"). I have no personal experience with Mojo, but I trust his ravings.

Then Mojo it is. Thanks so much for your post, Util, it helped me get farther in a lot of ways. I was able to get this to work on the command line, but I couldn't get Corion's curl converter to recognize a -S option, nor could I see it from man curl. The command seems to give the same output without it:

$ curl -s 'https://nationalmap.gov/epqs/pqs.php?units=Meters&output=js +on&x=-83.65&y=41.37' | jq '.USGS_Elevation_Point_Query_Service.Elevat +ion_Query.Elevation' 212.77 $

Unfortunately, I couldn't get this value in the perl script which I'm posting below. (Partial credit is great for me.)

NOAA has the elevation of the 2.5km grid polygon, which will be lower granularity, but might (I'm just guessing) be helpful for water, where the elevation varies:

I was thrilled to see this solution at the same server where I had been fishing! Furthermore, your example along with the jq command to see where the good data is in the haystack was exactly what I needed to get some numbers out of this. First I'll post the log then the source.

2020/06/23 15:26:07 INFO ./5.2.elev.pl 2020/06/23 15:26:07 INFO { "USGS_Elevation_Point_Query_Service" => { "Elevation_Query" => { "Data_Source" => "3DEP 1/3 arc-second", "Elevation" => -1000000, "Units" => "Meters", "x" => "41.37", "y" => "-83.65" } } } 2020/06/23 15:26:07 INFO 41.37 -83.65 0.208 2020/06/23 15:26:07 INFO ============== 2020/06/23 15:26:07 INFO https://api.weather.gov/points/41.37,-83.65 2020/06/23 15:26:08 INFO CLE 2020/06/23 15:26:08 INFO 16 52 2020/06/23 15:26:08 INFO ============== 2020/06/23 15:26:08 INFO https://api.weather.gov/gridpoints/CLE/16,52 2020/06/23 15:26:08 INFO ============== 2020/06/23 15:26:08 INFO 213.0552 wmoUnit:m

Source:

#!/usr/bin/env perl use strict; use warnings; use 5.016; use Log::Log4perl; use Mojo::UserAgent; use open ':std', OUT => ':utf8'; use Mojo::Util qw(dumper); # get rid of old log my $file = '/home/hogan/Documents/hogan/logs/4.log4perl.txt'; unlink $file or warn "Could not unlink $file: $!"; my $log_conf4 = "/home/hogan/Documents/hogan/logs/conf_files/4.conf"; Log::Log4perl::init($log_conf4); #info my $logger = Log::Log4perl->get_logger(); $logger->info("$0"); # weston, OH 41.37,-83.65 my $lat = 41.37; my $long = -83.65; # the API docs says you must identify yourself, please make this somet +hing legit my $name = '(example.com, contact@example.com)'; my $url2 = "https://nationalmap.gov/epqs/pqs.php?output=json&units=Meters&x=$lat& +y=$long"; my $ub = Mojo::UserAgent->new; $ub->transactor->name($name); # get JSON response my $qv = $ub->get($url2)->res->json; $logger->info( dumper $qv); my $elev = .208; # 676 ft = .206 +.02 for observer #km $logger->info("$lat $long $elev"); my $url1 = "https://api.weather.gov/points/$lat,$long"; $logger->info("=============="); $logger->info($url1); my $ua = Mojo::UserAgent->new; $ua->transactor->name($name); # get JSON response my $pv = $ua->get($url1)->res->json->{properties}; my $station = $pv->{'cwa'}; my $gridX = $pv->{'gridX'}; my $gridY = $pv->{'gridY'}; $logger->info("$station"); $logger->info("$gridX $gridY"); $logger->info("=============="); ## new transaction ## find elevation my $uc = Mojo::UserAgent->new; $uc->transactor->name($name); my $url3 = "https://api.weather.gov/gridpoints/$station/$gridX,$gridY" +; $logger->info( $url3 ); my $json = $uc->get($url3)->res->json; #my $props = $json->{properties}; my $elev2 = $json->{properties}->{elevation}->{value}; my $unit = $json->{properties}->{elevation}->{unitCode}; $logger->info("=============="); #$logger->info(dumper($props)); $logger->info( $elev2 . ' '. $unit); __END__

It takes me time fiddling with these things with perl and jq in order to get my head around them.

Q1) How do I get a valid value for the usgs query in lexical perl?

Thanks again for your post,

Replies are listed 'Best First'.
Re^3: creating a webpage to fetch/calculate weather data
by choroba (Cardinal) on Jun 23, 2020 at 23:55 UTC
    > recognize a -S option, nor could I see it from man curl.

    It seems to be a recent addition. See -S in man curl.

    -S, --show-error
    When used with -s, --silent, it makes curl show an error message if it fails.
    -s, --silent
    Silent or quiet mode. Don't show progress meter or error messages. Makes Curl mute. It will still output the data you ask for, potentially even to the terminal/stdout unless you redirect it.

    I doubt this option influences what gets downloaded.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11118397]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found