Skip to content

Commit d1a1f94

Browse files
committed
[ARC] working version of sfincs model wrapper
1 parent da97a6b commit d1a1f94

File tree

1 file changed

+48
-22
lines changed

1 file changed

+48
-22
lines changed

bluemath_tk/wrappers/sfincs/sfincs_wrapper.py

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import os
2+
import os.path as op
13
from typing import List
24

35
import numpy as np
6+
import pandas as pd
47
from hydromt_sfincs import SfincsModel
58

69
from .._base_wrappers import BaseModelWrapper
@@ -50,14 +53,6 @@ def __init__(
5053
name=self.__class__.__name__, level="DEBUG" if debug else "INFO"
5154
)
5255

53-
54-
class SfincsAlbaModelWrapper(SfincsModelWrapper):
55-
"""
56-
Wrapper for the SFINCS model.
57-
This class is a subclass of the SfincsModelWrapper class and is
58-
specifically designed for the Alba model.
59-
"""
60-
6156
def setup_dem(self, sf: SfincsModel, case_context: dict) -> List[dict]:
6257
"""
6358
Setup the DEM for the SFINCS model.
@@ -134,39 +129,49 @@ def setup_waterlevel_mask(self, sf: SfincsModel, case_context: dict) -> None:
134129

135130
sf.setup_mask_bounds(btype="waterlevel", include_mask=gdf, reset_bounds=True)
136131

137-
def set_tstop(self, case_context: dict) -> str:
132+
def set_ctimes(self, case_context: dict) -> str:
138133
"""
139-
Determine the end time (TSTOP) for the simulation based on
134+
Determine the start time (TSTART) end time (TSTOP) for the simulation based on
140135
precipitation and water level forcing, add 1 hour,
141136
and return it formatted as 'YYYYMMDD HHMMSS'.
142137
"""
143138

144139
precip_forcing = case_context.get("precipitation_forcing")
145140
waterlevel_forcing = case_context.get("waterlevel_forcing")
146141

147-
tstop_times = []
142+
tstart_times, tstop_times = [], []
148143

149144
if precip_forcing is not None:
145+
tstart_precip = precip_forcing["time"].values[0]
150146
tstop_precip = precip_forcing["time"].values[-1]
147+
148+
tstart_times.append(tstart_precip)
151149
tstop_times.append(tstop_precip)
152150

153151
if waterlevel_forcing is not None:
152+
tstart_waterlevel = waterlevel_forcing.index.values[0]
154153
tstop_waterlevel = waterlevel_forcing.index.values[-1]
154+
155+
tstart_times.append(tstart_waterlevel)
155156
tstop_times.append(tstop_waterlevel)
156157

158+
if not tstart_times:
159+
raise ValueError("No forcing data found to determine TSTART.")
160+
157161
if not tstop_times:
158162
raise ValueError("No forcing data found to determine TSTOP.")
159163

160164
# Get the latest time and add one hour
161165
tstop_max = max(tstop_times)
162-
tstop_plus_1h = (
163-
(tstop_max + np.timedelta64(1, "h")).astype("datetime64[s]").item()
164-
)
166+
tstop_plus_1h = tstop_max + np.timedelta64(1, "h")
165167

166-
# Format to 'YYYYMMDD HHMMSS'
167-
formatted_tstop = tstop_plus_1h.strftime("%Y%m%d %H%M%S")
168+
tstart_min = min(tstart_times)
168169

169-
return formatted_tstop
170+
# Convert to pandas.Timestamp (works with numpy.datetime64) then format
171+
formatted_tstop = pd.to_datetime(tstop_plus_1h).strftime("%Y%m%d %H%M%S")
172+
formatted_tstart = pd.to_datetime(tstart_min).strftime("%Y%m%d %H%M%S")
173+
174+
return formatted_tstart, formatted_tstop
170175

171176
def build_template_case(self) -> None:
172177
"""
@@ -211,6 +216,8 @@ def build_template_case(self) -> None:
211216

212217
self.setup_waterlevel_mask(sf=sf, case_context=self.fixed_parameters)
213218

219+
_ = sf.plot_basemap(bmap="sat", zoomlevel=12)
220+
214221
sf.write()
215222

216223
def build_case(self, case_context: dict, case_dir: str) -> None:
@@ -232,12 +239,33 @@ def build_case(self, case_context: dict, case_dir: str) -> None:
232239
rotation=case_context["rotation"],
233240
epsg=case_context["epsg"],
234241
)
242+
tstart, tstop = self.set_ctimes(case_context=case_context)
243+
244+
sf.config["tstop"] = tstop
245+
sf.config["tstart"] = tstart
246+
sf.config["dtout"] = 60
247+
sf.config["storemeteo"] = 1
248+
249+
if case_context.get("quickly_waterlevel_forcing") is not None:
250+
"""
251+
NOTE - There is not a specific Python function yet,
252+
but one could call the setup_waterlevel_forcing function
253+
twice with saving the files in between and changing their names
254+
"""
235255

236-
if case_context.get("waterlevel_forcing") is not None:
237256
sf.setup_waterlevel_forcing(
238-
timeseries=case_context.get("waterlevel_forcing"),
257+
timeseries=case_context.get("quickly_waterlevel_forcing"),
239258
locations=case_context.get("gdf_boundary_points"),
240259
)
260+
sf.write_forcing()
261+
os.rename(op.join(case_dir, "sfincs.bzs"), op.join(case_dir, "sfincs.bzi"))
262+
263+
if case_context.get("slowly_waterlevel_forcing") is not None:
264+
sf.setup_waterlevel_forcing(
265+
timeseries=case_context.get("slowly_waterlevel_forcing"),
266+
locations=case_context.get("gdf_boundary_points"),
267+
)
268+
sf.write_forcing()
241269

242270
if case_context.get("precipitation_forcing") is not None:
243271
sf.setup_precip_forcing_from_grid(
@@ -258,8 +286,6 @@ def build_case(self, case_context: dict, case_dir: str) -> None:
258286
# rivers=case_context.get("precipitation_forcing"), keep_rivers_geom=True
259287
# )
260288

261-
sf.write_forcing()
262-
263-
sf.config["tstop"] = self.set_tstop(case_context=case_context)
289+
# sf.write_forcing()
264290

265291
sf.write()

0 commit comments

Comments
 (0)