2013-09-20 20:16:23 +08:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
2019-09-19 01:11:12 +08:00
|
|
|
* Copyright by The HDF Group. *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
2013-09-20 20:16:23 +08:00
|
|
|
* This file is part of HDF5. The full HDF5 copyright notice, including *
|
|
|
|
* terms governing use, modification, and redistribution, is contained in *
|
2017-04-18 03:32:16 +08:00
|
|
|
* the COPYING file, which can be found at the root of the source code *
|
2021-02-17 22:52:36 +08:00
|
|
|
* distribution tree, or in https://www.hdfgroup.org/licenses. *
|
2017-04-18 03:32:16 +08:00
|
|
|
* If you do not have access to either file, you may request a copy from *
|
|
|
|
* help@hdfgroup.org. *
|
2013-09-20 20:16:23 +08:00
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This example illustrates how to read/write a subset of data (a slab)
|
|
|
|
* from/to a dataset in an HDF5 file. It is used in the HDF5 Tutorial.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
2016-11-15 06:04:27 +08:00
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2016-11-15 06:04:27 +08:00
|
|
|
#include <string>
|
2013-09-20 20:16:23 +08:00
|
|
|
#include "H5Cpp.h"
|
2016-11-15 06:04:27 +08:00
|
|
|
using namespace H5;
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
const H5std_string FILE_NAME("h5tutr_subset.h5");
|
|
|
|
const H5std_string DATASET_NAME("IntArray");
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
const int RANK = 2;
|
|
|
|
const int DIM0_SUB = 3; // subset dimensions
|
|
|
|
const int DIM1_SUB = 4;
|
|
|
|
const int DIM0 = 8; // size of dataset
|
|
|
|
const int DIM1 = 10;
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
int
|
|
|
|
main(void)
|
2013-09-20 20:16:23 +08:00
|
|
|
{
|
2020-09-30 22:27:10 +08:00
|
|
|
int i, j;
|
|
|
|
int data[DIM0][DIM1], sdata[DIM0_SUB][DIM1_SUB], rdata[DIM0][DIM1];
|
2013-09-20 20:16:23 +08:00
|
|
|
|
|
|
|
// Try block to detect exceptions raised by any of the calls inside it
|
2020-09-30 22:27:10 +08:00
|
|
|
try {
|
2019-09-19 01:11:12 +08:00
|
|
|
// Turn off the auto-printing when failure occurs so that we can
|
|
|
|
// handle the errors appropriately
|
|
|
|
Exception::dontPrint();
|
|
|
|
|
|
|
|
// ---------------------------------------------------
|
2020-04-21 07:12:00 +08:00
|
|
|
// Create a new file using the default property lists.
|
|
|
|
// Then create a dataset and write data to it.
|
2019-09-19 01:11:12 +08:00
|
|
|
// Close the file and dataset.
|
|
|
|
// ---------------------------------------------------
|
2020-04-21 07:12:00 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
H5File file(FILE_NAME, H5F_ACC_TRUNC);
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2020-04-21 07:12:00 +08:00
|
|
|
hsize_t dims[2];
|
2020-09-30 22:27:10 +08:00
|
|
|
dims[0] = DIM0;
|
|
|
|
dims[1] = DIM1;
|
|
|
|
DataSpace dataspace = DataSpace(RANK, dims);
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
DataSet dataset(file.createDataSet(DATASET_NAME, PredType::STD_I32BE, dataspace));
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
for (j = 0; j < DIM0; j++) {
|
2020-09-30 22:27:10 +08:00
|
|
|
for (i = 0; i < DIM1; i++)
|
|
|
|
if (i < (DIM1 / 2))
|
2019-09-19 01:11:12 +08:00
|
|
|
data[j][i] = 1;
|
2020-09-30 22:27:10 +08:00
|
|
|
else
|
2019-09-19 01:11:12 +08:00
|
|
|
data[j][i] = 2;
|
2020-09-30 22:27:10 +08:00
|
|
|
}
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
dataset.write(data, PredType::NATIVE_INT);
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
cout << endl << "Data Written to File:" << endl;
|
|
|
|
for (j = 0; j < DIM0; j++) {
|
|
|
|
for (i = 0; i < DIM1; i++)
|
2020-09-30 22:27:10 +08:00
|
|
|
cout << " " << data[j][i];
|
2019-09-19 01:11:12 +08:00
|
|
|
cout << endl;
|
|
|
|
}
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
dataspace.close();
|
|
|
|
dataset.close();
|
|
|
|
file.close();
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
// ---------------------------------------------------
|
|
|
|
// Reopen the file and dataset and write a subset of
|
|
|
|
// values to the dataset.
|
|
|
|
// ---------------------------------------------------
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
hsize_t offset[2], count[2], stride[2], block[2];
|
|
|
|
hsize_t dimsm[2];
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
file.openFile(FILE_NAME, H5F_ACC_RDWR);
|
|
|
|
dataset = file.openDataSet(DATASET_NAME);
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2020-04-21 07:12:00 +08:00
|
|
|
// Specify size and shape of subset to write.
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
offset[0] = 1;
|
|
|
|
offset[1] = 2;
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
count[0] = DIM0_SUB;
|
|
|
|
count[1] = DIM1_SUB;
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
stride[0] = 1;
|
|
|
|
stride[1] = 1;
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
block[0] = 1;
|
|
|
|
block[1] = 1;
|
2020-04-21 07:12:00 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
// Define Memory Dataspace. Get file dataspace and select
|
|
|
|
// a subset from the file dataspace.
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
dimsm[0] = DIM0_SUB;
|
|
|
|
dimsm[1] = DIM1_SUB;
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
DataSpace memspace(RANK, dimsm, NULL);
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
dataspace = dataset.getSpace();
|
2020-04-21 07:12:00 +08:00
|
|
|
dataspace.selectHyperslab(H5S_SELECT_SET, count, offset, stride, block);
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
// Write a subset of data to the dataset, then read the
|
|
|
|
// entire dataset back from the file.
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
cout << endl << "Write subset to file specifying: " << endl;
|
|
|
|
cout << " offset=1x2 stride=1x1 count=3x4 block=1x1" << endl;
|
|
|
|
for (j = 0; j < DIM0_SUB; j++) {
|
|
|
|
for (i = 0; i < DIM1_SUB; i++)
|
2020-09-30 22:27:10 +08:00
|
|
|
sdata[j][i] = 5;
|
2020-04-21 07:12:00 +08:00
|
|
|
}
|
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
dataset.write(sdata, PredType::NATIVE_INT, memspace, dataspace);
|
|
|
|
dataset.read(rdata, PredType::NATIVE_INT);
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2019-09-19 01:11:12 +08:00
|
|
|
cout << endl << "Data in File after Subset is Written:" << endl;
|
|
|
|
for (i = 0; i < DIM0; i++) {
|
2020-04-21 07:12:00 +08:00
|
|
|
for (j = 0; j < DIM1; j++)
|
2020-09-30 22:27:10 +08:00
|
|
|
cout << " " << rdata[i][j];
|
2019-09-19 01:11:12 +08:00
|
|
|
cout << endl;
|
|
|
|
}
|
|
|
|
cout << endl;
|
|
|
|
|
|
|
|
// It is not necessary to close these objects because close() will
|
|
|
|
// be called when the object instances are going out of scope.
|
|
|
|
dataspace.close();
|
|
|
|
memspace.close();
|
|
|
|
dataset.close();
|
|
|
|
file.close();
|
2013-09-20 20:16:23 +08:00
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
} // end of try block
|
2013-09-20 20:16:23 +08:00
|
|
|
|
|
|
|
// catch failure caused by the H5File operations
|
2020-09-30 22:27:10 +08:00
|
|
|
catch (FileIException error) {
|
2019-09-19 01:11:12 +08:00
|
|
|
error.printErrorStack();
|
|
|
|
return -1;
|
2013-09-20 20:16:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// catch failure caused by the DataSet operations
|
2020-09-30 22:27:10 +08:00
|
|
|
catch (DataSetIException error) {
|
2019-09-19 01:11:12 +08:00
|
|
|
error.printErrorStack();
|
|
|
|
return -1;
|
2013-09-20 20:16:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// catch failure caused by the DataSpace operations
|
2020-09-30 22:27:10 +08:00
|
|
|
catch (DataSpaceIException error) {
|
2019-09-19 01:11:12 +08:00
|
|
|
error.printErrorStack();
|
|
|
|
return -1;
|
2013-09-20 20:16:23 +08:00
|
|
|
}
|
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
return 0; // successfully terminated
|
2013-09-20 20:16:23 +08:00
|
|
|
}
|