Skip to content

Commit

Permalink
Per #1289, add support for explicitly settings file list file path fo…
Browse files Browse the repository at this point in the history
…r SeriesAnalysis
  • Loading branch information
georgemccabe committed Jan 31, 2022
1 parent 13bd4ad commit 50bb296
Showing 1 changed file with 93 additions and 54 deletions.
147 changes: 93 additions & 54 deletions metplus/wrappers/series_analysis_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,20 @@ def create_c_dict(self):

# get input dir, template, and datatype for FCST, OBS, and BOTH
for data_type in ('FCST', 'OBS', 'BOTH'):

# check if {data_type}_{app}_FILE_LIST is set
c_dict[f'{data_type}_INPUT_FILE_LIST'] = (
self.config.getraw(
'config',
f'{data_type}_SERIES_ANALYSIS_INPUT_FILE_LIST'
)
)

c_dict[f'{data_type}_INPUT_DIR'] = (
self.config.getdir(f'{data_type}_SERIES_ANALYSIS_INPUT_DIR', '')
)
c_dict[f'{data_type}_INPUT_TEMPLATE'] = (
self.config.getraw('filename_templates',
self.config.getraw('config',
f'{data_type}_SERIES_ANALYSIS_INPUT_TEMPLATE',
'')
)
Expand Down Expand Up @@ -233,43 +242,61 @@ def create_c_dict(self):
False)
)

# if BOTH is set, neither FCST or OBS can be set
c_dict['USING_BOTH'] = False
if c_dict['BOTH_INPUT_TEMPLATE']:
if c_dict['FCST_INPUT_TEMPLATE'] or c_dict['OBS_INPUT_TEMPLATE']:
self.log_error("Cannot set FCST_SERIES_ANALYSIS_INPUT_TEMPLATE"
" or OBS_SERIES_ANALYSIS_INPUT_TEMPLATE if "
"BOTH_SERIES_ANALYSIS_INPUT_TEMPLATE is set.")
c_dict['USING_BOTH'] = (c_dict['BOTH_INPUT_TEMPLATE'] or
c_dict['BOTH_INPUT_FILE_LIST'])

c_dict['USING_BOTH'] = True
if c_dict['USING_BOTH']:

# set *_WINDOW_* variables for BOTH
# used in CommandBuilder.find_data function)
self.handle_file_window_variables(c_dict, dtypes=['BOTH'])
# check if using explicit file list for BOTH
if c_dict['BOTH_INPUT_FILE_LIST']:
c_dict['EXPLICIT_FILE_LIST'] = True
else:
# set *_WINDOW_* variables for BOTH
# used in CommandBuilder.find_data function)
self.handle_file_window_variables(c_dict, dtypes=['BOTH'])

prob_thresh = self.config.getstr('config','BOTH_SERIES_ANALYSIS_PROB_THRESH','')
prob_thresh = self.config.getraw(
'config',
'BOTH_SERIES_ANALYSIS_PROB_THRESH'
)
c_dict['FCST_PROB_THRESH'] = prob_thresh
c_dict['OBS_PROB_THRESH'] = prob_thresh

# if BOTH is not set, both FCST or OBS must be set
else:
if (not c_dict['FCST_INPUT_TEMPLATE'] or
not c_dict['OBS_INPUT_TEMPLATE']):
self.log_error("Must either set "
"BOTH_SERIES_ANALYSIS_INPUT_TEMPLATE or both "
"FCST_SERIES_ANALYSIS_INPUT_TEMPLATE and "
"OBS_SERIES_ANALYSIS_INPUT_TEMPLATE to run "
"SeriesAnalysis wrapper.")

# set *_WINDOW_* variables for FCST and OBS
self.handle_file_window_variables(c_dict, dtypes=['FCST', 'OBS'])
fcst_input_list = c_dict['FCST_INPUT_FILE_LIST']
obs_input_list = c_dict['OBS_INPUT_FILE_LIST']
if fcst_input_list and obs_input_list:
c_dict['EXPLICIT_FILE_LIST'] = True
elif not fcst_input_list and not obs_input_list:
if (not c_dict['FCST_INPUT_TEMPLATE'] or
not c_dict['OBS_INPUT_TEMPLATE']):
self.log_error(
"Must either set "
"BOTH_SERIES_ANALYSIS_INPUT_TEMPLATE or both "
"FCST_SERIES_ANALYSIS_INPUT_TEMPLATE and "
"OBS_SERIES_ANALYSIS_INPUT_TEMPLATE to run "
"SeriesAnalysis wrapper."
)

# set *_WINDOW_* variables for FCST and OBS
self.handle_file_window_variables(c_dict,
dtypes=['FCST', 'OBS'])
# if fcst input list or obs input list are not set
else:
self.log_error('Cannot set '
'FCST_SERIES_ANALYSIS_INPUT_FILE_LIST '
'without OBS_SERIES_ANALYSIS_INPUT_FILE_LIST '
'and vice versa')

c_dict['FCST_PROB_THRESH'] = (
self.config.getstr('config','FCST_SERIES_ANALYSIS_PROB_THRESH','')
self.config.getraw('config',
'FCST_SERIES_ANALYSIS_PROB_THRESH')
)

c_dict['OBS_PROB_THRESH'] = (
self.config.getstr('config','OBS_SERIES_ANALYSIS_PROB_THRESH','')
self.config.getraw('config',
'OBS_SERIES_ANALYSIS_PROB_THRESH')
)

c_dict['TC_STAT_INPUT_DIR'] = (
Expand Down Expand Up @@ -624,49 +651,63 @@ def _get_fcst_and_obs_path(self, time_info, storm_id, lead_group):
key will match the format "NoLabel_<n>" and if no lead groups
are defined, the dictionary should be replaced with None
"""
if not self._check_python_embedding():
return None, None

time_info['storm_id'] = storm_id
all_fcst_files = []
all_obs_files = []
if not lead_group:
fcst_files, obs_files = self.subset_input_files(time_info)
if not fcst_files or not obs_files:
return None, None
all_fcst_files.extend(fcst_files)
all_obs_files.extend(obs_files)
label = ''
leads = None
else:

# get label and lead list if grouping by forecast leads
if lead_group:
label = lead_group[0]
leads = lead_group[1]
for lead in leads:
else:
label = ''
leads = None

# if file list are explicitly specified,
# return the file list file paths
if self.c_dict.get('EXPLICIT_FILE_LIST', False):
if self.c_dict['USING_BOTH']:
both_path = do_string_sub(self.c_dict['BOTH_INPUT_FILE_LIST'],
**time_info)
return both_path, both_path

fcst_path = do_string_sub(self.c_dict['FCST_INPUT_FILE_LIST'],
**time_info)
obs_path = do_string_sub(self.c_dict['OBS_INPUT_FILE_LIST'],
**time_info)
return fcst_path, obs_path

all_fcst_files = []
all_obs_files = []
lead_loop = leads if leads else [None]
for lead in lead_loop:
if lead is not None:
time_info['lead'] = lead
fcst_files, obs_files = self.subset_input_files(time_info)
if fcst_files and obs_files:
all_fcst_files.extend(fcst_files)
all_obs_files.extend(obs_files)

fcst_files, obs_files = self.subset_input_files(time_info)
if fcst_files and obs_files:
all_fcst_files.extend(fcst_files)
all_obs_files.extend(obs_files)

# skip if no files were found
if not all_fcst_files or not all_obs_files:
return None, None

output_dir = self.get_output_dir(time_info, storm_id, label)

if not self._check_python_embedding():
return None, None

# create forecast (or both) file list
if self.c_dict['USING_BOTH']:
data_type = 'BOTH'
else:
data_type = 'FCST'

fcst_ascii_filename = self._get_ascii_filename(data_type,
storm_id,
leads)
self.write_list_file(fcst_ascii_filename,
all_fcst_files,
output_dir=output_dir)

fcst_path = os.path.join(output_dir, fcst_ascii_filename)
fcst_path = self.write_list_file(fcst_ascii_filename,
all_fcst_files,
output_dir=output_dir)

if self.c_dict['USING_BOTH']:
return fcst_path, fcst_path
Expand All @@ -675,11 +716,9 @@ def _get_fcst_and_obs_path(self, time_info, storm_id, lead_group):
obs_ascii_filename = self._get_ascii_filename('OBS',
storm_id,
leads)
self.write_list_file(obs_ascii_filename,
all_obs_files,
output_dir=output_dir)

obs_path = os.path.join(output_dir, obs_ascii_filename)
obs_path = self.write_list_file(obs_ascii_filename,
all_obs_files,
output_dir=output_dir)

return fcst_path, obs_path

Expand Down

0 comments on commit 50bb296

Please sign in to comment.