Skip to content

Commit

Permalink
Do not overwrite facets from recipe with CMOR table facets for derive…
Browse files Browse the repository at this point in the history
…d variables (#2255)

Co-authored-by: Valeriu Predoi <[email protected]>
  • Loading branch information
bouweandela and valeriupredoi authored Jan 22, 2024
1 parent fa2f95b commit d6008f3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
8 changes: 6 additions & 2 deletions esmvalcore/_recipe/to_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions esmvalcore/cmor/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/recipe/test_to_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'}
Expand All @@ -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']
Expand All @@ -343,6 +369,7 @@ def find_files(self):


def test_get_supplementary_short_names(monkeypatch):

def _update_cmor_facets(facets):
facets['modeling_realm'] = 'atmos'

Expand Down

0 comments on commit d6008f3

Please sign in to comment.