"""Lecture 12, typical values. Left: the zero-lift drag coefficient by aircraft type (Sadraey 2017, Table 3.2).
Right: the maximum lift-to-drag ratio by type (Sadraey Table 5.1), with the four polars of fig-12-polar.py.
Writes img/fig-12-typical.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, INK, GREY
from _scripts import load

CD0_TABLE = [("glider", 0.012, 0.015), ("subsonic jet", 0.014, 0.020), ("large turboprop", 0.018, 0.024),
             ("small GA, fixed gear", 0.025, 0.040), ("supersonic jet", 0.020, 0.040), ("agricultural", 0.060, 0.065)]
LD_TABLE = [("sailplane", 30, 40), ("jet transport", 15, 20), ("light GA", 10, 15), ("subsonic fighter", 7, 10),
            ("supersonic fighter", 4, 7), ("helicopter", 3, 5)]
P = load("fig-12-polar")
DOTS = {"light GA": "HANSA-3 class (estimated)", "jet transport": "Sadraey's jet transport", "sailplane": "glider (representative)"}

if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(11, 4.8))
    fig.subplots_adjust(wspace=0.75)
    for i, (lab, lo, hi) in enumerate(CD0_TABLE):
        a1.plot([lo, hi], [i, i], color=BLUE, lw=8, solid_capstyle="butt")
    a1.set_yticks(range(len(CD0_TABLE))); a1.set_yticklabels([r[0] for r in CD0_TABLE])
    a1.set_xlabel("$C_{D_0}$"); a1.set_xlim(0, 0.07); a1.invert_yaxis()
    a1.set_title("zero-lift drag (Sadraey)", fontsize=13, loc="left")
    for i, (lab, lo, hi) in enumerate(LD_TABLE):
        a2.plot([lo, hi], [i, i], color=GREEN, lw=8, solid_capstyle="butt")
        if lab in DOTS:
            e = P.EMAX[DOTS[lab]][1]
            a2.plot(e, i, "o", color=MAROON, ms=8, zorder=5)
            a2.text(e - 1.0, i - 0.3, f"{e:.1f}", color=MAROON, fontsize=12, ha="right")
    a2.set_yticks(range(len(LD_TABLE))); a2.set_yticklabels([r[0] for r in LD_TABLE])
    a2.set_xlabel("$(L/D)_{max}$"); a2.set_xlim(0, 45); a2.invert_yaxis()
    a2.set_title("best lift-to-drag (Sadraey)", fontsize=13, loc="left")
    despine(a1); despine(a2)
    save(fig, "fig-12-typical")
