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

Script for getting model_region-to-iso mapping #254

Open
jkikstra opened this issue Jan 16, 2025 · 1 comment
Open

Script for getting model_region-to-iso mapping #254

jkikstra opened this issue Jan 16, 2025 · 1 comment

Comments

@jkikstra
Copy link
Contributor

jkikstra commented Jan 16, 2025

Some time ago, @dc-almeida wrote a script that I adapted a bit, creating and writing out ("region_df.csv") a dataframe that combines each registered region with each set of countries that are defined under it, and creates a list of iso3c for it for more standardized names.

See iiasa/emissions_harmonization_historical#25, and copied here for convenience:

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")

We're using this in a different package, and are currently thinking to put it there and make it point to someone's local installation.

However, maybe it is worth to have "user scripts" that create files based on this package for easy use elsewhere?
So maybe you want to host this code yourself, and allow users to simply run the script?

@jkikstra
Copy link
Contributor Author

See also related issue on nomenclature; IAMconsortium/nomenclature#403

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