Skip to content

Commit 26ddc08

Browse files
authored
Merge branch 'main' into 371-update-the-pr-checklist
2 parents 301622f + c2d31f4 commit 26ddc08

File tree

13 files changed

+146
-33
lines changed

13 files changed

+146
-33
lines changed

CMEW/app/add_datasets/bin/add_datasets_to_share.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,40 @@ def dict_namelists_in_work_dir():
225225
return filepaths
226226

227227

228+
def use_facet_as_key(filepath, key_facet="suite_id"):
229+
"""
230+
Edit a YAML file in place, from a list to a dictionary.
231+
232+
The keys of the new dictionary are the values of the specified facet,
233+
wich must be present in each section of the list and be unique.
234+
235+
Parameters
236+
----------
237+
filepath: str
238+
The file path to the YAML file to be edited.
239+
key_facet: str
240+
The facet to use as the key in the new dictionary.
241+
Defaults to 'suite_id', which is the unique identifier for now.
242+
"""
243+
# Read the YAML file that has datasets as a list
244+
with open(filepath, "r") as f:
245+
data = yaml.safe_load(f)
246+
247+
# Create a new dictionary with the same sections as the list
248+
new_dict = {}
249+
for section in data:
250+
251+
# Use the facet as a unique key
252+
unique = section[key_facet]
253+
254+
# The information in each section remains unchanged
255+
new_dict[unique] = section
256+
257+
# Write the new dictionary back to the existing YAML filepath
258+
with open(filepath, "w") as f:
259+
yaml.dump(new_dict, f)
260+
261+
228262
if __name__ == "__main__":
229263
# Read the target (shared) directory from the environment
230264
target_dir = os.environ["DATASETS_LIST_DIR"]
@@ -240,3 +274,6 @@ def dict_namelists_in_work_dir():
240274

241275
# Write the datasets to a YAML file in the target directory
242276
write_datasets_to_yaml(datasets, basename, target_dir)
277+
278+
# Reformat the model_runs YAML file to use suite_ids as keys
279+
use_facet_as_key(f"{target_dir}/model_runs.yml")

CMEW/app/add_datasets/bin/test_add_datasets_to_share.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
write_dict_to_yaml,
99
write_datasets_to_yaml,
1010
dict_namelists_in_work_dir,
11+
use_facet_as_key,
1112
)
1213
from pathlib import Path
1314
import pytest
1415
import yaml
16+
import shutil
1517
import tempfile
1618
from unittest.mock import patch
1719

@@ -24,36 +26,47 @@ def mock_env_vars(monkeypatch):
2426

2527

2628
@pytest.fixture
27-
def path_to_kgo_yaml():
29+
def path_to_mock_nl():
2830
path = (
2931
Path(__file__).parent.parent.parent
3032
/ "unittest"
31-
/ "kgo"
32-
/ "model_runs.yml"
33+
/ "mock_data"
34+
/ "model_runs.nl"
3335
)
3436
return str(path)
3537

3638

3739
@pytest.fixture
38-
def path_to_mock_nl():
40+
def path_to_kgo_dict():
41+
path = (
42+
Path(__file__).parent.parent.parent
43+
/ "unittest"
44+
/ "kgo"
45+
/ "basic_dict.yml"
46+
)
47+
return path
48+
49+
50+
@pytest.fixture
51+
def path_to_mock_yaml_list():
3952
path = (
4053
Path(__file__).parent.parent.parent
4154
/ "unittest"
4255
/ "mock_data"
43-
/ "model_runs.nl"
56+
/ "model_runs_as_list.yml"
4457
)
4558
return str(path)
4659

4760

4861
@pytest.fixture
49-
def path_to_kgo_dict():
62+
def path_to_kgo_yaml_dict():
5063
path = (
5164
Path(__file__).parent.parent.parent
5265
/ "unittest"
5366
/ "kgo"
54-
/ "basic_dict.yml"
67+
/ "model_runs_as_dict.yml"
5568
)
56-
return path
69+
return str(path)
5770

5871

5972
def test_extract_sections_from_naml(path_to_mock_nl):
@@ -152,7 +165,6 @@ def test_process_naml_file(path_to_mock_nl, mock_env_vars):
152165
assert actual == expected
153166

154167

155-
# This one was only *some* random copying of Google
156168
def test_write_dict_to_yaml(path_to_kgo_dict):
157169
# Note the keys are not alphabetical here but are in the output
158170
test_dict = {
@@ -204,3 +216,22 @@ def test_dict_namelists_in_work_dir(mock_dirname, mock_listdir, mock_env_vars):
204216
}
205217
actual = dict_namelists_in_work_dir()
206218
assert expected == actual
219+
220+
221+
def test_use_facet_as_key(path_to_mock_yaml_list, path_to_kgo_yaml_dict):
222+
# Copy known input to a temp file
223+
with tempfile.NamedTemporaryFile() as tmp:
224+
shutil.copyfile(path_to_mock_yaml_list, tmp.name)
225+
226+
# The filepath is given by .name
227+
use_facet_as_key(str(tmp.name))
228+
229+
# Read the result
230+
tmp.seek(0)
231+
actual = yaml.safe_load(tmp)
232+
233+
# Load the expected output
234+
with open(path_to_kgo_yaml_dict, "r") as file_handle:
235+
expected = yaml.safe_load(file_handle)
236+
237+
assert actual == expected

CMEW/app/configure_for/bin/test_update_recipe_file.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ def mock_env_vars(monkeypatch):
1212
# Time window
1313
monkeypatch.setenv("START_YEAR", "1993")
1414
monkeypatch.setenv("NUMBER_OF_YEARS", "1")
15-
monkeypatch.setenv("LABEL_FOR_PLOTS", "Test Label")
16-
monkeypatch.setenv("REF_LABEL_FOR_PLOTS", "Ref Test Label")
1715

1816
# Reference run metadata
1917
monkeypatch.setenv("REF_MODEL_ID", "HadGEM3-GC31-LL")
2018
monkeypatch.setenv("REF_VARIANT_LABEL", "r1i1p1f3")
19+
monkeypatch.setenv("REF_LABEL_FOR_PLOTS", "Ref Test Label")
20+
monkeypatch.setenv("REF_EXPERIMENT_ID", "historical")
2121

2222
# Evaluation run metadata
2323
monkeypatch.setenv("MODEL_ID", "UKESM1-0-LL")
2424
monkeypatch.setenv("VARIANT_LABEL", "r1i1p1f1")
25+
monkeypatch.setenv("LABEL_FOR_PLOTS", "Test Label")
26+
monkeypatch.setenv("EXPERIMENT_ID", "amip")
2527

2628
# For adding CMIP6 datasets
2729
monkeypatch.setenv(

CMEW/app/configure_for/bin/update_recipe_file.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ def update_recipe(recipe_path):
3434
3535
datasets:
3636
- {activity: <activity>, alias: <ref_alias>, dataset: <ref_model_id>,
37-
end_year: <end_year>, ensemble: <ref_variant>, exp: <exp>, grid: <grid>,
38-
project: <project>, start_year: <start_year>}
37+
end_year: <end_year>, ensemble: <ref_variant>, exp: <ref_experiment_id>,
38+
grid: <grid>, project: <project>, start_year: <start_year>}
3939
- {activity: <activity>, alias: <alias>, dataset: <eval_model_id>,
40-
end_year: <end_year>, ensemble: <eval_variant>, exp: <exp>, grid: <grid>,
41-
project: <project>, start_year: <start_year>}
40+
end_year: <end_year>, ensemble: <eval_variant>,
41+
exp: <eval_experiment_id>, grid: <grid>, project: <project>,
42+
start_year: <start_year>}
4243
4344
Notes
4445
-----
@@ -66,8 +67,10 @@ def update_recipe(recipe_path):
6667
# Model metadata from environment
6768
ref_model_id = os.environ["REF_MODEL_ID"]
6869
ref_variant = os.environ["REF_VARIANT_LABEL"]
70+
ref_experiment_id = os.environ["REF_EXPERIMENT_ID"]
6971
eval_model_id = os.environ["MODEL_ID"]
7072
eval_variant = os.environ["VARIANT_LABEL"]
73+
eval_experiment_id = os.environ["EXPERIMENT_ID"]
7174

7275
# Read given reference alias or use the suite ID
7376
if os.environ.get("REF_LABEL_FOR_PLOTS"):
@@ -91,14 +94,13 @@ def update_recipe(recipe_path):
9194
"one for the reference and one for the evaluation run."
9295
)
9396

94-
# Reference dataset: treat as a GCModelDev / ESMVal / amip run,
95-
# using REF_MODEL_ID & REF_VARIANT_LABEL, with the configured time window.
97+
# Reference dataset
9698
ref_dataset = datasets[0]
9799
ref_dataset.update(
98100
{
99101
"dataset": ref_model_id,
100102
"project": "ESMVal",
101-
"exp": "amip",
103+
"exp": ref_experiment_id,
102104
"activity": "ESMVal",
103105
"institute": "MOHC",
104106
"ensemble": ref_variant,
@@ -108,13 +110,13 @@ def update_recipe(recipe_path):
108110
}
109111
)
110112

111-
# Evaluation dataset: ESMVal / amip run using MODEL_ID and VARIANT_LABEL
113+
# Evaluation dataset
112114
eval_dataset = datasets[1]
113115
eval_dataset.update(
114116
{
115117
"dataset": eval_model_id,
116118
"project": "ESMVal",
117-
"exp": "amip",
119+
"exp": eval_experiment_id,
118120
"activity": "ESMVal",
119121
"institute": "MOHC",
120122
"ensemble": eval_variant,

CMEW/app/configure_standardise/bin/configure_standardise.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
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
# Send the output from 'set -x' to 'stdout' rather than 'stderr'.
55
BASH_XTRACEFD=1
@@ -20,9 +20,11 @@ echo "[INFO] Using REQUEST_PATH_EVAL=${REQUEST_PATH_EVAL}"
2020
: "${REF_MODEL_ID:?REF_MODEL_ID must be set}"
2121
: "${REF_SUITE_ID:?REF_SUITE_ID must be set}"
2222
: "${REF_CALENDAR:?REF_CALENDAR must be set}"
23+
: "${REF_EXPERIMENT_ID:??REF_EXPERIMENT_ID must be set}"
2324
: "${MODEL_ID:?MODEL_ID (evaluation) must be set}"
2425
: "${SUITE_ID:?SUITE_ID (evaluation) must be set}"
2526
: "${CALENDAR:?CALENDAR (evaluation) must be set}"
27+
: "${EXPERIMENT_ID:?EXPERIMENT_ID (evaluation) must be set}"
2628

2729
# ---------------------------------------------------------------------------
2830
# 1. Create variables.txt once (shared by both runs)
@@ -40,6 +42,7 @@ create_for_run() {
4042
local run_suite_id=""
4143
local run_calendar=""
4244
local run_variant=""
45+
local run_experiment_id=""
4346
local run_request=""
4447

4548
case "${RUN_LABEL}" in
@@ -48,6 +51,7 @@ create_for_run() {
4851
run_suite_id="${REF_SUITE_ID}"
4952
run_calendar="${REF_CALENDAR}"
5053
run_variant="${REF_VARIANT_LABEL:-}"
54+
run_experiment_id="${REF_EXPERIMENT_ID}"
5155
run_request="${REQUEST_PATH_REF}"
5256
;;
5357
EVAL)
@@ -56,6 +60,7 @@ create_for_run() {
5660
run_suite_id="${SUITE_ID}"
5761
run_calendar="${CALENDAR}"
5862
run_variant="${VARIANT_LABEL:-}"
63+
run_experiment_id="${EXPERIMENT_ID}"
5964
run_request="${REQUEST_PATH_EVAL}"
6065
;;
6166
*)
@@ -70,6 +75,7 @@ create_for_run() {
7075
export SUITE_ID="${run_suite_id}"
7176
export CALENDAR="${run_calendar}"
7277
export VARIANT_LABEL="${run_variant}"
78+
export EXPERIMENT_ID="${run_experiment_id}"
7379
export REQUEST_PATH="${run_request}"
7480

7581
echo "[INFO] Creating request for ${RUN_LABEL} run at: ${REQUEST_PATH}"

CMEW/app/configure_standardise/bin/create_request_file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def create_request():
2525
"base_date": "1850-01-01T00:00:00",
2626
"branch_method": "no parent",
2727
"calendar": os.environ["CALENDAR"],
28-
"experiment_id": "amip",
28+
"experiment_id": os.environ["EXPERIMENT_ID"],
2929
"institution_id": os.environ["INSTITUTION_ID"],
3030
"license": "GCModelDev model data is licensed under the Open Government License v3 (https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/)", # noqa: E501
3131
"mip": "ESMVal",

CMEW/app/configure_standardise/bin/test_create_request_file.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def test_create_request(monkeypatch):
1010
monkeypatch.setenv("START_YEAR", "1993")
1111
monkeypatch.setenv("NUMBER_OF_YEARS", "1")
1212
monkeypatch.setenv("CALENDAR", "360_day")
13+
monkeypatch.setenv("EXPERIMENT_ID", "amip")
1314
monkeypatch.setenv("INSTITUTION_ID", "MOHC")
1415
monkeypatch.setenv("MODEL_ID", "UKESM1-0-LL")
1516
monkeypatch.setenv("ROOT_PROC_DIR", "/path/to/proc/dir/")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
u-bv526:
2+
calendar: 360_day
3+
end_year: 2002
4+
label_for_plots: HadGEM3-GC3.1 N96ORCA1
5+
model_id: HadGEM3-GC31-LL
6+
project: CMIP6
7+
start_year: 1993
8+
suite_id: u-bv526
9+
variant_label: r5i1p1f3
10+
u-cw673:
11+
calendar: gregorian
12+
end_year: 2002
13+
label_for_plots: HadGEM3-GC5E-LL N96ORCA1
14+
model_id: HadGEM3-GC5E-LL
15+
project: CMIP6
16+
start_year: 1993
17+
suite_id: u-cw673
18+
variant_label: r1i1p1f1

CMEW/app/unittest/kgo/test_updated_radiation_budget_recipe.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ datasets:
1010
dataset: HadGEM3-GC31-LL
1111
end_year: 1993
1212
ensemble: r1i1p1f3
13-
exp: amip
13+
exp: historical
1414
grid: gn
1515
institute: MOHC
1616
project: ESMVal
File renamed without changes.

0 commit comments

Comments
 (0)