| 1 |
/* |
| 2 |
* Copyright(C) 1999-2022, 2024 National Technology & Engineering Solutions |
| 3 |
* of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 4 |
* NTESS, the U.S. Government retains certain rights in this software. |
| 5 |
* |
| 6 |
* See packages/seacas/LICENSE for details |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "exodusII.h" // for ex_err, etc |
| 10 |
#include "exodusII_int.h" // for EX_FATAL, etc |
| 11 |
#include <stdbool.h> |
| 12 |
|
| 13 |
/* An entity attribute is similar to an IOSS property consisting of |
| 14 |
a name, a type, and a value or values. It is not a value per entity |
| 15 |
in the entity, but a value for the entity. For now, the types |
| 16 |
will be limited to text, integer, and double to provide capability |
| 17 |
without the complexity of supporting the many types available in |
| 18 |
NetCDF-4 including user-defined types. Note that an attribute can |
| 19 |
have multiple values, for example if the attribute is a range, it |
| 20 |
could have the value {1.0, 100.0} |
| 21 |
|
| 22 |
NOTE: Need a better name or way of distinguishing from the |
| 23 |
attributes which are currently supported in Exodus. |
| 24 |
*/ |
| 25 |
|
| 26 |
static bool exi_is_internal_attribute(const char *name, ex_entity_type obj_type) |
| 27 |
{ |
| 28 |
if (name[0] == '_') { |
| 29 |
return true; |
| 30 |
} |
| 31 |
else if ((strcmp(name, ATT_NAME_ELB) == 0) || (strcmp(name, "entity_type1") == 0) || |
| 32 |
(strcmp(name, "entity_type2") == 0)) { |
| 33 |
return true; |
| 34 |
} |
| 35 |
else if (obj_type == EX_GLOBAL && |
| 36 |
((strcmp(name, ATT_API_VERSION) == 0) || (strcmp(name, ATT_API_VERSION_BLANK) == 0) || |
| 37 |
(strcmp(name, ATT_VERSION) == 0) || (strcmp(name, ATT_FLT_WORDSIZE) == 0) || |
| 38 |
(strcmp(name, ATT_FLT_WORDSIZE_BLANK) == 0) || (strcmp(name, ATT_FILESIZE) == 0) || |
| 39 |
(strcmp(name, ATT_MAX_NAME_LENGTH) == 0) || (strcmp(name, ATT_INT64_STATUS) == 0) || |
| 40 |
(strcmp(name, ATT_TITLE) == 0) || (strcmp(name, ATT_NEM_FILE_VERSION) == 0) || |
| 41 |
(strcmp(name, ATT_NEM_API_VERSION) == 0) || (strcmp(name, ATT_PROCESSOR_INFO) == 0) || |
| 42 |
(strcmp(name, ATT_LAST_WRITTEN_TIME) == 0))) { |
| 43 |
return true; |
| 44 |
} |
| 45 |
else if (strncmp(name, "Field@", 6) == 0) { |
| 46 |
return true; |
| 47 |
} |
| 48 |
else if (strncmp(name, "Basis@", 6) == 0) { |
| 49 |
return true; |
| 50 |
} |
| 51 |
else if (strncmp(name, "Quad@", 5) == 0) { |
| 52 |
return true; |
| 53 |
} |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
static int exi_get_attribute_count(int exoid, ex_entity_type obj_type, ex_entity_id id, int *varid) |
| 58 |
{ |
| 59 |
int att_count = 0; |
| 60 |
int status; |
| 61 |
|
| 62 |
if (obj_type == EX_GLOBAL) { |
| 63 |
*varid = NC_GLOBAL; |
| 64 |
|
| 65 |
if ((status = nc_inq(exoid, NULL, NULL, &att_count, NULL)) != NC_NOERR) { |
| 66 |
char errmsg[MAX_ERR_LENGTH]; |
| 67 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get GLOBAL attribute count in file id %d", |
| 68 |
exoid); |
| 69 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 70 |
return EX_FATAL; |
| 71 |
} |
| 72 |
} |
| 73 |
else { |
| 74 |
*varid = exi_get_varid(exoid, obj_type, id); |
| 75 |
if (*varid <= 0) { |
| 76 |
/* Error message handled in exi_get_varid */ |
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
if ((status = nc_inq_var(exoid, *varid, NULL, NULL, NULL, NULL, &att_count)) != NC_NOERR) { |
| 81 |
char errmsg[MAX_ERR_LENGTH]; |
| 82 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 83 |
"ERROR: failed to get attribute count on %s with id %" PRId64 " in file id %d", |
| 84 |
ex_name_of_object(obj_type), id, exoid); |
| 85 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 86 |
return EX_FATAL; |
| 87 |
} |
| 88 |
} |
| 89 |
return att_count; |
| 90 |
} |
| 91 |
|
| 92 |
/*! Get number of attributes defined on the specified entity |
| 93 |
type/entity id (EX_ASSEMBLY, 100). |
| 94 |
|
| 95 |
Filters out "internal" or "special" attributes defined by the |
| 96 |
NetCDF library or used by the exodus library internally. |
| 97 |
*/ |
| 98 |
int ex_get_attribute_count(int exoid, ex_entity_type obj_type, ex_entity_id id) |
| 99 |
{ |
| 100 |
EX_FUNC_ENTER(); |
| 101 |
|
| 102 |
int varid; |
| 103 |
int att_count = exi_get_attribute_count(exoid, obj_type, id, &varid); |
| 104 |
if (att_count < 0) { |
| 105 |
char errmsg[MAX_ERR_LENGTH]; |
| 106 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 107 |
"ERROR: Negative attribute count (%d) on %s with id %" PRId64 " in file id %d", |
| 108 |
att_count, ex_name_of_object(obj_type), id, exoid); |
| 109 |
ex_err_fn(exoid, __func__, errmsg, EX_INTERNAL); |
| 110 |
EX_FUNC_LEAVE(EX_FATAL); |
| 111 |
} |
| 112 |
|
| 113 |
/* Get names of each attribute and see if it is an 'internal' name */ |
| 114 |
int count = att_count; |
| 115 |
for (int i = 0; i < count; i++) { |
| 116 |
char name[NC_MAX_NAME + 1]; |
| 117 |
int status; |
| 118 |
if ((status = nc_inq_attname(exoid, varid, i, name)) != NC_NOERR) { |
| 119 |
char errmsg[MAX_ERR_LENGTH]; |
| 120 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 121 |
"ERROR: failed to get attribute named %s on %s with id %" PRId64 " in file id %d", |
| 122 |
name, ex_name_of_object(obj_type), id, exoid); |
| 123 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 124 |
EX_FUNC_LEAVE(EX_FATAL); |
| 125 |
} |
| 126 |
if (exi_is_internal_attribute(name, obj_type)) { |
| 127 |
att_count--; |
| 128 |
} |
| 129 |
} |
| 130 |
EX_FUNC_LEAVE(att_count); |
| 131 |
} |
| 132 |
|
| 133 |
/*! Get the parameters for all attributes defined on the specified |
| 134 |
entity type/entity id (ASSEMBLY 100). |
| 135 |
|
| 136 |
Filters out "internal" or "special" attributes defined by the |
| 137 |
NetCDF library or used by the exodus library internally. |
| 138 |
|
| 139 |
Returns the name, type, value_count, and optionally values (if not |
| 140 |
null) for all attributes on this entity. The `attr` argument must |
| 141 |
have enough space to hold all attributes defined on the specified |
| 142 |
entity. The attribute count can be determined from |
| 143 |
`ex_get_attribute_count()`. The `entity_type` and `entity_id` fields |
| 144 |
on `attr` will be populated with the `obj_type` and `id` function |
| 145 |
parameters. |
| 146 |
*/ |
| 147 |
int ex_get_attribute_param(int exoid, ex_entity_type obj_type, ex_entity_id id, ex_attribute *attr) |
| 148 |
{ |
| 149 |
char errmsg[MAX_ERR_LENGTH]; |
| 150 |
int varid; |
| 151 |
int att_count, count; |
| 152 |
|
| 153 |
EX_FUNC_ENTER(); |
| 154 |
|
| 155 |
att_count = exi_get_attribute_count(exoid, obj_type, id, &varid); |
| 156 |
if (att_count < 0) { |
| 157 |
EX_FUNC_LEAVE(EX_FATAL); |
| 158 |
} |
| 159 |
|
| 160 |
/* Get names of each attribute and see if it is an 'internal' name; if not, copy to `attr` and |
| 161 |
get other parameters of the attribute |
| 162 |
*/ |
| 163 |
count = 0; |
| 164 |
for (int i = 0; i < att_count; i++) { |
| 165 |
char name[NC_MAX_NAME + 1]; |
| 166 |
int status; |
| 167 |
if ((status = nc_inq_attname(exoid, varid, i, name)) != NC_NOERR) { |
| 168 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 169 |
"ERROR: failed to get attribute named %s on %s with id %" PRId64 " in file id %d", |
| 170 |
name, ex_name_of_object(obj_type), id, exoid); |
| 171 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 172 |
EX_FUNC_LEAVE(EX_FATAL); |
| 173 |
} |
| 174 |
if (!exi_is_internal_attribute(name, obj_type)) { |
| 175 |
nc_type type; |
| 176 |
size_t val_count; |
| 177 |
|
| 178 |
if ((status = nc_inq_att(exoid, varid, name, &type, &val_count)) != NC_NOERR) { |
| 179 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 180 |
"ERROR: failed to get parameters for attribute named %s on %s with id %" PRId64 |
| 181 |
" in file id %d", |
| 182 |
name, ex_name_of_object(obj_type), id, exoid); |
| 183 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 184 |
EX_FUNC_LEAVE(EX_FATAL); |
| 185 |
} |
| 186 |
ex_copy_string(attr[count].name, name, EX_MAX_NAME); |
| 187 |
attr[count].entity_type = obj_type; |
| 188 |
attr[count].entity_id = id; |
| 189 |
attr[count].value_count = val_count; |
| 190 |
attr[count].type = type; |
| 191 |
count++; |
| 192 |
} |
| 193 |
} |
| 194 |
EX_FUNC_LEAVE(EX_NOERR); |
| 195 |
} |
| 196 |
|
| 197 |
/*! Get the values for the specified attribute. */ |
| 198 |
int ex_get_attribute(int exoid, ex_attribute *attr) |
| 199 |
{ |
| 200 |
EX_FUNC_ENTER(); |
| 201 |
int varid; |
| 202 |
if (attr->entity_type == EX_GLOBAL) { |
| 203 |
varid = NC_GLOBAL; |
| 204 |
} |
| 205 |
else { |
| 206 |
varid = exi_get_varid(exoid, attr->entity_type, attr->entity_id); |
| 207 |
if (varid <= 0) { |
| 208 |
/* Error message handled in exi_get_varid */ |
| 209 |
EX_FUNC_LEAVE(varid); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
/* If attr->values is NULL, then this routine should allocate memory |
| 214 |
(which needs to be freed by the client. |
| 215 |
*/ |
| 216 |
if (attr->values == NULL) { |
| 217 |
if (attr->type == EX_INTEGER) { |
| 218 |
attr->values = calloc(attr->value_count, sizeof(int)); |
| 219 |
} |
| 220 |
else if (attr->type == EX_DOUBLE) { |
| 221 |
attr->values = calloc(attr->value_count, sizeof(double)); |
| 222 |
} |
| 223 |
else if (attr->type == EX_CHAR) { |
| 224 |
attr->values = calloc(attr->value_count + 1, sizeof(char)); |
| 225 |
} |
| 226 |
if (attr->values == NULL) { |
| 227 |
char errmsg[MAX_ERR_LENGTH]; |
| 228 |
snprintf( |
| 229 |
errmsg, MAX_ERR_LENGTH, |
| 230 |
"ERROR: failed allocate memory to store values for attribute %s on %s with id %" PRId64 |
| 231 |
" in file id %d", |
| 232 |
attr->name, ex_name_of_object(attr->entity_type), attr->entity_id, exoid); |
| 233 |
ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL); |
| 234 |
EX_FUNC_LEAVE(EX_FATAL); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
int status; |
| 239 |
if ((status = nc_get_att(exoid, varid, attr->name, attr->values)) != NC_NOERR) { |
| 240 |
char errmsg[MAX_ERR_LENGTH]; |
| 241 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 242 |
"ERROR: failed to read attribute %s on %s with id %" PRId64 " in file id %d", |
| 243 |
attr->name, ex_name_of_object(attr->entity_type), attr->entity_id, exoid); |
| 244 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 245 |
EX_FUNC_LEAVE(EX_FATAL); |
| 246 |
} |
| 247 |
|
| 248 |
EX_FUNC_LEAVE(EX_NOERR); |
| 249 |
} |
| 250 |
|
| 251 |
/*! Get the values for all of the specified attributes. */ |
| 252 |
int ex_get_attributes(int exoid, size_t attr_count, ex_attribute *attr) |
| 253 |
{ |
| 254 |
for (size_t i = 0; i < attr_count; i++) { |
| 255 |
int status = ex_get_attribute(exoid, &(attr[i])); |
| 256 |
if (status != EX_NOERR) { |
| 257 |
return status; |
| 258 |
} |
| 259 |
} |
| 260 |
return EX_NOERR; |
| 261 |
} |