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