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