Skip to content
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

Create aggregate regions for each IAM #25

Closed
jkikstra opened this issue Dec 26, 2024 · 2 comments
Closed

Create aggregate regions for each IAM #25

jkikstra opened this issue Dec 26, 2024 · 2 comments

Comments

@jkikstra
Copy link
Collaborator

Region definitions are available in https://github.com/IAMconsortium/common-definitions/

Code to do aggregation:

TBD.

Code to create a countries-to-region mapping file:

from nomenclature import countries
from nomenclature.definition import DataStructureDefinition
from nomenclature.processor import RegionProcessor
import pandas as pd

# a DSD stores the definitions info for regions, variables, scenarios
dsd = DataStructureDefinition("definitions")

# dsd.region stores a dictionary of <region name>: <RegionCode object>
# a RegionCode object stores the info for a region defined in the YAML files, such as the model (
region_df = pd.DataFrame(
    [(r.name, r.hierarchy, r.countries, r.iso3_codes) for r in dsd.region.values()],
    columns=["name", "hierarchy", "countries", "iso3"]
)
# fill currently empty iso3 column
# Function to fetch ISO3 codes
def get_iso3_list(country_list):
    if not country_list:
        return None
    iso3_list = []
    for country in country_list:
        try:
            iso3 = countries.get(name=country)
            if iso3:
                iso3_list.append(iso3.alpha_3)
            else:
                iso3_list.append(None)  # If no match found
        except Exception:
            iso3_list.append(None)
    return iso3_list
# fill the iso3 column
region_df["iso3"] = region_df["countries"].apply(get_iso3_list)


# the RegionProcessor creates the mappings object
# the mappings are defined at the model level in a RegionAggregationMapping object
# each RAM contains, among other things, the list of "common regions" (regions resulting
# from aggregation) and their constituents
rp = RegionProcessor.from_directory("mappings", dsd)
rows = []
for ram in rp.mappings.values():
    rows.extend(
        [
            (
                ram.model,
                common_region.name,
                [ram.rename_mapping[nr] for nr in common_region.constituent_regions],
            )
            for common_region in ram.common_regions
        ]
    )
mappings_df = pd.DataFrame(
    rows,
    columns=["model(s)", "common_region", "constituent_regions"],
)


region_df.to_csv("region_df.csv")
mappings_df.to_csv("mappings_df.csv")
@jkikstra
Copy link
Collaborator Author

First version for region aggregation in #37

@jkikstra
Copy link
Collaborator Author

Created a first reasonable version with the merging in of #36.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant