Skip to content

Commit

Permalink
ENH: prefer native coordinates over reconstructed arrays in vtk files
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Nov 6, 2024
1 parent 8af9783 commit 50a692f
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions nonos/_readers/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

@final
class VTKReader(ReaderMixin):
NATIVE_COORDINATE_REGEXP = re.compile(r"X(1|2|3)(L|C)_NATIVE_COORDINATES")

@staticmethod
def parse_output_number_and_filename(
file_or_number: Union[PathT, int],
Expand Down Expand Up @@ -81,6 +83,8 @@ def read(file, /, **meta) -> BinData:
if meta["geometry"] is not None:
V["geometry"] = Geometry(meta["geometry"])

native_coordinates = {}

# datatype we read
dt = np.dtype(">f") # Big endian single precision floats
dint = np.dtype(">i4") # Big endian integer
Expand Down Expand Up @@ -136,6 +140,11 @@ def read(file, /, **meta) -> BinData:
elif entry == "PERIODICITY":
# skip
fid.seek(dint.itemsize * 3, os.SEEK_CUR)
elif VTKReader.NATIVE_COORDINATE_REGEXP.match(entry):
native_name, _ncomp, native_dim, _dtype = entry.split()
native_coordinates[native_name] = np.fromfile(
fid, dtype=dt, count=int(native_dim)
)
else: # pragma: no cover
fid.close()
raise ValueError(f"Received unknown field: {entry!r}")
Expand Down Expand Up @@ -470,6 +479,35 @@ def read(file, /, **meta) -> BinData:
fid.readline() # extra line feed
fid.close()

geom: Geometry = V["geometry"]
if geom is Geometry.SPHERICAL:
native2attr = {
"X1L_NATIVE_COORDINATES": "rl",
"X1C_NATIVE_COORDINATES": "r",
"X2L_NATIVE_COORDINATES": "thetal",
"X2C_NATIVE_COORDINATES": "theta",
"X3L_NATIVE_COORDINATES": "phil",
"X3C_NATIVE_COORDINATES": "phi",
}
elif (
geom is Geometry.CARTESIAN
or geom is Geometry.CYLINDRICAL
or geom is Geometry.POLAR
):
native2attr = {
"X1L_NATIVE_COORDINATES": "xl",
"X1C_NATIVE_COORDINATES": "x",
"X2L_NATIVE_COORDINATES": "yl",
"X2C_NATIVE_COORDINATES": "y",
"X3L_NATIVE_COORDINATES": "zl",
"X3C_NATIVE_COORDINATES": "z",
}
else:
assert_never(geom)

for native_field, attr in native2attr.items():
V[attr][native_field] = native_coordinates[native_field]

return BinData(**V)


Expand Down

0 comments on commit 50a692f

Please sign in to comment.