Skip to content

Commit 95af99a

Browse files
authored
Merge branch 'main' into 156_raw_data_location_configurable
2 parents 7ca1122 + 985d25f commit 95af99a

File tree

11 files changed

+79
-48
lines changed

11 files changed

+79
-48
lines changed

CMEW/app/add_datasets/bin/add_datasets_to_share.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def convert_str_to_facets(section):
9999
return section_dict
100100

101101

102-
def add_common_facets(dataset_dict, project="CMIP6"):
102+
def add_common_facets(dataset_dict, project):
103103
"""
104104
Add start year, end year and project to a dataset dictionary.
105105
@@ -109,7 +109,6 @@ def add_common_facets(dataset_dict, project="CMIP6"):
109109
A dictionary containing the facets of a dataset.
110110
project: str
111111
A string indicating the project to which the dataset belongs.
112-
Default is "CMIP6".
113112
114113
Returns
115114
-------
@@ -127,17 +126,23 @@ def add_common_facets(dataset_dict, project="CMIP6"):
127126
dataset_dict["end_year"] = end_year
128127
dataset_dict["project"] = project
129128

129+
# Add MOHC as an institute only for generated CMEW runs
130+
if dataset_dict["project"] == "ESMVal":
131+
dataset_dict["institute"] = "MOHC"
132+
130133
return dataset_dict
131134

132135

133-
def process_naml_file(naml_fp):
136+
def process_naml_file(naml_fp, project=None):
134137
"""
135138
Extract the datasets and their facets from a namelist file.
136139
137140
Parameters
138141
----------
139142
naml_fp: str
140143
The file path to the namelist file containing the datasets.
144+
project: str, optional
145+
A string indicating the project to which the dataset belongs.
141146
142147
Returns
143148
-------
@@ -148,7 +153,7 @@ def process_naml_file(naml_fp):
148153
sections = extract_sections_from_naml(naml_fp)
149154
for section in sections:
150155
dataset_dict = convert_str_to_facets(section)
151-
dataset_dict = add_common_facets(dataset_dict)
156+
dataset_dict = add_common_facets(dataset_dict, project)
152157
datasets.append(dataset_dict)
153158
return datasets
154159

@@ -269,11 +274,19 @@ def use_facet_as_key(filepath, key_facet="suite_id"):
269274
# Loop over the namelist files in the work directory
270275
for basename, nl_fp in dict_namelists_in_work_dir().items():
271276

272-
# Extract the datasets from each file
273-
datasets = process_naml_file(nl_fp)
277+
# Check if it's model runs
278+
if basename == "model_runs":
279+
280+
# Write the datasets to a YAML file with ESMVal project
281+
datasets = process_naml_file(nl_fp, "ESMVal")
282+
write_datasets_to_yaml(datasets, basename, target_dir)
283+
284+
# Check if it's CMIP6:
285+
if basename == "cmip6_datasets":
274286

275-
# Write the datasets to a YAML file in the target directory
276-
write_datasets_to_yaml(datasets, basename, target_dir)
287+
# Write the datasets to a YAML file with CMIP6 project
288+
datasets = process_naml_file(nl_fp, "CMIP6")
289+
write_datasets_to_yaml(datasets, basename, target_dir)
277290

278291
# Reformat the model_runs YAML file to use suite_ids as keys
279292
use_facet_as_key(f"{target_dir}/model_runs.yml")

CMEW/app/add_datasets/bin/test_add_datasets_to_share.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def test_add_common_facets(mock_env_vars):
133133
"project": "CMIP6",
134134
}
135135

136-
actual = add_common_facets(dataset_dict)
136+
actual = add_common_facets(dataset_dict, "CMIP6")
137137
assert actual == expected
138138

139139

@@ -161,7 +161,7 @@ def test_process_naml_file(path_to_mock_nl, mock_env_vars):
161161
},
162162
]
163163

164-
actual = process_naml_file(path_to_mock_nl)
164+
actual = process_naml_file(path_to_mock_nl, "CMIP6")
165165
assert actual == expected
166166

167167

CMEW/app/add_datasets/meta/rose-meta.conf

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,28 @@
44
[namelist:cmip6_datasets]
55
duplicate=true
66

7-
[namelist:cmip6_datasets=alias]
7+
[namelist:cmip6_datasets=experiment_id]
8+
compulsory=true
9+
description=The experiment ID of the dataset.
10+
help=For example, 'historical', 'piControl' or 'abrupt4xco2'.
11+
sort-key=3
12+
type=quoted
13+
14+
[namelist:cmip6_datasets=grid]
15+
compulsory=true
16+
description=Whether the data is on the native grid of the model.
17+
help=This will either be 'gn' for native grid or 'gr' for regridded.
18+
sort-key=5
19+
values="gn","gr"
20+
21+
[namelist:cmip6_datasets=institute]
22+
compulsory=true
23+
description=The institute that produced the dataset.
24+
help=For example, 'MOHC' or 'EC-Earth-Consortium'.
25+
sort-key=2
26+
type=quoted
27+
28+
[namelist:cmip6_datasets=label_for_plots]
829
compulsory=false
930
description=A label to use on plots for this dataset.
1031
help=It is recommended to limit the length of this string as much as is
@@ -16,42 +37,21 @@ pattern=^".{0,25}"$
1637
sort-key=6
1738
type=quoted
1839

19-
[namelist:cmip6_datasets=dataset]
40+
[namelist:cmip6_datasets=model_id]
2041
compulsory=true
21-
description=The 'model_id' (or 'source_id') of the CMIP6 dataset.
42+
description=The 'source_id' of the CMIP6 dataset.
2243
help=For example, 'UKESM1-0-LL' or 'ACCESS-ESM1-5'.
2344
sort-key=1
2445
type=quoted
2546

26-
[namelist:cmip6_datasets=ensemble]
47+
[namelist:cmip6_datasets=variant_label]
2748
compulsory=true
28-
description=Also known as 'variant label'.
49+
description=Also known as 'ensemble'.
2950
help=Must adhere to CMIP6 variant label format: r<int>i<int>p<int>f<int>.
3051
=For example, 'r2i1p1f3'. https://help.ceda.ac.uk/article/4801-cmip6-data
31-
pattern=^r[0-9]+i[0-9]+p[0-9]+f[0-9]+$
52+
pattern=^"r[0-9]+i[0-9]+p[0-9]+f[0-9]+"$
3253
sort-key=4
3354

34-
[namelist:cmip6_datasets=exp]
35-
compulsory=true
36-
description=The experiment ID of the dataset.
37-
help=For example, 'historical', 'piControl' or 'abrupt4xco2'.
38-
sort-key=3
39-
type=quoted
40-
41-
[namelist:cmip6_datasets=grid]
42-
compulsory=true
43-
description=Whether the data is on the native grid of the model.
44-
help=This will either be 'gn' for native grid or 'gr' for regridded.
45-
sort-key=5
46-
values="gn","gr"
47-
48-
[namelist:cmip6_datasets=institute]
49-
compulsory=true
50-
description=The institute that produced the dataset.
51-
help=For example, 'MOHC' or 'EC-Earth-Consortium'.
52-
sort-key=2
53-
type=quoted
54-
5555
[namelist:model_runs]
5656
duplicate=true
5757

CMEW/app/add_datasets/rose-app.conf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ source=(namelist:cmip6_datasets(:))
1111
source=(namelist:model_runs(:))
1212

1313
[namelist:cmip6_datasets(1)]
14-
alias="CMIP6 dataset 001"
15-
dataset="UKESM1-0-LL"
16-
ensemble=r5i1p1f3
17-
exp="historical"
14+
experiment_id="historical"
1815
grid="gn"
1916
institute="MOHC"
17+
label_for_plots="CMIP6 dataset 001"
18+
model_id="UKESM1-0-LL"
19+
variant_label="r5i1p1f3"
2020

2121
[namelist:model_runs(1)]
2222
calendar="gregorian"

CMEW/app/configure_for/bin/update_recipe_file.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,20 @@ def add_extra_datasets(recipe, yaml_filepath):
147147
with open(yaml_filepath, "r") as file_handle:
148148
extra_datasets = yaml.safe_load(file_handle)
149149

150+
# ESMValTool recipes expect keys to be "dataset", "ensemble", "exp" etc.
151+
variables_conversion = {
152+
"label_for_plots": "alias",
153+
"model_id": "dataset",
154+
"variant_label": "ensemble",
155+
"experiment_id": "exp",
156+
}
157+
158+
# Convert the variable names in the extra datasets
159+
for dataset in extra_datasets:
160+
for old_key, new_key in variables_conversion.items():
161+
if old_key in dataset:
162+
dataset[new_key] = dataset.pop(old_key)
163+
150164
# Add the datasets to the datasets section of the recipe
151165
recipe["datasets"].extend(extra_datasets)
152166

CMEW/app/configure_standardise/bin/create_request_file.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def create_request():
5454
"model_workflow_id": os.environ["SUITE_ID"],
5555
"model_workflow_revision": "not used except with data request",
5656
"start_date": f"{os.environ['START_YEAR']}-01-01T00:00:00",
57-
"streams": "apm",
57+
# For now there is only one stream, for Amon and Emon mip.
58+
"streams": os.environ["STREAM_ID"],
5859
"variable_list_file": os.environ["VARIABLES_PATH"],
5960
}
6061
request["misc"] = {

CMEW/app/configure_standardise/bin/create_variables_file.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
# (C) Crown Copyright 2024-2025, Met Office.
2+
# (C) Crown Copyright 2024-2026, Met Office.
33
# The LICENSE.md file contains full licensing details.
44
"""
55
Generates the variables.txt file from the ESMValTool recipe.
@@ -52,8 +52,8 @@ def parse_variables_from_recipe(recipe_path):
5252
List of variables from the ESMValTool recipe,
5353
formatted as ``<mip>/<variable>:<stream>``.
5454
"""
55-
# For now, hard-code stream to apm, this is correct for Amon and Emon mip.
56-
stream = "apm"
55+
# For now there is only one stream, for Amon and Emon mip.
56+
stream = os.environ["STREAM_ID"]
5757
recipe = Recipe(recipe_path)
5858
diagnostics = recipe.data["diagnostics"]
5959
formatted_variables = []

CMEW/app/configure_standardise/bin/test_create_request_file.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def test_create_request(monkeypatch):
1818
monkeypatch.setenv("SUITE_ID", "u-az513")
1919
monkeypatch.setenv("VARIABLES_PATH", "/path/to/variables.txt")
2020
monkeypatch.setenv("VARIANT_LABEL", "r1i1p1f1")
21+
monkeypatch.setenv("STREAM_ID", "apm")
2122

2223
config = create_request()
2324
actual = {

CMEW/app/configure_standardise/bin/test_create_variables_file.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
# (C) Crown Copyright 2024-2025, Met Office.
2+
# (C) Crown Copyright 2024-2026, Met Office.
33
# The LICENSE.md file contains full licensing details.
44
"""
55
Tests for configure_standardise
@@ -8,7 +8,8 @@
88
from pathlib import Path
99

1010

11-
def test_parse_radiation_budget_variables():
11+
def test_parse_radiation_budget_variables(monkeypatch):
12+
monkeypatch.setenv("STREAM_ID", "apm")
1213
recipe_path = (
1314
Path(__file__).parent.parent.parent
1415
/ "unittest"

CMEW/app/unittest/kgo/extended_radiation_budget_recipe.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ datasets:
99
dataset: HadGEM3-GC31-LL
1010
end_year: 1993
1111
ensemble: r1i1p1f3
12-
exp: amip
12+
exp: historical
1313
grid: gn
1414
institute: MOHC
1515
project: ESMVal

0 commit comments

Comments
 (0)