diff options
author | Allen Byrne <50328838+byrnHDF@users.noreply.github.com> | 2023-11-27 21:30:15 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-27 21:30:15 (GMT) |
commit | fc88fcde1091cf12c1e88c783a14ee0f1cffe31c (patch) | |
tree | 91b88b62cd30ed37ee9227e43989e95035be43c3 /HDF5Examples/C/H5T/16 | |
parent | a067bf71f57723d2dfca7dfe2ffd9ea502eccd4f (diff) | |
download | hdf5-fc88fcde1091cf12c1e88c783a14ee0f1cffe31c.zip hdf5-fc88fcde1091cf12c1e88c783a14ee0f1cffe31c.tar.gz hdf5-fc88fcde1091cf12c1e88c783a14ee0f1cffe31c.tar.bz2 |
Develop merge examples (#3851)
* Merge examples repo into library
* Change grepTest to be more fault-tolerant
* Update examples macro file
* Exclude all Fortran examples from doxygen
Diffstat (limited to 'HDF5Examples/C/H5T/16')
28 files changed, 4594 insertions, 0 deletions
diff --git a/HDF5Examples/C/H5T/16/h5ex_t_array.c b/HDF5Examples/C/H5T/16/h5ex_t_array.c new file mode 100644 index 0000000..0537352 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_array.c @@ -0,0 +1,166 @@ +/************************************************************ + + 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_array.h5" +#define DATASET "DS1" +#define DIM0 4 +#define ADIM0 3 +#define ADIM1 5 + +int +main(void) +{ + hid_t file, filetype, memtype, space, dset; + /* Handles */ + herr_t status; + hsize_t dims[1] = {DIM0}, adims[2] = {ADIM0, ADIM1}; + int wdata[DIM0][ADIM0][ADIM1], /* Write buffer */ + ***rdata, /* Read buffer */ + ndims, i, j, k; + + /* + * Initialize data. i is the element in the dataspace, j and k the + * elements within the array datatype. + */ + for (i = 0; i < DIM0; i++) + for (j = 0; j < ADIM0; j++) + for (k = 0; k < ADIM1; k++) + wdata[i][j][k] = i * j - j * k + i * k; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create array datatypes for file and memory. + */ + filetype = H5Tarray_create(H5T_STD_I64LE, 2, adims, NULL); + memtype = H5Tarray_create(H5T_NATIVE_INT, 2, adims, NULL); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the dataset and write the array data to it. + */ + dset = H5Dcreate(file, DATASET, filetype, space, H5P_DEFAULT); + status = H5Dwrite(dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata[0][0]); + + /* + * Close and release resources. + */ + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Tclose(memtype); + status = H5Fclose(file); + + /* + * 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(). + */ + + /* + * Open file and dataset. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + + /* + * Get the datatype and its dimensions. + */ + filetype = H5Dget_type(dset); + ndims = H5Tget_array_dims(filetype, adims, NULL); + + /* + * 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. + */ + space = H5Dget_space(dset); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + + /* + * Allocate array of pointers to two-dimensional arrays (the + * elements of the dataset. + */ + rdata = (int ***)malloc(dims[0] * sizeof(int **)); + + /* + * Allocate two dimensional array of pointers to rows in the data + * elements. + */ + rdata[0] = (int **)malloc(dims[0] * adims[0] * sizeof(int *)); + + /* + * Allocate space for integer data. + */ + rdata[0][0] = (int *)malloc(dims[0] * adims[0] * adims[1] * sizeof(int)); + + /* + * Set the members of the pointer arrays allocated above to point + * to the correct locations in their respective arrays. + */ + for (i = 0; i < dims[0]; i++) { + rdata[i] = rdata[0] + i * adims[0]; + for (j = 0; j < adims[0]; j++) + rdata[i][j] = rdata[0][0] + (adims[0] * adims[1] * i) + (adims[1] * j); + } + + /* + * Create the memory datatype. + */ + memtype = H5Tarray_create(H5T_NATIVE_INT, 2, adims, NULL); + + /* + * Read the data. + */ + status = H5Dread(dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0][0]); + + /* + * Output the data to the screen. + */ + for (i = 0; i < dims[0]; i++) { + printf("%s[%d]:\n", DATASET, i); + for (j = 0; j < adims[0]; j++) { + printf(" ["); + for (k = 0; k < adims[1]; k++) + printf(" %3d", rdata[i][j][k]); + printf("]\n"); + } + printf("\n"); + } + + /* + * Close and release resources. + */ + free(rdata[0][0]); + free(rdata[0]); + free(rdata); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Tclose(memtype); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_arrayatt.c b/HDF5Examples/C/H5T/16/h5ex_t_arrayatt.c new file mode 100644 index 0000000..0750cc2 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_arrayatt.c @@ -0,0 +1,177 @@ +/************************************************************ + + 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_arrayatt.h5" +#define DATASET "DS1" +#define ATTRIBUTE "A1" +#define DIM0 4 +#define ADIM0 3 +#define ADIM1 5 + +int +main(void) +{ + hid_t file, filetype, memtype, space, dset, attr; + /* Handles */ + herr_t status; + hsize_t dims[1] = {DIM0}, adims[2] = {ADIM0, ADIM1}; + int wdata[DIM0][ADIM0][ADIM1], /* Write buffer */ + ***rdata, /* Read buffer */ + ndims, i, j, k; + + /* + * Initialize data. i is the element in the dataspace, j and k the + * elements within the array datatype. + */ + for (i = 0; i < DIM0; i++) + for (j = 0; j < ADIM0; j++) + for (k = 0; k < ADIM1; k++) + wdata[i][j][k] = i * j - j * k + i * k; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create array datatypes for file and memory. + */ + filetype = H5Tarray_create(H5T_STD_I64LE, 2, adims, NULL); + memtype = H5Tarray_create(H5T_NATIVE_INT, 2, adims, NULL); + + /* + * Create dataset with a scalar dataspace. + */ + space = H5Screate(H5S_SCALAR); + dset = H5Dcreate(file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); + status = H5Sclose(space); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the attribute and write the array data to it. + */ + attr = H5Acreate(dset, ATTRIBUTE, filetype, space, H5P_DEFAULT); + status = H5Awrite(attr, memtype, wdata[0][0]); + + /* + * Close and release resources. + */ + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Tclose(memtype); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the attribute 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(). + */ + + /* + * Open file, dataset, and attribute. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + attr = H5Aopen_name(dset, ATTRIBUTE); + + /* + * Get the datatype and its dimensions. + */ + filetype = H5Aget_type(attr); + ndims = H5Tget_array_dims(filetype, adims, NULL); + + /* + * Get dataspace and allocate memory for read buffer. This is a + * three dimensional attribute when the array datatype is included + * so the dynamic allocation must be done in steps. + */ + space = H5Aget_space(attr); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + + /* + * Allocate array of pointers to two-dimensional arrays (the + * elements of the attribute. + */ + rdata = (int ***)malloc(dims[0] * sizeof(int **)); + + /* + * Allocate two dimensional array of pointers to rows in the data + * elements. + */ + rdata[0] = (int **)malloc(dims[0] * adims[0] * sizeof(int *)); + + /* + * Allocate space for integer data. + */ + rdata[0][0] = (int *)malloc(dims[0] * adims[0] * adims[1] * sizeof(int)); + + /* + * Set the members of the pointer arrays allocated above to point + * to the correct locations in their respective arrays. + */ + for (i = 0; i < dims[0]; i++) { + rdata[i] = rdata[0] + i * adims[0]; + for (j = 0; j < adims[0]; j++) + rdata[i][j] = rdata[0][0] + (adims[0] * adims[1] * i) + (adims[1] * j); + } + + /* + * Create the memory datatype. + */ + memtype = H5Tarray_create(H5T_NATIVE_INT, 2, adims, NULL); + + /* + * Read the data. + */ + status = H5Aread(attr, memtype, rdata[0][0]); + + /* + * Output the data to the screen. + */ + for (i = 0; i < dims[0]; i++) { + printf("%s[%d]:\n", ATTRIBUTE, i); + for (j = 0; j < adims[0]; j++) { + printf(" ["); + for (k = 0; k < adims[1]; k++) + printf(" %3d", rdata[i][j][k]); + printf("]\n"); + } + printf("\n"); + } + + /* + * Close and release resources. + */ + free(rdata[0][0]); + free(rdata[0]); + free(rdata); + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Tclose(memtype); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_bit.c b/HDF5Examples/C/H5T/16/h5ex_t_bit.c new file mode 100644 index 0000000..39f0566 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_bit.c @@ -0,0 +1,137 @@ +/************************************************************ + + 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_bit.h5" +#define DATASET "DS1" +#define DIM0 4 +#define DIM1 7 + +int +main(void) +{ + hid_t file, space, dset; /* Handles */ + herr_t status; + hsize_t dims[2] = {DIM0, DIM1}; + unsigned char wdata[DIM0][DIM1], /* Write buffer */ + **rdata; /* Read buffer */ + int ndims, A, B, C, D, i, j; + + /* + * Initialize data. We will manually pack 4 2-bit integers into + * each unsigned char data element. + */ + for (i = 0; i < DIM0; i++) + for (j = 0; j < DIM1; j++) { + wdata[i][j] = 0; + wdata[i][j] |= (i * j - j) & 0x03; /* Field "A" */ + wdata[i][j] |= (i & 0x03) << 2; /* Field "B" */ + wdata[i][j] |= (j & 0x03) << 4; /* Field "C" */ + wdata[i][j] |= ((i + j) & 0x03) << 6; /* Field "D" */ + } + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(2, dims, NULL); + + /* + * Create the dataset and write the bitfield data to it. + */ + dset = H5Dcreate(file, DATASET, H5T_STD_B8BE, space, H5P_DEFAULT); + status = H5Dwrite(dset, H5T_NATIVE_B8, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata[0]); + + /* + * Close and release resources. + */ + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the dataset has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). + */ + + /* + * Open file and dataset. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + + /* + * Get dataspace and allocate memory for read buffer. This is a + * two dimensional dataset so the dynamic allocation must be done + * in steps. + */ + space = H5Dget_space(dset); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + + /* + * Allocate array of pointers to rows. + */ + rdata = (unsigned char **)malloc(dims[0] * sizeof(unsigned char *)); + + /* + * Allocate space for bitfield data. + */ + rdata[0] = (unsigned char *)malloc(dims[0] * dims[1] * sizeof(unsigned char)); + + /* + * Set the rest of the pointers to rows to the correct addresses. + */ + for (i = 1; i < dims[0]; i++) + rdata[i] = rdata[0] + i * dims[1]; + + /* + * Read the data. + */ + status = H5Dread(dset, H5T_NATIVE_B8, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0]); + + /* + * Output the data to the screen. + */ + printf("%s:\n", DATASET); + for (i = 0; i < dims[0]; i++) { + printf(" ["); + for (j = 0; j < dims[1]; j++) { + A = rdata[i][j] & 0x03; /* Retrieve field "A" */ + B = (rdata[i][j] >> 2) & 0x03; /* Retrieve field "B" */ + C = (rdata[i][j] >> 4) & 0x03; /* Retrieve field "C" */ + D = (rdata[i][j] >> 6) & 0x03; /* Retrieve field "D" */ + printf(" {%d, %d, %d, %d}", A, B, C, D); + } + printf(" ]\n"); + } + + /* + * Close and release resources. + */ + free(rdata[0]); + free(rdata); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_bitatt.c b/HDF5Examples/C/H5T/16/h5ex_t_bitatt.c new file mode 100644 index 0000000..be19a28 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_bitatt.c @@ -0,0 +1,148 @@ +/************************************************************ + + 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_bitatt.h5" +#define DATASET "DS1" +#define ATTRIBUTE "A1" +#define DIM0 4 +#define DIM1 7 + +int +main(void) +{ + hid_t file, space, dset, attr; /* Handles */ + herr_t status; + hsize_t dims[2] = {DIM0, DIM1}; + unsigned char wdata[DIM0][DIM1], /* Write buffer */ + **rdata; /* Read buffer */ + int ndims, A, B, C, D, i, j; + + /* + * Initialize data. We will manually pack 4 2-bit integers into + * each unsigned char data element. + */ + for (i = 0; i < DIM0; i++) + for (j = 0; j < DIM1; j++) { + wdata[i][j] = 0; + wdata[i][j] |= (i * j - j) & 0x03; /* Field "A" */ + wdata[i][j] |= (i & 0x03) << 2; /* Field "B" */ + wdata[i][j] |= (j & 0x03) << 4; /* Field "C" */ + wdata[i][j] |= ((i + j) & 0x03) << 6; /* Field "D" */ + } + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create dataset with a scalar dataspace. + */ + space = H5Screate(H5S_SCALAR); + dset = H5Dcreate(file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); + status = H5Sclose(space); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(2, dims, NULL); + + /* + * Create the attribute and write the bitfield data to it. + */ + attr = H5Acreate(dset, ATTRIBUTE, H5T_STD_B8BE, space, H5P_DEFAULT); + status = H5Awrite(attr, H5T_NATIVE_B8, wdata[0]); + + /* + * Close and release resources. + */ + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the attribute has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). + */ + + /* + * Open file, dataset, and attribute. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + attr = H5Aopen_name(dset, ATTRIBUTE); + + /* + * Get dataspace and allocate memory for read buffer. This is a + * two dimensional attribute so the dynamic allocation must be done + * in steps. + */ + space = H5Aget_space(attr); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + + /* + * Allocate array of pointers to rows. + */ + rdata = (unsigned char **)malloc(dims[0] * sizeof(unsigned char *)); + + /* + * Allocate space for bitfield data. + */ + rdata[0] = (unsigned char *)malloc(dims[0] * dims[1] * sizeof(unsigned char)); + + /* + * Set the rest of the pointers to rows to the correct addresses. + */ + for (i = 1; i < dims[0]; i++) + rdata[i] = rdata[0] + i * dims[1]; + + /* + * Read the data. + */ + status = H5Aread(attr, H5T_NATIVE_B8, rdata[0]); + + /* + * Output the data to the screen. + */ + printf("%s:\n", ATTRIBUTE); + for (i = 0; i < dims[0]; i++) { + printf(" ["); + for (j = 0; j < dims[1]; j++) { + A = rdata[i][j] & 0x03; /* Retrieve field "A" */ + B = (rdata[i][j] >> 2) & 0x03; /* Retrieve field "B" */ + C = (rdata[i][j] >> 4) & 0x03; /* Retrieve field "C" */ + D = (rdata[i][j] >> 6) & 0x03; /* Retrieve field "D" */ + printf(" {%d, %d, %d, %d}", A, B, C, D); + } + printf(" ]\n"); + } + + /* + * Close and release resources. + */ + free(rdata[0]); + free(rdata); + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_cmpd.c b/HDF5Examples/C/H5T/16/h5ex_t_cmpd.c new file mode 100644 index 0000000..4870857 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_cmpd.c @@ -0,0 +1,161 @@ +/************************************************************ + + 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_cmpd.h5" +#define DATASET "DS1" +#define DIM0 4 + +typedef struct { + int serial_no; + char *location; + double temperature; + double pressure; +} sensor_t; /* Compound type */ + +int +main(void) +{ + hid_t file, filetype, memtype, strtype, space, dset; + /* Handles */ + herr_t status; + hsize_t dims[1] = {DIM0}; + sensor_t wdata[DIM0], /* Write buffer */ + *rdata; /* Read buffer */ + int ndims, i; + + /* + * Initialize data. + */ + wdata[0].serial_no = 1153; + wdata[0].location = "Exterior (static)"; + wdata[0].temperature = 53.23; + wdata[0].pressure = 24.57; + wdata[1].serial_no = 1184; + wdata[1].location = "Intake"; + wdata[1].temperature = 55.12; + wdata[1].pressure = 22.95; + wdata[2].serial_no = 1027; + wdata[2].location = "Intake manifold"; + wdata[2].temperature = 103.55; + wdata[2].pressure = 31.23; + wdata[3].serial_no = 1313; + wdata[3].location = "Exhaust manifold"; + wdata[3].temperature = 1252.89; + wdata[3].pressure = 84.11; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create variable-length string datatype. + */ + strtype = H5Tcopy(H5T_C_S1); + status = H5Tset_size(strtype, H5T_VARIABLE); + + /* + * Create the compound datatype for memory. + */ + memtype = H5Tcreate(H5T_COMPOUND, sizeof(sensor_t)); + status = H5Tinsert(memtype, "Serial number", HOFFSET(sensor_t, serial_no), H5T_NATIVE_INT); + status = H5Tinsert(memtype, "Location", HOFFSET(sensor_t, location), strtype); + status = H5Tinsert(memtype, "Temperature (F)", HOFFSET(sensor_t, temperature), H5T_NATIVE_DOUBLE); + status = H5Tinsert(memtype, "Pressure (inHg)", HOFFSET(sensor_t, pressure), H5T_NATIVE_DOUBLE); + + /* + * 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. + */ + filetype = H5Tcreate(H5T_COMPOUND, 8 + sizeof(hvl_t) + 8 + 8); + status = H5Tinsert(filetype, "Serial number", 0, H5T_STD_I64BE); + status = H5Tinsert(filetype, "Location", 8, strtype); + status = H5Tinsert(filetype, "Temperature (F)", 8 + sizeof(hvl_t), H5T_IEEE_F64BE); + status = H5Tinsert(filetype, "Pressure (inHg)", 8 + sizeof(hvl_t) + 8, H5T_IEEE_F64BE); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the dataset and write the compound data to it. + */ + dset = H5Dcreate(file, DATASET, filetype, space, H5P_DEFAULT); + status = H5Dwrite(dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata); + + /* + * Close and release resources. + */ + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the dataset has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). For simplicity, we do not rebuild memtype. + */ + + /* + * Open file and dataset. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + + /* + * Get dataspace and allocate memory for read buffer. + */ + space = H5Dget_space(dset); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + rdata = (sensor_t *)malloc(dims[0] * sizeof(sensor_t)); + + /* + * Read the data. + */ + status = H5Dread(dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata); + + /* + * Output the data to the screen. + */ + for (i = 0; i < dims[0]; i++) { + printf("%s[%d]:\n", DATASET, i); + printf("Serial number : %d\n", rdata[i].serial_no); + printf("Location : %s\n", rdata[i].location); + printf("Temperature (F) : %f\n", rdata[i].temperature); + printf("Pressure (inHg) : %f\n\n", rdata[i].pressure); + } + + /* + * Close and release resources. H5Dvlen_reclaim will automatically + * traverse the structure and free any vlen data (strings in this + * case). + */ + status = H5Dvlen_reclaim(memtype, space, H5P_DEFAULT, rdata); + free(rdata); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(memtype); + status = H5Tclose(strtype); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_cmpdatt.c b/HDF5Examples/C/H5T/16/h5ex_t_cmpdatt.c new file mode 100644 index 0000000..c9f9b7e --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_cmpdatt.c @@ -0,0 +1,172 @@ +/************************************************************ + + 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_cmpdatt.h5" +#define DATASET "DS1" +#define ATTRIBUTE "A1" +#define DIM0 4 + +typedef struct { + int serial_no; + char *location; + double temperature; + double pressure; +} sensor_t; /* Compound type */ + +int +main(void) +{ + hid_t file, filetype, memtype, strtype, space, dset, attr; + /* Handles */ + herr_t status; + hsize_t dims[1] = {DIM0}; + sensor_t wdata[DIM0], /* Write buffer */ + *rdata; /* Read buffer */ + int ndims, i; + + /* + * Initialize data. + */ + wdata[0].serial_no = 1153; + wdata[0].location = "Exterior (static)"; + wdata[0].temperature = 53.23; + wdata[0].pressure = 24.57; + wdata[1].serial_no = 1184; + wdata[1].location = "Intake"; + wdata[1].temperature = 55.12; + wdata[1].pressure = 22.95; + wdata[2].serial_no = 1027; + wdata[2].location = "Intake manifold"; + wdata[2].temperature = 103.55; + wdata[2].pressure = 31.23; + wdata[3].serial_no = 1313; + wdata[3].location = "Exhaust manifold"; + wdata[3].temperature = 1252.89; + wdata[3].pressure = 84.11; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create variable-length string datatype. + */ + strtype = H5Tcopy(H5T_C_S1); + status = H5Tset_size(strtype, H5T_VARIABLE); + + /* + * Create the compound datatype for memory. + */ + memtype = H5Tcreate(H5T_COMPOUND, sizeof(sensor_t)); + status = H5Tinsert(memtype, "Serial number", HOFFSET(sensor_t, serial_no), H5T_NATIVE_INT); + status = H5Tinsert(memtype, "Location", HOFFSET(sensor_t, location), strtype); + status = H5Tinsert(memtype, "Temperature (F)", HOFFSET(sensor_t, temperature), H5T_NATIVE_DOUBLE); + status = H5Tinsert(memtype, "Pressure (inHg)", HOFFSET(sensor_t, pressure), H5T_NATIVE_DOUBLE); + + /* + * 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. + */ + filetype = H5Tcreate(H5T_COMPOUND, 8 + sizeof(hvl_t) + 8 + 8); + status = H5Tinsert(filetype, "Serial number", 0, H5T_STD_I64BE); + status = H5Tinsert(filetype, "Location", 8, strtype); + status = H5Tinsert(filetype, "Temperature (F)", 8 + sizeof(hvl_t), H5T_IEEE_F64BE); + status = H5Tinsert(filetype, "Pressure (inHg)", 8 + sizeof(hvl_t) + 8, H5T_IEEE_F64BE); + + /* + * Create dataset with a scalar dataspace. + */ + space = H5Screate(H5S_SCALAR); + dset = H5Dcreate(file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); + status = H5Sclose(space); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the attribute and write the compound data to it. + */ + attr = H5Acreate(dset, ATTRIBUTE, filetype, space, H5P_DEFAULT); + status = H5Awrite(attr, memtype, wdata); + + /* + * Close and release resources. + */ + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the attribute has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). For simplicity, we do not rebuild memtype. + */ + + /* + * Open file, dataset, and attribute. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + attr = H5Aopen_name(dset, ATTRIBUTE); + + /* + * Get dataspace and allocate memory for read buffer. + */ + space = H5Aget_space(attr); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + rdata = (sensor_t *)malloc(dims[0] * sizeof(sensor_t)); + + /* + * Read the data. + */ + status = H5Aread(attr, memtype, rdata); + + /* + * Output the data to the screen. + */ + for (i = 0; i < dims[0]; i++) { + printf("%s[%d]:\n", ATTRIBUTE, i); + printf("Serial number : %d\n", rdata[i].serial_no); + printf("Location : %s\n", rdata[i].location); + printf("Temperature (F) : %f\n", rdata[i].temperature); + printf("Pressure (inHg) : %f\n\n", rdata[i].pressure); + } + + /* + * Close and release resources. H5Dvlen_reclaim will automatically + * traverse the structure and free any vlen data (strings in this + * case). + */ + status = H5Dvlen_reclaim(memtype, space, H5P_DEFAULT, rdata); + free(rdata); + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(memtype); + status = H5Tclose(strtype); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_commit.c b/HDF5Examples/C/H5T/16/h5ex_t_commit.c new file mode 100644 index 0000000..eab8793 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_commit.c @@ -0,0 +1,114 @@ +/************************************************************ + + 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_commit.h5" +#define DATATYPE "Sensor_Type" + +int +main(void) +{ + hid_t file, filetype, strtype; + /* Handles */ + herr_t status; + H5T_class_t typeclass; + char *name; + int nmembs, i; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create variable-length string datatype. + */ + strtype = H5Tcopy(H5T_C_S1); + status = H5Tset_size(strtype, H5T_VARIABLE); + + /* + * Create the compound datatype. Because the standard types we are + * using may have different sizes than the corresponding native + * types, we must manually calculate the offset of each member. + */ + filetype = H5Tcreate(H5T_COMPOUND, 8 + sizeof(char *) + 8 + 8); + status = H5Tinsert(filetype, "Serial number", 0, H5T_STD_I64BE); + status = H5Tinsert(filetype, "Location", 8, strtype); + status = H5Tinsert(filetype, "Temperature (F)", 8 + sizeof(char *), H5T_IEEE_F64BE); + status = H5Tinsert(filetype, "Pressure (inHg)", 8 + sizeof(char *) + 8, H5T_IEEE_F64BE); + + /* + * Commit the compound datatype to the file, creating a named + * datatype. + */ + status = H5Tcommit(file, DATATYPE, filetype); + + /* + * Close and release resources. + */ + status = H5Tclose(filetype); + status = H5Tclose(strtype); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. + */ + + /* + * Open file. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + + /* + * Open the named datatype. + */ + filetype = H5Topen(file, DATATYPE); + + /* + * Output the data to the screen. + */ + printf("Named datatype: %s:\n", DATATYPE); + /* + * Get datatype class. If it isn't compound, we won't print + * anything. + */ + typeclass = H5Tget_class(filetype); + if (typeclass == H5T_COMPOUND) { + printf(" Class: H5T_COMPOUND\n"); + nmembs = H5Tget_nmembers(filetype); + /* + * Iterate over compound datatype members. + */ + for (i = 0; i < nmembs; i++) { + /* + * Get the member name and print it. Note that + * H5Tget_member_name allocates space for the string in + * name, so we must free() it after use. + */ + name = H5Tget_member_name(filetype, i); + printf(" %s\n", name); + free(name); + } + } + + /* + * Close and release resources. + */ + status = H5Tclose(filetype); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_convert.c b/HDF5Examples/C/H5T/16/h5ex_t_convert.c new file mode 100644 index 0000000..78aaadb --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_convert.c @@ -0,0 +1,145 @@ +/************************************************************ + + This example shows how to convert between different + datatypes in memory. The program converts DIM0 elements + of compound type sourcetype to desttype, then outputs the + converted data to the screen. A background buffer is used + to fill in the elements of desttype that are not in + sourcetype. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define DIM0 4 + +typedef struct { + double temperature; + double pressure; +} reading_t; /* Source type */ + +typedef struct { + int serial_no; + char *location; + double temperature; + double pressure; +} sensor_t; /* Destination type */ + +int +main(void) +{ + hid_t sourcetype, desttype, strtype, space; + /* Handles */ + herr_t status; + hsize_t dims[1] = {DIM0}; + reading_t *reading; /* Conversion buffer */ + sensor_t *sensor, /* Conversion buffer */ + bkgrd[DIM0]; /* Background buffer */ + int i; + + /* + * Allocate memory for conversion buffer. We will allocate space + * for it to hold DIM0 elements of the destination type, as the + * type conversion is performed in place. Of course, if the + * destination type were smaller than the source type, we would + * allocate space to hold DIM0 elements of the source type. + */ + reading = (reading_t *)malloc(DIM0 * sizeof(sensor_t)); + + /* + * Assign the allocated space to a pointer of the destination type, + * to allow the buffer to be accessed correctly after the + * conversion has taken place. + */ + sensor = (sensor_t *)reading; + + /* + * Initialize data. + */ + bkgrd[0].serial_no = 1153; + bkgrd[0].location = "Exterior (static)"; + bkgrd[0].temperature = 53.23; + bkgrd[0].pressure = 24.57; + bkgrd[1].serial_no = 1184; + bkgrd[1].location = "Intake"; + bkgrd[1].temperature = 55.12; + bkgrd[1].pressure = 22.95; + bkgrd[2].serial_no = 1027; + bkgrd[2].location = "Intake manifold"; + bkgrd[2].temperature = 103.55; + bkgrd[2].pressure = 31.23; + bkgrd[3].serial_no = 1313; + bkgrd[3].location = "Exhaust manifold"; + bkgrd[3].temperature = 1252.89; + bkgrd[3].pressure = 84.11; + + reading[0].temperature = 54.84; + reading[0].pressure = 24.76; + reading[1].temperature = 56.63; + reading[1].pressure = 23.10; + reading[2].temperature = 102.69; + reading[2].pressure = 30.97; + reading[3].temperature = 1238.27; + reading[3].pressure = 82.15; + + /* + * Create variable-length string datatype. + */ + strtype = H5Tcopy(H5T_C_S1); + status = H5Tset_size(strtype, H5T_VARIABLE); + + /* + * Create the compound datatype for memory. + */ + sourcetype = H5Tcreate(H5T_COMPOUND, sizeof(reading_t)); + status = H5Tinsert(sourcetype, "Temperature (F)", HOFFSET(reading_t, temperature), H5T_NATIVE_DOUBLE); + status = H5Tinsert(sourcetype, "Pressure (inHg)", HOFFSET(reading_t, pressure), H5T_NATIVE_DOUBLE); + + desttype = H5Tcreate(H5T_COMPOUND, sizeof(sensor_t)); + status = H5Tinsert(desttype, "Serial number", HOFFSET(sensor_t, serial_no), H5T_NATIVE_INT); + status = H5Tinsert(desttype, "Location", HOFFSET(sensor_t, location), strtype); + status = H5Tinsert(desttype, "Temperature (F)", HOFFSET(sensor_t, temperature), H5T_NATIVE_DOUBLE); + status = H5Tinsert(desttype, "Pressure (inHg)", HOFFSET(sensor_t, pressure), H5T_NATIVE_DOUBLE); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Convert the buffer in reading from sourcetype to desttype. + * After this conversion we will use sensor to access the buffer, + * as the buffer now matches its type. + */ + status = H5Tconvert(sourcetype, desttype, DIM0, reading, bkgrd, H5P_DEFAULT); + + /* + * Output the data to the screen. + */ + for (i = 0; i < DIM0; i++) { + printf("sensor[%d]:\n", i); + printf("Serial number : %d\n", sensor[i].serial_no); + printf("Location : %s\n", sensor[i].location); + printf("Temperature (F) : %f\n", sensor[i].temperature); + printf("Pressure (inHg) : %f\n\n", sensor[i].pressure); + } + + /* + * Close and release resources. In this case H5Tconvert preserves + * the memory locations of the variable-length strings in + * "location", so we do not need to free those strings as they were + * initialized as string constants. + */ + free(sensor); + status = H5Sclose(space); + status = H5Tclose(sourcetype); + status = H5Tclose(desttype); + status = H5Tclose(strtype); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_cpxcmpd.c b/HDF5Examples/C/H5T/16/h5ex_t_cpxcmpd.c new file mode 100644 index 0000000..d2fabe9 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_cpxcmpd.c @@ -0,0 +1,319 @@ +/************************************************************ + + This example shows how to read and write a complex + compound datatype to a dataset. The program first writes + complex compound structures to a dataset with a dataspace + of DIM0, then closes the file. Next, it reopens the file, + reads back selected fields in the structure, and outputs + them to the screen. + + Unlike the other datatype examples, in this example we + save to the file using native datatypes to simplify the + type definitions here. To save using standard types you + must manually calculate the sizes and offsets of compound + types as shown in h5ex_t_cmpd.c, and convert enumerated + values as shown in h5ex_t_enum.c. + + The datatype defined here consists of a compound + containing a variable-length list of compound types, as + well as a variable-length string, enumeration, double + array, object reference and region reference. The nested + compound type contains an int, variable-length string and + two doubles. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_cpxcmpd.h5" +#define DATASET "DS1" +#define DIM0 2 + +typedef struct { + int serial_no; + char *location; + double temperature; + double pressure; +} sensor_t; /* Nested compound type */ + +typedef enum { RED, GREEN, BLUE } color_t; /* Enumerated type */ + +typedef struct { + hvl_t sensors; + char *name; + color_t color; + double location[3]; + hobj_ref_t group; + hdset_reg_ref_t surveyed_areas; +} vehicle_t; /* Main compound type */ + +typedef struct { + hvl_t sensors; + char *name; +} rvehicle_t; /* Read type */ + +int +main(void) +{ + hid_t file, vehicletype, colortype, sensortype, sensorstype, loctype, strtype, rvehicletype, rsensortype, + rsensorstype, space, dset, group; + /* Handles */ + herr_t status; + hsize_t dims[1] = {DIM0}, adims[1] = {3}, adims2[2] = {32, 32}, start[2] = {8, 26}, count[2] = {4, 3}, + coords[3][2] = {{3, 2}, {3, 3}, {4, 4}}; + vehicle_t wdata[2]; /* Write buffer */ + rvehicle_t *rdata; /* Read buffer */ + color_t val; + sensor_t *ptr; + double wdata2[32][32]; + int ndims, i, j; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create dataset to use for region references. + */ + for (i = 0; i < 32; i++) + for (j = 0; j < 32; j++) + wdata2[i][j] = 70. + 0.1 * (i - 16.) + 0.1 * (j - 16.); + space = H5Screate_simple(2, adims2, NULL); + dset = H5Dcreate(file, "Ambient_Temperature", H5T_NATIVE_DOUBLE, space, H5P_DEFAULT); + status = H5Dwrite(dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata2[0]); + status = H5Dclose(dset); + + /* + * Create groups to use for object references. + */ + group = H5Gcreate(file, "Land_Vehicles", H5P_DEFAULT); + status = H5Gclose(group); + group = H5Gcreate(file, "Air_Vehicles", H5P_DEFAULT); + status = H5Gclose(group); + + /* + * Initialize variable-length compound in the first data element. + */ + wdata[0].sensors.len = 4; + ptr = (sensor_t *)malloc(wdata[0].sensors.len * sizeof(sensor_t)); + ptr[0].serial_no = 1153; + ptr[0].location = "Exterior (static)"; + ptr[0].temperature = 53.23; + ptr[0].pressure = 24.57; + ptr[1].serial_no = 1184; + ptr[1].location = "Intake"; + ptr[1].temperature = 55.12; + ptr[1].pressure = 22.95; + ptr[2].serial_no = 1027; + ptr[2].location = "Intake manifold"; + ptr[2].temperature = 103.55; + ptr[2].pressure = 31.23; + ptr[3].serial_no = 1313; + ptr[3].location = "Exhaust manifold"; + ptr[3].temperature = 1252.89; + ptr[3].pressure = 84.11; + wdata[0].sensors.p = (void *)ptr; + + /* + * Initialize other fields in the first data element. + */ + wdata[0].name = "Airplane"; + wdata[0].color = GREEN; + wdata[0].location[0] = -103234.21; + wdata[0].location[1] = 422638.78; + wdata[0].location[2] = 5996.43; + status = H5Rcreate(&wdata[0].group, file, "Air_Vehicles", H5R_OBJECT, -1); + status = H5Sselect_elements(space, H5S_SELECT_SET, 3, coords[0]); + status = H5Rcreate(&wdata[0].surveyed_areas, file, "Ambient_Temperature", H5R_DATASET_REGION, space); + + /* + * Initialize variable-length compound in the second data element. + */ + wdata[1].sensors.len = 1; + ptr = (sensor_t *)malloc(wdata[1].sensors.len * sizeof(sensor_t)); + ptr[0].serial_no = 3244; + ptr[0].location = "Roof"; + ptr[0].temperature = 83.82; + ptr[0].pressure = 29.92; + wdata[1].sensors.p = (void *)ptr; + + /* + * Initialize other fields in the second data element. + */ + wdata[1].name = "Automobile"; + wdata[1].color = RED; + wdata[1].location[0] = 326734.36; + wdata[1].location[1] = 221568.23; + wdata[1].location[2] = 432.36; + status = H5Rcreate(&wdata[1].group, file, "Land_Vehicles", H5R_OBJECT, -1); + status = H5Sselect_hyperslab(space, H5S_SELECT_SET, start, NULL, count, NULL); + status = H5Rcreate(&wdata[1].surveyed_areas, file, "Ambient_Temperature", H5R_DATASET_REGION, space); + + status = H5Sclose(space); + + /* + * Create variable-length string datatype. + */ + strtype = H5Tcopy(H5T_C_S1); + status = H5Tset_size(strtype, H5T_VARIABLE); + + /* + * Create the nested compound datatype. + */ + sensortype = H5Tcreate(H5T_COMPOUND, sizeof(sensor_t)); + status = H5Tinsert(sensortype, "Serial number", HOFFSET(sensor_t, serial_no), H5T_NATIVE_INT); + status = H5Tinsert(sensortype, "Location", HOFFSET(sensor_t, location), strtype); + status = H5Tinsert(sensortype, "Temperature (F)", HOFFSET(sensor_t, temperature), H5T_NATIVE_DOUBLE); + status = H5Tinsert(sensortype, "Pressure (inHg)", HOFFSET(sensor_t, pressure), H5T_NATIVE_DOUBLE); + + /* + * Create the variable-length datatype. + */ + sensorstype = H5Tvlen_create(sensortype); + + /* + * Create the enumerated datatype. + */ + colortype = H5Tenum_create(H5T_NATIVE_INT); + val = (color_t)RED; + status = H5Tenum_insert(colortype, "Red", &val); + val = (color_t)GREEN; + status = H5Tenum_insert(colortype, "Green", &val); + val = (color_t)BLUE; + status = H5Tenum_insert(colortype, "Blue", &val); + + /* + * Create the array datatype. + */ + loctype = H5Tarray_create(H5T_NATIVE_DOUBLE, 1, adims, NULL); + + /* + * Create the main compound datatype. + */ + vehicletype = H5Tcreate(H5T_COMPOUND, sizeof(vehicle_t)); + status = H5Tinsert(vehicletype, "Sensors", HOFFSET(vehicle_t, sensors), sensorstype); + status = H5Tinsert(vehicletype, "Name", HOFFSET(vehicle_t, name), strtype); + status = H5Tinsert(vehicletype, "Color", HOFFSET(vehicle_t, color), colortype); + status = H5Tinsert(vehicletype, "Location", HOFFSET(vehicle_t, location), loctype); + status = H5Tinsert(vehicletype, "Group", HOFFSET(vehicle_t, group), H5T_STD_REF_OBJ); + status = + H5Tinsert(vehicletype, "Surveyed areas", HOFFSET(vehicle_t, surveyed_areas), H5T_STD_REF_DSETREG); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the dataset and write the compound data to it. + */ + dset = H5Dcreate(file, DATASET, vehicletype, space, H5P_DEFAULT); + status = H5Dwrite(dset, vehicletype, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata); + + /* + * Close and release resources. Note that we cannot use + * H5Dvlen_reclaim as it would attempt to free() the string + * constants used to initialize the name fields in wdata. We must + * therefore manually free() only the data previously allocated + * through malloc(). + */ + for (i = 0; i < dims[0]; i++) + free(wdata[i].sensors.p); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(strtype); + status = H5Tclose(sensortype); + status = H5Tclose(sensorstype); + status = H5Tclose(colortype); + status = H5Tclose(loctype); + status = H5Tclose(vehicletype); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the dataset has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). We will only read back the variable length strings. + */ + + /* + * Open file and dataset. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + + /* + * Create variable-length string datatype. + */ + strtype = H5Tcopy(H5T_C_S1); + status = H5Tset_size(strtype, H5T_VARIABLE); + + /* + * Create the nested compound datatype for reading. Even though it + * has only one field, it must still be defined as a compound type + * so the library can match the correct field in the file type. + * This matching is done by name. However, we do not need to + * define a structure for the read buffer as we can simply treat it + * as a char *. + */ + rsensortype = H5Tcreate(H5T_COMPOUND, sizeof(char *)); + status = H5Tinsert(rsensortype, "Location", 0, strtype); + + /* + * Create the variable-length datatype for reading. + */ + rsensorstype = H5Tvlen_create(rsensortype); + + /* + * Create the main compound datatype for reading. + */ + rvehicletype = H5Tcreate(H5T_COMPOUND, sizeof(rvehicle_t)); + status = H5Tinsert(rvehicletype, "Sensors", HOFFSET(rvehicle_t, sensors), rsensorstype); + status = H5Tinsert(rvehicletype, "Name", HOFFSET(rvehicle_t, name), strtype); + + /* + * Get dataspace and allocate memory for read buffer. + */ + space = H5Dget_space(dset); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + rdata = (rvehicle_t *)malloc(dims[0] * sizeof(rvehicle_t)); + + /* + * Read the data. + */ + status = H5Dread(dset, rvehicletype, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata); + + /* + * Output the data to the screen. + */ + for (i = 0; i < dims[0]; i++) { + printf("%s[%d]:\n", DATASET, i); + printf(" Vehicle name :\n %s\n", rdata[i].name); + printf(" Sensor locations :\n"); + for (j = 0; j < rdata[i].sensors.len; j++) + printf(" %s\n", ((char **)rdata[i].sensors.p)[j]); + } + + /* + * Close and release resources. H5Dvlen_reclaim will automatically + * traverse the structure and free any vlen data (including + * strings). + */ + status = H5Dvlen_reclaim(rvehicletype, space, H5P_DEFAULT, rdata); + free(rdata); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(strtype); + status = H5Tclose(rsensortype); + status = H5Tclose(rsensorstype); + status = H5Tclose(rvehicletype); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_cpxcmpdatt.c b/HDF5Examples/C/H5T/16/h5ex_t_cpxcmpdatt.c new file mode 100644 index 0000000..5932e1e --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_cpxcmpdatt.c @@ -0,0 +1,331 @@ +/************************************************************ + + This example shows how to read and write a complex + compound datatype to an attribute. The program first + writes complex compound structures to an attribute with a + dataspace of DIM0, then closes the file. Next, it reopens + the file, reads back selected fields in the structure, and + outputs them to the screen. + + Unlike the other datatype examples, in this example we + save to the file using native datatypes to simplify the + type definitions here. To save using standard types you + must manually calculate the sizes and offsets of compound + types as shown in h5ex_t_cmpd.c, and convert enumerated + values as shown in h5ex_t_enum.c. + + The datatype defined here consists of a compound + containing a variable-length list of compound types, as + well as a variable-length string, enumeration, double + array, object reference and region reference. The nested + compound type contains an int, variable-length string and + two doubles. + + This file is intended for use with HDF5 Library version 1.8 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_cpxcmpdatt.h5" +#define DATASET "DS1" +#define ATTRIBUTE "A1" +#define DIM0 2 + +typedef struct { + int serial_no; + char *location; + double temperature; + double pressure; +} sensor_t; /* Nested compound type */ + +typedef enum { RED, GREEN, BLUE } color_t; /* Enumerated type */ + +typedef struct { + hvl_t sensors; + char *name; + color_t color; + double location[3]; + hobj_ref_t group; + hdset_reg_ref_t surveyed_areas; +} vehicle_t; /* Main compound type */ + +typedef struct { + hvl_t sensors; + char *name; +} rvehicle_t; /* Read type */ + +int +main(void) +{ + hid_t file, vehicletype, colortype, sensortype, sensorstype, loctype, strtype, rvehicletype, rsensortype, + rsensorstype, space, dset, group, attr; + /* Handles */ + herr_t status; + hsize_t dims[1] = {DIM0}, adims[1] = {3}, adims2[2] = {32, 32}, start[2] = {8, 26}, count[2] = {4, 3}, + coords[3][2] = {{3, 2}, {3, 3}, {4, 4}}; + vehicle_t wdata[2]; /* Write buffer */ + rvehicle_t *rdata; /* Read buffer */ + color_t val; + sensor_t *ptr; + double wdata2[32][32]; + int ndims, i, j; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create dataset to use for region references. + */ + for (i = 0; i < 32; i++) + for (j = 0; j < 32; j++) + wdata2[i][j] = 70. + 0.1 * (i - 16.) + 0.1 * (j - 16.); + space = H5Screate_simple(2, adims2, NULL); + dset = H5Dcreate(file, "Ambient_Temperature", H5T_NATIVE_DOUBLE, space, H5P_DEFAULT); + status = H5Dwrite(dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata2[0]); + status = H5Dclose(dset); + + /* + * Create groups to use for object references. + */ + group = H5Gcreate(file, "Land_Vehicles", H5P_DEFAULT); + status = H5Gclose(group); + group = H5Gcreate(file, "Air_Vehicles", H5P_DEFAULT); + status = H5Gclose(group); + + /* + * Initialize variable-length compound in the first data element. + */ + wdata[0].sensors.len = 4; + ptr = (sensor_t *)malloc(wdata[0].sensors.len * sizeof(sensor_t)); + ptr[0].serial_no = 1153; + ptr[0].location = "Exterior (static)"; + ptr[0].temperature = 53.23; + ptr[0].pressure = 24.57; + ptr[1].serial_no = 1184; + ptr[1].location = "Intake"; + ptr[1].temperature = 55.12; + ptr[1].pressure = 22.95; + ptr[2].serial_no = 1027; + ptr[2].location = "Intake manifold"; + ptr[2].temperature = 103.55; + ptr[2].pressure = 31.23; + ptr[3].serial_no = 1313; + ptr[3].location = "Exhaust manifold"; + ptr[3].temperature = 1252.89; + ptr[3].pressure = 84.11; + wdata[0].sensors.p = (void *)ptr; + + /* + * Initialize other fields in the first data element. + */ + wdata[0].name = "Airplane"; + wdata[0].color = GREEN; + wdata[0].location[0] = -103234.21; + wdata[0].location[1] = 422638.78; + wdata[0].location[2] = 5996.43; + status = H5Rcreate(&wdata[0].group, file, "Air_Vehicles", H5R_OBJECT, -1); + status = H5Sselect_elements(space, H5S_SELECT_SET, 3, coords[0]); + status = H5Rcreate(&wdata[0].surveyed_areas, file, "Ambient_Temperature", H5R_DATASET_REGION, space); + + /* + * Initialize variable-length compound in the second data element. + */ + wdata[1].sensors.len = 1; + ptr = (sensor_t *)malloc(wdata[1].sensors.len * sizeof(sensor_t)); + ptr[0].serial_no = 3244; + ptr[0].location = "Roof"; + ptr[0].temperature = 83.82; + ptr[0].pressure = 29.92; + wdata[1].sensors.p = (void *)ptr; + + /* + * Initialize other fields in the second data element. + */ + wdata[1].name = "Automobile"; + wdata[1].color = RED; + wdata[1].location[0] = 326734.36; + wdata[1].location[1] = 221568.23; + wdata[1].location[2] = 432.36; + status = H5Rcreate(&wdata[1].group, file, "Land_Vehicles", H5R_OBJECT, -1); + status = H5Sselect_hyperslab(space, H5S_SELECT_SET, start, NULL, count, NULL); + status = H5Rcreate(&wdata[1].surveyed_areas, file, "Ambient_Temperature", H5R_DATASET_REGION, space); + + status = H5Sclose(space); + + /* + * Create variable-length string datatype. + */ + strtype = H5Tcopy(H5T_C_S1); + status = H5Tset_size(strtype, H5T_VARIABLE); + + /* + * Create the nested compound datatype. + */ + sensortype = H5Tcreate(H5T_COMPOUND, sizeof(sensor_t)); + status = H5Tinsert(sensortype, "Serial number", HOFFSET(sensor_t, serial_no), H5T_NATIVE_INT); + status = H5Tinsert(sensortype, "Location", HOFFSET(sensor_t, location), strtype); + status = H5Tinsert(sensortype, "Temperature (F)", HOFFSET(sensor_t, temperature), H5T_NATIVE_DOUBLE); + status = H5Tinsert(sensortype, "Pressure (inHg)", HOFFSET(sensor_t, pressure), H5T_NATIVE_DOUBLE); + + /* + * Create the variable-length datatype. + */ + sensorstype = H5Tvlen_create(sensortype); + + /* + * Create the enumerated datatype. + */ + colortype = H5Tenum_create(H5T_NATIVE_INT); + val = (color_t)RED; + status = H5Tenum_insert(colortype, "Red", &val); + val = (color_t)GREEN; + status = H5Tenum_insert(colortype, "Green", &val); + val = (color_t)BLUE; + status = H5Tenum_insert(colortype, "Blue", &val); + + /* + * Create the array datatype. + */ + loctype = H5Tarray_create(H5T_NATIVE_DOUBLE, 1, adims, NULL); + + /* + * Create the main compound datatype. + */ + vehicletype = H5Tcreate(H5T_COMPOUND, sizeof(vehicle_t)); + status = H5Tinsert(vehicletype, "Sensors", HOFFSET(vehicle_t, sensors), sensorstype); + status = H5Tinsert(vehicletype, "Name", HOFFSET(vehicle_t, name), strtype); + status = H5Tinsert(vehicletype, "Color", HOFFSET(vehicle_t, color), colortype); + status = H5Tinsert(vehicletype, "Location", HOFFSET(vehicle_t, location), loctype); + status = H5Tinsert(vehicletype, "Group", HOFFSET(vehicle_t, group), H5T_STD_REF_OBJ); + status = + H5Tinsert(vehicletype, "Surveyed areas", HOFFSET(vehicle_t, surveyed_areas), H5T_STD_REF_DSETREG); + + /* + * Create dataset with a scalar dataspace. to serve as the parent + * for the attribute. + */ + space = H5Screate(H5S_SCALAR); + dset = H5Dcreate(file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); + status = H5Sclose(space); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the attribute and write the compound data to it. + */ + attr = H5Acreate(dset, ATTRIBUTE, vehicletype, space, H5P_DEFAULT); + status = H5Awrite(attr, vehicletype, wdata); + + /* + * Close and release resources. Note that we cannot use + * H5Dvlen_reclaim as it would attempt to free() the string + * constants used to initialize the name fields in wdata. We must + * therefore manually free() only the data previously allocated + * through malloc(). + */ + for (i = 0; i < dims[0]; i++) + free(wdata[i].sensors.p); + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(strtype); + status = H5Tclose(sensortype); + status = H5Tclose(sensorstype); + status = H5Tclose(colortype); + status = H5Tclose(loctype); + status = H5Tclose(vehicletype); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the attribute has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). We will only read back the variable length strings. + */ + + /* + * Open file, dataset, and attribute. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + attr = H5Aopen_name(dset, ATTRIBUTE); + + /* + * Create variable-length string datatype. + */ + strtype = H5Tcopy(H5T_C_S1); + status = H5Tset_size(strtype, H5T_VARIABLE); + + /* + * Create the nested compound datatype for reading. Even though it + * has only one field, it must still be defined as a compound type + * so the library can match the correct field in the file type. + * This matching is done by name. However, we do not need to + * define a structure for the read buffer as we can simply treat it + * as a char *. + */ + rsensortype = H5Tcreate(H5T_COMPOUND, sizeof(char *)); + status = H5Tinsert(rsensortype, "Location", 0, strtype); + + /* + * Create the variable-length datatype for reading. + */ + rsensorstype = H5Tvlen_create(rsensortype); + + /* + * Create the main compound datatype for reading. + */ + rvehicletype = H5Tcreate(H5T_COMPOUND, sizeof(rvehicle_t)); + status = H5Tinsert(rvehicletype, "Sensors", HOFFSET(rvehicle_t, sensors), rsensorstype); + status = H5Tinsert(rvehicletype, "Name", HOFFSET(rvehicle_t, name), strtype); + + /* + * Get dataspace and allocate memory for read buffer. + */ + space = H5Aget_space(attr); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + rdata = (rvehicle_t *)malloc(dims[0] * sizeof(rvehicle_t)); + + /* + * Read the data. + */ + status = H5Aread(attr, rvehicletype, rdata); + + /* + * Output the data to the screen. + */ + for (i = 0; i < dims[0]; i++) { + printf("%s[%d]:\n", ATTRIBUTE, i); + printf(" Vehicle name :\n %s\n", rdata[i].name); + printf(" Sensor locations :\n"); + for (j = 0; j < rdata[i].sensors.len; j++) + printf(" %s\n", ((char **)rdata[i].sensors.p)[j]); + } + + /* + * Close and release resources. H5Dvlen_reclaim will automatically + * traverse the structure and free any vlen data (including + * strings). + */ + status = H5Dvlen_reclaim(rvehicletype, space, H5P_DEFAULT, rdata); + free(rdata); + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(strtype); + status = H5Tclose(rsensortype); + status = H5Tclose(rsensorstype); + status = H5Tclose(rvehicletype); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_enum.c b/HDF5Examples/C/H5T/16/h5ex_t_enum.c new file mode 100644 index 0000000..108182b --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_enum.c @@ -0,0 +1,164 @@ +/************************************************************ + + This example shows how to read and write enumerated + datatypes to a dataset. The program first writes + enumerated values 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_enum.h5" +#define DATASET "DS1" +#define DIM0 4 +#define DIM1 7 +#define F_BASET H5T_STD_I16BE /* File base type */ +#define M_BASET H5T_NATIVE_INT /* Memory base type */ +#define NAME_BUF_SIZE 16 + +typedef enum { SOLID, LIQUID, GAS, PLASMA } phase_t; /* Enumerated type */ + +int +main(void) +{ + hid_t file, filetype, memtype, space, dset; + /* Handles */ + herr_t status; + hsize_t dims[2] = {DIM0, DIM1}; + phase_t wdata[DIM0][DIM1], /* Write buffer */ + **rdata, /* Read buffer */ + val; + char *names[4] = {"SOLID", "LIQUID", "GAS", "PLASMA"}, name[NAME_BUF_SIZE]; + int ndims, i, j; + + /* + * Initialize data. + */ + for (i = 0; i < DIM0; i++) + for (j = 0; j < DIM1; j++) + wdata[i][j] = (phase_t)((i + 1) * j - j) % (int)(PLASMA + 1); + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create the enumerated datatypes for file and memory. This + * process is simplified if native types are used for the file, + * as only one type must be defined. + */ + filetype = H5Tenum_create(F_BASET); + memtype = H5Tenum_create(M_BASET); + + for (i = (int)SOLID; i <= (int)PLASMA; i++) { + /* + * Insert enumerated value for memtype. + */ + val = (phase_t)i; + status = H5Tenum_insert(memtype, names[i], &val); + /* + * Insert enumerated value for filetype. We must first convert + * the numerical value val to the base type of the destination. + */ + status = H5Tconvert(M_BASET, F_BASET, 1, &val, NULL, H5P_DEFAULT); + status = H5Tenum_insert(filetype, names[i], &val); + } + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(2, dims, NULL); + + /* + * Create the dataset and write the enumerated data to it. + */ + dset = H5Dcreate(file, DATASET, filetype, space, H5P_DEFAULT); + status = H5Dwrite(dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata[0]); + + /* + * Close and release resources. + */ + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the dataset has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). For simplicity, we do not rebuild memtype. + */ + + /* + * Open file and dataset. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + + /* + * Get dataspace and allocate memory for read buffer. This is a + * two dimensional dataset so the dynamic allocation must be done + * in steps. + */ + space = H5Dget_space(dset); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + + /* + * Allocate array of pointers to rows. + */ + rdata = (phase_t **)malloc(dims[0] * sizeof(phase_t *)); + + /* + * Allocate space for enumerated data. + */ + rdata[0] = (phase_t *)malloc(dims[0] * dims[1] * sizeof(phase_t)); + + /* + * Set the rest of the pointers to rows to the correct addresses. + */ + for (i = 1; i < dims[0]; i++) + rdata[i] = rdata[0] + i * dims[1]; + + /* + * Read the data. + */ + status = H5Dread(dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0]); + + /* + * Output the data to the screen. + */ + printf("%s:\n", DATASET); + for (i = 0; i < dims[0]; i++) { + printf(" ["); + for (j = 0; j < dims[1]; j++) { + + /* + * Get the name of the enumeration member. + */ + status = H5Tenum_nameof(memtype, &rdata[i][j], name, NAME_BUF_SIZE); + printf(" %-6s", name); + } + printf("]\n"); + } + + /* + * Close and release resources. + */ + free(rdata[0]); + free(rdata); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(memtype); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_enumatt.c b/HDF5Examples/C/H5T/16/h5ex_t_enumatt.c new file mode 100644 index 0000000..ae6c087 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_enumatt.c @@ -0,0 +1,175 @@ +/************************************************************ + + This example shows how to read and write enumerated + datatypes to an attribute. The program first writes + enumerated values 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_enumatt.h5" +#define DATASET "DS1" +#define ATTRIBUTE "A1" +#define DIM0 4 +#define DIM1 7 +#define F_BASET H5T_STD_I16BE /* File base type */ +#define M_BASET H5T_NATIVE_INT /* Memory base type */ +#define NAME_BUF_SIZE 16 + +typedef enum { SOLID, LIQUID, GAS, PLASMA } phase_t; /* Enumerated type */ + +int +main(void) +{ + hid_t file, filetype, memtype, space, dset, attr; + /* Handles */ + herr_t status; + hsize_t dims[2] = {DIM0, DIM1}; + phase_t wdata[DIM0][DIM1], /* Write buffer */ + **rdata, /* Read buffer */ + val; + char *names[4] = {"SOLID", "LIQUID", "GAS", "PLASMA"}, name[NAME_BUF_SIZE]; + int ndims, i, j; + + /* + * Initialize data. + */ + for (i = 0; i < DIM0; i++) + for (j = 0; j < DIM1; j++) + wdata[i][j] = (phase_t)((i + 1) * j - j) % (int)(PLASMA + 1); + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create the enumerated datatypes for file and memory. This + * process is simplified if native types are used for the file, + * as only one type must be defined. + */ + filetype = H5Tenum_create(F_BASET); + memtype = H5Tenum_create(M_BASET); + + for (i = (int)SOLID; i <= (int)PLASMA; i++) { + /* + * Insert enumerated value for memtype. + */ + val = (phase_t)i; + status = H5Tenum_insert(memtype, names[i], &val); + /* + * Insert enumerated value for filetype. We must first convert + * the numerical value val to the base type of the destination. + */ + status = H5Tconvert(M_BASET, F_BASET, 1, &val, NULL, H5P_DEFAULT); + status = H5Tenum_insert(filetype, names[i], &val); + } + + /* + * Create dataset with a scalar dataspace. + */ + space = H5Screate(H5S_SCALAR); + dset = H5Dcreate(file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); + status = H5Sclose(space); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(2, dims, NULL); + + /* + * Create the attribute and write the enumerated data to it. + */ + attr = H5Acreate(dset, ATTRIBUTE, filetype, space, H5P_DEFAULT); + status = H5Awrite(attr, memtype, wdata[0]); + + /* + * Close and release resources. + */ + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the attribute has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). For simplicity, we do not rebuild memtype. + */ + + /* + * Open file, dataset, and attribute. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + attr = H5Aopen_name(dset, ATTRIBUTE); + + /* + * Get dataspace and allocate memory for read buffer. This is a + * two dimensional attribute so the dynamic allocation must be done + * in steps. + */ + space = H5Aget_space(attr); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + + /* + * Allocate array of pointers to rows. + */ + rdata = (phase_t **)malloc(dims[0] * sizeof(phase_t *)); + + /* + * Allocate space for enumerated data. + */ + rdata[0] = (phase_t *)malloc(dims[0] * dims[1] * sizeof(phase_t)); + + /* + * Set the rest of the pointers to rows to the correct addresses. + */ + for (i = 1; i < dims[0]; i++) + rdata[i] = rdata[0] + i * dims[1]; + + /* + * Read the data. + */ + status = H5Aread(attr, memtype, rdata[0]); + + /* + * Output the data to the screen. + */ + printf("%s:\n", ATTRIBUTE); + for (i = 0; i < dims[0]; i++) { + printf(" ["); + for (j = 0; j < dims[1]; j++) { + + /* + * Get the name of the enumeration member. + */ + status = H5Tenum_nameof(memtype, &rdata[i][j], name, NAME_BUF_SIZE); + printf(" %-6s", name); + } + printf("]\n"); + } + + /* + * Close and release resources. + */ + free(rdata[0]); + free(rdata); + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(memtype); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_float.c b/HDF5Examples/C/H5T/16/h5ex_t_float.c new file mode 100644 index 0000000..70e2249 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_float.c @@ -0,0 +1,130 @@ +/************************************************************ + + This example shows how to read and write float datatypes + to a dataset. The program first writes floats 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_float.h5" +#define DATASET "DS1" +#define DIM0 4 +#define DIM1 7 + +int +main(void) +{ + hid_t file, space, dset; /* Handles */ + herr_t status; + hsize_t dims[2] = {DIM0, DIM1}; + double wdata[DIM0][DIM1], /* Write buffer */ + **rdata; /* Read buffer */ + int ndims, i, j; + + /* + * Initialize data. + */ + for (i = 0; i < DIM0; i++) + for (j = 0; j < DIM1; j++) + wdata[i][j] = (double)i / (j + 0.5) + j; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(2, dims, NULL); + + /* + * 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. + */ + dset = H5Dcreate(file, DATASET, H5T_IEEE_F64LE, space, H5P_DEFAULT); + status = H5Dwrite(dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata[0]); + + /* + * Close and release resources. + */ + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the dataset has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). + */ + + /* + * Open file and dataset. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + + /* + * Get dataspace and allocate memory for read buffer. This is a + * two dimensional dataset so the dynamic allocation must be done + * in steps. + */ + space = H5Dget_space(dset); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + + /* + * Allocate array of pointers to rows. + */ + rdata = (double **)malloc(dims[0] * sizeof(double *)); + + /* + * Allocate space for floating point data. + */ + rdata[0] = (double *)malloc(dims[0] * dims[1] * sizeof(double)); + + /* + * Set the rest of the pointers to rows to the correct addresses. + */ + for (i = 1; i < dims[0]; i++) + rdata[i] = rdata[0] + i * dims[1]; + + /* + * Read the data. + */ + status = H5Dread(dset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0]); + + /* + * Output the data to the screen. + */ + printf("%s:\n", DATASET); + for (i = 0; i < dims[0]; i++) { + printf(" ["); + for (j = 0; j < dims[1]; j++) + printf(" %6.4f", rdata[i][j]); + printf("]\n"); + } + + /* + * Close and release resources. + */ + free(rdata[0]); + free(rdata); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_floatatt.c b/HDF5Examples/C/H5T/16/h5ex_t_floatatt.c new file mode 100644 index 0000000..f0a4404 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_floatatt.c @@ -0,0 +1,142 @@ +/************************************************************ + + 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_floatatt.h5" +#define DATASET "DS1" +#define ATTRIBUTE "A1" +#define DIM0 4 +#define DIM1 7 + +int +main(void) +{ + hid_t file, space, dset, attr; /* Handles */ + herr_t status; + hsize_t dims[2] = {DIM0, DIM1}; + double wdata[DIM0][DIM1], /* Write buffer */ + **rdata; /* Read buffer */ + int ndims, i, j; + + /* + * Initialize data. + */ + for (i = 0; i < DIM0; i++) + for (j = 0; j < DIM1; j++) + wdata[i][j] = (double)i / (j + 0.5) + j; + ; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create dataset with a scalar dataspace. + */ + space = H5Screate(H5S_SCALAR); + dset = H5Dcreate(file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); + status = H5Sclose(space); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(2, dims, NULL); + + /* + * Create the attribute 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. + */ + attr = H5Acreate(dset, ATTRIBUTE, H5T_IEEE_F64LE, space, H5P_DEFAULT); + status = H5Awrite(attr, H5T_NATIVE_DOUBLE, wdata[0]); + + /* + * Close and release resources. + */ + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the attribute has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). + */ + + /* + * Open file, dataset, and attribute. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + attr = H5Aopen_name(dset, ATTRIBUTE); + + /* + * Get dataspace and allocate memory for read buffer. This is a + * two dimensional attribute so the dynamic allocation must be done + * in steps. + */ + space = H5Aget_space(attr); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + + /* + * Allocate array of pointers to rows. + */ + rdata = (double **)malloc(dims[0] * sizeof(double *)); + + /* + * Allocate space for floating point data. + */ + rdata[0] = (double *)malloc(dims[0] * dims[1] * sizeof(double)); + + /* + * Set the rest of the pointers to rows to the correct addresses. + */ + for (i = 1; i < dims[0]; i++) + rdata[i] = rdata[0] + i * dims[1]; + + /* + * Read the data. + */ + status = H5Aread(attr, H5T_NATIVE_DOUBLE, rdata[0]); + + /* + * Output the data to the screen. + */ + printf("%s:\n", ATTRIBUTE); + for (i = 0; i < dims[0]; i++) { + printf(" ["); + for (j = 0; j < dims[1]; j++) + printf(" %6.4f", rdata[i][j]); + printf("]\n"); + } + + /* + * Close and release resources. + */ + free(rdata[0]); + free(rdata); + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_int.c b/HDF5Examples/C/H5T/16/h5ex_t_int.c new file mode 100644 index 0000000..8534025 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_int.c @@ -0,0 +1,129 @@ +/************************************************************ + + 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_int.h5" +#define DATASET "DS1" +#define DIM0 4 +#define DIM1 7 + +int +main(void) +{ + hid_t file, space, dset; /* Handles */ + herr_t status; + hsize_t dims[2] = {DIM0, DIM1}; + int wdata[DIM0][DIM1], /* Write buffer */ + **rdata, /* Read buffer */ + ndims, i, j; + + /* + * Initialize data. + */ + for (i = 0; i < DIM0; i++) + for (j = 0; j < DIM1; j++) + wdata[i][j] = i * j - j; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(2, dims, NULL); + + /* + * 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. + */ + dset = H5Dcreate(file, DATASET, H5T_STD_I64BE, space, H5P_DEFAULT); + status = H5Dwrite(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata[0]); + + /* + * Close and release resources. + */ + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the dataset has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). + */ + + /* + * Open file and dataset. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + + /* + * Get dataspace and allocate memory for read buffer. This is a + * two dimensional dataset so the dynamic allocation must be done + * in steps. + */ + space = H5Dget_space(dset); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + + /* + * Allocate array of pointers to rows. + */ + rdata = (int **)malloc(dims[0] * sizeof(int *)); + + /* + * Allocate space for integer data. + */ + rdata[0] = (int *)malloc(dims[0] * dims[1] * sizeof(int)); + + /* + * Set the rest of the pointers to rows to the correct addresses. + */ + for (i = 1; i < dims[0]; i++) + rdata[i] = rdata[0] + i * dims[1]; + + /* + * Read the data. + */ + status = H5Dread(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0]); + + /* + * Output the data to the screen. + */ + printf("%s:\n", DATASET); + for (i = 0; i < dims[0]; i++) { + printf(" ["); + for (j = 0; j < dims[1]; j++) + printf(" %3d", rdata[i][j]); + printf("]\n"); + } + + /* + * Close and release resources. + */ + free(rdata[0]); + free(rdata); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_intatt.c b/HDF5Examples/C/H5T/16/h5ex_t_intatt.c new file mode 100644 index 0000000..d7a43d6 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_intatt.c @@ -0,0 +1,140 @@ +/************************************************************ + + 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_intatt.h5" +#define DATASET "DS1" +#define ATTRIBUTE "A1" +#define DIM0 4 +#define DIM1 7 + +int +main(void) +{ + hid_t file, space, dset, attr; /* Handles */ + herr_t status; + hsize_t dims[2] = {DIM0, DIM1}; + int wdata[DIM0][DIM1], /* Write buffer */ + **rdata, /* Read buffer */ + ndims, i, j; + + /* + * Initialize data. + */ + for (i = 0; i < DIM0; i++) + for (j = 0; j < DIM1; j++) + wdata[i][j] = i * j - j; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create dataset with a scalar dataspace. + */ + space = H5Screate(H5S_SCALAR); + dset = H5Dcreate(file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); + status = H5Sclose(space); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(2, dims, NULL); + + /* + * Create the attribute 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. + */ + attr = H5Acreate(dset, ATTRIBUTE, H5T_STD_I64BE, space, H5P_DEFAULT); + status = H5Awrite(attr, H5T_NATIVE_INT, wdata[0]); + + /* + * Close and release resources. + */ + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the attribute has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). + */ + + /* + * Open file, dataset, and attribute. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + attr = H5Aopen_name(dset, ATTRIBUTE); + + /* + * Get dataspace and allocate memory for read buffer. This is a + * two dimensional attribute so the dynamic allocation must be done + * in steps. + */ + space = H5Aget_space(attr); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + + /* + * Allocate array of pointers to rows. + */ + rdata = (int **)malloc(dims[0] * sizeof(int *)); + + /* + * Allocate space for integer data. + */ + rdata[0] = (int *)malloc(dims[0] * dims[1] * sizeof(int)); + + /* + * Set the rest of the pointers to rows to the correct addresses. + */ + for (i = 1; i < dims[0]; i++) + rdata[i] = rdata[0] + i * dims[1]; + + /* + * Read the data. + */ + status = H5Aread(attr, H5T_NATIVE_INT, rdata[0]); + + /* + * Output the data to the screen. + */ + printf("%s:\n", ATTRIBUTE); + for (i = 0; i < dims[0]; i++) { + printf(" ["); + for (j = 0; j < dims[1]; j++) + printf(" %3d", rdata[i][j]); + printf("]\n"); + } + + /* + * Close and release resources. + */ + free(rdata[0]); + free(rdata); + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_objref.c b/HDF5Examples/C/H5T/16/h5ex_t_objref.c new file mode 100644 index 0000000..4c35239 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_objref.c @@ -0,0 +1,158 @@ +/************************************************************ + + 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_objref.h5" +#define DATASET "DS1" +#define DIM0 2 + +int +main(void) +{ + hid_t file, space, dset, obj; /* Handles */ + herr_t status; + hsize_t dims[1] = {DIM0}; + hobj_ref_t wdata[DIM0], /* Write buffer */ + *rdata; /* Read buffer */ + H5G_obj_t objtype; + ssize_t size; + char *name; + int ndims, i; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create a dataset with a scalar dataspace. + */ + space = H5Screate(H5S_SCALAR); + obj = H5Dcreate(file, "DS2", H5T_STD_I32LE, space, H5P_DEFAULT); + status = H5Dclose(obj); + status = H5Sclose(space); + + /* + * Create a group. + */ + obj = H5Gcreate(file, "G1", H5P_DEFAULT); + status = H5Gclose(obj); + + /* + * 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. + */ + status = H5Rcreate(&wdata[0], file, "G1", H5R_OBJECT, -1); + status = H5Rcreate(&wdata[1], file, "DS2", H5R_OBJECT, -1); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the dataset and write the object references to it. + */ + dset = H5Dcreate(file, DATASET, H5T_STD_REF_OBJ, space, H5P_DEFAULT); + status = H5Dwrite(dset, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata); + + /* + * Close and release resources. + */ + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the dataset has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). + */ + + /* + * Open file and dataset. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + + /* + * Get dataspace and allocate memory for read buffer. + */ + space = H5Dget_space(dset); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + rdata = (hobj_ref_t *)malloc(dims[0] * sizeof(hobj_ref_t)); + + /* + * Read the data. + */ + status = H5Dread(dset, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata); + + /* + * Output the data to the screen. + */ + for (i = 0; i < dims[0]; i++) { + printf("%s[%d]:\n ->", DATASET, i); + /* + * Open the referenced object, get its name and type. + */ + obj = H5Rdereference(dset, H5R_OBJECT, &rdata[i]); + objtype = H5Rget_obj_type(dset, H5R_OBJECT, &rdata[i]); + + /* + * Get the length of the name, allocate space, then retrieve + * the name. + */ + size = 1 + H5Iget_name(obj, NULL, 0); + name = (char *)malloc(size); + size = H5Iget_name(obj, name, size); + + /* + * Print the object type and close the object. + */ + switch (objtype) { + case H5G_GROUP: + printf("Group"); + status = H5Gclose(obj); + break; + case H5G_DATASET: + printf("Dataset"); + status = H5Dclose(obj); + break; + case H5G_TYPE: + printf("Named Datatype"); + status = H5Tclose(obj); + } + + /* + * Print the name and deallocate space for the name. + */ + printf(": %s\n", name); + free(name); + } + + /* + * Close and release resources. + */ + free(rdata); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_objrefatt.c b/HDF5Examples/C/H5T/16/h5ex_t_objrefatt.c new file mode 100644 index 0000000..95d86dc --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_objrefatt.c @@ -0,0 +1,175 @@ +/************************************************************ + + 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_objrefatt.h5" +#define DATASET "DS1" +#define ATTRIBUTE "A1" +#define DIM0 2 + +int +main(void) +{ + hid_t file; /* File Handle */ + hid_t space; /* Dataspace Handle */ + hid_t dset; /* Dataset Handle */ + hid_t obj; /* Object Handle */ + herr_t status; + hsize_t dims[1] = {DIM0}; + hobj_ref_t wdata[DIM0]; /* Write buffer */ + hobj_ref_t *rdata = NULL; /* Read buffer */ + H5G_obj_t objtype; + ssize_t size; + char *name = NULL; + int ndims; + int i; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create a dataset with a scalar dataspace. + */ + space = H5Screate(H5S_SCALAR); + obj = H5Dcreate(file, "DS2", H5T_STD_I32LE, space, H5P_DEFAULT); + status = H5Dclose(obj); + status = H5Sclose(space); + + /* + * Create a group. + */ + obj = H5Gcreate(file, "G1", H5P_DEFAULT); + status = H5Gclose(obj); + + /* + * 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. + */ + status = H5Rcreate(&wdata[0], file, "G1", H5R_OBJECT, -1); + status = H5Rcreate(&wdata[1], file, "DS2", H5R_OBJECT, -1); + + /* + * Create dataset with a scalar dataspace to serve as the parent + * for the attribute. + */ + space = H5Screate(H5S_SCALAR); + dset = H5Dcreate(file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); + status = H5Sclose(space); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the attribute and write the object references to it. + */ + attr = H5Acreate(dset, ATTRIBUTE, H5T_STD_REF_OBJ, space, H5P_DEFAULT); + status = H5Awrite(attr, H5T_STD_REF_OBJ, wdata); + + /* + * Close and release resources. + */ + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the attribute has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). + */ + + /* + * Open file, dataset, and attribute. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + attr = H5Aopen_name(dset, ATTRIBUTE); + + /* + * Get dataspace and allocate memory for read buffer. + */ + space = H5Aget_space(attr); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + rdata = (hobj_ref_t *)malloc(dims[0] * sizeof(hobj_ref_t)); + + /* + * Read the data. + */ + status = H5Aread(attr, H5T_STD_REF_OBJ, rdata); + + /* + * Output the data to the screen. + */ + for (i = 0; i < dims[0]; i++) { + printf("%s[%d]:\n ->", ATTRIBUTE, i); + + /* + * Open the referenced object, get its name and type. + */ + obj = H5Rdereference(dset, H5R_OBJECT, &rdata[i]); + objtype = H5Rget_obj_type(dset, H5R_OBJECT, &rdata[i]); + + /* + * Get the length of the name, allocate space, then retrieve + * the name. + */ + size = 1 + H5Iget_name(obj, NULL, 0); + name = (char *)malloc(size); + size = H5Iget_name(obj, name, size); + + /* + * Print the object type and close the object. + */ + switch (objtype) { + case H5G_GROUP: + printf("Group"); + status = H5Gclose(obj); + break; + case H5G_DATASET: + printf("Dataset"); + status = H5Dclose(obj); + break; + case H5G_TYPE: + printf("Named Datatype"); + status = H5Tclose(obj); + } + + /* + * Print the name and deallocate space for the name. + */ + printf(": %s\n", name); + free(name); + } + + /* + * Close and release resources. + */ + free(rdata); + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_opaque.c b/HDF5Examples/C/H5T/16/h5ex_t_opaque.c new file mode 100644 index 0000000..8755ccc --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_opaque.c @@ -0,0 +1,142 @@ +/************************************************************ + + 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_opaque.h5" +#define DATASET "DS1" +#define DIM0 4 +#define LEN 7 + +int +main(void) +{ + hid_t file, space, dtype, dset; /* Handles */ + herr_t status; + hsize_t dims[1] = {DIM0}; + size_t len; + char wdata[DIM0 * LEN], /* Write buffer */ + *rdata, /* Read buffer */ + str[LEN] = "OPAQUE", *tag; + int ndims, i, j; + unsigned majnum, minnum, relnum; + + /* Get library version to differentiate between acceptable version methods + * to free the tag returned by H5Tget_tag. */ + H5get_libversion(&majnum, &minnum, &relnum); + + /* + * Initialize data. + */ + for (i = 0; i < DIM0; i++) { + for (j = 0; j < LEN - 1; j++) + wdata[j + i * LEN] = str[j]; + wdata[LEN - 1 + i * LEN] = (char)i + '0'; + } + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create opaque datatype and set the tag to something appropriate. + * For this example we will write and view the data as a character + * array. + */ + dtype = H5Tcreate(H5T_OPAQUE, LEN); + status = H5Tset_tag(dtype, "Character array"); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the dataset and write the opaque data to it. + */ + dset = H5Dcreate(file, DATASET, dtype, space, H5P_DEFAULT); + status = H5Dwrite(dset, dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata); + + /* + * Close and release resources. + */ + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(dtype); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the dataset has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). + */ + + /* + * Open file and dataset. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + + /* + * Get datatype and properties for the datatype. Note that H5Tget_tag + * allocates space for the string in tag, so we must remember to free() it + * later. + */ + dtype = H5Dget_type(dset); + len = H5Tget_size(dtype); + tag = H5Tget_tag(dtype); + + /* + * Get dataspace and allocate memory for read buffer. + */ + space = H5Dget_space(dset); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + rdata = (char *)malloc(dims[0] * len); + + /* + * Read the data. + */ + status = H5Dread(dset, dtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata); + + /* + * Output the data to the screen. + */ + printf("Datatype tag for %s is: \"%s\"\n", DATASET, tag); + for (i = 0; i < dims[0]; i++) { + printf("%s[%u]: ", DATASET, i); + for (j = 0; j < len; j++) + printf("%c", rdata[j + i * len]); + printf("\n"); + } + + /* + * Close and release resources. + */ + free(rdata); + /* H5free_memory is available in 1.8.16 and above. + * Last version for 1.6 was 1.6.10. */ + if (minnum > 8 || relnum > 15) + H5free_memory(tag); + else + free(tag); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(dtype); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_opaqueatt.c b/HDF5Examples/C/H5T/16/h5ex_t_opaqueatt.c new file mode 100644 index 0000000..4a54fbb --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_opaqueatt.c @@ -0,0 +1,153 @@ +/************************************************************ + + 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_opaqueatt.h5" +#define DATASET "DS1" +#define ATTRIBUTE "A1" +#define DIM0 4 +#define LEN 7 + +int +main(void) +{ + hid_t file, space, dtype, dset, attr; /* Handles */ + herr_t status; + hsize_t dims[1] = {DIM0}; + size_t len; + char wdata[DIM0 * LEN], /* Write buffer */ + *rdata, /* Read buffer */ + str[LEN] = "OPAQUE", *tag; + int ndims, i, j; + unsigned majnum, minnum, relnum; + + /* Get library version to differentiate between acceptable version methods + * to free the tag returned by H5Tget_tag. */ + H5get_libversion(&majnum, &minnum, &relnum); + + /* + * Initialize data. + */ + for (i = 0; i < DIM0; i++) { + for (j = 0; j < LEN - 1; j++) + wdata[j + i * LEN] = str[j]; + wdata[LEN - 1 + i * LEN] = (char)i + '0'; + } + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create dataset with a scalar dataspace. + */ + space = H5Screate(H5S_SCALAR); + dset = H5Dcreate(file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); + status = H5Sclose(space); + + /* + * Create opaque datatype and set the tag to something appropriate. + * For this example we will write and view the data as a character + * array. + */ + dtype = H5Tcreate(H5T_OPAQUE, LEN); + status = H5Tset_tag(dtype, "Character array"); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the attribute and write the opaque data to it. + */ + attr = H5Acreate(dset, ATTRIBUTE, dtype, space, H5P_DEFAULT); + status = H5Awrite(attr, dtype, wdata); + + /* + * Close and release resources. + */ + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(dtype); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the attribute has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). + */ + + /* + * Open file, dataset, and attribute. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + attr = H5Aopen_name(dset, ATTRIBUTE); + + /* + * Get datatype and properties for the datatype. Note that H5Tget_tag + * allocates space for the string in tag, so we must remember to free() it + * later. + */ + dtype = H5Aget_type(attr); + len = H5Tget_size(dtype); + tag = H5Tget_tag(dtype); + + /* + * Get dataspace and allocate memory for read buffer. + */ + space = H5Aget_space(attr); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + rdata = (char *)malloc(dims[0] * len); + + /* + * Read the data. + */ + status = H5Aread(attr, dtype, rdata); + + /* + * Output the data to the screen. + */ + printf("Datatype tag for %s is: \"%s\"\n", ATTRIBUTE, tag); + for (i = 0; i < dims[0]; i++) { + printf("%s[%u]: ", ATTRIBUTE, i); + for (j = 0; j < len; j++) + printf("%c", rdata[j + i * len]); + printf("\n"); + } + + /* + * Close and release resources. + */ + free(rdata); + /* H5free_memory is available in 1.8.16 and above. + * Last version for 1.6 was 1.6.10. */ + if (minnum > 8 || relnum > 15) + H5free_memory(tag); + else + free(tag); + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(dtype); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_regref.c b/HDF5Examples/C/H5T/16/h5ex_t_regref.c new file mode 100644 index 0000000..50b3ff0 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_regref.c @@ -0,0 +1,170 @@ +/************************************************************ + + This example shows how to read and write region references + to a dataset. The program first creates a dataset + containing characters and writes references to region of + the dataset to a new dataset with a dataspace of DIM0, + then closes the file. Next, it reopens the file, + dereferences the references, and outputs the referenced + regions to the screen. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_regref.h5" +#define DATASET "DS1" +#define DATASET2 "DS2" +#define DIM0 2 +#define DS2DIM0 3 +#define DS2DIM1 16 + +int +main(void) +{ + hid_t file, space, memspace, dset, dset2; + /* Handles */ + herr_t status; + hsize_t dims[1] = {DIM0}, dims2[2] = {DS2DIM0, DS2DIM1}, coords[4][2] = {{0, 1}, {2, 11}, {1, 0}, {2, 4}}, + start[2] = {0, 0}, stride[2] = {2, 11}, count[2] = {2, 2}, block[2] = {1, 3}; + hssize_t npoints; + hdset_reg_ref_t wdata[DIM0], /* Write buffer */ + *rdata; /* Read buffer */ + ssize_t size; + char wdata2[DS2DIM0][DS2DIM1] = {"The quick brown", "fox jumps over ", "the 5 lazy dogs"}, *rdata2, *name; + int ndims, i; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create a dataset with character data. + */ + space = H5Screate_simple(2, dims2, NULL); + dset2 = H5Dcreate(file, DATASET2, H5T_STD_I8LE, space, H5P_DEFAULT); + status = H5Dwrite(dset2, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata2); + + /* + * Create reference to a list of elements in dset2. + */ + status = H5Sselect_elements(space, H5S_SELECT_SET, 4, coords[0]); + status = H5Rcreate(&wdata[0], file, DATASET2, H5R_DATASET_REGION, space); + + /* + * Create reference to a hyperslab in dset2, close dataspace. + */ + status = H5Sselect_hyperslab(space, H5S_SELECT_SET, start, stride, count, block); + status = H5Rcreate(&wdata[1], file, DATASET2, H5R_DATASET_REGION, space); + status = H5Sclose(space); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the dataset and write the region references to it. + */ + dset = H5Dcreate(file, DATASET, H5T_STD_REF_DSETREG, space, H5P_DEFAULT); + status = H5Dwrite(dset, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata); + + /* + * Close and release resources. + */ + status = H5Dclose(dset); + status = H5Dclose(dset2); + status = H5Sclose(space); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the dataset has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). + */ + + /* + * Open file and dataset. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + + /* + * Get dataspace and allocate memory for read buffer. + */ + space = H5Dget_space(dset); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + rdata = (hdset_reg_ref_t *)malloc(dims[0] * sizeof(hdset_reg_ref_t)); + status = H5Sclose(space); + + /* + * Read the data. + */ + status = H5Dread(dset, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata); + + /* + * Output the data to the screen. + */ + for (i = 0; i < dims[0]; i++) { + printf("%s[%d]:\n ->", DATASET, i); + + /* + * Open the referenced object, retrieve its region as a + * dataspace selection. + */ + dset2 = H5Rdereference(dset, H5R_DATASET_REGION, &rdata[i]); + space = H5Rget_region(dset, H5R_DATASET_REGION, &rdata[i]); + + /* + * Get the length of the object's name, allocate space, then + * retrieve the name. + */ + size = 1 + H5Iget_name(dset2, NULL, 0); + name = (char *)malloc(size); + size = 1 + H5Iget_name(dset2, name, size); + if (size <= 1) + name[0] = '\0'; + + /* + * Allocate space for the read buffer. We will only allocate + * enough space for the selection, plus a null terminator. The + * read buffer will be 1-dimensional. + */ + npoints = H5Sget_select_npoints(space); + rdata2 = (char *)malloc(npoints + 1); + + /* + * Read the dataset region, and add a null terminator so we can + * print it as a string. + */ + memspace = H5Screate_simple(1, (hsize_t *)&npoints, NULL); + status = H5Dread(dset2, H5T_NATIVE_CHAR, memspace, space, H5P_DEFAULT, rdata2); + rdata2[npoints] = '\0'; + + /* + * Print the name and region data, close and release resources. + */ + printf(" %s: %s\n", name, rdata2); + free(rdata2); + free(name); + status = H5Sclose(space); + status = H5Sclose(memspace); + status = H5Dclose(dset2); + } + + /* + * Close and release resources. + */ + free(rdata); + status = H5Dclose(dset); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_regrefatt.c b/HDF5Examples/C/H5T/16/h5ex_t_regrefatt.c new file mode 100644 index 0000000..1f993fb --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_regrefatt.c @@ -0,0 +1,182 @@ +/************************************************************ + + This example shows how to read and write region references + to an attribute. The program first creates a dataset + containing characters and writes references to region of + the dataset to a new attribute with a dataspace of DIM0, + then closes the file. Next, it reopens the file, + dereferences the references, and outputs the referenced + regions to the screen. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_regrefatt.h5" +#define DATASET "DS1" +#define DATASET2 "DS2" +#define ATTRIBUTE "A1" +#define DIM0 2 +#define DS2DIM0 3 +#define DS2DIM1 16 + +int +main(void) +{ + hid_t file, space, memspace, dset, dset2, attr; + /* Handles */ + herr_t status; + hsize_t dims[1] = {DIM0}, dims2[2] = {DS2DIM0, DS2DIM1}, coords[4][2] = {{0, 1}, {2, 11}, {1, 0}, {2, 4}}, + start[2] = {0, 0}, stride[2] = {2, 11}, count[2] = {2, 2}, block[2] = {1, 3}; + hssize_t npoints; + hdset_reg_ref_t wdata[DIM0], /* Write buffer */ + *rdata; /* Read buffer */ + ssize_t size; + char wdata2[DS2DIM0][DS2DIM1] = {"The quick brown", "fox jumps over ", "the 5 lazy dogs"}, *rdata2, *name; + int ndims, i; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create a dataset with character data. + */ + space = H5Screate_simple(2, dims2, NULL); + dset2 = H5Dcreate(file, DATASET2, H5T_STD_I8LE, space, H5P_DEFAULT); + status = H5Dwrite(dset2, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata2); + + /* + * Create reference to a list of elements in dset2. + */ + status = H5Sselect_elements(space, H5S_SELECT_SET, 4, coords[0]); + status = H5Rcreate(&wdata[0], file, DATASET2, H5R_DATASET_REGION, space); + + /* + * Create reference to a hyperslab in dset2, close dataspace. + */ + status = H5Sselect_hyperslab(space, H5S_SELECT_SET, start, stride, count, block); + status = H5Rcreate(&wdata[1], file, DATASET2, H5R_DATASET_REGION, space); + status = H5Sclose(space); + + /* + * Create dataset with a scalar dataspace to serve as the parent + * for the attribute. + */ + space = H5Screate(H5S_SCALAR); + dset = H5Dcreate(file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); + status = H5Sclose(space); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the attribute and write the region references to it. + */ + attr = H5Acreate(dset, ATTRIBUTE, H5T_STD_REF_DSETREG, space, H5P_DEFAULT); + status = H5Awrite(attr, H5T_STD_REF_DSETREG, wdata); + + /* + * Close and release resources. + */ + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Dclose(dset2); + status = H5Sclose(space); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the attribute has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). + */ + + /* + * Open file, dataset, and attribute. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + attr = H5Aopen_name(dset, ATTRIBUTE); + + /* + * Get dataspace and allocate memory for read buffer. + */ + space = H5Aget_space(attr); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + rdata = (hdset_reg_ref_t *)malloc(dims[0] * sizeof(hdset_reg_ref_t)); + status = H5Sclose(space); + + /* + * Read the data. + */ + status = H5Aread(attr, H5T_STD_REF_DSETREG, rdata); + + /* + * Output the data to the screen. + */ + for (i = 0; i < dims[0]; i++) { + printf("%s[%d]:\n ->", ATTRIBUTE, i); + + /* + * Open the referenced object, retrieve its region as a + * dataspace selection. + */ + dset2 = H5Rdereference(dset, H5R_DATASET_REGION, &rdata[i]); + space = H5Rget_region(dset, H5R_DATASET_REGION, &rdata[i]); + + /* + * Get the length of the object's name, allocate space, then + * retrieve the name. + */ + size = 1 + H5Iget_name(dset2, NULL, 0); + name = (char *)malloc(size); + size = H5Iget_name(dset2, name, size); + if (size <= 1) + name[0] = '\0'; + + /* + * Allocate space for the read buffer. We will only allocate + * enough space for the selection, plus a null terminator. The + * read buffer will be 1-dimensional. + */ + npoints = H5Sget_select_npoints(space); + rdata2 = (char *)malloc(npoints + 1); + + /* + * Read the dataset region, and add a null terminator so we can + * print it as a string. + */ + memspace = H5Screate_simple(1, (hsize_t *)&npoints, NULL); + status = H5Dread(dset2, H5T_NATIVE_CHAR, memspace, space, H5P_DEFAULT, rdata2); + rdata2[npoints] = '\0'; + + /* + * Print the name and region data, close and release resources. + */ + printf(" %s: %s\n", name, rdata2); + free(rdata2); + free(name); + status = H5Sclose(space); + status = H5Sclose(memspace); + status = H5Dclose(dset2); + } + + /* + * Close and release resources. + */ + free(rdata); + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_string.c b/HDF5Examples/C/H5T/16/h5ex_t_string.c new file mode 100644 index 0000000..532e664 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_string.c @@ -0,0 +1,144 @@ +/************************************************************ + + 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_string.h5" +#define DATASET "DS1" +#define DIM0 4 +#define SDIM 8 + +int +main(void) +{ + hid_t file, filetype, memtype, space, dset; + /* Handles */ + herr_t status; + hsize_t dims[1] = {DIM0}; + size_t sdim; + char wdata[DIM0][SDIM] = {"Parting", "is such", "sweet", "sorrow."}, + /* Write buffer */ + **rdata; /* Read buffer */ + int ndims, i; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * 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. + */ + filetype = H5Tcopy(H5T_FORTRAN_S1); + status = H5Tset_size(filetype, SDIM - 1); + memtype = H5Tcopy(H5T_C_S1); + status = H5Tset_size(memtype, SDIM); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the dataset and write the string data to it. + */ + dset = H5Dcreate(file, DATASET, filetype, space, H5P_DEFAULT); + status = H5Dwrite(dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata[0]); + + /* + * Close and release resources. + */ + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Tclose(memtype); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the dataset and string have the same name and rank, but can have + * any size. Therefore we must allocate a new array to read in + * data using malloc(). + */ + + /* + * Open file and dataset. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + + /* + * Get the datatype and its size. + */ + filetype = H5Dget_type(dset); + sdim = H5Tget_size(filetype); + sdim++; /* Make room for null terminator */ + + /* + * Get dataspace and allocate memory for read buffer. This is a + * two dimensional dataset so the dynamic allocation must be done + * in steps. + */ + space = H5Dget_space(dset); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + + /* + * Allocate array of pointers to rows. + */ + rdata = (char **)malloc(dims[0] * sizeof(char *)); + + /* + * Allocate space for integer data. + */ + rdata[0] = (char *)malloc(dims[0] * sdim * sizeof(char)); + + /* + * Set the rest of the pointers to rows to the correct addresses. + */ + for (i = 1; i < dims[0]; i++) + rdata[i] = rdata[0] + i * sdim; + + /* + * Create the memory datatype. + */ + memtype = H5Tcopy(H5T_C_S1); + status = H5Tset_size(memtype, sdim); + + /* + * Read the data. + */ + status = H5Dread(dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0]); + + /* + * Output the data to the screen. + */ + for (i = 0; i < dims[0]; i++) + printf("%s[%d]: %s\n", DATASET, i, rdata[i]); + + /* + * Close and release resources. + */ + free(rdata[0]); + free(rdata); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Tclose(memtype); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_stringatt.c b/HDF5Examples/C/H5T/16/h5ex_t_stringatt.c new file mode 100644 index 0000000..7aa0af2 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_stringatt.c @@ -0,0 +1,155 @@ +/************************************************************ + + 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_stringatt.h5" +#define DATASET "DS1" +#define ATTRIBUTE "A1" +#define DIM0 4 +#define SDIM 8 + +int +main(void) +{ + hid_t file, filetype, memtype, space, dset, attr; + /* Handles */ + herr_t status; + hsize_t dims[1] = {DIM0}; + size_t sdim; + char wdata[DIM0][SDIM] = {"Parting", "is such", "sweet", "sorrow."}, + /* Write buffer */ + **rdata; /* Read buffer */ + int ndims, i; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * 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. + */ + filetype = H5Tcopy(H5T_FORTRAN_S1); + status = H5Tset_size(filetype, SDIM - 1); + memtype = H5Tcopy(H5T_C_S1); + status = H5Tset_size(memtype, SDIM); + + /* + * Create dataset with a scalar dataspace. + */ + space = H5Screate(H5S_SCALAR); + dset = H5Dcreate(file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); + status = H5Sclose(space); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the attribute and write the string data to it. + */ + attr = H5Acreate(dset, ATTRIBUTE, filetype, space, H5P_DEFAULT); + status = H5Awrite(attr, memtype, wdata[0]); + + /* + * Close and release resources. + */ + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Tclose(memtype); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the attribute and string have the same name and rank, but can + * have any size. Therefore we must allocate a new array to read + * in data using malloc(). + */ + + /* + * Open file, dataset, and attribute. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + attr = H5Aopen_name(dset, ATTRIBUTE); + + /* + * Get the datatype and its size. + */ + filetype = H5Aget_type(attr); + sdim = H5Tget_size(filetype); + sdim++; /* Make room for null terminator */ + + /* + * Get dataspace and allocate memory for read buffer. This is a + * two dimensional attribute so the dynamic allocation must be done + * in steps. + */ + space = H5Aget_space(attr); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + + /* + * Allocate array of pointers to rows. + */ + rdata = (char **)malloc(dims[0] * sizeof(char *)); + + /* + * Allocate space for integer data. + */ + rdata[0] = (char *)malloc(dims[0] * sdim * sizeof(char)); + + /* + * Set the rest of the pointers to rows to the correct addresses. + */ + for (i = 1; i < dims[0]; i++) + rdata[i] = rdata[0] + i * sdim; + + /* + * Create the memory datatype. + */ + memtype = H5Tcopy(H5T_C_S1); + status = H5Tset_size(memtype, sdim); + + /* + * Read the data. + */ + status = H5Aread(attr, memtype, rdata[0]); + + /* + * Output the data to the screen. + */ + for (i = 0; i < dims[0]; i++) + printf("%s[%d]: %s\n", ATTRIBUTE, i, rdata[i]); + + /* + * Close and release resources. + */ + free(rdata[0]); + free(rdata); + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Tclose(memtype); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_vlen.c b/HDF5Examples/C/H5T/16/h5ex_t_vlen.c new file mode 100644 index 0000000..ab0e178 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_vlen.c @@ -0,0 +1,145 @@ +/************************************************************ + + This example shows how to read and write variable-length + datatypes to a dataset. The program first writes two + variable-length integer arrays to a dataset then closes + the file. Next, it reopens the file, reads back the data, + and outputs it to the screen. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_vlen.h5" +#define DATASET "DS1" +#define LEN0 3 +#define LEN1 12 + +int +main(void) +{ + hid_t file, filetype, memtype, space, dset; + /* Handles */ + herr_t status; + hvl_t wdata[2], /* Array of vlen structures */ + *rdata; /* Pointer to vlen structures */ + hsize_t dims[1] = {2}; + int *ptr, ndims, i, j; + + /* + * Initialize variable-length data. wdata[0] is a countdown of + * length LEN0, wdata[1] is a Fibonacci sequence of length LEN1. + */ + wdata[0].len = LEN0; + ptr = (int *)malloc(wdata[0].len * sizeof(int)); + for (i = 0; i < wdata[0].len; i++) + ptr[i] = wdata[0].len - i; /* 3 2 1 */ + wdata[0].p = (void *)ptr; + + wdata[1].len = LEN1; + ptr = (int *)malloc(wdata[1].len * sizeof(int)); + ptr[0] = 1; + ptr[1] = 1; + for (i = 2; i < wdata[1].len; i++) + ptr[i] = ptr[i - 1] + ptr[i - 2]; /* 1 1 2 3 5 8 etc. */ + wdata[1].p = (void *)ptr; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create variable-length datatype for file and memory. + */ + filetype = H5Tvlen_create(H5T_STD_I32LE); + memtype = H5Tvlen_create(H5T_NATIVE_INT); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the dataset and write the variable-length data to it. + */ + dset = H5Dcreate(file, DATASET, filetype, space, H5P_DEFAULT); + status = H5Dwrite(dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata); + + /* + * Close and release resources. Note the use of H5Dvlen_reclaim + * removes the need to manually free() the previously malloc'ed + * data. + */ + status = H5Dvlen_reclaim(memtype, space, H5P_DEFAULT, wdata); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Tclose(memtype); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the dataset has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). + */ + + /* + * Open file and dataset. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + + /* + * Get dataspace and allocate memory for array of vlen structures. + * This does not actually allocate memory for the vlen data, that + * will be done by the library. + */ + space = H5Dget_space(dset); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + rdata = (hvl_t *)malloc(dims[0] * sizeof(hvl_t)); + + /* + * Create the memory datatype. + */ + memtype = H5Tvlen_create(H5T_NATIVE_INT); + + /* + * Read the data. + */ + status = H5Dread(dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata); + + /* + * Output the variable-length data to the screen. + */ + for (i = 0; i < dims[0]; i++) { + printf("%s[%u]:\n {", DATASET, i); + ptr = rdata[i].p; + for (j = 0; j < rdata[i].len; j++) { + printf(" %d", ptr[j]); + if ((j + 1) < rdata[i].len) + printf(","); + } + printf(" }\n"); + } + + /* + * Close and release resources. Note we must still free the + * top-level pointer "rdata", as H5Dvlen_reclaim only frees the + * actual variable-length data, and not the structures themselves. + */ + status = H5Dvlen_reclaim(memtype, space, H5P_DEFAULT, rdata); + free(rdata); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(memtype); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_vlenatt.c b/HDF5Examples/C/H5T/16/h5ex_t_vlenatt.c new file mode 100644 index 0000000..084596c --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_vlenatt.c @@ -0,0 +1,156 @@ +/************************************************************ + + This example shows how to read and write variable-length + datatypes to an attribute. The program first writes two + variable-length integer arrays to the attribute then + closes the file. Next, it reopens the file, reads back + the data, and outputs it to the screen. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_vlenatt.h5" +#define DATASET "DS1" +#define ATTRIBUTE "A1" +#define LEN0 3 +#define LEN1 12 + +int +main(void) +{ + hid_t file, filetype, memtype, space, dset, attr; + /* Handles */ + herr_t status; + hvl_t wdata[2], /* Array of vlen structures */ + *rdata; /* Pointer to vlen structures */ + hsize_t dims[1] = {2}; + int *ptr, ndims, i, j; + + /* + * Initialize variable-length data. wdata[0] is a countdown of + * length LEN0, wdata[1] is a Fibonacci sequence of length LEN1. + */ + wdata[0].len = LEN0; + ptr = (int *)malloc(wdata[0].len * sizeof(int)); + for (i = 0; i < wdata[0].len; i++) + ptr[i] = wdata[0].len - i; /* 3 2 1 */ + wdata[0].p = (void *)ptr; + + wdata[1].len = LEN1; + ptr = (int *)malloc(wdata[1].len * sizeof(int)); + ptr[0] = 1; + ptr[1] = 1; + for (i = 2; i < wdata[1].len; i++) + ptr[i] = ptr[i - 1] + ptr[i - 2]; /* 1 1 2 3 5 8 etc. */ + wdata[1].p = (void *)ptr; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create variable-length datatype for file and memory. + */ + filetype = H5Tvlen_create(H5T_STD_I32LE); + memtype = H5Tvlen_create(H5T_NATIVE_INT); + + /* + * Create dataset with a scalar dataspace. + */ + space = H5Screate(H5S_SCALAR); + dset = H5Dcreate(file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); + status = H5Sclose(space); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the attribute and write the variable-length data to it + */ + attr = H5Acreate(dset, ATTRIBUTE, filetype, space, H5P_DEFAULT); + status = H5Awrite(attr, memtype, wdata); + + /* + * Close and release resources. Note the use of H5Dvlen_reclaim + * removes the need to manually free() the previously malloc'ed + * data. + */ + status = H5Dvlen_reclaim(memtype, space, H5P_DEFAULT, wdata); + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Tclose(memtype); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the attribute has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). + */ + + /* + * Open file, dataset, and attribute. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + attr = H5Aopen_name(dset, ATTRIBUTE); + + /* + * Get dataspace and allocate memory for array of vlen structures. + * This does not actually allocate memory for the vlen data, that + * will be done by the library. + */ + space = H5Aget_space(attr); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + rdata = (hvl_t *)malloc(dims[0] * sizeof(hvl_t)); + + /* + * Create the memory datatype. + */ + memtype = H5Tvlen_create(H5T_NATIVE_INT); + + /* + * Read the data. + */ + status = H5Aread(attr, memtype, rdata); + + /* + * Output the variable-length data to the screen. + */ + for (i = 0; i < dims[0]; i++) { + printf("%s[%u]:\n {", ATTRIBUTE, i); + ptr = rdata[i].p; + for (j = 0; j < rdata[i].len; j++) { + printf(" %d", ptr[j]); + if ((j + 1) < rdata[i].len) + printf(","); + } + printf(" }\n"); + } + + /* + * Close and release resources. Note we must still free the + * top-level pointer "rdata", as H5Dvlen_reclaim only frees the + * actual variable-length data, and not the structures themselves. + */ + status = H5Dvlen_reclaim(memtype, space, H5P_DEFAULT, rdata); + free(rdata); + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(memtype); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_vlstring.c b/HDF5Examples/C/H5T/16/h5ex_t_vlstring.c new file mode 100644 index 0000000..f790424 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_vlstring.c @@ -0,0 +1,126 @@ +/************************************************************ + + This example shows how to read and write variable-length + string datatypes to a dataset. The program first writes + variable-length 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_vlstring.h5" +#define DATASET "DS1" +#define DIM0 4 + +int +main(void) +{ + hid_t file, filetype, memtype, space, dset; + /* Handles */ + herr_t status; + hsize_t dims[1] = {DIM0}; + char *wdata[DIM0] = {"Parting", "is such", "sweet", "sorrow."}, + /* Write buffer */ + **rdata; /* Read buffer */ + int ndims, i; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create file and memory datatypes. For this example we will save + * the strings as FORTRAN strings. + */ + filetype = H5Tcopy(H5T_FORTRAN_S1); + status = H5Tset_size(filetype, H5T_VARIABLE); + memtype = H5Tcopy(H5T_C_S1); + status = H5Tset_size(memtype, H5T_VARIABLE); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the dataset and write the variable-length string data to + * it. + */ + dset = H5Dcreate(file, DATASET, filetype, space, H5P_DEFAULT); + status = H5Dwrite(dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata); + + /* + * Close and release resources. + */ + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Tclose(memtype); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the dataset has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). + */ + + /* + * Open file and dataset. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + + /* + * Get the datatype. + */ + filetype = H5Dget_type(dset); + + /* + * Get dataspace and allocate memory for read buffer. + */ + space = H5Dget_space(dset); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + rdata = (char **)malloc(dims[0] * sizeof(char *)); + + /* + * Create the memory datatype. + */ + memtype = H5Tcopy(H5T_C_S1); + status = H5Tset_size(memtype, H5T_VARIABLE); + + /* + * Read the data. + */ + status = H5Dread(dset, memtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata); + + /* + * Output the data to the screen. + */ + for (i = 0; i < dims[0]; i++) + printf("%s[%d]: %s\n", DATASET, i, rdata[i]); + + /* + * Close and release resources. Note that H5Dvlen_reclaim works + * for variable-length strings as well as variable-length arrays. + * Also note that we must still free the array of pointers stored + * in rdata, as H5Tvlen_reclaim only frees the data these point to. + */ + status = H5Dvlen_reclaim(memtype, space, H5P_DEFAULT, rdata); + free(rdata); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Tclose(memtype); + status = H5Fclose(file); + + return 0; +} diff --git a/HDF5Examples/C/H5T/16/h5ex_t_vlstringatt.c b/HDF5Examples/C/H5T/16/h5ex_t_vlstringatt.c new file mode 100644 index 0000000..6ee3118 --- /dev/null +++ b/HDF5Examples/C/H5T/16/h5ex_t_vlstringatt.c @@ -0,0 +1,138 @@ +/************************************************************ + + This example shows how to read and write variable-length + string datatypes to an attribute. The program first + writes variable-length 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. + + This file is intended for use with HDF5 Library version 1.6 + + ************************************************************/ + +#include "hdf5.h" +#include <stdio.h> +#include <stdlib.h> + +#define FILE "h5ex_t_vlstringatt.h5" +#define DATASET "DS1" +#define ATTRIBUTE "A1" +#define DIM0 4 + +int +main(void) +{ + hid_t file, filetype, memtype, space, dset, attr; + /* Handles */ + herr_t status; + hsize_t dims[1] = {DIM0}; + char *wdata[DIM0] = {"Parting", "is such", "sweet", "sorrow."}, + /* Write buffer */ + **rdata; /* Read buffer */ + int ndims, i; + + /* + * Create a new file using the default properties. + */ + file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); + + /* + * Create file and memory datatypes. For this example we will save + * the strings as FORTRAN strings. + */ + filetype = H5Tcopy(H5T_FORTRAN_S1); + status = H5Tset_size(filetype, H5T_VARIABLE); + memtype = H5Tcopy(H5T_C_S1); + status = H5Tset_size(memtype, H5T_VARIABLE); + + /* + * Create dataset with a scalar dataspace. + */ + space = H5Screate(H5S_SCALAR); + dset = H5Dcreate(file, DATASET, H5T_STD_I32LE, space, H5P_DEFAULT); + status = H5Sclose(space); + + /* + * Create dataspace. Setting maximum size to NULL sets the maximum + * size to be the current size. + */ + space = H5Screate_simple(1, dims, NULL); + + /* + * Create the attribute and write the variable-length string data + * to it. + */ + attr = H5Acreate(dset, ATTRIBUTE, filetype, space, H5P_DEFAULT); + status = H5Awrite(attr, memtype, wdata); + + /* + * Close and release resources. + */ + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Tclose(memtype); + status = H5Fclose(file); + + /* + * Now we begin the read section of this example. Here we assume + * the attribute has the same name and rank, but can have any size. + * Therefore we must allocate a new array to read in data using + * malloc(). + */ + + /* + * Open file, dataset, and attribute. + */ + file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT); + dset = H5Dopen(file, DATASET); + attr = H5Aopen_name(dset, ATTRIBUTE); + + /* + * Get the datatype. + */ + filetype = H5Aget_type(attr); + + /* + * Get dataspace and allocate memory for read buffer. + */ + space = H5Aget_space(attr); + ndims = H5Sget_simple_extent_dims(space, dims, NULL); + rdata = (char **)malloc(dims[0] * sizeof(char *)); + + /* + * Create the memory datatype. + */ + memtype = H5Tcopy(H5T_C_S1); + status = H5Tset_size(memtype, H5T_VARIABLE); + + /* + * Read the data. + */ + status = H5Aread(attr, memtype, rdata); + + /* + * Output the data to the screen. + */ + for (i = 0; i < dims[0]; i++) + printf("%s[%d]: %s\n", ATTRIBUTE, i, rdata[i]); + + /* + * Close and release resources. Note that H5Dvlen_reclaim works + * for variable-length strings as well as variable-length arrays. + * Also note that we must still free the array of pointers stored + * in rdata, as H5Tvlen_reclaim only frees the data these point to. + */ + status = H5Dvlen_reclaim(memtype, space, H5P_DEFAULT, rdata); + free(rdata); + status = H5Aclose(attr); + status = H5Dclose(dset); + status = H5Sclose(space); + status = H5Tclose(filetype); + status = H5Tclose(memtype); + status = H5Fclose(file); + + return 0; +} |