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