此篇讲述读取一个hdf5 格式数据集,以下为具体操作。
注:读取数据前要创建一个接受存储数据的数据空间
思路 :
1 创建接收数据存储空间
2 打开资源 指定读取hdf5文件及数据集
3 读取数据
注:H5Sselect_hyperslab() hdf5 API实现获取指定参数大小的数据元素切片
status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, offset_out, NULL, count_out, NULL);
4 关闭相关资源
详细代码如下:
1 /*Test dset2: read data
2 * copy and modify code
3 */
4
5 #include "hdf5.h"
6
7 #define H5FILE_NAME "dset2.h5"
8 #define GROUPNAME "mygroup"
9 #define DATASETNAME "dset2"
10 #define NX_SUB 4 /* hyperslab dimensions */ //3*3 dataspace from dset2
11 #define NY_SUB 4
12 #define NX 7 /* output buffer dimensions */
13 #define NY 7
14 #define NZ 3
15 #define RANK 2
16 #define RANK_OUT 3 // receive data space 7*7*3
17
18 int
19 main(void)
20 {
21 hid_t file, group, dataset; /* handles */
22 hid_t datatype, dataspace;
23 hid_t memspace;
24 H5T_class_t t_class; /* data type class */
25 H5T_order_t order; /* data order */
26 size_t size; /*
27 * size of the data element
28 * * stored in file
29 * */
30 hsize_t dimsm[3]; /* memory space dimensions */
31 hsize_t dims_out[2]; /* dataset dimensions */
32 herr_t status;
33
34 int data_out[NX][NY][NZ]; /* output buffer */
35
36 hsize_t count[2]; /* size of the hyperslab in the file */
37 hsize_t offset[2]; /* hyperslab offset in the file */
38 hsize_t count_out[3]; /* size of the hyperslab in memory */
39 hsize_t offset_out[3]; /* hyperslab offset in memory */
40 int i, j, k, status_n, rank;
41
42 for (j = 0; j < NX; j++) {
43 for (i = 0; i < NY; i++) {
44 for (k = 0; k < NZ; k++)
45 data_out[j][i][k] = 0;
46 }
47 }
48
49 /*
50 * * Open the file and the dataset.
51 * */
52 file = H5Fopen("file.h5", H5F_ACC_RDONLY, H5P_DEFAULT);
53 //dataset = H5Dopen2(file, DATASETNAME, H5P_DEFAULT);
54
55 group = H5Gopen(file, GROUPNAME, H5P_DEFAULT);
56 dataset = H5Dopen(group, DATASETNAME ,H5P_DEFAULT);
57 /*
58 * * Get datatype and dataspace handles and then query
59 * * dataset class, order, size, rank and dimensions.
60 * */
61
62 datatype = H5Dget_type(dataset);// datatype handle
63 t_class = H5Tget_class(datatype);
64 if (t_class == H5T_INTEGER)
65 printf("Data set has INTEGER type \n");
66 order = H5Tget_order(datatype);
67 if (order == H5T_ORDER_LE)
68 printf("Little endian order \n");
69
70 size = H5Tget_size(datatype);
71 printf(" Data size is %d \n", (int)size);
72
73 //
74 dataspace = H5Dget_space(dataset); /* dataspace handle */
75 rank = H5Sget_simple_extent_ndims(dataspace);
76 status_n = H5Sget_simple_extent_dims(dataspace, dims_out, NULL);
77 printf("rank %d, dimensions %lu x %lu \n", rank, (unsigned long)(di ms_out[0]),
78 (unsigned long)(dims_out[1]));
79
80 /*
81 * * Define hyperslab in the dataset.
82 * */
83 offset[0] = 0;
84 offset[1] = 0;
85 count[0] = NX_SUB;
86 count[1] = NY_SUB;
87 status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL , count, NULL);
88
89 /*
90 * * Define the memory dataspace.
91 * */
92 dimsm[0] = NX;
93 dimsm[1] = NY;
94 dimsm[2] = NZ;
95 memspace = H5Screate_simple(RANK_OUT, dimsm, NULL);
96
97 /*
98 * * Define memory hyperslab.
99 * */
100 offset_out[0] = 1;
101 offset_out[1] = 0;
102 offset_out[2] = 0;
103 count_out[0] = NX_SUB;
104 count_out[1] = NY_SUB;
105 count_out[2] = 1;
106 status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, offset_ou t, NULL, count_out, NULL);
107
108 /*
109 * * Read data from hyperslab in the file into the hyperslab in
110 * * memory and display.
111 * */
112 status = H5Dread(dataset, H5T_NATIVE_INT, memspace, dataspace, H5P_DEFA ULT, data_out);
113 for (j = 0; j < NX; j++) {
114 for (i = 0; i < NY; i++)
115 printf("%d ", data_out[j][i][0]);
116 printf("\n");
117 }
118 /*
119 * * Close/release resources.
120 * */
121 H5Tclose(datatype);
122 H5Dclose(dataset);
123 H5Sclose(dataspace);
124 H5Sclose(memspace);
125 H5Gclose(group);
126 H5Fclose(file);
127
128 return 0;
129 }