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