Skip to content

Commit f812ed4

Browse files
committed
New Step.time property
It offers a more robust way of accesing the time of a snapshot if time series data are missing.
1 parent 18ac7e5 commit f812ed4

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

stagpy/_step.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,22 @@ def geom(self):
519519
@property
520520
def timeinfo(self):
521521
"""Time series data of the time step."""
522-
return self.sdat.tseries.at_step(self.istep)
522+
try:
523+
info = self.sdat.tseries.at_step(self.istep)
524+
except KeyError:
525+
raise error.MissingDataError(f'No time series for {self!r}')
526+
return info
527+
528+
@property
529+
def time(self):
530+
"""Time of this time step."""
531+
steptime = None
532+
try:
533+
steptime = self.timeinfo['t']
534+
except error.MissingDataError:
535+
if self.isnap is not None:
536+
steptime = self.geom.ti_ad
537+
return steptime
523538

524539
@property
525540
def isnap(self):

stagpy/stagyydata.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,20 +487,17 @@ def at_time(self, time, after=False):
487487
Returns:
488488
:class:`~stagpy._step.Step`: the relevant snap.
489489
"""
490-
if self.sdat.tseries is None:
491-
return None
492-
493490
# in theory, this could be a valid implementation of _Steps.at_time
494491
# but this isn't safe against missing data...
495492
igm = 0
496493
igp = len(self) - 1
497494
while igp - igm > 1:
498495
istart = igm + (igp - igm) // 2
499-
if self[istart].timeinfo['t'] >= time:
496+
if self[istart].time >= time:
500497
igp = istart
501498
else:
502499
igm = istart
503-
if self[igp].timeinfo['t'] > time and not after and igp > 0:
500+
if self[igp].time > time and not after and igp > 0:
504501
igp -= 1
505502
return self[igp]
506503

0 commit comments

Comments
 (0)