Hi!
There are several Python modules to read/write Selafin files. But I do not know if all can build a file from scratch.
If you want ot use
PyTelTools, here is snippet to write a 2D Selafin file:
from scipy.spatial import Delaunay
import numpy as np
from pyteltools.slf import Serafin
nb_nodes_2d = 100
x = np.random.rand(nb_nodes_2d)
y = np.random.rand(nb_nodes_2d)
nodes = np.column_stack((x, y))
ikle = Delaunay(np.vstack((x, y)).T).simplices + 1 # IKLE tables are 1-indexed
with Serafin.Write("r2d.slf", "en", overwrite=True) as resout:
header = Serafin.SerafinHeader("My title")
header.from_triangulation(nodes, ikle)
header.add_variable_from_ID("H") # Water depth
header.add_variable_str("MYVAR", "MYVAR", "My/Unit") # Custom variable
resout.write_header(header)
for time in [0.0, 3600.0]:
values = np.random.rand(header.nb_var, nb_nodes_2d)
resout.write_entire_frame(header, time, values)
If you are facing some issue (in particular performance to build ipobo array for fine mesh), let me know, there are some in progress work.
Writing a
3D Selafin file would need some more methods, but it is still possible with PyTelTools.
If you wan to try
xarray-selafin (it uses PyTelTools to parse Selafin),
here is also a snippet to do it.
Best Regards,
Luc