From c4cd1972c42f38231d4bedd115535a476e7c3943 Mon Sep 17 00:00:00 2001 From: Stein Inge Dale Date: Fri, 24 Mar 2017 10:06:49 +0100 Subject: [PATCH] WIP: Inclusion of HDF5 access functions --- .../FileInterface/RifHdf5Reader.cpp | 247 ++++++++++++++++-- ApplicationCode/FileInterface/RifHdf5Reader.h | 14 + 2 files changed, 246 insertions(+), 15 deletions(-) diff --git a/ApplicationCode/FileInterface/RifHdf5Reader.cpp b/ApplicationCode/FileInterface/RifHdf5Reader.cpp index ed6fab95d8..bf6dedbd8a 100644 --- a/ApplicationCode/FileInterface/RifHdf5Reader.cpp +++ b/ApplicationCode/FileInterface/RifHdf5Reader.cpp @@ -21,6 +21,11 @@ #include #include +#include "H5Cpp.h" +#include + + + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -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 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 resultNames = getResultNames(file, groupName); + + for (std::vector::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* 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 RifHdf5Reader::getSubGroupNames(H5::H5File file, std::string baseGroupName) const +{ + H5::Group baseGroup = file.openGroup(baseGroupName.c_str()); + + std::vector 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 RifHdf5Reader::getStepTimeValues(H5::H5File file, std::string baseGroupName) const +{ + std::vector subGroupNames = getSubGroupNames(file, baseGroupName); + std::vector stepTimeValues; + + for (std::vector::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 RifHdf5Reader::getResultNames(H5::H5File file, std::string baseGroupName) const +{ + H5::Group baseGroup = file.openGroup(baseGroupName.c_str()); + + std::vector subGroupNames = getSubGroupNames(file, baseGroupName); + + std::vector resultNames; + + for (std::vector::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* 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); +} + + + diff --git a/ApplicationCode/FileInterface/RifHdf5Reader.h b/ApplicationCode/FileInterface/RifHdf5Reader.h index 49443b9e67..272595fec4 100644 --- a/ApplicationCode/FileInterface/RifHdf5Reader.h +++ b/ApplicationCode/FileInterface/RifHdf5Reader.h @@ -22,6 +22,9 @@ #include +#include "H5Cpp.h" + + //================================================================================================== // // @@ -43,6 +46,17 @@ public: virtual void resultNames(QStringList* resultNames, std::vector* 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 getSubGroupNames(H5::H5File file, std::string baseGroupName) const; + std::vector getStepTimeValues(H5::H5File file, std::string baseGroupName) const; + std::vector getResultNames(H5::H5File file, std::string baseGroupName) const; + void getElementResultValues(H5::H5File file, std::string groupName, std::vector* resultValues) const; + + private: QString m_fileName; };