Skip to content

Commit

Permalink
Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevenray Janke committed May 1, 2024
1 parent da5a6ec commit dd8128c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/rashdf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def __init__(self, name: str, **kwargs):
super().__init__(name, mode="r", **kwargs)

@classmethod
def open_uri(cls, uri: str, fsspec_kwargs: dict = {}, h5py_kwargs: dict = {}) -> "RasHdf":
def open_uri(
cls, uri: str, fsspec_kwargs: dict = {}, h5py_kwargs: dict = {}
) -> "RasHdf":
"""Open a HEC-RAS HDF file from a URI.
Parameters
Expand Down
93 changes: 71 additions & 22 deletions src/rashdf/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ def mesh_area_names(self) -> List[str]:
"""
if "/Geometry/2D Flow Areas" not in self:
return list()
return list([convert_ras_hdf_string(n) for n in self["/Geometry/2D Flow Areas/Attributes"][()]["Name"]])
return list(
[
convert_ras_hdf_string(n)
for n in self["/Geometry/2D Flow Areas/Attributes"][()]["Name"]
]
)

def mesh_areas(self) -> GeoDataFrame:
"""Return 2D flow area perimeter polygons.
Expand All @@ -64,7 +69,10 @@ def mesh_areas(self) -> GeoDataFrame:
mesh_area_names = self.mesh_area_names()
if not mesh_area_names:
return GeoDataFrame()
mesh_area_polygons = [Polygon(self[f"/Geometry/2D Flow Areas/{n}/Perimeter"][()]) for n in mesh_area_names]
mesh_area_polygons = [
Polygon(self[f"/Geometry/2D Flow Areas/{n}/Perimeter"][()])
for n in mesh_area_names
]
return GeoDataFrame(
{"mesh_name": mesh_area_names, "geometry": mesh_area_polygons},
geometry="geometry",
Expand All @@ -89,26 +97,37 @@ def mesh_cell_polygons(self) -> GeoDataFrame:
for i, mesh_name in enumerate(mesh_area_names):
cell_cnt = self["/Geometry/2D Flow Areas/Cell Info"][()][i][1]
cell_ids = list(range(cell_cnt))
cell_face_info = self[f"/Geometry/2D Flow Areas/{mesh_name}/Cells Face and Orientation Info"][()]
cell_face_values = self[f"/Geometry/2D Flow Areas/{mesh_name}/Cells Face and Orientation Values"][()][:, 0]
cell_face_info = self[
f"/Geometry/2D Flow Areas/{mesh_name}/Cells Face and Orientation Info"
][()]
cell_face_values = self[
f"/Geometry/2D Flow Areas/{mesh_name}/Cells Face and Orientation Values"
][()][:, 0]
face_id_lists = list(
np.vectorize(
lambda cell_id: str(
cell_face_values[
cell_face_info[cell_id][0] : cell_face_info[cell_id][0] + cell_face_info[cell_id][1]
cell_face_info[cell_id][0] : cell_face_info[cell_id][0]
+ cell_face_info[cell_id][1]
]
)
)(cell_ids)
)
mesh_faces = (
face_gdf[face_gdf.mesh_name == mesh_name][["face_id", "geometry"]].set_index("face_id").to_numpy()
face_gdf[face_gdf.mesh_name == mesh_name][["face_id", "geometry"]]
.set_index("face_id")
.to_numpy()
)
cell_dict["mesh_name"] += [mesh_name] * cell_cnt
cell_dict["cell_id"] += cell_ids
cell_dict["geometry"] += list(
np.vectorize(
lambda face_id_list: polygonize(
np.ravel(mesh_faces[np.array(face_id_list.strip("[]").split()).astype(int)])
np.ravel(
mesh_faces[
np.array(face_id_list.strip("[]").split()).astype(int)
]
)
).geoms[0]
)(face_id_lists)
)
Expand All @@ -128,11 +147,15 @@ def mesh_cell_points(self) -> GeoDataFrame:
pnt_dict = {"mesh_name": [], "cell_id": [], "geometry": []}
for i, mesh_name in enumerate(mesh_area_names):
starting_row, count = self["/Geometry/2D Flow Areas/Cell Info"][()][i]
cell_pnt_coords = self["/Geometry/2D Flow Areas/Cell Points"][()][starting_row : starting_row + count]
cell_pnt_coords = self["/Geometry/2D Flow Areas/Cell Points"][()][
starting_row : starting_row + count
]
pnt_dict["mesh_name"] += [mesh_name] * cell_pnt_coords.shape[0]
pnt_dict["cell_id"] += range(count)
pnt_dict["geometry"] += list(
np.vectorize(lambda coords: Point(coords), signature="(n)->()")(cell_pnt_coords)
np.vectorize(lambda coords: Point(coords), signature="(n)->()")(
cell_pnt_coords
)
)
return GeoDataFrame(pnt_dict, geometry="geometry", crs=self.projection())

Expand All @@ -149,10 +172,18 @@ def mesh_cell_faces(self) -> GeoDataFrame:
return GeoDataFrame()
face_dict = {"mesh_name": [], "face_id": [], "geometry": []}
for mesh_name in mesh_area_names:
facepoints_index = self[f"/Geometry/2D Flow Areas/{mesh_name}/Faces FacePoint Indexes"][()]
facepoints_coordinates = self[f"/Geometry/2D Flow Areas/{mesh_name}/FacePoints Coordinate"][()]
faces_perimeter_info = self[f"/Geometry/2D Flow Areas/{mesh_name}/Faces Perimeter Info"][()]
faces_perimeter_values = self[f"/Geometry/2D Flow Areas/{mesh_name}/Faces Perimeter Values"][()]
facepoints_index = self[
f"/Geometry/2D Flow Areas/{mesh_name}/Faces FacePoint Indexes"
][()]
facepoints_coordinates = self[
f"/Geometry/2D Flow Areas/{mesh_name}/FacePoints Coordinate"
][()]
faces_perimeter_info = self[
f"/Geometry/2D Flow Areas/{mesh_name}/Faces Perimeter Info"
][()]
faces_perimeter_values = self[
f"/Geometry/2D Flow Areas/{mesh_name}/Faces Perimeter Values"
][()]
face_id = -1
for pnt_a_index, pnt_b_index in facepoints_index:
face_id += 1
Expand All @@ -162,7 +193,9 @@ def mesh_cell_faces(self) -> GeoDataFrame:
coordinates.append(facepoints_coordinates[pnt_a_index])
starting_row, count = faces_perimeter_info[face_id]
if count > 0:
coordinates += list(faces_perimeter_values[starting_row : starting_row + count])
coordinates += list(
faces_perimeter_values[starting_row : starting_row + count]
)
coordinates.append(facepoints_coordinates[pnt_b_index])
face_dict["geometry"].append(LineString(coordinates))
return GeoDataFrame(face_dict, geometry="geometry", crs=self.projection())
Expand Down Expand Up @@ -198,7 +231,9 @@ def get_geom_2d_flow_area_attrs(self):
try:
d2_flow_area = get_first_hdf_group(self.get(self.FLOW_AREA_2D_PATH))
except AttributeError:
raise AttributeError(f"Unable to get 2D Flow Area; {self.FLOW_AREA_2D_PATH} group not found in HDF5 file.")
raise AttributeError(
f"Unable to get 2D Flow Area; {self.FLOW_AREA_2D_PATH} group not found in HDF5 file."
)

d2_flow_area_attrs = hdf5_attrs_to_dict(d2_flow_area.attrs)

Expand All @@ -221,12 +256,18 @@ def bc_lines(self) -> GeoDataFrame:
mesh_names = v_conv_str(bc_line_data["Attributes"][()]["SA-2D"])
types = v_conv_str(bc_line_data["Attributes"][()]["Type"])
geoms = list()
for pnt_start, pnt_cnt, part_start, part_cnt in bc_line_data["Polyline Info"][()]:
points = bc_line_data["Polyline Points"][()][pnt_start : pnt_start + pnt_cnt]
for pnt_start, pnt_cnt, part_start, part_cnt in bc_line_data["Polyline Info"][
()
]:
points = bc_line_data["Polyline Points"][()][
pnt_start : pnt_start + pnt_cnt
]
if part_cnt == 1:
geoms.append(LineString(points))
else:
parts = bc_line_data["Polyline Parts"][()][part_start : part_start + part_cnt]
parts = bc_line_data["Polyline Parts"][()][
part_start : part_start + part_cnt
]
geoms.append(
MultiLineString(
list(
Expand Down Expand Up @@ -259,14 +300,22 @@ def breaklines(self) -> GeoDataFrame:
return GeoDataFrame()
bl_line_data = self["/Geometry/2D Flow Area Break Lines"]
bl_line_ids = range(bl_line_data["Attributes"][()].shape[0])
names = np.vectorize(convert_ras_hdf_string)(bl_line_data["Attributes"][()]["Name"])
names = np.vectorize(convert_ras_hdf_string)(
bl_line_data["Attributes"][()]["Name"]
)
geoms = list()
for pnt_start, pnt_cnt, part_start, part_cnt in bl_line_data["Polyline Info"][()]:
points = bl_line_data["Polyline Points"][()][pnt_start : pnt_start + pnt_cnt]
for pnt_start, pnt_cnt, part_start, part_cnt in bl_line_data["Polyline Info"][
()
]:
points = bl_line_data["Polyline Points"][()][
pnt_start : pnt_start + pnt_cnt
]
if part_cnt == 1:
geoms.append(LineString(points))
else:
parts = bl_line_data["Polyline Parts"][()][part_start : part_start + part_cnt]
parts = bl_line_data["Polyline Parts"][()][
part_start : part_start + part_cnt
]
geoms.append(
MultiLineString(
list(
Expand Down
6 changes: 0 additions & 6 deletions tests/test_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


def test_get_plan_info_attrs(tmp_path):

with h5py.File(tmp_path / "test.hdf", "w") as f:
group = f.create_group(RasPlanHdf.PLAN_INFO_PATH)
for key, value in attrs_to_set.items():
Expand All @@ -18,7 +17,6 @@ def test_get_plan_info_attrs(tmp_path):


def test_get_plan_param_attrs(tmp_path):

with h5py.File(tmp_path / "test.hdf", "w") as f:
group = f.create_group(RasPlanHdf.PLAN_PARAMS_PATH)
for key, value in attrs_to_set.items():
Expand All @@ -30,7 +28,6 @@ def test_get_plan_param_attrs(tmp_path):


def test_get_meteorology_precip_attrs(tmp_path):

with h5py.File(tmp_path / "test.hdf", "w") as f:
group = f.create_group(RasPlanHdf.PRECIP_PATH)
for key, value in attrs_to_set.items():
Expand All @@ -42,7 +39,6 @@ def test_get_meteorology_precip_attrs(tmp_path):


def test_get_results_unsteady_attrs(tmp_path):

with h5py.File(tmp_path / "test.hdf", "w") as f:
group = f.create_group(RasPlanHdf.RESULTS_UNSTEADY_PATH)
for key, value in attrs_to_set.items():
Expand All @@ -54,7 +50,6 @@ def test_get_results_unsteady_attrs(tmp_path):


def test_get_results_unsteady_summary_attrs(tmp_path):

with h5py.File(tmp_path / "test.hdf", "w") as f:
group = f.create_group(RasPlanHdf.RESULTS_UNSTEADY_SUMMARY_PATH)
for key, value in attrs_to_set.items():
Expand All @@ -66,7 +61,6 @@ def test_get_results_unsteady_summary_attrs(tmp_path):


def test_get_results_volume_accounting_attrs(tmp_path):

with h5py.File(tmp_path / "test.hdf", "w") as f:
group = f.create_group(RasPlanHdf.VOLUME_ACCOUNTING_PATH)
for key, value in attrs_to_set.items():
Expand Down

0 comments on commit dd8128c

Please sign in to comment.