Skip to content

Commit e2eaa15

Browse files
authored
Change openPMD reader defaults (#4)
* Make `species_name` optional in openPMD reader * Increase version number * Add method to list species in openPMD file * Add method to list species in openpmd file
1 parent 291dda9 commit e2eaa15

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

aptools/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .data_analysis import beam_diagnostics
22

3-
__version__ = "0.1.24"
3+
__version__ = "0.1.25"
44
__all__ = ['beam_diagnostics', '__version__']

aptools/data_handling/reading.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def read_astra_data(file_path, remove_non_standard=True):
136136
return x, y, z, px, py, pz, q
137137

138138

139-
def read_openpmd_beam(file_path, species_name):
139+
def read_openpmd_beam(file_path, species_name=None):
140140
"""Reads particle data from a h5 file following the openPMD standard and
141141
returns it in the unis used by APtools.
142142
@@ -145,8 +145,9 @@ def read_openpmd_beam(file_path, species_name):
145145
file_path : str
146146
Path to the file with particle data
147147
148-
species_name : str
149-
Name of the particle species
148+
species_name : str, Optional
149+
Name of the particle species. Optional if only one particle species
150+
is available in the openpmd file.
150151
151152
Returns
152153
-------
@@ -159,6 +160,17 @@ def read_openpmd_beam(file_path, species_name):
159160
base_path = '/data/{}'.format(iteration)
160161
# get path under which particle data is stored
161162
particles_path = file_content.attrs['particlesPath'].decode()
163+
# list available species
164+
available_species = list(
165+
file_content[join_infile_path(base_path, particles_path)])
166+
if species_name is None:
167+
if len(available_species) == 1:
168+
species_name = available_species[0]
169+
else:
170+
raise ValueError(
171+
'More than one particle species is available. '
172+
'Please specify a `species_name`. '
173+
'Available species are: ' + str(available_species))
162174
# get species
163175
beam_species = file_content[
164176
join_infile_path(base_path, particles_path, species_name)]

aptools/data_handling/utilities.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
""" Defines utilities for the data handling modules """
2+
import openpmd_api as io
3+
4+
5+
def get_available_species(file_path):
6+
"""Get a list of available species in an openPMD file.
7+
8+
Parameters
9+
----------
10+
file_path : str
11+
Path of the openPMD file.
12+
13+
Returns
14+
-------
15+
list
16+
A list of strings with the names of the available species.
17+
"""
18+
series = io.Series(file_path, io.Access.read_only)
19+
i = list(series.iterations)[0]
20+
iteration = series.iterations[i]
21+
22+
return list(iteration.particles)

0 commit comments

Comments
 (0)