Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F96624878
hdf5_database.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Dec 29, 03:43
Size
6 KB
Mime Type
text/x-c++
Expires
Tue, Dec 31, 03:43 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
23218109
Attached To
rSPECMICP SpecMiCP / ReactMiCP
hdf5_database.cpp
View Options
/* =============================================================================
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 "hdf5_database.hpp"
#include "../data_container.hpp"
#include "../../utils/io/specmicp_hdf5.hpp"
#include <H5Cpp.h>
namespace specmicp {
namespace io {
namespace internal {
//! \brief Implementation of the database saver
class SPECMICP_DLL_LOCAL DatabaseHDF5Saver
{
public:
DatabaseHDF5Saver(database::RawDatabasePtr raw_data):
m_data(raw_data)
{}
//!
//! \brief Save the labels of a database
//! \param file HDF5 file
//! \param name Name of the group to be created
//! \param section Name of the group where the new group willbe created
//!
void save_labels(HDF5File& file,
const std::string& name,
const std::string& section
);
private:
//! \brief Save the component labels
void save_components_labels(HDF5File& file, const std::string& section);
//! \brief Save the aqueous labels
void save_aqueous_labels( HDF5File& file, const std::string& section);
//! \brief Save the gas labels
void save_gas_labels( HDF5File& file, const std::string& section);
//! \brief Save the minerals labels
void save_minerals_labels( HDF5File& file, const std::string& section);
//! \brief Generic function to save a group of labels
//!
//! \param file HDF5 file
//! \param section Group und
//! \param name
//! \param nb_species
//! \param get_label
//!
static void save_generic_labels(
HDF5File& file,
const std::string& name,
const std::string& section,
index_t nb_species,
std::function<std::string (index_t)> get_label
);
database::RawDatabasePtr m_data;
};
} //end namespace internal
void save_database_labels(
HDF5File& file,
const std::string& name,
const std::string& section,
database::RawDatabasePtr raw_data
)
{
internal::DatabaseHDF5Saver saver(raw_data);
saver.save_labels(file, name, section);
}
// Implementation
// ==============
namespace internal {
void DatabaseHDF5Saver::save_labels(
HDF5File& file,
const std::string& name,
const std::string& section
)
{
auto group_name = file.complete_name(name, section);
auto group = file.create_group(group_name);
save_components_labels(file, group_name);
save_aqueous_labels(file, group_name);
save_gas_labels(file, group_name);
save_minerals_labels(file, group_name);
}
void DatabaseHDF5Saver::save_components_labels(
HDF5File& file,
const std::string& section
)
{
return save_generic_labels(
file, "components", section,
m_data->nb_component(),
[this](index_t id) -> std::string {
return this->m_data->get_label_component(id);
}
);
}
void DatabaseHDF5Saver::save_aqueous_labels(
HDF5File& file,
const std::string& section
)
{
return save_generic_labels(
file, "aqueous", section,
m_data->nb_aqueous(),
[this](index_t id) -> std::string {
return this->m_data->get_label_aqueous(id);
}
);
}
void DatabaseHDF5Saver::save_gas_labels(
HDF5File& file,
const std::string& section
)
{
return save_generic_labels(
file, "gas", section,
m_data->nb_gas(),
[this](index_t id) -> std::string {
return this->m_data->get_label_gas(id);
}
);
}
void DatabaseHDF5Saver::save_minerals_labels(
HDF5File& file,
const std::string& section
)
{
return save_generic_labels(
file, "minerals", section,
m_data->nb_mineral(),
[this](index_t id) -> std::string {
return this->m_data->get_label_mineral(id);
}
);
}
void DatabaseHDF5Saver::save_generic_labels(
HDF5File& file,
const std::string& name,
const std::string& section,
index_t nb_species,
std::function<std::string (index_t)> get_label
)
{
// /!\ we need to create a vector of char* to pass to hdf5
// danger => explicit use of new[] and delete[]
std::vector<char *> labels(nb_species);
for (index_t id=0; id<nb_species; ++id) {
labels[id] = new char[get_label(id).size()+1];
std::strcpy(labels[id], get_label(id).c_str());
}
H5::DSetCreatPropList plist;
// create dataspace
auto type = H5::StrType(0, H5T_VARIABLE);
hsize_t dims[] = {static_cast<hsize_t>(nb_species)};
auto fspace = H5::DataSpace(1, dims);
// write to file
auto dataset = file.create_dataset(name, section, type, fspace, plist);
dataset->write(labels.data(), type);
// free memory
for (index_t id=0; id<nb_species; ++id)
{
delete[] labels[id];
}
}
} //end namespace internal
} //end namespace io
} //end namespace specmicp
Event Timeline
Log In to Comment