hdf5/c++/examples/h5group.cpp

225 lines
6.9 KiB
C++
Raw Normal View History

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* 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 creates a group in the file and dataset in the group.
* Hard link to the group object is created and the dataset is accessed
* under different names.
* Iterator function is used to find the object names in the root group.
* Note that the C++ API iterator function is not completed yet, thus
* the C version is used in this example.
*/
#include <iostream>
using std::cout;
using std::endl;
#include <string>
#include "H5Cpp.h"
using namespace H5;
2020-09-30 22:27:10 +08:00
const H5std_string FILE_NAME("Group.h5");
2019-09-19 01:11:12 +08:00
const int RANK = 2;
// Operator function
2020-09-30 22:27:10 +08:00
extern "C" herr_t file_info(hid_t loc_id, const char *name, const H5L_info2_t *linfo, void *opdata);
2020-09-30 22:27:10 +08:00
int
main(void)
{
2020-09-30 22:27:10 +08:00
hsize_t dims[2];
hsize_t cdims[2];
// 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();
/*
* Create the named file, truncating the existing one if any,
* using default create and access property lists.
*/
2020-09-30 22:27:10 +08:00
H5File *file = new H5File(FILE_NAME, H5F_ACC_TRUNC);
2019-09-19 01:11:12 +08:00
/*
* Create a group in the file
*/
2020-09-30 22:27:10 +08:00
Group *group = new Group(file->createGroup("/Data"));
2019-09-19 01:11:12 +08:00
/*
* Create dataset "Compressed Data" in the group using absolute
* name. Dataset creation property list is modified to use
* GZIP compression with the compression effort set to 6.
* Note that compression can be used only when dataset is chunked.
*/
2020-09-30 22:27:10 +08:00
dims[0] = 1000;
dims[1] = 20;
cdims[0] = 20;
cdims[1] = 20;
DataSpace *dataspace = new DataSpace(RANK, dims); // create new dspace
2020-09-30 22:27:10 +08:00
DSetCreatPropList ds_creatplist; // create dataset creation prop list
ds_creatplist.setChunk(2, cdims); // then modify it for compression
ds_creatplist.setDeflate(6);
2019-09-19 01:11:12 +08:00
/*
* Create the first dataset.
*/
2020-09-30 22:27:10 +08:00
DataSet *dataset = new DataSet(
file->createDataSet("/Data/Compressed_Data", PredType::NATIVE_INT, *dataspace, ds_creatplist));
2019-09-19 01:11:12 +08:00
/*
* Close the first dataset.
*/
delete dataset;
delete dataspace;
/*
* Create the second dataset.
*/
2020-09-30 22:27:10 +08:00
dims[0] = 500;
dims[1] = 20;
2019-09-19 01:11:12 +08:00
dataspace = new DataSpace(RANK, dims); // create second dspace
2020-09-30 22:27:10 +08:00
dataset = new DataSet(file->createDataSet("/Data/Float_Data", PredType::NATIVE_FLOAT, *dataspace));
2019-09-19 01:11:12 +08:00
delete dataset;
delete dataspace;
delete group;
delete file;
/*
* Now reopen the file and group in the file.
*/
2020-09-30 22:27:10 +08:00
file = new H5File(FILE_NAME, H5F_ACC_RDWR);
2019-09-19 01:11:12 +08:00
group = new Group(file->openGroup("Data"));
/*
* Access "Compressed_Data" dataset in the group.
*/
2020-09-30 22:27:10 +08:00
try { // to determine if the dataset exists in the group
dataset = new DataSet(group->openDataSet("Compressed_Data"));
2019-09-19 01:11:12 +08:00
}
2020-09-30 22:27:10 +08:00
catch (GroupIException not_found_error) {
2019-09-19 01:11:12 +08:00
cout << " Dataset is not found." << endl;
}
cout << "dataset \"/Data/Compressed_Data\" is open" << endl;
/*
* Close the dataset.
*/
delete dataset;
/*
* Create hard link to the Data group.
*/
2020-09-30 22:27:10 +08:00
file->link(H5L_TYPE_HARD, "Data", "Data_new");
2019-09-19 01:11:12 +08:00
/*
* We can access "Compressed_Data" dataset using created
* hard link "Data_new".
*/
2020-09-30 22:27:10 +08:00
try { // to determine if the dataset exists in the file
dataset = new DataSet(file->openDataSet("/Data_new/Compressed_Data"));
2019-09-19 01:11:12 +08:00
}
2020-09-30 22:27:10 +08:00
catch (FileIException not_found_error) {
2019-09-19 01:11:12 +08:00
cout << " Dataset is not found." << endl;
}
cout << "dataset \"/Data_new/Compressed_Data\" is open" << endl;
/*
* Close the dataset.
*/
delete dataset;
/*
* Use iterator to see the names of the objects in the file
* root directory.
*/
cout << endl << "Iterating over elements in the file" << endl;
herr_t idx = H5Literate2(file->getId(), H5_INDEX_NAME, H5_ITER_INC, NULL, file_info, NULL);
2019-09-19 01:11:12 +08:00
cout << endl;
/*
* Unlink name "Data" and use iterator to see the names
* of the objects in the file root direvtory.
*/
cout << "Unlinking..." << endl;
2020-09-30 22:27:10 +08:00
try { // attempt to unlink the dataset
file->unlink("Data");
2019-09-19 01:11:12 +08:00
}
2020-09-30 22:27:10 +08:00
catch (FileIException unlink_error) {
2019-09-19 01:11:12 +08:00
cout << " unlink failed." << endl;
}
cout << "\"Data\" is unlinked" << endl;
cout << endl << "Iterating over elements in the file again" << endl;
idx = H5Literate2(file->getId(), H5_INDEX_NAME, H5_ITER_INC, NULL, file_info, NULL);
2019-09-19 01:11:12 +08:00
cout << endl;
/*
* Close the group and file.
*/
delete group;
delete file;
2020-09-30 22:27:10 +08:00
} // end of try block
// 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;
}
// 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;
}
// 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;
}
// catch failure caused by the Attribute operations
2020-09-30 22:27:10 +08:00
catch (AttributeIException error) {
2019-09-19 01:11:12 +08:00
error.printErrorStack();
return -1;
}
return 0;
}
/*
* Operator function.
*/
herr_t
file_info(hid_t loc_id, const char *name, const H5L_info2_t *linfo, void *opdata)
{
hid_t group;
/*
* Open the group using its name.
*/
group = H5Gopen2(loc_id, name, H5P_DEFAULT);
/*
* Display group name.
*/
cout << "Name : " << name << endl;
H5Gclose(group);
return 0;
}