hdf5/examples/h5_subset.c

143 lines
4.7 KiB
C
Raw Normal View History

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the COPYING file, which can be found at the root of the source code *
* distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2020-04-21 07:12:00 +08:00
/*
* This example illustrates how to read/write a subset of data (a slab)
* from/to a dataset in an HDF5 file. It is used in the HDF5 Tutorial.
*/
2020-04-21 07:12:00 +08:00
#include "hdf5.h"
#define FILE "subset.h5"
2020-04-21 07:12:00 +08:00
#define DATASETNAME "IntArray"
2020-09-30 22:27:10 +08:00
#define RANK 2
2020-09-30 22:27:10 +08:00
#define DIM0_SUB 3 /* subset dimensions */
#define DIM1_SUB 4
2020-09-30 22:27:10 +08:00
#define DIM0 8 /* size of dataset */
#define DIM1 10
int
2020-09-30 22:27:10 +08:00
main(void)
{
2020-09-30 22:27:10 +08:00
hsize_t dims[2], dimsm[2];
int data[DIM0][DIM1]; /* data to write */
int sdata[DIM0_SUB][DIM1_SUB]; /* subset to write */
int rdata[DIM0][DIM1]; /* buffer for read */
2020-04-21 07:12:00 +08:00
2020-09-30 22:27:10 +08:00
hid_t file_id, dataset_id; /* handles */
hid_t dataspace_id, memspace_id;
2020-09-30 22:27:10 +08:00
herr_t status;
2020-09-30 22:27:10 +08:00
hsize_t count[2]; /* size of subset in the file */
hsize_t offset[2]; /* subset offset in the file */
hsize_t stride[2];
hsize_t block[2];
int i, j;
2020-04-21 07:12:00 +08:00
/*****************************************************************
* Create a new file with default creation and access properties.*
* Then create a dataset and write data to it and close the file *
* and dataset. *
*****************************************************************/
2020-09-30 22:27:10 +08:00
file_id = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
2020-09-30 22:27:10 +08:00
dims[0] = DIM0;
dims[1] = DIM1;
dataspace_id = H5Screate_simple(RANK, dims, NULL);
2020-09-30 22:27:10 +08:00
dataset_id =
H5Dcreate2(file_id, DATASETNAME, H5T_STD_I32BE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
for (j = 0; j < DIM0; j++) {
2020-09-30 22:27:10 +08:00
for (i = 0; i < DIM1; i++)
if (i < (DIM1 / 2))
data[j][i] = 1;
else
2020-09-30 22:27:10 +08:00
data[j][i] = 2;
2020-04-21 07:12:00 +08:00
}
2020-09-30 22:27:10 +08:00
status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
2020-09-30 22:27:10 +08:00
printf("\nData Written to File:\n");
for (i = 0; i < DIM0; i++) {
for (j = 0; j < DIM1; j++)
printf(" %i", data[i][j]);
printf("\n");
}
2020-09-30 22:27:10 +08:00
status = H5Sclose(dataspace_id);
status = H5Dclose(dataset_id);
status = H5Fclose(file_id);
/*****************************************************
* Reopen the file and dataset and write a subset of *
2020-04-21 07:12:00 +08:00
* values to the dataset.
*****************************************************/
2020-09-30 22:27:10 +08:00
file_id = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT);
dataset_id = H5Dopen2(file_id, DATASETNAME, H5P_DEFAULT);
/* Specify size and shape of subset to write. */
offset[0] = 1;
offset[1] = 2;
2020-09-30 22:27:10 +08:00
count[0] = DIM0_SUB;
count[1] = DIM1_SUB;
stride[0] = 1;
stride[1] = 1;
block[0] = 1;
block[1] = 1;
2020-04-21 07:12:00 +08:00
/* Create memory space with size of subset. Get file dataspace
and select subset from file dataspace. */
2020-09-30 22:27:10 +08:00
dimsm[0] = DIM0_SUB;
dimsm[1] = DIM1_SUB;
memspace_id = H5Screate_simple(RANK, dimsm, NULL);
2020-09-30 22:27:10 +08:00
dataspace_id = H5Dget_space(dataset_id);
status = H5Sselect_hyperslab(dataspace_id, H5S_SELECT_SET, offset, stride, count, block);
2020-04-21 07:12:00 +08:00
/* Write a subset of data to the dataset, then read the
entire dataset back from the file. */
2020-09-30 22:27:10 +08:00
printf("\nWrite subset to file specifying:\n");
printf(" offset=1x2 stride=1x1 count=3x4 block=1x1\n");
for (j = 0; j < DIM0_SUB; j++) {
2020-09-30 22:27:10 +08:00
for (i = 0; i < DIM1_SUB; i++)
sdata[j][i] = 5;
2020-04-21 07:12:00 +08:00
}
2020-09-30 22:27:10 +08:00
status = H5Dwrite(dataset_id, H5T_NATIVE_INT, memspace_id, dataspace_id, H5P_DEFAULT, sdata);
2020-04-21 07:12:00 +08:00
2020-09-30 22:27:10 +08:00
status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata);
2020-09-30 22:27:10 +08:00
printf("\nData in File after Subset is Written:\n");
for (i = 0; i < DIM0; i++) {
for (j = 0; j < DIM1; j++)
printf(" %i", rdata[i][j]);
printf("\n");
}
2020-09-30 22:27:10 +08:00
status = H5Sclose(memspace_id);
status = H5Sclose(dataspace_id);
status = H5Dclose(dataset_id);
status = H5Fclose(file_id);
}