Skip to content

Commit

Permalink
updated/added documentation for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemccabe committed Nov 7, 2023
1 parent 9737d7e commit a333d49
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions metplus/wrappers/series_analysis_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,21 @@ def get_formatted_fields(self, var_info, time_info, fcst_path, obs_path):
return fcst_fields, obs_fields

def _get_field_list(self, data_type, var_info, time_info, file_list_path):
"""!Get formatted field information in a list.
If no time (init/valid/lead) filename template tags were found in the
level value or if the time info contains all init/valid/lead values
(none are wildcards), then return a single formatted field item.
Otherwise, loop through the file list files and use the input template
to extract time information to use for each field entry.
The latter is done when processing one data type that has individual
files for each time and one data type has a single file with all times.
@param data_type type of data to process, e.g. fcst or obs
@param var_info dictionary containing info to format
@param time_info dictionary containing time information
@param file_list_path path to file list file to parse
@returns list containing formatted field info to pass to MET config
"""
other = 'OBS' if data_type == 'fcst' else 'FCST'
# if there are no time tags (init/valid/lead) in the field level
# or if init, valid, and lead have values in time_info,
Expand All @@ -1070,13 +1085,13 @@ def _get_field_list(self, data_type, var_info, time_info, file_list_path):
return self._get_field_sub_level(data_type, var_info, time_info)

field_list = []
# loop through fcst and obs files to extract time info

# handle multiple templates
templates = []
for template in self.c_dict[f'{other}_INPUT_TEMPLATE'].split(','):
templates.append(os.path.join(self.c_dict[f'{other}_INPUT_DIR'], template.strip()))

# loop through fcst/obs files to extract time info
# for each file apply time info to field info and add to list
for file_time_info in self._get_times_from_file_list(file_list_path,
templates):
Expand All @@ -1087,17 +1102,25 @@ def _get_field_list(self, data_type, var_info, time_info, file_list_path):
return field_list

@staticmethod
def _has_time_tag(level):
def _has_time_tag(string_to_parse):
"""!Get all filename template tags from raw string and check if any of
the time info tags (init/valid/lead) were found.
@param string_to_parse string to search for filename template tags
@returns True if init, valid, or lead tags, e.g. {lead?fmt=%H},
were found in string. False if none of them were found.
"""
return any(item in ['init', 'valid', 'lead']
for item in get_tags(level))
for item in get_tags(string_to_parse))

def _get_field_sub_level(self, data_type, var_info, time_dict):
"""!Get formatted field information for data type, substituting time
information into level value.
@param data_type type of data to find, e.g. fcst or obs
@param data_type type of data to find, e.g. fcst or obs
@param var_info dictionary containing info to format
@param time_dict dictionary containing time information
@returns string with formatted field info or None
"""
level = do_string_sub(var_info[f'{data_type}_level'], **time_dict)
return self.get_field_info(
Expand All @@ -1110,6 +1133,14 @@ def _get_field_sub_level(self, data_type, var_info, time_dict):

@staticmethod
def _get_times_from_file_list(file_path, templates):
"""!Generator that yields time info dictionaries.
Loops through file paths found in text file and use list of filename
templates to parse time information from each file.
@param file_path path to file list file to parse
@param templates list of filename templates to use to parse time info
out of file paths found in file_path file
"""
with open(file_path, 'r') as file_handle:
file_list = file_handle.read().splitlines()[1:]

Expand Down

0 comments on commit a333d49

Please sign in to comment.