
#!/usr/bin/env python3
from __future__ import annotations
import argparse, json
from pathlib import Path
import pandas as pd
from rbft_lib import read_sparc_table1, read_sparc_table2, add_baryonic_phase_columns, load_config, ensure_dir

def main():
    ap = argparse.ArgumentParser(description="Parse SPARC Table1/Table2 into clean reproducible CSV files.")
    ap.add_argument("--root", default=".", help="Package root")
    args = ap.parse_args()
    root = Path(args.root)
    cfg = load_config(root)
    outdir = ensure_dir(root / "outputs" / "01_clean_sparc")

    t1 = read_sparc_table1(root / "data" / "raw" / "Table1.mrt.txt")
    t2 = read_sparc_table2(root / "data" / "raw" / "Table2.mrt.txt")

    t1.to_csv(outdir / "sparc_table1_clean.csv", index=False)
    t2.to_csv(outdir / "sparc_table2_clean.csv", index=False)

    merged = t2.merge(t1, on="Galaxy", how="left", suffixes=("", "_T1"))
    merged = add_baryonic_phase_columns(
        merged,
        ml_star=cfg["stellar_ML_36"],
        sigma_floor=cfg["phase_sigma_floor"]
    )
    merged.to_csv(outdir / "sparc_merged_clean.csv", index=False)

    summary = t2.groupby("Galaxy").agg(
        N_points=("R","size"),
        R_first=("R","min"),
        R_last=("R","max"),
        Vobs_median=("Vobs","median"),
        eVobs_median=("e_Vobs","median")
    ).reset_index().merge(t1, on="Galaxy", how="left")
    summary.to_csv(outdir / "sparc_galaxy_summary_clean.csv", index=False)

    manifest = {
        "Table1_rows": int(len(t1)),
        "Table1_galaxies": int(t1["Galaxy"].nunique()),
        "Table2_rows": int(len(t2)),
        "Table2_galaxies": int(t2["Galaxy"].nunique()),
        "merged_rows": int(len(merged)),
        "merged_galaxies": int(merged["Galaxy"].nunique()),
        "overlap_galaxies": int(len(set(t1["Galaxy"]) & set(t2["Galaxy"]))),
        "valid_y_rows": int(merged["y_lnPhi_ML050_unsigned"].notna().sum()),
        "notes": "Clean SPARC parse using skiprows=98 for Table1 and skiprows=25 for Table2."
    }
    (outdir / "sparc_clean_manifest.json").write_text(json.dumps(manifest, indent=2))
    print(json.dumps(manifest, indent=2))

if __name__ == "__main__":
    main()
    import sys as _sys, os as _os
    _sys.stdout.flush()
    _sys.stderr.flush()
    _os._exit(0)
