1 /*
2  * Copyright (c) 2005-2017 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  * 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 NTESS 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  * exgvnm - ex_get_variable_name
38  *
39  * entry conditions -
40  *   input parameters:
41  *       int   exoid                   exodus file id
42  *       int   obj_type                variable type
43  *       int   var_num                 variable index to read 1..num_var
44  *
45  * exit conditions -
46  *       char*   var_name                ptr to variable name
47  *
48  * revision history -
49  *
50  *
51  *****************************************************************************/
52 
53 #include "exodusII.h"     // for ex_err, etc
54 #include "exodusII_int.h" // for EX_FATAL, etc
55 #include "vtk_netcdf.h"       // for NC_NOERR, nc_inq_varid
56 #include <stdio.h>
57 
58 /*!
59  * \ingroup ResultsData
60  *
61  * reads the name of a particular results variable from the database
62  */
63 
ex_get_variable_name(int exoid,ex_entity_type obj_type,int var_num,char * var_name)64 int ex_get_variable_name(int exoid, ex_entity_type obj_type, int var_num, char *var_name)
65 {
66   int         status;
67   int         varid;
68   char        errmsg[MAX_ERR_LENGTH];
69   const char *vname = NULL;
70 
71   EX_FUNC_ENTER();
72   ex_check_valid_file_id(exoid, __func__);
73 
74   /* inquire previously defined variables  */
75 
76   switch (obj_type) {
77   case EX_GLOBAL: vname = VAR_NAME_GLO_VAR; break;
78   case EX_NODAL: vname = VAR_NAME_NOD_VAR; break;
79   case EX_EDGE_BLOCK: vname = VAR_NAME_EDG_VAR; break;
80   case EX_FACE_BLOCK: vname = VAR_NAME_FAC_VAR; break;
81   case EX_ELEM_BLOCK: vname = VAR_NAME_ELE_VAR; break;
82   case EX_NODE_SET: vname = VAR_NAME_NSET_VAR; break;
83   case EX_EDGE_SET: vname = VAR_NAME_ESET_VAR; break;
84   case EX_FACE_SET: vname = VAR_NAME_FSET_VAR; break;
85   case EX_SIDE_SET: vname = VAR_NAME_SSET_VAR; break;
86   case EX_ELEM_SET: vname = VAR_NAME_ELSET_VAR; break;
87   default:
88     snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Invalid variable type (%d) given for file id %d",
89              obj_type, exoid);
90     ex_err(__func__, errmsg, EX_BADPARAM);
91     EX_FUNC_LEAVE(EX_FATAL);
92   }
93 
94   if ((status = nc_inq_varid(exoid, vname, &varid)) != NC_NOERR) {
95     snprintf(errmsg, MAX_ERR_LENGTH, "Warning: no %s variable names stored in file id %d",
96              ex_name_of_object(obj_type), exoid);
97     ex_err(__func__, errmsg, status);
98     EX_FUNC_LEAVE(EX_WARN);
99   }
100 
101   /* read the variable name */
102   {
103     int db_name_size  = ex_inquire_int(exoid, EX_INQ_DB_MAX_ALLOWED_NAME_LENGTH);
104     int api_name_size = ex_inquire_int(exoid, EX_INQ_MAX_READ_NAME_LENGTH);
105     int name_size     = db_name_size < api_name_size ? db_name_size : api_name_size;
106 
107     status =
108         ex_get_name_internal(exoid, varid, var_num - 1, var_name, name_size, obj_type, __func__);
109     if (status != NC_NOERR) {
110       EX_FUNC_LEAVE(EX_FATAL);
111     }
112   }
113   EX_FUNC_LEAVE(EX_NOERR);
114 }
115