"""Readers for GFZ definitive Kp (3-hourly) and the international quiet/disturbed days (data only).""" import datetime as dt, re, os import sys ROOT = os.path.abspath(sys.argv[1]) if len(sys.argv) > 1 else os.path.dirname(os.path.dirname(os.path.abspath(__file__))) GFZ = os.path.join(ROOT, 'downloads', 'gfz-kp') # hosted copy: the root folder is the first argument def read_kp(path=os.path.join(GFZ, 'Kp_ap_since_1932.txt'), start=dt.datetime(2012, 1, 1), end=dt.datetime(2019, 12, 31)): """Returns sorted list of (datetime of bin start, Kp float, definitive flag).""" out = [] with open(path, encoding='utf-8') as fh: for line in fh: if line.startswith('#') or not line.strip(): continue p = line.split() y, m, d = int(p[0]), int(p[1]), int(p[2]); hh = float(p[3]) t = dt.datetime(y, m, d, int(hh)) if t < start or t > end: continue out.append((t, float(p[7]), int(p[9]))) return out MON = {m: i for i, m in enumerate(['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'], 1)} def read_qd(years=range(2012, 2020)): """Returns dict date -> (rank 'Q1'..'Q10', flag '' / 'A' / 'K').""" q = {} for y in years: with open(os.path.join(GFZ, 'QD_days%d.txt' % y), encoding='utf-8') as fh: for line in fh: m = re.match(r'^\s*([A-Z][a-z]{2})\s+(\d{4})\s+(.*)$', line) if not m or m.group(1) not in MON: continue mon, yr = MON[m.group(1)], int(m.group(2)) toks = re.findall(r'(\d+)([AK*]?)', m.group(3)) for r, (dd, fl) in enumerate(toks[:10], 1): q[dt.date(yr, mon, int(dd))] = ('Q%d' % r, fl) return q