From 05de92a8f1242986068023832cb1013444866e71 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Sun, 28 Jan 2024 14:50:24 +0000 Subject: [PATCH] Testing: grib_copy_key (arrays) --- examples/C/CMakeLists.txt | 2 ++ examples/C/grib_copy_keys.c | 69 ++++++++++++++++++++++++++++++++++++ examples/C/grib_copy_keys.sh | 15 ++++++++ 3 files changed, 86 insertions(+) create mode 100644 examples/C/grib_copy_keys.c create mode 100755 examples/C/grib_copy_keys.sh diff --git a/examples/C/CMakeLists.txt b/examples/C/CMakeLists.txt index a50dc8d0a..5e3795ef8 100644 --- a/examples/C/CMakeLists.txt +++ b/examples/C/CMakeLists.txt @@ -25,6 +25,7 @@ list( APPEND test_bins grib_list grib_get_data grib_sections_copy + grib_copy_keys grib_iterator_bitmap grib_clone grib_copy_message @@ -65,6 +66,7 @@ if( HAVE_BUILD_TOOLS ) grib_set_data large_grib1 grib_sections_copy + grib_copy_keys get_product_kind_samples) list(APPEND tests_extra grib_iterator diff --git a/examples/C/grib_copy_keys.c b/examples/C/grib_copy_keys.c new file mode 100644 index 000000000..1b2de9fe7 --- /dev/null +++ b/examples/C/grib_copy_keys.c @@ -0,0 +1,69 @@ +/* + * (C) Copyright 2005- 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 "eccodes.h" +#include + +static void usage(const char* prog) +{ + printf("usage: %s in1.grib in2.grib\n", prog); + exit(1); +} + +int main(int argc, char* argv[]) +{ + codes_handle *hfrom, *hto; + FILE* in; + char *in_name1, *in_name2; + int i = 0, err = 0; + const char* keys[] = { "gridType", "pl", "values" }; + const int keys_count = sizeof(keys) / sizeof(keys[0]); + + if (argc != 3) usage(argv[0]); + + in_name1 = argv[1]; + in_name2 = argv[2]; + + in = fopen(in_name1, "rb"); + if (!in) { + perror(in_name1); + exit(1); + } + + hfrom = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err); + CODES_CHECK(err, 0); + fclose(in); + + in = fopen(in_name2, "rb"); + if (!in) { + perror(in_name2); + exit(1); + } + + hto = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err); + CODES_CHECK(err, 0); + fclose(in); + + for (i = 0; i < keys_count; i++) { + printf("Copy key: %s\n", keys[i]); + err = codes_copy_key(hfrom, hto, keys[i], GRIB_TYPE_UNDEFINED); + CODES_CHECK(err, 0); + } + + // codes_write_message(hto, "temp.out", "w"); CODES_CHECK(err, 0); + { + int dump_flags = GRIB_DUMP_FLAG_CODED | GRIB_DUMP_FLAG_OCTET | GRIB_DUMP_FLAG_VALUES | GRIB_DUMP_FLAG_READ_ONLY; + codes_dump_content(hto, stdout, "wmo", dump_flags, NULL); + } + + codes_handle_delete(hfrom); + codes_handle_delete(hto); + + return err; +} diff --git a/examples/C/grib_copy_keys.sh b/examples/C/grib_copy_keys.sh new file mode 100755 index 000000000..20b247242 --- /dev/null +++ b/examples/C/grib_copy_keys.sh @@ -0,0 +1,15 @@ +#!/bin/sh +# (C) Copyright 2005- 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.ctest.sh + +FROM_FILE=$ECCODES_SAMPLES_PATH/gg_sfc_grib2.tmpl +TO_FILE=$ECCODES_SAMPLES_PATH/GRIB2.tmpl + +${examples_dir}/c_grib_copy_keys $FROM_FILE $TO_FILE