2.2.1.6. HDF5 in Python with NAPI

A single code example is provided in this section that writes 3-D data to a NeXus HDF5 file in the Python language using the NAPI: NeXus Application Programmer Interface (frozen).

The data to be written to the file is a simple three-dimensional array (2 x 3 x 4) of integers. The single dataset is intended to demonstrate the order in which each value of the array is stored in a NeXus HDF5 data file.

2.2.1.6.1. NAPI Python Example: write simple NeXus file

 1#!/usr/bin/python
 2
 3import numpy
 4import nxs
 5
 6a = numpy.zeros((2, 3, 4), dtype=numpy.int)
 7val = 0
 8for i in range(2):
 9    for j in range(3):
10        for k in range(4):
11            a[i, j, k] = val
12            val = val + 1
13
14nf = nxs.open("simple3D.h5", "w5")
15
16nf.makegroup("entry", "NXentry")
17nf.opengroup("entry", "NXentry")
18
19nf.makegroup("data", "NXdata")
20nf.opengroup("data", "NXdata")
21nf.putattr("signal", "test")
22
23nf.makedata("test", "int32", [2, 3, 4])
24nf.opendata("test")
25nf.putdata(a)
26nf.closedata()
27
28nf.closegroup()  # NXdata
29nf.closegroup()  # NXentry
30
31nf.close()
32
33exit