Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Santacruz #58

Merged
merged 9 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 27 additions & 28 deletions hydromt_delft3dfm/dflowfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2356,18 +2356,11 @@ def setup_maps_from_rasterdataset(
raise ValueError(
f"Locationtype {locationtype} not allowed. Select from ['2d', '1d', 'all']"
)

for var in variables:
if var in self._MAPS:
self._MAPS[var]["locationtype"] = locationtype
self._MAPS[var]["interpolation"] = interpolation_method
if interpolation_method != "triangulation":
# adjust relative search cell size for averaging methods
relsize = np.round(
np.abs(self.maps[var].raster.res[0]) / self.res * np.sqrt(2)
+ 0.05,
2,
)
self._MAPS[var]["averagingrelsize"] = relsize
self.__set_map_parameters_based_on_variable(
var, locationtype, interpolation_method
)

def setup_maps_from_raster_reclass(
self,
Expand Down Expand Up @@ -2456,20 +2449,28 @@ def setup_maps_from_raster_reclass(
f"Locationtype {locationtype} not allowed. Select from ['2d', '1d', 'all']"
)
for var in reclass_variables:
if var in self._MAPS:
self._MAPS[var]["locationtype"] = locationtype
self._MAPS[var]["interpolation"] = interpolation_method
if interpolation_method != "triangulation":
# increase relative search cell size if raster resolution is coarser than model resolution
if self.maps[var].raster.res[0] > self.res:
relsize = np.round(
np.abs(self.maps[var].raster.res[0]) / self.res * np.sqrt(2)
+ 0.05,
2,
)
else:
relsize = 1.01
self._MAPS[var]["averagingrelsize"] = relsize
self.__set_map_parameters_based_on_variable(
var, locationtype, interpolation_method
)

def __set_map_parameters_based_on_variable(
self, var: str, locationtype: str, interpolation_method: str
) -> None:
"""Set map parameters by updating user inputs to default self._MAP"""
if var in self._MAPS:
self._MAPS[var]["locationtype"] = locationtype
self._MAPS[var]["interpolation"] = interpolation_method
if interpolation_method != "triangulation":
# adjust relative search cell size for averaging methods
if self.maps[var].raster.res[0] > self.res:
relsize = np.round(
np.abs(self.maps[var].raster.res[0]) / self.res * np.sqrt(2)
+ 0.05,
2,
)
else:
relsize = 1.01
self._MAPS[var]["averagingrelsize"] = relsize

def setup_2dboundary(
self,
Expand Down Expand Up @@ -3223,9 +3224,7 @@ def write_mesh(self, write_gui=True):
if "mesh1d" in self.mesh_names and write_gui:
self.logger.info("Writting branches.gui file")
if "manholes" in self.geoms:
self.geoms["manholes"]
# might not needed in the future if branch_type can be supported in the mesh https://github.com/Deltares/HYDROLIB-core/issues/561
_ = utils.write_branches_gui(self.branches, savedir)
utils.write_branches_gui(self.branches, savedir)

def read_states(self):
"""Read states at <root/?/> and parse to dict of xr.DataArray."""
Expand Down
25 changes: 13 additions & 12 deletions hydromt_delft3dfm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,24 +497,25 @@ def read_manholes(gdf: gpd.GeoDataFrame, fm_model: FMModel) -> gpd.GeoDataFrame:
for b in manholes.storagenode:
manholes_dict[b.id] = b.__dict__
df_manholes = pd.DataFrame.from_dict(manholes_dict, orient="index")
# replace 0e to 0d # TODO: fix this when hydrolib-core fix issue https://github.com/Deltares/HYDROLIB-core/issues/559
df_manholes["id"] = df_manholes["id"].apply(
lambda x: "0D" + x[2:] if isinstance(x, str) and x.startswith("0e") else x
)
df_manholes["name"] = df_manholes["name"].apply(
lambda x: "0D" + x[2:] if isinstance(x, str) and x.startswith("0e") else x
)
df_manholes["manholeid"] = df_manholes["manholeid"].apply(
lambda x: "0D" + x[2:] if isinstance(x, str) and x.startswith("0e") else x
)
df_manholes["nodeid"] = df_manholes["nodeid"].apply(
lambda x: "0D" + x[2:] if isinstance(x, str) and x.startswith("0e") else x
)

# Drop variables
df_manholes = df_manholes.drop(
["comments"],
axis=1,
)
# Rename case sensitive
df_manholes = df_manholes.rename(
columns={
"manholeid": "manholeid",
"nodeid": "nodeid",
"usetable": "usetable",
"bedlevel": "bedlevel",
"streetlevel": "streetlevel",
"streetstoragearea": "streetstoragearea",
"storagetype": "storagetype",
}
)

gdf_manholes = gpd.GeoDataFrame(
df_manholes.merge(gdf, on="nodeid", how="left"), crs=gdf.crs
Expand Down
8 changes: 3 additions & 5 deletions hydromt_delft3dfm/workflows/crosssections.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def set_branch_crosssections(
[crosssections_, _set_trapezoid_crs(trapezoid_crs)]
)
# Else prepares crosssections at both upstream and dowsntream extremities
# FIXME: for now only support circle profile for pipes
# for now only support circle profile for pipes
else:
# Upstream
ids = [f"{i}_up" for i in branches.index]
Expand Down Expand Up @@ -459,7 +459,7 @@ def set_point_crosssections(
# remove duplicated geometries
_nodes = crosssections.copy()
G = _nodes["geometry"].apply(lambda geom: geom.wkb)
len(G) - len(G.drop_duplicates().index)
# check for diff in numbers: n = len(G) - len(G.drop_duplicates().index)
crosssections = _nodes[_nodes.index.isin(G.drop_duplicates().index)]

# snap to branch
Expand Down Expand Up @@ -723,8 +723,6 @@ def _set_trapezoid_crs(crosssections: gpd.GeoDataFrame):
logger.error(
"Invalid DataFrame: Found non-positive values in the 'width', 't_width', or 'height' columns."
)
else:
pass

crsdefs = []
crslocs = []
Expand Down Expand Up @@ -901,14 +899,14 @@ def _set_yz_crs(crosssections: gpd.GeoDataFrame):
# # preliminary handling
# l = l.removeprefix(prefix)
# t = None
# table_dict = {}
# # parse zw profile
# if "lt lw\nTBLE" in l:
# # the table contains height, total width en flowing width.
# l, t = l.split("lt lw\nTBLE")
# levels, totalwidths, flowwidths = np.array(
# [shlex.split(r, posix=False) for r in t.split("<")][:-1]
# ).T # last element is the suffix of tble
# table_dict = {}
# table_dict["numlevels"] = len(levels)
# table_dict["levels"] = " ".join(str(n) for n in levels)
# table_dict["totalwidths"] = " ".join(str(n) for n in totalwidths)
Expand Down
Loading