【问题标题】:How do I get the native type of HDF5 attribute?如何获取 HDF5 属性的本机类型?
【发布时间】:2017-10-19 17:11:37
【问题描述】:

我正在使用 Visual Studio (C#) 和 HDF5 P/Invoke。

我制作了一个 HDF5 文件,其中包含具有不同数据类型属性的组和数据集(在这种情况下,假设它是一个整数,保存为 H5T.NATIVE_INT32,但我对其他数据类型也有同样的问题)。现在我正在编写一个代码来从 HDF5 文件中读取数据。要读取属性值,我首先需要确定属性值的数据类型。我尝试了以下方法:

attributeId = H5A.open(groupId, attributeName, H5P.DEFAULT);
hid_t attributeSpace = H5A.get_space(attributeId);
H5S.class_t extentType = H5S.get_simple_extent_type(attributeSpace);
hid_t typeId = H5A.get_type(attributeId);
attributeClass = H5T.get_class(typeId);
type = H5T.get_native_type(typeId, H5T.direction_t.DEFAULT);
H5T.close(typeId);

但是,结果变量 typeH5T.NATIVE_INTEGER 类型或我能想到的任何其他 H5T 类型不匹配。事实上,甚至

H5T.get_native_type(H5T.NATIVE_INT,H5T.direction_t.DEFAULT) == H5T.NATIVE_INT

返回false,所以看起来H5T.get_native_type() 没有返回类型,但可能是它的副本或指针,这与类型本身不同。这是预期的行为还是错误?关于如何正确判断属性值类型的任何想法?

【问题讨论】:

    标签: c# hdf5


    【解决方案1】:

    解决问题的另一种方法(即找出 HDF5 属性的数据类型)是使用 C# 的工具 HDFql,如下所示:

    using AS.HDFql;
    
    public class Example
    {
        public static void Main(string []args)
        {
    
            int dataType;
    
            // create an HDF file named "example.h5" and use (i.e. open) it
            HDFql.Execute("CREATE AND USE FILE example.h5");
    
            // create an attribute named "attrib" of type float
            HDFql.Execute("CREATE ATTRIBUTE attrib AS FLOAT");
    
            // get data type of attribute "attrib" and populate HDFql default cursor with it
            HDFql.Execute("SHOW DATA TYPE attrib");
    
            // move HDFql default cursor to first position
            HDFql.CursorFirst();
    
            // retrieve data type from HDFql default cursor
            dataType = HDFql.CursorGetInt();
    
            // print message according to data type
            if (dataType == HDFql.TinyInt || dataType == HDFql.VarTinyInt)
                System.Console.WriteLine("Data type is a char");
            else if (dataType == HDFql.UnsignedTinyInt || dataType == HDFql.UnsignedVarTinyInt)
                System.Console.WriteLine("Data type is an unsigned char");
            else if (dataType == HDFql.SmallInt || dataType == HDFql.VarSmallInt)
                System.Console.WriteLine("Data type is a short");
            else if (dataType == HDFql.UnsignedSmallInt || dataType == HDFql.UnsignedVarSmallInt)
                System.Console.WriteLine("Data type is an unsigned short");
            else if (dataType == HDFql.Int || dataType == HDFql.VarInt)
                System.Console.WriteLine("Data type is an int");
            else if (dataType == HDFql.UnsignedInt || dataType == HDFql.UnsignedVarInt)
                System.Console.WriteLine("Data type is an unsigned int");
            else if (dataType == HDFql.BigInt || dataType == HDFql.VarBigInt)
                System.Console.WriteLine("Data type is a long long");
            else if (dataType == HDFql.UnsignedBigInt || dataType == HDFql.UnsignedVarBigInt)
                System.Console.WriteLine("Data type is an unsigned long long");
            else if (dataType == HDFql.Float || dataType == HDFql.VarFloat)
                System.Console.WriteLine("Data type is a float");
            else if (dataType == HDFql.Double || dataType == HDFql.VarDouble)
                System.Console.WriteLine("Data type is a double");
            else if (dataType == HDFql.Char || dataType == HDFql.VarChar)
                System.Console.WriteLine("Data type is a char");
            else if (dataType == HDFql.Opaque)
                System.Console.WriteLine("Data type is an opaque");
            else if (dataType == HDFql.Enumeration)
                System.Console.WriteLine("Data type is an enumeration");
            else if (dataType == HDFql.Compound)
                System.Console.WriteLine("Data type is a compound");
            else
                System.Console.WriteLine("Unknown data type");
    
        }
    
    }
    

    如果您需要获取属性attrib 的字节序或大小,请执行HDFql.Execute("SHOW ENDIANNESS attrib");HDFql.Execute("SHOW SIZE attrib");

    【讨论】:

      【解决方案2】:

      HDF5 封装数据,您应该使用 HDF5 例程来处理它们。您获得的值称为“类型标识符”,它们是 HDF5 不透明数据类型。

      您应该使用H5Tequal 来评估两个类型标识符的类型是否相等

      H5Tequal(type, H5T.NATIVE_INT)
      

      PS:我是从 C API 到 HDF5 的角度来写这篇文章的,希望它也适用于你的情况。

      【讨论】:

      • 我认为应该是 H5T_NATIVE_INT 而不是 H5T.NATIVE_INT
      • 这是一个旧回复,但我猜用户使用了为 HDF5 库提供点命名空间的 C# 绑定 here
      • 那我收回我的声明。我假设您的答案是指 C API,因为您说这就是您使用的。这可能是一个旧评论,但它仍然对我有用,所以谢谢:)
      猜你喜欢
      • 2018-03-19
      • 2018-02-16
      • 2014-05-11
      • 1970-01-01
      • 2019-09-28
      • 2018-08-12
      • 1970-01-01
      • 2021-07-16
      • 2017-01-15
      相关资源
      最近更新 更多