diff options
Diffstat (limited to 'java/examples/datatypes')
21 files changed, 5979 insertions, 0 deletions
diff --git a/java/examples/datatypes/CMakeLists.txt b/java/examples/datatypes/CMakeLists.txt new file mode 100644 index 0000000..6525506 --- /dev/null +++ b/java/examples/datatypes/CMakeLists.txt @@ -0,0 +1,109 @@ +cmake_minimum_required (VERSION 3.1.0) +PROJECT (HDFJAVA_EXAMPLES_DATATYPES Java) + +set (CMAKE_VERBOSE_MAKEFILE 1) + +INCLUDE_DIRECTORIES ( + ${HDF5_JAVA_JNI_BINARY_DIR} + ${HDF5_JAVA_HDF5_LIB_DIR} +) + +set (HDF_JAVA_EXAMPLES + H5Ex_T_Array + H5Ex_T_ArrayAttribute + H5Ex_T_Bit + H5Ex_T_BitAttribute + H5Ex_T_Commit + H5Ex_T_Compound + H5Ex_T_CompoundAttribute + H5Ex_T_Float + H5Ex_T_FloatAttribute + H5Ex_T_Integer + H5Ex_T_IntegerAttribute + H5Ex_T_ObjectReference + H5Ex_T_ObjectReferenceAttribute + H5Ex_T_Opaque + H5Ex_T_OpaqueAttribute + H5Ex_T_String + H5Ex_T_StringAttribute + H5Ex_T_VLString +) + +if (WIN32) + set (CMAKE_JAVA_INCLUDE_FLAG_SEP ";") +else (WIN32) + set (CMAKE_JAVA_INCLUDE_FLAG_SEP ":") +endif (WIN32) + +set (CMAKE_JAVA_INCLUDE_PATH "${HDF5_JAVA_JARS}") + +set (CMAKE_JAVA_CLASSPATH ".") +foreach (CMAKE_INCLUDE_PATH ${CMAKE_JAVA_INCLUDE_PATH}) + set (CMAKE_JAVA_CLASSPATH "${CMAKE_JAVA_CLASSPATH}${CMAKE_JAVA_INCLUDE_FLAG_SEP}${CMAKE_INCLUDE_PATH}") +endforeach (CMAKE_INCLUDE_PATH) + +foreach (example ${HDF_JAVA_EXAMPLES}) + file (WRITE ${PROJECT_BINARY_DIR}/${example}_Manifest.txt + "Main-Class: examples.datatypes.${example} +" + ) + add_jar (${example} MANIFEST ${PROJECT_BINARY_DIR}/${example}_Manifest.txt ${example}.java) + get_target_property (${example}_JAR_FILE ${example} JAR_FILE) +# install_jar (${example} ${HJAVA_INSTALL_DATA_DIR}/examples examples) + get_target_property (${example}_CLASSPATH ${example} CLASSDIR) + add_dependencies (${example} ${HDF5_JAVA_HDF5_LIB_TARGET}) +endforeach (example ${HDF_JAVA_EXAMPLES}) + +set (CMAKE_JAVA_INCLUDE_PATH "${HDF5_JAVA_JARS};${HDF5_JAVA_LOGGING_JAR};${HDF5_JAVA_LOGGING_NOP_JAR}") + +set (CMAKE_JAVA_CLASSPATH ".") +foreach (HDFJAVA_JAR ${CMAKE_JAVA_INCLUDE_PATH}) + set (CMAKE_JAVA_CLASSPATH "${CMAKE_JAVA_CLASSPATH}${CMAKE_JAVA_INCLUDE_FLAG_SEP}${HDFJAVA_JAR}") +endforeach (HDFJAVA_JAR) + +MACRO (ADD_H5_TEST resultfile resultcode) + add_test ( + NAME JAVA_datatypes-${resultfile} + COMMAND "${CMAKE_COMMAND}" + -D "TEST_TESTER=${CMAKE_Java_RUNTIME};${CMAKE_Java_RUNTIME_FLAGS}" + -D "TEST_PROGRAM=examples.datatypes.${resultfile}" + -D "TEST_ARGS:STRING=${ARGN}" + -D "TEST_CLASSPATH:STRING=${CMAKE_JAVA_CLASSPATH}${CMAKE_JAVA_INCLUDE_FLAG_SEP}${${resultfile}_JAR_FILE}" + -D "TEST_LIBRARY_DIRECTORY=${CMAKE_TEST_OUTPUT_DIRECTORY}" + -D "TEST_FOLDER=${HDFJAVA_EXAMPLES_BINARY_DIR}" + -D "TEST_OUTPUT=datatypes/${resultfile}.out" + -D "TEST_EXPECT=${resultcode}" + -D "TEST_REFERENCE=datatypes/${resultfile}.txt" + -P "${HDF_RESOURCES_DIR}/jrunTest.cmake" + ) + if (NOT "${last_test}" STREQUAL "") + set_tests_properties (JAVA_datatypes-${resultfile} PROPERTIES DEPENDS ${last_test}) + endif (NOT "${last_test}" STREQUAL "") + set (last_test "JAVA_datatypes-${resultfile}") +ENDMACRO (ADD_H5_TEST file) + +if (BUILD_TESTING) + foreach (example ${HDF_JAVA_EXAMPLES}) + add_test ( + NAME JAVA_datatypes-${example}-clearall-objects + COMMAND ${CMAKE_COMMAND} + -E remove + ${HDFJAVA_EXAMPLES_BINARY_DIR}/${example}.h5 + ${example}.out + ${example}.out.err + ) + if (NOT "${last_test}" STREQUAL "") + set_tests_properties (JAVA_datatypes-${example}-clearall-objects PROPERTIES DEPENDS ${last_test}) + endif (NOT "${last_test}" STREQUAL "") + add_test ( + NAME JAVA_datatypes-${example}-copy-objects + COMMAND ${CMAKE_COMMAND} + -E copy_if_different + ${HDFJAVA_EXAMPLES_SOURCE_DIR}/testfiles/examples.datatypes.${example}.txt + ${HDFJAVA_EXAMPLES_DATATYPES_BINARY_DIR}/${example}.txt + ) + set_tests_properties (JAVA_datatypes-${example}-copy-objects PROPERTIES DEPENDS JAVA_datatypes-${example}-clearall-objects) + set (last_test "JAVA_datatypes-${example}-copy-objects") + ADD_H5_TEST (${example} 0) + endforeach (example ${HDF_JAVA_EXAMPLES}) +endif (BUILD_TESTING) diff --git a/java/examples/datatypes/H5Ex_T_Array.java b/java/examples/datatypes/H5Ex_T_Array.java new file mode 100644 index 0000000..7b7009a --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_Array.java @@ -0,0 +1,282 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to read and write array datatypes + to a dataset. The program first writes integers arrays of + dimension ADIM0xADIM1 to a dataset with a dataspace of + DIM0, then closes the file. Next, it reopens the file, + reads back the data, and outputs it to the screen. + ************************************************************/ + +package examples.datatypes; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +public class H5Ex_T_Array { + private static String FILENAME = "H5Ex_T_Array.h5"; + private static String DATASETNAME = "DS1"; + private static final int DIM0 = 4; + private static final int ADIM0 = 3; + private static final int ADIM1 = 5; + private static final int RANK = 1; + private static final int NDIMS = 2; + + private static void CreateDataset() { + long file_id = -1; + long filetype_id = -1; + long memtype_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long[] dims = { DIM0 }; + long[] adims = { ADIM0, ADIM1 }; + int[][][] dset_data = new int[DIM0][ADIM0][ADIM1]; + + // Initialize data. indx is the element in the dataspace, jndx and kndx the + // elements within the array datatype. + for (int indx = 0; indx < DIM0; indx++) + for (int jndx = 0; jndx < ADIM0; jndx++) + for (int kndx = 0; kndx < ADIM1; kndx++) + dset_data[indx][jndx][kndx] = indx * jndx - jndx * kndx + indx * kndx; + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create array datatypes for file. + try { + filetype_id = H5.H5Tarray_create(HDF5Constants.H5T_STD_I64LE, NDIMS, adims); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create array datatypes for memory. + try { + memtype_id = H5.H5Tarray_create(HDF5Constants.H5T_NATIVE_INT, NDIMS, adims); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + dataspace_id = H5.H5Screate_simple(RANK, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the dataset. + try { + if ((file_id >= 0) && (dataspace_id >= 0) && (filetype_id >= 0)) + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, filetype_id, dataspace_id, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the dataset. + try { + if ((dataset_id >= 0) && (memtype_id >= 0)) + H5.H5Dwrite(dataset_id, memtype_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // End access to the dataset and release resources used by it. + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the file type. + try { + if (filetype_id >= 0) + H5.H5Tclose(filetype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the mem type. + try { + if (memtype_id >= 0) + H5.H5Tclose(memtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void ReadDataset() { + long file_id = -1; + long filetype_id = -1; + long memtype_id = -1; + long dataset_id = -1; + long[] dims = { DIM0 }; + long[] adims = { ADIM0, ADIM1 }; + int[][][] dset_data; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open an existing dataset. + try { + if (file_id >= 0) + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get the datatype. + try { + if (dataset_id >= 0) + filetype_id = H5.H5Dget_type(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get the datatype's dimensions. + try { + if (filetype_id >= 0) + H5.H5Tget_array_dims(filetype_id, adims); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Allocate array of pointers to two-dimensional arrays (the + // elements of the dataset. + dset_data = new int[(int) dims[0]][(int) (adims[0])][(int) (adims[1])]; + + // Create array datatypes for memory. + try { + memtype_id = H5.H5Tarray_create(HDF5Constants.H5T_NATIVE_INT, 2, adims); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Read data. + try { + if ((dataset_id >= 0) && (memtype_id >= 0)) + H5.H5Dread(dataset_id, memtype_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + for (int indx = 0; indx < dims[0]; indx++) { + System.out.println(DATASETNAME + " [" + indx + "]:"); + for (int jndx = 0; jndx < adims[0]; jndx++) { + System.out.print(" ["); + for (int kndx = 0; kndx < adims[1]; kndx++) + System.out.print(dset_data[indx][jndx][kndx] + " "); + System.out.println("]"); + } + System.out.println(); + } + System.out.println(); + + // End access to the dataset and release resources used by it. + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the file type. + try { + if (filetype_id >= 0) + H5.H5Tclose(filetype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the mem type. + try { + if (memtype_id >= 0) + H5.H5Tclose(memtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + H5Ex_T_Array.CreateDataset(); + // Now we begin the read section of this example. Here we assume + // the dataset and array have the same name and rank, but can have + // any size. Therefore we must allocate a new array to read in + // data using malloc(). + H5Ex_T_Array.ReadDataset(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_ArrayAttribute.java b/java/examples/datatypes/H5Ex_T_ArrayAttribute.java new file mode 100644 index 0000000..ce97457 --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_ArrayAttribute.java @@ -0,0 +1,322 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to read and write array datatypes + to an attribute. The program first writes integers arrays + of dimension ADIM0xADIM1 to an attribute with a dataspace + of DIM0, then closes the file. Next, it reopens the + file, reads back the data, and outputs it to the screen. + ************************************************************/ + +package examples.datatypes; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +public class H5Ex_T_ArrayAttribute { + private static String FILENAME = "H5Ex_T_ArrayAttribute.h5"; + private static String DATASETNAME = "DS1"; + private static String ATTRIBUTENAME = "A1"; + private static final int DIM0 = 4; + private static final int ADIM0 = 3; + private static final int ADIM1 = 5; + private static final int RANK = 1; + private static final int NDIMS = 2; + + private static void CreateDataset() { + long file_id = -1; + long filetype_id = -1; + long memtype_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long attribute_id = -1; + long[] dims = { DIM0 }; + long[] adims = { ADIM0, ADIM1 }; + int[][][] dset_data = new int[DIM0][ADIM0][ADIM1]; + + // Initialize data. indx is the element in the dataspace, jndx and kndx the + // elements within the array datatype. + for (int indx = 0; indx < DIM0; indx++) + for (int jndx = 0; jndx < ADIM0; jndx++) + for (int kndx = 0; kndx < ADIM1; kndx++) + dset_data[indx][jndx][kndx] = indx * jndx - jndx * kndx + indx * kndx; + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create array datatypes for file. + try { + filetype_id = H5.H5Tarray_create(HDF5Constants.H5T_STD_I64LE, NDIMS, adims); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create array datatypes for memory. + try { + memtype_id = H5.H5Tarray_create(HDF5Constants.H5T_NATIVE_INT, NDIMS, adims); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataset with a scalar dataspace. + try { + dataspace_id = H5.H5Screate(HDF5Constants.H5S_SCALAR); + if (dataspace_id >= 0) { + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I32LE, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + H5.H5Sclose(dataspace_id); + dataspace_id = -1; + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + dataspace_id = H5.H5Screate_simple(RANK, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the attribute and write the array data to it. + try { + if ((dataset_id >= 0) && (dataspace_id >= 0) && (filetype_id >= 0)) + attribute_id = H5.H5Acreate(dataset_id, ATTRIBUTENAME, filetype_id, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the dataset. + try { + if ((attribute_id >= 0) && (memtype_id >= 0)) + H5.H5Awrite(attribute_id, memtype_id, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // End access to the dataset and release resources used by it. + try { + if (attribute_id >= 0) + H5.H5Aclose(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the file type. + try { + if (filetype_id >= 0) + H5.H5Tclose(filetype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the mem type. + try { + if (memtype_id >= 0) + H5.H5Tclose(memtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void ReadDataset() { + long file_id = -1; + long filetype_id = -1; + long memtype_id = -1; + long dataset_id = -1; + long attribute_id = -1; + long[] dims = { DIM0 }; + long[] adims = { ADIM0, ADIM1 }; + int[][][] dset_data; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open an existing dataset. + try { + if (file_id >= 0) + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + attribute_id = H5.H5Aopen_by_name(dataset_id, ".", ATTRIBUTENAME, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get the datatype. + try { + if (attribute_id >= 0) + filetype_id = H5.H5Aget_type(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get the datatype's dimensions. + try { + if (filetype_id >= 0) + H5.H5Tget_array_dims(filetype_id, adims); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Allocate array of pointers to two-dimensional arrays (the + // elements of the dataset. + dset_data = new int[(int) dims[0]][(int) (adims[0])][(int) (adims[1])]; + + // Create array datatypes for memory. + try { + memtype_id = H5.H5Tarray_create(HDF5Constants.H5T_NATIVE_INT, 2, adims); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Read data. + try { + if ((attribute_id >= 0) && (memtype_id >= 0)) + H5.H5Aread(attribute_id, memtype_id, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + for (int indx = 0; indx < dims[0]; indx++) { + System.out.println(ATTRIBUTENAME + " [" + indx + "]:"); + for (int jndx = 0; jndx < adims[0]; jndx++) { + System.out.print(" ["); + for (int kndx = 0; kndx < adims[1]; kndx++) + System.out.print(dset_data[indx][jndx][kndx] + " "); + System.out.println("]"); + } + System.out.println(); + } + System.out.println(); + + // End access to the dataset and release resources used by it. + try { + if (attribute_id >= 0) + H5.H5Aclose(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the file type. + try { + if (filetype_id >= 0) + H5.H5Tclose(filetype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the mem type. + try { + if (memtype_id >= 0) + H5.H5Tclose(memtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + H5Ex_T_ArrayAttribute.CreateDataset(); + // Now we begin the read section of this example. Here we assume + // the dataset and array have the same name and rank, but can have + // any size. Therefore we must allocate a new array to read in + // data using malloc(). + H5Ex_T_ArrayAttribute.ReadDataset(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_Bit.java b/java/examples/datatypes/H5Ex_T_Bit.java new file mode 100644 index 0000000..f76c7d5 --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_Bit.java @@ -0,0 +1,227 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to read and write bitfield + datatypes to a dataset. The program first writes bit + fields to a dataset with a dataspace of DIM0xDIM1, then + closes the file. Next, it reopens the file, reads back + the data, and outputs it to the screen. + ************************************************************/ + +package examples.datatypes; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +public class H5Ex_T_Bit { + private static String FILENAME = "H5Ex_T_Bit.h5"; + private static String DATASETNAME = "DS1"; + private static final int DIM0 = 4; + private static final int DIM1 = 7; + private static final int RANK = 2; + + private static void CreateDataset() { + long file_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long[] dims = { DIM0, DIM1 }; + int[][] dset_data = new int[DIM0][DIM1]; + + // Initialize data. + for (int indx = 0; indx < DIM0; indx++) + for (int jndx = 0; jndx < DIM1; jndx++) { + dset_data[indx][jndx] = 0; + dset_data[indx][jndx] |= (indx * jndx - jndx) & 0x03; /* Field "A" */ + dset_data[indx][jndx] |= (indx & 0x03) << 2; /* Field "B" */ + dset_data[indx][jndx] |= (jndx & 0x03) << 4; /* Field "C" */ + dset_data[indx][jndx] |= ((indx + jndx) & 0x03) << 6; /* Field "D" */ + } + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + dataspace_id = H5.H5Screate_simple(RANK, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the dataset. + try { + if ((file_id >= 0) && (dataspace_id >= 0)) + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_B8BE, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the bitfield data to the dataset. + try { + if (dataset_id >= 0) + H5.H5Dwrite(dataset_id, HDF5Constants.H5T_NATIVE_B8, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // End access to the dataset and release resources used by it. + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void ReadDataset() { + long file_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long[] dims = { DIM0, DIM1 }; + int[][] dset_data; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open an existing dataset. + try { + if (file_id >= 0) + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get dataspace and allocate memory for read buffer. + try { + if (dataset_id >= 0) + dataspace_id = H5.H5Dget_space(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataspace_id >= 0) + H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Allocate array of pointers to two-dimensional arrays (the + // elements of the dataset. + dset_data = new int[(int) dims[0]][(int) (dims[1])]; + + // Read data. + try { + if (dataset_id >= 0) + H5.H5Dread(dataset_id, HDF5Constants.H5T_NATIVE_B8, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + System.out.println(DATASETNAME + ":"); + for (int indx = 0; indx < dims[0]; indx++) { + System.out.print(" ["); + for (int jndx = 0; jndx < dims[1]; jndx++) { + System.out.print("{" + (dset_data[indx][jndx] & 0x03) + ", "); + System.out.print(((dset_data[indx][jndx] >> 2) & 0x03) + ", "); + System.out.print(((dset_data[indx][jndx] >> 4) & 0x03) + ", "); + System.out.print(((dset_data[indx][jndx] >> 6) & 0x03) + "}"); + } + System.out.println("]"); + } + System.out.println(); + + // End access to the dataset and release resources used by it. + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + H5Ex_T_Bit.CreateDataset(); + // Now we begin the read section of this example. Here we assume + // the dataset and array have the same name and rank, but can have + // any size. Therefore we must allocate a new array to read in + // data using malloc(). + H5Ex_T_Bit.ReadDataset(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_BitAttribute.java b/java/examples/datatypes/H5Ex_T_BitAttribute.java new file mode 100644 index 0000000..a5ab81b --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_BitAttribute.java @@ -0,0 +1,267 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to read and write bitfield + datatypes to an attribute. The program first writes bit + fields to an attribute with a dataspace of DIM0xDIM1, then + closes the file. Next, it reopens the file, reads back + the data, and outputs it to the screen. + ************************************************************/ + +package examples.datatypes; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +public class H5Ex_T_BitAttribute { + private static String FILENAME = "H5Ex_T_BitAttribute.h5"; + private static String DATASETNAME = "DS1"; + private static String ATTRIBUTENAME = "A1"; + private static final int DIM0 = 4; + private static final int DIM1 = 7; + private static final int RANK = 2; + + private static void CreateDataset() { + long file_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long attribute_id = -1; + long[] dims = { DIM0, DIM1 }; + int[][] dset_data = new int[DIM0][DIM1]; + + // Initialize data. + for (int indx = 0; indx < DIM0; indx++) + for (int jndx = 0; jndx < DIM1; jndx++) { + dset_data[indx][jndx] = 0; + dset_data[indx][jndx] |= (indx * jndx - jndx) & 0x03; /* Field "A" */ + dset_data[indx][jndx] |= (indx & 0x03) << 2; /* Field "B" */ + dset_data[indx][jndx] |= (jndx & 0x03) << 4; /* Field "C" */ + dset_data[indx][jndx] |= ((indx + jndx) & 0x03) << 6; /* Field "D" */ + } + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataset with a scalar dataspace. + try { + dataspace_id = H5.H5Screate(HDF5Constants.H5S_SCALAR); + if (dataspace_id >= 0) { + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I32LE, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + H5.H5Sclose(dataspace_id); + dataspace_id = -1; + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + dataspace_id = H5.H5Screate_simple(RANK, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the attribute and write the array data to it. + try { + if ((dataset_id >= 0) && (dataspace_id >= 0)) + attribute_id = H5.H5Acreate(dataset_id, ATTRIBUTENAME, HDF5Constants.H5T_STD_B8BE, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the dataset. + try { + if (attribute_id >= 0) + H5.H5Awrite(attribute_id, HDF5Constants.H5T_NATIVE_B8, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // End access to the dataset and release resources used by it. + try { + if (attribute_id >= 0) + H5.H5Aclose(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void ReadDataset() { + long file_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long attribute_id = -1; + long[] dims = { DIM0, DIM1 }; + int[][] dset_data; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open an existing dataset. + try { + if (file_id >= 0) + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + attribute_id = H5.H5Aopen_by_name(dataset_id, ".", ATTRIBUTENAME, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get dataspace and allocate memory for read buffer. + try { + if (attribute_id >= 0) + dataspace_id = H5.H5Aget_space(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataspace_id >= 0) + H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Allocate array of pointers to two-dimensional arrays (the + // elements of the dataset. + dset_data = new int[(int) dims[0]][(int) (dims[1])]; + + // Read data. + try { + if (attribute_id >= 0) + H5.H5Aread(attribute_id, HDF5Constants.H5T_NATIVE_B8, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + System.out.println(ATTRIBUTENAME + ":"); + for (int indx = 0; indx < dims[0]; indx++) { + System.out.print(" ["); + for (int jndx = 0; jndx < dims[1]; jndx++) { + System.out.print("{" + (dset_data[indx][jndx] & 0x03) + ", "); + System.out.print(((dset_data[indx][jndx] >> 2) & 0x03) + ", "); + System.out.print(((dset_data[indx][jndx] >> 4) & 0x03) + ", "); + System.out.print(((dset_data[indx][jndx] >> 6) & 0x03) + "}"); + } + System.out.println("]"); + } + System.out.println(); + + // End access to the dataset and release resources used by it. + try { + if (attribute_id >= 0) + H5.H5Aclose(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + H5Ex_T_BitAttribute.CreateDataset(); + // Now we begin the read section of this example. Here we assume + // the dataset and array have the same name and rank, but can have + // any size. Therefore we must allocate a new array to read in + // data using malloc(). + H5Ex_T_BitAttribute.ReadDataset(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_Commit.java b/java/examples/datatypes/H5Ex_T_Commit.java new file mode 100644 index 0000000..d4e43f9 --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_Commit.java @@ -0,0 +1,265 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to commit a named datatype to a + file, and read back that datatype. The program first + defines a compound datatype, commits it to a file, then + closes the file. Next, it reopens the file, opens the + datatype, and outputs the names of its fields to the + screen. + ************************************************************/ + +package examples.datatypes; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +import java.util.EnumSet; +import java.util.HashMap; +import java.util.Map; + +public class H5Ex_T_Commit { + private static String FILENAME = "H5Ex_T_Commit.h5"; + private static String DATATYPENAME = "Sensor_Type"; + protected static final int INTEGERSIZE = 4; + protected static final int DOUBLESIZE = 8; + protected final static int MAXSTRINGSIZE = 80; + + // Values for the various classes of datatypes + enum H5T_class { + H5T_NO_CLASS(HDF5Constants.H5T_NO_CLASS), // error + H5T_INTEGER(HDF5Constants.H5T_INTEGER), // integer types + H5T_FLOAT(HDF5Constants.H5T_FLOAT), // floating-point types + H5T_TIME(HDF5Constants.H5T_TIME), // date and time types + H5T_STRING(HDF5Constants.H5T_STRING), // character string types + H5T_BITFIELD(HDF5Constants.H5T_BITFIELD), // bit field types + H5T_OPAQUE(HDF5Constants.H5T_OPAQUE), // opaque types + H5T_COMPOUND(HDF5Constants.H5T_COMPOUND), // compound types + H5T_REFERENCE(HDF5Constants.H5T_REFERENCE), // reference types + H5T_ENUM(HDF5Constants.H5T_ENUM), // enumeration types + H5T_VLEN(HDF5Constants.H5T_VLEN), // Variable-Length types + H5T_ARRAY(HDF5Constants.H5T_ARRAY), // Array types + H5T_NCLASSES(11); // this must be last + + private static final Map<Long, H5T_class> lookup = new HashMap<Long, H5T_class>(); + + static { + for (H5T_class s : EnumSet.allOf(H5T_class.class)) + lookup.put(s.getCode(), s); + } + + private long code; + + H5T_class(long layout_type) { + this.code = layout_type; + } + + public long getCode() { + return this.code; + } + + public static H5T_class get(long typeclass_id) { + return lookup.get(typeclass_id); + } + } + + // The supporting Sensor_Datatype class. + private static class Sensor_Datatype { + static int numberMembers = 4; + static int[] memberDims = { 1, 1, 1, 1 }; + + String[] memberNames = { "Serial number", "Location", "Temperature (F)", "Pressure (inHg)" }; + long[] memberFileTypes = { HDF5Constants.H5T_STD_I32BE, HDF5Constants.H5T_C_S1, HDF5Constants.H5T_IEEE_F64BE, + HDF5Constants.H5T_IEEE_F64BE }; + static int[] memberStorage = { INTEGERSIZE, MAXSTRINGSIZE, DOUBLESIZE, DOUBLESIZE }; + + // Data size is the storage size for the members not the object. + static long getDataSize() { + long data_size = 0; + for (int indx = 0; indx < numberMembers; indx++) + data_size += memberStorage[indx] * memberDims[indx]; + return data_size; + } + + static int getOffset(int memberItem) { + int data_offset = 0; + for (int indx = 0; indx < memberItem; indx++) + data_offset += memberStorage[indx]; + return data_offset; + } + } + + private static void CreateDataType() { + long file_id = -1; + long strtype_id = -1; + long filetype_id = -1; + Sensor_Datatype datatypes = new Sensor_Datatype(); + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create string datatype. + try { + strtype_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1); + if (strtype_id >= 0) + H5.H5Tset_size(strtype_id, MAXSTRINGSIZE); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the compound datatype for the file. Because the standard + // types we are using for the file may have different sizes than + // the corresponding native types, we must manually calculate the + // offset of each member. + try { + filetype_id = H5.H5Tcreate(HDF5Constants.H5T_COMPOUND, Sensor_Datatype.getDataSize()); + if (filetype_id >= 0) { + for (int indx = 0; indx < Sensor_Datatype.numberMembers; indx++) { + long type_id = datatypes.memberFileTypes[indx]; + if (type_id == HDF5Constants.H5T_C_S1) + type_id = strtype_id; + H5.H5Tinsert(filetype_id, datatypes.memberNames[indx], Sensor_Datatype.getOffset(indx), type_id); + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Commit the compound datatype to the file, creating a named datatype. + try { + if ((file_id >= 0) && (filetype_id >= 0)) + H5.H5Tcommit(file_id, DATATYPENAME, filetype_id, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the file type. + try { + if (filetype_id >= 0) + H5.H5Tclose(filetype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the str type. + try { + if (strtype_id >= 0) + H5.H5Tclose(strtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void ReadDataType() { + long file_id = -1; + long typeclass_id = -1; + long filetype_id = -1; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open named datatype. + try { + if (file_id >= 0) + filetype_id = H5.H5Topen(file_id, DATATYPENAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + System.out.println("Named datatype: " + DATATYPENAME + ":"); + + // Get datatype class. If it isn't compound, we won't print anything. + try { + if (filetype_id >= 0) + typeclass_id = H5.H5Tget_class(filetype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + // Read data. + try { + if (H5T_class.get(typeclass_id) == H5T_class.H5T_COMPOUND) { + System.out.println(" Class: H5T_COMPOUND"); + int nmembs = H5.H5Tget_nmembers(filetype_id); + // Iterate over compound datatype members. + for (int indx = 0; indx < nmembs; indx++) { + String member_name = H5.H5Tget_member_name(filetype_id, indx); + System.out.println(" " + member_name); + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the mem type. + try { + if (filetype_id >= 0) + H5.H5Tclose(filetype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + H5Ex_T_Commit.CreateDataType(); + // Now we begin the read section of this example. Here we assume + // the dataset and array have the same name and rank, but can have + // any size. Therefore we must allocate a new array to read in + // data using malloc(). + H5Ex_T_Commit.ReadDataType(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_Compound.java b/java/examples/datatypes/H5Ex_T_Compound.java new file mode 100644 index 0000000..f270cb9 --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_Compound.java @@ -0,0 +1,443 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to read and write compound + datatypes to a dataset. The program first writes + compound structures to a dataset with a dataspace of DIM0, + then closes the file. Next, it reopens the file, reads + back the data, and outputs it to the screen. + ************************************************************/ + +package examples.datatypes; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.charset.Charset; + +public class H5Ex_T_Compound { + private static String FILENAME = "H5Ex_T_Compound.h5"; + private static String DATASETNAME = "DS1"; + private static final int DIM0 = 4; + private static final int RANK = 1; + protected static final int INTEGERSIZE = 4; + protected static final int DOUBLESIZE = 8; + protected final static int MAXSTRINGSIZE = 80; + + static class Sensor_Datatype { + static int numberMembers = 4; + static int[] memberDims = { 1, 1, 1, 1 }; + + static String[] memberNames = { "Serial number", "Location", "Temperature (F)", "Pressure (inHg)" }; + static long[] memberMemTypes = { HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5T_C_S1, + HDF5Constants.H5T_NATIVE_DOUBLE, HDF5Constants.H5T_NATIVE_DOUBLE }; + static long[] memberFileTypes = { HDF5Constants.H5T_STD_I32BE, HDF5Constants.H5T_C_S1, + HDF5Constants.H5T_IEEE_F64BE, HDF5Constants.H5T_IEEE_F64BE }; + static int[] memberStorage = { INTEGERSIZE, MAXSTRINGSIZE, DOUBLESIZE, DOUBLESIZE }; + + // Data size is the storage size for the members. + static long getTotalDataSize() { + long data_size = 0; + for (int indx = 0; indx < numberMembers; indx++) + data_size += memberStorage[indx] * memberDims[indx]; + return DIM0 * data_size; + } + + static long getDataSize() { + long data_size = 0; + for (int indx = 0; indx < numberMembers; indx++) + data_size += memberStorage[indx] * memberDims[indx]; + return data_size; + } + + static int getOffset(int memberItem) { + int data_offset = 0; + for (int indx = 0; indx < memberItem; indx++) + data_offset += memberStorage[indx]; + return data_offset; + } + } + + static class Sensor { + public int serial_no; + public String location; + public double temperature; + public double pressure; + + Sensor(int serial_no, String location, double temperature, double pressure) { + this.serial_no = serial_no; + this.location = location; + this.temperature = temperature; + this.pressure = pressure; + } + + Sensor(ByteBuffer databuf, int dbposition) { + readBuffer(databuf, dbposition); + } + + void writeBuffer(ByteBuffer databuf, int dbposition) { + databuf.putInt(dbposition + Sensor_Datatype.getOffset(0), serial_no); + byte[] temp_str = location.getBytes(Charset.forName("UTF-8")); + int arraylen = (temp_str.length > MAXSTRINGSIZE) ? MAXSTRINGSIZE : temp_str.length; + for (int ndx = 0; ndx < arraylen; ndx++) + databuf.put(dbposition + Sensor_Datatype.getOffset(1) + ndx, temp_str[ndx]); + for (int ndx = arraylen; ndx < MAXSTRINGSIZE; ndx++) + databuf.put(dbposition + Sensor_Datatype.getOffset(1) + arraylen, (byte) 0); + databuf.putDouble(dbposition + Sensor_Datatype.getOffset(2), temperature); + databuf.putDouble(dbposition + Sensor_Datatype.getOffset(3), pressure); + } + + void readBuffer(ByteBuffer databuf, int dbposition) { + this.serial_no = databuf.getInt(dbposition + Sensor_Datatype.getOffset(0)); + ByteBuffer stringbuf = databuf.duplicate(); + stringbuf.position(dbposition + Sensor_Datatype.getOffset(1)); + stringbuf.limit(dbposition + Sensor_Datatype.getOffset(1) + MAXSTRINGSIZE); + byte[] bytearr = new byte[stringbuf.remaining()]; + stringbuf.get(bytearr); + this.location = new String(bytearr, Charset.forName("UTF-8")).trim(); + this.temperature = databuf.getDouble(dbposition + Sensor_Datatype.getOffset(2)); + this.pressure = databuf.getDouble(dbposition + Sensor_Datatype.getOffset(3)); + } + + @Override + public String toString() { + return String.format("Serial number : " + serial_no + "%n" + + "Location : " + location + "%n" + + "Temperature (F) : " + temperature + "%n" + + "Pressure (inHg) : " + pressure + "%n"); + } + } + + private static void CreateDataset() { + long file_id = -1; + long strtype_id = -1; + long memtype_id = -1; + long filetype_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long[] dims = { DIM0 }; + Sensor[] object_data = new Sensor[DIM0]; + byte[] dset_data = null; + + // Initialize data. + object_data[0] = new Sensor(1153, new String("Exterior (static)"), 53.23, 24.57); + object_data[1] = new Sensor(1184, new String("Intake"), 55.12, 22.95); + object_data[2] = new Sensor(1027, new String("Intake manifold"), 103.55, 31.23); + object_data[3] = new Sensor(1313, new String("Exhaust manifold"), 1252.89, 84.11); + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create string datatype. + try { + strtype_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1); + if (strtype_id >= 0) + H5.H5Tset_size(strtype_id, MAXSTRINGSIZE); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the compound datatype for memory. + try { + memtype_id = H5.H5Tcreate(HDF5Constants.H5T_COMPOUND, Sensor_Datatype.getDataSize()); + if (memtype_id >= 0) { + for (int indx = 0; indx < Sensor_Datatype.numberMembers; indx++) { + long type_id = Sensor_Datatype.memberMemTypes[indx]; + if (type_id == HDF5Constants.H5T_C_S1) + type_id = strtype_id; + H5.H5Tinsert(memtype_id, Sensor_Datatype.memberNames[indx], Sensor_Datatype.getOffset(indx), + type_id); + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the compound datatype for the file. Because the standard + // types we are using for the file may have different sizes than + // the corresponding native types, we must manually calculate the + // offset of each member. + try { + filetype_id = H5.H5Tcreate(HDF5Constants.H5T_COMPOUND, Sensor_Datatype.getDataSize()); + if (filetype_id >= 0) { + for (int indx = 0; indx < Sensor_Datatype.numberMembers; indx++) { + long type_id = Sensor_Datatype.memberFileTypes[indx]; + if (type_id == HDF5Constants.H5T_C_S1) + type_id = strtype_id; + H5.H5Tinsert(filetype_id, Sensor_Datatype.memberNames[indx], Sensor_Datatype.getOffset(indx), + type_id); + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + dataspace_id = H5.H5Screate_simple(RANK, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the dataset. + try { + if ((file_id >= 0) && (dataspace_id >= 0) && (filetype_id >= 0)) + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, filetype_id, dataspace_id, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the compound data to the dataset. + // allocate memory for read buffer. + dset_data = new byte[(int)dims[0] * (int)Sensor_Datatype.getDataSize()]; + ByteBuffer outBuf = ByteBuffer.wrap(dset_data); + outBuf.order(ByteOrder.nativeOrder()); + for (int indx = 0; indx < (int) dims[0]; indx++) { + object_data[indx].writeBuffer(outBuf, indx * (int)Sensor_Datatype.getDataSize()); + } + try { + if ((dataset_id >= 0) && (memtype_id >= 0)) + H5.H5Dwrite(dataset_id, memtype_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // End access to the dataset and release resources used by it. + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the file type. + try { + if (filetype_id >= 0) + H5.H5Tclose(filetype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the mem type. + try { + if (memtype_id >= 0) + H5.H5Tclose(memtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (strtype_id >= 0) + H5.H5Tclose(strtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void ReadDataset() { + long file_id = -1; + long strtype_id = -1; + long memtype_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long[] dims = { DIM0 }; + Sensor[] object_data2; + byte[] dset_data; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open an existing dataset. + try { + if (file_id >= 0) + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get dataspace and allocate memory for read buffer. + try { + if (dataset_id >= 0) + dataspace_id = H5.H5Dget_space(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataspace_id >= 0) + H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create string datatype. + try { + strtype_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1); + if (strtype_id >= 0) + H5.H5Tset_size(strtype_id, MAXSTRINGSIZE); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the compound datatype for memory. + try { + memtype_id = H5.H5Tcreate(HDF5Constants.H5T_COMPOUND, Sensor_Datatype.getDataSize()); + if (memtype_id >= 0) { + for (int indx = 0; indx < Sensor_Datatype.numberMembers; indx++) { + long type_id = Sensor_Datatype.memberMemTypes[indx]; + if (type_id == HDF5Constants.H5T_C_S1) + type_id = strtype_id; + H5.H5Tinsert(memtype_id, Sensor_Datatype.memberNames[indx], Sensor_Datatype.getOffset(indx), + type_id); + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // allocate memory for read buffer. + dset_data = new byte[(int) dims[0] * (int)Sensor_Datatype.getDataSize()]; + + object_data2 = new Sensor[(int) dims[0]]; + + // Read data. + try { + if ((dataset_id >= 0) && (memtype_id >= 0)) + H5.H5Dread(dataset_id, memtype_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + + ByteBuffer inBuf = ByteBuffer.wrap(dset_data); + inBuf.order(ByteOrder.nativeOrder()); + for (int indx = 0; indx < (int) dims[0]; indx++) { + object_data2[indx] = new Sensor(inBuf, indx * (int)Sensor_Datatype.getDataSize()); + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + for (int indx = 0; indx < dims[0]; indx++) { + System.out.println(DATASETNAME + " [" + indx + "]:"); + System.out.println(object_data2[indx].toString()); + } + System.out.println(); + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the mem type. + try { + if (memtype_id >= 0) + H5.H5Tclose(memtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (strtype_id >= 0) + H5.H5Tclose(strtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + H5Ex_T_Compound.CreateDataset(); + // Now we begin the read section of this example. Here we assume + // the dataset and array have the same name and rank, but can have + // any size. Therefore we must allocate a new array to read in + // data using malloc(). + H5Ex_T_Compound.ReadDataset(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_CompoundAttribute.java b/java/examples/datatypes/H5Ex_T_CompoundAttribute.java new file mode 100644 index 0000000..25581d4 --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_CompoundAttribute.java @@ -0,0 +1,486 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to read and write compound + datatypes to an attribute. The program first writes + compound structures to an attribute with a dataspace of + DIM0, then closes the file. Next, it reopens the file, + reads back the data, and outputs it to the screen. + ************************************************************/ + +package examples.datatypes; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.charset.Charset; + +public class H5Ex_T_CompoundAttribute { + private static String FILENAME = "H5Ex_T_CompoundAttribute.h5"; + private static String DATASETNAME = "DS1"; + private static String ATTRIBUTENAME = "A1"; + private static final int DIM0 = 4; + private static final int RANK = 1; + protected static final int INTEGERSIZE = 4; + protected static final int DOUBLESIZE = 8; + protected final static int MAXSTRINGSIZE = 80; + + // Using Java Externalization will add a two-byte object header in + // the stream, which needs to be called out in the datatypes. + static class Sensor_Datatype { + static int numberMembers = 4; + static int[] memberDims = { 1, 1, 1, 1 }; + + static String[] memberNames = { "Serial number", "Location", "Temperature (F)", "Pressure (inHg)" }; + static long[] memberMemTypes = { HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5T_C_S1, + HDF5Constants.H5T_NATIVE_DOUBLE, HDF5Constants.H5T_NATIVE_DOUBLE }; + static long[] memberFileTypes = { HDF5Constants.H5T_STD_I32BE, HDF5Constants.H5T_C_S1, + HDF5Constants.H5T_IEEE_F64BE, HDF5Constants.H5T_IEEE_F64BE }; + static int[] memberStorage = { INTEGERSIZE, MAXSTRINGSIZE, DOUBLESIZE, DOUBLESIZE }; + + // Data size is the storage size for the members not the object. + static long getTotalDataSize() { + long data_size = 0; + for (int indx = 0; indx < numberMembers; indx++) + data_size += memberStorage[indx] * memberDims[indx]; + return DIM0 * data_size; + } + + static long getDataSize() { + long data_size = 0; + for (int indx = 0; indx < numberMembers; indx++) + data_size += memberStorage[indx] * memberDims[indx]; + return data_size; + } + + static int getOffset(int memberItem) { + int data_offset = 0; + for (int indx = 0; indx < memberItem; indx++) + data_offset += memberStorage[indx]; + return data_offset; + } + } + + static class Sensor { + public int serial_no; + public String location; + public double temperature; + public double pressure; + + Sensor(int serial_no, String location, double temperature, double pressure) { + this.serial_no = serial_no; + this.location = location; + this.temperature = temperature; + this.pressure = pressure; + } + + Sensor(ByteBuffer databuf, int dbposition) { + readBuffer(databuf, dbposition); + } + + void writeBuffer(ByteBuffer databuf, int dbposition) { + databuf.putInt(dbposition + Sensor_Datatype.getOffset(0), serial_no); + byte[] temp_str = location.getBytes(Charset.forName("UTF-8")); + int arraylen = (temp_str.length > MAXSTRINGSIZE) ? MAXSTRINGSIZE : temp_str.length; + for (int ndx = 0; ndx < arraylen; ndx++) + databuf.put(dbposition + Sensor_Datatype.getOffset(1) + ndx, temp_str[ndx]); + for (int ndx = arraylen; ndx < MAXSTRINGSIZE; ndx++) + databuf.put(dbposition + Sensor_Datatype.getOffset(1) + arraylen, (byte) 0); + databuf.putDouble(dbposition + Sensor_Datatype.getOffset(2), temperature); + databuf.putDouble(dbposition + Sensor_Datatype.getOffset(3), pressure); + } + + void readBuffer(ByteBuffer databuf, int dbposition) { + this.serial_no = databuf.getInt(dbposition + Sensor_Datatype.getOffset(0)); + ByteBuffer stringbuf = databuf.duplicate(); + stringbuf.position(dbposition + Sensor_Datatype.getOffset(1)); + stringbuf.limit(dbposition + Sensor_Datatype.getOffset(1) + MAXSTRINGSIZE); + byte[] bytearr = new byte[stringbuf.remaining()]; + stringbuf.get(bytearr); + this.location = new String(bytearr, Charset.forName("UTF-8")).trim(); + this.temperature = databuf.getDouble(dbposition + Sensor_Datatype.getOffset(2)); + this.pressure = databuf.getDouble(dbposition + Sensor_Datatype.getOffset(3)); + } + + @Override + public String toString() { + return String.format("Serial number : " + serial_no + "%n" + + "Location : " + location + "%n" + + "Temperature (F) : " + temperature + "%n" + + "Pressure (inHg) : " + pressure + "%n"); + } + } + + private static void CreateDataset() { + long file_id = -1; + long strtype_id = -1; + long memtype_id = -1; + long filetype_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long attribute_id = -1; + long[] dims = { DIM0 }; + Sensor[] object_data = new Sensor[DIM0]; + byte[] dset_data = null; + + // Initialize data. + object_data[0] = new Sensor(1153, new String("Exterior (static)"), 53.23, 24.57); + object_data[1] = new Sensor(1184, new String("Intake"), 55.12, 22.95); + object_data[2] = new Sensor(1027, new String("Intake manifold"), 103.55, 31.23); + object_data[3] = new Sensor(1313, new String("Exhaust manifold"), 1252.89, 84.11); + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create string datatype. + try { + strtype_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1); + if (strtype_id >= 0) + H5.H5Tset_size(strtype_id, MAXSTRINGSIZE); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the compound datatype for memory. + try { + memtype_id = H5.H5Tcreate(HDF5Constants.H5T_COMPOUND, Sensor_Datatype.getDataSize()); + if (memtype_id >= 0) { + for (int indx = 0; indx < Sensor_Datatype.numberMembers; indx++) { + long type_id = Sensor_Datatype.memberMemTypes[indx]; + if (type_id == HDF5Constants.H5T_C_S1) + type_id = strtype_id; + H5.H5Tinsert(memtype_id, Sensor_Datatype.memberNames[indx], Sensor_Datatype.getOffset(indx), + type_id); + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the compound datatype for the file. Because the standard + // types we are using for the file may have different sizes than + // the corresponding native types, we must manually calculate the + // offset of each member. + try { + filetype_id = H5.H5Tcreate(HDF5Constants.H5T_COMPOUND, Sensor_Datatype.getDataSize()); + if (filetype_id >= 0) { + for (int indx = 0; indx < Sensor_Datatype.numberMembers; indx++) { + long type_id = Sensor_Datatype.memberFileTypes[indx]; + if (type_id == HDF5Constants.H5T_C_S1) + type_id = strtype_id; + H5.H5Tinsert(filetype_id, Sensor_Datatype.memberNames[indx], Sensor_Datatype.getOffset(indx), + type_id); + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataset with a scalar dataspace. + try { + dataspace_id = H5.H5Screate(HDF5Constants.H5S_SCALAR); + if (dataspace_id >= 0) { + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I32LE, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + H5.H5Sclose(dataspace_id); + dataspace_id = -1; + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + dataspace_id = H5.H5Screate_simple(RANK, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the attribute. + try { + if ((dataset_id >= 0) && (dataspace_id >= 0) && (filetype_id >= 0)) + attribute_id = H5.H5Acreate(dataset_id, ATTRIBUTENAME, filetype_id, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the compound data. + dset_data = new byte[(int) dims[0] * (int)Sensor_Datatype.getDataSize()]; + ByteBuffer outBuf = ByteBuffer.wrap(dset_data); + outBuf.order(ByteOrder.nativeOrder()); + for (int indx = 0; indx < (int) dims[0]; indx++) { + object_data[indx].writeBuffer(outBuf, indx * (int)Sensor_Datatype.getDataSize()); + } + try { + if ((attribute_id >= 0) && (memtype_id >= 0)) + H5.H5Awrite(attribute_id, memtype_id, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // End access to the dataset and release resources used by it. + try { + if (attribute_id >= 0) + H5.H5Aclose(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the file type. + try { + if (filetype_id >= 0) + H5.H5Tclose(filetype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the mem type. + try { + if (memtype_id >= 0) + H5.H5Tclose(memtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (strtype_id >= 0) + H5.H5Tclose(strtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void ReadDataset() { + long file_id = -1; + long strtype_id = -1; + long memtype_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long attribute_id = -1; + long[] dims = { DIM0 }; + Sensor[] object_data2; + byte[] dset_data; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open an existing dataset. + try { + if (file_id >= 0) + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + attribute_id = H5.H5Aopen_by_name(dataset_id, ".", ATTRIBUTENAME, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get dataspace and allocate memory for read buffer. This is a + // three dimensional dataset when the array datatype is included so + // the dynamic allocation must be done in steps. + try { + if (attribute_id >= 0) + dataspace_id = H5.H5Aget_space(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataspace_id >= 0) + H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create string datatype. + try { + strtype_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1); + if (strtype_id >= 0) + H5.H5Tset_size(strtype_id, MAXSTRINGSIZE); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the compound datatype for memory. + try { + memtype_id = H5.H5Tcreate(HDF5Constants.H5T_COMPOUND, Sensor_Datatype.getDataSize()); + if (memtype_id >= 0) { + for (int indx = 0; indx < Sensor_Datatype.numberMembers; indx++) { + long type_id = Sensor_Datatype.memberMemTypes[indx]; + if (type_id == HDF5Constants.H5T_C_S1) + type_id = strtype_id; + H5.H5Tinsert(memtype_id, Sensor_Datatype.memberNames[indx], Sensor_Datatype.getOffset(indx), + type_id); + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // allocate memory for read buffer. + dset_data = new byte[(int) dims[0] * (int)Sensor_Datatype.getDataSize()]; + + object_data2 = new Sensor[(int) dims[0]]; + + // Read data. + try { + if ((attribute_id >= 0) && (memtype_id >= 0)) + H5.H5Aread(attribute_id, memtype_id, dset_data); + + ByteBuffer inBuf = ByteBuffer.wrap(dset_data); + inBuf.order(ByteOrder.nativeOrder()); + for (int indx = 0; indx < (int) dims[0]; indx++) { + object_data2[indx] = new Sensor(inBuf, indx * (int)Sensor_Datatype.getDataSize()); + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + for (int indx = 0; indx < dims[0]; indx++) { + System.out.println(ATTRIBUTENAME + " [" + indx + "]:"); + System.out.println(object_data2[indx].toString()); + } + System.out.println(); + + try { + if (attribute_id >= 0) + H5.H5Aclose(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the mem type. + try { + if (memtype_id >= 0) + H5.H5Tclose(memtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (strtype_id >= 0) + H5.H5Tclose(strtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + H5Ex_T_CompoundAttribute.CreateDataset(); + // Now we begin the read section of this example. Here we assume + // the dataset and array have the same name and rank, but can have + // any size. Therefore we must allocate a new array to read in + // data using malloc(). + H5Ex_T_CompoundAttribute.ReadDataset(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_Float.java b/java/examples/datatypes/H5Ex_T_Float.java new file mode 100644 index 0000000..1b5fd9b --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_Float.java @@ -0,0 +1,227 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to read and write integer datatypes + to a dataset. The program first writes integers to a + dataset with a dataspace of DIM0xDIM1, then closes the + file. Next, it reopens the file, reads back the data, and + outputs it to the screen. + ************************************************************/ + +package examples.datatypes; + +import java.text.DecimalFormat; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +public class H5Ex_T_Float { + private static String FILENAME = "H5Ex_T_Float.h5"; + private static String DATASETNAME = "DS1"; + private static final int DIM0 = 4; + private static final int DIM1 = 7; + private static final int RANK = 2; + + private static void CreateDataset() { + long file_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long[] dims = { DIM0, DIM1 }; + double[][] dset_data = new double[DIM0][DIM1]; + + // Initialize data. + for (int indx = 0; indx < DIM0; indx++) + for (int jndx = 0; jndx < DIM1; jndx++) { + dset_data[indx][jndx] = indx / (jndx + 0.5) + jndx; + } + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + dataspace_id = H5.H5Screate_simple(RANK, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the dataset and write the floating point data to it. In + // this example we will save the data as 64 bit little endian IEEE + // floating point numbers, regardless of the native type. The HDF5 + // library automatically converts between different floating point + // types. + try { + if ((file_id >= 0) && (dataspace_id >= 0)) + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_IEEE_F64LE, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the data to the dataset. + try { + if (dataset_id >= 0) + H5.H5Dwrite(dataset_id, HDF5Constants.H5T_NATIVE_DOUBLE, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // End access to the dataset and release resources used by it. + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void ReadDataset() { + long file_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long[] dims = { DIM0, DIM1 }; + double[][] dset_data; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open an existing dataset. + try { + if (file_id >= 0) + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get dataspace and allocate memory for read buffer. + try { + if (dataset_id >= 0) + dataspace_id = H5.H5Dget_space(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataspace_id >= 0) + H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Allocate array of pointers to two-dimensional arrays (the + // elements of the dataset. + dset_data = new double[(int) dims[0]][(int) (dims[1])]; + + // Read data. + try { + if (dataset_id >= 0) + H5.H5Dread(dataset_id, HDF5Constants.H5T_NATIVE_DOUBLE, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + DecimalFormat df = new DecimalFormat("#,##0.0000"); + System.out.println(DATASETNAME + ":"); + for (int indx = 0; indx < dims[0]; indx++) { + System.out.print(" ["); + for (int jndx = 0; jndx < dims[1]; jndx++) { + System.out.print(" " + df.format(dset_data[indx][jndx])); + } + System.out.println("]"); + } + System.out.println(); + + // End access to the dataset and release resources used by it. + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + H5Ex_T_Float.CreateDataset(); + // Now we begin the read section of this example. Here we assume + // the dataset and array have the same name and rank, but can have + // any size. Therefore we must allocate a new array to read in + // data using malloc(). + H5Ex_T_Float.ReadDataset(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_FloatAttribute.java b/java/examples/datatypes/H5Ex_T_FloatAttribute.java new file mode 100644 index 0000000..de1dac7 --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_FloatAttribute.java @@ -0,0 +1,263 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to read and write floating point + datatypes to an attribute. The program first writes + floating point numbers to an attribute with a dataspace of + DIM0xDIM1, then closes the file. Next, it reopens the + file, reads back the data, and outputs it to the screen. + ************************************************************/ + +package examples.datatypes; + +import java.text.DecimalFormat; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +public class H5Ex_T_FloatAttribute { + private static String FILENAME = "H5Ex_T_FloatAttribute.h5"; + private static String DATASETNAME = "DS1"; + private static String ATTRIBUTENAME = "A1"; + private static final int DIM0 = 4; + private static final int DIM1 = 7; + private static final int RANK = 2; + + private static void CreateDataset() { + long file_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long attribute_id = -1; + long[] dims = { DIM0, DIM1 }; + double[][] dset_data = new double[DIM0][DIM1]; + + // Initialize data. + for (int indx = 0; indx < DIM0; indx++) + for (int jndx = 0; jndx < DIM1; jndx++) { + dset_data[indx][jndx] = indx / (jndx + 0.5) + jndx; + } + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataset with a scalar dataspace. + try { + dataspace_id = H5.H5Screate(HDF5Constants.H5S_SCALAR); + if (dataspace_id >= 0) { + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I32LE, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + H5.H5Sclose(dataspace_id); + dataspace_id = -1; + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + dataspace_id = H5.H5Screate_simple(RANK, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the attribute and write the array data to it. + try { + if ((dataset_id >= 0) && (dataspace_id >= 0)) + attribute_id = H5.H5Acreate(dataset_id, ATTRIBUTENAME, HDF5Constants.H5T_IEEE_F64LE, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the dataset. + try { + if (attribute_id >= 0) + H5.H5Awrite(attribute_id, HDF5Constants.H5T_NATIVE_DOUBLE, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // End access to the dataset and release resources used by it. + try { + if (attribute_id >= 0) + H5.H5Aclose(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void ReadDataset() { + long file_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long attribute_id = -1; + long[] dims = { DIM0, DIM1 }; + double[][] dset_data; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open an existing dataset. + try { + if (file_id >= 0) + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + attribute_id = H5.H5Aopen_by_name(dataset_id, ".", ATTRIBUTENAME, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get dataspace and allocate memory for read buffer. + try { + if (attribute_id >= 0) + dataspace_id = H5.H5Aget_space(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataspace_id >= 0) + H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Allocate array of pointers to two-dimensional arrays (the + // elements of the dataset. + dset_data = new double[(int) dims[0]][(int) (dims[1])]; + + // Read data. + try { + if (attribute_id >= 0) + H5.H5Aread(attribute_id, HDF5Constants.H5T_NATIVE_DOUBLE, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + DecimalFormat df = new DecimalFormat("#,##0.0000"); + System.out.println(ATTRIBUTENAME + ":"); + for (int indx = 0; indx < dims[0]; indx++) { + System.out.print(" ["); + for (int jndx = 0; jndx < dims[1]; jndx++) { + System.out.print(" " + df.format(dset_data[indx][jndx])); + } + System.out.println("]"); + } + System.out.println(); + + // End access to the dataset and release resources used by it. + try { + if (attribute_id >= 0) + H5.H5Aclose(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + H5Ex_T_FloatAttribute.CreateDataset(); + // Now we begin the read section of this example. Here we assume + // the dataset and array have the same name and rank, but can have + // any size. Therefore we must allocate a new array to read in + // data using malloc(). + H5Ex_T_FloatAttribute.ReadDataset(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_Integer.java b/java/examples/datatypes/H5Ex_T_Integer.java new file mode 100644 index 0000000..2f365cd --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_Integer.java @@ -0,0 +1,226 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to read and write integer datatypes + to a dataset. The program first writes integers to a + dataset with a dataspace of DIM0xDIM1, then closes the + file. Next, it reopens the file, reads back the data, and + outputs it to the screen. + ************************************************************/ + +package examples.datatypes; + +import java.text.DecimalFormat; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +public class H5Ex_T_Integer { + private static String FILENAME = "H5Ex_T_Integer.h5"; + private static String DATASETNAME = "DS1"; + private static final int DIM0 = 4; + private static final int DIM1 = 7; + private static final int RANK = 2; + + private static void CreateDataset() { + long file_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long[] dims = { DIM0, DIM1 }; + int[][] dset_data = new int[DIM0][DIM1]; + + // Initialize data. + for (int indx = 0; indx < DIM0; indx++) + for (int jndx = 0; jndx < DIM1; jndx++) { + dset_data[indx][jndx] = indx * jndx - jndx; + } + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + dataspace_id = H5.H5Screate_simple(RANK, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the dataset and write the integer data to it. In this + // example we will save the data as 64 bit big endian integers, + // regardless of the native integer type. The HDF5 library + // automatically converts between different integer types. + try { + if ((file_id >= 0) && (dataspace_id >= 0)) + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I64BE, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the data to the dataset. + try { + if (dataset_id >= 0) + H5.H5Dwrite(dataset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // End access to the dataset and release resources used by it. + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void ReadDataset() { + long file_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long[] dims = { DIM0, DIM1 }; + int[][] dset_data; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open an existing dataset. + try { + if (file_id >= 0) + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get dataspace and allocate memory for read buffer. + try { + if (dataset_id >= 0) + dataspace_id = H5.H5Dget_space(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataspace_id >= 0) + H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Allocate array of pointers to two-dimensional arrays (the + // elements of the dataset. + dset_data = new int[(int) dims[0]][(int) (dims[1])]; + + // Read data. + try { + if (dataset_id >= 0) + H5.H5Dread(dataset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + DecimalFormat df = new DecimalFormat("#,##0"); + System.out.println(DATASETNAME + ":"); + for (int indx = 0; indx < dims[0]; indx++) { + System.out.print(" ["); + for (int jndx = 0; jndx < dims[1]; jndx++) { + System.out.print(" " + df.format(dset_data[indx][jndx])); + } + System.out.println("]"); + } + System.out.println(); + + // End access to the dataset and release resources used by it. + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + H5Ex_T_Integer.CreateDataset(); + // Now we begin the read section of this example. Here we assume + // the dataset and array have the same name and rank, but can have + // any size. Therefore we must allocate a new array to read in + // data using malloc(). + H5Ex_T_Integer.ReadDataset(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_IntegerAttribute.java b/java/examples/datatypes/H5Ex_T_IntegerAttribute.java new file mode 100644 index 0000000..c153d99 --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_IntegerAttribute.java @@ -0,0 +1,263 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to read and write integer datatypes + to an attribute. The program first writes integers to an + attribute with a dataspace of DIM0xDIM1, then closes the + file. Next, it reopens the file, reads back the data, and + outputs it to the screen. + ************************************************************/ + +package examples.datatypes; + +import java.text.DecimalFormat; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +public class H5Ex_T_IntegerAttribute { + private static String FILENAME = "H5Ex_T_IntegerAttribute.h5"; + private static String DATASETNAME = "DS1"; + private static String ATTRIBUTENAME = "A1"; + private static final int DIM0 = 4; + private static final int DIM1 = 7; + private static final int RANK = 2; + + private static void CreateDataset() { + long file_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long attribute_id = -1; + long[] dims = { DIM0, DIM1 }; + int[][] dset_data = new int[DIM0][DIM1]; + + // Initialize data. + for (int indx = 0; indx < DIM0; indx++) + for (int jndx = 0; jndx < DIM1; jndx++) { + dset_data[indx][jndx] = indx * jndx - jndx; + } + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataset with a scalar dataspace. + try { + dataspace_id = H5.H5Screate(HDF5Constants.H5S_SCALAR); + if (dataspace_id >= 0) { + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I32LE, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + H5.H5Sclose(dataspace_id); + dataspace_id = -1; + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + dataspace_id = H5.H5Screate_simple(RANK, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the attribute and write the array data to it. + try { + if ((dataset_id >= 0) && (dataspace_id >= 0)) + attribute_id = H5.H5Acreate(dataset_id, ATTRIBUTENAME, HDF5Constants.H5T_STD_I64BE, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the dataset. + try { + if (attribute_id >= 0) + H5.H5Awrite(attribute_id, HDF5Constants.H5T_NATIVE_INT, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // End access to the dataset and release resources used by it. + try { + if (attribute_id >= 0) + H5.H5Aclose(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void ReadDataset() { + long file_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long attribute_id = -1; + long[] dims = { DIM0, DIM1 }; + int[][] dset_data; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open an existing dataset. + try { + if (file_id >= 0) + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + attribute_id = H5.H5Aopen_by_name(dataset_id, ".", ATTRIBUTENAME, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get dataspace and allocate memory for read buffer. + try { + if (attribute_id >= 0) + dataspace_id = H5.H5Aget_space(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataspace_id >= 0) + H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Allocate array of pointers to two-dimensional arrays (the + // elements of the dataset. + dset_data = new int[(int) dims[0]][(int) (dims[1])]; + + // Read data. + try { + if (attribute_id >= 0) + H5.H5Aread(attribute_id, HDF5Constants.H5T_NATIVE_INT, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + DecimalFormat df = new DecimalFormat("#,##0"); + System.out.println(ATTRIBUTENAME + ":"); + for (int indx = 0; indx < dims[0]; indx++) { + System.out.print(" ["); + for (int jndx = 0; jndx < dims[1]; jndx++) { + System.out.print(" " + df.format(dset_data[indx][jndx])); + } + System.out.println("]"); + } + System.out.println(); + + // End access to the dataset and release resources used by it. + try { + if (attribute_id >= 0) + H5.H5Aclose(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + H5Ex_T_IntegerAttribute.CreateDataset(); + // Now we begin the read section of this example. Here we assume + // the dataset and array have the same name and rank, but can have + // any size. Therefore we must allocate a new array to read in + // data using malloc(). + H5Ex_T_IntegerAttribute.ReadDataset(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_ObjectReference.java b/java/examples/datatypes/H5Ex_T_ObjectReference.java new file mode 100644 index 0000000..3cad6c9 --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_ObjectReference.java @@ -0,0 +1,347 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to read and write object references + to a dataset. The program first creates objects in the + file and writes references to those objects to a dataset + with a dataspace of DIM0, then closes the file. Next, it + reopens the file, dereferences the references, and outputs + the names of their targets to the screen. + ************************************************************/ +package examples.datatypes; + +import java.util.EnumSet; +import java.util.HashMap; +import java.util.Map; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +public class H5Ex_T_ObjectReference { + private static String FILENAME = "H5Ex_T_ObjectReference.h5"; + private static String DATASETNAME = "DS1"; + private static String DATASETNAME2 = "DS2"; + private static String GROUPNAME = "G1"; + private static final int DIM0 = 2; + private static final int RANK = 1; + + // Values for the status of space allocation + enum H5G_obj { + H5G_UNKNOWN(HDF5Constants.H5O_TYPE_UNKNOWN), /* Unknown object type */ + H5G_GROUP(HDF5Constants.H5O_TYPE_GROUP), /* Object is a group */ + H5G_DATASET(HDF5Constants.H5O_TYPE_DATASET), /* Object is a dataset */ + H5G_TYPE(HDF5Constants.H5O_TYPE_NAMED_DATATYPE); /* Object is a named data type */ + private static final Map<Integer, H5G_obj> lookup = new HashMap<Integer, H5G_obj>(); + + static { + for (H5G_obj s : EnumSet.allOf(H5G_obj.class)) + lookup.put(s.getCode(), s); + } + + private int code; + + H5G_obj(int layout_type) { + this.code = layout_type; + } + + public int getCode() { + return this.code; + } + + public static H5G_obj get(int code) { + return lookup.get(code); + } + } + + private static void writeObjRef() { + long file_id = -1; + long dataspace_id = -1; + long filespace_id = -1; + long group_id = -1; + long dataset_id = -1; + long[] dims = { DIM0 }; + byte[][] dset_data = new byte[DIM0][8]; + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataset with a scalar dataspace. + try { + dataspace_id = H5.H5Screate(HDF5Constants.H5S_SCALAR); + if ((file_id >= 0) && (dataspace_id >= 0)) { + dataset_id = H5.H5Dcreate(file_id, DATASETNAME2, HDF5Constants.H5T_STD_I32LE, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + dataset_id = -1; + H5.H5Sclose(dataspace_id); + dataspace_id = -1; + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create a group in the file. + try { + if (file_id >= 0) + group_id = H5.H5Gcreate(file_id, GROUPNAME, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + if (group_id >= 0) + H5.H5Gclose(group_id); + group_id = -1; + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create references to the previously created objects. Passing -1 + // as space_id causes this parameter to be ignored. Other values + // besides valid dataspaces result in an error. + try { + if (file_id >= 0) { + byte rbuf0[] = H5.H5Rcreate(file_id, GROUPNAME, HDF5Constants.H5R_OBJECT, -1); + byte rbuf1[] = H5.H5Rcreate(file_id, DATASETNAME2, HDF5Constants.H5R_OBJECT, -1); + for (int indx = 0; indx < 8; indx++) { + dset_data[0][indx] = rbuf0[indx]; + dset_data[1][indx] = rbuf1[indx]; + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + filespace_id = H5.H5Screate_simple(RANK, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the dataset. + try { + if ((file_id >= 0) && (filespace_id >= 0)) + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_REF_OBJ, filespace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the object references to it. + try { + if (dataset_id >= 0) + H5.H5Dwrite(dataset_id, HDF5Constants.H5T_STD_REF_OBJ, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // End access to the dataset and release resources used by it. + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (filespace_id >= 0) + H5.H5Sclose(filespace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + private static void readObjRef() { + long file_id = -1; + long dataset_id = -1; + long dataspace_id = -1; + int object_type = -1; + long object_id = -1; + long[] dims = { DIM0 }; + byte[][] dset_data; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open an existing dataset. + try { + if (file_id >= 0) + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get dataspace and allocate memory for read buffer. + try { + if (dataset_id >= 0) + dataspace_id = H5.H5Dget_space(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataspace_id >= 0) + H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Allocate array of pointers to two-dimensional arrays (the + // elements of the dataset. + dset_data = new byte[(int)dims[0]][8]; + + // Read the data using the default properties. + try { + if (dataset_id >= 0) { + H5.H5Dread(dataset_id, HDF5Constants.H5T_STD_REF_OBJ, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + for (int indx = 0; indx < dims[0]; indx++) { + System.out.println(DATASETNAME + "[" + indx + "]:"); + System.out.print(" ->"); + // Open the referenced object, get its name and type. + try { + if (dataset_id >= 0) { + object_id = H5.H5Rdereference(dataset_id, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5R_OBJECT, dset_data[indx]); + object_type = H5.H5Rget_obj_type(dataset_id, HDF5Constants.H5R_OBJECT, dset_data[indx]); + } + String[] obj_name = new String[1]; + long name_size = 1; + if (object_type >= 0) { + // Get the length of the name and retrieve the name. + name_size = 1 + H5.H5Iget_name(object_id, obj_name, name_size); + } + if ((object_id >= 0) && (object_type >= -1)) { + switch (H5G_obj.get(object_type)) { + case H5G_GROUP: + System.out.print("H5G_GROUP"); + try { + if (object_id >= 0) + H5.H5Gclose(object_id); + } + catch (Exception e) { + e.printStackTrace(); + } + break; + case H5G_DATASET: + System.out.print("H5G_DATASET"); + try { + if (object_id >= 0) + H5.H5Dclose(object_id); + } + catch (Exception e) { + e.printStackTrace(); + } + break; + case H5G_TYPE: + System.out.print("H5G_TYPE"); + try { + if (object_id >= 0) + H5.H5Tclose(object_id); + } + catch (Exception e) { + e.printStackTrace(); + } + break; + default: + System.out.print("UNHANDLED"); + } + } + // Print the name. + if (name_size > 1) + System.out.println(": " + obj_name[0]); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + // End access to the dataset and release resources used by it. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + // Check if gzip compression is available and can be used for both + // compression and decompression. Normally we do not perform error + // checking in these examples for the sake of clarity, but in this + // case we will make an exception because this filter is an + // optional part of the hdf5 library. + H5Ex_T_ObjectReference.writeObjRef(); + H5Ex_T_ObjectReference.readObjRef(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_ObjectReferenceAttribute.java b/java/examples/datatypes/H5Ex_T_ObjectReferenceAttribute.java new file mode 100644 index 0000000..e354029 --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_ObjectReferenceAttribute.java @@ -0,0 +1,389 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to read and write object references + to an attribute. The program first creates objects in the + file and writes references to those objects to an + attribute with a dataspace of DIM0, then closes the file. + Next, it reopens the file, dereferences the references, + and outputs the names of their targets to the screen. + ************************************************************/ + +package examples.datatypes; + +import java.util.EnumSet; +import java.util.HashMap; +import java.util.Map; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +public class H5Ex_T_ObjectReferenceAttribute { + private static String FILENAME = "H5Ex_T_ObjectReferenceAttribute.h5"; + private static String DATASETNAME = "DS1"; + private static String ATTRIBUTENAME = "A1"; + private static String DATASETNAME2 = "DS2"; + private static String GROUPNAME = "G1"; + private static final int DIM0 = 2; + private static final int RANK = 1; + + // Values for the status of space allocation + enum H5G_obj { + H5G_UNKNOWN(HDF5Constants.H5O_TYPE_UNKNOWN), /* Unknown object type */ + H5G_GROUP(HDF5Constants.H5O_TYPE_GROUP), /* Object is a group */ + H5G_DATASET(HDF5Constants.H5O_TYPE_DATASET), /* Object is a dataset */ + H5G_TYPE(HDF5Constants.H5O_TYPE_NAMED_DATATYPE); /* Object is a named data type */ + private static final Map<Integer, H5G_obj> lookup = new HashMap<Integer, H5G_obj>(); + + static { + for (H5G_obj s : EnumSet.allOf(H5G_obj.class)) + lookup.put(s.getCode(), s); + } + + private int code; + + H5G_obj(int layout_type) { + this.code = layout_type; + } + + public int getCode() { + return this.code; + } + + public static H5G_obj get(int code) { + return lookup.get(code); + } + } + + private static void CreateDataset() { + long file_id = -1; + long dataspace_id = -1; + long group_id = -1; + long dataset_id = -1; + long attribute_id = -1; + long[] dims = { DIM0 }; + byte[][] dset_data = new byte[DIM0][8]; + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataset with a scalar dataspace. + try { + dataspace_id = H5.H5Screate(HDF5Constants.H5S_SCALAR); + if (dataspace_id >= 0) { + dataset_id = H5.H5Dcreate(file_id, DATASETNAME2, HDF5Constants.H5T_STD_I32LE, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + dataset_id = -1; + H5.H5Sclose(dataspace_id); + dataspace_id = -1; + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create a group in the file. + try { + if (file_id >= 0) + group_id = H5.H5Gcreate(file_id, GROUPNAME, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + if (group_id >= 0) + H5.H5Gclose(group_id); + group_id = -1; + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create references to the previously created objects. Passing -1 + // as space_id causes this parameter to be ignored. Other values + // besides valid dataspaces result in an error. + try { + if (file_id >= 0) { + byte rbuf0[] = H5.H5Rcreate(file_id, GROUPNAME, HDF5Constants.H5R_OBJECT, -1); + byte rbuf1[] = H5.H5Rcreate(file_id, DATASETNAME2, HDF5Constants.H5R_OBJECT, -1); + for (int indx = 0; indx < 8; indx++) { + dset_data[0][indx] = rbuf0[indx]; + dset_data[1][indx] = rbuf1[indx]; + } + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataset with a scalar dataspace to serve as the parent + // for the attribute. + try { + dataspace_id = H5.H5Screate(HDF5Constants.H5S_SCALAR); + if (dataspace_id >= 0) { + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I32LE, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + H5.H5Sclose(dataspace_id); + dataspace_id = -1; + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + dataspace_id = H5.H5Screate_simple(RANK, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the attribute and write the array data to it. + try { + if ((dataset_id >= 0) && (dataspace_id >= 0)) + attribute_id = H5.H5Acreate(dataset_id, ATTRIBUTENAME, HDF5Constants.H5T_STD_REF_OBJ, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the dataset. + try { + if (attribute_id >= 0) + H5.H5Awrite(attribute_id, HDF5Constants.H5T_STD_REF_OBJ, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // End access to the dataset and release resources used by it. + try { + if (attribute_id >= 0) + H5.H5Aclose(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void ReadDataset() { + long file_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long attribute_id = -1; + int object_type = -1; + long object_id = -1; + long[] dims = { DIM0 }; + byte[][] dset_data; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open an existing dataset. + try { + if (file_id >= 0) + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + attribute_id = H5.H5Aopen_by_name(dataset_id, ".", ATTRIBUTENAME, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get dataspace and allocate memory for read buffer. + try { + if (attribute_id >= 0) + dataspace_id = H5.H5Aget_space(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataspace_id >= 0) + H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Allocate array of pointers to two-dimensional arrays (the + // elements of the dataset. + dset_data = new byte[(int) dims[0]][8]; + + // Read data. + try { + if (attribute_id >= 0) + H5.H5Aread(attribute_id, HDF5Constants.H5T_STD_REF_OBJ, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + for (int indx = 0; indx < dims[0]; indx++) { + System.out.println(ATTRIBUTENAME + "[" + indx + "]:"); + System.out.print(" ->"); + // Open the referenced object, get its name and type. + try { + if (dataset_id >= 0) { + object_id = H5.H5Rdereference(dataset_id, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5R_OBJECT, dset_data[indx]); + object_type = H5.H5Rget_obj_type(dataset_id, HDF5Constants.H5R_OBJECT, dset_data[indx]); + } + String[] obj_name = new String[1]; + long name_size = 1; + if (object_type >= 0) { + // Get the length of the name and retrieve the name. + name_size = 1 + H5.H5Iget_name(object_id, obj_name, name_size); + } + if ((object_id >= 0) && (object_type >= -1)) { + switch (H5G_obj.get(object_type)) { + case H5G_GROUP: + System.out.print("H5G_GROUP"); + try { + if (object_id >= 0) + H5.H5Gclose(object_id); + } + catch (Exception e) { + e.printStackTrace(); + } + break; + case H5G_DATASET: + System.out.print("H5G_DATASET"); + try { + if (object_id >= 0) + H5.H5Dclose(object_id); + } + catch (Exception e) { + e.printStackTrace(); + } + break; + case H5G_TYPE: + System.out.print("H5G_TYPE"); + try { + if (object_id >= 0) + H5.H5Tclose(object_id); + } + catch (Exception e) { + e.printStackTrace(); + } + break; + default: + System.out.print("UNHANDLED"); + } + } + // Print the name. + if (name_size > 1) + System.out.println(": " + obj_name[0]); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + // End access to the dataset and release resources used by it. + try { + if (attribute_id >= 0) + H5.H5Aclose(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + H5Ex_T_ObjectReferenceAttribute.CreateDataset(); + // Now we begin the read section of this example. Here we assume + // the dataset and array have the same name and rank, but can have + // any size. Therefore we must allocate a new array to read in + // data using malloc(). + H5Ex_T_ObjectReferenceAttribute.ReadDataset(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_Opaque.java b/java/examples/datatypes/H5Ex_T_Opaque.java new file mode 100644 index 0000000..eb45686 --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_Opaque.java @@ -0,0 +1,270 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to read and write opaque datatypes + to a dataset. The program first writes opaque data to a + dataset with a dataspace of DIM0, then closes the file. + Next, it reopens the file, reads back the data, and + outputs it to the screen. + ************************************************************/ + +package examples.datatypes; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +public class H5Ex_T_Opaque { + private static String FILENAME = "H5Ex_T_Opaque.h5"; + private static String DATASETNAME = "DS1"; + private static final int DIM0 = 4; + private static final int LEN = 7; + private static final int RANK = 1; + + private static void CreateDataset() { + long file_id = -1; + long dataspace_id = -1; + long datatype_id = -1; + long dataset_id = -1; + long[] dims = { DIM0 }; + byte[] dset_data = new byte[DIM0 * LEN]; + byte[] str_data = { 'O', 'P', 'A', 'Q', 'U', 'E' }; + + // Initialize data. + for (int indx = 0; indx < DIM0; indx++) { + for (int jndx = 0; jndx < LEN - 1; jndx++) + dset_data[jndx + indx * LEN] = str_data[jndx]; + dset_data[LEN - 1 + indx * LEN] = (byte) (indx + '0'); + } + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create opaque datatype and set the tag to something appropriate. + // For this example we will write and view the data as a character + // array. + try { + datatype_id = H5.H5Tcreate(HDF5Constants.H5T_OPAQUE, (long)LEN); + if (datatype_id >= 0) + H5.H5Tset_tag(datatype_id, "Character array"); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + dataspace_id = H5.H5Screate_simple(RANK, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the dataset and write the integer data to it. In this + // example we will save the data as 64 bit big endian integers, + // regardless of the native integer type. The HDF5 library + // automatically converts between different integer types. + try { + if ((file_id >= 0) && (datatype_id >= 0) && (dataspace_id >= 0)) + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, datatype_id, dataspace_id, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the opaque data to the dataset. + try { + if ((dataset_id >= 0) && (datatype_id >= 0)) + H5.H5Dwrite(dataset_id, datatype_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // End access to the dataset and release resources used by it. + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (datatype_id >= 0) + H5.H5Tclose(datatype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void ReadDataset() { + long file_id = -1; + long datatype_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long type_len = -1; + long[] dims = { DIM0 }; + byte[] dset_data; + String tag_name = null; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open an existing dataset. + try { + if (file_id >= 0) + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get datatype and properties for the datatype. + try { + if (dataset_id >= 0) + datatype_id = H5.H5Dget_type(dataset_id); + if (datatype_id >= 0) { + type_len = H5.H5Tget_size(datatype_id); + tag_name = H5.H5Tget_tag(datatype_id); + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get dataspace and allocate memory for read buffer. + try { + if (dataset_id >= 0) + dataspace_id = H5.H5Dget_space(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataspace_id >= 0) + H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Allocate buffer. + dset_data = new byte[(int) (dims[0] * type_len)]; + + // Read data. + try { + if ((dataset_id >= 0) && (datatype_id >= 0)) + H5.H5Dread(dataset_id, datatype_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + System.out.println("Datatype tag for " + DATASETNAME + " is: \"" + tag_name + "\""); + for (int indx = 0; indx < dims[0]; indx++) { + System.out.print(DATASETNAME + "[" + indx + "]: "); + for (int jndx = 0; jndx < type_len; jndx++) { + char temp = (char) dset_data[jndx + indx * (int)type_len]; + System.out.print(temp); + } + System.out.println(""); + } + System.out.println(); + + // End access to the dataset and release resources used by it. + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (datatype_id >= 0) + H5.H5Tclose(datatype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + H5Ex_T_Opaque.CreateDataset(); + // Now we begin the read section of this example. Here we assume + // the dataset and array have the same name and rank, but can have + // any size. Therefore we must allocate a new array to read in + // data using malloc(). + H5Ex_T_Opaque.ReadDataset(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_OpaqueAttribute.java b/java/examples/datatypes/H5Ex_T_OpaqueAttribute.java new file mode 100644 index 0000000..e42bfe8 --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_OpaqueAttribute.java @@ -0,0 +1,307 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to read and write opaque datatypes + to an attribute. The program first writes opaque data to + an attribute with a dataspace of DIM0, then closes the + file. Next, it reopens the file, reads back the data, and + outputs it to the screen. + ************************************************************/ + +package examples.datatypes; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +public class H5Ex_T_OpaqueAttribute { + private static String FILENAME = "H5Ex_T_OpaqueAttribute.h5"; + private static String DATASETNAME = "DS1"; + private static String ATTRIBUTENAME = "A1"; + private static final int DIM0 = 4; + private static final int LEN = 7; + private static final int RANK = 1; + + private static void CreateDataset() { + long file_id = -1; + long dataspace_id = -1; + long datatype_id = -1; + long dataset_id = -1; + long attribute_id = -1; + long[] dims = { DIM0 }; + byte[] dset_data = new byte[DIM0 * LEN]; + byte[] str_data = { 'O', 'P', 'A', 'Q', 'U', 'E' }; + + // Initialize data. + for (int indx = 0; indx < DIM0; indx++) { + for (int jndx = 0; jndx < LEN - 1; jndx++) + dset_data[jndx + indx * LEN] = str_data[jndx]; + dset_data[LEN - 1 + indx * LEN] = (byte) (indx + '0'); + } + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataset with a scalar dataspace. + try { + dataspace_id = H5.H5Screate(HDF5Constants.H5S_SCALAR); + if (dataspace_id >= 0) { + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I32LE, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + H5.H5Sclose(dataspace_id); + dataspace_id = -1; + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create opaque datatype and set the tag to something appropriate. + // For this example we will write and view the data as a character + // array. + try { + datatype_id = H5.H5Tcreate(HDF5Constants.H5T_OPAQUE, (long)LEN); + if (datatype_id >= 0) + H5.H5Tset_tag(datatype_id, "Character array"); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + dataspace_id = H5.H5Screate_simple(RANK, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the attribute and write the array data to it. + try { + if ((dataset_id >= 0) && (datatype_id >= 0) && (dataspace_id >= 0)) + attribute_id = H5.H5Acreate(dataset_id, ATTRIBUTENAME, datatype_id, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the dataset. + try { + if ((attribute_id >= 0) && (datatype_id >= 0)) + H5.H5Awrite(attribute_id, datatype_id, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // End access to the dataset and release resources used by it. + try { + if (attribute_id >= 0) + H5.H5Aclose(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (datatype_id >= 0) + H5.H5Tclose(datatype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void ReadDataset() { + long file_id = -1; + long datatype_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long attribute_id = -1; + long type_len = -1; + long[] dims = { DIM0 }; + byte[] dset_data; + String tag_name = null; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open an existing dataset. + try { + if (file_id >= 0) + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + attribute_id = H5.H5Aopen_by_name(dataset_id, ".", ATTRIBUTENAME, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get datatype and properties for the datatype. + try { + if (attribute_id >= 0) + datatype_id = H5.H5Aget_type(attribute_id); + if (datatype_id >= 0) { + type_len = H5.H5Tget_size(datatype_id); + tag_name = H5.H5Tget_tag(datatype_id); + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get dataspace and allocate memory for read buffer. + try { + if (attribute_id >= 0) + dataspace_id = H5.H5Aget_space(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataspace_id >= 0) + H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Allocate buffer. + dset_data = new byte[(int) (dims[0] * type_len)]; + + // Read data. + try { + if ((attribute_id >= 0) && (datatype_id >= 0)) + H5.H5Aread(attribute_id, datatype_id, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + System.out.println("Datatype tag for " + ATTRIBUTENAME + " is: \"" + tag_name + "\""); + for (int indx = 0; indx < dims[0]; indx++) { + System.out.print(ATTRIBUTENAME + "[" + indx + "]: "); + for (int jndx = 0; jndx < type_len; jndx++) { + char temp = (char) dset_data[jndx + indx * (int)type_len]; + System.out.print(temp); + } + System.out.println(""); + } + System.out.println(); + + // End access to the dataset and release resources used by it. + try { + if (attribute_id >= 0) + H5.H5Aclose(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (datatype_id >= 0) + H5.H5Tclose(datatype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + H5Ex_T_OpaqueAttribute.CreateDataset(); + // Now we begin the read section of this example. Here we assume + // the dataset and array have the same name and rank, but can have + // any size. Therefore we must allocate a new array to read in + // data using malloc(). + H5Ex_T_OpaqueAttribute.ReadDataset(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_String.java b/java/examples/datatypes/H5Ex_T_String.java new file mode 100644 index 0000000..469172d --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_String.java @@ -0,0 +1,311 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to read and write string datatypes + to a dataset. The program first writes strings to a + dataset with a dataspace of DIM0, then closes the file. + Next, it reopens the file, reads back the data, and + outputs it to the screen. + ************************************************************/ + +package examples.datatypes; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +public class H5Ex_T_String { + private static String FILENAME = "H5Ex_T_String.h5"; + private static String DATASETNAME = "DS1"; + private static final int DIM0 = 4; + private static final int SDIM = 8; + private static final int RANK = 1; + + private static void CreateDataset() { + long file_id = -1; + long memtype_id = -1; + long filetype_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long[] dims = { DIM0 }; + byte[][] dset_data = new byte[DIM0][SDIM]; + StringBuffer[] str_data = { new StringBuffer("Parting"), new StringBuffer("is such"), + new StringBuffer("sweet"), new StringBuffer("sorrow.") }; + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create file and memory datatypes. For this example we will save + // the strings as FORTRAN strings, therefore they do not need space + // for the null terminator in the file. + try { + filetype_id = H5.H5Tcopy(HDF5Constants.H5T_FORTRAN_S1); + if (filetype_id >= 0) + H5.H5Tset_size(filetype_id, SDIM - 1); + } + catch (Exception e) { + e.printStackTrace(); + } + try { + memtype_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1); + if (memtype_id >= 0) + H5.H5Tset_size(memtype_id, SDIM); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + dataspace_id = H5.H5Screate_simple(RANK, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the dataset and write the string data to it. + try { + if ((file_id >= 0) && (filetype_id >= 0) && (dataspace_id >= 0)) + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, filetype_id, dataspace_id, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the data to the dataset. + try { + for (int indx = 0; indx < DIM0; indx++) { + for (int jndx = 0; jndx < SDIM; jndx++) { + if (jndx < str_data[indx].length()) + dset_data[indx][jndx] = (byte) str_data[indx].charAt(jndx); + else + dset_data[indx][jndx] = 0; + } + } + if ((dataset_id >= 0) && (memtype_id >= 0)) + H5.H5Dwrite(dataset_id, memtype_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // End access to the dataset and release resources used by it. + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the file type. + try { + if (filetype_id >= 0) + H5.H5Tclose(filetype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the mem type. + try { + if (memtype_id >= 0) + H5.H5Tclose(memtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void ReadDataset() { + long file_id = -1; + long filetype_id = -1; + long memtype_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long sdim = 0; + long[] dims = { DIM0 }; + byte[][] dset_data; + StringBuffer[] str_data; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open an existing dataset. + try { + if (file_id >= 0) + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get the datatype and its size. + try { + if (dataset_id >= 0) + filetype_id = H5.H5Dget_type(dataset_id); + if (filetype_id >= 0) { + sdim = H5.H5Tget_size(filetype_id); + sdim++; // Make room for null terminator + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get dataspace and allocate memory for read buffer. + try { + if (dataset_id >= 0) + dataspace_id = H5.H5Dget_space(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataspace_id >= 0) + H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Allocate space for data. + dset_data = new byte[(int) dims[0]][(int)sdim]; + str_data = new StringBuffer[(int) dims[0]]; + + // Create the memory datatype. + try { + memtype_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1); + if (memtype_id >= 0) + H5.H5Tset_size(memtype_id, sdim); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Read data. + try { + if ((dataset_id >= 0) && (memtype_id >= 0)) + H5.H5Dread(dataset_id, memtype_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + byte[] tempbuf = new byte[(int)sdim]; + for (int indx = 0; indx < (int) dims[0]; indx++) { + for (int jndx = 0; jndx < sdim; jndx++) { + tempbuf[jndx] = dset_data[indx][jndx]; + } + str_data[indx] = new StringBuffer(new String(tempbuf).trim()); + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + for (int indx = 0; indx < dims[0]; indx++) { + System.out.println(DATASETNAME + " [" + indx + "]: " + str_data[indx]); + } + System.out.println(); + + // End access to the dataset and release resources used by it. + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the file type. + try { + if (filetype_id >= 0) + H5.H5Tclose(filetype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the mem type. + try { + if (memtype_id >= 0) + H5.H5Tclose(memtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + H5Ex_T_String.CreateDataset(); + // Now we begin the read section of this example. Here we assume + // the dataset and array have the same name and rank, but can have + // any size. Therefore we must allocate a new array to read in + // data using malloc(). + H5Ex_T_String.ReadDataset(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_StringAttribute.java b/java/examples/datatypes/H5Ex_T_StringAttribute.java new file mode 100644 index 0000000..49361bc --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_StringAttribute.java @@ -0,0 +1,351 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + This example shows how to read and write string datatypes + to an attribute. The program first writes strings to an + attribute with a dataspace of DIM0, then closes the file. + Next, it reopens the file, reads back the data, and + outputs it to the screen. + ************************************************************/ + +package examples.datatypes; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +public class H5Ex_T_StringAttribute { + private static String FILENAME = "H5Ex_T_StringAttribute.h5"; + private static String DATASETNAME = "DS1"; + private static String ATTRIBUTENAME = "A1"; + private static final int DIM0 = 4; + private static final int SDIM = 8; + private static final int RANK = 1; + + private static void CreateDataset() { + long file_id = -1; + long memtype_id = -1; + long filetype_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long attribute_id = -1; + long[] dims = { DIM0 }; + byte[][] dset_data = new byte[DIM0][SDIM]; + StringBuffer[] str_data = { new StringBuffer("Parting"), new StringBuffer("is such"), + new StringBuffer("sweet"), new StringBuffer("sorrow.") }; + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create file and memory datatypes. For this example we will save + // the strings as FORTRAN strings, therefore they do not need space + // for the null terminator in the file. + try { + filetype_id = H5.H5Tcopy(HDF5Constants.H5T_FORTRAN_S1); + if (filetype_id >= 0) + H5.H5Tset_size(filetype_id, SDIM - 1); + } + catch (Exception e) { + e.printStackTrace(); + } + try { + memtype_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1); + if (memtype_id >= 0) + H5.H5Tset_size(memtype_id, SDIM); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataset with a scalar dataspace. + try { + dataspace_id = H5.H5Screate(HDF5Constants.H5S_SCALAR); + if (dataspace_id >= 0) { + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I32LE, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + H5.H5Sclose(dataspace_id); + dataspace_id = -1; + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + dataspace_id = H5.H5Screate_simple(RANK, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the attribute. + try { + if ((dataset_id >= 0) && (dataspace_id >= 0) && (filetype_id >= 0)) + attribute_id = H5.H5Acreate(dataset_id, ATTRIBUTENAME, filetype_id, dataspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the data to the dataset. + try { + for (int indx = 0; indx < DIM0; indx++) { + for (int jndx = 0; jndx < SDIM; jndx++) { + if (jndx < str_data[indx].length()) + dset_data[indx][jndx] = (byte) str_data[indx].charAt(jndx); + else + dset_data[indx][jndx] = 0; + } + } + if ((attribute_id >= 0) && (memtype_id >= 0)) + H5.H5Awrite(attribute_id, memtype_id, dset_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + // End access to the dataset and release resources used by it. + try { + if (attribute_id >= 0) + H5.H5Aclose(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the file type. + try { + if (filetype_id >= 0) + H5.H5Tclose(filetype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the mem type. + try { + if (memtype_id >= 0) + H5.H5Tclose(memtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + private static void ReadDataset() { + long file_id = -1; + long filetype_id = -1; + long memtype_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + long attribute_id = -1; + long sdim = 0; + long[] dims = { DIM0 }; + byte[][] dset_data; + StringBuffer[] str_data; + + // Open an existing file. + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Open an existing dataset. + try { + if (file_id >= 0) + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + attribute_id = H5.H5Aopen_by_name(dataset_id, ".", ATTRIBUTENAME, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get the datatype and its size. + try { + if (attribute_id >= 0) + filetype_id = H5.H5Aget_type(attribute_id); + if (filetype_id >= 0) { + sdim = H5.H5Tget_size(filetype_id); + sdim++; // Make room for null terminator + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Get dataspace and allocate memory for read buffer. + try { + if (attribute_id >= 0) + dataspace_id = H5.H5Aget_space(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataspace_id >= 0) + H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Allocate space for data. + dset_data = new byte[(int) dims[0]][(int)sdim]; + str_data = new StringBuffer[(int) dims[0]]; + + // Create the memory datatype. + try { + memtype_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1); + if (memtype_id >= 0) + H5.H5Tset_size(memtype_id, sdim); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Read data. + try { + if ((attribute_id >= 0) && (memtype_id >= 0)) + H5.H5Aread(attribute_id, memtype_id, dset_data); + byte[] tempbuf = new byte[(int)sdim]; + for (int indx = 0; indx < (int) dims[0]; indx++) { + for (int jndx = 0; jndx < sdim; jndx++) { + tempbuf[jndx] = dset_data[indx][jndx]; + } + str_data[indx] = new StringBuffer(new String(tempbuf).trim()); + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Output the data to the screen. + for (int indx = 0; indx < dims[0]; indx++) { + System.out.println(DATASETNAME + " [" + indx + "]: " + str_data[indx]); + } + System.out.println(); + + // End access to the dataset and release resources used by it. + try { + if (attribute_id >= 0) + H5.H5Aclose(attribute_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + if (dataset_id >= 0) + H5.H5Dclose(dataset_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the data space. + try { + if (dataspace_id >= 0) + H5.H5Sclose(dataspace_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the file type. + try { + if (filetype_id >= 0) + H5.H5Tclose(filetype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Terminate access to the mem type. + try { + if (memtype_id >= 0) + H5.H5Tclose(memtype_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Close the file. + try { + if (file_id >= 0) + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + + } + + public static void main(String[] args) { + H5Ex_T_StringAttribute.CreateDataset(); + // Now we begin the read section of this example. Here we assume + // the dataset and array have the same name and rank, but can have + // any size. Therefore we must allocate a new array to read in + // data using malloc(). + H5Ex_T_StringAttribute.ReadDataset(); + } + +} diff --git a/java/examples/datatypes/H5Ex_T_VLString.java b/java/examples/datatypes/H5Ex_T_VLString.java new file mode 100644 index 0000000..c8892ba --- /dev/null +++ b/java/examples/datatypes/H5Ex_T_VLString.java @@ -0,0 +1,138 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * 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 files COPYING and Copyright.html. COPYING can be found at the root * + * of the source code distribution tree; Copyright.html can be found at the * + * root level of an installed copy of the electronic HDF5 document set and * + * is linked from the top-level documents page. It can also be found at * + * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * + * access to either file, you may request a copy from help@hdfgroup.org. * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/************************************************************ + Creating and writing a VL string to a file. + ************************************************************/ + +package examples.datatypes; + +import hdf.hdf5lib.H5; +import hdf.hdf5lib.HDF5Constants; + +public class H5Ex_T_VLString +{ + private static String FILENAME = "H5Ex_T_VLString.h5"; + private static String DATASETNAME = "DS1"; + + private static void createDataset() { + long file_id = -1; + long type_id = -1; + long dataspace_id = -1; + long dataset_id = -1; + int rank = 1; + String[] str_data = { "Parting", "is such", "sweet", "sorrow." }; + long[] dims = { str_data.length }; + + // Create a new file using default properties. + try { + file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + type_id = H5.H5Tcopy(HDF5Constants.H5T_C_S1); + H5.H5Tset_size(type_id, HDF5Constants.H5T_VARIABLE); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create dataspace. Setting maximum size to NULL sets the maximum + // size to be the current size. + try { + dataspace_id = H5.H5Screate_simple(rank, dims, null); + } + catch (Exception e) { + e.printStackTrace(); + } + + // Create the dataset and write the string data to it. + try { + if ((file_id >= 0) && (type_id >= 0) && (dataspace_id >= 0)) { + dataset_id = H5.H5Dcreate(file_id, DATASETNAME, type_id, dataspace_id, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + } + } + catch (Exception e) { + e.printStackTrace(); + } + + // Write the data to the dataset. + try { + if (dataset_id >= 0) + H5.H5Dwrite_VLStrings(dataset_id, type_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, str_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + H5.H5Sclose(dataspace_id); + H5.H5Tclose(type_id); + H5.H5Dclose(dataset_id); + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + private static void readDataset() { + long file_id = -1; + long type_id = -1; + long dataset_id = -1; + String[] str_data = { "", "", "", "" }; + + try { + file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); + } + catch (Exception e) { + e.printStackTrace(); + } + + try { + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + type_id = H5.H5Dget_type(dataset_id); + H5.H5Dread_VLStrings(dataset_id, type_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, + str_data); + } + catch (Exception e) { + e.printStackTrace(); + } + + for (int indx = 0; indx < str_data.length; indx++) + System.out.println(DATASETNAME + " [" + indx + "]: " + str_data[indx]); + + try { + H5.H5Tclose(type_id); + H5.H5Dclose(dataset_id); + H5.H5Fclose(file_id); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + H5Ex_T_VLString.createDataset(); + H5Ex_T_VLString.readDataset(); + } + +} diff --git a/java/examples/datatypes/Makefile.am b/java/examples/datatypes/Makefile.am new file mode 100644 index 0000000..de2ea9d --- /dev/null +++ b/java/examples/datatypes/Makefile.am @@ -0,0 +1,78 @@ +# +# 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 files COPYING and Copyright.html. COPYING can be found at the root +# of the source code distribution tree; Copyright.html can be found at the +# root level of an installed copy of the electronic HDF5 document set and +# is linked from the top-level documents page. It can also be found at +# http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have +# access to either file, you may request a copy from help@hdfgroup.org. +## +## Makefile.am +## Run automake to generate a Makefile.in from this file. +## +# +# HDF5 Java Library Examples Makefile(.in) + +include $(top_srcdir)/config/commence.am + +# Mark this directory as part of the JNI API +JAVA_API=yes + +JAVAROOT = .classes + +classes: + $(MKDIR_P) $(@D)/$(JAVAROOT) + +pkgpath = examples/datatypes +hdfjarfile = jar$(PACKAGE_TARNAME)-$(PACKAGE_VERSION).jar +CLASSPATH_ENV=CLASSPATH=.:$(JAVAROOT):$(top_builddir)/java/src/$(hdfjarfile):$(top_srcdir)/java/lib/slf4j-api-1.7.5.jar:$(top_srcdir)/java/lib/ext/slf4j-simple-1.7.5.jar:$$CLASSPATH + +jarfile = jar$(PACKAGE_TARNAME)datatypes.jar + +AM_JAVACFLAGS = $(H5_JAVACFLAGS) -deprecation + +TESTPACKAGE = + +noinst_JAVA = \ + H5Ex_T_Array.java \ + H5Ex_T_ArrayAttribute.java \ + H5Ex_T_Bit.java \ + H5Ex_T_BitAttribute.java \ + H5Ex_T_Commit.java \ + H5Ex_T_Compound.java \ + H5Ex_T_CompoundAttribute.java \ + H5Ex_T_Float.java \ + H5Ex_T_FloatAttribute.java \ + H5Ex_T_Integer.java \ + H5Ex_T_IntegerAttribute.java \ + H5Ex_T_ObjectReference.java \ + H5Ex_T_ObjectReferenceAttribute.java \ + H5Ex_T_Opaque.java \ + H5Ex_T_OpaqueAttribute.java \ + H5Ex_T_String.java \ + H5Ex_T_StringAttribute.java \ + H5Ex_T_VLString.java + +$(jarfile): classnoinst.stamp classes + $(JAR) cvf $@ -C $(JAVAROOT)/ $(pkgpath) + +noinst_DATA = $(jarfile) + +.PHONY: classes + +check_SCRIPTS = runExample.sh +TEST_SCRIPT = $(check_SCRIPTS) + +CLEANFILES = classnoinst.stamp $(jarfile) $(JAVAROOT)/$(pkgpath)/*.class runExample.sh + +clean: + rm -rf $(JAVAROOT) + rm -f $(jarfile) + rm -f classnoinst.stamp + +include $(top_srcdir)/config/conclude.am diff --git a/java/examples/datatypes/runExample.sh.in b/java/examples/datatypes/runExample.sh.in new file mode 100644 index 0000000..9ed3954 --- /dev/null +++ b/java/examples/datatypes/runExample.sh.in @@ -0,0 +1,408 @@ +#! /bin/sh +# +# 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 files COPYING and Copyright.html. COPYING can be found at the root +# of the source code distribution tree; Copyright.html can be found at the +# root level of an installed copy of the electronic HDF5 document set and +# is linked from the top-level documents page. It can also be found at +# http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have +# access to either file, you may request a copy from help@hdfgroup.org. +# + +top_builddir=@top_builddir@ +top_srcdir=@top_srcdir@ +srcdir=@srcdir@ + +TESTNAME=EX_Datatypes +EXIT_SUCCESS=0 +EXIT_FAILURE=1 + +# Set up default variable values if not supplied by the user. +RM='rm -rf' +CMP='cmp' +DIFF='diff -c' +CP='cp' +DIRNAME='dirname' +LS='ls' +AWK='awk' + +nerrors=0 + +# where the libs exist +HDFLIB_HOME="$top_srcdir/java/lib" +BLDLIBDIR="./lib" +BLDDIR="." +HDFTEST_HOME="$top_srcdir/java/examples/datatypes" +JARFILE=jar@PACKAGE_TARNAME@-@PACKAGE_VERSION@.jar +TESTJARFILE=jar@PACKAGE_TARNAME@datatypes.jar +test -d $BLDLIBDIR || mkdir -p $BLDLIBDIR + +###################################################################### +# library files +# -------------------------------------------------------------------- +# All the library files copy from source directory to test directory +# NOTE: Keep this framework to add/remove test files. +# This list are also used for checking exist. +# Comment '#' without space can be used. +# -------------------------------------------------------------------- +LIST_LIBRARY_FILES=" +$HDFLIB_HOME/slf4j-api-1.7.5.jar +$HDFLIB_HOME/ext/slf4j-simple-1.7.5.jar +$top_builddir/src/.libs/libhdf5.* +$top_builddir/java/src/jni/.libs/libhdf5_java.* +$top_builddir/java/src/$JARFILE +" +LIST_DATA_FILES=" +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_Array.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_ArrayAttribute.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_Bit.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_BitAttribute.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_Commit.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_Compound.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_CompoundAttribute.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_Float.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_FloatAttribute.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_Integer.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_IntegerAttribute.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_ObjectReference.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_ObjectReferenceAttribute.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_Opaque.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_OpaqueAttribute.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_String.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_StringAttribute.txt +$HDFTEST_HOME/../testfiles/examples.datatypes.H5Ex_T_VLString.txt +" + +# +# copy files from source dirs to test dir +# +COPY_LIBFILES="$LIST_LIBRARY_FILES" + +COPY_LIBFILES_TO_BLDLIBDIR() +{ + # copy test files. Used -f to make sure get a new copy + for tstfile in $COPY_LIBFILES + do + # ignore '#' comment + echo $tstfile | tr -d ' ' | grep '^#' > /dev/null + RET=$? + if [ $RET -eq 1 ]; then + # skip cp if srcdir is same as destdir + # this occurs when build/test performed in source dir and + # make cp fail + SDIR=`$DIRNAME $tstfile` + INODE_SDIR=`$LS -i -d $SDIR | $AWK -F' ' '{print $1}'` + INODE_DDIR=`$LS -i -d $TESTDIR | $AWK -F' ' '{print $1}'` + if [ "$INODE_SDIR" != "$INODE_DDIR" ]; then + $CP -f $tstfile $BLDLIBDIR + if [ $? -ne 0 ]; then + echo "Error: FAILED to copy $tstfile ." + + # Comment out this to CREATE expected file + exit $EXIT_FAILURE + fi + fi + fi + done +} + +CLEAN_LIBFILES_AND_BLDLIBDIR() +{ + # skip rm if srcdir is same as destdir + # this occurs when build/test performed in source dir and + # make cp fail + SDIR=`$DIRNAME $tstfile` + INODE_SDIR=`$LS -i -d $SDIR | $AWK -F' ' '{print $1}'` + INODE_DDIR=`$LS -i -d $TESTDIR | $AWK -F' ' '{print $1}'` + if [ "$INODE_SDIR" != "$INODE_DDIR" ]; then + $RM $BLDLIBDIR + fi +} + +COPY_DATAFILES="$LIST_DATA_FILES" + +COPY_DATAFILES_TO_BLDDIR() +{ + # copy test files. Used -f to make sure get a new copy + for tstfile in $COPY_DATAFILES + do + # ignore '#' comment + echo $tstfile | tr -d ' ' | grep '^#' > /dev/null + RET=$? + if [ $RET -eq 1 ]; then + # skip cp if srcdir is same as destdir + # this occurs when build/test performed in source dir and + # make cp fail + SDIR=`$DIRNAME $tstfile` + INODE_SDIR=`$LS -i -d $SDIR | $AWK -F' ' '{print $1}'` + INODE_DDIR=`$LS -i -d $TESTDIR | $AWK -F' ' '{print $1}'` + if [ "$INODE_SDIR" != "$INODE_DDIR" ]; then + $CP -f $tstfile $BLDDIR + if [ $? -ne 0 ]; then + echo "Error: FAILED to copy $tstfile ." + + # Comment out this to CREATE expected file + exit $EXIT_FAILURE + fi + fi + fi + done +} + +CLEAN_DATAFILES_AND_BLDDIR() +{ + # skip rm if srcdir is same as destdir + # this occurs when build/test performed in source dir and + # make cp fail + SDIR=`$DIRNAME $tstfile` + INODE_SDIR=`$LS -i -d $SDIR | $AWK -F' ' '{print $1}'` + INODE_DDIR=`$LS -i -d $TESTDIR | $AWK -F' ' '{print $1}'` + if [ "$INODE_SDIR" != "$INODE_DDIR" ]; then + $RM $BLDDIR/*.txt + $RM $BLDDIR/*.out + fi +} + +# Print a line-line message left justified in a field of 70 characters +# beginning with the word "Testing". +# +TESTING() { + SPACES=" " + echo "Testing $* $SPACES" | cut -c1-70 | tr -d '\012' +} + +# where Java is installed (requires jdk1.7.x) +JAVAEXE=@JAVA@ +JAVAEXEFLAGS=@H5_JAVAFLAGS@ + +############################################################################### +# DO NOT MODIFY BELOW THIS LINE +############################################################################### + +# prepare for test +COPY_LIBFILES_TO_BLDLIBDIR +COPY_DATAFILES_TO_BLDDIR + +CPATH=".:"$BLDLIBDIR"/"$JARFILE":"$BLDLIBDIR"/slf4j-api-1.7.5.jar:"$BLDLIBDIR"/slf4j-simple-1.7.5.jar:"$TESTJARFILE"" + +TEST=/usr/bin/test +if [ ! -x /usr/bin/test ] +then +TEST=`which test` +fi + +if $TEST -z "$CLASSPATH"; then + CLASSPATH="" +fi +CLASSPATH=$CPATH":"$CLASSPATH +export CLASSPATH + +if $TEST -n "$JAVAPATH" ; then + PATH=$JAVAPATH":"$PATH + export PATH +fi + +if $TEST -e /bin/uname; then + os_name=`/bin/uname -s` +elif $TEST -e /usr/bin/uname; then + os_name=`/usr/bin/uname -s` +else + os_name=unknown +fi + +if $TEST -z "$LD_LIBRARY_PATH" ; then + LD_LIBRARY_PATH="" +fi + +case $os_name in + Darwin) + DYLD_LIBRARY_PATH=$BLDLIBDIR:$DYLD_LIBRARY_PATH + export DYLD_LIBRARY_PATH + LD_LIBRARY_PATH=$DYLD_LIBRARY_PATH + ;; + *) + LD_LIBRARY_PATH=$BLDLIBDIR:$LD_LIBRARY_PATH + ;; +esac + +export LD_LIBRARY_PATH + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_Array" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_Array > H5Ex_T_Array.out) +if diff H5Ex_T_Array.out examples.datatypes.H5Ex_T_Array.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_Array" +else + echo "**FAILED** datatypes.H5Ex_T_Array" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_ArrayAttribute" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_ArrayAttribute > H5Ex_T_ArrayAttribute.out) +if diff H5Ex_T_ArrayAttribute.out examples.datatypes.H5Ex_T_ArrayAttribute.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_ArrayAttribute" +else + echo "**FAILED** datatypes.H5Ex_T_ArrayAttribute" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_Bit" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_Bit > H5Ex_T_Bit.out) +if diff H5Ex_T_Bit.out examples.datatypes.H5Ex_T_Bit.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_Bit" +else + echo "**FAILED** datatypes.H5Ex_T_Bit" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_BitAttribute" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_BitAttribute > H5Ex_T_BitAttribute.out) +if diff H5Ex_T_BitAttribute.out examples.datatypes.H5Ex_T_BitAttribute.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_BitAttribute" +else + echo "**FAILED** datatypes.H5Ex_T_BitAttribute" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_Commit" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_Commit > H5Ex_T_Commit.out) +if diff H5Ex_T_Commit.out examples.datatypes.H5Ex_T_Commit.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_Commit" +else + echo "**FAILED** datatypes.H5Ex_T_Commit" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_Compound" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_Compound > H5Ex_T_Compound.out) +if diff H5Ex_T_Compound.out examples.datatypes.H5Ex_T_Compound.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_Compound" +else + echo "**FAILED** datatypes.H5Ex_T_Compound" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_CompoundAttribute" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_CompoundAttribute > H5Ex_T_CompoundAttribute.out) +if diff H5Ex_T_CompoundAttribute.out examples.datatypes.H5Ex_T_CompoundAttribute.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_CompoundAttribute" +else + echo "**FAILED** datatypes.H5Ex_T_CompoundAttribute" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_Float" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_Float > H5Ex_T_Float.out) +if diff H5Ex_T_Float.out examples.datatypes.H5Ex_T_Float.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_Float" +else + echo "**FAILED** datatypes.H5Ex_T_Float" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_FloatAttribute" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_FloatAttribute > H5Ex_T_FloatAttribute.out) +if diff H5Ex_T_FloatAttribute.out examples.datatypes.H5Ex_T_FloatAttribute.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_FloatAttribute" +else + echo "**FAILED** datatypes.H5Ex_T_FloatAttribute" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_Integer" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_Integer > H5Ex_T_Integer.out) +if diff H5Ex_T_Integer.out examples.datatypes.H5Ex_T_Integer.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_Integer" +else + echo "**FAILED** datatypes.H5Ex_T_Integer" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_IntegerAttribute" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_IntegerAttribute > H5Ex_T_IntegerAttribute.out) +if diff H5Ex_T_IntegerAttribute.out examples.datatypes.H5Ex_T_IntegerAttribute.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_IntegerAttribute" +else + echo "**FAILED** datatypes.H5Ex_T_IntegerAttribute" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_ObjectReference" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_ObjectReference > H5Ex_T_ObjectReference.out) +if diff H5Ex_T_ObjectReference.out examples.datatypes.H5Ex_T_ObjectReference.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_ObjectReference" +else + echo "**FAILED** datatypes.H5Ex_T_ObjectReference" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_ObjectReferenceAttribute" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_ObjectReferenceAttribute > H5Ex_T_ObjectReferenceAttribute.out) +if diff H5Ex_T_ObjectReferenceAttribute.out examples.datatypes.H5Ex_T_ObjectReferenceAttribute.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_ObjectReferenceAttribute" +else + echo "**FAILED** datatypes.H5Ex_T_ObjectReferenceAttribute" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_Opaque" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_Opaque > H5Ex_T_Opaque.out) +if diff H5Ex_T_Opaque.out examples.datatypes.H5Ex_T_Opaque.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_Opaque" +else + echo "**FAILED** datatypes.H5Ex_T_Opaque" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_OpaqueAttribute" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_OpaqueAttribute > H5Ex_T_OpaqueAttribute.out) +if diff H5Ex_T_OpaqueAttribute.out examples.datatypes.H5Ex_T_OpaqueAttribute.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_OpaqueAttribute" +else + echo "**FAILED** datatypes.H5Ex_T_OpaqueAttribute" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_String" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_String > H5Ex_T_String.out) +if diff H5Ex_T_String.out examples.datatypes.H5Ex_T_String.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_String" +else + echo "**FAILED** datatypes.H5Ex_T_String" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_StringAttribute" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_StringAttribute > H5Ex_T_StringAttribute.out) +if diff H5Ex_T_StringAttribute.out examples.datatypes.H5Ex_T_StringAttribute.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_StringAttribute" +else + echo "**FAILED** datatypes.H5Ex_T_StringAttribute" + nerrors="`expr $nerrors + 1`" +fi + +echo "$JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_VLString" +($JAVAEXE $JAVAEXEFLAGS -Xmx1024M -Dorg.slf4j.simpleLogger.defaultLog=trace -Djava.library.path=$BLDLIBDIR -cp $CLASSPATH examples.datatypes.H5Ex_T_VLString > H5Ex_T_VLString.out) +if diff H5Ex_T_VLString.out examples.datatypes.H5Ex_T_VLString.txt > /dev/null; then + echo " PASSED datatypes.H5Ex_T_VLString" +else + echo "**FAILED** datatypes.H5Ex_T_VLString" + nerrors="`expr $nerrors + 1`" +fi + +# Clean up temporary files/directories +CLEAN_LIBFILES_AND_BLDLIBDIR +CLEAN_DATAFILES_AND_BLDDIR + +# Report test results and exit +if test $nerrors -eq 0 ; then + echo "All $TESTNAME tests passed." + exit $EXIT_SUCCESS +else + echo "$TESTNAME tests failed with $nerrors errors." + exit $EXIT_FAILURE +fi |