"""Lecture 17, the best speeds of a propeller aircraft against those of a jet, in Hull's units u = V/V*
(flight.performance.drag_ratio): fuel flow follows thrust for a jet and power for a propeller, so the jet's range goes
as V/D and endurance as 1/D, the propeller's range as 1/D and endurance as 1/(DV). Each curve is scaled to its peak.
Writes img/fig-17-prop-jet.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, INK, GREY
from flight import performance as PF

U = np.linspace(0.4, 2.4, 400)
CURVES = {"jet, range $\\propto V/D$": (U / PF.drag_ratio(U), BLUE, "-"),
          "jet, endurance $\\propto 1/D$": (1 / PF.drag_ratio(U), BLUE, "--"),
          "propeller, range $\\propto 1/D$": (1 / PF.drag_ratio(U), MAROON, "-"),
          "propeller, endurance $\\propto 1/(DV)$": (1 / (PF.drag_ratio(U) * U), MAROON, "--")}
PEAKS = {k: U[np.argmax(v[0])] for k, v in CURVES.items()}

if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(11, 4.8), sharey=True)
    fig.subplots_adjust(wspace=0.08)
    for k, (y, c, ls) in CURVES.items():
        a = a1 if k.startswith("jet") else a2
        a.plot(U, y / y.max(), color=c, lw=2.5, ls=ls, label=k)
        a.plot(PEAKS[k], 1, "o", color=c, ms=7)
        a.text(PEAKS[k] + (0.12 if "range" in k else -0.12), 1.03, f"$u={PEAKS[k]:.2f}$", color=c, ha="center", fontsize=12)
    for a, t in ((a1, "jet"), (a2, "propeller")):
        a.set_xlabel("$u=V/V^*$"); a.set_ylim(0, 1.12); a.set_title(t, fontsize=13, loc="left")
        a.legend(frameon=False, fontsize=11, loc="lower center")
        despine(a)
    a1.set_ylabel("relative to the best")
    save(fig, "fig-17-prop-jet")
