FiberBundleHDF5  FiberHDF5 Documentation, Revision 2026
High-Performance Fiber Bundle Data Model for Scientific Visualization
Loading...
Searching...
No Matches
F5A.c File Reference
#include "F5A.h"
#include "F5private.h"
#include "F5defs.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>

Go to the source code of this file.

Functions

hid_t F5Aappend (hid_t loc_id, const char *name, hid_t type_id, hid_t space_id, hid_t create_plist)
hid_t F5Atry_to_open (hid_t location, const char *name)
hid_t F5Aopen_name (hid_t location, const char *name)
int F5Asave_string (hid_t loc_id, const char *name, const char *buf)
int F5Asave_strings (hid_t loc_id, const char *name, const char *buf, int HowMany)
char * F5Aget_string (hid_t loc_id, const char *name, char *buf, size_t buflen)
F5_API hid_t F5Aget_type (hid_t loc_id, const char *name, hid_t *attrib)
int F5LAsave_dimensions (hid_t Field_id, const char *aname, int rank, const hsize_t *dims)
int F5LAget_dimensions (hid_t Field_id, const char *aname, hsize_t dims[FIBER_MAX_RANK])
int F5Lget_dimensions (hid_t Field_id, hsize_t *dims)
herr_t F5Asave_version (hid_t loc_id, int major, int minor, int release)
herr_t F5Aget_version (hid_t loc_id, int *major, int *minor, int *release)
int F5Asave_ints (hid_t loc_id, const char *name, const int data[], hsize_t n)
long F5Aget_ints (hid_t loc_id, const char *name, int *data, hsize_t n)
int F5Astore_integer (hid_t loc_id, const char *name, int data)
int F5Awrite_integer (hid_t loc_id, const char *name, int data)
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)

Function Documentation

◆ F5Aappend()

hid_t F5Aappend ( hid_t loc_id,
const char * name,
hid_t type_id,
hid_t space_id,
hid_t create_plist )

Open some attribute if it exists, otherwise create one.

Definition at line 9 of file F5A.c.

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}
name
Definition F5P.c:82
hid_t F5Aopen_name(hid_t location, const char *name)
Definition F5A.c:34

References F5Aopen_name(), and name.

Referenced by F5Bappend_slice().

◆ F5Aget_ints()

long F5Aget_ints ( hid_t loc_id,
const char * name,
int * data,
hsize_t n )

Get a number of integers from an attribute which is an integer array. The specified number n must match exactly with the number of elements.

Returns
positive number of elements (n) if success, 0 if attribute not found, and the negative number of elements otherwise. Success can easily be tested by checking the return value to be larger than zero.

Definition at line 406 of file F5A.c.

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}
hid_t F5Atry_to_open(hid_t location, const char *name)
Definition F5A.c:23
herr_t F_H5Aread(hid_t attr_id, hid_t mem_type_id, void *buf, const char *name)
Definition F5private.c:332

References F5Atry_to_open(), F_H5Aread(), and name.

Referenced by F5Asave_ints(), F5LTexpand_dataspace(), F5LTget_index_depth(), F5LTget_skeleton_dimensionality(), F5LTset_dataspace(), and F5Tget_index_depth().

◆ F5Aget_string()

char * F5Aget_string ( hid_t loc_id,
const char * name,
char * buf,
size_t len )

Get a string attribute from the file. If buf is a NULL pointer, then the string is allocated dynamically and the caller needs to issue free() on the result. If the string could not be read, returns a NULL pointer.

Definition at line 109 of file F5A.c.

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}
H5Tclose(type_id)
#define F5printf(verbosity,...)
Definition F5private.h:60

References F5Atry_to_open(), F5printf, F_H5Aread(), H5Tclose(), and name.

Referenced by F5B_read_chart_domain().

◆ F5Aget_type()

F5_API hid_t F5Aget_type ( hid_t loc_id,
const char * name,
hid_t * attrib )

Get type of an attribute by name. After calling this function both, attribute and type must be closed, otherwise warnings will occur

Definition at line 167 of file F5A.c.

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}

References F5_API, F5Aopen_name(), F5printf, and name.

Referenced by F5Fis_attribute_of_type(), and F5Fis_fragment_attribute_of_type().

◆ F5Aget_version()

herr_t F5Aget_version ( hid_t loc_id,
int * major,
int * minor,
int * release )

Get version number for given object.

Parameters
loc_idAny HDF5 object that may carry attributes.

Definition at line 304 of file F5A.c.

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}
#define FIBER_VERSION_ATTRIBUTE_NAME
Definition F5defs.h:19

References F5Aopen_name(), F_H5Aread(), and FIBER_VERSION_ATTRIBUTE_NAME.

Referenced by F5Tget_field_Array_enum(), and F5Tget_field_ProcArray_enum().

◆ F5Asave()

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 at line 529 of file F5A.c.

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}

References F5printf, and name.

Referenced by F5FwriteIMAGE().

◆ F5Asave_ints()

int F5Asave_ints ( hid_t loc_id,
const char * name,
const int data[],
hsize_t n )

Save an array of integers as an attribute.

Returns
non-zero in case of error

Definition at line 355 of file F5A.c.

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}
long F5Aget_ints(hid_t loc_id, const char *name, int *data, hsize_t n)
Definition F5A.c:406

References F5Aget_ints(), F5printf, and name.

Referenced by F5B_save_global_chart(), F5file_type(), F5LTcreate_topology(), F5LwriteX(), and F5Tsave_tensor().

◆ F5Asave_string()

int F5Asave_string ( hid_t loc_id,
const char * name,
const char * buf )

Create a string attribute which is of the size of the given string and write it to the file.

Definition at line 45 of file F5A.c.

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}

References H5Tclose(), and name.

Referenced by F5FwriteIMAGE(), F5Tsave_F5field_enum(), F5Tsave_ProcArray_enum(), and F5Tsave_tensor().

◆ F5Asave_strings()

int F5Asave_strings ( hid_t loc_id,
const char * name,
const char * buf,
int HowMany )

Definition at line 79 of file F5A.c.

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}

References H5Tclose(), and name.

Referenced by F5FwriteIMAGE().

◆ F5Asave_version()

herr_t F5Asave_version ( hid_t loc_id,
int major,
int minor,
int release )

Definition at line 286 of file F5A.c.

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}

References FIBER_VERSION_ATTRIBUTE_NAME.

Referenced by F5I_create_contents(), F5Tsave_F5field_enum(), and F5Tsave_ProcArray_enum().

◆ F5Astore_integer()

int F5Astore_integer ( hid_t loc_id,
const char * name,
int data )

Save an integer to the file as an attribute, checking if it already existed, and if so, that it had the same value.

Returns
non-zero in case of error, such as different value in file.

Definition at line 436 of file F5A.c.

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}

References F5Aopen_name(), F5printf, and name.

Referenced by F5LTcreate_topology().

◆ F5Atry_to_open()

hid_t F5Atry_to_open ( hid_t location,
const char * name )

Try to open an HDF5 attribute, silently return -1 if it does not exist.

Definition at line 23 of file F5A.c.

24{
25hid_t retval;
26
27 H5E_BEGIN_TRY
28 retval = F5Aopen_name(location, name);
29 H5E_END_TRY
30
31 return retval;
32}

References F5Aopen_name(), and name.

Referenced by F5Aget_ints(), F5Aget_string(), F5B_read_chart_domain(), F5Dset_type_attribute(), F5Fget_attribute_byname(), F5Fset_attribute_byname(), F5Lget_type(), and F5Tget_field_Array_enum().

◆ F5Awrite_integer()

int F5Awrite_integer ( hid_t loc_id,
const char * name,
int data )

Save an integer to the file as an attribute, overwriting any existing value.

Returns
non-zero in case of error

Definition at line 494 of file F5A.c.

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}

References F5Aopen_name(), F5printf, and name.

◆ F5Lget_dimensions()

int F5Lget_dimensions ( hid_t Field_id,
hsize_t * dims )

Definition at line 262 of file F5A.c.

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}
#define FIBER_FIELD_DATASPACE_DIMENSIONS_ATTRIBUTE
Definition F5defs.h:153
int F5LAget_dimensions(hid_t Field_id, const char *aname, hsize_t dims[FIBER_MAX_RANK])
Definition F5A.c:233

References F5LAget_dimensions(), and FIBER_FIELD_DATASPACE_DIMENSIONS_ATTRIBUTE.