Skip to content

Commit

Permalink
Merge pull request #4 from ic97usop/elisabeth/messaging/hdf-reading
Browse files Browse the repository at this point in the history
Elisabeth/messaging/hdf reading
  • Loading branch information
ic97usop committed Apr 26, 2016
2 parents bbf5ea0 + 66b6646 commit 5621a46
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 0 deletions.
12 changes: 12 additions & 0 deletions messaging/hdf_reader/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
1. Download and install CMake:
▪ https://cmake.org/download/
▪ https://cmake.org/install

2. Build HDF5 library with CMake:
▪ https://www.hdfgroup.org/HDF5/release/cmakebuild.html#build

3. Build and run example:

$ cd src
$ cmake CMakeLists.txt
$ make
$ ./HDFFileReaderExample
14 changes: 14 additions & 0 deletions messaging/hdf_reader/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required (VERSION 3.1.0)
project (HDF5FileReaderExample)

find_package (HDF5 NAMES hdf5 COMPONENTS CXX static)
#find_package (HDF5)
#include_directories(${HDF5_INCLUDE_DIR})

set (LINK_LIBS ${LINK_LIBS} ${HDF5_C_STATIC_LIBRARY})

set (example HDFFileReaderExample)

add_executable (${example} ${PROJECT_SOURCE_DIR}/${example}.cpp)

target_link_libraries (${example} ${LINK_LIBS})
211 changes: 211 additions & 0 deletions messaging/hdf_reader/src/HDFFileReaderExample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
//
// HDFFileReaderExample.cpp
// Projectname: amos-ss16-proj5
//
// Created by Elisabeth Hoppe on 24.04.16.
// Copyright (c) 2016 de.fau.cs.osr.amos2016.gruppe5
//
// This file is part of the AMOS Project 2016 @ FAU
// (Friedrich-Alexander University Erlangen-Nürnberg)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public
// License along with this program. If not, see
// <http://www.gnu.org/licenses/>.
//

#ifdef OLD_HEADER_FILENAME
#include <iostream.h>
#else
#include <iostream>
#endif
#include <string>
#include <vector>

#ifndef H5_NO_NAMESPACE
#ifndef H5_NO_STD
using std::cout;
using std::endl;
#endif // H5_NO_STD
#endif

#include "H5Cpp.h"

#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif

const H5std_string FILE_NAME( "measurement_2016-04-08-13-27-15-507.hdf5" );
const H5std_string DATASET_NAME( "MFCImageRight" );


int main(void)
{

// Try block to detect exceptions raised by any of the calls inside it
try
{

// Turn off the auto-printing when failure occurs so that we can
// handle the errors appropriately
Exception::dontPrint();

// create the file and open it with read-only access.
H5File * file = new H5File( FILE_NAME, H5F_ACC_RDONLY );
DataSet dataSet = file->openDataSet(DATASET_NAME);

// get the class of the datatype that is used by the dataset.
H5T_class_t typeClass = dataSet.getTypeClass();


// get the data space of the dataset for obtaining information about dimension, sizes, etc.
DataSpace dataSpace = dataSet.getSpace();

// get info about dataspace
int dimNumber = dataSpace.getSimpleExtentNdims();
hssize_t elemsNumber = dataSpace.getSimpleExtentNpoints();
bool isSimple = dataSpace.isSimple();

// get dimension size of each dimension
hsize_t dimsOut[2];
int nDims = dataSpace.getSimpleExtentDims(dimsOut, NULL);

if(dimsOut[1] != 2)
{
//TODO exception handling
}

if(typeClass == H5T_INTEGER)
{
IntType intType = dataSet.getIntType();

// get order of the int type
H5std_string order_string;
H5T_order_t order = intType.getOrder(order_string);

// get size of int type
size_t size = intType.getSize();

}

// read raw data (timestamp + imageid)
// go to dataset with the imageid
// read the dataset behind image id
// = protobuf file

// create buffer for reading the data
int64_t buffer[dimsOut[0]][dimsOut[1]];

// initialize
for(int i = 0; i<dimsOut[0]; i++)
{
for(int j = 0; j<dimsOut[1]; j++)
{
buffer[i][j] = 0;
}
}


// read buffer -> without input for memory data space the whole data space will be read

dataSet.read(buffer, PredType::NATIVE_INT64);

// read the image ids from the buffer

// vector which holds all the image ids
std::vector<int> vecImages;
for(int i = 0; i<dimsOut[0]; i++)
{
vecImages.push_back(buffer[i][1]);
}

// open dataset for every image id; read it
for(int i = 0; i < vecImages.size(); i++)
{
int currentId = vecImages.at(i);
std::string sId = std::to_string(currentId);
DataSet currentImage = file->openDataSet(sId);

// get the class of the datatype that is used by the dataset.
H5T_class_t typeClass = dataSet.getTypeClass();

// get the data space of the dataset for obtaining information about dimension, sizes, etc.
DataSpace dataSpace = dataSet.getSpace();

// get info about dataspace
int dimNumber = dataSpace.getSimpleExtentNdims();
hssize_t elemsNumber = dataSpace.getSimpleExtentNpoints();
bool isSimple = dataSpace.isSimple();

// get dimension size of each dimension
hsize_t dimsOut[1];
int nDims = dataSpace.getSimpleExtentDims(dimsOut, NULL);

if(typeClass != H5T_INTEGER)
{
// TODO exception handling
}


// create buffer for reading the image data
int64_t imageBuffer[dimsOut[0]];

// initialize
for(int i = 0; i<dimsOut[0]; i++)
{
imageBuffer[i] = 0;
}


// read buffer -> without input for memory data space the whole data space will be read

dataSet.read(imageBuffer, PredType::NATIVE_INT64);

// TODO give this data to protobuf deserializer

}

//close file
file->close();

} // end of try block

// catch failure caused by the H5File operations
catch( FileIException error )
{
error.printError();
return -1;
}

// catch failure caused by the DataSet operations
catch( DataSetIException error )
{
error.printError();
return -1;
}

// catch failure caused by the DataSpace operations
catch( DataSpaceIException error )
{
error.printError();
return -1;
}

// catch failure caused by the DataSpace operations
catch( DataTypeIException error )
{
error.printError();
return -1;
}

return 0;
}

0 comments on commit 5621a46

Please sign in to comment.