diff --git a/SWEET_python/MODEL_DECISIONS.md b/SWEET_python/MODEL_DECISIONS.md index c06f5a2..6031698 100644 --- a/SWEET_python/MODEL_DECISIONS.md +++ b/SWEET_python/MODEL_DECISIONS.md @@ -636,7 +636,7 @@ A site captures its reported efficiency, else a default: the remediated value fo 3. `type-default`: 0.6 at a sanitary landfill, 0.45 at a controlled dump, 0 at a dumpsite, for a site with a capture system. 4. `none`: zero, when nothing says the site has a system. - **Paths:** - - `ct-site`: `reported`, `type-default` and `none`; Climate TRACE decides whether a site has a system. + - `ct-site`: `reported`, `type-default` and `none`; Climate TRACE decides whether a site has a system, and may give the value for unreported years. - `site-tool`: `reported`; left blank, `remediated` for a site upgraded in the scenario, else `type-default`. - `adst-site`, `adst-city`: the caller's value, by year where it gives years; none given means 0. - `city-model`: 0.6 on the share sent to landfills with capture; the cities table's efficiency is read but never used. @@ -646,7 +646,7 @@ A site captures its reported efficiency, else a default: the remediated value fo - **Better:** Let the city model read each city's reported efficiency. - **Value:** `SWEET_python.gas_capture`: `GAS_CAPTURE_EFFICIENCY`, `CONVERTED_DUMP_CAPTURE_EFFICIENCY`, `UPGRADED_CONTROLLED_DUMP_CAPTURE_EFFICIENCY` - **Code:** `SWEET_python/city_params.py` (2 places) -- **Reviewed:** 2026-10-23 +- **Reviewed:** 2026-12-01 ### `flaring-uniform` diff --git a/SWEET_python/city_params.py b/SWEET_python/city_params.py index 5ed4ef1..d7eb698 100644 --- a/SWEET_python/city_params.py +++ b/SWEET_python/city_params.py @@ -67,8 +67,15 @@ #: rows as one per YEAR of a site, rather than the legacy input's one per SOURCE record. ROW_YEAR_COLUMN = "row_year" +#: Optional column on a site's dated rows: the gas collection efficiency for every year +#: without a report of its own, used instead of carrying the nearest report. Climate +#: TRACE sets it to the average capture a GHGRP site's emissions and metered gas imply, +#: so the years after its last report take that average, not the last report alone. +#: Absent or blank, those years carry the nearest report, as before. +CAPTURE_FILL_COLUMN = "gas_collection_efficiency_fill" -def _carried_series(years, values, years_range): + +def _carried_series(years, values, years_range, fill=None): """One value per model year from the years a site reported one, or None if none did. One distinct value is that value in every year. Several make a step series: the @@ -76,23 +83,32 @@ def _carried_series(years, values, years_range): forward until the next report, and the latest forward to the end. Two values in one year are averaged first. + ``fill``, when given, replaces the carrying: each reported year keeps its value, and + every other year takes ``fill``. With nothing reported, every year takes ``fill``. + Used for rows that carry their own year (``ROW_YEAR_COLUMN``). The consolidated - facility table resolves gas collection efficiency and oxidation once per site today - and repeats the value on every year, so this gives a constant; if upstream starts - resolving them per year, the series follows the reports with no change here. + facility table's ``precise_v1`` strategy resolves gas collection efficiency and + oxidation within each year, so a site that reports them yearly gets a series that + follows the reports. """ # na_value: a nullable column (Int64, Float64) holds pd.NA, which a plain float # conversion refuses. years = pd.to_numeric(pd.Series(years, dtype=object), errors="coerce").to_numpy(dtype=float, na_value=np.nan) values = pd.to_numeric(pd.Series(values, dtype=object), errors="coerce").to_numpy(dtype=float, na_value=np.nan) usable = ~np.isnan(years) & ~np.isnan(values) + index = pd.Index(years_range) + has_fill = fill is not None and not pd.isna(fill) if not usable.any(): - return None + return pd.Series(float(fill), index=index) if has_fill else None reported = ( pd.Series(values[usable], index=years[usable].astype(int)) .groupby(level=0).mean().sort_index() ) - index = pd.Index(years_range) + if has_fill: + series = pd.Series(float(fill), index=index) + inside = reported.index.intersection(index) + series.loc[inside] = reported.loc[inside].to_numpy() + return series if reported.nunique() == 1: return pd.Series(float(reported.iloc[0]), index=index) full = reported.reindex(reported.index.union(index)).ffill().bfill() @@ -283,7 +299,13 @@ def _resolve_gas_capture(flag, site_type, canonical_row, time_series_rows, years and 'gas_collection_efficiency' in time_series_rows.columns ): efficiency = pd.to_numeric(time_series_rows['gas_collection_efficiency'], errors='coerce') - carried = _carried_series(time_series_rows[ROW_YEAR_COLUMN], efficiency, years_range) + # A caller's value for the years without a report, where it gives one. One per + # site; several are averaged. + fill = None + if CAPTURE_FILL_COLUMN in time_series_rows.columns: + given = pd.to_numeric(time_series_rows[CAPTURE_FILL_COLUMN], errors='coerce').dropna() + fill = float(given.mean()) if len(given) else None + carried = _carried_series(time_series_rows[ROW_YEAR_COLUMN], efficiency, years_range, fill=fill) if carried is None and efficiency.notna().any(): # A measured value on rows with no usable year is still the site's own value. # Falling through to the emissions-year rule below would drop it for the diff --git a/changelog/2026-12.md b/changelog/2026-12.md new file mode 100644 index 0000000..d93a7ab --- /dev/null +++ b/changelog/2026-12.md @@ -0,0 +1,21 @@ +# SWEET_python Changelog — December 2026 + +**Highlights:** A caller can give a site's gas collection efficiency for the years +it did not report, in place of carrying the nearest report into them. + +## Added + +- **A site's dated rows can say what capture to use in years without a report** + (no output change until a caller sets it, + [#135](https://github.com/RMI/SWEET_python/pull/135)). On rows that carry their own year + (`row_year`), `_resolve_gas_capture` carried the first report back to 1970, each + report forward to the next, and the latest forward to 2050. A site whose rows carry + `gas_collection_efficiency_fill` (`CAPTURE_FILL_COLUMN`) now keeps each reported + year's value and takes that one value in every other year: before the first report, + in gaps, and after the last. Blank or absent, nothing changes. Oxidation is not + affected. + - Why: Climate TRACE works out a GHGRP site's capture each year from its reported + emissions and its metered collected methane. A site's yearly values swing (the + median site spans 0.25 across its own years), so carrying the 2023 value to 2050 + would move PM after the last report by more than 25% at about 390 of 1,036 sites, + against the site's average. Climate TRACE sets the fill to that average. diff --git a/changelog/README.md b/changelog/README.md index 6132241..c5cf526 100644 --- a/changelog/README.md +++ b/changelog/README.md @@ -30,6 +30,7 @@ shows the month up to that release. Entries are not grouped by release. Newest first: +- [2026-12](2026-12.md) — a site's dated rows can give its gas collection efficiency for the years it did not report (`gas_collection_efficiency_fill`), in place of carrying the nearest report into them; Climate TRACE sets it to the average capture a GHGRP site's emissions and metered gas imply (no output change until a caller sets it) - [2026-11](2026-11.md) — incineration burns only waste that burns: metal and glass the city does not recycle are landfilled, and a share above the burnable waste left is cut to it (London's 2025 methane −21.6%, all 32 incinerating cities −3.6%; model-output change); the city model sends a controlled-dumpsite share to a fourth disposal bucket, with a dump's MCF (0.6) and a controlled dump's oxidation (0.05; 0.10 and 45% capture under "add gas capture"), instead of dropping it and rescaling the other shares; a city sending 40% to controlled dumps and 60% to open dumps was modelled as 100% open dump (no output change until a caller passes the share); `defaults_2019` keeps generated and disposed country mixes apart, each row with its source: the 28 EU countries' generated mixes from the European Reference Model (2015), and national inventories' disposed mixes (no output change: nothing reads them yet) - [2026-10](2026-10.md) — Indonesia sizes its waste from its own IPCC per-capita row instead of the South-Eastern Asia default, 2.4× higher (model-output change); a site with no recorded type is guessed by Climate TRACE's rule (income, region and named countries) in every SWEET path, moving 55 countries' unknown sites from open dump to sanitary landfill in the site tool (model-output change); gas collection efficiency and oxidation follow a site's reports year by year on rows that carry their year, and a modelled site keeps its measured efficiency (no output change on current data); `load_csv_new` normalizes a city's composition as the other loaders do, so WasteMAP's city DST runs for São Paulo (99%) and Brasília (87%), whose every scenario it rejected as "Invalid waste fractions"; the city DST of Brazil's precomputed cities scales by 100 ÷ their published sum (Brasília +14.9%, most −0.1%), and the no-change scenario reproduces the baseline (model-output change); a Custom Location city models landfill methane in every country: in 31 (Southern Asia, South-Eastern Asia and Southern Africa, which have no regional disposal row, plus Canada, Germany and Switzerland, whose rows are all zeros) its disposal split was (0, 0, 0), so its landfills took none of its waste; it now gets the cities table's split for a city with no landfill data (model-output change); the City DST's "move waste to existing gas capture" slider changes a city's emissions; it computed a new split and never applied it to the landfills, so every setting returned the no-change scenario for the 7 cities with a live slider (its arithmetic also scaled the shares without capture up, not down) (model-output change); the City DST's "add gas capture" lever applies the oxidation it sets (0.22 for the landfill it fits with gas capture, 0.1 for the dumpsite it converts) instead of resetting both to the no-capture defaults at the end of `implement_dst_changes_simple_v1_5`; its results fall another 10–13% after the implementation year, a city that landfills everything from −60% to −65.3% against no change in 2040 (model-output change); a Custom Location in Kosovo builds: SWEET looks a country up in its own table before asking pycountry, which has no Kosovo, so Kosovo's code XKX no longer raises "Country 'XKX' not found." (WasteMAP answered 500), and the name "Kosovo" no longer resolves to Serbia, which the site tool's prefill for a point in Kosovo returned (model-output change for a country given by name); a blank or junk country is not found instead of being modelled as another country (`""` was the UK, `"None"` Italy), and the site tool's prefill finds the country for a point in Turkey, both Congos and eight more places, and says so for a point in none, where it crashed (model-output change for a point in Sint Maarten); a city with no waste figure of its own generates waste at its country's per-capita default, instead of having that default moved a second time as if it described 2019; the 39 such cities in WasteMAP's cities table change by −8% to +54% (model-output change) - [2026-09](2026-09.md) — WasteMAP's site tool, city tool and Custom Location grow waste with the country's UN population year by year, as Climate TRACE does; big cities keep their own UN city rates (model-output change); model output frames get one fixed waste-type column order instead of a per-process one (a `set` fed the ordering, and Python randomizes set iteration per process), moving values in the last bit only, where a sum runs across the reordered columns; City DST's diversion sum stops passing pandas a keyword pandas is removing, which would have broken the model path on pandas 4 behind a misleading `AttributeError` (no output change); annual model no longer charges a flare's inefficiency twice (a hardcoded 2% slip stacked on the `flaring` destruction efficiency), so it agrees with the monthly model again and a site with gas capture emits ~3.5% less (model-output change); `gas_capture_efficiency` bounded to [0, 1], where an out-of-range value used to produce negative emissions silently; one methane density constant instead of two 9.1% apart diff --git a/tests/test_carried_series.py b/tests/test_carried_series.py index e63add4..ecc15e9 100644 --- a/tests/test_carried_series.py +++ b/tests/test_carried_series.py @@ -13,6 +13,7 @@ import pytest from SWEET_python.city_params import ( + CAPTURE_FILL_COLUMN, GAS_EFF_OPTIONS, _build_oxidation_series, _carried_series, @@ -125,3 +126,49 @@ def test_undated_oxidation_is_one_value_in_every_year(): "reported_emissions_year": [2020, np.nan]}) series = _build_oxidation_series(0.05, rows.iloc[0], rows, YEARS) assert np.allclose(series.to_numpy(), 0.2) + + +# ---- a caller's value for the years without a report ---- # + + +def test_a_fill_value_takes_the_place_of_carrying(): + """Reported years keep their values; before, between and after them, the fill.""" + series = _carried_series([2010, 2015], [0.5, 0.7], YEARS, fill=0.6) + assert series.loc[1990] == 0.6 # before the first report + assert series.loc[2010] == 0.5 + assert series.loc[2012] == 0.6 # a gap + assert series.loc[2015] == 0.7 + assert series.loc[2040] == 0.6 # after the latest report + + +@pytest.mark.parametrize("fill", [None, np.nan]) +def test_no_fill_value_keeps_carrying(fill): + series = _carried_series([2010, 2015], [0.5, 0.7], YEARS, fill=fill) + assert series.loc[1990] == 0.5 and series.loc[2012] == 0.5 and series.loc[2040] == 0.7 + + +def test_a_fill_value_with_nothing_reported_is_used_in_every_year(): + assert (_carried_series([2010], [np.nan], YEARS, fill=0.6) == 0.6).all() + + +def test_gas_capture_takes_the_fill_column_after_the_last_report(): + """Climate TRACE's GHGRP sites: each reported year its own capture, the site's + average after the last report instead of the last report carried to 2050.""" + average = float(np.mean(list(POLK.values()))) + rows = _rows(POLK, reported_emissions_year=list(POLK), **{CAPTURE_FILL_COLUMN: average}) + _, _, gce = _resolve_gas_capture(True, "Sanitary Landfill", pd.Series(dtype=object), rows, YEARS) + assert gce.loc[2019] == 0.67 and gce.loc[2023] == 0.76 + assert gce.loc[2024] == pytest.approx(average) and gce.loc[2050] == pytest.approx(average) + assert gce.loc[1990] == pytest.approx(average) + + +def test_a_blank_fill_column_keeps_carrying(): + rows = _rows(POLK, reported_emissions_year=list(POLK), **{CAPTURE_FILL_COLUMN: np.nan}) + _, _, gce = _resolve_gas_capture(True, "Sanitary Landfill", pd.Series(dtype=object), rows, YEARS) + assert gce.loc[1990] == 0.58 and gce.loc[2040] == 0.76 + + +def test_oxidation_ignores_the_fill_column(): + rows = _rows({2013: 0.10, 2020: 0.22}, column="oxidation", **{CAPTURE_FILL_COLUMN: 0.5}) + series = _build_oxidation_series(0.1, rows.iloc[0], rows, YEARS) + assert series.loc[1980] == 0.10 and series.loc[2045] == 0.22