summaryrefslogtreecommitdiffstats
path: root/doc/html/Tutor/examples/java/HyperSlab.java
blob: 5f8818d48b274b7916e12473a73c9fe587578390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/******************************************************************
 * HyperSlab.java (for HDF5 tutorial lesson 12)
 *
 *   -- Writing and reading a hyperslab
 *      (a java conversion from h5_hyperslab.c)
 *
 ******************************************************************/

import ncsa.hdf.hdf5lib.*;
import ncsa.hdf.hdf5lib.exceptions.*;

public class HyperSlab
{
   public static void main (String []argv) 
   {
      final String FILE = "sds.h5";
      final String DATASETNAME = "IntArray";
      final int NX_SUB = 3;                /* hyperslab dimensions */ 
      final int NY_SUB = 4;
      final int NX = 7;                    /* output buffer dimensions */ 
      final int NY = 7; 
      final int NZ = 3; 
      final int RANK = 2;
      final int RANK_OUT = 3;
      final int X = 5;                     /* dataset dimensions */
      final int Y = 6;
  
      long[] dimsf = new long[2];          /* dataset dimensions */
      int[][] data = new int[X][Y];        /* data to write */

    /* 
     * Data  and output buffer initialization. 
     */
      int file, dataset;                    /* handles */
      int dataspace;   
      int memspace; 
      long[] dimsm = new long[3];           /* memory space dimensions */
      long[] dims_out = new long[2];        /* dataset dimensions */      
      int status;                             
      
      int[][][] data_out = new int[NX][NY][NZ]; /* output buffer */
      
      long[] count = new long[2];      /* size of the hyperslab in the file */
      long[] offset = new long[2];     /* hyperslab offset in the file */
      long[] count_out = new long[3];  /* size of the hyperslab in memory */
      long[] offset_out = new long[3]; /* hyperslab offset in memory */
      int i, j, k, status_n, rank;
       
      /*********************************************************  
		 This writes data to the HDF5 file.  
      *********************************************************/  
      
      /* 
       * Data  and output buffer initialization. 
       */
      for (j = 0; j < X; j++) 
      {
	 for (i = 0; i < Y; i++)
	    data[j][i] = i + j;
      }     
      /*
       * 0 1 2 3 4 5 
       * 1 2 3 4 5 6
       * 2 3 4 5 6 7
       * 3 4 5 6 7 8
       * 4 5 6 7 8 9
       */

      /*
       * Create a new file using H5F_ACC_TRUNC access,
       * the default file creation properties, and the default file
       * access properties.
       */
      file = H5Fcreate_wrap (FILE, HDF5Constants.H5F_ACC_TRUNC, 
			     HDF5Constants.H5P_DEFAULT, 
			     HDF5Constants.H5P_DEFAULT);
      
      /*
       * Describe the size of the array and create the data space for fixed
       * size dataset. 
       */
      dimsf[0] = X;
      dimsf[1] = Y;
      dataspace = H5Screate_simple_wrap (RANK, dimsf, null); 

      /*
       * Create a new dataset within the file using defined dataspace and
       * default dataset creation properties.
       */
      dataset = H5Dcreate_wrap 
	  (file, DATASETNAME, H5.J2C (HDF5CDataTypes.JH5T_STD_I32BE), 
	   dataspace, HDF5Constants.H5P_DEFAULT);
      
      /*
       * Write the data to the dataset using default transfer properties.
       */
      status = H5Dwrite_wrap 
	  (dataset, H5.J2C (HDF5CDataTypes.JH5T_NATIVE_INT), 
	   HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL,
	   HDF5Constants.H5P_DEFAULT, data);
      
      /*
       * Close/release resources.
       */
      H5Sclose_wrap (dataspace);
      H5Dclose_wrap (dataset);
      H5Fclose_wrap (file);
      
      /*************************************************************  

  This reads the hyperslab from the sds.h5 file just 
  created, into a 2-dimensional plane of the 3-dimensional 
  array.

      ************************************************************/  

      for (j = 0; j < NX; j++) 
      {
	 for (i = 0; i < NY; i++) 
	 {
	    for (k = 0; k < NZ ; k++)
               data_out[j][i][k] = 0;
	 }
      } 
 
      /*
       * Open the file and the dataset.
       */
      file = H5Fopen_wrap (FILE, HDF5Constants.H5F_ACC_RDONLY, 
			   HDF5Constants.H5P_DEFAULT);
      dataset = H5Dopen_wrap (file, DATASETNAME);

      dataspace = H5Dget_space_wrap (dataset);    /* dataspace handle */
      rank = H5Sget_simple_extent_ndims_wrap (dataspace);
      status_n = H5Sget_simple_extent_dims_wrap (dataspace, dims_out, null);

      System.out.println ("Rank: " + rank);
      System.out.println ("Dimensions: "+ dims_out[0] + " x " + dims_out[1]);
      
      /* 
       * Define hyperslab in the dataset. 
       */
      offset[0] = 1;
      offset[1] = 2;
      count[0]  = NX_SUB;
      count[1]  = NY_SUB;
      status = H5Sselect_hyperslab_wrap (dataspace, 
					 HDF5Constants.H5S_SELECT_SET, 
					 offset, null, count, null);
      
      /*
       * Define the memory dataspace.
       */
      dimsm[0] = NX;
      dimsm[1] = NY;
      dimsm[2] = NZ;
      memspace = H5Screate_simple_wrap (RANK_OUT, dimsm, null);   
      
      /* 
       * Define memory hyperslab. 
       */
      offset_out[0] = 3;
      offset_out[1] = 0;
      offset_out[2] = 0;
      count_out[0]  = NX_SUB;
      count_out[1]  = NY_SUB;
      count_out[2]  = 1;
      status = H5Sselect_hyperslab_wrap (memspace, 
					 HDF5Constants.H5S_SELECT_SET, 
					 offset_out, null, count_out, null);
      
      /*
       * Read data from hyperslab in the file into the hyperslab in 
       * memory and display.
       */
      status = 
	  H5Dread_wrap (dataset, H5.J2C (HDF5CDataTypes.JH5T_NATIVE_INT), 
			memspace, dataspace, HDF5Constants.H5P_DEFAULT, 
			data_out);

      System.out.println ("Data:");
      for (j = 0; j < NX; j++) 
      {
	 for (i = 0; i < NY; i++) 
	    System.out.print (data_out[j][i][0]);
	 System.out.println ();
      }
      System.out.println ();

      /*
       * 0 0 0 0 0 0 0
       * 0 0 0 0 0 0 0
       * 0 0 0 0 0 0 0
       * 3 4 5 6 0 0 0  
       * 4 5 6 7 0 0 0
       * 5 6 7 8 0 0 0
       * 0 0 0 0 0 0 0
       */
      
      /*
       * Close and release resources.
       */
      H5Dclose_wrap (dataset);
      H5Sclose_wrap (dataspace);
      H5Sclose_wrap (memspace);
      H5Fclose_wrap (file);
   }     


   // Help function for creating a new file 
   public static int H5Fcreate_wrap (String name, int flags,
				     int create_id, int access_id)
   {
      int file_id = -1;    // file identifier 
      try 
      {
	 // Create a new file using default file properties.
	 file_id = H5.H5Fcreate (name, flags, create_id, access_id);
      }
      catch (HDF5Exception hdf5e)
      {
	 System.out.println 
	     ("HyperSlab.H5Fcreate_wrap() with HDF5Exception: "
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("HyperSlab.H5Fcreate_wrap() with other Exception: " 
	      + e.getMessage());
      }
      return file_id;
   }  


   // Help function for opening an existing file
   public static int H5Fopen_wrap (String name, int flags, int access_id)
   {
      int file_id = -1;    // file identifier 
      try 
      {
	 // Create a new file using default file properties.
	 file_id = H5.H5Fopen (name, flags, access_id);
      }
      catch (HDF5Exception hdf5e)
      {
	 System.out.println 
	     ("HyperSlab.H5Fopen_wrap() with HDF5Exception: "
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("HyperSlab.H5Fopen_wrap() with other Exception: " 
	      + e.getMessage());
      }
      return file_id;
   }


   // Help function for opening an existing dataset
   public static int H5Dopen_wrap (int loc_id, String name)
   {
      int dataset_id = -1;  // dataset identifier   
   
      try 
      {
	 // Opening an existing dataset
	 dataset_id = H5.H5Dopen (loc_id, name);
      }
      catch (HDF5Exception hdf5e)
      {
	 System.out.println 
	     ("HyperSlab.H5Dopen_wrap() with HDF5Exception: "
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("HyperSlab.H5Dopen_wrap() with other Exception: "
	      + e.getMessage());
      }
      return dataset_id;
   }

    
   // Help function for creating a new simple dataspace and opening it
   // for access
   public static int H5Screate_simple_wrap (int rank, long dims[],
					    long maxdims[])
   {
      int dataspace_id = -1;  // dataspace identifier   
        
      try 
      {
	 // Create the data space for the dataset.
	 dataspace_id = H5.H5Screate_simple (rank, dims, maxdims);
      }
      catch (HDF5Exception hdf5e)
      {
	 System.out.println 
	     ("HyperSlab.H5Screate_simple_wrap() with HDF5Exception: "
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("HyperSlab.H5Screate_simple_wrap() with other Exception: "
	      + e.getMessage());
      }
      return dataspace_id;
   }


   // Help function for getting an identifier for a copy of 
   // the dataspace for a dataset 
   public static int H5Dget_space_wrap (int dataset_id)
   {
      int dataspace_id = -1;

      try 
      {
         // Returning an identifier for a copy of the dataspace for a dataset
         dataspace_id = H5.H5Dget_space (dataset_id);
      }
      catch (HDF5Exception hdf5e)
      {
         System.out.println 
	     ("HyperSlab.H5Dget_space_wrap() with HDF5Exception: "
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("HyperSlab.H5Dget_space_wrap() with other Exception: "
	      + e.getMessage());
      }
      return dataspace_id;
   }


   // Help function for determining the dimensionality (or rank) of 
   // a dataspace
   public static int H5Sget_simple_extent_ndims_wrap (int space_id)
   {
      int rank = -1;  
   
      try 
      {
         // Determine the dimensionality (or rank) of a dataspace.
         rank = H5.H5Sget_simple_extent_ndims (space_id); 
      }
      catch (HDF5Exception hdf5e)
      {
         System.out.println 
	   ("HyperSlab.H5Sget_simple_extent_ndims_wrap() with HDF5Exception: "
	    + hdf5e.getMessage());
      }
      catch (Exception e)
      {
         System.out.println
	 ("HyperSlab.H5Sget_simple_extent_ndims_wrap() with other Exception: "
	  + e.getMessage());
      }
      return rank;
   }


   // Help function for returning the size and maximum sizes of each 
   // dimension of a dataspace through the dims and maxdims parameters.
   public static int H5Sget_simple_extent_dims_wrap (int space_id, 
						     long dims[],
						     long maxdims[]) 
   {
      int dimension_number = -1;  
   
      try 
      {
	 dimension_number = H5.H5Sget_simple_extent_dims (space_id, dims, 
							  maxdims);
      }
      catch (HDF5Exception hdf5e)
      {
         System.out.println 
	   ("HyperSlab.H5Sget_simple_extent_dims_wrap() with HDF5Exception: "
	    + hdf5e.getMessage());
      }
      catch (Exception e)
      {
         System.out.println
	  ("HyperSlab.H5Sget_simple_extent_dims_wrap() with other Exception: "
	   + e.getMessage());
      }
      return dimension_number;
   }


   // Help function for selecting a hyperslab region to add to the 
   // current selected region for the dataspace specified by space_id. 
   public static int H5Sselect_hyperslab_wrap (int space_id, int op,
					       long start[], long stride[],
					       long count[], long block[])
    {
      int status = -1;  
   
      try 
      {
	  status = H5.H5Sselect_hyperslab (space_id, op, start, stride, 
					   count, block);
      }
      catch (HDF5Exception hdf5e)
      {
         System.out.println 
	     ("HyperSlab.H5Sselect_hyperslab_wrap() with HDF5Exception: "
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
         System.out.println
	     ("HyperSlab.H5Sselect_hyperslab_wrap() with other Exception: "  
	      + e.getMessage());
      }
      return status;
   }


   // Help function for creating a dataset
   public static int H5Dcreate_wrap (int loc_id, String name, int type_id, 
				     int space_id, int create_plist_id)
   {
      int dataset_id = -1;  // dataset identifier   
   
      try 
      {
	 // Create the dataset
	  dataset_id = H5.H5Dcreate (loc_id, name, type_id, space_id, 
				     create_plist_id);
      }
      catch (HDF5Exception hdf5e)
      {
	 System.out.println 
	     ("HyperSlab.H5Dcreate_wrap() with HDF5Exception: "
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("HyperSlab.H5Dcreate_wrap() with other Exception: "
	      + e.getMessage());
      }
      return dataset_id;
   }


   // Help function for writing the dataset
   public static int H5Dwrite_wrap (int dataset_id, int mem_type_id,
				    int mem_space_id, int file_space_id,
				    int xfer_plist_id, Object buf)
   {
      int status = -1;
      
      try 
      {
	 // Write the dataset. 
	 status = H5.H5Dwrite (dataset_id, mem_type_id, mem_space_id, 
			       file_space_id, xfer_plist_id, buf);
      }
      catch (HDF5Exception hdf5e)
      {
	 System.out.println 
	     ("HyperSlab.H5Dwrite_wrap() with HDF5Exception: " 
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("HyperSlab.H5Dwrite_wrap() with other exception: " 
	      + e.getMessage());
      }
      return status;
   }


   // Help function for reading the dataset
   public static int H5Dread_wrap (int dataset_id, int mem_type_id,
				   int mem_space_id, int file_space_id,
				   int xfer_plist_id, Object obj)
   {
      int status = -1; 
      
      try 
      {
	 // Read the dataset. 
	 status = H5.H5Dread (dataset_id, mem_type_id, mem_space_id, 
			      file_space_id, xfer_plist_id, obj);
      }
      catch (HDF5Exception hdf5e)
      {
	 System.out.println 
	     ("HyperSlab.H5Dread_wrap() with HDF5Exception: " 
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("HyperSlab.H5Dread_wrap() with other exception: " 
	      + e.getMessage());
      }
      return status;
   }
 

   // Help function for terminating access to the data space. 
   public static int H5Sclose_wrap (int dataspace_id)
   {
      int status = -1;
      
      try 
      {
         // Terminate access to the data space. 
         status = H5.H5Sclose (dataspace_id);
      }
      catch (HDF5Exception hdf5e)
      {
         System.out.println 
	     ("HyperSlab.H5Sclose_wrap() with HDF5Exception: " 
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
         System.out.println 
	     ("HyperSlab.H5Sclose_wrap() with other exception: " 
	      + e.getMessage());
      }
      return status;
   }
 
 
   // Help function for ending access to the dataset and releasing 
   // resources used by it.
   public static int H5Dclose_wrap (int dataset_id)
   {
      int status = -1;
      
      try 
      {
	 // End access to the dataset and release resources used by it. 
	 status = H5.H5Dclose (dataset_id);
      }
      catch (HDF5Exception hdf5e)
      {
	 System.out.println 
	     ("HyperSlab.H5Dclose_wrap() with HDF5Exception: " 
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("HyperSlab.H5Dclose_wrap() with other exception: " 
	      + e.getMessage());
      }
      return status;
   }


   // Help function for terminating access to the file. 
   public static int H5Fclose_wrap (int file_id)
   {
      int status = -1;
      
      try 
      {
	 // Terminate access to the file. 
	 status = H5.H5Fclose (file_id);
      }
      catch (HDF5Exception hdf5e)
      { 
	 System.out.println 
	     ("HyperSlab.H5Fclose_wrap() with HDF5Exception: " 
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("HyperSlab.H5Fclose_wrap() with other exception: " 
	      + e.getMessage());
      }
      return status;
   }
}