"""PNG plot of the series of an "advanced" export, for the diagnostic agent to look at. Usage: .venv/bin/python tools/scripts/plot.py data/PS_DES_COUDRES/ [--sensors SENSOR_A,SENSOR_B] [--columns error_value,corr_x,corr_y] [--start ...] [--end ...] [--zone START/END ...] [--out FOLDER] One panel per column, one curve per sensor, UTC times; each --zone is greyed. Columns: error_value, corr_x, corr_y, relative_x, relative_y, x, y, sigma0, reproj_err, and the six image quality metrics (peak to snr, raw values; against a calm reference: quality.py). Prints the path of the PNG (out/diagnostic/, or --out: the report's folder, for an evidence figure). """ import argparse import os import sys import matplotlib matplotlib.use("Agg") import matplotlib.dates as mdates # noqa: E402 import matplotlib.pyplot as plt # noqa: E402 from common import COLUMNS, QUALITY, bound, check, sensors # noqa: E402 from data import filter_window, parse_time # noqa: E402 def zone(text): """'2026-10-13T01:00Z/2026-10-13T03:00Z' -> (start, end).""" start, sep, end = text.partition("/") if not sep: raise argparse.ArgumentTypeError(f"invalid zone: {text!r} (e.g. 2026-10-13T01:00Z/2026-10-13T03:00Z)") return parse_time(start), parse_time(end) def main(): a = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) a.add_argument("folder") a.add_argument("--sensors", help="comma-separated list (default: all)") a.add_argument("--columns", default="error_value,corr_x,corr_y") a.add_argument("--start", type=parse_time) a.add_argument("--end", type=parse_time) a.add_argument("--zone", type=zone, action="append", default=[], help="window to grey, START/END (repeatable)") a.add_argument("--out", default=os.path.join("out", "diagnostic"), help="folder of the PNG") o = a.parse_args() by, names = sensors(o.folder, o.sensors) columns = o.columns.split(",") check("sensors", names, by) check("columns", columns, COLUMNS + QUALITY) series = {s: filter_window(by[s], o.start, o.end) for s in names} if not any(series.values()): sys.exit("No measurement in the requested window.") empty = [c for c in columns if all(m[c] is None for s in names for m in series[s])] if len(empty) == len(columns): sys.exit(f"Nothing to plot: {', '.join(columns)} empty for these sensors in the window.") if empty: print(f"Empty panels (no value for these sensors): {', '.join(empty)}") fig, axes = plt.subplots(len(columns), 1, sharex=True, figsize=(14, 3.2 * len(columns)), squeeze=False) tab20 = plt.get_cmap("tab20").colors colors = tab20[::2] + tab20[1::2] # dark shades first: two neighbouring sensors do not share a hue for ax, col in zip(axes[:, 0], columns): for i, s in enumerate(names): ms = series[s] # an empty value becomes NaN: the curve breaks instead of bridging the gap ax.plot([m["t"] for m in ms], [float("nan") if m[col] is None else m[col] for m in ms], ".-", ms=2, lw=0.7, color=colors[i % len(colors)], label=s) for start, end in o.zone: ax.axvspan(start, end, color="0.85", zorder=0) ax.set_ylabel(col) ax.grid(alpha=0.3) axes[0, 0].legend(loc="upper left", bbox_to_anchor=(1.0, 1.0), fontsize=8) axes[-1, 0].xaxis.set_major_formatter(mdates.DateFormatter("%d/%m\n%H:%MZ")) station = os.path.basename(os.path.normpath(o.folder)) fig.suptitle(f"{station}: {', '.join(columns)} (UTC times)") os.makedirs(o.out, exist_ok=True) who = "-".join(names) if o.sensors else "all" path = os.path.join(o.out, f"{station}_{'-'.join(columns)}_{who}_" f"{bound(o.start, 'start')}_{bound(o.end, 'end')}.png") fig.savefig(path, dpi=100, bbox_inches="tight") print(path) if __name__ == "__main__": main()