ECC-221: add tests for 'new_from_message' functions (Part 2)

This commit is contained in:
Shahram Najm 2016-04-06 18:13:31 +01:00
parent 983e5b54cd
commit b479ed65d0
3 changed files with 96 additions and 0 deletions

View File

@ -32,6 +32,7 @@ list( APPEND test_bins
grib_iterator_bitmap
large_grib1
grib_clone
grib_copy_message
grib_ensemble_index
get_product_kind
bufr_attributes
@ -67,6 +68,7 @@ list( APPEND tests
grib_multi_write
grib_precision
grib_clone
grib_copy_message
grib_set_pv
grib_set_bitmap
grib_list

View File

@ -0,0 +1,72 @@
/*
* Copyright 2005-2016 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>
void usage(char *app)
{
fprintf(stderr,"Usage is: %s input_file ouput_file\n", app);
}
int main(int argc, char *argv[])
{
FILE *in = NULL;
codes_handle *source_handle = NULL;
int err = 0;
if (argc != 3) {
usage(argv[0]);
return 1;
}
in = fopen(argv[1],"r");
if (!in) {
perror("ERROR: unable to input file");
return 1;
}
/* loop over the GRIB messages in the source */
while ((source_handle = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err))!=NULL)
{
size_t totalLength = 0, size = 0;
const void* buffer = NULL;
codes_handle* new_handle = NULL;
CODES_CHECK(grib_get_message_size(source_handle,&totalLength),0);
buffer=(unsigned char*)malloc(totalLength*sizeof(char));
CODES_CHECK(codes_get_message(source_handle, &buffer, &size),0);
assert(size == totalLength);
new_handle = grib_handle_new_from_message(0, (void*)buffer, totalLength);
if (new_handle == NULL) {
perror("ERROR: could not create GRIB handle from message");
return 1;
}
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;
}

22
examples/C/grib_copy_message.sh Executable file
View File

@ -0,0 +1,22 @@
#!/bin/sh
# Copyright 2005-2016 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.
. ./include.sh
TEMP=c_grib_copy_message.grib
INPUT=${data_dir}/reduced_gaussian_pressure_level.grib1
${examples_dir}c_grib_copy_message ${INPUT} ${TEMP} > /dev/null
${tools_dir}grib_compare -b hour ${INPUT} ${TEMP}
INPUT=${data_dir}/sample.grib2
${examples_dir}c_grib_copy_message ${INPUT} ${TEMP} > /dev/null
${tools_dir}grib_compare -b hour ${INPUT} ${TEMP}
rm -f ${TEMP}