Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 15 additions & 25 deletions exptool/io/psp_to_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

args = parser.parse_args()

HDFConverter.convert_psp_to_hdf5(args.filename)
HDFConverter(args.filename)



Expand Down Expand Up @@ -89,32 +89,23 @@ def __init__(self, filename, comp=None, verbose=0):
# Start the conversion process
self.convert_psp_to_hdf5(self.filename, comp=self.comp, verbose=self.verbose)

@staticmethod
def convert_psp_to_hdf5(inputfilename, comp=None, verbose=0):
def convert_psp_to_hdf5(self):
"""
Convert a PSP input file to HDF5 format.

Args:
inputfilename (str): The name of the PSP input file to be converted.
comp (str, optional): The specific component to convert. If provided, only the data for the specified component will be converted.
verbose (int, optional): Verbosity level for printing progress and messages during conversion.
Uses the filename stored in self.filename during initialization.

"""
# Define the output file name
outputfilename = inputfilename + '.h5'
outputfilename = self.filename + '.h5'

if verbose > 0:
print(f"Converting {inputfilename} to {outputfilename}")

# Open the PSP input file and extract components
O = particle.Input(inputfilename)

# Determine which components to convert
if comp is not None:
comps = [comp] if comp in O.header.keys() else []
if not comps and verbose > 0:
print(f"Warning: Component '{comp}' not found in file")
else:
comps = list(O.header.keys())
O = particle.Input(self.filename)
comps = list(O.header.keys())


# Create a new HDF5 file for storing the converted data
f = h5py.File(outputfilename, 'w')
Expand All @@ -130,19 +121,18 @@ def convert_psp_to_hdf5(inputfilename, comp=None, verbose=0):
f.create_group(comp)

# Print the header information for the component
HDFConverter.print_component_header(f, O, comp)
self.print_component_header(f, O, comp)

# Create and store the phase space data for the component
HDFConverter.make_phasespace(f, inputfilename, comp)
self.make_phasespace(f, comp)

# Close the HDF5 file
f.close()

if verbose > 0:
print(f"Conversion complete: {outputfilename}")

@staticmethod
def print_component_header(f, O, comp):
def print_component_header(self, f, O, comp):
"""
Print header information for a component to an HDF5 file.

Expand Down Expand Up @@ -178,18 +168,18 @@ def print_component_header(f, O, comp):
# Create attributes for the top-level header
f['{}/header'.format(comp)].attrs.create(key, O.header[comp][key])

@staticmethod
def make_phasespace(f, inputfilename, comp):
def make_phasespace(self, f, comp):
"""
Convert and store phase space data for a component in an HDF5 file.

Args:
f (h5py.Group): The HDF5 group to store the phase space data.
inputfilename (str): The name of the PSP input file.
comp (str): The name of the component.

Uses the filename stored in self.filename during initialization.
"""
# Read data from the PSP input file
O1 = particle.Input(inputfilename, comp)
O1 = particle.Input(self.filename, comp)

# Create a phase space array
PS = np.array([O1.data['m'], O1.data['x'], O1.data['y'], O1.data['z'], O1.data['vx'], O1.data['vy'], O1.data['vz'], O1.data['potE']]).T
Expand Down