From dbc307dd59df6c61e34ac2948be33c2ffb59fb7e Mon Sep 17 00:00:00 2001 From: shahramn Date: Wed, 3 Apr 2024 16:43:50 +0100 Subject: [PATCH] ECC-1792: GRIB: headers_only cloning does not work as expected after changing geography --- src/grib_accessor_class_latitudes.cc | 7 ++++ src/grib_accessor_class_longitudes.cc | 7 ++++ src/grib_iterator_class_gen.cc | 19 ++++++--- tests/CMakeLists.txt | 1 + tests/grib_ecc-1792.sh | 57 +++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 5 deletions(-) create mode 100755 tests/grib_ecc-1792.sh diff --git a/src/grib_accessor_class_latitudes.cc b/src/grib_accessor_class_latitudes.cc index 4131ff066..555fb5b19 100644 --- a/src/grib_accessor_class_latitudes.cc +++ b/src/grib_accessor_class_latitudes.cc @@ -221,6 +221,13 @@ static int value_count(grib_accessor* a, long* len) } *len = size; + // See ECC-1792 + // Give priority to the Grid Section (rather than the Data Section) + long numberOfDataPoints = 0; + if (grib_get_long(h, "numberOfDataPoints", &numberOfDataPoints) == GRIB_SUCCESS) { + *len = numberOfDataPoints; + } + if (self->distinct) { ret = get_distinct(a, &val, len); if (ret != GRIB_SUCCESS) diff --git a/src/grib_accessor_class_longitudes.cc b/src/grib_accessor_class_longitudes.cc index 083d40138..78fc7a733 100644 --- a/src/grib_accessor_class_longitudes.cc +++ b/src/grib_accessor_class_longitudes.cc @@ -196,6 +196,13 @@ static int value_count(grib_accessor* a, long* len) } *len = size; + // See ECC-1792 + // Give priority to the Grid Section (rather than the Data Section) + long numberOfDataPoints = 0; + if (grib_get_long(h, "numberOfDataPoints", &numberOfDataPoints) == GRIB_SUCCESS) { + *len = numberOfDataPoints; + } + if (self->distinct) { ret = get_distinct(a, &val, len); if (ret != GRIB_SUCCESS) diff --git a/src/grib_iterator_class_gen.cc b/src/grib_iterator_class_gen.cc index 02fe38c28..7124e8cf9 100644 --- a/src/grib_iterator_class_gen.cc +++ b/src/grib_iterator_class_gen.cc @@ -193,12 +193,21 @@ static int init(grib_iterator* iter, grib_handle* h, grib_arguments* args) if ((err = grib_get_long_internal(h, s_numPoints, &numberOfPoints)) != GRIB_SUCCESS) return err; - if (numberOfPoints != dli) { - grib_context_log(h->context, GRIB_LOG_ERROR, "Geoiterator: %s != size(%s) (%ld!=%ld)", - s_numPoints, s_rawData, numberOfPoints, dli); - return GRIB_WRONG_GRID; + // See ECC-1792. If we don't want to decode the Data Section, we should not do a check + // to see if it is consistent with the Grid Section + if (iter->flags & GRIB_GEOITERATOR_NO_VALUES) { + // Iterator's number of values taken from the Grid Section + iter->nv = numberOfPoints; + } else { + // Check for consistency between the Grid and Data Sections + if (numberOfPoints != dli) { + grib_context_log(h->context, GRIB_LOG_ERROR, "Geoiterator: %s != size(%s) (%ld!=%ld)", + s_numPoints, s_rawData, numberOfPoints, dli); + return GRIB_WRONG_GRID; + } + iter->nv = dli; } - iter->nv = dli; + if (iter->nv == 0) { grib_context_log(h->context, GRIB_LOG_ERROR, "Geoiterator: size(%s) is %ld", s_rawData, dli); return GRIB_WRONG_GRID; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e1f11445c..ff56770f8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -266,6 +266,7 @@ if( HAVE_BUILD_TOOLS ) grib_ecc-1425 grib_ecc-1467 grib_ecc-1764 + grib_ecc-1792 grib_sub_hourly grib_set_bytes grib_set_force diff --git a/tests/grib_ecc-1792.sh b/tests/grib_ecc-1792.sh new file mode 100755 index 000000000..960da284d --- /dev/null +++ b/tests/grib_ecc-1792.sh @@ -0,0 +1,57 @@ +#!/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 + +REDIRECT=/dev/null + +label="grib_ecc-1792_test" +tempGrib=temp.$label.grib +tempFilt=temp.$label.filt +tempLog=temp.$label.log +tempRef=temp.$label.ref + +sample_grib2=$ECCODES_SAMPLES_PATH/GRIB2.tmpl + +# Create a message with its Data Section out-of-sync with its Grid +${tools_dir}/grib_set -s numberOfValues=2 $sample_grib2 $tempGrib +grib_check_key_equals $tempGrib numberOfDataPoints 496 + +# grib_get_data should fail +set +e +${tools_dir}/grib_get_data $tempGrib > $tempLog 2>&1 +status=$? +set -e +[ $status -ne 0 ] +grep "Grid description is wrong" $tempLog + +# Just getting lats and lons; no need for Data Section to be in-sync +cat >$tempFilt<$tempFilt< $tempLog 2>&1 +status=$? +set -e +[ $status -ne 0 ] +grep "Grid description is wrong" $tempLog + + +# Clean up +rm -f $tempGrib $tempFilt $tempLog $tempRef