Skip to content

Instantly share code, notes, and snippets.

@golobor
Created December 12, 2023 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save golobor/73707934c4ef503e54865676ca751bcc to your computer and use it in GitHub Desktop.
Save golobor/73707934c4ef503e54865676ca751bcc to your computer and use it in GitHub Desktop.
h5cp
import h5py
import argparse
def copy_dataset(source_file, destination_file, source_dataset, destination_dataset, overwrite):
with h5py.File(source_file, 'r') as source:
with h5py.File(destination_file, 'a') as destination:
if source_dataset not in source:
print(f"Dataset '{source_dataset}' does not exist in the source file.")
return
if destination_dataset in destination:
if not overwrite:
print(f"Dataset '{destination_dataset}' already exists in the destination file. Skipping...")
return
else:
print(f"Dataset '{destination_dataset}' already exists in the destination file. Overwriting...")
del destination[destination_dataset]
source_dataset_obj = source[source_dataset]
destination.create_dataset(destination_dataset, data=source_dataset_obj)
print(f"Dataset '{source_dataset}' copied to '{destination_dataset}' successfully.")
def main():
parser = argparse.ArgumentParser(description='Copy a dataset from one HDF5 file to another.')
parser.add_argument('source_file', help='Path to the source HDF5 file')
parser.add_argument('destination_file', help='Path to the destination HDF5 file')
parser.add_argument('source_dataset', help='Name of the dataset in the source file')
parser.add_argument('destination_dataset', help='Name of the dataset in the destination file')
parser.add_argument('-o', '--overwrite', action='store_true', help='Overwrite the destination dataset if it already exists')
args = parser.parse_args()
copy_dataset(args.source_file, args.destination_file, args.source_dataset, args.destination_dataset, args.overwrite)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment