"""
PRE-FLIGHT POWER FORECAST for the DFD sky-map proof (HARD RULE: no SN-lensing run without a >=3sigma forecast).
Test: SN Hubble residual delta-mu vs FOREGROUND convergence kappa_fg. DFD predicts slope b>0 (objects look
FARTHER through foreground mass, the optical screen); LCDM (cosmological constant) predicts b~0 / slightly
negative magnification (-2.17). Pass = |b_DFD - b_LCDM|/sigma_b >= 3 in the SAME units.
DECISIVE FINDING: the 648 Pantheon+ SNe in hand ALREADY give 6 sigma IF kappa is a TRUE convergence map; the
galaxy-density PROXY dilutes it to <1 sigma. So the bottleneck is the MAP, not the SNe.
"""
import numpy as np
d=np.load('boss_data/kappa_fg_dataset.npz')
dmu,k,err,z,ra,dec=d['dmu'],d['kappa_fg'],d['err'],d['z'],d['ra'],d['dec']
N=len(dmu); w=1.0/err**2
kbar=np.average(k,weights=w); dbar=np.average(dmu,weights=w)
Sxx=np.sum(w*(k-kbar)**2); b=np.sum(w*(k-kbar)*(dmu-dbar))/Sxx
sb=np.sqrt(1.0/Sxx)*np.sqrt(np.sum(w*(dmu-dbar-b*(k-kbar))**2)/(N-2))
bD,bL=10.0,-2.17                                   # DFD(+) vs LCDM(-) physical slope (delta-mu per unit TRUE kappa)
print(f"N_SNe={N}  sigma_b(measured, proxy units)={sb:.2f}  measured b={b:+.2f} ({b/sb:+.2f}s)")
print(f"DFD vs LCDM physical separation = {bD-bL:.1f}")
for t,lab in [(1.0,'TRUE convergence map'),(0.19,'galaxy proxy ~0.19x'),(0.085,'r-calibrated 0.085x')]:
    print(f"  {lab:22s}: forecast = {(bD-bL)*t/sb:4.2f} sigma  {'>= 3 PASS' if (bD-bL)*t/sb>=3 else '< 3 underpowered'}")
print(f"\nSN footprint: RA[{ra.min():.0f},{ra.max():.0f}] Dec[{dec.min():.0f},{dec.max():.0f}] -- "
      f"{np.sum(dec>0)} north, {np.sum(dec<0)} equatorial, {np.sum(dec<-20)} deep-south(DES).")
print("VERDICT: SNe are SUFFICIENT (6 sigma with true kappa). Proxy underpowered (<1 sigma). Need a REAL")
print("foreground weak-lensing convergence map over the SN footprint to run the decisive 3-6 sigma test.")
