"""Python 3 standard library. Run beside orbital-elements.csv; J2000 shape model, not dated ephemeris."""
from pathlib import Path
import csv,json,math
HERE=Path(__file__).resolve().parent
rows=[]
for row in csv.DictReader((HERE/'orbital-elements.csv').open(encoding='utf-8')):
    a=float(row['a_AU']);e=float(row['eccentricity'])
    near=a*(1-e);far=a*(1+e)
    ratio=(far/near)**2
    rows.append({'body':row['body'],'a_AU':a,'eccentricity':e,'perihelion_AU':near,'aphelion_AU':far,'near_flux_at_a_units':1/(1-e)**2,'far_flux_at_a_units':1/(1+e)**2,'near_far_flux_ratio':ratio,'increase_from_far_percent':100*(ratio-1)})
    assert math.isclose(ratio,((1+e)/(1-e))**2,rel_tol=1e-13)
assert math.isclose(((1+0)/(1-0))**2,1)
with (HERE/'sunlight-results.csv').open('w',newline='',encoding='utf-8') as f:
    w=csv.DictWriter(f,fieldnames=rows[0].keys());w.writeheader();w.writerows(rows)
(HERE/'orbit-results.json').write_text(json.dumps({'model':'Fixed J2000 eccentricity; isotropic steady solar output; irradiance normal to rays above atmosphere. No surface climate or time averaging.','source':'https://ssd.jpl.nasa.gov/planets/approx_pos.html','rows':rows,'uncertainty':'No confidence interval available from this approximate-element calculation; numerical digits are not ephemeris accuracy.'},indent=2),encoding='utf-8')
print(json.dumps(rows,indent=2))
