Created
July 18, 2024 02:29
-
-
Save mjkramer/ea9a8921db10f927ad4c4322c635445d to your computer and use it in GitHub Desktop.
concatenate_hdf5.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import argparse | |
| import h5py | |
| import numpy as np | |
| def concatenate_files(file1, file2, output_file): | |
| def concatenate_datasets(group1, group2, output_group): | |
| for name, item in group1.items(): | |
| if isinstance(item, h5py.Dataset): | |
| data1 = item[:] | |
| data2 = group2[name][:] | |
| combined_data = np.concatenate((data1, data2)) | |
| output_group.create_dataset(name, data=combined_data) | |
| elif isinstance(item, h5py.Group): | |
| output_group.create_group(name) | |
| concatenate_datasets(group1[name], group2[name], output_group[name]) | |
| with h5py.File(file1, 'r') as f1, h5py.File(file2, 'r') as f2, h5py.File(output_file, 'w') as fout: | |
| concatenate_datasets(f1, f2, fout) | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument('file1') | |
| ap.add_argument('file2') | |
| ap.add_argument('output_file') | |
| args = ap.parse_args() | |
| concatenate_files(args.file1, args.file2, args.output_file) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment