[svn-r3196]

Purpose:
    Adding Tutorial to development branch (R 1.4)
Platforms tested:
    IE 5
This commit is contained in:
Frank Baker
2000-12-22 15:47:59 -05:00
parent d4c3773742
commit c1d537db2b
91 changed files with 8418 additions and 0 deletions

View File

@@ -0,0 +1,153 @@
/*
* This example shows how to create a compound data type,
* write an array which has the compound data type to the file,
* and read back fields' subsets.
*/
#include "hdf5.h"
#define FILE "SDScompound.h5"
#define DATASETNAME "ArrayOfStructures"
#define LENGTH 10
#define RANK 1
int
main(void)
{
/* First structure and dataset*/
typedef struct s1_t {
int a;
float b;
double c;
} s1_t;
s1_t s1[LENGTH];
hid_t s1_tid; /* File datatype identifier */
/* Second structure (subset of s1_t) and dataset*/
typedef struct s2_t {
double c;
int a;
} s2_t;
s2_t s2[LENGTH];
hid_t s2_tid; /* Memory datatype handle */
/* Third "structure" ( will be used to read float field of s1) */
hid_t s3_tid; /* Memory datatype handle */
float s3[LENGTH];
int i;
hid_t file, dataset, space; /* Handles */
herr_t status;
hsize_t dim[] = {LENGTH}; /* Dataspace dimensions */
/*
* Initialize the data
*/
for (i = 0; i< LENGTH; i++) {
s1[i].a = i;
s1[i].b = i*i;
s1[i].c = 1./(i+1);
}
/*
* Create the data space.
*/
space = H5Screate_simple(RANK, dim, NULL);
/*
* Create the file.
*/
file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/*
* Create the memory data type.
*/
s1_tid = H5Tcreate (H5T_COMPOUND, sizeof(s1_t));
H5Tinsert(s1_tid, "a_name", HOFFSET(s1_t, a), H5T_NATIVE_INT);
H5Tinsert(s1_tid, "c_name", HOFFSET(s1_t, c), H5T_NATIVE_DOUBLE);
H5Tinsert(s1_tid, "b_name", HOFFSET(s1_t, b), H5T_NATIVE_FLOAT);
/*
* Create the dataset.
*/
dataset = H5Dcreate(file, DATASETNAME, s1_tid, space, H5P_DEFAULT);
/*
* Wtite data to the dataset;
*/
status = H5Dwrite(dataset, s1_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, s1);
/*
* Release resources
*/
H5Tclose(s1_tid);
H5Sclose(space);
H5Dclose(dataset);
H5Fclose(file);
/*
* Open the file and the dataset.
*/
file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT);
dataset = H5Dopen(file, DATASETNAME);
/*
* Create a data type for s2
*/
s2_tid = H5Tcreate(H5T_COMPOUND, sizeof(s2_t));
H5Tinsert(s2_tid, "c_name", HOFFSET(s2_t, c), H5T_NATIVE_DOUBLE);
H5Tinsert(s2_tid, "a_name", HOFFSET(s2_t, a), H5T_NATIVE_INT);
/*
* Read two fields c and a from s1 dataset. Fields in the file
* are found by their names "c_name" and "a_name".
*/
status = H5Dread(dataset, s2_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, s2);
/*
* Display the fields
*/
printf("\n");
printf("Field c : \n");
for( i = 0; i < LENGTH; i++) printf("%.4f ", s2[i].c);
printf("\n");
printf("\n");
printf("Field a : \n");
for( i = 0; i < LENGTH; i++) printf("%d ", s2[i].a);
printf("\n");
/*
* Create a data type for s3.
*/
s3_tid = H5Tcreate(H5T_COMPOUND, sizeof(float));
status = H5Tinsert(s3_tid, "b_name", 0, H5T_NATIVE_FLOAT);
/*
* Read field b from s1 dataset. Field in the file is found by its name.
*/
status = H5Dread(dataset, s3_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, s3);
/*
* Display the field
*/
printf("\n");
printf("Field b : \n");
for( i = 0; i < LENGTH; i++) printf("%.4f ", s3[i]);
printf("\n");
/*
* Release resources
*/
H5Tclose(s2_tid);
H5Tclose(s3_tid);
H5Dclose(dataset);
H5Fclose(file);
return 0;
}

View File

@@ -0,0 +1,148 @@
/***********************************************************************/
/* */
/* PROGRAM: h5_copy.c */
/* PURPOSE: Shows how to use the H5SCOPY function. */
/* DESCRIPTION: */
/* This program creates two files, copy1.h5, and copy2.h5. */
/* In copy1.h5, it creates a 3x4 dataset called 'Copy1', */
/* and write 0's to this dataset. */
/* In copy2.h5, it create a 3x4 dataset called 'Copy2', */
/* and write 1's to this dataset. */
/* It closes both files, reopens both files, selects two */
/* points in copy1.h5 and writes values to them. Then it */
/* does an H5Scopy from the first file to the second, and */
/* writes the values to copy2.h5. It then closes the */
/* files, reopens them, and prints the contents of the */
/* two datasets. */
/* */
/***********************************************************************/
#include "hdf5.h"
#define FILE1 "copy1.h5"
#define FILE2 "copy2.h5"
#define RANK 2
#define DIM1 3
#define DIM2 4
#define NUMP 2
int main (void)
{
hid_t file1, file2, dataset1, dataset2;
hid_t mid1, mid2, fid1, fid2;
hsize_t fdim[] = {DIM1, DIM2};
hsize_t mdim[] = {DIM1, DIM2};
hsize_t start[2], stride[2], count[2], block[2];
int buf1[DIM1][DIM2];
int buf2[DIM1][DIM2];
int bufnew[DIM1][DIM2];
int val[] = {53, 59};
hsize_t marray[] = {2};
hssize_t coord[NUMP][RANK];
herr_t ret;
uint i, j;
/***********************************************************************/
/* */
/* Create two files containing identical datasets. Write 0's to one */
/* and 1's to the other. */
/* */
/***********************************************************************/
for ( i = 0; i < DIM1; i++ )
for ( j = 0; j < DIM2; j++ )
buf1[i][j] = 0;
for ( i = 0; i < DIM1; i++ )
for ( j = 0; j < DIM2; j++ )
buf2[i][j] = 1;
file1 = H5Fcreate(FILE1, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
file2 = H5Fcreate(FILE2, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
fid1 = H5Screate_simple (RANK, fdim, NULL);
fid2 = H5Screate_simple (RANK, fdim, NULL);
dataset1 = H5Dcreate (file1, "Copy1", H5T_NATIVE_INT, fid1, H5P_DEFAULT);
dataset2 = H5Dcreate (file2, "Copy2", H5T_NATIVE_INT, fid2, H5P_DEFAULT);
ret = H5Dwrite(dataset1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf1);
ret = H5Dwrite(dataset2, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf2);
ret = H5Dclose (dataset1);
ret = H5Dclose (dataset2);
ret = H5Sclose (fid1);
ret = H5Sclose (fid2);
ret = H5Fclose (file1);
ret = H5Fclose (file2);
/***********************************************************************/
/* */
/* Open the two files. Select two points in one file, write values to */
/* those point locations, then do H5Scopy and write the values to the */
/* other file. Close files. */
/* */
/***********************************************************************/
file1 = H5Fopen (FILE1, H5F_ACC_RDWR, H5P_DEFAULT);
file2 = H5Fopen (FILE2, H5F_ACC_RDWR, H5P_DEFAULT);
dataset1 = H5Dopen (file1, "Copy1");
dataset2 = H5Dopen (file2, "Copy2");
fid1 = H5Dget_space (dataset1);
mid1 = H5Screate_simple(1, marray, NULL);
coord[0][0] = 0; coord[0][1] = 3;
coord[1][0] = 0; coord[1][1] = 1;
ret = H5Sselect_elements (fid1, H5S_SELECT_SET, NUMP, (const hssize_t **)coord);
ret = H5Dwrite (dataset1, H5T_NATIVE_INT, mid1, fid1, H5P_DEFAULT, val);
fid2 = H5Scopy (fid1);
ret = H5Dwrite (dataset2, H5T_NATIVE_INT, mid1, fid2, H5P_DEFAULT, val);
ret = H5Dclose (dataset1);
ret = H5Dclose (dataset2);
ret = H5Sclose (fid1);
ret = H5Sclose (fid2);
ret = H5Fclose (file1);
ret = H5Fclose (file2);
ret = H5Sclose (mid1);
/***********************************************************************/
/* */
/* Open both files and print the contents of the datasets. */
/* */
/***********************************************************************/
file1 = H5Fopen (FILE1, H5F_ACC_RDWR, H5P_DEFAULT);
file2 = H5Fopen (FILE2, H5F_ACC_RDWR, H5P_DEFAULT);
dataset1 = H5Dopen (file1, "Copy1");
dataset2 = H5Dopen (file2, "Copy2");
ret = H5Dread (dataset1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
H5P_DEFAULT, bufnew);
printf ("\nDataset 'Copy1' in file 'copy1.h5' contains: \n");
for (i=0;i<DIM1; i++) {
for (j=0;j<DIM2;j++) printf ("%3d ", bufnew[i][j]);
printf("\n");
}
printf ("\nDataset 'Copy2' in file 'copy2.h5' contains: \n");
ret = H5Dread (dataset2, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
H5P_DEFAULT, bufnew);
for (i=0;i<DIM1; i++) {
for (j=0;j<DIM2;j++) printf ("%3d ", bufnew[i][j]);
printf("\n");
}
ret = H5Dclose (dataset1);
ret = H5Dclose (dataset2);
ret = H5Fclose (file1);
ret = H5Fclose (file2);
}

View File

@@ -0,0 +1,46 @@
/*
* Creating a dataset attribute.
*/
#include <hdf5.h>
#define FILE "dset.h5"
main() {
hid_t file_id, dataset_id, attribute_id, dataspace_id; /* identifiers */
hsize_t dims;
int attr_data[2];
herr_t status;
/* Initialize the attribute data. */
attr_data[0] = 100;
attr_data[1] = 200;
/* Open an existing file. */
file_id = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT);
/* Open an existing dataset. */
dataset_id = H5Dopen(file_id, "/dset");
/* Create the data space for the attribute. */
dims = 2;
dataspace_id = H5Screate_simple(1, &dims, NULL);
/* Create a dataset attribute. */
attribute_id = H5Acreate(dataset_id, "attr", H5T_STD_I32BE, dataspace_id, H5P_DEFAULT);
/* Write the attribute data. */
status = H5Awrite(attribute_id, H5T_NATIVE_INT, attr_data);
/* Close the attribute. */
status = H5Aclose(attribute_id);
/* Close the dataspace. */
status = H5Sclose(dataspace_id);
/* Close to the dataset. */
status = H5Dclose(dataset_id);
/* Close the file. */
status = H5Fclose(file_id);
}

View File

@@ -0,0 +1,34 @@
/*
* Creating and closing a dataset.
*/
#include <hdf5.h>
#define FILE "dset.h5"
main() {
hid_t file_id, dataset_id, dataspace_id; /* identifiers */
hsize_t dims[2];
herr_t status;
/* Create a new file using default properties. */
file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/* Create the data space for the dataset. */
dims[0] = 4;
dims[1] = 6;
dataspace_id = H5Screate_simple(2, dims, NULL);
/* Create the dataset. */
dataset_id = H5Dcreate(file_id, "/dset", H5T_STD_I32BE, dataspace_id, H5P_DEFAULT);
/* End access to the dataset and release resources used by it. */
status = H5Dclose(dataset_id);
/* Terminate access to the data space. */
status = H5Sclose(dataspace_id);
/* Close the file. */
status = H5Fclose(file_id);
}

View File

@@ -0,0 +1,19 @@
/*
* Creating and closing a file.
*/
#include <hdf5.h>
#define FILE "file.h5"
main() {
hid_t file_id; /* file identifier */
herr_t status;
/* Create a new file using default properties. */
file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/* Terminate access to the file. */
status = H5Fclose(file_id);
}

View File

@@ -0,0 +1,24 @@
/*
* Creating and closing a group.
*/
#include <hdf5.h>
#define FILE "group.h5"
main() {
hid_t file_id, group_id; /* identifiers */
herr_t status;
/* Create a new file using default properties. */
file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/* Create a group named "/MyGroup" in the file. */
group_id = H5Gcreate(file_id, "/MyGroup", 0);
/* Close the group. */
status = H5Gclose(group_id);
/* Terminate access to the file. */
status = H5Fclose(file_id);
}

View File

@@ -0,0 +1,32 @@
/*
* Creating groups using absolute and relative names.
*/
#include <hdf5.h>
#define FILE "groups.h5"
main() {
hid_t file_id, group1_id, group2_id, group3_id; /* identifiers */
herr_t status;
/* Create a new file using default properties. */
file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/* Create group "MyGroup" in the root group using absolute name. */
group1_id = H5Gcreate(file_id, "/MyGroup", 0);
/* Create group "Group_A" in group "MyGroup" using absolute name. */
group2_id = H5Gcreate(file_id, "/MyGroup/Group_A", 0);
/* Create group "Group_B" in group "MyGroup" using relative name. */
group3_id = H5Gcreate(group1_id, "Group_B", 0);
/* Close groups. */
status = H5Gclose(group1_id);
status = H5Gclose(group2_id);
status = H5Gclose(group3_id);
/* Close the file. */
status = H5Fclose(file_id);
}

View File

@@ -0,0 +1,74 @@
/*
* Create two datasets within groups.
*/
#include <hdf5.h>
#define FILE "groups.h5"
main() {
hid_t file_id, group_id, dataset_id, dataspace_id; /* identifiers */
hsize_t dims[2];
herr_t status;
int i, j, dset1_data[3][3], dset2_data[2][10];
/* Initialize the first dataset. */
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
dset1_data[i][j] = j + 1;
/* Initialize the second dataset. */
for (i = 0; i < 2; i++)
for (j = 0; j < 10; j++)
dset2_data[i][j] = j + 1;
/* Open an existing file. */
file_id = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT);
/* Create the data space for the first dataset. */
dims[0] = 3;
dims[1] = 3;
dataspace_id = H5Screate_simple(2, dims, NULL);
/* Create a dataset in group "MyGroup". */
dataset_id = H5Dcreate(file_id, "/MyGroup/dset1", H5T_STD_I32BE, dataspace_id,
H5P_DEFAULT);
/* Write the first dataset. */
status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
dset1_data);
/* Close the data space for the first dataset. */
status = H5Sclose(dataspace_id);
/* Close the first dataset. */
status = H5Dclose(dataset_id);
/* Open an existing group of the specified file. */
group_id = H5Gopen(file_id, "/MyGroup/Group_A");
/* Create the data space for the second dataset. */
dims[0] = 2;
dims[1] = 10;
dataspace_id = H5Screate_simple(2, dims, NULL);
/* Create the second dataset in group "Group_A". */
dataset_id = H5Dcreate(group_id, "dset2", H5T_STD_I32BE, dataspace_id, H5P_DEFAULT);
/* Write the second dataset. */
status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
dset2_data);
/* Close the data space for the second dataset. */
status = H5Sclose(dataspace_id);
/* Close the second dataset */
status = H5Dclose(dataset_id);
/* Close the group. */
status = H5Gclose(group_id);
/* Close the file. */
status = H5Fclose(file_id);
}

View File

@@ -0,0 +1,141 @@
/**************************************************************
*
* This example shows how to work with extendible datasets.
* In the current version of the library a dataset MUST be
* chunked in order to be extendible.
*
* This example is derived from the h5_extend_write.c and
* h5_read_chunk.c examples that are in the "Introduction
* to HDF5".
*
*************************************************************/
#include "hdf5.h"
#define FILE "ext.h5"
#define DATASETNAME "ExtendibleArray"
#define RANK 2
int
main (void)
{
hid_t file; /* handles */
hid_t dataspace, dataset;
hid_t filespace;
hid_t cparms;
hid_t memspace;
hsize_t dims[2] = { 3, 3}; /* dataset dimensions
at creation time */
hsize_t dims1[2] = { 3, 3}; /* data1 dimensions */
hsize_t dims2[2] = { 7, 1}; /* data2 dimensions */
hsize_t maxdims[2] = {H5S_UNLIMITED, H5S_UNLIMITED};
hsize_t size[2];
hssize_t offset[2];
hsize_t i,j;
herr_t status, status_n;
int data1[3][3] = { {1, 1, 1}, /* data to write */
{1, 1, 1},
{1, 1, 1} };
int data2[7] = { 2, 2, 2, 2, 2, 2, 2};
/* Variables used in reading data back */
hsize_t chunk_dims[2] ={2, 5};
hsize_t chunk_dimsr[2];
hsize_t dimsr[2];
int data_out[10][3];
int rank, rank_chunk;
/* Create the data space with unlimited dimensions. */
dataspace = H5Screate_simple (RANK, dims, maxdims);
/* Create a new file. If file exists its contents will be overwritten. */
file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/* Modify dataset creation properties, i.e. enable chunking */
cparms = H5Pcreate (H5P_DATASET_CREATE);
status = H5Pset_chunk ( cparms, RANK, chunk_dims);
/* Create a new dataset within the file using cparms
creation properties. */
dataset = H5Dcreate (file, DATASETNAME, H5T_NATIVE_INT, dataspace,
cparms);
/* Extend the dataset. This call assures that dataset is 3 x 3.*/
size[0] = 3;
size[1] = 3;
status = H5Dextend (dataset, size);
/* Select a hyperslab */
filespace = H5Dget_space (dataset);
offset[0] = 0;
offset[1] = 0;
status = H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL,
dims1, NULL);
/* Write the data to the hyperslab */
status = H5Dwrite (dataset, H5T_NATIVE_INT, dataspace, filespace,
H5P_DEFAULT, data1);
/* Extend the dataset. Dataset becomes 10 x 3 */
dims[0] = dims1[0] + dims2[0];
size[0] = dims[0];
size[1] = dims[1];
status = H5Dextend (dataset, size);
/* Select a hyperslab */
filespace = H5Dget_space (dataset);
offset[0] = 3;
offset[1] = 0;
status = H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL,
dims2, NULL);
/* Define memory space */
dataspace = H5Screate_simple (RANK, dims2, NULL);
/* Write the data to the hyperslab */
status = H5Dwrite (dataset, H5T_NATIVE_INT, dataspace, filespace,
H5P_DEFAULT, data2);
/* Close resources */
status = H5Dclose (dataset);
status = H5Sclose (dataspace);
status = H5Sclose (filespace);
status = H5Fclose (file);
/****************************************************************
Read the data back
***************************************************************/
file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT);
dataset = H5Dopen (file, DATASETNAME);
filespace = H5Dget_space (dataset);
rank = H5Sget_simple_extent_ndims (filespace);
status_n = H5Sget_simple_extent_dims (filespace, dimsr, NULL);
cparms = H5Dget_create_plist (dataset);
if (H5D_CHUNKED == H5Pget_layout (cparms))
{
rank_chunk = H5Pget_chunk (cparms, 2, chunk_dimsr);
}
memspace = H5Screate_simple (rank,dimsr,NULL);
status = H5Dread (dataset, H5T_NATIVE_INT, memspace, filespace,
H5P_DEFAULT, data_out);
printf("\n");
printf("Dataset: \n");
for (j = 0; j < dimsr[0]; j++)
{
for (i = 0; i < dimsr[1]; i++)
printf("%d ", data_out[j][i]);
printf("\n");
}
status = H5Pclose (cparms);
status = H5Dclose (dataset);
status = H5Sclose (filespace);
status = H5Sclose (memspace);
status = H5Fclose (file);
}

View File

@@ -0,0 +1,192 @@
/************************************************************
This example shows how to write and read a hyperslab. It
is derived from the h5_read.c and h5_write.c examples in
the "Introduction to HDF5".
************************************************************/
#include "hdf5.h"
#define FILE "sds.h5"
#define DATASETNAME "IntArray"
#define NX_SUB 3 /* hyperslab dimensions */
#define NY_SUB 4
#define NX 7 /* output buffer dimensions */
#define NY 7
#define NZ 3
#define RANK 2
#define RANK_OUT 3
#define X 5 /* dataset dimensions */
#define Y 6
int
main (void)
{
hsize_t dimsf[2]; /* dataset dimensions */
int data[X][Y]; /* data to write */
/*
* Data and output buffer initialization.
*/
hid_t file, dataset; /* handles */
hid_t dataspace;
hid_t memspace;
hsize_t dimsm[3]; /* memory space dimensions */
hsize_t dims_out[2]; /* dataset dimensions */
herr_t status;
int data_out[NX][NY][NZ ]; /* output buffer */
hsize_t count[2]; /* size of the hyperslab in the file */
hssize_t offset[2]; /* hyperslab offset in the file */
hsize_t count_out[3]; /* size of the hyperslab in memory */
hssize_t offset_out[3]; /* hyperslab offset in memory */
int i, j, k, status_n, rank;
/*********************************************************
This writes data to the HDF5 file.
*********************************************************/
/*
* Data and output buffer initialization.
*/
for (j = 0; j < X; j++) {
for (i = 0; i < Y; i++)
data[j][i] = i + j;
}
/*
* 0 1 2 3 4 5
* 1 2 3 4 5 6
* 2 3 4 5 6 7
* 3 4 5 6 7 8
* 4 5 6 7 8 9
*/
/*
* Create a new file using H5F_ACC_TRUNC access,
* the default file creation properties, and the default file
* access properties.
*/
file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/*
* Describe the size of the array and create the data space for fixed
* size dataset.
*/
dimsf[0] = X;
dimsf[1] = Y;
dataspace = H5Screate_simple (RANK, dimsf, NULL);
/*
* Create a new dataset within the file using defined dataspace and
* default dataset creation properties.
*/
dataset = H5Dcreate (file, DATASETNAME, H5T_STD_I32BE, dataspace,
H5P_DEFAULT);
/*
* Write the data to the dataset using default transfer properties.
*/
status = H5Dwrite (dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
H5P_DEFAULT, data);
/*
* Close/release resources.
*/
H5Sclose (dataspace);
H5Dclose (dataset);
H5Fclose (file);
/*************************************************************
This reads the hyperslab from the sds.h5 file just
created, into a 2-dimensional plane of the 3-dimensional
array.
************************************************************/
for (j = 0; j < NX; j++) {
for (i = 0; i < NY; i++) {
for (k = 0; k < NZ ; k++)
data_out[j][i][k] = 0;
}
}
/*
* Open the file and the dataset.
*/
file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT);
dataset = H5Dopen (file, DATASETNAME);
dataspace = H5Dget_space (dataset); /* dataspace handle */
rank = H5Sget_simple_extent_ndims (dataspace);
status_n = H5Sget_simple_extent_dims (dataspace, dims_out, NULL);
printf("\nRank: %d\nDimensions: %lu x %lu \n", rank,
(unsigned long)(dims_out[0]), (unsigned long)(dims_out[1]));
/*
* Define hyperslab in the dataset.
*/
offset[0] = 1;
offset[1] = 2;
count[0] = NX_SUB;
count[1] = NY_SUB;
status = H5Sselect_hyperslab (dataspace, H5S_SELECT_SET, offset, NULL,
count, NULL);
/*
* Define the memory dataspace.
*/
dimsm[0] = NX;
dimsm[1] = NY;
dimsm[2] = NZ;
memspace = H5Screate_simple (RANK_OUT, dimsm, NULL);
/*
* Define memory hyperslab.
*/
offset_out[0] = 3;
offset_out[1] = 0;
offset_out[2] = 0;
count_out[0] = NX_SUB;
count_out[1] = NY_SUB;
count_out[2] = 1;
status = H5Sselect_hyperslab (memspace, H5S_SELECT_SET, offset_out, NULL,
count_out, NULL);
/*
* Read data from hyperslab in the file into the hyperslab in
* memory and display.
*/
status = H5Dread (dataset, H5T_NATIVE_INT, memspace, dataspace,
H5P_DEFAULT, data_out);
printf ("Data:\n ");
for (j = 0; j < NX; j++) {
for (i = 0; i < NY; i++) printf("%d ", data_out[j][i][0]);
printf("\n ");
}
printf("\n");
/*
* 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0
* 3 4 5 6 0 0 0
* 4 5 6 7 0 0 0
* 5 6 7 8 0 0 0
* 0 0 0 0 0 0 0
*/
/*
* Close and release resources.
*/
H5Dclose (dataset);
H5Sclose (dataspace);
H5Sclose (memspace);
H5Fclose (file);
}

View File

@@ -0,0 +1,111 @@
#include <hdf5.h>
#define FILE "iterate.h5"
#define FALSE 0
/* 1-D dataset with fixed dimensions */
#define SPACE1_NAME "Space1"
#define SPACE1_RANK 1
#define SPACE1_DIM1 4
herr_t file_info(hid_t loc_id, const char *name, void *opdata);
/* Operator function */
int
main(void) {
hid_t file; /* HDF5 File IDs */
hid_t dataset; /* Dataset ID */
hid_t group; /* Group ID */
hid_t sid; /* Dataspace ID */
hid_t tid; /* Datatype ID */
hsize_t dims[] = {SPACE1_DIM1};
herr_t ret; /* Generic return value */
/* Compound datatype */
typedef struct s1_t {
unsigned int a;
unsigned int b;
float c;
} s1_t;
/* Create file */
file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/* Create dataspace for datasets */
sid = H5Screate_simple(SPACE1_RANK, dims, NULL);
/* Create a group */
group=H5Gcreate(file,"Group1",-1);
/* Close a group */
ret = H5Gclose(group);
/* Create a dataset */
dataset=H5Dcreate(file,"Dataset1",H5T_STD_U32LE,sid,H5P_DEFAULT);
/* Close Dataset */
ret = H5Dclose(dataset);
/* Create a datatype */
tid = H5Tcreate (H5T_COMPOUND, sizeof(s1_t));
/* Insert fields */
ret=H5Tinsert (tid, "a", HOFFSET(s1_t,a), H5T_NATIVE_INT);
ret=H5Tinsert (tid, "b", HOFFSET(s1_t,b), H5T_NATIVE_INT);
ret=H5Tinsert (tid, "c", HOFFSET(s1_t,c), H5T_NATIVE_FLOAT);
/* Save datatype for later */
ret=H5Tcommit (file, "Datatype1", tid);
/* Close datatype */
ret = H5Tclose(tid);
/* Iterate through the file to see members of the root group */
printf(" Objects in the root group are:\n");
printf("\n");
H5Giterate(file, "/", NULL, file_info, NULL);
/* Close file */
ret = H5Fclose(file);
return 0;
}
/*
* Operator function.
*/
herr_t file_info(hid_t loc_id, const char *name, void *opdata)
{
H5G_stat_t statbuf;
/*
* Get type of the object and display its name and type.
* The name of the object is passed to this function by
* the Library. Some magic :-)
*/
H5Gget_objinfo(loc_id, name, FALSE, &statbuf);
switch (statbuf.type) {
case H5G_GROUP:
printf(" Object with name %s is a group \n", name);
break;
case H5G_DATASET:
printf(" Object with name %s is a dataset \n", name);
break;
case H5G_TYPE:
printf(" Object with name %s is a named datatype \n", name);
break;
default:
printf(" Unable to identify an object ");
}
return 0;
}

View File

@@ -0,0 +1,119 @@
/*
* This program shows the concept of "mounting files".
* Program creates one file with group G in it, and another
* file with dataset D. Then second file is mounted in the first one
* under the "mounting point" G. Dataset D is accessed in the first file
* under name /G/D and data is printed out.
*/
#include<hdf5.h>
#define FILE1 "mount1.h5"
#define FILE2 "mount2.h5"
#define RANK 2
#define NX 4
#define NY 5
int main(void)
{
hid_t fid1, fid2, gid; /* Files and group identifiers */
hid_t did, tid, sid; /* Dataset and datatype identifiers */
herr_t status;
hsize_t dims[] = {NX,NY}; /* Dataset dimensions */
int i, j;
int bm[NX][NY], bm_out[NX][NY]; /* Data buffers */
/*
* Initialization of buffer matrix "bm"
*/
for(i =0; i<NX; i++) {
for(j = 0; j<NY; j++)
bm[i][j] = i + j;
}
/*
* Create first file and a group in it.
*/
fid1 = H5Fcreate(FILE1, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
gid = H5Gcreate(fid1, "/G", 0);
/*
* Close group and file
*/
H5Gclose(gid);
H5Fclose(fid1);
/*
* Create second file and dataset "D" in it.
*/
fid2 = H5Fcreate(FILE2, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
dims[0] = NX;
dims[1] = NY;
sid = H5Screate_simple(RANK, dims, NULL);
did = H5Dcreate(fid2, "D", H5T_NATIVE_INT, sid, H5P_DEFAULT);
/*
* Write data to the dataset.
*/
status = H5Dwrite(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, bm);
/*
* Close all identifiers.
*/
H5Sclose(sid);
H5Dclose(did);
H5Fclose(fid2);
/*
* Reopen both files
*/
fid1 = H5Fopen(FILE1, H5F_ACC_RDONLY, H5P_DEFAULT);
fid2 = H5Fopen(FILE2, H5F_ACC_RDONLY, H5P_DEFAULT);
/*
* Mount second file under G in the first file.
*/
H5Fmount(fid1, "/G", fid2, H5P_DEFAULT);
/*
* Access dataset D in the first file under /G/D name.
*/
did = H5Dopen(fid1,"/G/D");
tid = H5Dget_type(did);
status = H5Dread(did, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, bm_out);
/*
* Print out the data.
*/
for(i=0; i<NX; i++){
for(j=0; j<NY; j++)
printf(" %d", bm_out[i][j]);
printf("\n");
}
/*
* Close all identifers
*/
H5Tclose(tid);
H5Dclose(did);
/*
* Unmounting second file
*/
H5Funmount(fid1, "/G");
/*
* Close both files
*/
H5Fclose(fid1);
H5Fclose(fid2);
return 0;
}

View File

@@ -0,0 +1,37 @@
/*
* Writing and reading an existing dataset.
*/
#include <hdf5.h>
#define FILE "dset.h5"
main() {
hid_t file_id, dataset_id; /* identifiers */
herr_t status;
int i, j, dset_data[4][6];
/* Initialize the dataset. */
for (i = 0; i < 4; i++)
for (j = 0; j < 6; j++)
dset_data[i][j] = i * 6 + j + 1;
/* Open an existing file. */
file_id = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT);
/* Open an existing dataset. */
dataset_id = H5Dopen(file_id, "/dset");
/* Write the dataset. */
status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
dset_data);
status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
dset_data);
/* Close the dataset. */
status = H5Dclose(dataset_id);
/* Close the file. */
status = H5Fclose(file_id);
}

View File

@@ -0,0 +1,136 @@
/*
* This example reads hyperslab from the SDS.h5 file
* created by h5_write.c program into two-dimensional
* plane of the three-dimensional array.
* Information about dataset in the SDS.h5 file is obtained.
*/
#include "hdf5.h"
#define FILE "SDS.h5"
#define DATASETNAME "IntArray"
#define NX_SUB 3 /* hyperslab dimensions */
#define NY_SUB 4
#define NX 7 /* output buffer dimensions */
#define NY 7
#define NZ 3
#define RANK 2
#define RANK_OUT 3
int
main (void)
{
hid_t file, dataset; /* handles */
hid_t datatype, dataspace;
hid_t memspace;
H5T_class_t class; /* data type class */
H5T_order_t order; /* data order */
size_t size; /*
* size of the data element
* stored in file
*/
hsize_t dimsm[3]; /* memory space dimensions */
hsize_t dims_out[2]; /* dataset dimensions */
herr_t status;
int data_out[NX][NY][NZ ]; /* output buffer */
hsize_t count[2]; /* size of the hyperslab in the file */
hssize_t offset[2]; /* hyperslab offset in the file */
hsize_t count_out[3]; /* size of the hyperslab in memory */
hssize_t offset_out[3]; /* hyperslab offset in memory */
int i, j, k, status_n, rank;
for (j = 0; j < NX; j++) {
for (i = 0; i < NY; i++) {
for (k = 0; k < NZ ; k++)
data_out[j][i][k] = 0;
}
}
/*
* Open the file and the dataset.
*/
file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT);
dataset = H5Dopen(file, DATASETNAME);
/*
* Get datatype and dataspace handles and then query
* dataset class, order, size, rank and dimensions.
*/
datatype = H5Dget_type(dataset); /* datatype handle */
class = H5Tget_class(datatype);
if (class == H5T_INTEGER) printf("Data set has INTEGER type \n");
order = H5Tget_order(datatype);
if (order == H5T_ORDER_LE) printf("Little endian order \n");
size = H5Tget_size(datatype);
printf(" Data size is %d \n", size);
dataspace = H5Dget_space(dataset); /* dataspace handle */
rank = H5Sget_simple_extent_ndims(dataspace);
status_n = H5Sget_simple_extent_dims(dataspace, dims_out, NULL);
printf("rank %d, dimensions %lu x %lu \n", rank,
(unsigned long)(dims_out[0]), (unsigned long)(dims_out[1]));
/*
* Define hyperslab in the dataset.
*/
offset[0] = 1;
offset[1] = 2;
count[0] = NX_SUB;
count[1] = NY_SUB;
status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL,
count, NULL);
/*
* Define the memory dataspace.
*/
dimsm[0] = NX;
dimsm[1] = NY;
dimsm[2] = NZ ;
memspace = H5Screate_simple(RANK_OUT,dimsm,NULL);
/*
* Define memory hyperslab.
*/
offset_out[0] = 3;
offset_out[1] = 0;
offset_out[2] = 0;
count_out[0] = NX_SUB;
count_out[1] = NY_SUB;
count_out[2] = 1;
status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, offset_out, NULL,
count_out, NULL);
/*
* Read data from hyperslab in the file into the hyperslab in
* memory and display.
*/
status = H5Dread(dataset, H5T_NATIVE_INT, memspace, dataspace,
H5P_DEFAULT, data_out);
for (j = 0; j < NX; j++) {
for (i = 0; i < NY; i++) printf("%d ", data_out[j][i][0]);
printf("\n");
}
/*
* 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0
* 3 4 5 6 0 0 0
* 4 5 6 7 0 0 0
* 5 6 7 8 0 0 0
* 0 0 0 0 0 0 0
*/
/*
* Close/release resources.
*/
H5Tclose(datatype);
H5Dclose(dataset);
H5Sclose(dataspace);
H5Sclose(memspace);
H5Fclose(file);
return 0;
}

View File

@@ -0,0 +1,93 @@
#include <stdlib.h>
#include <hdf5.h>
#define FILE1 "trefer1.h5"
/* dataset with fixed dimensions */
#define SPACE1_NAME "Space1"
#define SPACE1_RANK 1
#define SPACE1_DIM1 4
int
main(void)
{
hid_t fid1; /* HDF5 File IDs */
hid_t dataset, /* Dataset ID */
dset2; /* Dereferenced dataset ID */
hid_t group; /* Group ID */
hid_t sid1; /* Dataspace ID */
hid_t tid1; /* Datatype ID */
hobj_ref_t *rbuf; /* buffer to read from disk */
int *tu32; /* temp. buffer read from disk */
int i; /* counting variables */
char read_comment[10];
herr_t ret; /* Generic return value */
/* Allocate read buffers */
rbuf = malloc(sizeof(hobj_ref_t)*SPACE1_DIM1);
tu32 = malloc(sizeof(int)*SPACE1_DIM1);
/* Open the file */
fid1 = H5Fopen(FILE1, H5F_ACC_RDWR, H5P_DEFAULT);
/* Open the dataset */
dataset=H5Dopen(fid1,"/Dataset3");
/* Read selection from disk */
ret=H5Dread(dataset,H5T_STD_REF_OBJ,H5S_ALL,H5S_ALL,H5P_DEFAULT,rbuf);
/* Open dataset object */
dset2 = H5Rdereference(dataset,H5R_OBJECT,&rbuf[0]);
/* Check information in referenced dataset */
sid1 = H5Dget_space(dset2);
ret=H5Sget_simple_extent_npoints(sid1);
/* Read from disk */
ret=H5Dread(dset2,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,tu32);
printf("Dataset data : \n");
for (i=0; i < SPACE1_DIM1 ; i++) printf (" %d ", tu32[i]);
printf("\n");
printf("\n");
/* Close dereferenced Dataset */
ret = H5Dclose(dset2);
/* Open group object */
group = H5Rdereference(dataset,H5R_OBJECT,&rbuf[2]);
/* Get group's comment */
ret=H5Gget_comment(group,".",10,read_comment);
printf("Group comment is %s \n", read_comment);
printf(" \n");
/* Close group */
ret = H5Gclose(group);
/* Open datatype object */
tid1 = H5Rdereference(dataset,H5R_OBJECT,&rbuf[3]);
/* Verify correct datatype */
{
H5T_class_t tclass;
tclass= H5Tget_class(tid1);
if ((tclass == H5T_COMPOUND))
printf ("Number of compound datatype members is %d \n", H5Tget_nmembers(tid1));
printf(" \n");
}
/* Close datatype */
ret = H5Tclose(tid1);
/* Close Dataset */
ret = H5Dclose(dataset);
/* Close file */
ret = H5Fclose(fid1);
/* Free memory buffers */
free(rbuf);
free(tu32);
return 0;
}

View File

@@ -0,0 +1,120 @@
#include <hdf5.h>
#define FILE1 "trefer1.h5"
/* 1-D dataset with fixed dimensions */
#define SPACE1_NAME "Space1"
#define SPACE1_RANK 1
#define SPACE1_DIM1 4
/* 2-D dataset with fixed dimensions */
#define SPACE2_NAME "Space2"
#define SPACE2_RANK 2
#define SPACE2_DIM1 10
#define SPACE2_DIM2 10
int
main(void) {
hid_t fid1; /* HDF5 File IDs */
hid_t dataset; /* Dataset ID */
hid_t group; /* Group ID */
hid_t sid1; /* Dataspace ID */
hid_t tid1; /* Datatype ID */
hsize_t dims1[] = {SPACE1_DIM1};
hobj_ref_t *wbuf; /* buffer to write to disk */
int *tu32; /* Temporary pointer to int data */
int i; /* counting variables */
const char *write_comment="Foo!"; /* Comments for group */
herr_t ret; /* Generic return value */
/* Compound datatype */
typedef struct s1_t {
unsigned int a;
unsigned int b;
float c;
} s1_t;
/* Allocate write buffers */
wbuf=(hobj_ref_t *)malloc(sizeof(hobj_ref_t)*SPACE1_DIM1);
tu32=malloc(sizeof(int)*SPACE1_DIM1);
/* Create file */
fid1 = H5Fcreate(FILE1, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/* Create dataspace for datasets */
sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL);
/* Create a group */
group=H5Gcreate(fid1,"Group1",-1);
/* Set group's comment */
ret=H5Gset_comment(group,".",write_comment);
/* Create a dataset (inside Group1) */
dataset=H5Dcreate(group,"Dataset1",H5T_STD_U32LE,sid1,H5P_DEFAULT);
for(i=0; i<SPACE1_DIM1; i++)
tu32[i] = i*3;
/* Write selection to disk */
ret=H5Dwrite(dataset,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,tu32);
/* Close Dataset */
ret = H5Dclose(dataset);
/* Create another dataset (inside Group1) */
dataset=H5Dcreate(group,"Dataset2",H5T_NATIVE_UCHAR,sid1,H5P_DEFAULT);
/* Close Dataset */
ret = H5Dclose(dataset);
/* Create a datatype to refer to */
tid1 = H5Tcreate (H5T_COMPOUND, sizeof(s1_t));
/* Insert fields */
ret=H5Tinsert (tid1, "a", HOFFSET(s1_t,a), H5T_NATIVE_INT);
ret=H5Tinsert (tid1, "b", HOFFSET(s1_t,b), H5T_NATIVE_INT);
ret=H5Tinsert (tid1, "c", HOFFSET(s1_t,c), H5T_NATIVE_FLOAT);
/* Save datatype for later */
ret=H5Tcommit (group, "Datatype1", tid1);
/* Close datatype */
ret = H5Tclose(tid1);
/* Close group */
ret = H5Gclose(group);
/* Create a dataset to store references */
dataset=H5Dcreate(fid1,"Dataset3",H5T_STD_REF_OBJ,sid1,H5P_DEFAULT);
/* Create reference to dataset */
ret = H5Rcreate(&wbuf[0],fid1,"/Group1/Dataset1",H5R_OBJECT,-1);
/* Create reference to dataset */
ret = H5Rcreate(&wbuf[1],fid1,"/Group1/Dataset2",H5R_OBJECT,-1);
/* Create reference to group */
ret = H5Rcreate(&wbuf[2],fid1,"/Group1",H5R_OBJECT,-1);
/* Create reference to named datatype */
ret = H5Rcreate(&wbuf[3],fid1,"/Group1/Datatype1",H5R_OBJECT,-1);
/* Write selection to disk */
ret=H5Dwrite(dataset,H5T_STD_REF_OBJ,H5S_ALL,H5S_ALL,H5P_DEFAULT,wbuf);
/* Close disk dataspace */
ret = H5Sclose(sid1);
/* Close Dataset */
ret = H5Dclose(dataset);
/* Close file */
ret = H5Fclose(fid1);
free(wbuf);
free(tu32);
return 0;
}

View File

@@ -0,0 +1,119 @@
#include <stdlib.h>
#include <hdf5.h>
#define FILE2 "trefer2.h5"
#define NPOINTS 10
/* 1-D dataset with fixed dimensions */
#define SPACE1_NAME "Space1"
#define SPACE1_RANK 1
#define SPACE1_DIM1 4
/* 2-D dataset with fixed dimensions */
#define SPACE2_NAME "Space2"
#define SPACE2_RANK 2
#define SPACE2_DIM1 10
#define SPACE2_DIM2 10
int
main(void)
{
hid_t fid1; /* HDF5 File IDs */
hid_t dset1, /* Dataset ID */
dset2; /* Dereferenced dataset ID */
hid_t sid1, /* Dataspace ID #1 */
sid2; /* Dataspace ID #2 */
hsize_t * coords; /* Coordinate buffer */
hsize_t low[SPACE2_RANK]; /* Selection bounds */
hsize_t high[SPACE2_RANK]; /* Selection bounds */
hdset_reg_ref_t *rbuf; /* buffer to to read disk */
int *drbuf; /* Buffer for reading numeric data from disk */
int i, j; /* counting variables */
herr_t ret; /* Generic return value */
/* Output message about test being performed */
/* Allocate write & read buffers */
rbuf=malloc(sizeof(hdset_reg_ref_t)*SPACE1_DIM1);
drbuf=calloc(sizeof(int),SPACE2_DIM1*SPACE2_DIM2);
/* Open the file */
fid1 = H5Fopen(FILE2, H5F_ACC_RDWR, H5P_DEFAULT);
/* Open the dataset */
dset1=H5Dopen(fid1,"/Dataset1");
/* Read selection from disk */
ret=H5Dread(dset1,H5T_STD_REF_DSETREG,H5S_ALL,H5S_ALL,H5P_DEFAULT,rbuf);
/* Try to open objects */
dset2 = H5Rdereference(dset1,H5R_DATASET_REGION,&rbuf[0]);
/* Check information in referenced dataset */
sid1 = H5Dget_space(dset2);
ret=H5Sget_simple_extent_npoints(sid1);
printf(" Number of elements in the dataset is : %d\n",ret);
/* Read from disk */
ret=H5Dread(dset2,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,drbuf);
for(i=0; i<SPACE2_DIM1; i++) {
for (j=0; j<SPACE2_DIM2; j++) printf (" %d ", drbuf[i*SPACE2_DIM2+j]);
printf("\n"); }
/* Get the hyperslab selection */
sid2=H5Rget_region(dset1,H5R_DATASET_REGION,&rbuf[0]);
/* Verify correct hyperslab selected */
ret = H5Sget_select_npoints(sid2);
printf(" Number of elements in the hyperslab is : %d \n", ret);
ret = H5Sget_select_hyper_nblocks(sid2);
coords=malloc(ret*SPACE2_RANK*sizeof(hsize_t)*2); /* allocate space for the hyperslab blocks */
ret = H5Sget_select_hyper_blocklist(sid2,0,ret,coords);
printf(" Hyperslab coordinates are : \n");
printf (" ( %lu , %lu ) ( %lu , %lu ) \n", \
(unsigned long)coords[0],(unsigned long)coords[1],(unsigned long)coords[2],(unsigned long)coords[3]);
free(coords);
ret = H5Sget_select_bounds(sid2,low,high);
/* Close region space */
ret = H5Sclose(sid2);
/* Get the element selection */
sid2=H5Rget_region(dset1,H5R_DATASET_REGION,&rbuf[1]);
/* Verify correct elements selected */
ret = H5Sget_select_elem_npoints(sid2);
printf(" Number of selected elements is : %d\n", ret);
/* Allocate space for the element points */
coords= malloc(ret*SPACE2_RANK*sizeof(hsize_t));
ret = H5Sget_select_elem_pointlist(sid2,0,ret,coords);
printf(" Coordinates of selected elements are : \n");
for (i=0; i<2*NPOINTS; i=i+2)
printf(" ( %lu , %lu ) \n", (unsigned long)coords[i],(unsigned long)coords[i+1]);
free(coords);
ret = H5Sget_select_bounds(sid2,low,high);
/* Close region space */
ret = H5Sclose(sid2);
/* Close first space */
ret = H5Sclose(sid1);
/* Close dereferenced Dataset */
ret = H5Dclose(dset2);
/* Close Dataset */
ret = H5Dclose(dset1);
/* Close file */
ret = H5Fclose(fid1);
/* Free memory buffers */
free(rbuf);
free(drbuf);
return 0;
}

View File

@@ -0,0 +1,112 @@
#include <stdlib.h>
#include <hdf5.h>
#define FILE2 "trefer2.h5"
#define SPACE1_NAME "Space1"
#define SPACE1_RANK 1
#define SPACE1_DIM1 4
/* Dataset with fixed dimensions */
#define SPACE2_NAME "Space2"
#define SPACE2_RANK 2
#define SPACE2_DIM1 10
#define SPACE2_DIM2 10
/* Element selection information */
#define POINT1_NPOINTS 10
int
main(void)
{
hid_t fid1; /* HDF5 File IDs */
hid_t dset1, /* Dataset ID */
dset2; /* Dereferenced dataset ID */
hid_t sid1, /* Dataspace ID #1 */
sid2; /* Dataspace ID #2 */
hsize_t dims1[] = {SPACE1_DIM1},
dims2[] = {SPACE2_DIM1, SPACE2_DIM2};
hssize_t start[SPACE2_RANK]; /* Starting location of hyperslab */
hsize_t stride[SPACE2_RANK]; /* Stride of hyperslab */
hsize_t count[SPACE2_RANK]; /* Element count of hyperslab */
hsize_t block[SPACE2_RANK]; /* Block size of hyperslab */
hssize_t coord1[POINT1_NPOINTS][SPACE2_RANK];
/* Coordinates for point selection */
hdset_reg_ref_t *wbuf; /* buffer to write to disk */
int *dwbuf; /* Buffer for writing numeric data to disk */
int i; /* counting variables */
herr_t ret; /* Generic return value */
/* Allocate write & read buffers */
wbuf=calloc(sizeof(hdset_reg_ref_t), SPACE1_DIM1);
dwbuf=malloc(sizeof(int)*SPACE2_DIM1*SPACE2_DIM2);
/* Create file */
fid1 = H5Fcreate(FILE2, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/* Create dataspace for datasets */
sid2 = H5Screate_simple(SPACE2_RANK, dims2, NULL);
/* Create a dataset */
dset2=H5Dcreate(fid1,"Dataset2",H5T_STD_U8LE,sid2,H5P_DEFAULT);
for(i=0; i<SPACE2_DIM1*SPACE2_DIM2; i++)
dwbuf[i]=i*3;
/* Write selection to disk */
ret=H5Dwrite(dset2,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,dwbuf);
/* Close Dataset */
ret = H5Dclose(dset2);
/* Create dataspace for the reference dataset */
sid1 = H5Screate_simple(SPACE1_RANK, dims1, NULL);
/* Create a dataset */
dset1=H5Dcreate(fid1,"Dataset1",H5T_STD_REF_DSETREG,sid1,H5P_DEFAULT);
/* Create references */
/* Select 6x6 hyperslab for first reference */
start[0]=2; start[1]=2;
stride[0]=1; stride[1]=1;
count[0]=6; count[1]=6;
block[0]=1; block[1]=1;
ret = H5Sselect_hyperslab(sid2,H5S_SELECT_SET,start,stride,count,block);
/* Store first dataset region */
ret = H5Rcreate(&wbuf[0],fid1,"/Dataset2",H5R_DATASET_REGION,sid2);
/* Select sequence of ten points for second reference */
coord1[0][0]=6; coord1[0][1]=9;
coord1[1][0]=2; coord1[1][1]=2;
coord1[2][0]=8; coord1[2][1]=4;
coord1[3][0]=1; coord1[3][1]=6;
coord1[4][0]=2; coord1[4][1]=8;
coord1[5][0]=3; coord1[5][1]=2;
coord1[6][0]=0; coord1[6][1]=4;
coord1[7][0]=9; coord1[7][1]=0;
coord1[8][0]=7; coord1[8][1]=1;
coord1[9][0]=3; coord1[9][1]=3;
ret = H5Sselect_elements(sid2,H5S_SELECT_SET,POINT1_NPOINTS,(const hssize_t **)coord1);
/* Store second dataset region */
ret = H5Rcreate(&wbuf[1],fid1,"/Dataset2",H5R_DATASET_REGION,sid2);
/* Write selection to disk */
ret=H5Dwrite(dset1,H5T_STD_REF_DSETREG,H5S_ALL,H5S_ALL,H5P_DEFAULT,wbuf);
/* Close all objects */
ret = H5Sclose(sid1);
ret = H5Dclose(dset1);
ret = H5Sclose(sid2);
/* Close file */
ret = H5Fclose(fid1);
free(wbuf);
free(dwbuf);
return 0;
}

View File

@@ -0,0 +1,146 @@
/*
* This program illustrates how references to the objects can be used.
* Program creates two datasets in the file. It also creates the third
* dataset, and references to the first two datasets are stored in it.
* Program reopens the file and reads dataset with the references.
* References are used to open first two datasets and read datatspace
* and datatype information about them.
*
*/
#include<hdf5.h>
#define FILE "refere.h5"
int
main(void) {
hid_t fid; /* File, datasets, datatypes and */
hid_t did_a, sid_a; /* dataspaces identifiers for three */
hid_t did_b, tid_b, sid_b; /* datasets. */
hid_t did_r, tid_r, sid_r;
herr_t status;
hobj_ref_t *wbuf; /* buffer to write to disk */
hobj_ref_t *rbuf; /* buffer to read from disk */
hsize_t dim_r[1];
hsize_t dim_a[1];
hsize_t dim_b[2];
herr_t ret; /* return values */
/*
* Create a file using default properties.
*/
fid = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
/*
* Create dataset "A" in the file.
*/
dim_a[0] = 5;
sid_a = H5Screate_simple(1, dim_a, NULL);
did_a = H5Dcreate(fid, "A", H5T_NATIVE_INT, sid_a, H5P_DEFAULT);
/*
* Create dataset "B" in the file.
*/
dim_b[0] = 2;
dim_b[1] = 6;
sid_b = H5Screate_simple(2, dim_b, NULL);
did_b = H5Dcreate(fid, "B", H5T_NATIVE_FLOAT, sid_b, H5P_DEFAULT);
/*
* Create dataset "R" to store references to the datasets "A" and "B".
*/
dim_r[0] = 2;
sid_r = H5Screate_simple(1, dim_r, NULL);
tid_r = H5Tcopy(H5T_STD_REF_OBJ);
did_r = H5Dcreate(fid, "R", tid_r, sid_r, H5P_DEFAULT );
/*
* Allocate write and read buffers.
*/
wbuf = malloc(sizeof(hobj_ref_t)*2);
rbuf = malloc(sizeof(hobj_ref_t)*2);
/*
* Create references to the datasets "A" and "B"
* and store them in the wbuf.
*/
H5Rcreate(&wbuf[0], fid, "A", H5R_OBJECT, -1);
H5Rcreate(&wbuf[1], fid, "B", H5R_OBJECT, -1);
/*
* Write dataset R using default transfer properties.
*/
status = H5Dwrite(did_r, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL,
H5P_DEFAULT, wbuf);
/*
* Close all objects.
*/
H5Sclose(sid_a);
H5Dclose(did_a);
H5Sclose(sid_b);
H5Dclose(did_b);
H5Tclose(tid_r);
H5Sclose(sid_r);
H5Dclose(did_r);
H5Fclose(fid);
/*
* Reopen the file.
*/
fid = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT);
/*
* Open and read dataset "R".
*/
did_r = H5Dopen(fid, "R");
status = H5Dread(did_r, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL,
H5P_DEFAULT, rbuf);
/*
* Open dataset A using reference to it.
*/
did_a = H5Rdereference(did_r, H5R_OBJECT, &rbuf[0]);
/*
* Get rank of the dataset "A"
*/
printf("\n");
sid_a = H5Dget_space(did_a);
ret = H5Sget_simple_extent_ndims(sid_a);
if(ret == 1) printf("Rank of A is %d.\n", ret);
printf("\n");
/*
* Get datatype of the dataset "B"
*/
did_b = H5Rdereference(did_r, H5R_OBJECT, &rbuf[1]);
tid_b = H5Dget_type(did_b);
if(H5Tequal(tid_b, H5T_NATIVE_FLOAT))
printf("Datatype of B is H5T_NATIVE_FLOAT.\n");
printf("\n");
/*
* Close all objects.
*/
H5Dclose(did_a);
H5Sclose(sid_a);
H5Dclose(did_b);
H5Tclose(tid_b);
H5Fclose(fid);
return 0;
}