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