Kepler stellar observations
Use real Kepler observations to measure when a star dims.

| 1 | """Refine the published KIC 8191672 (Kepler-5 b) ephemeris on the early window only. |
| 2 | |
| 3 | The published period, reference epoch and duration are starting points taken from the |
| 4 | Kepler DR25 KOI table (K00018.01) as credited in the problem README. A deterministic |
| 5 | coordinate search then varies one of the three at a time, keeping the values that give |
| 6 | the best in-window signal-to-noise on the training light curve. The reserved late window |
| 7 | is never read here; verify.py is what tests the prediction against it. |
| 8 | """ |
| 9 | |
| 10 | import argparse |
| 11 | import json |
| 12 | import math |
| 13 | import sys |
| 14 | from pathlib import Path |
| 15 | from statistics import median |
| 16 | |
| 17 | sys.dont_write_bytecode = True |
| 18 | sys.path.insert(0, str(Path.cwd())) |
| 19 | from verify import lightcurve, measure |
| 20 | |
| 21 | # Published starting values (period days, reference epoch BKJD, duration hours). |
| 22 | SEED = (3.548465446, 122.9014315, 4.56904) |
| 23 | |
| 24 | |
| 25 | def scan(rows, period, epoch, duration, axis, start, stop, steps): |
| 26 | """Step one coordinate over a closed interval and keep the best in-window S/N.""" |
| 27 | best = None |
| 28 | for index in range(steps): |
| 29 | value = start + (stop - start) * index / (steps - 1) |
| 30 | candidate = {"period": period, "epoch": epoch, "duration": duration} |
| 31 | candidate[axis] = value |
| 32 | score = measure(rows, candidate["period"], candidate["epoch"], candidate["duration"]) |
| 33 | key = (score["snr"], score["depth_ppm"], score["events"]) |
| 34 | if best is None or key > best[0]: |
| 35 | best = (key, candidate, score) |
| 36 | return best[1][axis], best[2] |
| 37 | |
| 38 | |
| 39 | def refine(rows, period, epoch, duration): |
| 40 | """Alternate epoch, period and duration scans, then repeat two finer epoch passes.""" |
| 41 | for period_span, epoch_span, duration_span in ( |
| 42 | (0.004, 0.15, None), |
| 43 | (0.0008, 0.02, None), |
| 44 | (0.00016, 0.004, None), |
| 45 | ): |
| 46 | epoch, _ = scan(rows, period, epoch, duration, "epoch", |
| 47 | epoch - epoch_span, epoch + epoch_span, 41) |
| 48 | period, _ = scan(rows, period, epoch, duration, "period", |
| 49 | period - period_span, period + period_span, 61) |
| 50 | if duration_span is None: |
| 51 | duration, _ = scan(rows, period, epoch, duration, "duration", |
| 52 | max(0.75, duration - 0.8), duration + 0.8, 25) |
| 53 | epoch, _ = scan(rows, period, epoch, duration, "epoch", |
| 54 | epoch - 0.0016, epoch + 0.0016, 33) |
| 55 | return period, epoch, duration |
| 56 | |
| 57 | |
| 58 | def main(): |
| 59 | parser = argparse.ArgumentParser(description=__doc__) |
| 60 | catalog = json.loads(Path("observations/sources.json").read_text()) |
| 61 | parser.add_argument("star", choices=catalog) |
| 62 | parser.add_argument("--diagnostics", type=Path, |
| 63 | help="write the training-window fit and the two half-window fits here") |
| 64 | args = parser.parse_args() |
| 65 | |
| 66 | rows = lightcurve(catalog[args.star]["train"]["file"]) |
| 67 | period, epoch, duration = refine(rows, *SEED) |
| 68 | # Keep the reference epoch inside the training range and near its midpoint. |
| 69 | midpoint = (rows[0][0] + rows[-1][0]) / 2 |
| 70 | epoch += round((midpoint - epoch) / period) * period |
| 71 | |
| 72 | if args.diagnostics: |
| 73 | half = median([row[0] for row in rows]) |
| 74 | evidence = { |
| 75 | "training": measure(rows, period, epoch, duration), |
| 76 | "first_half": measure([row for row in rows if row[0] <= half], period, epoch, duration), |
| 77 | "second_half": measure([row for row in rows if row[0] > half], period, epoch, duration), |
| 78 | "epochs_differ_from_published_by_days": round(epoch - SEED[1], 6), |
| 79 | "period_differ_from_published_by_days": round(period - SEED[0], 9), |
| 80 | } |
| 81 | args.diagnostics.write_text(json.dumps(evidence, indent=1) + "\n") |
| 82 | |
| 83 | print(json.dumps({"star": args.star, "period_days": round(period, 9), |
| 84 | "epoch_bkjd": round(epoch, 8), "duration_hours": round(duration, 5)})) |
| 85 | |
| 86 | |
| 87 | if __name__ == "__main__": |
| 88 | main() |