Skip to content

Commit 9081b9b

Browse files
committed
mv StagyyData.walk to _helpers.walk
1 parent 6336283 commit 9081b9b

File tree

8 files changed

+46
-33
lines changed

8 files changed

+46
-33
lines changed

stagpy/_helpers.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@
1515
from matplotlib.figure import Figure
1616
from numpy import ndarray
1717

18+
from .config import Config
19+
from .stagyydata import StagyyData, _StepsView
20+
21+
22+
def walk(sdat: StagyyData, conf: Config) -> _StepsView:
23+
"""Return view on configured steps slice.
24+
25+
Other Parameters:
26+
conf.core.snapshots: the slice of snapshots.
27+
conf.core.timesteps: the slice of timesteps.
28+
"""
29+
if conf.core.timesteps:
30+
return sdat.steps[conf.core.timesteps]
31+
return sdat.snaps[conf.core.snapshots]
32+
1833

1934
def out_name(stem: str, timestep: Optional[int] = None) -> str:
2035
"""Return StagPy out file name.

stagpy/commands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from textwrap import TextWrapper, indent
1111

1212
from . import __version__, phyvars, stagyydata
13-
from ._helpers import baredoc
13+
from ._helpers import baredoc, walk
1414
from .config import CONFIG_LOCAL, Config
1515

1616
if typing.TYPE_CHECKING:
@@ -46,7 +46,7 @@ def info_cmd() -> None:
4646
else:
4747
print("Spherical", dimension)
4848
print()
49-
for step in sdat.walk:
49+
for step in walk(sdat, conf):
5050
print(f"Step {step.istep}/{lstep.istep}", end="")
5151
if step.isnap is not None:
5252
print(f", snapshot {step.isnap}/{lsnap.isnap}")

stagpy/field.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
from ._step import Step
2929
from .datatypes import Varf
30+
from .stagyydata import _StepsView
3031

3132

3233
# The location is off for vertical velocities: they have an extra
@@ -335,11 +336,11 @@ def plot_vec(
335336

336337

337338
def _findminmax(
338-
sdat: StagyyData, sovs: Iterable[str]
339+
view: _StepsView, sovs: Iterable[str]
339340
) -> Dict[str, Tuple[float, float]]:
340341
"""Find min and max values of several fields."""
341342
minmax: Dict[str, Tuple[float, float]] = {}
342-
for step in sdat.walk.filter(snap=True):
343+
for step in view.filter(snap=True):
343344
for var in sovs:
344345
if var in step.fields:
345346
vals = step.fields[var].values
@@ -363,15 +364,16 @@ def cmd() -> None:
363364
from . import conf
364365

365366
sdat = StagyyData(conf.core.path)
367+
view = _helpers.walk(sdat, conf)
366368
# no more than two fields in a subplot
367369
lovs = [[slov[:2] for slov in plov] for plov in conf.field.plot]
368370
minmax = {}
369371
if conf.plot.cminmax:
370372
conf.plot.vmin = None
371373
conf.plot.vmax = None
372374
sovs = set(slov[0] for plov in lovs for slov in plov)
373-
minmax = _findminmax(sdat, sovs)
374-
for step in sdat.walk.filter(snap=True):
375+
minmax = _findminmax(view, sovs)
376+
for step in view.filter(snap=True):
375377
for vfig in lovs:
376378
fig, axes = plt.subplots(
377379
ncols=len(vfig), squeeze=False, figsize=(6 * len(vfig), 6)

stagpy/plates.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,21 +400,22 @@ def cmd() -> None:
400400
from . import conf
401401

402402
sdat = StagyyData(conf.core.path)
403+
view = _helpers.walk(sdat, conf)
403404

404-
isurf = _isurf(next(iter(sdat.walk)))
405-
vrms_surf = sdat.walk.filter(rprofs=True).rprofs_averaged["vhrms"].values[isurf]
405+
isurf = _isurf(next(iter(view)))
406+
vrms_surf = view.filter(rprofs=True).rprofs_averaged["vhrms"].values[isurf]
406407
nb_plates = []
407408
time = []
408409
istart, iend = None, None
409410

410-
oname = _helpers.out_name(f"plates_trenches_{sdat.walk.stepstr}")
411+
oname = _helpers.out_name(f"plates_trenches_{view.stepstr}")
411412
with open(f"{oname}.dat", "w") as fid:
412413
fid.write(
413414
"# istep time time_My phi_trench vel_trench "
414415
"distance phi_cont age_trench_My\n"
415416
)
416417

417-
for step in sdat.walk.filter(fields=["T"]):
418+
for step in view.filter(fields=["T"]):
418419
# could check other fields too
419420
_write_trench_diagnostics(step, vrms_surf, fid, conf)
420421
plot_at_surface(step, conf.plates.plot, conf)

stagpy/rprof.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,14 @@ def cmd() -> None:
9999
from . import conf
100100

101101
sdat = StagyyData(conf.core.path)
102+
view = _helpers.walk(sdat, conf)
102103

103104
if conf.rprof.grid:
104-
for step in sdat.walk.filter(rprofs=True):
105+
for step in view.filter(rprofs=True):
105106
plot_grid(step)
106107

107108
if conf.rprof.average:
108-
plot_rprofs(sdat.walk.rprofs_averaged, conf.rprof.plot, conf)
109+
plot_rprofs(view.rprofs_averaged, conf.rprof.plot, conf)
109110
else:
110-
for step in sdat.walk.filter(rprofs=True):
111+
for step in view.filter(rprofs=True):
111112
plot_rprofs(step.rprofs, conf.rprof.plot, conf)

stagpy/stagyydata.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import numpy as np
2121

22-
from . import _helpers, _step, conf, error, phyvars, stagyyparsers
22+
from . import _helpers, _step, error, phyvars, stagyyparsers
2323
from ._step import Step
2424
from .datatypes import Rprof, Tseries, Vart
2525
from .parfile import StagyyPar
@@ -762,18 +762,6 @@ def _files(self) -> Set[Path]:
762762
return set(out_dir.iterdir())
763763
return set()
764764

765-
@property
766-
def walk(self) -> _StepsView:
767-
"""Return view on configured steps slice.
768-
769-
Other Parameters:
770-
conf.core.snapshots: the slice of snapshots.
771-
conf.core.timesteps: the slice of timesteps.
772-
"""
773-
if conf.core.timesteps:
774-
return self.steps[conf.core.timesteps]
775-
return self.snaps[conf.core.snapshots]
776-
777765
@property
778766
def nfields_max(self) -> Optional[int]:
779767
"""Maximum number of scalar fields kept in memory.

tests/test_helpers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
import pytest
2+
13
import stagpy
24
import stagpy._helpers
5+
from stagpy import _helpers
6+
from stagpy.config import Config
7+
from stagpy.stagyydata import StagyyData
8+
9+
10+
def test_walk_dflt(sdat: StagyyData) -> None:
11+
view = _helpers.walk(sdat, Config.default_())
12+
wlk = iter(view)
13+
assert next(wlk) is sdat.snaps[-1]
14+
with pytest.raises(StopIteration):
15+
next(wlk)
316

417

518
def test_out_name_conf() -> None:

tests/test_stagyydata.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ def test_sdat_tseries(sdat: StagyyData) -> None:
3535
assert sdat.tseries["Tmean"].time is sdat.tseries.time
3636

3737

38-
def test_sdat_walk_dflt(sdat: StagyyData) -> None:
39-
wlk = iter(sdat.walk)
40-
assert next(wlk) is sdat.snaps[-1]
41-
with pytest.raises(StopIteration):
42-
next(wlk)
43-
44-
4538
def test_steps_iter(sdat: StagyyData) -> None:
4639
assert sdat.steps[:] == sdat.steps
4740

0 commit comments

Comments
 (0)