-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathgrib_tools.c
1427 lines (1254 loc) · 46.3 KB
/
grib_tools.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
/*
* (C) Copyright 2005- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
*/
#include "grib_tools.h"
#include <stdlib.h>
#if HAVE_LIBJASPER
/* Remove compiler warnings re macros being redefined */
#undef PACKAGE_BUGREPORT
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#endif
#ifdef ENABLE_FLOATING_POINT_EXCEPTIONS
#define _GNU_SOURCE
#include <fenv.h>
int feenableexcept(int excepts);
#endif
GRIB_INLINE static int grib_inline_strcmp(const char* a, const char* b)
{
if (*a != *b)
return 1;
while ((*a != 0 && *b != 0) && *(a) == *(b)) {
a++;
b++;
}
return (*a == 0 && *b == 0) ? 0 : 1;
}
static void grib_print_header(grib_runtime_options* options, grib_handle* h);
static void grib_tools_set_print_keys(grib_runtime_options* options, grib_handle* h, const char* ns);
static int grib_tool_with_orderby(grib_runtime_options* options);
static int grib_tool_without_orderby(grib_runtime_options* options);
static int grib_tool_onlyfiles(grib_runtime_options* options);
static int grib_tool_index(grib_runtime_options* options);
static int process(grib_context* c, grib_runtime_options* options, const char* path);
static int scan(grib_context* c, grib_runtime_options* options, const char* dir);
FILE* dump_file;
static grib_runtime_options global_options = {
0, /* verbose */
0, /* fail */
0, /* skip */
12, /* default_print_width */
0, /* print_header */
0, /* name_space */
0, /* print_number */
1, /* print_statistics */
{{0,},}, /* grib_values requested_print_keys[MAX_KEYS] */
0, /* requested_print_keys_count */
{{0,},}, /* grib_values print_keys[MAX_KEYS] */
0, /* print_keys_count */
0, /* strict */
0, /* multi_support */
0, /* set_values_count */
{{0,},}, /* grib_values set_values[MAX_KEYS] */
{{0,},}, /* grib_values constraints[MAX_KEYS] */
0, /* constraints_count */
{{0,},}, /* grib_values compare[MAX_KEYS] */
0, /* compare_count */
0, /* handle_count */
0, /* filter_handle_count */
0, /* file_count */
0, /* grib_tools_file* infile_extra */
0, /* grib_tools_file* current_infile */
0, /* grib_tools_file* infile */
0, /* grib_tools_file* outfile */
0, /* grib_action* action */
0, /* grib_rule* rules */
0, /* int dump_flags; */
0, /* char* dump_mode; */
0, /* repack */
0, /* error */
0, /* gts */
0, /* orderby */
0, /* latlon */
{0,}, /* double lats[4] */
{0,}, /* double lons[4] */
{0,}, /* double values[4] */
{0,}, /* double distances[4] */
{0,}, /* int indexes[4] */
4, /* int latlon_mode */
0, /* char* latlon_mask */
-1, /* int latlon_idx */
{0,}, /* double mask_values[4] */
0, /* index */
0, /* index_on */
0, /* constant */
0, /* dump_filename*/
0, /* grib_fieldset* idx */
0, /* random */
0, /* format */
0, /* onlyfiles */
0, /* tolerance_count */
0, /* through_index */
0, /* index1 */
0, /* index2 */
0, /* context */
0, /* stop */
0, /* mode */
0, /* headers_only */
0, /* skip_all */
{{0,},}, /* grib_values tolerance[MAX_KEYS] */
0, /* infile_offset */
0 /* JSON output */
};
static grib_handle* grib_handle_new_from_file_x(grib_context* c, FILE* f, int mode, int headers_only, int* err)
{
if (mode == MODE_GRIB)
return grib_new_from_file(c, f, headers_only, err);
if (mode == MODE_BUFR)
return bufr_new_from_file(c, f, err);
if (mode == MODE_ANY)
return any_new_from_file(c, f, err);
if (mode == MODE_GTS)
return gts_new_from_file(c, f, err);
if (mode == MODE_METAR)
return metar_new_from_file(c, f, err);
if (mode == MODE_TAF)
return taf_new_from_file(c, f, err);
Assert(!"grib_handle_new_from_file_x: unknown mode");
return NULL;
}
int grib_tool(int argc, char** argv)
{
int ret = 0;
int i = 0;
grib_context* c = grib_context_get_default();
global_options.context = c;
/* This is a consequence of ECC-440.
* We want to keep the output file(s) opened as various
* messages are appended to them. Otherwise they will be opened/closed
* multiple times.
*/
if (c->file_pool_max_opened_files == 0)
c->file_pool_max_opened_files = 200;
#ifdef ENABLE_FLOATING_POINT_EXCEPTIONS
feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT);
#endif
if (getenv("DOXYGEN_USAGE") && argc == 1)
usage_doxygen();
grib_get_runtime_options(argc, argv, &global_options);
grib_tool_before_getopt(&global_options);
grib_process_runtime_options(c, argc, argv, &global_options);
grib_tool_init(&global_options);
if (global_options.dump_filename) {
dump_file = fopen(global_options.dump_filename, "w");
if (!dump_file) {
perror(global_options.dump_filename);
exit(1);
}
}
else {
dump_file = stdout;
}
/* ECC-926: Currently only GRIB indexing works. Disable the through_index if BUFR, GTS etc */
if (global_options.mode == MODE_GRIB &&
is_index_file(global_options.infile->name) &&
(global_options.infile_extra && is_index_file(global_options.infile_extra->name))) {
global_options.through_index = 1;
return grib_tool_index(&global_options);
}
if (global_options.onlyfiles)
ret = grib_tool_onlyfiles(&global_options);
else {
if (global_options.orderby)
ret = grib_tool_with_orderby(&global_options);
else
ret = grib_tool_without_orderby(&global_options);
}
if (global_options.dump_filename)
fclose(dump_file);
/* Free memory */
for (i = 0; i < global_options.print_keys_count; i++) {
if (global_options.print_keys[i].name) {
free((char*)global_options.print_keys[i].name);
}
}
return ret;
}
static int grib_tool_with_orderby(grib_runtime_options* options)
{
int err = 0;
grib_failed *failed = NULL, *p = NULL;
grib_handle* h = NULL;
grib_tools_file* infile = options->infile;
char** filenames;
int files_count = 0;
grib_fieldset* set = NULL;
int i = 0;
grib_context* c = grib_context_get_default();
if (infile)
infile->failed = NULL;
files_count = 0;
while (infile) {
files_count++;
infile = infile->next;
}
filenames = (char**)grib_context_malloc_clear(c, files_count * sizeof(char*));
infile = options->infile;
for (i = 0; i < files_count; i++) {
filenames[i] = infile->name;
infile = infile->next;
}
if (grib_options_on("7"))
c->no_fail_on_wrong_length = 1;
set = grib_fieldset_new_from_files(0, filenames, files_count, 0, 0, 0, options->orderby, &err);
if (err) {
grib_context_log(c, GRIB_LOG_ERROR, "unable to create index for input file %s (%s)",
filenames[0], grib_get_error_message(err));
exit(err);
}
options->handle_count = 0;
grib_context_set_handle_file_count(c, 0); /* ECC-873 */
grib_context_set_handle_total_count(c, 0); /* ECC-873 */
while (!options->skip_all && ((h = grib_fieldset_next_handle(set, &err)) != NULL || err != GRIB_SUCCESS)) {
options->handle_count++;
grib_context_set_handle_file_count(c, options->handle_count); /* ECC-873 */
grib_context_set_handle_total_count(c, options->handle_count); /* ECC-873 */
options->error = err;
if (!h) {
grib_no_handle_action(options, err);
failed = (grib_failed*)grib_context_malloc_clear(c, sizeof(grib_failed));
failed->count = infile->handle_count;
failed->error = err;
failed->next = NULL;
if (!infile->failed) {
infile->failed = failed;
}
else {
p = infile->failed;
while (p->next)
p = p->next;
p->next = failed;
}
continue;
}
if (options->json_output == 0 || options->latlon)
grib_print_header(options, h);
else
grib_tools_set_print_keys(options, h, options->name_space);
grib_skip_check(options, h);
if (options->skip && options->strict) {
grib_tool_skip_handle(options, h);
continue;
}
grib_tool_new_handle_action(options, h);
grib_tool_print_key_values(options, h);
grib_handle_delete(h);
}
if (set->size==0) fprintf(stderr, "no messages found in fieldset\n");
grib_tool_finalise_action(options);
grib_fieldset_delete(set);
free(filenames);
return 0;
}
static char iobuf[1024 * 1024];
static int grib_tool_without_orderby(grib_runtime_options* options)
{
int err = 0;
/*int nofail=0;*/
grib_failed *failed = NULL, *p = NULL;
grib_handle* h = NULL;
grib_tools_file* infile = options->infile;
grib_context* c = grib_context_get_default();
options->file_count = 0;
options->handle_count = 0;
options->filter_handle_count = 0;
options->current_infile = options->infile;
infile->failed = NULL;
if (grib_options_on("7"))
c->no_fail_on_wrong_length = 1;
while (infile != NULL && infile->name != NULL) {
if (options->print_statistics && options->verbose && !options->json_output)
fprintf(dump_file, "%s\n", infile->name);
if (strcmp(infile->name, "-") == 0)
infile->file = stdin;
else
infile->file = fopen(infile->name, "rb");
if (!infile->file) {
perror(infile->name);
exit(1);
}
if (options->infile_offset) {
#ifndef ECCODES_ON_WINDOWS
/* Check at compile time to ensure our file offset is at least 64 bits */
COMPILE_TIME_ASSERT(sizeof(options->infile_offset) >= 8);
#endif
err = fseeko(infile->file, options->infile_offset, SEEK_SET);
if (err) {
/*fprintf(stderr, "Invalid file offset: %ld\n", options->infile_offset);*/
perror("Invalid file offset");
exit(1);
}
}
setvbuf(infile->file, iobuf, _IOFBF, sizeof(iobuf));
options->file_count++;
infile->handle_count = 0;
infile->filter_handle_count = 0;
grib_tool_new_file_action(options, infile);
/*nofail=grib_options_on("f");*/
while (!options->skip_all && ((h = grib_handle_new_from_file_x(c, infile->file, options->mode,
options->headers_only, &err)) != NULL ||
err != GRIB_SUCCESS)) {
infile->handle_count++;
options->handle_count++;
if (c->no_fail_on_wrong_length && (err == GRIB_PREMATURE_END_OF_FILE || err == GRIB_WRONG_LENGTH))
err = 0;
if (!options->error) {
/* ECC-1086: Do not clear a previous error */
options->error = err;
}
if (!h) {
/* fprintf(dump_file,"\t\t\"ERROR: unreadable message\"\n"); */
grib_no_handle_action(options, err);
failed = (grib_failed*)grib_context_malloc_clear(c, sizeof(grib_failed));
failed->count = infile->handle_count;
failed->error = err;
failed->next = NULL;
if (!infile->failed) {
infile->failed = failed;
}
else {
p = infile->failed;
while (p->next)
p = p->next;
p->next = failed;
}
continue;
}
if (options->json_output == 0 || options->latlon)
grib_print_header(options, h);
else
grib_tools_set_print_keys(options, h, options->name_space);
grib_skip_check(options, h);
if (options->skip && options->strict) {
grib_tool_skip_handle(options, h);
continue;
}
grib_tool_new_handle_action(options, h);
grib_print_key_values(options, h);
grib_handle_delete(h);
}
grib_print_file_statistics(options, infile);
if (infile->file)
fclose(infile->file);
if (infile->handle_count == 0) {
fprintf(stderr, "no messages found in %s\n", infile->name);
if (options->fail)
exit(1);
}
infile = infile->next;
options->current_infile = infile;
}
grib_print_full_statistics(options);
grib_tool_finalise_action(options);
return options->error;
}
static int navigate(grib_field_tree* fields, grib_runtime_options* options)
{
int err = 0;
int message_type = 0;
if (!fields || options->stop)
return 0;
switch (options->mode) {
case MODE_GRIB:
message_type = CODES_GRIB;
break;
case MODE_BUFR:
message_type = CODES_BUFR;
break;
default:
Assert(0);
}
if (fields->field) {
grib_handle* h = codes_index_get_handle(fields->field, message_type, &err);
if (!options->index2->current)
options->index2->current = (grib_field_list*)grib_context_malloc_clear(options->context, sizeof(grib_field_list));
options->index2->current->field = fields->field;
if (!h)
return err;
grib_skip_check(options, h);
if (options->skip && options->strict) {
grib_tool_skip_handle(options, h);
}
else {
grib_tool_new_handle_action(options, h);
grib_handle_delete(h);
}
}
err = navigate(fields->next_level, options);
if (err)
return err;
err = navigate(fields->next, options);
return err;
}
static int grib_tool_index(grib_runtime_options* options)
{
int err = 0;
char* f1 = options->infile->name;
char* f2 = options->infile_extra->name;
grib_index_key *k1, *k2;
int found = 0;
grib_context* c = grib_context_get_default();
options->index1 = grib_index_read(c, f1, &err);
if (err)
grib_context_log(c, (GRIB_LOG_FATAL) | (GRIB_LOG_PERROR),
"unable to read index from %s", f1);
options->index2 = grib_index_read(c, f2, &err);
if (err)
grib_context_log(c, (GRIB_LOG_FATAL) | (GRIB_LOG_PERROR),
"unable to read index from %s", f2);
k1 = options->index1->keys;
while (k1) {
k2 = options->index2->keys;
found = 0;
while (k2) {
if (!strcmp(k1->name, k2->name)) {
found = 1;
break;
}
k2 = k2->next;
}
if (!found) {
printf("Indexes contained in the input files have different keys\n");
printf("keys in file %s:\n", f1);
k1 = options->index1->keys;
while (k1) {
printf("\t%s\n", k1->name);
k1 = k1->next;
}
printf("keys in file %s:\n", f2);
k2 = options->index2->keys;
while (k2) {
printf("\t%s\n", k2->name);
k2 = k2->next;
}
exit(1);
}
k1->value[0] = 0;
k1 = k1->next;
}
k2 = options->index2->keys;
while (k2) {
k1 = options->index1->keys;
found = 0;
while (k1) {
if (!strcmp(k1->name, k2->name)) {
found = 1;
break;
}
k1 = k1->next;
}
if (!found) {
printf("Indexes contained in the input files have different keys\n");
printf("keys in file %s:\n", f2);
k2 = options->index2->keys;
while (k2) {
printf("\t%s\n", k2->name);
k2 = k2->next;
}
printf("keys in file %s:\n", f1);
k1 = options->index1->keys;
while (k1) {
printf("\t%s\n", k1->name);
k1 = k1->next;
}
exit(1);
}
k2 = k2->next;
}
navigate(options->index2->fields, options);
if (options->index2)
grib_context_free(c, options->index2->current);
grib_tool_finalise_action(options);
return 0;
}
#ifndef ECCODES_ON_WINDOWS
static int scan(grib_context* c, grib_runtime_options* options, const char* dir)
{
struct dirent* s;
DIR* d;
int err = 0;
d = opendir(dir);
if (!d) {
grib_context_log(c, (GRIB_LOG_ERROR) | (GRIB_LOG_PERROR), "opendir %s", dir);
return GRIB_IO_PROBLEM;
}
while ((s = readdir(d)) && (err == 0)) {
if (strcmp(s->d_name, ".") != 0 && strcmp(s->d_name, "..") != 0) {
char buf[1024];
sprintf(buf, "%s/%s", dir, s->d_name);
process(c, options, buf);
}
}
closedir(d);
return 0;
}
#else
static int scan(grib_context* c, grib_runtime_options* options, const char* dir)
{
struct _finddata_t fileinfo;
intptr_t handle;
char buffer[1024];
sprintf(buffer, "%s/*", dir);
if ((handle = _findfirst(buffer, &fileinfo)) != -1) {
do {
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) {
char buf[1024];
sprintf(buf, "%s/%s", dir, fileinfo.name);
process(c, options, buf);
}
} while (!_findnext(handle, &fileinfo));
_findclose(handle);
}
else {
grib_context_log(c, (GRIB_LOG_ERROR) | (GRIB_LOG_PERROR), "opendir %s", dir);
return GRIB_IO_PROBLEM;
}
return 0;
}
#endif
static int process(grib_context* c, grib_runtime_options* options, const char* path)
{
struct stat s;
int stat_val = 0;
#ifndef ECCODES_ON_WINDOWS
stat_val = lstat(path, &s);
#else
stat_val = stat(path, &s);
#endif
if (stat_val != 0) {
grib_context_log(c, (GRIB_LOG_ERROR) | (GRIB_LOG_PERROR), "Cannot stat %s", path);
return GRIB_IO_PROBLEM;
}
if (S_ISDIR(s.st_mode) && !S_ISLNK(s.st_mode)) {
scan(c, options, path);
}
else {
grib_tool_new_filename_action(options, path);
}
return 0;
}
static int grib_tool_onlyfiles(grib_runtime_options* options)
{
grib_context* c = grib_context_get_default();
grib_tools_file* infile = options->infile;
while (infile != NULL && infile->name != NULL) {
process(c, options, infile->name);
infile = infile->next;
}
grib_tool_finalise_action(options);
return 0;
}
static void grib_print_header(grib_runtime_options* options, grib_handle* h)
{
size_t strlenkey = 0;
int width;
int written_to_dump = 0; /* boolean */
if (options->json_output && !options->latlon)
return; /* For JSON output we do not print a single header for all msgs */
if (options->handle_count != 1)
return;
grib_tools_set_print_keys(options, h, options->name_space);
if (options->verbose && options->print_header) {
int j = 0;
for (j = 0; j < options->print_keys_count; j++) {
strlenkey = strlen(options->print_keys[j].name);
width = strlenkey < options->default_print_width ? options->default_print_width + 2 : strlenkey + 2;
if (options->default_print_width < 0)
width = strlenkey + 1;
fprintf(dump_file, "%-*s", (int)width, options->print_keys[j].name);
written_to_dump = 1;
}
if (options->latlon) {
if (options->latlon_mode == 4) {
fprintf(dump_file, " value1 ");
fprintf(dump_file, " value2 ");
fprintf(dump_file, " value3 ");
fprintf(dump_file, " value4 ");
}
else {
fprintf(dump_file, " value ");
}
written_to_dump = 1;
}
if (options->index_on) {
fprintf(dump_file, " value(%d) ", (int)options->index);
written_to_dump = 1;
}
if (written_to_dump) {
fprintf(dump_file, "\n");
}
}
}
static int cmpstringp(const void* p1, const void* p2)
{
/* The actual arguments to this function are "pointers to
pointers to char", but strcmp(3) arguments are "pointers
to char", hence the following cast plus dereference */
return strcmp(*(char* const*)p1, *(char* const*)p2);
}
static void grib_tools_set_print_keys(grib_runtime_options* options, grib_handle* h, const char* ns)
{
int i = 0;
grib_keys_iterator* kiter = NULL;
options->print_keys_count = 0;
for (i = 0; i < options->requested_print_keys_count; i++) {
options->print_keys[options->print_keys_count].name = options->requested_print_keys[i].name;
if (strlen(options->requested_print_keys[i].name) > options->default_print_width)
options->default_print_width = (int)strlen(options->requested_print_keys[i].name);
options->print_keys[options->print_keys_count].type = options->requested_print_keys[i].type;
options->print_keys_count++;
}
if (ns) {
kiter = grib_keys_iterator_new(h, 0, ns);
if (!kiter) {
fprintf(stderr, "ERROR: Unable to create keys iterator\n");
exit(1);
}
while (grib_keys_iterator_next(kiter)) {
const char* name = grib_keys_iterator_get_name(kiter);
if (options->print_keys_count >= MAX_KEYS) {
fprintf(stderr, "ERROR: keys list too long (more than %d keys)\n",
options->print_keys_count);
exit(1);
}
if (options->print_keys[options->print_keys_count].name) {
free((char*)options->print_keys[options->print_keys_count].name);
}
options->print_keys[options->print_keys_count].name = strdup(name);
if (strlen(name) > options->default_print_width)
options->default_print_width = (int)strlen(name);
options->print_keys[options->print_keys_count].type = GRIB_TYPE_STRING;
options->print_keys_count++;
}
grib_keys_iterator_delete(kiter);
if (options->print_keys_count == 0 && options->latlon == 0) {
int j = 0, k = 0, ns_count = 0;
const char* all_namespace_vals[1024] = {
NULL,
}; /* sorted array containing all namespaces */
printf("ERROR: namespace \"%s\" does not contain any key.\n", ns);
printf("Here are the available namespaces in this message:\n");
for (i = 0; i < ACCESSORS_ARRAY_SIZE; i++) {
grib_accessor* anAccessor = h->accessors[i];
if (anAccessor) {
for (j = 0; j < MAX_ACCESSOR_NAMES; j++) {
const char* a_namespace = anAccessor->all_name_spaces[j];
if (a_namespace) {
all_namespace_vals[k++] = a_namespace;
ns_count++;
}
}
}
}
qsort(&all_namespace_vals, ns_count, sizeof(char*), cmpstringp);
for (i = 0; i < ns_count; ++i) {
if (all_namespace_vals[i]) {
int print_it = 1;
if (i > 0 && strcmp(all_namespace_vals[i], all_namespace_vals[i - 1]) == 0) {
print_it = 0; /* skip duplicate entries */
}
if (print_it)
printf("\t%s\n", all_namespace_vals[i]);
}
}
exit(1);
}
}
}
static int to_skip(grib_handle* h, grib_values* v, int* err)
{
double dvalue = 0;
int ret = 0;
long lvalue = 0;
char value[MAX_STRING_LEN] = {0,};
size_t len = MAX_STRING_LEN;
*err = 0;
switch (v->type) {
case GRIB_TYPE_STRING:
*err = grib_get_string(h, v->name, value, &len);
ret = v->equal ? grib_inline_strcmp(value, v->string_value) : !grib_inline_strcmp(value, v->string_value);
break;
case GRIB_TYPE_DOUBLE:
*err = grib_get_double(h, v->name, &dvalue);
ret = v->equal ? (dvalue != v->double_value) : (dvalue == v->double_value);
break;
case GRIB_TYPE_LONG:
*err = grib_get_long(h, v->name, &lvalue);
ret = v->equal ? (lvalue != v->long_value) : (lvalue == v->long_value);
break;
case GRIB_TYPE_MISSING:
lvalue = grib_is_missing(h, v->name, err);
ret = (lvalue == v->equal) ? 0 : 1;
break;
default:
fprintf(dump_file, "invalid type for %s\n", v->name);
exit(1);
}
return ret;
}
void grib_skip_check(grib_runtime_options* options, grib_handle* h)
{
int i, ret = 0;
grib_values* v = NULL;
/* ECC-1179: bufr_copy/bufr_ls: Allow 'where' clause with Data Section keys */
if (options->constraints_count > 0 && h->product_kind == PRODUCT_BUFR) {
for (i = 0; i < options->set_values_count; i++) {
if (strcmp(options->set_values[i].name, "unpack")==0) {
grib_set_long(h, "unpack", 1);
break;
}
}
}
for (i = 0; i < options->constraints_count; i++) {
v = &(options->constraints[i]);
if (v->equal) {
options->skip = 1;
while (v) {
if (!to_skip(h, v, &ret)) {
if (!strcmp(v->name, "count") && !v->next)
options->skip_all = 1;
options->skip = 0;
break;
}
if (ret != GRIB_SUCCESS && options->fail) {
grib_context_log(h->context, GRIB_LOG_ERROR, "unable to get \"%s\" (%s)",
v->name, grib_get_error_message(ret));
exit(ret);
}
v = v->next;
}
}
else {
options->skip = 0;
while (v) {
if (to_skip(h, v, &ret)) {
options->skip = 1;
break;
}
if (ret != GRIB_SUCCESS && options->fail) {
grib_context_log(h->context, GRIB_LOG_ERROR, "unable to get \"%s\" (%s)",
v->name, grib_get_error_message(ret));
exit(ret);
}
v = v->next;
}
}
if (options->skip == 1)
break;
}
if (!options->skip) {
options->filter_handle_count++;
if (options->current_infile)
options->current_infile->filter_handle_count++;
}
}
/* TODO: Does not work for 2.7e+01 */
static int is_valid_JSON_number(const char* input)
{
const char* p = input;
size_t len = 0;
int is_float = 0;
if (p == 0 || *p == '\0')
return 0;
if (*p == '-')
p++;
while (*p) {
if (*p == '.') {
if (is_float) return 0; /*more than 1 dot*/
is_float = 1;
}
if (*p != '.' && !isdigit(*p))
return 0;
p++;
len++;
}
/*
* Note: BUFR keys like typicalTime/rdbtimetime can have values
* like 000000 or 013329 which are invalid JSON numbers.
* In JSON a leading zero must not be followed by another digit
*/
if (!is_float && len > 2 && input[0] == '0' && isdigit(input[1]))
return 0; /* Not a valid JSON number */
return 1;
}
static int get_initial_element_of_array(grib_handle* h, const char* keyName, size_t num_vals, char* value)
{
int err = 0, type = 0;
size_t len = num_vals;
char* sval = NULL;
unsigned char* uval = NULL;
long* lval = NULL;
double* dval = NULL;
grib_context* c = h->context;
Assert(num_vals > 1); /* This is for array keys */
if ((err = grib_get_native_type(h, keyName, &type)) != GRIB_SUCCESS)
return err;
switch (type) {
case GRIB_TYPE_STRING:
grib_get_string_length(h, keyName, &len);
sval = (char*)grib_context_malloc(c, len * sizeof(char));
if (!sval)
return GRIB_OUT_OF_MEMORY;
if ((err = grib_get_string(h, keyName, sval, &len)) != GRIB_SUCCESS) {
free(sval);
return err;
}
sprintf(value, "%s", sval);
free(sval);
break;
case GRIB_TYPE_LONG:
lval = (long*)grib_context_malloc(c, num_vals * sizeof(long));
if (!lval)
return GRIB_OUT_OF_MEMORY;
if ((err = grib_get_long_array(h, keyName, lval, &len)) != GRIB_SUCCESS)
return err;
sprintf(value, "%ld...", lval[0]);
free(lval);
break;
case GRIB_TYPE_DOUBLE:
dval = (double*)grib_context_malloc(c, num_vals * sizeof(double));
if (!dval)
return GRIB_OUT_OF_MEMORY;
if ((err = grib_get_double_array(h, keyName, dval, &len)) != GRIB_SUCCESS)
return err;
sprintf(value, "%g...", dval[0]);
free(dval);
break;
case GRIB_TYPE_BYTES:
uval = (unsigned char*)grib_context_malloc(c, num_vals * sizeof(unsigned char));
if (!uval)
return GRIB_OUT_OF_MEMORY;
if ((err = grib_get_bytes(h, keyName, uval, &len)) != GRIB_SUCCESS)
return err;
sprintf(value, "%d...", (short)uval[0]);
free(uval);
break;
default:
sprintf(value, "%s...", "");
}
return GRIB_SUCCESS;
}
static void get_value_for_key(grib_handle* h, const char* key_name, int key_type, char* value_str,
const char* format, int fix_lsdate, int fix_lstime)
{
int ret = 0, type = key_type;
double dvalue = 0;
long lvalue = 0;
size_t len = MAX_STRING_LEN;
if (grib_is_missing(h, key_name, &ret) && ret == GRIB_SUCCESS) {
sprintf(value_str, "MISSING");
return;
}
if (ret == GRIB_NOT_FOUND) {
sprintf(value_str, "not_found");
return;
}
if (type == GRIB_TYPE_UNDEFINED) {
ret = grib_get_native_type(h, key_name, &type);
if (ret != GRIB_SUCCESS) {