Skip to content

Commit

Permalink
Merge pull request #341 from neutrinoceros/vtk/enh/prefer_native_coor…
Browse files Browse the repository at this point in the history
…dinates_if_available

ENH: prefer native coordinates over reconstructed arrays in vtk files
  • Loading branch information
neutrinoceros authored Nov 26, 2024
2 parents c156566 + 2dc72ea commit 26e7367
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/yt_idefix/_io/vtk_io.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import re
import struct
import warnings
from typing import Any, BinaryIO, Literal, overload
Expand Down Expand Up @@ -75,6 +76,9 @@ def parse_shape(s: str, md: dict[str, Any]) -> None:
md["shape"] = read_shape(s)


NATIVE_COORDINATE_REGEXP = re.compile(r"X(1|2|3)(L|C)_NATIVE_COORDINATES")


# this may not be kept in the following form
def read_metadata(fh: BinaryIO) -> dict[str, Any]:
fh.seek(0)
Expand Down Expand Up @@ -108,6 +112,12 @@ def read_metadata(fh: BinaryIO) -> dict[str, Any]:
metadata["periodicity"] = tuple(
np.fromfile(fh, dtype=">i4", count=3).astype(bool)
)
elif NATIVE_COORDINATE_REGEXP.match(d):
entry, _ncomp, native_dim, _dtype = d.split()
metadata.setdefault("native_coordinates", {})
metadata["native_coordinates"][entry] = np.fromfile(
fh, dtype=">f", count=int(native_dim)
)
else:
warnings.warn(f"Found unknown field {d!r}", stacklevel=2)
next(fh) # skip extra linefeed (empty line)
Expand Down Expand Up @@ -221,12 +231,21 @@ def warn_invalid(arr):
)
return arr

return Coordinates(
warn_invalid(coords[0]),
warn_invalid(coords[1]),
warn_invalid(coords[2]),
array_shape,
)
if "native_coordinates" in md:
nc = md["native_coordinates"]
return Coordinates(
nc["X1C_NATIVE_COORDINATES"],
nc["X2C_NATIVE_COORDINATES"],
nc["X3C_NATIVE_COORDINATES"],
array_shape,
)
else:
return Coordinates(
warn_invalid(coords[0]),
warn_invalid(coords[1]),
warn_invalid(coords[2]),
array_shape,
)


def read_field_offset_index(
Expand Down

0 comments on commit 26e7367

Please sign in to comment.