"""Independent polar surface and positive GL41 exposure check across hard UI extremes."""
from pathlib import Path
import json,numpy as np
from rotation_reference import flux_series
rows=json.loads(Path('exposure-injections.json').read_text());nodes,weights=np.polynomial.legendre.leggauss(41)
maximum=np.zeros(2)
for row in rows:
    settings=row['settings'];times=row['time']+nodes*row['exposureDays']/2
    flux,_=flux_series(times,equatorial_period=settings['rotationDays'],inclination_degrees=settings['spinInclinationDegrees'],latitude_degrees=settings['spotLatitudeDegrees'],shear=settings['differentialRotationShear'],lifetime_days=settings.get('spotEvolutionSigmaDays',np.inf),peak_day=30,longitudes=(np.deg2rad(settings['spotLongitudeDegrees']),),order=(96,480))
    reference=weights@flux/2
    maximum=np.maximum(maximum,abs(np.array([row['fluxGL3'],row['fluxGL9']])-reference))
assert maximum[1]<1e-7,maximum
report={'status':'PASS96 hard exposure states with independent polar GL41 reference','maximum_absolute_flux_error_GL3_GL9':maximum.tolist(),'selected_forward_rule':'GL9 positive exposure quadrature','scope':'One-day maximal exposure, four/forty-day equators, polar/high-latitude geometry, zero/max shear, persistent/half-day/three-day envelopes. GL3 is retained as a rejected low-order comparison.'}
Path('results/exposure-reference-results.json').write_text(json.dumps(report,indent=2)+'\n')
print(json.dumps(report,indent=2))
