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, ex_trim_internal, etc
38 #include "vtk_netcdf.h"       // for NC_NOERR, nc_get_vara_text, etc
39 #include <stddef.h>       // for size_t
40 #include <stdio.h>
41 
42 /*!
43 The function ex_get_qa() reads the QA records from the database. Each
44 QA record contains four MAX_STR_LENGTH-byte character
45 strings. The character strings are:
46  -  the analysis code name
47  -  the analysis code QA descriptor
48  -  the analysis date
49  -  the analysis time
50 
51 Memory must be allocated for the QA records before this call is
52 made. The number of QA records can be determined by invoking
53 ex_inquire().
54 
55 \return In case of an error, ex_get_qa() returns a negative number; a
56         warning will return a positive number.  Possible causes of errors
57         include:
58   -  data file not properly opened with call to ex_create() or ex_open()
59   -  a warning value is returned if no QA records were stored.
60 
61 \param[in] exoid          exodus file ID returned from a previous call to
62 ex_create() or ex_open().
63 \param[out]  qa_record    Returned array containing the QA records.
64 
65 The following will determine the number of QA records and
66 read them from the open exodus file:
67 
68 ~~~{.c}
69 int num_qa_rec, error, exoid
70 char *qa_record[MAX_QA_REC][4];
71 
72 \comment{read QA records}
73 num_qa_rec = ex_inquire_int(exoid, EX_INQ_QA);
74 
75 for (i=0; i<num_qa_rec; i++) {
76     for (j=0; j<4; j++)
77     qa_record[i][j] = (char *) calloc ((MAX_STR_LENGTH+1), sizeof(char));
78 }
79 error = ex_get_qa (exoid, qa_record);
80 ~~~
81 
82  */
83 
ex_get_qa(int exoid,char * qa_record[][4])84 int ex_get_qa(int exoid, char *qa_record[][4])
85 {
86   int    status;
87   int    dimid, varid;
88   size_t i, j;
89   size_t num_qa_records, start[3], count[3];
90 
91   char errmsg[MAX_ERR_LENGTH];
92 
93   int rootid = exoid & EX_FILE_ID_MASK;
94 
95   EX_FUNC_ENTER();
96   ex_check_valid_file_id(exoid, __func__);
97 
98   /* inquire previously defined dimensions and variables  */
99   if ((status = nc_inq_dimid(rootid, DIM_NUM_QA, &dimid)) != NC_NOERR) {
100     snprintf(errmsg, MAX_ERR_LENGTH, "Warning: no qa records stored in file id %d", rootid);
101     ex_err(__func__, errmsg, status);
102     EX_FUNC_LEAVE(EX_WARN);
103   }
104 
105   if ((status = nc_inq_dimlen(rootid, dimid, &num_qa_records)) != NC_NOERR) {
106     snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get number of qa records in file id %d",
107              rootid);
108     ex_err(__func__, errmsg, status);
109     EX_FUNC_LEAVE(EX_FATAL);
110   }
111 
112   /* do this only if there are any QA records */
113   if (num_qa_records > 0) {
114     if ((status = nc_inq_varid(rootid, VAR_QA_TITLE, &varid)) != NC_NOERR) {
115       snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to locate qa record data in file id %d",
116                rootid);
117       ex_err(__func__, errmsg, status);
118       EX_FUNC_LEAVE(EX_FATAL);
119     }
120 
121     /* read the QA records */
122     for (i = 0; i < num_qa_records; i++) {
123       for (j = 0; j < 4; j++) {
124         start[0] = i;
125         count[0] = 1;
126         start[1] = j;
127         count[1] = 1;
128         start[2] = 0;
129         count[2] = MAX_STR_LENGTH + 1;
130         if ((status = nc_get_vara_text(rootid, varid, start, count, qa_record[i][j])) != NC_NOERR) {
131           snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get qa record data in file id %d",
132                    rootid);
133           ex_err(__func__, errmsg, status);
134           EX_FUNC_LEAVE(EX_FATAL);
135         }
136         qa_record[i][j][MAX_STR_LENGTH] = '\0';
137         ex_trim_internal(qa_record[i][j]);
138       }
139     }
140   }
141   EX_FUNC_LEAVE(EX_NOERR);
142 }
143