29 import nwpservice.wps.namelistwps
30 import nwpservice.wps.calcecmwfp
40 '''Class for managing preparation and execution of ecmwf plevels components
45 wpswrf_distro_path=None,
47 ecmwf_coeffs_path=None,
51 ungribbed_data_dict=None,
54 '''Initialises the class
56 Brings in the ecmwfplevels, time and ungribbed_data dicts, to be used
57 for making the request to nwpservice
61 wpswrf_distro_path : str
62 (Optional) Full path to the WPS/WRF distribution to be used.
63 This is assumed to have been installed in a way that is compatible
64 with the nwpservice module. If arg is not present, uses a default
67 (Optional) Full path to a directory (assumed to have already
68 been created), to be used as scratch space for setting up and
69 running this geogrid instance. If arg is not present, uses a
72 (Optional) Full path to ecmwf_coeffs file. If arg is not present,
74 start_time_dt : datetime
75 Time of first ungribbed sfc/ml file
76 stop_time_dt : datetime
77 Time of last ungribbed sfc/ml file
79 Interval (in hours) between ungribbed sfc/ml files
80 ungribbed_data_dict : dict
81 Necessary information on the ecmwf_ml and ecmwf_sfc ungribbed
82 files. We expect dict with entries {'FG_NAME' : <path>, ...},
83 and expect FG_NAME to be either DEFAULTS.ecmwf_sfc_ungrib_prefix()
84 or DEFAULT.ecmwf_ml_ungrib_prefix
86 Python logging level (e.g. logging.INFO)
98 LOGGER.debug(
'started')
104 if not wpswrf_distro_path:
105 wpswrf_distro_path = DEFAULTS.wpswrf_distro_path()
109 raise FileNotFoundError(
'wpswrf_distro_path not found: %s' %
112 if not working_rootdir:
113 working_rootdir = DEFAULTS.working_scratch_rootdir()
116 raise FileNotFoundError(
'working_rootdir not found: %s' %
122 if ecmwf_coeffs_path:
123 ecmwf_coeffs = ecmwf_coeffs_path
125 ecmwf_coeffs = DEFAULTS.ecmwf_coeffs()
126 if not os.path.isfile(ecmwf_coeffs):
127 raise FileNotFoundError(
'ecmwf_coeffs: %s' % ecmwf_coeffs)
133 if not ungribbed_data_dict:
134 raise ValueError(
"ungribbed_data_dict not defined")
137 sfc_prefix = DEFAULTS.ecmwf_sfc_ungrib_prefix()
138 ml_prefix = DEFAULTS.ecmwf_ml_ungrib_prefix()
141 ungribbed_data_keys = ungribbed_data_dict.keys()
143 raise ValueError(
'Unable to retrieve ungribbed_data_keys')
144 if sfc_prefix
not in ungribbed_data_keys:
145 raise ValueError(
'Missing key: %s' % sfc_prefix)
146 if ml_prefix
not in ungribbed_data_keys:
147 raise ValueError(
'Missing key: %s' % ml_prefix)
151 ml_path = ungribbed_data_dict[ml_prefix]
152 sfc_path = ungribbed_data_dict[sfc_prefix]
153 curr_dt = start_time_dt
154 while curr_dt <= stop_time_dt:
155 LOGGER.debug(
'curr_dt: %s' % curr_dt)
156 curr_timestr = curr_dt.strftime(
'%Y-%m-%d_%H')
157 LOGGER.debug(
'curr_timestr: %s' % curr_timestr)
159 ml_filepath = os.path.join(ml_path, ml_prefix +
':' + curr_timestr)
160 sfc_filepath = os.path.join(sfc_path, sfc_prefix +
':' + curr_timestr)
161 if not os.path.isfile(ml_filepath):
162 raise FileNotFoundError(
'Input not found: %s' % ml_filepath)
163 if not os.path.isfile(sfc_filepath):
164 raise FileNotFoundError(
'Input not found: %s' % sfc_filepath)
165 curr_dt += datetime.timedelta(hours=hours_intvl)
183 '''Set up and run, via nwpservice module, instance of ecmwfplevels
187 ecmwfplevels_output_stagedir : str
188 (Optional) Full path to dir where ecmwfplevels files will be staged.
189 If not specified, no staging will be done. If specified, we
190 assume the dir already exists.
195 Dictionary with manifest of the ecmwfplevels files and the location
196 (if applicable) of staged files
200 LOGGER.debug(
'Start run_ecmwfplevels()')
203 ecmwfp_status_dict = {}
208 if ecmwfplevels_output_stagedir:
209 if os.path.isdir(ecmwfplevels_output_stagedir)
and \
210 os.access(ecmwfplevels_output_stagedir, os.W_OK):
211 LOGGER.debug(
'Output stage dir: %s' % ecmwfplevels_output_stagedir)
213 raise FileNotFoundError(
'Problem with output stage dir: %s' %
214 ecmwfplevels_output_stagedir)
225 'ecmwfplevels_namelist.wps')
226 LOGGER.debug(
'namelist_wps_path: %s' % namelist_wps_path)
231 'ecmwfplevels_rundir')
238 share_section_dict = {
240 'start_date_list' : [start_time_str],
241 'end_date_list' : [end_time_str],
242 'interval_seconds' : interval_secs
244 LOGGER.debug(
'share_section_dict: %s' % share_section_dict)
245 metgrid_section_dict = {
246 'fg_name' : [DEFAULTS.ecmwf_sfc_ungrib_prefix(),
247 DEFAULTS.ecmwf_ml_ungrib_prefix()]
249 LOGGER.debug(
'metgrid_section_dict: %s' % metgrid_section_dict)
251 section_data_dict = {
'share' : share_section_dict,
252 'metgrid' : metgrid_section_dict}
253 LOGGER.debug(
'section_data_dict: %s' % section_data_dict)
256 myobj = nwpservice.wps.namelistwps.NamelistWpsWriter(
257 destpath=namelist_wps_path,
258 section_data_dict=section_data_dict,
261 LOGGER.debug(
'Writing namelist: %s' % namelist_wps_path)
266 nwpecmwfp_obj = nwpservice.wps.calcecmwfp.CalcEcmwfPlevels(
268 wpswrf_rundir=wpswrf_rundir,
271 namelist_wps=namelist_wps_path,
272 output_dir=ecmwfplevels_output_stagedir,
276 nwpecmwfp_obj.setup()
277 output_manifest = nwpecmwfp_obj.run()
278 LOGGER.debug(
'output_manifest: %s' % output_manifest)
285 stage_success = nwpecmwfp_obj.stage_output(auxfiles=
False)
287 staging_dir = ecmwfplevels_output_stagedir
293 'ecmwfp_output_manifest' : output_manifest,
294 'staging_dir' : staging_dir
297 LOGGER.debug(
'return_dict: %s' % return_dict)