diff --git a/h5pp/include/h5pp/details/h5ppFile.h b/h5pp/include/h5pp/details/h5ppFile.h index 8cd46a46..aeadcc6d 100644 --- a/h5pp/include/h5pp/details/h5ppFile.h +++ b/h5pp/include/h5pp/details/h5ppFile.h @@ -874,16 +874,16 @@ namespace h5pp { hsize_t numRecordsToCopy, std::string_view tgtTablePath, hsize_t tgtStartIdx, - const OptDimsType & chunkDims = std::nullopt, - const std::optional compressionLevel = std::nullopt) { + const OptDimsType & chunkDims = std::nullopt, + const std::optional compression = std::nullopt) { Options options; options.linkPath = h5pp::util::safe_str(tgtTablePath); options.dsetDimsChunk = chunkDims; - options.compression = compressionLevel; + options.compression = getCompressionLevel(compression); auto tgtInfo = h5pp::scan::readTableInfo(openFileHandle(), options, plists); if(not tgtInfo.tableExists or not tgtInfo.tableExists.value()) tgtInfo = createTable( - srcInfo.tableType.value(), tgtInfo.tablePath.value(), srcInfo.tableTitle.value(), chunkDims, compressionLevel); + srcInfo.tableType.value(), tgtInfo.tablePath.value(), srcInfo.tableTitle.value(), chunkDims, compression); copyTableRecords(srcInfo, srcStartIdx, numRecordsToCopy, tgtInfo, tgtStartIdx); return tgtInfo; @@ -896,11 +896,11 @@ namespace h5pp { std::string_view tgtTablePath, hsize_t tgtStartIdx, const std::optional chunkDims = std::nullopt, - const std::optional compressionLevel = std::nullopt) { + const std::optional compression = std::nullopt) { Options options; options.linkPath = h5pp::util::safe_str(srcTablePath); auto srcInfo = h5pp::scan::readTableInfo(srcLocation, options, plists); - return copyTableRecords(srcInfo, srcTableSelection, tgtTablePath, tgtStartIdx, chunkDims, compressionLevel); + return copyTableRecords(srcInfo, srcTableSelection, tgtTablePath, tgtStartIdx, chunkDims, compression); } template> diff --git a/h5pp/include/h5pp/details/h5ppHdf5.h b/h5pp/include/h5pp/details/h5ppHdf5.h index 429967e0..b1170e2e 100644 --- a/h5pp/include/h5pp/details/h5ppHdf5.h +++ b/h5pp/include/h5pp/details/h5ppHdf5.h @@ -118,6 +118,21 @@ namespace h5pp::hdf5 { return getChunkDimensions(dcpl); } + [[nodiscard]] inline int getCompressionLevel(const hid::h5p &dsetCreatePropertyList) { + auto nfilter = H5Pget_nfilters(dsetCreatePropertyList); + H5Z_filter_t filter = H5Z_FILTER_NONE; + std::array cdval = {0}; + std::array cdelm = {0}; + for(int idx=0; idx < nfilter;idx++) { + constexpr size_t size = 10; + filter = H5Pget_filter(dsetCreatePropertyList, idx, nullptr, cdelm.data(), cdval.data(), 0, nullptr, nullptr); + if(filter != H5Z_FILTER_DEFLATE) continue; + H5Pget_filter_by_id(dsetCreatePropertyList, filter, nullptr, cdelm.data(), cdval.data(), 0, nullptr, nullptr); + } + return cdval[0]; + } + + [[nodiscard]] inline std::optional> getMaxDimensions(const hid::h5s &space, H5D_layout_t layout) { if(layout != H5D_CHUNKED) return std::nullopt; if(H5Sget_simple_extent_type(space) != H5S_SIMPLE) return std::nullopt; diff --git a/h5pp/include/h5pp/details/h5ppScan.h b/h5pp/include/h5pp/details/h5ppScan.h index b644619c..14a6e0e4 100644 --- a/h5pp/include/h5pp/details/h5ppScan.h +++ b/h5pp/include/h5pp/details/h5ppScan.h @@ -59,7 +59,10 @@ namespace h5pp::scan { else info.resizeMode = h5pp::ResizeMode::RESIZE_TO_FIT; } - + if(not info.compression){ + hid::h5p plist = H5Dget_create_plist(info.h5Dset.value()); + info.compression = h5pp::hdf5::getCompressionLevel(plist); + } // Get c++ properties if(not info.cppTypeIndex or not info.cppTypeName or not info.cppTypeSize) std::tie(info.cppTypeIndex, info.cppTypeName, info.cppTypeSize) = h5pp::hdf5::getCppType(info.h5Type.value()); @@ -534,6 +537,11 @@ namespace h5pp::scan { if(chunkVec and not chunkVec->empty()) info.chunkSize = chunkVec.value()[0]; } + if(not info.compressionLevel){ + hid::h5p plist = H5Dget_create_plist(info.tableDset.value()); + info.compressionLevel = h5pp::hdf5::getCompressionLevel(plist); + } + if(not info.cppTypeIndex or not info.cppTypeName or not info.cppTypeSize) { // Get c++ type information info.cppTypeIndex = std::vector(); @@ -569,7 +577,7 @@ namespace h5pp::scan { if(options.dsetDimsChunk and not options.dsetDimsChunk->empty()) info.chunkSize = options.dsetDimsChunk.value()[0]; if(not info.chunkSize) info.chunkSize = h5pp::util::getChunkDimensions(info.recordBytes.value(), {1}, std::nullopt, H5D_layout_t::H5D_CHUNKED).value()[0]; - if(not info.compressionLevel) info.compressionLevel = h5pp::hdf5::getValidCompressionLevel(info.compressionLevel); + if(not info.compressionLevel) info.compressionLevel = h5pp::hdf5::getValidCompressionLevel(); info.fieldTypes = std::vector(); info.fieldOffsets = std::vector(); diff --git a/tests/test-readWriteTables.cpp b/tests/test-readWriteTables.cpp index 5c38f387..2b87f855 100644 --- a/tests/test-readWriteTables.cpp +++ b/tests/test-readWriteTables.cpp @@ -29,6 +29,7 @@ h5pp::hid::h5t MY_HDF5_PARTICLE_TYPE; TEST_CASE("Test reading columns from table", "[Table fields]") { SECTION("Initialize file") { + file.setCompressionLevel(6); // Create a type for the char array from the template H5T_C_S1 // The template describes a string with a single char. // Set the size with H5Tset_size. @@ -52,7 +53,7 @@ TEST_CASE("Test reading columns from table", "[Table fields]") { } SECTION("Create table") { - auto tableInfo = file.createTable(MY_HDF5_PARTICLE_TYPE, "somegroup/particleTable", "particleTable"); + auto tableInfo = file.createTable(MY_HDF5_PARTICLE_TYPE, "somegroup/particleTable", "particleTable",std::nullopt,6); CHECK(tableInfo.tableTitle.value() == "particleTable"); CHECK(tableInfo.numRecords.value() == 0); CHECK(tableInfo.recordBytes.value() == sizeof(Particle));