-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen-meteo-graph.py
executable file
·162 lines (125 loc) · 4.23 KB
/
open-meteo-graph.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!./bin/python
import openmeteo_requests
from openmeteo_sdk.WeatherApiResponse import WeatherApiResponse
import requests_cache
from retry_requests import retry
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from datetime import datetime
from datetime import timedelta
import numpy as np
from numpy import ndarray
url = "https://api.open-meteo.com/v1/forecast"
hourly_metrics = "temperature_2m"
timezone = "auto"
past_days = 1
forecast_days = 3
colours = iter(["#2caffe", "#ffa808", "#35b035"])
entries = [{
"name": "Example 1",
"colour": next(colours),
"params": {
"latitude": 34.3434,
"longitude": -4.44,
}
}, {
"name": "Example 2",
"colour": next(colours),
"params": {
"latitude": 2.22,
"longitude": 11.1111,
}
}, {
"name": "Example 3",
"colour": next(colours),
"params": {
"latitude": 1.234,
"longitude": -4.321,
}
}]
for entry in entries:
entry["params"]["hourly"] = hourly_metrics
entry["params"]["timezone"] = timezone
entry["params"]["past_days"] = past_days
entry["params"]["forecast_days"] = forecast_days
def get_data(entries) -> WeatherApiResponse:
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)
return openmeteo.weather_api(url, params=entries["params"])[0]
def get_offset_time(now_dec: float) -> int:
if now_dec < 4:
return 10
elif now_dec < 16:
return 24
else:
return 24 + 10
def make_x_ticks(offset: int) -> list[str]:
return [(datetime.today() +
timedelta(days=-1, hours=hour)).strftime('%d %b') if hour %
24 == 0 else f'{hour % 24}:00' if hour % 2 == 0 else ''
for hour in range(offset, hours + offset)]
def get_temp(response: WeatherApiResponse, offset: int) -> list[float]:
return [temp for temp in response.Hourly().Variables(0).ValuesAsNumpy()
][offset:hours + offset] # type: ignore
def get_y_tick_spacing(temp: list[float]) -> float:
spacings = [1, 2, 5, 10]
spacing_idx = 0
tick_spacing = 1
while (max(temp) - min(temp)) / tick_spacing > 8:
spacing_idx += 1
tick_spacing = spacings[spacing_idx]
return tick_spacing
def round_with_precision(num: float, precision: float) -> float:
return precision * (num // precision)
def make_y_ticks(low: float, high: float, step: float) -> ndarray:
return np.arange(low, high + step, step)
def init_figure() -> Figure:
plt.style.use('dark_background')
mpl.rcParams['axes.spines.left'] = False
mpl.rcParams['axes.spines.top'] = False
mpl.rcParams['axes.spines.right'] = False
mpl.rcParams['toolbar'] = 'None'
return plt.figure(f'{hours - 1}h temperature', figsize=(12, 4))
hours = 34 + 1
now = datetime.today()
now_dec = now.hour + now.minute / 60
offset = get_offset_time(now_dec)
x_time = range(offset, hours + offset)
x_ticks = range(offset, hours + offset)
x_tick_labels = make_x_ticks(offset)
fig = init_figure()
tick_spacing = 0
low_lim = 10000 # if your location exceeds this you have bigger problems
high_lim = -10000
for entry in entries:
response = get_data(entry)
y_temp = get_temp(response, offset)
tick_spacing = max(tick_spacing, get_y_tick_spacing(y_temp))
low_lim = min(low_lim, round_with_precision(min(y_temp), tick_spacing))
high_lim = max(
high_lim,
round_with_precision(max(y_temp), tick_spacing) + tick_spacing)
plt.plot(x_time,
y_temp,
color=entry["colour"],
marker='o',
linewidth=1,
label=entry["name"])
y_ticks = make_y_ticks(low_lim, high_lim, tick_spacing)
high_lim += 0.001 * tick_spacing
plt.axvline(now_dec + 24 if (offset != 0 and now_dec < 24) else now_dec,
color='#646464',
marker='|',
linewidth=1)
plt.grid(True, axis='y', alpha=0.5)
fig.axes[0].tick_params(left=False)
plt.xlim([offset - 0.25, offset + hours - 1 + 0.25])
plt.xticks(x_ticks, x_tick_labels)
plt.ylim([low_lim, high_lim])
plt.yticks(y_ticks)
plt.ylabel('°C')
plt.legend()
plt.tight_layout()
plt.show()