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