Skip to content

Commit 8b0a59d

Browse files
authored
Merge pull request #751 from ESMValGroup/version2_exp_concatenation_name
Multiple experiments in preprocessor output file name
2 parents b7fb324 + 349eaa9 commit 8b0a59d

File tree

5 files changed

+54
-11
lines changed

5 files changed

+54
-11
lines changed

esmvaltool/_data_finder.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ def get_output_file(variable, preproc_dir):
268268
"""Return the full path to the output (preprocessed) file."""
269269
cfg = get_project_config(variable['project'])
270270

271+
# Join different experiment names
272+
if isinstance(variable.get('exp'), (list, tuple)):
273+
variable = dict(variable)
274+
variable['exp'] = '-'.join(variable['exp'])
275+
271276
outfile = os.path.join(
272277
preproc_dir,
273278
variable['diagnostic'],

esmvaltool/preprocessor/_io.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ def write_metadata(products, write_ncl=False):
216216
)
217217
metadata = OrderedDict()
218218
for product in sorted_products:
219+
if isinstance(product.attributes.get('exp'), (list, tuple)):
220+
product.attributes = dict(product.attributes)
221+
product.attributes['exp'] = '-'.join(product.attributes['exp'])
219222
metadata[product.filename] = product.attributes
220223

221224
output_filename = os.path.join(output_dir, 'metadata.yml')

esmvaltool/recipes/examples/recipe_concatenate_exps.yml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,26 @@ datasets:
1919

2020
diagnostics:
2121

22-
diag_concatenate_exps:
23-
description: Concatenate historical and RCP 8.5 data
22+
diag_regular:
23+
description: Only use historical data.
2424
variables:
25-
tas:
25+
tas: &variable_settings
2626
project: CMIP5
2727
mip: Amon
28+
field: T2Ms
2829
ensemble: r1i1p1
29-
exp: [historical, rcp85]
30+
exp: historical
3031
start_year: 1950
32+
end_year: 2000
33+
additional_datasets:
34+
- {dataset: ERA-Interim, project: OBS, tier: 3, type: reanaly, version: 1, start_year: 1980, end_year: 2000}
35+
scripts: null
36+
37+
diag_concatenate_exps:
38+
description: Concatenate historical and RCP 8.5 data
39+
variables:
40+
tas:
41+
<<: *variable_settings
42+
exp: [historical, rcp85]
3143
end_year: 2050
32-
field: T2Ms
33-
additional_datasets:
34-
- {dataset: ERA-Interim, project: OBS, tier: 3, type: reanaly, version: 1, start_year: 1980, end_year: 2000}
3544
scripts: null

tests/integration/data_finder.yml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22

3-
get_input_filelist:
4-
- drs: default
5-
variable: &variable
3+
get_output_file:
4+
- variable: &variable
5+
variable_group: test
66
short_name: ta
77
dataset: HadGEM2-ES
88
project: CMIP5
@@ -11,10 +11,27 @@ get_input_filelist:
1111
frequency: mon
1212
modeling_realm: [atmos]
1313
mip: Amon
14+
field: T3M
1415
exp: historical
1516
ensemble: r1i1p1
1617
start_year: 1960
1718
end_year: 1980
19+
diagnostic: test_diag
20+
preprocessor: test_preproc
21+
preproc_dir: this/is/a/path
22+
output_file: this/is/a/path/test_diag/test/CMIP5_HadGEM2-ES_Amon_historical_r1i1p1_T3M_ta_1960-1980.nc
23+
24+
- variable:
25+
<<: *variable
26+
exp: [historical, rcp85]
27+
preproc_dir: /test
28+
output_file: /test/test_diag/test/CMIP5_HadGEM2-ES_Amon_historical-rcp85_r1i1p1_T3M_ta_1960-1980.nc
29+
30+
31+
get_input_filelist:
32+
- drs: default
33+
variable:
34+
<<: *variable
1835
available_files:
1936
- ta_Amon_HadGEM2-ES_historical_r1i1p1_193412-195911.nc
2037
- ta_Amon_HadGEM2-ES_historical_r1i1p1_195912-198411.nc
@@ -141,6 +158,7 @@ get_input_filelist:
141158
found_files:
142159
- historical/Amon/ta/HadGEM2-ES/r1i1p1/ta_Amon_HadGEM2-ES_historical_r1i1p1_198412-200511.nc
143160

161+
144162
get_input_fx_filelist:
145163
- drs: default
146164
variable:

tests/integration/test_data_finder.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import yaml
88

99
import esmvaltool._config
10-
from esmvaltool._data_finder import get_input_filelist, get_input_fx_filelist
10+
from esmvaltool._data_finder import (get_input_filelist, get_input_fx_filelist,
11+
get_output_file)
1112
from esmvaltool.cmor.table import read_cmor_tables
1213

1314
# Initialize with standard config developer file
@@ -60,6 +61,13 @@ def create_tree(path, filenames=None, symlinks=None):
6061
os.symlink(symlink['target'], link_name)
6162

6263

64+
@pytest.mark.parametrize('cfg', CONFIG['get_output_file'])
65+
def test_get_output_file(cfg):
66+
"""Test getting output name for preprocessed files."""
67+
output_file = get_output_file(cfg['variable'], cfg['preproc_dir'])
68+
assert output_file == cfg['output_file']
69+
70+
6371
@pytest.fixture
6472
def root():
6573
"""Root function for tests."""

0 commit comments

Comments
 (0)