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 integer datatypes
16   to a dataset.  The program first writes integers to a
17   dataset with a dataspace of DIM0xDIM1, then closes the
18   file.  Next, it reopens the file, reads back the data, and
19   outputs it to the screen.
20  ************************************************************/
21 
22 package examples.datatypes;
23 
24 import java.text.DecimalFormat;
25 
26 import hdf.hdf5lib.H5;
27 import hdf.hdf5lib.HDF5Constants;
28 
29 public class H5Ex_T_Integer {
30     private static String FILENAME = "H5Ex_T_Integer.h5";
31     private static String DATASETNAME = "DS1";
32     private static final int DIM0 = 4;
33     private static final int DIM1 = 7;
34     private static final int RANK = 2;
35 
CreateDataset()36     private static void CreateDataset() {
37         long file_id = -1;
38         long dataspace_id = -1;
39         long dataset_id = -1;
40         long[] dims = { DIM0, DIM1 };
41         int[][] dset_data = new int[DIM0][DIM1];
42 
43         // Initialize data.
44         for (int indx = 0; indx < DIM0; indx++)
45             for (int jndx = 0; jndx < DIM1; jndx++) {
46                 dset_data[indx][jndx] = indx * jndx - jndx;
47             }
48 
49         // Create a new file using default properties.
50         try {
51             file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT,
52                     HDF5Constants.H5P_DEFAULT);
53         }
54         catch (Exception e) {
55             e.printStackTrace();
56         }
57 
58         // Create dataspace. Setting maximum size to NULL sets the maximum
59         // size to be the current size.
60         try {
61             dataspace_id = H5.H5Screate_simple(RANK, dims, null);
62         }
63         catch (Exception e) {
64             e.printStackTrace();
65         }
66 
67         // Create the dataset and write the integer data to it. In this
68         // example we will save the data as 64 bit big endian integers,
69         // regardless of the native integer type. The HDF5 library
70         // automatically converts between different integer types.
71         try {
72             if ((file_id >= 0) && (dataspace_id >= 0))
73                 dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I64BE, dataspace_id,
74                         HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
75         }
76         catch (Exception e) {
77             e.printStackTrace();
78         }
79 
80         // Write the data to the dataset.
81         try {
82             if (dataset_id >= 0)
83                 H5.H5Dwrite(dataset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
84                         HDF5Constants.H5P_DEFAULT, dset_data);
85         }
86         catch (Exception e) {
87             e.printStackTrace();
88         }
89 
90         // End access to the dataset and release resources used by it.
91         try {
92             if (dataset_id >= 0)
93                 H5.H5Dclose(dataset_id);
94         }
95         catch (Exception e) {
96             e.printStackTrace();
97         }
98 
99         // Terminate access to the data space.
100         try {
101             if (dataspace_id >= 0)
102                 H5.H5Sclose(dataspace_id);
103         }
104         catch (Exception e) {
105             e.printStackTrace();
106         }
107 
108         // Close the file.
109         try {
110             if (file_id >= 0)
111                 H5.H5Fclose(file_id);
112         }
113         catch (Exception e) {
114             e.printStackTrace();
115         }
116 
117     }
118 
ReadDataset()119     private static void ReadDataset() {
120         long file_id = -1;
121         long dataspace_id = -1;
122         long dataset_id = -1;
123         long[] dims = { DIM0, DIM1 };
124         int[][] dset_data;
125 
126         // Open an existing file.
127         try {
128             file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT);
129         }
130         catch (Exception e) {
131             e.printStackTrace();
132         }
133 
134         // Open an existing dataset.
135         try {
136             if (file_id >= 0)
137                 dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT);
138         }
139         catch (Exception e) {
140             e.printStackTrace();
141         }
142 
143         // Get dataspace and allocate memory for read buffer.
144         try {
145             if (dataset_id >= 0)
146                 dataspace_id = H5.H5Dget_space(dataset_id);
147         }
148         catch (Exception e) {
149             e.printStackTrace();
150         }
151 
152         try {
153             if (dataspace_id >= 0)
154                 H5.H5Sget_simple_extent_dims(dataspace_id, dims, null);
155         }
156         catch (Exception e) {
157             e.printStackTrace();
158         }
159 
160         // Allocate array of pointers to two-dimensional arrays (the
161         // elements of the dataset.
162         dset_data = new int[(int) dims[0]][(int) (dims[1])];
163 
164         // Read data.
165         try {
166             if (dataset_id >= 0)
167                 H5.H5Dread(dataset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
168                         HDF5Constants.H5P_DEFAULT, dset_data);
169         }
170         catch (Exception e) {
171             e.printStackTrace();
172         }
173 
174         // Output the data to the screen.
175         DecimalFormat df = new DecimalFormat("#,##0");
176         System.out.println(DATASETNAME + ":");
177         for (int indx = 0; indx < dims[0]; indx++) {
178             System.out.print(" [");
179             for (int jndx = 0; jndx < dims[1]; jndx++) {
180                 System.out.print(" " + df.format(dset_data[indx][jndx]));
181             }
182             System.out.println("]");
183         }
184         System.out.println();
185 
186         // End access to the dataset and release resources used by it.
187         try {
188             if (dataset_id >= 0)
189                 H5.H5Dclose(dataset_id);
190         }
191         catch (Exception e) {
192             e.printStackTrace();
193         }
194 
195         // Terminate access to the data space.
196         try {
197             if (dataspace_id >= 0)
198                 H5.H5Sclose(dataspace_id);
199         }
200         catch (Exception e) {
201             e.printStackTrace();
202         }
203 
204         // Close the file.
205         try {
206             if (file_id >= 0)
207                 H5.H5Fclose(file_id);
208         }
209         catch (Exception e) {
210             e.printStackTrace();
211         }
212 
213     }
214 
main(String[] args)215     public static void main(String[] args) {
216         H5Ex_T_Integer.CreateDataset();
217         // Now we begin the read section of this example. Here we assume
218         // the dataset and array have the same name and rank, but can have
219         // any size. Therefore we must allocate a new array to read in
220         // data using malloc().
221         H5Ex_T_Integer.ReadDataset();
222     }
223 
224 }
225