Chris773 commited on
Commit
7a03037
·
verified ·
1 Parent(s): d868b78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -70
app.py CHANGED
@@ -16,8 +16,6 @@
16
  # THIS VERSION (changes requested):
17
  # - Mode D constant southern speed changed from 447 kt -> 443 kt
18
  # - NEW Map feature: show lat/lon under mouse cursor (Leaflet MousePosition plugin)
19
- # - FIX: Avoid heavy work at import (lazy-load KML/KMZ + satLoc patch) to prevent exit 137
20
- # - FIX: Launch Gradio with Spaces PORT
21
 
22
  import os
23
  import re
@@ -239,6 +237,15 @@ def load_ping_rings_from_kml(path: str):
239
  return rings
240
 
241
 
 
 
 
 
 
 
 
 
 
242
  # ================= KMZ (Search Areas) =================
243
  def _kml_ns_from_root(root):
244
  m = re.match(r"\{(.*)\}", root.tag)
@@ -320,62 +327,13 @@ def load_search_areas_from_kmz(kmz_path: str):
320
  return out
321
 
322
 
323
- # ================= LAZY LOAD (prevents exit 137 on Spaces) =================
324
- PING_RINGS = {}
325
- SEARCH_AREAS = []
326
- KML_LOAD_ERROR = ""
327
- KMZ_LOAD_ERROR = ""
328
- _LOADED_ONCE = False
329
-
330
-
331
- def _patch_measured_bfo_0020():
332
- target_labels = {"24:20:00", "00:20:00"}
333
- patched = False
334
- try:
335
- for row in fp.satLoc:
336
- try:
337
- lbl = fp._extract_time_string(row[0])
338
- except Exception:
339
- continue
340
- if lbl in target_labels:
341
- try:
342
- row[SATLOC_IDX_MEAS_BFO] = float(MEASURED_BFO_0020_HZ)
343
- patched = True
344
- except Exception:
345
- pass
346
- except Exception:
347
- return False
348
- return patched
349
-
350
-
351
- def ensure_loaded_once():
352
- global PING_RINGS, SEARCH_AREAS, KML_LOAD_ERROR, KMZ_LOAD_ERROR, _LOADED_ONCE
353
- if _LOADED_ONCE:
354
- return
355
-
356
- # KML
357
- try:
358
- PING_RINGS = load_ping_rings_from_kml(KML_FILE)
359
- KML_LOAD_ERROR = ""
360
- except Exception as e:
361
- PING_RINGS = {}
362
- KML_LOAD_ERROR = str(e)
363
-
364
- # KMZ
365
- try:
366
- SEARCH_AREAS = load_search_areas_from_kmz(SEARCH_AREAS_KMZ)
367
- KMZ_LOAD_ERROR = ""
368
- except Exception as e:
369
- SEARCH_AREAS = []
370
- KMZ_LOAD_ERROR = str(e)
371
-
372
- # Patch satLoc
373
- try:
374
- _patch_measured_bfo_0020()
375
- except Exception:
376
- pass
377
-
378
- _LOADED_ONCE = True
379
 
380
 
381
  # ================= LEGS HELPERS =================
@@ -558,6 +516,30 @@ def dt_0020_utc() -> datetime:
558
  return fp._time_label_to_dt("24:20:00", fp.SAT_EPOCH_UTC)
559
 
560
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
561
  # ================= ALTITUDE HELPERS =================
562
  def altitude_ft_at_dt(dt_ping: datetime, start_dt: datetime, alt0_ft: float,
563
  vs_fpm: float, descent_start_dt: datetime | None):
@@ -1030,9 +1012,6 @@ def add_named_marker(m, name: str, lat: float, lon: float):
1030
 
1031
 
1032
  def build_leaflet_map(path_pts, legs, method, start_dt):
1033
- # Ensure KML/KMZ are loaded only when actually needed
1034
- ensure_loaded_once()
1035
-
1036
  start_lat, start_lon = path_pts[0]
1037
  is_guided = (start_dt == RADAR_FIX_TIME_UTC)
1038
 
@@ -1975,8 +1954,6 @@ def _run_common(start_lat, start_lon, legs, method, start_dt, alt_ft, vs_fpm, de
1975
 
1976
 
1977
  def run_sim_manual(start_lat, start_lon, legs_df, method, start_time_str, alt_ft, vs_fpm, descent_start_choice):
1978
- ensure_loaded_once()
1979
-
1980
  legs = parse_legs_df(legs_df)
1981
  start_dt = _parse_start_time(start_time_str)
1982
 
@@ -1988,8 +1965,6 @@ def run_sim_manual(start_lat, start_lon, legs_df, method, start_time_str, alt_ft
1988
 
1989
 
1990
  def run_guided(guided_mode, start_lat, start_lon, method, alt_ft, vs_fpm, descent_start_choice):
1991
- ensure_loaded_once()
1992
-
1993
  if guided_mode == "Straight heading":
1994
  legs, _report_md, start_dt = solve_guided_straight(method, alt_ft, vs_fpm, descent_start_choice)
1995
  elif guided_mode == "Variable heading & speed":
@@ -2027,8 +2002,6 @@ def run_guided(guided_mode, start_lat, start_lon, method, alt_ft, vs_fpm, descen
2027
  with gr.Blocks() as demo:
2028
  gr.Markdown("## ✈️ MH370 Flight Simulator — Leaflet + Esri Ocean/Satellite")
2029
 
2030
- # NOTE: KML/KMZ now lazy-loaded, so we can only show errors after load.
2031
- # We keep the old blocks but they will likely be empty at startup.
2032
  if KML_LOAD_ERROR:
2033
  gr.Markdown(
2034
  "### ⚠️ KML not loaded\n"
@@ -2132,6 +2105,4 @@ with gr.Blocks() as demo:
2132
  add_btn.click(add_leg, inputs=[legs_df], outputs=[legs_df])
2133
  reset_btn.click(reset_legs, outputs=[legs_df])
2134
 
2135
-
2136
- if __name__ == "__main__":
2137
- demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))
 
16
  # THIS VERSION (changes requested):
17
  # - Mode D constant southern speed changed from 447 kt -> 443 kt
18
  # - NEW Map feature: show lat/lon under mouse cursor (Leaflet MousePosition plugin)
 
 
19
 
20
  import os
21
  import re
 
237
  return rings
238
 
239
 
240
+ try:
241
+ PING_RINGS = load_ping_rings_from_kml(KML_FILE)
242
+ except Exception as e:
243
+ PING_RINGS = {}
244
+ KML_LOAD_ERROR = str(e)
245
+ else:
246
+ KML_LOAD_ERROR = ""
247
+
248
+
249
  # ================= KMZ (Search Areas) =================
250
  def _kml_ns_from_root(root):
251
  m = re.match(r"\{(.*)\}", root.tag)
 
327
  return out
328
 
329
 
330
+ try:
331
+ SEARCH_AREAS = load_search_areas_from_kmz(SEARCH_AREAS_KMZ)
332
+ except Exception as e:
333
+ SEARCH_AREAS = []
334
+ KMZ_LOAD_ERROR = str(e)
335
+ else:
336
+ KMZ_LOAD_ERROR = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337
 
338
 
339
  # ================= LEGS HELPERS =================
 
516
  return fp._time_label_to_dt("24:20:00", fp.SAT_EPOCH_UTC)
517
 
518
 
519
+ # ================= PATCH satLoc: ensure measured BFO at 00:20 is 182 Hz =================
520
+ def _patch_measured_bfo_0020():
521
+ target_labels = {"24:20:00", "00:20:00"}
522
+ patched = False
523
+ try:
524
+ for row in fp.satLoc:
525
+ try:
526
+ lbl = fp._extract_time_string(row[0])
527
+ except Exception:
528
+ continue
529
+ if lbl in target_labels:
530
+ try:
531
+ row[SATLOC_IDX_MEAS_BFO] = float(MEASURED_BFO_0020_HZ)
532
+ patched = True
533
+ except Exception:
534
+ pass
535
+ except Exception:
536
+ return False
537
+ return patched
538
+
539
+
540
+ _ = _patch_measured_bfo_0020()
541
+
542
+
543
  # ================= ALTITUDE HELPERS =================
544
  def altitude_ft_at_dt(dt_ping: datetime, start_dt: datetime, alt0_ft: float,
545
  vs_fpm: float, descent_start_dt: datetime | None):
 
1012
 
1013
 
1014
  def build_leaflet_map(path_pts, legs, method, start_dt):
 
 
 
1015
  start_lat, start_lon = path_pts[0]
1016
  is_guided = (start_dt == RADAR_FIX_TIME_UTC)
1017
 
 
1954
 
1955
 
1956
  def run_sim_manual(start_lat, start_lon, legs_df, method, start_time_str, alt_ft, vs_fpm, descent_start_choice):
 
 
1957
  legs = parse_legs_df(legs_df)
1958
  start_dt = _parse_start_time(start_time_str)
1959
 
 
1965
 
1966
 
1967
  def run_guided(guided_mode, start_lat, start_lon, method, alt_ft, vs_fpm, descent_start_choice):
 
 
1968
  if guided_mode == "Straight heading":
1969
  legs, _report_md, start_dt = solve_guided_straight(method, alt_ft, vs_fpm, descent_start_choice)
1970
  elif guided_mode == "Variable heading & speed":
 
2002
  with gr.Blocks() as demo:
2003
  gr.Markdown("## ✈️ MH370 Flight Simulator — Leaflet + Esri Ocean/Satellite")
2004
 
 
 
2005
  if KML_LOAD_ERROR:
2006
  gr.Markdown(
2007
  "### ⚠️ KML not loaded\n"
 
2105
  add_btn.click(add_leg, inputs=[legs_df], outputs=[legs_df])
2106
  reset_btn.click(reset_legs, outputs=[legs_df])
2107
 
2108
+ demo.launch()