h5py example writing a simple NeXus data file with links¶
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.
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''' 3Writes a simple NeXus HDF5 file using h5py with links 4according to the example from Figure 2.1 in the Design chapter 5''' 6 7importh5py 8importnumpy 910buffer=numpy.loadtxt("input.dat").T11tthData=buffer# float12countsData=numpy.asarray(buffer,'int32')# int1314f=h5py.File("writer_2_1.hdf5","w")# create the HDF5 NeXus file15f.attrs[u"default"]=u"entry"1617nxentry=f.create_group(u"entry")18nxentry.attrs[u"NX_class"]=u"NXentry"19nxentry.attrs[u"default"]=u"data"2021nxinstrument=nxentry.create_group(u"instrument")22nxinstrument.attrs[u"NX_class"]=u"NXinstrument"2324nxdetector=nxinstrument.create_group(u"detector")25nxdetector.attrs[u"NX_class"]=u"NXdetector"2627# store the data in the NXdetector group28ds_tth=nxdetector.create_dataset(u"two_theta",data=tthData)29ds_tth.attrs[u"units"]=u"degrees"30ds_counts=nxdetector.create_dataset(u"counts",data=countsData)31ds_counts.attrs[u"units"]=u"counts"3233# create the NXdata group to define the default plot34nxdata=nxentry.create_group(u"data")35nxdata.attrs[u"NX_class"]=u"NXdata"36nxdata.attrs[u"signal"]=u"counts"37nxdata.attrs[u"axes"]=u"two_theta"38nxdata.attrs[u"two_theta_indices"]=[0,]3940source_addr=u"/entry/instrument/detector/two_theta"# existing data41target_addr=u"two_theta"# new location42ds_tth.attrs[u"target"]=source_addr# a NeXus API convention for links43nxdata[target_addr]=f[source_addr]# hard link44# nxdata._id.link(source_addr, target_addr, h5py.h5g.LINK_HARD)4546source_addr=u"/entry/instrument/detector/counts"# existing data47target_addr=u"counts"# new location48ds_counts.attrs[u"target"]=source_addr# a NeXus API convention for links49nxdata[target_addr]=f[source_addr]# hard link50# nxdata._id.link(source_addr, target_addr, h5py.h5g.LINK_HARD)5152f.close()# be CERTAIN to close the file
It is interesting to compare the output of the h5dump
of the data file writer_2_1.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.