Skip to content

Commit b080ece

Browse files
committed
define a small emissions_1990_validation dataset list in config.yml and abstract complex logic out of spec
1 parent ae6275a commit b080ece

6 files changed

Lines changed: 50 additions & 23 deletions

File tree

lib/graph_data_validation/config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
dataset_collections:
2+
emissions_1990_validation:
3+
- nl2023
4+
- nl2019
5+
- DE_germany
6+
- UKNI01_northern_ireland
7+
28
countries:
39
- AT_austria
410
- BEGM11002_antwerpen

lib/graph_data_validation/lib/datasets.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def initialize(*area_codes, env: :dev)
1010
if env == :dev
1111
Settings.etsource_lazy_load_dataset = true
1212
Atlas.data_dir = Etsource::Base.clean_path(File.expand_path("../etsource"))
13+
Etsource::Loader.instance.clear!(:gqueries)
1314
end
1415

1516
# Silently reject unexisting areas
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
module GraphDataValidation
4+
class EmissionsCsvReconciler
5+
def initialize(dataset)
6+
@emissions = Atlas::Dataset.find(dataset.scenario.area_code).emissions
7+
end
8+
9+
# Returns the 1990 inventory total in Mt for the given GHG types,
10+
# excluding bunkers rows and negating LULUCF removals (stored as positive
11+
# values in the CSV but representing a carbon sink).
12+
def total_mt(ghgs:)
13+
@emissions.table.sum do |row|
14+
next 0.0 unless row[:year] == 1990 && ghgs.include?(row[:ghg])
15+
next 0.0 if row[:etm_sector].to_s.casecmp?('bunkers')
16+
17+
value = row[:value].to_f
18+
removal = row[:etm_sector].to_s.casecmp?('lulucf') &&
19+
row[:etm_subsector].to_s.casecmp?('removals')
20+
removal ? -value : value
21+
end / 1000.0
22+
end
23+
end
24+
end

lib/graph_data_validation/spec/dataset_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
end
1010
end
1111
end
12+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require_relative 'spec_helper'
2+
require_relative '../lib/datasets'
3+
require_relative '../lib/emissions_csv_reconciler'
4+
require_relative 'validation/emissions'
5+
6+
describe 'Validating 1990 emissions inventory' do
7+
GraphDataValidation::Datasets.from_collection(:emissions_1990_validation).each do |dataset|
8+
context "with area_code #{dataset.scenario.area_code}" do
9+
it_behaves_like 'emissions_1990_reconciliation', dataset
10+
end
11+
end
12+
end

lib/graph_data_validation/spec/validation/emissions.rb

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,23 @@
1818
end
1919
end
2020
end
21+
end
2122

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)
3325

3426
[
3527
{ total: 'direct_emissions_co2_1990', bunkers: 'direct_emissions_bunkers_co2_1990', ghgs: %w[co2] },
3628
{ total: 'direct_emissions_other_ghg_1990', bunkers: 'direct_emissions_bunkers_other_ghg_1990', ghgs: %w[other_ghg] },
3729
{ total: 'direct_emissions_total_ghg_1990', bunkers: 'direct_emissions_bunkers_total_ghg_1990', ghgs: %w[co2 other_ghg] }
3830
].each do |check|
39-
# Queries report Mt; the inventory CSV is in kton CO2-eq. Exclude bunkers.
4031
queried_mt = dataset.query("present:Q(#{check[:total]})") -
4132
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])
5134

52-
context "1990 inventory reconciliation for #{check[:total]}" do
35+
context "1990 emissions results for #{check[:total]}" do
5336
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)
5538
end
5639
end
5740
end

0 commit comments

Comments
 (0)