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 bitfield 16 datatypes to a dataset. The program first writes bit 17 fields to a dataset with a dataspace of DIM0xDIM1, then 18 closes the file. Next, it reopens the file, reads back 19 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_Bit { 28 private static String FILENAME = "H5Ex_T_Bit.h5"; 29 private static String DATASETNAME = "DS1"; 30 private static final int DIM0 = 4; 31 private static final int DIM1 = 7; 32 private static final int RANK = 2; 33 CreateDataset()34 private static void CreateDataset() { 35 long file_id = -1; 36 long dataspace_id = -1; 37 long dataset_id = -1; 38 long[] dims = { DIM0, DIM1 }; 39 int[][] dset_data = new int[DIM0][DIM1]; 40 41 // Initialize data. 42 for (int indx = 0; indx < DIM0; indx++) 43 for (int jndx = 0; jndx < DIM1; jndx++) { 44 dset_data[indx][jndx] = 0; 45 dset_data[indx][jndx] |= (indx * jndx - jndx) & 0x03; /* Field "A" */ 46 dset_data[indx][jndx] |= (indx & 0x03) << 2; /* Field "B" */ 47 dset_data[indx][jndx] |= (jndx & 0x03) << 4; /* Field "C" */ 48 dset_data[indx][jndx] |= ((indx + jndx) & 0x03) << 6; /* Field "D" */ 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 dataspace. Setting maximum size to NULL sets the maximum 61 // size to be the current size. 62 try { 63 dataspace_id = H5.H5Screate_simple(RANK, dims, null); 64 } 65 catch (Exception e) { 66 e.printStackTrace(); 67 } 68 69 // Create the dataset. 70 try { 71 if ((file_id >= 0) && (dataspace_id >= 0)) 72 dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_B8BE, dataspace_id, 73 HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); 74 } 75 catch (Exception e) { 76 e.printStackTrace(); 77 } 78 79 // Write the bitfield data to the dataset. 80 try { 81 if (dataset_id >= 0) 82 H5.H5Dwrite(dataset_id, HDF5Constants.H5T_NATIVE_B8, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, 83 HDF5Constants.H5P_DEFAULT, dset_data); 84 } 85 catch (Exception e) { 86 e.printStackTrace(); 87 } 88 89 // End access to the dataset and release resources used by it. 90 try { 91 if (dataset_id >= 0) 92 H5.H5Dclose(dataset_id); 93 } 94 catch (Exception e) { 95 e.printStackTrace(); 96 } 97 98 // Terminate access to the data space. 99 try { 100 if (dataspace_id >= 0) 101 H5.H5Sclose(dataspace_id); 102 } 103 catch (Exception e) { 104 e.printStackTrace(); 105 } 106 107 // Close the file. 108 try { 109 if (file_id >= 0) 110 H5.H5Fclose(file_id); 111 } 112 catch (Exception e) { 113 e.printStackTrace(); 114 } 115 116 } 117 ReadDataset()118 private static void ReadDataset() { 119 long file_id = -1; 120 long dataspace_id = -1; 121 long dataset_id = -1; 122 long[] dims = { DIM0, DIM1 }; 123 int[][] dset_data; 124 125 // Open an existing file. 126 try { 127 file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); 128 } 129 catch (Exception e) { 130 e.printStackTrace(); 131 } 132 133 // Open an existing dataset. 134 try { 135 if (file_id >= 0) 136 dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); 137 } 138 catch (Exception e) { 139 e.printStackTrace(); 140 } 141 142 // Get dataspace and allocate memory for read buffer. 143 try { 144 if (dataset_id >= 0) 145 dataspace_id = H5.H5Dget_space(dataset_id); 146 } 147 catch (Exception e) { 148 e.printStackTrace(); 149 } 150 151 try { 152 if (dataspace_id >= 0) 153 H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); 154 } 155 catch (Exception e) { 156 e.printStackTrace(); 157 } 158 159 // Allocate array of pointers to two-dimensional arrays (the 160 // elements of the dataset. 161 dset_data = new int[(int) dims[0]][(int) (dims[1])]; 162 163 // Read data. 164 try { 165 if (dataset_id >= 0) 166 H5.H5Dread(dataset_id, HDF5Constants.H5T_NATIVE_B8, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, 167 HDF5Constants.H5P_DEFAULT, dset_data); 168 } 169 catch (Exception e) { 170 e.printStackTrace(); 171 } 172 173 // Output the data to the screen. 174 System.out.println(DATASETNAME + ":"); 175 for (int indx = 0; indx < dims[0]; indx++) { 176 System.out.print(" ["); 177 for (int jndx = 0; jndx < dims[1]; jndx++) { 178 System.out.print("{" + (dset_data[indx][jndx] & 0x03) + ", "); 179 System.out.print(((dset_data[indx][jndx] >> 2) & 0x03) + ", "); 180 System.out.print(((dset_data[indx][jndx] >> 4) & 0x03) + ", "); 181 System.out.print(((dset_data[indx][jndx] >> 6) & 0x03) + "}"); 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_Bit.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_Bit.ReadDataset(); 223 } 224 225 } 226