From 30c4e5288d6cb978652b491397ee4a4353f8b969 Mon Sep 17 00:00:00 2001 From: PrometheusPi Date: Tue, 21 Feb 2017 16:54:12 +0100 Subject: [PATCH] extract time step from hdf5 directly not from file name --- opmd_viewer/openpmd_timeseries/utilities.py | 27 ++++++++++----------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/opmd_viewer/openpmd_timeseries/utilities.py b/opmd_viewer/openpmd_timeseries/utilities.py index 558497f6..e4c356a5 100644 --- a/opmd_viewer/openpmd_timeseries/utilities.py +++ b/opmd_viewer/openpmd_timeseries/utilities.py @@ -3,14 +3,14 @@ It defines a number of helper functions that are used in main.py -Copyright 2015-2016, openPMD-viewer contributors -Authors: Remi Lehe +Copyright 2015-2017, openPMD-viewer contributors +Authors: Remi Lehe, Richard Pausch License: 3-Clause-BSD-LBNL """ import os -import re import numpy as np +import h5py from .data_reader.particle_reader import read_species_data @@ -38,17 +38,16 @@ def list_h5_files(path_to_dir): for filename in all_files: # Use only the name that end with .h5 or .hdf5 if filename[-3:] == '.h5' or filename[-5:] == '.hdf5': - # Extract the iteration, using regular expressions (regex) - regex_match = re.search('(\d+).h[df]*5', filename) - if regex_match is None: - print('Ill-formated HDF5 file: %s\n File names should end with' - ' the iteration number, followed by ".h5"' % filename) - else: - iteration = int(regex_match.groups()[-1]) - full_name = os.path.join( - os.path.abspath(path_to_dir), filename) - # Create list of tuples (which can be sorted together) - iters_and_names.append((iteration, full_name)) + 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()) + f.close() + # for each found iteration create list of tuples + # (which can be sorted together) + for key_iteration in iterations: + iters_and_names.append((int(key_iteration), full_name)) # Sort the list of tuples according to the iteration iters_and_names.sort()