#!/usr/bin/env python3 """Check our rebuilt conjunction rule against the conjunctions SSGEOS lists for its 182 earthquakes. python3 -I validate_conjunctions.py Standard library only. Reads SSGEOS's listing (earthquake times are SSGEOS's own, from EMSC; no catalogue is used), and for each earthquake compares the sets of three bodies it lists with the sets our rule finds within 2° in the days it scanned: the earthquake's UTC day and the day before and after, or two days either side where the listing says "(+/- 2)". """ import datetime as dt import json import re import sys # one entry (031, Costa Rica) gives no magnitude, so it is optional here HEAD = re.compile(r"(\d{3}) (\d{4}-\d{2}-\d{2}), (\d{1,2})[:\-](\d{2})[:\-](\d{2}) (?:M ?(\d\.\d) )?(.+?)(?:\s{2,}|$)") TRIP = re.compile(r"^\s*([A-Z][a-z]+)-([A-Z][a-z]+)-([A-Z][a-z]+)\s+(\d{4}-\d{2}-\d{2}),") def parse_listing(path): quakes, cur = [], None for line in open(path, encoding="utf-8").read().splitlines(): m = HEAD.search(line) if m: cur = {"n": m.group(1), "date": m.group(2), "time": f"{int(m.group(3)):02d}:{m.group(4)}:{m.group(5)}", "mag": float(m.group(6)) if m.group(6) else None, "place": m.group(7).strip(), "pm2": False, "listed": []} quakes.append(cur) continue if cur is None: continue if "+/-" in line: cur["pm2"] = True t = TRIP.match(line) if t: cur["listed"].append({"triple": [t.group(1), t.group(2), t.group(3)], "date": t.group(4)}) return quakes def main(): quakes = parse_listing(sys.argv[1]) daily = json.load(open(sys.argv[2], encoding="utf-8")) bodies, triples, days = daily["bodies"], daily["triples"], daily["days"] key_of = {frozenset(t): i for i, t in enumerate(triples)} rows, both, theirs_only, ours_only = [], 0, 0, 0 agree_conv = 0 for q in quakes: d0 = dt.date.fromisoformat(q["date"]) span = 2 if q["pm2"] else 1 window = [(d0 + dt.timedelta(days=k)).isoformat() for k in range(-span, span + 1)] mins = [min(days[d][i] for d in window) for i in range(len(triples))] ours = {i for i, v in enumerate(mins) if v < 2.0} listed = {key_of[frozenset(x["triple"])] for x in q["listed"] if frozenset(x["triple"]) in key_of} unknown = [x["triple"] for x in q["listed"] if frozenset(x["triple"]) not in key_of] both += len(ours & listed) theirs_only += len(listed - ours) ours_only += len(ours - listed) conv_theirs, conv_ours = len(listed) >= 2, len(ours) >= 2 agree_conv += conv_theirs == conv_ours rows.append({"n": q["n"], "date": q["date"], "time": q["time"], "mag": q["mag"], "place": q["place"], "scan_days": span, "listed": sorted("-".join(triples[i]) for i in listed), "ours": sorted("-".join(triples[i]) for i in ours), "missed_by_us": {"-".join(triples[i]): round(mins[i], 3) for i in listed - ours}, "unknown_names": unknown, "convergence_listed": conv_theirs, "convergence_ours": conv_ours}) res = {"earthquakes": len(quakes), "listed_with_two_or_more": sum(r["convergence_listed"] for r in rows), "listed_with_none": sum(1 for r in rows if not r["listed"]), "ours_with_two_or_more": sum(r["convergence_ours"] for r in rows), "pairs_in_both": both, "listed_not_ours": theirs_only, "ours_not_listed": ours_only, "convergence_agrees": agree_conv, "rows": rows} with open(sys.argv[3], "w", encoding="utf-8", newline="\n") as fh: json.dump(res, fh, ensure_ascii=False, indent=1) sys.stdout.reconfigure(encoding="utf-8") print({k: v for k, v in res.items() if k != "rows"}) for r in rows[:400]: if r["missed_by_us"] or r["unknown_names"]: print(r["n"], r["date"], "missed:", r["missed_by_us"], "unknown:", r["unknown_names"]) if __name__ == "__main__": main()