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 * exggv - ex_get_glob_vars
38 *
39 * entry conditions -
40 *   input parameters:
41 *       int     exoid                   exodus file id
42 *       int     time_step               time step number
43 *       int     num_glob_vars           number of global vars in file
44 *
45 * exit conditions -
46 *       float*  glob_var_vals           array of global variable values
47 *
48 * revision history -
49 *
50 *
51 *****************************************************************************/
52 
53 #include "exodusII.h"
54 #include "exodusII_int.h"
55 
56 /*!
57 \ingroup ResultsData
58 
59 The function ex_get_glob_vars() reads the values of all the
60 global variables for a single time step. Memory must be allocated for
61 the global variables values array before this function is invoked.
62 
63 Because global variables are floating point values, the application
64 code must declare the array passed to be the appropriate type
65 (float or double) to match the compute word size passed in
66 ex_create() or ex_open().
67 
68 In case of an error, ex_get_glob_vars() returns a negative
69 number; a warning will return a positive number. Possible causes of
70 errors include:
71 
72  - data file not properly opened with call to ex_create() or ex_open()
73  - no global variables stored in the file.
74  - a warning value is returned if no global variables are stored in the file.
75 
76 \param[in] exoid        exodus file ID returned from a previous call to ex_create() or ex_open().
77 
78 \param[in] time_step    The time step, as described under ex_put_time(), at
79                         which the global variable values are desired. This is essentially
80                         an index (in the time dimension) into the global variable values
81                         array stored in the database. The first time step is 1.
82 
83 \param[in]  num_glob_vars The number of global variables stored in the database.
84 \param[out] glob_var_vals Returned array of num_glob_vars global variable values
85                           for the time_step'th time step.
86 
87 The following is an example code segment that reads all the global
88 variables at one time step:
89 
90 \verbatim
91 int num_glo_vars, error, time_step;
92 float *var_values;
93 
94 error = ex_get_variable_param (idexo, EX_GLOBAL, &num_glo_vars);
95 var_values = (float *) calloc (num_glo_vars, sizeof(float));
96 error = ex_get_glob_vars (idexo, time_step, num_glo_vars,
97                           var_values);
98 \endverbatim
99  */
100 
ex_get_glob_vars(int exoid,int time_step,int num_glob_vars,void * glob_var_vals)101 int ex_get_glob_vars (int   exoid,
102                       int   time_step,
103                       int   num_glob_vars,
104                       void *glob_var_vals)
105 {
106    int varid;
107    int status;
108    size_t start[2], count[2];
109    char errmsg[MAX_ERR_LENGTH];
110 
111    exerrval = 0; /* clear error code */
112 
113    /* inquire previously defined variable */
114    if ((status = nc_inq_varid (exoid, VAR_GLO_VAR, &varid)) != NC_NOERR) {
115      exerrval = status;
116      sprintf(errmsg,
117             "Warning: failed to locate global variables in file id %d",
118             exoid);
119      ex_err("ex_get_glob_vars",errmsg,exerrval);
120      return (EX_WARN);
121    }
122 
123    /* read values of global variables */
124    start[0] = --time_step;
125    start[1] = 0;
126 
127    count[0] = 1;
128    count[1] = num_glob_vars;
129 
130    if (ex_comp_ws(exoid) == 4) {
131      status = nc_get_vara_float(exoid, varid, start, count, glob_var_vals);
132    } else {
133      status = nc_get_vara_double(exoid, varid, start, count, glob_var_vals);
134    }
135 
136    if (status != NC_NOERR) {
137      exerrval = status;
138      sprintf(errmsg,
139             "Error: failed to get global variable values from file id %d",
140             exoid);
141      ex_err("ex_get_glob_vars",errmsg,exerrval);
142      return (EX_FATAL);
143    }
144    return (EX_NOERR);
145 }
146