#!/usr/bin/env python3
"""Extract only factual numerical measurements from Mills2016v1 ED Table1.

Optional original extraction needs pypdf. No article prose, figures or author code
are redistributed. The everyday reproduction reads the frozen small CSV.
"""
from pathlib import Path
import argparse
import csv
import hashlib
import json
import re

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--pdf", type=Path, required=True)
parser.add_argument("--output-directory", type=Path, required=True)
arguments = parser.parse_args()
source_hash = "ee22071732db64c776a333b99e3e487c7e4ec3ca246cb52c1a1ed5c00f2b8115"
assert hashlib.sha256(arguments.pdf.read_bytes()).hexdigest() == source_hash
from pypdf import PdfReader

text = PdfReader(arguments.pdf).pages[29].extract_text().replace("\u2212", "-")
header = re.compile(
    r"Kepler-223([bcde]):\s*P\s*=\s*([0-9.]+) days,\s*T0\s*-\s*2454900 \(BJD\)\s*=\s*([0-9.]+)"
)
rows = []
ephemerides = {}
planet = None
for line in text.splitlines():
    match = header.search(line)
    if match:
        planet = match[1]
        ephemerides[planet] = {
            "periodDays": float(match[2]),
            "zeroTransitBjdOffset": float(match[3]),
        }
        continue
    values = re.fullmatch(
        r"([0-9.]+)\s+(-?[0-9.]+)\s+(-?[0-9.]+)\s+(-?[0-9.]+)\s+(-?[0-9.]+)\s+(-?[0-9.]+)",
        line.strip(),
    )
    if values and planet:
        rows.append(
            [planet, 1 + sum(row[0] == planet for row in rows)] + list(values.groups())
        )
assert len(rows) == 44 and len(ephemerides) == 4
arguments.output_directory.mkdir(parents=True, exist_ok=True)
with (arguments.output_directory / "kepler223-quarterly-ttv.csv").open("w") as output:
    writer = csv.writer(output, lineterminator="\n")
    writer.writerow(
        [
            "planet",
            "common_quarter_index",
            "mean_bjd_minus_2454900",
            "lower_three_delta_days",
            "lower_one_delta_days",
            "best_ttv_days",
            "upper_one_delta_days",
            "upper_three_delta_days",
        ]
    )
    writer.writerows(rows)
metadata = {
    "kind": "Published measured quarterly transit-time shifts, not synthetic data or posterior draws",
    "source": "Mills et al., arXiv1612.07376v1, Extended Data Table1, PDF page30/index29",
    "sourceUrl": "https://arxiv.org/pdf/1612.07376v1",
    "sourceBytes": arguments.pdf.stat().st_size,
    "sourceSha256": source_hash,
    "timeConvention": "Published BJD minus2454900; paper does not explicitly name the BJD dynamical scale in this table. No UTC/TDB conversion is invented.",
    "ephemerides": ephemerides,
    "uncertaintyConvention": "Signed offsets from best transit shift at delta-chi-square1 and9; asymmetric profile intervals, not independent joint-posterior draws",
    "quarterMatching": "Rows are matched by chronological order across the same11 quarterly bins; mean observing times differ by planet",
    "rights": "Only factual numerical measurements and independently authored analysis are redistributed, with citation. The arXiv non-exclusive paper license is not presented as a license to redistribute its prose, figures or author code.",
}
(arguments.output_directory / "kepler223-source.json").write_text(
    json.dumps(metadata, indent=2) + "\n"
)
print("Extracted44 measured rows and4 original ephemerides")
