"""Lecture 24, the standard-rate turn of instrument flying (3 deg/s, 360 deg in 2 minutes; Wikipedia "Standard rate
turn", read 2026-11-28): the bank it needs against true airspeed, from the engine's level-turn relation, against the
pilots' rule of thumb TAS (kt)/10 + 7, and ICAO's holding rule "25 deg or 3 deg/s, whichever needs the lesser bank".
Writes img/fig-24-standard-rate.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREY, INK
from flight import performance as PF
from flight.constants import KT

RATE = np.radians(3.0)
TAS_KT = np.linspace(60, 500, 400)
V = TAS_KT * KT
BANK = np.degrees(PF.bank_for_turn_rate(V, RATE))
RULE = TAS_KT / 10 + 7
V25 = PF.level_turn(1.0, np.radians(25))[0] / RATE    # speed above which 25 deg gives less than 3 deg/s (rate ~ 1/V)

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 4.6))
    ax.plot(TAS_KT, BANK, color=MAROON, lw=2.6, label="bank for 3°/s (exact)")
    ax.plot(TAS_KT, RULE, color=BLUE, lw=1.8, ls="--", label="rule of thumb: TAS/10 + 7")
    ax.axhline(25, color=INK, lw=1.2, ls=":")
    ax.axvline(V25 / KT, color=GREY, lw=1.2)
    ax.text(490, 21, "ICAO holding: at most 25°", fontsize=11, ha="right")
    ax.text(V25 / KT + 5, 5, f"{V25 / KT:.0f} kt: above this,\na 25° turn is slower than 3°/s", fontsize=11, color=GREY)
    ax.set_xlabel("true airspeed (kt)"); ax.set_ylabel("bank angle (deg)")
    ax.set_xlim(60, 500); ax.set_ylim(0, 60)
    ax.legend(frameon=False, fontsize=11, loc="upper left")
    despine(ax)
    save(fig, "fig-24-standard-rate")
