From d6008f3d010e3ad6d2708d47ed8d48aa396af739 Mon Sep 17 00:00:00 2001 From: Bouwe Andela Date: Mon, 22 Jan 2024 12:31:40 +0100 Subject: [PATCH] Do not overwrite facets from recipe with CMOR table facets for derived variables (#2255) Co-authored-by: Valeriu Predoi --- esmvalcore/_recipe/to_datasets.py | 8 ++++++-- esmvalcore/cmor/table.py | 4 ++-- tests/unit/recipe/test_to_datasets.py | 27 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/esmvalcore/_recipe/to_datasets.py b/esmvalcore/_recipe/to_datasets.py index 56d9d44221..91ebed1179 100644 --- a/esmvalcore/_recipe/to_datasets.py +++ b/esmvalcore/_recipe/to_datasets.py @@ -483,8 +483,12 @@ def _get_input_datasets(dataset: Dataset) -> list[Dataset]: # idea: add option to specify facets in list of dicts that is value of # 'derive' in the recipe and use that instead of get_required? for input_facets in required_vars: - input_dataset = dataset.copy(**input_facets) - _update_cmor_facets(input_dataset.facets, override=True) + input_dataset = dataset.copy() + keep = {'alias', 'recipe_dataset_index', *dataset.minimal_facets} + input_dataset.facets = { + k: v for k, v in input_dataset.facets.items() if k in keep + } + input_dataset.facets.update(input_facets) input_dataset.augment_facets() _fix_cmip5_fx_ensemble(input_dataset) if input_facets.get('optional') and not input_dataset.files: diff --git a/esmvalcore/cmor/table.py b/esmvalcore/cmor/table.py index 6eba32426d..a0c685654a 100644 --- a/esmvalcore/cmor/table.py +++ b/esmvalcore/cmor/table.py @@ -36,7 +36,7 @@ ) -def _update_cmor_facets(facets, override=False): +def _update_cmor_facets(facets): """Update `facets` with information from CMOR table.""" project = facets['project'] mip = facets['mip'] @@ -53,7 +53,7 @@ def _update_cmor_facets(facets, override=False): f"'{short_name}' with mip '{mip}'") facets['original_short_name'] = table_entry.short_name for key in _CMOR_KEYS: - if key not in facets or override: + if key not in facets: value = getattr(table_entry, key, None) if value is not None: facets[key] = value diff --git a/tests/unit/recipe/test_to_datasets.py b/tests/unit/recipe/test_to_datasets.py index a2fe0e76d9..ac68dd2f41 100644 --- a/tests/unit/recipe/test_to_datasets.py +++ b/tests/unit/recipe/test_to_datasets.py @@ -277,6 +277,30 @@ def test_merge_supplementaries_missing_short_name_fails(session): Dataset.from_recipe(recipe_txt, session) +def test_get_input_datasets_derive(session): + dataset = Dataset( + dataset='ERA5', + project='native6', + mip='E1hr', + short_name='rlus', + alias='ERA5', + derive=True, + force_derivation=True, + frequency='1hr', + recipe_dataset_index=0, + tier='3', + type='reanaly', + version='v1', + ) + rlds, rlns = to_datasets._get_input_datasets(dataset) + assert rlds['short_name'] == 'rlds' + assert rlds['long_name'] == 'Surface Downwelling Longwave Radiation' + assert rlds['frequency'] == '1hr' + assert rlns['short_name'] == 'rlns' + assert rlns['long_name'] == 'Surface Net downward Longwave Radiation' + assert rlns['frequency'] == '1hr' + + def test_max_years(session): recipe_txt = textwrap.dedent(""" diagnostics: @@ -300,6 +324,7 @@ def test_max_years(session): @pytest.mark.parametrize('found_files', [True, False]) def test_dataset_from_files_fails(monkeypatch, found_files): + def from_files(_): file = LocalFile('/path/to/file') file.facets = {'facets1': 'value1'} @@ -323,6 +348,7 @@ def from_files(_): def test_fix_cmip5_fx_ensemble(monkeypatch): + def find_files(self): if self.facets['ensemble'] == 'r0i0p0': self._files = ['file1.nc'] @@ -343,6 +369,7 @@ def find_files(self): def test_get_supplementary_short_names(monkeypatch): + def _update_cmor_facets(facets): facets['modeling_realm'] = 'atmos'