Skip to content

Commit c2827db

Browse files
authored
Merge pull request #30 from Mixss/quickfixes
Generate advanced forecasts with real data and fix bot-breaking bugs
2 parents 78e9cc0 + 663caf5 commit c2827db

File tree

4 files changed

+83
-46
lines changed

4 files changed

+83
-46
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ env/
22
__pycache__/
33
venv/
44
.idea/
5-
logo.png
5+
logo.png
6+
.venv/

data/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"BroadcastChannels": [{"ServerId": 465940323365814272, "ChannelId": 828951713074380840}, {"ServerId": 913375693864308797, "ChannelId": 958682054126612480}]}
1+
{"BroadcastChannels": [{"ServerId": 465940323365814272, "ChannelId": 828951713074380840}, {"ServerId": 913375693864308797, "ChannelId": 958119472793780304}]}

logic/advanced_forecast.py

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import matplotlib as mpl
55
from scipy.interpolate import make_interp_spline, BSpline
66

7+
from logic.logic import get_advanced_hourly_forecast, get_day_of_week_capitalized, get_today
8+
79
INTERPOLATION_COEFFICIENT = 20
810

911
mpl.rcParams['axes.labelsize'] = 18
@@ -16,84 +18,85 @@ def interpolate_line(x, y, k_coeff):
1618
x_arr = np.array(x).astype(float)
1719
y_arr = np.array(y).astype(float)
1820

19-
new_x = np.linspace(x_arr.min(), x_arr.max(), INTERPOLATION_COEFFICIENT * x_arr.size)
21+
x_range = np.arange(len(x))
22+
23+
spline = make_interp_spline(x_range, y_arr, k=k_coeff)
24+
25+
x_smooth = np.linspace(0, len(x_arr) - 1, INTERPOLATION_COEFFICIENT * x_arr.size)
2026

21-
spline = make_interp_spline(x_arr, y_arr, k=k_coeff)
22-
y_smooth = spline(new_x)
27+
y_smooth = spline(x_smooth)
2328

24-
return new_x, y_smooth
29+
interpolated_x_in_x = np.interp(x_smooth, x_range, x_arr)
2530

31+
return interpolated_x_in_x, y_smooth
2632

27-
def graph_temperature(canvas, hours):
28-
temperatures = [-4.5, -2., 0., 5., 2., 1., -0.3, 1.7, 1.9, 6.9, 7.8, 3.0]
29-
temperatures_felt = [-5.3, -3.2, -1.0, 2.0, 1.0, -0.8, 1.3, -1.5, 0.1, 4.2, 5.0, 3.8]
3033

31-
_, temperatures_interp = interpolate_line(hours, temperatures, 3)
32-
hours_extended, temperatures_felt_interp = interpolate_line(hours, temperatures_felt, 3)
34+
def graph_temperature(canvas, hours, temperatures, temperatures_feel):
3335

34-
canvas.plot(hours_extended, temperatures_interp, 'r', label="Temperatura rzeczywista", linewidth=4.0)
35-
canvas.plot(hours_extended, temperatures_felt_interp, 'b', label="Temperatura odczuwalna", linewidth=4.0)
36-
canvas.plot(np.array(hours).astype(float), temperatures, 'r.', markersize=15)
37-
canvas.plot(np.array(hours).astype(float), temperatures_felt, 'b.', markersize=15)
36+
canvas.plot(hours, temperatures, 'r', label="Temperatura rzeczywista", linewidth=4.0)
37+
canvas.plot(hours, temperatures_feel, 'b', label="Temperatura odczuwalna", linewidth=4.0)
3838
canvas.legend(fontsize=14)
3939

40-
canvas.set_title('Poniedziałek, 24.06')
4140
canvas.set_ylabel('Temperatura [°C]')
4241

43-
canvas.set_xlim(float(hours[0]), float(hours[-1]))
42+
canvas.margins(x=0.0)
4443

4544
canvas.grid(True)
4645

4746

48-
def graph_rainfall(canvas, hours):
49-
50-
rainfall = [11, 12, 10, 15, 20, 10, 7, 0, 1, 3, 3, 6]
51-
humidity = [0, 0, 0, 0, 15, 20, 32, 11, 8, 4, 0, 1]
52-
53-
hours_interp, humidity_interp = interpolate_line(np.array(hours).astype(float), np.array(humidity).astype(float), 3)
47+
def graph_rainfall(canvas, hours, rainfall, humidity):
5448

5549
secondary_canvas = canvas.twinx()
56-
secondary_canvas.plot(hours_interp, humidity_interp, color='#ff9914', linewidth=4.0)
57-
canvas.bar(np.array(hours).astype(float), rainfall, width=0.7, color='#29bf12', edgecolor='#006400', linewidth=2.0)
50+
secondary_canvas.plot(hours, humidity, color='#ff9914', linewidth=4.0)
51+
canvas.bar(hours[:-1], rainfall[:-1], width=0.8, color='#29bf12', edgecolor='#006400', linewidth=4.0, align='edge')
52+
canvas.set_ylim(bottom=0)
5853

59-
canvas.set_xlim(float(hours[0]), float(hours[-1]))
54+
canvas.margins(x=0.0)
6055

6156
canvas.set_ylabel('Opady [mm/h]')
6257
secondary_canvas.set_ylabel('Wilgotność [%]')
6358
canvas.grid(True)
6459

6560

66-
def graph_wind(canvas, hours):
61+
def graph_wind(canvas, hours, wind_speed, wind_gust):
6762

68-
wind_speed = [0, 5, 6, 18, 3, 20, 25, 41, 20, 11, 6, 2]
69-
wind_gust = [5, 11, 21, 28, 27, 29, 45, 47, 26, 17, 10, 5]
63+
canvas.plot(hours, wind_speed, linewidth=3.0, color='#4189e8')
64+
canvas.hlines(wind_gust, xmin=[i - 0.2 for i, x in enumerate(hours)], xmax=[i + 0.2 for i, x in enumerate(hours)], colors='red', label='Podmuchy wiatru')
7065

71-
hours_interp, wind_interp = interpolate_line(hours, wind_speed, 3)
72-
73-
canvas.plot(hours_interp, wind_interp, linewidth=3.0, color='#4189e8')
74-
canvas.plot(np.array(hours).astype(float), wind_speed, '.', markersize=15, color='#4189e8')
75-
canvas.errorbar(np.array(hours).astype(float), wind_gust, xerr=0.45, fmt='none', color='#ed3a37')
76-
77-
canvas.set_xlim(float(hours[0]), float(hours[-1]))
66+
canvas.margins(x=0.0)
7867

7968
canvas.set_ylabel('Prędkość wiatru [km/h]')
8069

8170
secondary_canvas = canvas.twinx()
82-
secondary_canvas.plot(hours_interp, [x * 1000 / 3600 for x in wind_interp], linewidth=3.0, color='#4189e8')
71+
secondary_canvas.plot(hours, [x * 1000 / 3600 for x in wind_speed], linewidth=3.0, color='#4189e8')
8372
secondary_canvas.set_ylim(canvas.get_ylim()[0] * 1000 / 3600, canvas.get_ylim()[1] * 1000 / 3600)
8473
secondary_canvas.set_ylabel('Prędkość wiatru [m/s]')
8574

8675
canvas.grid(True)
8776

8877

8978
def generate_graphs_image():
90-
figure, axes = plt.subplots(3, 1, figsize=(10, 9))
79+
forecast = get_advanced_hourly_forecast()
9180

92-
hours = ['10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21']
81+
figure, axes = plt.subplots(3, 1, figsize=(10, 9))
9382

94-
graph_temperature(axes[0], hours)
95-
graph_rainfall(axes[1], hours)
96-
graph_wind(axes[2], hours)
83+
hours = forecast['hours']
84+
85+
axes[0].set_title(f'{get_day_of_week_capitalized()}, {get_today()}')
86+
87+
graph_temperature(
88+
axes[0], hours,
89+
temperatures=forecast['temperature'],
90+
temperatures_feel=forecast['temperature_feel'])
91+
graph_rainfall(
92+
axes[1], hours,
93+
rainfall=forecast['rainfall'],
94+
humidity=forecast['humidity'])
95+
graph_wind(
96+
axes[2], hours,
97+
wind_speed=forecast['wind_speed'],
98+
wind_gust=forecast['wind_gust']
99+
)
97100

98101
plt.tight_layout()
99102
plt.savefig('./assets/generated_images/graphs.png', pil_kwargs={'compress_level': 7})

logic/logic.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ def get_channels():
7474
6: "niedziela"
7575
}
7676

77+
days_of_week_capitalized = {
78+
0: "Poniedziałek",
79+
1: "Wtorek",
80+
2: "Środa",
81+
3: "Czwartek",
82+
4: "Piątek",
83+
5: "Sobota",
84+
6: "Niedziela"
85+
}
86+
7787
moon_phases = {
7888
"New": ":new_moon:",
7989
"WaningCrescent": ":waning_crescent_moon:",
@@ -135,6 +145,11 @@ def get_day_of_week():
135145
return days_of_week[day]
136146

137147

148+
def get_day_of_week_capitalized():
149+
day = datetime.today().weekday()
150+
return days_of_week_capitalized[day]
151+
152+
138153
def get_sunset_sunrise():
139154
data = download_forecast(type="daily")
140155
sunrise = data["DailyForecasts"][0]["Sun"]["Rise"][11:16]
@@ -163,7 +178,7 @@ async def get_birthday_message(our_server):
163178
for row in data:
164179
if row[0] == str(todays_date.day) and row[1] == str(todays_date.month):
165180
i = 2
166-
while row[i] != '':
181+
while i < len(row) and row[i] != '':
167182
ret += row[i] + ' - '
168183
ret += f'*{row[i + 1]}* - '
169184
ret += str(todays_date.year - int(row[i + 1])) + ' lat\n'
@@ -262,10 +277,28 @@ def get_hourly_forecast():
262277
def get_advanced_hourly_forecast():
263278
data = download_forecast(type='hourly')
264279

265-
result = {}
280+
# hours, temperature, temperature_feel, rainfall, humidity, wind_speed, wind_gust
266281

267-
for one_hour in data:
268-
pass
282+
forecast = {
283+
'hours': [],
284+
'temperature': [],
285+
'temperature_feel': [],
286+
'rainfall': [],
287+
'humidity': [],
288+
'wind_speed': [],
289+
'wind_gust': [],
290+
}
291+
292+
for sample in data:
293+
forecast['hours'].append(sample['DateTime'][11:13])
294+
forecast['temperature'].append(sample['Temperature']['Value'])
295+
forecast['temperature_feel'].append(sample['RealFeelTemperature']['Value'])
296+
forecast['rainfall'].append(sample['TotalLiquid']['Value'])
297+
forecast['humidity'].append(sample['RelativeHumidity'])
298+
forecast['wind_speed'].append(sample['Wind']['Speed']['Value'])
299+
forecast['wind_gust'].append(sample['WindGust']['Speed']['Value'])
300+
301+
return forecast
269302

270303

271304
def get_current_weather():

0 commit comments

Comments
 (0)