eccodes/examples/C/bufr_clone.c

96 lines
2.5 KiB
C
Raw Permalink Normal View History

2015-02-04 15:58:52 +00:00
/*
2020-01-28 14:32:34 +00:00
* (C) Copyright 2005- ECMWF.
2015-02-04 15:58:52 +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.
*/
/*
2015-02-09 15:41:15 +00:00
* C implementation: bufr_clone
2015-02-04 15:58:52 +00:00
*
2015-02-09 15:41:15 +00:00
* Description: how to create new BUFR messages by cloning
2015-02-04 15:58:52 +00:00
* an existing message.
*
*/
#include <stdio.h>
#include "eccodes.h"
2020-01-22 14:57:43 +00:00
int main(int argc, char* argv[])
2015-02-04 15:58:52 +00:00
{
2020-01-22 14:57:43 +00:00
FILE* in = NULL;
FILE* out = NULL;
2015-03-17 13:36:47 +00:00
2015-12-22 17:55:45 +00:00
/* message handle. Required in all the ecCodes calls acting on a message.*/
2020-01-22 14:57:43 +00:00
codes_handle* source_handle = NULL;
2015-03-17 13:36:47 +00:00
2020-01-22 14:57:43 +00:00
const void* buffer = NULL;
size_t size = 0;
2015-02-09 15:41:15 +00:00
int i, err = 0;
2015-02-04 15:58:52 +00:00
if (argc != 3) {
return 1;
}
2020-01-22 14:57:43 +00:00
in = fopen(argv[1], "rb");
out = fopen(argv[2], "wb");
2015-02-04 15:58:52 +00:00
2015-02-09 15:41:15 +00:00
/* open input and output */
2015-02-04 15:58:52 +00:00
if (!in || !out) {
perror("ERROR: unable to open files");
2015-03-05 15:00:55 +00:00
if (out) fclose(out);
if (in) fclose(in);
2015-02-04 15:58:52 +00:00
return 1;
}
2015-03-17 13:36:47 +00:00
2015-02-09 15:41:15 +00:00
/* create a handle for the first message */
2015-12-22 17:55:45 +00:00
source_handle = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err);
2015-02-09 15:41:15 +00:00
if (source_handle == NULL) {
perror("ERROR: could not create handle for message");
2015-02-09 16:27:44 +00:00
fclose(out);
fclose(in);
2015-02-09 15:41:15 +00:00
return 1;
}
/* create several clones of this message and alter them
in different ways */
2020-01-22 14:57:43 +00:00
for (i = 0; i < 3; i++) {
2015-02-04 16:32:00 +00:00
/* clone the current handle */
2020-01-22 14:57:43 +00:00
codes_handle* clone_handle = codes_handle_clone(source_handle);
2015-02-04 15:58:52 +00:00
if (clone_handle == NULL) {
perror("ERROR: could not clone field");
return 1;
}
2015-02-09 15:41:15 +00:00
/* This is the place where you may wish to modify the clone
2019-07-31 11:45:15 +00:00
E.g. we change the typicalDay key */
2015-03-17 13:36:47 +00:00
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_set_long(clone_handle, "typicalDay", 20 + i), 0);
2015-02-09 15:41:15 +00:00
2015-02-04 15:58:52 +00:00
/* get the coded message in a buffer */
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_message(clone_handle, &buffer, &size), 0);
2015-03-17 13:36:47 +00:00
2015-02-04 15:58:52 +00:00
/* write the buffer to a file */
2020-01-22 14:57:43 +00:00
if (fwrite(buffer, 1, size, out) != size) {
2015-02-09 15:41:15 +00:00
perror("ERROR: could not write message to file");
2015-02-04 15:58:52 +00:00
return 1;
}
2015-03-17 13:36:47 +00:00
2016-09-13 16:48:52 +00:00
/* release the clone's handle */
2015-02-04 15:58:52 +00:00
codes_handle_delete(clone_handle);
}
2015-03-17 13:36:47 +00:00
2015-02-09 15:41:15 +00:00
/* release the source's handle */
codes_handle_delete(source_handle);
2015-02-04 15:58:52 +00:00
fclose(out);
fclose(in);
return 0;
}