1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 /************************************************************
15   This example shows how to create and extend an unlimited
16   dataset.  The program first writes integers to a dataset
17   with dataspace dimensions of DIM_XxDIM_Y, then closes the
18   file.  Next, it reopens the file, reads back the data,
19   outputs it to the screen, extends the dataset, and writes
20   new data to the extended portions of the dataset.  Finally
21   it reopens the file again, reads back the data, and
22   outputs it to the screen.
23  ************************************************************/
24 package examples.datasets;
25 
26 import hdf.hdf5lib.H5;
27 import hdf.hdf5lib.HDF5Constants;
28 
29 public class H5Ex_D_UnlimitedAdd {
30     private static String FILENAME = "H5Ex_D_UnlimitedAdd.h5";
31     private static String DATASETNAME = "DS1";
32     private static final int DIM_X = 4;
33     private static final int DIM_Y = 7;
34     private static final int EDIM_X = 6;
35     private static final int EDIM_Y = 10;
36     private static final int CHUNK_X = 4;
37     private static final int CHUNK_Y = 4;
38     private static final int RANK = 2;
39     private static final int NDIMS = 2;
40 
writeUnlimited()41     private static void writeUnlimited() {
42         long file_id = -1;
43         long dcpl_id = -1;
44         long dataspace_id = -1;
45         long dataset_id = -1;
46         long[] dims = { DIM_X, DIM_Y };
47         long[] chunk_dims = { CHUNK_X, CHUNK_Y };
48         long[] maxdims = { HDF5Constants.H5S_UNLIMITED, HDF5Constants.H5S_UNLIMITED };
49         int[][] dset_data = new int[DIM_X][DIM_Y];
50 
51         // Initialize the dataset.
52         for (int indx = 0; indx < DIM_X; indx++)
53             for (int jndx = 0; jndx < DIM_Y; jndx++)
54                 dset_data[indx][jndx] = indx * jndx - jndx;
55 
56         // Create a new file using default properties.
57         try {
58             file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT,
59                     HDF5Constants.H5P_DEFAULT);
60         }
61         catch (Exception e) {
62             e.printStackTrace();
63         }
64 
65         // Create dataspace with unlimited dimensions.
66         try {
67             dataspace_id = H5.H5Screate_simple(RANK, dims, maxdims);
68         }
69         catch (Exception e) {
70             e.printStackTrace();
71         }
72 
73         // Create the dataset creation property list.
74         try {
75             dcpl_id = H5.H5Pcreate(HDF5Constants.H5P_DATASET_CREATE);
76         }
77         catch (Exception e) {
78             e.printStackTrace();
79         }
80 
81         // Set the chunk size.
82         try {
83             if (dcpl_id >= 0)
84                 H5.H5Pset_chunk(dcpl_id, NDIMS, chunk_dims);
85         }
86         catch (Exception e) {
87             e.printStackTrace();
88         }
89 
90         // Create the unlimited dataset.
91         try {
92             if ((file_id >= 0) && (dataspace_id >= 0) && (dcpl_id >= 0))
93                 dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I32LE, dataspace_id,
94                         HDF5Constants.H5P_DEFAULT, dcpl_id, HDF5Constants.H5P_DEFAULT);
95         }
96         catch (Exception e) {
97             e.printStackTrace();
98         }
99 
100         // Write the data to the dataset.
101         try {
102             if (dataset_id >= 0)
103                 H5.H5Dwrite(dataset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
104                         HDF5Constants.H5P_DEFAULT, dset_data);
105         }
106         catch (Exception e) {
107             e.printStackTrace();
108         }
109 
110         // End access to the dataset and release resources used by it.
111         try {
112             if (dataset_id >= 0)
113                 H5.H5Dclose(dataset_id);
114         }
115         catch (Exception e) {
116             e.printStackTrace();
117         }
118 
119         try {
120             if (dataspace_id >= 0)
121                 H5.H5Sclose(dataspace_id);
122         }
123         catch (Exception e) {
124             e.printStackTrace();
125         }
126 
127         try {
128             if (dcpl_id >= 0)
129                 H5.H5Pclose(dcpl_id);
130         }
131         catch (Exception e) {
132             e.printStackTrace();
133         }
134 
135         // Close the file.
136         try {
137             if (file_id >= 0)
138                 H5.H5Fclose(file_id);
139         }
140         catch (Exception e) {
141             e.printStackTrace();
142         }
143     }
144 
extendUnlimited()145     private static void extendUnlimited() {
146         long file_id = -1;
147         long dataspace_id = -1;
148         long dataset_id = -1;
149         long[] dims = { DIM_X, DIM_Y };
150         long[] extdims = { EDIM_X, EDIM_Y };
151         long[] start = { 0, 0 };
152         long[] count = new long[2];
153         int[][] dset_data;
154         int[][] extend_dset_data = new int[EDIM_X][EDIM_Y];
155 
156         // Open an existing file.
157         try {
158             file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDWR, HDF5Constants.H5P_DEFAULT);
159         }
160         catch (Exception e) {
161             e.printStackTrace();
162         }
163 
164         // Open an existing dataset.
165         try {
166             if (file_id >= 0)
167                 dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT);
168         }
169         catch (Exception e) {
170             e.printStackTrace();
171         }
172 
173         // Get dataspace and allocate memory for read buffer. This is a
174         // two dimensional dataset so the dynamic allocation must be done
175         // in steps.
176         try {
177             if (dataset_id >= 0)
178                 dataspace_id = H5.H5Dget_space(dataset_id);
179         }
180         catch (Exception e) {
181             e.printStackTrace();
182         }
183 
184         try {
185             if (dataspace_id >= 0)
186                 H5.H5Sget_simple_extent_dims(dataspace_id, dims, null);
187         }
188         catch (Exception e) {
189             e.printStackTrace();
190         }
191 
192         // Allocate array of pointers to rows.
193         dset_data = new int[(int) dims[0]][(int) dims[1]];
194 
195         // Read the data using the default properties.
196         try {
197             if (dataset_id >= 0)
198                 H5.H5Dread(dataset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
199                         HDF5Constants.H5P_DEFAULT, dset_data);
200         }
201         catch (Exception e) {
202             e.printStackTrace();
203         }
204 
205         // Output the data to the screen.
206         System.out.println("Dataset before extension:");
207         for (int indx = 0; indx < DIM_X; indx++) {
208             System.out.print(" [ ");
209             for (int jndx = 0; jndx < DIM_Y; jndx++)
210                 System.out.print(dset_data[indx][jndx] + " ");
211             System.out.println("]");
212         }
213         System.out.println();
214 
215         try {
216             if (dataspace_id >= 0)
217                 H5.H5Sclose(dataspace_id);
218         }
219         catch (Exception e) {
220             e.printStackTrace();
221         }
222 
223         // Extend the dataset.
224         try {
225             if (dataset_id >= 0)
226                 H5.H5Dset_extent(dataset_id, extdims);
227         }
228         catch (Exception e) {
229             e.printStackTrace();
230         }
231 
232         // Retrieve the dataspace for the newly extended dataset.
233         try {
234             if (dataset_id >= 0)
235                 dataspace_id = H5.H5Dget_space(dataset_id);
236         }
237         catch (Exception e) {
238             e.printStackTrace();
239         }
240 
241         // Initialize data for writing to the extended dataset.
242         for (int indx = 0; indx < EDIM_X; indx++)
243             for (int jndx = 0; jndx < EDIM_Y; jndx++)
244                 extend_dset_data[indx][jndx] = jndx;
245 
246         // Select the entire dataspace.
247         try {
248             if (dataspace_id >= 0) {
249                 H5.H5Sselect_all(dataspace_id);
250 
251                 // Subtract a hyperslab reflecting the original dimensions from the
252                 // selection. The selection now contains only the newly extended
253                 // portions of the dataset.
254                 count[0] = dims[0];
255                 count[1] = dims[1];
256                 H5.H5Sselect_hyperslab(dataspace_id, HDF5Constants.H5S_SELECT_NOTB, start, null, count, null);
257 
258                 // Write the data to the selected portion of the dataset.
259                 if (dataset_id >= 0)
260                     H5.H5Dwrite(dataset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, dataspace_id,
261                             HDF5Constants.H5P_DEFAULT, extend_dset_data);
262             }
263         }
264         catch (Exception e) {
265             e.printStackTrace();
266         }
267 
268         // End access to the dataset and release resources used by it.
269         try {
270             if (dataset_id >= 0)
271                 H5.H5Dclose(dataset_id);
272         }
273         catch (Exception e) {
274             e.printStackTrace();
275         }
276 
277         try {
278             if (dataspace_id >= 0)
279                 H5.H5Sclose(dataspace_id);
280         }
281         catch (Exception e) {
282             e.printStackTrace();
283         }
284 
285         // Close the file.
286         try {
287             if (file_id >= 0)
288                 H5.H5Fclose(file_id);
289         }
290         catch (Exception e) {
291             e.printStackTrace();
292         }
293     }
294 
readUnlimited()295     private static void readUnlimited() {
296         long file_id = -1;
297         long dataspace_id = -1;
298         long dataset_id = -1;
299         long[] dims = { DIM_X, DIM_Y };
300         int[][] dset_data;
301 
302         // Open an existing file.
303         try {
304             file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT);
305         }
306         catch (Exception e) {
307             e.printStackTrace();
308         }
309 
310         // Open an existing dataset.
311         try {
312             if (file_id >= 0)
313                 dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT);
314         }
315         catch (Exception e) {
316             e.printStackTrace();
317         }
318 
319         // Get dataspace and allocate memory for the read buffer as before.
320         try {
321             if (dataset_id >= 0)
322                 dataspace_id = H5.H5Dget_space(dataset_id);
323         }
324         catch (Exception e) {
325             e.printStackTrace();
326         }
327 
328         try {
329             if (dataspace_id >= 0)
330                 H5.H5Sget_simple_extent_dims(dataspace_id, dims, null);
331         }
332         catch (Exception e) {
333             e.printStackTrace();
334         }
335         // Allocate array of pointers to rows.
336         dset_data = new int[(int) dims[0]][(int) dims[1]];
337 
338         // Read the data using the default properties.
339         try {
340             if (dataset_id >= 0)
341                 H5.H5Dread(dataset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
342                         HDF5Constants.H5P_DEFAULT, dset_data);
343         }
344         catch (Exception e) {
345             e.printStackTrace();
346         }
347 
348         // Output the data to the screen.
349         System.out.println("Dataset after extension:");
350         for (int indx = 0; indx < dims[0]; indx++) {
351             System.out.print(" [ ");
352             for (int jndx = 0; jndx < dims[1]; jndx++)
353                 System.out.print(dset_data[indx][jndx] + " ");
354             System.out.println("]");
355         }
356         System.out.println();
357 
358         // End access to the dataset and release resources used by it.
359         try {
360             if (dataset_id >= 0)
361                 H5.H5Dclose(dataset_id);
362         }
363         catch (Exception e) {
364             e.printStackTrace();
365         }
366 
367         try {
368             if (dataspace_id >= 0)
369                 H5.H5Sclose(dataspace_id);
370         }
371         catch (Exception e) {
372             e.printStackTrace();
373         }
374 
375         // Close the file.
376         try {
377             if (file_id >= 0)
378                 H5.H5Fclose(file_id);
379         }
380         catch (Exception e) {
381             e.printStackTrace();
382         }
383     }
384 
main(String[] args)385     public static void main(String[] args) {
386         H5Ex_D_UnlimitedAdd.writeUnlimited();
387         H5Ex_D_UnlimitedAdd.extendUnlimited();
388         H5Ex_D_UnlimitedAdd.readUnlimited();
389     }
390 
391 }
392