-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimplecast.py
executable file
·91 lines (71 loc) · 2.12 KB
/
simplecast.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
#!/bin/env python3
from jinja2 import Environment, FileSystemLoader
from wetterdienst import Wetterdienst
from wetterdienst.provider.dwd.mosmix import DwdMosmixType
from wetterdienst.util.cli import setup_logging
import locale
locale.setlocale(locale.LC_TIME, 'de_DE.utf8')
def print_stations():
API = Wetterdienst(provider="dwd", network="mosmix")
stations = API(parameter="small", mosmix_type=DwdMosmixType.SMALL)
print(stations.all().df.to_string())
def load_data(station_ids):
API = Wetterdienst(provider="dwd", network="mosmix")
stations = API(parameter="small", mosmix_type=DwdMosmixType.SMALL).filter_by_station_id(
station_id=station_ids)
return stations.values.all()
def ms_to_bft(ms):
if ms <= 0.2:
return 0
if ms <= 1.5:
return 1
if ms <= 3.3:
return 2
if ms <= 5.4:
return 3
if ms <= 7.9:
return 4
if ms <= 10.7:
return 5
if ms <= 13.8:
return 6
if ms <= 17.1:
return 7
if ms <= 20.7:
return 8
if ms <= 24.4:
return 9
if ms <= 28.4:
return 10
if ms <= 32.6:
return 11
return 12
def weather_iter(weather):
for row in weather.itertuples():
ts = row.Index[1].astimezone("Europe/Berlin")
print(ts)
yield (ts, row)
def render(weather=[]):
weather = weather.df.sort_values(by=["station_id", "date"]).pivot_table(
index=["station_id", "date"], columns="parameter", values="value")
weather.interpolate(inplace=True)
print(weather.head())
file_loader = FileSystemLoader("templates")
env = Environment(loader=file_loader)
env.filters["ms_to_bft"] = ms_to_bft
template = env.get_template("index.html.jinja")
output = template.render(weather=weather_iter(weather))
f = open("dist/index.html", "w")
f.write(output)
f.close()
def main(stations=""):
setup_logging()
if stations == "":
print_stations()
else:
station_ids = stations.split(",")
weather = load_data(station_ids)
render(weather)
if __name__ == '__main__':
import plac
plac.call(main)