Skip to content

Commit

Permalink
Bugfix: keep original data-type (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus committed May 25, 2020
1 parent fecfebf commit 57cfeb6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion GooseHDF5/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
warnings.filterwarnings("ignore")


__version__ = '0.7.2'
__version__ = '0.7.3'


def abspath(path):
Expand Down
11 changes: 8 additions & 3 deletions GooseHDF5/cli/G5repack.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Options:
-c, --compress Apply compression (using the loss-less GZip algorithm).
-f, --float Store doubles as floats.
-h, --help Show help.
--version Show version.
Expand All @@ -24,20 +25,24 @@
import os
import tempfile
import warnings
import numpy as np
warnings.filterwarnings("ignore")

def check_isfile(fname):
if not os.path.isfile(fname):
raise IOError('"{0:s}" does not exist'.format(fname))

def copy_dataset(old, new, path, compress):
def copy_dataset(old, new, path, compress, double_to_float):

data = old[path][...]

if data.size == 1 or not compress or not isnumeric(data):
new[path] = old[path][...]
else:
dset = new.create_dataset(path, data.shape, compression="gzip")
dtype = old[path].dtype
if dtype == np.float64 and double_to_float:
dtype = np.float32
dset = new.create_dataset(path, data.shape, dtype=dtype, compression="gzip")
dset[:] = data

for key in old[path].attrs:
Expand All @@ -57,6 +62,6 @@ def main():
with h5py.File(filename, 'r') as source:
with h5py.File(tempname, 'w') as tmp:
for path in getpaths(source):
copy_dataset(source, tmp, path, args['--compress'])
copy_dataset(source, tmp, path, args['--compress'], args['--float'])

os.replace(tempname, filename)

0 comments on commit 57cfeb6

Please sign in to comment.