-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_meteo_api.py
100 lines (77 loc) · 4.07 KB
/
open_meteo_api.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# https://open-meteo.com/
import openmeteo_requests
import requests_cache
import pandas as pd
from retry_requests import retry
def get_weather_data(params):
# Setup the Open-Meteo API client with cache and retry on error
cache_session = requests_cache.CachedSession('.cache', expire_after = 3600)
retry_session = retry(cache_session, retries = 5, backoff_factor = 0.2)
openmeteo = openmeteo_requests.Client(session = retry_session)
# Make sure all required weather variables are listed here
# The order of variables in hourly or daily is important to assign them correctly below
url = "https://api.open-meteo.com/v1/forecast"
# params = {
# "latitude": 51.4108,
# "longitude": -0.6748,
# "current": "temperature_2m",
# "hourly": "temperature_2m"
# }
responses = openmeteo.weather_api(url, params=params)
# Process first location. Add a for-loop for multiple locations or weather models
response = responses[0]
print(f"Coordinates {response.Latitude()}°N {response.Longitude()}°E")
print(f"Elevation {response.Elevation()} m asl")
print(f"Timezone {response.Timezone()} {response.TimezoneAbbreviation()}")
print(f"Timezone difference to GMT+0 {response.UtcOffsetSeconds()} s")
# Current values. The order of variables needs to be the same as requested.
current = response.Current()
current_temperature_2m = current.Variables(0).Value()
print(f"Current time {current.Time()}")
print(f"Current temperature_2m {current_temperature_2m}")
# Process hourly data. The order of variables needs to be the same as requested.
# hourly = response.Hourly()
# hourly_temperature_2m = hourly.Variables(0).ValuesAsNumpy()
# hourly_data = {"date": pd.date_range(
# start = pd.to_datetime(hourly.Time(), unit = "s", utc = True),
# end = pd.to_datetime(hourly.TimeEnd(), unit = "s", utc = True),
# freq = pd.Timedelta(seconds = hourly.Interval()),
# inclusive = "left"
# )}
# hourly_data["temperature_2m"] = hourly_temperature_2m
# hourly_dataframe = pd.DataFrame(data = hourly_data)
# print(hourly_dataframe)
return current_temperature_2m
def get_air_data(params):
# Setup the Open-Meteo API client with cache and retry on error
cache_session = requests_cache.CachedSession('.cache', expire_after = 3600)
retry_session = retry(cache_session, retries = 5, backoff_factor = 0.2)
openmeteo = openmeteo_requests.Client(session = retry_session)
# Make sure all required weather variables are listed here
# The order of variables in hourly or daily is important to assign them correctly below
url = f"https://air-quality-api.open-meteo.com/v1/air-quality?latitude={params["latitude"]}&longitude={params["longitude"]}¤t=european_aqi"
responses = openmeteo.weather_api(url, params=params)
# Process first location. Add a for-loop for multiple locations or weather models
response = responses[0]
print(f"Coordinates {response.Latitude()}°N {response.Longitude()}°E")
print(f"Elevation {response.Elevation()} m asl")
print(f"Timezone {response.Timezone()} {response.TimezoneAbbreviation()}")
print(f"Timezone difference to GMT+0 {response.UtcOffsetSeconds()} s")
# Current values. The order of variables needs to be the same as requested.
current = response.Current()
print('Current:', current)
european_aqi = current.Variables(0).Value()
print(f"Current time {current.Time()}")
# Process hourly data. The order of variables needs to be the same as requested.
# hourly = response.Hourly()
# hourly_temperature_2m = hourly.Variables(0).ValuesAsNumpy()
# hourly_data = {"date": pd.date_range(
# start = pd.to_datetime(hourly.Time(), unit = "s", utc = True),
# end = pd.to_datetime(hourly.TimeEnd(), unit = "s", utc = True),
# freq = pd.Timedelta(seconds = hourly.Interval()),
# inclusive = "left"
# )}
# hourly_data["temperature_2m"] = hourly_temperature_2m
# hourly_dataframe = pd.DataFrame(data = hourly_data)
# print(hourly_dataframe)
return european_aqi