Dokumentation · Beregningerne
The emission calculation (B12a)
The rules of docs/architecture/phase-1.md §9, once in SQL (calc.v_emission_interval, calc.emission_matched,
migration 0100_emission) and once in Python (elo_core.emission, the reference a reviewer reads). Nothing is
stored: every figure is computed when it is asked for, from ts.reading, core.mp_geo and fx.factor_hour, so a
changed factor row changes the next query's result and there is nothing to recompute (§9.1). calc holds views and
functions only.
Two geographic splits (B83, docs/architecture/municipality-method.md §2): the price-area methods LB-PA-125 and
LB-PA-200 (phase 1's, unchanged), and the municipality method LB-MU-125 (and LB-MU-200, seeded reserved), which
also reads the municipal share of renewable energy (fx.re_coverage_hour) and the transmission grid's factors (TE).
Its rules are in "The municipality method" below; everything else on this page holds for both.
services/api/tests/test_emission.py is the guarantee that SQL and Python agree to the last decimal (same value,
same number of decimals): on every named case below, under both methods, both bases and both GWP sets, and as a
property on the demo organisation over seeded random periods and sets of metering points, in session time zones that
must not matter. services/api/tests/test_b83_municipal_emission.py is the same guarantee for the municipality method.
Units and exactness
Energy in kWh, factors in g/kWh (CO2, biogenic CO2) and mg/kWh (CH4, N2O), results in g (CO2, CO2e, biogenic CO2) and mg (CH4, N2O). Nothing is rounded or converted: tonnes = g ÷ 1 000 000, for display only.
No binary float anywhere between a stored value and a result. Every operation is +, − or × on numeric (SQL)
or Decimal (Python, in a context that raises rather than rounds), and both are exact for those three operations, so
both produce the same digits and the same scale. A GWP is applied as × GWP × 0.001, never as ÷ 1000: a division is
the one operation whose result scale the two would choose differently.
The rules
| Rule | Detail |
|---|---|
| What is counted | The series of an E17 root (root_mp_id = mp_id AND type_code = 'E17'), all of its series. A child, or a production point that is its own root, never changes a figure (L2) |
| Price area | The MP's price area on the interval's local day: of its core.mp_geo rows, the latest with valid_from <= day, if valid_to is NULL or after the day (so overlapping rows never count twice). No row, or a NULL price_area: no price area, no emission figure, never zero |
| Factor | The row of the method's dataset for the UTC hour of the interval start, the price area and the method's allocation: dataset 1 (DeclarationGridEmission, GE) for a price-area method, dataset 2 (DeclarationTransmissionEmission, TE) for a municipal one; allocation 1 for LB-PA-125 and LB-MU-125, 2 for LB-PA-200 and LB-MU-200. A quarter-hour takes its hour's row. A row with any of its six values NULL is no factor. Under a municipal method the row is used only with a share (below) |
| Data basis | best: a row of any version. final: only version 3 (Final); any other row is treated as missing |
| Pending | An hour without a factor under the basis is pending: the interval keeps its consumption and every amount is NULL, never zero, never carried over from another hour. Under a municipal method an hour without a usable share is pending too |
| Amounts (§9.2) | co2_g = q × co2_g, ch4_mg = q × ch4_mg, n2o_mg = q × n2o_mg, biogenic_co2_g = q × (co2_origin_g − co2_g), co2e_g = co2_g + ch4_mg × GWP_CH4 × 0.001 + n2o_mg × GWP_N2O × 0.001 |
| Losses | Transmission amount × (t_factor − 1), distribution amount × (d_factor − 1), for each gas, biogenic CO2 and CO2e, each on the amount excluding losses: added, never compounded (1.45 % + 3.87 % = 5.32 %, not 5.376 %). They are never part of co2e_g (Scope 2) |
| Provenance | factor_generated is true for a generated factor (the frozen extract, raw.fetch.source = 9). A generated factor is never Energinet's: a page that shows such a figure must not say "Kilde: Energinet" (B16a) |
The municipality method (B83)
docs/architecture/municipality-method.md §2.1 to §2.3 and §2.5; the golden cases are its §2.6. The method is method
3, LB-MU-125 (ref.method.geography = 'municipality', allocation 1, available); method 4, LB-MU-200
(allocation 2), is seeded reserved: it is in no list of methods, every calc function refuses it (22023) and the API
answers 422 for it, until one UPDATE ref.method SET status = 'available' makes it computed by TE's 200 % rows, with
nothing else changed.
| Rule | Detail |
|---|---|
| A root's municipality (§2.2) | The municipality_code of the current master-data version of the E17 root (core.mp_version, observed_to IS NULL), for every hour of every year (a changed code is a correction and holds for the past). Usable when it is a row of ref.municipality (the 98 municipalities; in the demo also the generator's 999 and 998). calc.mp_municipality(mp_ids int[]) returns per root the usable code or NULL and whether the setting wants it: the one place of the rule in SQL; nothing is stored |
| Wanted | By ingest.municipality_setting: under auto every usable code, under a list the listed codes. Decided from the setting, never from what is loaded: a wanted municipality whose rows are not loaded yet is pending, not noMunicipality |
| No municipality | No usable code (NULL, a code that is no municipality, no version), or a code the list leaves out: status noMunicipality (reasons no_municipality and municipality_not_loaded), no figure under the method. Never the price-area figure in its place, never 0 % local |
| The share (§2.3) | re_share_pct of fx.re_coverage_hour for the root's municipality and the UTC hour of the interval start (a quarter-hour takes its hour's share), used as delivered when it is between 0 and 100. No row, a NULL share or one outside 0 to 100 is no share: the interval is pending (no_local_share), never 0 %. A share of 100 % is a figure: every amount is zero. RE has no version and never decides the basis |
| The parts | Municipal part q_L = q × R × 0.01 (kWh, 11 decimals: × 0.01, never ÷ 100), remainder q_E = q − q_L |
| Amounts | The rule of §9.2 on q_E in place of q, with the TE row: CO2, CH4, N2O, CO2e, biogenic CO2 on the remainder too (the guide's "Forbrug kWh" in that line is a slip for ForbrugE), and the two losses on those amounts |
| Intensity | Σ amount ÷ Σ q of the intervals with both inputs: per kWh of the consumption, not of the remainder |
| Pending | Either input missing (the share, or the TE row under the basis). The interval keeps its consumption and has no amount and no municipal part |
| Reason | Why an interval has no figure: the first of no_price_area, no_municipality, municipality_not_loaded, no_local_share, no_factor (§5's order; under a price-area method only the first and the last occur) |
| Status (§2.5) | Of a point-hour, the first that applies: noPriceArea; noMunicipality; pending; otherwise the TE row's version |
| Comparison | Both periods by the same method; an hour without either input has no factor, so it is excluded for the factor (excluded_factor) |
| Data version | ops.data_version is incremented when a share changes (the loader, B81) and when a root's current version changes its code (write_version, B83) |
Golden case (§2.6): 1 000 kWh, share 30 %, TE row 100 g CO2, 0,5 mg CH4, 0,1 mg N2O, CO2 origin 150 g, factors 1,0145 and 1,0387, AR5: municipal part 300 kWh, remainder 700 kWh, 70,02835 kg CO2e (0,7 × §9.2's 100,0405), biogenic 35 kg, transmission loss 1,015411075 kg, distribution 2,710097145 kg, intensity 70,02835 g CO2e/kWh per kWh of the 1 000.
What is not equal to Eloverblik's report yet. The lead's trials on the owner's year 2025 against the report made
with "Kommune" on 15 November 2026 (municipality-method.md §2.7; relative residuals only, no figure of the owner's):
the consumption, the municipal part ("Kommunal vedvarende energi", +7e-8) and the three gases per kWh are equal; the
emission excluding losses differs by −2.0e-4 and the biogenic emission by +2.2e-4; the two losses are not found
(+5.0e-3 and −1.7e-2). §2.7 lists, in order, what is to be found and by whom: each is the lead's trial or validation,
none a builder's. No tool has validated the method yet (B85 adds the municipality target to make validate); until then
no page claims it.
calc.v_emission_interval
One row per interval of an E17 root × method × GWP set × data basis. A view cannot take parameters, so a query selects one of each:
SELECT * FROM calc.v_emission_interval
WHERE method_id = 1 AND gwp_set = 'AR5' AND basis = 'best'
AND mp_id = ANY (:mp_ids) AND interval_start >= :start AND interval_start < :end;
| Column | Meaning |
|---|---|
mp_id, series_id, interval_start, resolution, quality (1–5 = A01–A05), kwh | The reading as stored (kwh NULL only with A02) |
local_date, hour_utc | Its local day and its UTC hour (the factor key) |
method_id, method_code, gwp_set, basis | What the row is computed under: every figure names its method, GWP set and data basis |
price_area | NULL: no price area on the day |
pending | True when the hour has no factor under the basis (or no price area) |
factor_version, factor_retrieved_at, factor_generated, factor_co2_g, factor_ch4_mg, factor_n2o_mg, factor_co2_origin_g, factor_t, factor_d | The factor row used; all NULL when pending |
co2_g, ch4_mg, n2o_mg, biogenic_co2_g, co2e_g | The amounts; NULL when pending or when kwh is NULL |
transmission_*, distribution_* (co2_g, ch4_mg, n2o_mg, biogenic_co2_g, co2e_g) | The two losses of each amount |
re_share_pct | The municipality method: the usable share of the hour (NULL without one, and under a price-area method) |
local_kwh, grid_kwh | The municipality method: the municipal part and the remainder of an interval with a figure; NULL otherwise, and under a price-area method |
pending_reason | Why the interval has no figure (the table above); NULL with one |
no_municipality | Status noMunicipality: a price area, a municipal method and no wanted municipality. False under a price-area method |
The view has security_invoker, so it reads with the rights of whoever queries it. No column carries a municipality's
code: the code stays inside the calculation (calc.mp_municipality).
Two branches, one view (B83f). The view is the UNION ALL of calc.v_emission_interval_price_area (phase 1's view
join for join, for the methods whose geography is not municipality; its five new columns constants) and
calc.v_emission_interval_municipal (the root's municipality, the share and TE). calc.emission_intervals reads the
union as before; each function that reads the view reads its method's branch instead,
calc.emission_intervals_price_area or calc.emission_intervals_municipal (the same parameters, filters and row
type): it looks the method's geography up once and runs its statement in two copies, the same text but for the branch
(calc.weekday_hour_cells names the function in its formatted statement; in calc.emission_buckets and
calc.footprint the price-area copy is the statement as it was before B83, the new columns its constants). So a
price-area call plans phase 1's joins and nothing else (test_a_price_area_call_plans_no_municipal_join reads the plans
inside every function with auto_explain) and takes the time it took before B83 (the report gives the times). A union of
the two branches, with the unused one pruned, was tried and measured: PostgreSQL plans such a union as a subquery of its
own and projects every row once more, 4 to 6 % on a year of every root. A query file names neither branch view, as it
names no calc.v_emission_interval.
The functions that read the view (migration 0335_municipal_emission) take the method through unchanged in their
signatures, keep noMunicipality apart from pending (the pending minutes, hours and kWh leave it out; under a
price-area method nothing changes, no_municipality being false) and give new columns after their old ones:
| Function | New columns |
|---|---|
calc.emission_intervals | The view's five (it returns the view's rows). New beside it: calc.emission_intervals_price_area and calc.emission_intervals_municipal, one branch each (above) |
calc.emission_buckets | Per side (ac_, cp_): local_kwh, no_municipality_kwh, municipality_not_loaded_kwh (a part of the previous), no_local_share_kwh (a part of pending_kwh); for the period p_local_kwh and the shares p_no_municipality, p_municipality_not_loaded, p_no_local_share (9 decimals, as p_pending) |
calc.footprint | local_kwh (the block local_energy), and the hours and kWh of no_municipality, municipality_not_loaded and no_local_share |
calc.emission_series_intervals | re_share_pct, local_kwh, grid_kwh, pending_reason per interval (an hour of quarters summed to the grain takes their sums; a gap has none) |
calc.weekday_hour_cells | basis_no_municipality (G01) |
calc.emission_matched | None: an hour without either input has no factor. It reads its method's branch |
The intensity of one period (G16, outside a comparison) is Σ co2e_g ÷ Σ kwh over the rows that are not
pending, with the count of pending hours and their share of consumption for the note of §9.3. No function computes it:
the division belongs to the query that displays it (B15), and the reference test computes it the same way. Pending
hours leave it unchanged.
calc.emission_matched
calc.emission_matched(mp_ids int[], date_from date, date_to date, cmp_date_from date,
method_id int, basis text, gwp_set text DEFAULT 'AR5', cmp text DEFAULT 'py')
The like-for-like comparison of §9.3: the local days [date_from, date_to] (both inclusive) of the E17 roots mp_ids
(NULL: all) against the days of a comparison period. The correspondence is a parameter (B55, phase 2 §3.4): cmp is
py (the same days a year earlier, phase 1's rule and the default), pp (the period immediately before) or from (an
earlier period from cmp_date_from), resolved by calc.comparison_period (docs/time-rules.md, "The correspondence of two
periods"). cmp_date_from is the comparison's first day, so that a call states its comparison: for py it must be
calc.previous_year_date(date_from), for pp the day the rule gives, for from the date itself. Always one row.
- Eligibility (A3x-05). One row per side, MP and UTC hour. The hour's readings are complete when its intervals
cover exactly 60 minutes without overlap (minutes add up to 60, no two start together), each with a quantity, none
A02 or A05: one hourly value or four quarters, never a split hourly value. A complete estimated hour (A03) is
eligible. It has a factor when its intervals have one under the basis. Its quantity and amounts are the sums of its
own intervals.
2. Cells (§2). Hours correspond by the corresponding day, clock hour and occurrence: with
pyby month and day (29 February has no counterpart), withppandfromby the position of the month and the day of the month (29 to 31 March against February have none) or by offset. The second 02:00 of a 25-hour day against a day of 24 hours, and 02:00 against a 23-hour day, have no counterpart either: they are unmatched, never moved. 3. Counting.hours_in_period(H): the current MP-hours with at least one quantity. Of those, without a counterpart cell: neither matched nor excluded. With one:excluded_readingswhen either side is not complete (or the earlier side has no readings), elseexcluded_factorwhen either side has no factor, else matched (matched_hours, h). Several MPs count MP-hours. 4. Amounts. Summed over the h matched pairs only, for each side; all NULL when h = 0 (an unavailable figure is never a zero).
| Column | Meaning |
|---|---|
method_code, gwp, data_basis | What the figures are computed under |
matched_hours, hours_in_period, excluded_readings, excluded_factor | h, H and the two exclusions ("Sammenlignet over h af H timer") |
reason | NULL when h > 0. Otherwise why the comparison is not applicable (i.r.): no_data (H = 0), missing_readings or no_factor (whichever excluded more cells; missing_readings on a tie), no_counterpart (no cell of the period has one) |
kwh, co2e_g, co2_g, ch4_mg, n2o_mg, biogenic_co2_g, transmission_co2e_g, distribution_co2e_g | The current side's matched figures |
cmp_… (the same eight) | The comparison side's matched figures |
generated_factors | True when a factor behind the matched amounts is generated; NULL when h = 0 |
The variance and the variance of intensity are computed from these columns by the query that shows them (B15).
basis = 'final' on the running year has no Final factor, so h = 0 and reason = 'no_factor': "not applicable".
Refusals (SQLSTATE 22023): an unknown or unavailable method, a basis other than best/final, an unknown GWP
set, an empty period or a NULL date, an unknown cmp, a cmp_date_from other than the comparison's first day, and a
pp or from comparison that does not end before the period starts.
The comparison in the queries' functions (B55). calc.emission_buckets(…, p_grain, p_cmp text DEFAULT 'py', p_cmp_date date DEFAULT NULL) is this comparison once per bucket, with the whole period's correspondence: a bucket's cells
are its own days', their counterparts wherever the correspondence puts them, so the buckets' counts and matched figures sum
to the period's; p_cmp none reads no comparison side. Its totals count each comparison day in the bucket of the day it
corresponds to (docs/queries.md, "The comparison"). calc.consumption_matched(…, p_per_mp, p_cmp, p_cmp_date) is the same
cells without the factor. The Python reference of each is elo_core.emission.emission_matched with a
elo_core.comparison.Correspondence (its last parameter; without one, the previous year as before); test_queries_b55.py
holds the SQL to it on the demo organisation for pp and from: over a period with both clock-change days and over April
against March and March against February.
The sources have the same comparison with a complete mix (under the basis) in place of the factor (calc.mix_buckets,
excluded_mix): docs/mix.md; their queries are mix.structure and mix.series (docs/queries.md, "The sources in the
queries").
Grants. elo_api may select the view and execute the function; elo_worker and PUBLIC may not.
Cost. The query has no join between intermediate results, only scans, one window and groupings: every hour is
keyed by the current-period cell it belongs to (a comparison hour by the cell of its day + the shift, with py one year
later; none where that is not the same day of the month, 29 February a year on), and one marker row per cell of the
comparison days says whether the counterpart exists. Its cost grows with the rows read
whatever the planner's estimates. A first version joined two CTEs; before ANALYZE the planner estimated one row each,
chose a nested loop that re-ran calc.previous_year_cell per row, and took 40 s for one month of one MP (0.08 s once
the tables were analysed): exactly the state of a database right after a first load. Measured on the demo
organisation (18 E17 roots, about 395 000 readings from 2024, PostgreSQL 18 in the Colima VM, tables not analysed): a
year to the frontier against the year before, over every MP, takes about 5 s per method.
The tests
| Where | What |
|---|---|
elo_devtools.emission_cases | The named cases, each defined once: a store of facts, a selection and a check of the value §9.2 or §9.3 states. G_1–G_4 are the golden cases of §9.2; M_01–M_16 the tests of §9.3 and their eligibility rules (the specification gives them no row IDs); further interval cases cover allocations, children, the price area of the local day, A02 and the two bases |
libs/core/tests/test_emission_reference.py | Every case on the reference, without a database |
services/api/tests/test_emission.py | Every case through the SQL as elo_api, equal to the reference read back from the same tables, under all eight combinations of method, basis and GWP set; the property on the demo organisation (loaded once through the production merge and the loader's file mode, with a DK1 → DK2 → DK1 move, a period without a price area, an MP without any, NULL factor values, deleted rows and a few real rows); a changed factor row changing the next result; the refusals and the grants. The view's five new columns are compared on every row too |
elo_devtools.emission_cases (B83) | MUNICIPAL_INTERVAL_CASES and MUNICIPAL_MATCHED_CASES: §2.6's golden case under both bases and both GWP sets, share 100 % and 0 %, no share (no row, NULL, above 100, below 0), no municipality (no code, an unknown code, no version, a code outside the list) beside a wanted municipality without rows, a quarter-hour's share and the area's TE row, final with a Preliminary TE row, the allocation, two municipalities, and a comparison with and without a share |
services/api/tests/test_b83_municipal_emission.py | Those cases through the SQL under twelve combinations (three methods); the cases in the aggregates; calc.mp_municipality; a changed share; LB-MU-200 refused while reserved and computed by the 200 % rows once available; the property on the demo organisation (make demo's load: master data with 999 and 998, TE, RE with its NULL hour, its hour above 100, its day without rows of 998 and its last ten days without rows; one root's code removed) for calc.emission_matched, the view, calc.emission_buckets by every grain with py, pp and none, calc.footprint and calc.emission_series_intervals, and under a list; and the price-area methods before and after the migration: every function that reads the view, on the demo, the same rows value for value |
Checked against Eloverblik's report (B78)
On 24 November 2026 the owner made Eloverblik's report "Dit klimaaftryk" for one metering point and the year 2025
("Endelige tal") with the geographic split "Prisområde". The formal validation (§9.4, make validate, a throwaway
database, that day's snapshot of Energinet's datasets) was VALIDATED with the product's basis best (the pages'
default), LB-PA-125 and AR5: nine of nine blocks equal at the report's printed precision (consumption, the monthly
control, emissions excluding losses, distribution loss, transmission loss, biogenic emissions, CO2, CH4, N2O). Under basis
final the tool does not compare the seven emission blocks: Energinet publishes one hour of the year (the first of 1
January 2025, Danish time) as preliminary, so basis=final rests on less than the period, where the report's "Endelige
tal" includes it. The renewable share, the sources and the origin were compared by hand on the stack's answers (equal),
not by the tool. The procedure is docs/research/L2-validation-against-eloverblik.md; the report's figures are the
owner's and in no file.
The pages' emission footnote says so and no more (validationNote in web/apps/elo-web/src/shared/strings.ts; the
sentences in docs/pages.md): the emission and the consumption, one report, one metering point, one year, equal with the
page's best known factors (never "endelige"), and the worked examples; with LB-PA-200 chosen, that the report uses the
125 % method and the figures therefore differ from it.
The sources and origin blocks of report.footprint are the split "Prisområde" (docs/mix.md). In the pages' words
(a line that names the split and claims no validation):
Elmixet følger Eloverbliks geografiske opdeling »Prisområde«. Rapporten »Dit klimaaftryk« lavet med opdelingen »Kommune« viser andre andele, fordi den først medregner kommunens egen vedvarende produktion.
The report made first with "Kommune" agreed on the consumption alone: that split counts the municipality's own renewable production first ("Kommunal vedvarende energi"); the municipal method is computed since B83 ("The municipality method" above, with what the lead's trials found against that report), and its validation is B85's.
Kilde: docs/emission.md i løsningens
repository.