36 #include "Hdf5DataReader.hpp" 38 #include "OutputFileHandler.hpp" 45 const std::string& rBaseName,
47 std::string datasetName)
56 const std::string& rBaseName,
57 std::string datasetName)
77 EXCEPTION(
"Hdf5DataReader could not open " + file_name +
" , as it does not exist.");
81 mFileId = H5Fopen(file_name.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
85 EXCEPTION(
"Hdf5DataReader could not open " << file_name <<
86 " , H5Fopen error code = " <<
mFileId);
95 EXCEPTION(
"Hdf5DataReader opened " << file_name <<
" but could not find the dataset '" <<
104 H5Sget_simple_extent_dims(variables_dataspace,
mDatasetDims, dataset_max_sizes);
112 if (dataset_max_sizes[0] == H5S_UNLIMITED)
135 hid_t attribute_type = H5Aget_type(attribute_id);
136 hid_t attribute_space = H5Aget_space(attribute_id);
139 H5Sget_simple_extent_dims(attribute_space, &attr_dataspace_dim,
nullptr);
141 unsigned num_columns = H5Sget_simple_extent_npoints(attribute_space);
142 char* string_array = (
char *)malloc(
sizeof(
char)*MAX_STRING_SIZE*(int)num_columns);
143 H5Aread(attribute_id, attribute_type, string_array);
146 for (
unsigned index=0; index < num_columns; index++)
149 std::string column_name_unit(&string_array[MAX_STRING_SIZE*index]);
152 size_t name_length = column_name_unit.find(
'(');
153 size_t unit_length = column_name_unit.find(
')') - name_length - 1;
155 std::string column_name = column_name_unit.substr(0, name_length);
156 std::string column_unit = column_name_unit.substr(name_length+1, unit_length);
164 H5Tclose(attribute_type);
165 H5Sclose(attribute_space);
166 H5Aclose(attribute_id);
177 if (attribute_id < 0)
184 attribute_type = H5Aget_type(attribute_id);
185 attribute_space = H5Aget_space(attribute_id);
186 unsigned is_data_complete;
187 H5Aread(attribute_id, H5T_NATIVE_UINT, &is_data_complete);
190 H5Tclose(attribute_type);
191 H5Sclose(attribute_space);
192 H5Aclose(attribute_id);
195 if (is_data_complete)
203 attribute_type = H5Aget_type(attribute_id);
204 attribute_space = H5Aget_space(attribute_id);
207 unsigned num_node_indices = H5Sget_simple_extent_npoints(attribute_space);
214 H5Tclose(attribute_type);
215 H5Sclose(attribute_space);
216 H5Aclose(attribute_id);
227 unsigned actual_node_index = nodeIndex;
230 unsigned node_index = 0;
235 actual_node_index = node_index;
241 EXCEPTION(
"The incomplete dataset '" <<
mDatasetName <<
"' does not contain info of node " << nodeIndex);
246 EXCEPTION(
"The dataset '" <<
mDatasetName <<
"' doesn't contain info of node " << actual_node_index);
252 EXCEPTION(
"The dataset '" <<
mDatasetName <<
"' doesn't contain data for variable " << rVariableName);
254 unsigned column_index = (*col_iter).second;
257 hsize_t offset[3] = {0, actual_node_index, column_index};
260 H5Sselect_hyperslab(variables_dataspace, H5S_SELECT_SET, offset,
nullptr, count,
nullptr);
263 hid_t memspace = H5Screate_simple(1, &
mDatasetDims[0] ,
nullptr);
269 H5Dread(
mVariablesDatasetId, H5T_NATIVE_DOUBLE, memspace, variables_dataspace, H5P_DEFAULT, &ret[0]);
271 H5Sclose(variables_dataspace);
288 EXCEPTION(
"GetVariableOverTimeOverMultipleNodes() cannot be called using incomplete data sets (those for which data was only written for certain nodes)");
299 EXCEPTION(
"The dataset '" <<
mDatasetName <<
"' doesn't contain data for variable " << rVariableName);
301 unsigned column_index = (*col_iter).second;
304 hsize_t offset[3] = {0, lowerIndex, column_index};
307 H5Sselect_hyperslab(variables_dataspace, H5S_SELECT_SET, offset,
nullptr, count,
nullptr);
312 data_dimensions[1] = upperIndex-lowerIndex;
313 hid_t memspace = H5Screate_simple(2, data_dimensions,
nullptr);
315 double* data_read =
new double[
mDatasetDims[0]*(upperIndex-lowerIndex)];
318 H5Dread(
mVariablesDatasetId, H5T_NATIVE_DOUBLE, memspace, variables_dataspace, H5P_DEFAULT, data_read);
320 H5Sclose(variables_dataspace);
324 unsigned num_nodes_read = upperIndex-lowerIndex;
327 std::vector<std::vector<double> > ret(num_nodes_read);
329 for (
unsigned node_num=0; node_num<num_nodes_read; node_num++)
331 ret[node_num].resize(num_timesteps);
332 for (
unsigned time_num=0; time_num<num_timesteps; time_num++)
334 ret[node_num][time_num] = data_read[num_nodes_read*time_num + node_num];
344 const std::string& rVariableName,
349 EXCEPTION(
"You can only get a vector for complete data");
359 EXCEPTION(
"The dataset '" <<
mDatasetName <<
"' does not contain data for variable " << rVariableName);
361 unsigned column_index = (*col_iter).second;
366 EXCEPTION(
"The dataset '" <<
mDatasetName <<
"' does not contain data for timestep number " << timestep);
370 VecGetSize(data, &size);
373 EXCEPTION(
"Could not read data because Vec is the wrong size");
376 VecGetOwnershipRange(data, &lo, &hi);
382 hid_t memspace = H5Screate_simple(1, v_size,
nullptr);
388 H5Sselect_hyperslab(hyperslab_space, H5S_SELECT_SET, offset,
nullptr, count,
nullptr);
390 double* p_petsc_vector;
391 VecGetArray(data, &p_petsc_vector);
393 herr_t err = H5Dread(
mVariablesDatasetId, H5T_NATIVE_DOUBLE, memspace, hyperslab_space, H5P_DEFAULT, p_petsc_vector);
397 VecRestoreArray(data, &p_petsc_vector);
399 H5Sclose(hyperslab_space);
unsigned GetNumberOfRows()
void SetUnlimitedDatasetId()
std::string GetAbsolutePath() const
#define EXCEPTION(message)
std::string GetUnit(const std::string &rVariableName)
hid_t mVariablesDatasetId
hid_t mUnlimitedDatasetId
void GetVariableOverNodes(Vec data, const std::string &rVariableName, unsigned timestep=0)
hsize_t mDatasetDims[DATASET_DIMS]
std::map< std::string, unsigned > mVariableToColumnIndex
void SetMainDatasetRawChunkCache()
std::vector< std::vector< double > > GetVariableOverTimeOverMultipleNodes(const std::string &rVariableName, unsigned lowerIndex, unsigned upperIndex)
std::vector< std::string > mVariableNames
std::vector< unsigned > mIncompleteNodeIndices
std::vector< double > GetUnlimitedDimensionValues()
Hdf5DataReader(const std::string &rDirectory, const std::string &rBaseName, bool makeAbsolute=true, std::string datasetName="Data")
std::vector< double > GetVariableOverTime(const std::string &rVariableName, unsigned nodeIndex)
std::map< std::string, std::string > mVariableToUnit
unsigned mVariablesDatasetRank
std::vector< std::string > GetVariableNames()
bool mIsUnlimitedDimensionSet
static const unsigned DATASET_DIMS