Skip to content

Commit d3016b6

Browse files
committed
Use rprofs API to simplify rprof module
1 parent 3559c54 commit d3016b6

File tree

1 file changed

+26
-65
lines changed

1 file changed

+26
-65
lines changed

stagpy/rprof.py

Lines changed: 26 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,24 @@
77
from .stagyydata import StagyyData
88

99

10-
def _plot_rprof_list(sdat, lovs, rprofs, stepstr):
11-
"""Plot requested profiles."""
10+
def plot_rprofs(rprofs, lovs):
11+
"""Plot requested radial profiles.
12+
13+
Args:
14+
rprofs (:class:`~stagpy._step._Rprofs`): a radial profile viewer
15+
such as :attr:`Step.rprofs` and :attr:`_StepsView.rprofs_averaged`.
16+
lovs (nested list of str): nested list of profile names such as the one
17+
produced by :func:`stagpy.misc.list_of_vars`.
18+
"""
19+
try:
20+
stepstr = repr(rprofs.steps)
21+
reg = re.compile(
22+
r'^StagyyData\(.*\)\.(steps|snaps)\[(.*)\](?:.filter\(.*\))?$')
23+
stepstr = '_'.join(reg.match(stepstr).groups())
24+
except AttributeError:
25+
stepstr = str(rprofs.step.istep)
26+
sdat = rprofs.step.sdat
27+
1228
for vfig in lovs:
1329
fig, axes = plt.subplots(ncols=len(vfig), sharey=True,
1430
figsize=(4 * len(vfig), 6))
@@ -20,12 +36,10 @@ def _plot_rprof_list(sdat, lovs, rprofs, stepstr):
2036
fname += '_'.join(vplt) + '_'
2137
for ivar, (rprof, rad, meta) in enumerate(profs_on_plt):
2238
if conf.rprof.depth:
23-
rad = rprofs['bounds'][1] - rad
39+
rad = sdat.scale(rprofs.bounds[1], 'm')[0] - rad
2440
axes[iplt].plot(rprof, rad,
2541
conf.rprof.style,
2642
label=meta.description)
27-
if conf.rprof.depth:
28-
axes[iplt].invert_yaxis()
2943
if xlabel is None:
3044
xlabel = meta.kind
3145
elif xlabel != meta.kind:
@@ -34,18 +48,18 @@ def _plot_rprof_list(sdat, lovs, rprofs, stepstr):
3448
xlabel = meta.description
3549
if xlabel:
3650
_, unit = sdat.scale(1, meta.dim)
37-
if unit:
38-
xlabel += f' ({unit})'
51+
xlabel += f' ({unit})' if unit else ''
3952
axes[iplt].set_xlabel(xlabel)
4053
if vplt[0][:3] == 'eta': # list of log variables
4154
axes[iplt].set_xscale('log')
4255
axes[iplt].set_xlim(left=conf.plot.vmin, right=conf.plot.vmax)
4356
if ivar:
4457
axes[iplt].legend()
58+
if conf.rprof.depth:
59+
axes[0].invert_yaxis()
4560
ylabel = 'Depth' if conf.rprof.depth else 'Radius'
4661
_, unit = sdat.scale(1, 'm')
47-
if unit:
48-
ylabel += f' ({unit})'
62+
ylabel += f' ({unit})' if unit else ''
4963
axes[0].set_ylabel(ylabel)
5064
misc.saveplot(fig, fname + stepstr)
5165

@@ -73,60 +87,6 @@ def plot_grid(step):
7387
misc.saveplot(fig, 'grid', step.istep)
7488

7589

76-
def plot_average(sdat, lovs):
77-
"""Plot time averaged profiles.
78-
79-
Args:
80-
sdat (:class:`~stagpy.stagyydata.StagyyData`): a StagyyData instance.
81-
lovs (nested list of str): nested list of profile names such as
82-
the one produced by :func:`stagpy.misc.list_of_vars`.
83-
84-
Other Parameters:
85-
conf.core.snapshots: the slice of snapshots.
86-
conf.conf.timesteps: the slice of timesteps.
87-
"""
88-
reg = re.compile(
89-
r'^StagyyData\(.*\)\.(steps|snaps)\[(.*)\](?:.filter\(.*\))?$')
90-
stepstr = repr(sdat.walk)
91-
stepstr = '_'.join(reg.match(stepstr).groups())
92-
rprofs = sdat.walk.rprofs_averaged
93-
94-
sovs = misc.set_of_vars(lovs)
95-
96-
rprof_averaged = {rvar: rprofs[rvar] for rvar in sovs}
97-
98-
rcmb, rsurf = rprofs.bounds
99-
step = rprofs.step
100-
rprof_averaged['bounds'] = (step.sdat.scale(rcmb, 'm')[0],
101-
step.sdat.scale(rsurf, 'm')[0])
102-
103-
_plot_rprof_list(sdat, lovs, rprof_averaged, stepstr)
104-
105-
106-
def plot_every_step(sdat, lovs):
107-
"""Plot profiles at each time step.
108-
109-
Args:
110-
sdat (:class:`~stagpy.stagyydata.StagyyData`): a StagyyData instance.
111-
lovs (nested list of str): nested list of profile names such as
112-
the one produced by :func:`stagpy.misc.list_of_vars`.
113-
114-
Other Parameters:
115-
conf.core.snapshots: the slice of snapshots.
116-
conf.conf.timesteps: the slice of timesteps.
117-
"""
118-
sovs = misc.set_of_vars(lovs)
119-
120-
for step in sdat.walk.filter(rprofs=True):
121-
rprofs = {rvar: step.rprofs[rvar] for rvar in sovs}
122-
rcmb, rsurf = step.rprofs.bounds
123-
rprofs['bounds'] = (step.sdat.scale(rcmb, 'm')[0],
124-
step.sdat.scale(rsurf, 'm')[0])
125-
stepstr = str(step.istep)
126-
127-
_plot_rprof_list(sdat, lovs, rprofs, stepstr)
128-
129-
13090
def cmd():
13191
"""Implementation of rprof subcommand.
13292
@@ -145,6 +105,7 @@ def cmd():
145105
return
146106

147107
if conf.rprof.average:
148-
plot_average(sdat, lovs)
108+
plot_rprofs(sdat.walk.rprofs_averaged, lovs)
149109
else:
150-
plot_every_step(sdat, lovs)
110+
for step in sdat.walk.filter(rprofs=True):
111+
plot_rprofs(step.rprofs, lovs)

0 commit comments

Comments
 (0)