Skip to content

Commit

Permalink
Create new IamcDataFilter class (#360)
Browse files Browse the repository at this point in the history
Co-authored-by: Philip Hackstock <[email protected]>
  • Loading branch information
danielhuppmann and phackstock authored Aug 9, 2024
1 parent 2a9a3bf commit ff55b90
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
36 changes: 36 additions & 0 deletions nomenclature/processor/iamc.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions nomenclature/processor/required_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ff55b90

Please sign in to comment.