Skip to content

Commit

Permalink
summary output vars
Browse files Browse the repository at this point in the history
  • Loading branch information
thwllms committed Jun 7, 2024
1 parent 025cf0e commit 6a139d5
Show file tree
Hide file tree
Showing 12 changed files with 19,274 additions and 29 deletions.
22 changes: 14 additions & 8 deletions src/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""rashdf CLI."""

from rashdf import RasGeomHdf
from rashdf import RasGeomHdf, RasPlanHdf
from rashdf.utils import df_datetimes_to_str

import fiona
Expand All @@ -10,6 +10,7 @@
from ast import literal_eval
from pathlib import Path
import sys
import re
from typing import List, Optional
import warnings

Expand Down Expand Up @@ -108,17 +109,22 @@ def export(args: argparse.Namespace) -> Optional[str]:
for driver in fiona_supported_drivers():
print(driver)
return
if "://" in args.hdf_file:
geom_hdf = RasGeomHdf.open_uri(args.hdf_file)
if re.match(r"^.*\.p\d\d\.hdf$", args.hdf_file):
ras_hdf_class = RasPlanHdf

Check warning on line 113 in src/cli.py

View check run for this annotation

Codecov / codecov/patch

src/cli.py#L113

Added line #L113 was not covered by tests
else:
geom_hdf = RasGeomHdf(args.hdf_file)
ras_hdf_class = RasGeomHdf
if re.match(r"^\w+://", args.hdf_file):
geom_hdf = ras_hdf_class.open_uri(args.hdf_file)

Check warning on line 117 in src/cli.py

View check run for this annotation

Codecov / codecov/patch

src/cli.py#L117

Added line #L117 was not covered by tests
else:
geom_hdf = ras_hdf_class(args.hdf_file)
func = getattr(geom_hdf, args.func)
gdf: GeoDataFrame = func()
kwargs = literal_eval(args.kwargs) if args.kwargs else {}
if args.to_crs:
gdf = gdf.to_crs(args.to_crs)
if not args.output_file:
# convert any datetime columns to strings
# If an output file path isn't provided, write the GeoDataFrame to stdout
# as GeoJSON. Convert any datetime columns to strings.
gdf = df_datetimes_to_str(gdf)
with warnings.catch_warnings():
# Squash warnings about converting the CRS to OGC URN format.
Expand All @@ -143,9 +149,9 @@ def export(args: argparse.Namespace) -> Optional[str]:
output_file_path = Path(args.output_file)
output_file_ext = output_file_path.suffix
if output_file_ext not in [".gpkg"]:
# unless the user specifies a format that supports datetime,
# convert any datetime columns to string
# TODO: besides Geopackage, which of the standard Fiona formats allow datetime?
# Unless the user specifies a format that supports datetime,
# convert any datetime columns to string.
# TODO: besides Geopackage, which of the standard Fiona drivers allow datetime?
gdf = df_datetimes_to_str(gdf)
gdf.to_file(args.output_file, **kwargs)

Expand Down
24 changes: 12 additions & 12 deletions src/rashdf/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ def mesh_area_names(self) -> List[str]:
List[str]
A list of the 2D mesh area names (str) within the RAS geometry if 2D areas exist.
"""
if "/Geometry/2D Flow Areas" not in self:
if self.FLOW_AREA_2D_PATH not in self:
return list()
return list(
[
convert_ras_hdf_string(n)
for n in self["/Geometry/2D Flow Areas/Attributes"][()]["Name"]
for n in self[f"{self.FLOW_AREA_2D_PATH}/Attributes"][()]["Name"]
]
)

Expand All @@ -87,7 +87,7 @@ def mesh_areas(self) -> GeoDataFrame:
if not mesh_area_names:
return GeoDataFrame()
mesh_area_polygons = [
Polygon(self[f"/Geometry/2D Flow Areas/{n}/Perimeter"][()])
Polygon(self[f"{self.FLOW_AREA_2D_PATH}/{n}/Perimeter"][()])
for n in mesh_area_names
]
return GeoDataFrame(
Expand All @@ -112,13 +112,13 @@ def mesh_cell_polygons(self) -> GeoDataFrame:

cell_dict = {"mesh_name": [], "cell_id": [], "geometry": []}
for i, mesh_name in enumerate(mesh_area_names):
cell_cnt = self["/Geometry/2D Flow Areas/Cell Info"][()][i][1]
cell_cnt = self[f"{self.FLOW_AREA_2D_PATH}/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"
f"{self.FLOW_AREA_2D_PATH}/{mesh_name}/Cells Face and Orientation Info"
][()]
cell_face_values = self[
f"/Geometry/2D Flow Areas/{mesh_name}/Cells Face and Orientation Values"
f"{self.FLOW_AREA_2D_PATH}/{mesh_name}/Cells Face and Orientation Values"
][()][:, 0]
face_id_lists = list(
np.vectorize(
Expand Down Expand Up @@ -169,8 +169,8 @@ def mesh_cell_points(self) -> GeoDataFrame:
return 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, count = self[f"{self.FLOW_AREA_2D_PATH}/Cell Info"][()][i]
cell_pnt_coords = self[f"{self.FLOW_AREA_2D_PATH}/Cell Points"][()][
starting_row : starting_row + count
]
pnt_dict["mesh_name"] += [mesh_name] * cell_pnt_coords.shape[0]
Expand All @@ -196,16 +196,16 @@ def mesh_cell_faces(self) -> 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"
f"{self.FLOW_AREA_2D_PATH}/{mesh_name}/Faces FacePoint Indexes"
][()]
facepoints_coordinates = self[
f"/Geometry/2D Flow Areas/{mesh_name}/FacePoints Coordinate"
f"{self.FLOW_AREA_2D_PATH}/{mesh_name}/FacePoints Coordinate"
][()]
faces_perimeter_info = self[
f"/Geometry/2D Flow Areas/{mesh_name}/Faces Perimeter Info"
f"{self.FLOW_AREA_2D_PATH}/{mesh_name}/Faces Perimeter Info"
][()]
faces_perimeter_values = self[
f"/Geometry/2D Flow Areas/{mesh_name}/Faces Perimeter Values"
f"{self.FLOW_AREA_2D_PATH}/{mesh_name}/Faces Perimeter Values"
][()]
face_id = -1
for pnt_a_index, pnt_b_index in facepoints_index:
Expand Down
Loading

0 comments on commit 6a139d5

Please sign in to comment.