1 /* This is part of the netCDF package.  Copyright 2018 University
2    Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
3    conditions of use.
4 
5    This program does some HDF5 string stuff.
6 
7    Here's a HDF5 sample programs:
8    http://hdf.ncsa.uiuc.edu/training/other-ex5/sample-programs/strings.c
9 */
10 
11 #include "h5_err_macros.h"
12 #include <hdf5.h>
13 
14 #define FILE_NAME "tst_h_strings2.h5"
15 
16 int
main()17 main()
18 {
19    printf("\n*** Checking HDF5 string types even more.\n");
20    printf("*** Checking string dataset with unlimited dimension...");
21    {
22 #define VAR_NAME "Mark_Twain"
23 #define NDIMS 1
24 #define MY_CHUNK_CACHE_NELEMS 1009
25 #define MY_CHUNK_CACHE_SIZE 4194304
26 #define MY_CHUNK_CACHE_PREEMPTION .75
27 
28       hid_t fapl_id, fcpl_id, fileid, grpid, spaceid, access_plistid;
29       hid_t typeid, datasetid, plistid, create_propid, dimscaleid;
30       hid_t file_spaceid, mem_spaceid;
31       hsize_t dims[1] = {0}, max_dims[1] = {H5S_UNLIMITED}, chunk_dims[1] = {1};
32       hsize_t xtend_size[NDIMS] = {2}, start[NDIMS] = {1}, count[NDIMS] = {1};
33 /*      void *fillp;*/
34       char *data = "A man who carries a cat by the tail learns "
35 	 "something he can learn in no other way.";
36 /*      char *empty = "";*/
37 
38       /* Create file access and create property lists. */
39       if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR;
40       if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR;
41 
42       /* Set H5P_CRT_ORDER_TRACKED in the creation property list. This
43        * turns on HDF5 creation ordering in the file. */
44       if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
45 					       H5P_CRT_ORDER_INDEXED)) < 0) ERR;
46       if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
47 					       H5P_CRT_ORDER_INDEXED)) < 0) ERR;
48 
49       /* Create the file, open root group. */
50       if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, fcpl_id, fapl_id)) < 0) ERR;
51       if ((grpid = H5Gopen2(fileid, "/", H5P_DEFAULT)) < 0) ERR;
52       if (H5Pclose(fapl_id) < 0) ERR;
53       if (H5Pclose(fcpl_id) < 0) ERR;
54 
55       /* Create a dimension scale for unlimited dimension. */
56       if ((create_propid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR;
57       if (H5Pset_chunk(create_propid, NDIMS, chunk_dims) < 0) ERR;
58       if ((spaceid = H5Screate_simple(NDIMS, dims, max_dims)) < 0) ERR;
59       if (H5Pset_attr_creation_order(create_propid, H5P_CRT_ORDER_TRACKED|
60       				     H5P_CRT_ORDER_INDEXED) < 0) ERR;
61       if ((dimscaleid = H5Dcreate1(grpid, "unlimited_dim", H5T_IEEE_F32BE,
62       				   spaceid, create_propid)) < 0) ERR;
63       if (H5Sclose(spaceid) < 0) ERR;
64       if (H5Pclose(create_propid) < 0) ERR;
65       if (H5Dclose(dimscaleid) < 0) ERR;
66 
67       /* Create string type. */
68       if ((typeid = H5Tcopy(H5T_C_S1)) < 0) ERR;
69       if (H5Tset_size(typeid, H5T_VARIABLE) < 0) ERR;
70 
71       /* Create a space for our dataset. 0 data but unlimited dimension. */
72       if ((spaceid = H5Screate_simple(NDIMS, dims, max_dims)) < 0) ERR;
73 
74       /* Set up chunking, creation order, and cache. */
75       if ((plistid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR;
76       if (H5Pset_chunk(plistid, NDIMS, chunk_dims) < 0) ERR;
77       if (H5Pset_attr_creation_order(plistid, H5P_CRT_ORDER_TRACKED|
78 				     H5P_CRT_ORDER_INDEXED) < 0) ERR;
79 /*      if (H5Pset_fill_value(plistid, typeid, &empty) < 0) ERR;*/
80       if ((access_plistid = H5Pcreate(H5P_DATASET_ACCESS)) < 0) ERR;
81       if (H5Pset_chunk_cache(access_plistid, MY_CHUNK_CACHE_NELEMS,
82 			     MY_CHUNK_CACHE_SIZE, MY_CHUNK_CACHE_PREEMPTION) < 0) ERR;
83 
84       /* Create the dataset. It has zero records. */
85       if ((datasetid = H5Dcreate2(grpid, VAR_NAME, typeid, spaceid,
86 				  H5P_DEFAULT, plistid, access_plistid)) < 0) ERR;
87 
88       /* Now extend the dataset. */
89       if (H5Dextend(datasetid, xtend_size) < 0) ERR;
90 
91       /* Select space in file to write a record. */
92       if ((file_spaceid = H5Dget_space(datasetid)) < 0) ERR;
93       if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET,
94 			      start, NULL, count, NULL) < 0) ERR;
95 
96       /* Select space in memory to read from. */
97       if ((mem_spaceid = H5Screate_simple(NDIMS, count, NULL)) < 0) ERR;
98 
99       /*if ((xfer_plistid = H5Pcreate(H5P_DATASET_XFER)) < 0) ERR;*/
100 
101       /* Write the data. */
102       if (H5Dwrite(datasetid, typeid, mem_spaceid, file_spaceid,
103 		   H5P_DEFAULT, &data) < 0) ERR;
104 
105       /* Close up. */
106       if (H5Sclose(file_spaceid) < 0) ERR;
107       if (H5Sclose(mem_spaceid) < 0) ERR;
108       if (H5Dclose(datasetid) < 0) ERR;
109       if (H5Pclose(access_plistid) < 0) ERR;
110       if (H5Pclose(plistid) < 0) ERR;
111       if (H5Tclose(typeid) < 0) ERR;
112       if (H5Sclose(spaceid) < 0) ERR;
113       if (H5Gclose(grpid) < 0) ERR;
114       if (H5Fclose(fileid) < 0) ERR;
115    }
116    SUMMARIZE_ERR;
117    printf("*** Checking string dataset with unlimited dimension...");
118    {
119 #define VAR_NAME "Mark_Twain"
120 #define NDIMS 1
121 #define MY_CHUNK_CACHE_NELEMS 1009
122 #define MY_CHUNK_CACHE_SIZE 4194304
123 #define MY_CHUNK_CACHE_PREEMPTION .75
124 
125       hid_t fapl_id, fcpl_id, fileid, grpid, spaceid, access_plistid;
126       hid_t typeid, datasetid, plistid;
127       hid_t file_spaceid, mem_spaceid;
128       hsize_t dims[1] = {2}, chunk_dims[1] = {1};
129       hsize_t start[NDIMS] = {1}, count[NDIMS] = {1};
130 /*      void *fillp;*/
131       char *data = "A man who carries a cat by the tail learns "
132 	 "something he can learn in no other way.";
133 /* Man - a creature made at the end of the week's work when God was tired.
134 There are basically two types of people. People who accomplish things, and people who claim to have accomplished things. The first group is less crowded.
135 To be good is noble; but to show others how to be good is nobler and no trouble. */
136 /*      char *empty = "";*/
137 
138       /* Create file access and create property lists. */
139       if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR;
140       if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR;
141 
142       /* Set H5P_CRT_ORDER_TRACKED in the creation property list. This
143        * turns on HDF5 creation ordering in the file. */
144       if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
145 					       H5P_CRT_ORDER_INDEXED)) < 0) ERR;
146       if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
147 					       H5P_CRT_ORDER_INDEXED)) < 0) ERR;
148 
149       /* Create the file, open root group. */
150       if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, fcpl_id, fapl_id)) < 0) ERR;
151       if ((grpid = H5Gopen2(fileid, "/", H5P_DEFAULT)) < 0) ERR;
152       if (H5Pclose(fapl_id) < 0) ERR;
153       if (H5Pclose(fcpl_id) < 0) ERR;
154 
155       /* Create string type. */
156       if ((typeid = H5Tcopy(H5T_C_S1)) < 0) ERR;
157       if (H5Tset_size(typeid, H5T_VARIABLE) < 0) ERR;
158 
159       /* Create a space for our dataset. */
160       if ((spaceid = H5Screate_simple(NDIMS, dims, dims)) < 0) ERR;
161 
162       /* Set up chunking, creation order, and cache. */
163       if ((plistid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR;
164       if (H5Pset_chunk(plistid, NDIMS, chunk_dims) < 0) ERR;
165       if (H5Pset_attr_creation_order(plistid, H5P_CRT_ORDER_TRACKED|
166 				     H5P_CRT_ORDER_INDEXED) < 0) ERR;
167 /*      if (H5Pset_fill_value(plistid, typeid, &empty) < 0) ERR;*/
168       if ((access_plistid = H5Pcreate(H5P_DATASET_ACCESS)) < 0) ERR;
169       if (H5Pset_chunk_cache(access_plistid, MY_CHUNK_CACHE_NELEMS,
170 			     MY_CHUNK_CACHE_SIZE, MY_CHUNK_CACHE_PREEMPTION) < 0) ERR;
171 
172       /* Create the dataset. It has zero records. */
173       if ((datasetid = H5Dcreate2(grpid, VAR_NAME, typeid, spaceid,
174 				  H5P_DEFAULT, plistid, access_plistid)) < 0) ERR;
175 
176       /* Now extend the dataset. */
177       /*if (H5Dextend(datasetid, xtend_size) < 0) ERR;*/
178 
179       /* Select space in file to write a record. */
180       if ((file_spaceid = H5Dget_space(datasetid)) < 0) ERR;
181       if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET,
182 			      start, NULL, count, NULL) < 0) ERR;
183 
184       /* Select space in memory to read from. */
185       if ((mem_spaceid = H5Screate_simple(NDIMS, count, NULL)) < 0)
186 
187 	 /*if ((xfer_plistid = H5Pcreate(H5P_DATASET_XFER)) < 0) ERR;*/
188 
189       /* Write the data. */
190       if (H5Dwrite(datasetid, typeid, mem_spaceid, file_spaceid,
191 		   H5P_DEFAULT, &data) < 0)
192 
193       /* Close up. */
194       if (H5Dclose(datasetid) < 0) ERR;
195       if (H5Pclose(access_plistid) < 0) ERR;
196       if (H5Pclose(plistid) < 0) ERR;
197       if (H5Tclose(typeid) < 0) ERR;
198       if (H5Sclose(spaceid) < 0) ERR;
199       if (H5Gclose(grpid) < 0) ERR;
200       if (H5Fclose(fileid) < 0) ERR;
201    }
202    SUMMARIZE_ERR;
203    FINAL_RESULTS;
204 }
205