eccodes/examples/C/sections_copy.c

106 lines
3.0 KiB
C
Raw Normal View History

2013-03-25 12:04:10 +00:00
/*
2017-01-03 11:03:48 +00:00
* Copyright 2005-2017 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
2017-08-25 16:26:08 +00:00
static void usage(const char* prog) {
2014-06-18 16:14:01 +00:00
printf("usage: %s in1.grib in2.grib what out.grib\n",prog);
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
}
2014-06-18 16:14:01 +00:00
int main ( int argc, char* argv[])
{
codes_handle *hfrom,*hto,*h;
2014-06-18 16:14:01 +00:00
FILE *in;
char *in_name1, *in_name2, *what_str, *out_name;
int i, err=0, what=0;
2013-03-25 12:04:10 +00:00
2014-06-18 16:14:01 +00:00
if (argc<5) usage(argv[0]);
2013-03-25 12:04:10 +00:00
2014-06-18 16:14:01 +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
2014-06-18 16:14:01 +00:00
in=fopen(in_name1,"r");
if (!in) {
perror(in_name1);
exit(1);
}
2013-03-25 12:04:10 +00:00
2015-02-12 17:34:01 +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
2014-06-18 16:14:01 +00:00
in=fopen(in_name2,"r");
if (!in) {
perror(in_name2);
exit(1);
}
2013-03-25 12:04:10 +00:00
2015-02-12 17:34:01 +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
2014-06-18 16:14:01 +00:00
/* The sections for the "what" argument are:
* CODES_SECTION_PRODUCT
* CODES_SECTION_GRID
* CODES_SECTION_LOCAL
* CODES_SECTION_DATA
* CODES_SECTION_BITMAP
2014-06-18 16:14:01 +00:00
* 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
*/
for(i=0; i<strlen(what_str); ++i) {
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] == ',') {
/* Ignore spaces and comma separator */
}
else {
fprintf(stderr,"Invalid option: '%c'. Ignoring.\n",
what_str[i]);
}
}
2015-12-22 17:55:45 +00:00
h=codes_grib_util_sections_copy(hfrom,hto,what,&err);
CODES_CHECK(err,0);
2013-03-25 12:04:10 +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
}