2012-12-12 12:00:34 +08:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* Copyright by The HDF Group. *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
|
|
|
* This file is part of HDF5. The full HDF5 copyright notice, including *
|
|
|
|
* terms governing use, modification, and redistribution, is contained in *
|
2017-04-18 03:32:16 +08:00
|
|
|
* the COPYING file, which can be found at the root of the source code *
|
2021-02-17 22:52:36 +08:00
|
|
|
* distribution tree, or in https://www.hdfgroup.org/licenses. *
|
2017-04-18 03:32:16 +08:00
|
|
|
* If you do not have access to either file, you may request a copy from *
|
|
|
|
* help@hdfgroup.org. *
|
2012-12-12 12:00:34 +08:00
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
|
|
|
/*
|
2020-04-21 07:12:00 +08:00
|
|
|
* This example how to work with extendible datasets. The dataset
|
2012-12-12 12:00:34 +08:00
|
|
|
* must be chunked in order to be extendible.
|
2020-04-21 07:12:00 +08:00
|
|
|
*
|
2012-12-12 12:00:34 +08:00
|
|
|
* It is used in the HDF5 Tutorial.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "hdf5.h"
|
|
|
|
|
|
|
|
#define FILENAME "extend.h5"
|
|
|
|
#define DATASETNAME "ExtendibleArray"
|
2020-09-30 22:27:10 +08:00
|
|
|
#define RANK 2
|
2012-12-12 12:00:34 +08:00
|
|
|
|
|
|
|
int
|
2020-09-30 22:27:10 +08:00
|
|
|
main(void)
|
2012-12-12 12:00:34 +08:00
|
|
|
{
|
2020-09-30 22:27:10 +08:00
|
|
|
hid_t file; /* handles */
|
|
|
|
hid_t dataspace, dataset;
|
|
|
|
hid_t filespace, memspace;
|
|
|
|
hid_t prop;
|
|
|
|
|
|
|
|
hsize_t dims[2] = {3, 3}; /* dataset dimensions at creation time */
|
|
|
|
hsize_t maxdims[2] = {H5S_UNLIMITED, H5S_UNLIMITED};
|
|
|
|
herr_t status;
|
|
|
|
hsize_t chunk_dims[2] = {2, 5};
|
|
|
|
int data[3][3] = {{1, 1, 1}, /* data to write */
|
|
|
|
{1, 1, 1},
|
|
|
|
{1, 1, 1}};
|
2012-12-12 12:00:34 +08:00
|
|
|
|
|
|
|
/* Variables used in extending and writing to the extended portion of dataset */
|
2020-09-30 22:27:10 +08:00
|
|
|
hsize_t size[2];
|
|
|
|
hsize_t offset[2];
|
|
|
|
hsize_t dimsext[2] = {7, 3}; /* extend dimensions */
|
|
|
|
int dataext[7][3] = {{2, 3, 4}, {2, 3, 4}, {2, 3, 4}, {2, 3, 4}, {2, 3, 4}, {2, 3, 4}, {2, 3, 4}};
|
2012-12-12 12:00:34 +08:00
|
|
|
|
|
|
|
/* Variables used in reading data back */
|
2020-09-30 22:27:10 +08:00
|
|
|
hsize_t chunk_dimsr[2];
|
|
|
|
hsize_t dimsr[2];
|
|
|
|
hsize_t i, j;
|
|
|
|
int rdata[10][3];
|
|
|
|
herr_t status_n;
|
|
|
|
int rank, rank_chunk;
|
2012-12-12 12:00:34 +08:00
|
|
|
|
|
|
|
/* Create the data space with unlimited dimensions. */
|
2020-09-30 22:27:10 +08:00
|
|
|
dataspace = H5Screate_simple(RANK, dims, maxdims);
|
2012-12-12 12:00:34 +08:00
|
|
|
|
|
|
|
/* Create a new file. If file exists its contents will be overwritten. */
|
2020-09-30 22:27:10 +08:00
|
|
|
file = H5Fcreate(FILENAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
|
2012-12-12 12:00:34 +08:00
|
|
|
|
|
|
|
/* Modify dataset creation properties, i.e. enable chunking */
|
2020-09-30 22:27:10 +08:00
|
|
|
prop = H5Pcreate(H5P_DATASET_CREATE);
|
|
|
|
status = H5Pset_chunk(prop, RANK, chunk_dims);
|
2012-12-12 12:00:34 +08:00
|
|
|
|
2020-04-21 07:12:00 +08:00
|
|
|
/* Create a new dataset within the file using chunk
|
2012-12-12 12:00:34 +08:00
|
|
|
creation properties. */
|
2020-09-30 22:27:10 +08:00
|
|
|
dataset = H5Dcreate2(file, DATASETNAME, H5T_NATIVE_INT, dataspace, H5P_DEFAULT, prop, H5P_DEFAULT);
|
2012-12-12 12:00:34 +08:00
|
|
|
|
|
|
|
/* Write data to dataset */
|
2020-09-30 22:27:10 +08:00
|
|
|
status = H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
|
2012-12-12 12:00:34 +08:00
|
|
|
|
|
|
|
/* Extend the dataset. Dataset becomes 10 x 3 */
|
2020-09-30 22:27:10 +08:00
|
|
|
size[0] = dims[0] + dimsext[0];
|
2012-12-12 12:00:34 +08:00
|
|
|
size[1] = dims[1];
|
2020-09-30 22:27:10 +08:00
|
|
|
status = H5Dset_extent(dataset, size);
|
2012-12-12 12:00:34 +08:00
|
|
|
|
|
|
|
/* Select a hyperslab in extended portion of dataset */
|
2020-09-30 22:27:10 +08:00
|
|
|
filespace = H5Dget_space(dataset);
|
2012-12-12 12:00:34 +08:00
|
|
|
offset[0] = 3;
|
|
|
|
offset[1] = 0;
|
2020-09-30 22:27:10 +08:00
|
|
|
status = H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, dimsext, NULL);
|
2012-12-12 12:00:34 +08:00
|
|
|
|
|
|
|
/* Define memory space */
|
2020-09-30 22:27:10 +08:00
|
|
|
memspace = H5Screate_simple(RANK, dimsext, NULL);
|
2012-12-12 12:00:34 +08:00
|
|
|
|
|
|
|
/* Write the data to the extended portion of dataset */
|
2020-09-30 22:27:10 +08:00
|
|
|
status = H5Dwrite(dataset, H5T_NATIVE_INT, memspace, filespace, H5P_DEFAULT, dataext);
|
2012-12-12 12:00:34 +08:00
|
|
|
|
|
|
|
/* Close resources */
|
2020-09-30 22:27:10 +08:00
|
|
|
status = H5Dclose(dataset);
|
|
|
|
status = H5Pclose(prop);
|
|
|
|
status = H5Sclose(dataspace);
|
|
|
|
status = H5Sclose(memspace);
|
|
|
|
status = H5Sclose(filespace);
|
|
|
|
status = H5Fclose(file);
|
2012-12-12 12:00:34 +08:00
|
|
|
|
|
|
|
/********************************************
|
|
|
|
* Re-open the file and read the data back. *
|
|
|
|
********************************************/
|
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
file = H5Fopen(FILENAME, H5F_ACC_RDONLY, H5P_DEFAULT);
|
|
|
|
dataset = H5Dopen2(file, DATASETNAME, H5P_DEFAULT);
|
2012-12-12 12:00:34 +08:00
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
filespace = H5Dget_space(dataset);
|
|
|
|
rank = H5Sget_simple_extent_ndims(filespace);
|
|
|
|
status_n = H5Sget_simple_extent_dims(filespace, dimsr, NULL);
|
2012-12-12 12:00:34 +08:00
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
prop = H5Dget_create_plist(dataset);
|
2012-12-12 12:00:34 +08:00
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
if (H5D_CHUNKED == H5Pget_layout(prop))
|
|
|
|
rank_chunk = H5Pget_chunk(prop, rank, chunk_dimsr);
|
2012-12-12 12:00:34 +08:00
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
memspace = H5Screate_simple(rank, dimsr, NULL);
|
|
|
|
status = H5Dread(dataset, H5T_NATIVE_INT, memspace, filespace, H5P_DEFAULT, rdata);
|
2012-12-12 12:00:34 +08:00
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
printf("Dataset: \n");
|
2020-09-30 22:27:10 +08:00
|
|
|
for (j = 0; j < dimsr[0]; j++) {
|
|
|
|
for (i = 0; i < dimsr[1]; i++)
|
|
|
|
printf("%d ", rdata[j][i]);
|
|
|
|
printf("\n");
|
2012-12-12 12:00:34 +08:00
|
|
|
}
|
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
status = H5Pclose(prop);
|
|
|
|
status = H5Dclose(dataset);
|
|
|
|
status = H5Sclose(filespace);
|
|
|
|
status = H5Sclose(memspace);
|
|
|
|
status = H5Fclose(file);
|
2012-12-12 12:00:34 +08:00
|
|
|
}
|