-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather_data.py
80 lines (63 loc) · 2.86 KB
/
weather_data.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import numpy as np
import requests
ARCHIVE_URL = "https://archive-api.open-meteo.com/v1/era5"
FORECAST_URL = "https://api.open-meteo.com/v1/forecast"
def get_latest_recorded_date():
"""
Returns the latest recorded date in the weather archive database.
Returns:
str: The latest recorded date in YYYY-MM-DD format.
"""
filter = f"latitude=0&longitude=0&start_date=2023-01-01&end_date=2030-12-31"
r = requests.get(ARCHIVE_URL + '?' + filter)
latest_recorded_date = r.json()['reason'][-10:]
return latest_recorded_date
def get_archived_weather(lat, long, start, end, metrics):
"""
Returns historical weather data for a given latitude and longitude, time range, and set of metrics.
Args:
lat (float): Latitude of location.
long (float): Longitude of location.
start (str): Start date in YYYY-MM-DD format.
end (str): End date in YYYY-MM-DD format.
metrics (list): List of weather metrics to retrieve (https://open-meteo.com/en/docs for more details).
Returns:
dict: Dictionary containing weather data, with latitude and longitude as separate keys and weather metrics as sub-dictionaries.
"""
metrics = ','.join(metrics)
filter = f'latitude={lat}&longitude={long}&hourly={metrics}&start_date={start}&end_date={end}'
r = requests.get(ARCHIVE_URL + '?' + filter)
d = r.json()
return {**{'latitude': d['latitude'], 'longitude': d['longitude']}, **d['hourly']}
def get_forecast_weather(lat, long, metrics):
"""
Returns forecasted weather data for a given latitude and longitude and set of metrics.
Args:
lat (float): Latitude of location.
long (float): Longitude of location.
metrics (list): List of weather metrics to retrieve (e.g. ['temperature', 'precipitation']).
Returns:
dict: Dictionary containing weather data, with latitude and longitude as separate keys and weather metrics as sub-dictionaries.
"""
metrics = ','.join(metrics)
filter = f'latitude={lat}&longitude={long}&hourly={metrics}'
r = requests.get(FORECAST_URL + '?' + filter)
d = r.json()
return {**{'latitude': d['latitude'], 'longitude': d['longitude']}, **d['hourly']}
def get_distance(lon1, lat1, lon2, lat2):
"""
Returns the distance, in kilometers, between two sets of longitude/latitude coordinates.
Args:
lon1 (float): Longitude of first location.
lat1 (float): Latitude of first location.
lon2 (float): Longitude of second location.
lat2 (float): Latitude of second location.
Returns:
float: The distance, in kilometers, between the two sets of coordinates.
"""
lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])
newlon = lon2 - lon1
newlat = lat2 - lat1
haver_formula = np.sin(newlat/2.0)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(newlon/2.0)**2
km = 6367 * 2 * np.arcsin(np.sqrt(haver_formula ))
return km