#!/usr/bin/env python3
"""Draw the spot-check sample of RULES_spotcheck.txt: seven storms at random, plus the storms whose NS50 row never
recovers within 168 h, plus one storm from the 2-hourly era if none is in the sample yet.
Usage: python3 -I draw_spotcheck.py
Reads the note's storm_inventory.csv and recovery_times.csv from /downloads/s0-note-002-drive/ and writes
/results/spotcheck_sample.txt. On November 6, 2026 it gave IPK001, IPK007, IPK011, IPK012, IPK018, IPK031,
IPK050, IPK071, IPK080 and IPK084.
"""
import csv
import os
import random
import sys
def main():
base = sys.argv[1]
d = os.path.join(base, "downloads", "s0-note-002-drive")
with open(os.path.join(d, "storm_inventory.csv"), encoding="utf-8", newline="") as f:
inv = list(csv.DictReader(f))
with open(os.path.join(d, "recovery_times.csv"), encoding="utf-8", newline="") as f:
rec = list(csv.DictReader(f))
ids = sorted(r["storm_id"] for r in inv)
drawn = random.Random(20261106).sample(ids, 7)
never = [r["storm_id"] for r in rec if r["band"] == "NS50" and r["censor_reason"] == "no_recovery_within_168h"]
t0 = {r["storm_id"]: r["t0_utc"] for r in inv}
sample = list(dict.fromkeys(drawn + never))
if not any(t0[x] < "2014-10-18" for x in sample):
early = [x for x in ids if t0[x] < "2014-10-18"]
sample.append(random.Random(20261106 + 1).choice(early))
os.makedirs(os.path.join(base, "results"), exist_ok=True)
with open(os.path.join(base, "results", "spotcheck_sample.txt"), "w", encoding="utf-8", newline="\n") as f:
f.write("\n".join(sorted(sample)) + "\n")
print("spot-check sample:", ", ".join(sorted(sample)))
if __name__ == "__main__":
main()