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.1.6.1. NAPI Python Example: write simple NeXus file

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