comparison src/ls-hdf5.cc @ 11176:2271261f088a

Address precision issue in ranges saved to HDF5 files
author David Bateman <dbateman@free.fr>
date Tue, 02 Nov 2010 16:27:30 +0100
parents 8b3cfc1288e2
children 30bcd1aa9f09
comparison
equal deleted inserted replaced
11175:c0a95a5c6d25 11176:2271261f088a
154 hid_t attr_id = H5Aopen_name (loc_id, attr_name); 154 hid_t attr_id = H5Aopen_name (loc_id, attr_name);
155 155
156 if (attr_id >= 0) 156 if (attr_id >= 0)
157 { 157 {
158 // successful 158 // successful
159 retval = 1; 159 retval = true;
160 H5Aclose (attr_id); 160 H5Aclose (attr_id);
161 } 161 }
162 162
163 // restore error reporting: 163 // restore error reporting:
164 #if HAVE_HDF5_18 164 #if HAVE_HDF5_18
166 #else 166 #else
167 H5Eset_auto (err_func, err_func_data); 167 H5Eset_auto (err_func, err_func_data);
168 #endif 168 #endif
169 return retval; 169 return retval;
170 } 170 }
171
172 bool
173 hdf5_get_scalar_attr (hid_t loc_id, hid_t type_id,
174 const char *attr_name, void *buf)
175 {
176 bool retval = false;
177
178 // we have to pull some shenanigans here to make sure
179 // HDF5 doesn't print out all sorts of error messages if we
180 // call H5Aopen for a non-existing attribute
181
182 H5E_auto_t err_func;
183 void *err_func_data;
184
185 // turn off error reporting temporarily, but save the error
186 // reporting function:
187
188 #if HAVE_HDF5_18
189 H5Eget_auto (H5E_DEFAULT, &err_func, &err_func_data);
190 H5Eset_auto (H5E_DEFAULT, 0, 0);
191 #else
192 H5Eget_auto (&err_func, &err_func_data);
193 H5Eset_auto (0, 0);
194 #endif
195
196 hid_t attr_id = H5Aopen_name (loc_id, attr_name);
197
198 if (attr_id >= 0)
199 {
200 hid_t space_id = H5Aget_space (attr_id);
201
202 hsize_t rank = H5Sget_simple_extent_ndims (space_id);
203
204 if (rank == 0)
205 retval = H5Aread (attr_id, type_id, buf) >= 0;
206 H5Aclose (attr_id);
207 }
208
209 // restore error reporting:
210 #if HAVE_HDF5_18
211 H5Eset_auto (H5E_DEFAULT, err_func, err_func_data);
212 #else
213 H5Eset_auto (err_func, err_func_data);
214 #endif
215 return retval;
216 }
217
218
219
171 220
172 // The following subroutines creates an HDF5 representations of the way 221 // The following subroutines creates an HDF5 representations of the way
173 // we will store Octave complex types (pairs of floating-point numbers). 222 // we will store Octave complex types (pairs of floating-point numbers).
174 // NUM_TYPE is the HDF5 numeric type to use for storage (e.g. 223 // NUM_TYPE is the HDF5 numeric type to use for storage (e.g.
175 // H5T_NATIVE_DOUBLE to save as 'double'). Note that any necessary 224 // H5T_NATIVE_DOUBLE to save as 'double'). Note that any necessary
574 return retval; 623 return retval;
575 } 624 }
576 625
577 // Add an attribute named attr_name to loc_id (a simple scalar 626 // Add an attribute named attr_name to loc_id (a simple scalar
578 // attribute with value 1). Return value is >= 0 on success. 627 // attribute with value 1). Return value is >= 0 on success.
579 static herr_t 628 herr_t
580 hdf5_add_attr (hid_t loc_id, const char *attr_name) 629 hdf5_add_attr (hid_t loc_id, const char *attr_name)
581 { 630 {
582 herr_t retval = 0; 631 herr_t retval = 0;
583 632
584 hid_t as_id = H5Screate (H5S_SCALAR); 633 hid_t as_id = H5Screate (H5S_SCALAR);
608 else 657 else
609 retval = as_id; 658 retval = as_id;
610 659
611 return retval; 660 return retval;
612 } 661 }
662
663 herr_t
664 hdf5_add_scalar_attr (hid_t loc_id, hid_t type_id,
665 const char *attr_name, void *buf)
666 {
667 herr_t retval = 0;
668
669 hid_t as_id = H5Screate (H5S_SCALAR);
670
671 if (as_id >= 0)
672 {
673 #if HAVE_HDF5_18
674 hid_t a_id = H5Acreate (loc_id, attr_name, H5T_NATIVE_UCHAR,
675 as_id, H5P_DEFAULT, H5P_DEFAULT);
676 #else
677 hid_t a_id = H5Acreate (loc_id, attr_name,
678 H5T_NATIVE_UCHAR, as_id, H5P_DEFAULT);
679 #endif
680 if (a_id >= 0)
681 {
682 retval = H5Awrite (a_id, type_id, buf);
683
684 H5Aclose (a_id);
685 }
686 else
687 retval = a_id;
688
689 H5Sclose (as_id);
690 }
691 else
692 retval = as_id;
693
694 return retval;
695 }
696
697
698
699
700
701
613 702
614 // Save an empty matrix, if needed. Returns 703 // Save an empty matrix, if needed. Returns
615 // > 0 Saved empty matrix 704 // > 0 Saved empty matrix
616 // = 0 Not an empty matrix; did nothing 705 // = 0 Not an empty matrix; did nothing
617 // < 0 Error condition 706 // < 0 Error condition