21 #include <agx/config/AGX_USE_HDF5.h> 35 #include <H5Exception.h> 37 #include <H5Tpublic.h> 42 #define AGX_HDF5_VERIFY_BUFFER_SIZE 8192 43 #define agxHdf5VerifyN(x, node, format, ...) \ 46 char __str[AGX_HDF5_VERIFY_BUFFER_SIZE]; \ 47 agx_snprintf(__str, AGX_HDF5_VERIFY_BUFFER_SIZE, format, ##__VA_ARGS__); \ 48 ssize_t pathLength = H5Iget_name( (node)->getId(), nullptr, 0 ); \ 49 agx::String path = "<nameless>"; \ 50 if ( pathLength > 0 ) \ 52 char* pathBuffer = new char[pathLength+1]; \ 53 ::memset( pathBuffer, 0, pathLength+1 ); \ 54 H5Iget_name( (node)->getId(), pathBuffer, pathLength ); \ 57 std::stringstream __stream; \ 58 __stream << "[" << __FILE__ << ":" << __LINE__ << "] HDF5 error: " << __str << ". Error at object " << path << std::endl; \ 59 __stream << "==============================================================================" << std::endl; \ 60 const std::string& __string = __stream.str(); \ 61 std::cerr << __string << std::endl; \ 62 agxThrow std::runtime_error(__string); \ 108 struct IteratorData {
109 IteratorData() : m_name() {}
117 h2s(hsize_t a, hsize_t b) {
125 h2s() { m[0]=1; m[1]=1;}
126 #if !defined(__APPLE__) 128 hsize_t& operator[](
size_t i) {
return m[i]; }
130 hsize_t operator[](
size_t i)
const {
return m[i]; }
133 operator const hsize_t*()
const {
return m; }
134 operator hsize_t*() {
return m; }
143 const std::type_info& info;
144 const H5::PredType& predicate;
145 const H5::DataType type;
148 : info(typeid(void*)), predicate(H5::PredType::NATIVE_UINT64),
149 type(H5::IntType(H5::PredType::NATIVE_UINT64)), name(
"UNKNOWN")
152 h5_type(
const std::type_info& newInfo,
const H5::PredType& newPredicate,
153 const H5::DataType newType,
const char* newName)
154 : info(newInfo), predicate(newPredicate), type(newType), name(newName)
159 h5_type& operator= (
const h5_type&)
166 const h5_type& h5_type_find(
const std::type_info & type);
176 template <
typename R>
177 void dump_array(H5::CommonFG& g,
const std::string& name,
const R* a,
size_t m=1,
size_t n=1)
180 const h5_type& t = h5_type_find(
typeid(a[0]));
181 if (
typeid(a[0]) == t.info ) {
182 H5::DataSet dataset = g.createDataSet( name, t.type, create_dataspace(h2s(m, n)));
183 dataset.write( a, t.predicate);
186 }
catch (
const H5::DataSetIException& exception ) {
187 exception.printError();
191 template <
typename R>
192 void overwrite_scalar(H5::CommonFG& g,
const agx::String& name,
const R a) {
194 const h5_type& t = h5_type_find(
typeid(a));
195 if (
typeid(a) == t.info ) {
196 H5::DataSet dataset = g.openDataSet(name);
197 dataset.write(&a, t.predicate);
200 }
catch (
const H5::DataSetIException& exception ) {
201 exception.printError();
205 template <
typename R>
206 void dump_scalar(H5::CommonFG& g,
const agx::String& name,
const R a){
207 dump_array<R>(g, name, &a, 1, 0);
211 inline void dump_scalar<bool>(H5::CommonFG& g,
const agx::String& name,
const bool a) {
212 int8_t valueBits =
static_cast<int8_t
>(a);
214 const h5_type& t = h5_type_find(
typeid(valueBits));
215 if (
typeid(valueBits) == t.info ) {
216 H5::DataSet dataset = g.createDataSet( name, t.type, create_dataspace(h2s(1,0)) );
217 dataset.write( &valueBits, t.predicate );
220 }
catch (
const H5::DataSetIException& exception ) {
221 exception.printError();
225 template <
typename R>
226 void set_scalar_attribute (H5::H5Object& g,
const agx::String& name,
const R& val){
228 const h5_type& t = h5_type_find(
typeid(val));
229 if (
typeid(val) == t.info ) {
230 H5::Attribute attr = g.createAttribute(name, t.type, H5::DataSpace(H5S_SCALAR));
231 attr.write(t.type, &val);
235 catch (
const H5::AttributeIException& exception ) {
236 exception.printError();
239 catch (
const H5::DataTypeIException& exception ) {
240 exception.printError();
251 void print_attributes( H5::H5Object& loc,
const H5std_string attr_name,
void* operator_data);
258 template <
typename T >
259 bool get_scalar_attribute(H5::H5Object& g,
const agx::String& name, T& result){
261 const h5_type& t = h5_type_find(
typeid(result));
262 H5::Attribute attribute;
264 attribute = g.openAttribute(name);
265 }
catch (
const H5::AttributeIException& ) {
269 if ( attribute.getDataType() == t.predicate ) {
270 attribute.read( attribute.getDataType(), &result );
273 }
catch (
const H5::DataTypeIException& ) {
276 }
catch (
const H5::DataTypeIException& ) {
293 template <
typename R>
294 bool get_scalar(H5::CommonFG& g,
const agx::String& name, R& result)
296 const h5_type & t = h5_type_find(
typeid(result));
298 H5::DataSet dataset = g.openDataSet(name);
299 if ( dataset.getSpace().getSimpleExtentType() == H5S_SCALAR ) {
300 if ( dataset.getDataType() == t.predicate ) {
302 dataset.read(&result, dataset.getDataType() );
304 }
catch (
const H5::DataSetIException& ) {
312 }
catch (
const H5::DataSetIException& ) {
321 inline bool get_scalar<bool>(H5::CommonFG& g,
const agx::String& name,
bool& result)
323 const h5_type& t = h5_type_find(
typeid(result));
325 H5::DataSet dataset = g.openDataSet(name);
326 if (dataset.getSpace().getSimpleExtentType() == H5S_SCALAR) {
327 if (dataset.getDataType() == t.predicate) {
330 dataset.read(&value, dataset.getDataType());
331 result = (value != 0);
333 }
catch (
const H5::DataSetIException& ) {
336 }
else if (dataset.getDataType() == H5::IntType(H5::PredType::STD_I8BE)){
338 dataset.read(&result, dataset.getDataType());
340 }
catch (
const H5::DataSetIException& ) {
350 }
catch (
const H5::DataSetIException&) {
355 template <
typename R>
357 void dump_array_oct(H5::CommonFG& g,
const agx::String& name,
const R* a,
size_t m=1,
size_t n=1)
359 H5::Group gg = g.createGroup(name);
360 set_scalar_attribute(gg, oct_format, 1);
361 set_scalar_attribute(gg, agx_format, 1);
363 dump_string(gg, oct_type, oct_matrix);
366 dump_string(gg, oct_type, oct_scalar);
368 if ( m == n && n == 1)
370 dump_array<R>(gg,
"value", a, m, n);
376 template <
typename R>
377 void dump_scalar_oct(H5::CommonFG& g,
const agx::String& name, R a)
379 dump_array_oct<R>(g, name, &a);
396 AGXCORE_EXPORT hsize_t getIndexOf(
const H5::CommonFG& parent,
const H5std_string& childName );
397 AGXCORE_EXPORT bool hasChildNamed(
const H5::H5Object& parent,
const H5std_string& childName );
398 AGXCORE_EXPORT bool hasChildNamed(
const H5::H5File& parent,
const H5std_string& childName );
404 AGXCORE_EXPORT H5::Group getOrCreateGroup( H5::Group& parent,
const H5std_string& childName );
405 AGXCORE_EXPORT H5::Group getOrCreateGroup( H5::H5File* parent,
const H5std_string& childName );
420 int get_array(
const H5::CommonFG& g,
const agx::String& name, R& v )
424 H5::Exception::dontPrint();
425 H5::DataSet t = g.openDataSet(name);
427 H5::Exception::dontPrint();
428 H5::DataSpace ds = t.getSpace();
430 ds.getSimpleExtentDims(dims);
431 v.resize(
size_t(dims[0]));
432 t.read(&(v[0]), t.getFloatType());
435 }
catch ( H5::DataSetIException& ) {
438 catch (H5::GroupIException& ) {
444 template <
typename R>
445 int get_array_oct(
const H5::CommonFG& g,
const agx::String& name, R& v )
447 return get_array<R>(g, name, v);
454 herr_t list_nodes (hid_t group_id,
const char* member_name,
void* operator_data);
A component is an object containing other objects, enabling hierarchical structuring.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
std::recursive_mutex ReentrantMutex