|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import re |
3 | 4 | import typing |
| 5 | +from abc import ABC, abstractmethod |
4 | 6 | from collections import deque |
5 | 7 | from dataclasses import dataclass |
6 | 8 | from functools import cached_property |
7 | 9 |
|
| 10 | +from . import phyvars, stagyyparsers |
| 11 | + |
8 | 12 | if typing.TYPE_CHECKING: |
| 13 | + from typing import Mapping |
| 14 | + |
9 | 15 | from .datatypes import Field |
| 16 | + from .stagyydata import StagyyData |
10 | 17 |
|
11 | 18 |
|
12 | 19 | @dataclass(frozen=True) |
@@ -57,3 +64,119 @@ def evict_istep(self, istep: int) -> None: |
57 | 64 | self._stack.clear() |
58 | 65 | self._stack.extend(to_keep) |
59 | 66 | assert len(self._stack) == len(self._data) |
| 67 | + |
| 68 | + |
| 69 | +class StepSnap(ABC): |
| 70 | + """Keep track of the step/snap correspondence.""" |
| 71 | + |
| 72 | + @abstractmethod |
| 73 | + def istep(self, *, isnap: int) -> int | None: ... |
| 74 | + |
| 75 | + @abstractmethod |
| 76 | + def isnap(self, *, istep: int) -> int | None: ... |
| 77 | + |
| 78 | + @abstractmethod |
| 79 | + def len_snap(self) -> int: ... |
| 80 | + |
| 81 | + |
| 82 | +@dataclass(frozen=True) |
| 83 | +class StepSnapInfo: |
| 84 | + step_to_snap: Mapping[int, int] |
| 85 | + snap_to_step: Mapping[int, int] |
| 86 | + isnap_max: int |
| 87 | + |
| 88 | + |
| 89 | +@dataclass(frozen=True) |
| 90 | +class StepSnapH5(StepSnap): |
| 91 | + sdat: StagyyData |
| 92 | + |
| 93 | + @cached_property |
| 94 | + def _info(self) -> StepSnapInfo: |
| 95 | + assert self.sdat.hdf5 is not None |
| 96 | + isnap = -1 |
| 97 | + step_to_snap = {} |
| 98 | + snap_to_step = {} |
| 99 | + for isnap, istep in stagyyparsers.read_time_h5(self.sdat.hdf5): |
| 100 | + step_to_snap[istep] = isnap |
| 101 | + snap_to_step[isnap] = istep |
| 102 | + return StepSnapInfo( |
| 103 | + step_to_snap=step_to_snap, |
| 104 | + snap_to_step=snap_to_step, |
| 105 | + isnap_max=isnap, |
| 106 | + ) |
| 107 | + |
| 108 | + def istep(self, *, isnap: int) -> int | None: |
| 109 | + return self._info.snap_to_step.get(isnap) |
| 110 | + |
| 111 | + def isnap(self, *, istep: int) -> int | None: |
| 112 | + return self._info.step_to_snap.get(istep) |
| 113 | + |
| 114 | + def len_snap(self) -> int: |
| 115 | + return self._info.isnap_max + 1 |
| 116 | + |
| 117 | + |
| 118 | +@dataclass(frozen=True) |
| 119 | +class StepSnapLegacy(StepSnap): |
| 120 | + sdat: StagyyData |
| 121 | + |
| 122 | + @cached_property |
| 123 | + def _step_to_snap(self) -> dict[int, int | None]: |
| 124 | + return {} |
| 125 | + |
| 126 | + @cached_property |
| 127 | + def _snap_to_step(self) -> dict[int, int | None]: |
| 128 | + return {} |
| 129 | + |
| 130 | + @cached_property |
| 131 | + def isnap_max(self) -> int: |
| 132 | + imax = -1 |
| 133 | + out_stem = re.escape(self.sdat.par.legacy_output("_").name[:-1]) |
| 134 | + rgx = re.compile(f"^{out_stem}_([a-zA-Z]+)([0-9]{{5}})$") |
| 135 | + fstems = set(fstem for fstem in phyvars.FIELD_FILES) |
| 136 | + for fname in self.sdat._files: |
| 137 | + match = rgx.match(fname.name) |
| 138 | + if match is not None and match.group(1) in fstems: |
| 139 | + imax = max(int(match.group(2)), imax) |
| 140 | + return imax |
| 141 | + |
| 142 | + def len_snap(self) -> int: |
| 143 | + return self.isnap_max + 1 |
| 144 | + |
| 145 | + def istep(self, *, isnap: int) -> int | None: |
| 146 | + if isnap < 0 or isnap > self.isnap_max: |
| 147 | + return None |
| 148 | + istep = self._snap_to_step.get(isnap, -1) |
| 149 | + if istep == -1: |
| 150 | + binfiles = self.sdat._binfiles_set(isnap) |
| 151 | + if binfiles: |
| 152 | + istep = stagyyparsers.field_istep(binfiles.pop()) |
| 153 | + else: |
| 154 | + istep = None |
| 155 | + self._snap_to_step[isnap] = istep |
| 156 | + if istep is not None: |
| 157 | + self._step_to_snap[istep] = isnap |
| 158 | + return istep |
| 159 | + |
| 160 | + def isnap(self, *, istep: int) -> int | None: |
| 161 | + if istep < 0: |
| 162 | + return None |
| 163 | + isnap = self._step_to_snap.get(istep, -1) |
| 164 | + if isnap == -1: |
| 165 | + istep_try = None |
| 166 | + # might be more efficient to do 0 and -1 then bisection, even if |
| 167 | + # that means losing intermediate information |
| 168 | + while (istep_try is None or istep_try < istep) and isnap < 99999: |
| 169 | + isnap += 1 |
| 170 | + try: |
| 171 | + istep_try = self.sdat.snaps[isnap].istep |
| 172 | + except KeyError: |
| 173 | + pass |
| 174 | + # all intermediate istep could have their isnap to None |
| 175 | + self._snap_to_step[isnap] = istep_try |
| 176 | + if istep_try is not None: |
| 177 | + self._step_to_snap[istep_try] = isnap |
| 178 | + |
| 179 | + if istep_try != istep: |
| 180 | + self._step_to_snap[istep] = None |
| 181 | + |
| 182 | + return self._step_to_snap[istep] |
0 commit comments