Skip to content

Commit cfd52a3

Browse files
committed
deleted overwrite_file from FileReader
1 parent 999e456 commit cfd52a3

File tree

2 files changed

+0
-92
lines changed

2 files changed

+0
-92
lines changed

pySWATPlus/filereader.py

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pathlib
22
import pandas
3-
import warnings
43
import typing
54
from . import utils
65
from . import validators
@@ -86,51 +85,3 @@ def __init__(
8685
self.units_row = None
8786

8887
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)

tests/test_filereader.py

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import os
2-
import shutil
32
import pySWATPlus
4-
import tempfile
53

64

75
def test_filereader():
@@ -19,47 +17,6 @@ def test_filereader():
1917
df = file_reader.df
2018
assert df.shape[0] == 561
2119

22-
# change varibale value
23-
variable = 'epco'
24-
value = 0.75
25-
df[variable] = value
26-
27-
# rewrite the file
28-
file_reader.overwrite_file()
29-
30-
# read the new DataFrame
31-
df = file_reader.df
32-
assert df[variable].unique()[0] == value
33-
34-
with tempfile.TemporaryDirectory() as tmp_dir:
35-
# pass test for rewriting empty DataFrame in a TXT file
36-
file_name = 'hru_soilc_stat.txt'
37-
shutil.copy2(
38-
src=os.path.join(txtinout_folder, file_name),
39-
dst=os.path.join(tmp_dir, file_name)
40-
)
41-
file_reader = pySWATPlus.FileReader(
42-
path=os.path.join(tmp_dir, file_name),
43-
has_units=True
44-
)
45-
df = file_reader.df
46-
assert len(df) == 0
47-
file_reader.overwrite_file()
48-
49-
# pass test for rewriting DataFrame with 'has_units' key in a TXT file
50-
file_name = 'basin_carbon_all.txt'
51-
shutil.copy2(
52-
src=os.path.join(txtinout_folder, file_name),
53-
dst=os.path.join(tmp_dir, file_name)
54-
)
55-
file_reader = pySWATPlus.FileReader(
56-
path=os.path.join(tmp_dir, file_name),
57-
has_units=True
58-
)
59-
df = file_reader.df
60-
assert len(df) == 0
61-
file_reader.overwrite_file()
62-
6320

6421
def test_github():
6522

0 commit comments

Comments
 (0)