【问题标题】:Need to ensure C# HDF5 output (of attributes) is same as Visual C++. Or what is equivalent HDF5 formatting in C# as C++需要确保 C# HDF5 输出(属性)与 Visual C++ 相同。或者 C# 中与 C++ 等效的 HDF5 格式
【发布时间】:2015-01-05 09:11:00
【问题描述】:

我写了另一个问题 (https://stackoverflow.com/questions/26812721/hdf5-c-sharp-examples-to-solve-3-specific-questions-i-demonstrate-what-has-been),该问题被标记为过于笼统,因此我将重写以使其更清晰、更简洁和尽可能具体。我使用 Visual C++ 和 HDF5(参见:www.hdfgroup.org)来输出一些数据集。一切正常。为了格式化 C++ 中的两个属性,我使用了

DataSpace attr_dataspace = DataSpace(H5S_SCALAR);  
Attribute attribute_cardNum = dataSet.createAttribute(
    attrCardNumber, PredType::STD_I32BE, attr_dataspace, PropList::DEFAULT);
attribute_cardNum.write(PredType::NATIVE_INT, &cardNumber);  // write out the card number

StrType strdatatype(PredType::C_S1, 256); // of length 256 characters
// Create attribute and write to it
Attribute attribute_boardName = dataSet.createAttribute(
    attrBoardName, strdatatype, attr_dataspace);
attribute_boardName.write(strdatatype, asciiBoardName);

例如,使用 HDF5 Java 查看器,我得到了

Name        Value       Type        Array Size

Board Name  UltraMaster String,length=256   Scalar
Card number     0       32-bit integer      Scalar

当我使用 C# 导出 HDF5(使用 hdf5.net 上的库)时,我得到:

 Name       Value       Type        Array Size

Board Name  ˜„      String, length=256  1
Card Number 0       32-bit integer      1

请注意,Array Size 现在是“1”而不是标量,并且 Board Name 的值已完全用完。 我的 C# 代码是不同的(显然 :))。我有:

// Create card Number attribute
H5AttributeId attrCardId =   H5A.create(dataSetId, "Card Number", typeId, 
   H5S.create_simple(1, new long[1] { 1 }));
H5A.write(attrCardId, new H5DataTypeId(H5T.H5Type.NATIVE_INT), 
   new H5Array<int> (new int[]{cardNumber}));

// Create Board Name attribute
byte[] asciiStr = ASCIIEncoding.ASCII.GetBytes("Board Name");
H5AttributeId attrBoardNameId = H5A.create(dataSetId, "Board Name",
   H5T.create(H5T.CreateClass.STRING, 256), H5S.create_simple(1, new long[1] { 1 }));
H5A.write(attrBoardNameId, H5T.create(H5T.CreateClass.STRING,256), 
     new H5Array<string>(new string[] { GetBoardNameFromCardNum(cardNumber) }));

正如我所说,结果是不同的。最好让 C# 模仿 C++ 输出(尽管我想我可以更改 C++ 代码)。具体来说,

  1. 如何将数组大小设为“标量”
  2. 如何输出板名?我认为这与我处理 C# 字符串(unicode)而不是 C++ ascii 字符串的事实有关。

【问题讨论】:

    标签: c# visual-c++ hdf5


    【解决方案1】:

    如果以后有人需要这个,对我有用的相关代码是:

    // Card Number Attribute
     H5AttributeId attrCardId =   H5A.create(dataSetId, "Card Number", typeId, H5S.create(H5S.H5SClass.SCALAR));
            H5A.write(attrCardId, new H5DataTypeId(H5T.H5Type.NATIVE_INT), new H5Array<int> (new int[]{cardNumber}));
    

    注意 H5A.create 而不是 H5A.create_simple

     // Create Board Name attribute
            byte[] asciiStr = ASCIIEncoding.ASCII.GetBytes(GetBoardNameFromCardNum(cardNumber));
            H5AttributeId attrBoardNameId = H5A.create(dataSetId, "Board Name", H5T.create(H5T.CreateClass.STRING, 256), H5S.create(H5S.H5SClass.SCALAR));
            H5A.write(attrBoardNameId, H5T.create(H5T.CreateClass.STRING,256), new H5Array<byte>(asciiStr));
    

    另外,我想指出,有一些 HDF5.net 示例位于: HDF5.net。寻找•HDF5DotNet 源代码和示例 一旦你得到它,在“tests”文件夹以及“example”文件夹下查看。

    干杯,

    戴夫

    【讨论】:

    • 像魅力一样工作,谢谢
    猜你喜欢
    • 2019-01-17
    • 2015-09-29
    • 2019-07-30
    • 2014-05-11
    • 2014-05-15
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    相关资源
    最近更新 更多