forked from akleber/fronius-json-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogdata-data2csv.py
124 lines (76 loc) · 3.17 KB
/
logdata-data2csv.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
"""
Work-in-progress!!!
Reads the logdata-data json files generated by the Fronius Push Service.
Extracts some data and writes it to a excel compatible csv file.
Also generates a graph from some data.
"""
import json
import csv
import collections
import matplotlib.pyplot as plt
from pprint import pprint
def to_time(seconds):
seconds = int(seconds) + 7200
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
time = '{:02d}:{:02d}'.format(h, m)
#print(time)
return time
with open('examples/logdata-data20160913235000.json') as data_file:
data = json.load(data_file)
#pprint(data)
#print(data['Body']['inverter/1']['Data']['Current_DC_String_1']['Values'])
c_dc_1_values = data['Body']['inverter/1']['Data']['Current_DC_String_1']['Values']
# Dictionaraies, returned by the json reader, have no order.
# So, convert it to a list of key/value tupels (.items()) and
# sort it with sorted(), using a lambda function to convert the key/first tupel item to an integer
# so that the sorting is based on numbers not text
c_dc_1_values_ordered = sorted(c_dc_1_values.items(), key=lambda x: int(x[0]) )
# no header text for the time column so that excel treats it as x axis values
fieldnames = ['', 'Current_DC_String_1']
with open('examples/c_dc_1.csv', 'w') as csv_file:
writer = csv.writer(csv_file, dialect='excel-tab')
writer.writerow(fieldnames)
for key, value in c_dc_1_values_ordered:
writer.writerow([key, value])
wac_plus_abs = data['Body']['meter:16220118']['Data']['EnergyReal_WAC_Plus_Absolute']['Values']
wac_plus_abs_ordered = sorted(wac_plus_abs.items(), key=lambda x: int(x[0]) )
wac_plus_diff = []
previous_value = int(wac_plus_abs_ordered[0][1])
for key, value in wac_plus_abs_ordered:
diff = int(value) - previous_value
wac_plus_diff.append( (to_time(key), float(diff)/1000) )
previous_value = int(value)
fieldnames = ['', 'EnergyReal_WAC_Plus_Absolute']
with open('examples/wac_plus_diff.csv', 'w') as csv_file:
writer = csv.writer(csv_file, dialect='excel-tab')
writer.writerow(fieldnames)
for key, value in wac_plus_diff:
writer.writerow([key, value])
wac_minus_abs = data['Body']['meter:16220118']['Data']['EnergyReal_WAC_Minus_Absolute']['Values']
wac_minus_abs_ordered = sorted(wac_minus_abs.items(), key=lambda x: int(x[0]) )
wac_minus_diff = []
previous_value = int(wac_minus_abs_ordered[0][1])
for key, value in wac_minus_abs_ordered:
diff = int(value) - previous_value
wac_minus_diff.append( (to_time(key), float(diff)/1000) )
previous_value = int(value)
fieldnames = ['', 'EnergyReal_WAC_Minus_Absolute']
with open('examples/wac_minus_diff.csv', 'w') as csv_file:
writer = csv.writer(csv_file, dialect='excel-tab')
writer.writerow(fieldnames)
for key, value in wac_minus_diff:
writer.writerow([key, value])
plus_keys = []
plus_values = []
previous_value = int(wac_plus_abs_ordered[0][1])
for key, value in wac_plus_abs_ordered:
diff = int(value) - previous_value
previous_value = int(value)
plus_keys.append( int(key) )
plus_values.append( float(diff) )
print(plus_keys)
print(plus_values)
plt.plot(plus_keys, plus_values)
#plt.show()
plt.savefig('examples/plot.png', bbox_inches='tight')