77import configparser
88import os
99import sys
10- from pathlib import Path
1110import yaml
1211import logging
1312
1615logger = 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