#!/usr/bin/env python3 """Hand-worked cases for Emissions Inventory. Run: python3 tests/test_emissions_inventory.py""" import os, sys sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from envs.emissions_inventory import gen as G def close(a, b, tol=1e-6): assert abs(a - b) <= tol, (a, b) # ---------------------------------------------------------------- activity data # Case 1: 1200 litres of diesel at 2.5 kg CO2e per litre = 3000 kg = 3.0 t. close(G.emission_t(1200, 1.0, 2.5), 3.0) # Case 2: 2.5 MWh of electricity = 2500 kWh; at 0.4 kg per kWh = 1000 kg = 1.0 t. close(G.emission_t(2.5, 1000, 0.4), 1.0) # Case 3: 500 m3 of natural gas at 10.5 kWh per m3 = 5250 kWh; at 0.2 kg per kWh = 1050 kg = 1.05 t. close(G.emission_t(500, 10.5, 0.2), 1.05) # Case 4: 100 miles = 160.9344 km; at 0.25 kg per km = 40.2336 kg = 0.0402336 t. close(G.emission_t(100, 1.609344, 0.25), 0.0402336) # Case 5: 750 kg of waste = 0.75 t; at 400 kg CO2e per tonne = 300 kg = 0.3 t. close(G.emission_t(750, 0.001, 400), 0.3) # Case 6: 36 GJ of district heat = 36 x 277.78 = 10000.08 kWh; at 0.1 kg per kWh = 1000.008 kg = 1.000008 t. close(G.emission_t(36, 277.78, 0.1), 1.000008) # ---------------------------------------------------------------- multi-gas # The shipped GWP tables are the published AR4 and AR5 100-year values. assert G.GWP["AR4"][1] == {"CO2": 1, "CH4": 25, "N2O": 298, "HFC-134a": 1430, "HFC-32": 675, "SF6": 22800} assert G.GWP["AR5"][1] == {"CO2": 1, "CH4": 28, "N2O": 265, "HFC-134a": 1300, "HFC-32": 677, "SF6": 23500} # Case 1: 1000 litres at 2.5 kg CO2, 0.4 g CH4 and 0.1 g N2O per litre: # CO2 = 2500 kg = 2.5 t; CH4 = 400 g = 0.4 kg; N2O = 100 g = 0.1 kg. co2, ch4, n2o = G.gas_masses(1000, 2.5, 0.4, 0.1) close(co2, 2.5); close(ch4, 0.4); close(n2o, 0.1) # AR4: 2.5 + 0.4 x 25 / 1000 + 0.1 x 298 / 1000 = 2.5 + 0.0100 + 0.0298 = 2.5398 t CO2e. close(G.co2e_t(co2, ch4, n2o, 25, 298), 2.5398) # AR5: 2.5 + 0.4 x 28 / 1000 + 0.1 x 265 / 1000 = 2.5 + 0.0112 + 0.0265 = 2.5377 t CO2e. close(G.co2e_t(co2, ch4, n2o, 28, 265), 2.5377) # Case 2: 10 t of landfilled waste at 50 000 g CH4 per tonne = 500 kg CH4, no CO2 or N2O; AR5: 500 x 28 / 1000 = 14.0 t CO2e. co2, ch4, n2o = G.gas_masses(10, 0.0, 50000.0, 0.0) close(co2, 0.0); close(ch4, 500.0); close(n2o, 0.0); close(G.co2e_t(co2, ch4, n2o, 28, 265), 14.0) # Case 3: 100 dairy cows at 120 000 g CH4 and 1000 g N2O per head = 12 000 kg CH4 and 100 kg N2O; # AR4: 12000 x 25 / 1000 + 100 x 298 / 1000 = 300 + 29.8 = 329.8 t CO2e. co2, ch4, n2o = G.gas_masses(100, 0.0, 120000.0, 1000.0) close(ch4, 12000.0); close(n2o, 100.0); close(G.co2e_t(co2, ch4, n2o, 25, 298), 329.8) # Case 4: 2 t of fertiliser nitrogen at 15 700 g N2O per tonne = 31.4 kg N2O; AR5: 31.4 x 265 / 1000 = 8.321 t CO2e. co2, ch4, n2o = G.gas_masses(2, 0.0, 0.0, 15700.0) close(n2o, 31.4); close(G.co2e_t(co2, ch4, n2o, 28, 265), 8.321) # ---------------------------------------------------------------- time series # Changes: 100 -> 90 is -10 %; 100 -> 110 is +10 %; 1500 -> 1200 is -300 / 1500 = -20 %. close(G.percent_change(90, 100), -10.0); close(G.percent_change(110, 100), 10.0); close(G.percent_change(1200, 1500), -20.0) # Intensity: 500 t over 25 000 units = 500 000 kg / 25 000 = 20 kg per unit. close(G.intensity_kg_per_unit(500, 25000), 20.0) # Target: 40 % below 1000 t = 600 t. 42 % below 2500 t = 2500 x 0.58 = 1450 t. close(G.target_level(1000, 40), 600.0); close(G.target_level(2500, 42), 1450.0) # Equal cut: from 900 t to 600 t in 6 years = 300 / 6 = 50 t per year. close(G.linear_annual_cut(900, 600, 6), 50.0) # Constant-rate cut: 800 t to 200 t in 2 years: 1 - sqrt(0.25) = 0.5 -> 50 %. Check: 800 x 0.5 x 0.5 = 200. close(G.compound_annual_cut_percent(800, 200, 2), 50.0) # 1000 t to 729 t in 3 years: 1 - 0.729^(1/3) = 1 - 0.9 = 10 %. Check: 1000 x 0.9^3 = 729. close(G.compound_annual_cut_percent(1000, 729, 3), 10.0) # Straight line from 1000 t (2015) to 600 t (2025): 40 t per year; 2020 -> 800 t; 2018 -> 880 t; 2025 -> 600 t. close(G.linear_path(1000, 600, 2015, 2025, 2020), 800.0); close(G.linear_path(1000, 600, 2015, 2025, 2018), 880.0); close(G.linear_path(1000, 600, 2015, 2025, 2025), 600.0) # ---------------------------------------------------------------- data quality # A missing month takes the mean of its neighbours: (100 + 200) / 2 = 150; (301 + 302) / 2 = 301.5; (0 + 50) / 2 = 25. close(G.fill_gap(100, 200), 150.0); close(G.fill_gap(301, 302), 301.5); close(G.fill_gap(0, 50), 25.0) # A generated file, recounted here in a few lines: rows minus duplicates plus filled months gives 12 months per meter, # and the checks agree with the recount. import csv, io for d in (1, 2, 3): t = G.generate(11, d, "data-quality") seen, dup = {}, 0 for row in csv.DictReader(io.StringIO(t.files["meter_readings.csv"])): key = (row["meter_id"], row["month"]) kwh = float(row["consumption"]) * (1000 if row["unit"] == "MWh" else 1) if key in seen: dup += 1; assert abs(seen[key] - kwh) < 1e-6 else: seen[key] = kwh want = dict((c["path"], c["value"]) for c in t.checks if "path" in c) assert want["duplicate_rows_removed"] == dup filled = [p for p in want if p.startswith("filled.")] assert len(seen) + len(filled) == 12 * len(set(m for m, _ in seen)) close(want["total_kwh"], sum(seen.values()) + sum(want[p] for p in filled), 0.11) for d in (1, 2, 3): for fam in G.FAMILIES: t = G.generate(3, d, fam) assert t.family == fam and 6 <= len(t.checks) <= 45, (fam, d, len(t.checks)) print("test_emissions_inventory: all passed")