|
1 | 1 | import pathlib |
2 | 2 | import pandas |
3 | | -import warnings |
4 | 3 | import typing |
5 | 4 | from . import utils |
6 | 5 | from . import validators |
@@ -86,51 +85,3 @@ def __init__( |
86 | 85 | self.units_row = None |
87 | 86 |
|
88 | 87 | self.path = path |
89 | | - |
90 | | - def overwrite_file( |
91 | | - self |
92 | | - ) -> None: |
93 | | - ''' |
94 | | - Overwrite the original TXT file with the modified DataFrame content. |
95 | | - If the file originally contained a unit row (below the header), |
96 | | - it will be preserved and written back as part of the output. |
97 | | - If the file is a SWAT+ Output File, launch exception. |
98 | | - ''' |
99 | | - |
100 | | - # Check if units row matches the DataFrame's column count |
101 | | - if self.has_units: |
102 | | - if len(self.units_row) != self.df.shape[1]: |
103 | | - warnings.warn( |
104 | | - "Units row could not be parsed correctly. The file will still be written " |
105 | | - "and remain compatible with SWAT+, but formatting may not be preserved " |
106 | | - "due to a malformed units row.", |
107 | | - UserWarning |
108 | | - ) |
109 | | - |
110 | | - if self.units_row is not None: |
111 | | - _df = pandas.concat([pandas.DataFrame([self.units_row]), self.df], ignore_index=True) |
112 | | - else: |
113 | | - _df = self.df |
114 | | - |
115 | | - # Replace NaN with empty strings to avoid printing 'NaN' |
116 | | - _df = _df.fillna('') |
117 | | - |
118 | | - with open(self.path, 'w') as file: |
119 | | - # Write the header file first |
120 | | - file.write(self.header_file) |
121 | | - |
122 | | - if _df.empty: |
123 | | - # Calculate max width for each column name |
124 | | - col_widths = [max(len(col), 1) + 3 for col in _df.columns] |
125 | | - |
126 | | - # Create format string with fixed widths, right-aligned |
127 | | - fmt = ''.join([f'{{:>{w}}}' for w in col_widths]) |
128 | | - |
129 | | - # Format and write the header line |
130 | | - file.write(fmt.format(*_df.columns) + '\n') |
131 | | - return |
132 | | - |
133 | | - max_lengths = _df.apply(lambda x: x.astype(str).str.len()).max() |
134 | | - column_widths = {column: max_length + 3 for column, max_length in max_lengths.items()} |
135 | | - data_str = _df.to_string(index=False, justify='right', col_space=column_widths) |
136 | | - file.write(data_str) |
0 commit comments