Skip to content

Commit

Permalink
Merge pull request #1444 from metno/fix-modelmaps
Browse files Browse the repository at this point in the history
Fix modelmaps
  • Loading branch information
lewisblake authored Jan 8, 2025
2 parents 0b84ba2 + c9e08bb commit df9fe4b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
23 changes: 18 additions & 5 deletions pyaerocom/aeroval/modelmaps_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

logger = logging.getLogger(__name__)

MODELREADERS_USE_MAP_FREQ = ["ReadMscwCtm"]
MODELREADERS_USE_MAP_FREQ = ["ReadMscwCtm"] # , "ReadCAMS2_83"]


class ModelMapsEngine(ProcessingEngine, DataImporter):
Expand Down Expand Up @@ -126,12 +126,17 @@ def _run_model(self, model_name: str, var_list):
_files = self._process_contour_map_var(
model_name, var, self.reanalyse_existing
)
files.append(_files)

if isinstance(_files, str):
_files = [_files]
files.extend(_files)
if self.cfg.modelmaps_opts.plot_types == {OVERLAY} or make_overlay:
# create overlay (pixel) plots
_files = self._process_overlay_map_var(
model_name, var, self.reanalyse_existing
)
if isinstance(_files, str):
_files = [_files]
files.extend(_files)

except ModelVarNotAvailable as ex:
Expand Down Expand Up @@ -322,7 +327,7 @@ def _get_maps_freq(self) -> TsType:
Returns
-------
TSType
TsType
"""
maps_freq = TsType(self.cfg.modelmaps_opts.maps_freq)
if maps_freq == "coarsest": # TODO: Implement this in terms of a TsType object. #1267
Expand Down Expand Up @@ -368,7 +373,9 @@ def _get_read_model_freq(self, model_ts_types: list) -> TsType:
logger.info(f"Found coarsest maps_freq that is available as model data: {freq}")
return freq

raise ValueError("Could not find any TS type to read maps")
freq = min(TsType(fq) for fq in model_ts_types)
logger.info(f"Found coarsest freq available as model data: {freq}")
return freq

def _read_model_data(self, model_name: str, var: str) -> GriddedData:
"""
Expand Down Expand Up @@ -431,7 +438,13 @@ def _read_model_data(self, model_name: str, var: str) -> GriddedData:
ts_types = reader.ts_types
ts_type_read = str(self._get_read_model_freq(ts_types))
else:
ts_type_read = self.cfg.time_cfg.main_freq
model_ts_type_read = self.cfg.model_cfg.get_entry(model_name).model_ts_type_read
if model_ts_type_read:
ts_type_read = model_ts_type_read
else:
ts_type_read = (
self.cfg.colocation_opts.ts_type
) # emulates the old way closer than None

data = reader.read_var(
var,
Expand Down
2 changes: 1 addition & 1 deletion pyaerocom/io/cams2_83/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def check_files(paths: list[Path]) -> list[Path]:


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

def __init__(
Expand Down
25 changes: 20 additions & 5 deletions tests/aeroval/test_modelmaps_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pyaerocom.aeroval.modelmaps_engine import ModelMapsEngine
from pyaerocom.aeroval import EvalSetup
from pyaerocom.exceptions import ModelVarNotAvailable
from pyaerocom import GriddedData
from tests.fixtures.aeroval.cfg_test_exp1 import CFG


Expand All @@ -22,6 +23,13 @@ def test__run(caplog):
assert "no data for model TM5-AP3-CTRL, skipping" in caplog.text


def test__run_working(caplog):
stp = EvalSetup(**CFG)
engine = ModelMapsEngine(stp)
files = engine.run(model_list=["TM5-AP3-CTRL"], var_list=["od550aer"])
assert "PATH_TO_AEROVAL_OUT/data/test/exp1/contour/od550aer_TM5-AP3-CTRL.geojson" in files


@pytest.mark.parametrize(
"maps_freq, result",
[("monthly", "monthly"), ("yearly", "yearly"), ("coarsest", "yearly")],
Expand Down Expand Up @@ -64,11 +72,6 @@ def test__get_read_model_freq(maps_freq, result, ts_types):
["monthly", "yearly"],
"Could not find any model data for given maps_freq.*",
),
(
"coarsest",
["hourly", "weekly"],
"Could not find any TS type to read maps",
),
],
)
def test__get_read_model_freq_error(maps_freq, ts_types, errormsg):
Expand All @@ -79,3 +82,15 @@ def test__get_read_model_freq_error(maps_freq, ts_types, errormsg):

with pytest.raises(ValueError, match=errormsg):
engine._get_read_model_freq(ts_types)


def test__read_model_data():
model_name = "TM5-AP3-CTRL"
var_name = "od550aer"
CFG2 = CFG.copy()
stp = EvalSetup(**CFG2)
engine = ModelMapsEngine(stp)

data = engine._read_model_data(model_name, var_name)

assert isinstance(data, GriddedData)

0 comments on commit df9fe4b

Please sign in to comment.