Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

h5py: Support groupBased #301

Merged
merged 1 commit into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions openpmd_viewer/openpmd_timeseries/data_reader/data_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def read_openPMD_params(self, iteration, extract_parameters=True):
if backend == 'h5py':
filename = self.iteration_to_file[iteration]
return h5py_reader.read_openPMD_params(
filename, extract_parameters)
filename, iteration, extract_parameters)

elif backend == 'openpmd-api':
return io_reader.read_openPMD_params(
Expand Down Expand Up @@ -169,7 +169,7 @@ def read_field_cartesian( self, iteration, field, coord, axis_labels,
if backend == 'h5py':
filename = self.iteration_to_file[iteration]
return h5py_reader.read_field_cartesian(
filename, field, coord, axis_labels,
filename, iteration, field, coord, axis_labels,
slice_relative_position, slice_across )
elif backend == 'openpmd-api':
return io_reader.read_field_cartesian(
Expand Down Expand Up @@ -227,7 +227,7 @@ def read_field_circ( self, iteration, field, coord, slice_relative_position,
if backend == 'h5py':
filename = self.iteration_to_file[iteration]
return h5py_reader.read_field_circ(
filename, field, coord, slice_relative_position,
filename, iteration, field, coord, slice_relative_position,
slice_across, m, theta )
elif backend == 'openpmd-api':
return io_reader.read_field_circ(
Expand Down Expand Up @@ -256,7 +256,7 @@ def read_species_data( self, iteration, species, record_comp, extensions):
if backend == 'h5py':
filename = self.iteration_to_file[iteration]
return h5py_reader.read_species_data(
filename, species, record_comp, extensions )
filename, iteration, species, record_comp, extensions )
elif backend == 'openpmd-api':
return io_reader.read_species_data(
self.series, iteration, species, record_comp, extensions )
Expand Down Expand Up @@ -292,7 +292,7 @@ def get_grid_parameters(self, iteration, avail_fields, metadata ):
if backend == 'h5py':
filename = self.iteration_to_file[iteration]
return h5py_reader.get_grid_parameters(
filename, avail_fields, metadata )
filename, iteration, avail_fields, metadata )
elif backend == 'openpmd-api':
return io_reader.get_grid_parameters(
self.series, iteration, avail_fields, metadata )
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

import h5py
import numpy as np
from .utilities import get_shape, get_data, get_bpath, join_infile_path
from .utilities import get_shape, get_data, join_infile_path
from openpmd_viewer.openpmd_timeseries.field_metainfo import FieldMetaInformation
from openpmd_viewer.openpmd_timeseries.utilities import construct_3d_from_circ


def read_field_cartesian( filename, field, coord, axis_labels,
def read_field_cartesian( filename, iteration, field, coord, axis_labels,
slice_relative_position, slice_across ):
"""
Extract a given field from an HDF5 file in the openPMD format,
Expand All @@ -26,6 +26,9 @@ def read_field_cartesian( filename, field, coord, axis_labels,
filename : string
The absolute path to the HDF5 file

iteration : int
The iteration at which to obtain the data

field : string, optional
Which field to extract

Expand Down Expand Up @@ -64,7 +67,7 @@ def read_field_cartesian( filename, field, coord, axis_labels,
field_path = field
else:
field_path = join_infile_path( field, coord )
group, dset = find_dataset( dfile, field_path )
group, dset = find_dataset( dfile, iteration, field_path )

# Dimensions of the grid
shape = list( get_shape( dset ) )
Expand Down Expand Up @@ -115,8 +118,8 @@ def read_field_cartesian( filename, field, coord, axis_labels,
return( F, info )


def read_field_circ( filename, field, coord, slice_relative_position,
slice_across, m=0, theta=0. ):
def read_field_circ( filename, iteration, field, coord,
slice_relative_position, slice_across, m=0, theta=0. ):
"""
Extract a given field from an HDF5 file in the openPMD format,
when the geometry is thetaMode
Expand All @@ -126,6 +129,9 @@ def read_field_circ( filename, field, coord, slice_relative_position,
filename : string
The absolute path to the HDF5 file

iteration : int
The iteration at which to obtain the data

field : string, optional
Which field to extract

Expand Down Expand Up @@ -168,7 +174,7 @@ def read_field_circ( filename, field, coord, slice_relative_position,
field_path = field
else:
field_path = join_infile_path( field, coord )
group, dset = find_dataset( dfile, field_path )
group, dset = find_dataset( dfile, iteration, field_path )

# Extract the metainformation
Nm, Nr, Nz = get_shape( dset )
Expand Down Expand Up @@ -260,7 +266,7 @@ def read_field_circ( filename, field, coord, slice_relative_position,
return( F_total, info )


def find_dataset( dfile, field_path ):
def find_dataset( dfile, iteration, field_path ):
"""
Extract the dataset that corresponds to field_path,
and the corresponding group
Expand All @@ -285,7 +291,7 @@ def find_dataset( dfile, field_path ):
- an h5py.Dataset object
"""
# Find the meshes path
base_path = get_bpath( dfile )
base_path = '/data/{0}'.format( iteration )
relative_meshes_path = dfile.attrs["meshesPath"].decode()

# Get the proper dataset
Expand All @@ -301,7 +307,7 @@ def find_dataset( dfile, field_path ):
return( group, dset )


def get_grid_parameters( filename, avail_fields, metadata ):
def get_grid_parameters( filename, iteration, avail_fields, metadata ):
"""
Return the parameters of the spatial grid (grid size and grid range)
in two dictionaries
Expand All @@ -311,6 +317,9 @@ def get_grid_parameters( filename, avail_fields, metadata ):
filename : string
The absolute path to the HDF5 file

iteration : int
The iteration at which to obtain the data

avail_fields: list
A list of the available fields
e.g. ['B', 'E', 'rho']
Expand Down Expand Up @@ -342,7 +351,7 @@ def get_grid_parameters( filename, avail_fields, metadata ):
field_name = avail_fields[ index_best_field ]

# Get the corresponding field data
group, dset = find_dataset( dfile, field_name )
group, dset = find_dataset( dfile, iteration, field_name )
if metadata[field_name]['type'] == 'vector':
# For field vector, extract the first coordinate, to get the dataset
first_coord = next(iter(group.keys()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

import h5py
import numpy as np
from .utilities import is_scalar_record, get_shape, get_bpath, join_infile_path
from .utilities import is_scalar_record, get_shape, join_infile_path


def read_openPMD_params(filename, extract_parameters=True):
def read_openPMD_params(filename, iteration, extract_parameters=True):
"""
Extract the time and some openPMD parameters from a file

Expand All @@ -22,6 +22,9 @@ def read_openPMD_params(filename, extract_parameters=True):
filename: string
The path to the file from which parameters should be extracted

iteration : int
The iteration at which to obtain the data

extract_parameters: bool, optional
Whether to extract all parameters or only the time
(Function execution is faster when extract_parameters is False)
Expand All @@ -42,7 +45,7 @@ def read_openPMD_params(filename, extract_parameters=True):
"%s)" % (filename, version))

# Find the base path object, and extract the time
bpath = f[get_bpath(f)]
bpath = f['/data/{0}'.format( iteration )]
t = bpath.attrs["time"] * bpath.attrs["timeUnitSI"]

# If the user did not request more parameters, close file and exit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import h5py
import numpy as np
from scipy import constants
from .utilities import get_data, get_bpath, join_infile_path
from .utilities import get_data, join_infile_path


def read_species_data(filename, species, record_comp, extensions):
def read_species_data(filename, iteration, species, record_comp, extensions):
"""
Extract a given species' record_comp

Expand All @@ -24,6 +24,9 @@ def read_species_data(filename, species, record_comp, extensions):
filename : string
The absolute path to the HDF5 file

iteration : int
The iteration at which to obtain the data

species: string
The name of the species to extract (in the openPMD file)

Expand All @@ -50,7 +53,7 @@ def read_species_data(filename, species, record_comp, extensions):
opmd_record_comp = record_comp

# Open the HDF5 file
base_path = get_bpath(dfile)
base_path = '/data/{0}'.format( iteration )
particles_path = dfile.attrs['particlesPath'].decode()

# Extract the right dataset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,6 @@ def list_files(path_to_dir):
return iterations, iteration_to_file


def get_bpath(f):
"""
Return a string that corresponds to the base path of the data.

NB: For openPMD 1.0.0, the basePath is always of the form
'/data/%T' where %T is replaced by the actual iteration which
is present in the file.

Parameters:
-----------
f: am h5py.File object
"""
iteration = list(f['/data'].keys())[0]
return('/data/%s' % iteration)


def is_scalar_record(record):
"""
Determine whether a record is a scalar record or a vector record
Expand Down