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_Float {
30     private static String FILENAME = "H5Ex_T_Float.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         double[][] dset_data = new double[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 + 0.5) + 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 floating point data to it. In
68         // this example we will save the data as 64 bit little endian IEEE
69         // floating point numbers, regardless of the native type. The HDF5
70         // library automatically converts between different floating point
71         // types.
72         try {
73             if ((file_id >= 0) && (dataspace_id >= 0))
74                 dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_IEEE_F64LE, dataspace_id,
75                         HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
76         }
77         catch (Exception e) {
78             e.printStackTrace();
79         }
80 
81         // Write the data to the dataset.
82         try {
83             if (dataset_id >= 0)
84                 H5.H5Dwrite(dataset_id, HDF5Constants.H5T_NATIVE_DOUBLE, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
85                         HDF5Constants.H5P_DEFAULT, dset_data);
86         }
87         catch (Exception e) {
88             e.printStackTrace();
89         }
90 
91         // End access to the dataset and release resources used by it.
92         try {
93             if (dataset_id >= 0)
94                 H5.H5Dclose(dataset_id);
95         }
96         catch (Exception e) {
97             e.printStackTrace();
98         }
99 
100         // Terminate access to the data space.
101         try {
102             if (dataspace_id >= 0)
103                 H5.H5Sclose(dataspace_id);
104         }
105         catch (Exception e) {
106             e.printStackTrace();
107         }
108 
109         // Close the file.
110         try {
111             if (file_id >= 0)
112                 H5.H5Fclose(file_id);
113         }
114         catch (Exception e) {
115             e.printStackTrace();
116         }
117 
118     }
119 
ReadDataset()120     private static void ReadDataset() {
121         long file_id = -1;
122         long dataspace_id = -1;
123         long dataset_id = -1;
124         long[] dims = { DIM0, DIM1 };
125         double[][] dset_data;
126 
127         // Open an existing file.
128         try {
129             file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT);
130         }
131         catch (Exception e) {
132             e.printStackTrace();
133         }
134 
135         // Open an existing dataset.
136         try {
137             if (file_id >= 0)
138                 dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT);
139         }
140         catch (Exception e) {
141             e.printStackTrace();
142         }
143 
144         // Get dataspace and allocate memory for read buffer.
145         try {
146             if (dataset_id >= 0)
147                 dataspace_id = H5.H5Dget_space(dataset_id);
148         }
149         catch (Exception e) {
150             e.printStackTrace();
151         }
152 
153         try {
154             if (dataspace_id >= 0)
155                 H5.H5Sget_simple_extent_dims(dataspace_id, dims, null);
156         }
157         catch (Exception e) {
158             e.printStackTrace();
159         }
160 
161         // Allocate array of pointers to two-dimensional arrays (the
162         // elements of the dataset.
163         dset_data = new double[(int) dims[0]][(int) (dims[1])];
164 
165         // Read data.
166         try {
167             if (dataset_id >= 0)
168                 H5.H5Dread(dataset_id, HDF5Constants.H5T_NATIVE_DOUBLE, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
169                         HDF5Constants.H5P_DEFAULT, dset_data);
170         }
171         catch (Exception e) {
172             e.printStackTrace();
173         }
174 
175         // Output the data to the screen.
176         DecimalFormat df = new DecimalFormat("#,##0.0000");
177         System.out.println(DATASETNAME + ":");
178         for (int indx = 0; indx < dims[0]; indx++) {
179             System.out.print(" [");
180             for (int jndx = 0; jndx < dims[1]; jndx++) {
181                 System.out.print(" " + df.format(dset_data[indx][jndx]));
182             }
183             System.out.println("]");
184         }
185         System.out.println();
186 
187         // End access to the dataset and release resources used by it.
188         try {
189             if (dataset_id >= 0)
190                 H5.H5Dclose(dataset_id);
191         }
192         catch (Exception e) {
193             e.printStackTrace();
194         }
195 
196         // Terminate access to the data space.
197         try {
198             if (dataspace_id >= 0)
199                 H5.H5Sclose(dataspace_id);
200         }
201         catch (Exception e) {
202             e.printStackTrace();
203         }
204 
205         // Close the file.
206         try {
207             if (file_id >= 0)
208                 H5.H5Fclose(file_id);
209         }
210         catch (Exception e) {
211             e.printStackTrace();
212         }
213 
214     }
215 
main(String[] args)216     public static void main(String[] args) {
217         H5Ex_T_Float.CreateDataset();
218         // Now we begin the read section of this example. Here we assume
219         // the dataset and array have the same name and rank, but can have
220         // any size. Therefore we must allocate a new array to read in
221         // data using malloc().
222         H5Ex_T_Float.ReadDataset();
223     }
224 
225 }
226