1 /*
2  * Copyright (c) 2005 Sandia Corporation. Under the terms of Contract
3  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Governement
4  * retains certain rights in this software.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *
13  *     * Redistributions in binary form must reproduce the above
14  *       copyright notice, this list of conditions and the following
15  *       disclaimer in the documentation and/or other materials provided
16  *       with the distribution.
17  *
18  *     * Neither the name of Sandia Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 /*****************************************************************************
36 *
37 * expmp - ex_put_attr_param
38 *
39 * entry conditions -
40 *   input parameters:
41 *       int     exoid           exodus file id
42 *       int     obj_type        block/set type (node, edge, face, elem)
43 *       int     obj_id          block/set id (ignored for NODAL)
44 *       int     num_attrs       number of attributes
45 *
46 * exit conditions -
47 *
48 *
49 *****************************************************************************/
50 
51 #include "exodusII.h"
52 #include "exodusII_int.h"
53 
54 /*!
55  * defines the number of attributes.
56  * \param   exoid           exodus file id
57  * \param   obj_type        block/set type (node, edge, face, elem)
58  * \param   obj_id          block/set id (ignored for NODAL)
59  * \param   num_attrs       number of attributes
60  */
61 
ex_put_attr_param(int exoid,ex_entity_type obj_type,ex_entity_id obj_id,int num_attrs)62 int ex_put_attr_param (int   exoid,
63 		       ex_entity_type obj_type,
64 		       ex_entity_id   obj_id,
65 		       int   num_attrs)
66 {
67   int status;
68   int dims[2];
69   int strdim, varid;
70 
71   char errmsg[MAX_ERR_LENGTH];
72   const char *dnumobjent;
73   const char *dnumobjatt;
74   const char *vobjatt;
75   const char *vattnam;
76   int numobjentdim;
77   int obj_id_ndx;
78   int numattrdim;
79 
80   /* Determine index of obj_id in obj_type id array */
81   if (obj_type == EX_NODAL)
82     obj_id_ndx = 0;
83   else {
84     obj_id_ndx = ex_id_lkup(exoid,obj_type,obj_id);
85 
86     if (exerrval != 0) {
87       if (exerrval == EX_NULLENTITY) {
88 	sprintf(errmsg,
89 		"Warning: no attributes found for NULL %s %"PRId64" in file id %d",
90 		ex_name_of_object(obj_type),obj_id,exoid);
91 	ex_err("ex_put_attr_param",errmsg,EX_MSG);
92 	return (EX_WARN);              /* no attributes for this object */
93       } else {
94 	sprintf(errmsg,
95 		"Warning: failed to locate %s id %"PRId64" in id array in file id %d",
96 		ex_name_of_object(obj_type),obj_id, exoid);
97 	ex_err("ex_put_attr_param",errmsg,exerrval);
98 	return (EX_WARN);
99       }
100     }
101   }
102 
103   switch (obj_type) {
104   case EX_SIDE_SET:
105     dnumobjent = DIM_NUM_SIDE_SS(obj_id_ndx);
106     dnumobjatt = DIM_NUM_ATT_IN_SS(obj_id_ndx);
107     vobjatt = VAR_SSATTRIB(obj_id_ndx);
108     vattnam = VAR_NAME_SSATTRIB(obj_id_ndx);
109     break;
110   case EX_NODE_SET:
111     dnumobjent = DIM_NUM_NOD_NS(obj_id_ndx);
112     dnumobjatt = DIM_NUM_ATT_IN_NS(obj_id_ndx);
113     vobjatt = VAR_NSATTRIB(obj_id_ndx);
114     vattnam = VAR_NAME_NSATTRIB(obj_id_ndx);
115     break;
116   case EX_EDGE_SET:
117     dnumobjent = DIM_NUM_EDGE_ES(obj_id_ndx);
118     dnumobjatt = DIM_NUM_ATT_IN_ES(obj_id_ndx);
119     vobjatt = VAR_ESATTRIB(obj_id_ndx);
120     vattnam = VAR_NAME_ESATTRIB(obj_id_ndx);
121     break;
122   case EX_FACE_SET:
123     dnumobjent = DIM_NUM_FACE_FS(obj_id_ndx);
124     dnumobjatt = DIM_NUM_ATT_IN_FS(obj_id_ndx);
125     vobjatt = VAR_FSATTRIB(obj_id_ndx);
126     vattnam = VAR_NAME_FSATTRIB(obj_id_ndx);
127     break;
128   case EX_ELEM_SET:
129     dnumobjent = DIM_NUM_ELE_ELS(obj_id_ndx);
130     dnumobjatt = DIM_NUM_ATT_IN_ELS(obj_id_ndx);
131     vobjatt = VAR_ELSATTRIB(obj_id_ndx);
132     vattnam = VAR_NAME_ELSATTRIB(obj_id_ndx);
133     break;
134   case EX_NODAL:
135     dnumobjent = DIM_NUM_NODES;
136     dnumobjatt = DIM_NUM_ATT_IN_NBLK;
137     vobjatt = VAR_NATTRIB;
138     vattnam = VAR_NAME_NATTRIB;
139     break;
140   case EX_EDGE_BLOCK:
141     dnumobjent = DIM_NUM_ED_IN_EBLK(obj_id_ndx);
142     dnumobjatt = DIM_NUM_ATT_IN_EBLK(obj_id_ndx);
143     vobjatt = VAR_EATTRIB(obj_id_ndx);
144     vattnam = VAR_NAME_EATTRIB(obj_id_ndx);
145     break;
146   case EX_FACE_BLOCK:
147     dnumobjent = DIM_NUM_FA_IN_FBLK(obj_id_ndx);
148     dnumobjatt = DIM_NUM_ATT_IN_FBLK(obj_id_ndx);
149     vobjatt = VAR_FATTRIB(obj_id_ndx);
150     vattnam = VAR_NAME_FATTRIB(obj_id_ndx);
151     break;
152   case EX_ELEM_BLOCK:
153     dnumobjent = DIM_NUM_EL_IN_BLK(obj_id_ndx);
154     dnumobjatt = DIM_NUM_ATT_IN_BLK(obj_id_ndx);
155     vobjatt = VAR_ATTRIB(obj_id_ndx);
156     vattnam = VAR_NAME_ATTRIB(obj_id_ndx);
157     break;
158   default:
159     exerrval = EX_BADPARAM;
160     sprintf(errmsg, "Error: Bad block type (%d) specified for file id %d",
161 	    obj_type, exoid );
162     ex_err("ex_put_attr_param",errmsg,exerrval);
163     return (EX_FATAL);
164   }
165 
166   exerrval = 0; /* clear error code */
167 
168   if ((status = nc_inq_dimid(exoid, dnumobjent, &numobjentdim)) != NC_NOERR) {
169     exerrval = status;
170     sprintf(errmsg,
171 	    "Error: failed to locate number of entries for %s %"PRId64" in file id %d",
172 	    ex_name_of_object(obj_type), obj_id, exoid);
173     ex_err("ex_put_attr_param",errmsg,exerrval);
174     return (EX_FATAL);
175   }
176 
177   /* put netcdf file into define mode  */
178   if ((status = nc_redef (exoid)) != NC_NOERR) {
179     exerrval = status;
180     sprintf(errmsg,"Error: failed to place file id %d into define mode",exoid);
181     ex_err("ex_put_attr_param",errmsg,exerrval);
182     return (EX_FATAL);
183   }
184 
185   if ((status = nc_def_dim(exoid, dnumobjatt, num_attrs, &numattrdim)) != NC_NOERR) {
186     exerrval = status;
187     sprintf(errmsg,
188 	    "Error: failed to define number of attributes in %s %"PRId64" in file id %d",
189 	    ex_name_of_object(obj_type), obj_id,exoid);
190     ex_err("ex_put_attr_param",errmsg,exerrval);
191     goto error_ret;         /* exit define mode and return */
192   }
193 
194   dims[0] = numobjentdim;
195   dims[1] = numattrdim;
196 
197   if ((status = nc_def_var(exoid, vobjatt, nc_flt_code(exoid), 2, dims, &varid)) != NC_NOERR) {
198     exerrval = status;
199     sprintf(errmsg,
200 	    "Error:  failed to define attributes for %s %"PRId64" in file id %d",
201 	    ex_name_of_object(obj_type), obj_id,exoid);
202     ex_err("ex_put_attr_param",errmsg,exerrval);
203     goto error_ret;         /* exit define mode and return */
204   }
205   ex_compress_variable(exoid, varid, 2);
206 
207   /* inquire previously defined dimensions  */
208   if ((status = nc_inq_dimid(exoid, DIM_STR_NAME, &strdim)) != NC_NOERR) {
209     exerrval = status;
210     sprintf(errmsg,
211 	    "Error: failed to get string length in file id %d",exoid);
212     ex_err("ex_put_attr_param",errmsg,exerrval);
213     return (EX_FATAL);
214   }
215 
216   /* Attribute names... */
217   dims[0] = numattrdim;
218   dims[1] = strdim;
219 
220   if ((status = nc_def_var(exoid, vattnam, NC_CHAR, 2, dims, &varid)) != NC_NOERR) {
221     exerrval = status;
222     sprintf(errmsg,
223 	    "Error: failed to define %s attribute name array in file id %d",
224 	    ex_name_of_object(obj_type), exoid);
225     ex_err("ex_put_attr_param",errmsg,exerrval);
226     goto error_ret;         /* exit define mode and return */
227   }
228 
229   /* leave define mode  */
230   if ((status = nc_enddef(exoid)) != NC_NOERR) {
231     exerrval = status;
232     sprintf(errmsg,
233 	    "Error: failed to complete %s attribute parameter definition in file id %d",
234 	    ex_name_of_object(obj_type), exoid);
235     ex_err("ex_put_attr_param",errmsg,exerrval);
236     return (EX_FATAL);
237   }
238 
239   return (EX_NOERR);
240 
241   /* Fatal error: exit definition mode and return */
242  error_ret:
243   if (nc_enddef (exoid) != NC_NOERR) {     /* exit define mode */
244     sprintf(errmsg,
245 	    "Error: failed to complete definition for file id %d",
246 	    exoid);
247     ex_err("ex_put_attr_param",errmsg,exerrval);
248   }
249   return (EX_FATAL);
250 }
251