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 floating point
16   datatypes to an attribute.  The program first writes
17   floating point numbers to an attribute with a dataspace of
18   DIM0xDIM1, 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 java.text.DecimalFormat;
25 
26 import hdf.hdf5lib.H5;
27 import hdf.hdf5lib.HDF5Constants;
28 
29 public class H5Ex_T_FloatAttribute {
30     private static String FILENAME = "H5Ex_T_FloatAttribute.h5";
31     private static String DATASETNAME = "DS1";
32     private static String ATTRIBUTENAME = "A1";
33     private static final int DIM0 = 4;
34     private static final int DIM1 = 7;
35     private static final int RANK = 2;
36 
CreateDataset()37     private static void CreateDataset() {
38         long file_id = -1;
39         long dataspace_id = -1;
40         long dataset_id = -1;
41         long attribute_id = -1;
42         long[] dims = { DIM0, DIM1 };
43         double[][] dset_data = new double[DIM0][DIM1];
44 
45         // Initialize data.
46         for (int indx = 0; indx < DIM0; indx++)
47             for (int jndx = 0; jndx < DIM1; jndx++) {
48                 dset_data[indx][jndx] = indx / (jndx + 0.5) + jndx;
49             }
50 
51         // Create a new file using default properties.
52         try {
53             file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT,
54                     HDF5Constants.H5P_DEFAULT);
55         }
56         catch (Exception e) {
57             e.printStackTrace();
58         }
59 
60         // Create dataset with a scalar dataspace.
61         try {
62             dataspace_id = H5.H5Screate(HDF5Constants.H5S_SCALAR);
63             if (dataspace_id >= 0) {
64                 dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I32LE, dataspace_id,
65                         HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
66                 H5.H5Sclose(dataspace_id);
67                 dataspace_id = -1;
68             }
69         }
70         catch (Exception e) {
71             e.printStackTrace();
72         }
73 
74         // Create dataspace. Setting maximum size to NULL sets the maximum
75         // size to be the current size.
76         try {
77             dataspace_id = H5.H5Screate_simple(RANK, dims, null);
78         }
79         catch (Exception e) {
80             e.printStackTrace();
81         }
82 
83         // Create the attribute and write the array data to it.
84         try {
85             if ((dataset_id >= 0) && (dataspace_id >= 0))
86                 attribute_id = H5.H5Acreate(dataset_id, ATTRIBUTENAME, HDF5Constants.H5T_IEEE_F64LE, dataspace_id,
87                         HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
88         }
89         catch (Exception e) {
90             e.printStackTrace();
91         }
92 
93         // Write the dataset.
94         try {
95             if (attribute_id >= 0)
96                 H5.H5Awrite(attribute_id, HDF5Constants.H5T_NATIVE_DOUBLE, dset_data);
97         }
98         catch (Exception e) {
99             e.printStackTrace();
100         }
101 
102         // End access to the dataset and release resources used by it.
103         try {
104             if (attribute_id >= 0)
105                 H5.H5Aclose(attribute_id);
106         }
107         catch (Exception e) {
108             e.printStackTrace();
109         }
110 
111         try {
112             if (dataset_id >= 0)
113                 H5.H5Dclose(dataset_id);
114         }
115         catch (Exception e) {
116             e.printStackTrace();
117         }
118 
119         // Terminate access to the data space.
120         try {
121             if (dataspace_id >= 0)
122                 H5.H5Sclose(dataspace_id);
123         }
124         catch (Exception e) {
125             e.printStackTrace();
126         }
127 
128         // Close the file.
129         try {
130             if (file_id >= 0)
131                 H5.H5Fclose(file_id);
132         }
133         catch (Exception e) {
134             e.printStackTrace();
135         }
136 
137     }
138 
ReadDataset()139     private static void ReadDataset() {
140         long file_id = -1;
141         long dataspace_id = -1;
142         long dataset_id = -1;
143         long attribute_id = -1;
144         long[] dims = { DIM0, DIM1 };
145         double[][] dset_data;
146 
147         // Open an existing file.
148         try {
149             file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT);
150         }
151         catch (Exception e) {
152             e.printStackTrace();
153         }
154 
155         // Open an existing dataset.
156         try {
157             if (file_id >= 0)
158                 dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT);
159         }
160         catch (Exception e) {
161             e.printStackTrace();
162         }
163 
164         try {
165             if (dataset_id >= 0)
166                 attribute_id = H5.H5Aopen_by_name(dataset_id, ".", ATTRIBUTENAME, HDF5Constants.H5P_DEFAULT,
167                         HDF5Constants.H5P_DEFAULT);
168         }
169         catch (Exception e) {
170             e.printStackTrace();
171         }
172 
173         // Get dataspace and allocate memory for read buffer.
174         try {
175             if (attribute_id >= 0)
176                 dataspace_id = H5.H5Aget_space(attribute_id);
177         }
178         catch (Exception e) {
179             e.printStackTrace();
180         }
181 
182         try {
183             if (dataspace_id >= 0)
184                 H5.H5Sget_simple_extent_dims(dataspace_id, dims, null);
185         }
186         catch (Exception e) {
187             e.printStackTrace();
188         }
189 
190         // Allocate array of pointers to two-dimensional arrays (the
191         // elements of the dataset.
192         dset_data = new double[(int) dims[0]][(int) (dims[1])];
193 
194         // Read data.
195         try {
196             if (attribute_id >= 0)
197                 H5.H5Aread(attribute_id, HDF5Constants.H5T_NATIVE_DOUBLE, dset_data);
198         }
199         catch (Exception e) {
200             e.printStackTrace();
201         }
202 
203         // Output the data to the screen.
204         DecimalFormat df = new DecimalFormat("#,##0.0000");
205         System.out.println(ATTRIBUTENAME + ":");
206         for (int indx = 0; indx < dims[0]; indx++) {
207             System.out.print(" [");
208             for (int jndx = 0; jndx < dims[1]; jndx++) {
209                 System.out.print(" " + df.format(dset_data[indx][jndx]));
210             }
211             System.out.println("]");
212         }
213         System.out.println();
214 
215         // End access to the dataset and release resources used by it.
216         try {
217             if (attribute_id >= 0)
218                 H5.H5Aclose(attribute_id);
219         }
220         catch (Exception e) {
221             e.printStackTrace();
222         }
223 
224         try {
225             if (dataset_id >= 0)
226                 H5.H5Dclose(dataset_id);
227         }
228         catch (Exception e) {
229             e.printStackTrace();
230         }
231 
232         // Terminate access to the data space.
233         try {
234             if (dataspace_id >= 0)
235                 H5.H5Sclose(dataspace_id);
236         }
237         catch (Exception e) {
238             e.printStackTrace();
239         }
240 
241         // Close the file.
242         try {
243             if (file_id >= 0)
244                 H5.H5Fclose(file_id);
245         }
246         catch (Exception e) {
247             e.printStackTrace();
248         }
249 
250     }
251 
main(String[] args)252     public static void main(String[] args) {
253         H5Ex_T_FloatAttribute.CreateDataset();
254         // Now we begin the read section of this example. Here we assume
255         // the dataset and array have the same name and rank, but can have
256         // any size. Therefore we must allocate a new array to read in
257         // data using malloc().
258         H5Ex_T_FloatAttribute.ReadDataset();
259     }
260 
261 }
262