Debian Codegraph @

packages/seacas/applications/exo_format/exo_format.c

raw
1 /*
2 * Copyright(C) 1999-2021, 2023, 2025 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 * Determine the format of the exodus file:
11 * netcdf format: classic, 64-bit offset, netcdf-4, netcdf-4-classic
12 * exodus version,
13 * integer sizes for ids, maps, bulk data
14 * exodus file-size attribute (normal, large)
15 * change-set (netcdf groups) count
16 */
17
18 #include "exodusII.h"
19 #include "netcdf.h"
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #ifndef EXIT_SUCCESS
26 #define EXIT_SUCCESS 0
27 #endif
28 #ifndef EXIT_FAILURE
29 #define EXIT_FAILURE 1
30 #endif
31
32 #define NOT_NETCDF 2
33 #define NOT_EXODUSII 3
34
35 int main(int argc, char *argv[])
36 {
37 /* Determine if filename was given */
38 if (argc <= 1) {
39 fprintf(stderr, "USAGE: %s [-config] {filename}\n", argv[0]);
40 exit(EXIT_FAILURE);
41 }
42
43 int fn_idx = 1;
44 if (argv[1][0] == '-') {
45 if (argv[1][1] == 'c' || (argv[1][1] == '-' && argv[1][2] == 'c')) {
46 fprintf(stderr, "\nExodus Configuration Information:\n");
47 ex_print_config();
48 }
49 fn_idx = 2;
50 if (argc <= 2) {
51 exit(0);
52 }
53 }
54
55 /* examine file */
56 char *filename = argv[fn_idx]; /* filename path */
57
58 FILE *fid = fopen(filename, "r");
59 if (fid == NULL) {
60 fprintf(stderr, " Could not open %s\n", filename);
61 exit(EXIT_FAILURE);
62 }
63 int c1 = getc(fid);
64 int c2 = getc(fid);
65 int c3 = getc(fid);
66 int c4 = getc(fid);
67 fclose(fid);
68
69 bool netcdf_based = false;
70 bool hdf5_based = false;
71 if (c1 == 'C' && c2 == 'D' && c3 == 'F') {
72 netcdf_based = true;
73 }
74 else if (c2 == 'H' && c3 == 'D' && c4 == 'F') {
75 hdf5_based = true;
76 }
77 else {
78 fprintf(stderr, " %s is not an EXODUS or netCDF file\n", filename);
79 exit(NOT_NETCDF);
80 }
81 float version;
82 int CPU_word_size = 0; /* float or double */
83 int IO_word_size = 0; /* use what is stored in file */
84
85 int exoid = ex_open(filename, EX_READ, /* access mode = READ */
86 &CPU_word_size, /* CPU word size */
87 &IO_word_size, /* IO word size */
88 &version); /* Exodus library version */
89
90 if (exoid < 0) {
91 if (netcdf_based) {
92 fprintf(stderr, " %s is a NetCDF file, but not a valid EXODUS file\n", filename);
93 }
94 else if (hdf5_based) {
95 fprintf(stderr, " %s is an HDF5 file, but not a valid EXODUS file.\n", filename);
96 }
97 exit(NOT_EXODUSII);
98 }
99
100 int file_size = ex_large_model(exoid);
101
102 fprintf(stderr, "\n\t%s is an EXODUS file, version %4.2f\n\n", filename, version);
103
104 /* Determine int sizes */
105 int int64_status = ex_int64_status(exoid);
106 if (int64_status & EX_IDS_INT64_DB) {
107 fprintf(stderr, "\t\tIDs are stored as 64-bit integers\n");
108 }
109 else {
110 fprintf(stderr, "\t\tIDs are stored as 32-bit integers\n");
111 }
112
113 if (int64_status & EX_MAPS_INT64_DB) {
114 fprintf(stderr, "\t\tMap entries are stored as 64-bit integers\n");
115 }
116 else {
117 fprintf(stderr, "\t\tMap entries are stored as 32-bit integers\n");
118 }
119
120 if (int64_status & EX_BULK_INT64_DB) {
121 fprintf(stderr, "\t\tBulk data are stored as 64-bit integers\n");
122 }
123 else {
124 fprintf(stderr, "\t\tBulk data are stored as 32-bit integers\n");
125 }
126
127 if (IO_word_size == 4) {
128 fprintf(stderr, "\t\tFloating point data are stored as 32-bit floats\n");
129 }
130 else {
131 fprintf(stderr, "\t\tFloating point data are stored as 64-bit doubles\n");
132 }
133 int max_name_length = (int)ex_inquire_int(exoid, EX_INQ_DB_MAX_USED_NAME_LENGTH);
134 fprintf(stderr, "\n\t\tMaximum name length is %d\n", max_name_length);
135
136 int num_change_sets = ex_inquire_int(exoid, EX_INQ_NUM_CHILD_GROUPS);
137 if (num_change_sets > 0) {
138 fprintf(stderr, "\t\tFile contains %d change sets (groups)\n", num_change_sets);
139 }
140
141 if (file_size == 0) {
142 fprintf(stderr, "\n\t\tFile size attribute is 'normal model'\n");
143 }
144 else {
145 fprintf(stderr, "\n\t\tFile size attribute is 'large model'\n");
146 }
147
148 /* Determine netcdf file version... */
149 int nc_format = 0;
150 nc_inq_format(exoid, &nc_format);
151 if (nc_format == NC_FORMAT_CLASSIC) {
152 fprintf(stderr, "\t\tNetCDF Variant is 'classic'\n");
153 }
154 else if (nc_format == NC_FORMAT_64BIT) {
155 fprintf(stderr, "\t\tNetCDF Variant is '64-bit offset'\n");
156 }
157 #if defined NC_FORMAT_64BIT_DATA
158 else if (nc_format == NC_FORMAT_64BIT_DATA) {
159 fprintf(stderr, "\t\tNetCDF Variant is '64-bit data (CDF5)'\n");
160 }
161 #endif
162 else if (nc_format == NC_FORMAT_NETCDF4) {
163 fprintf(stderr, "\t\tNetCDF Variant is 'netCDF-4'\n");
164 }
165 else if (nc_format == NC_FORMAT_NETCDF4_CLASSIC) {
166 fprintf(stderr, "\t\tNetCDF Variant is 'netCDF-4 classic'\n");
167 }
168 else {
169 fprintf(stderr, "\t\tNetCDF Variant is 'unknown'\n");
170 }
171
172 if (netcdf_based) {
173 fprintf(stderr, "\t\tUnderlying data format is netcdf\n");
174 }
175 if (hdf5_based) {
176 fprintf(stderr, "\t\tUnderlying data format is hdf5\n");
177 }
178
179 fprintf(stderr, "\n");
180
181 /* Determine number of dims and vars -- useful in debugging incorrect NC_MAX_DIMS|VARS in netcdf.h
182 */
183 #if NC_HAS_HDF5
184 {
185 int ndims = 0;
186 int nvars = 0;
187 nc_inq_dimids(exoid, &ndims, NULL, 0);
188 nc_inq_varids(exoid, &nvars, NULL);
189 fprintf(stderr, "\t\tNumber of dims = %d\n", ndims);
190 fprintf(stderr, "\t\tNumber of vars = %d\n", nvars);
191 }
192 #endif
193
194 if (ex_close(exoid) == -1) {
195 printf("ex_close failed");
196 }
197
198 version += 0.00005F;
199 char cversion[9];
200 snprintf(cversion, 9, "%4.2f", version);
201
202 size_t k = strlen(cversion);
203 size_t j = 0;
204 for (; j < k; j++) {
205 if (cversion[j] == '.') {
206 break;
207 }
208 }
209 if (j == k) {
210 fprintf(stderr, " %s is not an EXODUS file\n", filename);
211 exit(NOT_EXODUSII);
212 }
213
214 exit(-2);
215 }