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 opaque datatypes 16 to an attribute. The program first writes opaque data to 17 an attribute with a dataspace of DIM0, 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 hdf.hdf5lib.H5; 25 import hdf.hdf5lib.HDF5Constants; 26 27 public class H5Ex_T_OpaqueAttribute { 28 private static String FILENAME = "H5Ex_T_OpaqueAttribute.h5"; 29 private static String DATASETNAME = "DS1"; 30 private static String ATTRIBUTENAME = "A1"; 31 private static final int DIM0 = 4; 32 private static final int LEN = 7; 33 private static final int RANK = 1; 34 CreateDataset()35 private static void CreateDataset() { 36 long file_id = -1; 37 long dataspace_id = -1; 38 long datatype_id = -1; 39 long dataset_id = -1; 40 long attribute_id = -1; 41 long[] dims = { DIM0 }; 42 byte[] dset_data = new byte[DIM0 * LEN]; 43 byte[] str_data = { 'O', 'P', 'A', 'Q', 'U', 'E' }; 44 45 // Initialize data. 46 for (int indx = 0; indx < DIM0; indx++) { 47 for (int jndx = 0; jndx < LEN - 1; jndx++) 48 dset_data[jndx + indx * LEN] = str_data[jndx]; 49 dset_data[LEN - 1 + indx * LEN] = (byte) (indx + '0'); 50 } 51 52 // Create a new file using default properties. 53 try { 54 file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, 55 HDF5Constants.H5P_DEFAULT); 56 } 57 catch (Exception e) { 58 e.printStackTrace(); 59 } 60 61 // Create dataset with a scalar dataspace. 62 try { 63 dataspace_id = H5.H5Screate(HDF5Constants.H5S_SCALAR); 64 if (dataspace_id >= 0) { 65 dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I32LE, dataspace_id, 66 HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); 67 H5.H5Sclose(dataspace_id); 68 dataspace_id = -1; 69 } 70 } 71 catch (Exception e) { 72 e.printStackTrace(); 73 } 74 75 // Create opaque datatype and set the tag to something appropriate. 76 // For this example we will write and view the data as a character 77 // array. 78 try { 79 datatype_id = H5.H5Tcreate(HDF5Constants.H5T_OPAQUE, (long)LEN); 80 if (datatype_id >= 0) 81 H5.H5Tset_tag(datatype_id, "Character array"); 82 } 83 catch (Exception e) { 84 e.printStackTrace(); 85 } 86 87 // Create dataspace. Setting maximum size to NULL sets the maximum 88 // size to be the current size. 89 try { 90 dataspace_id = H5.H5Screate_simple(RANK, dims, null); 91 } 92 catch (Exception e) { 93 e.printStackTrace(); 94 } 95 96 // Create the attribute and write the array data to it. 97 try { 98 if ((dataset_id >= 0) && (datatype_id >= 0) && (dataspace_id >= 0)) 99 attribute_id = H5.H5Acreate(dataset_id, ATTRIBUTENAME, datatype_id, dataspace_id, 100 HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); 101 } 102 catch (Exception e) { 103 e.printStackTrace(); 104 } 105 106 // Write the dataset. 107 try { 108 if ((attribute_id >= 0) && (datatype_id >= 0)) 109 H5.H5Awrite(attribute_id, datatype_id, dset_data); 110 } 111 catch (Exception e) { 112 e.printStackTrace(); 113 } 114 115 // End access to the dataset and release resources used by it. 116 try { 117 if (attribute_id >= 0) 118 H5.H5Aclose(attribute_id); 119 } 120 catch (Exception e) { 121 e.printStackTrace(); 122 } 123 124 try { 125 if (dataset_id >= 0) 126 H5.H5Dclose(dataset_id); 127 } 128 catch (Exception e) { 129 e.printStackTrace(); 130 } 131 132 // Terminate access to the data space. 133 try { 134 if (dataspace_id >= 0) 135 H5.H5Sclose(dataspace_id); 136 } 137 catch (Exception e) { 138 e.printStackTrace(); 139 } 140 141 try { 142 if (datatype_id >= 0) 143 H5.H5Tclose(datatype_id); 144 } 145 catch (Exception e) { 146 e.printStackTrace(); 147 } 148 149 // Close the file. 150 try { 151 if (file_id >= 0) 152 H5.H5Fclose(file_id); 153 } 154 catch (Exception e) { 155 e.printStackTrace(); 156 } 157 158 } 159 ReadDataset()160 private static void ReadDataset() { 161 long file_id = -1; 162 long datatype_id = -1; 163 long dataspace_id = -1; 164 long dataset_id = -1; 165 long attribute_id = -1; 166 long type_len = -1; 167 long[] dims = { DIM0 }; 168 byte[] dset_data; 169 String tag_name = null; 170 171 // Open an existing file. 172 try { 173 file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); 174 } 175 catch (Exception e) { 176 e.printStackTrace(); 177 } 178 179 // Open an existing dataset. 180 try { 181 if (file_id >= 0) 182 dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); 183 } 184 catch (Exception e) { 185 e.printStackTrace(); 186 } 187 188 try { 189 if (dataset_id >= 0) 190 attribute_id = H5.H5Aopen_by_name(dataset_id, ".", ATTRIBUTENAME, HDF5Constants.H5P_DEFAULT, 191 HDF5Constants.H5P_DEFAULT); 192 } 193 catch (Exception e) { 194 e.printStackTrace(); 195 } 196 197 // Get datatype and properties for the datatype. 198 try { 199 if (attribute_id >= 0) 200 datatype_id = H5.H5Aget_type(attribute_id); 201 if (datatype_id >= 0) { 202 type_len = H5.H5Tget_size(datatype_id); 203 tag_name = H5.H5Tget_tag(datatype_id); 204 } 205 } 206 catch (Exception e) { 207 e.printStackTrace(); 208 } 209 210 // Get dataspace and allocate memory for read buffer. 211 try { 212 if (attribute_id >= 0) 213 dataspace_id = H5.H5Aget_space(attribute_id); 214 } 215 catch (Exception e) { 216 e.printStackTrace(); 217 } 218 219 try { 220 if (dataspace_id >= 0) 221 H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); 222 } 223 catch (Exception e) { 224 e.printStackTrace(); 225 } 226 227 // Allocate buffer. 228 dset_data = new byte[(int) (dims[0] * type_len)]; 229 230 // Read data. 231 try { 232 if ((attribute_id >= 0) && (datatype_id >= 0)) 233 H5.H5Aread(attribute_id, datatype_id, dset_data); 234 } 235 catch (Exception e) { 236 e.printStackTrace(); 237 } 238 239 // Output the data to the screen. 240 System.out.println("Datatype tag for " + ATTRIBUTENAME + " is: \"" + tag_name + "\""); 241 for (int indx = 0; indx < dims[0]; indx++) { 242 System.out.print(ATTRIBUTENAME + "[" + indx + "]: "); 243 for (int jndx = 0; jndx < type_len; jndx++) { 244 char temp = (char) dset_data[jndx + indx * (int)type_len]; 245 System.out.print(temp); 246 } 247 System.out.println(""); 248 } 249 System.out.println(); 250 251 // End access to the dataset and release resources used by it. 252 try { 253 if (attribute_id >= 0) 254 H5.H5Aclose(attribute_id); 255 } 256 catch (Exception e) { 257 e.printStackTrace(); 258 } 259 260 try { 261 if (dataset_id >= 0) 262 H5.H5Dclose(dataset_id); 263 } 264 catch (Exception e) { 265 e.printStackTrace(); 266 } 267 268 // Terminate access to the data space. 269 try { 270 if (dataspace_id >= 0) 271 H5.H5Sclose(dataspace_id); 272 } 273 catch (Exception e) { 274 e.printStackTrace(); 275 } 276 277 try { 278 if (datatype_id >= 0) 279 H5.H5Tclose(datatype_id); 280 } 281 catch (Exception e) { 282 e.printStackTrace(); 283 } 284 285 // Close the file. 286 try { 287 if (file_id >= 0) 288 H5.H5Fclose(file_id); 289 } 290 catch (Exception e) { 291 e.printStackTrace(); 292 } 293 294 } 295 main(String[] args)296 public static void main(String[] args) { 297 H5Ex_T_OpaqueAttribute.CreateDataset(); 298 // Now we begin the read section of this example. Here we assume 299 // the dataset and array have the same name and rank, but can have 300 // any size. Therefore we must allocate a new array to read in 301 // data using malloc(). 302 H5Ex_T_OpaqueAttribute.ReadDataset(); 303 } 304 305 } 306