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 * exgpa - ex_get_prop_array: read object property array
38 *
39 * entry conditions -
40 *   input parameters:
41 *       int     exoid                   exodus file id
42 *       int     obj_type                type of object (element block, node
43 *                                               set or side set)
44 *       char*   prop_name               name of the property for which the
45 *                                               values will be read
46 *
47 * exit conditions -
48 *       int*    values                  returned array of property values
49 *
50 * revision history -
51 *
52 *
53 *****************************************************************************/
54 
55 #include <stdlib.h>
56 #include <string.h>
57 #include "exodusII.h"
58 #include "exodusII_int.h"
59 
60 /*!
61 
62 The function ex_get_prop_array() reads an array of integer property
63 values for all element blocks, node sets, or side sets. The order of
64 the values in the array correspond to the order in which the element
65 blocks, node sets, or side sets were introduced into the file. Before
66 this function is invoked, memory must be allocated for the returned
67 array of(\c num_elem_blk, \c num_node_sets, or {num_side_sets})
68 integer values.
69 
70 
71 This function can be used in place of
72  - ex_get_elem_blk_ids(),
73  - ex_get_node_set_ids(), and
74  - ex_get_side_set_ids()
75 to get element block, node set, and side set IDs, respectively, by
76 requesting the property name \b ID. One should also note that this
77 same function can be accomplished with multiple calls to
78 ex_get_prop().
79 
80 \return In case of an error, ex_get_prop_array() returns a negative
81 number; a warning will return a positive number.  Possible causes of
82 errors include:
83   -  data file not properly opened with call to ex_create() or ex_open()
84   -  invalid object type specified.
85   -  a warning value is returned if a property with the specified name is not found.
86 
87 \param[in]  exoid      exodus file ID returned from a previous call to ex_create() or ex_open().
88 \param[in]  obj_type   Type of object; use one of the options in the table below.
89 \param[in]  prop_name  The name of the property (maximum length of \p MAX_STR_LENGTH )
90                        for which the values are desired.
91 \param[out]  values    Returned array of property values.
92 
93 <table>
94 <tr><td> \c EX_NODE_SET   </td><td>  Node Set entity type     </td></tr>
95 <tr><td> \c EX_EDGE_BLOCK </td><td>  Edge Block entity type   </td></tr>
96 <tr><td> \c EX_EDGE_SET   </td><td>  Edge Set entity type     </td></tr>
97 <tr><td> \c EX_FACE_BLOCK </td><td>  Face Block entity type   </td></tr>
98 <tr><td> \c EX_FACE_SET   </td><td>  Face Set entity type     </td></tr>
99 <tr><td> \c EX_ELEM_BLOCK </td><td>  Element Block entity type</td></tr>
100 <tr><td> \c EX_ELEM_SET   </td><td>  Element Set entity type  </td></tr>
101 <tr><td> \c EX_SIDE_SET   </td><td>  Side Set entity type     </td></tr>
102 <tr><td> \c EX_ELEM_MAP   </td><td>  Element Map entity type  </td></tr>
103 <tr><td> \c EX_NODE_MAP   </td><td>  Node Map entity type     </td></tr>
104 <tr><td> \c EX_EDGE_MAP   </td><td>  Edge Map entity type     </td></tr>
105 <tr><td> \c EX_FACE_MAP   </td><td>  Face Map entity type     </td></tr>
106 </table>
107 
108 For an example of code to read an array of object properties, refer to
109 the description for ex_get_prop_names().
110 */
111 
ex_get_prop_array(int exoid,ex_entity_type obj_type,const char * prop_name,int * values)112 int ex_get_prop_array (int   exoid,
113                        ex_entity_type obj_type,
114                        const char *prop_name,
115                        int  *values)
116 {
117    int num_props, i, propid, status;
118    int found = FALSE;
119    char name[MAX_VAR_NAME_LENGTH+1];
120    char tmpstr[MAX_STR_LENGTH+1];
121 
122    char errmsg[MAX_ERR_LENGTH];
123 
124    exerrval  = 0; /* clear error code */
125 
126 /* open appropriate variable, depending on obj_type and prop_name */
127 
128    num_props = ex_get_num_props(exoid, obj_type);
129 
130    for (i=1; i<=num_props; i++)
131    {
132      switch (obj_type){
133        case EX_ELEM_BLOCK:
134          strcpy (name, VAR_EB_PROP(i));
135          break;
136        case EX_EDGE_BLOCK:
137          strcpy (name, VAR_ED_PROP(i));
138          break;
139        case EX_FACE_BLOCK:
140          strcpy (name, VAR_FA_PROP(i));
141          break;
142        case EX_NODE_SET:
143          strcpy (name, VAR_NS_PROP(i));
144          break;
145        case EX_EDGE_SET:
146          strcpy (name, VAR_ES_PROP(i));
147          break;
148        case EX_FACE_SET:
149          strcpy (name, VAR_FS_PROP(i));
150          break;
151        case EX_ELEM_SET:
152          strcpy (name, VAR_ELS_PROP(i));
153          break;
154        case EX_SIDE_SET:
155          strcpy (name, VAR_SS_PROP(i));
156          break;
157        case EX_ELEM_MAP:
158          strcpy (name, VAR_EM_PROP(i));
159          break;
160        case EX_FACE_MAP:
161          strcpy (name, VAR_FAM_PROP(i));
162          break;
163        case EX_EDGE_MAP:
164          strcpy (name, VAR_EDM_PROP(i));
165          break;
166        case EX_NODE_MAP:
167          strcpy (name, VAR_NM_PROP(i));
168          break;
169        default:
170          exerrval = EX_BADPARAM;
171          sprintf(errmsg, "Error: object type %d not supported; file id %d",
172            obj_type, exoid);
173          ex_err("ex_get_prop_array",errmsg,exerrval);
174          return(EX_FATAL);
175      }
176 
177      if ((status = nc_inq_varid(exoid, name, &propid)) != NC_NOERR) {
178        exerrval = status;
179        sprintf(errmsg,
180           "Error: failed to locate property array %s in file id %d",
181                name, exoid);
182        ex_err("ex_get_prop_array",errmsg,exerrval);
183        return (EX_FATAL);
184      }
185 
186      /*   compare stored attribute name with passed property name   */
187      memset(tmpstr, 0, MAX_STR_LENGTH+1);
188      if ((status = nc_get_att_text(exoid, propid, ATT_PROP_NAME, tmpstr)) != NC_NOERR) {
189        exerrval = status;
190        sprintf(errmsg,
191               "Error: failed to get property name in file id %d", exoid);
192        ex_err("ex_get_prop_array",errmsg,exerrval);
193        return (EX_FATAL);
194      }
195 
196      if (strcmp(tmpstr, prop_name) == 0) {
197        found = TRUE;
198        break;
199      }
200    }
201 
202    /* if property is not found, return warning */
203    if (!found) {
204      exerrval = EX_BADPARAM;
205      sprintf(errmsg,
206        "Warning: object type %d, property %s not defined in file id %d",
207         obj_type, prop_name, exoid);
208      ex_err("ex_get_prop_array",errmsg,exerrval);
209      return (EX_WARN);
210    }
211 
212    /* read num_obj values from property variable */
213    status = nc_get_var_int(exoid, propid, values);
214 
215    if (status != NC_NOERR) {
216      exerrval = status;
217      sprintf(errmsg,
218             "Error: failed to read values in %s property array in file id %d",
219              ex_name_of_object(obj_type), exoid);
220      ex_err("ex_get_prop_array",errmsg,exerrval);
221      return (EX_FATAL);
222    }
223 
224    return (EX_NOERR);
225 }
226