Skip to content

reflectometry/nexus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<title>
Nexus Python API - README
</title>
</head>

<body>
<!-- start content -->

<h1>Nexus Python API - README </h1>
<hr>
<table id="toc" class="toc" summary="Contents"><tbody><tr><td><h2>Contents</h2> </a>

<ul>

<li ><a href="#Overview">1. Overview</a>

<li ><a href="#Installation">2. Installation</a>
<ul>

<li ><a href="#Requirements">2.1 Requirements</a></li>
<li><a href="#Building and Installing">2.2 Building and Installing </a></li>
<li><a href="#Linux">2.3 Linux </a></li>
<li><a href="#Windows">2.4 Windows </a></li>

</ul>

<li ><a href="#Using API from Python">3. Using API from Python</a>
<ul>

<li><a href="#Test Files">3.1 Test Files </a></li>
<li><a href="#Using The API And An Example">3.2 Using The API And An Example</a></li>
<li><a href="#NeXus API Routines">3.3 NeXus API Routines</a></li>


</ul>
</ul>


<a name="#Overview"></a><h2> Overview</h2>

<p>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.

<p>Information on NeXus Dataformat: <A HREF="http://www.nexusformat.org/Introduction">http://www.nexusformat.org/Introduction</A>.
</p>
<p>
Information on IDL: <A HREF="http://www.ittvis.com/">http://www.nexusformat.org/Introduction</A>.
</p>

<p><br> </p>

<a name="Installation"></a><h2> Installation</h2>

<a name="Requirements"></a><h3> Requirements</h3>
<p>
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.
</p>

<p>

<p>  The NeXus packages and installation instructions are available at
<A HREF="http://www.nexusformat.org/Download">http://www.nexusformat.org/Download</A>. </p>
</p>

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

</p>

<a href="Building and Installing"></a><h3>Building and Installing</h3>

This package uses the standard distutils installer for python.
<TABLE WIDTH=85% BORDER=1 CELLPADDING=4 CELLSPACING=3 ALIGN=CENTER style="background-color: rgb(238, 238, 238);" >
	<COL WIDTH=376>
	<THEAD>
		<TR>
		<TD WIDTH=376 VALIGN=TOP>
<pre>
$ python setup.py install
</pre>
		</TD>
		</TR>
	</THEAD>
</TABLE>

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.

<a name="Using API from Python"></a><h2> Using API from Python</h2>

<a name="Test Files"></a><h3> Test Files</h3>

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

</P>After installing, you can run the test using:
<pre>
    python [options] [formats]
</pre>
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.
</P>

<a name="Using The API And An Example"></a><h3> Using The API And An Example</h3> 

<p> 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.</p>

<p>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:


<TABLE BORDER=1 WIDTH=50% CELLPADDING=2 CELLSPACING=1 ALIGN=CENTER style="background-color: rgb(238, 238, 238);">
	<COL WIDTH=373>
	<THEAD>
		<TR style="background-color: rgb(204, 204, 204);">
		<TD WIDTH=373 VALIGN=TOP>
		NeXus Datatype
		</TD>
		<TD WIDTH=373 VALIGN=TOP>
		Python Datatype
		</TD>

		</TR>
		<TR>
		<TD WIDTH=373 VALIGN=TOP>
		NX_CHAR
		</TD>
		<TD WIDTH=373 VALIGN=TOP>
		'char'
		</TD>
		</TR>

		<TR>
		<TD WIDTH=373 VALIGN=TOP>
		NX_FLOAT32
		</TD>
		<TD WIDTH=373 VALIGN=TOP>
		'float32'
		</TD>
		</TR>


		<TR>
		<TD WIDTH=373 VALIGN=TOP>
		NX_FLOAT64
		</TD>
		<TD WIDTH=373 VALIGN=TOP>
		'float64'
		</TD>
		</TR>

		<TR>
		<TD WIDTH=373 VALIGN=TOP>
		NX_UINT8
		</TD>
		<TD WIDTH=373 VALIGN=TOP>
		'uint8'
		</TD>
		</TR>

		<TR>
		<TD WIDTH=373 VALIGN=TOP>
		NX_INT16
		</TD>
		<TD WIDTH=373 VALIGN=TOP>
		'int16'
		</TD>
		</TR>


		<TR>
		<TD WIDTH=373 VALIGN=TOP>
		NX_UINT16
		</TD>
		<TD WIDTH=373 VALIGN=TOP>
		'uint16'
		</TD>
		</TR>

		<TR>
		<TD WIDTH=373 VALIGN=TOP>
		NX_INT32
		</TD>
		<TD WIDTH=373 VALIGN=TOP>
		'int32'
		</TD>
		</TR>


		<TR>
		<TD WIDTH=373 VALIGN=TOP>
		NX_UINT32
		</TD>
		<TD WIDTH=373 VALIGN=TOP>
		'uint32'
		</TD>
		</TR>

	</THEAD>
</TABLE>

<p> <br> </p>
Here is simple example program that demonstrates the basic functions and most important differences between the C Nexus Api and the Python Nexus API.
<ol>
<li>Creates a NeXus file with access method HDF5</li>
<li>adds datagroups</li>
<li>makes a data array of data type NX_INT32</li>
<li>puts data to the array</li>
<li>reads the data and attributes</li>
<li>prints data and attribute value</li>
<li>closes the groups and the file.</li>
</ol>



<TABLE WIDTH=85% BORDER=1 CELLPADDING=4 CELLSPACING=3 ALIGN=CENTER style="background-color: rgb(238, 238, 238);" >
	<COL WIDTH=376>
	<THEAD>
		<TR>
		<TD WIDTH=376 VALIGN=TOP>
<pre>
import nexus as 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()
</pre>
		</TD>
		</TR>
	</THEAD>
</TABLE>

</p>

<a name="NeXus API Routines"></a><h3> NeXus API Routines</h3>
<p>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:

<TABLE WIDTH=85% BORDER=1 CELLPADDING=4 CELLSPACING=3 ALIGN=CENTER style="background-color: rgb(238, 238, 238);" >
	<COL WIDTH=376>
	<THEAD>
		<TR>
		<TD WIDTH=376 VALIGN=TOP>
<pre>
import nxs
help(nxs)
</pre>
		</TD>
		</TR>
	</THEAD>
</TABLE>
</p>
</body></html>

About

Tree based version of nexus file interactor

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages