ras-commander 0.78.0__tar.gz → 0.79.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. {ras_commander-0.78.0/ras_commander.egg-info → ras_commander-0.79.1}/PKG-INFO +6 -4
  2. {ras_commander-0.78.0 → ras_commander-0.79.1}/README.md +5 -3
  3. ras_commander-0.79.1/ras_commander/HdfFluvialPluvial.py +416 -0
  4. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/HdfResultsMesh.py +0 -16
  5. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/RasUnsteady.py +1 -1
  6. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/__init__.py +1 -1
  7. {ras_commander-0.78.0 → ras_commander-0.79.1/ras_commander.egg-info}/PKG-INFO +6 -4
  8. {ras_commander-0.78.0 → ras_commander-0.79.1}/setup.py +1 -1
  9. ras_commander-0.78.0/ras_commander/HdfFluvialPluvial.py +0 -554
  10. {ras_commander-0.78.0 → ras_commander-0.79.1}/LICENSE +0 -0
  11. {ras_commander-0.78.0 → ras_commander-0.79.1}/pyproject.toml +0 -0
  12. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/Decorators.py +0 -0
  13. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/HdfBase.py +0 -0
  14. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/HdfBndry.py +0 -0
  15. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/HdfInfiltration.py +0 -0
  16. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/HdfMesh.py +0 -0
  17. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/HdfPipe.py +0 -0
  18. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/HdfPlan.py +0 -0
  19. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/HdfPlot.py +0 -0
  20. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/HdfPump.py +0 -0
  21. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/HdfResultsPlan.py +0 -0
  22. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/HdfResultsPlot.py +0 -0
  23. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/HdfResultsXsec.py +0 -0
  24. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/HdfStruc.py +0 -0
  25. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/HdfUtils.py +0 -0
  26. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/HdfXsec.py +0 -0
  27. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/LoggingConfig.py +0 -0
  28. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/RasCmdr.py +0 -0
  29. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/RasExamples.py +0 -0
  30. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/RasGeo.py +0 -0
  31. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/RasMap.py +0 -0
  32. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/RasPlan.py +0 -0
  33. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/RasPrj.py +0 -0
  34. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander/RasUtils.py +0 -0
  35. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander.egg-info/SOURCES.txt +0 -0
  36. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander.egg-info/dependency_links.txt +0 -0
  37. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander.egg-info/requires.txt +0 -0
  38. {ras_commander-0.78.0 → ras_commander-0.79.1}/ras_commander.egg-info/top_level.txt +0 -0
  39. {ras_commander-0.78.0 → ras_commander-0.79.1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ras-commander
3
- Version: 0.78.0
3
+ Version: 0.79.1
4
4
  Summary: A Python library for automating HEC-RAS 6.x operations
5
5
  Home-page: https://github.com/gpt-cmdr/ras-commander
6
6
  Author: William M. Katzenmeyer, P.E., C.F.M.
@@ -41,9 +41,11 @@ RAS Commander is a Python library for automating HEC-RAS operations, providing a
41
41
 
42
42
  *[Check out the ASFPM Presentation on RAS-Commander Here](https://drive.google.com/file/d/1kX0twae8NrpLwR0iQ0Dmd8zAXdq-pYXD/view)*
43
43
 
44
- ## Repository Author:
45
- [William Katzenmeyer, P.E., C.F.M.](https://engineeringwithllms.info)
46
- -----
44
+ ## Repository Author
45
+
46
+ **[William Katzenmeyer, P.E., C.F.M.](https://engineeringwithllms.info)**
47
+ Owner & Vice President, [CLB Engineering Corporation](https://clbengineering.com/)
48
+
47
49
 
48
50
  ## Don't Ask Me, Ask a GPT!
49
51
 
@@ -8,9 +8,11 @@ RAS Commander is a Python library for automating HEC-RAS operations, providing a
8
8
 
9
9
  *[Check out the ASFPM Presentation on RAS-Commander Here](https://drive.google.com/file/d/1kX0twae8NrpLwR0iQ0Dmd8zAXdq-pYXD/view)*
10
10
 
11
- ## Repository Author:
12
- [William Katzenmeyer, P.E., C.F.M.](https://engineeringwithllms.info)
13
- -----
11
+ ## Repository Author
12
+
13
+ **[William Katzenmeyer, P.E., C.F.M.](https://engineeringwithllms.info)**
14
+ Owner & Vice President, [CLB Engineering Corporation](https://clbengineering.com/)
15
+
14
16
 
15
17
  ## Don't Ask Me, Ask a GPT!
16
18
 
@@ -0,0 +1,416 @@
1
+ """
2
+ Class: HdfFluvialPluvial
3
+
4
+ All of the methods in this class are static and are designed to be used without instantiation.
5
+
6
+ List of Functions in HdfFluvialPluvial:
7
+ - calculate_fluvial_pluvial_boundary(): Returns LineStrings representing the boundary.
8
+ - generate_fluvial_pluvial_polygons(): Returns dissolved Polygons for fluvial, pluvial, and ambiguous zones.
9
+ - _process_cell_adjacencies()
10
+ - _get_boundary_cell_pairs()
11
+ - _identify_boundary_edges()
12
+
13
+ """
14
+
15
+ from typing import Dict, List, Tuple, Set, Optional
16
+ import pandas as pd
17
+ import geopandas as gpd
18
+ from collections import defaultdict
19
+ from shapely.geometry import LineString, MultiLineString
20
+ from tqdm import tqdm
21
+ from .HdfMesh import HdfMesh
22
+ from .HdfUtils import HdfUtils
23
+ from .Decorators import standardize_input
24
+ from .HdfResultsMesh import HdfResultsMesh
25
+ from .LoggingConfig import get_logger
26
+ from pathlib import Path
27
+
28
+ logger = get_logger(__name__)
29
+
30
+ class HdfFluvialPluvial:
31
+ """
32
+ A class for analyzing and visualizing fluvial-pluvial boundaries in HEC-RAS 2D model results.
33
+
34
+ This class provides methods to process and visualize HEC-RAS 2D model outputs,
35
+ specifically focusing on the delineation of fluvial and pluvial flood areas.
36
+ It includes functionality for calculating fluvial-pluvial boundaries based on
37
+ the timing of maximum water surface elevations.
38
+
39
+ Key Concepts:
40
+ - Fluvial flooding: Flooding from rivers/streams
41
+ - Pluvial flooding: Flooding from rainfall/surface water
42
+ - delta_t: Time threshold (in hours) used to distinguish between fluvial and pluvial cells.
43
+ Cells with max WSE time differences greater than delta_t are considered boundaries.
44
+
45
+ Data Requirements:
46
+ - HEC-RAS plan HDF file containing:
47
+ - 2D mesh cell geometry (accessed via HdfMesh)
48
+ - Maximum water surface elevation times (accessed via HdfResultsMesh)
49
+
50
+ Usage Example:
51
+ >>> from ras_commander import HdfFluvialPluvial
52
+ >>> hdf_path = Path("path/to/plan.hdf")
53
+
54
+ # To get just the boundary lines
55
+ >>> boundary_lines_gdf = HdfFluvialPluvial.calculate_fluvial_pluvial_boundary(
56
+ ... hdf_path,
57
+ ... delta_t=12
58
+ ... )
59
+
60
+ # To get classified flood polygons
61
+ >>> flood_polygons_gdf = HdfFluvialPluvial.generate_fluvial_pluvial_polygons(
62
+ ... hdf_path,
63
+ ... delta_t=12,
64
+ ... temporal_tolerance_hours=1.0
65
+ ... )
66
+ """
67
+ def __init__(self):
68
+ self.logger = get_logger(__name__) # Initialize logger with module name
69
+
70
+ @staticmethod
71
+ @standardize_input(file_type='plan_hdf')
72
+ def calculate_fluvial_pluvial_boundary(
73
+ hdf_path: Path,
74
+ delta_t: float = 12,
75
+ min_line_length: Optional[float] = None
76
+ ) -> gpd.GeoDataFrame:
77
+ """
78
+ Calculate the fluvial-pluvial boundary lines based on cell polygons and maximum water surface elevation times.
79
+
80
+ This function is useful for visualizing the line of transition between flooding mechanisms.
81
+
82
+ Args:
83
+ hdf_path (Path): Path to the HEC-RAS plan HDF file.
84
+ delta_t (float): Threshold time difference in hours. Cells with time differences
85
+ greater than this value are considered boundaries. Default is 12 hours.
86
+ min_line_length (float, optional): Minimum length (in CRS units) for boundary lines to be included.
87
+ Lines shorter than this will be dropped. Default is None (no filtering).
88
+
89
+ Returns:
90
+ gpd.GeoDataFrame: GeoDataFrame containing the fluvial-pluvial boundary lines.
91
+ """
92
+ try:
93
+ logger.info("Getting cell polygons from HDF file...")
94
+ cell_polygons_gdf = HdfMesh.get_mesh_cell_polygons(hdf_path)
95
+ if cell_polygons_gdf.empty:
96
+ raise ValueError("No cell polygons found in HDF file")
97
+
98
+ logger.info("Getting maximum water surface data from HDF file...")
99
+ max_ws_df = HdfResultsMesh.get_mesh_max_ws(hdf_path)
100
+ if max_ws_df.empty:
101
+ raise ValueError("No maximum water surface data found in HDF file")
102
+
103
+ logger.info("Converting maximum water surface timestamps...")
104
+ max_ws_df['maximum_water_surface_time'] = max_ws_df['maximum_water_surface_time'].apply(
105
+ lambda x: HdfUtils.parse_ras_datetime(x) if isinstance(x, str) else x
106
+ )
107
+
108
+ logger.info("Processing cell adjacencies...")
109
+ cell_adjacency, common_edges = HdfFluvialPluvial._process_cell_adjacencies(cell_polygons_gdf)
110
+
111
+ logger.info("Extracting cell times from maximum water surface data...")
112
+ cell_times = max_ws_df.set_index('cell_id')['maximum_water_surface_time'].to_dict()
113
+
114
+ logger.info("Identifying boundary edges...")
115
+ boundary_edges = HdfFluvialPluvial._identify_boundary_edges(
116
+ cell_adjacency, common_edges, cell_times, delta_t, min_line_length=min_line_length
117
+ )
118
+
119
+ logger.info("Creating final GeoDataFrame for boundaries...")
120
+ boundary_gdf = gpd.GeoDataFrame(
121
+ geometry=boundary_edges,
122
+ crs=cell_polygons_gdf.crs
123
+ )
124
+
125
+ logger.info("Boundary line calculation completed successfully.")
126
+ return boundary_gdf
127
+
128
+ except Exception as e:
129
+ logger.error(f"Error calculating fluvial-pluvial boundary lines: {str(e)}")
130
+ return gpd.GeoDataFrame()
131
+
132
+ @staticmethod
133
+ @standardize_input(file_type='plan_hdf')
134
+ def generate_fluvial_pluvial_polygons(
135
+ hdf_path: Path,
136
+ delta_t: float = 12,
137
+ temporal_tolerance_hours: float = 1.0,
138
+ min_polygon_area_acres: Optional[float] = None
139
+ ) -> gpd.GeoDataFrame:
140
+ """
141
+ Generates dissolved polygons representing fluvial, pluvial, and ambiguous flood zones.
142
+
143
+ This function classifies each wetted cell and merges them into three distinct regions
144
+ based on the timing of maximum water surface elevation.
145
+
146
+ Optionally, for polygons classified as fluvial or pluvial, if their area is less than
147
+ min_polygon_area_acres, they are reclassified to the opposite type and merged with
148
+ adjacent polygons of that type. Ambiguous polygons are exempt from this logic.
149
+
150
+ Args:
151
+ hdf_path (Path): Path to the HEC-RAS plan HDF file.
152
+ delta_t (float): The time difference (in hours) between adjacent cells that defines
153
+ the initial boundary between fluvial and pluvial zones. Default is 12.
154
+ temporal_tolerance_hours (float): The maximum time difference (in hours) for a cell
155
+ to be considered part of an expanding region.
156
+ Default is 1.0.
157
+ min_polygon_area_acres (float, optional): Minimum polygon area (in acres). For fluvial or pluvial
158
+ polygons smaller than this, reclassify to the opposite
159
+ type and merge with adjacent polygons of that type.
160
+ Ambiguous polygons are not affected.
161
+
162
+ Returns:
163
+ gpd.GeoDataFrame: A GeoDataFrame with dissolved polygons for 'fluvial', 'pluvial',
164
+ and 'ambiguous' zones.
165
+ """
166
+ try:
167
+ # --- 1. Data Loading and Preparation ---
168
+ logger.info("Loading mesh and results data...")
169
+ cell_polygons_gdf = HdfMesh.get_mesh_cell_polygons(hdf_path)
170
+ max_ws_df = HdfResultsMesh.get_mesh_max_ws(hdf_path)
171
+ max_ws_df['maximum_water_surface_time'] = max_ws_df['maximum_water_surface_time'].apply(
172
+ lambda x: HdfUtils.parse_ras_datetime(x) if isinstance(x, str) else x
173
+ )
174
+ cell_times = max_ws_df.set_index('cell_id')['maximum_water_surface_time'].to_dict()
175
+
176
+ logger.info("Processing cell adjacencies...")
177
+ cell_adjacency, _ = HdfFluvialPluvial._process_cell_adjacencies(cell_polygons_gdf)
178
+
179
+ # --- 2. Seeding the Classifications ---
180
+ logger.info(f"Identifying initial boundary seeds with delta_t = {delta_t} hours...")
181
+ boundary_pairs = HdfFluvialPluvial._get_boundary_cell_pairs(cell_adjacency, cell_times, delta_t)
182
+
183
+ classifications = pd.Series('unclassified', index=cell_polygons_gdf['cell_id'], name='classification')
184
+
185
+ for cell1, cell2 in boundary_pairs:
186
+ if cell_times.get(cell1) > cell_times.get(cell2):
187
+ classifications.loc[cell1] = 'fluvial'
188
+ classifications.loc[cell2] = 'pluvial'
189
+ else:
190
+ classifications.loc[cell1] = 'pluvial'
191
+ classifications.loc[cell2] = 'fluvial'
192
+
193
+ # --- 3. Iterative Region Growth ---
194
+ logger.info(f"Starting iterative region growth with tolerance = {temporal_tolerance_hours} hours...")
195
+ fluvial_frontier = set(classifications[classifications == 'fluvial'].index)
196
+ pluvial_frontier = set(classifications[classifications == 'pluvial'].index)
197
+
198
+ iteration = 0
199
+ with tqdm(desc="Region Growing", unit="iter") as pbar:
200
+ while fluvial_frontier or pluvial_frontier:
201
+ iteration += 1
202
+
203
+ next_fluvial_candidates = set()
204
+ for cell_id in fluvial_frontier:
205
+ for neighbor_id in cell_adjacency.get(cell_id, []):
206
+ if classifications.loc[neighbor_id] == 'unclassified' and pd.notna(cell_times.get(neighbor_id)):
207
+ time_diff_seconds = abs((cell_times[cell_id] - cell_times[neighbor_id]).total_seconds())
208
+ if time_diff_seconds <= temporal_tolerance_hours * 3600:
209
+ next_fluvial_candidates.add(neighbor_id)
210
+
211
+ next_pluvial_candidates = set()
212
+ for cell_id in pluvial_frontier:
213
+ for neighbor_id in cell_adjacency.get(cell_id, []):
214
+ if classifications.loc[neighbor_id] == 'unclassified' and pd.notna(cell_times.get(neighbor_id)):
215
+ time_diff_seconds = abs((cell_times[cell_id] - cell_times[neighbor_id]).total_seconds())
216
+ if time_diff_seconds <= temporal_tolerance_hours * 3600:
217
+ next_pluvial_candidates.add(neighbor_id)
218
+
219
+ # Resolve conflicts
220
+ ambiguous_cells = next_fluvial_candidates.intersection(next_pluvial_candidates)
221
+ if ambiguous_cells:
222
+ classifications.loc[list(ambiguous_cells)] = 'ambiguous'
223
+
224
+ # Classify non-conflicted cells
225
+ newly_fluvial = next_fluvial_candidates - ambiguous_cells
226
+ if newly_fluvial:
227
+ classifications.loc[list(newly_fluvial)] = 'fluvial'
228
+
229
+ newly_pluvial = next_pluvial_candidates - ambiguous_cells
230
+ if newly_pluvial:
231
+ classifications.loc[list(newly_pluvial)] = 'pluvial'
232
+
233
+ # Update frontiers for the next iteration
234
+ fluvial_frontier = newly_fluvial
235
+ pluvial_frontier = newly_pluvial
236
+
237
+ pbar.update(1)
238
+ pbar.set_postfix({
239
+ "Fluvial": len(fluvial_frontier),
240
+ "Pluvial": len(pluvial_frontier),
241
+ "Ambiguous": len(ambiguous_cells)
242
+ })
243
+
244
+ logger.info(f"Region growing completed in {iteration} iterations.")
245
+
246
+ # --- 4. Finalization and Dissolving ---
247
+ # Classify any remaining unclassified (likely isolated) cells as ambiguous
248
+ classifications[classifications == 'unclassified'] = 'ambiguous'
249
+
250
+ logger.info("Merging classifications with cell polygons...")
251
+ classified_gdf = cell_polygons_gdf.merge(classifications.to_frame(), left_on='cell_id', right_index=True)
252
+
253
+ logger.info("Dissolving polygons by classification...")
254
+ final_regions_gdf = classified_gdf.dissolve(by='classification', aggfunc='first').reset_index()
255
+
256
+ # --- 5. Minimum Polygon Area Filtering and Merging (if requested) ---
257
+ if min_polygon_area_acres is not None:
258
+ logger.info(f"Applying minimum polygon area filter: {min_polygon_area_acres} acres")
259
+ # Calculate area in acres (1 acre = 4046.8564224 m^2)
260
+ # If CRS is not projected, warn and skip area filtering
261
+ if not final_regions_gdf.crs or not final_regions_gdf.crs.is_projected:
262
+ logger.warning("CRS is not projected. Area-based filtering skipped.")
263
+ else:
264
+ # Explode to individual polygons for area filtering
265
+ exploded = final_regions_gdf.explode(index_parts=False, ignore_index=True)
266
+ exploded['area_acres'] = exploded.geometry.area / 4046.8564224
267
+
268
+ # Only consider fluvial and pluvial polygons for area filtering
269
+ mask_fluvial = (exploded['classification'] == 'fluvial') & (exploded['area_acres'] < min_polygon_area_acres)
270
+ mask_pluvial = (exploded['classification'] == 'pluvial') & (exploded['area_acres'] < min_polygon_area_acres)
271
+
272
+ n_fluvial = mask_fluvial.sum()
273
+ n_pluvial = mask_pluvial.sum()
274
+ logger.info(f"Found {n_fluvial} small fluvial and {n_pluvial} small pluvial polygons to reclassify.")
275
+
276
+ # Reclassify small fluvial polygons as pluvial, and small pluvial polygons as fluvial
277
+ exploded.loc[mask_fluvial, 'classification'] = 'pluvial'
278
+ exploded.loc[mask_pluvial, 'classification'] = 'fluvial'
279
+ # Ambiguous polygons are not changed
280
+
281
+ # Redissolve by classification to merge with adjacent polygons of the same type
282
+ final_regions_gdf = exploded.dissolve(by='classification', aggfunc='first').reset_index()
283
+ logger.info("Redissolved polygons after reclassification of small areas.")
284
+
285
+ logger.info("Polygon generation completed successfully.")
286
+ return final_regions_gdf
287
+
288
+ except Exception as e:
289
+ logger.error(f"Error generating fluvial-pluvial polygons: {str(e)}", exc_info=True)
290
+ return gpd.GeoDataFrame()
291
+
292
+
293
+ @staticmethod
294
+ def _process_cell_adjacencies(cell_polygons_gdf: gpd.GeoDataFrame) -> Tuple[Dict[int, List[int]], Dict[int, Dict[int, LineString]]]:
295
+ """
296
+ Optimized method to process cell adjacencies by extracting shared edges directly.
297
+ """
298
+ cell_adjacency = defaultdict(list)
299
+ common_edges = defaultdict(dict)
300
+ edge_to_cells = defaultdict(set)
301
+
302
+ def edge_key(coords1, coords2, precision=8):
303
+ coords1 = tuple(round(coord, precision) for coord in coords1)
304
+ coords2 = tuple(round(coord, precision) for coord in coords2)
305
+ return tuple(sorted([coords1, coords2]))
306
+
307
+ for _, row in cell_polygons_gdf.iterrows():
308
+ cell_id = row['cell_id']
309
+ geom = row['geometry']
310
+ if geom.is_empty or not geom.is_valid:
311
+ continue
312
+ coords = list(geom.exterior.coords)
313
+ for i in range(len(coords) - 1):
314
+ key = edge_key(coords[i], coords[i + 1])
315
+ edge_to_cells[key].add(cell_id)
316
+
317
+ for edge, cells in edge_to_cells.items():
318
+ cell_list = list(cells)
319
+ if len(cell_list) >= 2:
320
+ for i in range(len(cell_list)):
321
+ for j in range(i + 1, len(cell_list)):
322
+ cell1, cell2 = cell_list[i], cell_list[j]
323
+ cell_adjacency[cell1].append(cell2)
324
+ cell_adjacency[cell2].append(cell1)
325
+ common_edge = LineString([edge[0], edge[1]])
326
+ common_edges[cell1][cell2] = common_edge
327
+ common_edges[cell2][cell1] = common_edge
328
+
329
+ return cell_adjacency, common_edges
330
+
331
+ @staticmethod
332
+ def _get_boundary_cell_pairs(
333
+ cell_adjacency: Dict[int, List[int]],
334
+ cell_times: Dict[int, pd.Timestamp],
335
+ delta_t: float
336
+ ) -> List[Tuple[int, int]]:
337
+ """
338
+ Identifies pairs of adjacent cell IDs that form a boundary.
339
+
340
+ A boundary is defined where the difference in max water surface time
341
+ between two adjacent cells is greater than delta_t.
342
+
343
+ Args:
344
+ cell_adjacency (Dict[int, List[int]]): Dictionary of cell adjacencies.
345
+ cell_times (Dict[int, pd.Timestamp]): Dictionary mapping cell IDs to their max WSE times.
346
+ delta_t (float): Time threshold in hours.
347
+
348
+ Returns:
349
+ List[Tuple[int, int]]: A list of tuples, where each tuple contains a pair of
350
+ cell IDs forming a boundary.
351
+ """
352
+ boundary_cell_pairs = []
353
+ processed_pairs = set()
354
+ delta_t_seconds = delta_t * 3600
355
+
356
+ for cell_id, neighbors in cell_adjacency.items():
357
+ time1 = cell_times.get(cell_id)
358
+ if not pd.notna(time1):
359
+ continue
360
+
361
+ for neighbor_id in neighbors:
362
+ pair = tuple(sorted((cell_id, neighbor_id)))
363
+ if pair in processed_pairs:
364
+ continue
365
+
366
+ time2 = cell_times.get(neighbor_id)
367
+ if not pd.notna(time2):
368
+ continue
369
+
370
+ time_diff = abs((time1 - time2).total_seconds())
371
+
372
+ if time_diff >= delta_t_seconds:
373
+ boundary_cell_pairs.append(pair)
374
+
375
+ processed_pairs.add(pair)
376
+
377
+ return boundary_cell_pairs
378
+
379
+ @staticmethod
380
+ def _identify_boundary_edges(
381
+ cell_adjacency: Dict[int, List[int]],
382
+ common_edges: Dict[int, Dict[int, LineString]],
383
+ cell_times: Dict[int, pd.Timestamp],
384
+ delta_t: float,
385
+ min_line_length: Optional[float] = None
386
+ ) -> List[LineString]:
387
+ """
388
+ Identify boundary edges between cells with significant time differences.
389
+
390
+ This function now uses the helper `_get_boundary_cell_pairs`.
391
+
392
+ Args:
393
+ cell_adjacency (Dict[int, List[int]]): Dictionary of cell adjacencies.
394
+ common_edges (Dict[int, Dict[int, LineString]]): Dictionary of shared edges between cells.
395
+ cell_times (Dict[int, pd.Timestamp]): Dictionary mapping cell IDs to their max WSE times.
396
+ delta_t (float): Time threshold in hours.
397
+ min_line_length (float, optional): Minimum length (in CRS units) for boundary lines to be included.
398
+ Lines shorter than this will be dropped. Default is None (no filtering).
399
+
400
+ Returns:
401
+ List[LineString]: List of LineString geometries representing boundaries.
402
+ """
403
+ boundary_pairs = HdfFluvialPluvial._get_boundary_cell_pairs(cell_adjacency, cell_times, delta_t)
404
+
405
+ boundary_edges = [common_edges[c1][c2] for c1, c2 in boundary_pairs]
406
+
407
+ logger.info(f"Identified {len(boundary_edges)} boundary edges using delta_t of {delta_t} hours.")
408
+
409
+ if min_line_length is not None:
410
+ filtered_edges = [edge for edge in boundary_edges if edge.length >= min_line_length]
411
+ num_dropped = len(boundary_edges) - len(filtered_edges)
412
+ if num_dropped > 0:
413
+ logger.info(f"{num_dropped} boundary line(s) shorter than {min_line_length} units were dropped after filtering.")
414
+ boundary_edges = filtered_edges
415
+
416
+ return boundary_edges
@@ -42,22 +42,6 @@ HdfUtils for common operations. Methods use @log_call decorator for logging and
42
42
 
43
43
 
44
44
 
45
- REVISIONS MADE:
46
-
47
- Use get_ prefix for functions that return data.
48
- BUT, we will never set results data, so we should use get_ for results data.
49
-
50
- Renamed functions:
51
- - mesh_summary_output() to get_mesh_summary()
52
- - mesh_timeseries_output() to get_mesh_timeseries()
53
- - mesh_faces_timeseries_output() to get_mesh_faces_timeseries()
54
- - mesh_cells_timeseries_output() to get_mesh_cells_timeseries()
55
- - mesh_last_iter() to get_mesh_last_iter()
56
- - mesh_max_ws() to get_mesh_max_ws()
57
-
58
-
59
-
60
-
61
45
 
62
46
 
63
47
 
@@ -651,7 +651,7 @@ class RasUnsteady:
651
651
  formatted_values = []
652
652
  for i in range(0, len(df), 10):
653
653
  row = df['Value'].iloc[i:i+10]
654
- formatted_row = ''.join(f'{value:8.0f}' for value in row)
654
+ formatted_row = ''.join(f'{value:8.2f}' for value in row)
655
655
  formatted_values.append(formatted_row + '\n')
656
656
 
657
657
  # Replace old table with new formatted values
@@ -10,7 +10,7 @@ try:
10
10
  __version__ = version("ras-commander")
11
11
  except PackageNotFoundError:
12
12
  # package is not installed
13
- __version__ = "0.77.0"
13
+ __version__ = "0.79.1"
14
14
 
15
15
  # Set up logging
16
16
  setup_logging()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ras-commander
3
- Version: 0.78.0
3
+ Version: 0.79.1
4
4
  Summary: A Python library for automating HEC-RAS 6.x operations
5
5
  Home-page: https://github.com/gpt-cmdr/ras-commander
6
6
  Author: William M. Katzenmeyer, P.E., C.F.M.
@@ -41,9 +41,11 @@ RAS Commander is a Python library for automating HEC-RAS operations, providing a
41
41
 
42
42
  *[Check out the ASFPM Presentation on RAS-Commander Here](https://drive.google.com/file/d/1kX0twae8NrpLwR0iQ0Dmd8zAXdq-pYXD/view)*
43
43
 
44
- ## Repository Author:
45
- [William Katzenmeyer, P.E., C.F.M.](https://engineeringwithllms.info)
46
- -----
44
+ ## Repository Author
45
+
46
+ **[William Katzenmeyer, P.E., C.F.M.](https://engineeringwithllms.info)**
47
+ Owner & Vice President, [CLB Engineering Corporation](https://clbengineering.com/)
48
+
47
49
 
48
50
  ## Don't Ask Me, Ask a GPT!
49
51
 
@@ -28,7 +28,7 @@ class CustomBuildPy(build_py):
28
28
 
29
29
  setup(
30
30
  name="ras-commander",
31
- version="0.78.0",
31
+ version="0.79.1",
32
32
  packages=find_packages(),
33
33
  include_package_data=True,
34
34
  python_requires='>=3.10',
@@ -1,554 +0,0 @@
1
- """
2
- Class: HdfFluvialPluvial
3
-
4
- All of the methods in this class are static and are designed to be used without instantiation.
5
-
6
- List of Functions in HdfFluvialPluvial:
7
- - calculate_fluvial_pluvial_boundary()
8
- - _process_cell_adjacencies()
9
- - _identify_boundary_edges()
10
-
11
- """
12
-
13
- from typing import Dict, List, Tuple
14
- import pandas as pd
15
- import geopandas as gpd
16
- from collections import defaultdict
17
- from shapely.geometry import LineString, MultiLineString # Added MultiLineString import
18
- from tqdm import tqdm
19
- from .HdfMesh import HdfMesh
20
- from .HdfUtils import HdfUtils
21
- from .Decorators import standardize_input
22
- from .HdfResultsMesh import HdfResultsMesh
23
- from .LoggingConfig import get_logger
24
- from pathlib import Path
25
-
26
- logger = get_logger(__name__)
27
-
28
- class HdfFluvialPluvial:
29
- """
30
- A class for analyzing and visualizing fluvial-pluvial boundaries in HEC-RAS 2D model results.
31
-
32
- This class provides methods to process and visualize HEC-RAS 2D model outputs,
33
- specifically focusing on the delineation of fluvial and pluvial flood areas.
34
- It includes functionality for calculating fluvial-pluvial boundaries based on
35
- the timing of maximum water surface elevations.
36
-
37
- Key Concepts:
38
- - Fluvial flooding: Flooding from rivers/streams
39
- - Pluvial flooding: Flooding from rainfall/surface water
40
- - Delta_t: Time threshold (in hours) used to distinguish between fluvial and pluvial cells.
41
- Cells with max WSE time differences greater than delta_t are considered boundaries.
42
-
43
- Data Requirements:
44
- - HEC-RAS plan HDF file containing:
45
- - 2D mesh cell geometry (accessed via HdfMesh)
46
- - Maximum water surface elevation times (accessed via HdfResultsMesh)
47
-
48
- Usage Example:
49
- >>> ras = init_ras_project(project_path, ras_version)
50
- >>> hdf_path = Path("path/to/plan.hdf")
51
- >>> boundary_gdf = HdfFluvialPluvial.calculate_fluvial_pluvial_boundary(
52
- ... hdf_path,
53
- ... delta_t=12
54
- ... )
55
- """
56
- def __init__(self):
57
- self.logger = get_logger(__name__) # Initialize logger with module name
58
-
59
- @staticmethod
60
- @standardize_input(file_type='plan_hdf')
61
- def calculate_fluvial_pluvial_boundary(hdf_path: Path, delta_t: float = 12) -> gpd.GeoDataFrame:
62
- """
63
- Calculate the fluvial-pluvial boundary based on cell polygons and maximum water surface elevation times.
64
-
65
- Args:
66
- hdf_path (Path): Path to the HEC-RAS plan HDF file
67
- delta_t (float): Threshold time difference in hours. Cells with time differences
68
- greater than this value are considered boundaries. Default is 12 hours.
69
-
70
- Returns:
71
- gpd.GeoDataFrame: GeoDataFrame containing the fluvial-pluvial boundaries with:
72
- - geometry: LineString features representing boundaries
73
- - CRS: Coordinate reference system matching the input HDF file
74
-
75
- Raises:
76
- ValueError: If no cell polygons or maximum water surface data found in HDF file
77
- Exception: If there are errors during boundary calculation
78
-
79
- Note:
80
- The returned boundaries represent locations where the timing of maximum water surface
81
- elevation changes significantly (> delta_t), indicating potential transitions between
82
- fluvial and pluvial flooding mechanisms.
83
- """
84
- try:
85
- # Get cell polygons from HdfMesh
86
- logger.info("Getting cell polygons from HDF file...")
87
- cell_polygons_gdf = HdfMesh.get_mesh_cell_polygons(hdf_path)
88
- if cell_polygons_gdf.empty:
89
- raise ValueError("No cell polygons found in HDF file")
90
-
91
- # Get max water surface data from HdfResultsMesh
92
- logger.info("Getting maximum water surface data from HDF file...")
93
- max_ws_df = HdfResultsMesh.get_mesh_max_ws(hdf_path)
94
- if max_ws_df.empty:
95
- raise ValueError("No maximum water surface data found in HDF file")
96
-
97
- # Convert timestamps using the renamed utility function
98
- logger.info("Converting maximum water surface timestamps...")
99
- if 'maximum_water_surface_time' in max_ws_df.columns:
100
- max_ws_df['maximum_water_surface_time'] = max_ws_df['maximum_water_surface_time'].apply(
101
- lambda x: HdfUtils.parse_ras_datetime(x) if isinstance(x, str) else x
102
- )
103
-
104
- # Process cell adjacencies
105
- logger.info("Processing cell adjacencies...")
106
- cell_adjacency, common_edges = HdfFluvialPluvial._process_cell_adjacencies(cell_polygons_gdf)
107
-
108
- # Get cell times from max_ws_df
109
- logger.info("Extracting cell times from maximum water surface data...")
110
- cell_times = max_ws_df.set_index('cell_id')['maximum_water_surface_time'].to_dict()
111
-
112
- # Identify boundary edges
113
- logger.info("Identifying boundary edges...")
114
- boundary_edges = HdfFluvialPluvial._identify_boundary_edges(
115
- cell_adjacency, common_edges, cell_times, delta_t
116
- )
117
-
118
- # FOCUS YOUR REVISIONS HERE:
119
- # Join adjacent LineStrings into simple LineStrings by connecting them at shared endpoints
120
- logger.info("Joining adjacent LineStrings into simple LineStrings...")
121
-
122
- def get_coords(geom):
123
- """Helper function to extract coordinates from geometry objects
124
-
125
- Args:
126
- geom: A Shapely LineString or MultiLineString geometry
127
-
128
- Returns:
129
- tuple: Tuple containing:
130
- - list of original coordinates [(x1,y1), (x2,y2),...]
131
- - list of rounded coordinates for comparison
132
- - None if invalid geometry
133
- """
134
- if isinstance(geom, LineString):
135
- orig_coords = list(geom.coords)
136
- # Round coordinates to 0.01 for comparison
137
- rounded_coords = [(round(x, 2), round(y, 2)) for x, y in orig_coords]
138
- return orig_coords, rounded_coords
139
- elif isinstance(geom, MultiLineString):
140
- orig_coords = list(geom.geoms[0].coords)
141
- rounded_coords = [(round(x, 2), round(y, 2)) for x, y in orig_coords]
142
- return orig_coords, rounded_coords
143
- return None, None
144
-
145
- def find_connecting_line(current_end, unused_lines, endpoint_counts, rounded_endpoints):
146
- """Find a line that connects to the current endpoint
147
-
148
- Args:
149
- current_end: Tuple of (x, y) coordinates
150
- unused_lines: Set of unused line indices
151
- endpoint_counts: Dict of endpoint occurrence counts
152
- rounded_endpoints: Dict of rounded endpoint coordinates
153
-
154
- Returns:
155
- tuple: (line_index, should_reverse, found) or (None, None, False)
156
- """
157
- rounded_end = (round(current_end[0], 2), round(current_end[1], 2))
158
-
159
- # Skip if current endpoint is connected to more than 2 lines
160
- if endpoint_counts.get(rounded_end, 0) > 2:
161
- return None, None, False
162
-
163
- for i in unused_lines:
164
- start, end = rounded_endpoints[i]
165
- if start == rounded_end and endpoint_counts.get(start, 0) <= 2:
166
- return i, False, True
167
- elif end == rounded_end and endpoint_counts.get(end, 0) <= 2:
168
- return i, True, True
169
- return None, None, False
170
-
171
- # Initialize data structures
172
- joined_lines = []
173
- unused_lines = set(range(len(boundary_edges)))
174
-
175
- # Create endpoint lookup dictionaries
176
- line_endpoints = {}
177
- rounded_endpoints = {}
178
- for i, edge in enumerate(boundary_edges):
179
- coords_result = get_coords(edge)
180
- if coords_result:
181
- orig_coords, rounded_coords = coords_result
182
- line_endpoints[i] = (orig_coords[0], orig_coords[-1])
183
- rounded_endpoints[i] = (rounded_coords[0], rounded_coords[-1])
184
-
185
- # Count endpoint occurrences
186
- endpoint_counts = {}
187
- for start, end in rounded_endpoints.values():
188
- endpoint_counts[start] = endpoint_counts.get(start, 0) + 1
189
- endpoint_counts[end] = endpoint_counts.get(end, 0) + 1
190
-
191
- # Iteratively join lines
192
- while unused_lines:
193
- # Start a new line chain
194
- current_points = []
195
-
196
- # Find first unused line
197
- start_idx = unused_lines.pop()
198
- start_coords, _ = get_coords(boundary_edges[start_idx])
199
- if start_coords:
200
- current_points.extend(start_coords)
201
-
202
- # Try to extend in both directions
203
- continue_joining = True
204
- while continue_joining:
205
- continue_joining = False
206
-
207
- # Try to extend forward
208
- next_idx, should_reverse, found = find_connecting_line(
209
- current_points[-1],
210
- unused_lines,
211
- endpoint_counts,
212
- rounded_endpoints
213
- )
214
-
215
- if found:
216
- unused_lines.remove(next_idx)
217
- next_coords, _ = get_coords(boundary_edges[next_idx])
218
- if next_coords:
219
- if should_reverse:
220
- current_points.extend(reversed(next_coords[:-1]))
221
- else:
222
- current_points.extend(next_coords[1:])
223
- continue_joining = True
224
- continue
225
-
226
- # Try to extend backward
227
- prev_idx, should_reverse, found = find_connecting_line(
228
- current_points[0],
229
- unused_lines,
230
- endpoint_counts,
231
- rounded_endpoints
232
- )
233
-
234
- if found:
235
- unused_lines.remove(prev_idx)
236
- prev_coords, _ = get_coords(boundary_edges[prev_idx])
237
- if prev_coords:
238
- if should_reverse:
239
- current_points[0:0] = reversed(prev_coords[:-1])
240
- else:
241
- current_points[0:0] = prev_coords[:-1]
242
- continue_joining = True
243
-
244
- # Create final LineString from collected points
245
- if current_points:
246
- joined_lines.append(LineString(current_points))
247
-
248
- # FILL GAPS BETWEEN JOINED LINES
249
- logger.info(f"Starting gap analysis for {len(joined_lines)} line segments...")
250
-
251
- def find_endpoints(lines):
252
- """Get all endpoints of the lines with their indices"""
253
- endpoints = []
254
- for i, line in enumerate(lines):
255
- coords = list(line.coords)
256
- endpoints.append((coords[0], i, 'start'))
257
- endpoints.append((coords[-1], i, 'end'))
258
- return endpoints
259
-
260
- def find_nearby_points(point1, point2, tolerance=0.01):
261
- """Check if two points are within tolerance distance"""
262
- return (abs(point1[0] - point2[0]) <= tolerance and
263
- abs(point1[1] - point2[1]) <= tolerance)
264
-
265
- def find_gaps(lines, tolerance=0.01):
266
- """Find gaps between line endpoints"""
267
- logger.info("Analyzing line endpoints to identify gaps...")
268
- endpoints = []
269
- for i, line in enumerate(lines):
270
- coords = list(line.coords)
271
- start = coords[0]
272
- end = coords[-1]
273
- endpoints.append({
274
- 'point': start,
275
- 'line_idx': i,
276
- 'position': 'start',
277
- 'coords': coords
278
- })
279
- endpoints.append({
280
- 'point': end,
281
- 'line_idx': i,
282
- 'position': 'end',
283
- 'coords': coords
284
- })
285
-
286
- logger.info(f"Found {len(endpoints)} endpoints to analyze")
287
- gaps = []
288
-
289
- # Compare each endpoint with all others
290
- for i, ep1 in enumerate(endpoints):
291
- for ep2 in endpoints[i+1:]:
292
- # Skip if endpoints are from same line
293
- if ep1['line_idx'] == ep2['line_idx']:
294
- continue
295
-
296
- point1 = ep1['point']
297
- point2 = ep2['point']
298
-
299
- # Skip if points are too close (already connected)
300
- if find_nearby_points(point1, point2):
301
- continue
302
-
303
- # Check if this could be a gap
304
- dist = LineString([point1, point2]).length
305
- if dist < 10.0: # Maximum gap distance threshold
306
- gaps.append({
307
- 'start': ep1,
308
- 'end': ep2,
309
- 'distance': dist
310
- })
311
-
312
- logger.info(f"Identified {len(gaps)} potential gaps to fill")
313
- return sorted(gaps, key=lambda x: x['distance'])
314
-
315
- def join_lines_with_gap(line1_coords, line2_coords, gap_start_pos, gap_end_pos):
316
- """Join two lines maintaining correct point order based on gap positions"""
317
- if gap_start_pos == 'end' and gap_end_pos == 'start':
318
- # line1 end connects to line2 start
319
- return line1_coords + line2_coords
320
- elif gap_start_pos == 'start' and gap_end_pos == 'end':
321
- # line1 start connects to line2 end
322
- return list(reversed(line2_coords)) + line1_coords
323
- elif gap_start_pos == 'end' and gap_end_pos == 'end':
324
- # line1 end connects to line2 end
325
- return line1_coords + list(reversed(line2_coords))
326
- else: # start to start
327
- # line1 start connects to line2 start
328
- return list(reversed(line1_coords)) + line2_coords
329
-
330
- # Process gaps and join lines
331
- processed_lines = joined_lines.copy()
332
- line_groups = [[i] for i in range(len(processed_lines))]
333
- gaps = find_gaps(processed_lines)
334
-
335
- filled_gap_count = 0
336
- for gap_idx, gap in enumerate(gaps, 1):
337
- logger.info(f"Processing gap {gap_idx}/{len(gaps)} (distance: {gap['distance']:.3f})")
338
-
339
- line1_idx = gap['start']['line_idx']
340
- line2_idx = gap['end']['line_idx']
341
-
342
- # Find the groups containing these lines
343
- group1 = next(g for g in line_groups if line1_idx in g)
344
- group2 = next(g for g in line_groups if line2_idx in g)
345
-
346
- # Skip if lines are already in the same group
347
- if group1 == group2:
348
- continue
349
-
350
- # Get the coordinates for both lines
351
- line1_coords = gap['start']['coords']
352
- line2_coords = gap['end']['coords']
353
-
354
- # Join the lines in correct order
355
- joined_coords = join_lines_with_gap(
356
- line1_coords,
357
- line2_coords,
358
- gap['start']['position'],
359
- gap['end']['position']
360
- )
361
-
362
- # Create new joined line
363
- new_line = LineString(joined_coords)
364
-
365
- # Update processed_lines and line_groups
366
- new_idx = len(processed_lines)
367
- processed_lines.append(new_line)
368
-
369
- # Merge groups and remove old ones
370
- new_group = group1 + group2
371
- line_groups.remove(group1)
372
- line_groups.remove(group2)
373
- line_groups.append(new_group + [new_idx])
374
-
375
- filled_gap_count += 1
376
- logger.info(f"Successfully joined lines {line1_idx} and {line2_idx}")
377
-
378
- logger.info(f"Gap filling complete. Filled {filled_gap_count} out of {len(gaps)} gaps")
379
-
380
- # Get final lines (take the last line from each group)
381
- final_lines = [processed_lines[group[-1]] for group in line_groups]
382
-
383
- logger.info(f"Final cleanup complete. Resulting in {len(final_lines)} line segments")
384
- joined_lines = final_lines
385
-
386
- # Create final GeoDataFrame with CRS from cell_polygons_gdf
387
- logger.info("Creating final GeoDataFrame for boundaries...")
388
- boundary_gdf = gpd.GeoDataFrame(
389
- geometry=joined_lines,
390
- crs=cell_polygons_gdf.crs
391
- )
392
-
393
- # Clean up intermediate dataframes
394
- logger.info("Cleaning up intermediate dataframes...")
395
- del cell_polygons_gdf
396
- del max_ws_df
397
-
398
- logger.info("Fluvial-pluvial boundary calculation completed successfully.")
399
- return boundary_gdf
400
-
401
- except Exception as e:
402
- self.logger.error(f"Error calculating fluvial-pluvial boundary: {str(e)}")
403
- return None
404
-
405
-
406
- @staticmethod
407
- def _process_cell_adjacencies(cell_polygons_gdf: gpd.GeoDataFrame) -> Tuple[Dict[int, List[int]], Dict[int, Dict[int, LineString]]]:
408
- """
409
- Optimized method to process cell adjacencies by extracting shared edges directly.
410
-
411
- Args:
412
- cell_polygons_gdf (gpd.GeoDataFrame): GeoDataFrame containing 2D mesh cell polygons
413
- with 'cell_id' and 'geometry' columns.
414
-
415
- Returns:
416
- Tuple containing:
417
- - Dict[int, List[int]]: Dictionary mapping cell IDs to lists of adjacent cell IDs.
418
- - Dict[int, Dict[int, LineString]]: Nested dictionary storing common edges between cells,
419
- where common_edges[cell1][cell2] gives the shared boundary.
420
- """
421
- cell_adjacency = defaultdict(list)
422
- common_edges = defaultdict(dict)
423
-
424
- # Build an edge to cells mapping
425
- edge_to_cells = defaultdict(set)
426
-
427
- # Function to generate edge keys
428
- def edge_key(coords1, coords2, precision=8):
429
- # Round coordinates
430
- coords1 = tuple(round(coord, precision) for coord in coords1)
431
- coords2 = tuple(round(coord, precision) for coord in coords2)
432
- # Create sorted key to handle edge direction
433
- return tuple(sorted([coords1, coords2]))
434
-
435
- # For each polygon, extract edges
436
- for idx, row in cell_polygons_gdf.iterrows():
437
- cell_id = row['cell_id']
438
- geom = row['geometry']
439
- if geom.is_empty or not geom.is_valid:
440
- continue
441
- # Get exterior coordinates
442
- coords = list(geom.exterior.coords)
443
- num_coords = len(coords)
444
- for i in range(num_coords - 1):
445
- coord1 = coords[i]
446
- coord2 = coords[i + 1]
447
- key = edge_key(coord1, coord2)
448
- edge_to_cells[key].add(cell_id)
449
-
450
- # Now, process edge_to_cells to build adjacency
451
- for edge, cells in edge_to_cells.items():
452
- cells = list(cells)
453
- if len(cells) >= 2:
454
- # For all pairs of cells sharing this edge
455
- for i in range(len(cells)):
456
- for j in range(i + 1, len(cells)):
457
- cell1 = cells[i]
458
- cell2 = cells[j]
459
- # Update adjacency
460
- if cell2 not in cell_adjacency[cell1]:
461
- cell_adjacency[cell1].append(cell2)
462
- if cell1 not in cell_adjacency[cell2]:
463
- cell_adjacency[cell2].append(cell1)
464
- # Store common edge
465
- common_edge = LineString([edge[0], edge[1]])
466
- common_edges[cell1][cell2] = common_edge
467
- common_edges[cell2][cell1] = common_edge
468
-
469
- logger.info("Cell adjacencies processed successfully.")
470
- return cell_adjacency, common_edges
471
-
472
- @staticmethod
473
- def _identify_boundary_edges(cell_adjacency: Dict[int, List[int]],
474
- common_edges: Dict[int, Dict[int, LineString]],
475
- cell_times: Dict[int, pd.Timestamp],
476
- delta_t: float) -> List[LineString]:
477
- """
478
- Identify boundary edges between cells with significant time differences.
479
-
480
- Args:
481
- cell_adjacency (Dict[int, List[int]]): Dictionary of cell adjacencies
482
- common_edges (Dict[int, Dict[int, LineString]]): Dictionary of shared edges between cells
483
- cell_times (Dict[int, pd.Timestamp]): Dictionary mapping cell IDs to their max WSE times
484
- delta_t (float): Time threshold in hours
485
-
486
- Returns:
487
- List[LineString]: List of LineString geometries representing boundaries
488
- """
489
- # Validate cell_times data
490
- valid_times = {k: v for k, v in cell_times.items() if pd.notna(v)}
491
- if len(valid_times) < len(cell_times):
492
- logger.warning(f"Found {len(cell_times) - len(valid_times)} cells with invalid timestamps")
493
- cell_times = valid_times
494
-
495
- # Use a set to store processed cell pairs and avoid duplicates
496
- processed_pairs = set()
497
- boundary_edges = []
498
-
499
- # Track time differences for debugging
500
- time_diffs = []
501
-
502
- with tqdm(total=len(cell_adjacency), desc="Processing cell adjacencies") as pbar:
503
- for cell_id, neighbors in cell_adjacency.items():
504
- if cell_id not in cell_times:
505
- logger.debug(f"Skipping cell {cell_id} - no timestamp data")
506
- pbar.update(1)
507
- continue
508
-
509
- cell_time = cell_times[cell_id]
510
-
511
- for neighbor_id in neighbors:
512
- if neighbor_id not in cell_times:
513
- logger.debug(f"Skipping neighbor {neighbor_id} of cell {cell_id} - no timestamp data")
514
- continue
515
-
516
- # Create a sorted tuple of the cell pair to ensure uniqueness
517
- cell_pair = tuple(sorted([cell_id, neighbor_id]))
518
-
519
- # Skip if we've already processed this pair
520
- if cell_pair in processed_pairs:
521
- continue
522
-
523
- neighbor_time = cell_times[neighbor_id]
524
-
525
- # Ensure both timestamps are valid
526
- if pd.isna(cell_time) or pd.isna(neighbor_time):
527
- continue
528
-
529
- # Calculate time difference in hours
530
- time_diff = abs((cell_time - neighbor_time).total_seconds() / 3600)
531
- time_diffs.append(time_diff)
532
-
533
- logger.debug(f"Time difference between cells {cell_id} and {neighbor_id}: {time_diff:.2f} hours")
534
-
535
- if time_diff >= delta_t:
536
- logger.debug(f"Found boundary edge between cells {cell_id} and {neighbor_id} "
537
- f"(time diff: {time_diff:.2f} hours)")
538
- boundary_edges.append(common_edges[cell_id][neighbor_id])
539
-
540
- # Mark this pair as processed
541
- processed_pairs.add(cell_pair)
542
-
543
- pbar.update(1)
544
-
545
- # Log summary statistics
546
- if time_diffs:
547
- logger.info(f"Time difference statistics:")
548
- logger.info(f" Min: {min(time_diffs):.2f} hours")
549
- logger.info(f" Max: {max(time_diffs):.2f} hours")
550
- logger.info(f" Mean: {sum(time_diffs)/len(time_diffs):.2f} hours")
551
- logger.info(f" Number of boundaries found: {len(boundary_edges)}")
552
- logger.info(f" Delta-t threshold: {delta_t} hours")
553
-
554
- return boundary_edges
File without changes
File without changes