Skip to content

Commit

Permalink
backporting yt-project#2120
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Goldbaum committed Feb 22, 2019
1 parent 29eeb37 commit 0d0dab0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 9 additions & 4 deletions yt/frontends/gadget/io.py
Expand Up @@ -270,8 +270,8 @@ def _read_particle_coords(self, chunks, ptf):
for ptype in ptf:
# This is where we could implement sub-chunking
f.seek(poff[ptype, "Coordinates"], os.SEEK_SET)
pos = self._read_field_from_file(f,
tp[ptype], "Coordinates")
pos = self._read_field_from_file(
f, tp[ptype], "Coordinates")
yield ptype, (pos[:, 0], pos[:, 1], pos[:, 2])
f.close()

Expand All @@ -286,8 +286,8 @@ def _read_particle_fields(self, chunks, ptf, selector):
f = open(data_file.filename, "rb")
for ptype, field_list in sorted(ptf.items()):
f.seek(poff[ptype, "Coordinates"], os.SEEK_SET)
pos = self._read_field_from_file(f,
tp[ptype], "Coordinates")
pos = self._read_field_from_file(
f, tp[ptype], "Coordinates")
mask = selector.select_points(
pos[:, 0], pos[:, 1], pos[:, 2], 0.0)
del pos
Expand All @@ -314,9 +314,14 @@ def _read_field_from_file(self, f, count, name):
dt = self._endian + "u4"
else:
dt = self._endian + self._float_type
dt = np.dtype(dt)
if name in self._vector_fields:
count *= self._vector_fields[name]
arr = np.fromfile(f, dtype=dt, count=count)
# ensure data are in native endianness to avoid errors
# when field data are passed to cython
dt = dt.newbyteorder('N')
arr = arr.astype(dt)
if name in self._vector_fields:
factor = self._vector_fields[name]
arr = arr.reshape((count // factor, factor), order="C")
Expand Down
6 changes: 6 additions & 0 deletions yt/frontends/gadget/tests/test_outputs.py
Expand Up @@ -98,3 +98,9 @@ def test_pid_uniqueness():
ad = ds.all_data()
pid = ad['ParticleIDs']
assert len(pid) == len(set(pid.v))

@requires_ds(BE_Gadget)
def test_bigendian_field_access():
ds = data_dir_load(BE_Gadget)
data = ds.all_data()
data['Halo', 'Velocities']

0 comments on commit 0d0dab0

Please sign in to comment.