/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright by The HDF Group. * * Copyright by the Board of Trustees of the University of Illinois. * * All rights reserved. * * * * This file is part of HDF5. The full HDF5 copyright notice, including * * terms governing use, modification, and redistribution, is contained in * * the files COPYING and Copyright.html. COPYING can be found at the root * * of the source code distribution tree; Copyright.html can be found at the * * root level of an installed copy of the electronic HDF5 document set and * * is linked from the top-level documents page. It can also be found at * * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * * access to either file, you may request a copy from help@hdfgroup.org. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* * This example 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. */ #include "hdf5.h" #include #include #define FILE "scott.h5" #define DATASETNAME "MyArray" #define RANK 4 #define DIM0_SUB 1 /* subset dimensions */ #define DIM1_SUB 1 #define DIM2_SUB 1 #define DIM3_SUB 1 #define DIM0 360 #define DIM1 719 #define DIM2 10 #define DIM3 17 double data[DIM0][DIM1][DIM2][DIM3]; int main (void) { hsize_t dims[4], dimsm[4]; hid_t file_id, dataset_id; /* handles */ hid_t dataspace_id, memspace_id; herr_t status; double wdata[DIM0_SUB][DIM1_SUB][DIM2_SUB][DIM3_SUB]; hsize_t count[4]; /* size of subset in the file */ hsize_t offset[4]; /* subset offset in the file */ hsize_t stride[4]; hsize_t block[4]; int i, j, k,m; /* Create dataset */ file_id = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); dims[0] = DIM0; dims[1] = DIM1; dims[2] = DIM2; dims[3] = DIM3; dataspace_id = H5Screate_simple (RANK, dims, NULL); dataset_id = H5Dcreate2 (file_id, DATASETNAME, H5T_IEEE_F64LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); for (i = 0; i < DIM0; i++) for (j = 0; j < DIM1; j++) for (k = 0; k < DIM2; k++) for (m = 0; m < DIM3; m++) data[i][j][k][m] = j+3; status = H5Dwrite (dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data); printf ("\nFile created.\n"); status = H5Sclose (dataspace_id); status = H5Dclose (dataset_id); status = H5Fclose (file_id); /***************************************************** * Reopen the file and dataset and write a subset of * * values to the dataset. *****************************************************/ printf ("Write subset:\n\n"); file_id = H5Fopen (FILE, H5F_ACC_RDWR, H5P_DEFAULT); printf ("H5Fopen returns: %li\n", file_id); dataset_id = H5Dopen2 (file_id, DATASETNAME, H5P_DEFAULT); printf ("H5Dopen2 returns: %li\n", dataset_id); offset[0] = 179; offset[1] = 358; offset[2] = 0; offset[3] = 0; count[0] = DIM0_SUB; count[1] = DIM1_SUB; count[2] = DIM2_SUB; count[3] = DIM3_SUB; stride[0] = 1; stride[1] = 1; stride[2] = 1; stride[3] = 1; block[0] = 1; block[1] = 1; block[2] = 1; block[3] = 1; dimsm[0] = DIM0_SUB; dimsm[1] = DIM1_SUB; dimsm[2] = DIM2_SUB; dimsm[3] = DIM3_SUB; memspace_id = H5Screate_simple (RANK, dimsm, NULL); printf ("H5Screate_simple returns: %li\n", memspace_id); dataspace_id = H5Dget_space (dataset_id); printf ("H5Dget_space returns: %li\n", dataspace_id); status = H5Sselect_hyperslab (dataspace_id, H5S_SELECT_SET, offset, stride, count, block); printf ("H5Sselect_hyperslab returns: %li\n", status); for (i = 0; i < DIM0_SUB; i++) for (j = 0; j < DIM1_SUB; j++) for (k = 0; k < DIM2_SUB; k++) for (m = 0; m < DIM3_SUB; m++) wdata[i][j][k][m] = -423.1958; status = H5Dwrite (dataset_id, H5T_NATIVE_DOUBLE, memspace_id, dataspace_id, H5P_DEFAULT, wdata); printf ("H5Dwrite returns: %li\n", status); status = H5Sclose (memspace_id); printf ("H5Sclose returns: %li\n", status); status = H5Sclose (dataspace_id); printf ("H5Sclose returns: %li\n", status); status = H5Dclose (dataset_id); printf ("H5Dclose returns: %li\n", status); status = H5Fclose (file_id); printf ("H5Fclose returns: %li\n", status); }