Skip to content

Commit

Permalink
Added group examples
Browse files Browse the repository at this point in the history
Update #318
  • Loading branch information
eugenwintersberger committed Jan 13, 2019
1 parent ae5993e commit 81ec1c9
Show file tree
Hide file tree
Showing 9 changed files with 467 additions and 0 deletions.
26 changes: 26 additions & 0 deletions examples/hdfgroup/H5G/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@

add_executable(h5ex_g_compact h5ex_g_compact.cpp)
target_link_libraries(h5ex_g_compact h5cpp)

add_executable(h5ex_g_corder h5ex_g_corder.cpp)
target_link_libraries(h5ex_g_corder h5cpp)

add_executable(h5ex_g_create h5ex_g_create.cpp)
target_link_libraries(h5ex_g_create h5cpp)

add_executable(h5ex_g_intermediate h5ex_g_intermediate.cpp)
target_link_libraries(h5ex_g_intermediate h5cpp)

add_executable(h5ex_g_phase h5ex_g_phase.cpp)
target_link_libraries(h5ex_g_phase h5cpp)

configure_file(h5ex_g_iterate.h5 h5ex_g_iterate.h5 COPYONLY)
configure_file(h5ex_g_traverse.h5 h5ex_g_traverse.h5 COPYONLY)
configure_file(h5ex_g_visit.h5 h5ex_g_visit.h5 COPYONLY)

add_executable(h5ex_g_iterate h5ex_g_iterate.cpp)
target_link_libraries(h5ex_g_iterate h5cpp)

add_executable(h5ex_g_traverse h5ex_g_traverse.cpp)
target_link_libraries(h5ex_g_traverse h5cpp)

add_executable(h5ex_g_visit h5ex_g_visit.cpp)
target_link_libraries(h5ex_g_visit h5cpp)
79 changes: 79 additions & 0 deletions examples/hdfgroup/H5G/h5ex_g_compact.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/************************************************************
This example shows how to create "compact-or-indexed"
format groups, new to 1.8. This example also illustrates
the space savings of compact groups by creating 2 files
which are identical except for the group format, and
displaying the file size of each. Both files have one
empty group in the root group.
This file is intended for use with HDF5 Library version 1.8
************************************************************/

#include <h5cpp/hdf5.hpp>

#define FILE1 "h5ex_g_compact1.h5"
#define FILE2 "h5ex_g_compact2.h5"
#define GROUP "G1"

using namespace hdf5;

void create_default_file()
{
// Create file 1. This file will use original format groups.
file::File file = file::create (FILE1, file::AccessFlags::TRUNCATE);
node::Group group (file.root(), GROUP);

// Obtain the group info and print the group storage type.
node::GroupInfo info = group.info();
std::cout<<"Group storage type for "<<FILE1<<" is: "<<info.storage_type()<<std::endl;
}

void print_default_file_size()
{
// Re-open file. Needed to get the correct file size.
file::File file = file::open (FILE1, file::AccessFlags::READONLY);

// Obtain and print the file size.
std::cout<<"File size for "<<FILE1<<" is: "<<file.size()<<" bytes"
<<std::endl<<std::endl;
}

void create_compact_file()
{
// Set file access property list to allow the latest file format.
// This will allow the library to create new compact format groups.
property::FileCreationList fcpl;
property::FileAccessList fapl;
fapl.library_version_bounds(property::LibVersion::LATEST,property::LibVersion::LATEST);

// Create file 2 using the new file access property list.
file::File file = file::create (FILE2,file::AccessFlags::TRUNCATE, fcpl, fapl);
node::Group group(file.root(), GROUP);

// Obtain the group info and print the group storage type.
node::GroupInfo info = group.info();
std::cout<<"Group storage type for "<<FILE2<<" is: "<<info.storage_type()<<std::endl;
}

void print_compact_file_size()
{
property::FileAccessList fapl;
fapl.library_version_bounds(property::LibVersion::LATEST,property::LibVersion::LATEST);
file::File file = file::open (FILE2, file::AccessFlags::READONLY, fapl);

// Obtain and print the file size.
std::cout<<"File size for "<<FILE2<<" is: "<<file.size()<<" bytes"<<std::endl<<std::endl;

}

int main (void)
{
create_default_file();
print_default_file_size();
create_compact_file();
print_compact_file_size();

return 0;
}
69 changes: 69 additions & 0 deletions examples/hdfgroup/H5G/h5ex_g_corder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/************************************************************
This example shows how to track links in a group by
creation order. The program creates a series of groups,
then reads back their names: first in alphabetical order,
then in creation order.
This file is intended for use with HDF5 Library version 1.8
************************************************************/

#include <h5cpp/hdf5.hpp>

#define FILE "h5ex_g_corder.h5"

using namespace hdf5;

int main (void)
{
// Create a new file using the default properties.
file::File file = file::create (FILE, file::AccessFlags::TRUNCATE);

// Create group creation property list and enable link creation
// order tracking. Attempting to track by creation order in a
// group that does not have this property set will result in an
// error.
property::LinkCreationList lcpl;
property::GroupCreationList gcpl;
gcpl.link_creation_order (
property::CreationOrder ().enable_tracked ().enable_indexed ());

// Create primary group using the property list.
node::Group group (file.root (), "index_group", lcpl, gcpl);

// Create subgroups in the primary group. These will be tracked
// by creation order. Note that these groups do not have to have
// the creation order tracking property set.
node::Group (group, "H");
node::Group (group, "D");
node::Group (group, "F");
node::Group (group, "5");

// Traverse links in the primary group using alphabetical indices
// (H5_INDEX_NAME).
std::cout << "Traversing group using alphabetical indices:" << std::endl
<< std::endl;
group.iterator_config ().index (IterationIndex::NAME);
group.iterator_config ().order (IterationOrder::INCREASING);
size_t index = 0;
for (node::Node node : group.nodes)
{
std::cout << "Index " << index++ << ": " << node.link ().path ().name ()
<< std::endl;
}

// Traverse links in the primary group by creation order
// (H5_INDEX_CRT_ORDER).
std::cout << std::endl << "Traversing group using creation order indices:"
<< std::endl << std::endl;
group.iterator_config ().index (IterationIndex::CREATION_ORDER);
index = 0;
for (node::Node node : group.nodes)
{
std::cout << "Index " << index++ << ": " << node.link ().path ().name ()
<< std::endl;
}

return 0;
}
30 changes: 30 additions & 0 deletions examples/hdfgroup/H5G/h5ex_g_create.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/************************************************************
This example shows how to create, open, and close a group.
This file is intended for use with HDF5 Library version 1.8
************************************************************/

#include <h5cpp/hdf5.hpp>

#define FILE "h5ex_g_create.h5"

using namespace hdf5;

int main(void)
{
// Create a new file using the default properties.
file::File file = file::create (FILE, file::AccessFlags::TRUNCATE);

// Create a group named "G1" in the file.
node::Group group (file.root(), "/G1");

// Close the group. The handle "group" can no longer be used.
group.close ();

// Re-open the group, obtaining a new handle.
group = node::get_node (file.root (), "/G1");

return 0;
}
49 changes: 49 additions & 0 deletions examples/hdfgroup/H5G/h5ex_g_intermediate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/************************************************************
This example shows how to create intermediate groups with
a single call to H5Gcreate.
This file is intended for use with HDF5 Library version 1.8
************************************************************/

#include <h5cpp/hdf5.hpp>

#define FILE "h5ex_g_intermediate.h5"

using namespace hdf5;

int main(void)
{
// Create a new file using the default properties.
file::File file = file::create (FILE, file::AccessFlags::TRUNCATE);

// Create group creation property list and set it to allow creation
// of intermediate groups.
property::LinkCreationList lcpl;
lcpl.enable_intermediate_group_creation ();

// Create the group /G1/G2/G3. Note that /G1 and /G1/G2 do not
// exist yet. This call would cause an error if we did not use the
// previously created property list.
node::Group group (file.root (), "/G1/G2/G3", lcpl);

// Print all the objects in the files to show that intermediate
// groups have been created. See h5ex_g_visit for more information
// on how to use H5Ovisit.
std::cout << "Objects in the file:" << std::endl;
node::Group root_group = file.root ();
root_group.iterator_config ().index (IterationIndex::NAME);
root_group.iterator_config ().order (IterationOrder::NATIVE);
std::for_each (node::RecursiveNodeIterator::begin (root_group),
node::RecursiveNodeIterator::end (root_group),
[](const node::Node &node)
{
std::cout<<node.link().path()<<" "<<node.type()<<std::endl;
});

return 0;
}



33 changes: 33 additions & 0 deletions examples/hdfgroup/H5G/h5ex_g_iterate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/************************************************************
This example shows how to iterate over group members using
H5Literate.
This file is intended for use with HDF5 Library version 1.8
************************************************************/

#include <h5cpp/hdf5.hpp>
#include <algorithm>

#define FILE "h5ex_g_iterate.h5"

using namespace hdf5;

int main (void)
{
// Open file.
file::File file = file::open (FILE, file::AccessFlags::READONLY);
node::Group root_group = file.root ();

// Begin iteration.
std::cout << "Objects in root group:" << std::endl;
std::for_each (root_group.nodes.begin (), root_group.nodes.end (),
[](const node::Node &node)
{
std::cout<<node.type()<<" "<<node.link().path()<<std::endl;
});

return 0;
}

84 changes: 84 additions & 0 deletions examples/hdfgroup/H5G/h5ex_g_phase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/************************************************************
This example shows how to set the conditions for
conversion between compact and dense (indexed) groups.
This file is intended for use with HDF5 Library version 1.8
************************************************************/

#include <h5cpp/hdf5.hpp>
#include <stdio.h>

#define FILE "h5ex_g_phase.h5"
#define MAX_GROUPS 7
#define MAX_COMPACT 5
#define MIN_DENSE 3

using namespace hdf5;

void print_group_info(const node::GroupInfo &info)
{
std::string group = "Group";

group += info.number_of_links () == 1 ? " " : "s";
std::cout << info.number_of_links () << group << ": Storage type is "
<< info.storage_type () << std::endl;
}

int main (void)
{
char name[3] = "G0"; /* Name of subgroup */

// Set file access property list to allow the latest file format.
// This will allow the library to create new format groups.
property::FileCreationList fcpl;
property::FileAccessList fapl;
fapl.library_version_bounds (property::LibVersion::LATEST,
property::LibVersion::LATEST);

// Create group access property list and set the phase change
// conditions. In this example we lowered the conversion threshold
// to simplify the output, though this may not be optimal.
property::LinkCreationList lcpl;
property::GroupCreationList gcpl;
gcpl.link_storage_thresholds (MAX_COMPACT, MIN_DENSE);

// Create a new file using the default properties.
file::File file = file::create (FILE, file::AccessFlags::TRUNCATE, fcpl,
fapl);

// Create primary group.
node::Group group (file.root(), "G0", lcpl, gcpl);

// Add subgroups to "group" one at a time, print the storage type
// for "group" after each subgroup is created.
for (size_t i = 1; i <= MAX_GROUPS; i++)
{

{
std::stringstream ss;
ss << "G0_" << i;
node::Group (group, ss.str ());
}

// Obtain the group info and print the group storage type
print_group_info (group.info ());
}
std::cout<<std::endl;

// Delete subgroups one at a time, print the storage type for
// "group" after each subgroup is deleted.
for (ssize_t i = MAX_GROUPS; i >= 1; i--)
{
// Define the subgroup name and delete the subgroup.
std::stringstream ss;
ss << "G0_" << i;
node::remove (group, ss.str ());

// Obtain the group info and print the group storage type
print_group_info (group.info ());
}

return 0;
}
Loading

0 comments on commit 81ec1c9

Please sign in to comment.