-
Notifications
You must be signed in to change notification settings - Fork 2
/
NEPcalendarCR.py
executable file
·95 lines (83 loc) · 2.58 KB
/
NEPcalendarCR.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
#!/usr/bin/python3
# NEPviewerCR (https://github.com/DE-cr/NEPviewerCR)
# (file NEPcalendarCR.py: build *.png calendars from NEPgetCR.pl's *.json data)
# - (partial) replacement for the official nepviewer.com offer to monitor a
# photovoltaic micro inverter registered with their site
# - usage: python3 NEPcalendarCR.py *.json
colorful = False # set to True for green-to-red color coded energy bars
import json
import re
from sys import argv
import matplotlib.pyplot as plt
from matplotlib.colors import hsv_to_rgb
month = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
]
def plot_month(ax, m, kwh):
# make each month 31 days wide, and fill in possible gaps:
for d in range(1, 32):
if not d in kwh:
kwh[d] = 0
total = round(sum(kwh.values()))
high = f"(max/d={max(kwh.values()):.1f})"
if colorful:
color = []
for d in kwh: # hue: max:0=red to min:1/3=green
color.append(hsv_to_rgb(((1 - kwh[d] / max_kwh_per_day) / 3, 1, 1)))
ax.bar(kwh.keys(), kwh.values(), color=color)
else:
ax.bar(kwh.keys(), kwh.values())
ax.set_ylim(0, max_kwh_per_day)
ax.set_title(f"{month[m-1]}: {total:.0f} kWh {high}", y=-0.11)
return total
def plot_calendar(sn, y, kwh):
cols, rows = 3, 4
fig, ax = plt.subplots(rows, cols, figsize=(10, 10), layout="constrained")
total = 0
for m in kwh:
total += plot_month(ax[(m - 1) // cols][(m - 1) % cols], m, kwh[m])
fig.suptitle(f"{y}: {total:.0f} kWh", fontweight="bold")
for a in ax.ravel():
a.set_axis_off()
fn = f"NEPviewerCR_{sn}_{y}.png"
print("Writing", fn, "...")
plt.savefig(fn)
# load data:
kwh = {}
max_kwh_per_day = 0
for fn in argv[1:]:
# try to find serial number in file name given:
sn = re.search("[0-9a-f]{8}", argv[1])
sn = sn.group() if sn else "unknown"
with open(fn, "r") as file:
d = json.load(file)["data"]
if not d:
continue
d = [x for x in d if x] # strip empty rows
f = d[-1].split() # day's total is in last row
y, m, d = [int(x) for x in f[0].split("-")]
k = float(f[8])
if not sn in kwh:
kwh[sn] = {}
if not y in kwh[sn]:
kwh[sn][y] = {}
if not m in kwh[sn][y]:
kwh[sn][y][m] = {}
kwh[sn][y][m][d] = k
if k > max_kwh_per_day:
max_kwh_per_day = k
# plot data:
for sn in kwh:
for y in kwh[sn]:
plot_calendar(sn, y, kwh[sn][y])