-
Notifications
You must be signed in to change notification settings - Fork 6
/
sdffilter.c
2240 lines (1952 loc) · 66.8 KB
/
sdffilter.c
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* sdffilter - filter the contents of an SDF file
* Copyright (C) 2013-2016 SDF Development Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <sys/stat.h>
#include <math.h>
#include <time.h>
#include <float.h>
#include <limits.h>
#include "sdf.h"
#include "sdf_list_type.h"
#include "sdf_helper.h"
#include "stack_allocator.h"
#include "commit_info.h"
#ifdef PARALLEL
#include <mpi.h>
#endif
#define VERSION "2.6.12"
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
int metadata, contents, debug, single, use_mmap, ignore_summary, ascii_header;
int exclude_variables, derived, extension_info, index_offset, element_count;
int just_id, verbose_metadata, special_format, scale_factor;
int format_rowindex, format_index, format_number;
int purge_duplicate, ignore_nblocks;
int array_blocktypes, mesh_blocktypes;
int64_t array_ndims, *array_starts, *array_ends, *array_strides;
int slice_direction, slice_dim[3];
int *blocktype_mask;
char *output_file;
char *format_float, *format_int, *format_space;
//static char *default_float = "%9.6fE%+2.2d1p";
static char *default_float = "%13.7E";
static char *default_int = "%" PRIi64;
static char *default_space = " ";
static char *default_indent = " ";
static char indent[64];
static list_t *slice_list;
struct id_list {
sdf_block_t *b;
char *id;
int idx;
struct id_list *prev, *next;
} *variable_ids, *variable_last_id;
struct range_type {
int start, end;
} *range_list, *blocktype_list;
int nrange, nrange_max;
int nblist, nblist_max;
struct slice_block {
char *data;
int datatype, nelements, free_data;
};
enum output_types {
vtk
} output_type;
static char width_fmt[16];
#define SET_WIDTH_LEN(len) do { \
snprintf(width_fmt, 16, "%%-%is", (len)); \
} while(0)
#define SET_WIDTH(string) do { \
int _l = strlen((string)); \
SET_WIDTH_LEN(_l); \
} while(0)
#define PRINTC(name,variable,fmt) do { \
printf(indent, 1); \
printf(width_fmt, (name)); \
printf(" "); \
printf(fmt, (variable)); \
printf("\n"); \
} while(0)
#define PRINT(name,variable,fmt) do { \
PRINTC(name,variable,fmt); \
} while(0)
#define PRINTAR(name,array,fmt,len) do { \
int _i; \
if (!(array)) break; \
printf(indent, 1); \
printf(width_fmt, (name)); \
printf(" ("); \
printf(fmt, (array)[0]); \
for (_i = 1; _i < (len); _i++) { \
printf(","); \
printf(fmt, (array)[_i]); \
} \
printf(")\n"); \
} while(0)
#define PRINTDAR(name,array,fmt,len) do { \
int _i; \
if (!(array)) break; \
printf(indent, 1); \
printf(width_fmt, (name)); \
printf(" ("); \
printf(fmt, sdf_datatype_c[(array)[0]]); \
for (_i = 1; _i < (len); _i++) { \
printf(","); \
printf(fmt, sdf_datatype_c[(array)[_i]]); \
} \
printf(")\n"); \
} while(0)
int close_files(sdf_file_t *h);
int sdf_write_vtk_file(sdf_file_t *h, char *stem);
void usage(int err)
{
fprintf(stderr, "usage: sdffilter [options] <sdf_filename>\n");
fprintf(stderr, "\noptions:\n\
-h --help Show this usage message\n\
-n --no-metadata Don't show metadata blocks (shown by default)\n\
-j --just-id Only show ID and number for metadata blocks\n\
-l --less-verbose Print metadata less verbosely\n\
-c --contents Show block's data content\n\
-s --single Convert block data to single precision\n\
-v --variable=id Find the block with id matching 'id'\n\
-x --exclude=id Exclude the block with id matching 'id'\n\
-m --mmap Use mmap'ed file I/O\n\
-i --no-summary Ignore the metadata summary\n\
-b --no-nblocks Ignore the header value for nblocks\n\
-a --array-section=s Read in the specified array section. The array section\n\
's' mimics Python's slicing notation.\n\
-d --derived Add derived blocks\n\
-e --extension-info Print information about any loaded extension module\n\
-I --c-indexing Array indexing starts from 1 by default. If this flag\n\
is used then the indexing starts from 0.\n\
-1 --1dslice=arg Output 1D slice as a multi-column gnuplot file.\n\
The argument is 1,2 or 3 integers separated by commas.\n\
-H --no-ascii-header When writing multi-column ascii data, a header is\n\
included for use by gnuplot or other plotting\n\
utilities. This flag disables the header.\n\
-C --count=n When pretty-printing array contents, write 'n'\n\
elements per line.\n\
-F --format-float=f Use specified format for printing floating-point array\n\
contents.\n\
-N --format-int=f Use specified format for printing integer array\n\
contents.\n\
-S --format-space=f Use specified spacing between array elements.\n\
-K --format-number Show block number before each row of array elements.\n\
-R --format-rowindex Show array indices before each row of array elements.\n\
-J --format-index Show array indices before each array element.\n\
-p --purge-duplicate Delete duplicated block IDs\n\
-B --block-types List of SDF block types to consider\n\
-A --array-blocks Only consider array block types (%i,%i,%i,%i,%i)\n\
-M --mesh-blocks Only consider mesh block types (%i,%i,%i,%i)\n\
-P --print-types Print the list of SDF blocktypes\n\
-V --version Print version information and exit\n\
-t --output-type Output file format. Currently only vtk\n\
-o --output Output filename stem\n\
", SDF_BLOCKTYPE_PLAIN_VARIABLE, SDF_BLOCKTYPE_POINT_VARIABLE,
SDF_BLOCKTYPE_ARRAY,
SDF_BLOCKTYPE_PLAIN_DERIVED, SDF_BLOCKTYPE_POINT_DERIVED,
SDF_BLOCKTYPE_PLAIN_MESH, SDF_BLOCKTYPE_POINT_MESH,
SDF_BLOCKTYPE_UNSTRUCTURED_MESH, SDF_BLOCKTYPE_LAGRANGIAN_MESH);
/*
-D --debug Show the contents of the debug buffer\n\
*/
exit(err);
}
void print_blocktypes(void)
{
int i;
printf("%2i Header block\n", 0);
for (i=1; i < sdf_blocktype_len; i++) {
printf("%2i %s\n", i, sdf_blocktype_c[i]);
}
exit(0);
}
int range_sort(const void *v1, const void *v2)
{
struct range_type *a = (struct range_type *)v1;
struct range_type *b = (struct range_type *)v2;
return (a->start - b->start);
}
void parse_1d_slice(char *slice)
{
int i, len = strlen(slice);
int done_direction, done_dim1, dim;
char *old, *ptr;
done_direction = done_dim1 = 0;
slice_direction = 0;
for (i = 0, old = ptr = slice; i < len+1; i++, ptr++) {
if (*ptr == ',' || *ptr == '\0') {
if (done_dim1) {
dim = strtol(old, NULL, 10);
if (dim > 0) dim -= index_offset;
if (slice_direction == 2)
slice_dim[1] = dim;
else
slice_dim[2] = dim;
} else if (done_direction) {
dim = strtol(old, NULL, 10);
if (dim > 0) dim -= index_offset;
if (slice_direction == 0)
slice_dim[1] = dim;
else
slice_dim[0] = dim;
done_dim1 = 1;
} else {
slice_direction = strtol(old, NULL, 10) - index_offset;
if (slice_direction < 0 || slice_direction > 2) {
fprintf(stderr, "ERROR: invalid slice direction.\n");
exit(1);
}
done_direction = 1;
}
old = ptr + 1;
}
}
if (array_starts) free(array_starts);
if (array_ends) free(array_ends);
if (array_strides) free(array_strides);
array_ndims = 3;
array_starts = calloc(array_ndims, sizeof(*array_starts));
array_ends = malloc(array_ndims * sizeof(*array_ends));
array_strides = malloc(array_ndims * sizeof(*array_strides));
for (i = 0; i < array_ndims; i++) {
array_strides[i] = 1;
if (i == slice_direction) {
array_starts[i] = 0;
array_ends[i] = INT64_MAX;
} else {
array_starts[i] = slice_dim[i];
array_ends[i] = array_starts[i] + 1;
}
}
}
void parse_array_section(char *array_section)
{
int ndim, i, len = strlen(array_section), done_start, done_end;
char *ptr, *old;
if (array_starts) free(array_starts);
if (array_ends) free(array_ends);
if (array_strides) free(array_strides);
array_ndims = 1;
for (i = 0, ptr = array_section; i < len; i++, ptr++)
if (*ptr == ',') array_ndims++;
array_starts = calloc(array_ndims, sizeof(*array_starts));
array_ends = malloc(array_ndims * sizeof(*array_ends));
array_strides = malloc(array_ndims * sizeof(*array_strides));
for (i = 0; i < array_ndims; i++)
array_strides[i] = 1;
done_start = done_end = ndim = 0;
for (i = 0, old = ptr = array_section; i < len+1; i++, ptr++) {
if (*ptr == ':' || *ptr == ',' || *ptr == '\0') {
if (done_end) {
array_strides[ndim] = strtol(old, NULL, 10);
if (array_strides[ndim] == 0) array_strides[ndim] = 1;
if (array_strides[ndim] < 0) {
fprintf(stderr, "ERROR: negative stride values not"
" supported.\n");
exit(1);
}
} else if (done_start) {
if (ptr - old > 0) {
array_ends[ndim] = strtol(old, NULL, 10);
if (array_ends[ndim] > 0) array_ends[ndim] -= index_offset;
} else
array_ends[ndim] = INT64_MAX;
done_end = 1;
} else {
array_starts[ndim] = strtol(old, NULL, 10);
if (array_starts[ndim] > 0) array_starts[ndim] -= index_offset;
array_ends[ndim] = array_starts[ndim] + 1;
done_start = 1;
}
old = ptr + 1;
if (*ptr == ',') {
done_start = done_end = 0;
ndim++;
}
}
}
}
void parse_format(void)
{
char cc;
int len = strlen(format_float) - 1;
scale_factor = 1;
if (format_float[len] == 'p') {
format_float[len] = '\0';
for (; len > 1; len--) {
cc = format_float[len-1];
if (cc < '0' || cc > '9') break;
}
scale_factor = (int)strtol(&format_float[len], NULL, 10);
format_float[len--] = '\0';
}
special_format = 0;
if (format_float[len] == 'd')
special_format = 1;
}
int parse_range(char *optarg, struct range_type **range_list_p, int *nrange,
int *nrange_max)
{
int i, range;
size_t sz;
char *ptr;
struct range_type *range_list = *range_list_p;
struct range_type *range_tmp;
if (!((*optarg >= '0' && *optarg <= '9') || *optarg == '-'))
return 1;
sz = sizeof(struct range_type);
ptr = optarg;
range = 0;
while (ptr < optarg + strlen(optarg) + 1) {
if (range) {
i = (int)strtol(ptr, &ptr, 10);
if (i == 0)
range_list[*nrange-1].end = INT_MAX;
else if (i < range_list[*nrange-1].start)
(*nrange)--;
else
range_list[*nrange-1].end = i;
range = 0;
} else {
(*nrange)++;
// Grow array if necessary
if (*nrange > *nrange_max) {
if (*nrange_max == 0) {
*nrange_max = 128;
range_list = calloc(*nrange_max, sz);
} else {
i = 2 * *nrange_max;
range_tmp = calloc(i, sz);
memcpy(range_tmp, range_list, *nrange_max * sz);
free(range_list);
range_list = range_tmp;
*nrange_max = i;
}
}
if (*ptr == '-') {
range = 1;
range_list[*nrange-1].end = INT_MAX;
} else {
i = (int)strtol(ptr, &ptr, 10);
range_list[*nrange-1].start = i;
range_list[*nrange-1].end = i;
if (*ptr == '-') range = 1;
}
}
ptr++;
}
*range_list_p = range_list;
return 0;
}
void sort_range(struct range_type **range_list_p, int *nrange)
{
struct range_type *range_list = *range_list_p;
struct range_type *range_tmp;
size_t sz;
int i;
if (*nrange <= 0)
return;
sz = sizeof(struct range_type);
// Sanitize range list
qsort(range_list, *nrange, sz, &range_sort);
for (i=1; i < *nrange; ) {
if (range_list[i].start <= range_list[i-1].end+1) {
if (range_list[i].end > range_list[i-1].end)
range_list[i-1].end = range_list[i].end;
memcpy(range_list+i, range_list+i+1, (*nrange-i) * sz);
(*nrange)--;
} else
i++;
}
// Shrink array
range_tmp = malloc(*nrange * sz);
memcpy(range_tmp, range_list, *nrange * sz);
free(range_list);
*range_list_p = range_list = range_tmp;
}
void setup_blocklist_mask(void)
{
int i, n, blist_start = 0;
blocktype_mask = NULL;
if (mesh_blocktypes + array_blocktypes + nblist == 0)
return;
blocktype_mask = calloc(sdf_blocktype_len, sizeof(*blocktype_mask));
if (mesh_blocktypes) {
blocktype_mask[SDF_BLOCKTYPE_PLAIN_MESH] = 1;
blocktype_mask[SDF_BLOCKTYPE_POINT_MESH] = 1;
blocktype_mask[SDF_BLOCKTYPE_UNSTRUCTURED_MESH] = 1;
blocktype_mask[SDF_BLOCKTYPE_LAGRANGIAN_MESH] = 1;
}
if (array_blocktypes) {
blocktype_mask[SDF_BLOCKTYPE_PLAIN_VARIABLE] = 1;
blocktype_mask[SDF_BLOCKTYPE_POINT_VARIABLE] = 1;
blocktype_mask[SDF_BLOCKTYPE_PLAIN_DERIVED] = 1;
blocktype_mask[SDF_BLOCKTYPE_POINT_DERIVED] = 1;
blocktype_mask[SDF_BLOCKTYPE_ARRAY] = 1;
}
if (nblist == 0)
return;
for (i=0; i < sdf_blocktype_len; i++) {
for (n = blist_start; n < nblist; n++) {
if (i < blocktype_list[n].start)
break;
if (i <= blocktype_list[n].end) {
blocktype_mask[i] = 1;
break;
}
blist_start++;
}
}
free(blocktype_list);
}
char *parse_args(int *argc, char ***argv)
{
char *file = NULL;
int c, err, got_include, got_exclude;
#ifdef WIN32
struct __stat64 statbuf;
#else
struct stat statbuf;
#endif
static struct option longopts[] = {
{ "1dslice", required_argument, NULL, '1' },
{ "array-section", required_argument, NULL, 'a' },
{ "array-blocks", no_argument, NULL, 'A' },
{ "no-nblocks", no_argument, NULL, 'b' },
{ "block-types", required_argument, NULL, 'B' },
{ "contents", no_argument, NULL, 'c' },
{ "count", required_argument, NULL, 'C' },
{ "derived", no_argument, NULL, 'd' },
{ "extension-info", no_argument, NULL, 'e' },
{ "help", no_argument, NULL, 'h' },
{ "no-ascii-header", no_argument, NULL, 'H' },
{ "format-float", required_argument, NULL, 'F' },
{ "no-summary", no_argument, NULL, 'i' },
{ "c-indexing", no_argument, NULL, 'I' },
{ "just-id", no_argument, NULL, 'j' },
{ "format-index", no_argument, NULL, 'J' },
{ "format-number", no_argument, NULL, 'K' },
{ "less-verbose", no_argument, NULL, 'l' },
{ "mmap", no_argument, NULL, 'm' },
{ "mesh-blocks", no_argument, NULL, 'M' },
{ "no-metadata", no_argument, NULL, 'n' },
{ "format-int", required_argument, NULL, 'N' },
{ "output", required_argument, NULL, 'o' },
{ "purge-duplicate", no_argument, NULL, 'p' },
{ "print-types", no_argument, NULL, 'P' },
{ "format-rowindex", no_argument, NULL, 'R' },
{ "single", no_argument, NULL, 's' },
{ "format-space", required_argument, NULL, 'S' },
{ "output-type", required_argument, NULL, 't' },
{ "variable", required_argument, NULL, 'v' },
{ "exclude", required_argument, NULL, 'x' },
{ "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 }
//{ "debug", no_argument, NULL, 'D' },
};
metadata = debug = index_offset = element_count = verbose_metadata = 1;
ascii_header = 1;
contents = single = use_mmap = ignore_summary = exclude_variables = 0;
derived = format_rowindex = format_index = format_number = just_id = 0;
purge_duplicate = ignore_nblocks = extension_info = 0;
array_blocktypes = mesh_blocktypes = 0;
slice_direction = -1;
variable_ids = NULL;
variable_last_id = NULL;
output_file = NULL;
array_starts = array_ends = array_strides = NULL;
array_ndims = nrange_max = nrange = 0;
nblist_max = nblist = 0;
format_int = malloc(strlen(default_int)+1);
memcpy(format_int, default_int, strlen(default_int)+1);
format_float = malloc(strlen(default_float)+1);
memcpy(format_float, default_float, strlen(default_float)+1);
format_space = malloc(strlen(default_space)+1);
memcpy(format_space, default_space, strlen(default_space)+1);
got_include = got_exclude = 0;
output_type = vtk;
while ((c = getopt_long(*argc, *argv,
"1:a:AbB:cC:deF:hHiIjJKlmMnN:o:pPRsS:t:v:x:V",
longopts, NULL)) != -1) {
switch (c) {
case '1':
contents = 1;
parse_1d_slice(optarg);
break;
case 'a':
contents = 1;
parse_array_section(optarg);
break;
case 'A':
array_blocktypes = 1;
break;
case 'b':
ignore_nblocks = 1;
break;
case 'B':
parse_range(optarg, &blocktype_list, &nblist, &nblist_max);
break;
case 'c':
contents = 1;
break;
case 'C':
element_count = strtol(optarg, NULL, 10);
if (element_count < 1) element_count = 1;
break;
case 'd':
derived = 1;
break;
case 'e':
extension_info = 1;
break;
case 'F':
free(format_float);
format_float = malloc(strlen(optarg)+1);
memcpy(format_float, optarg, strlen(optarg)+1);
break;
case 'h':
usage(0);
break;
case 'H':
ascii_header = 0;
break;
case 'i':
ignore_summary = 1;
break;
case 'I':
index_offset = 0;
break;
case 'j':
just_id = 1;
break;
case 'J':
format_index = 1;
break;
case 'K':
format_number = 1;
break;
case 'l':
verbose_metadata = 0;
break;
case 'm':
use_mmap = 1;
break;
case 'M':
mesh_blocktypes = 1;
break;
case 'n':
metadata = 0;
break;
case 'N':
free(format_int);
format_int = malloc(strlen(optarg)+1);
memcpy(format_int, optarg, strlen(optarg)+1);
break;
case 'o':
if (output_file) free(output_file);
output_file = malloc(strlen(optarg)+1);
memcpy(output_file, optarg, strlen(optarg)+1);
break;
case 'p':
purge_duplicate = 1;
break;
case 'P':
print_blocktypes();
break;
case 'R':
format_rowindex = 1;
break;
case 's':
single = 1;
break;
case 'S':
free(format_space);
format_space = malloc(strlen(optarg)+1);
memcpy(format_space, optarg, strlen(optarg)+1);
break;
case 't':
if (!strncmp("vtk", optarg, 4)) {
output_type = vtk;
} else {
fprintf(stderr, "ERROR: output type not supported\n");
exit(1);
}
case 'V':
printf("sdffilter version %s\n", VERSION);
printf("commit info: %s, %s\n", SDF_COMMIT_ID, SDF_COMMIT_DATE);
printf("library commit info: %s, %s\n",
sdf_get_library_commit_id(), sdf_get_library_commit_date());
exit(0);
break;
case 'v':
case 'x':
err = 0;
if (c == 'v') {
if (got_exclude) err = 1;
got_include = 1;
} else {
if (got_include) err = 1;
got_exclude = 1;
exclude_variables = 1;
}
if (err) {
fprintf(stderr, "ERROR: cannot both include and "
"exclude variables.\n");
exit(1);
}
err = parse_range(optarg, &range_list, &nrange, &nrange_max);
if (err) {
if (!variable_ids) {
variable_last_id =
variable_ids = malloc(sizeof(*variable_ids));
} else {
variable_last_id->next = malloc(sizeof(*variable_ids));
variable_last_id = variable_last_id->next;
}
variable_last_id->next = NULL;
variable_last_id->id = malloc(strlen(optarg)+1);
memcpy(variable_last_id->id, optarg, strlen(optarg)+1);
}
break;
default:
usage(1);
}
}
if ((optind+1) == *argc) {
file = (*argv)[optind];
#ifdef WIN32
err = _stat64(file, &statbuf);
#else
err = stat(file, &statbuf);
#endif
if (err) {
fprintf(stderr, "Error opening file %s\n", file);
exit(1);
}
} else {
fprintf(stderr, "No file specified\n");
usage(1);
}
if (exclude_variables)
sort_range(&range_list, &nrange);
sort_range(&blocktype_list, &nblist);
setup_blocklist_mask();
parse_format();
return file;
}
void free_memory(sdf_file_t *h)
{
if (format_int) free(format_int);
if (format_float) free(format_float);
if (format_space) free(format_space);
sdf_stack_destroy(h);
}
static int set_array_section(sdf_block_t *b)
{
return sdf_block_set_array_section(b, array_ndims, array_starts,
array_ends, array_strides);
}
static void print_value(void *data, int datatype)
{
int exponent;
int64_t i64;
double r8;
switch (datatype) {
case SDF_DATATYPE_INTEGER4:
i64 = *((int32_t*)data);
printf(format_int, i64);
break;
case SDF_DATATYPE_INTEGER8:
printf(format_int, *((int64_t*)data));
break;
case SDF_DATATYPE_REAL4:
if (special_format) {
r8 = *((float*)data);
if (r8 == 0)
exponent = 0;
else {
exponent = (int)floor(log10(fabs(r8))+FLT_EPSILON) + 1;
exponent -= scale_factor;
r8 *= pow(10, -1.0 * exponent);
}
if (r8 == INFINITY)
printf("Infinity");
else
printf(format_float, r8, exponent);
} else
printf(format_float, *((float*)data));
break;
case SDF_DATATYPE_REAL8:
if (special_format) {
r8 = *((double*)data);
if (r8 == 0)
exponent = 0;
else {
exponent = (int)floor(log10(fabs(r8))+FLT_EPSILON) + 1;
exponent -= scale_factor;
r8 *= pow(10, -1.0 * exponent);
}
if (r8 == INFINITY)
printf("Infinity");
else
printf(format_float, r8, exponent);
} else
printf(format_float, *((double*)data));
break;
//case SDF_DATATYPE_REAL16:
// printf(format_float, (double)b->const_value);
// break;
case SDF_DATATYPE_CHARACTER:
printf("%c", *((char*)data));
break;
case SDF_DATATYPE_LOGICAL:
if (*((char*)data))
printf("T");
else
printf("F");
break;
}
}
static void print_value_element(char *data, int datatype, int n)
{
switch (datatype) {
case SDF_DATATYPE_INTEGER4:
case SDF_DATATYPE_REAL4:
print_value(data + 4*n, datatype);
break;
case SDF_DATATYPE_INTEGER8:
case SDF_DATATYPE_REAL8:
print_value(data + 8*n, datatype);
break;
case SDF_DATATYPE_CHARACTER:
case SDF_DATATYPE_LOGICAL:
print_value(data + n, datatype);
break;
}
}
static int pretty_print_slice(sdf_file_t *h, sdf_block_t *b)
{
static sdf_block_t *mesh = NULL;
int n, sz;
char *ptr, *dptr;
float r4;
double r8;
struct slice_block *sb;
static int need_init = 1;
if (b->blocktype != SDF_BLOCKTYPE_PLAIN_VARIABLE &&
b->blocktype != SDF_BLOCKTYPE_ARRAY &&
b->blocktype != SDF_BLOCKTYPE_PLAIN_DERIVED) return 0;
if (slice_direction >= b->ndims) return 0;
if (!mesh && b->blocktype != SDF_BLOCKTYPE_ARRAY) {
mesh = sdf_find_block_by_id(h, b->mesh_id);
if (!mesh) {
fprintf(stderr, "ERROR: unable to find mesh.\n");
exit(1);
}
set_array_section(mesh);
sdf_helper_read_data(h, mesh);
if (!mesh->grids) {
fprintf(stderr, "ERROR: unable to read mesh.\n");
exit(1);
}
sb = calloc(1, sizeof(*sb));
sb->datatype = mesh->datatype_out;
sb->nelements = mesh->array_ends[slice_direction] -
mesh->array_starts[slice_direction] - 1;
sz = SDF_TYPE_SIZES[sb->datatype];
list_init(&slice_list);
list_append(slice_list, sb);
need_init = 0;
// Create a new cell-centered grid array
ptr = mesh->grids[slice_direction];
dptr = sb->data = calloc(sb->nelements, sz);
sb->free_data = 1;
if (sb->datatype == SDF_DATATYPE_REAL4) {
for (n = 0; n < sb->nelements; n++) {
r4 = 0.5 * (*((float*)ptr) + *((float*)ptr+1));
memcpy(dptr, &r4, sz);
dptr += sz;
ptr += sz;
}
} else if (sb->datatype == SDF_DATATYPE_REAL8) {
for (n = 0; n < sb->nelements; n++) {
r8 = 0.5 * (*((double*)ptr) + *((double*)ptr+1));
memcpy(dptr, &r8, sz);
dptr += sz;
ptr += sz;
}
}
if (ascii_header) {
printf("# 1D array slice through (");
for (n = 0; n < mesh->ndims; n++) {
if (n != 0) printf(",");
if (n == slice_direction)
printf(":");
else
printf("%i", slice_dim[n]+index_offset);
}
printf(")\n#\n# %s\t%s\t(%s)\n", mesh->id,
mesh->dim_labels[slice_direction],
mesh->dim_units[slice_direction]);
}
}
if (need_init) {
list_init(&slice_list);
need_init = 0;
printf("# 1D array slice through (");
for (n = 0; n < b->ndims; n++) {
if (n != 0) printf(",");
if (n == slice_direction)
printf(":");
else
printf("%i", slice_dim[n]+index_offset);
}
printf(")\n#\n");
}
sb = calloc(1, sizeof(*sb));
sb->datatype = b->datatype_out;
sb->nelements = b->nelements_local;
sb->data = b->data;
list_append(slice_list, sb);
if (ascii_header) {
printf("# %s\t%s", b->id, b->name);
if (b->blocktype != SDF_BLOCKTYPE_ARRAY)
printf("\t(%s)", b->units);
printf("\n");
}
return 0;
}
static void pretty_print_slice_finish(void)
{
int i;
struct slice_block *sb, *first;
if (!slice_list) return;
if (ascii_header) printf("#\n");
first = list_start(slice_list);
for (i = 0; i < first->nelements; i++) {
sb = list_start(slice_list);
while (sb) {
if (sb != first) printf(format_space,1);
print_value_element(sb->data, sb->datatype, i);
sb = list_next(slice_list);
}
printf("\n");
}
// Cleanup
sb = list_start(slice_list);
while (sb) {
if (sb->free_data) free(sb->data);
sb = list_next(slice_list);
}
list_destroy(&slice_list);
}
static void pretty_print_mesh(sdf_file_t *h, sdf_block_t *b, int idnum)
{
int *idx, *fac;
int i, rem, sz, left, digit, ncount, dim;
int64_t n, nelements;
char *ptr;
static const int fmtlen = 32;
char **fmt;
if (slice_direction != -1) {
pretty_print_slice(h, b);
return;
}
idx = calloc(b->ndims, sizeof(*idx));
fac = calloc(b->ndims, sizeof(*fac));
fmt = calloc(b->ndims, sizeof(*fmt));