-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
4240 lines (3707 loc) · 189 KB
/
Copy pathmain.py
File metadata and controls
4240 lines (3707 loc) · 189 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
KRONOS-WX CLI — Oklahoma severe weather case library orchestration.
Commands:
build-case-skeleton Build initial case library from SPC tornado data
enrich-case Add sounding + Mesonet data to a single case
enrich-all Bulk enrichment over a year range
analyze-cap-behavior Compute full cap erosion trajectory for a case
build-bust-database Identify bust and alarm-bell cases
"""
import logging
import sys
from datetime import date, datetime, timezone, timedelta
import click
from rich.console import Console
from rich.table import Table
from rich.progress import track
from ok_weather_model.config import (
CASE_LIBRARY_START_YEAR,
CASE_LIBRARY_END_YEAR,
LOG_LEVEL,
LOG_DIR,
)
from ok_weather_model.models import (
EventClass,
CapBehavior,
OklahomaSoundingStation,
HistoricalCase,
)
console = Console()
# ── Logging setup ─────────────────────────────────────────────────────────────
def _setup_logging() -> None:
log_file = LOG_DIR / "kronos_wx.log"
logging.basicConfig(
level=getattr(logging, LOG_LEVEL, logging.INFO),
format="%(asctime)s %(name)s %(levelname)s %(message)s",
handlers=[
logging.StreamHandler(sys.stderr),
logging.FileHandler(log_file),
],
)
_setup_logging()
logger = logging.getLogger(__name__)
# ── CLI group ─────────────────────────────────────────────────────────────────
@click.group()
@click.version_option("0.1.0", prog_name="kronos-wx")
def cli():
"""KRONOS-WX: Oklahoma severe weather outbreak/bust analysis system."""
pass
# ── build-case-skeleton ───────────────────────────────────────────────────────
@cli.command("build-case-skeleton")
@click.option("--start-year", default=CASE_LIBRARY_START_YEAR, show_default=True,
help="First year to include")
@click.option("--end-year", default=CASE_LIBRARY_END_YEAR, show_default=True,
help="Last year to include")
@click.option("--overwrite", is_flag=True, default=False,
help="Overwrite existing cases (default: skip existing)")
def build_case_skeleton(start_year: int, end_year: int, overwrite: bool):
"""
Pull SPC tornado database, filter to Oklahoma, group by date,
classify EventClass, and save skeleton HistoricalCase objects.
"""
from ok_weather_model.ingestion import SPCClient
from ok_weather_model.storage import Database
console.rule("[bold red]Building case skeleton from SPC data[/bold red]")
db = Database()
start_date = date(start_year, 1, 1)
end_date = date(end_year, 12, 31)
with SPCClient() as spc:
with console.status(f"Downloading SPC tornado data {start_year}–{end_year}..."):
skeletons = spc.build_case_skeletons(start_date, end_date)
console.print(f"[green]Built {len(skeletons)} case skeletons from SPC data[/green]")
saved = skipped = 0
for case in track(skeletons, description="Saving to database..."):
if not overwrite and db.case_exists(case.case_id):
skipped += 1
continue
db.save_case(case)
saved += 1
# ── Summary table ─────────────────────────────────────────────────────────
stats = db.get_case_statistics()
table = Table(title=f"Case Library Summary ({start_year}–{end_year})", show_lines=True)
table.add_column("Event Class", style="cyan")
table.add_column("Cases", justify="right", style="green")
table.add_column("Avg Tornadoes", justify="right")
table.add_column("Avg Completeness", justify="right")
for event_class, info in stats.items():
table.add_row(
event_class,
str(info["count"]),
f"{info['avg_tornadoes']:.1f}",
f"{info['avg_completeness']:.0%}",
)
console.print(table)
console.print(f"\nSaved: [green]{saved}[/green] Skipped (already exist): [yellow]{skipped}[/yellow]")
# ── enrich-case ──────────────────────────────────────────────────────────────
@cli.command("enrich-case")
@click.argument("case_ref") # case_id (YYYYMMDD_OK) or date (YYYY-MM-DD / YYYYMMDD)
@click.option("--force", is_flag=True, default=False,
help="Re-enrich even if sounding data already loaded")
def enrich_case(case_ref: str, force: bool):
"""
Enrich a single case with sounding and Mesonet data.
CASE_REF: case_id (e.g. 19990503_OK) or date (e.g. 1999-05-03)
"""
from ok_weather_model.storage import Database
from ok_weather_model.ingestion import SoundingClient, MesonetClient
from ok_weather_model.processing import (
compute_thermodynamic_indices,
compute_kinematic_profile,
compute_convective_temp_gap,
)
db = Database()
case_id, case_date = _resolve_case_ref(case_ref)
case = db.load_case(case_id)
if case is None:
# Create a minimal skeleton
console.print(f"[yellow]Case {case_id} not in database — creating skeleton[/yellow]")
case = HistoricalCase(
case_id=case_id,
date=case_date,
event_class=EventClass.NULL_BUST,
)
if case.sounding_data_available and not force:
console.print(f"[yellow]Case {case_id} already has sounding data. Use --force to re-enrich.[/yellow]")
return
console.rule(f"[bold]Enriching {case_id}[/bold]")
# ── 12Z OUN sounding ──────────────────────────────────────────────────────
with console.status("Fetching 12Z OUN sounding..."):
with SoundingClient() as sc:
profile = sc.get_sounding(OklahomaSoundingStation.OUN, case_date, 12)
if profile is None:
console.print(f"[red]No 12Z OUN sounding found for {case_date}[/red]")
else:
db.save_sounding(profile)
console.print(f"[green]Sounding: {len(profile.levels)} levels[/green]")
with console.status("Computing thermodynamic indices..."):
try:
indices = compute_thermodynamic_indices(profile)
kinematics = compute_kinematic_profile(profile, indices)
case.sounding_12Z = indices
case.kinematics_12Z = kinematics
case.sounding_data_available = True
console.print(
f" MLCAPE={indices.MLCAPE:.0f} J/kg "
f"MLCIN={indices.MLCIN:.0f} J/kg "
f"cap={indices.cap_strength:.1f}°C "
f"SRH0-3={kinematics.SRH_0_3km:.0f} m²/s²"
)
except Exception as exc:
console.print(f"[red]MetPy computation failed: {exc}[/red]")
logger.exception("Thermodynamic index computation failed for %s", case_id)
# ── 12Z LMN sounding (Lamont — N-S cap gradient reference) ───────────────
if case.sounding_lmn_12Z is None or force:
with console.status("Fetching 12Z LMN sounding..."):
with SoundingClient() as sc:
lmn_profile = sc.get_sounding(OklahomaSoundingStation.LMN, case_date, 12)
if lmn_profile is None:
console.print("[dim]LMN 12Z: not available[/dim]")
else:
db.save_sounding(lmn_profile)
try:
lmn_idx = compute_thermodynamic_indices(lmn_profile)
lmn_kin = compute_kinematic_profile(lmn_profile, lmn_idx)
case.sounding_lmn_12Z = lmn_idx
case.kinematics_lmn_12Z = lmn_kin
console.print(
f"[green]LMN 12Z: {len(lmn_profile.levels)} levels "
f"MLCIN={lmn_idx.MLCIN:.0f} J/kg "
f"cap={lmn_idx.cap_strength:.1f}°C[/green]"
)
except Exception as exc:
console.print(f"[yellow]LMN 12Z indices failed: {exc}[/yellow]")
logger.debug("LMN 12Z indices failed for %s: %s", case_id, exc)
# ── Mesonet full-day pull ─────────────────────────────────────────────────
with console.status("Pulling Mesonet data..."):
try:
with MesonetClient() as mc:
station_data = mc.get_historical_case_data(case_date)
for station_id, ts in station_data.items():
db.save_mesonet_timeseries(ts)
case.mesonet_data_available = len(station_data) > 0
console.print(f"[green]Mesonet: {len(station_data)} stations[/green]")
# ── Convective temp gap at 12Z, 15Z, 18Z ──────────────────────────
if case.sounding_12Z is not None:
tc = case.sounding_12Z.convective_temperature
for label, hour in [("12Z", 12), ("15Z", 15), ("18Z", 18)]:
valid_time = datetime(
case_date.year, case_date.month, case_date.day, hour, 0,
tzinfo=timezone.utc
)
# Average OUN-county surface temp at that time
from ok_weather_model.models import OklahomaCounty
try:
oun_county = OklahomaCounty.CLEVELAND # OUN is in Cleveland Co.
county_ts_list = [
ts for sid, ts in station_data.items()
if ts.county == oun_county
]
surface_state = mc.compute_county_surface_state(
oun_county, valid_time, county_ts_list
)
if surface_state:
gap = compute_convective_temp_gap(surface_state.mean_temperature, tc)
setattr(case, f"convective_temp_gap_{label}", gap)
console.print(f" {label} Tc-gap: {gap:+.1f}°F")
except Exception as exc:
logger.warning("Could not compute %s Tc-gap: %s", label, exc)
# ── Dryline detection ──────────────────────────────────────────────
with console.status("Detecting dryline..."):
try:
from ok_weather_model.processing import analyze_dryline_from_mesonet
dryline_result = analyze_dryline_from_mesonet(station_data, case_date)
case.boundaries = [
*case.boundaries,
*dryline_result["boundaries"],
]
if dryline_result["dryline_lon_18Z"] is not None:
case.dryline_longitude_18Z = dryline_result["dryline_lon_18Z"]
if dryline_result["surge_rate_mph"] is not None:
case.dryline_surge_rate_mph = dryline_result["surge_rate_mph"]
n = len(dryline_result["boundaries"])
if n:
lon = dryline_result["dryline_lon_18Z"]
surge = dryline_result["surge_rate_mph"]
lon_str = f"{lon:.1f}°W" if lon is not None else "—"
surge_str = f"{surge:+.1f} mph" if surge is not None else "—"
console.print(
f"[green]Dryline: {n} snapshot(s) detected "
f"18Z lon={lon_str} surge={surge_str}[/green]"
)
else:
console.print("[dim]Dryline: not detected[/dim]")
except Exception as exc:
console.print(f"[yellow]Dryline detection failed: {exc}[/yellow]")
logger.warning("Dryline detection failed for %s: %s", case_id, exc)
except Exception as exc:
console.print(f"[red]Mesonet pull failed: {exc}[/red]")
logger.exception("Mesonet pull failed for %s", case_id)
# ── 12Z HRRR snapshot (2016-07-15 onward) ────────────────────────────────
_hrrr_start = date(2016, 7, 15)
if case_date >= _hrrr_start and (not case.hrrr_data_available or force):
with console.status("Fetching 12Z HRRR county snapshot..."):
try:
from ok_weather_model.ingestion.hrrr_client import HRRRClient
with HRRRClient() as hc:
snap = hc.get_12z_analysis(case_date)
if snap is not None:
db.save_hrrr_snapshot(snap, case_id)
case.hrrr_data_available = True
console.print(
f"[green]HRRR 12Z: {len(snap.counties)} counties "
f"valid {snap.valid_time.strftime('%H:%M UTC')}[/green]"
)
else:
console.print("[dim]HRRR 12Z: not available[/dim]")
except Exception as exc:
console.print(f"[yellow]HRRR fetch failed: {exc}[/yellow]")
logger.debug("HRRR 12Z fetch failed for %s: %s", case_id, exc)
# ── Save enriched case ───────────────────────────────────────────────────
case.recompute_completeness()
db.save_case(case)
console.print(
f"\n[bold green]Case {case_id} saved. "
f"Completeness: {case.data_completeness_score:.0%}[/bold green]"
)
# ── enrich-all ────────────────────────────────────────────────────────────────
@cli.command("enrich-all")
@click.argument("start_year", type=int)
@click.argument("end_year", type=int)
@click.option("--force", is_flag=True, default=False,
help="Re-enrich already-enriched cases (refetches all hours)")
@click.option("--upgrade", is_flag=True, default=False,
help="Add missing 00Z/18Z/21Z to cases that already have 12Z, "
"without re-fetching hours already present")
def enrich_all(start_year: int, end_year: int, force: bool, upgrade: bool):
"""
Enrich all cases in a year range. Supports resume (skips already-enriched).
START_YEAR END_YEAR: e.g. enrich-all 1994 2023
Default: skips cases that already have sounding data.
--upgrade: backfills 00Z/18Z/21Z for cases that have 12Z but are missing
the surrounding hours (only fetches the missing ones).
--force: re-enriches everything from scratch.
"""
from ok_weather_model.storage import Database
db = Database()
start_date = date(start_year, 1, 1)
end_date = date(end_year, 12, 31)
cases = db.query_parameter_space(
{"start_date": str(start_date), "end_date": str(end_date)}
)
if not cases:
console.print(f"[yellow]No cases found for {start_year}–{end_year}. "
f"Run build-case-skeleton first.[/yellow]")
return
if force:
to_enrich = cases
mode_label = "force re-enrich"
elif upgrade:
to_enrich = [
c for c in cases
if c.sounding_data_available and (
c.sounding_00Z is None or
c.sounding_18Z is None or
c.sounding_21Z is None
)
]
mode_label = "upgrade (backfill 00Z/18Z/21Z)"
else:
to_enrich = [c for c in cases if not c.sounding_data_available]
mode_label = "enrich new"
console.print(
f"Found {len(cases)} total cases. "
f"{mode_label}: {len(to_enrich)} "
f"(skipping {len(cases) - len(to_enrich)})."
)
from ok_weather_model.ingestion import SoundingClient
from ok_weather_model.ingestion.hrrr_client import HRRRClient
from ok_weather_model.processing import compute_thermodynamic_indices, compute_kinematic_profile
_HRRR_ARCHIVE_START = date(2016, 7, 15) # reliable NOAA AWS HRRR archive start
errors = []
enriched = 0
# Named hours stored as dedicated case fields.
# 12Z is the primary pre-convective sounding.
# 00Z captures the overnight cap state.
# 18Z and 21Z bracket the initiation window and LLJ onset.
_HOUR_FIELDS = {
0: ("sounding_00Z", "kinematics_00Z"),
12: ("sounding_12Z", "kinematics_12Z"),
18: ("sounding_18Z", "kinematics_18Z"),
21: ("sounding_21Z", "kinematics_21Z"),
}
# Routine twice-daily launches are reliably available at all four stations;
# fall back to adjacent stations when OUN misses these.
# Special-hour soundings (03Z, 06Z, 09Z, 15Z) are OUN-only event launches —
# adjacent stations won't have them, so fallback would just waste requests.
_FALLBACK_HOURS = {0, 12}
with SoundingClient() as sc:
for case in track(to_enrich, description="Enriching cases..."):
try:
if upgrade and not force:
hours_needed = {
h for h, (idx_f, _) in _HOUR_FIELDS.items()
if getattr(case, idx_f) is None
}
hours_needed |= {h for h in SoundingClient.STANDARD_HOURS
if h not in _HOUR_FIELDS}
else:
hours_needed = set(SoundingClient.STANDARD_HOURS)
# Fetch OUN hours
all_profiles: dict[int, object] = {}
for hour in sorted(hours_needed):
profile = sc.get_sounding(
OklahomaSoundingStation.OUN, case.date, hour
)
if profile is not None:
all_profiles[hour] = profile
# For routine 00Z/12Z only, try adjacent stations if OUN misses
for hour in _FALLBACK_HOURS & hours_needed:
if hour not in all_profiles:
fallback = sc.get_sounding_with_fallback(
OklahomaSoundingStation.OUN, case.date, hour
)
if fallback is not None:
all_profiles[hour] = fallback
# Fetch LMN 12Z if not already stored (N-S cap gradient).
# Independent of hours_needed — backfilled on cases that may
# already have all OUN hours but predate this field.
lmn_12z_needed = case.sounding_lmn_12Z is None or force
lmn_profile = None
if lmn_12z_needed:
lmn_profile = sc.get_sounding(
OklahomaSoundingStation.LMN, case.date, 12
)
if not all_profiles and lmn_profile is None:
logger.debug("No sounding at any station or hour for %s", case.case_id)
continue
# Persist all profiles
for profile in all_profiles.values():
db.save_sounding(profile)
if lmn_profile is not None:
db.save_sounding(lmn_profile)
# Compute indices for named OUN hours
for hour, (idx_field, kin_field) in _HOUR_FIELDS.items():
if hour in all_profiles:
profile = all_profiles[hour]
indices = compute_thermodynamic_indices(profile)
kinematics = compute_kinematic_profile(profile, indices)
setattr(case, idx_field, indices)
setattr(case, kin_field, kinematics)
# Compute LMN 12Z indices
if lmn_profile is not None:
try:
lmn_idx = compute_thermodynamic_indices(lmn_profile)
lmn_kin = compute_kinematic_profile(lmn_profile, lmn_idx)
case.sounding_lmn_12Z = lmn_idx
case.kinematics_lmn_12Z = lmn_kin
except Exception as lmn_exc:
logger.debug("LMN 12Z indices failed for %s: %s", case.case_id, lmn_exc)
if case.sounding_12Z is not None:
case.sounding_data_available = True
# Fetch 12Z HRRR snapshot for 2016-07-15+ cases
hrrr_needed = (
case.date >= _HRRR_ARCHIVE_START
and (not case.hrrr_data_available or force)
)
if hrrr_needed:
try:
with HRRRClient() as hc:
snap = hc.get_12z_analysis(case.date)
if snap is not None:
db.save_hrrr_snapshot(snap, case.case_id)
case.hrrr_data_available = True
else:
logger.debug("No HRRR snapshot for %s", case.case_id)
except Exception as hrrr_exc:
logger.debug("HRRR fetch failed for %s: %s", case.case_id, hrrr_exc)
case.recompute_completeness()
db.save_case(case)
enriched += 1
except Exception as exc:
errors.append((case.case_id, str(exc)))
logger.exception("Enrichment failed for %s", case.case_id)
console.print(f"\n[bold green]Enriched: {enriched}[/bold green] "
f"No sounding: {len(to_enrich) - enriched - len(errors)} "
f"Errors: {len(errors)}")
if errors:
console.print("\n[red]Failures:[/red]")
for case_id, err in errors[:10]:
console.print(f" {case_id}: {err}")
if len(errors) > 10:
console.print(f" ... and {len(errors) - 10} more (see log)")
# ── coverage-report ───────────────────────────────────────────────────────────
@cli.command("coverage-report")
@click.argument("start_year", type=int, default=1994)
@click.argument("end_year", type=int, default=2024)
@click.option("--list-gaps", is_flag=True, default=False,
help="Print every case that has no sounding data")
def coverage_report(start_year: int, end_year: int, list_gaps: bool):
"""
Report sounding coverage gaps in the case library.
Shows per-year counts of: total cases, 12Z OUN present, LMN 12Z present,
and cases with no sounding at any station.
"""
from ok_weather_model.storage import Database
from rich.table import Table
db = Database()
cases = db.query_parameter_space(
{"start_date": f"{start_year}-01-01", "end_date": f"{end_year}-12-31"}
)
if not cases:
console.print(f"[yellow]No cases found for {start_year}–{end_year}.[/yellow]")
return
# Aggregate by year
year_stats: dict[int, dict] = {}
gap_cases: list = []
for case in cases:
yr = case.date.year
if yr not in year_stats:
year_stats[yr] = {"total": 0, "has_12z": 0, "has_lmn": 0,
"has_00z": 0, "has_18z": 0, "has_21z": 0,
"has_hrrr": 0, "no_sounding": 0}
s = year_stats[yr]
s["total"] += 1
if case.sounding_12Z is not None:
s["has_12z"] += 1
if case.sounding_lmn_12Z is not None:
s["has_lmn"] += 1
if case.sounding_00Z is not None:
s["has_00z"] += 1
if case.sounding_18Z is not None:
s["has_18z"] += 1
if case.sounding_21Z is not None:
s["has_21z"] += 1
if case.hrrr_data_available:
s["has_hrrr"] += 1
if not case.sounding_data_available:
s["no_sounding"] += 1
gap_cases.append(case)
# Summary table
tbl = Table(title=f"Data Coverage {start_year}–{end_year}")
tbl.add_column("Year", justify="right")
tbl.add_column("Cases", justify="right")
tbl.add_column("12Z OUN", justify="right")
tbl.add_column("LMN 12Z", justify="right")
tbl.add_column("00Z", justify="right")
tbl.add_column("18Z", justify="right")
tbl.add_column("21Z", justify="right")
tbl.add_column("HRRR", justify="right")
tbl.add_column("No data", justify="right", style="red")
total_cases = total_12z = total_lmn = total_00z = total_18z = total_21z = total_hrrr = total_gaps = 0
for yr in sorted(year_stats):
s = year_stats[yr]
gap_color = "red" if s["no_sounding"] > 0 else "green"
hrrr_color = "green" if s["has_hrrr"] == s["total"] else ("yellow" if s["has_hrrr"] > 0 else "dim")
tbl.add_row(
str(yr),
str(s["total"]),
str(s["has_12z"]),
str(s["has_lmn"]),
str(s["has_00z"]),
str(s["has_18z"]),
str(s["has_21z"]),
f"[{hrrr_color}]{s['has_hrrr']}[/{hrrr_color}]",
f"[{gap_color}]{s['no_sounding']}[/{gap_color}]",
)
total_cases += s["total"]
total_12z += s["has_12z"]
total_lmn += s["has_lmn"]
total_00z += s["has_00z"]
total_18z += s["has_18z"]
total_21z += s["has_21z"]
total_hrrr += s["has_hrrr"]
total_gaps += s["no_sounding"]
tbl.add_section()
tbl.add_row(
"TOTAL",
str(total_cases),
str(total_12z),
str(total_lmn),
str(total_00z),
str(total_18z),
str(total_21z),
str(total_hrrr),
f"[red]{total_gaps}[/red]" if total_gaps else "[green]0[/green]",
)
console.print(tbl)
console.print(
f"\n[bold]Coverage:[/bold] "
f"12Z OUN {total_12z/total_cases:.0%} "
f"LMN {total_lmn/total_cases:.0%} "
f"00Z {total_00z/total_cases:.0%} "
f"18Z {total_18z/total_cases:.0%} "
f"21Z {total_21z/total_cases:.0%} "
f"HRRR {total_hrrr/total_cases:.0%} "
f"Gaps {total_gaps}/{total_cases}"
)
if list_gaps and gap_cases:
console.print(f"\n[red]Cases with no sounding ({len(gap_cases)}):[/red]")
for c in gap_cases:
console.print(f" {c.case_id} {c.event_class.value}")
if not list_gaps and total_gaps:
console.print(
f"\n[dim]Run with --list-gaps to see all {total_gaps} unenriched cases.[/dim]"
)
# ── analyze-cap-behavior ──────────────────────────────────────────────────────
@cli.command("analyze-cap-behavior")
@click.argument("case_ref")
@click.option("--forcing-window-hours", default=12, type=int, show_default=True,
help="Length of the synoptic forcing window in hours from 12Z")
def analyze_cap_behavior(case_ref: str, forcing_window_hours: int):
"""
Compute full CapErosionTrajectory and classify cap_behavior for a case.
CASE_REF: case_id (e.g. 19990503_OK) or date (e.g. 1999-05-03)
"""
from ok_weather_model.storage import Database
from ok_weather_model.processing import compute_cap_erosion_budget, estimate_erosion_trajectory
from ok_weather_model.models import OklahomaCounty, CapErosionBudget
db = Database()
case_id, case_date = _resolve_case_ref(case_ref)
case = db.load_case(case_id)
if case is None:
console.print(f"[red]Case {case_id} not found. Run enrich-case first.[/red]")
return
if case.sounding_12Z is None:
console.print(f"[red]Case {case_id} has no sounding data. Run enrich-case first.[/red]")
return
console.rule(f"[bold]Cap Analysis: {case_id}[/bold]")
forcing_window_close = datetime(
case_date.year, case_date.month, case_date.day, 12, 0, tzinfo=timezone.utc
) + timedelta(hours=forcing_window_hours)
# Build budgets for OUN county (Cleveland Co.) using available data.
# First try pre-stored Mesonet data; if absent, fetch live from the API
# and cache it so subsequent runs are fast.
budgets: list[CapErosionBudget] = []
county = OklahomaCounty.CLEVELAND
from ok_weather_model.ingestion import MesonetClient
# Check whether any Mesonet data is already stored for this case
_probe_time = datetime(case_date.year, case_date.month, case_date.day, 18, 0, tzinfo=timezone.utc)
_probe = db.load_mesonet_timeseries(county, _probe_time - timedelta(minutes=15), _probe_time + timedelta(minutes=15))
mesonet_in_db = _probe is not None and bool(_probe.observations)
live_station_data: dict = {}
if not mesonet_in_db:
console.print("[yellow]No Mesonet data in database — fetching live from API...[/yellow]")
try:
with MesonetClient() as mc:
live_station_data = mc.get_historical_case_data(case_date)
if live_station_data:
for ts in live_station_data.values():
db.save_mesonet_timeseries(ts)
case.mesonet_data_available = True
console.print(f"[green]Fetched and cached Mesonet data ({len(live_station_data)} stations)[/green]")
else:
console.print("[yellow]Mesonet API returned no data for this date[/yellow]")
except Exception as exc:
console.print(f"[yellow]Live Mesonet fetch failed: {exc}[/yellow]")
logger.warning("Live Mesonet fetch failed for %s: %s", case_id, exc)
# Load the full convective day (12Z–22Z) for the county so tendency
# computation can compare across snapshots (e.g. 12Z→18Z heating rate).
day_start = datetime(case_date.year, case_date.month, case_date.day, 11, 45, tzinfo=timezone.utc)
day_end = datetime(case_date.year, case_date.month, case_date.day, 22, 15, tzinfo=timezone.utc)
county_ts_day = db.load_mesonet_timeseries(county, day_start, day_end)
with MesonetClient() as mc:
for label, hour in [("12Z", 12), ("15Z", 15), ("18Z", 18), ("21Z", 21)]:
valid_time = datetime(
case_date.year, case_date.month, case_date.day, hour, 0,
tzinfo=timezone.utc
)
# Prefer full-day DB load (provides prior snapshots for tendency);
# fall back to the in-memory live data if DB had nothing.
if county_ts_day is not None and county_ts_day.observations:
surface_state = mc.compute_county_surface_state(county, valid_time, [county_ts_day])
elif live_station_data:
county_series = [ts for ts in live_station_data.values() if ts.county == county]
surface_state = mc.compute_county_surface_state(county, valid_time, county_series)
else:
surface_state = None
if surface_state is None:
logger.debug("No surface state at %s for %s", label, case_id)
continue
try:
budget = compute_cap_erosion_budget(case.sounding_12Z, surface_state)
budgets.append(budget)
except Exception as exc:
logger.warning("Budget computation failed at %s: %s", label, exc)
if not budgets:
console.print(
"[yellow]No Mesonet surface data available — running CES sounding-only analysis[/yellow]"
)
from ok_weather_model.processing.cap_calculator import compute_ces_from_sounding
from ok_weather_model.models.enums import ErosionMechanism
valid_time_12z = datetime(
case_date.year, case_date.month, case_date.day, 12, 0, tzinfo=timezone.utc
)
sounding_raw = db.load_sounding(OklahomaSoundingStation.OUN, valid_time_12z)
surface_temp_c = (
sounding_raw.levels[0].temperature
if sounding_raw and sounding_raw.levels
else None
)
if case.sounding_12Z is None or surface_temp_c is None:
console.print(
"[red]No 12Z sounding data available — cannot compute CES. "
"Run enrich-case first.[/red]"
)
return
ces = compute_ces_from_sounding(case.sounding_12Z, surface_temp_c, case_date)
ces_table = Table(title=f"CES Sounding-Only Analysis — {case_id}", show_lines=True)
ces_table.add_column("Metric", style="cyan")
ces_table.add_column("Value", style="green")
ces_table.add_row("Event Class", case.event_class.value)
ces_table.add_row("Cap Behavior", ces["cap_behavior"].value if ces["cap_behavior"] else "—")
ces_table.add_row("12Z MLCAPE", f"{case.sounding_12Z.MLCAPE:.0f} J/kg")
ces_table.add_row("12Z MLCIN", f"{case.sounding_12Z.MLCIN:.0f} J/kg")
ces_table.add_row("12Z Cap Strength", f"{case.sounding_12Z.cap_strength:.1f}°C")
ces_table.add_row(
"Projected Erosion",
ces["cap_erosion_time"].strftime("%H:%M UTC") if ces["cap_erosion_time"] else "No erosion"
)
if ces["convective_temp_gap_12Z"] is not None:
ces_table.add_row("12Z Tc Gap", f"{ces['convective_temp_gap_12Z']:+.1f}°F")
if ces["convective_temp_gap_15Z"] is not None:
ces_table.add_row("15Z Tc Gap", f"{ces['convective_temp_gap_15Z']:+.1f}°F")
if ces["convective_temp_gap_18Z"] is not None:
ces_table.add_row("18Z Tc Gap", f"{ces['convective_temp_gap_18Z']:+.1f}°F")
console.print(ces_table)
case.cap_behavior = ces["cap_behavior"]
case.cap_erosion_mechanism = ErosionMechanism.HEATING
if ces["cap_erosion_time"] is not None:
case.cap_erosion_time = ces["cap_erosion_time"]
case.convective_temp_gap_12Z = ces["convective_temp_gap_12Z"]
case.convective_temp_gap_15Z = ces["convective_temp_gap_15Z"]
case.convective_temp_gap_18Z = ces["convective_temp_gap_18Z"]
case.recompute_completeness()
db.save_case(case)
return
trajectory = estimate_erosion_trajectory(budgets, forcing_window_close)
# ── Classify cap_behavior ─────────────────────────────────────────────────
cap_behavior = _classify_cap_behavior(trajectory, case_date)
case.cap_behavior = cap_behavior
case.cap_erosion_mechanism = trajectory.primary_mechanism
if trajectory.erosion_time:
case.cap_erosion_time = trajectory.erosion_time.time()
case.cap_erosion_county = county
db.save_case(case)
# ── Color helpers ────────────────────────────────────────────────────────
def _cape_color(v: float) -> str:
if v >= 3000: return "bright_red"
if v >= 2000: return "red"
if v >= 1000: return "yellow"
if v >= 500: return "green"
return "white"
def _cin_color(v: float) -> str:
"""Higher CIN = more red (stronger cap)."""
if v >= 200: return "bright_red"
if v >= 100: return "red"
if v >= 50: return "yellow"
if v >= 20: return "bright_yellow"
return "green"
def _cap_strength_color(v: float) -> str:
if v >= 4.0: return "bright_red"
if v >= 2.0: return "red"
if v >= 1.0: return "yellow"
return "green"
def _tc_gap_color(v: float) -> str:
"""Positive Tc gap = surface below Tc (cap intact, red). Negative = broken."""
if v >= 20: return "bright_red"
if v >= 10: return "red"
if v >= 3: return "yellow"
if v >= -3: return "bright_yellow"
return "green"
def _bust_risk_color(v: float) -> str:
if v >= 0.7: return "bright_red"
if v >= 0.5: return "red"
if v >= 0.3: return "yellow"
if v >= 0.15: return "bright_yellow"
return "green"
def _cap_behavior_color(cb) -> str:
mapping = {
"CLEAN_EROSION": "bright_green",
"EARLY_EROSION": "green",
"LATE_EROSION": "yellow",
"NO_EROSION": "red",
"BOUNDARY_FORCED": "cyan",
"RECONSTITUTED": "magenta",
}
return mapping.get(cb.value if hasattr(cb, "value") else cb, "white")
def _event_class_color(ec) -> str:
mapping = {
"SIGNIFICANT_OUTBREAK": "bright_red",
"ISOLATED_SIGNIFICANT": "red",
"MARGINAL_EVENT": "yellow",
"NULL_BUST": "bright_blue",
"ACTIVE_NULL": "blue",
}
return mapping.get(ec.value if hasattr(ec, "value") else ec, "white")
def _net_tendency_color(v: float) -> str:
if v <= -15: return "bright_green"
if v <= -5: return "green"
if v <= 0: return "bright_yellow"
return "red"
def _cin_cell_color(v: float) -> str:
if v >= 150: return "bright_red"
if v >= 75: return "red"
if v >= 30: return "yellow"
if v >= 10: return "bright_yellow"
return "green"
def _hrs_color(v) -> str:
if v is None: return "red"
if v <= 2: return "bright_green"
if v <= 6: return "green"
if v <= 12: return "yellow"
return "red"
# ── Print summary report ─────────────────────────────────────────────────
table = Table(title=f"Cap Erosion Report — {case_id}", show_lines=True)
table.add_column("Metric", style="cyan")
table.add_column("Value")
ec_color = _event_class_color(case.event_class)
cb_color = _cap_behavior_color(cap_behavior)
table.add_row("Event Class",
f"[{ec_color}]{case.event_class.value}[/{ec_color}]")
table.add_row("Cap Behavior",
f"[{cb_color}]{cap_behavior.value}[/{cb_color}]")
table.add_row("Primary Mechanism", trajectory.primary_mechanism.value)
table.add_row("Erosion Achieved",
"[bright_green]YES[/bright_green]" if trajectory.erosion_achieved
else "[red]NO[/red]")
table.add_row(
"Erosion Time",
trajectory.erosion_time.strftime("%H:%M UTC") if trajectory.erosion_time else "[dim]—[/dim]"
)
br = trajectory.bust_risk_score
br_color = _bust_risk_color(br)
table.add_row("Bust Risk Score", f"[{br_color}]{br:.2f}[/{br_color}]")
cape = case.sounding_12Z.MLCAPE
cin = case.sounding_12Z.MLCIN
cap = case.sounding_12Z.cap_strength
table.add_row("12Z MLCAPE",
f"[{_cape_color(cape)}]{cape:.0f} J/kg[/{_cape_color(cape)}]")
table.add_row("12Z MLCIN",
f"[{_cin_color(cin)}]{cin:.0f} J/kg[/{_cin_color(cin)}]")
table.add_row("12Z Cap Strength",
f"[{_cap_strength_color(cap)}]{cap:.1f}°C[/{_cap_strength_color(cap)}]")
if case.convective_temp_gap_12Z is not None:
g = case.convective_temp_gap_12Z
table.add_row("12Z Tc Gap",
f"[{_tc_gap_color(g)}]{g:+.1f}°F[/{_tc_gap_color(g)}]")
if case.convective_temp_gap_15Z is not None:
g = case.convective_temp_gap_15Z
table.add_row("15Z Tc Gap",
f"[{_tc_gap_color(g)}]{g:+.1f}°F[/{_tc_gap_color(g)}]")
if case.convective_temp_gap_18Z is not None:
g = case.convective_temp_gap_18Z
table.add_row("18Z Tc Gap",
f"[{_tc_gap_color(g)}]{g:+.1f}°F[/{_tc_gap_color(g)}]")
table.add_row("Forcing Window Close",
forcing_window_close.strftime("%H:%M UTC"))
console.print(table)
# Budget timeline
budget_table = Table(title="Cap Erosion Budget Timeline", show_lines=True)
budget_table.add_column("Time", style="cyan")
budget_table.add_column("CIN (J/kg)", justify="right")
budget_table.add_column("Heating", justify="right")
budget_table.add_column("Dynamic", justify="right")
budget_table.add_column("Net", justify="right")
budget_table.add_column("Hrs to 0", justify="right")
for b in trajectory.budget_history:
hrs = b.hours_to_erosion
cin_c = _cin_cell_color(b.current_CIN)
net_c = _net_tendency_color(b.net_tendency)
hrs_c = _hrs_color(hrs)
# Heating/dynamic: green when eroding (negative), dim when near zero
def _forcing_str(v: float) -> str:
if v <= -5: return f"[green]{v:+.1f}[/green]"
if v < 0: return f"[bright_yellow]{v:+.1f}[/bright_yellow]"
if v > 5: return f"[red]{v:+.1f}[/red]"
return f"[dim]{v:+.1f}[/dim]"
budget_table.add_row(
b.valid_time.strftime("%H:%M Z"),
f"[{cin_c}]{b.current_CIN:.0f}[/{cin_c}]",
_forcing_str(b.heating_forcing),
_forcing_str(b.dynamic_forcing),
f"[{net_c}]{b.net_tendency:+.1f}[/{net_c}]",
f"[{hrs_c}]{hrs:.1f}[/{hrs_c}]" if hrs is not None
else f"[{hrs_c}]∞[/{hrs_c}]",
)
console.print(budget_table)
# ── Shared analogue helpers ────────────────────────────────────────────────────
def _dangerous_capped_flag(
indices,
kinematics,
) -> tuple[bool, list[str]]:
"""
Return (flag, reasons) when a strong cap sits over a violent kinematic
environment — the BOUNDARY_FORCED miss pattern.
Thresholds are intentionally modest: we want early warning, not perfect
precision. A false positive on a busted severe day is far less costly than
a missed outbreak.
"""
if indices is None or indices.MLCIN < 80:
return False, []
# Skip if no meaningful CAPE — cap without moisture is not the boundary-forced pattern
if indices.MLCAPE < 500:
return False, []
reasons: list[str] = []
if kinematics:
if kinematics.SRH_0_1km > 150:
reasons.append(f"SRH 0–1km {kinematics.SRH_0_1km:.0f} m²/s²")
if kinematics.SRH_0_3km > 300:
reasons.append(f"SRH 0–3km {kinematics.SRH_0_3km:.0f} m²/s²")
if kinematics.EHI is not None and kinematics.EHI > 2.5:
reasons.append(f"EHI {kinematics.EHI:.2f}")
if kinematics.BWD_0_6km > 50:
reasons.append(f"Shear 0–6km {kinematics.BWD_0_6km:.0f} kt")
return bool(reasons), reasons
def _feature_vector(
indices,