"""Reproduce the ideal-gas hypersonic-inlet reference curve and CSV using Python's standard library."""
import csv
from pathlib import Path

ROOT = Path(__file__).resolve().parent
GAMMA = 1.4
R = 287.05
CP = GAMMA * R / (GAMMA - 1)
T_STATIC = 220.0

with (ROOT / "mach-total-temperature.csv").open("w", newline="", encoding="utf-8") as handle:
    writer = csv.writer(handle)
    writer.writerow(["mach_number", "freestream_static_temperature_K", "ideal_stagnation_temperature_K", "ideal_kinetic_energy_per_kg_J_kg"])
    for i in range(21):
        mach = i * 0.25
        speed = mach * (GAMMA * R * T_STATIC) ** 0.5
        t_total = T_STATIC + speed * speed / (2 * CP)
        writer.writerow([f"{mach:.2f}", f"{T_STATIC:.1f}", f"{t_total:.3f}", f"{speed * speed / 2:.3f}"])

# Site-style vector figure; this analytical curve is not experimental data.
width, height = 880, 500
left, right, top, bottom = 100, 835, 55, 405
def xy(mach, temp):
    return left + mach / 5 * (right-left), bottom - (temp-200) / 1200 * (bottom-top)
pts = [xy(i*0.25, T_STATIC * (1 + (GAMMA-1)/2 * (i*0.25)**2)) for i in range(21)]
path = " ".join(("M" if i == 0 else "L") + f"{x:.1f},{y:.1f}" for i,(x,y) in enumerate(pts))
grid = []
for temp in range(200, 1401, 200):
    y = xy(0,temp)[1]
    grid.append(f'<line x1="{left}" y1="{y:.1f}" x2="{right}" y2="{y:.1f}" stroke="#d6def0"/><text x="{left-14}" y="{y+5:.1f}" text-anchor="end" fill="#364766" font-size="16">{temp}</text>')
for mach in range(6):
    x = xy(mach,200)[0]
    grid.append(f'<line x1="{x:.1f}" y1="{top}" x2="{x:.1f}" y2="{bottom}" stroke="#e6eaf3"/><text x="{x:.1f}" y="{bottom+28}" text-anchor="middle" fill="#364766" font-size="16">{mach}</text>')
svg = f'''<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {width} {height}" role="img" aria-labelledby="title desc">
<title id="title">Ideal stagnation temperature rises with Mach number</title><desc id="desc">Analytical constant-specific-heat air model at a selected static temperature of 220 kelvin. Stagnation temperature rises quadratically from 220 kelvin at Mach zero to about 1320 kelvin at Mach five.</desc>
<rect width="100%" height="100%" fill="#f7f9ff"/>{''.join(grid)}
<line x1="{left}" y1="{bottom}" x2="{right}" y2="{bottom}" stroke="#172646" stroke-width="2"/><line x1="{left}" y1="{top}" x2="{left}" y2="{bottom}" stroke="#172646" stroke-width="2"/>
<path d="{path}" fill="none" stroke="#ae35d5" stroke-width="5" stroke-linecap="round"/>
<circle cx="{pts[-1][0]:.1f}" cy="{pts[-1][1]:.1f}" r="7" fill="#f2098c"/>
<text x="{(left+right)/2}" y="{height-18}" text-anchor="middle" fill="#172646" font-size="18">Freestream Mach number</text>
<text transform="translate(28 {(top+bottom)/2}) rotate(-90)" text-anchor="middle" fill="#172646" font-size="18">Ideal total temperature (K)</text>
<text x="{pts[-1][0]-10:.1f}" y="{pts[-1][1]-16:.1f}" text-anchor="end" fill="#4d176f" font-size="16">Mach 5 · 1320 K</text>
<text x="{left+16}" y="{top+24}" fill="#364766" font-size="15">Analytical ideal-gas curve · T∞ = 220 K · γ = 1.4</text></svg>'''
(ROOT / "mach-total-temperature.svg").write_text(svg, encoding="utf-8")
print(f"Wrote 21 analytical rows and SVG under {ROOT}")
