forked from INM-6/h5py_wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_h5file
executable file
·65 lines (54 loc) · 2.28 KB
/
convert_h5file
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
#!/usr/bin/env python
# encoding: utf8
"""
Conversion script to convert files from a previous
release version to the current version.
Usage: convert_h5file [-h|--help] [<files>...] [--save-backup] [-v|--verbose] [--release=<version>]
Options:
<files> List of files to be converted, can be a file pattern
--save-backup save backup version of file in old file format [default: False].
--release=<version> release version used to create the file to be converted [default: 0.0.1]
-v, --verbose print informative output to screen
-h, --help print this text
"""
import docopt
import os
import importlib
import sys
import tempfile
import h5py_wrapper.wrapper as h5w
import h5py_wrapper.lib as h5w_lib
if __name__ == '__main__':
args = docopt.docopt(__doc__)
# First get release version used to create the file to be converted
version_stripped = args['--release'].replace('.', '')
release_base_name = '_'.join(('h5py_wrapper', version_stripped))
# This case distinction is necessary because the directory structure
# changes between versions
module = '.'.join((release_base_name, 'wrapper'))
try:
h5w_old = importlib.import_module(module)
except ImportError:
tmpdir = tempfile.mkdtemp()
h5w_lib.get_previous_version(args['--release'], tmpdir)
sys.path.append(tmpdir)
h5w_old = importlib.import_module(module)
files = args['<files>'] or sys.stdin.read().splitlines()
for fn in files:
if args['--verbose']:
print("Loading %s" % fn)
d = h5w_old.load_h5(fn)
# This step is necessary because the 0.0.1 release loads int and float types as numpy
# datatypes, which are not supported as scalar datatypes by the 1.0 release version.
if args['--verbose']:
print("Checking for numpy datatypes in scalar values.")
h5w_lib.convert_numpy_types_in_dict(d)
if args['--save-backup']:
if args['--verbose']:
print("Saving backup file.")
orig_name = os.path.splitext(fn)
backup_name = ''.join(('_'.join((orig_name[0], version_stripped)), orig_name[1]))
os.rename(fn, backup_name)
if args['--verbose']:
print("Saving file in new format.")
h5w.save(fn, d, write_mode='w')