【问题标题】:HDF5 Attribute unsigned long long valueHDF5 属性 unsigned long long 值
【发布时间】:2015-01-30 09:02:18
【问题描述】:

当我尝试将 unsigned long long 属性添加到数据集时,会添加属性但不会添加值。对整数使用类似的方法似乎可以工作。

我正在使用 HDF 视图来查看属性。显示属性名称,但对于 unsigned long long 属性,值不可见

代码如下:

    herr_t Result;

//Open the file
hid_t DataFile = H5Fopen(FileName, H5F_ACC_RDWR, H5P_DEFAULT);

//Open the dataset
hid_t DataSet = H5Dopen2(DataFile, "/Summary", H5P_DEFAULT);

//Create the data space for the attribute.
hsize_t AttributeDims = 1;
hid_t AttributeDataSpace = H5Screate_simple(1, &AttributeDims , NULL);
hid_t Attribute;

//Attribute 1: Fail to write a long long attribute
Attribute = H5Acreate2 (DataSet, "LongAttribute",  H5T_STD_U64BE, AttributeDataSpace, H5P_DEFAULT, H5P_DEFAULT);
if (Attribute < 0) {
    fprintf(stdout, "Failed to add the unsigned long long attribute to the file %s.", FileName);
    return false;
}

//Write the attribute data
unsigned long long* ULLAttribute = (unsigned long long*) malloc(sizeof(unsigned long long) * 1);
ULLAttribute[0] = (unsigned long long) 4;
Result = H5Awrite(Attribute, H5T_NATIVE_ULLONG, ULLAttribute);
if (Result < 0) {
    fprintf(stdout, "Failed to write the unsigned long long attribute to the file %s.", FileName);
    return false;
}

//Attribute 2: Succesfully Write a integer attribute
Attribute = H5Acreate2 (DataSet, "IntAttribute",  H5T_STD_I32BE, AttributeDataSpace, H5P_DEFAULT, H5P_DEFAULT);
if (Attribute < 0) {
    fprintf(stdout, "Failed to create the attribute for the file %s.", FileName);
    return false;
}

//Write the attribute data
int32_t* IAttribute = (int32_t*) malloc(sizeof(int32_t) * 1);
IAttribute[0] = (int32_t) 4;
Result = H5Awrite(Attribute,  H5T_NATIVE_INT, IAttribute);
if (Result < 0) {
    fprintf(stdout, "Failed to add the integer attribute to the file %s.", FileName);
    return false;
}

//Close the attribute, Dataset and DataFile
Result = H5Aclose(Attribute);
Result = H5Dclose(DataSet);
Result = H5Fclose(DataFile);

代码执行时不显示错误信息,但查看HDF5文件时,属性“IntAttribute”和“LongAttribute”都可见,但LongAttribute没有值。

HFView 2.9,Fedora 20 on intel 64。

挑选一些蒂莫西的问题 写: 你为什么要为属性创建一个简单的数据空间? 我正在咀嚼将模型参数存储为属性,有点像键值对。许多模型参数都是简单的标量值。

写: 按照同样的思路,您为什么要为属性编写一个数组? 我编辑了一个示例,该示例在一个数组中存储了 2 个值。我从您的示例中看到,您已经对空间进行了 malloc() 处理,我将从现在开始使用它,因为它看起来更清晰。

写: 你在 Intel 64 上,但你想写大端? 是的:这仍然让我感到困惑:H5T_STD_I32BE 和 H5T_STD_I32LE 都可以成功工作,但是 H5T_STD_U64BE 和 H5T_STD_U64LE 在 HDFView 中都没有显示值。我猜在 HDF5 库的某个地方,它正在检查大端与小端并相应地处理值,而不管参数如何。稍后我会尽量避免使用 Postgresql 二进制数来绊倒这个“特性”,这些二进制数总是大端值。

问题似乎出在 HDFView 中,它仍然没有在 Timothy 的代码或我的代码生成的 ull.h5 文件中显示 unsigned long long :

我正在使用适用于 Linux 的 HDFView 2.9。正如 Timothy 提到的,这在 HDFView 2.10 中有效,同时我将使用 h5dump。

【问题讨论】:

  • 有趣的是 HDFView 2.9 没有显示它。 Postgresql 仅以大端(又名网络字节顺序)传输数字。在与 Postgresql 交谈时,您真的应该使用 xdr()ntohl() 和朋友。为了抽象出您的应用程序不需要知道它正在运行的字节序这一事实。祝你好运!

标签: c hdf5


【解决方案1】:

几个问题,应该没问题,但我只是好奇。

  1. 为什么要为属性创建一个简单的数据空间?
  2. 按照同样的思路,你为什么要为属性写一个数组?
  3. 您使用的是 Intel 64,但您想编写大端序?

这是一个使用标量数据空间编写属性的简单示例:

#include <stdio.h>
#include <stdlib.h>
#include <hdf5.h>

int
main(int argc, char **argv)
{
        unsigned long long *ull = NULL;
        hid_t f_id = {0};
        hid_t d_id = {0};
        hid_t s_id = {0};
        hid_t a_id = {0};
        hid_t as_id = {0};
        hsize_t dims[2] = {2, 2};
        herr_t status = {0};

        f_id = H5Fcreate("ull.h5",H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
        s_id = H5Screate_simple(2, dims, NULL);
        d_id = H5Dcreate(f_id, "/data", H5T_STD_I32BE, s_id,
                         H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

        as_id = H5Screate(H5S_SCALAR);
        a_id = H5Acreate(d_id, "unsigned long long", H5T_STD_U64LE,
                         as_id, H5P_DEFAULT, H5P_DEFAULT);

        ull = malloc(sizeof(unsigned long long));
        *ull = 123;
        status = H5Awrite(a_id, H5T_NATIVE_ULLONG, ull);

        status = H5Aclose(a_id);
        status = H5Dclose(d_id);
        status = H5Sclose(s_id);
        status = H5Fclose(f_id);

        return(EXIT_SUCCESS);
}

编译运行时:

h5pcc -o test test.c && ./test && h5dump ull.h5

我觉得很好:

HDF5 "ull.h5" {
GROUP "/" {
   DATASET "data" {
      DATATYPE  H5T_STD_I32BE
      DATASPACE  SIMPLE { ( 2, 2 ) / ( 2, 2 ) }
      DATA {
      (0,0): 0, 0,
      (1,0): 0, 0
      }
      ATTRIBUTE "unsigned long long" {
         DATATYPE  H5T_STD_U64LE
         DATASPACE  SCALAR
         DATA {
         (0): 1234
         }
      }
   }
}
}

当然,如果我将标量属性数据空间更改为简单的数据空间,它仍然可以工作:

as_id = H5Screate_simple(1, adims, NULL);
a_id = H5Acreate(d_id, "unsigned long long", H5T_STD_U64LE,
                 as_id, H5P_DEFAULT, H5P_DEFAULT);
ull = malloc(sizeof(unsigned long long));
*ull = 123;

我们得到:

  ATTRIBUTE "unsigned long long" {
     DATATYPE  H5T_STD_U64LE
     DATASPACE  SIMPLE { ( 1 ) / ( 1 ) }
     DATA {
     (0): 123
     }

经过一个冗长的例子来说明如何做到这一点。查看您的代码,我真的找不到您的错误。事实上,在空的 HDF5 文件上使用您的代码是可行的:

localhost ~$ h5dump ull.h5
HDF5 "ull.h5" {
GROUP "/" {
   DATASET "Summary" {
      DATATYPE  H5T_STD_I32BE
      DATASPACE  SIMPLE { ( 2, 2 ) / ( 2, 2 ) }
      DATA {
      (0,0): 0, 0,
      (1,0): 0, 0
      }
   }
}
}
localhost ~$ ./foo
localhost ~$ h5dump ull.h5
HDF5 "ull.h5" {
GROUP "/" {
   DATASET "Summary" {
      DATATYPE  H5T_STD_I32BE
      DATASPACE  SIMPLE { ( 2, 2 ) / ( 2, 2 ) }
      DATA {
      (0,0): 0, 0,
      (1,0): 0, 0
      }
      ATTRIBUTE "IntAttribute" {
         DATATYPE  H5T_STD_I32BE
         DATASPACE  SIMPLE { ( 1 ) / ( 1 ) }
         DATA {
         (0): 4
         }
      }
      ATTRIBUTE "LongAttribute" {
         DATATYPE  H5T_STD_U64BE
         DATASPACE  SIMPLE { ( 1 ) / ( 1 ) }
         DATA {
         (0): 4
         }
      }
   }
}
}

你能检查(并发布)h5dump 给你什么吗?也许这只是使用HDFView的问题?


更新

我刚刚使用 HDFView(2.10 版)查看了该文件,看起来还不错。

你能确认/重新创建你的错误吗?

【讨论】:

  • 感谢您指出 h5dump:显示正确写入文件的值,正如您所观察到的,即您解决了我的问题:谢谢。
猜你喜欢
  • 2016-07-29
  • 1970-01-01
  • 2012-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多