My Project 1.10.8
readdata.cpp
This example shows how to read datasets.
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the COPYING file, which can be found at the root of the source code *
* distribution tree, or in https://www.hdfgroup.org/licenses. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
//
// This example reads hyperslab from the SDS.h5 file into
// two-dimensional plane of a three-dimensional array. Various
// information about the dataset in the SDS.h5 file is obtained.
//
#include <iostream>
using std::cout;
using std::endl;
#include <string>
#include "H5Cpp.h"
using namespace H5;
const H5std_string FILE_NAME("SDS.h5");
const H5std_string DATASET_NAME("IntArray");
const int NX_SUB = 3; // hyperslab dimensions
const int NY_SUB = 4;
const int NX = 7; // output buffer dimensions
const int NY = 7;
const int NZ = 3;
const int RANK_OUT = 3;
int
main(void)
{
/*
* Output buffer initialization.
*/
int i, j, k;
int data_out[NX][NY][NZ]; /* output buffer */
for (j = 0; j < NX; j++) {
for (i = 0; i < NY; i++) {
for (k = 0; k < NZ; k++)
data_out[j][i][k] = 0;
}
}
/*
* 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();
/*
* Open the specified file and the specified dataset in the file.
*/
H5File file(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 type_class = dataset.getTypeClass();
/*
* Get class of datatype and print message if it's an integer.
*/
if (type_class == H5T_INTEGER) {
cout << "Data set has INTEGER type" << endl;
/*
* Get the integer datatype
*/
IntType intype = dataset.getIntType();
/*
* Get order of datatype and print message if it's a little endian.
*/
H5std_string order_string;
H5T_order_t order = intype.getOrder(order_string);
cout << order_string << endl;
/*
* Get size of the data element stored in file and print it.
*/
size_t size = intype.getSize();
cout << "Data size is " << size << endl;
}
/*
* Get dataspace of the dataset.
*/
DataSpace dataspace = dataset.getSpace();
/*
* Get the number of dimensions in the dataspace.
*/
int rank = dataspace.getSimpleExtentNdims();
/*
* Get the dimension size of each dimension in the dataspace and
* display them.
*/
hsize_t dims_out[2];
int ndims = dataspace.getSimpleExtentDims(dims_out, NULL);
cout << "rank " << rank << ", dimensions " << (unsigned long)(dims_out[0]) << " x "
<< (unsigned long)(dims_out[1]) << endl;
/*
* Define hyperslab in the dataset; implicitly giving strike and
* block NULL.
*/
hsize_t offset[2]; // hyperslab offset in the file
hsize_t count[2]; // size of the hyperslab in the file
offset[0] = 1;
offset[1] = 2;
count[0] = NX_SUB;
count[1] = NY_SUB;
dataspace.selectHyperslab(H5S_SELECT_SET, count, offset);
/*
* Define the memory dataspace.
*/
hsize_t dimsm[3]; /* memory space dimensions */
dimsm[0] = NX;
dimsm[1] = NY;
dimsm[2] = NZ;
DataSpace memspace(RANK_OUT, dimsm);
/*
* Define memory hyperslab.
*/
hsize_t offset_out[3]; // hyperslab offset in memory
hsize_t count_out[3]; // size of the hyperslab in memory
offset_out[0] = 3;
offset_out[1] = 0;
offset_out[2] = 0;
count_out[0] = NX_SUB;
count_out[1] = NY_SUB;
count_out[2] = 1;
memspace.selectHyperslab(H5S_SELECT_SET, count_out, offset_out);
/*
* Read data from hyperslab in the file into the hyperslab in
* memory and display the data.
*/
dataset.read(data_out, PredType::NATIVE_INT, memspace, dataspace);
for (j = 0; j < NX; j++) {
for (i = 0; i < NY; i++)
cout << data_out[j][i][0] << " ";
cout << endl;
}
/*
* 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0
* 3 4 5 6 0 0 0
* 4 5 6 7 0 0 0
* 5 6 7 8 0 0 0
* 0 0 0 0 0 0 0
*/
} // end of try block
// catch failure caused by the H5File operations
catch (FileIException error) {
error.printErrorStack();
return -1;
}
// catch failure caused by the DataSet operations
catch (DataSetIException error) {
error.printErrorStack();
return -1;
}
// catch failure caused by the DataSpace operations
catch (DataSpaceIException error) {
error.printErrorStack();
return -1;
}
// catch failure caused by the DataSpace operations
catch (DataTypeIException error) {
error.printErrorStack();
return -1;
}
return 0; // successfully terminated
}
H5T_class_t getTypeClass() const
Creates an AbstractDs instance using an existing id.
Definition: H5AbstractDs.cpp:67
IntType getIntType() const
Returns the integer datatype of this abstract dataset which can be a dataset or an attribute.
Definition: H5AbstractDs.cpp:228
H5T_order_t getOrder() const
Returns the byte order of an atomic datatype.
Definition: H5AtomType.cpp:91
Class DataSet operates on HDF5 datasets.
Definition: H5DataSet.h:28
virtual DataSpace getSpace() const
Gets a copy of the dataspace of this dataset.
Definition: H5DataSet.cpp:133
void read(void *buf, const DataType &mem_type, const DataSpace &mem_space=DataSpace::ALL, const DataSpace &file_space=DataSpace::ALL, const DSetMemXferPropList &xfer_plist=DSetMemXferPropList::DEFAULT) const
Reads raw data from the specified dataset.
Definition: H5DataSet.cpp:417
Definition: H5Exception.h:131
Class DataSpace inherits from IdComponent and provides wrappers for the HDF5's dataspaces.
Definition: H5DataSpace.h:25
void selectHyperslab(H5S_seloper_t op, const hsize_t *count, const hsize_t *start, const hsize_t *stride=NULL, const hsize_t *block=NULL) const
Selects a hyperslab region to add to the current selected region.
Definition: H5DataSpace.cpp:598
int getSimpleExtentDims(hsize_t *dims, hsize_t *maxdims=NULL) const
Retrieves dataspace dimension size and maximum size.
Definition: H5DataSpace.cpp:245
int getSimpleExtentNdims() const
Returns the dimensionality of a dataspace.
Definition: H5DataSpace.cpp:263
Definition: H5Exception.h:103
size_t getSize() const
Returns the size of a datatype.
Definition: H5DataType.cpp:629
Definition: H5Exception.h:110
static void printErrorStack(FILE *stream=stderr, hid_t err_stack=H5E_DEFAULT)
Prints the error stack in a default manner.
Definition: H5Exception.cpp:315
Definition: H5Exception.h:89
Class H5File represents an HDF5 file and inherits from class Group as file is a root group.
Definition: H5File.h:25
DataSet openDataSet(const char *name, const DSetAccPropList &dapl=DSetAccPropList::DEFAULT) const
Opens an existing dataset at this location.
Definition: H5Location.cpp:1006
IntType is a derivative of a DataType and operates on HDF5 integer datatype.
Definition: H5IntType.h:25
Definition: H5AbstractDs.cpp:34


The HDF Group Help Desk:
  Copyright by The HDF Group
and the Board of Trustees of the University of Illinois