File: hdf2npz.py

package info (click to toggle)
kineticstools 0.6.1%2Bgit20220223.1326a4d%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 22,140 kB
  • sloc: python: 3,503; makefile: 202; ansic: 104; sh: 55; xml: 19
file content (30 lines) | stat: -rwxr-xr-x 710 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python3

"""
One-time utility to convert our ipdModel HDF5 files to NumPy's .npz format
"""

import argparse
import re
import sys

import h5py
import numpy as np


def run(argv):
    p = argparse.ArgumentParser(description=__doc__)
    p.add_argument("file_name", help="Path to hdf5 file")
    args = p.parse_args(argv)
    assert args.file_name.endswith(".h5")
    npz_file = re.sub(".h5", ".npz", args.file_name)
    with h5py.File(args.file_name, 'r') as f:
        k = list(f.keys())[0]
        d = {k2: f[k][k2][:] for k2 in f[k].keys()}
        np.savez(npz_file, **d)
        print("Wrote {o}".format(o=npz_file))
    return 0


if __name__ == "__main__":
    sys.exit(run(sys.argv[1:]))