-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheonet.py
56 lines (50 loc) · 1.69 KB
/
eonet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Plot of all EONET (Earth Observatory Natural Event Tracker) events data in the last 365 days.
API: https://eonet.gsfc.nasa.gov/docs/v2.1/events
(There is also an Category-API.)
"""
import requests
import json
import plotly.express as px
limit = -1 # All events in period
days = 365 # last year
file_name = 'events_eonet.json'
try:
# Read data
with open(file_name) as f:
events_data = f.readall()
except Exception:
# Request data
url = f'https://eonet.gsfc.nasa.gov/api/v2.1/events?limit={limit}&days={days}'
r = requests.get(url)
# Parse data
events_data = r.json()
# Save data
with open(file_name, 'w') as f:
f.write(json.dumps(events_data, indent=4))
# Extract coordinates from geoJSON and event title
longitudes, latitudes = [], []
hover_names = []
for event in events_data['events']:
coordinates = event['geometries'][0]['coordinates']
longitude, latitude = coordinates[0], coordinates[1]
# Check consitency
# Some coordinates are obvious switched and easily to filter but there is
# also an iclandic volcano in the indian ocean.
if -180 <= longitude <= 180 and -90 <= latitude <= 90:
longitudes.append(longitude)
latitudes.append(latitude)
# Configure hover name
title = event['title']
category = event['categories'][0]['title']
full_title = f'{title} [{category}]'
hover_names.append(full_title)
# Plot
plot_data = {'Latitude': latitudes,
'Longitude': longitudes,
'Hover_names': hover_names}
fig = px.scatter_geo(plot_data,
lat='Latitude',
lon='Longitude',
hover_name='Hover_names')
fig.show()