FiberBundleHDF5  FiberHDF5 Documentation, Revision 2026
High-Performance Fiber Bundle Data Model for Scientific Visualization
Loading...
Searching...
No Matches
F5A.c
Go to the documentation of this file.
1#include "F5A.h"
2#include "F5private.h"
3#include "F5defs.h"
4#include <assert.h>
5
6#include <string.h>
7#include <stdlib.h>
8
9hid_t F5Aappend(hid_t loc_id, const char *name, hid_t type_id, hid_t space_id, hid_t create_plist )
10{
11hid_t id;
12
13 H5E_BEGIN_TRY
14 id = H5Acreate2(loc_id, name, type_id, space_id, create_plist, H5P_DEFAULT );
15 H5E_END_TRY
16
17 if (id<0)
18 return F5Aopen_name(loc_id, name );
19
20 return id;
21}
22
23hid_t F5Atry_to_open(hid_t location, const char*name)
24{
25hid_t retval;
26
27 H5E_BEGIN_TRY
28 retval = F5Aopen_name(location, name);
29 H5E_END_TRY
30
31 return retval;
32}
33
34hid_t F5Aopen_name(hid_t location, const char*name)
35{
36hid_t retval;
37 F5printf(50, "-- F5Aopen_name(,%s)", name);
38// retval = H5Aopen_name(location, name);
39 retval = H5Aopen_by_name(location, ".", name, H5P_DEFAULT, H5P_DEFAULT );
40 return retval;
41}
42
43
44
45int F5Asave_string(hid_t loc_id, const char*name, const char*buf)
46{
47 // delete attribute first to "overwrite" existing string attribute
48 // if ( H5Lexists(loc_id, name, H5P_DEFAULT) )
49 // {
50 // if ( !H5Adelete(loc_id, name) )
51 // return -1;
52 // }
53
54hid_t type_id = H5Tcopy(H5T_C_S1);
55hid_t space_id = H5Screate(H5S_SCALAR);
56hid_t attr_id;
57 {
58 size_t size = strlen(buf);
59 H5Tset_size(type_id, size+1);
60 }
61 H5Tset_strpad(type_id, H5T_STR_NULLTERM);
62
63 H5E_BEGIN_TRY
64 attr_id = H5Acreate2(loc_id, name, type_id, space_id, H5P_DEFAULT, H5P_DEFAULT);
65 H5E_END_TRY
66
67 if (attr_id>0)
68 {
69 H5Awrite(attr_id, type_id, buf);
70 H5Aclose(attr_id);
71 }
72
73 H5Sclose(space_id);
74 H5Tclose(type_id);
75
76 return 1;
77}
78
79int F5Asave_strings(hid_t loc_id, const char*name, const char*buf, int HowMany)
80{
81hid_t type_id = H5Tcopy(H5T_C_S1);
82hsize_t Sdims = HowMany;
83hid_t space_id = H5Screate_simple(1, &Sdims, &Sdims);
84
85
86hid_t attr_id;
87 {
88 size_t size = strlen(buf);
89 H5Tset_size(type_id, size+1);
90 }
91 H5Tset_strpad(type_id, H5T_STR_NULLTERM);
92
93 H5E_BEGIN_TRY
94 attr_id = H5Acreate2(loc_id, name, type_id, space_id, H5P_DEFAULT, H5P_DEFAULT);
95 H5E_END_TRY
96 if (attr_id>0)
97 {
98 H5Awrite(attr_id, type_id, buf);
99 H5Aclose(attr_id);
100 }
101
102 H5Sclose(space_id);
103 H5Tclose(type_id);
104
105 return 1;
106}
107
108
109char* F5Aget_string(hid_t loc_id, const char*name, char*buf, size_t buflen)
110{
111hid_t space_hid;
112hsize_t N;
113hsize_t len;
114
115hid_t attr_type_hid;
116hid_t attr_hid = F5Atry_to_open(loc_id, name );
117
118 if (attr_hid<0)
119 return 0;
120
121 F5printf(300, "F5Aget_string()");
122
123 attr_type_hid = H5Aget_type(attr_hid);
124
125 if (H5Tget_class(attr_type_hid) != H5T_STRING)
126 {
127 F5printf(1, "F5 Error: getAttribute(string): attribute `%s' is not of type string!\n", name);
128 H5Tclose(attr_type_hid);
129 H5Aclose(attr_hid);
130 }
131
132 space_hid = H5Aget_space(attr_hid);
133 assert( space_hid > 0);
134 N = H5Sget_simple_extent_npoints(space_hid);
135
136 len = H5Tget_size(attr_type_hid);
137
138 F5printf(100,"F5 String Attribute `%s': %ld strings of len %ld\n", name, (long)N, (long)len);
139
140 if (!buf)
141 buf = (char*)malloc(N*(len+1));
142 else
143 {
144 if (len>buflen+1)
145 {
146 H5Tclose(attr_type_hid);
147 H5Sclose(space_hid);
148 H5Aclose(attr_hid);
149 return NULL;
150 }
151 }
152
153 if (F_H5Aread(attr_hid, attr_type_hid, buf, name ) < 0)
154 {
155 H5Tclose(attr_type_hid);
156 H5Sclose(space_hid);
157 H5Aclose(attr_hid);
158 return NULL;
159 }
160 H5Sclose(space_hid);
161 H5Tclose(attr_type_hid);
162 H5Aclose(attr_hid);
163 buf[N*len] = 0;
164 return buf;
165}
166
167F5_API hid_t F5Aget_type(hid_t loc_id, const char*name, hid_t* attrib)
168{
169 if (!attrib)
170 {
171 F5printf(50, "-- F5Aget_type() hid_t attrib == NULL");
172 return -1;
173 }
174
175 *attrib = F5Aopen_name(loc_id, name);
176
177 if (*attrib < 0)
178 {
179 F5printf(50, "-- F5Aget_type() hid_t attrib < 0");
180 return *attrib;
181 }
182
183 {
184 hid_t type = H5Aget_type(*attrib);
185 if (type < 0)
186 F5printf(50, "-- F5Aget_type hid_t type < 0");
187 return type;
188 }
189}
190
191/*****************************************************************************************/
192int F5LAsave_dimensions(hid_t Field_id, const char*aname, int rank, const hsize_t*dims)
193{
194int UADims[FIBER_MAX_RANK];
195int i;
196hsize_t Sdims = rank;
197hid_t space_hid = H5Screate_simple(1, &Sdims, &Sdims);
198hid_t attr_hid;
199
200 F5printf(50, "-- F5LAsave_dimensions(,aname=%s, rank=%d,)", aname, rank);
201
202
203 H5E_BEGIN_TRY
204 attr_hid = H5Acreate2(Field_id, aname, H5T_NATIVE_INT, space_hid, H5P_DEFAULT, H5P_DEFAULT);
205 H5E_END_TRY
206
207 if (attr_hid<0)
208 {
209 H5E_BEGIN_TRY
210 attr_hid = H5Aopen_name(Field_id, aname);
211 H5E_END_TRY
212 }
213 if (attr_hid<0)
214 return 0;
215
216 for(i=0; i<rank; i++)
217 UADims[i] = dims[i];
218
219 if (H5Awrite(attr_hid, H5T_NATIVE_INT, UADims )<0)
220 {
221 fprintf(stderr, "F5LAsave_dimensions: CANNOT WRITE %s attribute\n", aname);
222
223 H5Aclose(attr_hid);
224 H5Sclose(space_hid);
225 return 0;
226 }
227
228 H5Aclose(attr_hid);
229 H5Sclose(space_hid);
230 return 1;
231}
232
233int F5LAget_dimensions(hid_t Field_id, const char*aname, hsize_t dims[FIBER_MAX_RANK])
234{
235 hid_t attr_id;
236/*int rank = -1, i, UADims[FIBER_MAX_RANK]; */
237
238 F5printf(50, "-- F5LAget_dimensions(,aname=%s,)", aname);
239
240 H5E_BEGIN_TRY
241 attr_id = F5Aopen_name(Field_id, aname);
242 H5E_END_TRY
243
244 if (attr_id>0)
245 {
246 hid_t space_id = H5Aget_space(attr_id);
247 int rank = H5Sget_simple_extent_npoints(space_id);
248 assert (space_id > 0);
249
250 F_H5Aread(attr_id, H5T_NATIVE_HSIZE, dims, aname );
251 H5Aclose(attr_id);
252 H5Sclose(space_id);
253/*
254 //for(i=0; i<rank; i++)
255 // dims[i] = UADims[i];
256*/
257 return rank;
258 }
259 return -1;
260}
261
262int F5Lget_dimensions(hid_t Field_id, hsize_t*dims)
263{
264hid_t space_id;
266
267 if (rank>=0)
268 return rank;
269
270 H5E_BEGIN_TRY
271 space_id = H5Dget_space(Field_id);
272 H5E_END_TRY
273
274 if (space_id<0)
275 {
276 return -1;
277 }
278 rank = H5Sget_simple_extent_dims(space_id, dims, NULL);
279 H5Sclose(space_id);
280 return rank;
281}
282
283/*
284 A version number is built from three integers.
285 */
286herr_t F5Asave_version(hid_t loc_id, int major, int minor, int release)
287{
288int version_info[3];
289hsize_t Sdims = 3;
290hid_t space_hid = H5Screate_simple(1, &Sdims, &Sdims);
291hid_t attr_hid = H5Acreate2(loc_id, FIBER_VERSION_ATTRIBUTE_NAME, H5T_NATIVE_INT, space_hid, H5P_DEFAULT, H5P_DEFAULT);
292
293 version_info[0] = major;
294 version_info[1] = minor;
295 version_info[2] = release;
296
297 H5Awrite(attr_hid, H5T_NATIVE_INT, version_info);
298 H5Aclose(attr_hid);
299
300 H5Sclose(space_hid);
301 return 0;
302}
303
304herr_t F5Aget_version(hid_t loc_id, int*major, int*minor, int*release)
305{
306int version_info[3];
307hid_t attr_hid;
308hid_t space_hid;
309
310 if (major ) *major = 0;
311 if (minor ) *minor = 0;
312 if (release) *release = 0;
313
314 if (loc_id<=0) return -1;
315
316 attr_hid = F5Aopen_name(loc_id, FIBER_VERSION_ATTRIBUTE_NAME);
317
318 if (attr_hid<=0) return -1;
319
320 space_hid = H5Aget_space(attr_hid);
321
322 if (space_hid<=0)
323 {
324 H5Aclose(attr_hid);
325 return -1;
326 }
327
328 assert( space_hid >0 );
329 /*
330 Could be more generous here...
331 */
332 if (H5Sget_simple_extent_npoints(space_hid) != 3)
333 {
334 if (major ) *major = 0;
335 if (minor ) *minor = 0;
336 if (release) *release = 0;
337
338 H5Sclose(space_hid);
339 H5Aclose(attr_hid);
340 return -1;
341 }
342
343 F_H5Aread(attr_hid, H5T_NATIVE_INT, version_info, FIBER_VERSION_ATTRIBUTE_NAME);
344 H5Aclose(attr_hid);
345 H5Sclose(space_hid);
346
347 if (major ) *major = version_info[0];
348 if (minor ) *minor = version_info[1];
349 if (release) *release = version_info[2];
350
351 return 0;
352}
353
354
355int F5Asave_ints(hid_t loc_id, const char*name, const int data[], hsize_t n)
356{
357hid_t attr_hid;
358 F5printf(50, "-- F5Asave_ints(,name=%s,,n=%d)", name, n);
359
360 {
361 hid_t space_hid = n==1? H5Screate(H5S_SCALAR) : H5Screate_simple(1, &n , &n);
362 H5E_BEGIN_TRY
363 attr_hid = H5Acreate2(loc_id, name, H5T_NATIVE_INT, space_hid, H5P_DEFAULT, H5P_DEFAULT);
364 H5E_END_TRY
365
366 if (attr_hid<0)
367 {
368 int file_data[ 128 ];
369 H5Sclose(space_hid);
370 if (n>128)
371 {
372 F5printf(30, "F5Asave_ints(,%s,,%d) FAILED, comparison >128 unsupported.", name, (int)n);
373 return 0;
374 }
375 if (F5Aget_ints(loc_id, name, file_data, n)>0)
376 {
377 hsize_t i;
378 for(i=0; i<n; i++)
379 {
380 if (file_data[i] != data[i])
381 {
382 if (n==1)
383 F5printf(10, "F5Asave_ints(,%s,,1) FAILED for storing attribute: current value %d; it already exists and has different value %d.",
384 name, (int)data[0], (int)file_data[0]);
385 else
386 F5printf(10, "F5Asave_ints(,%s,,%d) FAILED, attribute already exists and has different values.", name, (int)n);
387 return 0;
388 }
389 }
390 F5printf(50, "F5Asave_ints(,%s,,%d) attribute already exists and has same values, nothing changed.", name, (int)n);
391 return 1;
392 }
393 F5printf(10, "F5Asave_ints(,%s,,%d) FAILED because attribute could not be created.", name, (int)n);
394 H5Sclose(space_hid);
395 return 0;
396 }
397 H5Awrite(attr_hid, H5T_NATIVE_INT, data);
398 H5Aclose(attr_hid);
399
400 H5Sclose(space_hid);
401 }
402 return 1;
403}
404
405
406long F5Aget_ints(hid_t loc_id, const char*name, int*data, hsize_t n)
407{
408hsize_t elems;
409hid_t space_hid,
410 attr_hid = F5Atry_to_open(loc_id, name);
411
412 if (attr_hid<0)
413 return 0;
414
415 space_hid = H5Aget_space(attr_hid);
416
417 assert( space_hid > 0);
418 /*
419 Could be more generous here... currently must match exactly.
420 */
421 elems = H5Sget_simple_extent_npoints(space_hid);
422 if (elems != n)
423 {
424 H5Sclose(space_hid);
425 H5Aclose(attr_hid);
426 return -elems;
427 }
428
429 F_H5Aread(attr_hid, H5T_NATIVE_INT, data, name);
430 H5Aclose(attr_hid);
431 H5Sclose(space_hid);
432
433 return elems;
434}
435
436int F5Astore_integer(hid_t loc_id, const char*name, int data)
437{
438herr_t status;
439hid_t attr_id;
440hid_t space_id = H5Screate(H5S_SCALAR);
441
442 F5printf(50, "-- F5Asave_integer(,%s,%d)\n", name, data);
443
444 H5E_BEGIN_TRY
445 attr_id = H5Acreate2(loc_id, name, H5T_NATIVE_INT, space_id, H5P_DEFAULT, H5P_DEFAULT);
446 H5E_END_TRY
447 H5Sclose(space_id);
448 if (attr_id<0)
449 {
450 attr_id = F5Aopen_name(loc_id, name );
451 if (attr_id>=0)
452 {
453 int file_value = data;
454 status = H5Aread(attr_id, H5T_NATIVE_INT, &file_value);
455 if (status>=0)
456 {
457 H5Aclose(attr_id);
458 /*
459 value in file is identical to what shall be
460 written, so we are just fine
461 */
462 if (file_value == data)
463 {
464 F5printf(40, "F5Asave_integer(,%s,%d): attribute already has value %d, not overwritten!\n",
465 name, data, file_value);
466
467 return 1;
468 }
469
470 F5printf(4, "F5Asave_integer(,%s,%d): attribute has value %d, not overwritten!\n",
471 name, data, file_value);
472 }
473 else
474 {
475 F5printf(4, "F5Asave_integer(,%s,%d): attribute could not be read, not changed!\n",
476 name, data);
477 }
478 }
479 else
480 {
481 F5printf(4, "F5Asave_integer(,%s,%d): can neither create nor open, don't know what to do.\n",
482 name, data);
483 }
484 return 0;
485 }
486 assert(attr_id>=0);
487
488 status = H5Awrite(attr_id, H5T_NATIVE_INT, &data);
489 assert(status>=0);
490 status = H5Aclose(attr_id); assert(status>=0);
491 return 1;
492}
493
494int F5Awrite_integer(hid_t loc_id, const char*name, int data)
495{
496herr_t status;
497hid_t attr_id;
498hid_t space_id = H5Screate(H5S_SCALAR);
499
500 F5printf(50, "-- F5Awrite_integer(,%s,%d)\n", name, data);
501
502 H5E_BEGIN_TRY
503 attr_id = H5Acreate2(loc_id, name, H5T_NATIVE_INT, space_id, H5P_DEFAULT, H5P_DEFAULT);
504 H5E_END_TRY
505 H5Sclose(space_id);
506 if (attr_id<0)
507 {
508 attr_id = F5Aopen_name(loc_id, name );
509 if (attr_id<0)
510 {
511 F5printf(4, "F5Awrite_integer(,%s,%d): can neither create nor open, don't know what to do.\n",
512 name, data);
513 return 0;
514 }
515 }
516 status = H5Awrite(attr_id, H5T_NATIVE_INT, &data);
517 assert(status>=0);
518 if (status<0) // might fail if attribute is different type, in this case need to do something about it
519 {
520 H5Aclose(attr_id);
521 return status;
522 }
523 status = H5Aclose(attr_id);
524 assert(status>=0);
525 return 1;
526}
527
528
529int F5Asave(hid_t loc_id, const char*name, hid_t file_type_id, hid_t mem_type_id, const void*data, hsize_t n)
530{
531hid_t attr_hid;
532 F5printf(50, "-- F5Asave(,name=%s,,n=%d)", name, n);
533
534 {
535 hid_t space_hid = n==1? H5Screate(H5S_SCALAR) : H5Screate_simple(1, &n , &n);
536 H5E_BEGIN_TRY
537 attr_hid = H5Acreate2(loc_id, name, file_type_id, space_hid, H5P_DEFAULT, H5P_DEFAULT);
538 H5E_END_TRY
539
540 if (attr_hid<0)
541 {
542 H5Sclose(space_hid);
543 return 0;
544 }
545
546 H5Awrite(attr_hid, mem_type_id, data);
547 H5Aclose(attr_hid);
548
549 H5Sclose(space_hid);
550 }
551 return 1;
552}
long F5Aget_ints(hid_t loc_id, const char *name, int *data, hsize_t n)
Definition F5A.c:406
char * F5Aget_string(hid_t loc_id, const char *name, char *buf, size_t buflen)
Definition F5A.c:109
hid_t F5Atry_to_open(hid_t location, const char *name)
Definition F5A.c:23
int F5Awrite_integer(hid_t loc_id, const char *name, int data)
Definition F5A.c:494
int F5Asave_string(hid_t loc_id, const char *name, const char *buf)
Definition F5A.c:45
herr_t F5Asave_version(hid_t loc_id, int major, int minor, int release)
Definition F5A.c:286
int F5Asave(hid_t loc_id, const char *name, hid_t file_type_id, hid_t mem_type_id, const void *data, hsize_t n)
Definition F5A.c:529
hid_t F5Aappend(hid_t loc_id, const char *name, hid_t type_id, hid_t space_id, hid_t create_plist)
Definition F5A.c:9
herr_t F5Aget_version(hid_t loc_id, int *major, int *minor, int *release)
Definition F5A.c:304
int F5Astore_integer(hid_t loc_id, const char *name, int data)
Definition F5A.c:436
int F5Asave_ints(hid_t loc_id, const char *name, const int data[], hsize_t n)
Definition F5A.c:355
int F5Asave_strings(hid_t loc_id, const char *name, const char *buf, int HowMany)
Definition F5A.c:79
F5_API hid_t F5Aget_type(hid_t loc_id, const char *name, hid_t *attrib)
Definition F5A.c:167
int F5Lget_dimensions(hid_t Field_id, hsize_t *dims)
Definition F5A.c:262
H5Tclose(type_id)
name
Definition F5P.c:82
#define F5_API
Definition F5WinDLLApi.h:11
#define FIBER_VERSION_ATTRIBUTE_NAME
Definition F5defs.h:19
#define FIBER_MAX_RANK
Definition F5defs.h:105
#define FIBER_FIELD_DATASPACE_DIMENSIONS_ATTRIBUTE
Definition F5defs.h:153
herr_t F_H5Aread(hid_t attr_id, hid_t mem_type_id, void *buf, const char *name)
Definition F5private.c:332
#define F5printf(verbosity,...)
Definition F5private.h:60
int F5LAsave_dimensions(hid_t Field_id, const char *aname, int rank, const hsize_t *dims)
Definition F5A.c:192
int F5LAget_dimensions(hid_t Field_id, const char *aname, hsize_t dims[FIBER_MAX_RANK])
Definition F5A.c:233
hid_t F5Aopen_name(hid_t location, const char *name)
Definition F5A.c:34