"""Refine the published KIC 8191672 (Kepler-5 b) ephemeris on the early window only. The published period, reference epoch and duration are starting points taken from the Kepler DR25 KOI table (K00018.01) as credited in the problem README. A deterministic coordinate search then varies one of the three at a time, keeping the values that give the best in-window signal-to-noise on the training light curve. The reserved late window is never read here; verify.py is what tests the prediction against it. """ import argparse import json import math import sys from pathlib import Path from statistics import median sys.dont_write_bytecode = True sys.path.insert(0, str(Path.cwd())) from verify import lightcurve, measure # Published starting values (period days, reference epoch BKJD, duration hours). SEED = (3.548465446, 122.9014315, 4.56904) def scan(rows, period, epoch, duration, axis, start, stop, steps): """Step one coordinate over a closed interval and keep the best in-window S/N.""" best = None for index in range(steps): value = start + (stop - start) * index / (steps - 1) candidate = {"period": period, "epoch": epoch, "duration": duration} candidate[axis] = value score = measure(rows, candidate["period"], candidate["epoch"], candidate["duration"]) key = (score["snr"], score["depth_ppm"], score["events"]) if best is None or key > best[0]: best = (key, candidate, score) return best[1][axis], best[2] def refine(rows, period, epoch, duration): """Alternate epoch, period and duration scans, then repeat two finer epoch passes.""" for period_span, epoch_span, duration_span in ( (0.004, 0.15, None), (0.0008, 0.02, None), (0.00016, 0.004, None), ): epoch, _ = scan(rows, period, epoch, duration, "epoch", epoch - epoch_span, epoch + epoch_span, 41) period, _ = scan(rows, period, epoch, duration, "period", period - period_span, period + period_span, 61) if duration_span is None: duration, _ = scan(rows, period, epoch, duration, "duration", max(0.75, duration - 0.8), duration + 0.8, 25) epoch, _ = scan(rows, period, epoch, duration, "epoch", epoch - 0.0016, epoch + 0.0016, 33) return period, epoch, duration def main(): parser = argparse.ArgumentParser(description=__doc__) catalog = json.loads(Path("observations/sources.json").read_text()) parser.add_argument("star", choices=catalog) parser.add_argument("--diagnostics", type=Path, help="write the training-window fit and the two half-window fits here") args = parser.parse_args() rows = lightcurve(catalog[args.star]["train"]["file"]) period, epoch, duration = refine(rows, *SEED) # Keep the reference epoch inside the training range and near its midpoint. midpoint = (rows[0][0] + rows[-1][0]) / 2 epoch += round((midpoint - epoch) / period) * period if args.diagnostics: half = median([row[0] for row in rows]) evidence = { "training": measure(rows, period, epoch, duration), "first_half": measure([row for row in rows if row[0] <= half], period, epoch, duration), "second_half": measure([row for row in rows if row[0] > half], period, epoch, duration), "epochs_differ_from_published_by_days": round(epoch - SEED[1], 6), "period_differ_from_published_by_days": round(period - SEED[0], 9), } args.diagnostics.write_text(json.dumps(evidence, indent=1) + "\n") print(json.dumps({"star": args.star, "period_days": round(period, 9), "epoch_bkjd": round(epoch, 8), "duration_hours": round(duration, 5)})) if __name__ == "__main__": main()