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 #include "exodusII.h"     // for ex_err, etc
37 #include "exodusII_int.h" // for EX_FATAL, VAR_WHOLE_TIME, etc
38 #include "vtk_netcdf.h"       // for NC_NOERR, nc_get_var_double, etc
39 #include <stdio.h>
40 
41 /*!
42 
43 The function ex_get_all_times() reads the time values for all time
44 steps. Memory must be allocated for the time values array before this
45 function is invoked. The storage requirements (equal to the number of
46 time steps) can be determined by using the ex_inquire() or
47 ex_inquire_int() routines.
48 
49 Because time values are floating point values, the application code
50 must declare the array passed to be the appropriate type (float or
51 double) to match the compute word size passed in ex_create() or
52 ex_open().
53 
54 \return In case of an error, ex_get_all_times() returns a negative
55 number; a warning will return a positive number. Possible causes of
56 errors include:
57   -  data file not properly opened with call to ex_create() or ex_open()
58   -  no time steps have been stored in the file.
59 
60 \param[in]   exoid        exodus file ID returned from a previous call to
61 ex_create() or ex_open().
62 \param[out]  time_values  Returned array of times. These are the time values at
63 all time steps.
64 
65 The following code segment will read the time values for all time
66 steps stored in the data file:
67 
68 ~~~{.c}
69 int error, exoid, num_time_steps;
70 float *time_values;
71 
72 \comment{determine how many time steps are stored}
73 num_time_steps = ex_inquire_int(exoid, EX_INQ_TIME);
74 
75 \comment{read time values at all time steps}
76 time_values = (float *) calloc(num_time_steps, sizeof(float));
77 
78 error = ex_get_all_times(exoid, time_values);
79 ~~~
80 
81 */
82 
ex_get_all_times(int exoid,void * time_values)83 int ex_get_all_times(int exoid, void *time_values)
84 {
85   int  varid;
86   int  status;
87   char errmsg[MAX_ERR_LENGTH];
88 
89   EX_FUNC_ENTER();
90   ex_check_valid_file_id(exoid, __func__);
91 
92   if ((status = nc_inq_varid(exoid, VAR_WHOLE_TIME, &varid)) != NC_NOERR) {
93     snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate time variable %s in file id %d",
94              VAR_WHOLE_TIME, exoid);
95     ex_err(__func__, errmsg, status);
96     EX_FUNC_LEAVE(EX_FATAL);
97   }
98 
99   /*read time values */
100   if (ex_comp_ws(exoid) == 4) {
101     status = nc_get_var_float(exoid, varid, time_values);
102   }
103   else {
104     status = nc_get_var_double(exoid, varid, time_values);
105   }
106 
107   if (status != NC_NOERR) {
108     snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get time values from file id %d", exoid);
109     ex_err(__func__, errmsg, status);
110     EX_FUNC_LEAVE(EX_FATAL);
111   }
112 
113   EX_FUNC_LEAVE(EX_NOERR);
114 }
115