|
1 | 1 | import math
|
2 | 2 | import os
|
3 |
| -from typing import List, Dict, Tuple, Union |
| 3 | +from typing import List, Dict, Tuple, Union, Sequence |
4 | 4 | from typing_extensions import Literal
|
5 | 5 | import numpy as np
|
6 | 6 |
|
@@ -122,7 +122,7 @@ class Boundary:
|
122 | 122 | :ivar n_t: Total number of time steps for which output data has been written.
|
123 | 123 | """
|
124 | 124 |
|
125 |
| - def __init__(self, quantity: Quantity, cell_centered: bool, times: np.ndarray, n_t: int, patches: List[Patch], |
| 125 | + def __init__(self, quantity: Quantity, cell_centered: bool, times: Sequence[float], n_t: int, patches: List[Patch], |
126 | 126 | lower_bounds: np.ndarray, upper_bounds: np.ndarray):
|
127 | 127 | self.quantity = quantity
|
128 | 128 | self.cell_centered = cell_centered
|
@@ -215,7 +215,7 @@ def __init__(self, side_surfaces: Tuple, bound_indices: Tuple[int, int, int, int
|
215 | 215 | self.show_times = list()
|
216 | 216 |
|
217 | 217 | def _add_patches(self, bid: int, cell_centered: bool, quantity: str, short_name: str, unit: str,
|
218 |
| - patches: List[Patch], times: np.ndarray, n_t: int, lower_bounds: np.ndarray, |
| 218 | + patches: List[Patch], times: Sequence[float], n_t: int, lower_bounds: np.ndarray, |
219 | 219 | upper_bounds: np.ndarray):
|
220 | 220 | if bid not in self._boundary_data:
|
221 | 221 | self._boundary_data[bid] = Boundary(Quantity(quantity, short_name, unit), cell_centered, times, n_t,
|
@@ -276,20 +276,28 @@ def _show(self, time: float):
|
276 | 276 |
|
277 | 277 | @property
|
278 | 278 | def n_t(self):
|
279 |
| - return next(iter(self._boundary_data.values())).n_t |
| 279 | + """Returns the number of timesteps for which boundary data is available. |
| 280 | + """ |
| 281 | + if self.has_boundary_data: |
| 282 | + return next(iter(self._boundary_data.values())).n_t |
| 283 | + else: |
| 284 | + return 0 |
280 | 285 |
|
281 | 286 | @property
|
282 | 287 | def times(self):
|
283 |
| - return next(iter(self._boundary_data.values())).times |
| 288 | + """Return all timesteps for which boundary data is available, if any. |
| 289 | + """ |
| 290 | + if self.has_boundary_data: |
| 291 | + return next(iter(self._boundary_data.values())).times |
| 292 | + else: |
| 293 | + return np.array([]) |
284 | 294 |
|
285 |
| - @property |
286 |
| - def visible_times(self) -> np.ndarray: |
287 |
| - """Returns an ndarray containing all time steps when there is data available for the SubObstruction. Will return an |
288 |
| - empty list when no data is output at all. |
| 295 | + def get_visible_times(self, times: Sequence[float]) -> np.ndarray: |
| 296 | + """Returns an ndarray filtering all time steps when theSubObstruction is visible/not hidden. |
289 | 297 | """
|
290 | 298 | ret = list()
|
291 | 299 | hidden = False
|
292 |
| - for time in self.times: |
| 300 | + for time in times: |
293 | 301 | if time in self.show_times:
|
294 | 302 | hidden = False
|
295 | 303 | if time in self.hide_times:
|
@@ -375,15 +383,20 @@ def orientations(self):
|
375 | 383 |
|
376 | 384 | @property
|
377 | 385 | def n_t(self):
|
| 386 | + """Returns the number of timesteps for which boundary data is available. |
| 387 | + """ |
378 | 388 | return next(iter(self._subobstructions.values())).n_t
|
379 | 389 |
|
380 | 390 | @property
|
381 | 391 | def times(self):
|
| 392 | + """Return all timesteps for which boundary data is available, if any. |
| 393 | + """ |
382 | 394 | return next(iter(self._subobstructions.values())).times
|
383 | 395 |
|
384 |
| - @property |
385 |
| - def visible_times(self): |
386 |
| - return next(iter(self._subobstructions.values())).visible_times |
| 396 | + def get_visible_times(self, times: Sequence[float]): |
| 397 | + """Returns an ndarray filtering all time steps when theSubObstruction is visible/not hidden. |
| 398 | + """ |
| 399 | + return next(iter(self._subobstructions.values())).get_visible_times(times) |
387 | 400 |
|
388 | 401 | def get_coordinates(self, ignore_cell_centered: bool = False) -> Dict[
|
389 | 402 | int, Dict[Literal['x', 'y', 'z'], np.ndarray]]:
|
@@ -489,7 +502,7 @@ def get_boundary_data(self, quantity: Union[Quantity, str],
|
489 | 502 | def get_nearest_timestep(self, time: float, visible_only: bool = False) -> int:
|
490 | 503 | """Calculates the nearest timestep for which data has been output for this obstruction.
|
491 | 504 | """
|
492 |
| - times = self.visible_times if visible_only else self.times |
| 505 | + times = self.get_visible_times(self.times) if visible_only else self.times |
493 | 506 | idx = np.searchsorted(times, time, side="left")
|
494 | 507 | if time > 0 and (idx == len(times) or np.math.fabs(
|
495 | 508 | time - times[idx - 1]) < np.math.fabs(time - times[idx])):
|
|
0 commit comments