-
Notifications
You must be signed in to change notification settings - Fork 6
/
h5_dump_summary.py
executable file
·57 lines (49 loc) · 1.93 KB
/
h5_dump_summary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /usr/local/python-2.7.6/bin/python
import sys,os
import h5py
import re
import numpy as np
import h5lib
# ------------------------------------------------------------------------------
def parse_h5_item(h5_root, item_path, verbose):
item_pointer = h5_root[item_path]
try:
# item is a group
keys = item_pointer.keys()
if verbose:
# print "group: path=", item_path, " members=" + str(keys)
print "group: path=", item_path, " num_members=", len(keys)
for k in keys:
if len(item_path) == 1: # item_path == '/'
item_path1 = item_path + k
else:
item_path1 = item_path + '/' + k
parse_h5_item(h5_root, item_path1, verbose)
except:
# item is a dataset
try:
data = np.array(item_pointer)
if verbose:
print "dataset: path=", item_path, " , shape=", data.shape, \
" , dtype=", data.dtype, " data=", data.tolist()
except:
sys.exit("Path " + path + " is not valid")
# ------------------------------------------------------------------------------
if __name__ == "__main__":
# print "len(sys.argv)=", len(sys.argv)
if len(sys.argv) in [2,3]:
h5_data = sys.argv[1]
verbose = 1
if len(sys.argv) == 3:
verbose = int(sys.argv[2])
# print "verbose=", verbose
else:
sys.exit("\nUsage: h5_dump_all.py <h5_file_or_dir> \n")
file_list = h5lib.get_file_list(h5_data, "")
# Parse the contexts of entire file, and dump its summary as text
for h5_file in file_list:
print "\nParsing file ", h5_file, "\n"
base_name = os.path.basename(h5_file)
h5_root = h5py.File(h5_file, "r")
path = '/'
parse_h5_item(h5_root, path, verbose)