Skip to content

CO2 AFOLU handling #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 43 additions & 16 deletions src/gcages/cmip7_scenariomip/pre_processing/reaggregation/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,8 @@ def to_required_variables(self, all_species: tuple[str, ...]) -> tuple[str, ...]
GriddingSectorComponentsReporting(
gridding_sector="Agriculture",
spatial_resolution=SpatialResolutionOption.MODEL_REGION,
input_sectors=("AFOLU|Agriculture",),
input_sectors_optional=(),
input_species_optional=(),
),
GriddingSectorComponentsReporting(
gridding_sector="Agricultural Waste Burning",
spatial_resolution=SpatialResolutionOption.MODEL_REGION,
input_sectors=(
"AFOLU|Agricultural Waste Burning",
"AFOLU|Agriculture",
"AFOLU|Land|Harvested Wood Products",
"AFOLU|Land|Land Use and Land-Use Change",
"AFOLU|Land|Other",
Expand All @@ -131,6 +124,13 @@ def to_required_variables(self, all_species: tuple[str, ...]) -> tuple[str, ...]
"Sulfur",
),
),
GriddingSectorComponentsReporting(
gridding_sector="Agricultural Waste Burning",
spatial_resolution=SpatialResolutionOption.MODEL_REGION,
input_sectors=("AFOLU|Agricultural Waste Burning",),
input_sectors_optional=(),
input_species_optional=(),
),
GriddingSectorComponentsReporting(
gridding_sector="Aircraft",
spatial_resolution=SpatialResolutionOption.WORLD,
Expand Down Expand Up @@ -1004,23 +1004,31 @@ def to_gridding_sectors(
{"Energy|Demand|Transportation": "Transportation Sector"},
axis="columns",
)
# Do other compilations.
# We can do this here with confidence
# because we assume that the users have used `to_complete`
# before calling this function.

# To handle CO2 differently
# Get boolean masks for CO2 and non-CO2 species
is_co2 = region_sector_df_gridding.index.get_level_values("species") == "CO2"
# Special treatment for CO2
region_sector_df_gridding_co2 = region_sector_df_gridding[is_co2]
# Select rows for non-CO2
region_sector_df_gridding = region_sector_df_gridding[~is_co2]

# # Do other compilations.
# # We can do this here with confidence
# # because we assume that the users have used `to_complete`
# # before calling this function.
for gridding_sector, components in (
("Agriculture", ["AFOLU|Agriculture"]),
(
"Agricultural Waste Burning",
"Agriculture",
[
"AFOLU|Agricultural Waste Burning",
# Hmmm, almost definitely wrong
"AFOLU|Agriculture",
"AFOLU|Land|Harvested Wood Products",
"AFOLU|Land|Land Use and Land-Use Change",
"AFOLU|Land|Other",
"AFOLU|Land|Wetlands",
],
),
("Agricultural Waste Burning", ["AFOLU|Agricultural Waste Burning"]),
("Energy Sector", ["Energy|Supply"]),
("Forest Burning", ["AFOLU|Land|Fires|Forest Burning"]),
("Grassland Burning", ["AFOLU|Land|Fires|Grassland Burning"]),
Expand All @@ -1042,13 +1050,32 @@ def to_gridding_sectors(
("Solvents Production and Application", ["Product Use"]),
("Waste", ["Waste"]),
):
if any("AFOLU" in c for c in components):
region_sector_df_gridding_co2 = region_sector_df_gridding_co2.drop(
list(set(components)), axis="columns", errors="ignore"
)
else:
region_sector_df_gridding_co2[gridding_sector] = (
region_sector_df_gridding_co2[components].sum(axis="columns")
) # type: ignore # pandas-stubs confused
region_sector_df_gridding_co2 = region_sector_df_gridding_co2.drop(
list(set(components) - {gridding_sector}), axis="columns"
)

region_sector_df_gridding[gridding_sector] = region_sector_df_gridding[
components
].sum(axis="columns") # type: ignore # pandas-stubs confused

region_sector_df_gridding = region_sector_df_gridding.drop(
list(set(components) - {gridding_sector}), axis="columns"
)

region_sector_df_gridding = (
pd.concat([region_sector_df_gridding, region_sector_df_gridding_co2])
.sort_index()
.fillna(0)
)

sector_df_gridding_like_input = combine_sectors(
set_new_single_value_levels(
sector_df_gridding.unstack().stack("sectors", future_stack=True), # type: ignore # pandas-stubs confused
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,8 @@ def to_required_variables(self, all_species: tuple[str, ...]) -> tuple[str, ...]
GriddingSectorComponentsReporting(
gridding_sector="Agriculture",
spatial_resolution="model region",
input_sectors=("AFOLU|Agriculture",),
input_sectors_optional=(),
input_species_optional=(),
),
GriddingSectorComponentsReporting(
gridding_sector="Agricultural Waste Burning",
spatial_resolution="model region",
input_sectors=(
"AFOLU|Agricultural Waste Burning",
"AFOLU|Agriculture",
"AFOLU|Land|Harvested Wood Products",
"AFOLU|Land|Land Use and Land-Use Change",
"AFOLU|Land|Other",
Expand All @@ -148,6 +141,13 @@ def to_required_variables(self, all_species: tuple[str, ...]) -> tuple[str, ...]
"Sulfur",
),
),
GriddingSectorComponentsReporting(
gridding_sector="Agricultural Waste Burning",
spatial_resolution="model region",
input_sectors=("AFOLU|Agricultural Waste Burning",),
input_sectors_optional=(),
input_species_optional=(),
),
GriddingSectorComponentsReporting(
gridding_sector="Aircraft",
spatial_resolution="world",
Expand Down
Loading
Loading