1 /*
2  * Copyright 2005-2018 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 "grib_api.h"
11 #include <ctype.h>
12 
usage(char * prog)13 void usage(char* prog) {
14     printf("usage: %s in1.grib in2.grib what out.grib\n",prog);
15     printf("in1.grib   The grib in whose sections we are interested, i.e. the source of the sections (read-only)\n");
16     printf("in2.grib   The input grib (read-only)\n");
17     printf("what       The section(s) to copy: p(Product), g(Grid), l(Local), d(Data), b(Bitmap)\n");
18     printf("out.grib   The output file\n");
19     exit(1);
20 }
21 
main(int argc,char * argv[])22 int main ( int argc, char* argv[])
23 {
24     grib_handle *hfrom,*hto,*h;
25     FILE *in;
26     char *in_name1, *in_name2, *what_str, *out_name;
27     int i, err=0, what=0;
28 
29     if (argc<5) usage(argv[0]);
30 
31     in_name1=argv[1];
32     in_name2=argv[2];
33     what_str=argv[3];
34     out_name=argv[4];
35 
36     in=fopen(in_name1,"r");
37     if (!in) {
38         perror(in_name1);
39         exit(1);
40     }
41 
42     hfrom=grib_handle_new_from_file(0,in,&err);
43     GRIB_CHECK(err,0);
44     fclose(in);
45 
46     in=fopen(in_name2,"r");
47     if (!in) {
48         perror(in_name2);
49         exit(1);
50     }
51 
52     hto=grib_handle_new_from_file(0,in,&err);
53     GRIB_CHECK(err,0);
54     fclose(in);
55 
56     /* The sections for the "what" argument are:
57      *  GRIB_SECTION_PRODUCT
58      *  GRIB_SECTION_GRID
59      *  GRIB_SECTION_LOCAL
60      *  GRIB_SECTION_DATA
61      *  GRIB_SECTION_BITMAP
62      * One can bitwise-OR them to have more than one section copied
63      * E.g. what = GRIB_SECTION_PRODUCT | GRIB_SECTION_LOCAL;
64      */
65     for(i=0; i<strlen(what_str); ++i) {
66         if (what_str[i] == 'p') {
67             printf("Copying the PRODUCT section\n");
68             what |= GRIB_SECTION_PRODUCT;
69         }
70         else if (what_str[i] == 'g') {
71             printf("Copying the GRID section\n");
72             what |= GRIB_SECTION_GRID;
73         }
74         else if (what_str[i] == 'l') {
75             printf("Copying the LOCAL section\n");
76             what |= GRIB_SECTION_LOCAL;
77         }
78         else if (what_str[i] == 'd') {
79             printf("Copying the DATA section\n");
80             what |= GRIB_SECTION_DATA;
81         }
82         else if (what_str[i] == 'b') {
83             printf("Copying the BITMAP section\n");
84             what |= GRIB_SECTION_BITMAP;
85         }
86         else if (isspace(what_str[i]) || what_str[i] == ',') {
87             /* Ignore spaces and comma separator */
88         }
89         else {
90             fprintf(stderr,"Invalid option: '%c'.  Ignoring.\n",
91                     what_str[i]);
92         }
93     }
94 
95     h=grib_util_sections_copy(hfrom,hto,what,&err);
96     GRIB_CHECK(err,0);
97 
98     err=grib_write_message(h,out_name,"w");
99 
100     grib_handle_delete(hfrom);
101     grib_handle_delete(hto);
102     grib_handle_delete(h);
103 
104     return err;
105 }
106