Debian Codegraph @

packages/seacas/libraries/exodus/src/ex_put_variable_names.c

raw
1 /*
2 * Copyright(C) 1999-2020, 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 * expvan - ex_put_variable_names
11 *
12 * entry conditions -
13 * input parameters:
14 * int exoid exodus file id
15 * int obj_type variable type: G,N, or E
16 * int num_vars # of variables to read
17 * char* var_names ptr array of variable names
18 *
19 * exit conditions -
20 *
21 * revision history -
22 *
23 *
24 *****************************************************************************/
25
26 #include "exodusII.h" // for ex_err, etc
27 #include "exodusII_int.h" // for EX_FATAL, etc
28
29 static int ex_put_var_names_int(int exoid, char *tname, char *dnumvar, char *vnames, int *varid)
30 {
31 int status;
32 int dimid;
33 char errmsg[MAX_ERR_LENGTH];
34
35 if ((status = nc_inq_dimid(exoid, dnumvar, &dimid)) != NC_NOERR) {
36 if (status == NC_EBADDIM) {
37 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: no %s variables defined in file id %d", tname,
38 exoid);
39 ex_err_fn(exoid, __func__, errmsg, status);
40 }
41 else {
42 snprintf(errmsg, MAX_ERR_LENGTH,
43 "ERROR: failed to locate number of %s variables in file id %d", tname, exoid);
44 ex_err_fn(exoid, __func__, errmsg, status);
45 }
46 return EX_FATAL;
47 }
48
49 if ((status = nc_inq_varid(exoid, vnames, varid)) != NC_NOERR) {
50 if (status == NC_ENOTVAR) {
51 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: no %s variable names defined in file id %d", tname,
52 exoid);
53 ex_err_fn(exoid, __func__, errmsg, status);
54 }
55 else {
56 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: %s name variable names not found in file id %d",
57 tname, exoid);
58 ex_err_fn(exoid, __func__, errmsg, status);
59 }
60 return EX_FATAL;
61 }
62 return EX_NOERR;
63 }
64
65 /*!
66 \ingroup ResultsData
67
68 The function ex_put_variable_names() writes the names of the results
69 variables to the database. The maximum length of the names returned is
70 specified by the return value from ex_inquire_int()(exoid,
71 EX_INQ_MAX_READ_NAME_LENGTH). The function ex_put_variable_param()
72 must be called before this function is invoked.
73
74 \return In case of an error, ex_put_variable_names() returns a negative
75 number; a warning will return a positive number. Possible causes of
76 errors include:
77 - data file not properly opened with call to ex_create() or ex_open()
78 - data file not initialized properly with call to ex_put_init().
79 - invalid variable type specified.
80 - ex_put_variable_param() was not called previously or was
81 called with zero variables of the specified type.
82 - ex_put_variable_names() has been called previously for the
83 specified variable type.
84
85 \param[in] exoid exodus file ID returned from a previous call to
86 ex_create() or ex_open().
87 \param[in] obj_type Variable indicating the type of variable which is
88 described.
89 Use one of the options in the table below.
90 \param[in] num_vars The number of var_type variables that will be written
91 to the database.
92 \param[in] var_names Array of pointers to num_vars variable names.
93
94 | ex_entity_type| description |
95 |---------------|---------------------------|
96 | #EX_GLOBAL | Global entity type |
97 | #EX_NODAL | Nodal entity type |
98 | #EX_NODE_SET | Node Set entity type |
99 | #EX_EDGE_BLOCK | Edge Block entity type |
100 | #EX_EDGE_SET | Edge Set entity type |
101 | #EX_FACE_BLOCK | Face Block entity type |
102 | #EX_FACE_SET | Face Set entity type |
103 | #EX_ELEM_BLOCK | Element Block entity type|
104 | #EX_ELEM_SET | Element Set entity type |
105 | #EX_SIDE_SET | Side Set entity type |
106
107 The following coding will write out the names associated with the
108 nodal variables:
109 ~~~{.c}
110 int num_nod_vars, error, exoid;
111 char *var_names[2];
112
113 \comment{write results variables parameters and names}
114 num_nod_vars = 2;
115
116 var_names[0] = "disx";
117 var_names[1] = "disy";
118
119 error = ex_put_variable_param (exoid, EX_NODAL, num_nod_vars);
120 error = ex_put_variable_names (exoid, EX_NODAL, num_nod_vars, var_names);
121 ~~~
122
123 */
124
125 int ex_put_variable_names(int exoid, ex_entity_type obj_type, int num_vars, char *const var_names[])
126 {
127 int varid = 0;
128 int status = 0;
129 char errmsg[MAX_ERR_LENGTH];
130
131 EX_FUNC_ENTER();
132 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
133 EX_FUNC_LEAVE(EX_FATAL);
134 }
135
136 switch (obj_type) {
137 case EX_GLOBAL:
138 ex_put_var_names_int(exoid, "global", DIM_NUM_GLO_VAR, VAR_NAME_GLO_VAR, &varid);
139 break;
140 case EX_NODAL:
141 ex_put_var_names_int(exoid, "nodal", DIM_NUM_NOD_VAR, VAR_NAME_NOD_VAR, &varid);
142 break;
143 case EX_ASSEMBLY:
144 ex_put_var_names_int(exoid, "assembly", DIM_NUM_ASSEMBLY_VAR, VAR_NAME_ASSEMBLY_VAR, &varid);
145 break;
146 case EX_BLOB:
147 ex_put_var_names_int(exoid, "blob", DIM_NUM_BLOB_VAR, VAR_NAME_BLOB_VAR, &varid);
148 break;
149 case EX_EDGE_BLOCK:
150 ex_put_var_names_int(exoid, "edge", DIM_NUM_EDG_VAR, VAR_NAME_EDG_VAR, &varid);
151 break;
152 case EX_FACE_BLOCK:
153 ex_put_var_names_int(exoid, "face", DIM_NUM_FAC_VAR, VAR_NAME_FAC_VAR, &varid);
154 break;
155 case EX_ELEM_BLOCK:
156 ex_put_var_names_int(exoid, "element", DIM_NUM_ELE_VAR, VAR_NAME_ELE_VAR, &varid);
157 break;
158 case EX_NODE_SET:
159 ex_put_var_names_int(exoid, "node set", DIM_NUM_NSET_VAR, VAR_NAME_NSET_VAR, &varid);
160 break;
161 case EX_EDGE_SET:
162 ex_put_var_names_int(exoid, "edge set", DIM_NUM_ESET_VAR, VAR_NAME_ESET_VAR, &varid);
163 break;
164 case EX_FACE_SET:
165 ex_put_var_names_int(exoid, "face set", DIM_NUM_FSET_VAR, VAR_NAME_FSET_VAR, &varid);
166 break;
167 case EX_SIDE_SET:
168 ex_put_var_names_int(exoid, "side set", DIM_NUM_SSET_VAR, VAR_NAME_SSET_VAR, &varid);
169 break;
170 case EX_ELEM_SET:
171 ex_put_var_names_int(exoid, "element set", DIM_NUM_ELSET_VAR, VAR_NAME_ELSET_VAR, &varid);
172 break;
173 default:
174 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Invalid variable type %d specified in file id %d",
175 obj_type, exoid);
176 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
177 EX_FUNC_LEAVE(EX_FATAL);
178 }
179
180 /* write EXODUS variable names */
181 status = exi_put_names(exoid, varid, num_vars, var_names, obj_type, "variable", __func__);
182
183 EX_FUNC_LEAVE(status);
184 }