### Basic CMake Setup for Fortran HDF5 Examples Source: https://github.com/hdfgroup/hdf5/blob/develop/HDF5Examples/FORTRAN/H5G/CMakeLists.txt Initializes CMake for a Fortran project and sets the minimum required version. This is a standard starting point for CMake projects. ```cmake cmake_minimum_required (VERSION 3.26) project (HDF5Examples_FORTRAN_H5G Fortran) ``` -------------------------------- ### Copy Example Data Files (Common) Source: https://github.com/hdfgroup/hdf5/blob/develop/HDF5Examples/FORTRAN/H5T/CMakeLists.txt Copies common example data files to the build directory if tools are provided. This is a general setup for most examples. ```cmake if (HDF5_PROVIDES_TOOLS) foreach (example_name ${common_examples}) add_custom_command ( TARGET ${EXAMPLE_VARNAME}_f90_${example_name} POST_BUILD COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${PROJECT_SOURCE_DIR}/tfiles/18/${example_name}.ddl ${PROJECT_BINARY_DIR}/${example_name}.ddl ) endforeach () endif () ``` -------------------------------- ### Modern CMake Example Video Link Source: https://github.com/hdfgroup/hdf5/wiki/2024 Offers a link to a video presentation on modern CMake, serving as a visual guide for implementing updated build system conventions. ```url https://www.youtube.com/watch?v=mn1ZnO3MtVk&t=12s&ab_channel=NDCConferences ``` -------------------------------- ### Run HDF5 Examples with ctest (Custom Options) Source: https://github.com/hdfgroup/hdf5/blob/develop/HDF5Examples/Using_CMake.txt Execute the ctest script with custom options for source name and installation directory. Results are logged to test.log. ```bash ctest -S HDF5_Examples.cmake,CTEST_SOURCE_NAME=MyExamples,INSTALLDIR=MyLocation -C Release -VV -O test.log ``` -------------------------------- ### Run HDF5 Examples with CMake Source: https://github.com/hdfgroup/hdf5/blob/develop/release_docs/RELEASE_PROCESS.md Execute HDF5 examples using ctest with specified configurations. Ensure CMake and HDF5 are installed in default locations. The script can be customized with options for source directory, installation path, build configuration, and library type (static/shared). ```bash ctest -S HDF5_Examples.cmake -C Release -V -O test.log ``` ```bash ctest -S HDF5_Examples.cmake,CTEST_SOURCE_NAME=MyExamples,INSTALLDIR=MyLocation -C Release -V -O test.log ``` -------------------------------- ### List Available HDF5 Examples Source: https://github.com/hdfgroup/hdf5/blob/develop/docs/doxygen/dox/cookbook/MavenArtifacts.dox Run this command to list the available example programs included in the HDF5 Java examples artifact. This helps in identifying specific examples to run. ```bash # List available examples java -cp hdf5-java-examples-2.0.0.jar examples.intro.H5_CreateFile ``` -------------------------------- ### Create Dataset Example - C++ Source: https://github.com/hdfgroup/hdf5/blob/develop/docs/doxygen/dox/CppAPIIntro.dox Example demonstrating how to create a dataset using the HDF5 C++ API. This is part of the tutorial examples. ```cpp #include "H5Cpp.h" #include int main() { // Turn off the auto-printing of the C++ exception mechanism // that occurs when H5::Exception::throwMe() is called. Exception::dontPrint(); try { // Turn on the auto-printing of the C++ exception mechanism // that occurs when H5::Exception::throwMe() is called. Exception::dontPrint(); // Use the static fileid for H5File H5::H5File file("h5tutr_crtdat.cpp", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); // Create a dataspace. 3 dimensions, size 10 in each dimension. hsize_t dims[3] = {10, 10, 10}; H5::DataSpace dataspace(3, dims); // Create a dataset H5::DataSet dataset = file.createDataSet("MyDataset", H5::PredType::NATIVE_INT, dataspace, H5P_DEFAULT); // Close the dataset and the file dataset.close(); file.close(); } catch (const H5::Exception &err) { // catch H5 exceptions err.printError(); return 1; } catch (const std::exception &err) { // catch other std:: exceptions std::cerr << err.what() << std::endl; return 1; } catch (...) { // catch other exceptions std::cerr << "Unknown exception" << std::endl; return 1; } return 0; } ``` -------------------------------- ### Copy Example Files with CMake Source: https://github.com/hdfgroup/hdf5/blob/develop/HDF5Examples/FORTRAN/H5G/CMakeLists.txt Uses a custom command to copy example HDF5 files if they differ. This ensures that example files are available in the build directory for testing. ```cmake set (exfiles h5ex_g_iterate h5ex_g_traverse h5ex_g_visit ) foreach (example ${exfiles}) add_custom_command ( TARGET ${EXAMPLE_VARNAME}_f90_${example} POST_BUILD COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${PROJECT_SOURCE_DIR}/${example}.h5 ${PROJECT_BINARY_DIR}/${example}.h5 ) endforeach () ``` -------------------------------- ### Install HDF5 on macOS Source: https://github.com/hdfgroup/hdf5/blob/develop/docs/INSTALL_CMake.md Open the DMG file to install HDF5 on macOS. The installation process will guide you through the setup. ```bash HDF5-2.X.Y-Darwin.dmg ``` -------------------------------- ### Quick Start with CMake Presets Source: https://github.com/hdfgroup/hdf5/blob/develop/docs/README_HPC.md Recommended for general builds using CMake 3.26 or greater. Use '--fresh' for a clean build environment. ```bash cd hdf5 cmake --workflow --preset ci-StdShar-GNUC --fresh ``` -------------------------------- ### Build HDF5 Examples from Command Line Source: https://github.com/hdfgroup/hdf5/blob/develop/HDF5Examples/Using_CMake.txt Build the HDF5 examples from the command line after configuration. The --config option may be optional depending on the platform. ```bash cmake --build . --config {Debug | Release} ``` -------------------------------- ### Alternative Command Line Example using pkg-config Source: https://github.com/hdfgroup/hdf5/blob/develop/HDF5Examples/Using_CMake.txt Demonstrates an alternative method for testing HDF5 examples using shell scripts and pkg-config. Ensure HDF5_HOME is set correctly and paths are valid. ```bash export HDF5_HOME=/HDF_Group/HDF5/2.0.0 cd /HDF5Examples/ sh ./test-pc.sh /HDF5Examples/ /H5EXAMPLE_BUILD/ . ``` -------------------------------- ### HDF5 VFD Configuration Example Source: https://github.com/hdfgroup/hdf5/wiki/2025 This example demonstrates a comma-separated configuration string for multiple VFDs, specifying driver, page size, buffer size, and mode for each. It's an alternative to Lisp-like syntax. ```text vfd_configs[3]{driver,page_size,buffer_size,mode}: page_buffer,4096,65536,LRU encryption,4096,32768,AES-GCM sec2,0,0,default ``` -------------------------------- ### Custom Install Directory for HDF5 on Linux Source: https://github.com/hdfgroup/hdf5/blob/develop/docs/INSTALL_CMake.md Specify a custom installation directory for HDF5 on Linux by adding the INSTALLDIR option to the ctest command. This example sets the install directory to '/usr/local/myhdf5'. ```bash ctest -S HDF5config.cmake,INSTALLDIR=/usr/local/myhdf5,BUILD_GENERATOR=Unix -C Release -VV -O hdf5.log ``` -------------------------------- ### Create HDF5 Dataset and Select/Read Subset Example Source: https://github.com/hdfgroup/hdf5/blob/develop/docs/doxygen/dox/LearnBasics.dox Demonstrates creating an HDF5 file and dataset, then selecting and reading a subset from the dataset. Available in C, Fortran, C++, and Java. ```C #include "hdf5.h" int main() { hid_t file_id, dataset_id, dataspace_id, mem_dataspace_id; hsize_t dims[2] = { 10, 10 }; hsize_t subset_dims[2] = { 5, 5 }; int *data; int i, j; /* Create a new file using the default properties. */ file_id = H5Fcreate("h5_subset.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* Create a dataset that is rank 2 and has dimensions 10x10. */ dataset_id = H5Dcreate(file_id, "dset", H5T_NATIVE_INT, H5S_UNLIMITED, H5P_DEFAULT, H5P_DEFAULT); /* Create a dataspace for the dataset. */ dataspace_id = H5Dget_space(dataset_id); /* Create a dataspace for the subset. */ mem_dataspace_id = H5Screate_simple(2, subset_dims, NULL); /* Select a 5x5 subset of the dataset. */ H5Sselect_hyperslab(dataspace_id, H5S_SELECT_SET, subset_dims, NULL, subset_dims, NULL); /* Allocate memory for the subset. */ data = (int *)malloc(25 * sizeof(int)); /* Initialize data and write it to the subset. */ for (i = 0; i < 25; i++) data[i] = i; H5Dwrite(dataset_id, H5T_NATIVE_INT, mem_dataspace_id, dataspace_id, H5P_DEFAULT, data); /* Read data back from the subset. */ H5Dread(dataset_id, H5T_NATIVE_INT, mem_dataspace_id, dataspace_id, H5P_DEFAULT, data); /* Print the data. */ for (i = 0; i < 5; i++) { for (j = 0; j < 5; j++) printf("%d ", data[i * 5 + j]); printf("\n"); } /* Terminate access to the dataspace and release resources. */ H5Sclose(mem_dataspace_id); H5Sclose(dataspace_id); /* Terminate access to the dataset and release resources. */ H5Dclose(dataset_id); /* Terminate access to the file. */ H5Fclose(file_id); return 0; } ``` ```Fortran program h5_subset use hdf5 implicit none integer(kind=h5fid_t) :: file_id integer(kind=h5did_t) :: dataset_id integer(kind=h5sid_t) :: dataspace_id integer(kind=h5sid_t) :: mem_dataspace_id integer(kind=hsize_t), dimension(2) :: dims = [10, 10] integer(kind=hsize_t), dimension(2) :: subset_dims = [5, 5] integer, dimension(25) :: data integer :: i, j ! Create a new file using the default properties. call h5fcreate_f("h5_subset.h5", h5f_acc_trunc_f, file_id) ! Create a dataset that is rank 2 and has dimensions 10x10. call h5dcreate_f(file_id, "dset", "H5T_NATIVE_INT", dims, dataset_id) ! Create a dataspace for the dataset. call h5dget_space_f(dataset_id, dataspace_id) ! Create a dataspace for the subset. call h5screate_simple_f(2, subset_dims, mem_dataspace_id) ! Select a 5x5 subset of the dataset. call h5sselect_hyperslab_f(dataspace_id, h5s_select_set_f, subset_dims, start=[1, 1], stride=[1, 1], count=subset_dims) ! Initialize data and write it to the subset. do i = 1, 25 data(i) = i - 1 end do call h5dwrite_f(dataset_id, "H5T_NATIVE_INT", mem_dataspace_id, dataspace_id, data) ! Read data back from the subset. call h5dread_f(dataset_id, "H5T_NATIVE_INT", mem_dataspace_id, dataspace_id, data) ! Print the data. do i = 1, 5 do j = 1, 5 print *, data(i * 5 + j - 5) end do print * end do ! Terminate access to the dataspace and release resources. call h5sclose_f(mem_dataspace_id) call h5sclose_f(dataspace_id) ! Terminate access to the dataset and release resources. call h5dclose_f(dataset_id) ! Terminate access to the file. call h5fclose_f(file_id) end program h5_subset ``` ```C++ #include "H5Cpp.h" int main() { H5std_string filename("h5tutr_subset.cpp.h5"); H5std_string datasetname("dset"); int rank = 2; hsize_t dims[2] = { 10, 10 }; hsize_t subset_dims[2] = { 5, 5 }; int *data; int i, j; try { // Turn off the auto-printing of exceptions. Exception::dontPrint(); // Create a new file using the default file access properties. H5File file( filename, H5F_ACC_TRUNC ); // Create the dataset. DataSpace dataspace( rank, dims ); DataSet dataset = file.createDataSet( datasetname, PredType::NATIVE_INT, dataspace ); // Get the dataspace of the dataset. DataSpace dataset_dataspace = dataset.getSpace(); // Create a dataspace for the subset. DataSpace mem_dataspace( rank, subset_dims ); // Select a 5x5 subset of the dataset. dataset_dataspace.selectHyperslab( H5S_SELECT_SET, subset_dims, NULL, NULL, subset_dims, NULL ); // Allocate memory for the subset. data = new int[25]; // Initialize data and write it to the subset. for (i = 0; i < 25; i++) data[i] = i; dataset.write( data, PredType::NATIVE_INT, mem_dataspace, dataset_dataspace ); // Read data back from the subset. dataset.read( data, PredType::NATIVE_INT, mem_dataspace, dataset_dataspace ); // Print the data. for (i = 0; i < 5; i++) { for (j = 0; j < 5; j++) printf("%d ", data[i * 5 + j]); printf("\n"); } // Terminate access to the dataspace and release resources. mem_dataspace.close(); dataset_dataspace.close(); // Terminate access to the dataset and release resources. dataset.close(); // Close the file. file.close(); } catch (const FileIException &err) { // catch potential exceptions err.printError(); return -1; } catch (const DataSetException &err) { // catch potential exceptions err.printError(); return -1; } catch (const DataSpaceException &err) { // catch potential exceptions err.printError(); return -1; } catch (const DataTypeException &err) { // catch potential exceptions err.printError(); return -1; } return 0; } ``` ```Java import ncsa.hdf.hdf5lib.H5; import ncsa.hdf.hdf5lib.HDF5Constants; public class HDF5SubsetSelect { public static void main(String[] args) { long file_id = H5.H5Fcreate("HDF5SubsetSelect.h5", HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); long dataspace_id = H5.H5Screate_simple(2, new long[]{10, 10}, HDF5Constants.H5P_DEFAULT); long dataset_id = H5.H5Dcreate(file_id, "dset", HDF5Constants.H5T_NATIVE_INT, dataspace_id, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); long dataset_dataspace_id = H5.H5Dget_space(dataset_id); long mem_dataspace_id = H5.H5Screate_simple(2, new long[]{5, 5}, HDF5Constants.H5P_DEFAULT); H5.H5Sselect_hyperslab(dataset_dataspace_id, HDF5Constants.H5S_SELECT_SET, new long[]{0, 0}, new long[]{1, 1}, new long[]{5, 5}, null); int[] data = new int[25]; for (int i = 0; i < 25; i++) { data[i] = i; } H5.H5Dwrite(dataset_id, HDF5Constants.H5T_NATIVE_INT, mem_dataspace_id, dataset_dataspace_id, HDF5Constants.H5P_DEFAULT, data); H5.H5Dread(dataset_id, HDF5Constants.H5T_NATIVE_INT, mem_dataspace_id, dataset_dataspace_id, HDF5Constants.H5P_DEFAULT, data); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { System.out.print(data[i * 5 + j] + " "); } System.out.println(); } H5.H5Sclose(mem_dataspace_id); H5.H5Sclose(dataset_dataspace_id); H5.H5Sclose(dataspace_id); H5.H5Dclose(dataset_id); H5.H5Fclose(file_id); } } ``` -------------------------------- ### Set HDF5_ROOT Environment Variable (Windows Example) Source: https://github.com/hdfgroup/hdf5/blob/develop/docs/doxygen/dox/LearnBasics3.dox Example of setting the HDF5_ROOT environment variable on Windows to point to the HDF5 installation directory for CMake. ```text HDF5_ROOT=C:/Program Files/HDF_Group/HDF5/z.y.x ``` -------------------------------- ### Example CMake Command Line Configuration on Windows Source: https://github.com/hdfgroup/hdf5/blob/develop/docs/USING_CMake_Examples.md Configure HDF5 examples on Windows using CMake with specific generator and build options. Ensure you are in the build directory. ```cmd cmake -G "Visual Studio 16 2019" -DH5EXAMPLE_BUILD_TESTING:BOOL=ON -DBUILD_SHARED_LIBS:BOOL=ON .. ``` -------------------------------- ### Special Test Setup for Lite Examples Source: https://github.com/hdfgroup/hdf5/blob/develop/HDF5Examples/C/HL/CMakeLists.txt Configures specific tests for 'h5ex_lite1' and 'h5ex_lite2', including a common clearall command and conditional setup for memory checker. ```cmake add_test ( NAME ${EXAMPLE_VARNAME}_h5ex_lite-clearall COMMAND ${CMAKE_COMMAND} -E remove h5ex_lite1.h5 ) if (HDF5_ENABLE_USING_MEMCHECKER) add_test (NAME ${EXAMPLE_VARNAME}_h5ex_lite1 COMMAND $) set_tests_properties (${EXAMPLE_VARNAME}_h5ex_lite1 PROPERTIES DEPENDS ${EXAMPLE_VARNAME}_h5ex_lite-clearall ) add_test (NAME ${EXAMPLE_VARNAME}_h5ex_lite2 COMMAND $) set_tests_properties (${EXAMPLE_VARNAME}_h5ex_lite2 PROPERTIES DEPENDS ${EXAMPLE_VARNAME}_h5ex_lite1 ) else () add_test ( NAME ${EXAMPLE_VARNAME}_h5ex_lite1 COMMAND "${CMAKE_COMMAND}" ``` -------------------------------- ### Add HDF5 Example Tests Source: https://github.com/hdfgroup/hdf5/blob/develop/HDF5Examples/CXX/TUTR/CMakeLists.txt Defines CMake tests for HDF5 C++ examples, including setup for memory checking and a generic test runner script. Ensures tests clean up generated files. ```cmake if (H5EXAMPLE_BUILD_TESTING) macro (ADD_H5_TEST testname) add_test ( NAME ${EXAMPLE_VARNAME}_cpp_ex_${testname}-clearall COMMAND ${CMAKE_COMMAND} -E remove ${testname}.h5 ) if (HDF5_ENABLE_USING_MEMCHECKER) add_test (NAME ${EXAMPLE_VARNAME}_cpp_ex_${testname} COMMAND $) set_tests_properties (${EXAMPLE_VARNAME}_cpp_ex_${testname} PROPERTIES DEPENDS ${EXAMPLE_VARNAME}_cpp_ex_${testname}-clearall) else () add_test ( NAME ${EXAMPLE_VARNAME}_cpp_ex_${testname} COMMAND "${CMAKE_COMMAND}" -D "TEST_PROGRAM=$" -D "TEST_ARGS:STRING=" -D "TEST_FOLDER=${PROJECT_BINARY_DIR}" -D "TEST_EXPECT=0" -D "TEST_SKIP_COMPARE=TRUE" -D "TEST_OUTPUT=${testname}.out" -D "TEST_LIBRARY_DIRECTORY=${CMAKE_TEST_LIB_DIRECTORY}" -P "${H5EXAMPLE_RESOURCES_DIR}/runTest.cmake" ) set_tests_properties (${EXAMPLE_VARNAME}_cpp_ex_${testname} PROPERTIES DEPENDS ${EXAMPLE_VARNAME}_cpp_ex_${testname}-clearall ) endif () if (last_test) set_tests_properties (${EXAMPLE_VARNAME}_cpp_ex_${testname} PROPERTIES DEPENDS ${last_test}) endif () set (last_test "${EXAMPLE_VARNAME}_cpp_ex_${testname}") endmacro () foreach (example_name ${examples}) ADD_H5_TEST (${example_name}) endforeach () endif () ``` -------------------------------- ### Create JAR Manifest for Examples Source: https://github.com/hdfgroup/hdf5/blob/develop/HDF5Examples/JAVA/compat/H5T/CMakeLists.txt Writes a manifest file for each Java example, specifying the main class and class path. ```cmake foreach (example ${HDF_JAVA_EXAMPLES}) get_filename_component (example_name ${example} NAME_WE) file (WRITE ${PROJECT_BINARY_DIR}/${example_name}_Manifest.txt "Main-Class: ${example_name} Class-Path: ${HDFJAVA_CLASSJARS} " ) add_jar (${EXAMPLE_VARNAME}JC_${example_name} SOURCES ${example} MANIFEST ${PROJECT_BINARY_DIR}/${example_name}_Manifest.txt ) get_target_property (${EXAMPLE_VARNAME}JC_${example_name}_JAR_FILE ${EXAMPLE_VARNAME}JC_${example_name} JAR_FILE) get_target_property (${EXAMPLE_VARNAME}JC_${example_name}_CLASSPATH ${EXAMPLE_VARNAME}JC_${example_name} CLASSDIR) if (H5EXAMPLE_JAVA_LIBRARIES) add_dependencies (${EXAMPLE_VARNAME}JC_${example_name} ${H5EXAMPLE_JAVA_LIBRARIES}) endif () endforeach () ``` -------------------------------- ### Example CMake Configuration for Non-Standard Argobots Installation Source: https://github.com/hdfgroup/hdf5/blob/develop/docs/doxygen/dox/cmake-vols-fetchcontent.dox Use these CMake definitions to help CMake find an Argobots installation located in a non-standard directory. Ensure the paths to the include directory and library are correctly specified. ```cmake -DABT_INCLUDE_DIR=/path/to/argobots/build/include -DABT_LIBRARY=/path/to/argbots/build/lib/libabt.so ``` -------------------------------- ### Create Chunked and Compressed Dataset Example Source: https://github.com/hdfgroup/hdf5/blob/develop/docs/doxygen/dox/LearnBasics.dox Illustrates the creation of a dataset that is both chunked and compressed. Examples are provided for C, Fortran, C++, Java, and Python. ```C /* C example for creating a chunked and compressed dataset */ /* See h5_cmprss.c for full code */ ``` ```Fortran ! Fortran example for creating a chunked and compressed dataset ! See h5_cmprss.f90 for full code ``` ```C++ // C++ example for creating a chunked and compressed dataset // See h5tutr_cmprss.cpp for full code ``` ```Java // Java example for creating a chunked and compressed dataset ``` ```Python # Python example for creating a chunked and compressed dataset ``` -------------------------------- ### Setup HDF5 Example Test Fixtures Source: https://github.com/hdfgroup/hdf5/blob/develop/HDF5Examples/C/TUTR/CMakeLists.txt Configures CMake to manage test fixtures for HDF5 C examples. It defines lists of output files to be cleaned before and after test runs, and sets up 'clear' and 'clean' tests to manage these files. ```cmake if (H5EXAMPLE_BUILD_TESTING) file (MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/red ${PROJECT_BINARY_DIR}/blue ${PROJECT_BINARY_DIR}/u2w) set (${EXAMPLE_VARNAME}_tutr_CLEANFILES Attributes.h5 btrees_file.h5 cmprss.h5 default_file.h5 dset.h5 extend.h5 extlink_prefix_source.h5 extlink_source.h5 extlink_target.h5 group.h5 groups.h5 hard_link.h5 mount1.h5 mount2.h5 one_index_file.h5 only_dspaces_and_attrs_file.h5 only_huge_mesgs_file.h5 REF_REG.h5 refere.h5 refer_deprec.h5 refer_extern1.h5 refer_extern2.h5 SDS.h5 SDScompound.h5 SDSextendible.h5 Select.h5 separate_indexes_file.h5 small_lists_file.h5 soft_link.h5 subset.h5 unix2win.h5 blue/prefix_target.h5 red/prefix_target.h5 u2w/u2w_target.h5 ) # Remove any output file left over from previous test run add_test ( NAME ${EXAMPLE_VARNAME}_tutr-clear-objects COMMAND ${CMAKE_COMMAND} -E remove ${${EXAMPLE_VARNAME}_tutr_CLEANFILES} ) set_tests_properties (${EXAMPLE_VARNAME}_tutr-clear-objects PROPERTIES FIXTURES_SETUP clear_${EXAMPLE_VARNAME}_tutr WORKING_DIRECTORY ${PROJECT_BINARY_DIR} ) add_test ( NAME ${EXAMPLE_VARNAME}_tutr-clean-objects COMMAND ${CMAKE_COMMAND} -E remove ${${EXAMPLE_VARNAME}_tutr_CLEANFILES} ) set_tests_properties (${EXAMPLE_VARNAME}_tutr-clean-objects PROPERTIES FIXTURES_CLEANUP clear_${EXAMPLE_VARNAME}_tutr WORKING_DIRECTORY ${PROJECT_BINARY_DIR} ) macro (ADD_H5_TEST testname) if (HDF5_USING_ANALYSIS_TOOL) add_test (NAME ${EXAMPLE_VARNAME}_tutr_${testname} COMMAND $) else () add_test ( NAME ${EXAMPLE_VARNAME}_tutr_${testname} COMMAND "${CMAKE_COMMAND}" -D "TEST_PROGRAM=$" -D "TEST_ARGS:STRING=" -D "TEST_FOLDER=${PROJECT_BINARY_DIR}" -D "TEST_EXPECT=0" -D "TEST_SKIP_COMPARE=TRUE" -D "TEST_OUTPUT=${testname}.out" -D "TEST_LIBRARY_DIRECTORY=${CMAKE_TEST_LIB_DIRECTORY}" -P "${H5EXAMPLE_RESOURCES_DIR}/runTest.cmake" ) endif () set_tests_properties (${EXAMPLE_VARNAME}_tutr_${testname} PROPERTIES FIXTURES_REQUIRED clear_${EXAMPLE_VARNAME}_tutr ) if (last_test) set_tests_properties (${EXAMPLE_VARNAME}_tutr_${testname} PROPERTIES DEPENDS ${last_test}) endif () set (last_test "${EXAMPLE_VARNAME}_tutr_${testname}") endmacro () foreach (example_name ${examples}) ADD_H5_TEST (${example_name}) endforeach () endif () ``` -------------------------------- ### Get Dataset Offset Source: https://github.com/hdfgroup/hdf5/blob/develop/c++/src/C2Cppfunction_map.htm Retrieves the offset of the dataset within the HDF5 file. This indicates the starting position of the dataset's data. ```C++ haddr_t DataSet::getOffset() ``` -------------------------------- ### Test HDF5 Examples with pkg-config Wrappers Source: https://github.com/hdfgroup/hdf5/blob/develop/HDF5Examples/README.md Execute the test-pc.sh script to test examples using h5*cc pkg-config wrappers. Ensure the correct paths are provided. ```bash cd export HDF5_HOME="hdf5 installation root"; sh ./test-pc.sh . ``` -------------------------------- ### Get Selection Bounds Source: https://github.com/hdfgroup/hdf5/blob/develop/c++/src/C2Cppfunction_map.htm Use DataSpace::getSelectBounds to determine the bounding box of the current selection within the dataspace. It returns the start and end coordinates of the bounding box. ```c void DataSpace::getSelectBounds (hsize_t* start, hsize_t* end) ``` -------------------------------- ### Compile C Example with h5cc Source: https://github.com/hdfgroup/hdf5/blob/develop/HDF5Examples/README.md Compile a C example file using the h5cc wrapper. ```bash h5cc -o example1 example1.c ``` -------------------------------- ### Add HDF5 Example Test Macro Source: https://github.com/hdfgroup/hdf5/blob/develop/HDF5Examples/C/H5VDS/CMakeLists.txt Defines a CMake macro to add tests for HDF5 examples. It includes setup for clearing test files, running the test program, and optionally using H5DUMP for data description language output. This macro is intended for use within the HDF5 build system. ```cmake macro (ADD_H5_TEST testname) add_test ( NAME ${EXAMPLE_VARNAME}_${testname}-clearall COMMAND ${CMAKE_COMMAND} -E remove ${testname}*.h5 ) if (HDF5_ENABLE_USING_MEMCHECKER) add_test (NAME ${EXAMPLE_VARNAME}_${testname} COMMAND $) set_tests_properties (${EXAMPLE_VARNAME}_${testname} PROPERTIES DEPENDS ${EXAMPLE_VARNAME}_${testname}-clearall) else () add_test ( NAME ${EXAMPLE_VARNAME}_${testname} COMMAND "${CMAKE_COMMAND}" -D "TEST_PROGRAM=$" -D "TEST_ARGS:STRING=" -D "TEST_FOLDER=${PROJECT_BINARY_DIR}" -D "TEST_EXPECT=0" -D "TEST_OUTPUT=${testname}.out" -D "TEST_REFERENCE=${testname}.tst" -D "TEST_LIBRARY_DIRECTORY=${CMAKE_TEST_LIB_DIRECTORY}" -P "${H5EXAMPLE_RESOURCES_DIR}/runTest.cmake" ) set_tests_properties (${EXAMPLE_VARNAME}_${testname} PROPERTIES DEPENDS ${EXAMPLE_VARNAME}_${testname}-clearall ) if (HDF5_PROVIDES_TOOLS) add_test ( NAME ${EXAMPLE_VARNAME}_H5DUMP-${testname} COMMAND "${CMAKE_COMMAND}" -D "TEST_PROGRAM=${H5EXAMPLE_HDF5_DUMP_EXECUTABLE}" -D "TEST_ARGS:STRING=${ARGN};${testname}.h5" -D "TEST_FOLDER=${PROJECT_BINARY_DIR}" -D "TEST_OUTPUT=${testname}.ddl.out" -D "TEST_EXPECT=0" -D "TEST_REFERENCE=${testname}.ddl" -D "TEST_LIBRARY_DIRECTORY=${CMAKE_TEST_LIB_DIRECTORY}" -P "${H5EXAMPLE_RESOURCES_DIR}/runTest.cmake" ) set_tests_properties (${EXAMPLE_VARNAME}_H5DUMP-${testname} PROPERTIES DEPENDS ${EXAMPLE_VARNAME}_${testname} ) endif () endif () endmacro () ``` -------------------------------- ### Compile Fortran Example with h5fc Source: https://github.com/hdfgroup/hdf5/blob/develop/HDF5Examples/README.md Compile a Fortran example file using the h5fc wrapper. ```bash h5fc -o example3 example3.f90 ``` -------------------------------- ### Test HDF5 Applications (Command Line) Source: https://github.com/hdfgroup/hdf5/blob/develop/docs/USING_HDF5_CMake.md Navigate to your build directory and run this command to test the HDF5 build. The `-C` flag may be optional and should match the build configuration. ```bash ctest . -C {Debug | Release} ``` -------------------------------- ### h5dump Output: Subset Selection Details Source: https://github.com/hdfgroup/hdf5/blob/develop/docs/doxygen/dox/ViewTools.dox Example output from h5dump showing the details of a subset selection, including start, stride, count, and block parameters, along with the extracted data. ```text HDF5 "OMI-Aura.he5" { DATASET "HDFEOS/GRIDS/OMI Column Amount O3/Data Fields/SolarZenithAngle" { DATATYPE H5T_IEEE_F32LE DATASPACE SIMPLE { ( 720, 1440 ) / ( 720, 1440 ) } SUBSET { START ( 1, 0 ); STRIDE ( 2, 3 ); COUNT ( 3, 3 ); BLOCK ( 2, 3 ); DATA { (1,0): 79.071, 79.071, 79.071, 79.071, 79.071, 79.071, 79.071, 79.071, 79.071, (2,0): 78.867, 78.867, 78.867, 78.867, 78.867, 78.867, 78.867, 78.867, 78.867, (3,0): 78.632, 78.632, 78.632, 78.632, 78.632, 78.632, 78.632, 78.632, 78.632, (4,0): 78.429, 78.429, 78.429, 78.429, 78.429, 78.429, 78.429, 78.429, 78.429, (5,0): 78.225, 78.225, 78.225, 78.225, 78.225, 78.225, 78.225, 78.225, 78.225, (6,0): 78.021, 78.021, 78.021, 78.021, 78.021, 78.021, 78.021, 78.021, 78.021 } } } } ``` -------------------------------- ### h5dump Output with Hyperslab Selection Source: https://github.com/hdfgroup/hdf5/blob/develop/docs/doxygen/dox/ViewTools.dox This is an example of the output format when using h5dump with hyperslab selections, showing the defined START, STRIDE, COUNT, and BLOCK parameters along with the extracted data. ```text HDF5 "OMI-Aura.he5" { DATASET "HDFEOS/GRIDS/OMI Column Amount O3/Data Fields/SolarZenithAngle" { DATATYPE H5T_IEEE_F32LE DATASPACE SIMPLE { ( 720, 1440 ) / ( 720, 1440 ) } SUBSET { START ( 0, 0 ); STRIDE ( 1, 1 ); COUNT ( 15, 10 ); BLOCK ( 1, 1 ); DATA { (0,0): 79.403, 79.403, 79.403, 79.403, 79.403, 79.403, 79.403, 79.403, 79.403, 79.403, (1,0): 79.071, 79.071, 79.071, 79.071, 79.071, 79.071, 79.071, 79.071, 79.071, 79.071, (2,0): 78.867, 78.867, 78.867, 78.867, 78.867, 78.867, 78.867, 78.867, 78.867, 78.867, (3,0): 78.632, 78.632, 78.632, 78.632, 78.632, 78.632, 78.632, 78.632, 78.632, 78.632, (4,0): 78.429, 78.429, 78.429, 78.429, 78.429, 78.429, 78.429, 78.429, 78.429, 78.429, (5,0): 78.225, 78.225, 78.225, 78.225, 78.225, 78.225, 78.225, 78.225, 78.225, 78.225, (6,0): 78.021, 78.021, 78.021, 78.021, 78.021, 78.021, 78.021, 78.021, 78.021, 78.021, (7,0): 77.715, 77.715, 77.715, 77.715, 77.715, 77.715, 77.715, 77.715, 77.715, 77.715, (8,0): 77.511, 77.511, 77.511, 77.511, 77.511, 77.511, 77.511, 77.511, 77.511, 77.511, (9,0): 77.658, 77.658, 77.658, 77.307, 77.307, 77.307, 77.307, 77.307, 77.307, 77.307, (10,0): 77.556, 77.556, 77.556, 77.556, 77.556, 77.556, 77.556, 77.556, 77.102, 77.102, (11,0): 78.408, 78.408, 78.408, 78.408, 78.408, 78.408, 78.408, 78.408, 77.102, 77.102, (12,0): 76.34, 78.413, 78.413, 78.413, 78.413, 78.413, 78.413, 78.413, 78.413, 78.413, (13,0): 78.107, 78.107, 78.107, 78.107, 78.107, 78.107, 78.107, 78.107, 78.107, 77.195, (14,0): 78.005, 78.005, 78.005, 78.005, 78.005, 78.005, 76.991, 76.991, 76.991, 76.991 } } } } ``` -------------------------------- ### Create HDF5 File Example Source: https://github.com/hdfgroup/hdf5/blob/develop/docs/doxygen/dox/LearnBasics.dox Demonstrates the creation of a new HDF5 file. Available in C, Fortran, C++, Java, and Python. ```C #include "hdf5.h" int main() { hid_t file_id; /* Create a new file accessible by the runtime. * H5F_ACC_TRUNC truncates the file if it already exists. */ file_id = H5Fcreate("h5_crtgrp.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* Terminate access to the file. */ H5Fclose(file_id); return 0; } ``` ```Fortran program h5_crtgrp use hdf5 implicit none integer(kind=h5fid_t) :: file_id ! Create a new file accessible by the runtime. ! H5F_ACC_TRUNC truncates the file if it already exists. call h5fcreate_f("h5_crtgrp.h5", h5f_acc_trunc_f, file_id) ! Terminate access to the file. call h5fclose_f(file_id) end program h5_crtgrp ``` ```C++ #include "H5Cpp.h" int main() { H5std_string filename("h5tutr_crtgrp.h5"); try { // Turn off the auto-printing of exceptions. Exception::dontPrint(); // Create a new file using the default file access properties. H5File file( filename, H5F_ACC_TRUNC ); // Close the file. file.close(); } catch (const FileIException &err) { // catch potential exceptions err.printError(); return -1; } catch (const DataSetException &err) { // catch potential exceptions err.printError(); return -1; } catch (const DataSpaceException &err) { // catch potential exceptions err.printError(); return -1; } catch (const DataTypeException &err) { // catch potential exceptions err.printError(); return -1; } return 0; } ``` ```Java import ncsa.hdf.hdf5lib.H5; import ncsa.hdf.hdf5lib.HDF5Constants; public class HDF5FileCreate { public static void main(String[] args) { long file_id = H5.H5Fcreate("HDF5FileCreate.h5", HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); H5.H5Fclose(file_id); } } ``` ```Python import h5py f = h5py.File('h5_crtgrp.h5', 'w') f.close() ``` -------------------------------- ### Subset h5dump Output with Shorthand Notation Source: https://github.com/hdfgroup/hdf5/blob/develop/docs/doxygen/dox/ViewTools.dox This command achieves the same subsetting as the previous example but uses the shorthand notation within the -d option. It selects the first 15x10 elements starting from (0,0). ```bash h5dump -A 0 -d "HDFEOS/GRIDS/OMI Column Amount O3/Data Fields/SolarZenithAngle[0,0;;15,10;]" -w 0 OMI-Aura.he5 ``` -------------------------------- ### Create HDF5 Dataset Example Source: https://github.com/hdfgroup/hdf5/blob/develop/docs/doxygen/dox/LearnBasics.dox Demonstrates the creation of a dataset within an HDF5 file. Available in C, Fortran, C++, Java, and Python. ```C #include "hdf5.h" int main() { hid_t file_id, dataset_id; hsize_t dims[2] = { 5, 7 }; int rank = 2; /* Create a new file using the default properties. */ file_id = H5Fcreate("h5_crtdat.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* Create a dataset that is rank 2 and has dimensions 5x7. */ dataset_id = H5Dcreate(file_id, "dset", H5T_NATIVE_INT, H5S_UNLIMITED, H5P_DEFAULT, H5P_DEFAULT); /* Terminate access to the dataset and release resources. */ H5Dclose(dataset_id); /* Terminate access to the file. */ H5Fclose(file_id); return 0; } ``` ```Fortran program h5_crtdat use hdf5 implicit none integer(kind=h5fid_t) :: file_id integer(kind=h5did_t) :: dataset_id integer(kind=hsize_t), dimension(2) :: dims = [5, 7] integer :: rank = 2 ! Create a new file using the default properties. call h5fcreate_f("h5_crtdat.h5", h5f_acc_trunc_f, file_id) ! Create a dataset that is rank 2 and has dimensions 5x7. call h5dcreate_f(file_id, "dset", "H5T_NATIVE_INT", dims, dataset_id) ! Terminate access to the dataset and release resources. call h5dclose_f(dataset_id) ! Terminate access to the file. call h5fclose_f(file_id) end program h5_crtdat ``` ```C++ #include "H5Cpp.h" int main() { H5std_string filename("h5tutr_crtdat.cpp.h5"); H5std_string datasetname("dset"); int rank = 2; hsize_t dims[2] = { 5, 7 }; try { // Turn off the auto-printing of exceptions. Exception::dontPrint(); // Create a new file using the default file access properties. H5File file( filename, H5F_ACC_TRUNC ); // Create the dataset. DataSpace dataspace( rank, dims ); DataSet dataset = file.createDataSet( datasetname, PredType::NATIVE_INT, dataspace ); // Close the dataset and close the file. dataset.close(); file.close(); } catch (const FileIException &err) { // catch potential exceptions err.printError(); return -1; } catch (const DataSetException &err) { // catch potential exceptions err.printError(); return -1; } catch (const DataSpaceException &err) { // catch potential exceptions err.printError(); return -1; } catch (const DataTypeException &err) { // catch potential exceptions err.printError(); return -1; } return 0; } ``` ```Java import ncsa.hdf.hdf5lib.H5; import ncsa.hdf.hdf5lib.HDF5Constants; public class HDF5DatasetCreate { public static void main(String[] args) { long file_id = H5.H5Fcreate("HDF5DatasetCreate.h5", HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); long dataspace_id = H5.H5Screate_simple(1, new long[]{5}, HDF5Constants.H5P_DEFAULT); long dataset_id = H5.H5Dcreate(file_id, "dset", HDF5Constants.H5T_NATIVE_INT, dataspace_id, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); H5.H5Dclose(dataset_id); H5.H5Sclose(dataspace_id); H5.H5Fclose(file_id); } } ``` ```Python import h5py import numpy as np f = h5py.File('h5_crtdat.h5', 'w') dset = f.create_dataset('dset', (5,7), 'i') f.close() ```