如何使用C ++ API在HDF5文件中创建多值属性 [英] How to create multi-value attribute in a HDF5 file using C++ API

查看:329
本文介绍了如何使用C ++ API在HDF5文件中创建多值属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑开始

我正在尝试基于本机类型(float,int ...)创建"pair,triplet或n-uplet"属性:

I'm trying to create an "pair, triplet or n-uplet" attribute based on a native type (float, int...) :

  • 成对的浮点数,三元组的浮点数,n-uplet of floats属性
  • int对,int三元组,int属性的n-uplet

我不是要创建数组"属性,我不是要创建复合"属性

I'm not trying to create an "Array" attribute, I'm not trying to create a "Compound" attribute

编辑结束

我正在尝试基于本机类型(float,int ...)创建一个属性,但该属性包含2,3个或更多值(相当于一个对或一个n-uplet).

I'm trying to create an attribute based on a native type (float, int...) but which contains 2,3 or more values (equivalent to a pair or a n-uplet).

我不想创建一个数组!我想创建与数组非常相似但又不同的东西

我可以通过这种方式创建单值属性(对于"double"属性):

I can create single value attribute this way (for a "double" attribute) :

H5::DataSpace dataSpace = H5::DataSpace();
H5::Attribute attribute = group.createAttribute(attributeName, H5::PredType::IEEE_F64LE, dataSpace);
attribute.write(H5::PredType::IEEE_F64LE, &attributeValue);

要创建几个"double",我已经尝试过:

To create a couple of "double", I've tried this :

hsize_t dimension;
dimension = 2;
H5::ArrayType dataType(H5::PredType::IEEE_F64LE, 1, &dimension);

H5::DataSpace dataSpace = H5::DataSpace();
H5::Attribute attribute = group.createAttribute(attributeName, dataType, dataSpace);

double attributeValue[2];
attributeValue[0] = x;
attributeValue[1] = y;

attribute.write(dataType, attributeValue);

但是它会在HDF5文件中创建一个数组类型属性.

But it creates an array type attribute in the HDF5 file.

我知道有可能创建一个包含多个值的属性,因为我可以使用HDFView GUI软件进行创建(第一个是使用上述代码创建的,第二行是使用GUI创建的属性-我想创建这种属性):

I know it's possible to create an attribute whcih contains multiple values because I can do it using the HDFView GUI software (the first one is created using the above code, the second line is an attribute created using the GUI - I want to create this kind of attribute) :

任何帮助将不胜感激!

推荐答案

在不完全了解您要完成的工作的情况下,我相信您正在寻找的是使用HDF5复合数据类型H5 :: CompType的自定义数据类型,通常用于保存简单的结构.取自 HDF5 C ++复合示例页面,该结构

Without exactly knowing what you are trying to accomplish, I believe what you are looking for is a self-defined datatype using the HDF5 compound datatype H5::CompType, which is usually used to save simple structs. Taken from the HDF5 C++ compound example page, the struct

  typedef struct s1_t {
    int    a;
    float  b;
    double c;
  } s1_t;

具有关联的化合物数据类型:

has the associated compound datatype:

  CompType mtype1( sizeof(s1_t) );
  mtype1.insertMember( MEMBER1, HOFFSET(s1_t, a), PredType::NATIVE_INT);
  mtype1.insertMember( MEMBER3, HOFFSET(s1_t, c), PredType::NATIVE_DOUBLE);
  mtype1.insertMember( MEMBER2, HOFFSET(s1_t, b), PredType::NATIVE_FLOAT);

然后,将复合数据类型与本机数据类型相同地对待,也可以将其另存为属性.

Compound datatyped are then treated the same way as native datatypes and may also be saved as attributes.

修改

您在上面的代码中犯的错误是,当您实际上不想保存数组时,将数据类型定义为另存为H5 :: ArrayType.您真正想要的是保存在高维数据空间中的简单数据类型(例如PredType :: NATIVE_DOUBLE).

The error you made in your code above was to define your datatype to be saved as an H5::ArrayType when you didn't actually want to save an Array. What you really want is a simple datatype (such as PredType::NATIVE_DOUBLE) saved in a higher dimensional dataspace.

#include "H5Cpp.h"

#ifndef H5_NO_NAMESPACE
  using namespace H5;
#ifndef H5_NO_STD
  using std::cout;
  using std::endl;
#endif  // H5_NO_STD
#endif  
const H5std_string FILE_NAME("save.h5");
const H5std_string ATT_NAME("Attribute");

int main(){
  const hsize_t dims=5;
  int ndims=1;

  DataType dtype=PredType::NATIVE_DOUBLE;

  H5File h5file(FILE_NAME, H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);

  DataSpace* dspace = new DataSpace(ndims,&dims);
  Attribute att=h5file.createAttribute(ATT_NAME,dtype,*dspace);
  delete dspace;

  double attvalue[dims];
  for(auto i=0;i<dims;++i) attvalue[i]=i;

  att.write(dtype,attvalue);
  h5file.close();

  return 0;
} 

这应该重现上面的"createdUsingHDFVIEW"属性(数据类型除外).我没有HDFView,因此无法检查.起初我没有想到这一点,因为我倾向于将H5 :: DataSpace视为一种数组类型(实际上是数组).

This should reproduce the "createdUsingHDFVIEW" attribute above (except for the datatype). I can't check to make sure as I dont have HDFView. This didn't occur to me at first as I tend to think of H5::DataSpace as a type of array (which it actually is).

这篇关于如何使用C ++ API在HDF5文件中创建多值属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆