v1.0.0-rc.60
This commit is contained in:
@@ -400,6 +400,149 @@ HDF5Object & HDF5Object::Attr(const std::string &name, const std::vector<double>
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::vector<double> HDF5Object::ReadAttrVec(const std::string &name) {
|
||||
hid_t attr_id = H5Aopen(id, name.c_str(), H5P_DEFAULT);
|
||||
if (attr_id < 0)
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot open attribute " + name);
|
||||
|
||||
hid_t space_id = H5Aget_space(attr_id);
|
||||
if (space_id < 0) {
|
||||
H5Aclose(attr_id);
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot get dataspace for attribute " + name);
|
||||
}
|
||||
|
||||
int ndims = H5Sget_simple_extent_ndims(space_id);
|
||||
if (ndims != 1) {
|
||||
H5Sclose(space_id);
|
||||
H5Aclose(attr_id);
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Attribute " + name + " is not a vector");
|
||||
}
|
||||
|
||||
hsize_t dims[1];
|
||||
H5Sget_simple_extent_dims(space_id, dims, NULL);
|
||||
std::vector<double> ret(dims[0]);
|
||||
|
||||
if (H5Aread(attr_id, H5T_NATIVE_DOUBLE, ret.data()) < 0) {
|
||||
H5Sclose(space_id);
|
||||
H5Aclose(attr_id);
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot read attribute " + name);
|
||||
}
|
||||
|
||||
H5Sclose(space_id);
|
||||
H5Aclose(attr_id);
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string HDF5Object::ReadAttrStr(const std::string &name) {
|
||||
hid_t attr_id = H5Aopen(id, name.c_str(), H5P_DEFAULT);
|
||||
if (attr_id < 0)
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot open attribute " + name);
|
||||
|
||||
// Get attribute dataspace
|
||||
hid_t space_id = H5Aget_space(attr_id);
|
||||
if (space_id < 0) {
|
||||
H5Aclose(attr_id);
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot get dataspace for attribute " + name);
|
||||
}
|
||||
|
||||
// Check if attribute is scalar
|
||||
H5S_class_t space_type = H5Sget_simple_extent_type(space_id);
|
||||
if (space_type != H5S_SCALAR) {
|
||||
H5Sclose(space_id);
|
||||
H5Aclose(attr_id);
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Attribute " + name + " is not a scalar");
|
||||
}
|
||||
H5Sclose(space_id);
|
||||
|
||||
// Get attribute type
|
||||
hid_t type_id = H5Aget_type(attr_id);
|
||||
if (type_id < 0) {
|
||||
H5Aclose(attr_id);
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot get type for attribute " + name);
|
||||
}
|
||||
|
||||
// Check if attribute is a string type
|
||||
H5T_class_t type_class = H5Tget_class(type_id);
|
||||
if (type_class != H5T_STRING) {
|
||||
H5Tclose(type_id);
|
||||
H5Aclose(attr_id);
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Attribute " + name + " is not a string");
|
||||
}
|
||||
|
||||
// Get string size and read it
|
||||
size_t size = H5Tget_size(type_id);
|
||||
std::vector<char> buffer(size + 1, '\0'); // +1 for null terminator to be safe
|
||||
|
||||
if (H5Aread(attr_id, type_id, buffer.data()) < 0) {
|
||||
H5Tclose(type_id);
|
||||
H5Aclose(attr_id);
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot read attribute " + name);
|
||||
}
|
||||
|
||||
H5Tclose(type_id);
|
||||
H5Aclose(attr_id);
|
||||
return {buffer.data()};
|
||||
}
|
||||
|
||||
|
||||
double HDF5Object::ReadAttrDouble(const std::string &name) {
|
||||
hid_t attr_id = H5Aopen(id, name.c_str(), H5P_DEFAULT);
|
||||
if (attr_id < 0)
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot open attribute " + name);
|
||||
|
||||
hid_t space_id = H5Aget_space(attr_id);
|
||||
if (space_id < 0) {
|
||||
H5Aclose(attr_id);
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot get dataspace for attribute " + name);
|
||||
}
|
||||
|
||||
H5S_class_t space_type = H5Sget_simple_extent_type(space_id);
|
||||
if (space_type != H5S_SCALAR) {
|
||||
H5Sclose(space_id);
|
||||
H5Aclose(attr_id);
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Attribute " + name + " is not a scalar");
|
||||
}
|
||||
H5Sclose(space_id);
|
||||
|
||||
double value;
|
||||
if (H5Aread(attr_id, H5T_NATIVE_DOUBLE, &value) < 0) {
|
||||
H5Aclose(attr_id);
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot read attribute " + name);
|
||||
}
|
||||
|
||||
H5Aclose(attr_id);
|
||||
return value;
|
||||
}
|
||||
|
||||
int64_t HDF5Object::ReadAttrInt(const std::string &name) {
|
||||
hid_t attr_id = H5Aopen(id, name.c_str(), H5P_DEFAULT);
|
||||
if (attr_id < 0)
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot open attribute " + name);
|
||||
|
||||
hid_t space_id = H5Aget_space(attr_id);
|
||||
if (space_id < 0) {
|
||||
H5Aclose(attr_id);
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot get dataspace for attribute " + name);
|
||||
}
|
||||
|
||||
H5S_class_t space_type = H5Sget_simple_extent_type(space_id);
|
||||
if (space_type != H5S_SCALAR) {
|
||||
H5Sclose(space_id);
|
||||
H5Aclose(attr_id);
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Attribute " + name + " is not a scalar");
|
||||
}
|
||||
H5Sclose(space_id);
|
||||
|
||||
int64_t value;
|
||||
if (H5Aread(attr_id, H5T_NATIVE_INT64, &value) < 0) {
|
||||
H5Aclose(attr_id);
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot read attribute " + name);
|
||||
}
|
||||
|
||||
H5Aclose(attr_id);
|
||||
return value;
|
||||
}
|
||||
|
||||
HDF5Object & HDF5Object::Units(const std::string &val) {
|
||||
if (!val.empty()) Attr("units", val);
|
||||
return *this;
|
||||
@@ -496,6 +639,7 @@ HDF5Group::~HDF5Group() {
|
||||
H5Gclose(id);
|
||||
}
|
||||
|
||||
|
||||
HDF5File::HDF5File(const std::string& filename, bool v1_10) : HDF5Object() {
|
||||
HDF5Fapl fapl;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user