Skip to content

Commit a301f6f

Browse files
committed
Introduce XmfEntry
Lay some ground work to avoid keeping the full ElementTree in memory.
1 parent ae066f3 commit a301f6f

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

stagpy/stagyyparsers.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,17 @@
2525

2626
if typing.TYPE_CHECKING:
2727
from pathlib import Path
28-
from typing import Any, BinaryIO, Callable, Dict, Iterator, List, Optional, Tuple
28+
from typing import (
29+
Any,
30+
BinaryIO,
31+
Callable,
32+
Dict,
33+
Iterator,
34+
List,
35+
Mapping,
36+
Optional,
37+
Tuple,
38+
)
2939
from xml.etree.ElementTree import Element
3040

3141
from numpy import ndarray
@@ -855,6 +865,13 @@ def _maybe_get(
855865
return maybe_info
856866

857867

868+
@dataclass(frozen=True)
869+
class XmfEntry:
870+
time: Optional[float]
871+
mo_lambda: Optional[float]
872+
mo_thick_sol: Optional[float]
873+
874+
858875
@dataclass(frozen=True)
859876
class FieldXmf:
860877
path: Path
@@ -863,6 +880,29 @@ class FieldXmf:
863880
def _root(self) -> Element:
864881
return xmlET.parse(str(self.path)).getroot()
865882

883+
@cached_property
884+
def _data(self) -> Mapping[int, XmfEntry]:
885+
# Geometry stuff from surface field is not useful
886+
data = {}
887+
# FIXME: get isnap from a field name
888+
for isnap, snap in enumerate(self._root[0][0]):
889+
time = _maybe_get(snap, "Time", "Value", float)
890+
mo_lambda = _maybe_get(snap, "mo_lambda", "Value", float)
891+
mo_thick_sol = _maybe_get(snap, "mo_thick_sol", "Value", float)
892+
893+
data[isnap] = XmfEntry(
894+
time=time,
895+
mo_lambda=mo_lambda,
896+
mo_thick_sol=mo_thick_sol,
897+
)
898+
return data
899+
900+
def __getitem__(self, isnap: int) -> XmfEntry:
901+
try:
902+
return self._data[isnap]
903+
except KeyError:
904+
raise ParsingError(self.path, f"no data for snapshot {isnap}")
905+
866906
def get_snap(self, isnap: int) -> Element:
867907
# Domain, Temporal Collection, Snapshot
868908
# should check that this is indeed the required snapshot
@@ -884,9 +924,10 @@ def read_geom_h5(xdmf: FieldXmf, snapshot: int) -> dict[str, Any]:
884924
header: Dict[str, Any] = {}
885925

886926
elt_snap = xdmf.get_snap(snapshot)
887-
header["ti_ad"] = _maybe_get(elt_snap, "Time", "Value", float)
888-
header["mo_lambda"] = _maybe_get(elt_snap, "mo_lambda", "Value", float)
889-
header["mo_thick_sol"] = _maybe_get(elt_snap, "mo_thick_sol", "Value", float)
927+
entry = xdmf[snapshot]
928+
header["ti_ad"] = entry.time
929+
header["mo_lambda"] = entry.mo_lambda
930+
header["mo_thick_sol"] = entry.mo_thick_sol
890931
header["ntb"] = 1
891932
coord_h5 = [] # all the coordinate files
892933
coord_shape = [] # shape of meshes

0 commit comments

Comments
 (0)