Skip to content

Commit

Permalink
remove "auto" derivation of timeseries format because it's prone to e…
Browse files Browse the repository at this point in the history
…rrors
  • Loading branch information
roeldegoede committed Aug 28, 2024
1 parent 74dc4f1 commit 9f1ea95
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 28 deletions.
8 changes: 1 addition & 7 deletions hydromt_sfincs/sfincs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3122,7 +3122,6 @@ def write_forcing(self, data_vars: Union[List, str] = None, fmt: str = "%7.2f"):
List of data variables to write, by default None (all)
fmt : str, optional
Format string for timeseries data, by default "%7.2f".
If "auto", the format is derived from the data.
"""
self._assert_write_mode

Expand Down Expand Up @@ -3167,13 +3166,8 @@ def write_forcing(self, data_vars: Union[List, str] = None, fmt: str = "%7.2f"):
if f"{ts_name}file" not in self.config:
self.set_config(f"{ts_name}file", f"sfincs.{ts_name}")
fn = self.get_config(f"{ts_name}file", abs_path=True)
# derive timeseries format from data if not provided
if fmt is "auto":
fmt_ts = utils.derive_format_string(df.values)
else:
fmt_ts = fmt
# write timeseries
utils.write_timeseries(fn, df, tref, fmt=fmt_ts)
utils.write_timeseries(fn, df, tref, fmt=fmt)
# write xy
if xy_name and da is not None:
# parse data to geodataframe
Expand Down
21 changes: 0 additions & 21 deletions hydromt_sfincs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1285,24 +1285,3 @@ def binary_search(vals, val):
if vals[indx] == val:
return indx
return None


def derive_format_string(arr):
"""Derive a format string for an array of numbers."""
# Flatten the array to 1D to handle all elements uniformly
arr = arr.flatten()

# Get the maximum number of decimal places
def count_decimal_places(x):
if "." in str(x):
return len(str(x).split(".")[-1].rstrip("0"))
return 0

max_decimal_places = max(count_decimal_places(x) for x in arr)

# Get the maximum width required
max_width = max(len(str(x)) for x in arr)

# Construct format string
format_string = f"%{max_width}.{max_decimal_places}f"
return format_string

0 comments on commit 9f1ea95

Please sign in to comment.