How to create multi-value attribute in a HDF5 file using C++ API

后端 未结 1 757
生来不讨喜
生来不讨喜 2021-01-26 02:46

EDIT STARTS

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

  • pair of float, triple
相关标签:
1条回答
  • 2021-01-26 03:12

    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.

    Edit

    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;
    } 
    

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

    0 讨论(0)
提交回复
热议问题