| 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 |
* |
| 10 |
* exopen - ex_open |
| 11 |
* |
| 12 |
* entry conditions - |
| 13 |
* input parameters: |
| 14 |
* char* path exodus filename path |
| 15 |
* int mode access mode w/r |
| 16 |
* |
| 17 |
* exit conditions - |
| 18 |
* int exoid exodus file id |
| 19 |
* int* comp_ws computer word size |
| 20 |
* int* io_ws storage word size |
| 21 |
* float* version EXODUS interface version number |
| 22 |
* |
| 23 |
* revision history - |
| 24 |
* |
| 25 |
* |
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
#include "exodusII.h" // for ex_err, etc |
| 29 |
#include "exodusII_int.h" // for EX_FATAL, etc |
| 30 |
#include <stdlib.h> |
| 31 |
/*! |
| 32 |
\ingroup Utilities |
| 33 |
|
| 34 |
\note The ex_open_int() is an internal function called by |
| 35 |
ex_open(). The user should call ex_open() and not ex_open_int(). |
| 36 |
|
| 37 |
The function ex_open() opens an existing exodus file and returns |
| 38 |
an ID that can subsequently be used to refer to the file, the word |
| 39 |
size of the floating point values stored in the file, and the version |
| 40 |
of the exodus database (returned as a ``float'', regardless of the |
| 41 |
compute or I/O word size). Multiple files may be ``open'' simultaneously. |
| 42 |
|
| 43 |
\return In case of an error, ex_open() returns a negative |
| 44 |
number. Possible causes of errors include: |
| 45 |
- The specified file does not exist. |
| 46 |
- The mode specified is something other than the predefined constant |
| 47 |
\fparam{#EX_READ} or \fparam{#EX_WRITE}. |
| 48 |
- Database version is earlier than 2.0. |
| 49 |
|
| 50 |
\param path The file name of the exodus file. This can be given as either an |
| 51 |
absolute path name (from the root of the file system) or a relative |
| 52 |
path name (from the current directory). |
| 53 |
|
| 54 |
\param mode Access mode. Use one of the following predefined constants: |
| 55 |
- \fparam{EX_READ} To open the file just for reading. |
| 56 |
- \fparam{EX_WRITE} To open the file for writing and reading. |
| 57 |
|
| 58 |
\param[in,out] comp_ws The word size in bytes (0, 4 or 8) of the floating point |
| 59 |
variables |
| 60 |
used in the application program. If 0 (zero) is passed, the |
| 61 |
default size of floating point values for the machine will be |
| 62 |
used and returned in this variable. WARNING: all exodus functions |
| 63 |
requiring reals must be passed reals declared with this passed |
| 64 |
in or returned compute word size (4 or 8). |
| 65 |
|
| 66 |
\param[in,out] io_ws The word size in bytes (0, 4 or 8) of the floating |
| 67 |
point data as they are stored in the exodus file. If the |
| 68 |
word size does not match the word size of data stored in |
| 69 |
the file, a fatal error is returned. If this argument is |
| 70 |
0, the word size of the floating point data already |
| 71 |
stored in the file is returned. |
| 72 |
|
| 73 |
\param[out] version Returned exodus database version number. Note that this is always a float, |
| 74 |
never a double. |
| 75 |
|
| 76 |
\param[in] run_version Internally generated to verify library compatibility. |
| 77 |
|
| 78 |
The following opens an exodus file named \file{test.exo} for read |
| 79 |
only, using default settings for compute and I/O word sizes: |
| 80 |
|
| 81 |
~~~{.c} |
| 82 |
int CPU_word_size,IO_word_size, exoid; |
| 83 |
float version; |
| 84 |
|
| 85 |
CPU_word_size = sizeof(float); \co{float or double} |
| 86 |
IO_word_size = 0; \co{use what is stored in file} |
| 87 |
|
| 88 |
\comment{open exodus files} |
| 89 |
exoid = ex_open ("test.exo", \co{filename path} |
| 90 |
EX_READ, \co{access mode = READ} |
| 91 |
&CPU_word_size, \co{CPU word size} |
| 92 |
&IO_word_size, \co{IO word size} |
| 93 |
&version); \co{ExodusII library version} |
| 94 |
~~~ |
| 95 |
*/ |
| 96 |
|
| 97 |
/* NOTE: Do *not* call `ex_open_int()` directly. The public API |
| 98 |
* function name is `ex_open()` which is a wrapper that calls |
| 99 |
* `ex_open_int` with an additional argument to make sure |
| 100 |
* library and include file are consistent |
| 101 |
*/ |
| 102 |
int ex_open_int(const char *path, int mode, int *comp_ws, int *io_ws, float *version, |
| 103 |
int run_version) |
| 104 |
{ |
| 105 |
int exoid = -1; |
| 106 |
int status = 0; |
| 107 |
nc_type att_type = NC_NAT; |
| 108 |
int old_fill = 0; |
| 109 |
int file_wordsize = 0; |
| 110 |
int int64_status = 0; |
| 111 |
int nc_mode = 0; |
| 112 |
|
| 113 |
char errmsg[MAX_ERR_LENGTH]; |
| 114 |
|
| 115 |
EX_FUNC_ENTER(); |
| 116 |
*version = 0.0f; |
| 117 |
|
| 118 |
/* set error handling mode to no messages, non-fatal errors */ |
| 119 |
ex_opts(exoptval); /* call required to set ncopts first time through */ |
| 120 |
|
| 121 |
exi_check_version(run_version); |
| 122 |
|
| 123 |
if ((mode & EX_READ) && (mode & EX_WRITE)) { |
| 124 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Cannot specify both EX_READ and EX_WRITE"); |
| 125 |
ex_err(__func__, errmsg, EX_BADFILEMODE); |
| 126 |
EX_FUNC_LEAVE(EX_FATAL); |
| 127 |
} |
| 128 |
|
| 129 |
if (!path || strlen(path) == 0) { |
| 130 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Filename is not specified."); |
| 131 |
ex_err(__func__, errmsg, EX_BADFILEMODE); |
| 132 |
EX_FUNC_LEAVE(EX_FATAL); |
| 133 |
} |
| 134 |
|
| 135 |
char *canon_path = exi_canonicalize_filename(path); |
| 136 |
|
| 137 |
/* Verify that this file is not already open for read or write... |
| 138 |
In theory, should be ok for the file to be open multiple times |
| 139 |
for read, but bad things can happen if being read and written |
| 140 |
at the same time... |
| 141 |
*/ |
| 142 |
if (exi_check_multiple_open(canon_path, mode, __func__)) { |
| 143 |
free(canon_path); |
| 144 |
EX_FUNC_LEAVE(EX_FATAL); |
| 145 |
} |
| 146 |
|
| 147 |
/* The EX_READ mode is the default if EX_WRITE is not specified... */ |
| 148 |
if (!(mode & EX_WRITE)) { /* READ ONLY */ |
| 149 |
nc_mode = NC_NOWRITE | NC_SHARE; |
| 150 |
|
| 151 |
#if NC_HAS_DISKLESS |
| 152 |
if (mode & EX_DISKLESS) { |
| 153 |
nc_mode |= NC_DISKLESS; |
| 154 |
} |
| 155 |
#endif |
| 156 |
|
| 157 |
if ((status = nc_open(canon_path, nc_mode, &exoid)) != NC_NOERR) { |
| 158 |
/* NOTE: netCDF returns an id of -1 on an error - but no error code! */ |
| 159 |
/* It is possible that the user is trying to open a netcdf4 |
| 160 |
file, but the netcdf4 capabilities aren't available in the |
| 161 |
netcdf linked to this library. Note that we can't just use a |
| 162 |
compile-time define since we could be using a shareable |
| 163 |
netcdf library, so the netcdf4 capabilities aren't known |
| 164 |
until runtime... |
| 165 |
|
| 166 |
Later versions of netcdf-4.X have a function that can be |
| 167 |
queried to determine whether the library being used was |
| 168 |
compiled with --enable-netcdf4, but not everyone is using that |
| 169 |
version yet, so we may have to do some guessing... |
| 170 |
|
| 171 |
At this time, query the beginning of the file and see if it |
| 172 |
is an HDF-5 file and if it is assume that the open failure |
| 173 |
is due to the netcdf library not enabling netcdf4 features unless |
| 174 |
we have the define that shows it is enabled, then assume other error... |
| 175 |
*/ |
| 176 |
int type = 0; |
| 177 |
ex_opts(EX_VERBOSE); |
| 178 |
|
| 179 |
exi_check_file_type(canon_path, &type); |
| 180 |
|
| 181 |
if (type == 0) { |
| 182 |
/* Error message printed at lower level */ |
| 183 |
} |
| 184 |
else if (type == 5) { |
| 185 |
#if NC_HAS_HDF5 |
| 186 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 187 |
"EXODUS: ERROR: Attempting to open the netcdf-4 " |
| 188 |
"file:\n\t'%s'\n\tfailed. The netcdf library supports " |
| 189 |
"netcdf-4 so there must be a filesystem or some other " |
| 190 |
"issue.\n", |
| 191 |
canon_path); |
| 192 |
ex_err(__func__, errmsg, status); |
| 193 |
free(canon_path); |
| 194 |
EX_FUNC_LEAVE(EX_FATAL); |
| 195 |
#else |
| 196 |
/* This is an hdf5 (netcdf4) file. If NC_HAS_HDF5 is not defined, |
| 197 |
then we either don't have hdf5 support in this netcdf version, |
| 198 |
OR this is an older netcdf version that doesn't provide that define. |
| 199 |
|
| 200 |
In either case, we don't have enough information, so we |
| 201 |
assume that the netcdf doesn't have netcdf4 capabilities |
| 202 |
enabled. Tell the user... |
| 203 |
*/ |
| 204 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 205 |
"EXODUS: ERROR: Attempting to open the netcdf-4 " |
| 206 |
"file:\n\t'%s'.\n\tEither the netcdf library does not " |
| 207 |
"support netcdf-4 or there is a filesystem or some " |
| 208 |
"other issue.\n", |
| 209 |
canon_path); |
| 210 |
ex_err(__func__, errmsg, status); |
| 211 |
free(canon_path); |
| 212 |
EX_FUNC_LEAVE(EX_FATAL); |
| 213 |
#endif |
| 214 |
} |
| 215 |
else if (type == 4) { |
| 216 |
#if NC_HAS_CDF5 |
| 217 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 218 |
"EXODUS: ERROR: Attempting to open the CDF5 " |
| 219 |
"file:\n\t'%s'\n\tfailed. The netcdf library supports " |
| 220 |
"CDF5-type files so there must be a filesystem or some other " |
| 221 |
"issue \n", |
| 222 |
canon_path); |
| 223 |
ex_err(__func__, errmsg, status); |
| 224 |
free(canon_path); |
| 225 |
EX_FUNC_LEAVE(EX_FATAL); |
| 226 |
#else |
| 227 |
/* This is an cdf5 (64BIT_DATA) file. If NC_64BIT_DATA is not defined, |
| 228 |
then we either don't have cdf5 support in this netcdf version, |
| 229 |
OR this is an older netcdf version that doesn't provide that define. |
| 230 |
|
| 231 |
In either case, we don't have enough information, so we |
| 232 |
assume that the netcdf doesn't have cdf5 capabilities |
| 233 |
enabled. Tell the user... |
| 234 |
*/ |
| 235 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 236 |
"EXODUS: ERROR: Attempting to open the CDF5 " |
| 237 |
"file:\n\t'%s'.\n\tEither the netcdf library does not " |
| 238 |
"support CDF5 or there is a filesystem or some " |
| 239 |
"other issue \n", |
| 240 |
canon_path); |
| 241 |
ex_err(__func__, errmsg, status); |
| 242 |
free(canon_path); |
| 243 |
EX_FUNC_LEAVE(EX_FATAL); |
| 244 |
|
| 245 |
#endif |
| 246 |
} |
| 247 |
else if (type == 1) { |
| 248 |
/* Possibly an issue with an older file created by a Java-based NetCDF library which wrote |
| 249 |
* corrupted data which is now being checked by newer releases of the NetCDF library... |
| 250 |
* SEE: https://github.com/Unidata/netcdf-c/issues/1115 |
| 251 |
*/ |
| 252 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 253 |
"ERROR: failed to open '%s' of type %d for reading.\n" |
| 254 |
"\t\tIf this is an old file, it is possible that it has some internal corruption\n" |
| 255 |
"\t\tthat is now being checked by recent versions of the NetCDF library.\n" |
| 256 |
"\t\tTo fix, you can find an older version of `nccopy` (prior to 4.6.0)\n" |
| 257 |
"\t\tthen try `nccopy bad_file.g fixed_file.g`.", |
| 258 |
canon_path, type); |
| 259 |
ex_err(__func__, errmsg, status); |
| 260 |
free(canon_path); |
| 261 |
EX_FUNC_LEAVE(EX_FATAL); |
| 262 |
} |
| 263 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 264 |
"ERROR: failed to open %s of type %d for reading.\n\t\tThe " |
| 265 |
"file does not exist, or there is a permission or file " |
| 266 |
"format issue.", |
| 267 |
canon_path, type); |
| 268 |
ex_err(__func__, errmsg, status); |
| 269 |
free(canon_path); |
| 270 |
EX_FUNC_LEAVE(EX_FATAL); |
| 271 |
} |
| 272 |
} |
| 273 |
else { /* (mode & EX_WRITE) READ/WRITE */ |
| 274 |
nc_mode = NC_WRITE | NC_SHARE; |
| 275 |
#if NC_HAS_DISKLESS |
| 276 |
if (mode & EX_DISKLESS) { |
| 277 |
nc_mode |= NC_DISKLESS; |
| 278 |
#if defined NC_PERSIST |
| 279 |
nc_mode |= NC_PERSIST; |
| 280 |
#endif |
| 281 |
} |
| 282 |
#endif |
| 283 |
if ((status = nc_open(canon_path, nc_mode, &exoid)) != NC_NOERR) { |
| 284 |
/* NOTE: netCDF returns an id of -1 on an error - but no error code! */ |
| 285 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 286 |
"ERROR: failed to open %s for read/write.\n\tEither the file " |
| 287 |
"does not exist,\n\tor there is a permission or file format " |
| 288 |
"issue.", |
| 289 |
canon_path); |
| 290 |
ex_err(__func__, errmsg, status); |
| 291 |
free(canon_path); |
| 292 |
EX_FUNC_LEAVE(EX_FATAL); |
| 293 |
} |
| 294 |
|
| 295 |
/* turn off automatic filling of netCDF variables */ |
| 296 |
if ((status = nc_set_fill(exoid, NC_NOFILL, &old_fill)) != NC_NOERR) { |
| 297 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to set nofill mode in file id %d named %s", |
| 298 |
exoid, canon_path); |
| 299 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 300 |
free(canon_path); |
| 301 |
EX_FUNC_LEAVE(EX_FATAL); |
| 302 |
} |
| 303 |
|
| 304 |
size_t att_len = 0; |
| 305 |
int stat_att = nc_inq_att(exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, &att_type, &att_len); |
| 306 |
int dim_str_name = 0; |
| 307 |
int stat_dim = nc_inq_dimid(exoid, DIM_STR_NAME, &dim_str_name); |
| 308 |
if (stat_att != NC_NOERR || stat_dim != NC_NOERR) { |
| 309 |
/* This must still be nc_redef */ |
| 310 |
if ((status = nc_redef(exoid)) != NC_NOERR) { |
| 311 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 312 |
"ERROR: failed to place file id %d named %s into define mode", exoid, canon_path); |
| 313 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 314 |
free(canon_path); |
| 315 |
EX_FUNC_LEAVE(EX_FATAL); |
| 316 |
} |
| 317 |
|
| 318 |
if (stat_att != NC_NOERR) { |
| 319 |
int max_so_far = 32; |
| 320 |
if ((status = nc_put_att_int(exoid, NC_GLOBAL, ATT_MAX_NAME_LENGTH, NC_INT, 1, |
| 321 |
&max_so_far)) != NC_NOERR) { |
| 322 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 323 |
"ERROR: failed to add maximum_name_length attribute in file id %d", exoid); |
| 324 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 325 |
free(canon_path); |
| 326 |
EX_FUNC_LEAVE(EX_FATAL); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
/* If the DIM_STR_NAME variable does not exist on the database, we need to |
| 331 |
* add it now. */ |
| 332 |
if (stat_dim != NC_NOERR) { |
| 333 |
/* Not found; set to default value of 32+1. */ |
| 334 |
int max_name = exi_default_max_name_length < 32 ? 32 : exi_default_max_name_length; |
| 335 |
if ((status = nc_def_dim(exoid, DIM_STR_NAME, max_name + 1, &dim_str_name)) != NC_NOERR) { |
| 336 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 337 |
"ERROR: failed to define string name dimension in file id %d named %s", exoid, |
| 338 |
canon_path); |
| 339 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 340 |
free(canon_path); |
| 341 |
EX_FUNC_LEAVE(EX_FATAL); |
| 342 |
} |
| 343 |
} |
| 344 |
if ((status = nc_enddef(exoid)) != NC_NOERR) { |
| 345 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode in file id %d", exoid); |
| 346 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 347 |
free(canon_path); |
| 348 |
EX_FUNC_LEAVE(EX_FATAL); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
/* determine version of EXODUS file, and the word size of |
| 354 |
* floating point and integer values stored in the file |
| 355 |
*/ |
| 356 |
|
| 357 |
if ((status = nc_get_att_float(exoid, NC_GLOBAL, ATT_VERSION, version)) != NC_NOERR) { |
| 358 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get database version for file id: %d", |
| 359 |
exoid); |
| 360 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 361 |
free(canon_path); |
| 362 |
EX_FUNC_LEAVE(EX_FATAL); |
| 363 |
} |
| 364 |
|
| 365 |
/* check ExodusII file version - old version 1.x files are not supported */ |
| 366 |
if (*version < 2.0) { |
| 367 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Unsupported file version %.2f in file id: %d", |
| 368 |
*version, exoid); |
| 369 |
ex_err_fn(exoid, __func__, errmsg, EX_BADPARAM); |
| 370 |
free(canon_path); |
| 371 |
EX_FUNC_LEAVE(EX_FATAL); |
| 372 |
} |
| 373 |
|
| 374 |
if (nc_get_att_int(exoid, NC_GLOBAL, ATT_FLT_WORDSIZE, &file_wordsize) != NC_NOERR) { |
| 375 |
/* try old (prior to db version 2.02) attribute name */ |
| 376 |
if ((status = nc_get_att_int(exoid, NC_GLOBAL, ATT_FLT_WORDSIZE_BLANK, &file_wordsize)) != |
| 377 |
NC_NOERR) { |
| 378 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to get file wordsize from file id: %d", |
| 379 |
exoid); |
| 380 |
ex_err_fn(exoid, __func__, errmsg, status); |
| 381 |
free(canon_path); |
| 382 |
EX_FUNC_LEAVE(EX_FATAL); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
/* See if int64 status attribute exists and if so, what data is stored as |
| 387 |
* int64 |
| 388 |
* Older files don't have the attribute, so it is not an error if it is |
| 389 |
* missing |
| 390 |
*/ |
| 391 |
if (nc_get_att_int(exoid, NC_GLOBAL, ATT_INT64_STATUS, &int64_status) != NC_NOERR) { |
| 392 |
int64_status = 0; /* Just in case it gets munged by a failed get_att_int call */ |
| 393 |
} |
| 394 |
|
| 395 |
/* Merge in API int64 status flags as specified by caller of function... */ |
| 396 |
int64_status |= (mode & EX_ALL_INT64_API); |
| 397 |
|
| 398 |
/* Verify that there is not an existing file_item struct for this |
| 399 |
exoid This could happen (and has) when application calls |
| 400 |
ex_open(), but then closes file using nc_close() and then reopens |
| 401 |
file. NetCDF will possibly reuse the exoid which results in |
| 402 |
internal corruption in exodus data structures since exodus does |
| 403 |
not know that file was closed and possibly new file opened for |
| 404 |
this exoid |
| 405 |
*/ |
| 406 |
if (exi_find_file_item(exoid) != NULL) { |
| 407 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 408 |
"ERROR: There is an existing file already using the file " |
| 409 |
"id %d which was also assigned to file %s.\n\tWas " |
| 410 |
"nc_close() called instead of ex_close() on an open Exodus " |
| 411 |
"file?\n", |
| 412 |
exoid, canon_path); |
| 413 |
ex_err_fn(exoid, __func__, errmsg, EX_BADFILEID); |
| 414 |
nc_close(exoid); |
| 415 |
free(canon_path); |
| 416 |
EX_FUNC_LEAVE(EX_FATAL); |
| 417 |
} |
| 418 |
|
| 419 |
/* initialize floating point and integer size conversion. */ |
| 420 |
if (exi_conv_init(exoid, comp_ws, io_ws, file_wordsize, int64_status, false, false, false, |
| 421 |
mode & EX_WRITE) != EX_NOERR) { |
| 422 |
snprintf(errmsg, MAX_ERR_LENGTH, |
| 423 |
"ERROR: failed to initialize conversion routines in file id %d named %s", exoid, |
| 424 |
canon_path); |
| 425 |
ex_err_fn(exoid, __func__, errmsg, EX_LASTERR); |
| 426 |
free(canon_path); |
| 427 |
EX_FUNC_LEAVE(EX_FATAL); |
| 428 |
} |
| 429 |
|
| 430 |
free(canon_path); |
| 431 |
EX_FUNC_LEAVE(exoid); |
| 432 |
} |