Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2020, 2023 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 * expmp - ex_put_attr_param
11 *
12 * entry conditions -
13 * input parameters:
14 * int exoid exodus file id
15 * int obj_type block/set type (node, edge, face, elem)
16 * int obj_id block/set id (ignored for NODAL)
17 * int num_attrs number of attributes
18 *
19 * exit conditions -
20 *
21 *
22 *****************************************************************************/
23
24 #include "exodusII.h" // for ex_err, etc
25 #include "exodusII_int.h" // for EX_FATAL, EX_WARN, etc
26
27 /*!
28 * defines the number of attributes.
29 * \param exoid exodus file id
30 * \param obj_type block/set type (node, edge, face, elem)
31 * \param obj_id block/set id (ignored for NODAL)
32 * \param num_attrs number of attributes
33 */
34
35 int ex_put_attr_param(int exoid, ex_entity_type obj_type, ex_entity_id obj_id, int num_attrs)
36 {
37 int status;
38 int dims[2];
39 int strdim, varid;
40
41 char errmsg[MAX_ERR_LENGTH];
42 const char *dnumobjent;
43 const char *dnumobjatt;
44 const char *vobjatt;
45 const char *vattnam;
46 int numobjentdim;
47 int obj_id_ndx;
48 int numattrdim;
49
50 EX_FUNC_ENTER();
51 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
52 EX_FUNC_LEAVE(EX_FATAL);
53 }
54
55 /* Determine index of obj_id in obj_type id array */
56 if (obj_type == EX_NODAL) {
57 obj_id_ndx = 0;
58 }
59 else {
60 obj_id_ndx = exi_id_lkup(exoid, obj_type, obj_id);
61 if (obj_id_ndx <= 0) {
62 ex_get_err(NULL, NULL, &status);
63
64 if (status != 0) {
65 if (status == EX_NULLENTITY) {
66 snprintf(errmsg, MAX_ERR_LENGTH,
67 "Warning: no attributes found for NULL %s %" PRId64 " in file id %d",
68 ex_name_of_object(obj_type), obj_id, exoid);
69 ex_err_fn(exoid, __func__, errmsg, EX_NULLENTITY);
70 EX_FUNC_LEAVE(EX_WARN); /* no attributes for this object */
71 }
72 snprintf(errmsg, MAX_ERR_LENGTH,
73 "Warning: failed to locate %s id %" PRId64 " in id array in file id %d",
74 ex_name_of_object(obj_type), obj_id, exoid);
75 ex_err_fn(exoid, __func__, errmsg, status);
76 EX_FUNC_LEAVE(EX_WARN);
77 }
78 }
79 }
80
81 switch (obj_type) {
82 case EX_SIDE_SET:
83 dnumobjent = DIM_NUM_SIDE_SS(obj_id_ndx);
84 dnumobjatt = DIM_NUM_ATT_IN_SS(obj_id_ndx);
85 vobjatt = VAR_SSATTRIB(obj_id_ndx);
86 vattnam = VAR_NAME_SSATTRIB(obj_id_ndx);
87 break;
88 case EX_NODE_SET:
89 dnumobjent = DIM_NUM_NOD_NS(obj_id_ndx);
90 dnumobjatt = DIM_NUM_ATT_IN_NS(obj_id_ndx);
91 vobjatt = VAR_NSATTRIB(obj_id_ndx);
92 vattnam = VAR_NAME_NSATTRIB(obj_id_ndx);
93 break;
94 case EX_EDGE_SET:
95 dnumobjent = DIM_NUM_EDGE_ES(obj_id_ndx);
96 dnumobjatt = DIM_NUM_ATT_IN_ES(obj_id_ndx);
97 vobjatt = VAR_ESATTRIB(obj_id_ndx);
98 vattnam = VAR_NAME_ESATTRIB(obj_id_ndx);
99 break;
100 case EX_FACE_SET:
101 dnumobjent = DIM_NUM_FACE_FS(obj_id_ndx);
102 dnumobjatt = DIM_NUM_ATT_IN_FS(obj_id_ndx);
103 vobjatt = VAR_FSATTRIB(obj_id_ndx);
104 vattnam = VAR_NAME_FSATTRIB(obj_id_ndx);
105 break;
106 case EX_ELEM_SET:
107 dnumobjent = DIM_NUM_ELE_ELS(obj_id_ndx);
108 dnumobjatt = DIM_NUM_ATT_IN_ELS(obj_id_ndx);
109 vobjatt = VAR_ELSATTRIB(obj_id_ndx);
110 vattnam = VAR_NAME_ELSATTRIB(obj_id_ndx);
111 break;
112 case EX_NODAL:
113 dnumobjent = DIM_NUM_NODES;
114 dnumobjatt = DIM_NUM_ATT_IN_NBLK;
115 vobjatt = VAR_NATTRIB;
116 vattnam = VAR_NAME_NATTRIB;
117 break;
118 case EX_EDGE_BLOCK:
119 dnumobjent = DIM_NUM_ED_IN_EBLK(obj_id_ndx);
120 dnumobjatt = DIM_NUM_ATT_IN_EBLK(obj_id_ndx);
121 vobjatt = VAR_EATTRIB(obj_id_ndx);
122 vattnam = VAR_NAME_EATTRIB(obj_id_ndx);
123 break;
124 case EX_FACE_BLOCK:
125 dnumobjent = DIM_NUM_FA_IN_FBLK(obj_id_ndx);
126 dnumobjatt = DIM_NUM_ATT_IN_FBLK(obj_id_ndx);
127 vobjatt = VAR_FATTRIB(obj_id_ndx);
128 vattnam = VAR_NAME_FATTRIB(obj_id_ndx);
129 break;
130 case EX_ELEM_BLOCK:
131 dnumobjent = DIM_NUM_EL_IN_BLK(obj_id_ndx);
132 dnumobjatt = DIM_NUM_ATT_IN_BLK(obj_id_ndx);
133 vobjatt = VAR_ATTRIB(obj_id_ndx);
134 vattnam = VAR_NAME_ATTRIB(obj_id_ndx);
135 break;
136 default:
137 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Bad block type (%d) specified for file id %d",
138 obj_type, exoid);
139 ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM);
140 EX_FUNC_LEAVE(EX_FATAL);
141 }
142
143 if ((status = nc_inq_dimid(exoid, dnumobjent, &numobjentdim)) != NC_NOERR) {
144 snprintf(errmsg, MAX_ERR_LENGTH,
145 "ERROR: failed to locate number of entries for %s %" PRId64 " in file id %d",
146 ex_name_of_object(obj_type), obj_id, exoid);
147 ex_err_fn(exoid, __func__, errmsg, status);
148 EX_FUNC_LEAVE(EX_FATAL);
149 }
150
151 /* put netcdf file into define mode */
152 if ((status = exi_redef(exoid, __func__)) != NC_NOERR) {
153 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to place file id %d into define mode", exoid);
154 ex_err_fn(exoid, __func__, errmsg, status);
155 EX_FUNC_LEAVE(EX_FATAL);
156 }
157
158 if ((status = nc_def_dim(exoid, dnumobjatt, num_attrs, &numattrdim)) != NC_NOERR) {
159 snprintf(errmsg, MAX_ERR_LENGTH,
160 "ERROR: failed to define number of attributes in %s %" PRId64 " in file id %d",
161 ex_name_of_object(obj_type), obj_id, exoid);
162 ex_err_fn(exoid, __func__, errmsg, status);
163 goto error_ret; /* exit define mode and return */
164 }
165
166 dims[0] = numobjentdim;
167 dims[1] = numattrdim;
168
169 if ((status = nc_def_var(exoid, vobjatt, nc_flt_code(exoid), 2, dims, &varid)) != NC_NOERR) {
170 snprintf(errmsg, MAX_ERR_LENGTH,
171 "ERROR: failed to define attributes for %s %" PRId64 " in file id %d",
172 ex_name_of_object(obj_type), obj_id, exoid);
173 ex_err_fn(exoid, __func__, errmsg, status);
174 goto error_ret; /* exit define mode and return */
175 }
176 exi_compress_variable(exoid, varid, 2);
177
178 /* inquire previously defined dimensions */
179 if ((status = nc_inq_dimid(exoid, DIM_STR_NAME, &strdim)) != NC_NOERR) {
180 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get string length in file id %d", exoid);
181 ex_err_fn(exoid, __func__, errmsg, status);
182 EX_FUNC_LEAVE(EX_FATAL);
183 }
184
185 /* Attribute names... */
186 dims[0] = numattrdim;
187 dims[1] = strdim;
188
189 if ((status = nc_def_var(exoid, vattnam, NC_CHAR, 2, dims, &varid)) != NC_NOERR) {
190 snprintf(errmsg, MAX_ERR_LENGTH,
191 "ERROR: failed to define %s attribute name array in file id %d",
192 ex_name_of_object(obj_type), exoid);
193 ex_err_fn(exoid, __func__, errmsg, status);
194 goto error_ret; /* exit define mode and return */
195 }
196 #if defined(EX_CAN_USE_NC_DEF_VAR_FILL)
197 int fill = NC_FILL_CHAR;
198 nc_def_var_fill(exoid, varid, 0, &fill);
199 #endif
200
201 /* leave define mode */
202 if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) {
203 EX_FUNC_LEAVE(EX_FATAL);
204 }
205
206 EX_FUNC_LEAVE(EX_NOERR);
207
208 /* Fatal error: exit definition mode and return */
209 error_ret:
210 exi_leavedef(exoid, __func__);
211 EX_FUNC_LEAVE(EX_FATAL);
212 }