diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cce98f1f1..e4f033d7e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -53,6 +53,7 @@ list(APPEND test_c_bins grib_set_bytes grib_copy_message grib_packing_order + grib_unpack_subarray grib_sh_imag grib_spectral grib_lam_bf @@ -156,6 +157,7 @@ if( HAVE_BUILD_TOOLS ) grib_grid_space_view grib_partial_message grib_headers_only + grib_unpack_subarray grib_count bufr_templates bufr_dump_data diff --git a/tests/grib_unpack_subarray.cc b/tests/grib_unpack_subarray.cc new file mode 100644 index 000000000..a858e17cb --- /dev/null +++ b/tests/grib_unpack_subarray.cc @@ -0,0 +1,47 @@ +/* + * (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 "grib_api_internal.h" + +int main(int argc, char** argv) +{ + Assert(argc == 2); + + int err = 0; + size_t nvalues = 0; + const char* filename = argv[1]; + grib_context* c = grib_context_get_default(); + + FILE* fin = fopen(filename, "r"); + Assert(fin); + grib_handle* h = grib_handle_new_from_file(0, fin, &err); + Assert(h); + Assert(!err); + + grib_accessor* a = grib_find_accessor(h, "codedValues"); + Assert(a); + GRIB_CHECK(grib_get_size(h, "codedValues", &nvalues), 0); + double* values = (double*)grib_context_malloc(c, sizeof(double) * nvalues); + Assert(values); + + size_t start = nvalues / 10; + size_t len = nvalues / 5; + printf("nvalues=%zu, start=%zu, len=%zu\n", nvalues, start, len); + GRIB_CHECK(grib_unpack_double_subarray(a, values, start, len), 0); + for (size_t i = 0; i < len; ++i) { + printf("v[%zu]=%.10e\n", start + i, values[i]); + } + + grib_context_free(c, values); + grib_handle_delete(h); + fclose(fin); + + return 0; +} diff --git a/tests/grib_unpack_subarray.sh b/tests/grib_unpack_subarray.sh new file mode 100755 index 000000000..43bc730d8 --- /dev/null +++ b/tests/grib_unpack_subarray.sh @@ -0,0 +1,20 @@ +#!/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_unpack_subarray_test" +temp=${label}".grib.tmp" + +infile=$data_dir/sample.grib2 +$EXEC ${test_dir}/grib_unpack_subarray $infile + +# Clean up +rm -f $temp