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_time() reads the time value for a specified time
43 step.
44 
45 Because time values are floating point values, the application code
46 must declare the array passed to be the appropriate type (\c float or
47 \c double) to match the compute word size passed in ex_create() or
48 ex_open().
49 
50 \return In case of an error, ex_get_time() returns a negative number; a
51 warning will return a positive number. Possible causes of errors
52 include:
53   -  data file not properly opened with call to ex_create() or ex_open()
54   -  no time steps have been stored in the file.
55 
56 
57 \param[in]  exoid       exodus file ID returned from a previous call to ex_create() or ex_open().
58 \param[in]  time_step   The time step number. This is essentially an index (in the time
59                         dimension) into the global, nodal, and element variables arrays stored
60 			in the database. The first time step is 1.
61 \param[out] time_value  Returned time at the specified time step.
62 
63 As an example, the following coding will read the time value stored in
64 the data file for time step n:
65 
66 \code
67 int n, error, exoid;
68 float time_value;
69 
70 \comment{read time value at time step 3}
71 n = 3;
72 error = ex_get_time (exoid, n, &time_value);
73 \endcode
74 
75 */
76 
ex_get_time(int exoid,int time_step,void * time_value)77 int ex_get_time (int   exoid,
78                  int   time_step,
79                  void *time_value)
80 {
81    int status;
82    int varid;
83    size_t start[1];
84    char errmsg[MAX_ERR_LENGTH];
85 
86    exerrval = 0;        /* clear error code */
87 
88    /* inquire previously defined variable */
89    if ((status = nc_inq_varid(exoid, VAR_WHOLE_TIME, &varid)) != NC_NOERR) {
90      exerrval = status;
91      sprintf(errmsg,
92             "Error: failed to locate time variable in file id %d", exoid);
93      ex_err("ex_get_time",errmsg,exerrval);
94      return (EX_FATAL);
95    }
96 
97    /* read time value */
98    start[0] = --time_step;
99 
100    if (ex_comp_ws(exoid) == 4) {
101      status = nc_get_var1_float(exoid, varid, start, time_value);
102    } else {
103      status = nc_get_var1_double(exoid, varid, start, time_value);
104    }
105 
106    if (status != NC_NOERR) {
107      exerrval = status;
108      sprintf(errmsg,
109             "Error: failed to get time value in file id %d", exoid);
110      ex_err("ex_get_time",errmsg,exerrval);
111      return (EX_FATAL);
112    }
113    return (EX_NOERR);
114 }
115