From f70675987ff051e71997610e332cce528e33ed7b Mon Sep 17 00:00:00 2001 From: Jameson Graef Rollins <jrollins@finestructure.net> Date: Fri, 1 Jun 2018 17:59:50 -0700 Subject: [PATCH] initial HDF5 file read/write support Functions to save/load noise traces and IFO model parameters to/from HDF5 files. Requires the h5py module. --- gwinc/util.py | 34 ++++++++++++++++++++++++++++++++++ setup.py | 1 + 2 files changed, 35 insertions(+) create mode 100644 gwinc/util.py diff --git a/gwinc/util.py b/gwinc/util.py new file mode 100644 index 00000000..b0a76182 --- /dev/null +++ b/gwinc/util.py @@ -0,0 +1,34 @@ +import datetime +import h5py + +from . import struct + + +def load_hdf5(path): + """Load IFO and noises from HDF5 file. + + Returns (name, ifo, noises) tuple load from file. + + """ + with h5py.File(path, 'r') as f: + name = f.attrs['name'] + ifo = struct.Struct.from_yaml(f.attrs['IFO']) + noises = {} + for name, data in f['/traces'].items(): + noises[name] = data.value + return name, ifo, noises + + +def save_hdf5(name, ifo, noises, path): + """Save IFO and noises to HDF5 file. + + """ + with h5py.File(path, 'w') as f: + f.attrs['schema'] = 'GWINC noise budget' + # FIXME: add GWINC version + f.attrs['date'] = datetime.datetime.now().isoformat() + f.attrs['name'] = name + f.attrs['IFO'] = ifo.to_yaml() + tgrp = f.create_group('traces') + for noise, data in noises.items(): + tgrp.create_dataset(noise, data=data) diff --git a/setup.py b/setup.py index 8113c907..ecb7f502 100755 --- a/setup.py +++ b/setup.py @@ -30,6 +30,7 @@ setup_args = dict( install_requires = [ 'numpy', 'scipy', + 'h5py', 'matplotlib', ], -- GitLab