1 /*
2  * (C) Copyright 2005- ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  *
7  * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8  * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9  */
10 #include "eccodes.h"
11 #include <ctype.h>
12 
usage(const char * prog)13 static void usage(const char* prog)
14 {
15     printf("usage: %s in1.grib in2.grib what out.grib\n", prog);
16     printf("in1.grib   The grib in whose sections we are interested, i.e. the source of the sections (read-only)\n");
17     printf("in2.grib   The input grib (read-only)\n");
18     printf("what       The section(s) to copy: p(Product), g(Grid), l(Local), d(Data), b(Bitmap)\n");
19     printf("out.grib   The output file\n");
20     exit(1);
21 }
22 
main(int argc,char * argv[])23 int main(int argc, char* argv[])
24 {
25     codes_handle *hfrom, *hto, *h;
26     FILE* in;
27     char *in_name1, *in_name2, *what_str, *out_name;
28     int i, err = 0, what = 0;
29 
30     if (argc < 5) usage(argv[0]);
31 
32     in_name1 = argv[1];
33     in_name2 = argv[2];
34     what_str = argv[3];
35     out_name = argv[4];
36 
37     in = fopen(in_name1, "rb");
38     if (!in) {
39         perror(in_name1);
40         exit(1);
41     }
42 
43     hfrom = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
44     CODES_CHECK(err, 0);
45     fclose(in);
46 
47     in = fopen(in_name2, "rb");
48     if (!in) {
49         perror(in_name2);
50         exit(1);
51     }
52 
53     hto = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
54     CODES_CHECK(err, 0);
55     fclose(in);
56 
57     /* The sections for the "what" argument are:
58      *  CODES_SECTION_PRODUCT
59      *  CODES_SECTION_GRID
60      *  CODES_SECTION_LOCAL
61      *  CODES_SECTION_DATA
62      *  CODES_SECTION_BITMAP
63      * One can bitwise-OR them to have more than one section copied
64      * E.g. what = CODES_SECTION_PRODUCT | CODES_SECTION_LOCAL;
65      */
66     for (i = 0; i < strlen(what_str); ++i) {
67         if (what_str[i] == 'p') {
68             printf("Copying the PRODUCT section\n");
69             what |= CODES_SECTION_PRODUCT;
70         }
71         else if (what_str[i] == 'g') {
72             printf("Copying the GRID section\n");
73             what |= CODES_SECTION_GRID;
74         }
75         else if (what_str[i] == 'l') {
76             printf("Copying the LOCAL section\n");
77             what |= CODES_SECTION_LOCAL;
78         }
79         else if (what_str[i] == 'd') {
80             printf("Copying the DATA section\n");
81             what |= CODES_SECTION_DATA;
82         }
83         else if (what_str[i] == 'b') {
84             printf("Copying the BITMAP section\n");
85             what |= CODES_SECTION_BITMAP;
86         }
87         else if (isspace(what_str[i]) || what_str[i] == ',') {
88             /* Ignore spaces and comma separator */
89         }
90         else {
91             fprintf(stderr, "Invalid option: '%c'.  Ignoring.\n",
92                     what_str[i]);
93         }
94     }
95 
96     h = codes_grib_util_sections_copy(hfrom, hto, what, &err);
97     CODES_CHECK(err, 0);
98 
99     err = codes_write_message(h, out_name, "w");
100 
101     codes_handle_delete(hfrom);
102     codes_handle_delete(hto);
103     codes_handle_delete(h);
104 
105     return err;
106 }
107