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 read and write object references
16   to an attribute.  The program first creates objects in the
17   file and writes references to those objects to an
18   attribute with a dataspace of DIM0, then closes the file.
19   Next, it reopens the file, dereferences the references,
20   and outputs the names of their targets to the screen.
21  ************************************************************/
22 
23 package examples.datatypes;
24 
25 import java.util.EnumSet;
26 import java.util.HashMap;
27 import java.util.Map;
28 
29 import hdf.hdf5lib.H5;
30 import hdf.hdf5lib.HDF5Constants;
31 
32 public class H5Ex_T_ObjectReferenceAttribute {
33     private static String FILENAME = "H5Ex_T_ObjectReferenceAttribute.h5";
34     private static String DATASETNAME = "DS1";
35     private static String ATTRIBUTENAME = "A1";
36     private static String DATASETNAME2 = "DS2";
37     private static String GROUPNAME = "G1";
38     private static final int DIM0 = 2;
39     private static final int RANK = 1;
40 
41     // Values for the status of space allocation
42     enum H5G_obj {
43         H5G_UNKNOWN(HDF5Constants.H5O_TYPE_UNKNOWN), /* Unknown object type */
44         H5G_GROUP(HDF5Constants.H5O_TYPE_GROUP), /* Object is a group */
45         H5G_DATASET(HDF5Constants.H5O_TYPE_DATASET), /* Object is a dataset */
46         H5G_TYPE(HDF5Constants.H5O_TYPE_NAMED_DATATYPE); /* Object is a named data type */
47         private static final Map<Integer, H5G_obj> lookup = new HashMap<Integer, H5G_obj>();
48 
49         static {
50             for (H5G_obj s : EnumSet.allOf(H5G_obj.class))
s.getCode()51                 lookup.put(s.getCode(), s);
52         }
53 
54         private int code;
55 
H5G_obj(int layout_type)56         H5G_obj(int layout_type) {
57             this.code = layout_type;
58         }
59 
getCode()60         public int getCode() {
61             return this.code;
62         }
63 
get(int code)64         public static H5G_obj get(int code) {
65             return lookup.get(code);
66         }
67     }
68 
CreateDataset()69     private static void CreateDataset() {
70         long file_id = -1;
71         long dataspace_id = -1;
72         long group_id = -1;
73         long dataset_id = -1;
74         long attribute_id = -1;
75         long[] dims = { DIM0 };
76         byte[][] dset_data = new byte[DIM0][8];
77 
78         // Create a new file using default properties.
79         try {
80             file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT,
81                     HDF5Constants.H5P_DEFAULT);
82         }
83         catch (Exception e) {
84             e.printStackTrace();
85         }
86 
87         // Create dataset with a scalar dataspace.
88         try {
89             dataspace_id = H5.H5Screate(HDF5Constants.H5S_SCALAR);
90             if (dataspace_id >= 0) {
91                 dataset_id = H5.H5Dcreate(file_id, DATASETNAME2, HDF5Constants.H5T_STD_I32LE, dataspace_id,
92                         HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
93                 if (dataset_id >= 0)
94                     H5.H5Dclose(dataset_id);
95                 dataset_id = -1;
96                 H5.H5Sclose(dataspace_id);
97                 dataspace_id = -1;
98             }
99         }
100         catch (Exception e) {
101             e.printStackTrace();
102         }
103 
104         // Create a group in the file.
105         try {
106             if (file_id >= 0)
107                 group_id = H5.H5Gcreate(file_id, GROUPNAME, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT,
108                         HDF5Constants.H5P_DEFAULT);
109             if (group_id >= 0)
110                 H5.H5Gclose(group_id);
111             group_id = -1;
112         }
113         catch (Exception e) {
114             e.printStackTrace();
115         }
116 
117         // Create references to the previously created objects. Passing -1
118         // as space_id causes this parameter to be ignored. Other values
119         // besides valid dataspaces result in an error.
120         try {
121             if (file_id >= 0) {
122                 byte rbuf0[] = H5.H5Rcreate(file_id, GROUPNAME, HDF5Constants.H5R_OBJECT, -1);
123                 byte rbuf1[] = H5.H5Rcreate(file_id, DATASETNAME2, HDF5Constants.H5R_OBJECT, -1);
124                 for (int indx = 0; indx < 8; indx++) {
125                     dset_data[0][indx] = rbuf0[indx];
126                     dset_data[1][indx] = rbuf1[indx];
127                 }
128             }
129         }
130         catch (Exception e) {
131             e.printStackTrace();
132         }
133 
134         // Create dataset with a scalar dataspace to serve as the parent
135         // for the attribute.
136         try {
137             dataspace_id = H5.H5Screate(HDF5Constants.H5S_SCALAR);
138             if (dataspace_id >= 0) {
139                 dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I32LE, dataspace_id,
140                         HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
141                 H5.H5Sclose(dataspace_id);
142                 dataspace_id = -1;
143             }
144         }
145         catch (Exception e) {
146             e.printStackTrace();
147         }
148 
149         // Create dataspace. Setting maximum size to NULL sets the maximum
150         // size to be the current size.
151         try {
152             dataspace_id = H5.H5Screate_simple(RANK, dims, null);
153         }
154         catch (Exception e) {
155             e.printStackTrace();
156         }
157 
158         // Create the attribute and write the array data to it.
159         try {
160             if ((dataset_id >= 0) && (dataspace_id >= 0))
161                 attribute_id = H5.H5Acreate(dataset_id, ATTRIBUTENAME, HDF5Constants.H5T_STD_REF_OBJ, dataspace_id,
162                         HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
163         }
164         catch (Exception e) {
165             e.printStackTrace();
166         }
167 
168         // Write the dataset.
169         try {
170             if (attribute_id >= 0)
171                 H5.H5Awrite(attribute_id, HDF5Constants.H5T_STD_REF_OBJ, dset_data);
172         }
173         catch (Exception e) {
174             e.printStackTrace();
175         }
176 
177         // End access to the dataset and release resources used by it.
178         try {
179             if (attribute_id >= 0)
180                 H5.H5Aclose(attribute_id);
181         }
182         catch (Exception e) {
183             e.printStackTrace();
184         }
185 
186         try {
187             if (dataset_id >= 0)
188                 H5.H5Dclose(dataset_id);
189         }
190         catch (Exception e) {
191             e.printStackTrace();
192         }
193 
194         // Terminate access to the data space.
195         try {
196             if (dataspace_id >= 0)
197                 H5.H5Sclose(dataspace_id);
198         }
199         catch (Exception e) {
200             e.printStackTrace();
201         }
202 
203         // Close the file.
204         try {
205             if (file_id >= 0)
206                 H5.H5Fclose(file_id);
207         }
208         catch (Exception e) {
209             e.printStackTrace();
210         }
211 
212     }
213 
ReadDataset()214     private static void ReadDataset() {
215         long file_id = -1;
216         long dataspace_id = -1;
217         long dataset_id = -1;
218         long attribute_id = -1;
219         int object_type = -1;
220         long object_id = -1;
221         long[] dims = { DIM0 };
222         byte[][] dset_data;
223 
224         // Open an existing file.
225         try {
226             file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT);
227         }
228         catch (Exception e) {
229             e.printStackTrace();
230         }
231 
232         // Open an existing dataset.
233         try {
234             if (file_id >= 0)
235                 dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT);
236         }
237         catch (Exception e) {
238             e.printStackTrace();
239         }
240 
241         try {
242             if (dataset_id >= 0)
243                 attribute_id = H5.H5Aopen_by_name(dataset_id, ".", ATTRIBUTENAME, HDF5Constants.H5P_DEFAULT,
244                         HDF5Constants.H5P_DEFAULT);
245         }
246         catch (Exception e) {
247             e.printStackTrace();
248         }
249 
250         // Get dataspace and allocate memory for read buffer.
251         try {
252             if (attribute_id >= 0)
253                 dataspace_id = H5.H5Aget_space(attribute_id);
254         }
255         catch (Exception e) {
256             e.printStackTrace();
257         }
258 
259         try {
260             if (dataspace_id >= 0)
261                 H5.H5Sget_simple_extent_dims(dataspace_id, dims, null);
262         }
263         catch (Exception e) {
264             e.printStackTrace();
265         }
266 
267         // Allocate array of pointers to two-dimensional arrays (the
268         // elements of the dataset.
269         dset_data = new byte[(int) dims[0]][8];
270 
271         // Read data.
272         try {
273             if (attribute_id >= 0)
274                 H5.H5Aread(attribute_id, HDF5Constants.H5T_STD_REF_OBJ, dset_data);
275         }
276         catch (Exception e) {
277             e.printStackTrace();
278         }
279 
280         // Output the data to the screen.
281         for (int indx = 0; indx < dims[0]; indx++) {
282             System.out.println(ATTRIBUTENAME + "[" + indx + "]:");
283             System.out.print("  ->");
284             // Open the referenced object, get its name and type.
285             try {
286                 if (dataset_id >= 0) {
287                     object_id = H5.H5Rdereference(dataset_id, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5R_OBJECT, dset_data[indx]);
288                     object_type = H5.H5Rget_obj_type(dataset_id, HDF5Constants.H5R_OBJECT, dset_data[indx]);
289                 }
290                 String obj_name = null;
291                 if (object_type >= 0) {
292                     // Get the length of the name and retrieve the name.
293                     obj_name = H5.H5Iget_name(object_id);
294                 }
295                 if ((object_id >= 0) && (object_type >= -1)) {
296                     switch (H5G_obj.get(object_type)) {
297                     case H5G_GROUP:
298                         System.out.print("H5G_GROUP");
299                         try {
300                             if (object_id >= 0)
301                                 H5.H5Gclose(object_id);
302                         }
303                         catch (Exception e) {
304                             e.printStackTrace();
305                         }
306                         break;
307                     case H5G_DATASET:
308                         System.out.print("H5G_DATASET");
309                         try {
310                             if (object_id >= 0)
311                                 H5.H5Dclose(object_id);
312                         }
313                         catch (Exception e) {
314                             e.printStackTrace();
315                         }
316                         break;
317                     case H5G_TYPE:
318                         System.out.print("H5G_TYPE");
319                         try {
320                             if (object_id >= 0)
321                                 H5.H5Tclose(object_id);
322                         }
323                         catch (Exception e) {
324                             e.printStackTrace();
325                         }
326                         break;
327                     default:
328                         System.out.print("UNHANDLED");
329                     }
330                 }
331                 // Print the name.
332                 System.out.println(": " + obj_name);
333             }
334             catch (Exception e) {
335                 e.printStackTrace();
336             }
337         }
338 
339         // End access to the dataset and release resources used by it.
340         try {
341             if (attribute_id >= 0)
342                 H5.H5Aclose(attribute_id);
343         }
344         catch (Exception e) {
345             e.printStackTrace();
346         }
347 
348         try {
349             if (dataset_id >= 0)
350                 H5.H5Dclose(dataset_id);
351         }
352         catch (Exception e) {
353             e.printStackTrace();
354         }
355 
356         // Terminate access to the data space.
357         try {
358             if (dataspace_id >= 0)
359                 H5.H5Sclose(dataspace_id);
360         }
361         catch (Exception e) {
362             e.printStackTrace();
363         }
364 
365         // Close the file.
366         try {
367             if (file_id >= 0)
368                 H5.H5Fclose(file_id);
369         }
370         catch (Exception e) {
371             e.printStackTrace();
372         }
373 
374     }
375 
main(String[] args)376     public static void main(String[] args) {
377         H5Ex_T_ObjectReferenceAttribute.CreateDataset();
378         // Now we begin the read section of this example. Here we assume
379         // the dataset and array have the same name and rank, but can have
380         // any size. Therefore we must allocate a new array to read in
381         // data using malloc().
382         H5Ex_T_ObjectReferenceAttribute.ReadDataset();
383     }
384 
385 }
386