diff --git a/nomenclature/processor/iamc.py b/nomenclature/processor/iamc.py new file mode 100644 index 00000000..083fa26a --- /dev/null +++ b/nomenclature/processor/iamc.py @@ -0,0 +1,36 @@ +from typing import List +from pydantic import BaseModel, field_validator + +from pyam import IAMC_IDX + +from nomenclature.definition import DataStructureDefinition + + +class IamcDataFilter(BaseModel): + model: List[str] | None = None + scenario: List[str] | None = None + region: List[str] | None = None + variable: List[str] | None = None + unit: List[str] | None = None + year: List[int] | None = None + + @field_validator("*", mode="before") + @classmethod + def single_input_to_list(cls, v): + return v if isinstance(v, list) else [v] + + def validate_with_definition(self, dsd: DataStructureDefinition) -> None: + error_msg = "" + + # check for filter-items that are not defined in the codelists + for dimension in IAMC_IDX: + if codelist := getattr(dsd, dimension, None) is None: + continue + if invalid := codelist.validate_items(getattr(self, dimension) or []): + error_msg += ( + f"The following {dimension}s were not found in the " + f"DataStructureDefinition:\n{invalid}\n" + ) + + if error_msg: + raise ValueError(error_msg) diff --git a/nomenclature/processor/required_data.py b/nomenclature/processor/required_data.py index 010caacb..3d229e81 100644 --- a/nomenclature/processor/required_data.py +++ b/nomenclature/processor/required_data.py @@ -49,6 +49,7 @@ class RequiredData(BaseModel): variable: List[str] | None = None region: List[str] | None = None year: List[int] | None = None + # TODO consider merging with IamcDataFilter @field_validator("measurand", "region", "year", "variable", mode="before") @classmethod