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" |
|
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 variable, set in read_hdf5_data(), tells whether we are using |
|
181 // a version of HDF5 with a buggy H5Giterate (i.e. which neglects to |
|
182 // increment the index parameter to the next unread item). |
|
183 static bool have_h5giterate_bug = false; |
|
184 |
|
185 // This function is designed to be passed to H5Giterate, which calls it |
|
186 // on each data item in an HDF5 file. For the item whose name is NAME in |
|
187 // the group GROUP_ID, this function sets dv->tc to an Octave representation |
|
188 // of that item. (dv must be a pointer to hdf5_callback_data.) (It also |
|
189 // sets the other fields of dv). |
|
190 // |
|
191 // It returns 1 on success (in which case H5Giterate stops and returns), |
|
192 // -1 on error, and 0 to tell H5Giterate to continue on to the next item |
|
193 // (e.g. if NAME was a data type we don't recognize). |
|
194 |
4687
|
195 herr_t |
4634
|
196 hdf5_read_next_data (hid_t group_id, const char *name, void *dv) |
|
197 { |
|
198 hdf5_callback_data *d = static_cast <hdf5_callback_data *> (dv); |
4687
|
199 hid_t type_id = -1, type_class_id = -1, data_id = -1, subgroup_id = -1, |
|
200 space_id = -1; |
4634
|
201 |
|
202 H5G_stat_t info; |
|
203 herr_t retval = 0; |
|
204 bool ident_valid = valid_identifier (name); |
|
205 |
|
206 std::string vname = name; |
|
207 |
|
208 // Allow identifiers as all digits so we can load lists saved by |
|
209 // earlier versions of Octave. |
|
210 |
4687
|
211 if (! ident_valid ) |
4634
|
212 { |
|
213 // fix the identifier, replacing invalid chars with underscores |
|
214 vname = make_valid_identifier (vname); |
|
215 |
|
216 // check again (in case vname was null, empty, or some such thing): |
|
217 ident_valid = valid_identifier (vname); |
|
218 } |
|
219 |
|
220 H5Gget_objinfo (group_id, name, 1, &info); |
|
221 |
4687
|
222 if (info.type == H5G_GROUP && ident_valid) |
4634
|
223 { |
4687
|
224 subgroup_id = H5Gopen (group_id, name); |
4634
|
225 |
|
226 if (subgroup_id < 0) |
|
227 { |
|
228 retval = subgroup_id; |
|
229 goto done; |
|
230 } |
|
231 |
4687
|
232 if (hdf5_check_attr (subgroup_id, "OCTAVE_NEW_FORMAT")) |
|
233 { |
|
234 data_id = H5Dopen (subgroup_id, "type"); |
4634
|
235 |
4687
|
236 if (data_id < 0) |
4634
|
237 { |
4687
|
238 retval = data_id; |
|
239 goto done; |
4634
|
240 } |
|
241 |
4687
|
242 type_id = H5Dget_type (data_id); |
|
243 |
|
244 type_class_id = H5Tget_class (type_id); |
|
245 |
|
246 if (type_class_id != H5T_STRING) |
|
247 goto done; |
|
248 |
|
249 space_id = H5Dget_space (data_id); |
|
250 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
251 |
|
252 if (rank != 0) |
|
253 goto done; |
|
254 |
|
255 int slen = H5Tget_size (type_id); |
|
256 if (slen < 0) |
|
257 goto done; |
|
258 |
|
259 OCTAVE_LOCAL_BUFFER (char, typ, slen); |
|
260 |
|
261 // create datatype for (null-terminated) string to read into: |
|
262 hid_t st_id = H5Tcopy (H5T_C_S1); |
|
263 H5Tset_size (st_id, slen); |
|
264 |
|
265 if (H5Dread (data_id, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
5760
|
266 typ) < 0) |
4687
|
267 goto done; |
|
268 |
|
269 H5Tclose (st_id); |
|
270 H5Dclose (data_id); |
|
271 |
|
272 d->tc = octave_value_typeinfo::lookup_type (typ); |
|
273 |
|
274 retval = (d->tc.load_hdf5 (subgroup_id, "value", |
|
275 have_h5giterate_bug) ? 1 : -1); |
|
276 |
|
277 // check for OCTAVE_GLOBAL attribute: |
|
278 d->global = hdf5_check_attr (subgroup_id, "OCTAVE_GLOBAL"); |
|
279 |
|
280 H5Gclose (subgroup_id); |
4634
|
281 } |
|
282 else |
|
283 { |
4687
|
284 // an HDF5 group is treated as an octave structure by |
|
285 // default (since that preserves name information), and an |
|
286 // octave list otherwise. |
|
287 |
|
288 if (hdf5_check_attr (subgroup_id, "OCTAVE_LIST")) |
|
289 d->tc = octave_value_typeinfo::lookup_type ("list"); |
|
290 else |
4948
|
291 d->tc = octave_value_typeinfo::lookup_type ("struct"); |
4687
|
292 |
|
293 // check for OCTAVE_GLOBAL attribute: |
|
294 d->global = hdf5_check_attr (subgroup_id, "OCTAVE_GLOBAL"); |
|
295 |
|
296 H5Gclose (subgroup_id); |
4634
|
297 |
4687
|
298 retval = (d->tc.load_hdf5 (group_id, name, have_h5giterate_bug) |
|
299 ? 1 : -1); |
|
300 } |
|
301 |
|
302 } |
|
303 else if (info.type == H5G_DATASET && ident_valid) |
|
304 { |
|
305 // For backwards compatiability. |
|
306 data_id = H5Dopen (group_id, name); |
|
307 |
|
308 if (data_id < 0) |
|
309 { |
|
310 retval = data_id; |
|
311 goto done; |
4634
|
312 } |
|
313 |
4687
|
314 type_id = H5Dget_type (data_id); |
|
315 |
|
316 type_class_id = H5Tget_class (type_id); |
|
317 |
|
318 if (type_class_id == H5T_FLOAT) |
|
319 { |
|
320 space_id = H5Dget_space (data_id); |
|
321 |
|
322 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
323 |
|
324 if (rank == 0) |
|
325 d->tc = octave_value_typeinfo::lookup_type ("scalar"); |
|
326 else |
|
327 d->tc = octave_value_typeinfo::lookup_type ("matrix"); |
|
328 |
|
329 H5Sclose (space_id); |
|
330 } |
4948
|
331 else if (type_class_id == H5T_INTEGER) |
|
332 { |
|
333 // What integer type do we really have.. |
|
334 std::string int_typ; |
|
335 #ifdef HAVE_H5T_GET_NATIVE_TYPE |
5775
|
336 // FIXME test this code and activated with an autoconf |
5351
|
337 // test!! It is also incorrect for 64-bit indexing!! |
|
338 |
4948
|
339 switch (H5Tget_native_type (type_id, H5T_DIR_ASCEND)) |
|
340 { |
|
341 case H5T_NATIVE_CHAR: |
|
342 int_typ = "int8 "; |
|
343 break; |
|
344 |
|
345 case H5T_NATIVE_SHORT: |
|
346 int_typ = "int16 "; |
|
347 break; |
|
348 |
|
349 case H5T_NATIVE_INT: |
|
350 case H5T_NATIVE_LONG: |
|
351 int_typ = "int32 "; |
|
352 break; |
|
353 |
|
354 case H5T_NATIVE_LLONG: |
|
355 int_typ = "int64 "; |
|
356 break; |
|
357 |
|
358 case H5T_NATIVE_UCHAR: |
|
359 int_typ = "uint8 "; |
|
360 break; |
|
361 |
|
362 case H5T_NATIVE_USHORT: |
|
363 int_typ = "uint16 "; |
|
364 break; |
|
365 |
|
366 case H5T_NATIVE_UINT: |
|
367 case H5T_NATIVE_ULONG: |
|
368 int_typ = "uint32 "; |
|
369 break; |
|
370 |
|
371 case H5T_NATIVE_ULLONG: |
|
372 int_typ = "uint64 "; |
|
373 break; |
|
374 } |
|
375 #else |
|
376 hid_t int_sign = H5Tget_sign (type_id); |
|
377 |
|
378 if (int_sign == H5T_SGN_ERROR) |
|
379 warning ("load: can't read `%s' (unknown datatype)", name); |
|
380 else |
|
381 { |
|
382 if (int_sign == H5T_SGN_NONE) |
|
383 int_typ.append ("u"); |
|
384 int_typ.append ("int"); |
|
385 |
|
386 int slen = H5Tget_size (type_id); |
|
387 if (slen < 0) |
|
388 warning ("load: can't read `%s' (unknown datatype)", name); |
|
389 else |
|
390 { |
|
391 switch (slen) |
|
392 { |
|
393 case 1: |
|
394 int_typ.append ("8 "); |
|
395 break; |
|
396 |
|
397 case 2: |
|
398 int_typ.append ("16 "); |
|
399 break; |
|
400 |
|
401 case 4: |
|
402 int_typ.append ("32 "); |
|
403 break; |
|
404 |
|
405 case 8: |
|
406 int_typ.append ("64 "); |
|
407 break; |
|
408 |
|
409 default: |
|
410 warning ("load: can't read `%s' (unknown datatype)", |
|
411 name); |
|
412 int_typ = ""; |
|
413 break; |
|
414 } |
|
415 } |
|
416 } |
|
417 #endif |
|
418 if (int_typ == "") |
|
419 warning ("load: can't read `%s' (unknown datatype)", name); |
|
420 else |
|
421 { |
|
422 // Matrix or scalar? |
|
423 space_id = H5Dget_space (data_id); |
|
424 |
|
425 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
426 |
|
427 if (rank == 0) |
|
428 int_typ.append ("scalar"); |
|
429 else |
|
430 int_typ.append ("matrix"); |
|
431 |
|
432 d->tc = octave_value_typeinfo::lookup_type (int_typ); |
|
433 H5Sclose (space_id); |
|
434 } |
|
435 } |
4687
|
436 else if (type_class_id == H5T_STRING) |
|
437 d->tc = octave_value_typeinfo::lookup_type ("string"); |
|
438 else if (type_class_id == H5T_COMPOUND) |
|
439 { |
|
440 hid_t complex_type = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); |
|
441 |
|
442 if (hdf5_types_compatible (type_id, complex_type)) |
|
443 { |
|
444 // read complex matrix or scalar variable |
|
445 space_id = H5Dget_space (data_id); |
|
446 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
447 |
|
448 if (rank == 0) |
|
449 d->tc = octave_value_typeinfo::lookup_type ("complex scalar"); |
|
450 else |
|
451 d->tc = octave_value_typeinfo::lookup_type ("complex matrix"); |
|
452 |
|
453 H5Sclose (space_id); |
|
454 } |
|
455 else |
|
456 // Assume that if its not complex its a range. If its not |
|
457 // it'll be rejected later in the range code |
|
458 d->tc = octave_value_typeinfo::lookup_type ("range"); |
|
459 |
|
460 H5Tclose (complex_type); |
|
461 } |
|
462 else |
|
463 { |
|
464 warning ("load: can't read `%s' (unknown datatype)", name); |
|
465 retval = 0; // unknown datatype; skip |
|
466 } |
|
467 |
|
468 // check for OCTAVE_GLOBAL attribute: |
|
469 d->global = hdf5_check_attr (data_id, "OCTAVE_GLOBAL"); |
|
470 |
|
471 H5Tclose (type_id); |
|
472 H5Dclose (data_id); |
|
473 |
|
474 retval = (d->tc.load_hdf5 (group_id, name, have_h5giterate_bug) |
|
475 ? 1 : -1); |
4634
|
476 } |
4687
|
477 |
|
478 if (!ident_valid) |
4634
|
479 { |
|
480 // should we attempt to handle invalid identifiers by converting |
|
481 // bad characters to '_', say? |
|
482 warning ("load: skipping invalid identifier `%s' in hdf5 file", |
|
483 name); |
|
484 } |
|
485 |
|
486 done: |
|
487 if (retval < 0) |
|
488 error ("load: error while reading hdf5 item %s", name); |
4687
|
489 |
4634
|
490 if (retval > 0) |
|
491 { |
|
492 // get documentation string, if any: |
|
493 int comment_length = H5Gget_comment (group_id, name, 0, 0); |
|
494 |
|
495 if (comment_length > 1) |
|
496 { |
|
497 OCTAVE_LOCAL_BUFFER (char, tdoc, comment_length); |
|
498 H5Gget_comment (group_id, name, comment_length, tdoc); |
|
499 d->doc = tdoc; |
|
500 } |
|
501 else if (vname != name) |
|
502 { |
4687
|
503 // the name was changed; store the original name |
4634
|
504 // as the documentation string: |
|
505 d->doc = name; |
|
506 } |
|
507 |
|
508 // copy name (actually, vname): |
|
509 d->name = vname; |
|
510 } |
|
511 |
|
512 return retval; |
|
513 } |
|
514 |
|
515 // Read the next Octave variable from the stream IS, which must really be |
|
516 // an hdf5_ifstream. Return the variable value in tc, its doc string |
|
517 // in doc, and whether it is global in global. The return value is |
|
518 // the name of the variable, or NULL if none were found or there was |
4687
|
519 // and error. |
4634
|
520 std::string |
4687
|
521 read_hdf5_data (std::istream& is, const std::string& /* filename */, |
|
522 bool& global, octave_value& tc, std::string& doc) |
4634
|
523 { |
|
524 std::string retval; |
|
525 |
|
526 doc.resize (0); |
|
527 |
5760
|
528 hdf5_ifstream& hs = dynamic_cast<hdf5_ifstream&> (is); |
4634
|
529 hdf5_callback_data d; |
|
530 |
|
531 // Versions of HDF5 prior to 1.2.2 had a bug in H5Giterate where it |
|
532 // would return the index of the last item processed instead of the |
|
533 // next item to be processed, forcing us to increment the index manually. |
|
534 |
|
535 unsigned int vers_major, vers_minor, vers_release; |
|
536 |
|
537 H5get_libversion (&vers_major, &vers_minor, &vers_release); |
|
538 |
5775
|
539 // FIXME -- this test looks wrong. |
4634
|
540 have_h5giterate_bug |
|
541 = (vers_major < 1 |
|
542 || (vers_major == 1 && (vers_minor < 2 |
|
543 || (vers_minor == 2 && vers_release < 2)))); |
|
544 |
4696
|
545 herr_t H5Giterate_retval = -1; |
|
546 |
|
547 #ifdef HAVE_H5GGET_NUM_OBJS |
|
548 hsize_t num_obj = 0; |
5060
|
549 hid_t group_id = H5Gopen (hs.file_id, "/"); |
|
550 H5Gget_num_objs (group_id, &num_obj); |
|
551 H5Gclose (group_id); |
4696
|
552 if (hs.current_item < static_cast<int> (num_obj)) |
|
553 #endif |
|
554 H5Giterate_retval = H5Giterate (hs.file_id, "/", &hs.current_item, |
|
555 hdf5_read_next_data, &d); |
4634
|
556 |
|
557 if (have_h5giterate_bug) |
|
558 { |
|
559 // H5Giterate sets current_item to the last item processed; we want |
|
560 // the index of the next item (for the next call to read_hdf5_data) |
|
561 |
|
562 hs.current_item++; |
|
563 } |
|
564 |
|
565 if (H5Giterate_retval > 0) |
|
566 { |
|
567 global = d.global; |
|
568 tc = d.tc; |
|
569 doc = d.doc; |
|
570 } |
|
571 else |
|
572 { |
|
573 // an error occurred (H5Giterate_retval < 0) or there are no |
|
574 // more datasets print an error message if retval < 0? |
|
575 // hdf5_read_next_data already printed one, probably. |
|
576 } |
|
577 |
|
578 if (! d.name.empty ()) |
|
579 retval = d.name; |
|
580 |
|
581 return retval; |
|
582 } |
|
583 |
|
584 // Add an attribute named attr_name to loc_id (a simple scalar |
|
585 // attribute with value 1). Return value is >= 0 on success. |
|
586 static herr_t |
|
587 hdf5_add_attr (hid_t loc_id, const char *attr_name) |
|
588 { |
|
589 herr_t retval = 0; |
|
590 |
|
591 hid_t as_id = H5Screate (H5S_SCALAR); |
|
592 |
|
593 if (as_id >= 0) |
|
594 { |
|
595 hid_t a_id = H5Acreate (loc_id, attr_name, |
|
596 H5T_NATIVE_UCHAR, as_id, H5P_DEFAULT); |
|
597 |
|
598 if (a_id >= 0) |
|
599 { |
|
600 unsigned char attr_val = 1; |
|
601 |
5760
|
602 retval = H5Awrite (a_id, H5T_NATIVE_UCHAR, &attr_val); |
4634
|
603 |
|
604 H5Aclose (a_id); |
|
605 } |
|
606 else |
|
607 retval = a_id; |
|
608 |
|
609 H5Sclose (as_id); |
|
610 } |
|
611 else |
|
612 retval = as_id; |
|
613 |
|
614 return retval; |
|
615 } |
|
616 |
4805
|
617 // Save an empty matrix, if needed. Returns |
|
618 // > 0 Saved empty matrix |
|
619 // = 0 Not an empty matrix; did nothing |
|
620 // < 0 Error condition |
|
621 int |
|
622 save_hdf5_empty (hid_t loc_id, const char *name, const dim_vector d) |
|
623 { |
|
624 hsize_t sz = d.length (); |
6276
|
625 OCTAVE_LOCAL_BUFFER (octave_idx_type, dims, sz); |
4805
|
626 bool empty = false; |
|
627 hid_t space_hid = -1, data_hid = -1; |
|
628 int retval; |
|
629 for (hsize_t i = 0; i < sz; i++) |
|
630 { |
|
631 dims[i] = d(i); |
|
632 if (dims[i] < 1) |
|
633 empty = true; |
|
634 } |
|
635 |
|
636 if (!empty) |
|
637 return 0; |
|
638 |
5760
|
639 space_hid = H5Screate_simple (1, &sz, 0); |
4805
|
640 if (space_hid < 0) return space_hid; |
|
641 |
5351
|
642 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_IDX, space_hid, |
4805
|
643 H5P_DEFAULT); |
|
644 if (data_hid < 0) |
|
645 { |
|
646 H5Sclose (space_hid); |
|
647 return data_hid; |
|
648 } |
|
649 |
5351
|
650 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
5760
|
651 H5P_DEFAULT, dims) >= 0; |
4805
|
652 |
|
653 H5Dclose (data_hid); |
|
654 H5Sclose (space_hid); |
|
655 |
|
656 if (retval >= 0) |
|
657 retval = hdf5_add_attr (loc_id, "OCTAVE_EMPTY_MATRIX"); |
|
658 |
|
659 return (retval == 0 ? 1 : retval); |
|
660 } |
|
661 |
|
662 // Load an empty matrix, if needed. Returns |
|
663 // > 0 loaded empty matrix, dimensions returned |
|
664 // = 0 Not an empty matrix; did nothing |
|
665 // < 0 Error condition |
|
666 int |
|
667 load_hdf5_empty (hid_t loc_id, const char *name, dim_vector &d) |
|
668 { |
|
669 if (!hdf5_check_attr(loc_id, "OCTAVE_EMPTY_MATRIX")) |
|
670 return 0; |
|
671 |
|
672 hsize_t hdims, maxdims; |
|
673 hid_t data_hid = H5Dopen (loc_id, name); |
|
674 hid_t space_id = H5Dget_space (data_hid); |
|
675 H5Sget_simple_extent_dims (space_id, &hdims, &maxdims); |
|
676 int retval; |
|
677 |
5351
|
678 OCTAVE_LOCAL_BUFFER (octave_idx_type, dims, hdims); |
|
679 |
|
680 retval = H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
5760
|
681 H5P_DEFAULT, dims); |
4805
|
682 if (retval >= 0) |
|
683 { |
|
684 d.resize (hdims); |
|
685 for (hsize_t i = 0; i < hdims; i++) |
|
686 d(i) = dims[i]; |
|
687 } |
|
688 |
|
689 H5Sclose (space_id); |
|
690 H5Dclose (data_hid); |
|
691 |
|
692 return (retval == 0 ? hdims : retval); |
|
693 } |
|
694 |
4634
|
695 // save_type_to_hdf5 is not currently used, since hdf5 doesn't yet support |
|
696 // automatic float<->integer conversions: |
|
697 |
|
698 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
|
699 |
|
700 // return the HDF5 type id corresponding to the Octave save_type |
|
701 |
4687
|
702 hid_t |
4634
|
703 save_type_to_hdf5 (save_type st) |
|
704 { |
|
705 switch (st) |
|
706 { |
|
707 case LS_U_CHAR: |
|
708 return H5T_NATIVE_UCHAR; |
|
709 |
|
710 case LS_U_SHORT: |
|
711 return H5T_NATIVE_USHORT; |
|
712 |
|
713 case LS_U_INT: |
|
714 return H5T_NATIVE_UINT; |
|
715 |
|
716 case LS_CHAR: |
|
717 return H5T_NATIVE_CHAR; |
|
718 |
|
719 case LS_SHORT: |
|
720 return H5T_NATIVE_SHORT; |
|
721 |
|
722 case LS_INT: |
|
723 return H5T_NATIVE_INT; |
|
724 |
|
725 case LS_FLOAT: |
|
726 return H5T_NATIVE_FLOAT; |
|
727 |
|
728 case LS_DOUBLE: |
|
729 default: |
|
730 return H5T_NATIVE_DOUBLE; |
|
731 } |
|
732 } |
|
733 #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */ |
|
734 |
|
735 // Add the data from TC to the HDF5 location loc_id, which could |
|
736 // be either a file or a group within a file. Return true if |
|
737 // successful. This function calls itself recursively for lists |
|
738 // (stored as HDF5 groups). |
|
739 |
4687
|
740 bool |
4634
|
741 add_hdf5_data (hid_t loc_id, const octave_value& tc, |
|
742 const std::string& name, const std::string& doc, |
|
743 bool mark_as_global, bool save_as_floats) |
|
744 { |
|
745 hsize_t dims[3]; |
4687
|
746 hid_t type_id = -1, space_id = -1, data_id = -1, data_type_id = -1; |
|
747 bool retval = false; |
|
748 octave_value val = tc; |
|
749 std::string t = tc.type_name(); |
4634
|
750 |
4687
|
751 data_id = H5Gcreate (loc_id, name.c_str (), 0); |
|
752 if (data_id < 0) |
|
753 goto error_cleanup; |
4634
|
754 |
4687
|
755 // attach the type of the variable |
|
756 type_id = H5Tcopy (H5T_C_S1); H5Tset_size (type_id, t.length () + 1); |
|
757 if (type_id < 0) |
|
758 goto error_cleanup; |
4634
|
759 |
4687
|
760 dims[0] = 0; |
5760
|
761 space_id = H5Screate_simple (0 , dims, 0); |
4687
|
762 if (space_id < 0) |
|
763 goto error_cleanup; |
4634
|
764 |
4687
|
765 data_type_id = H5Dcreate (data_id, "type", type_id, space_id, H5P_DEFAULT); |
|
766 if (data_type_id < 0 || H5Dwrite (data_type_id, type_id, H5S_ALL, H5S_ALL, |
5760
|
767 H5P_DEFAULT, t.c_str ()) < 0) |
4687
|
768 goto error_cleanup; |
4634
|
769 |
4687
|
770 // Now call the real function to save the variable |
|
771 retval = val.save_hdf5 (data_id, "value", save_as_floats); |
4634
|
772 |
|
773 // attach doc string as comment: |
4687
|
774 if (retval && doc.length () > 0 |
4634
|
775 && H5Gset_comment (loc_id, name.c_str (), doc.c_str ()) < 0) |
4687
|
776 retval = false; |
4634
|
777 |
|
778 // if it's global, add an attribute "OCTAVE_GLOBAL" with value 1 |
4687
|
779 if (retval && mark_as_global) |
4634
|
780 retval = hdf5_add_attr (data_id, "OCTAVE_GLOBAL") >= 0; |
|
781 |
4687
|
782 // We are saving in the new variable format, so mark it |
|
783 if (retval) |
|
784 retval = hdf5_add_attr (data_id, "OCTAVE_NEW_FORMAT") >= 0; |
|
785 |
4634
|
786 error_cleanup: |
|
787 |
4687
|
788 if (data_type_id >= 0) |
|
789 H5Dclose (data_type_id); |
4634
|
790 |
4687
|
791 if (type_id >= 0) |
|
792 H5Tclose (type_id); |
4634
|
793 |
|
794 if (space_id >= 0) |
|
795 H5Sclose (space_id); |
|
796 |
4687
|
797 if (data_id >= 0) |
|
798 H5Gclose (data_id); |
|
799 |
|
800 if (! retval) |
|
801 error ("save: error while writing `%s' to hdf5 file", name.c_str ()); |
4634
|
802 |
|
803 return retval; |
|
804 } |
|
805 |
|
806 // Write data from TC in HDF5 (binary) format to the stream OS, |
|
807 // which must be an hdf5_ofstream, returning true on success. |
|
808 |
|
809 bool |
|
810 save_hdf5_data (std::ostream& os, const octave_value& tc, |
|
811 const std::string& name, const std::string& doc, |
|
812 bool mark_as_global, bool save_as_floats) |
|
813 { |
5760
|
814 hdf5_ofstream& hs = dynamic_cast<hdf5_ofstream&> (os); |
4634
|
815 |
|
816 return add_hdf5_data (hs.file_id, tc, name, doc, |
|
817 mark_as_global, save_as_floats); |
|
818 } |
|
819 |
|
820 #endif |
|
821 |
|
822 /* |
|
823 ;;; Local Variables: *** |
|
824 ;;; mode: C++ *** |
|
825 ;;; End: *** |
|
826 */ |