#!/usr/bin/env python3 """Download the public inputs of our analysis for factchecked.science/may-2024-dc-ac-circuit/ and check them. Usage: python3 -I fetch_inputs.py DATA_FOLDER [inputs.json] Standard library only (Python 3.9 or later). Reads inputs.json (beside this script by default): for each input its URL, the path under DATA_FOLDER, and the SHA-256 of the copy we used on 2026-11-15. Fetches each one under the plain client name factchecked-research/1.0, waits a second between requests, and says whether it matches. NMDB's pages embed the time their query took, so for them only the data rows are compared. Live services (Kyoto's provisional Dst and SYM-H, NMDB, the Iowa Environmental Mesonet) can revise their data, so a mismatch there means "check", not "wrong". Files already present and matching are not fetched again. The two zips of the authors' data are extracted, with a check that no member leaves its folder. The preprint PDF isn't fetched: SSRN refused our client, and the copy we read was downloaded by the site's owner. Get it yourself from https://doi.org/10.2139/ssrn.7493598 and pass it to e_figures.py and descriptive_post_hoc.py. About 25 MB is downloaded; the authors' Purulia files take about 220 MB once extracted. Terms: NMDB data are free for non-commercial use, with NMDB's acknowledgement and the station PIs credited; Kyoto's Dst and SYM-H are for scientific, non-commercial use with acknowledgement; GFZ's Kp is CC BY 4.0; the authors' Mendeley data are CC BY 4.0; NOAA's GOES data, NASA's OMNI and the Mesonet's METARs are public. See the page's section 10. """ import hashlib import json import os import re import sys import time import urllib.request import zipfile UA = "factchecked-research/1.0" ROW = re.compile(r"^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2});\s*([-+\d.eE]+|null|NaN)?\s*$") def sha_file(path): h = hashlib.sha256() with open(path, "rb") as f: for block in iter(lambda: f.read(1 << 20), b""): h.update(block) return h.hexdigest() def sha_rows(path): with open(path, encoding="utf-8", errors="replace") as f: lines = [l.strip() for l in f if ROW.match(l.strip())] return hashlib.sha256(("\n".join(lines) + "\n").encode()).hexdigest(), len(lines) def matches(entry, path): if not os.path.exists(path): return False if entry["check"] == "data-rows": return sha_rows(path)[0] == entry["rows_sha256"] return sha_file(path) == entry["sha256"] def fetch(url, path): os.makedirs(os.path.dirname(path), exist_ok=True) req = urllib.request.Request(url, headers={"User-Agent": UA}) with urllib.request.urlopen(req, timeout=300) as r, open(path + ".part", "wb") as f: while True: block = r.read(1 << 20) if not block: break f.write(block) os.replace(path + ".part", path) def extract(zpath, dest): base = os.path.realpath(dest) with zipfile.ZipFile(zpath) as z: for m in z.infolist(): target = os.path.realpath(os.path.join(dest, m.filename)) if not (target == base or target.startswith(base + os.sep)): sys.exit(f"refusing to extract {m.filename!r}: it leaves {dest}") z.extractall(dest) def main(): data = sys.argv[1] spec = sys.argv[2] if len(sys.argv) > 2 else os.path.join(os.path.dirname(os.path.abspath(__file__)), "inputs.json") inputs = json.load(open(spec, encoding="utf-8"))["inputs"] bad = 0 for e in inputs: path = os.path.join(data, *e["path"].split("/")) if matches(e, path): status = "same as ours (already here)" else: try: fetch(e["url"], path) except Exception as ex: # report and go on; the analysis scripts say what is missing print(f"FAILED {e['path']}: {ex}") bad += 1 continue time.sleep(1) status = "same as ours" if matches(e, path) else "DIFFERENT from ours: check before use" bad += status.startswith("DIFFERENT") print(f"{status:40s} {e['path']}") if "extract_to" in e: dest = os.path.join(data, *e["extract_to"].split("/")) if not os.path.isdir(dest): extract(path, dest) print(f"{'extracted':40s} {e['extract_to']}") print(f"\n{len(inputs)} inputs, {bad} to check. Next: python3 -I ab_storm_cutoffs.py {data} OUT and the other scripts (section 10).") if __name__ == "__main__": main()