import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) import parse_log as P df_s, df_e = P.parse_log(str(P.ROOT / "21092025.txt")) df, ph = P.build_physical(df_s, df_e) # variante PNG para inspeccion visual (las figuras del articulo son PDF) import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt import matplotlib.dates as mdates import numpy as np import pandas as pd # noqa: E402 orig = P.ARTICULO / "figs" P.ARTICULO_FIGS = P.ARTICULO / "resultados_21092025" def save_png(fig, name): fig.savefig((P.ROOT / "articulo" / "resultados_21092025" / name)) plt.close(fig) # --- Fig 1 (PNG preview) --- t = df["timestamp"] lit_mask = (df["G_POA"] > 50.0).values cmds = df_e[df_e["kind"] == "cmd"] fig, ax1 = plt.subplots(figsize=(7.2, 3.4), dpi=110) ax1.plot(t, df["P_CPV"] * 1000, color="tab:red", lw=1.0, label="$P_{CPV}$") ax1.plot(t, df["P_ref"] * 1000, color="k", ls="--", lw=1.0, label="$P_{ref}$") ax1.fill_between(t, df["P_CPV"] * 1000, df["P_ref"] * 1000, where=lit_mask, color="tab:red", alpha=0.12) ax2 = ax1.twinx() ax2.plot(t, df["G_POA"], color="tab:orange", lw=0.8, alpha=0.85) for _, r in cmds.iterrows(): if int(r["cmd"]) != 2: continue # solo "Track solar" (igual que la figura del articulo) ax1.axvline(r["timestamp"], color="tab:blue", ls=":", lw=1.0, alpha=0.8) fig.tight_layout() save_png(fig, "preview_power.png") fig, axes = plt.subplots(2, 1, figsize=(7.2, 4.2), dpi=110, sharex=True) for ax, col in ((axes[0], "x"), (axes[1], "y")): ax.plot(t, df[col], color="tab:blue", lw=0.9) tc = df["tgt_x" if col == "x" else "tgt_y"] ax.step(t, tc, where="post", color="k", ls="--", lw=1.0) for _, r in cmds.iterrows(): ax.axvline(r["timestamp"], color="tab:blue", ls=":", lw=0.8, alpha=0.7) fig.tight_layout() save_png(fig, "preview_position.png") fig, ax1 = plt.subplots(figsize=(7.2, 3.2), dpi=110) ax1.plot(t, df["er"], color="tab:purple", lw=0.7, alpha=0.9) ax2 = ax1.twinx() ax2.semilogy(t, df["dC"].clip(lower=1), color="tab:red", lw=0.5, alpha=0.7) at = df_e[df_e["kind"] == "at_rec"] for _, r in at.iterrows(): ax1.axvline(r["timestamp"], color="k", ls=":", lw=1.0) fig.tight_layout() save_png(fig, "preview_finetune.png") rec = [r for _, r in cmds.iterrows() if int(r["cmd"]) in (2, 5)] fig, axes = plt.subplots(1, min(2, len(rec)), figsize=(7.2, 2.9), dpi=110) axes = np.atleast_1d(axes) for ax, r in zip(axes, rec[:2]): t0 = r["timestamp"] w = df[(df["timestamp"] >= t0 - pd.Timedelta(minutes=1)) & (df["timestamp"] <= t0 + pd.Timedelta(minutes=20))] ax.plot(w["timestamp"], w["P_CPV"] * 1000, color="tab:red", lw=1.0) ax.plot(w["timestamp"], w["P_ref"] * 1000, color="k", ls="--", lw=1.0) ax.plot(w["timestamp"], 0.95 * w["P_ref"] * 1000, color="tab:green", ls=":", lw=1.0) ax.axvline(t0, color="tab:blue", ls=":", lw=1.0) ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M:%S")) fig.tight_layout() save_png(fig, "preview_recovery.png") print("previews guardados en articulo/resultados_21092025")