"""Lecture 14, specific impulse of rockets and air-breathing engines (fuel only), on a logarithmic scale.
Rockets (vacuum): S139 269 s and Vikas 293 s (dataset, Wikipedia via data/vehicles/pslv-ca.toml), PS4 308 s
(dataset, Spaceflight101), CE-20 442 s (ISRO, CryogenicEngine page). Air-breathers: Isp = 1/c from the thrust-specific
fuel consumption c (flight.propulsion.isp_from_tsfc): Hull's GE CJ610 turbojet, c = 1.18 per hour (Hull 2007,
Table 3.7), and Sadraey's typical ranges (2017, Table 4.7): turbojet 0.8-0.9, low-bypass turbofan 0.7-0.8, high-bypass
turbofan 0.4-0.5 per hour. Writes img/fig-14-isp.png."""
import numpy as np
from _visual import plt, save, MAROON, BLUE, GREEN, AMBER, INK, GREY
from flight import propulsion as PR, vehicle

PSLV = vehicle.load("pslv-ca")
ROCKETS = [("S139 (PSLV first stage, solid)", PSLV.stages[0].engine.isp_vac), ("Vikas (PSLV second stage)", PSLV.stages[1].engine.isp_vac),
           ("PS4 (PSLV fourth stage)", PSLV.stages[3].engine.isp_vac), ("CE-20 (LVM3 cryogenic stage)", 442.0)]
AIR = [("turbojet (Sadraey)", 0.9, 0.8), ("low-bypass turbofan (Sadraey)", 0.8, 0.7), ("high-bypass turbofan (Sadraey)", 0.5, 0.4)]
CJ610 = PR.isp_from_tsfc(1.18 / 3600.0)

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10.5, 5.6))
    rows = []
    for lab, isp in ROCKETS:
        rows.append((lab, isp, isp, MAROON))
    rows.append(("GE CJ610 turbojet (Hull)", CJ610, CJ610, BLUE))
    for lab, c_hi, c_lo in AIR:
        rows.append((lab, PR.isp_from_tsfc(c_hi / 3600), PR.isp_from_tsfc(c_lo / 3600), BLUE))
    for i, (lab, lo, hi, c) in enumerate(rows):
        if hi > lo * 1.01:
            ax.plot([lo, hi], [i, i], color=c, lw=9, solid_capstyle="butt")
            ax.text(hi * 1.08, i, f"{lo:.0f}–{hi:.0f} s", va="center", fontsize=12, color=c)
        else:
            ax.plot(lo, i, "o", color=c, ms=10)
            ax.text(lo * 1.08, i, f"{lo:.0f} s", va="center", fontsize=12, color=c)
    ax.set_yticks(range(len(rows))); ax.set_yticklabels([r[0] for r in rows], fontsize=12)
    ax.invert_yaxis(); ax.set_xscale("log"); ax.set_xlim(150, 20000)
    ax.set_xlabel("specific impulse (s), propellant or fuel carried")
    ax.axhline(3.5, color=GREY, lw=0.8, ls=":")
    ax.text(1200, 1.5, "rockets: carry their oxidiser", color=MAROON, fontsize=12, va="center")
    ax.text(170, 5.5, "air-breathers: take\noxygen from the air", color=BLUE, fontsize=12, va="center")
    for s in ("top", "right"):
        ax.spines[s].set_visible(False)
    save(fig, "fig-14-isp")
