libpniio
Public Types | Public Member Functions | Data Fields | Private Member Functions | Static Private Member Functions | Private Attributes
pni::io::nx::nxfield< IMPID > Class Template Reference

nxfield base class More...

#include <nxfield.hpp>

Collaboration diagram for pni::io::nx::nxfield< IMPID >:
Collaboration graph
[legend]

Public Types

typedef nximp_map< IMPID >
::field_imp 
imp_type
 implementation type More...
 
typedef nximp_map< IMPID >
::type_imp 
type_type
 type implementation type More...
 
typedef nxfield< IMPID > field_type
 typedef for this type More...
 
typedef nxobject_trait< IMPID >
::attribute_type 
attribute_type
 attribute type More...
 

Public Member Functions

 nxfield ()
 default constructor More...
 
 nxfield (const field_type &o)
 copy constructor More...
 
 nxfield (field_type &&o)
 move constructor More...
 
 nxfield (imp_type &&o)
 move constructor from implementation object More...
 
 nxfield (const typename nxobject_trait< IMPID >::object_type &o)
 constructor More...
 
 ~nxfield ()
 destructor More...
 
field_typeoperator= (const field_type &f)
 copy assignment operator More...
 
field_typeoperator= (field_type &&f)
 move assignment operator More...
 
field_typeoperator= (const typename nxobject_trait< IMPID >::object_type &o)
 conversion assignment More...
 
template<typename CTYPE >
CTYPE shape () const
 field shape More...
 
size_t size () const
 return size More...
 
pni::core::type_id_t type_id () const
 get the type ID More...
 
nxobject_trait< IMPID >
::object_type 
parent () const
 get parent object More...
 
void grow (const size_t &e, const size_t &n=1)
 grow field along a particular dimension More...
 
size_t rank () const
 number of dimensions More...
 
template<typename T >
void read (T &value) const
 reading simple data from the dataset More...
 
template<typename T >
void read (size_t n, T *values) const
 reading data to memory More...
 
template<typename T >
void read (std::vector< T > &data) const
 reading data vector More...
 
template<typename STORAGE , typename IMAP , typename IPA >
void read (pni::core::mdarray< STORAGE, IMAP, IPA > &array) const
 read data to array More...
 
template<typename ATYPE >
void read (pni::core::array_view< ATYPE > &v) const
 
template<typename ATYPE >
void read (pni::core::array_view< ATYPE > &&v) const
 
void read (array &a) const
 read data to a array instance More...
 
template<typename T >
void write (const T &value) const
 write a single value More...
 
template<typename T >
void write (size_t n, const T *value) const
 write data from memory More...
 
template<typename T >
void write (const std::vector< T > &data) const
 write data from vector More...
 
template<typename STORAGE , typename IMAP , typename IPA >
void write (const pni::core::mdarray< STORAGE, IMAP, IPA > &a) const
 write data from mdarray More...
 
template<typename ATYPE >
void write (const pni::core::array_view< ATYPE > &v) const
 write data from array view More...
 
void write (const char *value) const
 write old style string More...
 
void write (const pni::core::array &a) const
 write array erasure More...
 
template<typename... ITYPES>
field_type operator() (ITYPES...indices) const
 set a selection on the field More...
 
field_type operator() ()
 clear a selection More...
 
field_type operator() (const std::vector< pni::core::slice > &selection) const
 set a selection on the field More...
 
pni::core::string name () const
 return field name More...
 
pni::core::string filename () const
 return filename More...
 
void close ()
 close field More...
 
bool is_valid () const noexcept
 check validity More...
 

Data Fields

nxattribute_manager< field_typeattributes
 attribute manager More...
 

Private Member Functions

template<typename ATYPE >
void _read_array (ATYPE &a) const
 read data to array More...
 
template<typename ATYPE >
void _write_array (const ATYPE &a) const
 write data from array More...
 

Static Private Member Functions

static pni::core::string _shape_mismatch_error_message (const pni::core::shape_t &ashape, const pni::core::shape_t &fshape)
 gernerate error message More...
 

Private Attributes

imp_type _imp
 field implementation More...
 

Detailed Description

template<nximp_code IMPID>
class pni::io::nx::nxfield< IMPID >

nxfield is a type used to store multidimensional data in a file. The IO methods it provides support all the array templates provided by libpnicore. Partial IO is provided by the () operator (see below). The interface of nxfield is quite similar to those of the array templates provided by libpnicore. Like them it provides a shape() and a size() method returning the number of elements along each dimension and the total number of elements in the field. rank() returns the number of dimensions of the field.

Unlike the array templates which provide the ID of their element type via a static member variable the method type_id() is provided by NXField for the same purpose. The number of dimensions a field provides is fixed at the time it is created. However, the number of elements along each of these dimensions can be altered at runtime. Two methods of the class are responsible for resizing a field: resize() and grow(). The difference between resize and grow is shown in the figure below. While the grow() operation resizes the field by a fixed amount of elements along a particular dimension a resize operation can enlarge multiple dimensions at the same time. The latter one is useful in situations where the number of elements along each dimension is unknown at the time the field is created. The grow() operation is however very useful in situations where the number of elements to store in an element is not known at the time the field is created. One can use this to append data along one dimensions. Consider for instance this example that shows a very common situation during experiments. Data should be stored in a field but the total number of points is not known when the field is created

//we start with a field of size 0 - no element
nxfield field = group.create_field<float32>("motor",shape_t{0});
size_t np = 0;
while(true)
{
field.grow(0);
field(np++).write(get_data());
if(get_break_condition()) break;
}

The above loop runs until get_break_condition() return true. In no case we can no in advance when the loop will exit and thus how many elements should be stored. The grow() method offers an easy way to handle such situations.

As HDF5 datasets, nxfield objects provide the possibility to read or write only a part of the data. The mechanism works pretty much as the array view selection in libpnicore. Compare the following code with the figures on the right side

darray<uint32> strip(shape_t{1024});
//selection 1
field1(3,slice(0,1024)).read(strip);
//selection 2
darray<uint32> block(shape_t{4,6});
field2(slice(3,7),slice(2,8)).read(block);
//selection 3
darray<uint32> block2(shape_t{4,5});
field3(slice(2,6),slice(2,10,2)).read(block2);

Under the hood the () operator of NXField applies a selection to the underlying HDF5 dataset according to the arguments passed to it. Every call to read or write immediately after reading or writing the data removes this selections.

Template Parameters
IMPIDimplementation id

Member Typedef Documentation

template<nximp_code IMPID>
typedef nxobject_trait<IMPID>::attribute_type pni::io::nx::nxfield< IMPID >::attribute_type
template<nximp_code IMPID>
typedef nxfield<IMPID> pni::io::nx::nxfield< IMPID >::field_type
template<nximp_code IMPID>
typedef nximp_map<IMPID>::field_imp pni::io::nx::nxfield< IMPID >::imp_type
template<nximp_code IMPID>
typedef nximp_map<IMPID>::type_imp pni::io::nx::nxfield< IMPID >::type_type

Constructor & Destructor Documentation

template<nximp_code IMPID>
pni::io::nx::nxfield< IMPID >::nxfield ( )
inlineexplicit
template<nximp_code IMPID>
pni::io::nx::nxfield< IMPID >::nxfield ( const field_type o)
inline
template<nximp_code IMPID>
pni::io::nx::nxfield< IMPID >::nxfield ( field_type &&  o)
inline
template<nximp_code IMPID>
pni::io::nx::nxfield< IMPID >::nxfield ( imp_type &&  o)
inlineexplicit
template<nximp_code IMPID>
pni::io::nx::nxfield< IMPID >::nxfield ( const typename nxobject_trait< IMPID >::object_type &  o)
inline

Constructs a field from an instance of nxobject.

Exceptions
type_errorif o does not store a field
Parameters
oreference to nxobject that should hold a field
template<nximp_code IMPID>
pni::io::nx::nxfield< IMPID >::~nxfield ( )
inline

Member Function Documentation

template<nximp_code IMPID>
template<typename ATYPE >
void pni::io::nx::nxfield< IMPID >::_read_array ( ATYPE &  a) const
inlineprivate

Read data from the field and store its content into an array.

Exceptions
memory_not_allocated_errorif array buffer not allocated
size_mismatch_errorif array and field size do not match
invalid_object_errorif field is not valid
object_errorin case of any other error
type_errorif array data type cannot be handled
io_errorin case of IO errors
Template Parameters
ATYPEarray type
Parameters
areference to an instance fo ATYPE
template<nximp_code IMPID>
static pni::core::string pni::io::nx::nxfield< IMPID >::_shape_mismatch_error_message ( const pni::core::shape_t &  ashape,
const pni::core::shape_t &  fshape 
)
inlinestaticprivate

Generate the error message for a shape mismatch error between a field and an array type.

Parameters
ashapearray shape
fshapefield shape
Returns
error message
template<nximp_code IMPID>
template<typename ATYPE >
void pni::io::nx::nxfield< IMPID >::_write_array ( const ATYPE &  a) const
inlineprivate

Write the content of an array to a field.

Exceptions
memory_not_allocated_errorif array buffer not allocated
size_mismatch_errorif array and field size do not match
invalid_object_errorif field is not valid
object_errorin case of any other error
type_errorif array type cannot be handled
io_errorin case of IO errors
Template Parameters
ATYPEarray type
Parameters
areference to an instance fo ATYPE
template<nximp_code IMPID>
void pni::io::nx::nxfield< IMPID >::close ( )
inline
Exceptions
type_errorif object type could not be determined during closing
object_errorin case of any other error
template<nximp_code IMPID>
pni::core::string pni::io::nx::nxfield< IMPID >::filename ( ) const
inline

Returns the name of the file the field belongs too.

Exceptions
invalid_object_errorif the field is not valid
io_errorif the filename information retrieval fails
type_errorif the internal object type does not support filename retrieval
object_errorin case of any other error
Returns
name of the file
template<nximp_code IMPID>
void pni::io::nx::nxfield< IMPID >::grow ( const size_t &  e,
const size_t &  n = 1 
)
inline

Grows the field by n elements along dimension e. This method is pretty useful in cases where an arbitrary number of points shall be stored in a field and their number is not known when the field was created.

Exceptions
index_errorif e exceeds the rank of the field
object_errorin case of any other error
invalid_object_errorif field not valid
Parameters
eindex of dimension along which to grow
nnumber of elements by which to grow
template<nximp_code IMPID>
bool pni::io::nx::nxfield< IMPID >::is_valid ( ) const
inlinenoexcept
Exceptions
object_errorvalidity check fails
Returns
true if the field is valid, false otherwise
template<nximp_code IMPID>
pni::core::string pni::io::nx::nxfield< IMPID >::name ( ) const
inline
Exceptions
invalid_object_errorif field is not valid
io_errorif name could not be read
type_errorin case of an internal type issue
object_errorin case of any other error
Returns
name of the field
template<nximp_code IMPID>
template<typename... ITYPES>
field_type pni::io::nx::nxfield< IMPID >::operator() ( ITYPES...  indices) const
inline

This method applies a selection to the field and return a reference to this field. This can now be used to write data only to the selection of the array.

nxfield f = g.create_field<uint16>("frame",shape_t{1024,1024});
darray<uint16> spec(shape_t{1024})k
f(100,slice(0,1024)).read(spec)

The selection will be reset with every call to the read() or write methods.

Exceptions
invalid_object_errorif field is not valid
shape_missmatch_errorif selection and field rank do not match
object_errorin case of any other error
Template Parameters
ITYPESindex types
Parameters
indicesinstances of ITYPES
Returns
field object with selection set
template<nximp_code IMPID>
field_type pni::io::nx::nxfield< IMPID >::operator() ( )
inline

Clears a selection applied to the field.

Exceptions
object_errorin case of failure
Returns
new field instance with clearn selection
template<nximp_code IMPID>
field_type pni::io::nx::nxfield< IMPID >::operator() ( const std::vector< pni::core::slice > &  selection) const
inline

Operator to set a selection to the field. The selection is given by a vector container of slice objects. The selection will be reset with each call to the read() or write() methods.

Exceptions
invalid_object_errorif field is not valid
shape_missmatch_errorif selection and field rank do not match
object_errorin case of any other error
Parameters
selectioncontainer with instances of slice
Returns
instance of NXField with selection set
template<nximp_code IMPID>
field_type& pni::io::nx::nxfield< IMPID >::operator= ( const field_type f)
inline
Parameters
flvalue reference to a field
Returns
reference to the new field
template<nximp_code IMPID>
field_type& pni::io::nx::nxfield< IMPID >::operator= ( field_type &&  f)
inline
Parameters
frvalue reference to an instance of nxfield
Returns
reference to nxfield
template<nximp_code IMPID>
field_type& pni::io::nx::nxfield< IMPID >::operator= ( const typename nxobject_trait< IMPID >::object_type &  o)
inline
Exceptions
type_errorif object is not a field
Parameters
olvalue reference to an instance of nxobject
Returns
reference to the new field
template<nximp_code IMPID>
nxobject_trait<IMPID>::object_type pni::io::nx::nxfield< IMPID >::parent ( ) const
inline

Returns the parent object of the field.

Exceptions
invalid_object_errorif field is not valid
type_errorif the interal type of the field object could not be determined
objet_errorin case of any other error
Returns
parent object
template<nximp_code IMPID>
size_t pni::io::nx::nxfield< IMPID >::rank ( ) const
inline

Returns the number of dimensions of the field.

Exceptions
invalid_object_errorif field is not valid
object_errorin case of any other error
Returns
number of dimensions
template<nximp_code IMPID>
template<typename T >
void pni::io::nx::nxfield< IMPID >::read ( T &  value) const
inline

Read a single data value from the dataset. In order to succeed the dataset must be a scalar dataset or the total size of the dataset must be 1.

uint32 scalar;
field.read(scalar);
Exceptions
size_mismatch_errorif dataset is not scalar
invalid_object_errorif field is not valid
type_errorif argument type cannot be handled
io_errorin case of IO failure
object_errorin case of any other error
Template Parameters
Tdata type of the argument
Parameters
valuevariable where to store the data
template<nximp_code IMPID>
template<typename T >
void pni::io::nx::nxfield< IMPID >::read ( size_t  n,
T *  values 
) const
inline

Read data from a field to a region in memory referedn to by poitner values. This method does not check any shape or size bounds.

Exceptions
size_mismatch_errorif n does not match the array size
invalid_object_errorif field is not valid
type_errorif argument type cannot be handled
io_errorin case of IO failure
object_errorin case of any other error
Template Parameters
Tdata type to read (in memory)
Parameters
nnumber of elements that should be read to values
valuespointer to memory reagion
template<nximp_code IMPID>
template<typename T >
void pni::io::nx::nxfield< IMPID >::read ( std::vector< T > &  data) const
inline

Read from disk and store it to an STL vector.

Exceptions
size_mismatch_errorif the size of the vector does not match the size of the field
invalid_object_errorif field is not valid
type_errorif argument type cannot be handled
io_errorin case of IO failure
object_errorin case of any other error
Template Parameters
Tvalue type of the vector
Parameters
datareference to the STL vector where to store the data
template<nximp_code IMPID>
template<typename STORAGE , typename IMAP , typename IPA >
void pni::io::nx::nxfield< IMPID >::read ( pni::core::mdarray< STORAGE, IMAP, IPA > &  array) const
inline

Copy the data stored in the field to an array object. An exception is thrown if the buffer holding the arrays data is not allocated or the shape of the array does not match the shape of the field.

darray<float32> a(field.shape<shape_t>());
field.read(a);
Exceptions
memory_not_allocated_errorif array buffer not allocated
size_mismatch_errorif array and field size do not match
invalid_object_errorif field is not valid
object_errorin case of any other error
type_errorif array data type cannot be handled
io_errorin case of IO errors
Template Parameters
STORAGEstorage type of the mdarray template
IMAPindex map type of the array type
IPAinplace arithmetic policy
Parameters
arrayArray instance where to store the data
template<nximp_code IMPID>
template<typename ATYPE >
void pni::io::nx::nxfield< IMPID >::read ( pni::core::array_view< ATYPE > &  v) const
inline
template<nximp_code IMPID>
template<typename ATYPE >
void pni::io::nx::nxfield< IMPID >::read ( pni::core::array_view< ATYPE > &&  v) const
inline
template<nximp_code IMPID>
void pni::io::nx::nxfield< IMPID >::read ( array &  a) const
inline

This method reads data to an array type erasure.

Exceptions
memory_not_allocated_errorif array buffer not allocated
size_mismatch_errorif array and field size do not match
invalid_object_errorif field is not valid
object_errorin case of any other error
type_errorif array data type cannot be handled
io_errorin case of IO errors
Parameters
ainstance of the array
template<nximp_code IMPID>
template<typename CTYPE >
CTYPE pni::io::nx::nxfield< IMPID >::shape ( ) const
inline

Returns the shape of the field

Exceptions
invalid_object_errorif field is not valid
object_errorin case of any other error
Returns
container with elements per dimension
template<nximp_code IMPID>
size_t pni::io::nx::nxfield< IMPID >::size ( ) const
inline

Return the size (number of elements) in the field.

Exceptions
invalid_object_errorif field is not valid
objecct_errorin case of any other error
Returns
total number of elements in the field
template<nximp_code IMPID>
pni::core::type_id_t pni::io::nx::nxfield< IMPID >::type_id ( ) const
inline

Return the ID of the data type stored in the field.

Exceptions
invalid_object_errorif field is not valid
object_errorin case of any other error
type_errorif data type is not supported
Returns
data type ID
template<nximp_code IMPID>
template<typename T >
void pni::io::nx::nxfield< IMPID >::write ( const T &  value) const
inline

Writes a single value of type T to the field. This method will succeed only if the field can hold only a single value.

float64 data = 1.2340;
field.write(data);
Exceptions
size_mismatch_errorif the dataspace is not scalar
invalid_object_errorin case of IO errors
io_errorin case of IO errors
object_errorin case of all other errors
type_errorif tid does not have a corresponding HDF5 data type
Template Parameters
Tmemory data type of the source
Parameters
valuevalue to write
template<nximp_code IMPID>
template<typename T >
void pni::io::nx::nxfield< IMPID >::write ( size_t  n,
const T *  value 
) const
inline

Write data from a memory region referend to by value to the field. The method does not perform any size or shape checking. It assumes that the region of memory is large enough.

Exceptions
size_mismatch_errorif n does not match field size
invalid_object_errorin case of IO errors
io_errorin case of IO errors
object_errorin case of all other errors
type_errorif tid does not have a corresponding HDF5 data type
Template Parameters
Tmemory data type of the source
Parameters
nnumber of elements that should be written to the field
valuepointer to memory region
template<nximp_code IMPID>
template<typename T >
void pni::io::nx::nxfield< IMPID >::write ( const std::vector< T > &  data) const
inline

Write data stored in an STL vector to disk.

Exceptions
size_mismatch_errorif the size of the vector does not match the size of the field
invalid_object_errorin case of IO errors
io_errorin case of IO errors
object_errorin case of all other errors
type_errorif tid does not have a corresponding HDF5 data type
Template Parameters
Tvalue type of the vector
Parameters
datareference to the STL vector
template<nximp_code IMPID>
template<typename STORAGE , typename IMAP , typename IPA >
void pni::io::nx::nxfield< IMPID >::write ( const pni::core::mdarray< STORAGE, IMAP, IPA > &  a) const
inline

Write data stored in an instance of mdarray.

Exceptions
memory_not_allocated_errorif array buffer not allocated
size_mismatch_errorif array and field size do not match
invalid_object_errorif field is not valid
object_errorin case of any other error
type_errorif array type cannot be handled
io_errorin case of IO errors
Template Parameters
STORAGEstorage type for mdarray
IMAPindex map type for mdarray
IPAinplance arithmetic type
Parameters
ainstance of mdarray
template<nximp_code IMPID>
template<typename ATYPE >
void pni::io::nx::nxfield< IMPID >::write ( const pni::core::array_view< ATYPE > &  v) const
inline

This function writes data from an array view. In principle this function works the same as for mdarray. However, there can be a significant performance penalty when the selection is not contiguous. In this case the content of the view must be copied to a temporary buffer before being writen to disk.

Exceptions
memory_not_allocated_errorif array buffer not allocated
size_mismatch_errorif array and field size do not match
invalid_object_errorif field is not valid
object_errorin case of any other error
type_errorif array type cannot be handled
io_errorin case of IO errors
Template Parameters
ATYPEthe array type of the view
Parameters
varray view instance
template<nximp_code IMPID>
void pni::io::nx::nxfield< IMPID >::write ( const char *  value) const
inline

Writes a C-style string to disk. This method is a specialization of the write(const T &value) template mathod.

Exceptions
size_mismatch_errorif the dataspace is not scalar
invalid_object_errorin case of IO errors
io_errorin case of IO errors
object_errorin case of all other errors
type_errorif tid does not have a corresponding HDF5 data type
Parameters
valuepointer to string data
template<nximp_code IMPID>
void pni::io::nx::nxfield< IMPID >::write ( const pni::core::array &  a) const
inline

Write the data stored by an array erasure.

Exceptions
memory_not_allocated_errorif array buffer not allocated
size_mismatch_errorif array and field size do not match
invalid_object_errorif field is not valid
object_errorin case of any other error
type_errorif array type cannot be handled
io_errorin case of IO errors
Parameters
areference to array erasure

Field Documentation

template<nximp_code IMPID>
imp_type pni::io::nx::nxfield< IMPID >::_imp
private
template<nximp_code IMPID>
nxattribute_manager<field_type> pni::io::nx::nxfield< IMPID >::attributes

This public attribute provides access to the attribute manager instance of this field.

See also
nxattribute_manager

The documentation for this class was generated from the following file: