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  * 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"     // for ex_err, etc
54 #include "exodusII_int.h" // for ex_comp_ws, EX_FATAL, etc
55 #include "vtk_netcdf.h"       // for NC_NOERR, etc
56 #include <stddef.h>       // for size_t
57 #include <stdio.h>
58 
59 /*!
60  Internal function. Do not use in client code.
61  */
62 
ex_get_glob_vars_int(int exoid,int time_step,int num_glob_vars,void * glob_var_vals)63 int ex_get_glob_vars_int(int exoid, int time_step, int num_glob_vars, void *glob_var_vals)
64 {
65   int    varid;
66   int    status;
67   size_t start[2], count[2];
68   char   errmsg[MAX_ERR_LENGTH];
69 
70   EX_FUNC_ENTER();
71   ex_check_valid_file_id(exoid, __func__);
72 
73   /* inquire previously defined variable */
74   if ((status = nc_inq_varid(exoid, VAR_GLO_VAR, &varid)) != NC_NOERR) {
75     snprintf(errmsg, MAX_ERR_LENGTH, "Warning: failed to locate global variables in file id %d",
76              exoid);
77     ex_err(__func__, errmsg, status);
78     EX_FUNC_LEAVE(EX_WARN);
79   }
80 
81   /* Verify that time_step is within bounds */
82   {
83     int num_time_steps = ex_inquire_int(exoid, EX_INQ_TIME);
84     if (time_step <= 0 || time_step > num_time_steps) {
85       snprintf(errmsg, MAX_ERR_LENGTH,
86                "ERROR: time_step is out-of-range. Value = %d, valid "
87                "range is 1 to %d in file id %d",
88                time_step, num_time_steps, exoid);
89       ex_err(__func__, errmsg, EX_BADPARAM);
90       EX_FUNC_LEAVE(EX_FATAL);
91     }
92   }
93 
94   /* read values of global variables */
95   start[0] = --time_step;
96   start[1] = 0;
97 
98   count[0] = 1;
99   count[1] = num_glob_vars;
100 
101   if (ex_comp_ws(exoid) == 4) {
102     status = nc_get_vara_float(exoid, varid, start, count, glob_var_vals);
103   }
104   else {
105     status = nc_get_vara_double(exoid, varid, start, count, glob_var_vals);
106   }
107 
108   if (status != NC_NOERR) {
109     snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get global variable values from file id %d",
110              exoid);
111     ex_err(__func__, errmsg, status);
112     EX_FUNC_LEAVE(EX_FATAL);
113   }
114   EX_FUNC_LEAVE(EX_NOERR);
115 }
116