确定 HDF5 路径的对象类型
Determine the object type of a HDF5 path
我有一个路径名(一个字符串)和一个打开的 HDF5 文件。我已经使用 H5Lexists
来确保存在具有该名称的对象。如何确定对象类型是什么?例如数据集、组等
// open the file
hid_t fapl = H5Pcreate(H5P_FILE_ACCESS);
H5Pset_fclose_degree(fapl, H5F_CLOSE_STRONG);
hid_t hid = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, fapl);
// ensure the object exists
const string path = "/some/path/to/an/object";
if (H5Lexists(m_hid, path.c_str(), H5P_DEFAULT) < 0) throw runtime_error();
// determine the object type
// TODO
if (dataset) {
hid_t dataset = H5Dopen(hid, path.c_str(), H5P_DEFAULT);
// do something
}
else if (group) {
hid_t group = H5Gopen(hid, path.c_str(), H5P_DEFAULT);
// do something
}
H5Fclose(hid);
我尽量不使用基于 C++ class 的接口,因为它已被添加到遗留代码中。我尝试只打开数据集以查看是否 dataset < 0
以确定它是否是数据集,但是,HDF5 库向 stderr 抛出大量警告,所以我想以正确的方式进行操作。
用H5Oopen
if you don't know the type in advance and use H5Iget_type
判断。
hid_t object = H5Oopen(hid, path.c_str(), H5P_DEFAULT);
H5I_type_t h5type = H5Iget_type(object);
if (h5type == H5I_BADID) {
... // error handling stuff
} else if (h5type == H5I_DATASET) {
... // dataset stuff
} else if (h5type == H5I_GROUP) {
... // group stuff
} else { // other stuff maybe
}
H5Oclose(hid);
如果您没有绑定到特定的库,您可能想看看 HDFql,因为它大大减轻了您对 HDF5 的 low-level 细节的负担。使用HDFql,你的use-case在C中可以如下解决(原则上不会产生错误信息):
int type;
if (hdfql_execute("SHOW TYPE /some/path/to/an/object") == HDFQL_SUCCESS)
{
hdfql_cursor_next(NULL);
type = *hdfql_cursor_get_int(NULL);
if (type == HDFQL_GROUP)
{
printf("Object is a group\n");
}
else if (type == HDFQL_DATASET)
{
printf("Object is a dataset\n");
}
else if (type == HDFQL_ATTRIBUTE)
{
printf("Object is an attribute\n");
}
else if (type == HDFQL_SOFT_LINK)
{
printf("Object is a soft link\n");
}
else if (type == HDFQL_EXTERNAL_LINK)
{
printf("Object is an external link\n");
}
else if (type == HDFQL_SOFT_LINK | HDFQL_GROUP)
{
printf("Object is a soft link pointing to a group\n");
}
else if (type == HDFQL_SOFT_LINK | HDFQL_DATASET)
{
printf("Object is a soft link pointing to a dataset\n");
}
else if (type == HDFQL_EXTERNAL_LINK | HDFQL_GROUP)
{
printf("Object is an external link pointing to a group\n");
}
else if (type == HDFQL_EXTERNAL_LINK | HDFQL_DATASET)
{
printf("Object is an external link pointing to a dataset\n");
}
else
{
printf("Object is of unknown type!\n");
}
}
else
{
printf("Object was not found!");
}
图书馆的 H5O
部分就是答案。此外,H5Lexists
也不是确定对象是否存在的正确方法,因此我也对其进行了更新。
// open the file
hid_t fapl = H5Pcreate(H5P_FILE_ACCESS);
H5Pset_fclose_degree(fapl, H5F_CLOSE_STRONG);
hid_t hid = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, fapl);
// ensure the object exists
const string path = "/some/path/to/an/object";
if (H5Oexists_by_name(hid, path.c_str(), H5P_DEFAULT) <= 0) throw runtime_error();
// determine the object type
H5O_info_t info;
H5Oget_info_by_name(m_hid, parent.c_str(), &info, H5P_DEFAULT);
if (info.type == H5O_TYPE_DATASET) {
hid_t dataset = H5Dopen(hid, path.c_str(), H5P_DEFAULT);
// do something
}
else if (info.type == H5O_TYPE_GROUP) {
hid_t group = H5Gopen(hid, path.c_str(), H5P_DEFAULT);
// do something
}
H5Fclose(hid);
我有一个路径名(一个字符串)和一个打开的 HDF5 文件。我已经使用 H5Lexists
来确保存在具有该名称的对象。如何确定对象类型是什么?例如数据集、组等
// open the file
hid_t fapl = H5Pcreate(H5P_FILE_ACCESS);
H5Pset_fclose_degree(fapl, H5F_CLOSE_STRONG);
hid_t hid = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, fapl);
// ensure the object exists
const string path = "/some/path/to/an/object";
if (H5Lexists(m_hid, path.c_str(), H5P_DEFAULT) < 0) throw runtime_error();
// determine the object type
// TODO
if (dataset) {
hid_t dataset = H5Dopen(hid, path.c_str(), H5P_DEFAULT);
// do something
}
else if (group) {
hid_t group = H5Gopen(hid, path.c_str(), H5P_DEFAULT);
// do something
}
H5Fclose(hid);
我尽量不使用基于 C++ class 的接口,因为它已被添加到遗留代码中。我尝试只打开数据集以查看是否 dataset < 0
以确定它是否是数据集,但是,HDF5 库向 stderr 抛出大量警告,所以我想以正确的方式进行操作。
用H5Oopen
if you don't know the type in advance and use H5Iget_type
判断。
hid_t object = H5Oopen(hid, path.c_str(), H5P_DEFAULT);
H5I_type_t h5type = H5Iget_type(object);
if (h5type == H5I_BADID) {
... // error handling stuff
} else if (h5type == H5I_DATASET) {
... // dataset stuff
} else if (h5type == H5I_GROUP) {
... // group stuff
} else { // other stuff maybe
}
H5Oclose(hid);
如果您没有绑定到特定的库,您可能想看看 HDFql,因为它大大减轻了您对 HDF5 的 low-level 细节的负担。使用HDFql,你的use-case在C中可以如下解决(原则上不会产生错误信息):
int type;
if (hdfql_execute("SHOW TYPE /some/path/to/an/object") == HDFQL_SUCCESS)
{
hdfql_cursor_next(NULL);
type = *hdfql_cursor_get_int(NULL);
if (type == HDFQL_GROUP)
{
printf("Object is a group\n");
}
else if (type == HDFQL_DATASET)
{
printf("Object is a dataset\n");
}
else if (type == HDFQL_ATTRIBUTE)
{
printf("Object is an attribute\n");
}
else if (type == HDFQL_SOFT_LINK)
{
printf("Object is a soft link\n");
}
else if (type == HDFQL_EXTERNAL_LINK)
{
printf("Object is an external link\n");
}
else if (type == HDFQL_SOFT_LINK | HDFQL_GROUP)
{
printf("Object is a soft link pointing to a group\n");
}
else if (type == HDFQL_SOFT_LINK | HDFQL_DATASET)
{
printf("Object is a soft link pointing to a dataset\n");
}
else if (type == HDFQL_EXTERNAL_LINK | HDFQL_GROUP)
{
printf("Object is an external link pointing to a group\n");
}
else if (type == HDFQL_EXTERNAL_LINK | HDFQL_DATASET)
{
printf("Object is an external link pointing to a dataset\n");
}
else
{
printf("Object is of unknown type!\n");
}
}
else
{
printf("Object was not found!");
}
图书馆的 H5O
部分就是答案。此外,H5Lexists
也不是确定对象是否存在的正确方法,因此我也对其进行了更新。
// open the file
hid_t fapl = H5Pcreate(H5P_FILE_ACCESS);
H5Pset_fclose_degree(fapl, H5F_CLOSE_STRONG);
hid_t hid = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, fapl);
// ensure the object exists
const string path = "/some/path/to/an/object";
if (H5Oexists_by_name(hid, path.c_str(), H5P_DEFAULT) <= 0) throw runtime_error();
// determine the object type
H5O_info_t info;
H5Oget_info_by_name(m_hid, parent.c_str(), &info, H5P_DEFAULT);
if (info.type == H5O_TYPE_DATASET) {
hid_t dataset = H5Dopen(hid, path.c_str(), H5P_DEFAULT);
// do something
}
else if (info.type == H5O_TYPE_GROUP) {
hid_t group = H5Gopen(hid, path.c_str(), H5P_DEFAULT);
// do something
}
H5Fclose(hid);