v1.0.0-rc.31
This commit is contained in:
+93
-6
@@ -177,6 +177,23 @@ size_t HDF5DataType::GetElemSize() const {
|
||||
return H5Tget_size(id);
|
||||
}
|
||||
|
||||
bool HDF5DataType::IsSigned() const {
|
||||
if (IsFloat())
|
||||
return true;
|
||||
else if (IsInteger())
|
||||
return H5Tget_sign(id) == H5T_SGN_2;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool HDF5DataType::IsInteger() const {
|
||||
return H5Tget_class(id) == H5T_INTEGER;
|
||||
}
|
||||
|
||||
bool HDF5DataType::IsFloat() const {
|
||||
return H5Tget_class(id) == H5T_FLOAT;
|
||||
}
|
||||
|
||||
HDF5Dcpl::HDF5Dcpl() : HDF5Id() {
|
||||
id = H5Pcreate(H5P_DATASET_CREATE);
|
||||
}
|
||||
@@ -205,6 +222,14 @@ void HDF5Dcpl::SetFillValue16(int16_t val) {
|
||||
H5Pset_fill_value(id, H5T_NATIVE_INT16, &val);
|
||||
}
|
||||
|
||||
void HDF5Dcpl::SetFillValueU32(uint32_t val) {
|
||||
H5Pset_fill_value(id, H5T_NATIVE_UINT32, &val);
|
||||
}
|
||||
|
||||
void HDF5Dcpl::SetFillValueU16(uint16_t val) {
|
||||
H5Pset_fill_value(id, H5T_NATIVE_UINT16, &val);
|
||||
}
|
||||
|
||||
void HDF5Dcpl::SetVirtual(const std::string &path, const std::string &dataset, const HDF5DataSpace &src_dataspace, const HDF5DataSpace &dest_dataspace) {
|
||||
std::string filename = ExtractFilename(path);
|
||||
if (H5Pset_virtual(id, dest_dataspace.GetID(), filename.c_str(), dataset.c_str(), src_dataspace.GetID()) < 0)
|
||||
@@ -357,6 +382,12 @@ HDF5Object & HDF5Object::HardLink(const std::string &source, const std::string &
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::vector<hsize_t> HDF5Object::GetDimension(const std::string &name) {
|
||||
HDF5DataSet dataset(*this, name);
|
||||
HDF5DataSpace dscp(dataset);
|
||||
return dscp.GetDimensions();
|
||||
}
|
||||
|
||||
std::unique_ptr<HDF5DataSet> HDF5Object::SaveScalar(const std::string &name, const std::string &val) {
|
||||
return SaveScalar(name, val.c_str());
|
||||
}
|
||||
@@ -398,8 +429,12 @@ HDF5Group::~HDF5Group() {
|
||||
H5Gclose(id);
|
||||
}
|
||||
|
||||
HDF5File::HDF5File(const std::string& filename) : HDF5Object() {
|
||||
id = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
|
||||
HDF5File::HDF5File(const std::string& filename, bool v1_10) : HDF5Object() {
|
||||
HDF5Fapl fapl;
|
||||
|
||||
if (v1_10)
|
||||
fapl.SetVersionTo1p10orNewer();
|
||||
id = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, fapl.GetID());
|
||||
if (id < 0)
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot open/create data HDF5 file " + filename);
|
||||
}
|
||||
@@ -447,10 +482,10 @@ HDF5DataSet::HDF5DataSet(const HDF5Object &parent, const std::string &name, cons
|
||||
HDF5DataSet::HDF5DataSet(const HDF5Object &parent, const std::string &name) : ndim(0) {
|
||||
id = H5Dopen2(parent.GetID(), name.c_str(), H5P_DEFAULT);
|
||||
|
||||
ndim = HDF5DataSpace(*this).GetNumOfDimensions();
|
||||
|
||||
if (id < 0)
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot open HDF5 dataset");
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot open HDF5 dataset " + name);
|
||||
|
||||
ndim = HDF5DataSpace(*this).GetNumOfDimensions();
|
||||
}
|
||||
|
||||
void HDF5DataSet::SetExtent(const std::vector<hsize_t> &dims) {
|
||||
@@ -496,7 +531,7 @@ std::string HDF5DataSet::ReadString() const {
|
||||
if (H5Dread(id, file_data_type.GetID(), H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp) < 0)
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Dataset read unsuccessful");
|
||||
|
||||
return std::string(tmp);
|
||||
return {tmp};
|
||||
}
|
||||
|
||||
HDF5DataSet::~HDF5DataSet() {
|
||||
@@ -556,4 +591,56 @@ std::string ExtractFilename(const std::string& str) {
|
||||
std::filesystem::path path(str);
|
||||
// if (std::filesystem::is_regular_file(path))
|
||||
return path.filename();
|
||||
}
|
||||
|
||||
float HDF5Object::GetFloat(const std::string &name) const {
|
||||
HDF5DataSet dataset(*this, name);
|
||||
return dataset.ReadScalar<float>();
|
||||
}
|
||||
|
||||
int64_t HDF5Object::GetInt(const std::string &name) const {
|
||||
HDF5DataSet dataset(*this, name);
|
||||
return dataset.ReadScalar<int64_t>();
|
||||
}
|
||||
|
||||
std::optional<float> HDF5Object::GetOptFloat(const std::string &name) const {
|
||||
if (Exists(name))
|
||||
return GetFloat(name);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<int64_t> HDF5Object::GetOptInt(const std::string &name) const {
|
||||
if (Exists(name))
|
||||
return GetInt(name);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string HDF5Object::GetString(const std::string &name) const {
|
||||
if (Exists(name)) {
|
||||
HDF5DataSet dataset(*this, name);
|
||||
return dataset.ReadString();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
bool HDF5Object::Exists(const std::string &name) const {
|
||||
return H5Lexists(GetID(), name.c_str(), H5P_DEFAULT) > 0;
|
||||
}
|
||||
|
||||
herr_t func(hid_t group, const char *name, const H5L_info2_t *info, void *op_data) {
|
||||
auto ret = reinterpret_cast<std::vector<std::string> *>(op_data);
|
||||
ret->emplace_back(name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::vector<std::string> HDF5Object::FindLeafs(const std::string &name) const {
|
||||
hid_t gid = H5Gopen(id, name.c_str(), H5P_DEFAULT);
|
||||
if (gid < 0)
|
||||
throw JFJochException(JFJochExceptionCategory::HDF5, "Cannot open HDF5 group " + name);
|
||||
std::vector<std::string> ret;
|
||||
hsize_t idx = 0;
|
||||
H5Literate(gid, H5_INDEX_NAME, H5_ITER_INC, &idx, func, &ret);
|
||||
|
||||
H5Gclose(gid);
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user