Skip to content

Commit

Permalink
Merge pull request #346 from ax3l/support-group-based-singlefile
Browse files Browse the repository at this point in the history
Support groupBased Encoding (Single File Reads)
  • Loading branch information
RemiLehe authored Aug 29, 2022
2 parents 542e113 + c70201e commit bffb850
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
26 changes: 18 additions & 8 deletions openpmd_viewer/openpmd_timeseries/data_reader/data_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,33 @@ def list_iterations(self, path_to_dir):
elif self.backend == 'openpmd-api':
# guess file ending from first file in directory
first_file_name = None
for file_name in os.listdir( path_to_dir ):
if file_name.split(os.extsep)[-1] in io.file_extensions:
first_file_name = file_name

is_single_file = os.path.isfile(path_to_dir)
if is_single_file:
first_file_name = path_to_dir
else:
for file_name in os.listdir( path_to_dir ):
if file_name.split(os.extsep)[-1] in io.file_extensions:
first_file_name = file_name
if first_file_name is None:
raise RuntimeError(
"Found no valid files in directory {0}.\n"
"Please check that this is the path to the openPMD files."
"(valid files must have one of the following extensions: {1})"
.format(path_to_dir, io.file_extensions))

# match last occurance of integers and replace with %T wildcards
# examples: data00000100.h5 diag4_00000500.h5 io12.0.bp
# te42st.1234.yolo.json scan7_run14_data123.h5
file_path = re.sub(r'(\d+)(\.(?!\d).+$)', r'%T\2', first_file_name)
if is_single_file:
file_path = path_to_dir
series_name = file_path
else:
# match last occurance of integers and replace with %T wildcards
# examples: data00000100.h5 diag4_00000500.h5 io12.0.bp
# te42st.1234.yolo.json scan7_run14_data123.h5
file_path = re.sub(r'(\d+)(\.(?!\d).+$)', r'%T\2', first_file_name)
series_name = os.path.join( path_to_dir, file_path)

self.series = io.Series(
os.path.join( path_to_dir, file_path),
series_name,
io.Access.read_only )
iterations = np.array( self.series.iterations )

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,26 @@ def list_files(path_to_dir):
- an array of integers which correspond to the iteration of each file
- a dictionary that matches iterations to the corresponding filename
"""
# Find all the files in the provided directory
all_files = os.listdir(path_to_dir)
# group based encoding?
is_single_file = os.path.isfile(path_to_dir)

if is_single_file:
all_files = [path_to_dir]
else:
# Find all the files in the provided directory
all_files = os.listdir(path_to_dir)

# Select the hdf5 files, and fill dictionary of correspondence
# between iterations and files
iteration_to_file = {}
for filename in all_files:
# Use only the name that end with .h5 or .hdf5
if filename.endswith('.h5') or filename.endswith('.hdf5'):
full_name = os.path.join(
os.path.abspath(path_to_dir), filename)
if is_single_file:
full_name = filename
else:
full_name = os.path.join(
os.path.abspath(path_to_dir), filename)
# extract all iterations from hdf5 file
f = h5py.File(full_name, 'r')
iterations = list(f['/data'].keys())
Expand Down

0 comments on commit bffb850

Please sign in to comment.