Skip to content

Commit 4071aa7

Browse files
authored
Merge branch 'main' into 558_remove_update_recipe_file_KGOs
2 parents ce4dda2 + ff616f0 commit 4071aa7

7 files changed

Lines changed: 263 additions & 67 deletions

File tree

CMEW/app/configure_standardise/bin/command_line.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# The LICENSE.md file contains full licensing details.
33
import argparse
44
from create_variables_file import create_variables_file
5+
from create_request_file import create_request_file
56

67

78
def parse_args_for_create_variables_file(arguments):
@@ -70,3 +71,119 @@ def main_for_create_variables_file(arguments=None):
7071
args.stream_config_fp,
7172
args.output_filepath,
7273
)
74+
75+
76+
def parse_args_for_create_request_file(arguments):
77+
"""
78+
Return the names and values of the command line arguments for
79+
:func:`main_for_create_request_file`.
80+
81+
Parameters
82+
----------
83+
arguments : :obj:`list` of :obj:`str`
84+
The command line arguments to be parsed.
85+
86+
Returns
87+
-------
88+
:class:`argparse.Namespace`
89+
The names and values of the command line arguments.
90+
"""
91+
parser = argparse.ArgumentParser(
92+
description=(
93+
"Create a request file to standardise model data with CDDS."
94+
),
95+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
96+
)
97+
parser.add_argument(
98+
"--dataset",
99+
help="The model run to be extracted from MASS and processed.",
100+
)
101+
parser.add_argument(
102+
"--output_filepath",
103+
help=(
104+
"The full path to the file where the "
105+
"request file will be written."
106+
),
107+
)
108+
parser.add_argument(
109+
"--defaults_path",
110+
help=(
111+
"The full path to the file where the "
112+
"default values for a CDDS request are written."
113+
),
114+
)
115+
parser.add_argument(
116+
"--mip_table_dir",
117+
help=(
118+
"The full path to the directory containing the "
119+
"MIP tables for CDDS to use."
120+
),
121+
)
122+
parser.add_argument(
123+
"--model_runs_yml_fp",
124+
help=(
125+
"The full path to the YAML file "
126+
"containing details of the model runs."
127+
),
128+
)
129+
parser.add_argument(
130+
"--root_proc_dir",
131+
help=(
132+
"The full path to the directory where CDDS "
133+
"should store data for the processing workflow."
134+
),
135+
)
136+
parser.add_argument(
137+
"--root_data_dir",
138+
help=(
139+
"The full path to the directory where CDDS "
140+
"should store raw and standardised data."
141+
),
142+
)
143+
parser.add_argument(
144+
"--variables_file",
145+
help=(
146+
"The full path to the file where the "
147+
"variables to be retrieved by CDDS are written."
148+
),
149+
)
150+
parser.add_argument(
151+
"--raw_data_dir_mode",
152+
help=("Whether to save or reuse raw CDDS data files."),
153+
)
154+
return parser.parse_args(arguments)
155+
156+
157+
def main_for_create_request_file(arguments=None):
158+
"""
159+
Create a request file to standardise model data with CDDS.
160+
161+
Parameters
162+
----------
163+
arguments : :obj:`list` of :obj:`str`
164+
The command line arguments to be parsed.
165+
"""
166+
# Parse the arguments.
167+
args = parse_args_for_create_request_file(arguments)
168+
169+
# Run the code.
170+
print(f"dataset: {args.dataset}"),
171+
print(f"output_filepath: {args.output_filepath}"),
172+
print(f"defaults_path: {args.defaults_path}"),
173+
print(f"mip_table_dir: {args.mip_table_dir}"),
174+
print(f"model_runs_yml_fp: {args.model_runs_yml_fp}"),
175+
print(f"root_proc_dir: {args.root_proc_dir}"),
176+
print(f"root_data_dir: {args.root_data_dir}"),
177+
print(f"variables_file: {args.variables_file}"),
178+
print(f"raw_data_dir_mode: {args.raw_data_dir_mode}"),
179+
create_request_file(
180+
args.dataset,
181+
args.output_filepath,
182+
args.defaults_path,
183+
args.mip_table_dir,
184+
args.model_runs_yml_fp,
185+
args.root_proc_dir,
186+
args.root_data_dir,
187+
args.variables_file,
188+
args.raw_data_dir_mode,
189+
)

CMEW/app/configure_standardise/bin/configure_standardise.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ set -eux
77

88
echo "Running configure_standardise"
99

10-
# Create request configuration file.
11-
cmew-esmvaltool-env create_request_file.py
12-
1310
# Create CDDS directory structure and variables list.
1411
cmew-standardise-env create_cdds_directory_structure "${REQUEST_PATH}"
1512
cmew-standardise-env prepare_generate_variable_list "${REQUEST_PATH}"

CMEW/app/configure_standardise/bin/configure_standardise_conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@ def mock_data_dir():
88
return Path(__file__).parent.parent.parent / "unittest" / "mock_data"
99

1010

11+
def model_runs_yml_fp():
12+
return mock_data_dir() / "model_runs.yml"
13+
14+
1115
def kgo_dir():
1216
return Path(__file__).parent.parent.parent / "unittest" / "kgo"
1317

1418

19+
def request_u_cw673_cfg_fp():
20+
return kgo_dir() / "request_u-cw673.cfg"
21+
22+
1523
def variables_txt_fp():
1624
return kgo_dir() / "variables.txt"
1725

@@ -20,5 +28,9 @@ def etc_dir():
2028
return Path(__file__).parent.parent / "etc"
2129

2230

31+
def request_defaults_yml_fp():
32+
return etc_dir() / "request_defaults.yml"
33+
34+
2335
def streams_yml_fp():
2436
return etc_dir() / "streams.yml"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python
2+
# (C) Crown Copyright 2026, Met Office.
3+
# The LICENSE.md file contains full licensing details.
4+
from command_line import main_for_create_request_file
5+
6+
7+
if __name__ == "__main__":
8+
main_for_create_request_file()

CMEW/app/configure_standardise/bin/create_request_file.py

100755100644
Lines changed: 95 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import configparser
88
import os
99
import sys
10-
from pathlib import Path
1110
import yaml
1211
import logging
1312

@@ -16,20 +15,22 @@
1615
logger = logging.getLogger(filename)
1716

1817

19-
def load_request_defaults():
18+
def load_request_defaults(defaults_path):
2019
"""
2120
Load default values for request file.
2221
22+
Parameters
23+
----------
24+
defaults_path : str
25+
Path to the default configuration file.
26+
2327
Returns
2428
-------
2529
dict
2630
CDDS request configuration default settings.
2731
"""
28-
# Get path to default settings
29-
defaults = os.environ["REQUEST_DEFAULTS_PATH"]
30-
3132
# Read the defaults
32-
with open(defaults, "r") as f:
33+
with open(defaults_path, "r") as f:
3334
config = yaml.safe_load(f)
3435

3536
logger.debug(
@@ -84,28 +85,54 @@ def list_streams(variables_file):
8485
return stream_str
8586

8687

87-
def create_request(model_run):
88+
def create_request(
89+
defaults_path,
90+
dataset,
91+
mip_table_dir,
92+
model_runs_yml_fp,
93+
root_proc_dir,
94+
root_data_dir,
95+
variables_file,
96+
raw_data_dir_mode,
97+
):
8898
"""
8999
Build a CDDS request configuration for a run identified by a suite_id.
90100
91101
Uses information from the model_runs.yml file.
92102
103+
Parameters
104+
----------
105+
defaults_path : str
106+
The full path to the file containing default values for a CDDS request.
107+
dataset : str
108+
The model run to be extracted from MASS and processed.
109+
mip_table_dir : str
110+
The path to the MIP table directory to use from CDDS.
111+
model_runs_yml_fp : str
112+
The full path to the YAML file containing details of the model runs.
113+
root_proc_dir : str
114+
The directory for use when processing data with CDDS.
115+
root_data_dir : str
116+
The root directory for CDDS to store data.
117+
variables_file : str
118+
The full path to the file containing
119+
the list of variables to be processed.
120+
raw_data_dir_mode : str
121+
Whether to save or reuse raw CDDS data files.
122+
93123
Returns
94124
-------
95125
dict
96126
CDDS request configuration.
97127
"""
98-
defaults = load_request_defaults()
99-
100-
mip_table_dir = os.environ["MIP_TABLE_DIR"]
128+
defaults = load_request_defaults(defaults_path)
101129

102130
# Read the model run information from the model_runs.yml file
103-
model_runs_yaml = Path(os.environ["DATASETS_LIST_DIR"]) / "model_runs.yml"
104-
with open(model_runs_yaml, "r") as f:
105-
dataset_dict = yaml.safe_load(f)[model_run]
131+
with open(model_runs_yml_fp, "r") as file_handle:
132+
dataset_dict = yaml.safe_load(file_handle)[dataset]
106133
logger.debug(
107134
"Dataset % config:\n%s",
108-
model_run,
135+
dataset,
109136
dataset_dict,
110137
)
111138

@@ -124,21 +151,21 @@ def create_request(model_run):
124151
request["common"] = {
125152
**defaults["common"],
126153
"mip_table_dir": os.path.expanduser(mip_table_dir),
127-
"root_proc_dir": os.environ["ROOT_PROC_DIR"],
128-
"root_data_dir": os.environ["ROOT_DATA_DIR"],
154+
"root_proc_dir": root_proc_dir,
155+
"root_data_dir": root_data_dir,
129156
"workflow_basename": dataset_dict["suite_id"],
130157
}
131158
request["data"] = {
132159
**defaults["data"],
133160
"start_date": f"{dataset_dict['start_year']}-01-01T00:00:00",
134161
"end_date": f"{int(dataset_dict['end_year'])+1}-01-01T00:00:00",
135162
"model_workflow_id": dataset_dict["suite_id"],
136-
"streams": list_streams(os.environ["VARIABLES_PATH"]),
137-
"variable_list_file": os.environ["VARIABLES_PATH"],
163+
"streams": list_streams(variables_file),
164+
"variable_list_file": variables_file,
138165
}
139166
request["misc"] = dict(defaults["misc"])
140167
request["conversion"] = dict(defaults["conversion"])
141-
if os.environ["RAW_DATA_DIR_MODE"] == "use_saved":
168+
if raw_data_dir_mode == "use_saved":
142169
request["conversion"]["skip_extract"] = "True"
143170
request["netcdf_global_attributes"] = dict(
144171
defaults["netcdf_global_attributes"]
@@ -148,15 +175,15 @@ def create_request(model_run):
148175
return request
149176

150177

151-
def write_request(request, target_path):
152-
"""Write the request configuration to a file at ``target_path``.
178+
def write_request(request, output_filepath):
179+
"""Write the request configuration to a file at ``output_filepath``.
153180
154181
Parameters
155182
----------
156183
request : dict
157184
The request configuration.
158185
159-
target_path: Path
186+
output_filepath: Path
160187
The full path to the file
161188
where the request configuration will be written.
162189
"""
@@ -165,25 +192,57 @@ def write_request(request, target_path):
165192

166193
logger.debug("Writing request config:\n%s", cfg)
167194

168-
with open(target_path, mode="w") as file_handle:
195+
with open(output_filepath, mode="w") as file_handle:
169196
cfg.write(file_handle)
170197

171198

172-
def main():
199+
def create_request_file(
200+
dataset,
201+
output_filepath,
202+
defaults_path,
203+
mip_table_dir,
204+
model_runs_yml_fp,
205+
root_proc_dir,
206+
root_data_dir,
207+
variables_file,
208+
raw_data_dir_mode,
209+
):
173210
"""
174-
Generate and write the request file for the current task environment.
211+
Create a request file to standardise model data with CDDS.
175212
176-
The output file location is taken from the REQUEST_PATH environment
177-
variable. All other required inputs are read from the environment
178-
by ``create_request()``.
213+
Parameters
214+
----------
215+
dataset : str
216+
The model run to be extracted from MASS and processed.
217+
output_filepath : str
218+
The full path to the file where the
219+
request configuration will be written.
220+
defaults_path : str
221+
The full path to the file containing default values for a CDDS request.
222+
mip_table_dir : str
223+
The path to the MIP table directory to use from CDDS.
224+
model_runs_yml_fp : str
225+
The full path to the YAML file containing details of the model runs.
226+
root_proc_dir : str
227+
The directory for use when processing data with CDDS.
228+
root_data_dir : str
229+
The root directory for CDDS to store data.
230+
variables_file : str
231+
The full path to the file containing
232+
the list of variables to be processed.
233+
raw_data_dir_mode : str
234+
Whether to save or reuse raw CDDS data files.
179235
"""
180-
dataset = os.environ["CYLC_TASK_PARAM_dataset"].strip()
181236
logger.info("Creating CDDS request for dataset %s", dataset)
182237

183-
request = create_request(dataset)
184-
target_path = Path(os.environ["REQUEST_PATH"])
185-
write_request(request, target_path)
186-
187-
188-
if __name__ == "__main__":
189-
main()
238+
request = create_request(
239+
defaults_path,
240+
dataset,
241+
mip_table_dir,
242+
model_runs_yml_fp,
243+
root_proc_dir,
244+
root_data_dir,
245+
variables_file,
246+
raw_data_dir_mode,
247+
)
248+
write_request(request, output_filepath)

0 commit comments

Comments
 (0)