Skip to content

Commit

Permalink
Parse forcing precision (#210)
Browse files Browse the repository at this point in the history
* Default for baro should be set to 1! As is default in SFINCS itself, otherwise any pressure input is ignored....

* cahnge default value also in test-inputs

* added option to parse precision of forcing timeseries

* remove "auto" derivation of timeseries format because it's prone to errors

* fix tests

---------

Co-authored-by: Tim Leijnse <[email protected]>
  • Loading branch information
roeldegoede and Leynse authored Aug 29, 2024
1 parent 74039af commit 84262d0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
12 changes: 7 additions & 5 deletions hydromt_sfincs/sfincs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3113,22 +3113,24 @@ def read_forcing(self, data_vars: List = None):
else:
logger.warning(f"No forcing variables found in {fname}file")

def write_forcing(self, data_vars: Union[List, str] = None):
def write_forcing(self, data_vars: Union[List, str] = None, fmt: str = "%7.2f"):
"""Write forcing to ascii or netcdf (netampr) files.
Filenames are based on the `config` attribute.
Parameters
----------
data_vars : list of str, optional
List of data variables to write, by default None (all)
fmt : str, optional
Format string for timeseries data, by default "%7.2f".
"""
self._assert_write_mode

# change precision of coordinates according to crs
if self.crs.is_geographic:
fmt = "%.6f"
fmt_xy = "%.6f"
else:
fmt = "%.1f"
fmt_xy = "%.1f"

if self.forcing:
self.logger.info("Write forcing files")
Expand Down Expand Up @@ -3166,7 +3168,7 @@ def write_forcing(self, data_vars: Union[List, str] = None):
self.set_config(f"{ts_name}file", f"sfincs.{ts_name}")
fn = self.get_config(f"{ts_name}file", abs_path=True)
# write timeseries
utils.write_timeseries(fn, df, tref)
utils.write_timeseries(fn, df, tref, fmt=fmt)
# write xy
if xy_name and da is not None:
# parse data to geodataframe
Expand All @@ -3179,7 +3181,7 @@ def write_forcing(self, data_vars: Union[List, str] = None):
self.set_config(f"{xy_name}file", f"sfincs.{xy_name}")
fn_xy = self.get_config(f"{xy_name}file", abs_path=True)
# write xy
hydromt.io.write_xy(fn_xy, gdf, fmt=fmt)
hydromt.io.write_xy(fn_xy, gdf, fmt=fmt_xy)
if self._write_gis: # write geojson file to gis folder
self.write_vector(variables=f"forcing.{ts_names[0]}")

Expand Down
31 changes: 31 additions & 0 deletions tests/test_1model_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,37 @@ def test_observations(tmpdir):
assert len(mod.geoms["crs"].index) == nr_observation_lines * 2


def test_forcing_io(tmpdir):
root = TESTMODELDIR
mod = SfincsModel(root=root, mode="r")
# read
mod.read_forcing()

# write forcing
tmp_root = str(tmpdir.join("forcing_test"))
mod.set_root(tmp_root, mode="w")
mod.write_forcing()
mod.write_config()

# read and check if identical
mod1 = SfincsModel(root=tmp_root, mode="r")
mod1.read_forcing()
assert np.allclose(mod1.forcing["bzs"], mod.forcing["bzs"])

# now change the timeseries-format and write again
tmp_root = str(tmpdir.join("forcing_test2"))
mod1.set_root(tmp_root, mode="w+")
mod1.write_forcing(fmt="%7.1f")
mod1.write_config()

# read and check if identical
mod2 = SfincsModel(root=tmp_root, mode="r")
mod2.read_forcing()
assert np.isclose(
np.sum(mod2.forcing["bzs"].values - mod1.forcing["bzs"].values), 0.73
)


def test_read_results():
root = TESTMODELDIR
mod = SfincsModel(root=root, mode="r")
Expand Down

0 comments on commit 84262d0

Please sign in to comment.