69 lines
2.5 KiB
Python
69 lines
2.5 KiB
Python
"""The Points page: the point labels of labeling/points/, one boolean per measurement (valid or not) on a range, set by
|
|||
|
|
the user. Their format, their files and their check are labeling/scripts/points.py; the measurements are read by
|
||
|
|
tools/scripts/data.py, as everywhere. A station of data/ only (a run of data_diff/ refused).
|
||
|
|
"""
|
||
|
|
import threading
|
||
|
|
|
||
|
|
import station
|
||
|
|
from station import Refusal, data
|
||
|
|
|
||
|
|
import points # noqa: E402 (labeling/scripts, on the path through station)
|
||
|
|
|
||
|
|
|
||
|
|
def refused(f):
|
||
|
|
def wrapped(*a):
|
||
|
|
try:
|
||
|
|
return f(*a)
|
||
|
|
except (ValueError, FileNotFoundError) as e:
|
||
|
|
raise Refusal(str(e)) from e
|
||
|
|
return wrapped
|
||
|
|
|
||
|
|
|
||
|
|
def summary(n):
|
||
|
|
p = points.read(n)
|
||
|
|
s, start, end = points.parts(n)
|
||
|
|
return {"name": n, "station": s, "start": start, "end": end, "points": len(p), "invalid": sum(not v for v in p.values())}
|
||
|
|
|
||
|
|
|
||
|
|
@refused
|
||
|
|
def listing(st=""):
|
||
|
|
return [summary(n) for n in points.datasets(st or None)]
|
||
|
|
|
||
|
|
|
||
|
|
@refused
|
||
|
|
def create(body):
|
||
|
|
st, start, end = body.get("station", ""), body.get("start", ""), body.get("end", "")
|
||
|
|
station.pumped(st)
|
||
|
|
n = points.name(st, start, end)
|
||
|
|
a, b = station.bounds(start, end)
|
||
|
|
points.create(n, {s: [m["t"] for m in ms if a <= m["t"] < b and m["corr_x"] is not None and m["corr_y"] is not None]
|
||
|
|
for s, ms in data.load(station.folder(st)).items()})
|
||
|
|
return summary(n)
|
||
|
|
|
||
|
|
|
||
|
|
@refused
|
||
|
|
def dataset(n):
|
||
|
|
"""-> {name, station, start, end, sensors: {sensor: {t, x, y, valid}}}: corr_x, corr_y (px, the curves labelled) of
|
||
|
|
each point, None where the data no longer have it (a pumping again)."""
|
||
|
|
p, st = points.read(n), points.parts(n)[0]
|
||
|
|
now = {(s, points.time(m["t"])): m for s, ms in data.load(station.folder(st)).items() for m in ms}
|
||
|
|
out = {}
|
||
|
|
for (s, t), v in sorted(p.items()):
|
||
|
|
m, o = now.get((s, t), {}), out.setdefault(s, {"t": [], "x": [], "y": [], "valid": []})
|
||
|
|
o["t"].append(t[:19].replace("T", " ")) # as Plotly shows it, UTC
|
||
|
|
o["x"].append(m.get("corr_x"))
|
||
|
|
o["y"].append(m.get("corr_y"))
|
||
|
|
o["valid"].append(v)
|
||
|
|
return {**summary(n), "sensors": out}
|
||
|
|
|
||
|
|
|
||
|
|
LOCK = threading.Lock() # the requests run in threads: two marks never read and write the file at the same time
|
||
|
|
|
||
|
|
|
||
|
|
@refused
|
||
|
|
def mark(n, body):
|
||
|
|
"""{sensor, times (as the page shows them), valid} -> the summary."""
|
||
|
|
times = [t.replace(" ", "T") + "Z" for t in body.get("times", [])]
|
||
|
|
with LOCK:
|
||
|
|
points.mark(n, body.get("sensor", ""), times, bool(body.get("valid")))
|
||
|
|
return summary(n)
|