diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8079b9082..686d63b93 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -17,6 +17,7 @@ list(APPEND test_c_bins grib_indexing grib_fieldset grib_multi_from_message + grib_clone_lightweight grib_read_index unit_tests bufr_keys_iter @@ -171,6 +172,7 @@ if( HAVE_BUILD_TOOLS ) grib_headers_only grib_unpack_subarray grib_count + grib_clone_lightweight bufr_templates bufr_dump_data bufr_dump_descriptors diff --git a/tests/grib_clone_lightweight.cc b/tests/grib_clone_lightweight.cc new file mode 100644 index 000000000..8bdb5bd08 --- /dev/null +++ b/tests/grib_clone_lightweight.cc @@ -0,0 +1,86 @@ +/* + * (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 +#include "eccodes.h" +#undef NDEBUG +#include + +static void usage(const char* app) +{ + fprintf(stderr, "Usage is: %s input_file ouput_file\n", app); +} + +int main(int argc, char* argv[]) +{ + FILE* in = NULL; + FILE* out = NULL; + codes_handle* source_handle = NULL; + const void* buffer = NULL; + int err = 0; + + long totalLength_src = 0, totalLength_dst = 0; + long edition = 0; + long dataSectionLength_src = 0, dataSectionLength_dst = 0; + long bitmapPresent_dst = 0, isConstant = 0; + size_t messageLength_src = 0, messageLength_dst = 0; + + if (argc != 3) { + usage(argv[0]); + return 1; + } + + in = fopen(argv[1], "rb"); + assert(in); + out = fopen(argv[2], "wb"); + assert(out); + + while ((source_handle = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err)) != NULL) { + codes_handle* clone_handle = codes_handle_clone_lightweight(source_handle); + assert(clone_handle); + + CODES_CHECK(codes_get_message(source_handle, &buffer, &messageLength_src), 0); + CODES_CHECK(codes_get_message(clone_handle, &buffer, &messageLength_dst), 0); + assert( messageLength_src > messageLength_dst ); + + CODES_CHECK(codes_get_long(source_handle, "totalLength", &totalLength_src), 0); + CODES_CHECK(codes_get_long(clone_handle, "totalLength", &totalLength_dst), 0); + assert(totalLength_src > totalLength_dst); + + CODES_CHECK(codes_get_long(source_handle, "edition", &edition), 0); + if (edition == 1) { + CODES_CHECK(codes_get_long(source_handle, "section4Length", &dataSectionLength_src), 0); + CODES_CHECK(codes_get_long(clone_handle, "section4Length", &dataSectionLength_dst), 0); + } else if (edition == 2) { + CODES_CHECK(codes_get_long(source_handle, "section7Length", &dataSectionLength_src), 0); + CODES_CHECK(codes_get_long(clone_handle, "section7Length", &dataSectionLength_dst), 0); + } + //printf("sectionLengths: %ld %ld\n", dataSectionLength_src, dataSectionLength_dst); + assert( dataSectionLength_src > dataSectionLength_dst ); + + codes_get_long(clone_handle, "bitmapPresent", &bitmapPresent_dst); + assert(bitmapPresent_dst == 0); + codes_get_long(clone_handle, "isConstant", &isConstant); + assert(isConstant == 1); + + /* write out the cloned buffer */ + if (fwrite(buffer, 1, messageLength_dst, out) != messageLength_dst) { + perror(argv[1]); + return 1; + } + codes_handle_delete(clone_handle); + codes_handle_delete(source_handle); + } + + fclose(out); + fclose(in); + + printf("All OK\n"); + return 0; +} diff --git a/tests/grib_clone_lightweight.sh b/tests/grib_clone_lightweight.sh new file mode 100755 index 000000000..3e87f8c64 --- /dev/null +++ b/tests/grib_clone_lightweight.sh @@ -0,0 +1,30 @@ +#!/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 + +label="grib_clone_lightweight_test" +temp=temp.$label.grib + +inputs=" + sst_globus0083.grib + sample.grib2 + mixed.grib +" + +for f in $inputs; do + infile=$data_dir/$f + rm -f $temp + $EXEC ${test_dir}/grib_clone_lightweight $infile $temp + ${tools_dir}/grib_compare -H -b totalLength $infile $temp +done + +# Clean up +rm -f $temp