Package reflectometry :: Package reduction :: Module nxs :: Class NeXus

Class NeXus

source code


Instance Methods
 
__init__(self, filename, mode='r')
Open the NeXus file returning a handle.
source code
 
__del__(self)
Be sure to close the file before deleting the last reference.
source code
 
__str__(self)
Return a string representation of the NeXus file handle.
source code
 
open(self)
Opens the NeXus file handle if it is not already open.
source code
 
close(self)
Close the NeXus file associated with handle.
source code
 
flush(self)
Flush all data to the NeXus file.
source code
 
setnumberformat(self, type, format)
Set the output format for the numbers of the given type (only applies to XML).
source code
 
makegroup(self, name, nxclass)
Create the group nxclass:name.
source code
 
openpath(self, path)
Open a particular group '/path/to/group'.
source code
 
opengrouppath(self, path)
Open a particular group '/path/to/group', or the dataset containing the group if the path refers to a dataset.
source code
 
opengroup(self, name, nxclass)
Open the group nxclass:name.
source code
 
closegroup(self)
Close the currently open group.
source code
 
getgroupinfo(self)
Query the currently open group returning the tuple numentries, path, nxclass.
source code
 
initgroupdir(self)
Reset getnextentry to return the first entry in the group.
source code
 
getnextentry(self)
Return the next entry in the group as name,nxclass tuple.
source code
 
entries(self)
Iterator of entries.
source code
 
getinfo(self)
Returns the tuple dimensions,type for the currently open dataset.
source code
 
opendata(self, name)
Open the named data set within the current group.
source code
 
closedata(self)
Close the currently open data set.
source code
 
makedata(self, name, dtype=None, shape=None)
Create a data element of the given type and shape.
source code
 
compmakedata(self, name, dtype=None, shape=None, mode='lzw', chunks=None)
Create a data element of the given dimensions and type.
source code
 
getdata(self)
Return the data.
source code
 
getslab(self, slab_offset, slab_shape)
Get a slab from the data array.
source code
 
putdata(self, data)
Write data into the currently open data block.
source code
 
putslab(self, data, slab_offset, slab_shape)
Put a slab into the data array.
source code
 
initattrdir(self)
Reset the getnextattr list to the first attribute.
source code
 
getattrinfo(self)
Returns the number of attributes for the currently open group/data object.
source code
 
getnextattr(self)
Returns the name, length, and data type for the next attribute.
source code
 
getattr(self, name, length, dtype)
Returns the value of the named attribute.
source code
 
putattr(self, name, value, dtype=None)
Saves the named attribute.
source code
 
attrs(self)
Iterate over attributes.
source code
 
getgroupID(self)
Return the id of the current group so we can link to it later.
source code
 
getdataID(self)
Return the id of the current data so we can link to it later.
source code
 
makelink(self, ID)
Link the previously captured group/data ID into the currently open group.
source code
 
makenamedlink(self, name, ID)
Link the previously captured group/data ID into the currently open group, but under a different name.
source code
 
sameID(self, ID1, ID2)
Return True of ID1 and ID2 point to the same group/data.
source code
 
opensourcegroup(self)
If the current node is a linked to another group or data, then open the group or data that it is linked to.
source code
 
link(self)
Returns the item which the current item links to, or None if the current item is not linked.
source code
 
inquirefile(self, maxnamelen=1024)
Return the filename for the current file.
source code
 
linkexternal(self, name, nxclass, url)
Return the filename for the external link if there is one, otherwise return None.
source code
 
isexternalgroup(self, name, nxclass, maxnamelen=1024)
Return the filename for the external link if there is one, otherwise return None.
source code
 
show(self, path=None, indent=0)
Print the structure of a NeXus file from the current node.
source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__

Class Variables
  lib = <CDLL '/usr/local/lib/libNeXus.dylib', handle 115260 at ...
Properties

Inherited from object: __class__

Method Details

__init__(self, filename, mode='r')
(Constructor)

source code 

Open the NeXus file returning a handle.

mode can be one of the following:
nxs.ACC_READ 'r' nxs.ACC_RDWR 'rw' nxs.ACC_CREATE 'w' nxs.ACC_CREATE4 'w4' nxs.ACC_CREATE5 'w5' nxs.ACC_CREATEXML 'wx'

Raises RuntimeError if the file could not be opened, with the filename as part of the error message.

Corresponds to NXopen(filename,mode,&handle)

Overrides: object.__init__

__str__(self)
(Informal representation operator)

source code 
Return a string representation of the NeXus file handle.
Overrides: object.__str__

close(self)

source code 

Close the NeXus file associated with handle.

Raises RuntimeError if file could not be opened.

Corresponds to NXclose(&handle)

flush(self)

source code 

Flush all data to the NeXus file.

Raises RuntimeError if this fails.

Corresponds to NXflush(&handle)

setnumberformat(self, type, format)

source code 

Set the output format for the numbers of the given type (only applies to XML).

Raises ValueError if this fails.

Corresponds to NXsetnumberformat(&handle,type,format)

makegroup(self, name, nxclass)

source code 

Create the group nxclass:name.

Raises RuntimeError if the group could not be created.

Corresponds to NXmakegroup(handle, name, nxclass)

openpath(self, path)

source code 

Open a particular group '/path/to/group'. Paths can be relative to the currently open group.

Raises ValueError.

Corresponds to NXopenpath(handle, path)

opengrouppath(self, path)

source code 

Open a particular group '/path/to/group', or the dataset containing the group if the path refers to a dataset. Paths can be relative to the currently open group.

Raises ValueError.

Corresponds to NXopengrouppath(handle, path)

opengroup(self, name, nxclass)

source code 

Open the group nxclass:name.

Raises ValueError if the group could not be opened.

Corresponds to NXopengroup(handle, name, nxclass)

closegroup(self)

source code 

Close the currently open group.

Raises RuntimeError if the group could not be closed.

Corresponds to NXclosegroup(handle)

getgroupinfo(self)

source code 

Query the currently open group returning the tuple numentries, path, nxclass. The path consists of names of subgroups starting at the root separated by "/".

Raises ValueError if the group could not be opened.

Corresponds to NXgetgroupinfo(handle)

initgroupdir(self)

source code 

Reset getnextentry to return the first entry in the group.

Raises RuntimeError if this fails.

Corresponds to NXinitgroupdir(handle)

getnextentry(self)

source code 

Return the next entry in the group as name,nxclass tuple.

Raises RuntimeError if this fails, or if there is no next entry.

Corresponds to NXgetnextentry(handle,name,nxclass,&storage).

This function doesn't return the storage class for data entries since getinfo returns shape and storage, both of which are required to read the data.

entries(self)

source code 

Iterator of entries.

for name,nxclass in nxs.entries():
process(name,nxclass)

This automatically opens the corresponding group/data for you, and closes it when you are done. Do not rely on any paths remaining open between entries as we restore the current path each time.

This does not correspond to an existing NeXus API function, but instead combines the work of initgroupdir/getnextentry and open/close on data and group.

getinfo(self)

source code 

Returns the tuple dimensions,type for the currently open dataset. Dimensions is an integer array whose length corresponds to the rank of the dataset and whose elements are the size of the individual dimensions. Storage type is returned as a string, with 'char' for a stored string, '[u]int[8|16|32]' for various integer values or 'float[32|64]' for floating point values. No support for complex values.

Raises RuntimeError if this fails.

Note that this is the recommended way to establish if you have a dataset open.

Corresponds to NXgetinfo(handle, &rank, dims, &storage), but with storage converted from HDF values to numpy compatible strings, and rank implicit in the length of the returned dimensions.

opendata(self, name)

source code 

Open the named data set within the current group.

Raises ValueError if could not open the dataset.

Corresponds to NXopendata(handle, name)

closedata(self)

source code 

Close the currently open data set.

Raises RuntimeError if this fails (e.g., because no dataset is open).

Corresponds to NXclosedata(handle)

makedata(self, name, dtype=None, shape=None)

source code 

Create a data element of the given type and shape. See getinfo for details on types. This does not open the data for writing.

Set the first dimension to nxs.UNLIMITED, for extensible data sets, and use putslab to write individual slabs.

Raises ValueError if it fails.

Corresponds to NXmakedata(handle,name,type,rank,dims)

compmakedata(self, name, dtype=None, shape=None, mode='lzw', chunks=None)

source code 

Create a data element of the given dimensions and type. See getinfo for details on types. Compression mode is one of 'none', 'lzw', 'rle' or 'huffman'. chunks gives the alignment of the compressed chunks in the data file. There should be one chunk size for each dimension in the data.

Defaults to mode='lzw' with chunk size set to the length of the fastest varying dimension.

Raises ValueError if it fails.

Corresponds to NXmakedata(handle,name,type,rank,dims).

getdata(self)

source code 

Return the data. If data is a string (1-D char array), a python string is returned. If data is a scalar (1-D numeric array of length 1), a python numeric scalar is returned.

Raises RuntimeError if this fails.

Corresponds to NXgetdata(handle, data)

getslab(self, slab_offset, slab_shape)

source code 

Get a slab from the data array.

Offsets are 0-origin. Shape can be inferred from the data. Offset and shape must each have one entry per dimension.

Raises ValueError if this fails.

Corresponds to NXgetslab(handle,data,offset,shape)

putdata(self, data)

source code 

Write data into the currently open data block.

Raises ValueError if this fails.

Corresponds to NXputdata(handle, data)

putslab(self, data, slab_offset, slab_shape)

source code 

Put a slab into the data array.

Offsets are 0-origin. Shape can be inferred from the data. Offset and shape must each have one entry per dimension.

Raises ValueError if this fails.

Corresponds to NXputslab(handle,data,offset,shape)

initattrdir(self)

source code 

Reset the getnextattr list to the first attribute.

Raises RuntimeError if this fails.

Corresponds to NXinitattrdir(handle)

getattrinfo(self)

source code 

Returns the number of attributes for the currently open group/data object. Do not call getnextattr() more than this number of times.

Raises RuntimeError if this fails.

Corresponds to NXgetattrinfo(handl, &n)

getnextattr(self)

source code 

Returns the name, length, and data type for the next attribute. Call getattrinfo to determine the number of attributes before calling getnextattr. Data type is returned as a string. See getinfo for details. Length is the number of elements in the attribute.

Raises RuntimeError if NeXus returns ERROR or EOD.

Corresponds to NXgetnextattr(handle,name,&length,&storage) but with storage converted from HDF values to numpy compatible strings.

Note: NeXus API documentation seems to say that length is the number of bytes required to store the entire attribute.

getattr(self, name, length, dtype)

source code 

Returns the value of the named attribute. Requires length and data type from getnextattr to allocate the appropriate amount of space for the attribute.

Corresponds to NXgetattr(handle,name,data,&length,&storage)

putattr(self, name, value, dtype=None)

source code 

Saves the named attribute. The attribute value is a string or a scalar.

Raises ValueError if the attribute could not be saved.

Corresponds to NXputattr(handle,name,data,length,storage)

Note length is the number of elements to write rather than the number of bytes to write.

attrs(self)

source code 

Iterate over attributes.

for name,value in file.attrs():
process(name,value)

This automatically reads the attributes of the group/data. Do not change the active group/data while processing the list.

This does not correspond to an existing NeXus API function, but combines the work of attrinfo/initattrdir/getnextattr/getattr.

getgroupID(self)

source code 

Return the id of the current group so we can link to it later.

Raises RuntimeError

Corresponds to NXgetgroupID(handle, &ID)

getdataID(self)

source code 

Return the id of the current data so we can link to it later.

Raises RuntimeError

Corresponds to NXgetdataID(handle, &ID)

makelink(self, ID)

source code 

Link the previously captured group/data ID into the currently open group.

Raises RuntimeError

Corresponds to NXmakelink(handle, &ID)

makenamedlink(self, name, ID)

source code 

Link the previously captured group/data ID into the currently open group, but under a different name.

Raises RuntimeError

Corresponds to NXmakenamedlink(handle,name,&ID)

sameID(self, ID1, ID2)

source code 

Return True of ID1 and ID2 point to the same group/data.

This should not raise any errors.

Corresponds to NXsameID(handle,&ID1,&ID2)

opensourcegroup(self)

source code 

If the current node is a linked to another group or data, then open the group or data that it is linked to.

Note: it is unclear how can we tell if we are linked, other than perhaps the existence of a 'target' attribute in the current item.

Raises RuntimeError

Corresponds to NXopensourcegroup(handle)

link(self)

source code 

Returns the item which the current item links to, or None if the current item is not linked. This is equivalent to scanning the attributes for target and returning it if target is not equal to self.

This does not correspond to an existing NeXus API function, but combines the work of attrinfo/initattrdir/getnextattr/getattr.

inquirefile(self, maxnamelen=1024)

source code 

Return the filename for the current file. This may be different from the file that was opened (file.filename) if the current group is an external link to another file.

Raises RuntimeError if this fails.

Corresponds to NXinquirefile(&handle,file,len)

linkexternal(self, name, nxclass, url)

source code 

Return the filename for the external link if there is one, otherwise return None.

Corresponds to NXisexternalgroup(&handle,name,nxclass,file,len)

isexternalgroup(self, name, nxclass, maxnamelen=1024)

source code 

Return the filename for the external link if there is one, otherwise return None.

Corresponds to NXisexternalgroup(&handle,name,nxclass,file,len)

show(self, path=None, indent=0)

source code 

Print the structure of a NeXus file from the current node.

TODO: Break this into a tree walker and a visitor.


Class Variable Details

lib

Value:
<CDLL '/usr/local/lib/libNeXus.dylib', handle 115260 at 8058e10>