"""Reproduce AMR calculations from the public battery-price bundle.""" from pathlib import Path from decimal import Decimal import csv import json def rows(name): path=Path(__file__).parent/name if not path.is_file(): path=Path(__file__).parent/'data'/name with path.open() as file: return list(csv.reader(file))[1:] def percent(start,end): return ((Decimal(end)/Decimal(start)-1)*100).quantize(Decimal('0.0001')) chemistry=rows('iea-chemistry-pack-index.csv') assert [int(r[0]) for r in chemistry]==list(range(2020,2026)) changes=rows('amr-chemistry-index-changes.csv') for i,row in enumerate(changes,1): assert row[1:4]==[chemistry[j][i] for j in (0,-2,-1)] assert list(map(Decimal,row[4:]))==[percent(row[1],row[3]),percent(row[2],row[3])] regional={r[0]:list(map(Decimal,r[1:])) for r in rows('iea-regional-pack-index.csv')} indices={'North America':2,'Europe':1} for year,lower,reference,lowest,highest,discount in rows('amr-regional-discounts.csv'): assert lower=='China' and Decimal(lowest)==regional[year][0] and Decimal(highest)==regional[year][indices[reference]] assert Decimal(discount)==-percent(highest,lowest) discount=-percent(chemistry[-1][3],chemistry[-1][2]) assert rows('iea-lfp-comparison-notes.csv')[0][1]==str(discount)+'% lower' workbook=rows('doe-pack-cost-history.csv') assert [int(row[0]) for row in workbook]==list(range(2008,2024)) for source,derived in zip(workbook,rows('amr-doe-real-cost-index.csv'),strict=True): assert source[0]==derived[0] assert Decimal(derived[1])==(100*Decimal(source[2])/Decimal(workbook[0][2])).quantize(Decimal('0.0001')) comparisons=rows('amr-doe-endpoint-change.csv') for i,row in zip((2,1),comparisons,strict=True): assert row[1:3]==[workbook[0][i],workbook[-1][i]] assert Decimal(row[3])==percent(row[1],row[2]) print(json.dumps({'market_index_change_percent':changes[0][4],'lfp_chart_discount_percent':str(discount),'doe_real_cost_change_percent':comparisons[0][3]}))