2017-04-18 03:32:16 +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 *
|
|
|
|
* 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. *
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2015-03-11 06:15:20 +08:00
|
|
|
/************************************************************
|
|
|
|
|
|
|
|
This example illustrates the concept of virtual dataset I/O
|
|
|
|
The program creates 2-dim source dataset and writes
|
|
|
|
data to it. Then it creates 2-dim virtual dataset that has
|
2020-04-21 07:12:00 +08:00
|
|
|
the same dimension sizes and maps the all elements of the
|
2015-03-11 06:15:20 +08:00
|
|
|
virtual dataset to all elements of the source dataset.
|
|
|
|
Then VDS is read back.
|
|
|
|
|
|
|
|
This file is intended for use with HDF5 Library version 1.10
|
|
|
|
|
|
|
|
************************************************************/
|
|
|
|
/* EIP Add link to the picture */
|
|
|
|
|
|
|
|
#include "hdf5.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
#define FILE "vds-simpleIO.h5"
|
|
|
|
#define DATASET "VDS"
|
|
|
|
#define DIM1 6
|
|
|
|
#define DIM0 4
|
|
|
|
#define RANK 2
|
2015-03-11 06:15:20 +08:00
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
#define SRC_FILE "a.h5"
|
|
|
|
#define SRC_DATASET "/A"
|
2015-03-11 06:15:20 +08:00
|
|
|
|
|
|
|
int
|
2020-09-30 22:27:10 +08:00
|
|
|
main(void)
|
2015-03-11 06:15:20 +08:00
|
|
|
{
|
2020-09-30 22:27:10 +08:00
|
|
|
hid_t file, space, src_space, vspace, dset; /* Handles */
|
|
|
|
hid_t dcpl;
|
|
|
|
herr_t status;
|
|
|
|
hsize_t vdsdims[2] = {DIM0, DIM1}, /* Virtual dataset dimension */
|
|
|
|
dims[2] = {DIM0, DIM1}; /* Source dataset dimensions */
|
|
|
|
int wdata[DIM0][DIM1], /* Write buffer for source dataset */
|
|
|
|
rdata[DIM0][DIM1], /* Read buffer for virtual dataset */
|
|
|
|
i, j;
|
|
|
|
H5D_layout_t layout; /* Storage layout */
|
|
|
|
size_t num_map; /* Number of mappings */
|
|
|
|
ssize_t len; /* Length of the string; also a return value */
|
|
|
|
char * filename;
|
|
|
|
char * dsetname;
|
2015-03-11 06:15:20 +08:00
|
|
|
/*
|
|
|
|
* Initialize data.
|
|
|
|
*/
|
2020-09-30 22:27:10 +08:00
|
|
|
for (i = 0; i < DIM0; i++)
|
|
|
|
for (j = 0; j < DIM1; j++)
|
|
|
|
wdata[i][j] = i + 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the source file and the dataset. Write data to the source dataset
|
|
|
|
* and close all resources.
|
|
|
|
*/
|
|
|
|
|
|
|
|
file = H5Fcreate(SRC_FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
|
|
|
|
space = H5Screate_simple(RANK, dims, NULL);
|
|
|
|
dset = H5Dcreate2(file, SRC_DATASET, H5T_NATIVE_INT, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
|
|
|
|
status = H5Dwrite(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata[0]);
|
|
|
|
status = H5Sclose(space);
|
|
|
|
status = H5Dclose(dset);
|
|
|
|
status = H5Fclose(file);
|
2015-03-11 06:15:20 +08:00
|
|
|
|
|
|
|
/* Create file in which virtual dataset will be stored. */
|
2020-09-30 22:27:10 +08:00
|
|
|
file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
|
2015-03-11 06:15:20 +08:00
|
|
|
|
|
|
|
/* Create VDS dataspace. */
|
2020-09-30 22:27:10 +08:00
|
|
|
vspace = H5Screate_simple(RANK, vdsdims, NULL);
|
2015-03-11 06:15:20 +08:00
|
|
|
|
|
|
|
/* Set VDS creation property. */
|
2020-09-30 22:27:10 +08:00
|
|
|
dcpl = H5Pcreate(H5P_DATASET_CREATE);
|
2020-04-21 07:12:00 +08:00
|
|
|
|
|
|
|
/*
|
2015-03-11 06:15:20 +08:00
|
|
|
* Build the mappings.
|
|
|
|
* Selections in the source datasets are H5S_ALL.
|
2020-04-21 07:12:00 +08:00
|
|
|
* In the virtual dataset we select the first, the second and the third rows
|
|
|
|
* and map each row to the data in the corresponding source dataset.
|
2015-03-11 06:15:20 +08:00
|
|
|
*/
|
2020-09-30 22:27:10 +08:00
|
|
|
src_space = H5Screate_simple(RANK, dims, NULL);
|
|
|
|
status = H5Pset_virtual(dcpl, vspace, SRC_FILE, SRC_DATASET, src_space);
|
2015-03-11 06:15:20 +08:00
|
|
|
|
|
|
|
/* Create a virtual dataset. */
|
2020-09-30 22:27:10 +08:00
|
|
|
dset = H5Dcreate2(file, DATASET, H5T_NATIVE_INT, vspace, H5P_DEFAULT, dcpl, H5P_DEFAULT);
|
|
|
|
status = H5Sclose(vspace);
|
|
|
|
status = H5Sclose(src_space);
|
|
|
|
status = H5Dclose(dset);
|
|
|
|
status = H5Fclose(file);
|
2020-04-21 07:12:00 +08:00
|
|
|
|
2015-03-11 06:15:20 +08:00
|
|
|
/*
|
|
|
|
* Now we begin the read section of this example.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the file and virtual dataset.
|
|
|
|
*/
|
2020-09-30 22:27:10 +08:00
|
|
|
file = H5Fopen(FILE, H5F_ACC_RDONLY, H5P_DEFAULT);
|
|
|
|
dset = H5Dopen2(file, DATASET, H5P_DEFAULT);
|
2015-03-11 06:15:20 +08:00
|
|
|
/*
|
|
|
|
* Get creation property list and mapping properties.
|
|
|
|
*/
|
2020-09-30 22:27:10 +08:00
|
|
|
dcpl = H5Dget_create_plist(dset);
|
2015-03-11 06:15:20 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get storage layout.
|
|
|
|
*/
|
2020-09-30 22:27:10 +08:00
|
|
|
layout = H5Pget_layout(dcpl);
|
2020-04-21 07:12:00 +08:00
|
|
|
if (H5D_VIRTUAL == layout)
|
2015-03-11 06:15:20 +08:00
|
|
|
printf(" Dataset has a virtual layout \n");
|
|
|
|
else
|
|
|
|
printf(" Wrong layout found \n");
|
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
/*
|
|
|
|
* Find the number of mappings.
|
|
|
|
*/
|
|
|
|
status = H5Pget_virtual_count(dcpl, &num_map);
|
|
|
|
printf(" Number of mappings is %lu\n", (unsigned long)num_map);
|
2015-03-11 06:15:20 +08:00
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
/*
|
|
|
|
* Get mapping parameters for each mapping.
|
|
|
|
*/
|
2020-04-21 07:12:00 +08:00
|
|
|
for (i = 0; i < (int)num_map; i++) {
|
2015-03-11 06:15:20 +08:00
|
|
|
printf(" Mapping %d \n", i);
|
|
|
|
printf(" Selection in the virtual dataset ");
|
|
|
|
/* Get selection in the virttual dataset */
|
2020-09-30 22:27:10 +08:00
|
|
|
vspace = H5Pget_virtual_vspace(dcpl, (size_t)i);
|
2015-03-11 06:15:20 +08:00
|
|
|
|
|
|
|
/* Make sure it is ALL selection and then print selection. */
|
2020-09-30 22:27:10 +08:00
|
|
|
if (H5Sget_select_type(vspace) == H5S_SEL_ALL) {
|
|
|
|
printf("Selection is H5S_ALL \n");
|
2015-03-11 06:15:20 +08:00
|
|
|
}
|
|
|
|
/* Get source file name. */
|
2020-09-30 22:27:10 +08:00
|
|
|
len = H5Pget_virtual_filename(dcpl, (size_t)i, NULL, 0);
|
|
|
|
filename = (char *)malloc((size_t)len * sizeof(char) + 1);
|
|
|
|
H5Pget_virtual_filename(dcpl, (size_t)i, filename, len + 1);
|
2015-03-11 06:15:20 +08:00
|
|
|
printf(" Source filename %s\n", filename);
|
|
|
|
|
|
|
|
/* Get source dataset name. */
|
2020-09-30 22:27:10 +08:00
|
|
|
len = H5Pget_virtual_dsetname(dcpl, (size_t)i, NULL, 0);
|
|
|
|
dsetname = (char *)malloc((size_t)len * sizeof(char) + 1);
|
|
|
|
H5Pget_virtual_dsetname(dcpl, (size_t)i, dsetname, len + 1);
|
2015-03-11 06:15:20 +08:00
|
|
|
printf(" Source dataset name %s\n", dsetname);
|
|
|
|
|
|
|
|
/* Get selection in the source dataset. */
|
|
|
|
printf(" Selection in the source dataset ");
|
2020-09-30 22:27:10 +08:00
|
|
|
src_space = H5Pget_virtual_srcspace(dcpl, (size_t)i);
|
2015-03-11 06:15:20 +08:00
|
|
|
|
|
|
|
/* Make sure it is ALL selection and then print selection. */
|
2020-09-30 22:27:10 +08:00
|
|
|
if (H5Sget_select_type(src_space) == H5S_SEL_ALL) {
|
|
|
|
printf("Selection is H5S_ALL \n");
|
2015-03-11 06:15:20 +08:00
|
|
|
}
|
|
|
|
H5Sclose(vspace);
|
|
|
|
H5Sclose(src_space);
|
|
|
|
free(filename);
|
|
|
|
free(dsetname);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Read the data using the default properties.
|
|
|
|
*/
|
2020-09-30 22:27:10 +08:00
|
|
|
status = H5Dread(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata[0]);
|
2015-03-11 06:15:20 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Output the data to the screen.
|
|
|
|
*/
|
2020-09-30 22:27:10 +08:00
|
|
|
printf(" VDS Data:\n");
|
|
|
|
for (i = 0; i < DIM0; i++) {
|
|
|
|
printf(" [");
|
|
|
|
for (j = 0; j < DIM1; j++)
|
|
|
|
printf(" %3d", rdata[i][j]);
|
|
|
|
printf("]\n");
|
2015-03-11 06:15:20 +08:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Close and release resources.
|
|
|
|
*/
|
2020-09-30 22:27:10 +08:00
|
|
|
status = H5Pclose(dcpl);
|
|
|
|
status = H5Dclose(dset);
|
|
|
|
status = H5Fclose(file);
|
2015-03-11 06:15:20 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|