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 create and extend an unlimited 16 dataset. The program first writes integers to a dataset 17 with dataspace dimensions of DIM_XxDIM_Y, then closes the 18 file. Next, it reopens the file, reads back the data, 19 outputs it to the screen, extends the dataset, and writes 20 new data to the entire extended dataset. Finally it 21 reopens the file again, reads back the data, and utputs it 22 to the screen. 23 ************************************************************/ 24 package examples.datasets; 25 26 import hdf.hdf5lib.H5; 27 import hdf.hdf5lib.HDF5Constants; 28 29 public class H5Ex_D_UnlimitedMod { 30 private static String FILENAME = "H5Ex_D_UnlimitedMod.h5"; 31 private static String DATASETNAME = "DS1"; 32 private static final int DIM_X = 4; 33 private static final int DIM_Y = 7; 34 private static final int EDIM_X = 6; 35 private static final int EDIM_Y = 10; 36 private static final int CHUNK_X = 4; 37 private static final int CHUNK_Y = 4; 38 private static final int RANK = 2; 39 private static final int NDIMS = 2; 40 writeUnlimited()41 private static void writeUnlimited() { 42 long file_id = -1; 43 long dcpl_id = -1; 44 long dataspace_id = -1; 45 long dataset_id = -1; 46 long[] dims = { DIM_X, DIM_Y }; 47 long[] chunk_dims = { CHUNK_X, CHUNK_Y }; 48 long[] maxdims = { HDF5Constants.H5S_UNLIMITED, HDF5Constants.H5S_UNLIMITED }; 49 int[][] dset_data = new int[DIM_X][DIM_Y]; 50 51 // Initialize the dataset. 52 for (int indx = 0; indx < DIM_X; indx++) 53 for (int jndx = 0; jndx < DIM_Y; jndx++) 54 dset_data[indx][jndx] = indx * jndx - jndx; 55 56 // Create a new file using default properties. 57 try { 58 file_id = H5.H5Fcreate(FILENAME, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, 59 HDF5Constants.H5P_DEFAULT); 60 } 61 catch (Exception e) { 62 e.printStackTrace(); 63 } 64 65 // Create dataspace with unlimited dimensions. 66 try { 67 dataspace_id = H5.H5Screate_simple(RANK, dims, maxdims); 68 } 69 catch (Exception e) { 70 e.printStackTrace(); 71 } 72 73 // Create the dataset creation property list. 74 try { 75 dcpl_id = H5.H5Pcreate(HDF5Constants.H5P_DATASET_CREATE); 76 } 77 catch (Exception e) { 78 e.printStackTrace(); 79 } 80 81 // Set the chunk size. 82 try { 83 if (dcpl_id >= 0) 84 H5.H5Pset_chunk(dcpl_id, NDIMS, chunk_dims); 85 } 86 catch (Exception e) { 87 e.printStackTrace(); 88 } 89 90 // Create the unlimited dataset. 91 try { 92 if ((file_id >= 0) && (dataspace_id >= 0) && (dcpl_id >= 0)) 93 dataset_id = H5.H5Dcreate(file_id, DATASETNAME, HDF5Constants.H5T_STD_I32LE, dataspace_id, 94 HDF5Constants.H5P_DEFAULT, dcpl_id, HDF5Constants.H5P_DEFAULT); 95 } 96 catch (Exception e) { 97 e.printStackTrace(); 98 } 99 100 // Write the data to the dataset. 101 try { 102 if (dataset_id >= 0) 103 H5.H5Dwrite(dataset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, 104 HDF5Constants.H5P_DEFAULT, dset_data); 105 } 106 catch (Exception e) { 107 e.printStackTrace(); 108 } 109 110 // End access to the dataset and release resources used by it. 111 try { 112 if (dataset_id >= 0) 113 H5.H5Dclose(dataset_id); 114 } 115 catch (Exception e) { 116 e.printStackTrace(); 117 } 118 119 try { 120 if (dataspace_id >= 0) 121 H5.H5Sclose(dataspace_id); 122 } 123 catch (Exception e) { 124 e.printStackTrace(); 125 } 126 127 try { 128 if (dcpl_id >= 0) 129 H5.H5Pclose(dcpl_id); 130 } 131 catch (Exception e) { 132 e.printStackTrace(); 133 } 134 135 // Close the file. 136 try { 137 if (file_id >= 0) 138 H5.H5Fclose(file_id); 139 } 140 catch (Exception e) { 141 e.printStackTrace(); 142 } 143 } 144 extendUnlimited()145 private static void extendUnlimited() { 146 long file_id = -1; 147 long dataspace_id = -1; 148 long dataset_id = -1; 149 long[] dims = { DIM_X, DIM_Y }; 150 long[] extdims = { EDIM_X, EDIM_Y }; 151 int[][] dset_data; 152 int[][] extend_dset_data = new int[EDIM_X][EDIM_Y]; 153 154 // Open an existing file. 155 try { 156 file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDWR, HDF5Constants.H5P_DEFAULT); 157 } 158 catch (Exception e) { 159 e.printStackTrace(); 160 } 161 162 // Open an existing dataset. 163 try { 164 if (file_id >= 0) 165 dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); 166 } 167 catch (Exception e) { 168 e.printStackTrace(); 169 } 170 171 // Get dataspace and allocate memory for read buffer. This is a 172 // two dimensional dataset so the dynamic allocation must be done 173 // in steps. 174 try { 175 if (dataset_id >= 0) 176 dataspace_id = H5.H5Dget_space(dataset_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 rows. 191 dset_data = new int[(int) dims[0]][(int) dims[1]]; 192 193 // Read the data using the default properties. 194 try { 195 if (dataset_id >= 0) 196 H5.H5Dread(dataset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, 197 HDF5Constants.H5P_DEFAULT, dset_data); 198 } 199 catch (Exception e) { 200 e.printStackTrace(); 201 } 202 203 // Output the data to the screen. 204 System.out.println("Dataset before extension:"); 205 for (int indx = 0; indx < DIM_X; indx++) { 206 System.out.print(" [ "); 207 for (int jndx = 0; jndx < DIM_Y; jndx++) 208 System.out.print(dset_data[indx][jndx] + " "); 209 System.out.println("]"); 210 } 211 System.out.println(); 212 213 try { 214 if (dataspace_id >= 0) 215 H5.H5Sclose(dataspace_id); 216 } 217 catch (Exception e) { 218 e.printStackTrace(); 219 } 220 221 // Extend the dataset. 222 try { 223 if (dataset_id >= 0) 224 H5.H5Dset_extent(dataset_id, extdims); 225 } 226 catch (Exception e) { 227 e.printStackTrace(); 228 } 229 230 // Retrieve the dataspace for the newly extended dataset. 231 try { 232 if (dataset_id >= 0) 233 dataspace_id = H5.H5Dget_space(dataset_id); 234 } 235 catch (Exception e) { 236 e.printStackTrace(); 237 } 238 239 // Initialize data for writing to the extended dataset. 240 for (int indx = 0; indx < EDIM_X; indx++) 241 for (int jndx = 0; jndx < EDIM_Y; jndx++) 242 extend_dset_data[indx][jndx] = jndx; 243 244 // Write the data tto the extended dataset. 245 try { 246 if ((dataspace_id >= 0) && (dataset_id >= 0)) 247 H5.H5Dwrite(dataset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, dataspace_id, 248 HDF5Constants.H5P_DEFAULT, extend_dset_data); 249 } 250 catch (Exception e) { 251 e.printStackTrace(); 252 } 253 254 // End access to the dataset and release resources used by it. 255 try { 256 if (dataset_id >= 0) 257 H5.H5Dclose(dataset_id); 258 } 259 catch (Exception e) { 260 e.printStackTrace(); 261 } 262 263 try { 264 if (dataspace_id >= 0) 265 H5.H5Sclose(dataspace_id); 266 } 267 catch (Exception e) { 268 e.printStackTrace(); 269 } 270 271 // Close the file. 272 try { 273 if (file_id >= 0) 274 H5.H5Fclose(file_id); 275 } 276 catch (Exception e) { 277 e.printStackTrace(); 278 } 279 } 280 readUnlimited()281 private static void readUnlimited() { 282 long file_id = -1; 283 long dataspace_id = -1; 284 long dataset_id = -1; 285 long[] dims = { DIM_X, DIM_Y }; 286 int[][] dset_data; 287 288 // Open an existing file. 289 try { 290 file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); 291 } 292 catch (Exception e) { 293 e.printStackTrace(); 294 } 295 296 // Open an existing dataset. 297 try { 298 if (file_id >= 0) 299 dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); 300 } 301 catch (Exception e) { 302 e.printStackTrace(); 303 } 304 305 // Get dataspace and allocate memory for the read buffer as before. 306 try { 307 if (dataset_id >= 0) 308 dataspace_id = H5.H5Dget_space(dataset_id); 309 } 310 catch (Exception e) { 311 e.printStackTrace(); 312 } 313 314 try { 315 if (dataspace_id >= 0) 316 H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); 317 } 318 catch (Exception e) { 319 e.printStackTrace(); 320 } 321 // Allocate array of pointers to rows. 322 dset_data = new int[(int) dims[0]][(int) dims[1]]; 323 324 // Read the data using the default properties. 325 try { 326 if (dataset_id >= 0) 327 H5.H5Dread(dataset_id, HDF5Constants.H5T_NATIVE_INT, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, 328 HDF5Constants.H5P_DEFAULT, dset_data); 329 } 330 catch (Exception e) { 331 e.printStackTrace(); 332 } 333 334 // Output the data to the screen. 335 System.out.println("Dataset after extension:"); 336 for (int indx = 0; indx < dims[0]; indx++) { 337 System.out.print(" [ "); 338 for (int jndx = 0; jndx < dims[1]; jndx++) 339 System.out.print(dset_data[indx][jndx] + " "); 340 System.out.println("]"); 341 } 342 System.out.println(); 343 344 // End access to the dataset and release resources used by it. 345 try { 346 if (dataset_id >= 0) 347 H5.H5Dclose(dataset_id); 348 } 349 catch (Exception e) { 350 e.printStackTrace(); 351 } 352 353 try { 354 if (dataspace_id >= 0) 355 H5.H5Sclose(dataspace_id); 356 } 357 catch (Exception e) { 358 e.printStackTrace(); 359 } 360 361 // Close the file. 362 try { 363 if (file_id >= 0) 364 H5.H5Fclose(file_id); 365 } 366 catch (Exception e) { 367 e.printStackTrace(); 368 } 369 } 370 main(String[] args)371 public static void main(String[] args) { 372 H5Ex_D_UnlimitedMod.writeUnlimited(); 373 H5Ex_D_UnlimitedMod.extendUnlimited(); 374 H5Ex_D_UnlimitedMod.readUnlimited(); 375 } 376 377 } 378