2020-07-22 23:37:45 +08:00
|
|
|
#include <hdf5.h>
|
|
|
|
#include <hdf5_hl.h>
|
|
|
|
#include <nc_tests.h>
|
|
|
|
#include "err_macros.h"
|
|
|
|
|
|
|
|
#define FILE_NAME_A "tst_virtual_a.nc"
|
|
|
|
#define FILE_NAME_B "tst_virtual_b.nc"
|
|
|
|
#define VARIABLE_SIZE 5
|
|
|
|
#define VARIABLE_NAME "v"
|
|
|
|
|
|
|
|
static void
|
|
|
|
create_dataset_a() {
|
|
|
|
hsize_t dims[1] = {VARIABLE_SIZE};
|
|
|
|
float data[VARIABLE_SIZE];
|
2022-09-27 03:04:10 +08:00
|
|
|
size_t i;
|
|
|
|
for(i = 0; i < VARIABLE_SIZE; ++i) {
|
2020-07-22 23:37:45 +08:00
|
|
|
data[i] = (float)i;
|
|
|
|
}
|
|
|
|
|
|
|
|
hid_t file = H5Fcreate(FILE_NAME_A, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
|
|
|
|
hid_t space = H5Screate_simple(1, dims, NULL);
|
|
|
|
hid_t dset = H5Dcreate2(file, VARIABLE_NAME, H5T_IEEE_F32LE, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
|
|
|
|
|
|
|
|
H5Dwrite(dset, H5T_IEEE_F32LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
|
|
|
|
H5DSset_scale(dset, VARIABLE_NAME);
|
|
|
|
|
|
|
|
H5Dclose(dset);
|
|
|
|
H5Sclose(space);
|
|
|
|
H5Fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
create_dataset_b() {
|
|
|
|
hsize_t dims[1] = {VARIABLE_SIZE};
|
|
|
|
|
|
|
|
hid_t file = H5Fcreate(FILE_NAME_B, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
|
|
|
|
hid_t src_space = H5Screate_simple(1, dims, NULL);
|
|
|
|
hid_t space = H5Screate_simple(1, dims, NULL);
|
|
|
|
|
|
|
|
hid_t dcpl = H5Pcreate(H5P_DATASET_CREATE);
|
|
|
|
H5Pset_virtual(dcpl, space, FILE_NAME_A, VARIABLE_NAME, src_space);
|
|
|
|
hid_t dset = H5Dcreate2(file, VARIABLE_NAME, H5T_IEEE_F32LE, space, H5P_DEFAULT, dcpl, H5P_DEFAULT);
|
|
|
|
|
|
|
|
H5DSset_scale(dset, VARIABLE_NAME);
|
|
|
|
|
|
|
|
H5Dclose(dset);
|
2020-09-02 22:11:43 +08:00
|
|
|
H5Pclose(dcpl);
|
2020-07-22 23:37:45 +08:00
|
|
|
H5Sclose(space);
|
2020-09-02 22:11:43 +08:00
|
|
|
H5Sclose(src_space);
|
2020-07-22 23:37:45 +08:00
|
|
|
H5Fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
int read_back_contents(const char* filename) {
|
|
|
|
int ncid, varid, ndims;
|
2022-09-27 03:04:10 +08:00
|
|
|
size_t i;
|
2020-07-22 23:37:45 +08:00
|
|
|
float data[VARIABLE_SIZE];
|
2023-12-13 07:49:13 +08:00
|
|
|
|
2020-07-22 23:37:45 +08:00
|
|
|
|
|
|
|
char var_name[NC_MAX_NAME+1];
|
|
|
|
int dimids_var[1], var_type, natts;
|
|
|
|
|
|
|
|
printf("\treading back %s.\n", filename);
|
|
|
|
|
|
|
|
if (nc_open(filename, 0, &ncid)) ERR;
|
|
|
|
if (nc_inq_varid(ncid, VARIABLE_NAME, &varid)) ERR;
|
|
|
|
if (nc_inq_var(ncid, varid, var_name, &var_type, &ndims, dimids_var, &natts)) ERR;
|
|
|
|
if (nc_get_var_float(ncid, varid, data)) ERR;
|
|
|
|
printf("\t %s: ", var_name);
|
2022-09-27 03:04:10 +08:00
|
|
|
for (i = 0; i < VARIABLE_SIZE; ++i) {
|
2020-07-22 23:37:45 +08:00
|
|
|
printf("%f, ", data[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
if (nc_close(ncid)) ERR;
|
|
|
|
SUMMARIZE_ERR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
|
|
|
|
printf("\n*** Testing reading of virtual datasets.\n");
|
|
|
|
|
|
|
|
create_dataset_a();
|
|
|
|
create_dataset_b();
|
|
|
|
|
|
|
|
int status;
|
|
|
|
if((status = read_back_contents(FILE_NAME_A))) return status;
|
|
|
|
if((status = read_back_contents(FILE_NAME_B))) return status;
|
|
|
|
}
|