summaryrefslogtreecommitdiffstats
path: root/c++/src/H5DataSpace.cpp
blob: 46ee18c892f7628b1039d2f598e7da4cacef6856 (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  * Copyright by the Board of Trustees of the University of Illinois.         *
  * All rights reserved.                                                      *
  *                                                                           *
  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
  * terms governing use, modification, and redistribution, is contained in    *
  * the files COPYING and Copyright.html.  COPYING can be found at the root   *
  * of the source code distribution tree; Copyright.html can be found at the  *
  * root level of an installed copy of the electronic HDF5 document set and   *
  * is linked from the top-level documents page.  It can also be found at     *
  * http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html.  If you do not have     *
  * access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. *
  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <string>
#ifdef OLD_HEADER_FILENAME
#include <iostream.h>
#else
#include <iostream>
#endif

#include "H5Include.h"
#include "H5Exception.h"
#include "H5RefCounter.h"
#include "H5IdComponent.h"
#include "H5Idtemplates.h"
#include "H5DataSpace.h"

#ifndef H5_NO_NAMESPACE
namespace H5 {
#endif

const DataSpace DataSpace::ALL( H5S_ALL );

//--------------------------------------------------------------------------
// Function:	DataSpace default constructor
///\brief	Default constructor: Creates a stub datatype
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
DataSpace::DataSpace() : IdComponent() {}

//--------------------------------------------------------------------------
// Function:	DataSpace overloaded constructor
///\brief	Creates a dataspace given a dataspace type.
///\param	type - IN: Type of the dataspace to be created, which 
///		currently can be either \c H5S_SCALAR or \c H5S_SIMPLE
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
DataSpace::DataSpace( H5S_class_t type ) : IdComponent()
{
   id = H5Screate( type );
   if( id <= 0 )
   {
      throw DataSpaceIException("DataSpace constructor", "H5Screate failed");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace overloaded constructor
///\brief	Creates a new simple dataspace and opens it for access.
///\param	rank - IN: Number of dimensions of dataspace. 
///\param	dims - IN: An array of the size of each dimension. 
///\param	maxdims - IN: An array of the maximum size of each dimension. 
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
DataSpace::DataSpace( int rank, const hsize_t * dims, const hsize_t * maxdims) : IdComponent()
{
   id = H5Screate_simple( rank, dims, maxdims );
   if( id <= 0 )
   {
      throw DataSpaceIException("DataSpace constructor", "H5Screate_simple failed");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace overloaded constructor
///\brief	Creates an DataSpace object using the id of an existing 
///		dataspace.
///\param	existing_id - IN: Id of an existing dataspace
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
DataSpace::DataSpace(const hid_t existing_id) : IdComponent(existing_id) {}

//--------------------------------------------------------------------------
// Function:	DataSpace copy constructor
///\brief	Copy constructor: makes a copy of the original DataSpace object.
///\param	original - IN: DataSpace instance to copy
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
DataSpace::DataSpace( const DataSpace& original ) : IdComponent( original ) {}

//--------------------------------------------------------------------------
// Function:	DataSpace::copy
///\brief	Makes a copy of an existing dataspace.
///\param	like_space  - IN: Dataspace to be copied
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
void DataSpace::copy( const DataSpace& like_space )
{
   // reset the identifier of this instance - send 'this' in so that
   // H5Sclose can be called appropriately
    try {
        resetIdComponent( this ); }
    catch (Exception close_error) { // thrown by p_close
        throw DataSpaceIException("DataSpace::copy", close_error.getDetailMsg());
    }

   // call C routine to copy the dataspace 
   id = H5Scopy( like_space.getId() );

   // new ref counter for this id 
   ref_count = new RefCounter;

   if( id <= 0 )
   {
      throw DataSpaceIException("DataSpace::copy", "H5Scopy failed");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace::operator=
///\brief	Assignment operator
///\param	rhs - IN: Reference to the existing dataspace
///\return	Reference to DataSpace instance
///\exception	H5::DataSpaceIException
// Description
//		Makes a copy of the type on the right hand side and stores
//		the new id in the left hand side object.
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
DataSpace& DataSpace::operator=( const DataSpace& rhs )
{
   copy(rhs);
   return(*this);
}

//--------------------------------------------------------------------------
// Function:	DataSpace::isSimple
///\brief	Determines whether this dataspace is a simple dataspace.
///\return	true if the dataspace is a simple dataspace, and false, 
///		otherwise
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
bool DataSpace::isSimple () const
{
   htri_t simple = H5Sis_simple( id );
   if( simple > 0 )
      return true;
   else if( simple == 0 )
      return false;
   else
   {
      throw DataSpaceIException("DataSpace::isSimple", 
		"H5Sis_simple returns negative value");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace::offsetSimple
///\brief	Sets the offset of this simple dataspace.
///\param	offset  - IN: Offset to position the selection at
///\exception	H5::DataSpaceIException
///\par Description
///		This function creates an offset for the selection within 
///		an extent, allowing the same shaped selection to be moved 
///		to different locations within a dataspace without requiring 
///		it to be re-defined.
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
void DataSpace::offsetSimple ( const hssize_t* offset ) const
{
   herr_t ret_value = H5Soffset_simple( id, offset );
   if( ret_value < 0 )
   {
      throw DataSpaceIException("DataSpace::offsetSimple", "H5Soffset_simple failed");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace::getSimpleExtentDims
///\brief	Retrieves dataspace dimension size and maximum size.
///\param	dims  - IN: Name of the new member
///\param	maxdims - IN: Pointer to the value of the new member
///\return	Number of dimensions, the same value as returned by 
///		\c DataSpace::getSimpleExtentNdims()
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
int DataSpace::getSimpleExtentDims ( hsize_t *dims, hsize_t *maxdims ) const
{
   int ndims = H5Sget_simple_extent_dims( id, dims, maxdims );
   if( ndims < 0 )
   {
      throw DataSpaceIException("DataSpace::getSimpleExtentDims", 
		"H5Sget_simple_extent_dims returns negative number of dimensions");
   }
   return( ndims );
}

//--------------------------------------------------------------------------
// Function:	DataSpace::getSimpleExtentNdims
///\brief	Determines the dimensionality of a dataspace.
///\return	Number of dimensions
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
int DataSpace::getSimpleExtentNdims () const
{
   int ndims = H5Sget_simple_extent_ndims( id );
   if( ndims < 0 )
   {
      throw DataSpaceIException("DataSpace::getSimpleExtentNdims", 
		"H5Sget_simple_extent_ndims returns negative value for dimensionality of the dataspace");
   }
   return( ndims );
}

//--------------------------------------------------------------------------
// Function:	DataSpace::getSimpleExtentNpoints
///\brief	Determines the number of elements in a dataspace.
///\return	Number of elements
///\exception	H5::DataSpaceIException
// Modification
//		12/05/00: due to C API change
//			return type hssize_t vs. hsize_t
//			num_elements = -1 when failure occurs vs. 0
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
hssize_t DataSpace::getSimpleExtentNpoints () const
{
   hssize_t num_elements = H5Sget_simple_extent_npoints( id );

   if( num_elements > -1 )
      return( num_elements );
   else
   {
      throw DataSpaceIException("DataSpace::getSimpleExtentNpoints", 
	"H5Sget_simple_extent_npoints returns negative value for the number of elements in the dataspace");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace::getSimpleExtentType
///\brief	Determine the current class of a dataspace.
///\return	Class of the dataspace
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
H5S_class_t DataSpace::getSimpleExtentType () const
{
   H5S_class_t class_name = H5Sget_simple_extent_type( id );
   if( class_name == H5S_NO_CLASS )
   {
      throw DataSpaceIException("DataSpace::getSimpleExtentType", 
		"H5Sget_simple_extent_type returns H5S_NO_CLASS");
   }
   return( class_name );
}

//--------------------------------------------------------------------------
// Function:	DataSpace::extentCopy
///\brief	Copies the extent of a dataspace.
///\param	dest_space  - IN: DataSpace to copy from
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
void DataSpace::extentCopy ( DataSpace& dest_space ) const
{
   hid_t dest_space_id = dest_space.getId();
   herr_t ret_value = H5Sextent_copy( dest_space_id, id );
   if( ret_value < 0 )
   {
      throw DataSpaceIException("DataSpace::extentCopy", "H5Sextent_copy failed");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace::setExtentSimple
///\brief	Sets or resets the size of an existing dataspace.
///\param	rank  - IN: Rank of the dataspace
///\param	current_size - IN: Array containing current size of dataspace
///\param	maximum_size - IN: Array containing maximum size of dataspace
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
void DataSpace::setExtentSimple( int rank, const hsize_t *current_size, const hsize_t *maximum_size ) const
{
   herr_t ret_value;
   ret_value = H5Sset_extent_simple( id, rank, current_size, maximum_size );
   if( ret_value < 0 )
   {
      throw DataSpaceIException("DataSpace::setExtentSimple", "H5Sset_extent_simple failed");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace::setExtentNone
///\brief	Removes the extent from a dataspace.
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
void DataSpace::setExtentNone () const
{
   herr_t ret_value = H5Sset_extent_none( id );
   if( ret_value < 0 )
   {
      throw DataSpaceIException("DataSpace::setExtentNone", "H5Sset_extent_none failed");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace::getSelectNpoints
///\brief	Returns the number of elements in a dataspace selection.
///\return	Number of elements
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
hssize_t DataSpace::getSelectNpoints () const
{
   hssize_t num_elements = H5Sget_select_npoints( id );
   if( num_elements < 0 )
   {
      throw DataSpaceIException("DataSpace::getSelectNpoints", 
		"H5Sget_select_npoints returns negative value for number of elements in the dataspace selection");
   }
   return( num_elements );
}

//--------------------------------------------------------------------------
// Function:	DataSpace::getSelectHyperNblocks
///\brief	Returns number of hyperslab blocks.
///\return	Number of hyperslab blocks
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
hssize_t DataSpace::getSelectHyperNblocks () const
{
   hssize_t num_blocks = H5Sget_select_hyper_nblocks( id );
   if( num_blocks < 0 )
   {
      throw DataSpaceIException("DataSpace::getSelectHyperNblocks", 
		"H5Sget_select_hyper_nblocks returns negative value for the number of hyperslab blocks");
   }
   return( num_blocks );
}

//--------------------------------------------------------------------------
// Function:	DataSpace::getSelectHyperBlocklist
///\brief	Gets the list of hyperslab blocks currently selected
///\param	startblock  - IN: Hyperslab block to start with
///\param	numblocks - IN: Number of hyperslab blocks to get
///\param	buf - IN: List of hyperslab blocks selected
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
void DataSpace::getSelectHyperBlocklist( hsize_t startblock, hsize_t numblocks, hsize_t *buf ) const
{
   herr_t ret_value;
   ret_value = H5Sget_select_hyper_blocklist( id, startblock, numblocks, buf );
   if( ret_value < 0 )
   {
      throw DataSpaceIException("DataSpace::getSelectHyperBlocklist", 
		"H5Sget_select_hyper_blocklist failed");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace::getSelectElemNpoints
///\brief	Returns the number of element points in the current selection.
///\return	Number of element points
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
hssize_t DataSpace::getSelectElemNpoints () const
{
   hssize_t num_points = H5Sget_select_elem_npoints( id );
   if( num_points < 0 )
   {
      throw DataSpaceIException("DataSpace::getSelectElemNpoints", 
		"H5Sget_select_elem_npoints failed");
   }
   return( num_points );
}

//--------------------------------------------------------------------------
// Function:	DataSpace::getSelectElemPointlist
///\brief	Gets the list of element points currently selected
///\param	startpoint  - IN: Element point to start with
///\param	numpoints - IN: Number of element points to get
///\param	buf - IN: List of element points selected
///\exception	H5::DataSpaceIException
///\par Description
///		For more information, please refer to the C layer Reference
///		Manual at:
/// http:
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
void DataSpace::getSelectElemPointlist ( hsize_t startpoint, hsize_t numpoints, hsize_t *buf ) const
{
   herr_t ret_value;
   ret_value = H5Sget_select_elem_pointlist( id, startpoint, numpoints, buf );
   if( ret_value < 0 )
   {
      throw DataSpaceIException("DataSpace::getSelectElemPointlist",
                "H5Sget_select_elem_pointlist failed");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace::getSelectBounds
///\brief	Gets the bounding box containing the current selection.
///\param	start  - IN: Starting coordinates of the bounding box
///\param	end - IN: Ending coordinates of the bounding box, i.e., 
///		the coordinates of the diagonally opposite corner
///\exception	H5::DataSpaceIException
///\par Description
///		For more information, please refer to the C layer Reference
///		Manual at:
/// http://hdf.ncsa.uiuc.edu/HDF5/doc/RM_H5S.html#Dataspace-SelectBounds
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
void DataSpace::getSelectBounds ( hssize_t* start, hssize_t* end ) const
{
   herr_t ret_value = H5Sget_select_bounds( id, start, end );
   if( ret_value < 0 )
   {
      throw DataSpaceIException("DataSpace::getSelectBounds",
                "H5Sget_select_bounds failed");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace::H5Sselect_elements
///\brief	Selects array elements to be included in the selection for 
///		this dataspace.
///\param	op  - IN: Operator specifying how the new selection is to be 
///		combined with the existing selection for the dataspace
///\param	num_elements  - IN: Number of elements to be selected
///\param	coord  - IN: A 2-dimensional array of 0-based values 
///		specifying the coordinates of the elements being selected
///\exception	H5::DataSpaceIException
///\par Description
///		For more information, please refer to the C layer Reference
///		Manual at:
/// http://hdf.ncsa.uiuc.edu/HDF5/doc/RM_H5S.html#Dataspace-SelectElements
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
void DataSpace::selectElements ( H5S_seloper_t op, const size_t num_elements, const hssize_t *coord[ ] ) const
{
   herr_t ret_value;
   ret_value = H5Sselect_elements( id, op, num_elements, coord );
   if( ret_value < 0 )
   {
      throw DataSpaceIException("DataSpace::selectElements",
                "H5Sselect_elements failed");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace::selectAll
///\brief	Selects the entire dataspace.
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
void DataSpace::selectAll () const
{
   herr_t ret_value = H5Sselect_all( id );
   if( ret_value < 0 )
   {
      throw DataSpaceIException("DataSpace::selectAll", "H5Sselect_all failed");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace::selectNone
///\brief	Resets the selection region to include no elements.
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
void DataSpace::selectNone () const
{
  herr_t ret_value = H5Sselect_none( id );
   if( ret_value < 0 )
   {
      throw DataSpaceIException("DataSpace::selectNone",
                "H5Sselect_none failed");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace::selectValid
///\brief	Verifies that the selection is within the extent of the 
///		dataspace.
///\return	true if the selection is within the extent of the
///		dataspace, and false, otherwise
///\exception	H5::DataSpaceIException
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
bool DataSpace::selectValid () const
{
  htri_t ret_value = H5Sselect_valid( id );
   if( ret_value > 0 )
      return true;
   else if( ret_value == 0 )
      return false;
   else
   {
      throw DataSpaceIException("DataSpace::selectValid",
                "H5Sselect_valid returns negative value");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace::selectHyperslab
///\brief	Selects a hyperslab region to add to the current selected region.
///\param	op - IN: Operation to perform on current selection
///\param	count - IN: Offset of the start of hyperslab
///\param	start - IN: Number of blocks included in the hyperslab
///\param	stride - IN: Hyperslab stride
///\param	block - IN: Size of block in the hyperslab
///\exception	H5::DataSpaceIException
///\par Description
///		For more information, please refer to the C layer Reference
///		Manual at:
/// http:
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
void DataSpace::selectHyperslab( H5S_seloper_t op, const hsize_t *count, const hssize_t *start, const hsize_t *stride, const hsize_t *block ) const
{
   herr_t ret_value;
   ret_value = H5Sselect_hyperslab( id, op, start, stride, count, block );
   if( ret_value < 0 )
   {
      throw DataSpaceIException("DataSpace::selectHyperslab",
		"H5Sselect_hyperslab failed");
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace::p_close (private)
///\brief	Closes the dataspace if it is not a constant
///\exception	H5::FileIException
///\note
///	This function will be obsolete because its functionality
///	is recently handled by the C library layer.
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
void DataSpace::p_close() const
{
   hid_t space_id = id;
   if( space_id != H5S_ALL ) // not a constant, should call H5Sclose
   {
      herr_t ret_value = H5Sclose( space_id );
      if( ret_value < 0 )
      {
         throw DataSpaceIException(0, "H5Sclose failed");
      }
   }
}

//--------------------------------------------------------------------------
// Function:	DataSpace destructor
///\brief	Properly terminates access to this dataspace.
// Programmer	Binh-Minh Ribler - 2000
//--------------------------------------------------------------------------
DataSpace::~DataSpace()
{  
   // The dataspace id will be closed properly
    try {
        resetIdComponent( this ); }
    catch (Exception close_error) { // thrown by p_close
        cerr << "DataSpace::~DataSpace - " << close_error.getDetailMsg() << endl;
    }
}  

#ifndef H5_NO_NAMESPACE
} // end namespace
#endif