"""Reproduce an approximate digitized redraw of NASA Figure 19, T093.

The points were read from the published raster plot, not from raw telemetry.
Linear connectors are visual guides only. See methods.json for limits.
Requires Python 3, Matplotlib, and the adjacent CSV.
"""
from csv import DictReader
from pathlib import Path

import matplotlib.pyplot as plt

HERE = Path(__file__).resolve().parent
with (HERE / "x43a-t093-figure19-digitized.csv").open(
    newline="", encoding="utf-8"
) as stream:
    rows = list(DictReader(stream))

def series(column):
    pairs = [
        (int(row["time_from_drop_s"]), float(row[column]))
        for row in rows
        if row[column].strip()
    ]
    return [p[0] for p in pairs], [p[1] for p in pairs]

plt.rcParams.update({
    "font.family": "DejaVu Sans",
    "font.size": 12,
    "axes.labelsize": 13,
    "axes.edgecolor": "#26364b",
    "axes.linewidth": 1.0,
    "xtick.color": "#26364b",
    "ytick.color": "#26364b",
    "text.color": "#172a46",
    "svg.fonttype": "none",
})
fig, ax = plt.subplots(figsize=(11.6, 6.5), constrained_layout=True)
fig.patch.set_facecolor("white")
ax.set_facecolor("white")

x, y = series("flight_T093_F")
ax.plot(x, y, color="#172a46", linewidth=3.0, marker="o", markersize=4.5,
        label="Flight thermocouple T093", zorder=4)
x, y = series("nominal_prediction_F")
ax.plot(x, y, color="#2367a6", linewidth=2.7, linestyle="--", marker="s",
        markersize=3.4, label="Nominal prediction", zorder=3)
x, y = series("plus35_heat_flux_case_F")
ax.plot(x, y, color="#bb3f34", linewidth=2.7, linestyle="-.", marker="^",
        markersize=3.6, label="Prediction with +35% heat-flux factor", zorder=2)

# NASA's text identifies the approximate point where the flight record rises
# above the +35% heat-flux design case; it is not calculated by interpolation.
ax.axvline(83, color="#8b5a17", linestyle=":", linewidth=1.8, zorder=1)
ax.text(85.0, 1860, "NASA report: above +35% case\nat about 83 s",
        color="#704510", fontsize=10, va="top", ha="left")

ax.set_xlim(0, 140)
ax.set_ylim(-50, 1950)
ax.set_xticks(range(0, 141, 20))
ax.set_yticks(range(-50, 1951, 200))
ax.set_xlabel("Time from booster drop (s)")
ax.set_ylabel("Temperature (°F)")
ax.grid(True, color="#d8dee7", linewidth=0.8)
ax.set_axisbelow(True)
ax.legend(loc="upper left", frameon=True, facecolor="white",
          edgecolor="#cbd4df", framealpha=0.97, fontsize=10)
for side in ("top", "right"):
    ax.spines[side].set_visible(False)

fig.savefig(HERE / "x43a-t093-figure19-digitized.svg", facecolor="white")
fig.savefig(HERE / "x43a-t093-figure19-digitized.png", dpi=180, facecolor="white")

# A taller, larger-type companion keeps the axis and legend readable on phones.
fig.set_size_inches(7.2, 7.1)
ax.tick_params(axis="both", labelsize=11)
ax.xaxis.label.set_size(13)
ax.yaxis.label.set_size(13)
ax.legend(loc="upper center", bbox_to_anchor=(0.5, -0.13), ncol=1,
          frameon=True, facecolor="white", edgecolor="#cbd4df",
          framealpha=0.97, fontsize=10)
ax.texts[-1].set_fontsize(10)
fig.savefig(HERE / "x43a-t093-figure19-digitized-mobile.svg", facecolor="white")
fig.savefig(HERE / "x43a-t093-figure19-digitized-mobile.png", dpi=180, facecolor="white")
plt.close(fig)
