Page MenuHomec4science

specmicp_hdf5.cpp
No OneTemporary

File Metadata

Created
Thu, Jan 2, 20:40

specmicp_hdf5.cpp

/* =============================================================================
Copyright (c) 2014 - 2016
F. Georget <fabieng@princeton.edu> Princeton University
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
============================================================================= */
#include "specmicp_hdf5.hpp"
#include <H5Cpp.h>
#include "../compat.hpp"
namespace specmicp {
namespace io {
HDF5File::HDF5File(const std::string& filename, HDF5_OpenMode mode)
{
open(filename, mode);
}
HDF5File::~HDF5File() {
close();
}
bool HDF5File::open(
const std::string& filename,
HDF5_OpenMode mode
)
{
if (m_file != nullptr)
m_file->close();
unsigned int flag = H5F_ACC_DEFAULT;
switch (mode) {
case HDF5_OpenMode::CreateTruncate:
flag = H5F_ACC_TRUNC;
break;
case HDF5_OpenMode::CreateFailIfExist:
flag = H5F_ACC_EXCL;
break;
case HDF5_OpenMode::OpenReadOnly:
flag = H5F_ACC_RDONLY;
break;
case HDF5_OpenMode::OpenReadWrite:
flag = H5F_ACC_RDWR;
break;
}
m_file = make_unique<H5::H5File>(filename, flag);
return is_open();
}
bool HDF5File::is_open() {
return (m_file != nullptr);
}
void HDF5File::close() {
if (is_open()) {
m_file->close();
m_file.reset(nullptr);
}
}
std::unique_ptr<H5::DataSet> HDF5File::create_dataset(
const std::string& name,
const H5::DataType& type,
const H5::DataSpace& data_space,
const H5::DSetCreatPropList& list
)
{
return make_unique<H5::DataSet>(
m_file->createDataSet(name, type, data_space, list));
}
std::unique_ptr<H5::DataSet> HDF5File::create_dataset(
const std::string& name,
const std::string& section,
const H5::DataType& type,
const H5::DataSpace& data_space,
const H5::DSetCreatPropList& list
)
{
return create_dataset(complete_name(name, section), type, data_space, list);
}
std::unique_ptr<H5::DataSet> HDF5File::open_dataset(
const std::string& name
) const
{
return make_unique<H5::DataSet>(m_file->openDataSet(name));
}
std::unique_ptr<H5::DataSet> HDF5File::open_dataset(
const std::string& name,
const std::string& section
) const
{
return open_dataset(complete_name(name, section));
}
std::unique_ptr<H5::Group> HDF5File::create_group(
const std::string& name
)
{
return make_unique<H5::Group>(
m_file->createGroup(name)
);
}
std::unique_ptr<H5::Group> HDF5File::open_group(
const std::string& name
) const
{
return make_unique<H5::Group>(
m_file->openGroup(name)
);
}
//! \brief Create an attribute
std::unique_ptr<H5::Attribute> HDF5File::create_attribute(
const std::string& name,
const H5::DataType& type,
const H5::DataSpace& data_space
)
{
return make_unique<H5::Attribute>(
m_file->createAttribute(
name.c_str(), type, data_space));
}
//! \brief Open an attribute
std::unique_ptr<H5::Attribute> HDF5File::open_attribute(
const std::string& name
) const
{
return make_unique<H5::Attribute>(
m_file->openAttribute(name.c_str()));
}
std::size_t HDF5File::get_number_objects() {
return m_file->getNumObjs();
}
std::size_t HDF5File::get_number_objects(const std::string& section) {
auto the_group = open_group(section);
return the_group->getNumObjs();
}
int HDF5File::iterate_over_elements(
iterate_function_t func
)
{
return iterate_over_elements(func, nullptr);
}
int HDF5File::iterate_over_elements(
iterate_function_t func,
void* extra_data
)
{
// arguments
// 1 : iterate over the root
// 2 : start at the beginning
return m_file->iterateElems("/", NULL, func, extra_data);
}
std::string HDF5File::complete_name(
const std::string& name,
const std::string& section
)
{
std::string comp = section;
if (comp[comp.size()-1] != '/') {
comp.reserve(comp.size()+1+name.size());
comp += "/"+name;
}
else {
comp += name;
}
return comp;
}
} //end namespace io
} //end namespace specmicp

Event Timeline