def dataframe_from_rpt(rpt_path, section): """ create a dataframe from a section of an RPT file :param rpt_path: :param section: :return: pd.DataFrame """ # get list of all section headers in rpt to use as section ending flags headers = get_rpt_sections_details(rpt_path) if section not in headers: warnings.warn(f'{section} section not found in {rpt_path}') return pd.DataFrame() # and get the list of columns to use for parsing this section end_strings = list(headers.keys()) end_strings.append('***********') start_strings = [section, '-' * 20, '-' * 20] cols = headers[section]['columns'] # extract the string and read into a dataframe s = extract_section_of_file(rpt_path, start_strings, end_strings) df = pd.read_csv(StringIO(s), header=None, delim_whitespace=True, skiprows=[0], index_col=0, names=cols) return df
def headers(self): """ Return all section headers and associated column names found in the RPT file. """ if self._rpt_section_details is None: self._rpt_section_details = get_rpt_sections_details(self.path) return self._rpt_section_details
def test_complete_headers_rpt(test_model_02): headers = get_rpt_sections_details(test_model_02.rpt.path) sections_in_rpt = [ 'Link Flow Summary', 'Link Flow Summary', 'Subcatchment Summary', 'Cross Section Summary', 'Link Summary' ] assert (all(section in headers for section in sections_in_rpt)) assert headers['Link Summary']['columns'] == [ 'Name', 'FromNode', 'ToNode', 'Type', 'Length', 'SlopePerc', 'Roughness' ]
def dataframe_from_rpt(rpt_path, section, element_id=None): """ create a dataframe from a section of an RPT file :param rpt_path: path to rep file :param section: title of section to extract :param element_id: type of element when extracting time series data :return: pd.DataFrame """ # get list of all section headers in rpt to use as section ending flags headers = get_rpt_sections_details(rpt_path) if section not in headers: warnings.warn(f'{section} section not found in {rpt_path}') return pd.DataFrame() # handle case for extracting timeseries results if element_id is not None: end_strings = ['<<< '] start_strings = [ section, f"<<< {section.replace(' Results', '')} {element_id} >>>", '-' * 20, '-' * 20 ] else: # and get the list of columns to use for parsing this section end_strings = list(headers.keys()) end_strings.append('***********') start_strings = [section, '-'*20, '-'*20] cols = headers[section]['columns'] # extract the string and read into a dataframe s = extract_section_of_file(rpt_path, start_strings, end_strings) df = pd.read_csv(StringIO(s), header=None, delim_whitespace=True, skiprows=[0], index_col=0, names=cols) # confirm index name is string df = df.rename(index=str) return df