-
Notifications
You must be signed in to change notification settings - Fork 15
/
merge_big.py
executable file
·106 lines (71 loc) · 2.26 KB
/
merge_big.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
"""
Merge hdf5 (big) files
"""
import sys
import h5py
from parser import get_args_merge as parser
import msg
import check
from merge import get_filelist
from combine_big import load
def get_size(filelist):
"""Get total size of datasets; return size and ranges per file.
Keyword arguments:
filelist -- the list of input files
"""
total_size = 0
ranges = {}
for f in filelist:
data = h5py.File(f, 'r')
size = check.get_size(data)
ranges[f] = [total_size, total_size + size]
total_size = total_size + size
data.close()
return total_size, ranges
def create_datasets(output, source, size):
"""Prepare datasets for merged file (based on one of input files).
Keyword argument:
output -- output merged hdf5 file
source -- path to one of input hdf5 files
size -- total number of entries per dataset
"""
data = load(source)
for key in data:
shape = list(data[key].shape)
shape[0] = size
output.create_dataset(key, shape, dtype=data[key].dtype,
compression='gzip')
data.close()
def add_data(source, output, range):
"""Merge dictionaries with data.
Keyword arguments:
source -- input hdf5 file path
output -- output hdf5 file
range -- where to save data in output arrays
"""
data = h5py.File(source, 'r')
print("\nAdding entries from %(f)s in [%(i)d:%(j)d]"
% {"f": source, "i": range[0], "j": range[1]})
check.check_keys(data, output)
check.check_shapes(data, output)
for key in data:
output[key][range[0]:range[1]] = data[key]
data.close()
if __name__ == '__main__':
msg.box("HDF5 MANIPULATOR: MERGE")
args = parser()
filelist = get_filelist([f.strip() for f in args.input_files.split(',')])
if not filelist:
msg.error("No files matching --input were found.")
sys.exit(1)
print("The following input files were found:\n")
for f in filelist:
print("\t - %s" % f)
output = h5py.File(args.output, 'w')
size, ranges = get_size(filelist)
create_datasets(output, filelist[0], size)
for f in filelist:
add_data(f, output, ranges[f])
output.close()
msg.info("Done")