WIP: hdf5: Dataset
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Christian Zimmermann 2024-01-31 00:16:34 +01:00
parent 84f18e1d5e
commit df228d545b
3 changed files with 155 additions and 0 deletions

View file

@ -0,0 +1,51 @@
#ifndef __cxz_h5_dataset_h__
#define __cxz_h5_dataset_h__
#include "h5_types.h"
#include "h5_content_base.h"
namespace CNORXZ
{
namespace hdf5
{
class Dataset : public ContentBase
{
public:
DEFAULT_MEMBERS(Dataset);
Dataset(const String& name, const ContentBase* _parent);
~Dataset();
virtual ContentType type() const override final;
virtual bool ro() const override final;
virtual Dataset& open() override final;
virtual Dataset& close() override final;
virtual String path() const override final;
virtual String filename() const override final;
bool exists() const;
Dataset& init(const RangePtr& dataRange, hid_t type);
const RangePtr& dataRange() const;
protected:
RangePtr mDataRange;
hid_t mType;
hid_t mFilespace;
};
template <typename T>
class SDataset : public Dataset
{
public:
DEFAULT_MEMBERS(SDataset);
SDataset(const String& name, const ContentBase* _parent);
MArray<T> read() const;
};
}
}
#endif

View file

@ -4,6 +4,7 @@ set(libcnorxzhdf5_a_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/h5_file.cc
${CMAKE_CURRENT_SOURCE_DIR}/h5_group.cc
${CMAKE_CURRENT_SOURCE_DIR}/h5_table.cc
${CMAKE_CURRENT_SOURCE_DIR}/h5_dataset.cc
)
add_library(cnorxzhdf5_obj OBJECT

View file

@ -0,0 +1,103 @@
#include "h5_dataset.h"
namespace CNORXZ
{
namespace hdf5
{
Dataset::Dataset(const String& name, const ContentBase* _parent) :
ContentBase(name, _parent)
{
if(exists()){
open();
}
}
Dataset::~Dataset()
{
this->close();
}
ContentType Dataset::type() const
{
return ContentType::DSET;
}
bool Dataset::ro() const
{
return mParent->ro();
}
Dataset& Dataset::open()
{
if(mId == 0 and exists()){
mId = H5Dopen(mParent->id(), mName.c_str(), H5P_DEFAULT);
mType = H5Dget_type(mId);
mFilespace = H5Dget_space(mId);
SizeT ndims = H5Sget_simple_extent_ndims(mFilespace);
Vector<hsize_t> dims(ndims);
H5Sget_simple_extent_dims(mFilespace, dims.data(), nullptr);
Vector<RangePtr> rs(ndims);
for(SizeT i = 0; i != ndims; ++i){
rs[i] = CRangeFactory(dims[i]).create();
}
mDataRange = yrange(rs);
// use when reading!!!:
//hid_t mem_space_id = H5Screate_simple(static_cast<hsize_t>(ndims), dims.data(), nullptr);
//hid_t xfer_plist_id = H5Pcreate(H5P_DATASET_XFER);
}
return *this;
}
Dataset& Dataset::close()
{
if(mId != 0){
//!!!
H5Sclose(mFilespace);
H5Tclose(mType);
H5Dclose(mId);
mId = 0;
}
return *this;
}
String Dataset::path() const
{
return mParent->path() + "/" + mName;
}
String Dataset::filename() const
{
return mParent->filename();
}
bool Dataset::exists() const
{
return H5Lexists(mParent->id(), mName.c_str(), H5P_DEFAULT);
}
Dataset& Dataset::init(const RangePtr& dataRange, hid_t type)
{
// check if type is valid type!!!
const hid_t dcpl_id = H5Pcreate(H5P_DATASET_CREATE);
// TODO: all sub-ranges explicity!!!:
const SizeT ndim = dataRange->dim();
Vector<hsize_t> exts(ndim);
mFilespace = H5Screate_simple(ndim, exts.data(), NULL);
mType = type;
mId = H5Dcreate(mParent->id(), mName.c_str(), mType, mFilespace,
H5P_DEFAULT, dcpl_id, H5P_DEFAULT);
H5Pclose(dcpl_id);
return *this;
}
const RangePtr& Dataset::dataRange() const
{
return mDataRange;
}
}
}