| 1 |
/* |
| 2 |
* Copyright(C) 1999-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_FATAL, exerrval, ex_err, etc |
| 10 |
#include "exodusII_int.h" // for exi_get_counter_list, etc |
| 11 |
|
| 12 |
#define STRINGIFY(x) #x |
| 13 |
#define TOSTRING(x) STRINGIFY(x) |
| 14 |
|
| 15 |
#define EXCHECK(funcall) \ |
| 16 |
if ((funcall) != NC_NOERR) { \ |
| 17 |
fprintf(stderr, "Error calling %s\n", TOSTRING(funcall)); \ |
| 18 |
EX_FUNC_LEAVE(EX_FATAL); \ |
| 19 |
} |
| 20 |
|
| 21 |
#define EXCHECKI(funcall) \ |
| 22 |
if ((funcall) != NC_NOERR) { \ |
| 23 |
fprintf(stderr, "Error calling %s\n", TOSTRING(funcall)); \ |
| 24 |
return EX_FATAL; \ |
| 25 |
} |
| 26 |
|
| 27 |
#define EXCHECKF(funcall) \ |
| 28 |
if ((funcall) != NC_NOERR) { \ |
| 29 |
fprintf(stderr, "Error calling %s\n", TOSTRING(funcall)); \ |
| 30 |
goto err_ret; \ |
| 31 |
} |
| 32 |
|
| 33 |
/*! \cond INTERNAL */ |
| 34 |
struct ncvar |
| 35 |
{ /* variable */ |
| 36 |
char name[MAX_VAR_NAME_LENGTH]; |
| 37 |
nc_type type; |
| 38 |
int ndims; |
| 39 |
int dims[NC_MAX_VAR_DIMS]; |
| 40 |
int natts; |
| 41 |
}; |
| 42 |
|
| 43 |
struct ncatt |
| 44 |
{ /* attribute */ |
| 45 |
char name[MAX_VAR_NAME_LENGTH]; |
| 46 |
nc_type type; |
| 47 |
size_t len; |
| 48 |
}; |
| 49 |
|
| 50 |
static size_t type_size(nc_type type); |
| 51 |
static int cpy_variable_data(int in_exoid, int out_exoid, int in_large, int mesh_only); |
| 52 |
static int cpy_variables(int in_exoid, int out_exoid, int in_large, int mesh_only); |
| 53 |
static int cpy_dimension(int in_exoid, int out_exoid, int mesh_only); |
| 54 |
static int cpy_global_att(int in_exoid, int out_exoid); |
| 55 |
static int cpy_att(int in_id, int out_id, int var_in_id, int var_out_id); |
| 56 |
static int cpy_var_def(int in_id, int out_id, int rec_dim_id, char *var_nm); |
| 57 |
static int cpy_var_val(int in_id, int out_id, char *var_nm); |
| 58 |
static int cpy_coord_def(int in_id, int out_id, int rec_dim_id, char *var_nm, int in_large); |
| 59 |
static int cpy_coord_val(int in_id, int out_id, char *var_nm, int in_large); |
| 60 |
static void update_structs(int out_exoid); |
| 61 |
static void update_internal_structs(int out_exoid, ex_inquiry inqcode, |
| 62 |
struct exi_list_item **ctr_list); |
| 63 |
|
| 64 |
static int is_truth_table_variable(const char *var_name) |
| 65 |
{ |
| 66 |
/* If copying just the "mesh" or "non-transient" portion of the |
| 67 |
* input DB, these are the variables that won't be copied: |
| 68 |
*/ |
| 69 |
return strstr(var_name, "_var_tab") != NULL; |
| 70 |
} |
| 71 |
|
| 72 |
static int is_non_mesh_variable(const char *var_name) |
| 73 |
{ |
| 74 |
/* If copying just the "mesh" or "non-transient" portion of the |
| 75 |
* input DB, these are the variables that won't be copied: |
| 76 |
*/ |
| 77 |
return (strncmp(var_name, "vals_", 5) == 0) || (strncmp(var_name, "name_", 5) == 0); |
| 78 |
} |
| 79 |
/*! \endcond */ |
| 80 |
|
| 81 |
/*! \cond INTERNAL */ |
| 82 |
static int ex_copy_internal(int in_exoid, int out_exoid, int mesh_only) |
| 83 |
{ |
| 84 |
EX_FUNC_ENTER(); |
| 85 |
if (exi_check_valid_file_id(in_exoid, __func__) != EX_NOERR) { |
| 86 |
EX_FUNC_LEAVE(EX_FATAL); |
| 87 |
} |
| 88 |
if (exi_check_valid_file_id(out_exoid, __func__) != EX_NOERR) { |
| 89 |
EX_FUNC_LEAVE(EX_FATAL); |
| 90 |
} |
| 91 |
|
| 92 |
/* |
| 93 |
* Get exodus_large_model setting on both input and output |
| 94 |
* databases so know how to handle coordinates. |
| 95 |
*/ |
| 96 |
int in_large = ex_large_model(in_exoid); |
| 97 |
|
| 98 |
/* |
| 99 |
* Get integer sizes for both input and output databases. |
| 100 |
* Currently they should both match or there will be an error. |
| 101 |
*/ |
| 102 |
if (ex_int64_status(in_exoid) != ex_int64_status(out_exoid)) { |
| 103 |
char errmsg[MAX_ERR_LENGTH]; |
| 104 |
snprintf_nowarn(errmsg, MAX_ERR_LENGTH, |
| 105 |
"ERROR: integer sizes do not match for input and output databases."); |
| 106 |
ex_err_fn(in_exoid, __func__, errmsg, EX_WRONGFILETYPE); |
| 107 |
EX_FUNC_LEAVE(EX_FATAL); |
| 108 |
} |
| 109 |
|
| 110 |
/* put output file into define mode */ |
| 111 |
EXCHECK(exi_redef(out_exoid, __func__)); |
| 112 |
|
| 113 |
/* copy global attributes */ |
| 114 |
EXCHECK(cpy_global_att(in_exoid, out_exoid)); |
| 115 |
|
| 116 |
/* copy dimensions */ |
| 117 |
EXCHECK(cpy_dimension(in_exoid, out_exoid, mesh_only)); |
| 118 |
|
| 119 |
/* copy variable definitions and variable attributes */ |
| 120 |
EXCHECK(cpy_variables(in_exoid, out_exoid, in_large, mesh_only)); |
| 121 |
|
| 122 |
/* take the output file out of define mode */ |
| 123 |
if (exi_leavedef(out_exoid, __func__) != NC_NOERR) { |
| 124 |
EX_FUNC_LEAVE(EX_FATAL); |
| 125 |
} |
| 126 |
|
| 127 |
/* output variable data */ |
| 128 |
EXCHECK(cpy_variable_data(in_exoid, out_exoid, in_large, mesh_only)); |
| 129 |
|
| 130 |
/* ensure internal data structures are updated */ |
| 131 |
update_structs(out_exoid); |
| 132 |
|
| 133 |
ex_update(out_exoid); |
| 134 |
|
| 135 |
EX_FUNC_LEAVE(EX_NOERR); |
| 136 |
} |
| 137 |
/*! \endcond */ |
| 138 |
|
| 139 |
/*! |
| 140 |
\ingroup Utilities |
| 141 |
\undoc |
| 142 |
|
| 143 |
* efficiently copies all non-transient information (attributes, |
| 144 |
* dimensions, and variables from an opened EXODUS file to another |
| 145 |
* opened EXODUS file. Will not overwrite a dimension or variable |
| 146 |
* already defined in the new file. |
| 147 |
* \param in_exoid exodus file id for input file |
| 148 |
* \param out_exoid exodus file id for output file |
| 149 |
*/ |
| 150 |
|
| 151 |
int ex_copy(int in_exoid, int out_exoid) |
| 152 |
{ |
| 153 |
int mesh_only = 1; |
| 154 |
return ex_copy_internal(in_exoid, out_exoid, mesh_only); |
| 155 |
} |
| 156 |
|
| 157 |
/*! |
| 158 |
\ingroup Utilities |
| 159 |
\undoc |
| 160 |
|
| 161 |
* efficiently copies all non-transient and transient information |
| 162 |
* (attributes, dimensions, and variables from an opened EXODUS file |
| 163 |
* to another opened EXODUS file. Will not overwrite a dimension or |
| 164 |
* variable already defined in the new file. |
| 165 |
* \param in_exoid exodus file id for input file |
| 166 |
* \param out_exoid exodus file id for output file |
| 167 |
*/ |
| 168 |
|
| 169 |
int ex_copy_transient(int in_exoid, int out_exoid) |
| 170 |
{ |
| 171 |
int mesh_only = 0; |
| 172 |
return ex_copy_internal(in_exoid, out_exoid, mesh_only); |
| 173 |
} |
| 174 |
|
| 175 |
/*! \cond INTERNAL */ |
| 176 |
static int cpy_variable_data(int in_exoid, int out_exoid, int in_large, int mesh_only) |
| 177 |
{ |
| 178 |
int nvars; /* number of variables */ |
| 179 |
/* NOTE: This is incorrect for files containing groups */ |
| 180 |
EXCHECKI(nc_inq(in_exoid, NULL, &nvars, NULL, NULL)); |
| 181 |
for (int varid = 0; varid < nvars; varid++) { |
| 182 |
bool is_filtered; |
| 183 |
struct ncvar var; /* variable */ |
| 184 |
EXCHECKI(nc_inq_var(in_exoid, varid, var.name, &var.type, &var.ndims, var.dims, &var.natts)); |
| 185 |
if ((strcmp(var.name, VAR_QA_TITLE) == 0) || (strcmp(var.name, VAR_INFO) == 0)) { |
| 186 |
is_filtered = true; |
| 187 |
} |
| 188 |
else if (is_truth_table_variable(var.name)) { |
| 189 |
is_filtered = true; |
| 190 |
} |
| 191 |
else if (mesh_only == 1 && |
| 192 |
(is_non_mesh_variable(var.name) || (strcmp(var.name, VAR_WHOLE_TIME) == 0))) { |
| 193 |
is_filtered = true; |
| 194 |
} |
| 195 |
else if (mesh_only == 0 && |
| 196 |
(!is_non_mesh_variable(var.name) && strcmp(var.name, VAR_WHOLE_TIME) != 0)) { |
| 197 |
is_filtered = true; |
| 198 |
} |
| 199 |
else { |
| 200 |
is_filtered = false; |
| 201 |
} |
| 202 |
|
| 203 |
if (!is_filtered) { |
| 204 |
if (strncmp(var.name, VAR_COORD, 5) == 0) { |
| 205 |
cpy_coord_val(in_exoid, out_exoid, var.name, in_large); |
| 206 |
} |
| 207 |
else { |
| 208 |
cpy_var_val(in_exoid, out_exoid, var.name); |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
return EX_NOERR; |
| 213 |
} |
| 214 |
|
| 215 |
/*! \cond INTERNAL */ |
| 216 |
static int cpy_variables(int in_exoid, int out_exoid, int in_large, int mesh_only) |
| 217 |
{ |
| 218 |
int recdimid; /* id of unlimited dimension */ |
| 219 |
int nvars; /* number of variables */ |
| 220 |
/* NOTE: This is incorrect for files containing groups */ |
| 221 |
EXCHECKI(nc_inq(in_exoid, NULL, &nvars, NULL, &recdimid)); |
| 222 |
for (int varid = 0; varid < nvars; varid++) { |
| 223 |
struct ncvar var; /* variable */ |
| 224 |
EXCHECKI(nc_inq_var(in_exoid, varid, var.name, &var.type, &var.ndims, var.dims, &var.natts)); |
| 225 |
|
| 226 |
bool is_filtered; |
| 227 |
if ((strcmp(var.name, VAR_QA_TITLE) == 0) || (strcmp(var.name, VAR_INFO) == 0)) { |
| 228 |
is_filtered = true; |
| 229 |
} |
| 230 |
else if (is_truth_table_variable(var.name)) { |
| 231 |
is_filtered = true; |
| 232 |
} |
| 233 |
else if (mesh_only == 1 && is_non_mesh_variable(var.name)) { |
| 234 |
is_filtered = true; |
| 235 |
} |
| 236 |
else { |
| 237 |
is_filtered = false; |
| 238 |
} |
| 239 |
|
| 240 |
if (!is_filtered) { |
| 241 |
int var_out_id; /* variable id */ |
| 242 |
if (strncmp(var.name, VAR_COORD, 5) == 0) { |
| 243 |
var_out_id = cpy_coord_def(in_exoid, out_exoid, recdimid, var.name, in_large); |
| 244 |
} |
| 245 |
else { |
| 246 |
var_out_id = cpy_var_def(in_exoid, out_exoid, recdimid, var.name); |
| 247 |
} |
| 248 |
|
| 249 |
/* copy the variable's attributes */ |
| 250 |
cpy_att(in_exoid, out_exoid, varid, var_out_id); |
| 251 |
} |
| 252 |
} |
| 253 |
return EX_NOERR; |
| 254 |
} |
| 255 |
/*! \endcond */ |
| 256 |
|
| 257 |
/*! \cond INTERNAL */ |
| 258 |
static int cpy_dimension(int in_exoid, int out_exoid, int mesh_only) |
| 259 |
{ |
| 260 |
int dim_out_id; /* dimension id */ |
| 261 |
|
| 262 |
int ndims; /* number of dimensions */ |
| 263 |
int recdimid; /* id of unlimited dimension */ |
| 264 |
/* NOTE: This is incorrect for files containing groups */ |
| 265 |
EXCHECKI(nc_inq(in_exoid, &ndims, NULL, NULL, &recdimid)); |
| 266 |
for (int dimid = 0; dimid < ndims; dimid++) { |
| 267 |
|
| 268 |
char dim_nm[NC_MAX_NAME + 1]; |
| 269 |
size_t dim_sz; |
| 270 |
EXCHECK(nc_inq_dim(in_exoid, dimid, dim_nm, &dim_sz)); |
| 271 |
|
| 272 |
/* If the dimension isn't one we specifically don't want |
| 273 |
* to copy (ie, number of QA or INFO records) and it |
| 274 |
* hasn't been defined, copy it */ |
| 275 |
bool is_filtered; |
| 276 |
if (strcmp(dim_nm, DIM_NUM_QA) == 0 || strcmp(dim_nm, DIM_NUM_INFO) == 0 || |
| 277 |
strcmp(dim_nm, DIM_N4) == 0 || strcmp(dim_nm, DIM_STR) == 0 || |
| 278 |
strcmp(dim_nm, DIM_LIN) == 0) { |
| 279 |
is_filtered = true; |
| 280 |
} |
| 281 |
else if (mesh_only == 1 && |
| 282 |
((strcmp(dim_nm, DIM_NUM_NOD_VAR) == 0) || (strcmp(dim_nm, DIM_NUM_EDG_VAR) == 0) || |
| 283 |
(strcmp(dim_nm, DIM_NUM_FAC_VAR) == 0) || (strcmp(dim_nm, DIM_NUM_ELE_VAR) == 0) || |
| 284 |
(strcmp(dim_nm, DIM_NUM_NSET_VAR) == 0) || (strcmp(dim_nm, DIM_NUM_ESET_VAR) == 0) || |
| 285 |
(strcmp(dim_nm, DIM_NUM_FSET_VAR) == 0) || (strcmp(dim_nm, DIM_NUM_SSET_VAR) == 0) || |
| 286 |
(strcmp(dim_nm, DIM_NUM_ELSET_VAR) == 0) || (strcmp(dim_nm, DIM_NUM_GLO_VAR) == 0) || |
| 287 |
(strcmp(dim_nm, DIM_NUM_ASSEMBLY_VAR) == 0) || |
| 288 |
(strcmp(dim_nm, DIM_NUM_BLOB_VAR) == 0) || (strstr(dim_nm, "_red_var") != NULL))) { |
| 289 |
is_filtered = true; |
| 290 |
} |
| 291 |
else { |
| 292 |
is_filtered = false; |
| 293 |
} |
| 294 |
|
| 295 |
if (!is_filtered) { |
| 296 |
/* See if the dimension has already been defined */ |
| 297 |
int status = nc_inq_dimid(out_exoid, dim_nm, &dim_out_id); |
| 298 |
|
| 299 |
if (status != NC_NOERR) { |
| 300 |
if (dimid != recdimid) { |
| 301 |
status = nc_def_dim(out_exoid, dim_nm, dim_sz, &dim_out_id); |
| 302 |
} |
| 303 |
else { |
| 304 |
status = nc_def_dim(out_exoid, dim_nm, NC_UNLIMITED, &dim_out_id); |
| 305 |
} |
| 306 |
if (status != NC_NOERR) { |
| 307 |
char errmsg[MAX_ERR_LENGTH]; |
| 308 |
snprintf_nowarn(errmsg, MAX_ERR_LENGTH, |
| 309 |
"ERROR: failed to define %s dimension in file id %d", dim_nm, out_exoid); |
| 310 |
ex_err_fn(out_exoid, __func__, errmsg, status); |
| 311 |
EX_FUNC_LEAVE(EX_FATAL); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
/* DIM_STR_NAME is a newly added dimension required by current API. |
| 318 |
* If it doesn't exist on the source database, we need to add it to |
| 319 |
* the target... |
| 320 |
*/ |
| 321 |
int status = nc_inq_dimid(in_exoid, DIM_STR_NAME, &dim_out_id); |
| 322 |
if (status != NC_NOERR) { |
| 323 |
/* |
| 324 |
* See if it already exists in the output file |
| 325 |
* (ex_put_init_ext may have been called on the |
| 326 |
* output file prior to calling ex_copy) |
| 327 |
*/ |
| 328 |
status = nc_inq_dimid(out_exoid, DIM_STR_NAME, &dim_out_id); |
| 329 |
if (status != NC_NOERR) { |
| 330 |
/* Not found; set to default value of 32+1. */ |
| 331 |
|
| 332 |
if ((status = nc_def_dim(out_exoid, DIM_STR_NAME, 33, &dim_out_id)) != NC_NOERR) { |
| 333 |
char errmsg[MAX_ERR_LENGTH]; |
| 334 |
snprintf_nowarn(errmsg, MAX_ERR_LENGTH, |
| 335 |
"ERROR: failed to define string name dimension in file id %d", out_exoid); |
| 336 |
ex_err_fn(out_exoid, __func__, errmsg, status); |
| 337 |
EX_FUNC_LEAVE(EX_FATAL); |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
return EX_NOERR; |
| 342 |
} |
| 343 |
|
| 344 |
/*! \cond INTERNAL */ |
| 345 |
static int cpy_global_att(int in_exoid, int out_exoid) |
| 346 |
{ |
| 347 |
struct ncatt att; /* attribute */ |
| 348 |
|
| 349 |
int ngatts; |
| 350 |
/* NOTE: This is incorrect for files containing groups */ |
| 351 |
EXCHECKI(nc_inq(in_exoid, NULL, NULL, &ngatts, NULL)); |
| 352 |
|
| 353 |
/* copy global attributes */ |
| 354 |
for (int i = 0; i < ngatts; i++) { |
| 355 |
|
| 356 |
EXCHECKI(nc_inq_attname(in_exoid, NC_GLOBAL, i, att.name)); |
| 357 |
|
| 358 |
/* if attribute exists in output file, don't overwrite it; compute |
| 359 |
* word size, I/O word size etc. are global attributes stored when |
| 360 |
* file is created with ex_create; we don't want to overwrite those |
| 361 |
*/ |
| 362 |
if (nc_inq_att(out_exoid, NC_GLOBAL, att.name, &att.type, &att.len) != NC_NOERR) { |
| 363 |
|
| 364 |
/* The "last_written_time" attribute is a special attribute used |
| 365 |
by the IOSS library to determine whether a timestep has been |
| 366 |
fully written to the database in order to try to detect a |
| 367 |
database crash that happens in the middle of a database |
| 368 |
output step. Don't want to copy that attribute. |
| 369 |
*/ |
| 370 |
if (strcmp(att.name, "last_written_time") != 0) { |
| 371 |
/* attribute doesn't exist in new file so OK to create it */ |
| 372 |
EXCHECKI(nc_copy_att(in_exoid, NC_GLOBAL, att.name, out_exoid, NC_GLOBAL)); |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
/* If the `ATT_MAX_NAME_LENGTH` attribute exists on input database, |
| 378 |
* add its value to the output database. Won't be set above since |
| 379 |
* it is added during ex_create(), so it already exists in the |
| 380 |
* output database, but with potentially wrong value. |
| 381 |
*/ |
| 382 |
{ |
| 383 |
nc_type att_type = NC_NAT; |
| 384 |
size_t att_len = 0; |
| 385 |
int status = nc_inq_att(in_exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, &att_type, &att_len); |
| 386 |
if (status == NC_NOERR) { |
| 387 |
EXCHECKI(nc_copy_att(in_exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, out_exoid, NC_GLOBAL)); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
return EX_NOERR; |
| 392 |
} |
| 393 |
/*! \endcond */ |
| 394 |
|
| 395 |
/*! \cond INTERNAL */ |
| 396 |
static int cpy_att(int in_id, int out_id, int var_in_id, int var_out_id) |
| 397 |
{ |
| 398 |
/* Routine to copy all the attributes from the input netCDF |
| 399 |
file to the output netCDF file. If var_in_id == NC_GLOBAL, |
| 400 |
then the global attributes are copied. Otherwise the variable's |
| 401 |
attributes are copied. */ |
| 402 |
|
| 403 |
int nbr_att; |
| 404 |
|
| 405 |
if (var_in_id == NC_GLOBAL) { |
| 406 |
EXCHECKI(nc_inq_natts(in_id, &nbr_att)); |
| 407 |
} |
| 408 |
else { |
| 409 |
EXCHECKI(nc_inq_varnatts(in_id, var_in_id, &nbr_att)); |
| 410 |
} |
| 411 |
|
| 412 |
/* Get the attributes names, types, lengths, and values */ |
| 413 |
for (int idx = 0; idx < nbr_att; idx++) { |
| 414 |
char att_nm[MAX_VAR_NAME_LENGTH]; |
| 415 |
|
| 416 |
EXCHECKI(nc_inq_attname(in_id, var_in_id, idx, att_nm)); |
| 417 |
nc_copy_att(in_id, var_in_id, att_nm, out_id, var_out_id); |
| 418 |
} |
| 419 |
|
| 420 |
return EX_NOERR; |
| 421 |
} |
| 422 |
/*! \endcond */ |
| 423 |
|
| 424 |
/*! \internal */ |
| 425 |
static int cpy_coord_def(int in_id, int out_id, int rec_dim_id, char *var_nm, int in_large) |
| 426 |
{ |
| 427 |
/* Handle easiest situation first: in_large matches out_large (1) */ |
| 428 |
if (in_large == 1) { |
| 429 |
return cpy_var_def(in_id, out_id, rec_dim_id, var_nm); /* OK */ |
| 430 |
} |
| 431 |
|
| 432 |
/* At this point, know that in_large != out_large, so only supported |
| 433 |
option is that in_large == 0 and out_large == 1. Also will need |
| 434 |
the spatial dimension, so get that now. |
| 435 |
*/ |
| 436 |
int temp; |
| 437 |
size_t spatial_dim; |
| 438 |
const char *routine = NULL; |
| 439 |
exi_get_dimension(in_id, DIM_NUM_DIM, "dimension", &spatial_dim, &temp, routine); |
| 440 |
|
| 441 |
/* output file will have coordx, coordy, coordz (if 3d). See if |
| 442 |
they are already defined in output file. Assume either all or |
| 443 |
none are defined. */ |
| 444 |
|
| 445 |
{ |
| 446 |
int var_out_idx, var_out_idy, var_out_idz; |
| 447 |
int status1 = nc_inq_varid(out_id, VAR_COORD_X, &var_out_idx); |
| 448 |
int status2 = nc_inq_varid(out_id, VAR_COORD_Y, &var_out_idy); |
| 449 |
int status3 = nc_inq_varid(out_id, VAR_COORD_Y, &var_out_idz); |
| 450 |
|
| 451 |
if (status1 == NC_NOERR && status2 == NC_NOERR && (spatial_dim == 2 || status3 == NC_NOERR)) { |
| 452 |
return NC_NOERR; /* already defined in output file */ /* OK */ |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
/* Get dimid of the num_nodes dimension in output file... */ |
| 457 |
int dim_out_id[2]; |
| 458 |
EXCHECKI(nc_inq_dimid(out_id, DIM_NUM_NODES, &dim_out_id[0])); |
| 459 |
|
| 460 |
/* Define the variables in the output file */ |
| 461 |
|
| 462 |
/* Define according to the EXODUS file's IO_word_size */ |
| 463 |
int nbr_dim = 1; |
| 464 |
int var_out_id = -1; |
| 465 |
EXCHECKI(nc_def_var(out_id, VAR_COORD_X, nc_flt_code(out_id), nbr_dim, dim_out_id, &var_out_id)); |
| 466 |
exi_compress_variable(out_id, var_out_id, 2); |
| 467 |
if (spatial_dim > 1) { |
| 468 |
EXCHECKI( |
| 469 |
nc_def_var(out_id, VAR_COORD_Y, nc_flt_code(out_id), nbr_dim, dim_out_id, &var_out_id)); |
| 470 |
exi_compress_variable(out_id, var_out_id, 2); |
| 471 |
} |
| 472 |
if (spatial_dim > 2) { |
| 473 |
EXCHECKI( |
| 474 |
nc_def_var(out_id, VAR_COORD_Z, nc_flt_code(out_id), nbr_dim, dim_out_id, &var_out_id)); |
| 475 |
exi_compress_variable(out_id, var_out_id, 2); |
| 476 |
} |
| 477 |
|
| 478 |
return var_out_id; /* OK */ |
| 479 |
} |
| 480 |
/*! \endcond */ |
| 481 |
|
| 482 |
/*! \internal */ |
| 483 |
static int cpy_var_def(int in_id, int out_id, int rec_dim_id, char *var_nm) |
| 484 |
{ |
| 485 |
/* Routine to copy the variable metadata from an input netCDF file |
| 486 |
* to an output netCDF file. |
| 487 |
*/ |
| 488 |
|
| 489 |
/* See if the requested variable is already in the output file. */ |
| 490 |
int var_out_id; |
| 491 |
int status = nc_inq_varid(out_id, var_nm, &var_out_id); |
| 492 |
if (status == NC_NOERR) { |
| 493 |
return var_out_id; /* OK */ |
| 494 |
} |
| 495 |
|
| 496 |
/* See if the requested variable is in the input file. */ |
| 497 |
int var_in_id; |
| 498 |
EXCHECKI(nc_inq_varid(in_id, var_nm, &var_in_id)); |
| 499 |
|
| 500 |
/* Get the type of the variable and the number of dimensions. */ |
| 501 |
nc_type var_type; |
| 502 |
EXCHECKI(nc_inq_vartype(in_id, var_in_id, &var_type)); |
| 503 |
int nbr_dim; |
| 504 |
EXCHECKI(nc_inq_varndims(in_id, var_in_id, &nbr_dim)); |
| 505 |
|
| 506 |
/* Recall: |
| 507 |
1. The dimensions must be defined before the variable. |
| 508 |
2. The variable must be defined before the attributes. */ |
| 509 |
|
| 510 |
/* Get the dimension IDs */ |
| 511 |
int dim_in_id[NC_MAX_VAR_DIMS]; |
| 512 |
EXCHECKI(nc_inq_vardimid(in_id, var_in_id, dim_in_id)); |
| 513 |
|
| 514 |
/* Get the dimension sizes and names */ |
| 515 |
int dim_out_id[NC_MAX_VAR_DIMS]; |
| 516 |
for (int idx = 0; idx < nbr_dim; idx++) { |
| 517 |
char dim_nm[NC_MAX_NAME + 1]; |
| 518 |
size_t dim_sz; |
| 519 |
|
| 520 |
EXCHECKI(nc_inq_dim(in_id, dim_in_id[idx], dim_nm, &dim_sz)); |
| 521 |
|
| 522 |
/* See if the dimension has already been defined */ |
| 523 |
status = nc_inq_dimid(out_id, dim_nm, &dim_out_id[idx]); |
| 524 |
|
| 525 |
/* If the dimension hasn't been defined, copy it */ |
| 526 |
if (status != NC_NOERR) { |
| 527 |
if (dim_in_id[idx] != rec_dim_id) { |
| 528 |
EXCHECKI(nc_def_dim(out_id, dim_nm, dim_sz, &dim_out_id[idx])); |
| 529 |
} |
| 530 |
else { |
| 531 |
EXCHECKI(nc_def_dim(out_id, dim_nm, NC_UNLIMITED, &dim_out_id[idx])); |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
/* Define the variable in the output file */ |
| 537 |
|
| 538 |
/* If variable is float or double, define it according to the EXODUS |
| 539 |
file's IO_word_size */ |
| 540 |
|
| 541 |
if ((var_type == NC_FLOAT) || (var_type == NC_DOUBLE)) { |
| 542 |
EXCHECKI(nc_def_var(out_id, var_nm, nc_flt_code(out_id), nbr_dim, dim_out_id, &var_out_id)); |
| 543 |
exi_compress_variable(out_id, var_out_id, 2); |
| 544 |
} |
| 545 |
else { |
| 546 |
EXCHECKI(nc_def_var(out_id, var_nm, var_type, nbr_dim, dim_out_id, &var_out_id)); |
| 547 |
exi_compress_variable(out_id, var_out_id, 1); |
| 548 |
} |
| 549 |
return var_out_id; /* OK */ |
| 550 |
|
| 551 |
} /* end cpy_var_def() */ |
| 552 |
|
| 553 |
/*! \internal */ |
| 554 |
static int cpy_var_val(int in_id, int out_id, char *var_nm) |
| 555 |
{ |
| 556 |
void *void_ptr = NULL; |
| 557 |
/* Routine to copy the variable data from an input netCDF file |
| 558 |
* to an output netCDF file. |
| 559 |
*/ |
| 560 |
|
| 561 |
/* Get the var_id for the requested variable from both files. */ |
| 562 |
int var_in_id; |
| 563 |
EXCHECKI(nc_inq_varid(in_id, var_nm, &var_in_id)); |
| 564 |
int var_out_id; |
| 565 |
EXCHECKI(nc_inq_varid(out_id, var_nm, &var_out_id)); |
| 566 |
|
| 567 |
/* Get the number of dimensions for the variable. */ |
| 568 |
nc_type var_type_out; |
| 569 |
EXCHECKI(nc_inq_vartype(out_id, var_out_id, &var_type_out)); |
| 570 |
|
| 571 |
nc_type var_type_in; |
| 572 |
EXCHECKI(nc_inq_vartype(in_id, var_in_id, &var_type_in)); |
| 573 |
int nbr_dim; |
| 574 |
EXCHECKI(nc_inq_varndims(in_id, var_in_id, &nbr_dim)); |
| 575 |
|
| 576 |
/* Get the dimension IDs from the input file */ |
| 577 |
int dim_id_in[NC_MAX_VAR_DIMS]; |
| 578 |
EXCHECKF(nc_inq_vardimid(in_id, var_in_id, dim_id_in)); |
| 579 |
int dim_id_out[NC_MAX_VAR_DIMS]; |
| 580 |
EXCHECKF(nc_inq_vardimid(out_id, var_out_id, dim_id_out)); |
| 581 |
|
| 582 |
/* Get the dimension sizes and names from the input file */ |
| 583 |
size_t dim_str[NC_MAX_VAR_DIMS]; |
| 584 |
size_t dim_cnt[NC_MAX_VAR_DIMS]; |
| 585 |
size_t var_sz = 1L; |
| 586 |
bool string_len_same = true; |
| 587 |
|
| 588 |
for (int idx = 0; idx < nbr_dim; idx++) { |
| 589 |
/* NB: For the unlimited dimension, ncdiminq() returns the maximum |
| 590 |
value used so far in writing data for that dimension. |
| 591 |
Thus if you read the dimension sizes from the output file, then |
| 592 |
the ncdiminq() returns dim_sz=0 for the unlimited dimension |
| 593 |
until a variable has been written with that dimension. This is |
| 594 |
the reason for always reading the input file for the dimension |
| 595 |
sizes. */ |
| 596 |
size_t dim_in = 0; |
| 597 |
size_t dim_out = 0; |
| 598 |
|
| 599 |
/* If client is increasing any sizes, then need to make sure |
| 600 |
the void_ptr is large enough to hold new dimension */ |
| 601 |
EXCHECKF(nc_inq_dimlen(in_id, dim_id_in[idx], &dim_in)); |
| 602 |
EXCHECKF(nc_inq_dimlen(out_id, dim_id_out[idx], &dim_out)); |
| 603 |
|
| 604 |
/* Initialize the indicial offset and stride arrays */ |
| 605 |
size_t dim_max = dim_in > dim_out ? dim_in : dim_out; |
| 606 |
|
| 607 |
/* Need to know if input and output have different size strings. */ |
| 608 |
if (var_type_in == NC_CHAR && idx == 1 && dim_in != dim_out) { |
| 609 |
string_len_same = false; |
| 610 |
} |
| 611 |
var_sz *= dim_max; |
| 612 |
|
| 613 |
/* Handle case where output variable is smaller than input (rare, but happens) */ |
| 614 |
dim_cnt[idx] = dim_out > 0 ? dim_out : dim_in; |
| 615 |
dim_str[idx] = 0; |
| 616 |
|
| 617 |
} /* end loop over dim */ |
| 618 |
|
| 619 |
/* Allocate enough space to hold the variable */ |
| 620 |
if (var_sz > 0) { |
| 621 |
void_ptr = calloc(var_sz, type_size(var_type_in)); |
| 622 |
} |
| 623 |
|
| 624 |
/* Get the variable */ |
| 625 |
|
| 626 |
/* if variable is float or double, convert if necessary */ |
| 627 |
|
| 628 |
if (nbr_dim == 0) { /* variable is a scalar */ |
| 629 |
|
| 630 |
if (var_type_in == NC_INT && var_type_out == NC_INT) { |
| 631 |
EXCHECKF(nc_get_var1_int(in_id, var_in_id, 0L, void_ptr)); |
| 632 |
EXCHECKF(nc_put_var1_int(out_id, var_out_id, 0L, void_ptr)); |
| 633 |
} |
| 634 |
|
| 635 |
else if (var_type_in == NC_INT64 && var_type_out == NC_INT64) { |
| 636 |
EXCHECKF(nc_get_var1_longlong(in_id, var_in_id, 0L, void_ptr)); |
| 637 |
EXCHECKF(nc_put_var1_longlong(out_id, var_out_id, 0L, void_ptr)); |
| 638 |
} |
| 639 |
|
| 640 |
else if (var_type_in == NC_FLOAT) { |
| 641 |
EXCHECKF(nc_get_var1_float(in_id, var_in_id, 0L, void_ptr)); |
| 642 |
EXCHECKF(nc_put_var1_float(out_id, var_out_id, 0L, void_ptr)); |
| 643 |
} |
| 644 |
|
| 645 |
else if (var_type_in == NC_DOUBLE) { |
| 646 |
EXCHECKF(nc_get_var1_double(in_id, var_in_id, 0L, void_ptr)); |
| 647 |
EXCHECKF(nc_put_var1_double(out_id, var_out_id, 0L, void_ptr)); |
| 648 |
} |
| 649 |
|
| 650 |
else if (var_type_in == NC_CHAR) { |
| 651 |
EXCHECKF(nc_get_var1_text(in_id, var_in_id, 0L, void_ptr)); |
| 652 |
EXCHECKF(nc_put_var1_text(out_id, var_out_id, 0L, void_ptr)); |
| 653 |
} |
| 654 |
|
| 655 |
else { |
| 656 |
assert(1 == 0); |
| 657 |
} |
| 658 |
} |
| 659 |
else { /* variable is a vector */ |
| 660 |
|
| 661 |
if (var_type_in == NC_INT && var_type_out == NC_INT) { |
| 662 |
EXCHECKF(nc_get_var_int(in_id, var_in_id, void_ptr)); |
| 663 |
EXCHECKF(nc_put_vara_int(out_id, var_out_id, dim_str, dim_cnt, void_ptr)); |
| 664 |
} |
| 665 |
|
| 666 |
else if (var_type_in == NC_INT64 && var_type_out == NC_INT64) { |
| 667 |
EXCHECKF(nc_get_var_longlong(in_id, var_in_id, void_ptr)); |
| 668 |
EXCHECKF(nc_put_vara_longlong(out_id, var_out_id, dim_str, dim_cnt, void_ptr)); |
| 669 |
} |
| 670 |
|
| 671 |
else if (var_type_in == NC_FLOAT) { |
| 672 |
EXCHECKF(nc_get_var_float(in_id, var_in_id, void_ptr)); |
| 673 |
EXCHECKF(nc_put_vara_float(out_id, var_out_id, dim_str, dim_cnt, void_ptr)); |
| 674 |
} |
| 675 |
|
| 676 |
else if (var_type_in == NC_DOUBLE) { |
| 677 |
EXCHECKF(nc_get_var_double(in_id, var_in_id, void_ptr)); |
| 678 |
EXCHECKF(nc_put_vara_double(out_id, var_out_id, dim_str, dim_cnt, void_ptr)); |
| 679 |
} |
| 680 |
|
| 681 |
else if (var_type_in == NC_CHAR) { |
| 682 |
|
| 683 |
if (string_len_same) { |
| 684 |
EXCHECKF(nc_get_var_text(in_id, var_in_id, void_ptr)); |
| 685 |
EXCHECKF(nc_put_vara_text(out_id, var_out_id, dim_str, dim_cnt, void_ptr)); |
| 686 |
} |
| 687 |
else { |
| 688 |
/* Use void_ptr for the input read; alloc new space for output... */ |
| 689 |
EXCHECKF(nc_get_var_text(in_id, var_in_id, void_ptr)); |
| 690 |
|
| 691 |
if (void_ptr != NULL) { |
| 692 |
size_t num_string = 0; |
| 693 |
size_t in_size = 0; |
| 694 |
size_t out_size = 0; |
| 695 |
EXCHECKF(nc_inq_dimlen(in_id, dim_id_in[0], &num_string)); |
| 696 |
EXCHECKF(nc_inq_dimlen(in_id, dim_id_in[1], &in_size)); |
| 697 |
EXCHECKF(nc_inq_dimlen(out_id, dim_id_out[1], &out_size)); |
| 698 |
size_t min_size = in_size < out_size ? in_size : out_size; |
| 699 |
char *out_strings = calloc(num_string * out_size, 1); |
| 700 |
/* Read the input strings...*/ |
| 701 |
|
| 702 |
/* Copy to the output strings...*/ |
| 703 |
const char *in_strings = void_ptr; |
| 704 |
for (size_t i = 0; i < num_string; i++) { |
| 705 |
size_t in_off = i * in_size; |
| 706 |
size_t out_off = i * out_size; |
| 707 |
ex_copy_string(&out_strings[out_off], &in_strings[in_off], min_size); |
| 708 |
out_strings[out_off + out_size - 1] = '\0'; |
| 709 |
} |
| 710 |
dim_cnt[1] = out_size; |
| 711 |
EXCHECKF(nc_put_vara_text(out_id, var_out_id, dim_str, dim_cnt, out_strings)); |
| 712 |
free(out_strings); |
| 713 |
} |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
else { |
| 718 |
assert(1 == 0); |
| 719 |
} |
| 720 |
} /* end if variable is an array */ |
| 721 |
|
| 722 |
/* Free the space that held the variable */ |
| 723 |
free(void_ptr); |
| 724 |
|
| 725 |
return EX_NOERR; |
| 726 |
|
| 727 |
err_ret: |
| 728 |
free(void_ptr); |
| 729 |
return EX_FATAL; |
| 730 |
|
| 731 |
} /* end cpy_var_val() */ |
| 732 |
|
| 733 |
/*! \internal */ |
| 734 |
static int cpy_coord_val(int in_id, int out_id, char *var_nm, int in_large) |
| 735 |
{ |
| 736 |
/* Routine to copy the coordinate data from an input netCDF file |
| 737 |
* to an output netCDF file. |
| 738 |
*/ |
| 739 |
|
| 740 |
/* Handle easiest situation first: in_large matches out_large (1) */ |
| 741 |
if (in_large == 1) { |
| 742 |
return cpy_var_val(in_id, out_id, var_nm); /* OK */ |
| 743 |
} |
| 744 |
|
| 745 |
/* At this point, know that in_large == 0, so will need to |
| 746 |
copy a vector to multiple scalars. Also |
| 747 |
will need a couple dimensions, so get them now.*/ |
| 748 |
const char *routine = NULL; |
| 749 |
int temp; |
| 750 |
size_t spatial_dim, num_nodes; |
| 751 |
exi_get_dimension(in_id, DIM_NUM_DIM, "dimension", &spatial_dim, &temp, routine); |
| 752 |
exi_get_dimension(in_id, DIM_NUM_NODES, "nodes", &num_nodes, &temp, routine); |
| 753 |
|
| 754 |
/* output file will have coordx, coordy, coordz (if 3d). */ |
| 755 |
/* Get the var_id for the requested variable from both files. */ |
| 756 |
int var_in_id, var_out_id[3]; |
| 757 |
EXCHECKI(nc_inq_varid(in_id, VAR_COORD, &var_in_id)); |
| 758 |
|
| 759 |
EXCHECKI(nc_inq_varid(out_id, VAR_COORD_X, &var_out_id[0])); |
| 760 |
if (spatial_dim > 1) { |
| 761 |
EXCHECKI(nc_inq_varid(out_id, VAR_COORD_Y, &var_out_id[1])); |
| 762 |
} |
| 763 |
if (spatial_dim > 2) { |
| 764 |
EXCHECKI(nc_inq_varid(out_id, VAR_COORD_Z, &var_out_id[2])); |
| 765 |
} |
| 766 |
|
| 767 |
nc_type var_type_in, var_type_out; |
| 768 |
EXCHECKI(nc_inq_vartype(in_id, var_in_id, &var_type_in)); |
| 769 |
EXCHECKI(nc_inq_vartype(out_id, var_out_id[0], &var_type_out)); |
| 770 |
|
| 771 |
void *void_ptr = NULL; |
| 772 |
if (num_nodes > 0) { |
| 773 |
void_ptr = malloc(num_nodes * type_size(var_type_in)); |
| 774 |
} |
| 775 |
|
| 776 |
/* Copy each component of the variable... */ |
| 777 |
size_t start[2], count[2]; |
| 778 |
for (size_t i = 0; i < spatial_dim; i++) { |
| 779 |
start[0] = i; |
| 780 |
start[1] = 0; |
| 781 |
count[0] = 1; |
| 782 |
count[1] = num_nodes; |
| 783 |
if (var_type_in == NC_FLOAT) { |
| 784 |
EXCHECKI(nc_get_vara_float(in_id, var_in_id, start, count, void_ptr)); |
| 785 |
EXCHECKI(nc_put_var_float(out_id, var_out_id[i], void_ptr)); |
| 786 |
} |
| 787 |
else { |
| 788 |
assert(var_type_in == NC_DOUBLE); |
| 789 |
EXCHECKI(nc_get_vara_double(in_id, var_in_id, start, count, void_ptr)); |
| 790 |
EXCHECKI(nc_put_var_double(out_id, var_out_id[i], void_ptr)); |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
/* Free the space that held the variable */ |
| 795 |
free(void_ptr); |
| 796 |
return EX_NOERR; |
| 797 |
|
| 798 |
} /* end cpy_coord_val() */ |
| 799 |
|
| 800 |
/*! \internal */ |
| 801 |
static void update_structs(int out_exoid) |
| 802 |
{ |
| 803 |
update_internal_structs(out_exoid, EX_INQ_EDGE_BLK, exi_get_counter_list(EX_EDGE_BLOCK)); |
| 804 |
update_internal_structs(out_exoid, EX_INQ_FACE_BLK, exi_get_counter_list(EX_FACE_BLOCK)); |
| 805 |
update_internal_structs(out_exoid, EX_INQ_ELEM_BLK, exi_get_counter_list(EX_ELEM_BLOCK)); |
| 806 |
update_internal_structs(out_exoid, EX_INQ_ASSEMBLY, exi_get_counter_list(EX_ASSEMBLY)); |
| 807 |
update_internal_structs(out_exoid, EX_INQ_BLOB, exi_get_counter_list(EX_BLOB)); |
| 808 |
|
| 809 |
update_internal_structs(out_exoid, EX_INQ_NODE_SETS, exi_get_counter_list(EX_NODE_SET)); |
| 810 |
update_internal_structs(out_exoid, EX_INQ_EDGE_SETS, exi_get_counter_list(EX_EDGE_SET)); |
| 811 |
update_internal_structs(out_exoid, EX_INQ_FACE_SETS, exi_get_counter_list(EX_FACE_SET)); |
| 812 |
update_internal_structs(out_exoid, EX_INQ_SIDE_SETS, exi_get_counter_list(EX_SIDE_SET)); |
| 813 |
update_internal_structs(out_exoid, EX_INQ_ELEM_SETS, exi_get_counter_list(EX_ELEM_SET)); |
| 814 |
|
| 815 |
update_internal_structs(out_exoid, EX_INQ_NODE_MAP, exi_get_counter_list(EX_NODE_MAP)); |
| 816 |
update_internal_structs(out_exoid, EX_INQ_EDGE_MAP, exi_get_counter_list(EX_EDGE_MAP)); |
| 817 |
update_internal_structs(out_exoid, EX_INQ_FACE_MAP, exi_get_counter_list(EX_FACE_MAP)); |
| 818 |
update_internal_structs(out_exoid, EX_INQ_ELEM_MAP, exi_get_counter_list(EX_ELEM_MAP)); |
| 819 |
} |
| 820 |
|
| 821 |
/*! \internal */ |
| 822 |
static void update_internal_structs(int out_exoid, ex_inquiry inqcode, |
| 823 |
struct exi_list_item **ctr_list) |
| 824 |
{ |
| 825 |
int64_t number = ex_inquire_int(out_exoid, inqcode); |
| 826 |
if (number > 0) { |
| 827 |
for (int64_t i = 0; i < number; i++) { |
| 828 |
exi_inc_file_item(out_exoid, ctr_list); |
| 829 |
} |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
static size_t type_size(nc_type type) |
| 834 |
{ |
| 835 |
if (type == NC_CHAR) { |
| 836 |
return sizeof(char); /* OK */ |
| 837 |
} |
| 838 |
if (type == NC_INT) { |
| 839 |
return sizeof(int); /* OK */ |
| 840 |
} |
| 841 |
if (type == NC_INT64) { |
| 842 |
return sizeof(int64_t); /* OK */ |
| 843 |
} |
| 844 |
if (type == NC_FLOAT) { |
| 845 |
return sizeof(float); /* OK */ |
| 846 |
} |
| 847 |
if (type == NC_DOUBLE) { |
| 848 |
return sizeof(double); /* OK */ |
| 849 |
} |
| 850 |
else { |
| 851 |
return 0; /* OK */ |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
/* \endcond */ |