Skip to content

Commit

Permalink
HDF5 multidim: implement GDALMDArray::GetBlockSize() and GetStructura…
Browse files Browse the repository at this point in the history
…lInfo()
  • Loading branch information
rouault committed Apr 18, 2024
1 parent 88b0dd8 commit 9f69b4c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
Binary file added autotest/gdrivers/data/hdf5/deflate.h5
Binary file not shown.
13 changes: 13 additions & 0 deletions autotest/gdrivers/hdf5multidim.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,3 +827,16 @@ def test_hdf5_multidim_eos_swath_no_explicit_dimension_map():
coordinates[1].GetFullName()
== "/HDFEOS/SWATHS/MySwath/Geolocation Fields/Latitude"
)


###############################################################################
# Test GetBlockSize() and GetStructuralInfo()


def test_hdf5_multidim_block_size_structural_info():

ds = gdal.OpenEx("data/hdf5/deflate.h5", gdal.OF_MULTIDIM_RASTER)
rg = ds.GetRootGroup()
var = rg.OpenMDArray("Band1")
assert var.GetBlockSize() == [1, 2]
assert var.GetStructuralInfo() == {"COMPRESSION": "DEFLATE", "FILTER": "SHUFFLE"}
84 changes: 84 additions & 0 deletions frmts/hdf5/hdf5multidim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ class HDF5Array final : public GDALMDArray
mutable bool m_bHasDimensionLabels = false;
std::shared_ptr<OGRSpatialReference> m_poSRS{};
haddr_t m_nOffset;
mutable CPLStringList m_aosStructuralInfo{};

HDF5Array(const std::string &osParentName, const std::string &osName,
const std::shared_ptr<HDF5SharedResources> &poShared,
Expand Down Expand Up @@ -365,6 +366,10 @@ class HDF5Array final : public GDALMDArray
std::vector<std::shared_ptr<GDALAttribute>>
GetAttributes(CSLConstList papszOptions = nullptr) const override;

std::vector<GUInt64> GetBlockSize() const override;

CSLConstList GetStructuralInfo() const override;

const void *GetRawNoDataValue() const override
{
return m_abyNoData.empty() ? nullptr : m_abyNoData.data();
Expand Down Expand Up @@ -1808,6 +1813,85 @@ HDF5Array::GetAttributes(CSLConstList papszOptions) const
return m_oListAttributes;
}

/************************************************************************/
/* GetBlockSize() */
/************************************************************************/

std::vector<GUInt64> HDF5Array::GetBlockSize() const
{
HDF5_GLOBAL_LOCK();

const auto nDimCount = GetDimensionCount();
std::vector<GUInt64> res(nDimCount);
if (res.empty())
return res;

const hid_t nListId = H5Dget_create_plist(m_hArray);
if (nListId > 0)
{
if (H5Pget_layout(nListId) == H5D_CHUNKED)
{
std::vector<hsize_t> anChunkDims(nDimCount);
const int nDimSize = H5Pget_chunk(
nListId, static_cast<int>(nDimCount), &anChunkDims[0]);
if (static_cast<size_t>(nDimSize) == nDimCount)
{
for (size_t i = 0; i < nDimCount; ++i)
{
res[i] = anChunkDims[i];
}
}
}

H5Pclose(nListId);
}

return res;
}

/************************************************************************/
/* GetStructuralInfo() */
/************************************************************************/

CSLConstList HDF5Array::GetStructuralInfo() const
{
if (m_aosStructuralInfo.empty())
{
HDF5_GLOBAL_LOCK();
const hid_t nListId = H5Dget_create_plist(m_hArray);
if (nListId > 0)
{
const int nFilters = H5Pget_nfilters(nListId);
for (int i = 0; i < nFilters; ++i)
{
unsigned int flags = 0;
size_t cd_nelmts = 0;
char szName[64 + 1] = {0};
const auto eFilter = H5Pget_filter(
nListId, i, &flags, &cd_nelmts, nullptr, 64, szName);
if (eFilter == H5Z_FILTER_DEFLATE)
{
m_aosStructuralInfo.SetNameValue("COMPRESSION", "DEFLATE");
}
else if (eFilter == H5Z_FILTER_SZIP)
{
m_aosStructuralInfo.SetNameValue("COMPRESSION", "SZIP");
}
else if (eFilter == H5Z_FILTER_SHUFFLE)
{
m_aosStructuralInfo.SetNameValue("FILTER", "SHUFFLE");
}
else
{
CPLDebug("HDF5", "Filter used: %s", szName);
}
}
H5Pclose(nListId);
}
}
return m_aosStructuralInfo.List();
}

/************************************************************************/
/* CopyBuffer() */
/************************************************************************/
Expand Down

0 comments on commit 9f69b4c

Please sign in to comment.