summaryrefslogtreecommitdiffstats
path: root/doc/html/Tutor/examples/java/Compound.java
blob: 219e1c1101f587f184ed13a6a62a8a4a4e4e731b (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
/******************************************************************
 * Compound.java (for HDF5 tutorial lesson 11)
 *
 *   -- Creating a compound data type
 *      (a java conversion from compound.c)
 *
 ******************************************************************/

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

public class Compound
{
   public static void main (String []argv) 
   {    
      final String FILE = "SDScompound.h5";
      final String DATASETNAME = "ArrayOfStructures";
      final int LENGTH = 10;
      final int RANK = 1;

      /* First structure and dataset */
      /* an array of LENGTH 'complex' numbers */
      byte[] data1 = new byte[LENGTH * 16]; 

      int[] AR = new int[1];
      float[] BR = new float[1];
      double[] CR = new double[1];

      byte [] ARec = new byte[4];
      byte [] BRec = new byte[4];
      byte [] CRec = new byte[8];
      
      int s1_tid;     /* File datatype identifier */

      /* Second structure (subset of s1_t) and dataset*/
      byte[] data2 = new byte[LENGTH * 12]; 
      int s2_tid;    /* Memory datatype handle */
      
      /* Third "structure" ( will be used to read float field of s1) */
      int s3_tid;   /* Memory datatype handle */
      float[] s3 = new float[LENGTH];
      
      int i;
      int file, dataset, space; /* Handles */
      int status;
      long[] dim = new long[1];   /* Dataspace dimensions */
      dim[0] = LENGTH;
    
      /*
       * Initialize the data
       */
      for (i = 0; i < LENGTH; i++) 
      {
	 AR[0] = (int) i;
	 BR[0] = (float) i * i;
	 CR[0] = (double) 1. / (i + 1);

	 ARec = HDFNativeData.intToByte (0, 1, AR);
	 BRec = HDFNativeData.floatToByte (0, 1, BR);
	 CRec = HDFNativeData.doubleToByte (0, 1, CR);

	 System.arraycopy (ARec, 0, data1, (i * 16), 4);
	 System.arraycopy (BRec, 0, data1, (i * 16) + 4, 4); 
	 System.arraycopy (CRec, 0, data1, (i * 16) + 8, 8);
      }
      
      /*
       * Create the data space.
       */
      space = H5Screate_simple_wrap (RANK, dim, null);
      
      /*
       * Create the file.
       */
      file = H5Fcreate_wrap (FILE, HDF5Constants.H5F_ACC_TRUNC, 
			     HDF5Constants.H5P_DEFAULT, 
			     HDF5Constants.H5P_DEFAULT);
      
      /*
       * Create the memory data type. 
       */
      s1_tid = H5Tcreate_wrap (HDF5Constants.H5T_COMPOUND, 16);
      H5Tinsert_wrap (s1_tid, "a_name", 0,  
		      H5.J2C (HDF5CDataTypes.JH5T_NATIVE_INT));
      H5Tinsert_wrap (s1_tid, "b_name", 4,
		      H5.J2C (HDF5CDataTypes.JH5T_NATIVE_FLOAT));
      H5Tinsert_wrap (s1_tid, "c_name", 8, 
		      H5.J2C (HDF5CDataTypes.JH5T_NATIVE_DOUBLE));

      /* 
       * Create the dataset.
       */
      dataset = H5Dcreate_wrap (file, DATASETNAME, s1_tid, 
				space, HDF5Constants.H5P_DEFAULT);

      /*
       * Wtite data to the dataset; 
       */
      status = H5Dwrite_wrap (dataset, s1_tid, 
			      HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, 
			      HDF5Constants.H5P_DEFAULT, data1);

      /*
       * Release resources
       */
      H5Tclose_wrap (s1_tid);
      H5Sclose_wrap (space);
      H5Dclose_wrap (dataset);
      H5Fclose_wrap (file);
 
      /*
       * Open the file and the dataset.
       */
      file = H5Fopen_wrap (FILE, HDF5Constants.H5F_ACC_RDONLY, 
			   HDF5Constants.H5P_DEFAULT);

      dataset = H5Dopen_wrap (file, DATASETNAME);
      
      /* 
       * Create a data type for s2
       */
      s2_tid = H5Tcreate_wrap (HDF5Constants.H5T_COMPOUND, 12);
      H5Tinsert_wrap (s2_tid, "c_name", 0,  
		      H5.J2C (HDF5CDataTypes.JH5T_NATIVE_DOUBLE));
      H5Tinsert_wrap (s2_tid, "a_name", 8,  
		      H5.J2C (HDF5CDataTypes.JH5T_NATIVE_INT));

      /*
       * Read two fields c and a from s1 dataset. Fields in the file
       * are found by their names "c_name" and "a_name".
       */
      status = H5Dread_wrap (dataset, s2_tid, HDF5Constants.H5S_ALL, 
			     HDF5Constants.H5S_ALL, 
			     HDF5Constants.H5P_DEFAULT, data2);
      
      /*
       * Display the fields.  Convert from bytes into numbers.
       */
      System.out.println ("\nField c : ");     
      for( i = 0; i < LENGTH; i++)  {
	 System.arraycopy (data2, (i*12), CRec, 0, 8);
	 CR = HDFNativeData.byteToDouble(0, 1, CRec);
	 System.out.print (CR[0]+" ");
      }
      System.out.println ();

      System.out.println("\nField a :");
      for( i = 0; i < LENGTH; i++) {
	 System.arraycopy (data2, (i*12)+8, ARec, 0, 4);
	 AR = HDFNativeData.byteToInt(0, 1, ARec);
	 System.out.print (AR[0]+" ");
	}
      System.out.println ();

      /* 
       * Create a data type for s3.
       */
      s3_tid = H5Tcreate_wrap (HDF5Constants.H5T_COMPOUND, 4);
      
      status = 
	  H5Tinsert_wrap (s3_tid, "b_name", 0,  
			  H5.J2C (HDF5CDataTypes.JH5T_NATIVE_FLOAT));
      
      /*
       * Read field b from s1 dataset. Field in the file is found by its name.
       */
      status = H5Dread_wrap (dataset, s3_tid, HDF5Constants.H5S_ALL, 
			     HDF5Constants.H5S_ALL, 
			     HDF5Constants.H5P_DEFAULT, s3);
      
      /*
       * Display the field.  Data is read directly into array of 'float'.
       */
      System.out.println ();
      System.out.println ("Field b :");
      for( i = 0; i < LENGTH; i++) {
	 System.out.print (s3[i]+" ");
	}
      System.out.println ();
      
      /*
       * Release resources
       */
      H5Tclose_wrap (s2_tid);
      H5Tclose_wrap (s3_tid);
      H5Dclose_wrap (dataset);
      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 
	     ("Compound.H5Fcreate_wrap() with HDF5Exception: "
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("Compound.H5Fcreate_wrap() with other Exception: " 
	      + e.getMessage());
      }
      return file_id;
   }  


   // Help function for adding another member to the compound 
   // datatype datatype_id.
   public static int H5Tinsert_wrap (int type_id, String name,
				     long offset, int field_id)
   {
      int status = -1;  
      try 
      {
         // Adding another member to the compound datatype datatype_id.
	  status = H5.H5Tinsert (type_id, name, offset, field_id);
	  
      }
      catch (HDF5Exception hdf5e)
      {
         System.out.println 
	     ("Compound.H5Tinsert_wrap() with HDF5Exception: "
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
         System.out.println 
	     ("Compound.H5Tinsert_wrap() with HDF5Exception: "
	      + e.getMessage());
      }
      return status;
   }


   // Help function for creating the memory data type.
   public static int H5Tcreate_wrap (int dclass, int size)
   {
      int datatype_id = -1;    // memory data type identifier 
      try 
      {
         // Create the memory data type. 
         datatype_id = H5.H5Tcreate (dclass, size);

      }
      catch (HDF5Exception hdf5e)
      {
         System.out.println 
	     ("Compound.H5Tcreate_wrap() with HDF5Exception: "
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
         System.out.println 
	     ("Compound.H5Tcreate_wrap() with other Exception: " 
	      + e.getMessage());
      }
      return datatype_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 
	     ("Compound.H5Fopen_wrap() with HDF5Exception: "
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("Compound.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 
	     ("Compound.H5Dopen_wrap() with HDF5Exception: "
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("Compound.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 
	     ("Compound.H5Screate_simple_wrap() with HDF5Exception: "
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("Compound.H5Screate_simple_wrap() with other Exception: "
	      + e.getMessage());
      }
      return dataspace_id;
   }


   // 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 
	     ("Compound.H5Dcreate_wrap() with HDF5Exception: "
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("Compound.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 
	     ("Compound.H5Dwrite_wrap() with HDF5Exception: " 
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("Compound.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 
	     ("Compound.H5Dread_wrap() with HDF5Exception: " 
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("Compound.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 
	      ("Compound.H5Sclose_wrap() with HDF5Exception: " 
	       + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("Compound.H5Sclose_wrap() with other exception: " 
	      + e.getMessage());
      }
      return status;
   }
 

   // Help function for releasing a datatype.
   public static int H5Tclose_wrap (int type_id)
   {
      int status = -1;
      
      try 
      {
         // Releasing a datatype.
         status = H5.H5Tclose (type_id);
      }
      catch (HDF5Exception hdf5e)
      {
         System.out.println 
	     ("Compound.H5Tclose_wrap() with HDF5Exception: " 
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
         System.out.println 
	     ("Compound.H5Tclose_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 
	     ("Compound.H5Dclose_wrap() with HDF5Exception: " 
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("Compound.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 
	     ("Compound.H5Fclose_wrap() with HDF5Exception: " 
	      + hdf5e.getMessage());
      }
      catch (Exception e)
      {
	 System.out.println 
	     ("Compound.H5Fclose_wrap() with other exception: " 
	      + e.getMessage());
      }
      return status;
   }  
}