WIP: Inclusion of HDF5 access functions

This commit is contained in:
Stein Inge Dale 2017-03-24 10:06:49 +01:00
parent b29f664723
commit c4cd1972c4
2 changed files with 246 additions and 15 deletions

View File

@ -21,6 +21,11 @@
#include <QStringList>
#include <QDateTime>
#include "H5Cpp.h"
#include <H5Exception.h>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -45,18 +50,30 @@ bool RifHdf5Reader::dynamicResult(const QString& result, size_t stepIndex, std::
{
QStringList myProps = propertyNames();
// if (std::find(begin(myProps), end(myProps), result) != end(myProps))
if (myProps.indexOf(result) != -1)
{
for (size_t i = 0; i < 16336; i++)
{
values->push_back(i);
}
try
{
H5::Exception::dontPrint(); // Turn off auto-printing of failures to handle the errors appropriately
return true;
}
std::string fileName = m_fileName.toStdString(); // her ligger trøbbel mht Unicode or det smalt i H5File med direkte bruk av c_str()
return false;
H5::H5File file(fileName.c_str(), H5F_ACC_RDONLY);
std::string groupName = "/Timestep_00001/GridFunctions/GridPart_00000/GridFunction_00002";
getElementResultValues(file, groupName, values);
return true;
}
catch (H5::FileIException error) // catch failure caused by the H5File operations
{
return false;
}
catch (H5::DataSetIException error) // catch failure caused by the DataSet operations
{
return false;
}
}
//--------------------------------------------------------------------------------------------------
@ -79,13 +96,22 @@ std::vector<QDateTime> RifHdf5Reader::timeSteps() const
//--------------------------------------------------------------------------------------------------
QStringList RifHdf5Reader::propertyNames() const
{
QStringList myProps;
QStringList propNames;
myProps.push_back("msj1");
myProps.push_back("msj2");
myProps.push_back("msj3");
std::string str = m_fileName.toStdString(); // her ligger trøbbel mht Unicode or det smalt i H5File med direkte bruk av c_str()
return myProps;
H5::H5File file(str.c_str(), H5F_ACC_RDONLY);
std::string groupName = "/Timestep_00001/GridFunctions/GridPart_00000";
std::vector<std::string> resultNames = getResultNames(file, groupName);
for (std::vector<std::string>::iterator it = resultNames.begin(); it != resultNames.end(); it++)
{
propNames.push_back(it->c_str());
}
return propNames;
}
//--------------------------------------------------------------------------------------------------
@ -100,3 +126,194 @@ void RifHdf5Reader::resultNames(QStringList* resultNames, std::vector<size_t>* r
resultDataItemCounts->push_back(16336);
}
}
//=========================== PRIVATE METHODS =====================================================
//
//
//
//==================================================================================================
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RifHdf5Reader::getIntAttribute(H5::H5File file, std::string groupName, std::string attributeName) const
{
try
{
H5::Group group = file.openGroup(groupName.c_str());
H5::Attribute attr = group.openAttribute(attributeName.c_str());
int value = 0;
H5::DataType type = attr.getDataType();
attr.read(type, &value);
return value;
}
catch (H5::AttributeIException error)
{
return 0;
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RifHdf5Reader::getDoubleAttribute(H5::H5File file, std::string groupName, std::string attributeName) const
{
try
{
H5::Group group = file.openGroup(groupName.c_str());
H5::Attribute attr = group.openAttribute(attributeName.c_str());
double value = 0.0;
H5::DataType type = attr.getDataType();
attr.read(type, &value);
return value;
}
catch (H5::AttributeIException error)
{
return 0.0;
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::string RifHdf5Reader::getStringAttribute(H5::H5File file, std::string groupName, std::string attributeName) const
{
try
{
H5::Group group = file.openGroup(groupName.c_str());
H5::Attribute attr = group.openAttribute(attributeName.c_str());
std::string stringAttribute(1024, '\0');
H5::DataType nameType = attr.getDataType();
attr.read(nameType, &stringAttribute[0]);
return stringAttribute;
}
catch (H5::AttributeIException error)
{
return "";
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<std::string> RifHdf5Reader::getSubGroupNames(H5::H5File file, std::string baseGroupName) const
{
H5::Group baseGroup = file.openGroup(baseGroupName.c_str());
std::vector<std::string> subGroupNames;
hsize_t groupSize = baseGroup.getNumObjs();
for (hsize_t i = 0; i < groupSize; i++)
{
std::string nodeName(1024, '\0');
ssize_t slen = baseGroup.getObjnameByIdx(i, &nodeName[0], 1023);
nodeName.resize(slen + 1);
subGroupNames.push_back(nodeName);
}
return subGroupNames;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<double> RifHdf5Reader::getStepTimeValues(H5::H5File file, std::string baseGroupName) const
{
std::vector<std::string> subGroupNames = getSubGroupNames(file, baseGroupName);
std::vector<double> stepTimeValues;
for (std::vector<std::string>::iterator it = subGroupNames.begin(); it != subGroupNames.end(); it++)
{
if (it->find("Timestep_") != std::string::npos)
{
std::string groupName = baseGroupName + *it;
double timestep_value = getDoubleAttribute(file, groupName, "timestep");
stepTimeValues.push_back(timestep_value);
}
}
return stepTimeValues;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<std::string> RifHdf5Reader::getResultNames(H5::H5File file, std::string baseGroupName) const
{
H5::Group baseGroup = file.openGroup(baseGroupName.c_str());
std::vector<std::string> subGroupNames = getSubGroupNames(file, baseGroupName);
std::vector<std::string> resultNames;
for (std::vector<std::string>::iterator it = subGroupNames.begin(); it != subGroupNames.end(); it++)
{
std::string groupName = baseGroupName + "/" + *it;
std::string name = getStringAttribute(file, groupName, "name");
resultNames.push_back(name);
}
return resultNames;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifHdf5Reader::getElementResultValues(H5::H5File file, std::string groupName, std::vector<double>* resultValues) const
{
H5::Group group = file.openGroup(groupName.c_str());
H5::DataSet dataset = H5::DataSet(group.openDataSet("values"));
hsize_t dims[2];
H5::DataSpace dataspace = dataset.getSpace();
dataspace.getSimpleExtentDims(dims, NULL);
(*resultValues).resize(dims[0]);
dataset.read(resultValues->data(), H5::PredType::NATIVE_DOUBLE);
}

View File

@ -22,6 +22,9 @@
#include <QString>
#include "H5Cpp.h"
//==================================================================================================
//
//
@ -43,6 +46,17 @@ public:
virtual void resultNames(QStringList* resultNames, std::vector<size_t>* resultDataItemCounts) override;
private:
int getIntAttribute(H5::H5File file, std::string groupName, std::string attributeName) const;
double getDoubleAttribute(H5::H5File file, std::string groupName, std::string attributeName) const;
std::string getStringAttribute(H5::H5File file, std::string groupName, std::string attributeName) const;
std::vector<std::string> getSubGroupNames(H5::H5File file, std::string baseGroupName) const;
std::vector<double> getStepTimeValues(H5::H5File file, std::string baseGroupName) const;
std::vector<std::string> getResultNames(H5::H5File file, std::string baseGroupName) const;
void getElementResultValues(H5::H5File file, std::string groupName, std::vector<double>* resultValues) const;
private:
QString m_fileName;
};