#!/usr/bin/env python """Cross-check the SWPC X-flare list for 2017 on against NOAA's GOES-R science flare summaries. Reads the three NetCDF files as data. Writes ../out/goesr_crosscheck.json. Run: python -I 01b_check_goesr.py """ import csv import datetime as dt import glob import json import os import sys import netCDF4 import numpy as np sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import rr_core as rr # noqa: E402 rows = [] info = {} for f in sorted(glob.glob(os.path.join(rr.DL, "ncei-goesr-flsum", "*.nc"))): ds = netCDF4.Dataset(f) vn = list(ds.variables) info[os.path.basename(f)] = dict(variables=vn, title=getattr(ds, "title", ""), summary=getattr(ds, "summary", "")[:400]) t = ds.variables["time"] times = netCDF4.num2date(t[:], t.units, only_use_cftime_datetimes=False, only_use_python_datetimes=True) status = ds.variables["status"] st = status[:] if status.dtype is str else netCDF4.chartostring(status[:]) cls_var = ds.variables["flare_class"] cls = cls_var[:] if cls_var.dtype is str else netCDF4.chartostring(cls_var[:]) sat = os.path.basename(f).split("_")[2] for ti, s, c in zip(times, st, cls): s = str(s).strip() c = str(c).strip() if "PEAK" in s.upper() and c.startswith("X"): rows.append((ti.date(), ti.strftime("%H%M"), float(c[1:]), sat)) ds.close() rows.sort() sw = [r for r in rr.read_csv(os.path.join(rr.OUT, "events_noaa_x.csv")) if r["date"] >= "2017-06-29"] sw_dates = {} for r in sw: sw_dates.setdefault(r["date"], []).append(float(r["reported_class_X"])) sci = {} for d, hm, m, sat in rows: sci.setdefault(d.isoformat(), {}).setdefault(sat, []).append(m) # dates where a science summary has X1+ and SWPC has none, and the other way round only_sci = {d: v for d, v in sci.items() if d not in sw_dates} only_sw = {d: sw_dates[d] for d in sw_dates if d not in sci} print("science X rows:", len(rows), "by satellite:", {s: sum(1 for r in rows if r[3] == s) for s in {r[3] for r in rows}}) print("dates with X in the science summaries but not in the SWPC reports:", json.dumps(only_sci, indent=0)[:3000]) print("dates with X in the SWPC reports but not in the science summaries:", only_sw) rr.save_json( os.path.join(rr.OUT, "goesr_crosscheck.json"), dict(info=info, n_rows=len(rows), only_in_science=only_sci, only_in_swpc=only_sw, science=sci), )