Add CSV loading per station for energy analysis
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
55ec0499bb
commit
422904757f
2 files changed
+116
No files matched your search
@@ -0,0 +1,48 @@
|
||||
"""Analyse énergétique MA7D_DROP : tendance de BusVoltage en moyenne glissante.
|
||||
|
||||
Spec : docs/superpowers/specs/2026-10-09-energy-ma7d-design.md
|
||||
Bibliothèque standard uniquement (cible : système embarqué de la station).
|
||||
"""
|
||||
import csv
|
||||
import glob
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from datetime import datetime
|
||||
|
||||
NAME_RE = re.compile(r"^(?P<station>[^_]+)_(?P<metric>.+?)_\d{4}_\d{2}_\d{2}_\d{2}_\d{2} to .+\.csv$")
|
||||
|
||||
|
||||
def parse_name(path):
|
||||
"""'<STATION>_<Metric>_<début> to <fin>.csv' -> (station, metric), ou None."""
|
||||
m = NAME_RE.match(os.path.basename(path))
|
||||
return (m["station"], m["metric"]) if m else None
|
||||
|
||||
|
||||
def load_csv(path):
|
||||
"""CSV ',time_utc,metric' -> {datetime UTC: float ou None (pas de mesure)}."""
|
||||
with open(path, newline="") as f:
|
||||
rows = csv.reader(f)
|
||||
next(rows)
|
||||
return {datetime.fromisoformat(r[1]): float(r[2]) if r[2] else None for r in rows}
|
||||
|
||||
|
||||
def load_stations(paths):
|
||||
"""Fichiers et/ou dossiers -> {station: {metric: série}}."""
|
||||
files = []
|
||||
for p in paths:
|
||||
files += sorted(glob.glob(os.path.join(p, "*.csv"))) if os.path.isdir(p) else [p]
|
||||
stations, seen = {}, {}
|
||||
for path in files:
|
||||
real = os.path.realpath(path)
|
||||
if real in seen.values():
|
||||
continue
|
||||
key = parse_name(path)
|
||||
if key is None:
|
||||
print(f"ignoré (nom non reconnu) : {path}", file=sys.stderr)
|
||||
continue
|
||||
if key in seen:
|
||||
raise ValueError(f"doublon {key[0]}/{key[1]} : {seen[key]} et {path}")
|
||||
seen[key] = real
|
||||
stations.setdefault(key[0], {})[key[1]] = load_csv(path)
|
||||
return stations
|
||||
Reference in new issue
Block a user