2.1.2. HDF5 in Python

One way to gain a quick familiarity with NeXus is to start working with some data. For at least the first few examples in this section, we have a simple two-column set of 1-D data, collected as part of a series of alignment scans by the Advanced Photon Source USAXS instrument during the time it was stationed at beam line 32ID. We will show how to read and write this data in Python using both the nexusformat [1] and h5py [2] packages. The nexusformat package provides a simplified syntax for reading and writing NeXus-compliant files by automatically handling some of the features required by the NeXus standard, such as the attributes that define group classes and plottable data. However, it also uses the h5py package to read/write the HDF5 files on disk. We provide tabbed examples showing how to produce equivalent files either using nexusformat or directly in h5py.

The actual data to be written was extracted (elsewhere) from a spec [3] data file and read as a text block from a file by the Python source code. Our examples will start with the simplest case and add only mild complexity with each new case since these examples are meant for those who are unfamiliar with NeXus.

2.1.2.1. Code examples

2.1.2.2. Example data used

The data shown plotted in the next figure will be written to the NeXus HDF5 file using only two NeXus base classes, NXentry and NXdata, in the first example and then minor variations on this structure in the next two examples. The data model is identical to the one in the Introduction chapter except that the names will be different, as shown below:

simple data structure

data structure of the simple example

1 /entry:NXentry
2     /mr_scan:NXdata
3        /mr : float64[31]
4        /I00 : int32[31]
simple-example-plot

plot of the simple example data

Simple example values

 1 17.92608    1037
 2 17.92591    1318
 3 17.92575    1704
 4 17.92558    2857
 5 17.92541    4516
 6 17.92525    9998
 7 17.92508    23819
 8 17.92491    31662
 9 17.92475    40458
10 17.92458    49087
11 17.92441    56514
12 17.92425    63499
13 17.92408    66802
14 17.92391    66863
15 17.92375    66599
16 17.92358    66206
17 17.92341    65747
18 17.92325    65250
19 17.92308    64129
20 17.92291    63044
21 17.92275    60796
22 17.92258    56795
23 17.92241    51550
24 17.92225    43710
25 17.92208    29315
26 17.92191    19782
27 17.92175    12992
28 17.92158    6622
29 17.92141    4198
30 17.92125    2248
31 17.92108    1321