61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
import time
|
|
import datetime as dt
|
|
import requests
|
|
from zoneinfo import ZoneInfo
|
|
from matplotlib import pyplot as plt
|
|
import pandas as pd
|
|
|
|
TZ = "Europe/Berlin"
|
|
DAYS = 2
|
|
|
|
OPEN_METEO_URL = "https://api.open-meteo.com/v1/forecast"
|
|
|
|
class WeatherForecaster:
|
|
def __init__(self, latitude, longitude):
|
|
self.lat = latitude
|
|
self.lon = longitude
|
|
|
|
def get_hourly_forecast(self, start_hour, days):
|
|
start_hour_local = start_hour
|
|
end_hour_local = start_hour_local + dt.timedelta(days=days)
|
|
|
|
params = {
|
|
"latitude": self.lat,
|
|
"longitude": self.lon,
|
|
"hourly": ["temperature_2m", "shortwave_radiation", "wind_speed_10m"],
|
|
"timezone": TZ,
|
|
"start_hour": start_hour_local.strftime("%Y-%m-%dT%H:%M"),
|
|
"end_hour": end_hour_local.strftime("%Y-%m-%dT%H:%M")
|
|
}
|
|
|
|
h = requests.get(OPEN_METEO_URL, params=params).json()["hourly"]
|
|
|
|
time_stamps = h["time"]
|
|
time_stamps = [
|
|
dt.datetime.fromisoformat(t).replace(tzinfo=ZoneInfo(TZ))
|
|
for t in time_stamps
|
|
]
|
|
|
|
weather = pd.DataFrame(index=time_stamps)
|
|
weather["ghi"] = h["shortwave_radiation"]
|
|
weather["temp_air"] = h["temperature_2m"]
|
|
weather["wind_speed"] = h["wind_speed_10m"]
|
|
|
|
return weather
|
|
|
|
|
|
|
|
if __name__=='__main__':
|
|
|
|
weather_forecast = WeatherForecaster(latitude=48.041, longitude=7.862)
|
|
while True:
|
|
now = dt.datetime.now()
|
|
secs = 60 - now.second #(60 - now.minute) * 60 - now.second # Sekunden bis volle Stunde
|
|
time.sleep(secs)
|
|
|
|
now_local = dt.datetime.now()
|
|
start_hour_local = (now_local + dt.timedelta(hours=1)).replace(minute=0, second=0, microsecond=0)
|
|
time_stamps, temps, ghi, wind_speed = weather_forecast.get_hourly_forecast(start_hour_local, DAYS)
|
|
plt.plot(time_stamps, temps)
|
|
plt.show() |