Skip to content

Commit

Permalink
Merge pull request #4884 from matthewturk/tipsy_aux_fh
Browse files Browse the repository at this point in the history
BUG: Fix tipsy file handler opening
  • Loading branch information
chrishavlin authored Jun 7, 2024
2 parents 748014d + d49a5a7 commit 13dc4fb
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions yt/frontends/tipsy/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,12 @@ def _read_particle_data_file(self, data_file, ptf, selector=None):
f = open(data_file.filename, "rb")

# we need to open all aux files for chunking to work
aux_fh = {}
for afield in self._aux_fields:
aux_fh[afield] = open(data_file.filename + "." + afield, "rb")
_aux_fh = {}

def aux_fh(afield):
if afield not in _aux_fh:
_aux_fh[afield] = open(data_file.filename + "." + afield, "rb")
return _aux_fh[afield]

for ptype, field_list in sorted(ptf.items(), key=lambda a: poff.get(a[0], -1)):
if data_file.total_particles[ptype] == 0:
Expand All @@ -170,19 +173,19 @@ def _read_particle_data_file(self, data_file, ptf, selector=None):
p = np.fromfile(f, self._pdtypes[ptype], count=count)
auxdata = []
for afield in afields:
aux_fh[afield].seek(aux_fields_offsets[afield][ptype])
aux_fh(afield).seek(aux_fields_offsets[afield][ptype])
if isinstance(self._aux_pdtypes[afield], np.dtype):
auxdata.append(
np.fromfile(
aux_fh[afield], self._aux_pdtypes[afield], count=count
aux_fh(afield), self._aux_pdtypes[afield], count=count
)
)
else:
aux_fh[afield].seek(0)
aux_fh(afield).seek(0)
sh = aux_fields_offsets[afield][ptype]
if tp[ptype] > 0:
aux = np.genfromtxt(
aux_fh[afield], skip_header=sh, max_rows=count
aux_fh(afield), skip_header=sh, max_rows=count
)
if aux.ndim < 1:
aux = np.array([aux])
Expand All @@ -209,7 +212,7 @@ def _read_particle_data_file(self, data_file, ptf, selector=None):

# close all file handles
f.close()
for fh in list(aux_fh.values()):
for fh in _aux_fh.values():
fh.close()

return return_data
Expand Down

0 comments on commit 13dc4fb

Please sign in to comment.