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 array datatypes
16   to an attribute.  The program first writes integers arrays
17   of dimension ADIM0xADIM1 to an attribute with a dataspace
18   of DIM0, then closes the  file.  Next, it reopens the
19   file, reads back the data, and outputs it to the screen.
20  ************************************************************/
21 
22 package examples.datatypes;
23 
24 import hdf.hdf5lib.H5;
25 import hdf.hdf5lib.HDF5Constants;
26 
27 public class H5Ex_T_ArrayAttribute {
28     private static String FILENAME = "H5Ex_T_ArrayAttribute.h5";
29     private static String DATASETNAME = "DS1";
30     private static String ATTRIBUTENAME = "A1";
31     private static final int DIM0 = 4;
32     private static final int ADIM0 = 3;
33     private static final int ADIM1 = 5;
34     private static final int RANK = 1;
35     private static final int NDIMS = 2;
36 
CreateDataset()37     private static void CreateDataset() {
38         long file_id = -1;
39         long filetype_id = -1;
40         long memtype_id = -1;
41         long dataspace_id = -1;
42         long dataset_id = -1;
43         long attribute_id = -1;
44         long[] dims = { DIM0 };
45         long[] adims = { ADIM0, ADIM1 };
46         int[][][] dset_data = new int[DIM0][ADIM0][ADIM1];
47 
48         // Initialize data. indx is the element in the dataspace, jndx and kndx the
49         // elements within the array datatype.
50         for (int indx = 0; indx < DIM0; indx++)
51             for (int jndx = 0; jndx < ADIM0; jndx++)
52                 for (int kndx = 0; kndx < ADIM1; kndx++)
53                     dset_data[indx][jndx][kndx] = indx * jndx - jndx * kndx + indx * kndx;
54 
55         // Create a new file using default properties.
56         try {
57             file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT,
58                     HDF5Constants.H5P_DEFAULT);
59         }
60         catch (Exception e) {
61             e.printStackTrace();
62         }
63 
64         // Create array datatypes for file.
65         try {
66             filetype_id = H5.H5Tarray_create(HDF5Constants.H5T_STD_I64LE, NDIMS, adims);
67         }
68         catch (Exception e) {
69             e.printStackTrace();
70         }
71 
72         // Create array datatypes for memory.
73         try {
74             memtype_id = H5.H5Tarray_create(HDF5Constants.H5T_NATIVE_INT, NDIMS, adims);
75         }
76         catch (Exception e) {
77             e.printStackTrace();
78         }
79 
80         // Create dataset with a scalar dataspace.
81         try {
82             dataspace_id = H5.H5Screate(HDF5Constants.H5S_SCALAR);
83             if (dataspace_id >= 0) {
84                 dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I32LE, dataspace_id,
85                         HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
86                 H5.H5Sclose(dataspace_id);
87                 dataspace_id = -1;
88             }
89         }
90         catch (Exception e) {
91             e.printStackTrace();
92         }
93 
94         // Create dataspace. Setting maximum size to NULL sets the maximum
95         // size to be the current size.
96         try {
97             dataspace_id = H5.H5Screate_simple(RANK, dims, null);
98         }
99         catch (Exception e) {
100             e.printStackTrace();
101         }
102 
103         // Create the attribute and write the array data to it.
104         try {
105             if ((dataset_id >= 0) && (dataspace_id >= 0) && (filetype_id >= 0))
106                 attribute_id = H5.H5Acreate(dataset_id, ATTRIBUTENAME, filetype_id, dataspace_id,
107                         HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
108         }
109         catch (Exception e) {
110             e.printStackTrace();
111         }
112 
113         // Write the dataset.
114         try {
115             if ((attribute_id >= 0) && (memtype_id >= 0))
116                 H5.H5Awrite(attribute_id, memtype_id, dset_data);
117         }
118         catch (Exception e) {
119             e.printStackTrace();
120         }
121 
122         // End access to the dataset and release resources used by it.
123         try {
124             if (attribute_id >= 0)
125                 H5.H5Aclose(attribute_id);
126         }
127         catch (Exception e) {
128             e.printStackTrace();
129         }
130 
131         try {
132             if (dataset_id >= 0)
133                 H5.H5Dclose(dataset_id);
134         }
135         catch (Exception e) {
136             e.printStackTrace();
137         }
138 
139         // Terminate access to the data space.
140         try {
141             if (dataspace_id >= 0)
142                 H5.H5Sclose(dataspace_id);
143         }
144         catch (Exception e) {
145             e.printStackTrace();
146         }
147 
148         // Terminate access to the file type.
149         try {
150             if (filetype_id >= 0)
151                 H5.H5Tclose(filetype_id);
152         }
153         catch (Exception e) {
154             e.printStackTrace();
155         }
156 
157         // Terminate access to the mem type.
158         try {
159             if (memtype_id >= 0)
160                 H5.H5Tclose(memtype_id);
161         }
162         catch (Exception e) {
163             e.printStackTrace();
164         }
165 
166         // Close the file.
167         try {
168             if (file_id >= 0)
169                 H5.H5Fclose(file_id);
170         }
171         catch (Exception e) {
172             e.printStackTrace();
173         }
174 
175     }
176 
ReadDataset()177     private static void ReadDataset() {
178         long file_id = -1;
179         long filetype_id = -1;
180         long memtype_id = -1;
181         long dataset_id = -1;
182         long attribute_id = -1;
183         long[] dims = { DIM0 };
184         long[] adims = { ADIM0, ADIM1 };
185         int[][][] dset_data;
186 
187         // Open an existing file.
188         try {
189             file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT);
190         }
191         catch (Exception e) {
192             e.printStackTrace();
193         }
194 
195         // Open an existing dataset.
196         try {
197             if (file_id >= 0)
198                 dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT);
199         }
200         catch (Exception e) {
201             e.printStackTrace();
202         }
203 
204         try {
205             if (dataset_id >= 0)
206                 attribute_id = H5.H5Aopen_by_name(dataset_id, ".", ATTRIBUTENAME, HDF5Constants.H5P_DEFAULT,
207                         HDF5Constants.H5P_DEFAULT);
208         }
209         catch (Exception e) {
210             e.printStackTrace();
211         }
212 
213         // Get the datatype.
214         try {
215             if (attribute_id >= 0)
216                 filetype_id = H5.H5Aget_type(attribute_id);
217         }
218         catch (Exception e) {
219             e.printStackTrace();
220         }
221 
222         // Get the datatype's dimensions.
223         try {
224             if (filetype_id >= 0)
225                 H5.H5Tget_array_dims(filetype_id, adims);
226         }
227         catch (Exception e) {
228             e.printStackTrace();
229         }
230 
231         // Allocate array of pointers to two-dimensional arrays (the
232         // elements of the dataset.
233         dset_data = new int[(int) dims[0]][(int) (adims[0])][(int) (adims[1])];
234 
235         // Create array datatypes for memory.
236         try {
237             memtype_id = H5.H5Tarray_create(HDF5Constants.H5T_NATIVE_INT, 2, adims);
238         }
239         catch (Exception e) {
240             e.printStackTrace();
241         }
242 
243         // Read data.
244         try {
245             if ((attribute_id >= 0) && (memtype_id >= 0))
246                 H5.H5Aread(attribute_id, memtype_id, dset_data);
247         }
248         catch (Exception e) {
249             e.printStackTrace();
250         }
251 
252         // Output the data to the screen.
253         for (int indx = 0; indx < dims[0]; indx++) {
254             System.out.println(ATTRIBUTENAME + " [" + indx + "]:");
255             for (int jndx = 0; jndx < adims[0]; jndx++) {
256                 System.out.print(" [");
257                 for (int kndx = 0; kndx < adims[1]; kndx++)
258                     System.out.print(dset_data[indx][jndx][kndx] + " ");
259                 System.out.println("]");
260             }
261             System.out.println();
262         }
263         System.out.println();
264 
265         // End access to the dataset and release resources used by it.
266         try {
267             if (attribute_id >= 0)
268                 H5.H5Aclose(attribute_id);
269         }
270         catch (Exception e) {
271             e.printStackTrace();
272         }
273 
274         try {
275             if (dataset_id >= 0)
276                 H5.H5Dclose(dataset_id);
277         }
278         catch (Exception e) {
279             e.printStackTrace();
280         }
281 
282         // Terminate access to the file type.
283         try {
284             if (filetype_id >= 0)
285                 H5.H5Tclose(filetype_id);
286         }
287         catch (Exception e) {
288             e.printStackTrace();
289         }
290 
291         // Terminate access to the mem type.
292         try {
293             if (memtype_id >= 0)
294                 H5.H5Tclose(memtype_id);
295         }
296         catch (Exception e) {
297             e.printStackTrace();
298         }
299 
300         // Close the file.
301         try {
302             if (file_id >= 0)
303                 H5.H5Fclose(file_id);
304         }
305         catch (Exception e) {
306             e.printStackTrace();
307         }
308 
309     }
310 
main(String[] args)311     public static void main(String[] args) {
312         H5Ex_T_ArrayAttribute.CreateDataset();
313         // Now we begin the read section of this example. Here we assume
314         // the dataset and array have the same name and rank, but can have
315         // any size. Therefore we must allocate a new array to read in
316         // data using malloc().
317         H5Ex_T_ArrayAttribute.ReadDataset();
318     }
319 
320 }
321