| 1 |
/* |
| 2 |
* Copyright(C) 1999-2020 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, EX_WARN, etc |
| 11 |
#include <stdbool.h> |
| 12 |
|
| 13 |
/*! |
| 14 |
|
| 15 |
The function ex_get_prop() reads an integer object property value |
| 16 |
stored for a single element block, node set, or side set. |
| 17 |
|
| 18 |
\return In case of an error, ex_get_prop() returns a negative number; a |
| 19 |
warning will return a positive number. Possible causes of errors |
| 20 |
include: |
| 21 |
- data file not properly opened with call to ex_create() or ex_open() |
| 22 |
- invalid object type specified. |
| 23 |
- a warning value is returned if a property with the specified name is not |
| 24 |
found. |
| 25 |
|
| 26 |
\param[in] exoid exodus file ID returned from a previous call to |
| 27 |
ex_create() or ex_open(). |
| 28 |
\param[in] obj_type Type of object; use one of the options in the table below. |
| 29 |
\param[in] obj_id The element block, node set, or side set ID. |
| 30 |
\param[in] prop_name The name of the property (maximum length is \p |
| 31 |
MAX_STR_LENGTH ) for |
| 32 |
which the value is desired. |
| 33 |
\param[out] value Returned value of the property. |
| 34 |
|
| 35 |
| #ex_entity_type | description | |
| 36 |
| -------------- | ------------------------- | |
| 37 |
| #EX_NODE_SET | Node Set entity type | |
| 38 |
| #EX_EDGE_BLOCK | Edge Block entity type | |
| 39 |
| #EX_EDGE_SET | Edge Set entity type | |
| 40 |
| #EX_FACE_BLOCK | Face Block entity type | |
| 41 |
| #EX_FACE_SET | Face Set entity type | |
| 42 |
| #EX_ELEM_BLOCK | Element Block entity type| |
| 43 |
| #EX_ELEM_SET | Element Set entity type | |
| 44 |
| #EX_SIDE_SET | Side Set entity type | |
| 45 |
| #EX_ELEM_MAP | Element Map entity type | |
| 46 |
| #EX_NODE_MAP | Node Map entity type | |
| 47 |
| #EX_EDGE_MAP | Edge Map entity type | |
| 48 |
| #EX_FACE_MAP | Face Map entity type | |
| 49 |
|
| 50 |
For an example of code to read an object property, refer to the |
| 51 |
description for ex_get_prop_names(). |
| 52 |
|
| 53 |
*/ |
| 54 |
|
| 55 |
int ex_get_prop(int exoid, ex_entity_type obj_type, ex_entity_id obj_id, const char *prop_name, |
| 56 |
void_int *value) |
| 57 |
{ |
| 58 |
int status; |
| 59 |
int num_props, i, propid; |
| 60 |
int found = false; |
| 61 |
size_t start[1]; |
| 62 |
char *name; |
| 63 |
char tmpstr[MAX_STR_LENGTH + 1]; |
| 64 |
|
| 65 |
char errmsg[MAX_ERR_LENGTH]; |
| 66 |
|
| 67 |
EX_FUNC_ENTER(); |
| 68 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 69 |
EX_FUNC_LEAVE(EX_FATAL); |
| 70 |
} |
| 71 |
|
| 72 |
/* open appropriate variable, depending on obj_type and prop_name */ |
| 73 |
num_props = ex_get_num_props(exoid, obj_type); |
| 74 |
|
| 75 |
for (i = 1; i <= num_props; i++) { |
| 76 |
switch (obj_type) { |
| 77 |
case EX_ELEM_BLOCK: name = VAR_EB_PROP(i); break; |
| 78 |
case EX_EDGE_BLOCK: name = VAR_ED_PROP(i); break; |
| 79 |
case EX_FACE_BLOCK: name = VAR_FA_PROP(i); break; |
| 80 |
case EX_NODE_SET: name = VAR_NS_PROP(i); break; |
| 81 |
case EX_EDGE_SET: name = VAR_ES_PROP(i); break; |
| 82 |
case EX_FACE_SET: name = VAR_FS_PROP(i); break; |
| 83 |
case EX_ELEM_SET: name = VAR_ELS_PROP(i); break; |
| 84 |
case EX_SIDE_SET: name = VAR_SS_PROP(i); break; |
| 85 |
case EX_ELEM_MAP: name = VAR_EM_PROP(i); break; |
| 86 |
case EX_FACE_MAP: name = VAR_FAM_PROP(i); break; |
| 87 |
case EX_EDGE_MAP: name = VAR_EDM_PROP(i); break; |
| 88 |
case EX_NODE_MAP: name = VAR_NM_PROP(i); break; |
| 89 |
default: |
| 90 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: object type %d not supported; file id %d", obj_type, |
| 91 |
exoid); |
| 92 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 93 |
EX_FUNC_LEAVE(EX_FATAL); |
| 94 |
} |
| 95 |
|
| 96 |
if ((status = nc_inq_varid(exoid, name, &propid)) != NC_NOERR) { |
| 97 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate property array %s in file id %d", |
| 98 |
name, exoid); |
| 99 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 100 |
EX_FUNC_LEAVE(EX_FATAL); |
| 101 |
} |
| 102 |
|
| 103 |
/* compare stored attribute name with passed property name */ |
| 104 |
memset(tmpstr, 0, MAX_STR_LENGTH + 1); |
| 105 |
if ((status = nc_get_att_text(exoid, propid, ATT_PROP_NAME, tmpstr)) != NC_NOERR) { |
| 106 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get property name in file id %d", exoid); |
| 107 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 108 |
EX_FUNC_LEAVE(EX_FATAL); |
| 109 |
} |
| 110 |
|
| 111 |
if (strcmp(tmpstr, prop_name) == 0) { |
| 112 |
found = true; |
| 113 |
break; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/* if property is not found, return warning */ |
| 118 |
if (!found) { |
| 119 |
snprintf(errmsg, MAX_ERR_LENGTH, "Warning: %s property %s not defined in file id %d", |
| 120 |
ex_name_of_object(obj_type), prop_name, exoid); |
| 121 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 122 |
EX_FUNC_LEAVE(EX_WARN); |
| 123 |
} |
| 124 |
|
| 125 |
/* find index into property array using obj_id; read value from property */ |
| 126 |
/* array at proper index; exi_id_lkup returns an index that is 1-based, */ |
| 127 |
/* but netcdf expects 0-based arrays so subtract 1 */ |
| 128 |
status = exi_id_lkup(exoid, obj_type, obj_id); |
| 129 |
if (status > 0) { |
| 130 |
start[0] = status - 1; |
| 131 |
} |
| 132 |
else { |
| 133 |
ex_get_err(NULL, NULL, &status); |
| 134 |
|
| 135 |
if (status != 0) { |
| 136 |
if (status == EX_NULLENTITY) { |
| 137 |
snprintf(errmsg, MAX_ERR_LENGTH, "Warning: %s id %" PRId64 " is NULL in file id %d", |
| 138 |
ex_name_of_object(obj_type), obj_id, exoid); |
| 139 |
ex_err_fn(exoid, __func__, errmsg, EX_NULLENTITY); |
| 140 |
EX_FUNC_LEAVE(EX_WARN); |
| 141 |
} |
| 142 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 143 |
"ERROR: failed to locate id %" PRId64 " in %s property array in file id %d", obj_id, |
| 144 |
ex_name_of_object(obj_type), exoid); |
| 145 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 146 |
EX_FUNC_LEAVE(EX_FATAL); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if (ex_int64_status(exoid) & EX_IDS_INT64_API) { |
| 151 |
long long l_val; |
| 152 |
status = nc_get_var1_longlong(exoid, propid, start, &l_val); |
| 153 |
if (status == NC_NOERR) { |
| 154 |
int64_t *val = (int64_t *)value; |
| 155 |
*val = l_val; |
| 156 |
} |
| 157 |
} |
| 158 |
else { |
| 159 |
int i_val; |
| 160 |
status = nc_get_var1_int(exoid, propid, start, &i_val); |
| 161 |
if (status == NC_NOERR) { |
| 162 |
int *val = (int *)value; |
| 163 |
*val = i_val; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
if (status != NC_NOERR) { |
| 168 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 169 |
"ERROR: failed to read value in %s property array in file id %d", |
| 170 |
ex_name_of_object(obj_type), exoid); |
| 171 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 172 |
EX_FUNC_LEAVE(EX_FATAL); |
| 173 |
} |
| 174 |
|
| 175 |
EX_FUNC_LEAVE(EX_NOERR); |
| 176 |
} |