93 lines
4.4 KiB
Python
93 lines
4.4 KiB
Python
"""x / y of the advanced exports as absolute positions in the image, on every station. Run by pump.sh after each pumping.
|
||
|
||
A sensor of a file is relative when the medians of its x and y are both under RELATIVE_PX (relative: up to ~40 px seen,
|
||
NADELHORN c*_mon, SUSTENHORN, LENZSPITZE; absolute: 429 px and more). Rewrites in place data/<STATION>/displacement/*.csv:
|
||
x = x_pattern + DX + x_csv, y = y_pattern + DY + y_csv, where (x_pattern, y_pattern) is the centre of the pattern in
|
||
data/<STATION>/*_patterns.json and (DX, DY) the offset between the frame of the JSON and the IRON images (OFFSETS);
|
||
an empty pattern_type is filled from the JSON.
|
||
A relative sensor without a position (no JSON, no offset, missing from the JSON) is dropped: its rows are removed.
|
||
A file with no relative sensor is left as it is (already converted). Pumping again restores the originals.
|
||
|
||
Usage: python3 extraction/absolute.py [DATA] (default: the repository's data/)
|
||
python3 extraction/absolute.py --check (a check of the conversion, without touching data/)
|
||
"""
|
||
import csv
|
||
import glob
|
||
import json
|
||
import os
|
||
import statistics
|
||
import sys
|
||
|
||
RELATIVE_PX = 100
|
||
# Measured on 2026-11-04 on LENZSPITZE: median (spot − pattern of the JSON) over 9 night images from 17.10 to 03.11, 25
|
||
# to 34 matched targets each, stable within 0.5 px. Includes the constant offset spot − (x, y) already seen on NORDEND
|
||
# (displacement-columns.md). A station with a JSON but no offset here: its relative sensors are dropped.
|
||
OFFSETS = {"LENZSPITZE": (330.5, 15.8)}
|
||
|
||
|
||
def relative_sensors(rows):
|
||
xy = {}
|
||
for r in rows:
|
||
if r["x"] and r["y"]:
|
||
xy.setdefault(r["sensor_name"], []).append((float(r["x"]), float(r["y"])))
|
||
return {s for s, v in xy.items()
|
||
if max(abs(statistics.median(p[0] for p in v)), abs(statistics.median(p[1] for p in v))) < RELATIVE_PX}
|
||
|
||
|
||
def convert(rows, patterns, offset):
|
||
"""-> (rows with absolute x / y, relative sensors dropped); None if nothing is relative."""
|
||
relative = relative_sensors(rows)
|
||
if not relative:
|
||
return None
|
||
out = []
|
||
for r in rows:
|
||
if r["sensor_name"] in relative:
|
||
m = patterns.get(r["sensor_name"].strip())
|
||
if m is None or offset is None:
|
||
continue
|
||
r["pattern_type"] = r["pattern_type"] or m.get("pattern_type", "")
|
||
if r["x"]:
|
||
r["x"] = repr(m["x"] + offset[0] + float(r["x"]))
|
||
if r["y"]:
|
||
r["y"] = repr(m["y"] + offset[1] + float(r["y"]))
|
||
out.append(r)
|
||
return out
|
||
|
||
|
||
def station(folder):
|
||
name = os.path.basename(folder)
|
||
patterns = {}
|
||
for path in glob.glob(os.path.join(folder, "*_patterns.json")):
|
||
patterns.update({str(v["sensor_name"]): v for v in json.load(open(path)).values()})
|
||
offset = OFFSETS.get(name) if patterns else None
|
||
for path in sorted(glob.glob(os.path.join(folder, "displacement", "*.csv"))):
|
||
with open(path, newline="") as f:
|
||
reader = csv.DictReader(f)
|
||
fields, rows = reader.fieldnames, list(reader)
|
||
if not fields or "x" not in fields:
|
||
continue # an empty day
|
||
out = convert(rows, patterns, offset)
|
||
if out is None:
|
||
continue
|
||
with open(path, "w", newline="") as f:
|
||
w = csv.DictWriter(f, fieldnames=fields)
|
||
w.writeheader()
|
||
w.writerows(out)
|
||
print(f"{name} {os.path.basename(path)}: {len(out)} rows kept, {len(rows) - len(out)} relative dropped")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
if sys.argv[1:] == ["--check"]:
|
||
def row(s, x, y, t=""):
|
||
return {"sensor_name": s, "x": x, "y": y, "pattern_type": t}
|
||
rows = [row("a", "1.5", "-2", ""), row("a", "", "", ""), row("b", "0.1", "0.2"), row("c", "800", "600", "ref")]
|
||
out = convert(rows, {"a": {"x": 100, "y": 200, "pattern_type": "target"}}, (10, 20))
|
||
assert out == [row("a", "111.5", "218.0", "target"), row("a", "", "", "target"), row("c", "800", "600", "ref")], out
|
||
assert convert(out, {}, None) is None # already absolute: left as it is
|
||
assert convert([row("b", "0.1", "0.2")], {"b": {"x": 1, "y": 1}}, None) == [] # no offset: dropped
|
||
print("ok")
|
||
sys.exit()
|
||
data = sys.argv[1] if len(sys.argv) > 1 else os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "data")
|
||
for folder in sorted(glob.glob(os.path.join(data, "*", ""))):
|
||
station(folder.rstrip(os.sep))
|