#!/usr/bin/env python3 """Offline AMR compilation. USD exact Decimal inputs; no live data or imputation.""" from pathlib import Path from decimal import Decimal, ROUND_HALF_UP import csv,json,hashlib P=Path(__file__).resolve().parent; S=P/'sources'; D=P/'data';D.mkdir(exist_ok=True) def read(p):return list(csv.DictReader(p.open())) O=read(S/'audited-and-supplemental-inputs.csv'); V={r['id']:Decimal(r['value']) for r in O}; R={r['id']:r for r in O} def n(k):return V[k] def fmt(x):return format(x,'f') def save(name,rows): assert rows and all(set(x)==set(rows[0]) for x in rows) with (D/name).open('w',newline='') as f: w=csv.DictWriter(f,fieldnames=list(rows[0]));w.writeheader();w.writerows(rows) def percent(x,y):return fmt((x/y*100).quantize(Decimal('.01'),ROUND_HALF_UP)) # Independent source identities: the individual components must reconcile to totals. assert n('tx_fair')+n('tx_yield')+n('tx_lend')==n('tx_invest') assert n('tx_begin')+n('tx_deposit')+n('tx_invest')-n('tx_payout')-n('tx_opex')==n('tx_end') assert n('tx_nonexpend')+n('tx_expend')+n('tx_capital')==n('tx_end') assert n('nm_fair')+n('nm_yield')==n('nm_invest') assert n('nm_invest')+n('nm_grazing')==n('nm_totalrevenue') assert n('nm_begin')+n('nm_deposit')+n('nm_totalrevenue')-n('nm_admin')-n('nm_payout')==n('nm_end') assert n('nm_oilgas')+n('nm_otherroy')==n('nm_royalty') assert sum(n(k) for k in ['nm_royalty','nm_rental','nm_reapportion','nm_lease','nm_lease_interest','nm_fees','nm_interest','nm_other'])==n('nm_netrevenue') assert n('nm_deposit')-n('nm_royalty')==Decimal(1) assert n('nm_admin_rental')+n('nm_admin_other')==n('nm_admin_nonroyalty') assert n('nm_admin_nonroyalty')+n('nm_admin_royalty')==n('nm_admin_all') assert n('nm_admin_school_rental')