-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbin2vtk.c
More file actions
executable file
·603 lines (510 loc) · 22.2 KB
/
bin2vtk.c
File metadata and controls
executable file
·603 lines (510 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdarg.h>
#define LONGSTRING 200
char FilePrefix[LONGSTRING], MeshFilePrefix[LONGSTRING];
int Suffix_TMin, Suffix_TMax, Suffix_TDelta, NumFiles, DataType, ND, IntFormat = 0, ScalarTag = 0, VectorTag = 0;
void ProcessCommandLineArguements(int argc, char *argv[]);
void PrintUsage(void);
void ProcessTracerData(void);
void ProcessUnstructuredData(void);
void ReadCoordinateData(int *NumNodes, double **Coordinates);
void ReadConnectivityData(int *NumElements, int **Connectivity);
void ProcessCartesianData(void);
void FatalError(char *text, ...);
int main(int argc, char *argv[]) {
ProcessCommandLineArguements(argc, argv);
if(DataType == 0)
ProcessTracerData();
else if(DataType >= 1 && DataType <= 4)
ProcessUnstructuredData();
else if(DataType >= 5 && DataType <= 8)
ProcessCartesianData();
return 0;
}
void ProcessCommandLineArguements(int argc, char *argv[]) {
if(argc < 2)
PrintUsage();
else if(argc < 7 || argc > 9)
FatalError("Unsupported number of command line arguements\n");
/* Parse argv for parameters */
DataType = atoi(argv[1]);
ND = atoi(argv[2]);
sprintf(FilePrefix, "%s", argv[3]);
Suffix_TMin = atoi(argv[4]);
Suffix_TMax = atoi(argv[5]);
Suffix_TDelta = atoi(argv[6]);
if(Suffix_TMax != Suffix_TMin) {
if(Suffix_TDelta < 1)
FatalError("Suffix_TDelta < 1");
NumFiles = (Suffix_TMax - Suffix_TMin) / Suffix_TDelta + 1;
}
else
NumFiles = 1;
/* Determine mesh file */
if(DataType != 0)
sprintf(MeshFilePrefix, "%s", argv[7]);
/* See if integer flag set */
if(argc == 9) {
if(!strcmp("-i", argv[8]))
IntFormat = 1;
else
FatalError("Unrecognized flag %s\n", argv[8]);
}
/* Check if tracers have scalar attached to them */
if(DataType == 0 && argc == 8) {
if(!strcmp("-s", argv[7])) {
ScalarTag = 1;
printf("ScalarTag being set to 1\n");
}
else if (!strcmp("-v", argv[7])) {
VectorTag = 1;
printf("VectorTag being set to 1\n");
}
else
FatalError("Unrecognized flag %s (Note: tracers cannot have scalar AND vector data)\n", argv[7]);
}
if(ND != 2 && ND != 3)
FatalError("Invalid ND");
if(DataType < 0 && DataType > 8)
FatalError("Invalid DataType");
}
void PrintUsage(void) {
printf("\nUsage:\n");
printf(" bin2vtk DataType ND FilePrefix Start End Delta (MeshFilePrefix) (-flags)\n\n");
printf("Description:\n");
printf(" Converts FilePrefix.#.bin to FilePrefix.#.vtk,\n");
printf(" where # varies from Start to End in increments of Delta.\n");
printf(" ND should 2 or 3 depending if data is 2D or 3D\n");
printf(" flags:\n");
printf(" -i: used for integer valued data\n");
printf(" -s: used if tracer data has scalar assigned to each tracer\n");
printf(" -v: used if tracer data has vector assigned to each tracer\n\n");
printf(" Supported values for DataType:\n");
printf(" 0: Tracer position data\n");
printf(" 1: Scalar unstructured node data*\n");
printf(" 2: Vector unstructured node data*\n");
printf(" 3: Scalar unstructured element data*\n");
printf(" 4: Vector unstructured element data*\n");
printf(" 5: Scalar Cartesian node data**\n");
printf(" 6: Vector Cartesian node data**\n");
printf(" 7: Scalar Cartesian element data**\n");
printf(" 8: Vector Cartesian element data**\n");
printf(" *Requires files MeshFilePrefix_coordinates.bin and MeshFilePrefix_connectivity.bin\n");
printf(" **Requires file MeshFilePrefix_Cartesian.bin\n");
exit(1);
}
void ProcessTracerData(void) {
int i, j, num_tracers;
double X[6], *ScalarArray, *Vx, *Vy, *Vz, time;
char InFile[LONGSTRING], OutFile[LONGSTRING];
FILE *InFileID, *OutFileID;
for(i = 0; i < NumFiles; i++) {
/* File names */
sprintf(InFile, "%s.%d.bin", FilePrefix, Suffix_TMin + i * Suffix_TDelta);
sprintf(OutFile, "%s.%d.vtk", FilePrefix, Suffix_TMin + i * Suffix_TDelta);
printf("Converting %s to %s...", InFile, OutFile);
fflush(stdout);
/* Open files */
if((InFileID = fopen(InFile, "rb")) == NULL)
FatalError("Could not open %s", InFile);
if((OutFileID = fopen(OutFile, "w")) == NULL)
FatalError("Could not open %s", OutFile);
/* Read time stamp */
if(fread(&time, sizeof(double), 1, InFileID) < 1)
FatalError("Could not read time stamp from file %s", InFile);
/* Read data to determine how many points there are */
num_tracers = 0;
if(ScalarTag)
while(!(fread(X, sizeof(double), 4, InFileID) < 4))
num_tracers++;
else if(VectorTag)
while(!(fread(X, sizeof(double), 6, InFileID) < 6))
num_tracers++;
else
while(!(fread(X, sizeof(double), 3, InFileID) < 3))
num_tracers++;
printf("Number of tracers: %d\n", num_tracers);
fflush(stdout);
if(fseek(InFileID, sizeof(double), SEEK_SET))
FatalError("Could not reset stream to start of coordinate values");
/* Write VTK header */
fprintf(OutFileID, "# vtk DataFile Version 3.0\n");
fprintf(OutFileID, "t = %g\n", time);
fprintf(OutFileID, "ASCII\n");
fprintf(OutFileID, "DATASET POLYDATA\n");
fprintf(OutFileID, "\nPOINTS %d float\n", num_tracers);
/* Allocated memeory for attributes as needed */
if(ScalarTag) {
if((ScalarArray = (double *)calloc(num_tracers, sizeof(double))) == NULL)
FatalError("calloc failed for ScalarArray in function ProcessTracerData()");
}
else if(VectorTag) {
if((Vx = (double *)calloc(num_tracers, sizeof(double))) == NULL)
FatalError("calloc failed for Vx in function ProcessTracerData()");
if((Vy = (double *)calloc(num_tracers, sizeof(double))) == NULL)
FatalError("calloc failed for Vy in function ProcessTracerData()");
if((Vz = (double *)calloc(num_tracers, sizeof(double))) == NULL)
FatalError("calloc failed for Vz in function ProcessTracerData()");
}
/* Read/Write data */
for(j = 0; j < num_tracers; j++) {
if(fread(X, sizeof(double), 3, InFileID) < 3)
FatalError("Could not read location of tracer %d of %d from %s", j + 1, num_tracers, InFile);
if(ScalarTag) {
if(fread(&ScalarArray[j], sizeof(double), 1, InFileID) < 1)
FatalError("Could not read scalar value for tracer %d of %d from %s", j + 1, num_tracers, InFile);
}
else if(VectorTag) {
if(fread(&Vx[j], sizeof(double), 1, InFileID) < 1)
FatalError("Could not read vector value 1 for tracer %d of %d from %s", j + 1, num_tracers, InFile);
if(fread(&Vy[j], sizeof(double), 1, InFileID) < 1)
FatalError("Could not read vector value 2 for tracer %d of %d from %s", j + 1, num_tracers, InFile);
if(fread(&Vz[j], sizeof(double), 1, InFileID) < 1)
FatalError("Could not read vector value 3 for tracer %d of %d from %s", j + 1, num_tracers, InFile);
}
if(ND == 3)
fprintf(OutFileID, "%.9f %.9f %.9f\n", X[0], X[1], X[2]);
else
fprintf(OutFileID, "%.9f %.9f 0.0\n", X[0], X[1]);
}
if(ScalarTag) {
fprintf(OutFileID, "\nPOINT_DATA %d\n", num_tracers);
fprintf(OutFileID, "SCALARS scalar float 1\n");
fprintf(OutFileID, "LOOKUP_TABLE default\n");
for(j = 0; j < num_tracers; j++)
fprintf(OutFileID, "%.9f ", ScalarArray[j]);
}
else if(VectorTag) {
fprintf(OutFileID, "\nPOINT_DATA %d\n", num_tracers);
fprintf(OutFileID, "VECTORS vector float\n");
/*fprintf(OutFileID, "LOOKUP_TABLE default\n");*/
for(j = 0; j < num_tracers; j++)
fprintf(OutFileID, "%.9f %.9f %.9f\n", Vx[j], Vy[j], Vz[j]);
}
/* Free memeory for attributes as needed */
if(ScalarTag)
free(ScalarArray);
else if(VectorTag) {
free(Vx);
free(Vy);
free(Vz);
}
printf("OK!\n");
fflush(stdout);
/* Close files */
fclose(InFileID);
fclose(OutFileID);
}
}
void ProcessUnstructuredData(void) {
int i, j, NumNodes, NumElements, **Connectivity, ISV, IVV[3];
double **Coordinates, FSV, FVV[3], time;
char InFile[LONGSTRING], OutFile[LONGSTRING];
FILE *InFileID, *OutFileID;
/* Open coordinate data file */
sprintf(InFile, "%s_coordinates.bin", MeshFilePrefix);
if((InFileID = fopen(InFile, "rb")) == NULL)
FatalError("Could not open %s", InFile);
/* Read number of nodes */
if(fread(&NumNodes, sizeof(int), 1, InFileID) < 1)
FatalError("Could not read number of nodes from %s", InFile);
printf("Reading coordinates for %d nodes...", NumNodes);
fflush(stdout);
/* Allocate memory for Coordinates array */
if((Coordinates = (double **)malloc(NumNodes * sizeof(double *))) == NULL)
FatalError("Malloc failed for Coordinates");
for(i = 0; i < NumNodes; i++)
if((Coordinates[i] = (double *)malloc(3 * sizeof(double))) == NULL)
FatalError("Malloc failed for Coordinates[%d]", i);
/* Read coordinates */
for(i = 0; i < NumNodes; i++) /* { */
if(fread(Coordinates[i], sizeof(double), 3, InFileID) < 3)
FatalError("Could read Coordinates[%d] from %s", i, InFile);
printf("OK!\n");
/* Close coordinates file */
fclose(InFileID);
/* Open connectivity data file */
sprintf(InFile, "%s_connectivity.bin", MeshFilePrefix);
if((InFileID = fopen(InFile, "rb")) == NULL)
FatalError("Could not open %s", InFile);
/* Read number of elements */
if(fread(&NumElements, sizeof(int), 1, InFileID) < 1)
FatalError("Could not read number of elements from %s", InFile);
printf("Reading connectivity for %d elements...", NumElements);
fflush(stdout);
/* Allocate memory for Connectivity array */
if((Connectivity = (int **)malloc(NumElements * sizeof(int *))) == NULL)
FatalError("Malloc failed for Connectivity");
for(i = 0; i < NumElements; i++)
if((Connectivity[i] = (int *)malloc(4 * sizeof(int))) == NULL)
FatalError("Malloc failed for Connectivity[%d]", i);
/* Read connectivity */
for(i = 0; i < NumElements; i++) /* { */
if(fread(Connectivity[i], sizeof(int), 4, InFileID) < 4)
FatalError("Could not read Connectivity[%d] from %s", i, InFile);
printf("OK!\n");
/* Close connectivity file */
fclose(InFileID);
/* Process field data files */
for(i = 0; i < NumFiles; i++) {
/* File names */
sprintf(InFile, "%s.%d.bin", FilePrefix, Suffix_TMin + i * Suffix_TDelta);
sprintf(OutFile, "%s.%d.vtk", FilePrefix, Suffix_TMin + i * Suffix_TDelta);
printf("Converting %s to %s...\n", InFile, OutFile);
fflush(stdout);
/* Open files */
if((InFileID = fopen(InFile, "rb")) == NULL)
FatalError("Could not open %s", InFile);
if((OutFileID = fopen(OutFile, "w")) == NULL)
FatalError("Could not open %s", OutFile);
/* Read time stamp */
if(fread(&time, sizeof(double), 1, InFileID) < 1)
FatalError("Could not read time stamp from file %s", InFile);
printf(" t = %f\n", time);
fflush(stdout);
/* Write VTK header */
fprintf(OutFileID, "# vtk DataFile Version 3.0\n");
fprintf(OutFileID, "t = %.9f\n", time);
fprintf(OutFileID, "ASCII\n");
fprintf(OutFileID, "DATASET UNSTRUCTURED_GRID\n\n");
printf(" Printing coordinates...\n");
fflush(stdout);
/* Print node coordinates */
fprintf(OutFileID, "POINTS %d float\n", NumNodes);
for(j = 0; j < NumNodes; j++) {
if(ND == 3)
fprintf(OutFileID, "%.9f %.9f %.9f\n", Coordinates[j][0], Coordinates[j][1], Coordinates[j][2]);
else
fprintf(OutFileID, "%.9f %.9f 0.0\n", Coordinates[j][0], Coordinates[j][1]);
}
printf(" Printing connectivity...\n");
fflush(stdout);
/* Print connectivity */
if(ND == 3) {
fprintf(OutFileID, "\nCELLS %d %d\n", NumElements, 5*NumElements);
for(j = 0; j < NumElements; j++)
fprintf(OutFileID, "4 %d %d %d %d\n", Connectivity[j][0], Connectivity[j][1], Connectivity[j][2], Connectivity[j][3]);
}
else {
fprintf(OutFileID, "\nCELLS %d %d\n", NumElements, 4*NumElements);
for(j = 0; j < NumElements; j++)
fprintf(OutFileID, "3 %d %d %d\n", Connectivity[j][0], Connectivity[j][1], Connectivity[j][2]);
}
/* Print cell type (10 = VTK_TETRA, 5 = VTK_TRIANGLE) */
fprintf(OutFileID, "\nCELL_TYPES %d\n", NumElements);
if(ND == 3)
for(j = 0; j < NumElements; j++)
fprintf(OutFileID, "10\n");
else
for(j = 0; j < NumElements; j++)
fprintf(OutFileID, "5\n");
/* Print header for field data */
if(DataType == 1 || DataType == 2) /* Node data */
fprintf(OutFileID, "\nPOINT_DATA %d\n", NumNodes);
else /* Element data */
fprintf(OutFileID, "\nCELL_DATA %d\n", NumElements);
if(DataType == 1 || DataType == 3) { /* Scalar values */
if(IntFormat)
fprintf(OutFileID, "SCALARS scalar_field int\n");
else /* Floating point data */
fprintf(OutFileID, "SCALARS scalar_field float\n");
fprintf(OutFileID, "LOOKUP_TABLE default\n");
}
else { /* Vector values */
if(IntFormat)
fprintf(OutFileID, "VECTORS vector_field int\n");
else /* Floating point data */
fprintf(OutFileID, "VECTORS vector_field float\n");
}
printf(" Printing field data...\n");
fflush(stdout);
/* Read/Write data */
for(j = 0; j < ((DataType == 1 || DataType == 2)? NumNodes:NumElements); j++) {
if(DataType == 1 || DataType == 3) { /* Scalar data */
if(IntFormat) {
if(fread(&ISV, sizeof(int), 1, InFileID) < 1)
FatalError("Could not read integer scalar value %d of %d from file %s", j + 1, (DataType == 1 || DataType == 2)? NumNodes:NumElements, InFile);
fprintf(OutFileID, "%d\n", ISV);
}
else { /* Floating point data */
if(fread(&FSV, sizeof(double), 1, InFileID) < 1)
FatalError("Could not read float scalar value %d of %d from file %s", j + 1, (DataType == 1 || DataType == 2)? NumNodes:NumElements, InFile);
fprintf(OutFileID, "%.9f\n", FSV);
}
}
else { /* Vector data */
if(IntFormat) {
if(fread(IVV, sizeof(int), 3, InFileID) < 3)
FatalError("Could not read integer vector value %d of %d from file %s", j + 1, (DataType == 1 || DataType == 2)? NumNodes:NumElements, InFile);
if(ND == 3)
fprintf(OutFileID, "%d %d %d\n", IVV[0], IVV[1], IVV[2]);
else /* ND == 2 */
fprintf(OutFileID, "%d %d\n", IVV[0], IVV[1]);
}
else { /* Floating point data */
if(fread(&FVV, sizeof(double), 3, InFileID) < 3)
FatalError("Could not read float vector value %d of %d from file %s", j + 1, (DataType == 1 || DataType == 2)? NumNodes:NumElements, InFile);
if(ND == 3)
fprintf(OutFileID, "%.9f %.9f %.9f\n", FVV[0], FVV[1], FVV[2]);
else /* ND == 2 */
fprintf(OutFileID, "%.9f %.9f 0.0\n", FVV[0], FVV[1]);
}
}
}
printf("OK!\n");
fflush(stdout);
/* Close files */
fclose(InFileID);
fclose(OutFileID);
}
/* Free memory */
for(i = 0; i < NumNodes; i++)
free(Coordinates[i]);
free(Coordinates);
for(i = 0; i < NumElements; i++)
free(Connectivity[i]);
free(Connectivity);
}
void ProcessCartesianData(void) {
int ii, i, j, k, imax, jmax, kmax, XRes, YRes, ZRes, NumNodes, NumElements;
double time, SV, VV[3], XMin, XMax, XDelta, YMin, YMax, YDelta, ZMin, ZMax, ZDelta;
char InFile[LONGSTRING], OutFile[LONGSTRING];
FILE *InFileID, *OutFileID;
/* Open Cartesian mesh information file */
sprintf(InFile, "%s_Cartesian.bin", MeshFilePrefix);
if((InFileID = fopen(InFile, "rb")) == NULL)
FatalError("Could not open file %s", InFile);
/* Read mesh parameters */
if(fread(&XMin, sizeof(double), 1, InFileID) < 1 ||
fread(&XMax, sizeof(double), 1, InFileID) < 1 ||
fread(&XRes, sizeof(int), 1, InFileID) < 1 ||
fread(&YMin, sizeof(double), 1, InFileID) < 1 ||
fread(&YMax, sizeof(double), 1, InFileID) < 1 ||
fread(&YRes, sizeof(int), 1, InFileID) < 1 ||
fread(&ZMin, sizeof(double), 1, InFileID) < 1 ||
fread(&ZMax, sizeof(double), 1, InFileID) < 1 ||
fread(&ZRes, sizeof(int), 1, InFileID) < 1)
FatalError("Could not read Catesian mesh parameters from %s", InFile);
/* Close file */
fclose(InFileID);
/* Derived parameters */
XDelta = (XMax - XMin) / (XRes - 1);
YDelta = (YMax - YMin) / (YRes - 1);
if(ND == 3)
ZDelta = (ZMax - ZMin) / (ZRes - 1);
else {
ZDelta = 0;
ZRes = 1;
}
NumNodes = XRes * YRes * ZRes;
if(ND == 3)
NumElements = (XRes - 1) * (YRes - 1) * (ZRes - 1);
else
NumElements = (XRes - 1) * (YRes - 1);
for(ii = 0; ii < NumFiles; ii++) {
/* File names */
sprintf(InFile, "%s.%d.bin", FilePrefix, Suffix_TMin + ii * Suffix_TDelta);
sprintf(OutFile, "%s.%d.vtk", FilePrefix, Suffix_TMin + ii * Suffix_TDelta);
printf("Converting %s to %s...", InFile, OutFile);
fflush(stdout);
/* Open files */
if((InFileID = fopen(InFile, "rb")) == NULL)
FatalError("Could not open %s", InFile);
if((OutFileID = fopen(OutFile, "w")) == NULL)
FatalError("Could not open %s", OutFile);
/* Read time stamp */
if(fread(&time, sizeof(double), 1, InFileID) < 1)
FatalError("Could not read time stamp from file %s", InFile);
/* Write VTK header */
fprintf(OutFileID, "# vtk DataFile Version 3.0\n");
fprintf(OutFileID, "t = %.9f\n", time);
fprintf(OutFileID, "ASCII\n");
fprintf(OutFileID, "DATASET STRUCTURED_POINTS\n");
fprintf(OutFileID, "DIMENSIONS %d %d %d\n", XRes, YRes, ZRes);
fprintf(OutFileID, "ORIGIN %f %f %f\n", XMin, YMin, ZMin);
fprintf(OutFileID, "SPACING %.9f %.9f %.9f\n", XDelta, YDelta, ZDelta);
/* Print header for field data */
if(DataType == 5 || DataType == 6) /* Node data */
fprintf(OutFileID, "\nPOINT_DATA %d\n", NumNodes);
else if(DataType == 7 || DataType == 8) /* Element data */
fprintf(OutFileID, "\nCELL_DATA %d\n", NumElements);
else
FatalError("Unexpected value, DataType = %d");
if(DataType == 5 || DataType == 7) { /* Scalar values */
if(IntFormat)
fprintf(OutFileID, "SCALARS scalar_field int\n");
else /* Floating point data */
fprintf(OutFileID, "SCALARS scalar_field float\n");
fprintf(OutFileID, "LOOKUP_TABLE default\n");
}
else { /* Vector values */
if(IntFormat)
fprintf(OutFileID, "VECTORS vector_field int\n");
else /* Floating point data */
fprintf(OutFileID, "VECTORS vector_field float\n");
}
/* Read/Write data */
imax = (DataType == 5 || DataType == 6)?XRes:(XRes-1);
jmax = (DataType == 5 || DataType == 6)?YRes:(YRes-1);
if(ND == 3)
kmax = (DataType == 5 || DataType == 6)?ZRes:(ZRes-1);
else
kmax = 1;
for(k = 0; k < kmax; k++)
for(j = 0; j < jmax; j++)
for(i = 0; i < imax; i++)
if(DataType == 5 || DataType == 7) { /* Scalar data */
if(fread(&SV, sizeof(double), 1, InFileID) < 1)
FatalError("Could read scalar value (%d, %d, %d) from file %s, SV = %f", i, j, k, InFile, SV);
fprintf(OutFileID, "%.9f\n", SV);
}
else { /* Vector data */
if(fread(VV, sizeof(double), 3, InFileID) < 3)
FatalError("Could not read vector value (%d, %d, %d) from file %s", i, j, k, InFile);
fprintf(OutFileID, "%.9f %.9f %.9f\n", VV[0], VV[1], VV[2]);
}
printf("OK!\n");
fflush(stdout);
/* Close files */
fclose(InFileID);
fclose(OutFileID);
}
}
void FatalError(char *text, ...) {
va_list ap;
char *p, *sval;
int ival;
double dval;
fprintf(stderr, "\nERROR:\n");
va_start(ap, text);
for(p = text; *p; p++) {
if(*p != '%') {
putc(*p, stderr);
continue;
}
switch (*++p) {
case 'd':
ival = va_arg(ap, int);
fprintf(stderr, "%d", ival);
break;
case 'f':
dval = va_arg(ap, double);
fprintf(stderr, "%f", dval);
break;
case 's':
for(sval = va_arg(ap, char *); *sval; sval++)
putc(*sval, stderr);
break;
default:
putc(*p, stderr);
break;
}
}
va_end(ap);
fprintf(stderr, "\n");
fflush(stderr);
exit(1);
}