eccodes/examples/C/grib_copy_message.c

72 lines
2.0 KiB
C
Raw Normal View History

/*
2019-04-15 13:44:45 +00:00
* Copyright 2005-2019 ECMWF.
*
* 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.
*/
/*
* C Implementation: grib_copy_message
*
* Description: How to copy a GRIB message in memory
*
*/
#include <stdio.h>
#include "eccodes.h"
#include <assert.h>
2020-01-22 14:57:43 +00:00
static void usage(const char* prog)
{
2020-01-22 14:57:43 +00:00
fprintf(stderr, "Usage is: %s input_file ouput_file\n", prog);
}
2020-01-22 14:57:43 +00:00
int main(int argc, char* argv[])
{
2020-01-22 14:57:43 +00:00
FILE* in = NULL;
codes_handle* source_handle = NULL;
int err = 0;
if (argc != 3) {
usage(argv[0]);
return 1;
}
2020-01-22 14:57:43 +00:00
in = fopen(argv[1], "rb");
if (!in) {
perror("ERROR: unable to input file");
return 1;
}
/* loop over the GRIB messages in the source */
2020-01-22 14:57:43 +00:00
while ((source_handle = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err)) != NULL) {
size_t totalLength = 0, size = 0;
2020-01-22 14:57:43 +00:00
const void* buffer = NULL;
codes_handle* new_handle = NULL;
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_message_size(source_handle, &totalLength), 0);
buffer = (unsigned char*)malloc(totalLength * sizeof(char));
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_message(source_handle, &buffer, &size), 0);
assert(size == totalLength);
2017-01-11 13:58:06 +00:00
new_handle = codes_handle_new_from_message(0, buffer, totalLength);
if (new_handle == NULL) {
perror("ERROR: could not create GRIB handle from message");
return 1;
}
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_set_long(new_handle, "hour", 18), 0);
CODES_CHECK(codes_write_message(new_handle, argv[2], "w"), 0);
codes_handle_delete(new_handle);
codes_handle_delete(source_handle);
}
fclose(in);
return 0;
}