Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: prefer native coordinates over reconstructed arrays in vtk files #341

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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