Skip to content

Commit 8a9194c

Browse files
committed
introduce Rprofs ABC
`RprofsInstant` (replacing the previous `Rprofs`) and `RprofsAveraged` both implement this ABC instead of having the latter inherit from the former.
1 parent 777f6fd commit 8a9194c

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

src/stagpy/stagyydata.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,11 @@ class RprofsAveraged(step.Rprofs):
230230
231231
The [`StepsView.rprofs_averaged`][stagpy.stagyydata.StepsView.rprofs_averaged]
232232
attribute is an instance of this class.
233-
234-
It implements the same interface as [`Rprofs`][stagpy.step.Rprofs] but
235-
returns time-averaged profiles instead.
236233
"""
237234

238235
def __init__(self, steps: StepsView):
239236
self.steps = steps.filter(rprofs=True)
240237
self._cached_data: dict[str, dt.Rprof] = {}
241-
super().__init__(next(iter(self.steps)))
242238

243239
def __getitem__(self, name: str) -> dt.Rprof:
244240
# the averaging method has two shortcomings:
@@ -259,9 +255,25 @@ def __getitem__(self, name: str) -> dt.Rprof:
259255

260256
@property
261257
def stepstr(self) -> str:
262-
"""String representation of steps indices."""
263258
return self.steps.stepstr
264259

260+
@cached_property
261+
def _first_rprofs(self) -> step.RprofsInstant:
262+
first_step = next(iter(self.steps))
263+
return first_step.rprofs
264+
265+
@property
266+
def centers(self) -> NDArray:
267+
return self._first_rprofs.centers
268+
269+
@property
270+
def walls(self) -> NDArray:
271+
return self._first_rprofs.walls
272+
273+
@property
274+
def bounds(self) -> tuple[float, float]:
275+
return self._first_rprofs.bounds
276+
265277

266278
@dataclass(frozen=True)
267279
class Steps:

src/stagpy/step.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from __future__ import annotations
1010

1111
import typing
12+
from abc import ABC, abstractmethod
1213
from collections import abc
1314
from functools import cached_property
1415
from itertools import chain
@@ -449,16 +450,47 @@ def __iter__(self) -> NoReturn:
449450
raise TypeError("tracers collection is not iterable")
450451

451452

452-
class Rprofs:
453-
"""Radial profiles data structure.
454-
455-
The `Step.rprofs` attribute is an instance of this class.
453+
class Rprofs(ABC):
454+
"""Radial profiles.
456455
457456
`Rprofs` implements the getitem mechanism. Keys are profile names
458457
defined in `stagpy.phyvars.RPROF[_EXTRA]`. Items are
459458
[`Rprof`][stagpy.datatypes.Rprof] instances.
460459
"""
461460

461+
@abstractmethod
462+
def __getitem__(self, name: str) -> Rprof: ...
463+
464+
@property
465+
@abstractmethod
466+
def stepstr(self) -> str:
467+
"""String representation of steps indices."""
468+
469+
@property
470+
@abstractmethod
471+
def centers(self) -> NDArray:
472+
"""Radial position of cell centers."""
473+
474+
@property
475+
@abstractmethod
476+
def walls(self) -> NDArray:
477+
"""Radial position of cell walls."""
478+
479+
@property
480+
@abstractmethod
481+
def bounds(self) -> tuple[float, float]:
482+
"""Radial or vertical position of boundaries.
483+
484+
Radial/vertical positions of boundaries of the domain.
485+
"""
486+
487+
488+
class RprofsInstant(Rprofs):
489+
"""Radial profiles at a given step.
490+
491+
The `Step.rprofs` attribute is an instance of this class.
492+
"""
493+
462494
def __init__(self, step: Step):
463495
self.step = step
464496
self._cached_extra: dict[str, Rprof] = {}
@@ -595,7 +627,7 @@ def __init__(self, istep: int, sdat: StagyyData):
595627
self, phyvars.SFIELD, {}, phyvars.SFIELD_FILES, phyvars.SFIELD_FILES_H5
596628
)
597629
self.tracers = Tracers(self)
598-
self.rprofs = Rprofs(self)
630+
self.rprofs = RprofsInstant(self)
599631

600632
def __repr__(self) -> str:
601633
if self.isnap is not None:

0 commit comments

Comments
 (0)