#!/usr/bin/env python3 """Reproduce AMR's historical water calculations from the accompanying CSVs. Uses only Python's standard library. --self-test proves meaningful defect gates fire. Source endpoint snapshots below are the values printed in the primary article main text. """ from pathlib import Path from decimal import Decimal as D import csv,json,sys,copy R=Path(__file__).resolve().parent P=R/'data' if (R/'data').is_dir() else R T=D('0.000051') def rows(name): with (P/name).open(newline='') as f:return list(csv.DictReader(f)) def close(a,b,tol=T): a,b=D(str(a)),D(str(b)) if not a.is_finite() or abs(a-b)>tol:raise AssertionError((str(a),str(b))) def pct(a,b):return (D(str(b))/D(str(a))-1)*100 def check(data): inj=data['injection-endpoints.csv'];fp=data['first-year-fp-endpoints.csv'] # These five expected tuples pin the published source windows and input values. expected_inj=[('Marcellus region',2011,2016,23400,27950),('Permian oil case',2011,2016,4900,42500)] expected_fp=[('Eagle Ford gas',2011,2015,1340,20700),('Eagle Ford oil',2011,2015,2400,16900),('Niobrara',2011,2016,1800,2300)] assert len(inj)==len(expected_inj) and len(fp)==len(expected_fp) for rr,expected,kind in [(inj,expected_inj,'injection'),(fp,expected_fp,'fp')]: for r,(case,start,end,a,b) in zip(rr,expected): assert r['Source case']==case if kind=='injection':sc,ec='Start year','End year';av,bv='Start median injection (m3 per well)','End median injection (m3 per well)' else:sc,ec='Start first-production cohort','End first-production cohort';av,bv='Start year-1 FP water (m3 per well)','End year-1 FP water (m3 per well)' assert int(r[sc])==start and int(r[ec])==end;close(r[av],a,0);close(r[bv],b,0) close(r['AMR absolute change (m3 per well)'],b-a);close(r['AMR change from printed endpoints (%)'],pct(a,b)) headline=data['headline-reconciliation.csv'];expected=[(20,23400,27950,2016),(770,4900,42500,2016),(610,2400,16900,2015),(1440,1340,20700,2015)] for r,(published,a,b,end) in zip(headline,expected): close(r['Published source increase (%)'],published,0);assert int(r['End year'])==end close(r['AMR recomputation from printed endpoints (%)'],pct(a,b));close(r['AMR minus published (percentage points)'],pct(a,b)-published) lateral=data['lateral-normalization.csv'] for r,case,a,b in zip(lateral,['Gas-producing wells','Oil-producing wells'],['4.3','3.9'],['29.0','19.0']): assert r['Permian source case']==case;assert (int(r['Start year']),int(r['End year']))==(2011,2016) close(r['Start injection intensity (m3 per m lateral)'],a,0);close(r['End injection intensity (m3 per m lateral)'],b,0);close(r['AMR endpoint multiple'],D(b)/D(a));close(r['AMR change (%)'],pct(a,b)) energy=data['energy-normalization.csv'][0];assert energy['Source case']=='Permian oil';close(energy['Start water-use intensity (L per GJ)'],11,0);close(energy['End water-use intensity (L per GJ)'],28,0);close(energy['AMR endpoint multiple'],D(28)/11);close(energy['AMR change (%)'],pct(11,28));assert 'first 12 months' in energy['Energy window'] for r,v in zip(data['finding-inputs.csv'],[pct(4900,42500),pct(1340,20700),pct('3.9','19.0'),pct(11,28)]):close(r['AMR result'],v) cohorts=data['cohort-alignment.csv'];assert cohorts[0]['Grouping clock']=='Spud year';assert cohorts[1]['Grouping clock']=='Year of first production, then month since first production' assert 'first 12' in cohorts[1]['Published statistic construction'] coverage=data['coverage-rules.csv'];assert coverage[0]['Specified period or threshold']=='Below 0.5% of well-count denominator';assert coverage[1]['Specified period or threshold']=='2016' return {'permian_injection_change_percent':str(pct(4900,42500)),'marcellus_injection_change_percent':str(pct(23400,27950)),'eagle_ford_gas_first_year_FP_change_percent':str(pct(1340,20700)),'eagle_ford_oil_first_year_FP_change_percent':str(pct(2400,16900)),'niobrara_first_year_FP_change_percent':str(pct(1800,2300)),'permian_gas_lateral_normalized_change_percent':str(pct('4.3','29.0')),'permian_oil_lateral_normalized_change_percent':str(pct('3.9','19.0')),'permian_oil_energy_normalized_change_percent':str(pct(11,28)),'source_windows':'Eagle Ford FP ends2015; injection and Niobrara cases end2016','source_values':'Five printed case tuples and three normalized endpoint pairs match pinned primary facts'} files=['injection-endpoints.csv','first-year-fp-endpoints.csv','headline-reconciliation.csv','lateral-normalization.csv','energy-normalization.csv','finding-inputs.csv','cohort-alignment.csv','coverage-rules.csv'];data={x:rows(x) for x in files};out=check(data) if '--self-test' in sys.argv: mutations=[('injection-endpoints.csv',1,'End median injection (m3 per well)','43000'),('first-year-fp-endpoints.csv',0,'End first-production cohort','2016'),('first-year-fp-endpoints.csv',1,'AMR change from printed endpoints (%)','610'),('headline-reconciliation.csv',0,'AMR minus published (percentage points)','0'),('lateral-normalization.csv',1,'AMR change (%)','767.3469'),('energy-normalization.csv',0,'End water-use intensity (L per GJ)','93'),('cohort-alignment.csv',0,'Grouping clock','Year of first production'),('coverage-rules.csv',0,'Specified period or threshold','No water production')] for filename,row,col,value in mutations: bad=copy.deepcopy(data);bad[filename][row][col]=value try:check(bad) except AssertionError:continue raise AssertionError('Defect gate did not fire: '+filename+' '+col) out['deliberate_defect_tests_passed']=len(mutations) print(json.dumps(out,indent=2))