#!/usr/bin/env python3 """Our analysis: the paper's solar-wind timeline (sect. 3.1) against NASA's OMNI 1-min file for May 2024. Usage: python3 -I omni_timeline.py OMNI_FILE OUT_TXT Reads omni_min202405.asc as data with the fixed-width layout of the OMNI high-resolution files. Rules: code/RULES_before_running.txt, "Third test". Standard library only. """ import datetime as dt import sys sys.stdout.reconfigure(encoding="utf-8", errors="replace") WIDTHS = [4, 4, 3, 3, 3, 3, 4, 4, 4, 7, 7, 6, 7] + [8] * 8 + [8] * 4 + [7, 9, 6, 7, 7, 6] + [8] * 6 + [6] * 7 + [7, 5] NAMES = ["year", "doy", "hour", "minute", "imf_id", "sw_id", "n_imf", "n_plasma", "pct_interp", "timeshift", "rms_timeshift", "rms_phase", "dt_obs", "B", "Bx", "By_gse", "Bz_gse", "By", "Bz", "rms_sd_b", "rms_sd_vec", "V", "Vx", "Vy", "Vz", "Np", "T", "P", "E", "beta", "MA", "x", "y", "z", "bx_nose", "by_nose", "bz_nose", "AE", "AL", "AU", "SYMD", "SYMH", "ASYD", "ASYH", "PCN", "MSM"] FILL = {"B": 9999.99, "Bz": 9999.99, "By": 9999.99, "V": 99999.9, "Np": 999.99, "P": 99.99, "SYMH": 99999, "AE": 99999, "E": 999.99} def read(path): rows = [] for line in open(path, encoding="ascii", errors="replace"): vals, pos = {}, 0 for w, n in zip(WIDTHS, NAMES): vals[n] = line[pos:pos + w].strip() pos += w t = dt.datetime(int(vals["year"]), 1, 1) + dt.timedelta(days=int(vals["doy"]) - 1, hours=int(vals["hour"]), minutes=int(vals["minute"])) rec = {"t": t} for n, fill in FILL.items(): try: x = float(vals[n]) except ValueError: x = None rec[n] = None if x is None or x >= fill * 0.999 else x rows.append(rec) return rows def main(): rows = read(sys.argv[1]) start = dt.datetime(2024, 5, 10, 12) stop = dt.datetime(2024, 5, 12, 12) w = [r for r in rows if start <= r["t"] < stop] out = [] def say(s=""): out.append(s) print(s) say("OMNI 1-min file; times are at the bow-shock nose. Our analysis.") say(f"records in 12:00 May 10 to 12:00 May 12: {len(w)}") for key, label in (("V", "speed km/s"), ("B", "|B| nT"), ("P", "flow pressure nPa"), ("Np", "density /cc")): good = [r for r in w if r[key] is not None] m = max(good, key=lambda r: r[key]) say(f"max {label}: {m[key]:.1f} at {m['t']:%m-%d %H:%M}; minutes with data {len(good)} of {len(w)}") good = [r for r in w if r["Bz"] is not None] m = min(good, key=lambda r: r["Bz"]) say(f"min Bz GSM: {m['Bz']:.1f} nT at {m['t']:%m-%d %H:%M}") good = [r for r in w if r["SYMH"] is not None] m = min(good, key=lambda r: r["SYMH"]) say(f"min SYM-H: {m['SYMH']:.0f} nT at {m['t']:%m-%d %H:%M}") m = max([r for r in good if r["t"] < dt.datetime(2024, 5, 10, 20)], key=lambda r: r["SYMH"]) say(f"max SYM-H before 20:00 May 10: {m['SYMH']:.0f} nT at {m['t']:%m-%d %H:%M}") good = [r for r in w if r["AE"] is not None] m = max(good, key=lambda r: r["AE"]) say(f"max AE: {m['AE']:.0f} nT at {m['t']:%m-%d %H:%M}") say("") m = max([r for r in w if r["V"] is not None and r["t"] < dt.datetime(2024, 5, 11, 3)], key=lambda r: r["V"]) say(f"max speed before 03:00 May 11: {m['V']:.1f} km/s at {m['t']:%m-%d %H:%M}") m = max([r for r in w if r["P"] is not None and r["t"] < dt.datetime(2024, 5, 11)], key=lambda r: r["P"]) say(f"max flow pressure on May 10: {m['P']:.1f} nPa at {m['t']:%m-%d %H:%M}") sheath = sorted(r["Np"] for r in w if r["Np"] is not None and dt.datetime(2024, 5, 10, 17, 5) <= r["t"] <= dt.datetime(2024, 5, 10, 23)) say(f"density from 17:05 to 23:00 May 10: median {sheath[len(sheath) // 2]:.1f} /cc, peak {sheath[-1]:.1f} /cc, {len(sheath)} minutes with data") say("") say("Minutes around the first shock (16:55-17:20) and the second (22:05-22:45):") say("time |B| By Bz V Np P SYM-H") def show(a, b): for r in w: if a <= r["t"] <= b: f = lambda k, d=1: " ---" if r[k] is None else f"{r[k]:6.{d}f}" say(f"{r['t']:%H:%M} {f('B')} {f('By')} {f('Bz')} {f('V', 0)} {f('Np')} {f('P')} {f('SYMH', 0)}") show(dt.datetime(2024, 5, 10, 16, 55), dt.datetime(2024, 5, 10, 17, 20)) say("...") show(dt.datetime(2024, 5, 10, 22, 5), dt.datetime(2024, 5, 10, 22, 45)) say("") t0 = dt.datetime(2024, 5, 10, 17, 5) for hours in (24, 12, 6): z = t0 + dt.timedelta(hours=hours) vals = [r["Bz"] for r in w if t0 <= r["t"] < z and r["Bz"] is not None] neg = sum(b < 0 for b in vals) say(f"Bz<0 share from 17:05 May 10 over {hours} h: {100 * neg / len(vals):.1f}% of {len(vals)} minutes with data (of {hours * 60})") say("") say("Hourly mean Bz and By from 17:00 May 10 (nT), with the share of Bz<0 minutes:") h = dt.datetime(2024, 5, 10, 17) while h < dt.datetime(2024, 5, 11, 18): z = h + dt.timedelta(hours=1) bz = [r["Bz"] for r in w if h <= r["t"] < z and r["Bz"] is not None] by = [r["By"] for r in w if h <= r["t"] < z and r["By"] is not None] if bz: say(f"{h:%m-%d %H}h Bz {sum(bz) / len(bz):7.1f} By {sum(by) / len(by):7.1f} Bz<0 {100 * sum(b < 0 for b in bz) / len(bz):5.1f}% n={len(bz)}") else: say(f"{h:%m-%d %H}h no data") h = z say("") say("Northward spells of 10 minutes or longer from 17:05 May 10 for 24 h (Bz>0, gaps ignored):") run = [] for r in w: if not (t0 <= r["t"] < t0 + dt.timedelta(hours=24)) or r["Bz"] is None: continue if r["Bz"] > 0: run.append(r) else: if len(run) >= 10: say(f" {run[0]['t']:%m-%d %H:%M} to {run[-1]['t']:%H:%M}: {len(run)} min, peak Bz {max(x['Bz'] for x in run):.1f}") run = [] if len(run) >= 10: say(f" {run[0]['t']:%m-%d %H:%M} to {run[-1]['t']:%H:%M}: {len(run)} min, peak Bz {max(x['Bz'] for x in run):.1f}") with open(sys.argv[2], "w", encoding="utf-8", newline="\n") as f: f.write("\n".join(out) + "\n") if __name__ == "__main__": main()