Skip to content

Commit 1470f70

Browse files
committed
Fix discovery of output files when out-of-dir
StagyyPar.legacy_output() and .h5_output() now return absolute paths. These methods would previously return relative paths, which were not always made absolute at the call-site. This meant output files were not correctly discovered when running stagpy from a different location than the root of the run directory.
1 parent 1dcf4b8 commit 1470f70

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

stagpy/parfile.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class StagyyPar:
2323
"""A Fortran namelist file written for StagYY."""
2424

2525
nml: Namelist
26+
root: Path
2627

2728
@staticmethod
2829
def _from_file(parfile: Path) -> StagyyPar:
@@ -35,7 +36,7 @@ def _from_file(parfile: Path) -> StagyyPar:
3536
content[option] = value.strip()
3637
except AttributeError:
3738
pass
38-
return StagyyPar(nml=par)
39+
return StagyyPar(nml=par, root=parfile.parent)
3940

4041
def _update(self, par_new: StagyyPar) -> None:
4142
for section, content in par_new.nml.items():
@@ -54,15 +55,14 @@ def from_main_par(parfile: Path) -> StagyyPar:
5455
- `par_file` itself;
5556
- `parameters.dat` if it can be found in the StagYY output directories.
5657
"""
57-
root = parfile.parent
5858
par_main = StagyyPar._from_file(parfile)
5959
if "default_parameters_parfile" in par_main.nml:
6060
dfltfile = par_main.get(
6161
"default_parameters_parfile",
6262
"par_name_defaultparameters",
6363
"par_defaults",
6464
)
65-
par_dflt = StagyyPar._from_file(root / dfltfile)
65+
par_dflt = StagyyPar._from_file(par_main.root / dfltfile)
6666
par_dflt._update(par_main)
6767
par_main = par_dflt
6868

@@ -80,8 +80,8 @@ def get(self, section: str, option: str, default: T) -> T:
8080

8181
def legacy_output(self, suffix: str) -> Path:
8282
stem = self.get("ioin", "output_file_stem", "test")
83-
return Path(stem + suffix)
83+
return self.root / (stem + suffix)
8484

8585
def h5_output(self, filename: str) -> Path:
8686
h5folder = self.get("ioin", "hdf5_output_folder", "+hdf5")
87-
return Path(h5folder) / filename
87+
return self.root / h5folder / filename

stagpy/stagyydata.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ def __len__(self) -> int:
447447
length = isnap
448448
self._all_isteps_known = True
449449
if length < 0:
450-
out_stem = re.escape(Path(self.sdat.par.legacy_output("_")).name[:-1])
450+
out_stem = re.escape(self.sdat.par.legacy_output("_").name[:-1])
451451
rgx = re.compile(f"^{out_stem}_([a-zA-Z]+)([0-9]{{5}})$")
452452
fstems = set(fstem for fstem in phyvars.FIELD_FILES)
453453
for fname in self.sdat._files:
@@ -745,8 +745,7 @@ def rtimes(self) -> Optional[DataFrame]:
745745
@cached_property
746746
def _files(self) -> set[Path]:
747747
"""Set of found binary files output by StagYY."""
748-
out_stem = self.par.legacy_output("_")
749-
out_dir = self.path / out_stem.parent
748+
out_dir = self.par.legacy_output("_").parent
750749
if out_dir.is_dir():
751750
return set(out_dir.iterdir())
752751
return set()
@@ -791,10 +790,9 @@ def filename(
791790
fname += f"{timestep:05d}"
792791
fname += suffix
793792
if not force_legacy and self.hdf5:
794-
fpath = self.hdf5 / fname
793+
fpath = self.par.h5_output(fname)
795794
else:
796795
fpath = self.par.legacy_output(f"_{fname}")
797-
fpath = self.path / fpath
798796
return fpath
799797

800798
def _binfiles_set(self, isnap: int) -> set[Path]:

0 commit comments

Comments
 (0)