# Independent spot-check by the coordinator of the nova verifier's TESS finding. Own code; numpy and scipy only. import io, math, re, sys, zipfile, json import numpy as np from scipy.signal import lombscargle def cards(block): out = {} for i in range(0, len(block), 80): c = block[i:i+80].decode("ascii", "replace") if c.startswith("END"): return out, True if c[8:10] == "= ": k = c[:8].strip(); v = c[10:].split("/")[0].strip() if v.startswith("'"): v = c[10:].split("'")[1].strip() else: try: v = float(v) if ("." in v or "E" in v) else int(v) except ValueError: pass out[k] = v return out, False def hdus(b): pos = 0; res = [] while pos < len(b): h = {}; while True: hh, end = cards(b[pos:pos+2880]); h.update(hh); pos += 2880 if end: break n = abs(h.get("BITPIX", 8))//8 for i in range(1, h.get("NAXIS", 0)+1): n *= h[f"NAXIS{i}"] if h.get("NAXIS", 0) == 0: n = 0 n += h.get("PCOUNT", 0) res.append((h, pos, n)) pos += (n + 2879)//2880*2880 return res def load(path): z = zipfile.ZipFile(path); name = [n for n in z.namelist() if n.endswith(".fits")][0] b = z.read(name); hs = hdus(b); h, pos, n = hs[1] # column layout fields = []; off = 0; codes = {"D":8,"E":4,"J":4,"I":2,"A":1} for i in range(1, h["TFIELDS"]+1): m = re.match(r"(\d*)([A-Z])", h[f"TFORM{i}"]); rep = int(m.group(1) or 1); c = m.group(2) fields.append((h[f"TTYPE{i}"], off, rep, c, h.get(f"TDIM{i}"))); off += rep*codes[c] nrow, width = h["NAXIS2"], h["NAXIS1"] raw = np.frombuffer(b, dtype=np.uint8, count=nrow*width, offset=pos).reshape(nrow, width) col = {} for name_, o, rep, c, dim in fields: if name_ in ("TIME", "FLUX", "QUALITY"): dt = {"D":">f8","E":">f4","J":">i4"}[c] arr = np.frombuffer(raw[:, o:o+rep*codes[c]].tobytes(), dtype=dt).astype(float) if rep > 1: sh = tuple(int(x) for x in re.findall(r"\d+", dim))[::-1] arr = arr.reshape((nrow,)+sh) col[name_] = arr return h, hs[2][0], col def aperture(flux, cx, cy, r, ri=2.5, ro=3.5): ny, nx = flux.shape[1:]; yy, xx = np.mgrid[:ny, :nx] d = np.hypot(xx-cx, yy-cy); a = d <= r; s = (d >= ri) & (d <= ro) sky = np.nanmedian(flux[:, s], axis=1) return np.nansum(flux[:, a] - sky[:, None], axis=1) def highpass(t, y, win=3.0): # running median per orbit gap, simple out = y.copy() for i in range(len(t)): m = np.abs(t - t[i]) <= win/2 out[i] = y[i] - np.nanmedian(y[m]) return out def ls(t, y, periods): w = 2*np.pi/periods; y0 = y - np.mean(y) p = lombscargle(t, y0, w, normalize=False) return np.sqrt(4*p/len(t)) def sinfit(t, y, P, harmonics=1): A = [np.ones_like(t)] for k in range(1, harmonics+1): A += [np.cos(2*np.pi*k*t/P), np.sin(2*np.pi*k*t/P)] A = np.vstack(A).T; c, *_ = np.linalg.lstsq(A, y, rcond=None) return c, A @ c path = sys.argv[1]; dra_as, ddec_as = float(sys.argv[2]), float(sys.argv[3]) h1, h2, col = load(path) t, q, F = col["TIME"], col["QUALITY"], col["FLUX"] good = (q == 0) & np.isfinite(t) & np.isfinite(F).all(axis=(1, 2)) t, F = t[good], F[good] print("sector", h1.get("SECTOR"), "frames", len(t), "span", round(t[0], 2), round(t[-1], 2), "shape", F.shape[1:], "RA_OBJ", h2.get("RA_OBJ"), "DEC_OBJ", h2.get("DEC_OBJ")) cd = np.array([[h2.get("CD1_1", 0), h2.get("CD1_2", 0)], [h2.get("CD2_1", 0), h2.get("CD2_2", 0)]]) if not cd.any(): cd = np.array([[h2.get("PC1_1", 1)*h2.get("CDELT1", 0), h2.get("PC1_2", 0)*h2.get("CDELT1", 0)], [h2.get("PC2_1", 0)*h2.get("CDELT2", 0), h2.get("PC2_2", 1)*h2.get("CDELT2", 0)]]) print("CD", cd.tolist(), "CRPIX", h2.get("CRPIX1"), h2.get("CRPIX2"), "CRVAL", h2.get("CRVAL1"), h2.get("CRVAL2")) # requested position's pixel by the linear WCS (0-based), and the star's offset in pixels ra0, de0 = h2.get("RA_OBJ"), h2.get("DEC_OBJ") inv = np.linalg.inv(cd) def pix(ra, dec): d = np.array([(ra - h2["CRVAL1"])*math.cos(math.radians(dec)), dec - h2["CRVAL2"]]) dp = inv @ d return h2["CRPIX1"] - 1 + dp[0], h2["CRPIX2"] - 1 + dp[1] nx, ny = pix(ra0, de0) star = (ra0 + dra_as/3600/math.cos(math.radians(de0)), de0 + ddec_as/3600) sx, sy = pix(*star) print("nova pixel (linear WCS)", round(nx, 2), round(ny, 2), "| star pixel", round(sx, 2), round(sy, 2), "| separation px", round(math.hypot(sx-nx, sy-ny), 2)) lc_nova = aperture(F, nx, ny, 1.5); lc_star = aperture(F, sx, sy, 1.5) per = 1/np.linspace(1/12.0, 1/0.1, 40000) for name, lc in (("nova aperture", lc_nova), ("star aperture", lc_star)): y = highpass(t, lc, 6.0) amp = ls(t, y, per) top = [] a = amp.copy() for _ in range(7): i = int(np.argmax(a)); top.append((round(float(per[i]), 4), round(float(a[i]), 2))) a[np.abs(1/per - 1/per[i]) < 2/(t[-1]-t[0])] = 0 m = (per > 1.2) & (per < 1.7) print(name, "top peaks (P d, amp e/s):", top) i = np.argmax(np.where(m, amp, 0)); print(" best 1.2-1.7 d:", round(float(per[i]), 4), round(float(amp[i]), 2), "| amp at 1.437:", round(float(amp[np.argmin(abs(per-1.437))]), 2), "| at 1.42365:", round(float(amp[np.argmin(abs(per-1.42365))]), 2)) # pixel maps at 1.42365 d, at 1.437 d and at 0.268 d; centroid of the amplitude's peak for P in (5.6946/4, 1.437, 0.268): ny_, nx_ = F.shape[1:]; M = np.zeros((ny_, nx_)) bg = np.nanmedian(F, axis=(1, 2)) for j in range(ny_): for i in range(nx_): y = highpass(t[::1], F[:, j, i]-bg, 3.0) if False else F[:, j, i]-bg y = y - np.polyval(np.polyfit(t-t.mean(), y, 5), t-t.mean()) c, _ = sinfit(t, y, P); M[j, i] = math.hypot(c[1], c[2]) jj, ii = np.unravel_index(np.argmax(M), M.shape) print(f"map P={P:.5f}: strongest pixel (x={ii}, y={jj}) amp {M[jj, ii]:.2f}; distance to nova {math.hypot(ii-nx, jj-ny):.2f} px, to star {math.hypot(ii-sx, jj-sy):.2f} px; amp at nova pixel {M[int(round(ny)), int(round(nx))]:.2f}") # remove the star's harmonics from the nova aperture and look again y = highpass(t, lc_nova, 6.0) c, fit = sinfit(t, y, 5.6946, harmonics=30) res = y - fit amp = ls(t, res, per); m = (per > 1.2) & (per < 1.7); i = np.argmax(np.where(m, amp, 0)) print("after removing 30 harmonics of 5.6946 d: best 1.2-1.7 d", round(float(per[i]), 4), round(float(amp[i]), 2), "| median amp in band", round(float(np.median(amp[m])), 2), "| amp at 1.437", round(float(amp[np.argmin(abs(per-1.437))]), 2)) top = int(np.argmax(amp)); print(" highest overall", round(float(per[top]), 4), round(float(amp[top]), 2))