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