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 
16   This example shows how to read and write data to a
17   dataset.  The program first writes integers to a dataset
18   with dataspace dimensions of DIM_XxDIM_Y, then closes the
19   file.  Next, it reopens the file, reads back the data, and
20   outputs it to the screen.
21  ************************************************************/
22 package examples.datasets;
23 
24 import hdf.hdf5lib.H5;
25 import hdf.hdf5lib.HDF5Constants;
26 
27 public class H5Ex_D_ReadWrite {
28     private static String FILENAME = "H5Ex_D_ReadWrite.h5";
29     private static String DATASETNAME = "DS1";
30     private static final int DIM_X = 4;
31     private static final int DIM_Y = 7;
32     private static final int RANK = 2;
33 
WriteDataset()34     private static void WriteDataset() {
35         long file_id = -1;
36         long filespace_id = -1;
37         long dataset_id = -1;
38         long[] dims = { DIM_X, DIM_Y };
39         int[][] dset_data = new int[DIM_X][DIM_Y];
40 
41         // Initialize data.
42         for (int indx = 0; indx < DIM_X; indx++)
43             for (int jndx = 0; jndx < DIM_Y; jndx++)
44                 dset_data[indx][jndx] = indx * jndx - jndx;
45 
46         // Create a new file using default properties.
47         try {
48             file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT,
49                     HDF5Constants.H5P_DEFAULT);
50         }
51         catch (Exception e) {
52             e.printStackTrace();
53         }
54 
55         // Create dataspace. Setting maximum size to NULL sets the maximum
56         // size to be the current size.
57         try {
58             filespace_id = H5.H5Screate_simple(RANK, dims, null);
59         }
60         catch (Exception e) {
61             e.printStackTrace();
62         }
63 
64         // Create the dataset. We will use all default properties for this example.
65         try {
66             if ((file_id >= 0) && (filespace_id >= 0))
67                 dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I32LE, filespace_id,
68                         HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT);
69         }
70         catch (Exception e) {
71             e.printStackTrace();
72         }
73 
74         // Write the data to the dataset.
75         try {
76             if (dataset_id >= 0)
77                 H5.H5Dwrite(dataset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
78                         HDF5Constants.H5P_DEFAULT, dset_data);
79         }
80         catch (Exception e) {
81             e.printStackTrace();
82         }
83 
84         // End access to the dataset and release resources used by it.
85         try {
86             if (dataset_id >= 0)
87                 H5.H5Dclose(dataset_id);
88         }
89         catch (Exception e) {
90             e.printStackTrace();
91         }
92 
93         try {
94             if (filespace_id >= 0)
95                 H5.H5Sclose(filespace_id);
96         }
97         catch (Exception e) {
98             e.printStackTrace();
99         }
100 
101         // Close the file.
102         try {
103             if (file_id >= 0)
104                 H5.H5Fclose(file_id);
105         }
106         catch (Exception e) {
107             e.printStackTrace();
108         }
109     }
110 
ReadDataset()111     private static void ReadDataset() {
112         long file_id = -1;
113         long dataset_id = -1;
114         int[][] dset_data = new int[DIM_X][DIM_Y];
115 
116         // Open file using the default properties.
117         try {
118             file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDWR, HDF5Constants.H5P_DEFAULT);
119         }
120         catch (Exception e) {
121             e.printStackTrace();
122         }
123 
124         // Open dataset using the default properties.
125         try {
126             if (file_id >= 0)
127                 dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT);
128         }
129         catch (Exception e) {
130             e.printStackTrace();
131         }
132 
133         // Read the data using the default properties.
134         try {
135             if (dataset_id >= 0)
136                 H5.H5Dread(dataset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
137                         HDF5Constants.H5P_DEFAULT, dset_data);
138         }
139         catch (Exception e) {
140             e.printStackTrace();
141         }
142 
143         // Output the data to the screen.
144         System.out.println(DATASETNAME + ":");
145         for (int indx = 0; indx < DIM_X; indx++) {
146             System.out.print(" [ ");
147             for (int jndx = 0; jndx < DIM_Y; jndx++)
148                 System.out.print(dset_data[indx][jndx] + " ");
149             System.out.println("]");
150         }
151         System.out.println();
152 
153         // Close the dataset.
154         try {
155             if (dataset_id >= 0)
156                 H5.H5Dclose(dataset_id);
157         }
158         catch (Exception e) {
159             e.printStackTrace();
160         }
161 
162         // Close the file.
163         try {
164             if (file_id >= 0)
165                 H5.H5Fclose(file_id);
166         }
167         catch (Exception e) {
168             e.printStackTrace();
169         }
170     }
171 
main(String[] args)172     public static void main(String[] args) {
173         H5Ex_D_ReadWrite.WriteDataset();
174         H5Ex_D_ReadWrite.ReadDataset();
175     }
176 
177 }
178