#!/usr/bin/env python """ Writes a simple NeXus HDF5 file using h5py with links according to the example from Figure 2.1 in the Design chapter """ from pathlib import Path import h5py import numpy filename = str(Path(__file__).absolute().parent.parent / "simple_example.dat") buffer = numpy.loadtxt(filename).T tthData = buffer[0] # float[] countsData = numpy.asarray(buffer[1], "int32") # int[] with h5py.File("simple_example_write2.hdf5", "w") as f: # create the HDF5 NeXus file f.attrs["default"] = "entry" nxentry = f.create_group("entry") nxentry.attrs["NX_class"] = "NXentry" nxentry.attrs["default"] = "data" nxinstrument = nxentry.create_group("instrument") nxinstrument.attrs["NX_class"] = "NXinstrument" nxdetector = nxinstrument.create_group("detector") nxdetector.attrs["NX_class"] = "NXdetector" # store the data in the NXdetector group ds_tth = nxdetector.create_dataset("two_theta", data=tthData) ds_tth.attrs["units"] = "degrees" ds_counts = nxdetector.create_dataset("counts", data=countsData) ds_counts.attrs["units"] = "counts" # create the NXdata group to define the default plot nxdata = nxentry.create_group("data") nxdata.attrs["NX_class"] = "NXdata" nxdata.attrs["signal"] = "counts" nxdata.attrs["axes"] = "two_theta" nxdata.attrs["two_theta_indices"] = [ 0, ] source_addr = "/entry/instrument/detector/two_theta" # existing data target_addr = "two_theta" # new location ds_tth.attrs["target"] = source_addr # a NeXus API convention for links nxdata[target_addr] = f[source_addr] # hard link # nxdata._id.link(source_addr, target_addr, h5py.h5g.LINK_HARD) source_addr = "/entry/instrument/detector/counts" # existing data target_addr = "counts" # new location ds_counts.attrs["target"] = source_addr # a NeXus API convention for links nxdata[target_addr] = f[source_addr] # hard link # nxdata._id.link(source_addr, target_addr, h5py.h5g.LINK_HARD)