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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
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 "lo-sstream.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, |
|
268 (void *) typ) < 0) |
|
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 |
|
338 // XXX FIXME XX test this code and activated with an autoconf test!! |
|
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 |
|
528 hdf5_ifstream& hs = (hdf5_ifstream&) is; |
|
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 |
|
539 // XXX FIXME XXX -- this test looks wrong. |
|
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 |
|
602 retval = H5Awrite (a_id, H5T_NATIVE_UCHAR, (void*) &attr_val); |
|
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 (); |
|
625 int dims[sz]; |
|
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 |
|
639 space_hid = H5Screate_simple (1, &sz, (hsize_t *) 0); |
|
640 if (space_hid < 0) return space_hid; |
|
641 |
|
642 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_INT, space_hid, |
|
643 H5P_DEFAULT); |
|
644 if (data_hid < 0) |
|
645 { |
|
646 H5Sclose (space_hid); |
|
647 return data_hid; |
|
648 } |
|
649 |
|
650 retval = H5Dwrite (data_hid, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, |
|
651 H5P_DEFAULT, (void*) dims) >= 0; |
|
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 dims[hdims]; |
|
677 int retval; |
|
678 |
|
679 retval = H5Dread (data_hid, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, |
|
680 H5P_DEFAULT, (void *) dims); |
|
681 if (retval >= 0) |
|
682 { |
|
683 d.resize (hdims); |
|
684 for (hsize_t i = 0; i < hdims; i++) |
|
685 d(i) = dims[i]; |
|
686 } |
|
687 |
|
688 H5Sclose (space_id); |
|
689 H5Dclose (data_hid); |
|
690 |
|
691 return (retval == 0 ? hdims : retval); |
|
692 } |
|
693 |
4634
|
694 // save_type_to_hdf5 is not currently used, since hdf5 doesn't yet support |
|
695 // automatic float<->integer conversions: |
|
696 |
|
697 #if HAVE_HDF5_INT2FLOAT_CONVERSIONS |
|
698 |
|
699 // return the HDF5 type id corresponding to the Octave save_type |
|
700 |
4687
|
701 hid_t |
4634
|
702 save_type_to_hdf5 (save_type st) |
|
703 { |
|
704 switch (st) |
|
705 { |
|
706 case LS_U_CHAR: |
|
707 return H5T_NATIVE_UCHAR; |
|
708 |
|
709 case LS_U_SHORT: |
|
710 return H5T_NATIVE_USHORT; |
|
711 |
|
712 case LS_U_INT: |
|
713 return H5T_NATIVE_UINT; |
|
714 |
|
715 case LS_CHAR: |
|
716 return H5T_NATIVE_CHAR; |
|
717 |
|
718 case LS_SHORT: |
|
719 return H5T_NATIVE_SHORT; |
|
720 |
|
721 case LS_INT: |
|
722 return H5T_NATIVE_INT; |
|
723 |
|
724 case LS_FLOAT: |
|
725 return H5T_NATIVE_FLOAT; |
|
726 |
|
727 case LS_DOUBLE: |
|
728 default: |
|
729 return H5T_NATIVE_DOUBLE; |
|
730 } |
|
731 } |
|
732 #endif /* HAVE_HDF5_INT2FLOAT_CONVERSIONS */ |
|
733 |
|
734 // Add the data from TC to the HDF5 location loc_id, which could |
|
735 // be either a file or a group within a file. Return true if |
|
736 // successful. This function calls itself recursively for lists |
|
737 // (stored as HDF5 groups). |
|
738 |
4687
|
739 bool |
4634
|
740 add_hdf5_data (hid_t loc_id, const octave_value& tc, |
|
741 const std::string& name, const std::string& doc, |
|
742 bool mark_as_global, bool save_as_floats) |
|
743 { |
|
744 hsize_t dims[3]; |
4687
|
745 hid_t type_id = -1, space_id = -1, data_id = -1, data_type_id = -1; |
|
746 bool retval = false; |
|
747 octave_value val = tc; |
|
748 std::string t = tc.type_name(); |
4634
|
749 |
4687
|
750 data_id = H5Gcreate (loc_id, name.c_str (), 0); |
|
751 if (data_id < 0) |
|
752 goto error_cleanup; |
4634
|
753 |
4687
|
754 // attach the type of the variable |
|
755 type_id = H5Tcopy (H5T_C_S1); H5Tset_size (type_id, t.length () + 1); |
|
756 if (type_id < 0) |
|
757 goto error_cleanup; |
4634
|
758 |
4687
|
759 dims[0] = 0; |
|
760 space_id = H5Screate_simple (0 , dims, (hsize_t*) 0); |
|
761 if (space_id < 0) |
|
762 goto error_cleanup; |
4634
|
763 |
4687
|
764 data_type_id = H5Dcreate (data_id, "type", type_id, space_id, H5P_DEFAULT); |
|
765 if (data_type_id < 0 || H5Dwrite (data_type_id, type_id, H5S_ALL, H5S_ALL, |
|
766 H5P_DEFAULT, (void*) t.c_str ()) < 0) |
|
767 goto error_cleanup; |
4634
|
768 |
4687
|
769 // Now call the real function to save the variable |
|
770 retval = val.save_hdf5 (data_id, "value", save_as_floats); |
4634
|
771 |
|
772 // attach doc string as comment: |
4687
|
773 if (retval && doc.length () > 0 |
4634
|
774 && H5Gset_comment (loc_id, name.c_str (), doc.c_str ()) < 0) |
4687
|
775 retval = false; |
4634
|
776 |
|
777 // if it's global, add an attribute "OCTAVE_GLOBAL" with value 1 |
4687
|
778 if (retval && mark_as_global) |
4634
|
779 retval = hdf5_add_attr (data_id, "OCTAVE_GLOBAL") >= 0; |
|
780 |
4687
|
781 // We are saving in the new variable format, so mark it |
|
782 if (retval) |
|
783 retval = hdf5_add_attr (data_id, "OCTAVE_NEW_FORMAT") >= 0; |
|
784 |
4634
|
785 error_cleanup: |
|
786 |
4687
|
787 if (data_type_id >= 0) |
|
788 H5Dclose (data_type_id); |
4634
|
789 |
4687
|
790 if (type_id >= 0) |
|
791 H5Tclose (type_id); |
4634
|
792 |
|
793 if (space_id >= 0) |
|
794 H5Sclose (space_id); |
|
795 |
4687
|
796 if (data_id >= 0) |
|
797 H5Gclose (data_id); |
|
798 |
|
799 if (! retval) |
|
800 error ("save: error while writing `%s' to hdf5 file", name.c_str ()); |
4634
|
801 |
|
802 return retval; |
|
803 } |
|
804 |
|
805 // Write data from TC in HDF5 (binary) format to the stream OS, |
|
806 // which must be an hdf5_ofstream, returning true on success. |
|
807 |
|
808 bool |
|
809 save_hdf5_data (std::ostream& os, const octave_value& tc, |
|
810 const std::string& name, const std::string& doc, |
|
811 bool mark_as_global, bool save_as_floats) |
|
812 { |
|
813 hdf5_ofstream& hs = (hdf5_ofstream&) os; |
|
814 |
|
815 return add_hdf5_data (hs.file_id, tc, name, doc, |
|
816 mark_as_global, save_as_floats); |
|
817 } |
|
818 |
|
819 #endif |
|
820 |
|
821 /* |
|
822 ;;; Local Variables: *** |
|
823 ;;; mode: C++ *** |
|
824 ;;; End: *** |
|
825 */ |