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

Extract interface for gridded modeldata reader #1269

Merged
merged 10 commits into from
Jul 24, 2024
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 @@
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 @@
self.model = ModelName[model]
self.forecast_day = int(day)

@property
def years_avail(self):
return np.unique(

Check warning on line 297 in pyaerocom/io/cams2_83/reader.py

View check run for this annotation

Codecov / codecov/patch

pyaerocom/io/cams2_83/reader.py#L297

Added line #L297 was not covered by tests
reader.daterange.values.astype("datetime64[Y]").astype("int") + 1970
).astype("str")

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

Check warning on line 303 in pyaerocom/io/cams2_83/reader.py

View check run for this annotation

Codecov / codecov/patch

pyaerocom/io/cams2_83/reader.py#L303

Added line #L303 was not covered by tests

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

Check warning on line 307 in pyaerocom/io/cams2_83/reader.py

View check run for this annotation

Codecov / codecov/patch

pyaerocom/io/cams2_83/reader.py#L307

Added line #L307 was not covered by tests

@property
def run_type(self):
if self._run_type is None:
Expand Down Expand Up @@ -443,6 +458,11 @@
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(
heikoklein marked this conversation as resolved.
Show resolved Hide resolved
np.unique(reader.daterange.values.astype("datetime64[Y]").astype("int") + 1970).astype(
"str"
)
)
print(reader.filepaths)
# dates = ("2021-12-01", "2021-12-04")
heikoklein marked this conversation as resolved.
Show resolved Hide resolved
lewisblake marked this conversation as resolved.
Show resolved Hide resolved

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