| 1 |
/* |
| 2 |
* Copyright(C) 1999-2021, 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 exi_file_item, EX_FATAL, etc |
| 11 |
#include "stdbool.h" |
| 12 |
|
| 13 |
/*! \file |
| 14 |
* this file contains code needed to support the various floating point word |
| 15 |
* size combinations for computation and i/o that applications might want to |
| 16 |
* use. See the netcdf documentation for more details on the floating point |
| 17 |
* conversion capabilities. |
| 18 |
* |
| 19 |
* netCDF supports two floating point word sizes for its files: |
| 20 |
* - NC_FLOAT - 32 bit IEEE |
| 21 |
* - NC_DOUBLE - 64 bit IEEE |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
#define NC_FLOAT_WORDSIZE 4 |
| 26 |
|
| 27 |
static struct exi_file_item *file_list = NULL; |
| 28 |
|
| 29 |
struct exi_file_item *exi_find_file_item(int exoid) |
| 30 |
{ |
| 31 |
/* Find base filename in case exoid refers to a group */ |
| 32 |
int base_exoid = (unsigned)exoid & EX_FILE_ID_MASK; |
| 33 |
struct exi_file_item *ptr = file_list; |
| 34 |
while (ptr) { |
| 35 |
if (ptr->file_id == base_exoid) { |
| 36 |
break; |
| 37 |
} |
| 38 |
ptr = ptr->next; |
| 39 |
} |
| 40 |
return ptr; |
| 41 |
} |
| 42 |
|
| 43 |
#define EX__MAX_PATHLEN 8192 |
| 44 |
int exi_check_multiple_open(const char *path, int mode, const char *func) |
| 45 |
{ |
| 46 |
EX_FUNC_ENTER(); |
| 47 |
bool is_write = mode & EX_WRITE; |
| 48 |
char tmp[EX__MAX_PATHLEN]; |
| 49 |
size_t pathlen; |
| 50 |
struct exi_file_item *ptr = file_list; |
| 51 |
while (ptr) { |
| 52 |
nc_inq_path(ptr->file_id, &pathlen, tmp); |
| 53 |
/* If path is too long, assume it is ok... */ |
| 54 |
if (pathlen < EX__MAX_PATHLEN && strncmp(path, tmp, EX__MAX_PATHLEN) == 0) { |
| 55 |
/* Found matching file. See if any open for write */ |
| 56 |
if (ptr->is_write || is_write) { |
| 57 |
char errmsg[MAX_ERR_LENGTH]; |
| 58 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 59 |
"ERROR: The file '%s' is open for both read and write." |
| 60 |
" File corruption or incorrect behavior can occur.\n", |
| 61 |
path); |
| 62 |
ex_err(func, errmsg, EX_BADFILEID); |
| 63 |
#if defined BUILT_IN_SIERRA |
| 64 |
EX_FUNC_LEAVE(EX_NOERR); |
| 65 |
#else |
| 66 |
EX_FUNC_LEAVE(EX_FATAL); |
| 67 |
#endif |
| 68 |
} |
| 69 |
} |
| 70 |
ptr = ptr->next; |
| 71 |
} |
| 72 |
EX_FUNC_LEAVE(EX_NOERR); |
| 73 |
} |
| 74 |
|
| 75 |
int exi_check_valid_file_id(int exoid, const char *func) |
| 76 |
{ |
| 77 |
bool error = false; |
| 78 |
if (exoid <= 0) { |
| 79 |
error = true; |
| 80 |
} |
| 81 |
#if !defined BUILT_IN_SIERRA |
| 82 |
else { |
| 83 |
int rootid = exoid & EX_FILE_ID_MASK; |
| 84 |
struct exi_file_item *file = exi_find_file_item(rootid); |
| 85 |
|
| 86 |
if (!file) { |
| 87 |
error = true; |
| 88 |
} |
| 89 |
} |
| 90 |
#endif |
| 91 |
|
| 92 |
if (error) { |
| 93 |
int old_opt = ex_opts(EX_VERBOSE); |
| 94 |
if (old_opt & EX_ABORT) { |
| 95 |
ex_opts(EX_VERBOSE | EX_ABORT); |
| 96 |
} |
| 97 |
int rootid = exoid & EX_FILE_ID_MASK; |
| 98 |
char errmsg[MAX_ERR_LENGTH]; |
| 99 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 100 |
"ERROR: In \"%s\", the file id %d was not obtained via a call " |
| 101 |
"to \"ex_open\" or \"ex_create\".\n\t\tIt does not refer to a " |
| 102 |
"valid open exodus file.\n\t\tAborting to avoid file " |
| 103 |
"corruption or data loss or other potential problems.", |
| 104 |
func, rootid); |
| 105 |
ex_err(__func__, errmsg, EX_BADFILEID); |
| 106 |
ex_opts(old_opt); |
| 107 |
return EX_FATAL; |
| 108 |
} |
| 109 |
return EX_NOERR; |
| 110 |
} |
| 111 |
|
| 112 |
int exi_conv_init(int exoid, int *comp_wordsize, int *io_wordsize, int file_wordsize, |
| 113 |
int int64_status, bool is_parallel, bool is_hdf5, bool is_pnetcdf, bool is_write) |
| 114 |
{ |
| 115 |
char errmsg[MAX_ERR_LENGTH]; |
| 116 |
struct exi_file_item *new_file = NULL; |
| 117 |
|
| 118 |
/*! exi_conv_init() initializes the floating point conversion process. |
| 119 |
* |
| 120 |
* \param exoid an integer uniquely identifying the file of interest. |
| 121 |
* |
| 122 |
* \param comp_wordsize compute floating point word size in the user's code. |
| 123 |
* a zero value indicates that the user is requesting the |
| 124 |
* default float size for the machine. The appropriate |
| 125 |
* value is chosen and returned in comp_wordsize, and |
| 126 |
* used in subsequent conversions. a valid but |
| 127 |
* inappropriate for this parameter cannot be detected. |
| 128 |
* |
| 129 |
* \param io_wordsize the desired floating point word size for a netCDF |
| 130 |
* file. For an existing file, if this parameter doesn't |
| 131 |
* match the word size of data already stored in the file, |
| 132 |
* a fatal error is generated. a value of 0 for an |
| 133 |
* existing file indicates that the word size of the file |
| 134 |
* was not known a priori, so use whatever is in the file. |
| 135 |
* a value of 0 for a new file means to use the default |
| 136 |
* size, an NC_FLOAT (4 bytes). when a value of 0 is |
| 137 |
* specified the actual value used is returned in |
| 138 |
* io_wordsize. |
| 139 |
* |
| 140 |
* \param file_wordsize floating point word size in an existing netCDF file. |
| 141 |
* a value of 0 should be passed in for a new netCDF |
| 142 |
* file. |
| 143 |
* |
| 144 |
* \param int64_status the flags specifying how integer values should be |
| 145 |
* stored on the database and how they should be |
| 146 |
* passes through the api functions. |
| 147 |
* |
| 148 |
* \param is_parallel 1 if parallel file; 0 if serial |
| 149 |
* |
| 150 |
* \param is_hdf5 1 if parallel netcdf-4 mode; 0 if not. |
| 151 |
* |
| 152 |
* \param is_pnetcdf 1 if parallel PNetCDF file; 0 if not. |
| 153 |
* |
| 154 |
* \param is_write 1 if output file; 0 if readonly |
| 155 |
* |
| 156 |
* word size parameters are specified in bytes. valid values are 0, 4, and 8: |
| 157 |
*/ |
| 158 |
|
| 159 |
EX_FUNC_ENTER(); |
| 160 |
|
| 161 |
/* check to make sure machine word sizes are sane */ |
| 162 |
/* If the following line causes a compile-time error, then there is a problem |
| 163 |
* which will cause exodus to not work correctly on this platform. |
| 164 |
* |
| 165 |
* Contact Greg Sjaardema, [email protected] for assistance. |
| 166 |
*/ |
| 167 |
#define CT_ASSERT(e) extern char(*ct_assert(void))[sizeof(char[1 - 2 * !(e)])] |
| 168 |
CT_ASSERT((sizeof(float) == 4 || sizeof(float) == 8) && |
| 169 |
(sizeof(double) == 4 || sizeof(double) == 8)); |
| 170 |
|
| 171 |
/* check to see if requested word sizes are valid */ |
| 172 |
if (!*io_wordsize) { |
| 173 |
if (!file_wordsize) { |
| 174 |
*io_wordsize = NC_FLOAT_WORDSIZE; |
| 175 |
} |
| 176 |
else { |
| 177 |
*io_wordsize = file_wordsize; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
else if (*io_wordsize != 4 && *io_wordsize != 8) { |
| 182 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unsupported I/O word size for file id: %d", exoid); |
| 183 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 184 |
EX_FUNC_LEAVE(EX_FATAL); |
| 185 |
} |
| 186 |
|
| 187 |
else if (file_wordsize && *io_wordsize != file_wordsize) { |
| 188 |
*io_wordsize = file_wordsize; |
| 189 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 190 |
"ERROR: invalid I/O word size specified for existing file id: " |
| 191 |
"%d, Requested I/O word size overridden.", |
| 192 |
exoid); |
| 193 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 194 |
} |
| 195 |
|
| 196 |
if (!*comp_wordsize) { |
| 197 |
*comp_wordsize = sizeof(float); |
| 198 |
} |
| 199 |
else if (*comp_wordsize != 4 && *comp_wordsize != 8) { |
| 200 |
ex_err_fn(exoid, __func__, "ERROR: invalid compute wordsize specified", EX_BADPARAM); |
| 201 |
EX_FUNC_LEAVE(EX_FATAL); |
| 202 |
} |
| 203 |
|
| 204 |
/* Check that the int64_status contains only valid bits... */ |
| 205 |
{ |
| 206 |
int valid_int64 = EX_ALL_INT64_API | EX_ALL_INT64_DB; |
| 207 |
if ((int64_status & valid_int64) != int64_status) { |
| 208 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 209 |
"Warning: invalid int64_status flag (%d) specified for " |
| 210 |
"existing file id: %d. Ignoring invalids", |
| 211 |
int64_status, exoid); |
| 212 |
ex_err_fn(exoid, __func__, errmsg, -EX_BADPARAM); |
| 213 |
} |
| 214 |
int64_status &= valid_int64; |
| 215 |
} |
| 216 |
|
| 217 |
/* Verify filetype |
| 218 |
* 0 -- classic format (NC_FORMAT_CLASSIC -1) |
| 219 |
* 1 -- 64 bit classic (NC_FORMAT_64BIT -1) |
| 220 |
* 2 -- netcdf4 (NC_FORMAT_NETCDF4 -1) |
| 221 |
* 3 -- netcdf4 classic (NC_FORMAT_NETCDF4_CLASSIC -1) |
| 222 |
*/ |
| 223 |
|
| 224 |
int filetype = 0; |
| 225 |
nc_inq_format(exoid, &filetype); |
| 226 |
|
| 227 |
if (!(new_file = malloc(sizeof(struct exi_file_item)))) { |
| 228 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 229 |
"ERROR: failed to allocate memory for internal file " |
| 230 |
"structure storage file id %d", |
| 231 |
exoid); |
| 232 |
ex_err_fn(exoid, __func__, errmsg, EX_MEMFAIL); |
| 233 |
EX_FUNC_LEAVE(EX_FATAL); |
| 234 |
} |
| 235 |
|
| 236 |
new_file->file_id = (unsigned)exoid & EX_FILE_ID_MASK; |
| 237 |
new_file->user_compute_wordsize = *comp_wordsize == 4 ? 0 : 1; |
| 238 |
new_file->int64_status = int64_status; |
| 239 |
new_file->maximum_name_length = exi_default_max_name_length; |
| 240 |
new_file->time_varid = -1; |
| 241 |
new_file->compression_algorithm = EX_COMPRESS_GZIP; |
| 242 |
new_file->assembly_count = 0; |
| 243 |
new_file->blob_count = 0; |
| 244 |
new_file->compression_level = 0; |
| 245 |
new_file->quantize_nsd = 0; |
| 246 |
new_file->shuffle = 0; |
| 247 |
new_file->file_type = filetype - 1; |
| 248 |
new_file->is_parallel = is_parallel; |
| 249 |
new_file->is_hdf5 = is_hdf5; |
| 250 |
new_file->is_pnetcdf = is_pnetcdf; |
| 251 |
new_file->has_nodes = 1; /* default to yes in case not set */ |
| 252 |
new_file->has_edges = 1; |
| 253 |
new_file->has_faces = 1; |
| 254 |
new_file->has_elems = 1; |
| 255 |
new_file->in_define_mode = 0; |
| 256 |
new_file->persist_define_mode = 0; |
| 257 |
new_file->is_write = is_write; |
| 258 |
|
| 259 |
new_file->next = file_list; |
| 260 |
file_list = new_file; |
| 261 |
|
| 262 |
if (*io_wordsize == NC_FLOAT_WORDSIZE) { |
| 263 |
new_file->netcdf_type_code = NC_FLOAT; |
| 264 |
} |
| 265 |
else { |
| 266 |
new_file->netcdf_type_code = NC_DOUBLE; |
| 267 |
} |
| 268 |
|
| 269 |
EX_FUNC_LEAVE(EX_NOERR); |
| 270 |
} |
| 271 |
|
| 272 |
/*............................................................................*/ |
| 273 |
/*............................................................................*/ |
| 274 |
|
| 275 |
/*! exi_conv_exit() takes the structure identified by "exoid" out of the linked |
| 276 |
* list which describes the files that ex_conv_array() knows how to convert. |
| 277 |
* |
| 278 |
* \note it is absolutely necessary for exi_conv_exit() to be called after |
| 279 |
* ncclose(), if the parameter used as "exoid" is the id returned from |
| 280 |
* an ncopen() or nccreate() call, as netCDF reuses file ids! |
| 281 |
* the best place to do this is ex_close(), which is where I did it. |
| 282 |
* |
| 283 |
* \param exoid integer which uniquely identifies the file of interest. |
| 284 |
*/ |
| 285 |
void exi_conv_exit(int exoid) |
| 286 |
{ |
| 287 |
struct exi_file_item *file = file_list; |
| 288 |
struct exi_file_item *prev = NULL; |
| 289 |
|
| 290 |
int root_id = (unsigned)exoid & EX_FILE_ID_MASK; |
| 291 |
|
| 292 |
EX_FUNC_ENTER(); |
| 293 |
while (file) { |
| 294 |
if (file->file_id == root_id) { |
| 295 |
break; |
| 296 |
} |
| 297 |
|
| 298 |
prev = file; |
| 299 |
file = file->next; |
| 300 |
} |
| 301 |
|
| 302 |
if (!file) { |
| 303 |
char errmsg[MAX_ERR_LENGTH]; |
| 304 |
snprintf(errmsg, MAX_ERR_LENGTH, "Warning: failure to clear file id %d - not in list.", exoid); |
| 305 |
ex_err(__func__, errmsg, -EX_BADFILEID); |
| 306 |
EX_FUNC_VOID(); |
| 307 |
} |
| 308 |
|
| 309 |
if (prev) { |
| 310 |
prev->next = file->next; |
| 311 |
} |
| 312 |
else { |
| 313 |
file_list = file->next; |
| 314 |
} |
| 315 |
|
| 316 |
free(file); |
| 317 |
EX_FUNC_VOID(); |
| 318 |
} |
| 319 |
|
| 320 |
/*............................................................................*/ |
| 321 |
/*............................................................................*/ |
| 322 |
|
| 323 |
nc_type nc_flt_code(int exoid) |
| 324 |
{ |
| 325 |
/*! |
| 326 |
* \ingroup Utilities |
| 327 |
* nc_flt_code() returns either NC_FLOAT or NC_DOUBLE, based on the parameters |
| 328 |
* with which exi_conv_init() was called. nc_flt_code() is used as the nc_type |
| 329 |
* parameter on ncvardef() calls that define floating point variables. |
| 330 |
* |
| 331 |
* "exoid" is some integer which uniquely identifies the file of interest. |
| 332 |
*/ |
| 333 |
EX_FUNC_ENTER(); |
| 334 |
struct exi_file_item *file = exi_find_file_item(exoid); |
| 335 |
|
| 336 |
if (!file) { |
| 337 |
char errmsg[MAX_ERR_LENGTH]; |
| 338 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unknown file id %d for nc_flt_code().", exoid); |
| 339 |
ex_err(__func__, errmsg, EX_BADFILEID); |
| 340 |
EX_FUNC_LEAVE((nc_type)-1); |
| 341 |
} |
| 342 |
EX_FUNC_LEAVE(file->netcdf_type_code); |
| 343 |
} |
| 344 |
|
| 345 |
unsigned ex_int64_status(int exoid) |
| 346 |
{ |
| 347 |
/*! |
| 348 |
* \ingroup Utilities |
| 349 |
ex_int64_status() returns an int that can be tested |
| 350 |
against the defines listed below to determine which, if any, |
| 351 |
'types' in the database are to be stored as int64 types and which, if any, |
| 352 |
types are passed/returned as int64 types in the API |
| 353 |
|
| 354 |
| Defines: | | |
| 355 |
|----------|-| |
| 356 |
| #EX_MAPS_INT64_DB | All maps (id, order, ...) store int64_t values | |
| 357 |
| #EX_IDS_INT64_DB | All entity ids (sets, blocks, maps) are int64_t values | |
| 358 |
| #EX_BULK_INT64_DB | All integer bulk data (local indices, counts, maps); not ids | |
| 359 |
| #EX_ALL_INT64_DB | (#EX_MAPS_INT64_DB \| #EX_IDS_INT64_DB \| #EX_BULK_INT64_DB) | |
| 360 |
| #EX_MAPS_INT64_API| All maps (id, order, ...) passed as int64_t values | |
| 361 |
| #EX_IDS_INT64_API | All entity ids (sets, blocks, maps) are passed as int64_t values | |
| 362 |
| #EX_BULK_INT64_API| All integer bulk data (local indices, counts, maps); not ids| |
| 363 |
| #EX_INQ_INT64_API | Integers passed to/from ex_inquire() are int64_t | |
| 364 |
| #EX_ALL_INT64_API | (#EX_MAPS_INT64_API \| #EX_IDS_INT64_API \| #EX_BULK_INT64_API \| |
| 365 |
#EX_INQ_INT64_API) | |
| 366 |
*/ |
| 367 |
EX_FUNC_ENTER(); |
| 368 |
struct exi_file_item *file = exi_find_file_item(exoid); |
| 369 |
|
| 370 |
if (!file) { |
| 371 |
char errmsg[MAX_ERR_LENGTH]; |
| 372 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unknown file id %d for ex_int64_status().", exoid); |
| 373 |
ex_err(__func__, errmsg, EX_BADFILEID); |
| 374 |
EX_FUNC_LEAVE(0); |
| 375 |
} |
| 376 |
EX_FUNC_LEAVE(file->int64_status); |
| 377 |
} |
| 378 |
|
| 379 |
int ex_set_int64_status(int exoid, int mode) |
| 380 |
{ |
| 381 |
/*! |
| 382 |
\ingroup Utilities |
| 383 |
|
| 384 |
ex_set_int64_status() sets the value of the INT64_API flags which |
| 385 |
specify how integer types are passed/returned as int64 types in |
| 386 |
the API |
| 387 |
|
| 388 |
| Mode can be one of: | | |
| 389 |
|----------|-| |
| 390 |
| 0 | All integers are passed as int32_t values. | |
| 391 |
| #EX_MAPS_INT64_API| All maps (id, order, ...) passed as int64_t values | |
| 392 |
| #EX_IDS_INT64_API | All entity ids (sets, blocks, maps) are passed as int64_t values | |
| 393 |
| #EX_BULK_INT64_API| All integer bulk data (local indices, counts, maps); not ids| |
| 394 |
| #EX_INQ_INT64_API | Integers passed to/from ex_inquire() are int64_t | |
| 395 |
| #EX_ALL_INT64_API | (#EX_MAPS_INT64_API \| #EX_IDS_INT64_API \| #EX_BULK_INT64_API \| |
| 396 |
#EX_INQ_INT64_API) | |
| 397 |
*/ |
| 398 |
EX_FUNC_ENTER(); |
| 399 |
struct exi_file_item *file = exi_find_file_item(exoid); |
| 400 |
|
| 401 |
if (!file) { |
| 402 |
char errmsg[MAX_ERR_LENGTH]; |
| 403 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unknown file id %d for ex_int64_status().", exoid); |
| 404 |
ex_err(__func__, errmsg, EX_BADFILEID); |
| 405 |
EX_FUNC_LEAVE(0); |
| 406 |
} |
| 407 |
|
| 408 |
/* Strip of all non-INT64_API values */ |
| 409 |
int api_mode = mode & EX_ALL_INT64_API; |
| 410 |
int db_mode = file->int64_status & EX_ALL_INT64_DB; |
| 411 |
|
| 412 |
file->int64_status = api_mode | db_mode; |
| 413 |
EX_FUNC_LEAVE(file->int64_status); |
| 414 |
} |
| 415 |
|
| 416 |
/*! |
| 417 |
\ingroup Utilities |
| 418 |
\undoc |
| 419 |
*/ |
| 420 |
int ex_set_option(int exoid, ex_option_type option, int option_value) |
| 421 |
{ |
| 422 |
EX_FUNC_ENTER(); |
| 423 |
struct exi_file_item *file = exi_find_file_item(exoid); |
| 424 |
if (!file) { |
| 425 |
char errmsg[MAX_ERR_LENGTH]; |
| 426 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unknown file id %d for ex_set_option().", exoid); |
| 427 |
ex_err(__func__, errmsg, EX_BADFILEID); |
| 428 |
EX_FUNC_LEAVE(EX_FATAL); |
| 429 |
} |
| 430 |
|
| 431 |
switch (option) { |
| 432 |
case EX_OPT_MAX_NAME_LENGTH: file->maximum_name_length = option_value; break; |
| 433 |
case EX_OPT_COMPRESSION_TYPE: file->compression_algorithm = option_value; break; |
| 434 |
case EX_OPT_COMPRESSION_LEVEL: /* 0 (disabled/fastest) ... 9 (best/slowest) */ |
| 435 |
/* Check whether file type supports compression... */ |
| 436 |
if (file->is_hdf5) { |
| 437 |
int value = option_value; |
| 438 |
if (file->compression_algorithm == EX_COMPRESS_ZLIB) { |
| 439 |
if (value > 9) { |
| 440 |
value = 9; |
| 441 |
} |
| 442 |
if (value < 0) { |
| 443 |
value = 0; |
| 444 |
} |
| 445 |
} |
| 446 |
else if (file->compression_algorithm == EX_COMPRESS_SZIP) { |
| 447 |
if (value % 2 != 0 || value < 4 || value > 32) { |
| 448 |
char errmsg[MAX_ERR_LENGTH]; |
| 449 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 450 |
"ERROR: invalid value %d for SZIP Compression. Must be even and 4 <= value <= " |
| 451 |
"32. Setting value to 4.", |
| 452 |
value); |
| 453 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 454 |
value = 4; |
| 455 |
} |
| 456 |
} |
| 457 |
else if (file->compression_algorithm == EX_COMPRESS_ZSTD) { |
| 458 |
#if NC_HAS_ZSTD == 1 |
| 459 |
if (value < -131072 || value > 22) { |
| 460 |
char errmsg[MAX_ERR_LENGTH]; |
| 461 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 462 |
"ERROR: invalid value %d for ZSTD Compression. Must be between -131072 and 22. " |
| 463 |
"Setting value to 4", |
| 464 |
value); |
| 465 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 466 |
value = 4; |
| 467 |
} |
| 468 |
#else |
| 469 |
char errmsg[MAX_ERR_LENGTH]; |
| 470 |
snprintf( |
| 471 |
errmsg, MAX_ERR_LENGTH, |
| 472 |
"ERROR: Zstandard compression is not supported in this version of netCDF library."); |
| 473 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 474 |
#endif |
| 475 |
} |
| 476 |
else if (file->compression_algorithm == EX_COMPRESS_BZ2) { |
| 477 |
#if NC_HAS_BZ2 == 1 |
| 478 |
if (value < 0 || value > 9) { |
| 479 |
char errmsg[MAX_ERR_LENGTH]; |
| 480 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 481 |
"ERROR: invalid value %d for BZIP2 Compression. Must be between 0 and 9 " |
| 482 |
"inclusive. Setting value to 1.", |
| 483 |
value); |
| 484 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 485 |
value = 1; |
| 486 |
} |
| 487 |
#else |
| 488 |
char errmsg[MAX_ERR_LENGTH]; |
| 489 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 490 |
"ERROR: Bzip2 compression is not supported in this version of netCDF library."); |
| 491 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 492 |
#endif |
| 493 |
} |
| 494 |
file->compression_level = value; |
| 495 |
} |
| 496 |
else { |
| 497 |
file->compression_level = 0; |
| 498 |
} |
| 499 |
break; |
| 500 |
case EX_OPT_QUANTIZE_NSD: |
| 501 |
#if NC_HAS_QUANTIZE == 1 |
| 502 |
if (option_value > 15) { |
| 503 |
char errmsg[MAX_ERR_LENGTH]; |
| 504 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 505 |
"ERROR: invalid value %d for Quantize NSD. Must be less than or equal to 15. " |
| 506 |
"Setting value to 15.", |
| 507 |
option_value); |
| 508 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 509 |
option_value = 15; |
| 510 |
} |
| 511 |
file->quantize_nsd = option_value; |
| 512 |
#else |
| 513 |
{ |
| 514 |
char errmsg[MAX_ERR_LENGTH]; |
| 515 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 516 |
"ERROR: Quanitzation is not supported in this version of netCDF library."); |
| 517 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 518 |
} |
| 519 |
#endif |
| 520 |
break; |
| 521 |
case EX_OPT_COMPRESSION_SHUFFLE: /* 0 (disabled); 1 (enabled) */ |
| 522 |
file->shuffle = option_value != 0 ? 1 : 0; |
| 523 |
break; |
| 524 |
case EX_OPT_INTEGER_SIZE_API: /* See *_INT64_* values above */ |
| 525 |
ex_set_int64_status(exoid, option_value); |
| 526 |
break; |
| 527 |
case EX_OPT_INTEGER_SIZE_DB: /* (query only) */ break; |
| 528 |
default: { |
| 529 |
char errmsg[MAX_ERR_LENGTH]; |
| 530 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: invalid option %d for ex_set_option().", (int)option); |
| 531 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 532 |
EX_FUNC_LEAVE(EX_FATAL); |
| 533 |
} |
| 534 |
} |
| 535 |
EX_FUNC_LEAVE(EX_NOERR); |
| 536 |
} |
| 537 |
|
| 538 |
/*! |
| 539 |
* \ingroup Utilities |
| 540 |
* exi_comp_ws() returns 4 (i.e. sizeof(float)) or 8 (i.e. sizeof(double)), |
| 541 |
* depending on the value of floating point word size used to initialize |
| 542 |
* the conversion facility for this file id (exoid). |
| 543 |
* \param exoid integer which uniquely identifies the file of interest. |
| 544 |
*/ |
| 545 |
int exi_comp_ws(int exoid) |
| 546 |
{ |
| 547 |
struct exi_file_item *file = exi_find_file_item(exoid); |
| 548 |
|
| 549 |
if (!file) { |
| 550 |
char errmsg[MAX_ERR_LENGTH]; |
| 551 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unknown file id %d", exoid); |
| 552 |
ex_err(__func__, errmsg, EX_BADFILEID); |
| 553 |
return EX_FATAL; |
| 554 |
} |
| 555 |
/* Stored as 0 for 4-byte; 1 for 8-byte */ |
| 556 |
return (file->user_compute_wordsize + 1) * 4; |
| 557 |
} |
| 558 |
|
| 559 |
/*! |
| 560 |
* \ingroup Utilities |
| 561 |
* exi_is_parallel() returns 1 (true) or 0 (false) depending on whether |
| 562 |
* the file was opened in parallel or serial/file-per-processor mode. |
| 563 |
* Note that in this case parallel assumes the output of a single file, |
| 564 |
* not a parallel run using file-per-processor. |
| 565 |
* \param exoid integer which uniquely identifies the file of interest. |
| 566 |
*/ |
| 567 |
int exi_is_parallel(int exoid) |
| 568 |
{ |
| 569 |
EX_FUNC_ENTER(); |
| 570 |
struct exi_file_item *file = exi_find_file_item(exoid); |
| 571 |
|
| 572 |
if (!file) { |
| 573 |
char errmsg[MAX_ERR_LENGTH]; |
| 574 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unknown file id %d", exoid); |
| 575 |
ex_err(__func__, errmsg, EX_BADFILEID); |
| 576 |
EX_FUNC_LEAVE(EX_FATAL); |
| 577 |
} |
| 578 |
/* Stored as 1 for parallel, 0 for serial or file-per-processor */ |
| 579 |
EX_FUNC_LEAVE(file->is_parallel); |
| 580 |
} |
| 581 |
|
| 582 |
/*! |
| 583 |
* \ingroup Utilities |
| 584 |
* \note |
| 585 |
* Do not use this unless you know what you are doing and why you |
| 586 |
* are doing it. One use is if calling ex_get_partial_set() in a |
| 587 |
* serial mode (proc 0 only) on a file opened in parallel. |
| 588 |
* Make sure to reset the value to original value after done with |
| 589 |
* special case... |
| 590 |
* |
| 591 |
* ex_set_parallel() sets the parallel setting for a file. |
| 592 |
* returns 1 (true) or 0 (false) depending on the current setting. |
| 593 |
* \param exoid integer which uniquely identifies the file of interest. |
| 594 |
* \param is_parallel 1 if parallel, 0 if serial. |
| 595 |
*/ |
| 596 |
int ex_set_parallel(int exoid, int is_parallel) |
| 597 |
{ |
| 598 |
EX_FUNC_ENTER(); |
| 599 |
struct exi_file_item *file = exi_find_file_item(exoid); |
| 600 |
|
| 601 |
if (!file) { |
| 602 |
char errmsg[MAX_ERR_LENGTH]; |
| 603 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: unknown file id %d", exoid); |
| 604 |
ex_err(__func__, errmsg, EX_BADFILEID); |
| 605 |
EX_FUNC_LEAVE(EX_FATAL); |
| 606 |
} |
| 607 |
|
| 608 |
int old_value = file->is_parallel; |
| 609 |
file->is_parallel = is_parallel; |
| 610 |
/* Stored as 1 for parallel, 0 for serial or file-per-processor */ |
| 611 |
EX_FUNC_LEAVE(old_value); |
| 612 |
} |