Write a NeXus HDF5 file with plottable data

Building on the previous example, we wish to identify our measured data with the detector on the instrument where it was generated. In this hypothetical case, since the detector was positioned at some angle two_theta, we choose to store both datasets, two_theta and counts, in a NeXus group. One appropriate NeXus group is NXdetector. This group is placed in a NXinstrument group which is placed in a NXentry group. To support a default plot, we provide a NXdata group. Rather than duplicate the same data already placed in the detector group, we choose to link to those datasets from the NXdata group. (Compare the next figure with Linking in a NeXus file in the NeXus Design chapter of the NeXus User Manual.) The NeXus Design chapter provides a figure (Linking in a NeXus file) with a small variation from our previous example, placing the measured data within the /entry/instrument/detector group. Links are made from that data to the /entry/data group.

fig.simple_example_write2

h5py example showing linking in a NeXus file

The Python code to build an HDF5 data file with that structure (using numerical data from the previous example) is shown below.

 1 #!/usr/bin/env python
 2 """
 3 Writes a simple NeXus HDF5 file using h5py with links
 4 according to the example from Figure 2.1 in the Design chapter
 5 """
 6 
 7 from pathlib import Path
 8 
 9 import numpy
10 
11 from nexusformat.nexus import (NXdata, NXdetector, NXentry, NXfield,
12                                NXinstrument, NXlink, nxopen)
13 
14 filename = str(Path(__file__).absolute().parent.parent / "simple_example.dat")
15 buffer = numpy.loadtxt(filename).T
16 tthData = buffer[0]  # float[]
17 countsData = numpy.asarray(buffer[1], "int32")  # int[]
18 
19 with nxopen("simple_example_write2.hdf5", "w") as f:  # create the HDF5 NeXus file
20     f["entry"] = NXentry()
21     f["entry/instrument"] = NXinstrument()
22     f["entry/instrument/detector"] = NXdetector()
23 
24     # store the data in the NXdetector group
25     f["entry/instrument/detector/two_theta"] = NXfield(tthData, units="degrees")
26     f["entry/instrument/detector/counts"] = NXfield(countsData, units="counts")
27 
28     f["entry/data"] = NXdata(NXlink("/entry/instrument/detector/counts"),
29                              NXlink("/entry/instrument/detector/two_theta"))
30     f["entry/data"].set_default()

It is interesting to compare the output of the h5dump of the data file simple_example_write2.hdf5 with our Python instructions. See the downloads section below.

Look carefully! It appears in the output of h5dump that the actual data for two_theta and counts has moved into the NXdata group at HDF5 path /entry/data! But we stored that data in the NXdetector group at /entry/instrument/detector. This is normal for h5dump output.

A bit of explanation is necessary at this point. The data is not stored in either HDF5 group directly. Instead, HDF5 creates a DATA storage element in the file and posts a reference to that DATA storage element as needed. An HDF5 hard link requests another reference to that same DATA storage element. The h5dump tool describes in full that DATA storage element the first time (alphabetically) it is called. In our case, that is within the NXdata group. The next time it is called, within the NXdetector group, h5dump reports that a hard link has been made and shows the HDF5 path to the description.

NeXus recognizes this behavior of the HDF5 library and adds an additional structure when building hard links, the target attribute, to preserve the original location of the data. Not that it actually matters. the punx tree tool knows about the additional NeXus target attribute and shows the data to appear in its original location, in the NXdetector group.

 1   @default = "entry"
 2   entry:NXentry
 3     @NX_class = "NXentry"
 4     @default = "data"
 5     data:NXdata
 6       @NX_class = "NXdata"
 7       @axes = "two_theta"
 8       @signal = "counts"
 9       @two_theta_indices = [0]
10       counts --> /entry/instrument/detector/counts
11       two_theta --> /entry/instrument/detector/two_theta
12     instrument:NXinstrument
13       @NX_class = "NXinstrument"
14       detector:NXdetector
15         @NX_class = "NXdetector"
16         counts:NX_INT32[31] = [1037, 1318, 1704, '...', 1321]
17           @target = "/entry/instrument/detector/counts"
18           @units = "counts"
19         two_theta:NX_FLOAT64[31] = [17.92608, 17.92591, 17.92575, '...', 17.92108]
20           @target = "/entry/instrument/detector/two_theta"
21           @units = "degrees"

downloads

The Python code and files related to this section may be downloaded from the following table.

file

description

../simple_example.dat

2-column ASCII data used in this section

simple_example_write2.py

h5py code to write example simple_example_write2

nexusformat/simple_example_write2.py

nexusformat code to write example simple_example_write2

simple_example_write2.hdf5

NeXus file written by this code

simple_example_write2_h5dump.txt

h5dump analysis of the NeXus file

simple_example_write2_structure.txt

punx tree analysis of the NeXus file