Skip to content

Commit

Permalink
Merge pull request #279 from sjgrimm/dev2
Browse files Browse the repository at this point in the history
various smilei reader bugfixes
  • Loading branch information
skuschel committed Apr 14, 2024
2 parents 3eb08c6 + 8545d4c commit bdbafd6
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions postpic/datareader/smileih5.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,19 @@ def _generateh5indexfile(indexfile, fnames):
# indexfile already exists. do not recreate
return

dirname = os.path.dirname(fnames[0])
indexfile = os.path.join(dirname, indexfile)

def visitf(key):
# key is a string
# only link if key points to a dataset. Do not link groups
if isinstance(hf[key], h5py._hl.dataset.Dataset):
if key.endswith('latest_IDs'):
return
# only link if key points to a dataset. Generally do not link groups.
# However single scalars (identified by ´value in hf[key].attrs´)
# maybe a group and must be linked as well.
if isinstance(hf[key], h5py._hl.dataset.Dataset) or "value" in hf[key].attrs:
ih[key] = h5py.ExternalLink(fname, key)
elif isinstance(hf[key], h5py._hl.group.Group) and key not in ih:
ih.create_group(key)
for attr in hf[key].attrs:
ih[key].attrs[attr] = hf[key].attrs[attr]

with h5py.File(indexfile, 'w') as ih:
for fname in fnames:
Expand Down Expand Up @@ -306,6 +311,36 @@ def _simgridkeys(self):
'Bx', 'By', 'Bz', 'Br', 'Bl', 'Bt',
'Jx', 'Jy', 'Jz', 'Jr', 'Jl', 'Jt', 'Rho']

def getSpecies(self, species, attrib):
"""
Returns one of the attributes out of (x,y,z,px,py,pz,weight,ID,mass,charge) of
this particle species.
"""
attribid = helper.attribidentify[attrib]
options = {9: 'particles/{}/weight',
0: 'particles/{}/position/x',
1: 'particles/{}/position/y',
2: 'particles/{}/position/z',
3: 'particles/{}/momentum/x',
4: 'particles/{}/momentum/y',
5: 'particles/{}/momentum/z',
10: 'particles/{}/id',
11: 'particles/{}/mass',
12: 'particles/{}/charge'}
optionsoffset = {0: 'particles/{}/positionOffset/x',
1: 'particles/{}/positionOffset/y',
2: 'particles/{}/positionOffset/z'}
key = options[attribid]
offsetkey = optionsoffset.get(attribid)
try:
data = self.data(key.format(species))
if offsetkey is not None:
data += self.data(offsetkey.format(species))
ret = np.asarray(data, dtype=np.float64)
except IndexError:
raise KeyError
return ret

def getderived(self):
'''
return all other fields dumped, except E and B.
Expand Down Expand Up @@ -346,7 +381,7 @@ def __init__(self, h5file, dumpreadercls=SmileiReader, **kwargs):
indexfile = _getindexfile(h5file)
self._h5 = h5py.File(indexfile, 'r')
with h5py.File(indexfile, 'r') as h5:
self._dumpkeys = list(h5['data'].keys())
self._dumpkeys = [int(i) for i in h5['data'].keys()]
else:
raise IOError('{} does not exist.'.format(h5file))

Expand Down

0 comments on commit bdbafd6

Please sign in to comment.