cat > temp_vs_time.py <<'PY'
#!/usr/bin/env python3
import re
from datetime import datetime

INFILE  = "VOLTAGE.txt"
OUTTXT  = "TEMP-TIME.txt"
OUTPNG  = "TEMP-TIME.png"

LINE_RE = re.compile(r"""
    ,
    (?P<iso>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[+-]\d{2}:\d{2})?)
    ,
    .*?temp=(?P<temp>[0-9.]+)
""", re.VERBOSE)

times = []
temps = []

with open(INFILE, "r", encoding="utf-8", errors="ignore") as f:
    for line in f:
        m = LINE_RE.search(line)
        if not m:
            continue
        try:
            t = datetime.fromisoformat(m.group("iso"))
            temp = float(m.group("temp"))
        except Exception:
            continue
        times.append(t)
        temps.append(temp)

# Write cleaned text file
with open(OUTTXT, "w") as out:
    out.write("# iso_time,temp_C\n")
    for t, temp in zip(times, temps):
        out.write(f"{t.isoformat()},{temp:.2f}\n")

# Plot
try:
    import matplotlib.pyplot as plt
except Exception as e:
    print("Text file written, but matplotlib missing:", e)
    exit(0)

plt.figure(figsize=(10,4))
plt.plot(times, temps)
plt.title("Temperature vs Time")
plt.xlabel("Time")
plt.ylabel("Temperature (°C)")
plt.grid(True)
plt.tight_layout()
plt.savefig(OUTPNG, dpi=160)

print(f"Wrote {OUTTXT} and {OUTPNG}")
PY

chmod +x temp_vs_time.py
./temp_vs_time.py
ls -l TEMP-TIME.*
