"""Reproduce the 2026-10-17 Th-232 evaluation-level spacing diagnostic.
Run: python compare.py (requires numpy and scipy; download adjacent data files).
Ordinary KS thresholds/p-values are nominal, not calibrated for correlated,
mean-normalized spacings, fitted unfolding, or evaluated resonance selection.
"""
import hashlib
import gzip
import json
from pathlib import Path
import numpy as np
from scipy.stats import ks_2samp
from scipy.special import erf

ROOT = Path(__file__).resolve().parent


def endf_float(field):
    text = field.strip()
    index = max(text.rfind("+"), text.rfind("-"))
    return float(text[:index] + "e" + text[index:]) if index > 0 else float(text)


def extract(path):
    raw = gzip.decompress(path.read_bytes()) if path.suffix == ".gz" else path.read_bytes()
    lines = [line for line in raw.decode().splitlines()
             if line[70:72].strip() == "2" and line[72:75].strip() == "151"]
    fields = lambda line: [endf_float(line[i:i+11]) for i in range(0, 66, 11)]
    region, control = fields(lines[2]), fields(lines[3])
    if region[2:4] != [1, 3] or region[1] != 4000 or control[0] != 0:
        raise ValueError("Expected Th-232 first resolved Reich-Moore region, target spin 0")
    cursor, selected = 4, []
    for _ in range(int(control[4])):
        header = fields(lines[cursor])
        count = int(header[5])
        if int(header[4]) != 6 * count:
            raise ValueError("Unexpected Reich-Moore LIST length")
        rows = [fields(line) for line in lines[cursor+1:cursor+1+count]]
        if len(rows) != count:
            raise ValueError("Truncated resonance block")
        if int(header[2]) == 0:
            selected.extend(row[0] for row in rows
                            if abs(row[1]) == 0.5 and 0 < row[0] <= 4000)
        cursor += 1 + count
    return np.sort(selected)


def spacing(unfolded):
    values = np.diff(unfolded)
    if len(values) < 2 or np.any(values <= 0):
        raise ValueError("Need strictly increasing levels")
    return values / values.mean()


def ks(values, cdf):
    x = np.sort(values)
    upper = np.arange(1, len(x)+1) / len(x)
    return float(np.max(np.maximum(abs(upper-cdf(x)), abs(upper-1/len(x)-cdf(x)))))


def metrics(values):
    grid = np.linspace(0, 6, 6001)
    integral = np.cumsum(32/np.pi**2 * grid**2 * np.exp(-4*grid**2/np.pi)) * (grid[1]-grid[0])
    gue_exact = lambda s: erf(2*s/np.sqrt(np.pi)) - 4*s/np.pi*np.exp(-4*s*s/np.pi)
    return {
        "spacings": len(values), "variance": float(values.var()),
        "small_gap_fraction": float(np.mean(values < 0.25)),
        "nominal_5pct_threshold": float(1.36 / np.sqrt(len(values))),
        "ks_goe": ks(values, lambda s: 1-np.exp(-np.pi*s*s/4)),
        "ks_gue_previous_grid_method": ks(values, lambda s: np.interp(s, grid, integral)),
        "ks_gue_exact_cdf_sensitivity": ks(values, gue_exact),
        "ks_poisson": ks(values, lambda s: 1-np.exp(-s)),
    }


def run(root=ROOT):
    source = root / "n-090_Th_232.endf.gz"
    energies = extract(source)
    fit = np.polyfit(energies, np.arange(1, len(energies)+1), 1)
    a = spacing(np.polyval(fit, energies))
    zeros = np.loadtxt(root / "zeros-first2000.txt")[:len(energies)]
    b = spacing(zeros/(2*np.pi)*np.log(zeros/(2*np.pi*np.e)))
    comparison = ks_2samp(a, b, method="exact")
    return {
        "run_date": "2026-10-17", "evaluation": "ENDF/B-VII.1, MAT9040, MF2/MT151, LRF3",
        "source_url": "https://www.nndc.bnl.gov/endf-b7.1/zips/ENDF-B-VII.1-neutrons.zip",
        "source_member": "neutrons/n-090_Th_232.endf",
        "source_sha256": hashlib.sha256(gzip.decompress(source.read_bytes())).hexdigest(),
        "zero_source_url": "https://www-users.cse.umn.edu/~odlyzko/zeta_tables/zeros1",
        "zero_copy_url": "https://raw.githubusercontent.com/kwnorton000-hash/spectral-verification/main/data/zeta_zeros_first2000.txt",
        "selection": "First resolved region, l=0, abs(J)=0.5, 0<E<=4000 eV; no p-wave or unresolved averages",
        "levels": len(energies), "energy_min_eV": float(energies[0]), "energy_max_eV": float(energies[-1]),
        "fitted_spacing_eV": float(1/fit[0]), "mean_gap_eV": float(np.diff(energies).mean()),
        "zero_min": float(zeros[0]), "zero_max": float(zeros[-1]),
        "thorium": metrics(a), "zeros": metrics(b),
        "two_sample_ks": float(comparison.statistic), "nominal_two_sample_p": float(comparison.pvalue),
        "caveat": "Nominal iid KS diagnostics only; correlated spacings, fitted unfolding, normalization and evaluation selection are not calibrated. No independent validation or predictive mapping.",
    }


if __name__ == "__main__":
    print(json.dumps(run(), indent=2))