"""Write the crossing lists used downstream: R-C (primary, post hoc) and R-A (pre-registered). usage: py -3.12 -I make_lists.py Needs numpy. """ import datetime as dt import json import os import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import numpy as np # noqa: E402 import common as C # noqa: E402 D = dt.date dl, outdir = sys.argv[1], sys.argv[2] dates, gse, gsm = C.read_omni_daily(os.path.join(dl, 'omni', 'omni_01_av.dat'), 1999, 2024) periods = {'P1': (D(2000, 8, 31), D(2007, 4, 30)), 'P1yrs': (D(2000, 1, 1), D(2007, 12, 31)), 'P2': (D(2010, 1, 1), D(2023, 12, 31))} base = {'P1': 'P1yrs', 'P1yrs': 'P1yrs', 'P2': 'P2'} summary = {} for label, period in periods.items(): _, bb = C.subset(dates, gse, *periods[base[label]]) thr = 0.5 * np.nanmean(np.abs(bb)) dd, b = C.subset(dates, gse, *period) for rule, fn in (('RC', lambda: C.detect_crossings_rc(dd, b, thr)), ('RD', lambda: C.detect_crossings_rc(dd, b, thr, after_start=1)), ('RA', lambda: C.detect_crossings(dd, b, thr, rule='A', win='W1'))): cr = fn() el = C.eligible(cr, dd, 40) g = np.diff([c['index'] for c in cr]) summary[f'{label}_{rule}'] = { 'n': len(cr), 'T-A': sum(c['direction'] == 'T-A' for c in cr), 'A-T': sum(c['direction'] == 'A-T' for c in cr), 'eligible40': len(el), 'eligible40_T-A': sum(c['direction'] == 'T-A' for c in el), 'eligible40_A-T': sum(c['direction'] == 'A-T' for c in el), 'gap_mean': round(float(g.mean()), 2), 'gap_se': round(float(g.std(ddof=1) / np.sqrt(len(g))), 2), 'gap_sd': round(float(g.std(ddof=1)), 2), 'gap_median': float(np.median(g)), 'gap_min': int(g.min()), 'gap_max': int(g.max())} with open(os.path.join(outdir, f'crossings_{label}_{rule}.csv'), 'w', encoding='utf-8', newline='\n') as f: f.write('date,direction,abs_mean_By_before_nT,abs_mean_By_after_nT\n') for c in cr: f.write(f"{c['date']},{c['direction']},{c['before']:.3f},{c['after']:.3f}\n") if label == 'P2' and rule == 'RC': per_year = {} for c in el: per_year[c['date'].year] = per_year.get(c['date'].year, 0) + 1 summary['P2_RC_eligible_per_year'] = per_year months = {} for c in el: key = (c['date'].month, c['direction']) months[f'{key[0]:02d}_{key[1]}'] = months.get(f'{key[0]:02d}_{key[1]}', 0) + 1 summary['P2_RC_eligible_by_month_direction'] = dict(sorted(months.items())) # summer counts for R-C for label, (sm, em), lab2 in (('P2', ((3, 15), (9, 15)), 'USA'), ('P2', ((4, 1), (10, 1)), 'Canada'), ('P1', ((4, 1), (10, 1)), 'UK')): rows = C.read_crossings_csv(os.path.join(outdir, f'crossings_{label}_RC.csv')) dd = C.daterange(*periods[label]) first, last = dd[40], dd[-41] for margin in (40, 0): s0 = dt.date(2001, *sm) + dt.timedelta(days=margin) e0 = dt.date(2001, *em) - dt.timedelta(days=margin) sel = [r for r in rows if first <= r[0] <= last and C.in_season(r[0], (s0.month, s0.day), (e0.month, e0.day))] summary[f'summer_{lab2}_margin{margin}'] = {'n': len(sel), 'T-A': sum(r[1] == 'T-A' for r in sel), 'A-T': sum(r[1] == 'A-T' for r in sel)} with open(os.path.join(outdir, 'crossing_lists_summary.json'), 'w', encoding='utf-8', newline='\n') as f: json.dump(summary, f, indent=1) print(json.dumps(summary, indent=1))