eccodes/examples/C/grib_sections_copy.c

106 lines
3.1 KiB
C
Raw Normal View History

2013-03-25 12:04:10 +00:00
/*
2020-01-28 14:32:34 +00:00
* (C) Copyright 2005- ECMWF.
2013-03-25 12:04:10 +00:00
*
* 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 "eccodes.h"
2014-06-18 16:14:01 +00:00
#include <ctype.h>
2013-03-25 12:04:10 +00:00
2020-01-22 14:57:43 +00:00
static void usage(const char* prog)
{
printf("usage: %s in1.grib in2.grib what out.grib\n", prog);
2014-06-18 16:14:01 +00:00
printf("in1.grib The grib in whose sections we are interested, i.e. the source of the sections (read-only)\n");
printf("in2.grib The input grib (read-only)\n");
printf("what The section(s) to copy: p(Product), g(Grid), l(Local), d(Data), b(Bitmap)\n");
printf("out.grib The output file\n");
exit(1);
2013-03-25 12:04:10 +00:00
}
2020-01-22 14:57:43 +00:00
int main(int argc, char* argv[])
2014-06-18 16:14:01 +00:00
{
2020-01-22 14:57:43 +00:00
codes_handle *hfrom, *hto, *h;
FILE* in;
2014-06-18 16:14:01 +00:00
char *in_name1, *in_name2, *what_str, *out_name;
2020-01-22 14:57:43 +00:00
int i, err = 0, what = 0;
2013-03-25 12:04:10 +00:00
2020-01-22 14:57:43 +00:00
if (argc < 5) usage(argv[0]);
2013-03-25 12:04:10 +00:00
2020-01-22 14:57:43 +00:00
in_name1 = argv[1];
in_name2 = argv[2];
what_str = argv[3];
out_name = argv[4];
2013-03-25 12:04:10 +00:00
2020-01-22 14:57:43 +00:00
in = fopen(in_name1, "rb");
2014-06-18 16:14:01 +00:00
if (!in) {
perror(in_name1);
exit(1);
}
2013-03-25 12:04:10 +00:00
2020-01-22 14:57:43 +00:00
hfrom = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
CODES_CHECK(err, 0);
2014-06-18 16:14:01 +00:00
fclose(in);
2013-03-25 12:04:10 +00:00
2020-01-22 14:57:43 +00:00
in = fopen(in_name2, "rb");
2014-06-18 16:14:01 +00:00
if (!in) {
perror(in_name2);
exit(1);
}
2013-03-25 12:04:10 +00:00
2020-01-22 14:57:43 +00:00
hto = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
CODES_CHECK(err, 0);
2014-06-18 16:14:01 +00:00
fclose(in);
2013-03-25 12:04:10 +00:00
2021-11-30 12:44:37 +00:00
/* the sections for the "what" argument are:
* CODES_SECTION_PRODUCT
* CODES_SECTION_GRID
* CODES_SECTION_LOCAL
* CODES_SECTION_DATA
* CODES_SECTION_BITMAP
* one can bitwise-OR them to have more than one section copied
* e.g. what = CODES_SECTION_PRODUCT | CODES_SECTION_LOCAL;
2014-06-18 16:14:01 +00:00
*/
2020-01-22 14:57:43 +00:00
for (i = 0; i < strlen(what_str); ++i) {
2014-06-18 16:14:01 +00:00
if (what_str[i] == 'p') {
printf("Copying the PRODUCT section\n");
what |= CODES_SECTION_PRODUCT;
2014-06-18 16:14:01 +00:00
}
else if (what_str[i] == 'g') {
printf("Copying the GRID section\n");
what |= CODES_SECTION_GRID;
2014-06-18 16:14:01 +00:00
}
else if (what_str[i] == 'l') {
printf("Copying the LOCAL section\n");
what |= CODES_SECTION_LOCAL;
2014-06-18 16:14:01 +00:00
}
else if (what_str[i] == 'd') {
printf("Copying the DATA section\n");
what |= CODES_SECTION_DATA;
2014-06-18 16:14:01 +00:00
}
else if (what_str[i] == 'b') {
printf("Copying the BITMAP section\n");
what |= CODES_SECTION_BITMAP;
2014-06-18 16:14:01 +00:00
}
else if (isspace(what_str[i]) || what_str[i] == ',') {
2021-11-30 12:44:37 +00:00
/* ignore spaces and comma separator */
2014-06-18 16:14:01 +00:00
}
else {
2021-11-30 12:44:37 +00:00
fprintf(stderr, "Invalid option: '%c'. Ignoring.\n", what_str[i]);
2014-06-18 16:14:01 +00:00
}
}
2015-12-22 17:55:45 +00:00
2020-01-22 14:57:43 +00:00
h = codes_grib_util_sections_copy(hfrom, hto, what, &err);
CODES_CHECK(err, 0);
2013-03-25 12:04:10 +00:00
2020-01-22 14:57:43 +00:00
err = codes_write_message(h, out_name, "w");
2013-03-25 12:04:10 +00:00
codes_handle_delete(hfrom);
codes_handle_delete(hto);
codes_handle_delete(h);
2014-06-18 16:14:01 +00:00
return err;
2013-03-25 12:04:10 +00:00
}