30 import nwpservice.wrf.wrf
39 '''Class for managing preparation and execution of wrf components
45 wpswrf_distro_path=None,
55 bypass_namelist_input=None,
58 '''Check and initialise the class
63 wpswrf_distro_path : str
64 (Optional) Full path to the WPS/WRF distribution to be used.
65 This is assumed to have been installed in a way that is compatible
66 with the nwpservice module. If arg is not present, uses a default
69 (Optional) Full path to a directory (assumed to have already
70 been created), to be used as scratch space for setting up and
71 running this real instance. If arg is not present, uses a
73 start_time_dt : datetime
74 Time of first ungribbed file(s)
75 stop_time_dt : datetime
76 Time of last ungribbed file(s)
78 Interval (in hours) between ungribbed files
80 Number of domain nests represented in geogrid data, and in
81 the metgrid data that will be produced
82 nests_defn_dict : dict
83 Dict of nest definitions (of use if generating namelist.input)
85 (Optional) Number of MPI tasks to use for running this instance
86 of metgrid. If arg is not present, non-MPI execution is assumed.
88 Necessary information on the real files. We expect dict with
89 entries {'path' : <path>, 'filelist' : [list of real files]}.
90 This implies that the real files are all in the same src
93 Python logging level (e.g. logging.INFO)
94 bypass_namelist_input : str
95 This is an assumed-correct namelist.input to be used for
96 execution of real. Its presence bypasses the normal process
97 of creating a namelist.input, and puts complete trust in the
98 correctness of the provided namelist.input. If this argument
99 is present, then any values for start_time_dt, stop_time_dt,
100 hours_intvl and num_nests are ignored. This is meant primarily
101 for devtest operations, and probably shouldn't be used for
107 These are examples of the real_data_dict
112 'path' : '/path/to/dir/with/real/files'
113 'filelist' : ['wrfbdy_d01',
127 LOGGER.debug(
'started')
138 if bypass_namelist_input:
139 if not os.path.isfile(bypass_namelist_input):
140 raise FileNotFoundError(
'bypass_namelist_input not found: %s' %
141 bypass_namelist_input)
143 LOGGER.debug(
'bypass_namelist_input: %s' %
146 if not wpswrf_distro_path:
147 wpswrf_distro_path = DEFAULTS.wpswrf_distro_path()
151 raise FileNotFoundError(
'wpswrf_distro_path not found: %s' %
154 if not working_rootdir:
155 working_rootdir = DEFAULTS.working_scratch_rootdir()
158 raise FileNotFoundError(
'working_rootdir not found: %s' %
166 real_dir = real_data_dict[
'path_to_files']
167 real_files = real_data_dict[
'files_list']
168 for fname
in real_files:
169 fpath = os.path.join(real_dir, fname)
170 if not os.path.isfile(fpath):
171 raise FileNotFoundError(
'fpath: %s' % fpath)
179 raise ValueError(
"Missing num_nests arg")
181 raise ValueError(
"num_nests: %d" % num_nests)
188 LOGGER.debug(
'NUM_NESTS: %d, real_files: %s' % (self.
_num_nests, real_files))
190 expected_fname =
'wrfinput_d%02d' % (i+1)
191 if expected_fname
not in real_files:
192 raise ValueError(
'Did not find nest: %s' % expected_fname)
198 raise ValueError(
'Did not find nests_defn_dict...')
206 if 1 <= num_mpi_tasks <= DEFAULTS.max_mpi_tasks():
208 mpirunpath = DEFAULTS.mpirun_path()
209 if os.path.isfile(mpirunpath)
and \
210 os.access(mpirunpath, os.X_OK):
214 raise FileNotFoundError(
'mpirun not executable: %s' %
217 raise ValueError(
'Bad num_mpi_tasks value: %d' % num_mpi_tasks)
250 '''Set up and run, via nwpservice module, instance of wrf
254 wrf_output_stagedir : str
255 (Optional) Full path to dir where wrf output files will be staged.
256 If not specified, no staging will be done. If specified, we
257 assume the dir already exists.
262 Dictionary with manifest of the wrf output files and the location
263 (if applicable) of staged files
267 LOGGER.debug(
'Starting run_wrf()...')
286 nml[
'time_control'][
'interval_seconds'] / 3600)
292 namelist_input_path =
None
293 raise NotImplementedError(
294 'namelist.input creation not yet supported')
299 LOGGER.debug(
'domainpath: %s' % domainpath)
302 nwpwrf_obj = nwpservice.wrf.wrf.Wrf(
304 wpswrf_rundir=domainpath,
306 namelist_input=namelist_input_path,
307 output_dir=wrf_output_stagedir,
314 output_manifest = nwpwrf_obj.run()
315 LOGGER.debug(
'output_manifest: %s' % output_manifest)
319 stage_success = nwpwrf_obj.stage_output(auxfiles=
True)
321 staging_dir = wrf_output_stagedir
329 'wrf_output_manifest' : output_manifest,
330 'staging_dir' : staging_dir,
333 LOGGER.debug(
'return_dict: %s' % return_dict)