Skip to content

Instantly share code, notes, and snippets.

@graeme-winter
Created September 11, 2024 08:24
Show Gist options
  • Save graeme-winter/f665f39ffbaedd7d65e3afefd178dbc5 to your computer and use it in GitHub Desktop.
Save graeme-winter/f665f39ffbaedd7d65e3afefd178dbc5 to your computer and use it in GitHub Desktop.
Tool to extract raw data from NeXus formatted HDF5 files to "raw" memory mapped data files
import os
import sys
import tqdm
import hdf5plugin
import h5py
def extract(nxs, template, start=None, end=None):
"""Extract data from the /entry/data/data dataset in nxs to
individual frames following template, from start to end (range-style)"""
with h5py.File(nxs, "r") as f:
d = f["/entry/data/data"]
s = d.shape
if start is None:
start = 0
if end is None:
end = s[0]
if end > s[0]:
raise ValueError(f"{end} beyond end of data set ({s[0]})")
print(f"Source dataset shape is: {s}")
for j in tqdm.tqdm(range(start, end)):
i = d[j, :, :]
i.tofile(template % j)
def main(filename):
if not filename.endswith(".nxs"):
raise ValueError(f"{filename} is not NeXus")
template = os.path.split(filename)[-1].replace(".nxs", "_%06d.raw")
extract(filename, template)
if __name__ == "__main__":
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment