From d0d1c1fa31b35d447ea1d98efae8ead33a21ab24 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Wed, 14 Jun 2023 19:49:23 +0100 Subject: [PATCH] ECC-1611: Add test --- tests/CMakeLists.txt | 3 ++ tests/bufr_coordinate_descriptors.cc | 74 ++++++++++++++++++++++++++++ tests/bufr_coordinate_descriptors.sh | 21 ++++++++ 3 files changed, 98 insertions(+) create mode 100644 tests/bufr_coordinate_descriptors.cc create mode 100755 tests/bufr_coordinate_descriptors.sh diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8e64e8692..5ca17c7a9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -41,6 +41,7 @@ list(APPEND test_c_bins bufr_extract_headers extract_offsets bufr_check_descriptors + bufr_coordinate_descriptors codes_new_from_samples codes_set_samples_path grib_sh_ieee64 @@ -75,6 +76,7 @@ if( HAVE_BUILD_TOOLS ) grib_dump_samples bufr_dump_samples bufr_check_descriptors + bufr_coordinate_descriptors definitions grib2_version grib_calendar @@ -440,6 +442,7 @@ else() unit_tests julian bufr_check_descriptors + bufr_coordinate_descriptors grib_sh_imag grib_sh_spectral_complex grib_2nd_order_numValues diff --git a/tests/bufr_coordinate_descriptors.cc b/tests/bufr_coordinate_descriptors.cc new file mode 100644 index 000000000..c089f16e2 --- /dev/null +++ b/tests/bufr_coordinate_descriptors.cc @@ -0,0 +1,74 @@ +/* + * (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" +#undef NDEBUG +#include + +static int is_coord_using_Xcode(const char* X) +{ + if (strcmp(X, "01")==0) return 1; + if (strcmp(X, "02")==0) return 1; + if (strcmp(X, "04")==0) return 1; + if (strcmp(X, "05")==0) return 1; + if (strcmp(X, "06")==0) return 1; + if (strcmp(X, "07")==0) return 1; + if (strcmp(X, "08")==0) return 1; + return 0; +} + +int main(int argc, char* argv[]) +{ + int err = 0; + codes_handle* h = NULL; + char* filename = NULL; + FILE* fin = NULL; + + assert (argc == 2); + filename = argv[1]; + + fin = fopen(filename, "rb"); + assert(fin); + while ((h = codes_handle_new_from_file(NULL, fin, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) { + codes_bufr_keys_iterator* kiter = NULL; + CODES_CHECK(codes_set_long(h, "unpack", 1), 0); + kiter = codes_bufr_keys_iterator_new(h, 0); + assert(kiter); + while (codes_bufr_keys_iterator_next(kiter)) { + char* name = codes_bufr_keys_iterator_get_name(kiter); + if (strcmp(name, "subsetNumber") == 0) continue; + if (!codes_bufr_key_is_header(h, name, &err) && !err) { + char name1[256] = {0,}; + char scode[256] = {0,}; + char X[3] = {0,}; // the 'X' part of FXY + size_t slen = 256; + snprintf(name1, 256, "%s->code", name); + int error = codes_get_string(h, name1, scode, &slen); + if (!error) { + assert( strlen(scode)==6); + X[0] = scode[1]; + X[1] = scode[2]; + X[2] = 0; + int is_X_coord = is_coord_using_Xcode(X); + int is_coord = codes_bufr_key_is_coordinate(h, name, &error); + assert(!error); + if (is_coord != is_X_coord) { + fprintf(stderr, "ERROR: %s X=%s is_coord=%d is_X_coord=%d\n", name, X, is_coord, is_X_coord); + return 1; + } + } + } + } + codes_bufr_keys_iterator_delete(kiter); + codes_handle_delete(h); + } + fclose(fin); + return 0; +} diff --git a/tests/bufr_coordinate_descriptors.sh b/tests/bufr_coordinate_descriptors.sh new file mode 100755 index 000000000..41aebf8ba --- /dev/null +++ b/tests/bufr_coordinate_descriptors.sh @@ -0,0 +1,21 @@ +#!/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="bufr_coordinate_descriptors_test" +temp=temp.$label.txt + +bufr_files=`cat ${data_dir}/bufr/bufr_data_files.txt` +for f in ${bufr_files}; do + fpath=${data_dir}/bufr/$f + ${test_dir}/bufr_coordinate_descriptors $fpath +done +rm -f $temp