Skip to content

Instantly share code, notes, and snippets.

@zonca
Created July 3, 2014 16:53
Show Gist options
  • Save zonca/8e0dda9d246297616de9 to your computer and use it in GitHub Desktop.
Save zonca/8e0dda9d246297616de9 to your computer and use it in GitHub Desktop.
# every input file has a different length,
# so I first scan through the files, compute their length,
# `self.lengths` here is an array of cumulative lengths
# so first I create the dataset for full length with resize(lengths[-1])
# then I fill it in.
def run(self):
with h5py.File(self.filename, mode='w') as h5f:
for n, fitsfile in enumerate(self.files):
print("Processing %s" % fitsfile)
data = self.get_data(fitsfile)
print("Data read, length %d" % len(data))
try:
h5f['data'][self.lengths[n]:self.lengths[n+1]] = data
except exceptions.KeyError: #if dataset not created yet
h5f.create_dataset('data', data=data, maxshape=(None,))
h5f['data'].resize((self.lengths[-1],))
h5f.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment