import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# --- Dati ---
df = pd.read_csv("data.csv")
df["time_s"] = df["time since start [ms]"] / 1000.0
df_plot = df[df["time_s"] >= 114]

# --- Parametri fit ---
T_INF  = 22.99
t_full = np.linspace(114, df["time_s"].max(), 1000)

# 1° tratto: t0=115.0, A=194.51, tau=13.17
T_curve_1 = T_INF + 194.51 * np.exp(-(t_full - 115.0) / 13.17)

# 2° tratto: t0=117.5, A=154.94, tau=17.12
T_curve_2 = T_INF + 154.94 * np.exp(-(t_full - 117.5) / 17.12)

# --- Plot ---
fig, ax = plt.subplots(figsize=(12, 5))

ax.plot(df_plot["time_s"], df_plot["temp_obj IR [C]"],
        color="steelblue", linewidth=0.8, label="Dati raw (temp_obj)")
ax.plot(t_full, T_curve_1,
        color="tomato", linewidth=2, linestyle="--",
        label=r"Fit 1° tratto: $22.99 + 194.51\cdot e^{-(t-115)/13.17}$")
ax.plot(t_full, T_curve_2,
        color="seagreen", linewidth=2, linestyle="--",
        label=r"Fit 2° tratto: $22.99 + 154.94\cdot e^{-(t-117.5)/17.12}$")
ax.axvspan(115.0, 115.9, color="tomato", alpha=0.10, label="Finestra fit 1° tratto")
ax.axvspan(117.5, df["time_s"].max(), color="seagreen", alpha=0.07, label="Finestra fit 2° tratto")

ax.set_xlabel("Tempo [s]")
ax.set_ylabel("Temperatura [°C]")
ax.set_title("Confronto curve di fit sui dati raw")
ax.legend(fontsize=8)
ax.grid(True, alpha=0.3)

plt.tight_layout()
plt.savefig("plot_confronto_fit.png", dpi=150, bbox_inches="tight")
plt.show()
