-
Notifications
You must be signed in to change notification settings - Fork 7
refactor: iamc export #158
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
leonieschweiger
merged 13 commits into
pik-piam:main
from
leonieschweiger:refactor_iamc_export
Jul 21, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
83f21d0
refactor iamc export
leonieschweiger 7ec076c
add plastics iamc export variables and fix aggregation bugs in common…
leonieschweiger 721a515
remove redundant export_stock() function from plastics_export
leonieschweiger 0269af5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e04ce8a
[vibecoded] validation plotting script
leonieschweiger 940adfc
Merge branch 'refactor_iamc_export' of github.com:leonieschweiger/rem…
leonieschweiger c41bb20
address review comments
leonieschweiger 326cc27
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 26e0095
split write_iamc function into better understandable bits
leonieschweiger d1a4f8c
Merge branch 'refactor_iamc_export' of github.com:leonieschweiger/rem…
leonieschweiger 2841ee8
define a custom time period for iamc export
leonieschweiger 7739f52
document iamc nomenclature
leonieschweiger ed6bbf4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,120 +1,47 @@ | ||
| import pyam | ||
| import flodym as fd | ||
|
|
||
| from remind_mfa.common.common_export import CommonDataExporter | ||
| from remind_mfa.common.common_export import CommonDataExporter, IamcVariable | ||
|
|
||
|
|
||
| class CementDataExporter(CommonDataExporter): | ||
| def write_iamc(self, mfa: fd.MFASystem): | ||
|
|
||
| model = "REMIND 3.0" | ||
| scenario = "SSP2_NPi" | ||
| constants = {"model": model, "scenario": scenario} | ||
|
|
||
| reported_dims = ("t", "r", "s") | ||
|
|
||
| # cement production | ||
| cement_prod_by_stock_type = ( | ||
| mfa.flows["prod_cement => market_cement"] + mfa.flows["prod_cement => sysenv"] | ||
| ) | ||
| prod_df = self.to_iamc_df(cement_prod_by_stock_type) | ||
| prod_df["variable"] = "Production|Non-Metallic Minerals|Cement" | ||
| cement_prod_idf = pyam.IamDataFrame( | ||
| prod_df, | ||
| unit="t/yr", | ||
| **constants, | ||
| ) | ||
| cement_prod_idf.aggregate( | ||
| variable="Production|Non-Metallic Minerals|Cement", | ||
| append=True, | ||
| ) | ||
|
|
||
| # clinker production | ||
| clinker_prod_by_stock_type = ( | ||
| mfa.flows["prod_clinker => market_clinker"] + mfa.flows["prod_clinker => sysenv"] | ||
| ) | ||
| clinker_prod_df = self.to_iamc_df(clinker_prod_by_stock_type) | ||
| clinker_prod_df["variable"] = "Production|Non-Metallic Minerals|Cement Clinker" | ||
| clinker_prod_idf = pyam.IamDataFrame( | ||
| clinker_prod_df, | ||
| unit="t/yr", | ||
| **constants, | ||
| ) | ||
| clinker_prod_idf.aggregate( | ||
| variable="Production|Non-Metallic Minerals|Cement Clinker", | ||
| append=True, | ||
| ) | ||
|
|
||
| # cement demand | ||
| cement_demand_by_stock_type = mfa.flows["market_cement => prod_product"].sum_to( | ||
| reported_dims | ||
| ) | ||
| demand_df = self.to_iamc_df(cement_demand_by_stock_type) | ||
| demand_df["variable"] = ( | ||
| "Material Demand|Non-Metallic Minerals|Cement|" + demand_df["Stock Type"] | ||
| ) | ||
| demand_df = demand_df.drop(columns=["Stock Type"]) | ||
| demand_idf = pyam.IamDataFrame( | ||
| demand_df, | ||
| unit="t/yr", | ||
| **constants, | ||
| ) | ||
| demand_idf.aggregate( | ||
| variable="Material Demand|Non-Metallic Minerals|Cement", | ||
| append=True, | ||
| ) | ||
|
|
||
| # cement stocks | ||
| cement_stock_by_stock_type = ( | ||
| mfa.stocks["in_use"].stock[{"k": "cement"}].sum_to(reported_dims) | ||
| ) | ||
| stock_df = self.to_iamc_df(cement_stock_by_stock_type) | ||
| stock_df["variable"] = ( | ||
| "Material Stock|Non-Metallic Minerals|Cement|" + stock_df["Stock Type"] | ||
| ) | ||
| stock_df = stock_df.drop(columns=["Stock Type"]) | ||
| stock_idf = pyam.IamDataFrame( | ||
| stock_df, | ||
| unit="t", | ||
| **constants, | ||
| ) | ||
| stock_idf.aggregate( | ||
| variable="Material Stock|Non-Metallic Minerals|Cement", | ||
| append=True, | ||
| ) | ||
|
|
||
| # cement eol | ||
| cement_scrap_by_stock_type = ( | ||
| mfa.stocks["in_use"].outflow[{"k": "cement"}].sum_to(reported_dims) | ||
| ) | ||
| scrap_df = self.to_iamc_df(cement_scrap_by_stock_type) | ||
| scrap_df["variable"] = "Scrap|Non-Metallic Minerals|Cement|" + scrap_df["Stock Type"] | ||
| scrap_df = scrap_df.drop(columns=["Stock Type"]) | ||
| scrap_idf = pyam.IamDataFrame( | ||
| scrap_df, | ||
| unit="t/yr", | ||
| **constants, | ||
| ) | ||
| scrap_idf.aggregate( | ||
| variable="Scrap|Non-Metallic Minerals|Cement", | ||
| append=True, | ||
| ) | ||
|
|
||
| idf = pyam.concat( | ||
| [ | ||
| cement_prod_idf, | ||
| clinker_prod_idf, | ||
| demand_idf, | ||
| stock_idf, | ||
| scrap_idf, | ||
| ] | ||
| ) | ||
| idf.aggregate_region( | ||
| variable=idf.variable, | ||
| region="World", | ||
| append=True, | ||
| ) | ||
| idf.convert_unit(current="t/yr", to="Mt/yr", inplace=True) | ||
| idf.convert_unit(current="t", to="Mt", inplace=True) | ||
|
|
||
| idf.to_excel(self.export_path("iamc", f"output_iamc.xlsx")) | ||
| def iamc_variables(self) -> list[IamcVariable]: | ||
| return [ | ||
| IamcVariable( | ||
| variable_name="Production|Non-Metallic Minerals|Cement", # PRISMA nomenclature | ||
| calculation_function=lambda mfa: ( | ||
| mfa.flows["prod_cement => market_cement"] + mfa.flows["prod_cement => sysenv"] | ||
| ).sum_to(("t", "r")), | ||
| unit="t/yr", | ||
| ), | ||
| IamcVariable( | ||
| variable_name="Production|Non-Metallic Minerals|Cement Clinker", # PRISMA nomenclature | ||
| calculation_function=lambda mfa: ( | ||
| mfa.flows["prod_clinker => market_clinker"] | ||
| + mfa.flows["prod_clinker => sysenv"] | ||
| ).sum_to(("t", "r")), | ||
| unit="t/yr", | ||
| ), | ||
| IamcVariable( | ||
| variable_name="Material Demand|Non-Metallic Minerals|Cement", # PRISMA nomenclature | ||
| calculation_function=lambda mfa: mfa.flows["market_cement => prod_product"].sum_to( | ||
| ("t", "r", "s") | ||
| ), | ||
| unit="t/yr", | ||
| split_name="Stock Type", | ||
| ), | ||
| IamcVariable( | ||
| variable_name="Material Stock|Non-Metallic Minerals|Cement", # PRISMA nomenclature | ||
| calculation_function=lambda mfa: mfa.stocks["in_use"] | ||
| .stock[{"k": "cement"}] | ||
| .sum_to(("t", "r", "s")), | ||
| unit="t", | ||
| split_name="Stock Type", | ||
| ), | ||
| IamcVariable( | ||
| variable_name="Scrap|Non-Metallic Minerals|Cement", # PRISMA nomenclature | ||
| calculation_function=lambda mfa: ( | ||
| mfa.stocks["in_use"].outflow[{"k": "cement"}].sum_to(("t", "r", "s")) | ||
| ), | ||
| unit="t/yr", | ||
| split_name="Stock Type", | ||
| ), | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally prefer not to have the whole function logic in the return statement. It is understandible for me, though. :)