| 1 |
/* |
| 2 |
* Copyright(C) 1999-2020, 2022, 2024 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 |
* See packages/seacas/LICENSE for details |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "exodusII.h" // for ex_err, etc |
| 10 |
#include "exodusII_int.h" // for EX_FATAL, DIM_NUM_QA, etc |
| 11 |
|
| 12 |
/*! |
| 13 |
\ingroup Utilities |
| 14 |
|
| 15 |
The function ex_put_qa() writes the QA records to the database. Each |
| 16 |
QA record contains four #MAX_STR_LENGTH byte character |
| 17 |
strings. The character strings are: |
| 18 |
|
| 19 |
- the analysis code name |
| 20 |
- the analysis code QA descriptor |
| 21 |
- the analysis date |
| 22 |
- the analysis time |
| 23 |
|
| 24 |
\return In case of an error, ex_put_qa() returns a negative number; a |
| 25 |
warning will return a positive number. Possible causes of errors |
| 26 |
include: |
| 27 |
- data file not properly opened with call to ex_create() or ex_open() |
| 28 |
- data file opened for read only. |
| 29 |
- QA records already exist in file. |
| 30 |
|
| 31 |
\param[in] exoid exodus file ID returned from a previous call to |
| 32 |
ex_create() or ex_open(). |
| 33 |
|
| 34 |
\param[in] num_qa_records The number of QA records. |
| 35 |
\param[in] qa_record Array containing the QA records. To only |
| 36 |
define the number of qa records instead of |
| 37 |
defining and outputting, pass NULL for |
| 38 |
qa_record argument. |
| 39 |
|
| 40 |
The following code segment will write out two QA records: |
| 41 |
|
| 42 |
~~~{.c} |
| 43 |
int num_qa_rec, error, exoid; |
| 44 |
char *qa_record[2][4]; |
| 45 |
|
| 46 |
\comment{write QA records} |
| 47 |
num_qa_rec = 2; |
| 48 |
|
| 49 |
qa_record[0][0] = "TESTWT1"; |
| 50 |
qa_record[0][1] = "testwt1"; |
| 51 |
qa_record[0][2] = "07/07/93"; |
| 52 |
qa_record[0][3] = "15:41:33"; |
| 53 |
qa_record[1][0] = "FASTQ"; |
| 54 |
qa_record[1][1] = "fastq"; |
| 55 |
qa_record[1][2] = "07/07/93"; |
| 56 |
qa_record[1][3] = "16:41:33"; |
| 57 |
|
| 58 |
error = ex_put_qa (exoid, num_qa_rec, qa_record); |
| 59 |
~~~ |
| 60 |
|
| 61 |
*/ |
| 62 |
|
| 63 |
int ex_put_qa(int exoid, int num_qa_records, char *qa_record[][4]) |
| 64 |
{ |
| 65 |
int status; |
| 66 |
int i, j, strdim, num_qa_dim, varid, n4dim; |
| 67 |
int dims[3]; |
| 68 |
size_t start[3], count[3]; |
| 69 |
char errmsg[MAX_ERR_LENGTH]; |
| 70 |
|
| 71 |
EX_FUNC_ENTER(); |
| 72 |
int rootid = exoid & EX_FILE_ID_MASK; |
| 73 |
|
| 74 |
if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) { |
| 75 |
EX_FUNC_LEAVE(EX_FATAL); |
| 76 |
} |
| 77 |
|
| 78 |
/* only do this if there are records */ |
| 79 |
|
| 80 |
if (num_qa_records > 0) { |
| 81 |
/* See if the number of qa records has already been defined. |
| 82 |
Assume that if the DIM_NUM_QA dimension exists, then the |
| 83 |
VAR_QA_TITLE variable also exists... |
| 84 |
*/ |
| 85 |
status = nc_inq_dimid(rootid, DIM_NUM_QA, &num_qa_dim); |
| 86 |
if (status != NC_NOERR) { |
| 87 |
|
| 88 |
/* put file into define mode */ |
| 89 |
if ((status = exi_redef(rootid, __func__)) != NC_NOERR) { |
| 90 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", |
| 91 |
rootid); |
| 92 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 93 |
EX_FUNC_LEAVE(EX_FATAL); |
| 94 |
} |
| 95 |
|
| 96 |
/* define dimensions */ |
| 97 |
if ((status = nc_def_dim(rootid, DIM_NUM_QA, num_qa_records, &num_qa_dim)) != NC_NOERR) { |
| 98 |
if (status == NC_ENAMEINUSE) { /* duplicate entry? */ |
| 99 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: qa records already exist in file id %d", rootid); |
| 100 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 101 |
} |
| 102 |
else { |
| 103 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 104 |
"ERROR: failed to define qa record array size in file id %d", rootid); |
| 105 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 106 |
} |
| 107 |
|
| 108 |
goto error_ret; /* exit define mode and return */ |
| 109 |
} |
| 110 |
|
| 111 |
/* create number "4" dimension; must be of type long */ |
| 112 |
if ((status = nc_def_dim(rootid, DIM_N4, 4L, &n4dim)) != NC_NOERR) { |
| 113 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 114 |
"ERROR: failed to define number \"4\" dimension in file id %d", rootid); |
| 115 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 116 |
goto error_ret; /* exit define mode and return */ |
| 117 |
} |
| 118 |
|
| 119 |
/* create string length dimension -- only used for QA records */ |
| 120 |
if ((status = nc_def_dim(rootid, DIM_STR, (MAX_STR_LENGTH + 1), &strdim)) != NC_NOERR) { |
| 121 |
if (status == NC_ENAMEINUSE) { /* already defined */ |
| 122 |
nc_inq_dimid(rootid, DIM_STR, &strdim); |
| 123 |
} |
| 124 |
else { |
| 125 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define string length in file id %d", |
| 126 |
rootid); |
| 127 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 128 |
goto error_ret; /* exit define mode and return */ |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/* define variable */ |
| 133 |
dims[0] = num_qa_dim; |
| 134 |
dims[1] = n4dim; |
| 135 |
dims[2] = strdim; |
| 136 |
|
| 137 |
if ((status = nc_def_var(rootid, VAR_QA_TITLE, NC_CHAR, 3, dims, &varid)) != NC_NOERR) { |
| 138 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to define qa record array in file id %d", |
| 139 |
rootid); |
| 140 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 141 |
goto error_ret; /* exit define mode and return */ |
| 142 |
} |
| 143 |
/* In parallel, only rank=0 will write the QA records. |
| 144 |
* Should be able to take advantage of HDF5 handling identical data on all ranks |
| 145 |
* or use the compact storage, but we had issues on some NFS filesystems and some |
| 146 |
* compilers/mpi so are doing it this way... |
| 147 |
*/ |
| 148 |
#if defined(PARALLEL_AWARE_EXODUS) |
| 149 |
if (exi_is_parallel(rootid)) { |
| 150 |
nc_var_par_access(rootid, varid, NC_INDEPENDENT); |
| 151 |
} |
| 152 |
#endif |
| 153 |
|
| 154 |
/* leave define mode */ |
| 155 |
if ((status = exi_leavedef(rootid, __func__)) != NC_NOERR) { |
| 156 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode"); |
| 157 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 158 |
EX_FUNC_LEAVE(EX_FATAL); |
| 159 |
} |
| 160 |
} |
| 161 |
else { |
| 162 |
if ((status = nc_inq_varid(rootid, VAR_QA_TITLE, &varid)) != NC_NOERR) { |
| 163 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to find qa records variable in file id %d", |
| 164 |
rootid); |
| 165 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 166 |
EX_FUNC_LEAVE(EX_FATAL); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
if (qa_record != NULL) { |
| 171 |
/* write out QA records */ |
| 172 |
|
| 173 |
for (i = 0; i < num_qa_records; i++) { |
| 174 |
for (j = 0; j < 4; j++) { |
| 175 |
start[0] = i; |
| 176 |
start[1] = j; |
| 177 |
start[2] = 0; |
| 178 |
|
| 179 |
count[0] = 1; |
| 180 |
count[1] = 1; |
| 181 |
count[2] = strlen(qa_record[i][j]) + 1; |
| 182 |
|
| 183 |
if ((status = nc_put_vara_text(rootid, varid, start, count, qa_record[i][j])) != |
| 184 |
NC_NOERR) { |
| 185 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store qa record in file id %d", |
| 186 |
rootid); |
| 187 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 188 |
EX_FUNC_LEAVE(EX_FATAL); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
/* PnetCDF applies setting to entire file, so put back to collective... */ |
| 194 |
#if defined(PARALLEL_AWARE_EXODUS) |
| 195 |
if (exi_is_parallel(rootid)) { |
| 196 |
nc_var_par_access(rootid, varid, NC_COLLECTIVE); |
| 197 |
} |
| 198 |
#endif |
| 199 |
} |
| 200 |
EX_FUNC_LEAVE(EX_NOERR); |
| 201 |
|
| 202 |
/* Fatal error: exit definition mode and return */ |
| 203 |
error_ret: |
| 204 |
exi_leavedef(rootid, __func__); |
| 205 |
EX_FUNC_LEAVE(EX_FATAL); |
| 206 |
} |