"""First pass of stage 5: find where the samples' Kp sequence sits in GFZ's Kp series.""" import csv import sys sys.stdout.reconfigure(encoding="utf-8", errors="replace") samples = list(csv.DictReader(open(sys.argv[1], encoding="utf-8", newline=""))) gfz = sys.argv[2] series = [] for line in open(gfz, encoding="utf-8", errors="replace"): if line.startswith("#"): continue p = line.split() if len(p) < 10: continue y, m, d, h = int(p[0]), int(p[1]), int(p[2]), float(p[3]) if y < 2013: continue series.append(((y, m, d, h), float(p[7]))) print("GFZ rows", len(series), series[0], series[-1]) # Run-length sequence of distinct Kp values over consecutive samples, in order. seq = [] for r in samples: vals = [float(x) for x in r["kp_values"].split("|")] # Order within a sample is unknown from the sorted list; use only single-valued samples for the sequence. if len(vals) == 1: if not seq or seq[-1] != vals[0]: seq.append(vals[0]) print("distinct single-valued Kp run sequence length", len(seq)) print(seq[:40]) def rle(values): out = [] for v in values: if not out or abs(out[-1] - v) > 1e-6: out.append(v) return out # Search: the sample run sequence should be a subsequence of the 3-hourly RLE around the true date. kp = [v for _, v in series] stamps = [s for s, _ in series] target = seq[:12] hits = [] for i in range(len(kp) - 40): win = rle(kp[i : i + 40]) if all(abs(a - b) < 0.01 for a, b in zip(win[: len(target)], target)): hits.append(i) print("hits on the first 12 runs:", [stamps[i] for i in hits][:30]) target = seq for L in (20, 30, 40, 60, len(seq)): t = seq[:L] hits2 = [] for i in hits: win = rle(kp[i : i + 3 * L]) if len(win) >= L and all(abs(a - b) < 0.01 for a, b in zip(win[:L], t)): hits2.append(i) print("runs", L, "hits", [stamps[i] for i in hits2][:10])