Skip to content

Commit

Permalink
Merge pull request #1269 from metno/1175-evaluation-of-uemep-output-i…
Browse files Browse the repository at this point in the history
…n-pyaeroval

Extract interface for gridded modeldata reader
  • Loading branch information
lewisblake authored Jul 24, 2024
2 parents 034fe3c + 01a158a commit 8913284
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 95 deletions.
22 changes: 21 additions & 1 deletion pyaerocom/io/cams2_83/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from pyaerocom import const
from pyaerocom.griddeddata import GriddedData
from pyaerocom.io.cams2_83.models import ModelData, ModelName, RunType
from pyaerocom.io.gridded_reader import GriddedReader

# from pyaerocom.units_helpers import UALIASES

Expand Down Expand Up @@ -220,7 +221,7 @@ def check_files(paths: list[Path]) -> list[Path]:
return new_paths


class ReadCAMS2_83:
class ReadCAMS2_83(GriddedReader):
FREQ_CODES = dict(hour="hourly", day="daily", month="monthly", fullrun="yearly")
REVERSE_FREQ_CODES = {val: key for key, val in FREQ_CODES.items()}

Expand Down Expand Up @@ -291,6 +292,20 @@ def data_id(self, val):
self.model = ModelName[model]
self.forecast_day = int(day)

@property
def years_avail(self):
return np.unique(
reader.daterange.values.astype("datetime64[Y]").astype("int") + 1970
).astype("str")

@property
def ts_types(self):
return self.REVERSE_FREQ_CODES.keys()

@property
def vars_provided(self):
return AEROCOM_NAMES.values()

@property
def run_type(self):
if self._run_type is None:
Expand Down Expand Up @@ -443,6 +458,11 @@ def read_var(self, var_name: str, ts_type: str | None = None, **kwargs) -> Gridd
data_id = "CAMS2-83.EMEP.day0.AN"
reader = ReadCAMS2_83(data_dir=data_dir, data_id=data_id)
reader.daterange = ("2021-12-01", "2021-12-04")
print(
np.unique(reader.daterange.values.astype("datetime64[Y]").astype("int") + 1970).astype(
"str"
)
)
print(reader.filepaths)
# dates = ("2021-12-01", "2021-12-04")

Expand Down
82 changes: 82 additions & 0 deletions pyaerocom/io/gridded_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import abc
from typing import Iterator

from pyaerocom.griddeddata import GriddedData


class GriddedReader(abc.ABC):
"""Abstract base class for griddel model reader used for collocation"""

@property
@abc.abstractmethod
def data_id(self) -> str:
"""
Data ID of dataset
"""
pass

@property
@abc.abstractmethod
def ts_types(self) -> Iterator[str]:
"""
List of available frequencies
Raises
------
AttributeError
if :attr:`data_dir` is not set.
Returns
-------
list
list of available frequencies
"""
pass

@property
@abc.abstractmethod
def years_avail(self) -> Iterator[str]:
"""
Years available in dataset
"""
pass

@property
@abc.abstractmethod
def vars_provided(self) -> Iterator[str]:
"""Variables provided by this dataset"""
pass

@abc.abstractmethod
def has_var(self, var_name) -> bool:
"""Check if variable is supported
Parameters
----------
var_name : str
variable to be checked
Returns
-------
bool
"""
pass

@abc.abstractmethod
def read_var(self, var_name, ts_type=None, **kwargs) -> GriddedData:
"""Load data for given variable.
Parameters
----------
var_name : str
Variable to be read
ts_type : str
Temporal resolution of data to read. Supported are
"hourly", "daily", "monthly" , "yearly".
Returns
-------
GriddedData
"""
pass
Loading

0 comments on commit 8913284

Please sign in to comment.