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 * exgvp - ex_get_variable_param
38 *
39 * entry conditions -
40 *   input parameters:
41 *       int     exoid                   exodus file id
42 *       int     obj_type                variable type
43 *
44 * exit conditions -
45 *       int*    num_vars                number of variables in database
46 *
47 * revision history -
48 *
49 *
50 *****************************************************************************/
51 
52 #include "exodusII.h"
53 #include "exodusII_int.h"
54 
55 #include <ctype.h>
56 
57 /*!
58 
59 The function ex_get_variable_param() reads the number of global,
60 nodal, or element variables stored in the database.
61 
62 \return In case of an error, ex_get_variable_param() returns a negative
63 number; a warning will return a positive number. Possible causes of
64 errors include:
65   -  data file not properly opened with call to ex_create() or ex_open()
66   -  invalid variable type specified.
67 
68 \param[in]  exoid     exodus file ID returned from a previous call to ex_create() or ex_open().
69 \param[in]  obj_type  Variable indicating the type of variable which is described. Use one
70                       of the options in the table below.
71 \param[out] num_vars  Returned number of \c var_type variables that are stored in the database.
72 
73 <table>
74 <tr><td> \c EX_GLOBAL}    </td><td>  Global entity type       </td></tr>
75 <tr><td> \c EX_NODAL}     </td><td>  Nodal entity type        </td></tr>
76 <tr><td> \c EX_NODE_SET   </td><td>  Node Set entity type     </td></tr>
77 <tr><td> \c EX_EDGE_BLOCK </td><td>  Edge Block entity type   </td></tr>
78 <tr><td> \c EX_EDGE_SET   </td><td>  Edge Set entity type     </td></tr>
79 <tr><td> \c EX_FACE_BLOCK </td><td>  Face Block entity type   </td></tr>
80 <tr><td> \c EX_FACE_SET   </td><td>  Face Set entity type     </td></tr>
81 <tr><td> \c EX_ELEM_BLOCK </td><td>  Element Block entity type</td></tr>
82 <tr><td> \c EX_ELEM_SET   </td><td>  Element Set entity type  </td></tr>
83 <tr><td> \c EX_SIDE_SET   </td><td>  Side Set entity type     </td></tr>
84 </table>
85 
86 As an example, the following coding will determine the number of
87 global variables stored in the data file:
88 
89 \code
90 int num_glo_vars, error, exoid;
91 
92 \comment{read global variables parameters}
93 error = ex_get_variable_param(exoid, EX_GLOBAL, &num_glo_vars);
94 \endcode
95 
96 */
97 
ex_get_variable_param(int exoid,ex_entity_type obj_type,int * num_vars)98 int ex_get_variable_param (int   exoid,
99 			   ex_entity_type obj_type,
100 			   int  *num_vars)
101 {
102   int dimid;
103   size_t dimlen;
104   char errmsg[MAX_ERR_LENGTH];
105   const char* dnumvar;
106   int status;
107 
108   exerrval = 0; /* clear error code */
109   *num_vars = 0;
110 
111   switch (obj_type) {
112   case EX_GLOBAL:
113     dnumvar = DIM_NUM_GLO_VAR;
114     break;
115   case EX_NODAL:
116     dnumvar = DIM_NUM_NOD_VAR;
117     break;
118   case EX_EDGE_BLOCK:
119     dnumvar = DIM_NUM_EDG_VAR;
120     break;
121   case EX_FACE_BLOCK:
122     dnumvar = DIM_NUM_FAC_VAR;
123     break;
124   case EX_ELEM_BLOCK:
125     dnumvar = DIM_NUM_ELE_VAR;
126     break;
127   case EX_NODE_SET:
128     dnumvar = DIM_NUM_NSET_VAR;
129     break;
130   case EX_EDGE_SET:
131     dnumvar = DIM_NUM_ESET_VAR;
132     break;
133   case EX_FACE_SET:
134     dnumvar = DIM_NUM_FSET_VAR;
135     break;
136   case EX_SIDE_SET:
137     dnumvar = DIM_NUM_SSET_VAR;
138     break;
139   case EX_ELEM_SET:
140     dnumvar = DIM_NUM_ELSET_VAR;
141     break;
142   default:
143     exerrval = EX_BADPARAM;
144     sprintf(errmsg,
145             "Warning: invalid variable type %d requested from file id %d",
146             obj_type, exoid);
147     ex_err("ex_get_var_param",errmsg,exerrval);
148     return (EX_WARN);
149   }
150 
151   if ((status = nc_inq_dimid (exoid, dnumvar, &dimid)) != NC_NOERR) {
152     *num_vars = 0;
153     if (status == NC_EBADDIM)
154       return(EX_NOERR);      /* no global variables defined */
155     else {
156       exerrval = status;
157       sprintf(errmsg,
158 	      "Error: failed to locate %s variable names in file id %d",
159 	      ex_name_of_object(obj_type),exoid);
160       ex_err("ex_get_var_param",errmsg,exerrval);
161       return (EX_FATAL);
162     }
163   }
164 
165   if ((status = nc_inq_dimlen(exoid, dimid, &dimlen)) != NC_NOERR) {
166     exerrval = status;
167     sprintf(errmsg,
168 	    "Error: failed to get number of %s variables in file id %d",
169 	    ex_name_of_object(obj_type),exoid);
170     ex_err("ex_get_var_param",errmsg,exerrval);
171     return (EX_FATAL);
172   }
173   *num_vars = dimlen;
174 
175   return(EX_NOERR);
176 }
177