Skip to content

Commit

Permalink
hook up createFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
JDanielSmith committed Jan 27, 2023
1 parent 8c7e447 commit 1f9d07e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion modules/c++/hdf5.lite/include/hdf5/lite/Write.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ namespace hdf5
{
namespace lite
{
CODA_OSS_API void createFile(const coda_oss::filesystem::path&, const std::string& ds, types::RowCol<size_t>);
template<typename TDataSet> // currently implemented for float and double
CODA_OSS_API void createFile(const coda_oss::filesystem::path&, const std::string& ds, const types::RowCol<size_t>&);

CODA_OSS_API void writeFile(const coda_oss::filesystem::path&, const std::string& loc, std::vector<double>&);
CODA_OSS_API void writeFile(const coda_oss::filesystem::path&, const std::string& loc, std::vector<float>&);
Expand Down
43 changes: 43 additions & 0 deletions modules/c++/hdf5.lite/source/Write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,47 @@
#include "H5.h"
#include "hdf5.lite.h"

// https://raw.githubusercontent.com/HDFGroup/hdf5/develop/c++/examples/h5tutr_rdwt.cpp

template<typename T> static H5::PredType getPredType();
template <>
inline H5::PredType getPredType<float>()
{
static_assert(sizeof(float) * 8 == 32, "'float' should be 32-bits"); // IEEE_F32LE
return H5::PredType::IEEE_F32LE;
}
template <>
inline H5::PredType getPredType<double>()
{
static_assert(sizeof(double) * 8 == 64, "'double' should be 64-bits"); // IEEE_F64LE
return H5::PredType::IEEE_F64LE;
}

template<typename T>
static void createFile_(const coda_oss::filesystem::path& fileName, const std::string& ds, const types::RowCol<size_t>& sz)
{
// https://raw.githubusercontent.com/HDFGroup/hdf5/develop/c++/examples/h5tutr_crtdat.cpp
//
// Create a new file using the default property lists.
H5::H5File file(fileName.string(), H5F_ACC_TRUNC);

// Create the data space for the dataset.
constexpr int RANK = 2;
const hsize_t dims[]{sz.row, sz.col}; // dataset dimensions
H5::DataSpace dataspace(RANK, dims);

// Create the dataset.
const auto data_type = getPredType<T>();
std::ignore = file.createDataSet(ds, data_type, dataspace);
}

template<>
void hdf5::lite::createFile<float>(const coda_oss::filesystem::path& fileName, const std::string& ds, const types::RowCol<size_t>& sz)
{
details::try_catch_H5ExceptionsV(createFile_<float>, __FILE__, __LINE__, fileName, ds, sz);
}
template<>
void hdf5::lite::createFile<double>(const coda_oss::filesystem::path& fileName, const std::string& ds, const types::RowCol<size_t>& sz)
{
details::try_catch_H5ExceptionsV(createFile_<double>, __FILE__, __LINE__, fileName, ds, sz);
}
7 changes: 7 additions & 0 deletions modules/c++/hdf5.lite/source/hdf5.lite.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ auto try_catch_H5Exceptions(TFunc f, const char* file, int line, TArgs&&... args
details::try_catch_H5Exceptions_(call_f, file, line, &retval /*context*/);
return retval;
}
template<typename TFunc, typename ...TArgs>
auto try_catch_H5ExceptionsV(TFunc f, const char* file, int line, TArgs&&... args)
{
// "Hide" the arguments inside of a lambda
auto call_f = [&](void*) { f(std::forward<TArgs>(args)...); };
details::try_catch_H5Exceptions_(call_f, file, line, nullptr /*context*/);
}

}
}
Expand Down

0 comments on commit 1f9d07e

Please sign in to comment.