1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
|
/* Copyright (c) 2020, Dyssol Development Team.
* Copyright (c) 2023, DyssolTEC GmbH.
* All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */
#include "H5Handler.h"
#include "PacketTable.h"
#include "StringFunctions.h"
#include <regex>
namespace
{
const std::string FILE_EXT = "dflw";
const std::string DOT_FILE_EXT = "." + FILE_EXT;
const std::string FILE_EXT_SPEC = "%d";
const std::string FILE_EXT_INIT_REGEX = R"(\[\[([0-9]+)\]\])";
const std::string FILE_EXT_FINAL_REGEX = R"(\[\[(%d)\]\])";
const std::string FILE_EXT_MULT = "[[" + FILE_EXT_SPEC + "]]";
const std::string DOT_FILE_EXT_MULT = "." + FILE_EXT_MULT;
H5::FileAccPropList CreateFileAccPropList(bool _isSingleFile)
{
constexpr hsize_t CHUNK_SIZE = 500;
constexpr hsize_t CACHE_SIZE = 1024 * 1024 * 50; // in bytes
constexpr hsize_t MAX_H5FILE_SIZE = 1024 * 1024 * 2000; // in bytes
H5::FileAccPropList h5AccPropList;
if (!_isSingleFile)
h5AccPropList.setFamily(MAX_H5FILE_SIZE, H5P_DEFAULT);
h5AccPropList.setFcloseDegree(H5F_CLOSE_STRONG);
h5AccPropList.setCache(CHUNK_SIZE, CHUNK_SIZE, CACHE_SIZE, 0.5);
return h5AccPropList;
}
// Returns results of the regex search.
std::smatch FindSuffix(const std::filesystem::path& _str, const std::string_view& _regexStr)
{
const auto& str = _str.string(); // Convert path to string
const std::regex r(_regexStr.begin(), _regexStr.end()); // Compile regex expression
std::smatch m; // Results of regex search
std::regex_search(str, m, r); // Perform regex search
return m;
}
// Replaces _len symbols starting from _pos in _str with a multi-file suffix.
std::filesystem::path ReplaceMultiFileSuffix(const std::filesystem::path& _str, size_t _pos, size_t _len)
{
std::string copy = _str.string();
copy.replace(_pos, _len, FILE_EXT_SPEC); // Replace existing digits with multi-file extension
return copy;
}
// Transforms the file name to the form needed to read from multi-file.
std::filesystem::path MultiFileReadName(const std::filesystem::path& _fileName)
{
if (_fileName.empty())
return {};
// Already in proper multi-file format, i.e., contains [[%d]]
if (FindSuffix(_fileName, FILE_EXT_FINAL_REGEX).size() == 2)
return _fileName;
// Search for a multi-file suffix in the form [[N]], where N is any number
const auto& m = FindSuffix(_fileName, FILE_EXT_INIT_REGEX);
if (m.size() < 2)
return _fileName; // No valid multi-file suffix found
// Replace existing digits with a multi-file suffix %d
return ReplaceMultiFileSuffix(_fileName, m.position(1), m[1].length());
}
// Transforms the file name to the form needed to write to multi-file.
std::filesystem::path MultiFileWriteName(std::filesystem::path _fileName)
{
if (_fileName.empty())
return {};
// Check if the extension is missing or incorrect
if (!StringFunctions::CompareCaseInsensitive(_fileName.extension().string(), DOT_FILE_EXT))
_fileName += DOT_FILE_EXT; // Add proper extension
// Already in proper multi-file format, i.e., contains [[%d]]
if (FindSuffix(_fileName, FILE_EXT_FINAL_REGEX).size() == 2)
return _fileName;
// Search for a multi-file suffix in the form [[N]], where N is any number
const auto& m = FindSuffix(_fileName, FILE_EXT_INIT_REGEX);
// Found valid multi-file suffix
if (m.size() == 2)
return ReplaceMultiFileSuffix(_fileName, m.position(1), m[1].length()); // Replace existing digits with a multi-file suffix %d
std::string copy = _fileName.string();
const size_t dotPos = copy.rfind('.'); // Find a dot before extension
copy.insert(dotPos, DOT_FILE_EXT_MULT); // Insert a multi-file suffix .[[%d]]
return copy;
}
// Converts the file name to a Dyssol format.
std::filesystem::path ConvertFileName(const std::filesystem::path& _fileName, bool _open, bool _isSingleFile)
{
if (_isSingleFile)
return _fileName;
return (_open) ? MultiFileReadName(_fileName) : MultiFileWriteName(_fileName);
}
}
CH5Handler::CH5Handler()
{
H5::Exception::dontPrint();
}
CH5Handler::~CH5Handler()
{
Close();
}
void CH5Handler::Create(const std::filesystem::path& _fileName, bool _isSingleFile /*= true*/)
{
H5::Exception::dontPrint();
OpenH5File(_fileName, false, _isSingleFile);
}
void CH5Handler::Open(const std::filesystem::path& _fileName)
{
H5::Exception::dontPrint();
OpenH5File(_fileName, true, !m_isFileValid);
}
void CH5Handler::Close()
{
if (!m_h5File)
return;
m_h5File->close();
delete m_h5File;
m_h5File = nullptr;
m_isFileValid = false;
}
std::filesystem::path CH5Handler::FileName() const
{
return m_fileName;
}
std::string CH5Handler::CreateGroup(const std::string& _path, const std::string& _groupName) const
{
if (!m_isFileValid)
return "";
const std::string& path = _path + "/" + _groupName;
try
{
m_h5File->createGroup(path);
}
catch (...)
{
return "";
}
return path;
}
std::string CH5Handler::OpenGroup(const std::string& _path, const std::string& _groupName) const
{
if (!m_isFileValid)
return "";
const std::string& path = _path + "/" + _groupName;
try
{
m_h5File->openGroup(path);
}
catch (...)
{
return "";
}
return path;
}
void CH5Handler::WriteAttribute(const std::string& _path, const std::string& _attrName, int _value) const
{
if (!m_isFileValid) return;
H5::DataSpace h5Dataspace(H5S_SCALAR);
H5::Group h5Group(m_h5File->openGroup(_path));
H5::Attribute h5Attribute(h5Group.createAttribute(_attrName, H5::PredType::NATIVE_INT, h5Dataspace, H5::PropList::DEFAULT));
h5Attribute.write(H5::PredType::NATIVE_INT, &_value);
h5Attribute.close();
h5Group.close();
h5Dataspace.close();
}
int CH5Handler::ReadAttribute(const std::string& _path, const std::string& _attrName) const
{
if (!m_isFileValid) return 0;
int attrValue;
try
{
H5::Group h5Group(m_h5File->openGroup(_path));
if (h5Group.getId() == -1) return 0;
H5::Attribute h5Attribute(h5Group.openAttribute(_attrName));
h5Attribute.read(H5::PredType::NATIVE_INT, &attrValue);
h5Attribute.close();
h5Group.close();
}
catch (...)
{
attrValue = 0;
}
return attrValue;
}
void CH5Handler::WriteData(const std::string& _path, const std::string& _dataset, const std::vector<std::vector<double>>& _data) const
{
if (!m_isFileValid) return;
if (_data.empty()) return;
const hsize_t size{ _data.size() };
H5::Group h5Group(m_h5File->openGroup(_path));
H5::DataSpace h5Dataspace(1, &size);
auto itemtype = H5::PredType::NATIVE_DOUBLE;
auto h5Varlentype = H5::VarLenType(&itemtype);
H5::DataSet h5Dataset = h5Group.createDataSet(_dataset, h5Varlentype, h5Dataspace);
std::vector<hvl_t> buffer(size);
for (size_t i = 0; i < size; ++i)
{
buffer[i].len = _data[i].size();
buffer[i].p = !_data[i].empty() ? const_cast<double*>(&_data[i].front()) : nullptr;
}
h5Dataset.write(buffer.data(), h5Varlentype);
h5Dataset.close();
h5Varlentype.close();
h5Dataspace.close();
h5Group.close();
}
void CH5Handler::ReadData(const std::string& _path, const std::string& _dataset, std::vector<std::vector<double>>& _data) const
{
if (!m_isFileValid) return;
try
{
H5::Group h5Group(m_h5File->openGroup(_path));
H5::DataSet h5Dataset = h5Group.openDataSet(_dataset);
H5::DataSpace h5Dataspace = h5Dataset.getSpace();
auto itemtype = H5::PredType::NATIVE_DOUBLE;
auto h5Varlentype = H5::VarLenType(&itemtype);
const auto nRows = static_cast<size_t>(h5Dataspace.getSimpleExtentNpoints());
if (nRows != 0)
{
auto* buffer = new hvl_t[nRows];
h5Dataset.read(buffer, h5Varlentype);
_data.resize(nRows);
for (size_t i = 0; i < nRows; ++i)
{
const size_t nCols = buffer[i].len;
_data[i].resize(buffer[i].len);
auto* pTemp = static_cast<double*>(buffer[i].p);
std::copy(&pTemp[0], &pTemp[nCols], _data[i].begin());
free(buffer[i].p);
}
delete[] buffer;
}
h5Varlentype.close();
h5Dataspace.close();
h5Dataset.close();
h5Group.close();
}
catch (...)
{
ReadDataOld(_path, _dataset, _data);
}
}
void CH5Handler::ReadDataOld(const std::string& _path, const std::string& _dataset, std::vector<std::vector<double>>& _data) const
{
if (!m_isFileValid) return;
try
{
H5::Group h5Group(m_h5File->openGroup(_path));
CH5PacketTable h5PacketTable(h5Group, _dataset);
if (h5PacketTable.getId() == -1)
{
_data.clear();
h5PacketTable.close();
h5Group.close();
return;
}
const auto nLen = static_cast<size_t>(h5PacketTable.GetPacketCount());
if (nLen != 0)
{
auto* buffer = new hvl_t[nLen];
h5PacketTable.GetPackets(0, nLen - 1, buffer);
_data.resize(nLen);
const size_t nDimensions = buffer[0].len;
for (auto& v : _data)
v.resize(nDimensions);
for (size_t i = 0; i < nLen; ++i)
{
auto* pTemp = static_cast<double*>(buffer[i].p);
std::copy(&pTemp[0], &pTemp[nDimensions], _data[i].begin());
}
for (size_t i = 0; i < nLen; ++i)
free(buffer[i].p);
delete[] buffer;
}
h5PacketTable.close();
h5Group.close();
}
catch (...)
{
_data.clear();
}
}
bool CH5Handler::IsValid() const
{
return m_isFileValid;
}
void CH5Handler::WriteValue(const std::string& _path, const std::string& _dataset, size_t _size, const H5::DataType& _type, const void* _value) const
{
if (!m_isFileValid) return;
H5::Group h5Group(m_h5File->openGroup(_path));
H5::DataSpace h5Dataspace(1, &static_cast<const hsize_t&>(_size));
H5::DataSet h5Dataset = h5Group.createDataSet(_dataset, _type, h5Dataspace);
h5Dataset.write(_value, _type);
h5Dataset.close();
h5Dataspace.close();
h5Group.close();
}
size_t CH5Handler::ReadSize(const std::string& _path, const std::string& _dataset) const
{
if (!m_isFileValid) return 0;
try
{
H5::Group h5Group(m_h5File->openGroup(_path));
#if H5_VERSION_GE(1, 10, 1) == true
if (!h5Group.nameExists(_dataset))
return 0;
#endif
H5::DataSet h5Dataset = h5Group.openDataSet(_dataset);
H5::DataSpace h5Dataspace = h5Dataset.getSpace();
const auto elemNum = static_cast<size_t>(h5Dataspace.getSimpleExtentNpoints());
h5Dataset.close();
h5Dataspace.close();
h5Group.close();
return elemNum;
}
catch (...)
{
return 0;
}
}
bool CH5Handler::ReadValue(const std::string& _path, const std::string& _dataset, const H5::DataType& _type, void* _value) const
{
if (!m_isFileValid) return false;
try
{
H5::Group h5Group(m_h5File->openGroup(_path));
H5::DataSet h5Dataset = h5Group.openDataSet(_dataset);
h5Dataset.read(_value, _type);
h5Dataset.close();
h5Group.close();
return true;
}
catch (...)
{
return false;
}
}
void CH5Handler::OpenH5File(const std::filesystem::path& _fileName, bool _isOpen, bool _isSingleFile)
{
Close();
m_fileName = ConvertFileName(_fileName, _isOpen, _isSingleFile);
if (m_h5File || m_fileName.empty()) return;
H5::FileAccPropList h5AccPropList = CreateFileAccPropList(_isSingleFile);
try
{
m_h5File = new H5::H5File(_fileName.string(), _isOpen ? H5F_ACC_RDONLY : H5F_ACC_TRUNC, H5P_DEFAULT, h5AccPropList);
}
catch (...)
{
m_h5File = nullptr;
}
h5AccPropList.close();
if (m_h5File && m_h5File->getId() != -1)
m_isFileValid = true;
}
std::filesystem::path CH5Handler::DisplayFileName(std::filesystem::path _fileName)
{
if (_fileName.empty()) return {};
std::smatch m = FindSuffix(_fileName, FILE_EXT_FINAL_REGEX); // apply a regex search of a multi-file suffix in form [[%d]]
if (m.size() < 2) // no such suffix found
m = FindSuffix(_fileName, FILE_EXT_INIT_REGEX); // apply a regex search of a multi-file suffix in form [[N]], where N is any number
if (m.size() < 2) return _fileName; // no valid multi-file suffix found
const size_t pos = m.position(0); // position of a multi-file suffix
std::string copy = _fileName.string();
copy.erase(m.position(0), m[0].length()); // remove multi-file suffix
if (pos - 1 < copy.size() && copy[pos - 1] == '.') // a dot before multi-file suffix
copy.erase(pos - 1, 1); // remove dot
return copy;
}
|