|
18 | 18 | end |
19 | 19 | end |
20 | 20 | end |
| 21 | +end |
21 | 22 |
|
22 | | - # Reconcile the 1990 direct_emissions total queries against the dataset's |
23 | | - # emissions.csv (read from the local ETSource). |
24 | | - # |
25 | | - # Bunkers is excluded on both sides: its query scales the inventory by the |
26 | | - # bunkers_allocated_percentage_* inputs (default 0), so it cannot equal the raw |
27 | | - # CSV. Subtracting the bunkers query and the Bunkers CSV rows makes the check |
28 | | - # allocation-independent. |
29 | | - # |
30 | | - # LULUCF removals are stored as positive values in the CSV but represent a |
31 | | - # carbon sink; the queries subtract them (NEG), so the CSV sum negates them too. |
32 | | - emissions = Atlas::Dataset.find(dataset.scenario.area_code).emissions |
| 23 | +RSpec.shared_examples('emissions_1990_reconciliation') do |dataset| |
| 24 | + reconciler = GraphDataValidation::EmissionsCsvReconciler.new(dataset) |
33 | 25 |
|
34 | 26 | [ |
35 | 27 | { total: 'direct_emissions_co2_1990', bunkers: 'direct_emissions_bunkers_co2_1990', ghgs: %w[co2] }, |
36 | 28 | { total: 'direct_emissions_other_ghg_1990', bunkers: 'direct_emissions_bunkers_other_ghg_1990', ghgs: %w[other_ghg] }, |
37 | 29 | { total: 'direct_emissions_total_ghg_1990', bunkers: 'direct_emissions_bunkers_total_ghg_1990', ghgs: %w[co2 other_ghg] } |
38 | 30 | ].each do |check| |
39 | | - # Queries report Mt; the inventory CSV is in kton CO2-eq. Exclude bunkers. |
40 | 31 | queried_mt = dataset.query("present:Q(#{check[:total]})") - |
41 | 32 | dataset.query("present:Q(#{check[:bunkers]})") |
42 | | - emissions_csv_mt = emissions.table.sum do |row| |
43 | | - next 0.0 unless row[:year] == 1990 && check[:ghgs].include?(row[:ghg]) |
44 | | - next 0.0 if row[:etm_sector].to_s.casecmp?('bunkers') |
45 | | - |
46 | | - value = row[:value].to_f |
47 | | - removal = row[:etm_sector].to_s.casecmp?('lulucf') && |
48 | | - row[:etm_subsector].to_s.casecmp?('removals') |
49 | | - removal ? -value : value |
50 | | - end / 1000.0 |
| 33 | + expected_mt = reconciler.total_mt(ghgs: check[:ghgs]) |
51 | 34 |
|
52 | | - context "1990 inventory reconciliation for #{check[:total]}" do |
| 35 | + context "1990 emissions results for #{check[:total]}" do |
53 | 36 | it 'matches emissions.csv (excl. bunkers)' do |
54 | | - expect(queried_mt).to be_within(0.001 * emissions_csv_mt + 1e-6).of(emissions_csv_mt) |
| 37 | + expect(queried_mt).to be_within(0.001 * expected_mt + 1e-6).of(expected_mt) |
55 | 38 | end |
56 | 39 | end |
57 | 40 | end |
|
0 commit comments