ResInsight/ApplicationCode/UnitTests/HDF5FileReader-Test.cpp

134 lines
4.3 KiB
C++
Raw Normal View History

#ifdef USE_HDF5
2017-03-13 06:23:03 -05:00
#include "gtest/gtest.h"
#include "H5Cpp.h"
#include <iostream>
2017-03-13 06:23:03 -05:00
#include <H5Exception.h>
2017-03-13 06:23:03 -05:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2017-08-17 07:40:47 -05:00
TEST(DISABLED_HDFTests, BasicFileRead)
2017-03-13 06:23:03 -05:00
{
2018-03-12 05:12:39 -05:00
std::string file_path = "D:/ResInsight/SourSim/PKMUNK_NOV_TEST_SS.sourpre.00001";
2017-03-13 06:23:03 -05:00
2018-03-12 05:12:39 -05:00
try
{
H5::Exception::dontPrint(); // Turn off auto-printing of failures to handle the errors appropriately
2018-03-12 05:12:39 -05:00
H5::H5File file(file_path.c_str(), H5F_ACC_RDONLY);
2018-03-12 05:12:39 -05:00
{
H5::Group timestep = file.openGroup("Timestep_00001");
H5::Attribute attr = timestep.openAttribute("timestep");
2018-03-12 05:12:39 -05:00
double timestep_value = 0.0;
2018-03-12 05:12:39 -05:00
H5::DataType type = attr.getDataType();
attr.read(type, &timestep_value);
2018-03-12 05:12:39 -05:00
//std::cout << "Timestep value " << timestep_value << std::endl;
EXPECT_NEAR(timestep_value, 1.0, 1e-1);
}
2018-03-12 05:12:39 -05:00
{
// Group size is not an attribute!
2018-03-12 05:12:39 -05:00
H5::Group GridFunctions = file.openGroup("Timestep_00001/GridParts/GridPart_00000/GridFunctions");
2018-03-12 05:12:39 -05:00
hsize_t group_size = GridFunctions.getNumObjs();
2018-03-12 05:12:39 -05:00
//std::cout << "GridFunctions group_size " << group_size << std::endl;
EXPECT_EQ(size_t(20), group_size);
2018-03-12 05:12:39 -05:00
/* for (hsize_t i = 0; i < group_size; i++)
{
// H5std_string node_name = GridFunctions.getObjnameByIdx(i); // crashes on VS2017 due to lib/heap/runtime differences to HDF5 VS2015 lib
2018-03-12 05:12:39 -05:00
std::string node_name;
node_name.resize(1024);
2018-03-12 05:12:39 -05:00
ssize_t slen = GridFunctions.getObjnameByIdx(i, &node_name[0], 1023);
2018-03-12 05:12:39 -05:00
node_name.resize(slen + 1);
2018-03-12 05:12:39 -05:00
std::cout << "GridFunctions sub-node name " << node_name << std::endl;
}
2017-03-13 06:23:03 -05:00
*/
2018-03-12 05:12:39 -05:00
std::string first_subnode(1024, '\0');
2017-03-13 06:23:03 -05:00
2018-03-12 05:12:39 -05:00
ssize_t slen = GridFunctions.getObjnameByIdx(0, &first_subnode[0], 1023);
first_subnode.resize(slen + 1);
2018-03-12 05:12:39 -05:00
EXPECT_TRUE(first_subnode.compare(0, slen, "GridFunction_00002") == 0);
}
2018-03-12 05:12:39 -05:00
{
H5::Group GridFunction_00002 = file.openGroup("Timestep_00001/GridParts/GridPart_00000/GridFunctions/GridFunction_00002");
H5::Attribute attr = GridFunction_00002.openAttribute("limits_max");
2018-03-12 05:12:39 -05:00
double limits_max = 0.0;
H5::DataType type = attr.getDataType();
attr.read(type, &limits_max);
2018-03-12 05:12:39 -05:00
// std::cout << "limits_max " << limits_max << std::endl;
EXPECT_NEAR(limits_max, 0.3970204292629652, 1e-10);
}
2018-03-12 05:12:39 -05:00
{
H5::Group GridFunction_00002 = file.openGroup("Timestep_00001/GridParts/GridPart_00000/GridFunctions/GridFunction_00002");
H5::DataSet dataset = H5::DataSet(GridFunction_00002.openDataSet("values"));
2018-03-12 05:12:39 -05:00
hsize_t dims[2];
H5::DataSpace dataspace = dataset.getSpace();
dataspace.getSimpleExtentDims(dims, nullptr);
2018-03-12 05:12:39 -05:00
std::vector<double> values;
values.resize(dims[0]);
dataset.read(values.data(), H5::PredType::NATIVE_DOUBLE);
2018-03-12 05:12:39 -05:00
/* for (hsize_t i = 0; i < dims[0]; i++)
{
std::cout << "value " << i << " " << values[i] << std::endl;
2018-03-12 05:12:39 -05:00
}
2017-03-13 06:23:03 -05:00
*/
2018-03-12 05:12:39 -05:00
EXPECT_NEAR(values[0], 0.32356910366452146, 1e-10);
EXPECT_NEAR(values[dims[0] - 1], 0.12200070891582514, 1e-10);
}
} // end of try block
catch (H5::FileIException error) // catch failure caused by the H5File operations
{
std::cout << error.getCDetailMsg();
}
catch (H5::DataSetIException error) // catch failure caused by the DataSet operations
{
std::cout << error.getCDetailMsg();
}
catch (H5::DataSpaceIException error) // catch failure caused by the DataSpace operations
{
std::cout << error.getCDetailMsg();
}
catch (H5::DataTypeIException error) // catch failure caused by the DataSpace operations
{
std::cout << error.getCDetailMsg();
}
2017-03-13 06:23:03 -05:00
}
#endif //USE_HDF5