#!/usr/bin/env python3 """Rule K: He II 4686 against H beta, P Cygni and line widths in Aydi et al.'s published spectra of days 45, 49 and 68. Reads their online_material zip as data, and hosts no spectrum. Usage: python -I spectra_check.py """ import json import math import os import sys import zipfile import numpy as np C = 299792.458 HE2, HB = 4685.7, 4861.33 FILES = {"45": "online_material/2022-03-10.txt", "49": "online_material/2022-03-15_smoothed.txt", "68": "online_material/2022-04-03_smoothed.txt"} def load(z, name): rows = [] with z.open(name) as fh: for raw in fh: parts = raw.decode("latin-1").replace(",", " ").split() if len(parts) < 2: continue try: rows.append((float(parts[0]), float(parts[1]))) except ValueError: continue a = np.array(rows) return a[np.argsort(a[:, 0])] def vel(w, rest): return (w - rest) / rest * C def window(w, f, rest, v1, v2): v = vel(w, rest) m = (v >= v1) & (v <= v2) return w[m], f[m] def line_flux(w, f, rest, vin, vc1, vc2): """Flux over +/-vin km/s above a line through the mean of the continuum windows vc1..vc2 on each side.""" wl, fl = window(w, f, rest, -vc2, -vc1) wr, fr = window(w, f, rest, vc1, vc2) if wl.size < 3 or wr.size < 3: return None x1, y1, x2, y2 = wl.mean(), fl.mean(), wr.mean(), fr.mean() slope = (y2 - y1) / (x2 - x1) cont = lambda x: y1 + slope * (x - x1) wi, fi = window(w, f, rest, -vin, vin) d = np.gradient(wi) flux = float(np.sum((fi - cont(wi)) * d)) noise = float(np.std(np.concatenate([fl - cont(wl), fr - cont(wr)]))) err = noise * math.sqrt(float(np.sum(d**2))) return {"flux": flux, "err": err, "cont_at_line": float(cont(rest)), "noise_per_pixel": noise, "n": int(wi.size), "cont_fn": cont} def fwhm(w, f, rest, cont, vmax=1500.0): wi, fi = window(w, f, rest, -vmax, vmax) above = fi - cont(wi) i = int(np.argmax(above)) half = above[i] / 2 v = vel(wi, rest) lo = i while lo > 0 and above[lo] > half: lo -= 1 hi = i while hi < above.size - 1 and above[hi] > half: hi += 1 return float(v[hi] - v[lo]), float(v[i]), float(above[i]) def main(): zpath, out = sys.argv[1:3] res = {"note": "Rule K. Our measurement on Aydi et al.'s spectra as they posted them; no spectrum is hosted.", "epochs": {}} with zipfile.ZipFile(zpath) as z: for day, name in FILES.items(): a = load(z, name) w, f = a[:, 0], a[:, 1] e = {"file": name, "n_points": int(w.size), "wavelength_range": [float(w.min()), float(w.max())]} he = line_flux(w, f, HE2, 1000, 1000, 1400) hb = line_flux(w, f, HB, 1000, 1000, 1400) he6 = line_flux(w, f, HE2, 600, 700, 1000) hb6 = line_flux(w, f, HB, 600, 700, 1000) hbw = line_flux(w, f, HB, 2500, 2500, 3000) r = he["flux"] / hb["flux"] err = abs(r) * math.hypot(he["err"] / he["flux"], hb["err"] / hb["flux"]) e["HeII_flux_1000"] = he["flux"] e["Hbeta_flux_1000"] = hb["flux"] e["ratio_1000"] = r e["ratio_1000_err"] = err e["ratio_600"] = he6["flux"] / hb6["flux"] e["ratio_to_Hbeta_with_wings"] = he["flux"] / hbw["flux"] e["dereddened_ratio_1000"] = {str(ebv): r * 10 ** (0.4 * 0.16 * ebv) for ebv in (0.5, 0.9, 1.13)} # P Cygni in H beta wi, fi = window(w, f, HB, -2500, -500) cont = hb["cont_fn"](wi) depth = (cont - fi) / hb["noise_per_pixel"] i = int(np.argmax(depth)) e["Hbeta_absorption"] = { "v_at_minimum_km_s": float(vel(wi[i], HB)), "flux_over_continuum": float(fi[i] / cont[i]), "depth_in_noise": float(depth[i]), "below_3_sigma": bool(depth[i] > 3), } fh, vh, ph = fwhm(w, f, HB, hb["cont_fn"]) e["Hbeta_FWHM_km_s"] = fh e["Hbeta_peak_velocity_km_s"] = vh fh2, vh2, ph2 = fwhm(w, f, HE2, he["cont_fn"]) e["HeII_FWHM_km_s"] = fh2 e["HeII_peak_velocity_km_s"] = vh2 e["peak_over_continuum_HeII"] = float(ph2 / he["cont_at_line"]) if he["cont_at_line"] else None e["peak_over_continuum_Hbeta"] = float(ph / hb["cont_at_line"]) if hb["cont_at_line"] else None res["epochs"][day] = e with open(os.path.join(out, "spectra_check.json"), "w", encoding="utf-8", newline="\n") as fh: json.dump(res, fh, indent=1) print(json.dumps(res, indent=0)[:3000]) if __name__ == "__main__": main()