1 /* This is part of the netCDF package. Copyright 2010 University
2 Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
3 conditions of use.
4
5 Test that HDF5 and NetCDF-4 can read and write the same file.
6
7 $Id: tst_interops.c,v 1.24 2010/06/01 15:34:52 ed Exp $
8 */
9 #include <config.h>
10 #include <nc_tests.h>
11 #include "err_macros.h"
12 #include <hdf5.h>
13 #include <H5DSpublic.h>
14
15 #define FILE_NAME "tst_interops6.h5"
16
17 int
main(int argc,char ** argv)18 main(int argc, char **argv)
19 {
20 printf("\n*** Testing HDF5/NetCDF-4 interoperability yet again...\n");
21 printf("*** Checking scalar string attribute...");
22 {
23 #define ATT_NAME "Stooge_Statements"
24 hid_t fcpl_id, fileid, grpid, spaceid, typeid, attid;
25 hid_t class;
26 size_t type_size;
27 htri_t is_str;
28 char *data_in;
29 char *data = "The art of war is of vital "
30 "importance to the State. It is a matter of life and death, a road either"
31 "to safety or to ruin. Hence it is a subject of inquiry"
32 "which can on no account be neglected.";
33
34 /* Create create property list. */
35 if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR;
36
37 /* Set H5P_CRT_ORDER_TRACKED in the creation property list. This
38 * turns on HDF5 creation ordering in the file. */
39 if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
40 H5P_CRT_ORDER_INDEXED)) < 0) ERR;
41 if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
42 H5P_CRT_ORDER_INDEXED)) < 0) ERR;
43
44 /* Open file. */
45 if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, fcpl_id,
46 H5P_DEFAULT)) < 0) ERR;
47 if ((grpid = H5Gopen2(fileid, "/", H5P_DEFAULT)) < 0) ERR;
48
49 /* Create string type. */
50 if ((typeid = H5Tcopy(H5T_C_S1)) < 0) ERR;
51 if (H5Tset_size(typeid, H5T_VARIABLE) < 0) ERR;
52
53 /* Write an attribute of this type. */
54 if ((spaceid = H5Screate(H5S_SCALAR)) < 0) ERR;
55 if ((attid = H5Acreate(grpid, ATT_NAME, typeid, spaceid,
56 H5P_DEFAULT)) < 0) ERR;
57 if (H5Awrite(attid, typeid, &data) < 0) ERR;
58
59 /* Close up. */
60 if (H5Aclose(attid) < 0) ERR;
61 if (H5Tclose(typeid) < 0) ERR;
62 if (H5Sclose(spaceid) < 0) ERR;
63 if (H5Gclose(grpid) < 0) ERR;
64 if (H5Fclose(fileid) < 0) ERR;
65 if (H5Pclose(fcpl_id) < 0) ERR;
66
67 /* Now reopen the file and check it out. */
68 if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR;
69 if ((grpid = H5Gopen(fileid, "/")) < 0) ERR;
70 if ((attid = H5Aopen_name(grpid, ATT_NAME)) < 0) ERR;
71 if ((typeid = H5Aget_type(attid)) < 0) ERR;
72 if ((spaceid = H5Aget_space(attid)) < 0) ERR;
73
74 /* Given this type id, how would we know this is a string
75 * attribute? */
76 if ((class = H5Tget_class(typeid)) < 0)
77 return NC_EHDFERR;
78 if (class != H5T_STRING) ERR;
79 if (!(type_size = H5Tget_size(typeid))) ERR;
80 if ((is_str = H5Tis_variable_str(typeid)) < 0) ERR;
81
82 /* Make sure this is a scalar. */
83 if (H5Sget_simple_extent_type(spaceid) != H5S_SCALAR) ERR;
84
85 /* Read the attribute. */
86 if (H5Aread(attid, typeid, &data_in) < 0) ERR;
87
88 /* Check the data. */
89 if (strcmp(data, data_in)) ERR;
90
91 /* Free our memory. */
92 free(data_in);
93
94 /* Close HDF5 stuff. */
95 if (H5Aclose(attid) < 0) ERR;
96 if (H5Tclose(typeid) < 0) ERR;
97 if (H5Sclose(spaceid) < 0) ERR;
98 if (H5Gclose(grpid) < 0) ERR;
99 if (H5Fclose(fileid) < 0) ERR;
100 }
101 SUMMARIZE_ERR;
102 printf("*** Checking a HDF5 file with scalar, fixed-length string dataset...");
103 {
104 #define VAR_NAME "Marcus_Aurelius"
105 hid_t fcpl_id, fileid, grpid, spaceid, typeid, datasetid, plistid;
106 int ncid, nvars_in, ndims_in, natts_in, unlimdim_in, type_in;
107 char *data = "Thou art no dissatisfied, I suppose, because "
108 "thou weighest only so many liters and not three hundred. Be not "
109 "dissatisfied then that thou must live only so many years and not more; "
110 "for as thou art satisfied with the amount of substance which has "
111 "been assigned to thee, so be content with the time.";
112 char name_in[NC_MAX_NAME + 1];
113 #if 0
114 size_t size_in;
115 char *empty = "";
116 char *data_in2;
117 #endif
118
119 /* Create create property list. */
120 if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR;
121
122 /* Set H5P_CRT_ORDER_TRACKED in the creation property list. This
123 * turns on HDF5 creation ordering in the file. */
124 if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
125 H5P_CRT_ORDER_INDEXED)) < 0) ERR;
126 if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
127 H5P_CRT_ORDER_INDEXED)) < 0) ERR;
128
129 /* Create the file, open root group. */
130 if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, fcpl_id,
131 H5P_DEFAULT)) < 0) ERR;
132 if ((grpid = H5Gopen2(fileid, "/", H5P_DEFAULT)) < 0) ERR;
133
134 /* Create string type. */
135 if ((typeid = H5Tcopy(H5T_C_S1)) < 0) ERR;
136 if (H5Tset_size(typeid, strlen(data) + 1) < 0) ERR;
137
138 /* Create a scalar space. */
139 if ((spaceid = H5Screate(H5S_SCALAR)) < 0) ERR;
140
141 /* Write an scalar dataset of this type. */
142 if ((plistid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR;
143 /* if (H5Pset_fill_value(plistid, typeid, &empty) < 0) ERR;*/
144 if ((datasetid = H5Dcreate2(grpid, VAR_NAME, typeid, spaceid,
145 H5P_DEFAULT, plistid, H5P_DEFAULT)) < 0) ERR;
146 if (H5Dwrite(datasetid, typeid, spaceid, spaceid, H5P_DEFAULT,
147 data) < 0) ERR;
148
149 /* Close up. */
150 if (H5Dclose(datasetid) < 0) ERR;
151 if (H5Pclose(plistid) < 0) ERR;
152 if (H5Pclose(fcpl_id) < 0) ERR;
153 if (H5Tclose(typeid) < 0) ERR;
154 if (H5Gclose(grpid) < 0) ERR;
155 if (H5Fclose(fileid) < 0) ERR;
156
157 /* Read the file with netCDF-4. */
158 /* nc_set_log_level(6); */
159 if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
160 if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdim_in)) ERR;
161 if (ndims_in != 0 || nvars_in != 1 || natts_in != 0 || unlimdim_in != -1) ERR;
162 if (nc_inq_var(ncid, 0, name_in, &type_in, &ndims_in, NULL, &natts_in)) ERR;
163 /* if (strcmp(name_in, VAR_NAME) || type_in != NC_STRING ||
164 ndims_in != 0 || natts_in != 0) ERR;*/
165 /*if (nc_get_var_string(ncid, 0, &data_in2)) ERR;
166 if (strcmp(data_in2, data)) ERR;
167 if (nc_free_string(size_in, &data_in2)) ERR;*/
168 if (nc_close(ncid)) ERR;
169 }
170 SUMMARIZE_ERR;
171 /* printf("*** Checking a HDF5 file with scalar, fixed-length string dataset..."); */
172 /* { */
173 /* #define VAR_NAME "Gettysburg Address" */
174 /* hid_t fapl_id, fcpl_id, fileid, grpid, spaceid, typeid, datasetid, plistid; */
175 /* int ncid, nvars_in, ndims_in, natts_in, unlimdim_in, type_in; */
176 /* size_t size_in; */
177 /* char data[] = "Four score and seven years ago our fathers brought forth on " */
178 /* "this continent, a new nation, conceived in Liberty, and dedicated to " */
179 /* "the proposition that all men are created equal. Now we are engaged " */
180 /* "in a great civil war, testing whether that nation, or any nation so " */
181 /* "conceived and so dedicated, can long endure. We are met on a great " */
182 /* "battle-field of that war. We have come to dedicate a portion of that " */
183 /* "field, as a final resting place for those who here gave their lives " */
184 /* "that that nation might live. It is altogether fitting and proper that " */
185 /* "we should do this. But, in a larger sense, we can not dedicate -- we " */
186 /* "can not consecrate -- we can not hallow -- this ground. The brave men, " */
187 /* "living and dead, who struggled here, have consecrated it, far above our " */
188 /* "poor power to add or detract. The world will little note, nor long " */
189 /* "remember what we say here, but it can never forget what they did here. " */
190 /* "It is for us the living, rather, to be dedicated here to the unfinished " */
191 /* "work which they who fought here have thus far so nobly advanced. It is " */
192 /* "rather for us to be here dedicated to the great task remaining before " */
193 /* "us -- that from these honored dead we take increased devotion to that " */
194 /* "cause for which they gave the last full measure of devotion -- that we " */
195 /* "here highly resolve that these dead shall not have died in vain -- that " */
196 /* "this nation, under God, shall have a new birth of freedom -- and that " */
197 /* "government of the people, by the people, for the people, shall not " */
198 /* "perish from the earth."; */
199 /* char *empty = ""; */
200 /* char *data_in2, name_in[NC_MAX_NAME + 1]; */
201
202 /* /\* Create file access and create property lists. *\/ */
203 /* if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR; */
204 /* if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR; */
205
206 /* /\* Set latest_format in access propertly list. This ensures that */
207 /* * the latest, greatest, HDF5 versions are used in the file. *\/ */
208 /* if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) ERR; */
209
210 /* /\* Set H5P_CRT_ORDER_TRACKED in the creation property list. This */
211 /* * turns on HDF5 creation ordering in the file. *\/ */
212 /* if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED | */
213 /* H5P_CRT_ORDER_INDEXED)) < 0) ERR; */
214 /* if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED | */
215 /* H5P_CRT_ORDER_INDEXED)) < 0) ERR; */
216
217 /* /\* Create the file, open root group. *\/ */
218 /* if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, fcpl_id, fapl_id)) < 0) ERR; */
219 /* if ((grpid = H5Gopen2(fileid, "/", H5P_DEFAULT)) < 0) ERR; */
220
221 /* /\* Create string type. *\/ */
222 /* if ((typeid = H5Tcopy(H5T_C_S1)) < 0) ERR; */
223 /* if (H5Tset_size(typeid, strlen(data) + 1) < 0) ERR; */
224
225 /* /\* Create a scalar space. *\/ */
226 /* if ((spaceid = H5Screate(H5S_SCALAR)) < 0) ERR; */
227
228 /* /\* Write an scalar dataset of this type. *\/ */
229 /* if ((plistid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; */
230 /* if (H5Pset_fill_value(plistid, typeid, &empty) < 0) ERR; */
231 /* if ((datasetid = H5Dcreate1(grpid, VAR_NAME, typeid, */
232 /* spaceid, plistid)) < 0) ERR; */
233 /* if (H5Dwrite(datasetid, typeid, spaceid, spaceid, */
234 /* H5P_DEFAULT, data) < 0) ERR; */
235
236 /* /\* Close up. *\/ */
237 /* if (H5Dclose(datasetid) < 0) ERR; */
238 /* if (H5Pclose(fapl_id) < 0) ERR; */
239 /* if (H5Pclose(fcpl_id) < 0) ERR; */
240 /* if (H5Pclose(plistid) < 0) ERR; */
241 /* if (H5Tclose(typeid) < 0) ERR; */
242 /* if (H5Gclose(grpid) < 0) ERR; */
243 /* if (H5Fclose(fileid) < 0) ERR; */
244
245 /* /\* Read the file with netCDF-4. *\/ */
246 /* if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; */
247 /* if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdim_in)) ERR; */
248 /* if (ndims_in != 0 || nvars_in != 1 || natts_in != 0 || unlimdim_in != -1) ERR; */
249 /* if (nc_inq_var(ncid, 0, name_in, &type_in, &ndims_in, NULL, &natts_in)) ERR; */
250 /* if (strcmp(name_in, VAR_NAME) || type_in != NC_STRING || */
251 /* ndims_in != 0 || natts_in != 0) ERR; */
252 /* if (nc_get_var_string(ncid, 0, &data_in2)) ERR; */
253 /* if (strcmp(data_in2, data)) ERR; */
254 /* if (nc_free_string(size_in, &data_in2)) ERR; */
255 /* if (nc_close(ncid)) ERR; */
256 /* } */
257 /* SUMMARIZE_ERR; */
258 FINAL_RESULTS;
259 }
260