"""Reproduce AMR comparisons from the matching published CSVs.""" from collections import Counter from decimal import Decimal as D from pathlib import Path import csv import json HERE = Path(__file__).resolve().parent DATA = HERE/'data' if (HERE/'data').is_dir() else HERE def rows(name): with (DATA/(name+'.csv')).open(newline='') as file: return list(csv.reader(file))[1:] def pct(part,total): return f'{D(part)*100/D(total):.2f}' values = [D(row[1]) for row in rows('iea-lead-time-stages')] total = sum(values) assert total == D('16.9') early = pct(values[0],total) assert early == rows('iea-lead-time-stages')[0][2]=='73.96' mines = {r[0]:D(r[1]) for r in rows('iea-mineral-lead-times')} nickel = mines['Nickel (Laterite)']-mines['Nickel (Sulfide)'] assert nickel==D('6.7') samples = {r[0]:r[1:] for r in rows('sp-cohort-comparison')} assert samples['United States']==['3','13','13','Almost 29'] print(json.dumps({'iea_stage_sum_years':str(total),'early_stage_share_pct':early,'nickel_laterite_minus_sulfide_years':str(nickel),'us_cohort_comparison':samples['United States']}))