| 1 |
/* |
| 2 |
* Copyright(C) 1999-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 |
* |
| 10 |
* expvp - ex_put_all_var_param_ext |
| 11 |
* |
| 12 |
* entry conditions - |
| 13 |
* input parameters: |
| 14 |
* int exoid exodus file id |
| 15 |
* const ex_var_params* vp pointer to variable parameter info |
| 16 |
* |
| 17 |
* exit conditions - |
| 18 |
* |
| 19 |
*****************************************************************************/ |
| 20 |
|
| 21 |
#include "exodusII.h" // for ex_err, etc |
| 22 |
#include "exodusII_int.h" // for exi_get_dimension, etc |
| 23 |
|
| 24 |
static int define_dimension(int exoid, const char *DIMENSION, int count, const char *label, |
| 25 |
int *dimid); |
| 26 |
static int define_variable_name_variable(int exoid, const char *VARIABLE, int dimension, |
| 27 |
const char *label); |
| 28 |
static int *get_status_array(int exoid, int var_count, const char *VARIABLE, const char *label); |
| 29 |
static int put_truth_table(int exoid, int varid, int *table, const char *label); |
| 30 |
static int define_truth_table(ex_entity_type obj_type, int exoid, int num_ent, int num_var, |
| 31 |
int *var_tab, int *status_tab, void_int *ids, const char *label); |
| 32 |
static int exi_define_vars(int exoid, ex_entity_type obj_type, const char *entity_name, |
| 33 |
const char *entity_blk_name, int numvar, const char *DNAME, int dimid1, |
| 34 |
int dimid2, int DVAL, void_int **entity_ids, const char *VNOV, |
| 35 |
const char *VTV, int **status_var, int *truth_table, |
| 36 |
int *truth_table_var); |
| 37 |
|
| 38 |
#define EX_GET_IDS_STATUS(TNAME, NUMVAR, DNAME, DID, DVAL, VIDS, EIDS, VSTAT, VSTATVAL) \ |
| 39 |
if (NUMVAR > 0) { \ |
| 40 |
status = exi_get_dimension(exoid, DNAME, TNAME "s", &DVAL, &DID, __func__); \ |
| 41 |
if (status != NC_NOERR) \ |
| 42 |
goto error_ret; \ |
| 43 |
\ |
| 44 |
/* get element block IDs */ \ |
| 45 |
if (!(VIDS = malloc(sizeof(int64_t) * DVAL))) { \ |
| 46 |
snprintf(errmsg, MAX_ERR_LENGTH, \ |
| 47 |
"ERROR: failed to allocate memory for " TNAME " id array for file id %d", exoid); \ |
| 48 |
ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL); \ |
| 49 |
goto error_ret; \ |
| 50 |
} \ |
| 51 |
ex_get_ids(exoid, EIDS, VIDS); \ |
| 52 |
\ |
| 53 |
/* Get element block status array for later use (allocates memory) */ \ |
| 54 |
VSTATVAL = get_status_array(exoid, DVAL, VSTAT, TNAME); \ |
| 55 |
if (VSTATVAL == NULL) { \ |
| 56 |
goto error_ret; \ |
| 57 |
} \ |
| 58 |
} |
| 59 |
|
| 60 |
/*! |
| 61 |
* writes the number of global, nodal, element, nodeset, and sideset variables |
| 62 |
* that will be written to the database |
| 63 |
* \param exoid exodus file id |
| 64 |
* \param *vp pointer to variable parameter info |
| 65 |
*/ |
| 66 |
|
| 67 |
int ex_put_all_var_param_ext(int exoid, const ex_var_params *vp) |
| 68 |
{ |
| 69 |
int in_define = 0; |
| 70 |
int status = 0; |
| 71 |
int temp = 0; |
| 72 |
int time_dim = 0, num_nod_dim = 0, dimid = 0; |
| 73 |
size_t num_elem_blk = 0, num_edge_blk = 0, num_face_blk = 0; |
| 74 |
size_t num_nset = 0, num_eset = 0, num_fset = 0, num_sset = 0, num_elset = 0; |
| 75 |
int numelblkdim = 0, numelvardim = 0, numedvardim = 0, numedblkdim = 0, numfavardim = 0, |
| 76 |
numfablkdim = 0, numnsetdim = 0, nsetvardim = 0, numesetdim = 0, esetvardim = 0, |
| 77 |
numfsetdim = 0, fsetvardim = 0, numssetdim = 0, ssetvardim = 0, numelsetdim = 0, |
| 78 |
elsetvardim = 0; |
| 79 |
int i = 0; |
| 80 |
|
| 81 |
int edblk_varid = 0, fablk_varid = 0, eblk_varid = 0, nset_varid = 0, eset_varid = 0, |
| 82 |
fset_varid = 0, sset_varid = 0, elset_varid = 0, varid = 0; |
| 83 |
|
| 84 |
void_int *eblk_ids = NULL; |
| 85 |
void_int *edblk_ids = NULL; |
| 86 |
void_int *fablk_ids = NULL; |
| 87 |
void_int *nset_ids = NULL; |
| 88 |
void_int *eset_ids = NULL; |
| 89 |
void_int *fset_ids = NULL; |
| 90 |
void_int *sset_ids = NULL; |
| 91 |
void_int *elset_ids = NULL; |
| 92 |
|
| 93 |
int *eblk_stat = NULL; |
| 94 |
int *edblk_stat = NULL; |
| 95 |
int *fablk_stat = NULL; |
| 96 |
int *nset_stat = NULL; |
| 97 |
int *eset_stat = NULL; |
| 98 |
int *fset_stat = NULL; |
| 99 |
int *sset_stat = NULL; |
| 100 |
int *elset_stat = NULL; |
| 101 |
|
| 102 |
int dims[3]; |
| 103 |
char errmsg[MAX_ERR_LENGTH]; |
| 104 |
|
| 105 |
EX_FUNC_ENTER(); |
| 106 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 107 |
EX_FUNC_LEAVE(EX_FATAL); |
| 108 |
} |
| 109 |
|
| 110 |
/* inquire previously defined dimensions */ |
| 111 |
|
| 112 |
if ((status = nc_inq_dimid(exoid, DIM_TIME, &time_dim)) != NC_NOERR) { |
| 113 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate time dimension in file id %d", exoid); |
| 114 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 115 |
goto error_ret; |
| 116 |
} |
| 117 |
|
| 118 |
if (nc_inq_dimid(exoid, DIM_NUM_NODES, &num_nod_dim) != NC_NOERR) { |
| 119 |
num_nod_dim = -1; /* There is probably no nodes on this file */ |
| 120 |
} |
| 121 |
|
| 122 |
/* Check this now so we can use it later without checking for errors */ |
| 123 |
if ((status = nc_inq_dimid(exoid, DIM_STR_NAME, &temp)) != NC_NOERR) { |
| 124 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get string length in file id %d", exoid); |
| 125 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 126 |
goto error_ret; |
| 127 |
} |
| 128 |
|
| 129 |
EX_GET_IDS_STATUS("edge block", vp->num_edge, DIM_NUM_ED_BLK, numedblkdim, num_edge_blk, |
| 130 |
edblk_ids, EX_EDGE_BLOCK, VAR_STAT_ED_BLK, edblk_stat); |
| 131 |
EX_GET_IDS_STATUS("face block", vp->num_face, DIM_NUM_FA_BLK, numfablkdim, num_face_blk, |
| 132 |
fablk_ids, EX_FACE_BLOCK, VAR_STAT_FA_BLK, fablk_stat); |
| 133 |
EX_GET_IDS_STATUS("element block", vp->num_elem, DIM_NUM_EL_BLK, numelblkdim, num_elem_blk, |
| 134 |
eblk_ids, EX_ELEM_BLOCK, VAR_STAT_EL_BLK, eblk_stat); |
| 135 |
EX_GET_IDS_STATUS("node set", vp->num_nset, DIM_NUM_NS, numnsetdim, num_nset, nset_ids, |
| 136 |
EX_NODE_SET, VAR_NS_STAT, nset_stat); |
| 137 |
EX_GET_IDS_STATUS("edge set", vp->num_eset, DIM_NUM_ES, numesetdim, num_eset, eset_ids, |
| 138 |
EX_EDGE_SET, VAR_ES_STAT, eset_stat); |
| 139 |
EX_GET_IDS_STATUS("face set", vp->num_fset, DIM_NUM_FS, numfsetdim, num_fset, fset_ids, |
| 140 |
EX_FACE_SET, VAR_FS_STAT, fset_stat); |
| 141 |
EX_GET_IDS_STATUS("side set", vp->num_sset, DIM_NUM_SS, numssetdim, num_sset, sset_ids, |
| 142 |
EX_SIDE_SET, VAR_SS_STAT, sset_stat); |
| 143 |
EX_GET_IDS_STATUS("element set", vp->num_elset, DIM_NUM_ELS, numelsetdim, num_elset, elset_ids, |
| 144 |
EX_ELEM_SET, VAR_ELS_STAT, elset_stat); |
| 145 |
|
| 146 |
/* put file into define mode */ |
| 147 |
if ((status = exi_redef(exoid, __func__)) != NC_NOERR) { |
| 148 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", exoid); |
| 149 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 150 |
goto error_ret; |
| 151 |
} |
| 152 |
in_define = 1; |
| 153 |
|
| 154 |
/* define dimensions and variables */ |
| 155 |
if (vp->num_glob > 0) { |
| 156 |
if (define_dimension(exoid, DIM_NUM_GLO_VAR, vp->num_glob, "global", &dimid) != NC_NOERR) { |
| 157 |
goto error_ret; |
| 158 |
} |
| 159 |
|
| 160 |
dims[0] = time_dim; |
| 161 |
dims[1] = dimid; |
| 162 |
if ((status = nc_def_var(exoid, VAR_GLO_VAR, nc_flt_code(exoid), 2, dims, &varid)) != |
| 163 |
NC_NOERR) { |
| 164 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define global variables in file id %d", |
| 165 |
exoid); |
| 166 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 167 |
goto error_ret; /* exit define mode and return */ |
| 168 |
} |
| 169 |
exi_compress_variable(exoid, varid, 2); |
| 170 |
|
| 171 |
/* Now define global variable name variable */ |
| 172 |
if (define_variable_name_variable(exoid, VAR_NAME_GLO_VAR, dimid, "global") != NC_NOERR) { |
| 173 |
goto error_ret; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
if (vp->num_node > 0) { |
| 178 |
if (define_dimension(exoid, DIM_NUM_NOD_VAR, vp->num_node, "nodal", &dimid) != NC_NOERR) { |
| 179 |
goto error_ret; |
| 180 |
} |
| 181 |
|
| 182 |
if (num_nod_dim > 0) { |
| 183 |
for (i = 1; i <= vp->num_node; i++) { |
| 184 |
dims[0] = time_dim; |
| 185 |
dims[1] = num_nod_dim; |
| 186 |
if ((status = nc_def_var(exoid, VAR_NOD_VAR_NEW(i), nc_flt_code(exoid), 2, dims, &varid)) != |
| 187 |
NC_NOERR) { |
| 188 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 189 |
"ERROR: failed to define nodal variable %d in file id %d", i, exoid); |
| 190 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 191 |
goto error_ret; /* exit define mode and return */ |
| 192 |
} |
| 193 |
exi_compress_variable(exoid, varid, 2); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/* Now define nodal variable name variable */ |
| 198 |
if (define_variable_name_variable(exoid, VAR_NAME_NOD_VAR, dimid, "nodal") != NC_NOERR) { |
| 199 |
goto error_ret; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
if (exi_define_vars(exoid, EX_EDGE_BLOCK, "edge", "edge block", vp->num_edge, DIM_NUM_EDG_VAR, |
| 204 |
numedblkdim, numedvardim, num_edge_blk, &edblk_ids, VAR_NAME_EDG_VAR, |
| 205 |
VAR_EBLK_TAB, &edblk_stat, vp->edge_var_tab, &edblk_varid) != EX_NOERR) { |
| 206 |
goto error_ret; |
| 207 |
} |
| 208 |
|
| 209 |
if (exi_define_vars(exoid, EX_FACE_BLOCK, "face", "face block", vp->num_face, DIM_NUM_FAC_VAR, |
| 210 |
numfablkdim, numfavardim, num_face_blk, &fablk_ids, VAR_NAME_FAC_VAR, |
| 211 |
VAR_FBLK_TAB, &fablk_stat, vp->face_var_tab, &fablk_varid) != EX_NOERR) { |
| 212 |
goto error_ret; |
| 213 |
} |
| 214 |
|
| 215 |
if (exi_define_vars(exoid, EX_ELEM_BLOCK, "element", "element block", vp->num_elem, |
| 216 |
DIM_NUM_ELE_VAR, numelblkdim, numelvardim, num_elem_blk, &eblk_ids, |
| 217 |
VAR_NAME_ELE_VAR, VAR_ELEM_TAB, &eblk_stat, vp->elem_var_tab, |
| 218 |
&eblk_varid) != EX_NOERR) { |
| 219 |
goto error_ret; |
| 220 |
} |
| 221 |
|
| 222 |
if (exi_define_vars(exoid, EX_NODE_SET, "nodeset", "node set", vp->num_nset, DIM_NUM_NSET_VAR, |
| 223 |
numnsetdim, nsetvardim, num_nset, &nset_ids, VAR_NAME_NSET_VAR, VAR_NSET_TAB, |
| 224 |
&nset_stat, vp->nset_var_tab, &nset_varid) != EX_NOERR) { |
| 225 |
goto error_ret; |
| 226 |
} |
| 227 |
|
| 228 |
if (exi_define_vars(exoid, EX_EDGE_SET, "edgeset", "edge set", vp->num_eset, DIM_NUM_ESET_VAR, |
| 229 |
numesetdim, esetvardim, num_eset, &eset_ids, VAR_NAME_ESET_VAR, VAR_ESET_TAB, |
| 230 |
&eset_stat, vp->eset_var_tab, &eset_varid) != EX_NOERR) { |
| 231 |
goto error_ret; |
| 232 |
} |
| 233 |
|
| 234 |
if (exi_define_vars(exoid, EX_FACE_SET, "faceset", "face set", vp->num_fset, DIM_NUM_FSET_VAR, |
| 235 |
numfsetdim, fsetvardim, num_fset, &fset_ids, VAR_NAME_FSET_VAR, VAR_FSET_TAB, |
| 236 |
&fset_stat, vp->fset_var_tab, &fset_varid) != EX_NOERR) { |
| 237 |
goto error_ret; |
| 238 |
} |
| 239 |
|
| 240 |
if (exi_define_vars(exoid, EX_SIDE_SET, "sideset", "side set", vp->num_sset, DIM_NUM_SSET_VAR, |
| 241 |
numssetdim, ssetvardim, num_sset, &sset_ids, VAR_NAME_SSET_VAR, VAR_SSET_TAB, |
| 242 |
&sset_stat, vp->sset_var_tab, &sset_varid) != EX_NOERR) { |
| 243 |
goto error_ret; |
| 244 |
} |
| 245 |
|
| 246 |
if (exi_define_vars(exoid, EX_ELEM_SET, "elemset", "element set", vp->num_elset, |
| 247 |
DIM_NUM_ELSET_VAR, numelsetdim, elsetvardim, num_elset, &elset_ids, |
| 248 |
VAR_NAME_ELSET_VAR, VAR_ELSET_TAB, &elset_stat, vp->elset_var_tab, |
| 249 |
&elset_varid) != EX_NOERR) { |
| 250 |
goto error_ret; |
| 251 |
} |
| 252 |
|
| 253 |
/* leave define mode */ |
| 254 |
|
| 255 |
in_define = 0; |
| 256 |
if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) { |
| 257 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode"); |
| 258 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 259 |
goto error_ret; |
| 260 |
} |
| 261 |
|
| 262 |
/* write out the variable truth tables */ |
| 263 |
if (vp->num_edge > 0) { |
| 264 |
if (put_truth_table(exoid, edblk_varid, vp->edge_var_tab, "edge") != NC_NOERR) { |
| 265 |
goto error_ret; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
if (vp->num_face > 0) { |
| 270 |
if (put_truth_table(exoid, fablk_varid, vp->face_var_tab, "face") != NC_NOERR) { |
| 271 |
goto error_ret; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
if (vp->num_elem > 0) { |
| 276 |
if (put_truth_table(exoid, eblk_varid, vp->elem_var_tab, "element") != NC_NOERR) { |
| 277 |
goto error_ret; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
if (vp->num_nset > 0) { |
| 282 |
if (put_truth_table(exoid, nset_varid, vp->nset_var_tab, "nodeset") != NC_NOERR) { |
| 283 |
goto error_ret; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
if (vp->num_eset > 0) { |
| 288 |
if (put_truth_table(exoid, eset_varid, vp->eset_var_tab, "edgeset") != NC_NOERR) { |
| 289 |
goto error_ret; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
if (vp->num_fset > 0) { |
| 294 |
if (put_truth_table(exoid, fset_varid, vp->fset_var_tab, "faceset") != NC_NOERR) { |
| 295 |
goto error_ret; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
if (vp->num_sset > 0) { |
| 300 |
if (put_truth_table(exoid, sset_varid, vp->sset_var_tab, "sideset") != NC_NOERR) { |
| 301 |
goto error_ret; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
if (vp->num_elset > 0) { |
| 306 |
if (put_truth_table(exoid, elset_varid, vp->elset_var_tab, "elemset") != NC_NOERR) { |
| 307 |
goto error_ret; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
EX_FUNC_LEAVE(EX_NOERR); |
| 312 |
|
| 313 |
/* Fatal error: exit definition mode and return */ |
| 314 |
error_ret: |
| 315 |
if (in_define == 1) { |
| 316 |
exi_leavedef(exoid, __func__); |
| 317 |
} |
| 318 |
free(eblk_ids); |
| 319 |
free(edblk_ids); |
| 320 |
free(fablk_ids); |
| 321 |
free(nset_ids); |
| 322 |
free(eset_ids); |
| 323 |
free(fset_ids); |
| 324 |
free(sset_ids); |
| 325 |
free(elset_ids); |
| 326 |
|
| 327 |
free(eblk_stat); |
| 328 |
free(edblk_stat); |
| 329 |
free(fablk_stat); |
| 330 |
free(nset_stat); |
| 331 |
free(eset_stat); |
| 332 |
free(fset_stat); |
| 333 |
free(sset_stat); |
| 334 |
free(elset_stat); |
| 335 |
|
| 336 |
EX_FUNC_LEAVE(EX_FATAL); |
| 337 |
} |
| 338 |
|
| 339 |
static int define_dimension(int exoid, const char *DIMENSION, int count, const char *label, |
| 340 |
int *dimid) |
| 341 |
{ |
| 342 |
int status; |
| 343 |
if ((status = nc_def_dim(exoid, DIMENSION, count, dimid)) != NC_NOERR) { |
| 344 |
if (status == NC_ENAMEINUSE) { |
| 345 |
char errmsg[MAX_ERR_LENGTH]; |
| 346 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 347 |
"ERROR: %s variable name parameters are already defined " |
| 348 |
"in file id %d", |
| 349 |
label, exoid); |
| 350 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 351 |
} |
| 352 |
else { |
| 353 |
char errmsg[MAX_ERR_LENGTH]; |
| 354 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 355 |
"ERROR: failed to define number of %s variables in file id %d", label, exoid); |
| 356 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 357 |
} |
| 358 |
} |
| 359 |
return status; |
| 360 |
} |
| 361 |
|
| 362 |
static int define_variable_name_variable(int exoid, const char *VARIABLE, int dimension, |
| 363 |
const char *label) |
| 364 |
{ |
| 365 |
int dims[2]; |
| 366 |
dims[0] = dimension; |
| 367 |
(void)nc_inq_dimid(exoid, DIM_STR_NAME, &dims[1]); /* Checked earlier, so known to exist */ |
| 368 |
|
| 369 |
int variable; |
| 370 |
int status; |
| 371 |
if ((status = nc_def_var(exoid, VARIABLE, NC_CHAR, 2, dims, &variable)) != NC_NOERR) { |
| 372 |
if (status == NC_ENAMEINUSE) { |
| 373 |
char errmsg[MAX_ERR_LENGTH]; |
| 374 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: %s variable names are already defined in file id %d", |
| 375 |
label, exoid); |
| 376 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 377 |
} |
| 378 |
else { |
| 379 |
char errmsg[MAX_ERR_LENGTH]; |
| 380 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define %s variable names in file id %d", |
| 381 |
label, exoid); |
| 382 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 383 |
} |
| 384 |
} |
| 385 |
exi_set_compact_storage(exoid, variable); |
| 386 |
#if defined(EX_CAN_USE_NC_DEF_VAR_FILL) |
| 387 |
int fill = NC_FILL_CHAR; |
| 388 |
nc_def_var_fill(exoid, variable, 0, &fill); |
| 389 |
#endif |
| 390 |
return status; |
| 391 |
} |
| 392 |
|
| 393 |
static int *get_status_array(int exoid, int var_count, const char *VARIABLE, const char *label) |
| 394 |
{ |
| 395 |
int *stat_vals = NULL; |
| 396 |
|
| 397 |
if (!(stat_vals = malloc(var_count * sizeof(int)))) { |
| 398 |
char errmsg[MAX_ERR_LENGTH]; |
| 399 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 400 |
"ERROR: failed to allocate memory for %s status array for file id %d", label, exoid); |
| 401 |
ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL); |
| 402 |
return NULL; |
| 403 |
} |
| 404 |
|
| 405 |
/* get variable id of status array */ |
| 406 |
int varid; |
| 407 |
if ((nc_inq_varid(exoid, VARIABLE, &varid)) == NC_NOERR) { |
| 408 |
/* if status array exists (V 2.01+), use it, otherwise assume |
| 409 |
object exists to be backward compatible */ |
| 410 |
|
| 411 |
int status; |
| 412 |
if ((status = nc_get_var_int(exoid, varid, stat_vals)) != NC_NOERR) { |
| 413 |
free(stat_vals); |
| 414 |
char errmsg[MAX_ERR_LENGTH]; |
| 415 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get %s status array from file id %d", |
| 416 |
label, exoid); |
| 417 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 418 |
return NULL; |
| 419 |
} |
| 420 |
} |
| 421 |
else { |
| 422 |
/* status array doesn't exist (V2.00), dummy one up for later checking */ |
| 423 |
for (int i = 0; i < var_count; i++) { |
| 424 |
stat_vals[i] = 1; |
| 425 |
} |
| 426 |
} |
| 427 |
return stat_vals; |
| 428 |
} |
| 429 |
|
| 430 |
static int put_truth_table(int exoid, int varid, int *table, const char *label) |
| 431 |
{ |
| 432 |
int iresult = nc_put_var_int(exoid, varid, table); |
| 433 |
|
| 434 |
if (iresult != NC_NOERR) { |
| 435 |
char errmsg[MAX_ERR_LENGTH]; |
| 436 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store %s variable truth table in file id %d", |
| 437 |
label, exoid); |
| 438 |
ex_err_fn(exoid, __func__, errmsg, iresult); |
| 439 |
} |
| 440 |
return iresult; |
| 441 |
} |
| 442 |
|
| 443 |
static int define_truth_table(ex_entity_type obj_type, int exoid, int num_ent, int num_var, |
| 444 |
int *var_tab, int *status_tab, void_int *ids, const char *label) |
| 445 |
{ |
| 446 |
int time_dim; |
| 447 |
int varid; |
| 448 |
int status; |
| 449 |
|
| 450 |
if ((status = nc_inq_dimid(exoid, DIM_TIME, &time_dim)) != NC_NOERR) { |
| 451 |
char errmsg[MAX_ERR_LENGTH]; |
| 452 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate time dimension in file id %d", exoid); |
| 453 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 454 |
return -1; |
| 455 |
} |
| 456 |
|
| 457 |
if (var_tab == NULL) { |
| 458 |
char errmsg[MAX_ERR_LENGTH]; |
| 459 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: %s variable truth table is NULL in file id %d", label, |
| 460 |
exoid); |
| 461 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 462 |
return -1; |
| 463 |
} |
| 464 |
|
| 465 |
int k = 0; |
| 466 |
for (int i = 0; i < num_ent; i++) { |
| 467 |
int64_t id; |
| 468 |
if (ex_int64_status(exoid) & EX_IDS_INT64_API) { |
| 469 |
id = ((int64_t *)ids)[i]; |
| 470 |
} |
| 471 |
else { |
| 472 |
id = ((int *)ids)[i]; |
| 473 |
} |
| 474 |
|
| 475 |
for (int j = 1; j <= num_var; j++) { |
| 476 |
|
| 477 |
/* check if variables are to be put out for this block */ |
| 478 |
if (var_tab[k] != 0) { |
| 479 |
if (status_tab[i] != 0) { /* only define variable if active */ |
| 480 |
int dims[2]; |
| 481 |
dims[0] = time_dim; |
| 482 |
|
| 483 |
/* Determine number of entities in entity */ |
| 484 |
/* Need way to make this more generic... */ |
| 485 |
status = nc_inq_dimid(exoid, exi_dim_num_entries_in_object(obj_type, i + 1), &dims[1]); |
| 486 |
if (status != NC_NOERR) { |
| 487 |
char errmsg[MAX_ERR_LENGTH]; |
| 488 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 489 |
"ERROR: failed to locate number of entities in %s %" PRId64 " in file id %d", |
| 490 |
label, id, exoid); |
| 491 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 492 |
return status; |
| 493 |
} |
| 494 |
|
| 495 |
/* define netCDF variable to store variable values; |
| 496 |
* the j index cycles from 1 through the number of variables so |
| 497 |
* that the index of the EXODUS variable (which is part of |
| 498 |
* the name of the netCDF variable) will begin at 1 instead of 0 |
| 499 |
*/ |
| 500 |
status = nc_def_var(exoid, exi_name_var_of_object(obj_type, j, i + 1), nc_flt_code(exoid), |
| 501 |
2, dims, &varid); |
| 502 |
if (status != NC_NOERR) { |
| 503 |
if (status != NC_ENAMEINUSE) { |
| 504 |
char errmsg[MAX_ERR_LENGTH]; |
| 505 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 506 |
"ERROR: failed to define %s variable for %s %" PRId64 " in file id %d", |
| 507 |
label, label, id, exoid); |
| 508 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 509 |
return status; |
| 510 |
} |
| 511 |
} |
| 512 |
exi_compress_variable(exoid, varid, 2); |
| 513 |
} |
| 514 |
} /* if */ |
| 515 |
k++; /* increment truth table pointer */ |
| 516 |
} /* for j */ |
| 517 |
} /* for i */ |
| 518 |
return NC_NOERR; |
| 519 |
} |
| 520 |
|
| 521 |
static int exi_define_vars(int exoid, ex_entity_type obj_type, const char *entity_name, |
| 522 |
const char *entity_blk_name, int numvar, const char *DNAME, int dimid1, |
| 523 |
int dimid2, int DVAL, void_int **entity_ids, const char *VNOV, |
| 524 |
const char *VTV, int **status_var, int *truth_table, |
| 525 |
int *truth_table_var) |
| 526 |
{ |
| 527 |
if (numvar > 0) { |
| 528 |
int status; |
| 529 |
if ((status = define_dimension(exoid, DNAME, numvar, entity_name, &dimid2)) != NC_NOERR) { |
| 530 |
return status; |
| 531 |
} |
| 532 |
|
| 533 |
/* Now define entity_name variable name variable */ |
| 534 |
if ((status = define_variable_name_variable(exoid, VNOV, dimid2, entity_name)) != NC_NOERR) { |
| 535 |
return status; |
| 536 |
} |
| 537 |
|
| 538 |
if ((status = define_truth_table(obj_type, exoid, DVAL, numvar, truth_table, *status_var, |
| 539 |
*entity_ids, entity_blk_name)) != NC_NOERR) { |
| 540 |
return status; |
| 541 |
} |
| 542 |
|
| 543 |
free(*status_var); |
| 544 |
*status_var = NULL; |
| 545 |
free(*entity_ids); |
| 546 |
*entity_ids = NULL; |
| 547 |
|
| 548 |
/* create a variable array in which to store the entity_name variable truth |
| 549 |
* table |
| 550 |
*/ |
| 551 |
|
| 552 |
int dims[] = {dimid1, dimid2}; |
| 553 |
if ((status = nc_def_var(exoid, VTV, NC_INT, 2, dims, truth_table_var)) != NC_NOERR) { |
| 554 |
char errmsg[MAX_ERR_LENGTH]; |
| 555 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 556 |
"ERROR: failed to define %s variable truth table in file id %d", entity_name, exoid); |
| 557 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 558 |
return status; |
| 559 |
} |
| 560 |
exi_set_compact_storage(exoid, *truth_table_var); |
| 561 |
} |
| 562 |
return NC_NOERR; |
| 563 |
} |