Nexus Python API - README


Contents

Overview

NeXus Python Api binds the NeXus libraries to Python. It brings functionality of the NeXus API to Python for reading, writing and modifying NeXus Files. Python NeXus API imitates the functionality NeXus API though with a more object oriented flavour.

Information on NeXus Dataformat: http://www.nexusformat.org/Introduction.

Information on IDL: http://www.nexusformat.org/Introduction.


Installation

Requirements

This package provides a ctypes binding from Python+numpy to the precompiled NeXus library. It has been tested on Python 2.5 in Windows, OS X and Linux. The bindings should be easily modified for any version of Python which supports ctypes and numpy.

The NeXus packages and installation instructions are available at http://www.nexusformat.org/Download.

For Windows the NeXus Data Format Windows Installer Kit, which includes the necessary hdf5, hdf4 and xml libraries, is recommended. http://download.nexusformat.org/kits/windows/

Building and Installing

This package uses the standard distutils installer for python.
$ python setup.py install
You will also need to make sure that libNeXus can be found. For windows, libNeXus.dll and the associated hdf/xml dlls should be together in nxs.py directory. The package will also look in the standard nexus windows installer location, C:/Program Files/NeXus Data Format/. For Linux, libNeXus.so should be in nxs.py directory, /usr/lib, /usr/local/lib, or one of the directories listed on LD_LIBRARY_PATH. For Apple OS X, libNeXus.dylib should be in the nxs.py directory, /usr/lib, /usr/local/lib, or one of the directories listed on DYLD_LIBRARY_PATH. If the file is not in a standard place with the standard name, set NEXUSLIB to the full path to the NeXus library.

Using API from Python

Test Files

The Python NeXus-API includes nxstest.py, which provides similar tests to the original C api file napi_test.c.

After installing, you can run the test using:
    python [options] [formats]
where options are -q for quiet and -x for external, and formats are hdf4, hdf5 and xml. The default is to test hdf5 format read/write.

Using The API And An Example

The API's functions aim to reproduce the funtionality of the C API closely. Some low level functionality has been hidden from the user. Memory allocation functions NXmalloc and NXfree are done automatically in the API when needed. The file handle is an object with methods rather than a parameter to functions. Instead of checking status codes, errors raise exceptions.

The input and returned values match the format of the data in the files. On return, python creates values of the correct type. However on input, numeric types must be created correctly using numpy.array(...,dtype='type'). The matching datatypes are:

NeXus Datatype Python Datatype
NX_CHAR 'char'
NX_FLOAT32 'float32'
NX_FLOAT64 'float64'
NX_UINT8 'uint8'
NX_INT16 'int16'
NX_UINT16 'uint16'
NX_INT32 'int32'
NX_UINT32 'uint32'


Here is simple example program that demonstrates the basic functions and most important differences between the C Nexus Api and the Python Nexus API.
  1. Creates a NeXus file with access method HDF5
  2. adds datagroups
  3. makes a data array of data type NX_INT32
  4. puts data to the array
  5. reads the data and attributes
  6. prints data and attribute value
  7. closes the groups and the file.
import nxs,numpy

# Access method accepts strings or integer (e.g., nxs.ACC_CREATE5)
f = nxs.open("test.h5", 'w5')
f.makegroup("testgroup", "NXentry")
f.opengroup("testgroup", "NXentry")
f.makegroup("anothergroup", "NXentry")

# Some data to store in the file, this of type int16
data = numpy.array([[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15] ],'int16')

# Make a data set for the array. Note that this could also
# be done as f.makedata('data1','int16',[4,4])
f.makedata('data1', dtype=data.dtype, shape=data.shape)
f.opendata("data1")
f.putdata(data)

# Attribute type can be inferred from the data or specified.  If inferred, it
# must match the type of the data.  Attributes are scalars or strings, with
# string length inferred from value.
f.putattr('integer-attribute', 42, 'int16')
f.putattr('double-attribute', 3.14159)
f.closedata() 
# NeXus returns arrays from getattr/getdata/getslab
f.opendata("data1")
print 'data :',f.getdata()

# getnext functions return tuples
attrname,length,type = f.getnextattr ()
value = f.getattr(attrname, length, type)
print 'first attribute: ', value

# ... or you can use iterators for attrs and entries
print 'all attributes'
for attr,value in f.attrs(): 
    print "  %s: %s"%(attr,value)

f.closedata()
f.closegroup()
f.close()

NeXus API Routines

Documentation for the individual methods, and how they differ from the basic NAPI methods is available from the Python command line. Rather than duplicate it here, use the following in Python:

import nxs
help(nxs)