2026-07-10 10:38:57 +02:00

51 lines
1.3 KiB
Python

import base64
import glob
import json
import matplotlib.pyplot as plt
import os
import sys
import zlib
sys.path.append("../../../../..")
from ScEpTIC.emulator.energy import energy_utils
try:
os.mkdir("graphs/")
except FileExistsError:
pass
for name in glob.glob("*.json"):
with open(name, 'r') as fp:
data = json.load(fp)
load_resistance = 30000 if data['load_resistance'] is None else energy_utils.str_to_float(data['load_resistance'])
if data['measures_compressed']:
vals = json.loads(zlib.decompress(base64.b64decode(data['measures'])).decode('utf-8'))
else:
vals = data['measures']
mult = energy_utils.str_to_float(data['measures_multiplier'] if data['measures_multiplier'] else '1')
if mult != 1:
vals = [x*mult for x in vals]
sampling_time = energy_utils.str_to_float(data['sampling_time'])
t = [x*sampling_time for x in range(len(vals))]
if data['measures_type'] == 'power':
vals = [energy_utils.P_R_to_V(x, load_resistance) for x in vals]
name = name.replace(".json", "")
plt.plot(t, vals)
plt.xlabel("Time (s)")
plt.ylabel("Voltage (V)")
plt.title(f"Source: {name}")
plt.grid(True)
path = f"graphs/{name}.pdf"
print(f"Saving {path}")
plt.savefig(path, dpi=300, bbox_inches="tight")
plt.clf()
plt.close()