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
 3import sys
 4import nxs
 5import numpy
 6
 7a = numpy.zeros((2,3,4),dtype=numpy.int)
 8val = 0
 9for 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
15nf = nxs.open("simple3D.h5", "w5")
16
17nf.makegroup("entry","NXentry")
18nf.opengroup("entry","NXentry")
19
20nf.makegroup("data","NXdata")
21nf.opengroup("data","NXdata")
22nf.putattr("signal","test")
23
24nf.makedata("test",'int32',[2,3,4])
25nf.opendata("test")
26nf.putdata(a)
27nf.closedata()
28
29nf.closegroup() # NXdata
30nf.closegroup() # NXentry
31
32nf.close()
33
34exit