Mercurial > hg > octave-lyh
annotate src/ls-hdf5.cc @ 9881:b3089dba88bf
Remove HDF5 cruft for older versions of HDF5
author | Kacper Kowalik |
---|---|
date | Sat, 28 Nov 2009 14:00:56 +0100 |
parents | eb63fbe60fab |
children | ac69e6f4b33d |
rev | line source |
---|---|
4634 | 1 /* |
2 | |
8920 | 3 Copyright (C) 1996, 1997, 2003, 2004, 2005, 2006, 2007, 2008, |
4 2009 John W. Eaton | |
4634 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
4634 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
4634 | 21 |
22 */ | |
23 | |
24 // Author: Steven G. Johnson <stevenj@alum.mit.edu> | |
25 | |
26 #ifdef HAVE_CONFIG_H | |
27 #include <config.h> | |
28 #endif | |
29 | |
30 #if defined (HAVE_HDF5) | |
31 | |
32 #include <cfloat> | |
33 #include <cstring> | |
34 #include <cctype> | |
35 | |
36 #include <fstream> | |
37 #include <iomanip> | |
38 #include <iostream> | |
39 #include <string> | |
4726 | 40 #include <vector> |
4634 | 41 |
42 #include "byte-swap.h" | |
43 #include "data-conv.h" | |
44 #include "file-ops.h" | |
45 #include "glob-match.h" | |
46 #include "lo-mappers.h" | |
47 #include "mach-info.h" | |
48 #include "oct-env.h" | |
49 #include "oct-time.h" | |
50 #include "quit.h" | |
51 #include "str-vec.h" | |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
7336
diff
changeset
|
52 #include "oct-locbuf.h" |
4634 | 53 |
54 #include "Cell.h" | |
55 #include "defun.h" | |
56 #include "error.h" | |
57 #include "gripes.h" | |
58 #include "load-save.h" | |
59 #include "oct-obj.h" | |
60 #include "oct-map.h" | |
61 #include "ov-cell.h" | |
62 #include "pager.h" | |
63 #include "pt-exp.h" | |
64 #include "sysdep.h" | |
65 #include "unwind-prot.h" | |
66 #include "utils.h" | |
67 #include "variables.h" | |
68 #include "version.h" | |
69 #include "dMatrix.h" | |
70 | |
71 #include "ls-utils.h" | |
72 #include "ls-hdf5.h" | |
73 | |
74 static std::string | |
75 make_valid_identifier (const std::string& nm) | |
76 { | |
77 std::string retval; | |
78 | |
79 size_t nm_len = nm.length (); | |
80 | |
81 if (nm_len > 0) | |
82 { | |
83 if (! isalpha (nm[0])) | |
84 retval += '_'; | |
85 | |
86 for (size_t i = 0; i < nm_len; i++) | |
87 { | |
88 char c = nm[i]; | |
89 retval += (isalnum (c) || c == '_') ? c : '_'; | |
90 } | |
91 } | |
92 | |
93 return retval; | |
94 } | |
95 | |
96 // Define this to 1 if/when HDF5 supports automatic conversion between | |
97 // integer and floating-point binary data: | |
98 #define HAVE_HDF5_INT2FLOAT_CONVERSIONS 0 | |
99 | |
100 // Given two compound types t1 and t2, determine whether they | |
101 // are compatible for reading/writing. This function only | |
102 // works for non-nested types composed of simple elements (ints, floats...), | |
103 // which is all we need it for | |
104 | |
105 bool | |
106 hdf5_types_compatible (hid_t t1, hid_t t2) | |
107 { | |
108 int n; | |
109 if ((n = H5Tget_nmembers (t1)) != H5Tget_nmembers (t2)) | |
110 return false; | |
111 | |
112 for (int i = 0; i < n; ++i) | |
113 { | |
114 hid_t mt1 = H5Tget_member_type (t1, i); | |
115 hid_t mt2 = H5Tget_member_type (t2, i); | |
116 | |
117 if (H5Tget_class (mt1) != H5Tget_class (mt2)) | |
118 return false; | |
119 | |
120 H5Tclose (mt2); | |
121 H5Tclose (mt1); | |
122 } | |
123 | |
124 return true; | |
125 } | |
126 | |
127 // Return true if loc_id has the attribute named attr_name, and false | |
128 // otherwise. | |
129 | |
130 bool | |
131 hdf5_check_attr (hid_t loc_id, const char *attr_name) | |
132 { | |
133 bool retval = false; | |
134 | |
135 // we have to pull some shenanigans here to make sure | |
136 // HDF5 doesn't print out all sorts of error messages if we | |
137 // call H5Aopen for a non-existing attribute | |
138 | |
139 H5E_auto_t err_func; | |
140 void *err_func_data; | |
141 | |
142 // turn off error reporting temporarily, but save the error | |
143 // reporting function: | |
144 | |
145 H5Eget_auto (&err_func, &err_func_data); | |
146 H5Eset_auto (0, 0); | |
147 | |
148 hid_t attr_id = H5Aopen_name (loc_id, attr_name); | |
149 | |
150 if (attr_id >= 0) | |
151 { | |
152 // successful | |
153 retval = 1; | |
154 H5Aclose (attr_id); | |
155 } | |
156 | |
157 // restore error reporting: | |
158 H5Eset_auto (err_func, err_func_data); | |
159 | |
160 return retval; | |
161 } | |
162 | |
4687 | 163 // The following subroutines creates an HDF5 representations of the way |
164 // we will store Octave complex types (pairs of floating-point numbers). | |
165 // NUM_TYPE is the HDF5 numeric type to use for storage (e.g. | |
166 // H5T_NATIVE_DOUBLE to save as 'double'). Note that any necessary | |
167 // conversions are handled automatically by HDF5. | |
4634 | 168 |
4687 | 169 hid_t |
4634 | 170 hdf5_make_complex_type (hid_t num_type) |
171 { | |
172 hid_t type_id = H5Tcreate (H5T_COMPOUND, sizeof (double) * 2); | |
173 | |
174 H5Tinsert (type_id, "real", 0 * sizeof (double), num_type); | |
175 H5Tinsert (type_id, "imag", 1 * sizeof (double), num_type); | |
176 | |
177 return type_id; | |
178 } | |
179 | |
180 // This function is designed to be passed to H5Giterate, which calls it | |
181 // on each data item in an HDF5 file. For the item whose name is NAME in | |
182 // the group GROUP_ID, this function sets dv->tc to an Octave representation | |
183 // of that item. (dv must be a pointer to hdf5_callback_data.) (It also | |
184 // sets the other fields of dv). | |
185 // | |
186 // It returns 1 on success (in which case H5Giterate stops and returns), | |
187 // -1 on error, and 0 to tell H5Giterate to continue on to the next item | |
188 // (e.g. if NAME was a data type we don't recognize). | |
189 | |
4687 | 190 herr_t |
4634 | 191 hdf5_read_next_data (hid_t group_id, const char *name, void *dv) |
192 { | |
193 hdf5_callback_data *d = static_cast <hdf5_callback_data *> (dv); | |
4687 | 194 hid_t type_id = -1, type_class_id = -1, data_id = -1, subgroup_id = -1, |
195 space_id = -1; | |
4634 | 196 |
197 H5G_stat_t info; | |
198 herr_t retval = 0; | |
199 bool ident_valid = valid_identifier (name); | |
200 | |
201 std::string vname = name; | |
202 | |
203 // Allow identifiers as all digits so we can load lists saved by | |
204 // earlier versions of Octave. | |
205 | |
4687 | 206 if (! ident_valid ) |
4634 | 207 { |
208 // fix the identifier, replacing invalid chars with underscores | |
209 vname = make_valid_identifier (vname); | |
210 | |
211 // check again (in case vname was null, empty, or some such thing): | |
212 ident_valid = valid_identifier (vname); | |
213 } | |
214 | |
215 H5Gget_objinfo (group_id, name, 1, &info); | |
216 | |
4687 | 217 if (info.type == H5G_GROUP && ident_valid) |
4634 | 218 { |
4687 | 219 subgroup_id = H5Gopen (group_id, name); |
4634 | 220 |
221 if (subgroup_id < 0) | |
222 { | |
223 retval = subgroup_id; | |
224 goto done; | |
225 } | |
226 | |
4687 | 227 if (hdf5_check_attr (subgroup_id, "OCTAVE_NEW_FORMAT")) |
228 { | |
229 data_id = H5Dopen (subgroup_id, "type"); | |
4634 | 230 |
4687 | 231 if (data_id < 0) |
4634 | 232 { |
4687 | 233 retval = data_id; |
234 goto done; | |
4634 | 235 } |
236 | |
4687 | 237 type_id = H5Dget_type (data_id); |
238 | |
239 type_class_id = H5Tget_class (type_id); | |
240 | |
241 if (type_class_id != H5T_STRING) | |
242 goto done; | |
243 | |
244 space_id = H5Dget_space (data_id); | |
245 hsize_t rank = H5Sget_simple_extent_ndims (space_id); | |
246 | |
247 if (rank != 0) | |
248 goto done; | |
249 | |
250 int slen = H5Tget_size (type_id); | |
251 if (slen < 0) | |
252 goto done; | |
253 | |
254 OCTAVE_LOCAL_BUFFER (char, typ, slen); | |
255 | |
256 // create datatype for (null-terminated) string to read into: | |
257 hid_t st_id = H5Tcopy (H5T_C_S1); | |
258 H5Tset_size (st_id, slen); | |
259 | |
260 if (H5Dread (data_id, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, | |
5760 | 261 typ) < 0) |
4687 | 262 goto done; |
263 | |
264 H5Tclose (st_id); | |
265 H5Dclose (data_id); | |
266 | |
267 d->tc = octave_value_typeinfo::lookup_type (typ); | |
268 | |
9881
b3089dba88bf
Remove HDF5 cruft for older versions of HDF5
Kacper Kowalik
parents:
8920
diff
changeset
|
269 retval = (d->tc.load_hdf5 (subgroup_id, "value") ? 1 : -1); |
4687 | 270 |
271 // check for OCTAVE_GLOBAL attribute: | |
272 d->global = hdf5_check_attr (subgroup_id, "OCTAVE_GLOBAL"); | |
273 | |
274 H5Gclose (subgroup_id); | |
4634 | 275 } |
276 else | |
277 { | |
4687 | 278 // an HDF5 group is treated as an octave structure by |
279 // default (since that preserves name information), and an | |
280 // octave list otherwise. | |
281 | |
282 if (hdf5_check_attr (subgroup_id, "OCTAVE_LIST")) | |
283 d->tc = octave_value_typeinfo::lookup_type ("list"); | |
284 else | |
4948 | 285 d->tc = octave_value_typeinfo::lookup_type ("struct"); |
4687 | 286 |
287 // check for OCTAVE_GLOBAL attribute: | |
288 d->global = hdf5_check_attr (subgroup_id, "OCTAVE_GLOBAL"); | |
289 | |
290 H5Gclose (subgroup_id); | |
4634 | 291 |
9881
b3089dba88bf
Remove HDF5 cruft for older versions of HDF5
Kacper Kowalik
parents:
8920
diff
changeset
|
292 retval = (d->tc.load_hdf5 (group_id, name) ? 1 : -1); |
4687 | 293 } |
294 | |
295 } | |
296 else if (info.type == H5G_DATASET && ident_valid) | |
297 { | |
298 // For backwards compatiability. | |
299 data_id = H5Dopen (group_id, name); | |
300 | |
301 if (data_id < 0) | |
302 { | |
303 retval = data_id; | |
304 goto done; | |
4634 | 305 } |
306 | |
4687 | 307 type_id = H5Dget_type (data_id); |
308 | |
309 type_class_id = H5Tget_class (type_id); | |
310 | |
311 if (type_class_id == H5T_FLOAT) | |
312 { | |
313 space_id = H5Dget_space (data_id); | |
314 | |
315 hsize_t rank = H5Sget_simple_extent_ndims (space_id); | |
316 | |
317 if (rank == 0) | |
318 d->tc = octave_value_typeinfo::lookup_type ("scalar"); | |
319 else | |
320 d->tc = octave_value_typeinfo::lookup_type ("matrix"); | |
321 | |
322 H5Sclose (space_id); | |
323 } | |
4948 | 324 else if (type_class_id == H5T_INTEGER) |
325 { | |
326 // What integer type do we really have.. | |
327 std::string int_typ; | |
328 #ifdef HAVE_H5T_GET_NATIVE_TYPE | |
5775 | 329 // FIXME test this code and activated with an autoconf |
5351 | 330 // test!! It is also incorrect for 64-bit indexing!! |
331 | |
4948 | 332 switch (H5Tget_native_type (type_id, H5T_DIR_ASCEND)) |
333 { | |
334 case H5T_NATIVE_CHAR: | |
335 int_typ = "int8 "; | |
336 break; | |
337 | |
338 case H5T_NATIVE_SHORT: | |
339 int_typ = "int16 "; | |
340 break; | |
341 | |
342 case H5T_NATIVE_INT: | |
343 case H5T_NATIVE_LONG: | |
344 int_typ = "int32 "; | |
345 break; | |
346 | |
347 case H5T_NATIVE_LLONG: | |
348 int_typ = "int64 "; | |
349 break; | |
350 | |
351 case H5T_NATIVE_UCHAR: | |
352 int_typ = "uint8 "; | |
353 break; | |
354 | |
355 case H5T_NATIVE_USHORT: | |
356 int_typ = "uint16 "; | |
357 break; | |
358 | |
359 case H5T_NATIVE_UINT: | |
360 case H5T_NATIVE_ULONG: | |
361 int_typ = "uint32 "; | |
362 break; | |
363 | |
364 case H5T_NATIVE_ULLONG: | |
365 int_typ = "uint64 "; | |
366 break; | |
367 } | |
368 #else | |
369 hid_t int_sign = H5Tget_sign (type_id); | |
370 | |
371 if (int_sign == H5T_SGN_ERROR) | |
372 warning ("load: can't read `%s' (unknown datatype)", name); | |
373 else | |
374 { | |
375 if (int_sign == H5T_SGN_NONE) | |
376 int_typ.append ("u"); | |
377 int_typ.append ("int"); | |
378 | |
379 int slen = H5Tget_size (type_id); | |
380 if (slen < 0) | |
381 warning ("load: can't read `%s' (unknown datatype)", name); | |
382 else | |
383 { | |
384 switch (slen) | |
385 { | |
386 case 1: | |
387 int_typ.append ("8 "); | |
388 break; | |
389 | |
390 case 2: | |
391 int_typ.append ("16 "); | |
392 break; | |
393 | |
394 case 4: | |
395 int_typ.append ("32 "); | |
396 break; | |
397 | |
398 case 8: | |
399 int_typ.append ("64 "); | |
400 break; | |
401 | |
402 default: | |
403 warning ("load: can't read `%s' (unknown datatype)", | |
404 name); | |
405 int_typ = ""; | |
406 break; | |
407 } | |
408 } | |
409 } | |
410 #endif | |
411 if (int_typ == "") | |
412 warning ("load: can't read `%s' (unknown datatype)", name); | |
413 else | |
414 { | |
415 // Matrix or scalar? | |
416 space_id = H5Dget_space (data_id); | |
417 | |
418 hsize_t rank = H5Sget_simple_extent_ndims (space_id); | |
419 | |
420 if (rank == 0) | |
421 int_typ.append ("scalar"); | |
422 else | |
423 int_typ.append ("matrix"); | |
424 | |
425 d->tc = octave_value_typeinfo::lookup_type (int_typ); | |
426 H5Sclose (space_id); | |
427 } | |
428 } | |
4687 | 429 else if (type_class_id == H5T_STRING) |
430 d->tc = octave_value_typeinfo::lookup_type ("string"); | |
431 else if (type_class_id == H5T_COMPOUND) | |
432 { | |
433 hid_t complex_type = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); | |
434 | |
435 if (hdf5_types_compatible (type_id, complex_type)) | |
436 { | |
437 // read complex matrix or scalar variable | |
438 space_id = H5Dget_space (data_id); | |
439 hsize_t rank = H5Sget_simple_extent_ndims (space_id); | |
440 | |
441 if (rank == 0) | |
442 d->tc = octave_value_typeinfo::lookup_type ("complex scalar"); | |
443 else | |
444 d->tc = octave_value_typeinfo::lookup_type ("complex matrix"); | |
445 | |
446 H5Sclose (space_id); | |
447 } | |
448 else | |
449 // Assume that if its not complex its a range. If its not | |
450 // it'll be rejected later in the range code | |
451 d->tc = octave_value_typeinfo::lookup_type ("range"); | |
452 | |
453 H5Tclose (complex_type); | |
454 } | |
455 else | |
456 { | |
457 warning ("load: can't read `%s' (unknown datatype)", name); | |
458 retval = 0; // unknown datatype; skip | |
459 } | |
460 | |
461 // check for OCTAVE_GLOBAL attribute: | |
462 d->global = hdf5_check_attr (data_id, "OCTAVE_GLOBAL"); | |
463 | |
464 H5Tclose (type_id); | |
465 H5Dclose (data_id); | |
466 | |
9881
b3089dba88bf
Remove HDF5 cruft for older versions of HDF5
Kacper Kowalik
parents:
8920
diff
changeset
|
467 retval = (d->tc.load_hdf5 (group_id, name) ? 1 : -1); |
4634 | 468 } |
4687 | 469 |
470 if (!ident_valid) | |
4634 | 471 { |
472 // should we attempt to handle invalid identifiers by converting | |
473 // bad characters to '_', say? | |
474 warning ("load: skipping invalid identifier `%s' in hdf5 file", | |
475 name); | |
476 } | |
477 | |
478 done: | |
479 if (retval < 0) | |
480 error ("load: error while reading hdf5 item %s", name); | |
4687 | 481 |
4634 | 482 if (retval > 0) |
483 { | |
484 // get documentation string, if any: | |
485 int comment_length = H5Gget_comment (group_id, name, 0, 0); | |
486 | |
487 if (comment_length > 1) | |
488 { | |
489 OCTAVE_LOCAL_BUFFER (char, tdoc, comment_length); | |
490 H5Gget_comment (group_id, name, comment_length, tdoc); | |
491 d->doc = tdoc; | |
492 } | |
493 else if (vname != name) | |
494 { | |
4687 | 495 // the name was changed; store the original name |
4634 | 496 // as the documentation string: |
497 d->doc = name; | |
498 } | |
499 | |
500 // copy name (actually, vname): | |
501 d->name = vname; | |
502 } | |
503 | |
504 return retval; | |
505 } | |
506 | |
507 // Read the next Octave variable from the stream IS, which must really be | |
508 // an hdf5_ifstream. Return the variable value in tc, its doc string | |
509 // in doc, and whether it is global in global. The return value is | |
510 // the name of the variable, or NULL if none were found or there was | |
4687 | 511 // and error. |
4634 | 512 std::string |
4687 | 513 read_hdf5_data (std::istream& is, const std::string& /* filename */, |
514 bool& global, octave_value& tc, std::string& doc) | |
4634 | 515 { |
516 std::string retval; | |
517 | |
518 doc.resize (0); | |
519 | |
5760 | 520 hdf5_ifstream& hs = dynamic_cast<hdf5_ifstream&> (is); |
4634 | 521 hdf5_callback_data d; |
522 | |
4696 | 523 herr_t H5Giterate_retval = -1; |
524 | |
525 hsize_t num_obj = 0; | |
5060 | 526 hid_t group_id = H5Gopen (hs.file_id, "/"); |
527 H5Gget_num_objs (group_id, &num_obj); | |
528 H5Gclose (group_id); | |
4696 | 529 if (hs.current_item < static_cast<int> (num_obj)) |
530 H5Giterate_retval = H5Giterate (hs.file_id, "/", &hs.current_item, | |
531 hdf5_read_next_data, &d); | |
4634 | 532 |
533 | |
534 if (H5Giterate_retval > 0) | |
535 { | |
536 global = d.global; | |
537 tc = d.tc; | |
538 doc = d.doc; | |
539 } | |
540 else | |
541 { | |
542 // an error occurred (H5Giterate_retval < 0) or there are no | |
543 // more datasets print an error message if retval < 0? | |
544 // hdf5_read_next_data already printed one, probably. | |
545 } | |
546 | |
547 if (! d.name.empty ()) | |
548 retval = d.name; | |
549 | |
550 return retval; | |
551 } | |
552 | |
553 // Add an attribute named attr_name to loc_id (a simple scalar | |
554 // attribute with value 1). Return value is >= 0 on success. | |
555 static herr_t | |
556 hdf5_add_attr (hid_t loc_id, const char *attr_name) | |
557 { | |
558 herr_t retval = 0; | |
559 | |
560 hid_t as_id = H5Screate (H5S_SCALAR); | |
561 | |
562 if (as_id >= 0) | |
563 { | |
564 hid_t a_id = H5Acreate (loc_id, attr_name, | |
565 H5T_NATIVE_UCHAR, as_id, H5P_DEFAULT); | |
566 | |
567 if (a_id >= 0) | |
568 { | |
569 unsigned char attr_val = 1; | |
570 | |
5760 | 571 retval = H5Awrite (a_id, H5T_NATIVE_UCHAR, &attr_val); |
4634 | 572 |
573 H5Aclose (a_id); | |
574 } | |
575 else | |
576 retval = a_id; | |
577 | |
578 H5Sclose (as_id); | |
579 } | |
580 else | |
581 retval = as_id; | |
582 | |
583 return retval; | |
584 } | |
585 | |
4805 | 586 // Save an empty matrix, if needed. Returns |
587 // > 0 Saved empty matrix | |
588 // = 0 Not an empty matrix; did nothing | |
589 // < 0 Error condition | |
590 int | |
591 save_hdf5_empty (hid_t loc_id, const char *name, const dim_vector d) | |
592 { | |
593 hsize_t sz = d.length (); | |
6276 | 594 OCTAVE_LOCAL_BUFFER (octave_idx_type, dims, sz); |
4805 | 595 bool empty = false; |
596 hid_t space_hid = -1, data_hid = -1; | |
597 int retval; | |
598 for (hsize_t i = 0; i < sz; i++) | |
599 { | |
600 dims[i] = d(i); | |
601 if (dims[i] < 1) | |
602 empty = true; | |
603 } | |
604 | |
605 if (!empty) | |
606 return 0; | |
607 | |
5760 | 608 space_hid = H5Screate_simple (1, &sz, 0); |
4805 | 609 if (space_hid < 0) return space_hid; |
610 | |
5351 | 611 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_IDX, space_hid, |
4805 | 612 H5P_DEFAULT); |
613 if (data_hid < 0) | |
614 { | |
615 H5Sclose (space_hid); | |
616 return data_hid; | |
617 } | |
618 | |
5351 | 619 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
5760 | 620 H5P_DEFAULT, dims) >= 0; |
4805 | 621 |
622 H5Dclose (data_hid); | |
623 H5Sclose (space_hid); | |
624 | |
625 if (retval >= 0) | |
626 retval = hdf5_add_attr (loc_id, "OCTAVE_EMPTY_MATRIX"); | |
627 | |
628 return (retval == 0 ? 1 : retval); | |
629 } | |
630 | |
631 // Load an empty matrix, if needed. Returns | |
632 // > 0 loaded empty matrix, dimensions returned | |
633 // = 0 Not an empty matrix; did nothing | |
634 // < 0 Error condition | |
635 int | |
636 load_hdf5_empty (hid_t loc_id, const char *name, dim_vector &d) | |
637 { | |
638 if (!hdf5_check_attr(loc_id, "OCTAVE_EMPTY_MATRIX")) | |
639 return 0; | |
640 | |
641 hsize_t hdims, maxdims; | |
642 hid_t data_hid = H5Dopen (loc_id, name); | |
643 hid_t space_id = H5Dget_space (data_hid); | |
644 H5Sget_simple_extent_dims (space_id, &hdims, &maxdims); | |
645 int retval; | |
646 | |
5351 | 647 OCTAVE_LOCAL_BUFFER (octave_idx_type, dims, hdims); |
648 | |
649 retval = H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, | |
5760 | 650 H5P_DEFAULT, dims); |
4805 | 651 if (retval >= 0) |
652 { | |
653 d.resize (hdims); | |
654 for (hsize_t i = 0; i < hdims; i++) | |
655 d(i) = dims[i]; | |
656 } | |
657 | |
658 H5Sclose (space_id); | |
659 H5Dclose (data_hid); | |
660 | |
661 return (retval == 0 ? hdims : retval); | |
662 } | |
663 | |
4634 | 664 // save_type_to_hdf5 is not currently used, since hdf5 doesn't yet support |
665 // automatic float<->integer conversions: | |
666 | |
667 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS | |
668 | |
669 // return the HDF5 type id corresponding to the Octave save_type | |
670 | |
4687 | 671 hid_t |
4634 | 672 save_type_to_hdf5 (save_type st) |
673 { | |
674 switch (st) | |
675 { | |
676 case LS_U_CHAR: | |
677 return H5T_NATIVE_UCHAR; | |
678 | |
679 case LS_U_SHORT: | |
680 return H5T_NATIVE_USHORT; | |
681 | |
682 case LS_U_INT: | |
683 return H5T_NATIVE_UINT; | |
684 | |
685 case LS_CHAR: | |
686 return H5T_NATIVE_CHAR; | |
687 | |
688 case LS_SHORT: | |
689 return H5T_NATIVE_SHORT; | |
690 | |
691 case LS_INT: | |
692 return H5T_NATIVE_INT; | |
693 | |
694 case LS_FLOAT: | |
695 return H5T_NATIVE_FLOAT; | |
696 | |
697 case LS_DOUBLE: | |
698 default: | |
699 return H5T_NATIVE_DOUBLE; | |
700 } | |
701 } | |
702 #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */ | |
703 | |
704 // Add the data from TC to the HDF5 location loc_id, which could | |
705 // be either a file or a group within a file. Return true if | |
706 // successful. This function calls itself recursively for lists | |
707 // (stored as HDF5 groups). | |
708 | |
4687 | 709 bool |
4634 | 710 add_hdf5_data (hid_t loc_id, const octave_value& tc, |
711 const std::string& name, const std::string& doc, | |
712 bool mark_as_global, bool save_as_floats) | |
713 { | |
714 hsize_t dims[3]; | |
4687 | 715 hid_t type_id = -1, space_id = -1, data_id = -1, data_type_id = -1; |
716 bool retval = false; | |
717 octave_value val = tc; | |
8401
712cfdc2e417
allow saving diagonal & permutation matrices to hdf5 as full
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
718 // FIXME: diagonal & permutation matrices currently don't know how to save |
712cfdc2e417
allow saving diagonal & permutation matrices to hdf5 as full
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
719 // themselves, so we convert them first to normal matrices using A = A(:,:). |
712cfdc2e417
allow saving diagonal & permutation matrices to hdf5 as full
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
720 // This is a temporary hack. |
712cfdc2e417
allow saving diagonal & permutation matrices to hdf5 as full
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
721 if (val.is_diag_matrix () || val.is_perm_matrix ()) |
8914
354179c24c79
fix hdf5 saving of diag & perm matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
8676
diff
changeset
|
722 val = val.full_value (); |
8401
712cfdc2e417
allow saving diagonal & permutation matrices to hdf5 as full
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
723 |
712cfdc2e417
allow saving diagonal & permutation matrices to hdf5 as full
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
724 std::string t = val.type_name(); |
4634 | 725 |
4687 | 726 data_id = H5Gcreate (loc_id, name.c_str (), 0); |
727 if (data_id < 0) | |
728 goto error_cleanup; | |
4634 | 729 |
4687 | 730 // attach the type of the variable |
731 type_id = H5Tcopy (H5T_C_S1); H5Tset_size (type_id, t.length () + 1); | |
732 if (type_id < 0) | |
733 goto error_cleanup; | |
4634 | 734 |
4687 | 735 dims[0] = 0; |
5760 | 736 space_id = H5Screate_simple (0 , dims, 0); |
4687 | 737 if (space_id < 0) |
738 goto error_cleanup; | |
4634 | 739 |
4687 | 740 data_type_id = H5Dcreate (data_id, "type", type_id, space_id, H5P_DEFAULT); |
741 if (data_type_id < 0 || H5Dwrite (data_type_id, type_id, H5S_ALL, H5S_ALL, | |
5760 | 742 H5P_DEFAULT, t.c_str ()) < 0) |
4687 | 743 goto error_cleanup; |
4634 | 744 |
4687 | 745 // Now call the real function to save the variable |
746 retval = val.save_hdf5 (data_id, "value", save_as_floats); | |
4634 | 747 |
748 // attach doc string as comment: | |
4687 | 749 if (retval && doc.length () > 0 |
4634 | 750 && H5Gset_comment (loc_id, name.c_str (), doc.c_str ()) < 0) |
4687 | 751 retval = false; |
4634 | 752 |
753 // if it's global, add an attribute "OCTAVE_GLOBAL" with value 1 | |
4687 | 754 if (retval && mark_as_global) |
4634 | 755 retval = hdf5_add_attr (data_id, "OCTAVE_GLOBAL") >= 0; |
756 | |
4687 | 757 // We are saving in the new variable format, so mark it |
758 if (retval) | |
759 retval = hdf5_add_attr (data_id, "OCTAVE_NEW_FORMAT") >= 0; | |
760 | |
4634 | 761 error_cleanup: |
762 | |
4687 | 763 if (data_type_id >= 0) |
764 H5Dclose (data_type_id); | |
4634 | 765 |
4687 | 766 if (type_id >= 0) |
767 H5Tclose (type_id); | |
4634 | 768 |
769 if (space_id >= 0) | |
770 H5Sclose (space_id); | |
771 | |
4687 | 772 if (data_id >= 0) |
773 H5Gclose (data_id); | |
774 | |
775 if (! retval) | |
776 error ("save: error while writing `%s' to hdf5 file", name.c_str ()); | |
4634 | 777 |
778 return retval; | |
779 } | |
780 | |
781 // Write data from TC in HDF5 (binary) format to the stream OS, | |
782 // which must be an hdf5_ofstream, returning true on success. | |
783 | |
784 bool | |
785 save_hdf5_data (std::ostream& os, const octave_value& tc, | |
786 const std::string& name, const std::string& doc, | |
787 bool mark_as_global, bool save_as_floats) | |
788 { | |
5760 | 789 hdf5_ofstream& hs = dynamic_cast<hdf5_ofstream&> (os); |
4634 | 790 |
791 return add_hdf5_data (hs.file_id, tc, name, doc, | |
792 mark_as_global, save_as_floats); | |
793 } | |
794 | |
795 #endif | |
796 | |
797 /* | |
798 ;;; Local Variables: *** | |
799 ;;; mode: C++ *** | |
800 ;;; End: *** | |
801 */ |