Page MenuHomec4science

dataset.cpp
No OneTemporary

File Metadata

Created
Sat, Jun 22, 10:02

dataset.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 "dataset.hpp"
#include "dataspace.hpp"
#include "H5Dpublic.h"
#include "H5Spublic.h"
#include "attribute.hpp"
#include "H5Apublic.h"
#include "H5Ppublic.h"
#include <stdexcept>
namespace specmicp {
namespace io {
namespace hdf5 {
Dataset Dataset::acquire(hid_t id, const std::string& path)
{
if (id < 0 )
{
throw std::runtime_error("Invalid dataset : " + path + ".");
}
return Dataset(id, path);
}
Dataset::Dataset(hid_t id, const std::string& path):
Path(id, path)
{}
Dataset::~Dataset()
{
H5Dclose(get_id());
}
Dataspace Dataset::get_dataspace() const
{
hid_t space_id = H5Dget_space(get_id());
return Dataspace::acquire(space_id);
}
Attribute Dataset::create_scalar_attribute(
const std::string& name,
const Dataspace& d_space
)
{
hid_t id = H5Acreate2(get_id(), name.c_str(), H5T_NATIVE_DOUBLE,
d_space.get_id(), H5P_DEFAULT, H5P_DEFAULT);
return Attribute::acquire(id);
}
Attribute Dataset::open_attribute(const std::string& name) const
{
hid_t id = H5Aopen(get_id(), name.c_str(), H5P_DEFAULT);
return Attribute::acquire(id);
}
Attribute Dataset::create_scalar_attribute(
const std::string& name,
std::size_t N,
const double values[]
)
{
hsize_t dims[] = {N,};
auto d_space = Dataspace::create_simple(1, dims);
auto attr = create_scalar_attribute(name, d_space);
H5Awrite(attr.get_id(), H5T_NATIVE_DOUBLE, values);
return attr;
}
void Dataset::read_scalar_attribute(
const std::string& name,
std::size_t size,
double values[]
) const
{
Attribute attr = open_attribute(name);
Dataspace d_space = attr.get_dataspace();
if (d_space.get_rank() != 1) {
throw std::runtime_error("Expected attribute with rank 1");
}
hsize_t dims[1];
d_space.get_dimensions(dims);
if (dims[0] != size)
{
throw std::runtime_error("Unexpected dimensions for the attribute."
"Expected " + std::to_string(size) + "x1;"
" got : " + std::to_string(dims[0]) + "x1."
);
}
H5Aread(attr.get_id(), H5T_NATIVE_DOUBLE, values);
}
} // end namespace hdf5
} // end namespace io
} // end namespace specmicp

Event Timeline