1 /*
2  * Copyright (c) 2006 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 * expoea - ex_put_one_attr
38 *
39 * entry conditions -
40 *   input parameters:
41 *       int     exoid                   exodus file id
42 *       int     obj_type                object type (edge, face, elem block)
43 *       int     obj_id                  object id (edge, face, elem block ID)
44 *       int     attrib_index            index of attribute to write
45 *       float*  attrib                  array of attributes
46 *
47 * exit conditions -
48 *
49 *
50 *****************************************************************************/
51 
52 #include "exodusII.h"
53 #include "exodusII_int.h"
54 
55 /*!
56  * writes the specified attribute for a block
57  * \param      exoid                   exodus file id
58  * \param      obj_type                object type (edge, face, elem block)
59  * \param      obj_id                  object id (edge, face, elem block ID)
60  * \param      attrib_index            index of attribute to write
61  * \param      attrib                  array of attributes
62  */
63 
ex_put_one_attr(int exoid,ex_entity_type obj_type,ex_entity_id obj_id,int attrib_index,const void * attrib)64 int ex_put_one_attr( int   exoid,
65                      ex_entity_type obj_type,
66                      ex_entity_id   obj_id,
67                      int   attrib_index,
68                      const void *attrib )
69 {
70   int status;
71   int attrid, obj_id_ndx, temp;
72   size_t num_entries_this_obj, num_attr;
73   size_t start[2], count[2];
74   ptrdiff_t stride[2];
75   char errmsg[MAX_ERR_LENGTH];
76   const char* dnumobjent;
77   const char* dnumobjatt;
78   const char* vattrbname;
79 
80   exerrval = 0; /* clear error code */
81 
82   /* Determine index of obj_id in id array */
83   if (obj_type != EX_NODAL) {
84     obj_id_ndx = ex_id_lkup(exoid,obj_type,obj_id);
85     if (exerrval != 0) {
86       if (exerrval == EX_NULLENTITY) {
87 	sprintf(errmsg,
88 		"Warning: no attributes allowed for NULL %s %"PRId64" in file id %d",
89 		ex_name_of_object(obj_type),obj_id,exoid);
90 	ex_err("ex_put_one_attr",errmsg,EX_MSG);
91 	return (EX_WARN);              /* no attributes for this element block */
92       } else {
93 	sprintf(errmsg,
94 		"Error: no %s id %"PRId64" in id array in file id %d",
95 		ex_name_of_object(obj_type), obj_id, exoid);
96 	ex_err("ex_put_one_attr",errmsg,exerrval);
97 	return (EX_FATAL);
98       }
99     }
100   }
101 
102   switch (obj_type) {
103   case EX_SIDE_SET:
104     dnumobjent = DIM_NUM_SIDE_SS(obj_id_ndx);
105     dnumobjatt = DIM_NUM_ATT_IN_SS(obj_id_ndx);
106     vattrbname = VAR_SSATTRIB(obj_id_ndx);
107     break;
108   case EX_NODE_SET:
109     dnumobjent = DIM_NUM_NOD_NS(obj_id_ndx);
110     dnumobjatt = DIM_NUM_ATT_IN_NS(obj_id_ndx);
111     vattrbname = VAR_NSATTRIB(obj_id_ndx);
112     break;
113   case EX_EDGE_SET:
114     dnumobjent = DIM_NUM_EDGE_ES(obj_id_ndx);
115     dnumobjatt = DIM_NUM_ATT_IN_ES(obj_id_ndx);
116     vattrbname = VAR_ESATTRIB(obj_id_ndx);
117     break;
118   case EX_FACE_SET:
119     dnumobjent = DIM_NUM_FACE_FS(obj_id_ndx);
120     dnumobjatt = DIM_NUM_ATT_IN_FS(obj_id_ndx);
121     vattrbname = VAR_FSATTRIB(obj_id_ndx);
122     break;
123   case EX_ELEM_SET:
124     dnumobjent = DIM_NUM_ELE_ELS(obj_id_ndx);
125     dnumobjatt = DIM_NUM_ATT_IN_ELS(obj_id_ndx);
126     vattrbname = VAR_ELSATTRIB(obj_id_ndx);
127     break;
128   case EX_NODAL:
129     dnumobjent = DIM_NUM_NODES;
130     dnumobjatt = DIM_NUM_ATT_IN_NBLK;
131     vattrbname = VAR_NATTRIB;
132     break;
133   case EX_EDGE_BLOCK:
134     dnumobjent = DIM_NUM_ED_IN_EBLK(obj_id_ndx);
135     dnumobjatt = DIM_NUM_ATT_IN_EBLK(obj_id_ndx);
136     vattrbname = VAR_EATTRIB(obj_id_ndx);
137     break;
138   case EX_FACE_BLOCK:
139     dnumobjent = DIM_NUM_FA_IN_FBLK(obj_id_ndx);
140     dnumobjatt = DIM_NUM_ATT_IN_FBLK(obj_id_ndx);
141     vattrbname = VAR_FATTRIB(obj_id_ndx);
142     break;
143   case EX_ELEM_BLOCK:
144     dnumobjent = DIM_NUM_EL_IN_BLK(obj_id_ndx);
145     dnumobjatt = DIM_NUM_ATT_IN_BLK(obj_id_ndx);
146     vattrbname = VAR_ATTRIB(obj_id_ndx);
147     break;
148   default:
149     exerrval = 1005;
150     sprintf(errmsg,
151 	    "Internal Error: unrecognized object type in switch: %d in file id %d",
152 	    obj_type,exoid);
153     ex_err("ex_put_one_attr",errmsg,EX_MSG);
154     return (EX_FATAL);              /* number of attributes not defined */
155   }
156 
157   /* inquire id's of previously defined dimensions  */
158   if (ex_get_dimension(exoid, dnumobjent,"entries", &num_entries_this_obj, &temp, "ex_put_one_attr") != NC_NOERR)
159     return EX_FATAL;
160 
161   if (ex_get_dimension(exoid, dnumobjatt,"attributes", &num_attr, &temp, "ex_put_one_attr") != NC_NOERR)
162     return EX_FATAL;
163 
164   if (attrib_index < 1 || attrib_index > (int)num_attr) {
165     exerrval = EX_FATAL;
166     sprintf(errmsg,
167             "Error: Invalid attribute index specified: %d.  Valid range is 1 to %d for %s %"PRId64" in file id %d",
168             attrib_index, (int)num_attr, ex_name_of_object(obj_type), obj_id, exoid);
169     ex_err("ex_put_one_attr",errmsg,exerrval);
170     return (EX_FATAL);
171   }
172 
173   if ((status = nc_inq_varid(exoid, vattrbname, &attrid)) != NC_NOERR) {
174     exerrval = status;
175     sprintf(errmsg,
176 	    "Error: failed to locate attribute variable for %s %"PRId64" in file id %d",
177 	    ex_name_of_object(obj_type),obj_id,exoid);
178     ex_err("ex_put_one_attr",errmsg,exerrval);
179     return (EX_FATAL);
180   }
181 
182   /* write out the attributes  */
183 
184   start[0] = 0;
185   start[1] = attrib_index-1;
186 
187   count[0] = num_entries_this_obj;
188   count[1] = 1;
189 
190   stride[0] = 1;
191   stride[1] = num_attr;
192 
193   if (ex_comp_ws(exoid) == 4) {
194     status = nc_put_vars_float(exoid, attrid, start, count, stride, attrib);
195   } else {
196     status = nc_put_vars_double(exoid, attrid, start, count, stride, attrib);
197   }
198 
199   if (status != NC_NOERR) {
200     exerrval = status;
201     sprintf(errmsg,
202             "Error: failed to put attribute %d for %s %"PRId64" in file id %d",
203             attrib_index, ex_name_of_object(obj_type), obj_id, exoid);
204     ex_err("ex_put_one_attr",errmsg,exerrval);
205     return (EX_FATAL);
206   }
207   return(EX_NOERR);
208 }
209