| 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 |
|
| 12 |
/* An entity attribute is similar to an IOSS property consisting of |
| 13 |
a name, a type, and a value or values. It is not a value per entity |
| 14 |
in the entity, but a value for the entity itself. For now, the types |
| 15 |
will be limited to text, integer, and double to provide capability |
| 16 |
without the complexity of supporting the many types available in |
| 17 |
NetCDF-4 including user-defined types. Note that an attribute can |
| 18 |
have multiple values, for example if the attribute is a range, it |
| 19 |
could have the value {1.0, 100.0} |
| 20 |
|
| 21 |
NOTE: Need a better name or way of distinguishing from the |
| 22 |
attributes which are currently supported in Exodus. |
| 23 |
*/ |
| 24 |
|
| 25 |
/* define and output a double attribute */ |
| 26 |
int ex_put_double_attribute(int exoid, ex_entity_type obj_type, ex_entity_id id, |
| 27 |
const char *atr_name, int num_values, const double *values) |
| 28 |
{ |
| 29 |
int status; |
| 30 |
char errmsg[MAX_ERR_LENGTH]; |
| 31 |
int varid; |
| 32 |
|
| 33 |
EX_FUNC_ENTER(); |
| 34 |
varid = exi_get_varid(exoid, obj_type, id); |
| 35 |
if (varid <= 0 && obj_type != EX_GLOBAL) { |
| 36 |
/* Error message handled in exi_get_varid */ |
| 37 |
EX_FUNC_LEAVE(varid); |
| 38 |
} |
| 39 |
|
| 40 |
/* put netcdf file into define mode */ |
| 41 |
if ((status = exi_redef(exoid, __func__)) != NC_NOERR) { |
| 42 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", exoid); |
| 43 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 44 |
EX_FUNC_LEAVE(EX_FATAL); |
| 45 |
} |
| 46 |
|
| 47 |
if ((status = nc_put_att_double(exoid, varid, atr_name, NC_DOUBLE, num_values, values)) != |
| 48 |
NC_NOERR) { |
| 49 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 50 |
"ERROR: failed to store double attribute %s on %s with id %" PRId64 " in file id %d", |
| 51 |
atr_name, ex_name_of_object(obj_type), id, exoid); |
| 52 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 53 |
goto error_ret; |
| 54 |
} |
| 55 |
|
| 56 |
/* leave define mode */ |
| 57 |
if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) { |
| 58 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode"); |
| 59 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 60 |
EX_FUNC_LEAVE(EX_FATAL); |
| 61 |
} |
| 62 |
|
| 63 |
EX_FUNC_LEAVE(EX_NOERR); |
| 64 |
|
| 65 |
/* Fatal error: exit definition mode and return */ |
| 66 |
error_ret: |
| 67 |
exi_leavedef(exoid, __func__); |
| 68 |
EX_FUNC_LEAVE(EX_FATAL); |
| 69 |
} |
| 70 |
|
| 71 |
/* define and output an integer attribute */ |
| 72 |
int ex_put_integer_attribute(int exoid, ex_entity_type obj_type, ex_entity_id id, |
| 73 |
const char *atr_name, int num_values, const void_int *values) |
| 74 |
{ |
| 75 |
int status; |
| 76 |
char errmsg[MAX_ERR_LENGTH]; |
| 77 |
int varid; |
| 78 |
|
| 79 |
EX_FUNC_ENTER(); |
| 80 |
varid = exi_get_varid(exoid, obj_type, id); |
| 81 |
if (varid <= 0 && obj_type != EX_GLOBAL) { |
| 82 |
/* Error message handled in exi_get_varid */ |
| 83 |
EX_FUNC_LEAVE(varid); |
| 84 |
} |
| 85 |
|
| 86 |
/* put netcdf file into define mode */ |
| 87 |
if ((status = exi_redef(exoid, __func__)) != NC_NOERR) { |
| 88 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", exoid); |
| 89 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 90 |
EX_FUNC_LEAVE(EX_FATAL); |
| 91 |
} |
| 92 |
|
| 93 |
if (ex_int64_status(exoid) & EX_IDS_INT64_DB) { |
| 94 |
status = nc_put_att_longlong(exoid, varid, atr_name, NC_INT64, num_values, values); |
| 95 |
} |
| 96 |
else { |
| 97 |
status = nc_put_att_int(exoid, varid, atr_name, NC_INT, num_values, values); |
| 98 |
} |
| 99 |
|
| 100 |
if (status != NC_NOERR) { |
| 101 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 102 |
"ERROR: failed to store integer attribute %s on %s with id %" PRId64 " in file id %d", |
| 103 |
atr_name, ex_name_of_object(obj_type), id, exoid); |
| 104 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 105 |
goto error_ret; |
| 106 |
} |
| 107 |
|
| 108 |
/* leave define mode */ |
| 109 |
if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) { |
| 110 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode"); |
| 111 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 112 |
EX_FUNC_LEAVE(EX_FATAL); |
| 113 |
} |
| 114 |
|
| 115 |
EX_FUNC_LEAVE(EX_NOERR); |
| 116 |
|
| 117 |
/* Fatal error: exit definition mode and return */ |
| 118 |
error_ret: |
| 119 |
exi_leavedef(exoid, __func__); |
| 120 |
EX_FUNC_LEAVE(EX_FATAL); |
| 121 |
} |
| 122 |
|
| 123 |
/* define and output a text attribute.... */ |
| 124 |
int ex_put_text_attribute(int exoid, ex_entity_type obj_type, ex_entity_id id, const char *atr_name, |
| 125 |
const char *value) |
| 126 |
{ |
| 127 |
int status; |
| 128 |
char errmsg[MAX_ERR_LENGTH]; |
| 129 |
int varid; |
| 130 |
|
| 131 |
EX_FUNC_ENTER(); |
| 132 |
|
| 133 |
varid = exi_get_varid(exoid, obj_type, id); |
| 134 |
if (varid <= 0 && obj_type != EX_GLOBAL) { |
| 135 |
/* Error message handled in exi_get_varid */ |
| 136 |
EX_FUNC_LEAVE(varid); |
| 137 |
} |
| 138 |
|
| 139 |
/* put netcdf file into define mode */ |
| 140 |
if ((status = exi_redef(exoid, __func__)) != NC_NOERR) { |
| 141 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", exoid); |
| 142 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 143 |
EX_FUNC_LEAVE(EX_FATAL); |
| 144 |
} |
| 145 |
|
| 146 |
if ((status = nc_put_att_text(exoid, varid, atr_name, strlen(value) + 1, value)) != NC_NOERR) { |
| 147 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 148 |
"ERROR: failed to store text attribute %s on %s with id %" PRId64 " in file id %d", |
| 149 |
atr_name, ex_name_of_object(obj_type), id, exoid); |
| 150 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 151 |
goto error_ret; |
| 152 |
} |
| 153 |
|
| 154 |
/* leave define mode */ |
| 155 |
if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) { |
| 156 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode"); |
| 157 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 158 |
EX_FUNC_LEAVE(EX_FATAL); |
| 159 |
} |
| 160 |
|
| 161 |
EX_FUNC_LEAVE(EX_NOERR); |
| 162 |
|
| 163 |
/* Fatal error: exit definition mode and return */ |
| 164 |
error_ret: |
| 165 |
exi_leavedef(exoid, __func__); |
| 166 |
EX_FUNC_LEAVE(EX_FATAL); |
| 167 |
} |
| 168 |
|
| 169 |
int ex_put_attribute(int exoid, ex_attribute attribute) |
| 170 |
{ |
| 171 |
char errmsg[MAX_ERR_LENGTH]; |
| 172 |
|
| 173 |
switch (attribute.type) { |
| 174 |
case EX_INTEGER: |
| 175 |
return ex_put_integer_attribute(exoid, attribute.entity_type, attribute.entity_id, |
| 176 |
attribute.name, attribute.value_count, attribute.values); |
| 177 |
case EX_DOUBLE: |
| 178 |
return ex_put_double_attribute(exoid, attribute.entity_type, attribute.entity_id, |
| 179 |
attribute.name, attribute.value_count, attribute.values); |
| 180 |
case EX_CHAR: |
| 181 |
return ex_put_text_attribute(exoid, attribute.entity_type, attribute.entity_id, attribute.name, |
| 182 |
attribute.values); |
| 183 |
default: |
| 184 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 185 |
"ERROR: Unrecognized attribute type %d for attribute %s on %s with id %" PRId64 |
| 186 |
" in file id %d", |
| 187 |
attribute.type, attribute.name, ex_name_of_object(attribute.entity_type), |
| 188 |
attribute.entity_id, exoid); |
| 189 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 190 |
return EX_FATAL; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/*! Define and output the specified attributes. */ |
| 195 |
int ex_put_attributes(int exoid, size_t attr_count, const ex_attribute *attr) |
| 196 |
{ |
| 197 |
for (size_t i = 0; i < attr_count; i++) { |
| 198 |
int status = ex_put_attribute(exoid, attr[i]); |
| 199 |
if (status != EX_NOERR) { |
| 200 |
return status; |
| 201 |
} |
| 202 |
} |
| 203 |
return EX_NOERR; |
| 204 |
} |