#!/usr/bin/env python3 """The storm indices behind the first chart of /preseismic-geomagnetic-network/: hourly Dst and three-hourly Kp, 31 July to 22 August 2023. Reads GFZ's Kp (CC BY 4.0) and OMNI2 for 2023, whose Dst comes from WDC Kyoto, as data. Run: python3 -I swarma_chart_data.py Writes swarma_august_indices.csv: one row an hour. Dst stands for the hour beginning at t_utc, Kp for the three-hour block holding it. """ import csv import sys from datetime import datetime, timedelta, timezone from pathlib import Path UTC = timezone.utc START, END = datetime(2023, 7, 31, tzinfo=UTC), datetime(2023, 8, 23, tzinfo=UTC) def main(): idx, outdir = Path(sys.argv[1]), Path(sys.argv[2]) kp = {} for line in open(idx / "Kp_ap_since_1932.txt", encoding="utf-8", errors="replace"): if line.startswith("#"): continue p = line.split() if len(p) >= 10 and int(p[0]) == 2023: kp[datetime(int(p[0]), int(p[1]), int(p[2]), int(float(p[3])), tzinfo=UTC)] = p[7] dst = {} for line in open(idx / "omni2_2023.dat", encoding="ascii", errors="replace"): p = line.split() dst[datetime(int(p[0]), 1, 1, int(p[2]), tzinfo=UTC) + timedelta(days=int(p[1]) - 1)] = p[40] with open(outdir / "swarma_august_indices.csv", "w", encoding="utf-8", newline="\n") as f: w = csv.writer(f, lineterminator="\n") w.writerow(["t_utc", "dst_omni_nT", "kp_gfz"]) t = START while t < END: block = t.replace(hour=t.hour - t.hour % 3) w.writerow([t.strftime("%Y-%m-%dT%H:%M"), dst[t], kp[block]]) t += timedelta(hours=1) if __name__ == "__main__": main()