Skip to content

Commit 167e01b

Browse files
committed
rprof parsers use names in output files
1 parent 734eb8a commit 167e01b

3 files changed

Lines changed: 17 additions & 29 deletions

File tree

src/stagpy/stagyydata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,14 +732,14 @@ def par(self) -> StagyyPar:
732732
@cached_property
733733
def _rprof_and_times(self) -> tuple[dict[int, DataFrame], DataFrame | None]:
734734
rproffile = self.filename("rprof.h5")
735-
data = stagyyparsers.rprof_h5(rproffile, list(phyvars.RPROF.keys()))
735+
data = stagyyparsers.rprof_h5(rproffile)
736736
if data[1] is not None:
737737
return data
738738
rproffile = self.filename("rprof.dat")
739739
if self.hdf5 and not rproffile.is_file():
740740
# check legacy folder as well
741741
rproffile = self.filename("rprof.dat", force_legacy=True)
742-
return stagyyparsers.rprof(rproffile, list(phyvars.RPROF.keys()))
742+
return stagyyparsers.rprof(rproffile)
743743

744744
@property
745745
def rtimes(self) -> DataFrame | None:

src/stagpy/stagyyparsers.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import pandas as pd
2121

2222
from .error import ParsingError
23-
from .phyvars import FIELD_FILES_H5, SFIELD_FILES_H5
23+
from .phyvars import FIELD_FILES_H5, RPROF, SFIELD_FILES_H5
2424
from .xdmf import XmlStream
2525

2626
if typing.TYPE_CHECKING:
@@ -165,25 +165,24 @@ def _extract_rsnap_isteps(
165165
return isteps
166166

167167

168-
def rprof(
169-
rproffile: Path, colnames: list[str]
170-
) -> tuple[dict[int, DataFrame], DataFrame | None]:
168+
def rprof(rproffile: Path) -> tuple[dict[int, DataFrame], DataFrame | None]:
171169
"""Extract radial profiles data.
172170
173-
If `colnames` is too long, it will be truncated. If it is too short,
174-
additional numeric column names from 0 to N-1 will be attributed to the N
175-
extra columns present in `timefile`.
176-
177171
Args:
178172
rproffile: path of the rprof.dat file.
179-
colnames: names of the variables expected in `rproffile`.
180173
181174
Returns:
182175
profs: a dict mapping istep to radial profiles.
183176
times: the time indexed by time steps.
184177
"""
185178
if not rproffile.is_file():
186179
return {}, None
180+
181+
with rproffile.open() as fid:
182+
colnames = fid.readline().strip().split()
183+
if not colnames:
184+
colnames = list(RPROF.keys())
185+
187186
data = pd.read_csv(
188187
rproffile,
189188
sep=r"\s+",
@@ -213,17 +212,11 @@ def rprof(
213212
return all_data, df_times
214213

215214

216-
def rprof_h5(
217-
rproffile: Path, colnames: list[str]
218-
) -> tuple[dict[int, DataFrame], DataFrame | None]:
215+
def rprof_h5(rproffile: Path) -> tuple[dict[int, DataFrame], DataFrame | None]:
219216
"""Extract radial profiles data.
220217
221-
If `colnames` is too long, it will be truncated. If it is too short,
222-
additional column names will be deduced from the content of the file.
223-
224218
Args:
225219
rproffile: path of the rprof.h5 file.
226-
colnames: names of the variables expected in `rproffile`.
227220
228221
Returns:
229222
profs: a dict mapping istep to radial profiles.
@@ -235,13 +228,13 @@ def rprof_h5(
235228
data = {}
236229
with h5py.File(rproffile, "r") as h5f:
237230
dnames = sorted(dname for dname in h5f.keys() if dname.startswith("rprof_"))
238-
h5names = h5f["names"].asstr()[len(colnames) :]
231+
colnames = h5f["names"].asstr()[()]
239232
for dname in dnames:
240233
dset = h5f[dname]
241234
arr = dset[()]
242235
istep = dset.attrs["istep"]
243236
step_cols = list(colnames)
244-
_tidy_names(step_cols, arr.shape[1], h5names) # check shape
237+
_tidy_names(step_cols, arr.shape[1]) # check shape
245238
data[istep] = pd.DataFrame(arr, columns=step_cols)
246239
isteps.append((istep, dset.attrs["time"]))
247240

tests/test_parsers.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,20 @@ def test_time_series_invalid_prs() -> None:
2525

2626
def test_rprof_prs(sdat_legacy: StagyyData) -> None:
2727
sdat = sdat_legacy
28-
names = ["aa", "bb", "cc"]
29-
data, time = prs.rprof(sdat.filename("rprof.dat"), list(names))
30-
assert all((df.columns[:3] == names).all() for df in data.values())
31-
assert all(
32-
(df.columns[3:] == list(map(str, range(df.shape[1] - 3)))).all()
33-
for df in data.values()
34-
)
28+
data, time = prs.rprof(sdat.filename("rprof.dat"))
29+
assert all((df.columns[:3] == ["r", "Tmean", "Tmin"]).all() for df in data.values())
3530

3631

3732
def test_rprof_h5(sdat_h5: StagyyData) -> None:
3833
sdat = sdat_h5
3934
assert sdat.hdf5 is not None
40-
data, _times = prs.rprof_h5(sdat.hdf5 / "rprof.h5", colnames=[])
35+
data, _times = prs.rprof_h5(sdat.hdf5 / "rprof.h5")
4136
assert data is not None
4237
assert (data[1000].columns[:3] == ["r", "Tmean", "Tmin"]).all()
4338

4439

4540
def test_rprof_invalid_prs() -> None:
46-
assert prs.rprof(Path("dummy"), []) == ({}, None)
41+
assert prs.rprof(Path("dummy")) == ({}, None)
4742

4843

4944
def test_fields_prs(sdat_legacy: StagyyData) -> None:

0 commit comments

Comments
 (0)