Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,11 +8,13 @@
|
|
| 8 |
# (14) BFO analysis chart + MAE/RMSE
|
| 9 |
# Guided mode scenarios:
|
| 10 |
# A) Straight (single heading+speed) with FIXED schedule
|
|
|
|
| 11 |
# B) Turn after Ping Ring 6 (two headings: legs 1-5, legs 6-7)
|
| 12 |
-
# C)
|
| 13 |
# NEW: Altitude vs Time chart (visualize descent profile)
|
| 14 |
# NEW: Validation table at schedule timestamps (lat/lon/hdg/spd + BFO/BTO measured vs calculated + errors)
|
| 15 |
# NEW: Explicit weighting between BFO and BTO in guided solver (to emphasize BTO)
|
|
|
|
| 16 |
# Esri Ocean + Satellite basemaps with layer control
|
| 17 |
# Ping ring timestamps (Option A) shown in ring tooltip
|
| 18 |
# FIX: robust KML loading + no empty default selection crash
|
|
@@ -65,7 +67,7 @@ RING_TIMES = {
|
|
| 65 |
BFO_SCALE_HZ = 5.0
|
| 66 |
BTO_SCALE_US = 50.0
|
| 67 |
|
| 68 |
-
# EXPLICIT WEIGHTS
|
| 69 |
DEFAULT_WEIGHT_BFO = 1.0
|
| 70 |
DEFAULT_WEIGHT_BTO = 2.0 # emphasize BTO by default (increase to care more about BTO)
|
| 71 |
|
|
@@ -94,6 +96,15 @@ TURN_DELTA_MAX_DEG = 10.0 # heading2 = heading1 +/- up to 10 degrees
|
|
| 94 |
SPEED_SEARCH_PLUS_MINUS_KT = 15.0
|
| 95 |
SPEED_SEARCH_STEP_KT = 5.0
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
# ================= RADAR FIX (LAST KNOWN PRIMARY RADAR POINT) =================
|
| 98 |
# You provided:
|
| 99 |
# Start: 2014-03-07 18:22:12Z @ 6.578°N, 96.341°E, FL350
|
|
@@ -515,6 +526,10 @@ def _predict_state_from_leg_start(method, leg_start_lat, leg_start_lon, hdg_deg,
|
|
| 515 |
return float(lat), float(lon)
|
| 516 |
|
| 517 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 518 |
def _deg2rad(x: float) -> float:
|
| 519 |
return float(x) * math.pi / 180.0
|
| 520 |
|
|
@@ -555,7 +570,6 @@ def compute_turn_leg_from_radar_fix(turn_lat, turn_lon, method):
|
|
| 555 |
if dt_sec <= 0:
|
| 556 |
return None
|
| 557 |
|
| 558 |
-
# We compute distance/bearing with GC inverse; then the *simulation* itself can still use chosen method.
|
| 559 |
dist_m, bearing_deg = gc_distance_m_and_bearing_deg(
|
| 560 |
float(RADAR_FIX_LAT), float(RADAR_FIX_LON),
|
| 561 |
float(turn_lat), float(turn_lon)
|
|
@@ -787,6 +801,106 @@ def score_legs_bfo_bto(
|
|
| 787 |
}
|
| 788 |
|
| 789 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 790 |
# ================= VALIDATION TABLE =================
|
| 791 |
def _to_label_utc(dt: datetime) -> str:
|
| 792 |
return dt.strftime("%H:%M:%SZ")
|
|
@@ -965,6 +1079,96 @@ def solve_guided_straight(
|
|
| 965 |
return legs, report, start_dt
|
| 966 |
|
| 967 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 968 |
def solve_guided_turn_after_ping6(
|
| 969 |
start_lat, start_lon, method, speed_knots, alt_ft, vs_fpm,
|
| 970 |
descent_start_choice, weight_bfo, weight_bto
|
|
@@ -1047,26 +1251,23 @@ def solve_guided_radar_fix_turn_then_southern(
|
|
| 1047 |
descent_start_choice, weight_bfo, weight_bto
|
| 1048 |
):
|
| 1049 |
"""
|
| 1050 |
-
Scenario C:
|
| 1051 |
- Leg 0: RADAR_FIX_TIME_UTC (18:22:12Z) -> TURN_COMPLETE_TIME_UTC (18:28:00Z) to user-chosen completion point.
|
| 1052 |
-
- Then: solve southern leg
|
| 1053 |
- Output: combined legs starting at 18:22:12Z.
|
| 1054 |
"""
|
| 1055 |
-
# 1) Compute the "turn completion" leg (single straight segment)
|
| 1056 |
out = compute_turn_leg_from_radar_fix(float(turn_complete_lat), float(turn_complete_lon), method)
|
| 1057 |
if out is None:
|
| 1058 |
return None, "Invalid radar-fix turn time window.", None
|
| 1059 |
|
| 1060 |
turn_hdg, turn_spd_kt, turn_minutes, turn_dist_km = out
|
| 1061 |
|
| 1062 |
-
# Simple plausibility guardrail: if wildly high/low, tell user to move completion point
|
| 1063 |
if not (200.0 <= turn_spd_kt <= 650.0):
|
| 1064 |
return None, (
|
| 1065 |
f"Turn leg implies **{turn_spd_kt:.0f} kt** from radar fix to completion @18:28Z. "
|
| 1066 |
f"That’s implausible — move the completion point closer/farther."
|
| 1067 |
), None
|
| 1068 |
|
| 1069 |
-
# 2) Solve southern heading from the completion point (18:28Z start)
|
| 1070 |
start_dt = dt_guided_start_utc()
|
| 1071 |
end_dt = dt_guided_end_utc()
|
| 1072 |
|
|
@@ -1076,7 +1277,6 @@ def solve_guided_radar_fix_turn_then_southern(
|
|
| 1076 |
elif (descent_start_choice or "").startswith("After Ping Ring 7"):
|
| 1077 |
descent_start_dt = dt_ping7_utc()
|
| 1078 |
|
| 1079 |
-
# Schedule sanity for the southern part
|
| 1080 |
total_sched_min = float(np.sum(GUIDED_LEG_SCHEDULE_MIN))
|
| 1081 |
total_window_min = (end_dt - start_dt).total_seconds() / 60.0
|
| 1082 |
if abs(total_sched_min - total_window_min) > 0.01:
|
|
@@ -1104,8 +1304,6 @@ def solve_guided_radar_fix_turn_then_southern(
|
|
| 1104 |
return None, "Radar-fix scenario: not enough ping points in 18:28→00:20 window to solve southern heading.", None
|
| 1105 |
|
| 1106 |
southern_legs = make_legs_from_schedule(best_h, float(speed_knots_southern), GUIDED_LEG_SCHEDULE_MIN)
|
| 1107 |
-
|
| 1108 |
-
# 3) Combine: turn leg + southern legs
|
| 1109 |
all_legs = [(turn_hdg, turn_spd_kt, turn_minutes)] + southern_legs
|
| 1110 |
|
| 1111 |
rbto = best["rmse_bto"]
|
|
@@ -1124,7 +1322,6 @@ def solve_guided_radar_fix_turn_then_southern(
|
|
| 1124 |
f"- BTO points: **{best['n_bto']}** | RMSE_BTO: **{rbto_txt}**",
|
| 1125 |
])
|
| 1126 |
|
| 1127 |
-
# Overall start time is radar fix time
|
| 1128 |
return all_legs, report, RADAR_FIX_TIME_UTC
|
| 1129 |
|
| 1130 |
|
|
@@ -1166,11 +1363,21 @@ def run_guided(
|
|
| 1166 |
start_lat, start_lon, method, rings, alt_ft, vs_fpm, descent_start_choice,
|
| 1167 |
speed_profile, custom_mach, custom_knots, guided_scenario,
|
| 1168 |
weight_bfo, weight_bto,
|
| 1169 |
-
turn_complete_lat, turn_complete_lon
|
|
|
|
| 1170 |
):
|
| 1171 |
spd_knots = speed_from_profile(speed_profile, alt_ft, custom_mach, custom_knots)
|
| 1172 |
|
| 1173 |
-
if (guided_scenario or "").startswith("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1174 |
legs, report_md, start_dt = solve_guided_radar_fix_turn_then_southern(
|
| 1175 |
turn_complete_lat, turn_complete_lon,
|
| 1176 |
method,
|
|
@@ -1178,7 +1385,6 @@ def run_guided(
|
|
| 1178 |
alt_ft, vs_fpm,
|
| 1179 |
descent_start_choice, weight_bfo, weight_bto
|
| 1180 |
)
|
| 1181 |
-
# For this scenario, the simulation start location/time is the radar fix
|
| 1182 |
sim_start_lat = RADAR_FIX_LAT
|
| 1183 |
sim_start_lon = RADAR_FIX_LON
|
| 1184 |
|
|
@@ -1257,6 +1463,7 @@ with gr.Blocks() as demo:
|
|
| 1257 |
guided_scenario = gr.Dropdown(
|
| 1258 |
[
|
| 1259 |
"Straight (single heading)",
|
|
|
|
| 1260 |
"Turn after Ping Ring 6 (two headings)",
|
| 1261 |
"Radar fix → turn completion @18:28Z → southern solve"
|
| 1262 |
],
|
|
@@ -1271,6 +1478,11 @@ with gr.Blocks() as demo:
|
|
| 1271 |
custom_mach = gr.Number(value=0.82, label="Custom Mach (if selected)")
|
| 1272 |
custom_knots = gr.Number(value=470, label="Custom knots (if selected)")
|
| 1273 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1274 |
gr.Markdown("### Radar-fix turn completion point (Scenario C only)")
|
| 1275 |
gr.Markdown(
|
| 1276 |
f"- Radar fix is fixed at **18:22:12Z @ {RADAR_FIX_LAT:.3f}, {RADAR_FIX_LON:.3f}**\n"
|
|
@@ -1281,7 +1493,7 @@ with gr.Blocks() as demo:
|
|
| 1281 |
turn_complete_lat = gr.Slider(-10, 15, value=6.76, step=0.01, label="Turn completion latitude (°)")
|
| 1282 |
turn_complete_lon = gr.Slider(70, 115, value=95.69, step=0.01, label="Turn completion longitude (°)")
|
| 1283 |
|
| 1284 |
-
gr.Markdown("### Guided scoring weights
|
| 1285 |
with gr.Row():
|
| 1286 |
weight_bfo = gr.Slider(0.0, 5.0, value=DEFAULT_WEIGHT_BFO, step=0.1, label="Weight: BFO")
|
| 1287 |
weight_bto = gr.Slider(0.0, 10.0, value=DEFAULT_WEIGHT_BTO, step=0.1, label="Weight: BTO (increase to emphasize BTO)")
|
|
@@ -1336,7 +1548,8 @@ with gr.Blocks() as demo:
|
|
| 1336 |
start_lat, start_lon, method, rings, alt_ft, vs_fpm, descent_start_choice,
|
| 1337 |
speed_profile, custom_mach, custom_knots, guided_scenario,
|
| 1338 |
weight_bfo, weight_bto,
|
| 1339 |
-
turn_complete_lat, turn_complete_lon
|
|
|
|
| 1340 |
],
|
| 1341 |
outputs=[legs_df, status_md, out_map, stats, bfo_plot, bfo_score, alt_plot, validation_df],
|
| 1342 |
)
|
|
|
|
| 8 |
# (14) BFO analysis chart + MAE/RMSE
|
| 9 |
# Guided mode scenarios:
|
| 10 |
# A) Straight (single heading+speed) with FIXED schedule
|
| 11 |
+
# A2) NEW: Straight (nominal heading) + small per-leg drift (±deg) with penalty
|
| 12 |
# B) Turn after Ping Ring 6 (two headings: legs 1-5, legs 6-7)
|
| 13 |
+
# C) Radar fix → user-chosen turn completion point (by 18:28Z) → southern leg solve (kept; you can ignore)
|
| 14 |
# NEW: Altitude vs Time chart (visualize descent profile)
|
| 15 |
# NEW: Validation table at schedule timestamps (lat/lon/hdg/spd + BFO/BTO measured vs calculated + errors)
|
| 16 |
# NEW: Explicit weighting between BFO and BTO in guided solver (to emphasize BTO)
|
| 17 |
+
# NEW: Heading-drift penalty (keeps drift small + defensible)
|
| 18 |
# Esri Ocean + Satellite basemaps with layer control
|
| 19 |
# Ping ring timestamps (Option A) shown in ring tooltip
|
| 20 |
# FIX: robust KML loading + no empty default selection crash
|
|
|
|
| 67 |
BFO_SCALE_HZ = 5.0
|
| 68 |
BTO_SCALE_US = 50.0
|
| 69 |
|
| 70 |
+
# EXPLICIT WEIGHTS
|
| 71 |
DEFAULT_WEIGHT_BFO = 1.0
|
| 72 |
DEFAULT_WEIGHT_BTO = 2.0 # emphasize BTO by default (increase to care more about BTO)
|
| 73 |
|
|
|
|
| 96 |
SPEED_SEARCH_PLUS_MINUS_KT = 15.0
|
| 97 |
SPEED_SEARCH_STEP_KT = 5.0
|
| 98 |
|
| 99 |
+
# ================= HEADING DRIFT (NEW) =================
|
| 100 |
+
# We allow small per-leg deviations around a nominal heading, but add a penalty
|
| 101 |
+
# so it doesn't "free-run" into overfitting.
|
| 102 |
+
DEFAULT_DRIFT_MAX_DEG = 2.0 # typical: 1–2 degrees
|
| 103 |
+
DRIFT_STEP_DEG = 0.5 # discrete search step for deltas
|
| 104 |
+
DEFAULT_WEIGHT_DRIFT = 0.30 # penalty strength in "combined score" units
|
| 105 |
+
DEFAULT_DRIFT_ITERS = 2 # coordinate descent passes
|
| 106 |
+
DRIFT_SCALE_DEG = 1.0 # normalization for drift penalty (rms_deg / scale)
|
| 107 |
+
|
| 108 |
# ================= RADAR FIX (LAST KNOWN PRIMARY RADAR POINT) =================
|
| 109 |
# You provided:
|
| 110 |
# Start: 2014-03-07 18:22:12Z @ 6.578°N, 96.341°E, FL350
|
|
|
|
| 526 |
return float(lat), float(lon)
|
| 527 |
|
| 528 |
|
| 529 |
+
def _wrap_heading_deg(h: float) -> float:
|
| 530 |
+
return float(h) % 360.0
|
| 531 |
+
|
| 532 |
+
|
| 533 |
def _deg2rad(x: float) -> float:
|
| 534 |
return float(x) * math.pi / 180.0
|
| 535 |
|
|
|
|
| 570 |
if dt_sec <= 0:
|
| 571 |
return None
|
| 572 |
|
|
|
|
| 573 |
dist_m, bearing_deg = gc_distance_m_and_bearing_deg(
|
| 574 |
float(RADAR_FIX_LAT), float(RADAR_FIX_LON),
|
| 575 |
float(turn_lat), float(turn_lon)
|
|
|
|
| 801 |
}
|
| 802 |
|
| 803 |
|
| 804 |
+
def _drift_rms_deg(deltas):
|
| 805 |
+
if deltas is None or len(deltas) == 0:
|
| 806 |
+
return 0.0
|
| 807 |
+
d = np.array(deltas, dtype=float)
|
| 808 |
+
return float(np.sqrt(np.mean(d * d)))
|
| 809 |
+
|
| 810 |
+
|
| 811 |
+
def score_legs_bfo_bto_with_drift(
|
| 812 |
+
start_lat, start_lon, method, start_dt, end_dt, legs,
|
| 813 |
+
alt_ft, vs_fpm, descent_start_dt,
|
| 814 |
+
weight_bfo: float, weight_bto: float,
|
| 815 |
+
drift_weight: float, deltas_deg
|
| 816 |
+
):
|
| 817 |
+
s = score_legs_bfo_bto(
|
| 818 |
+
start_lat, start_lon, method, start_dt, end_dt, legs,
|
| 819 |
+
alt_ft, vs_fpm, descent_start_dt,
|
| 820 |
+
weight_bfo, weight_bto
|
| 821 |
+
)
|
| 822 |
+
if s is None:
|
| 823 |
+
return None
|
| 824 |
+
|
| 825 |
+
rms = _drift_rms_deg(deltas_deg)
|
| 826 |
+
s["drift_rms_deg"] = rms
|
| 827 |
+
if float(drift_weight) > 0.0:
|
| 828 |
+
s["combined"] = float(s["combined"]) + float(drift_weight) * (rms / DRIFT_SCALE_DEG)
|
| 829 |
+
return s
|
| 830 |
+
|
| 831 |
+
|
| 832 |
+
def _apply_base_plus_deltas(base_headings, deltas_deg):
|
| 833 |
+
headings = []
|
| 834 |
+
for b, d in zip(base_headings, deltas_deg):
|
| 835 |
+
headings.append(_wrap_heading_deg(float(b) + float(d)))
|
| 836 |
+
return headings
|
| 837 |
+
|
| 838 |
+
|
| 839 |
+
def _coordinate_descent_deltas(
|
| 840 |
+
start_lat, start_lon, method, start_dt, end_dt,
|
| 841 |
+
speed_knots, schedule_minutes,
|
| 842 |
+
alt_ft, vs_fpm, descent_start_dt,
|
| 843 |
+
weight_bfo, weight_bto,
|
| 844 |
+
drift_weight,
|
| 845 |
+
base_headings,
|
| 846 |
+
drift_max_deg: float,
|
| 847 |
+
drift_step_deg: float,
|
| 848 |
+
iters: int
|
| 849 |
+
):
|
| 850 |
+
n = len(schedule_minutes)
|
| 851 |
+
deltas = [0.0] * n
|
| 852 |
+
cand = list(np.arange(-float(drift_max_deg), float(drift_max_deg) + 1e-9, float(drift_step_deg)))
|
| 853 |
+
|
| 854 |
+
best_s = None
|
| 855 |
+
best_d = deltas[:]
|
| 856 |
+
|
| 857 |
+
# simple coordinate descent
|
| 858 |
+
for _it in range(max(1, int(iters))):
|
| 859 |
+
improved = False
|
| 860 |
+
for j in range(n):
|
| 861 |
+
best_local_s = None
|
| 862 |
+
best_local_dj = deltas[j]
|
| 863 |
+
for dv in cand:
|
| 864 |
+
trial = deltas[:]
|
| 865 |
+
trial[j] = float(dv)
|
| 866 |
+
trial_headings = _apply_base_plus_deltas(base_headings, trial)
|
| 867 |
+
legs = make_legs_from_schedule(trial_headings, float(speed_knots), schedule_minutes)
|
| 868 |
+
|
| 869 |
+
s = score_legs_bfo_bto_with_drift(
|
| 870 |
+
start_lat, start_lon, method, start_dt, end_dt, legs,
|
| 871 |
+
alt_ft, vs_fpm, descent_start_dt,
|
| 872 |
+
weight_bfo, weight_bto,
|
| 873 |
+
drift_weight, trial
|
| 874 |
+
)
|
| 875 |
+
if s is None:
|
| 876 |
+
continue
|
| 877 |
+
if best_local_s is None or s["combined"] < best_local_s["combined"]:
|
| 878 |
+
best_local_s = s
|
| 879 |
+
best_local_dj = float(dv)
|
| 880 |
+
|
| 881 |
+
deltas[j] = best_local_dj
|
| 882 |
+
|
| 883 |
+
# track best overall
|
| 884 |
+
if best_local_s is not None and (best_s is None or best_local_s["combined"] < best_s["combined"]):
|
| 885 |
+
best_s = best_local_s
|
| 886 |
+
best_d = deltas[:]
|
| 887 |
+
improved = True
|
| 888 |
+
|
| 889 |
+
if not improved:
|
| 890 |
+
break
|
| 891 |
+
|
| 892 |
+
# final score (ensure consistency)
|
| 893 |
+
final_headings = _apply_base_plus_deltas(base_headings, best_d)
|
| 894 |
+
final_legs = make_legs_from_schedule(final_headings, float(speed_knots), schedule_minutes)
|
| 895 |
+
final_s = score_legs_bfo_bto_with_drift(
|
| 896 |
+
start_lat, start_lon, method, start_dt, end_dt, final_legs,
|
| 897 |
+
alt_ft, vs_fpm, descent_start_dt,
|
| 898 |
+
weight_bfo, weight_bto,
|
| 899 |
+
drift_weight, best_d
|
| 900 |
+
)
|
| 901 |
+
return final_legs, final_s, best_d
|
| 902 |
+
|
| 903 |
+
|
| 904 |
# ================= VALIDATION TABLE =================
|
| 905 |
def _to_label_utc(dt: datetime) -> str:
|
| 906 |
return dt.strftime("%H:%M:%SZ")
|
|
|
|
| 1079 |
return legs, report, start_dt
|
| 1080 |
|
| 1081 |
|
| 1082 |
+
def solve_guided_straight_with_drift(
|
| 1083 |
+
start_lat, start_lon, method, speed_knots, alt_ft, vs_fpm,
|
| 1084 |
+
descent_start_choice, weight_bfo, weight_bto,
|
| 1085 |
+
drift_max_deg, drift_weight
|
| 1086 |
+
):
|
| 1087 |
+
"""
|
| 1088 |
+
New: Straight nominal heading H0, but allow small per-leg deltas within ±drift_max_deg.
|
| 1089 |
+
We add a penalty proportional to RMS(delta) so the drift stays small/defensible.
|
| 1090 |
+
"""
|
| 1091 |
+
start_dt = dt_guided_start_utc()
|
| 1092 |
+
end_dt = dt_guided_end_utc()
|
| 1093 |
+
|
| 1094 |
+
descent_start_dt = None
|
| 1095 |
+
if (descent_start_choice or "").startswith("After Ping Ring 6"):
|
| 1096 |
+
descent_start_dt = dt_ping6_utc()
|
| 1097 |
+
elif (descent_start_choice or "").startswith("After Ping Ring 7"):
|
| 1098 |
+
descent_start_dt = dt_ping7_utc()
|
| 1099 |
+
|
| 1100 |
+
total_sched_min = float(np.sum(GUIDED_LEG_SCHEDULE_MIN))
|
| 1101 |
+
total_window_min = (end_dt - start_dt).total_seconds() / 60.0
|
| 1102 |
+
if abs(total_sched_min - total_window_min) > 0.01:
|
| 1103 |
+
return None, (
|
| 1104 |
+
f"Guided schedule mismatch: schedule totals {total_sched_min:.1f} min but window is {total_window_min:.1f} min."
|
| 1105 |
+
), None
|
| 1106 |
+
|
| 1107 |
+
# runtime guard: drift makes evaluation heavier, so use a slightly coarser base search
|
| 1108 |
+
base_step = 1.0 if float(drift_max_deg) > 0 else STRAIGHT_STEP_DEG
|
| 1109 |
+
|
| 1110 |
+
best_s = None
|
| 1111 |
+
best_legs = None
|
| 1112 |
+
best_h0 = None
|
| 1113 |
+
best_deltas = None
|
| 1114 |
+
|
| 1115 |
+
n = len(GUIDED_LEG_SCHEDULE_MIN)
|
| 1116 |
+
|
| 1117 |
+
h0 = GUIDED_HEADING_MIN
|
| 1118 |
+
while h0 <= GUIDED_HEADING_MAX + 1e-9:
|
| 1119 |
+
base_headings = [float(h0)] * n
|
| 1120 |
+
|
| 1121 |
+
legs, s, deltas = _coordinate_descent_deltas(
|
| 1122 |
+
start_lat, start_lon, method, start_dt, end_dt,
|
| 1123 |
+
speed_knots, GUIDED_LEG_SCHEDULE_MIN,
|
| 1124 |
+
alt_ft, vs_fpm, descent_start_dt,
|
| 1125 |
+
weight_bfo, weight_bto,
|
| 1126 |
+
float(drift_weight),
|
| 1127 |
+
base_headings,
|
| 1128 |
+
float(drift_max_deg),
|
| 1129 |
+
DRIFT_STEP_DEG,
|
| 1130 |
+
DEFAULT_DRIFT_ITERS
|
| 1131 |
+
)
|
| 1132 |
+
|
| 1133 |
+
if s is not None and (best_s is None or s["combined"] < best_s["combined"]):
|
| 1134 |
+
best_s = s
|
| 1135 |
+
best_legs = legs
|
| 1136 |
+
best_h0 = float(h0)
|
| 1137 |
+
best_deltas = deltas
|
| 1138 |
+
|
| 1139 |
+
h0 += float(base_step)
|
| 1140 |
+
|
| 1141 |
+
if best_s is None or best_legs is None:
|
| 1142 |
+
return None, f"Guided drift solve: not enough ping points between {GUIDED_START_LABEL} and {GUIDED_END_LABEL} to score.", None
|
| 1143 |
+
|
| 1144 |
+
rbto = best_s["rmse_bto"]
|
| 1145 |
+
rbto_txt = f"{rbto:.1f} µs" if rbto is not None else "n/a"
|
| 1146 |
+
rms_drift = best_s.get("drift_rms_deg", _drift_rms_deg(best_deltas))
|
| 1147 |
+
sched_txt = ", ".join([f"{m:.0f}" for m in GUIDED_LEG_SCHEDULE_MIN]) + " min"
|
| 1148 |
+
|
| 1149 |
+
# compact drift string (7 values)
|
| 1150 |
+
drift_str = ""
|
| 1151 |
+
if best_deltas is not None:
|
| 1152 |
+
drift_str = ", ".join([f"{d:+.1f}°" for d in best_deltas])
|
| 1153 |
+
|
| 1154 |
+
report = "\n".join([
|
| 1155 |
+
"**Guided mode: Straight + small per-leg heading drift (penalized)**",
|
| 1156 |
+
f"- Window: **{GUIDED_START_LABEL}Z → 00:20:00Z**",
|
| 1157 |
+
f"- Start: lat **{float(start_lat):.2f}**, lon **{float(start_lon):.2f}**",
|
| 1158 |
+
f"- Legs: **{sched_txt}**",
|
| 1159 |
+
f"- Speed: **{float(speed_knots):.1f} kt**",
|
| 1160 |
+
f"- Nominal heading H₀: **{best_h0:.1f}°** (searched {GUIDED_HEADING_MIN:.0f}–{GUIDED_HEADING_MAX:.0f}°, step {1.0 if float(drift_max_deg)>0 else STRAIGHT_STEP_DEG}°)",
|
| 1161 |
+
f"- Drift bounds: **±{float(drift_max_deg):.1f}°** | Drift penalty weight: **{float(drift_weight):.2f}**",
|
| 1162 |
+
f"- Drift RMS: **{rms_drift:.2f}°**",
|
| 1163 |
+
f"- Drift per leg (Δ): {drift_str}" if drift_str else "",
|
| 1164 |
+
f"- Weights: BFO **{float(weight_bfo):.2f}**, BTO **{float(weight_bto):.2f}**",
|
| 1165 |
+
f"- BFO points: **{best_s['n_bfo']}** | RMSE_BFO: **{best_s['rmse_bfo']:.2f} Hz**",
|
| 1166 |
+
f"- BTO points: **{best_s['n_bto']}** | RMSE_BTO: **{rbto_txt}**",
|
| 1167 |
+
]).strip()
|
| 1168 |
+
|
| 1169 |
+
return best_legs, report, start_dt
|
| 1170 |
+
|
| 1171 |
+
|
| 1172 |
def solve_guided_turn_after_ping6(
|
| 1173 |
start_lat, start_lon, method, speed_knots, alt_ft, vs_fpm,
|
| 1174 |
descent_start_choice, weight_bfo, weight_bto
|
|
|
|
| 1251 |
descent_start_choice, weight_bfo, weight_bto
|
| 1252 |
):
|
| 1253 |
"""
|
| 1254 |
+
Scenario C (kept for compatibility; you can ignore it):
|
| 1255 |
- Leg 0: RADAR_FIX_TIME_UTC (18:22:12Z) -> TURN_COMPLETE_TIME_UTC (18:28:00Z) to user-chosen completion point.
|
| 1256 |
+
- Then: solve southern leg heading using the standard window 18:28 -> 00:20 with standard schedule.
|
| 1257 |
- Output: combined legs starting at 18:22:12Z.
|
| 1258 |
"""
|
|
|
|
| 1259 |
out = compute_turn_leg_from_radar_fix(float(turn_complete_lat), float(turn_complete_lon), method)
|
| 1260 |
if out is None:
|
| 1261 |
return None, "Invalid radar-fix turn time window.", None
|
| 1262 |
|
| 1263 |
turn_hdg, turn_spd_kt, turn_minutes, turn_dist_km = out
|
| 1264 |
|
|
|
|
| 1265 |
if not (200.0 <= turn_spd_kt <= 650.0):
|
| 1266 |
return None, (
|
| 1267 |
f"Turn leg implies **{turn_spd_kt:.0f} kt** from radar fix to completion @18:28Z. "
|
| 1268 |
f"That’s implausible — move the completion point closer/farther."
|
| 1269 |
), None
|
| 1270 |
|
|
|
|
| 1271 |
start_dt = dt_guided_start_utc()
|
| 1272 |
end_dt = dt_guided_end_utc()
|
| 1273 |
|
|
|
|
| 1277 |
elif (descent_start_choice or "").startswith("After Ping Ring 7"):
|
| 1278 |
descent_start_dt = dt_ping7_utc()
|
| 1279 |
|
|
|
|
| 1280 |
total_sched_min = float(np.sum(GUIDED_LEG_SCHEDULE_MIN))
|
| 1281 |
total_window_min = (end_dt - start_dt).total_seconds() / 60.0
|
| 1282 |
if abs(total_sched_min - total_window_min) > 0.01:
|
|
|
|
| 1304 |
return None, "Radar-fix scenario: not enough ping points in 18:28→00:20 window to solve southern heading.", None
|
| 1305 |
|
| 1306 |
southern_legs = make_legs_from_schedule(best_h, float(speed_knots_southern), GUIDED_LEG_SCHEDULE_MIN)
|
|
|
|
|
|
|
| 1307 |
all_legs = [(turn_hdg, turn_spd_kt, turn_minutes)] + southern_legs
|
| 1308 |
|
| 1309 |
rbto = best["rmse_bto"]
|
|
|
|
| 1322 |
f"- BTO points: **{best['n_bto']}** | RMSE_BTO: **{rbto_txt}**",
|
| 1323 |
])
|
| 1324 |
|
|
|
|
| 1325 |
return all_legs, report, RADAR_FIX_TIME_UTC
|
| 1326 |
|
| 1327 |
|
|
|
|
| 1363 |
start_lat, start_lon, method, rings, alt_ft, vs_fpm, descent_start_choice,
|
| 1364 |
speed_profile, custom_mach, custom_knots, guided_scenario,
|
| 1365 |
weight_bfo, weight_bto,
|
| 1366 |
+
turn_complete_lat, turn_complete_lon,
|
| 1367 |
+
drift_max_deg, drift_weight
|
| 1368 |
):
|
| 1369 |
spd_knots = speed_from_profile(speed_profile, alt_ft, custom_mach, custom_knots)
|
| 1370 |
|
| 1371 |
+
if (guided_scenario or "").startswith("Straight + drift"):
|
| 1372 |
+
legs, report_md, start_dt = solve_guided_straight_with_drift(
|
| 1373 |
+
start_lat, start_lon, method, spd_knots, alt_ft, vs_fpm,
|
| 1374 |
+
descent_start_choice, weight_bfo, weight_bto,
|
| 1375 |
+
drift_max_deg, drift_weight
|
| 1376 |
+
)
|
| 1377 |
+
sim_start_lat = start_lat
|
| 1378 |
+
sim_start_lon = start_lon
|
| 1379 |
+
|
| 1380 |
+
elif (guided_scenario or "").startswith("Radar fix"):
|
| 1381 |
legs, report_md, start_dt = solve_guided_radar_fix_turn_then_southern(
|
| 1382 |
turn_complete_lat, turn_complete_lon,
|
| 1383 |
method,
|
|
|
|
| 1385 |
alt_ft, vs_fpm,
|
| 1386 |
descent_start_choice, weight_bfo, weight_bto
|
| 1387 |
)
|
|
|
|
| 1388 |
sim_start_lat = RADAR_FIX_LAT
|
| 1389 |
sim_start_lon = RADAR_FIX_LON
|
| 1390 |
|
|
|
|
| 1463 |
guided_scenario = gr.Dropdown(
|
| 1464 |
[
|
| 1465 |
"Straight (single heading)",
|
| 1466 |
+
"Straight + drift (per-leg ±deg, penalized)",
|
| 1467 |
"Turn after Ping Ring 6 (two headings)",
|
| 1468 |
"Radar fix → turn completion @18:28Z → southern solve"
|
| 1469 |
],
|
|
|
|
| 1478 |
custom_mach = gr.Number(value=0.82, label="Custom Mach (if selected)")
|
| 1479 |
custom_knots = gr.Number(value=470, label="Custom knots (if selected)")
|
| 1480 |
|
| 1481 |
+
gr.Markdown("### Heading drift settings (for “Straight + drift” scenario)")
|
| 1482 |
+
with gr.Row():
|
| 1483 |
+
drift_max_deg = gr.Slider(0.0, 5.0, value=DEFAULT_DRIFT_MAX_DEG, step=0.1, label="Max drift per leg (±deg)")
|
| 1484 |
+
drift_weight = gr.Slider(0.0, 2.0, value=DEFAULT_WEIGHT_DRIFT, step=0.05, label="Drift penalty weight (higher = less drift)")
|
| 1485 |
+
|
| 1486 |
gr.Markdown("### Radar-fix turn completion point (Scenario C only)")
|
| 1487 |
gr.Markdown(
|
| 1488 |
f"- Radar fix is fixed at **18:22:12Z @ {RADAR_FIX_LAT:.3f}, {RADAR_FIX_LON:.3f}**\n"
|
|
|
|
| 1493 |
turn_complete_lat = gr.Slider(-10, 15, value=6.76, step=0.01, label="Turn completion latitude (°)")
|
| 1494 |
turn_complete_lon = gr.Slider(70, 115, value=95.69, step=0.01, label="Turn completion longitude (°)")
|
| 1495 |
|
| 1496 |
+
gr.Markdown("### Guided scoring weights")
|
| 1497 |
with gr.Row():
|
| 1498 |
weight_bfo = gr.Slider(0.0, 5.0, value=DEFAULT_WEIGHT_BFO, step=0.1, label="Weight: BFO")
|
| 1499 |
weight_bto = gr.Slider(0.0, 10.0, value=DEFAULT_WEIGHT_BTO, step=0.1, label="Weight: BTO (increase to emphasize BTO)")
|
|
|
|
| 1548 |
start_lat, start_lon, method, rings, alt_ft, vs_fpm, descent_start_choice,
|
| 1549 |
speed_profile, custom_mach, custom_knots, guided_scenario,
|
| 1550 |
weight_bfo, weight_bto,
|
| 1551 |
+
turn_complete_lat, turn_complete_lon,
|
| 1552 |
+
drift_max_deg, drift_weight
|
| 1553 |
],
|
| 1554 |
outputs=[legs_df, status_md, out_map, stats, bfo_plot, bfo_score, alt_plot, validation_df],
|
| 1555 |
)
|