diff --git a/src/grib_accessor_class_bufr_data_array.c b/src/grib_accessor_class_bufr_data_array.c index 31604c2fb..8f74e163f 100644 --- a/src/grib_accessor_class_bufr_data_array.c +++ b/src/grib_accessor_class_bufr_data_array.c @@ -702,6 +702,19 @@ static void set_missing_long_to_double(grib_darray* dvalues) } } +/* ECC-750: The 'factor' argument is 10^-scale */ +static int descriptor_get_min_max(bufr_descriptor* bd, long width, long reference, double factor, + double* minAllowed, double* maxAllowed) +{ + /* Maximum value is allowed to be the largest number (all bits 1) which means it's MISSING */ + unsigned long max1 = (1UL << width) - 1; /* Highest value for number with 'width' bits */ + DebugAssert(width > 0 && width <= 32); + + *maxAllowed = (max1 + reference) * factor; + *minAllowed = reference * factor; + return GRIB_SUCCESS; +} + static int encode_double_array(grib_context* c,grib_buffer* buff,long* pos, bufr_descriptor* bd, grib_accessor_bufr_data_array* self,grib_darray* dvalues) { @@ -731,8 +744,7 @@ static int encode_double_array(grib_context* c,grib_buffer* buff,long* pos, bufr inverseFactor= grib_power(bd->scale,10); modifiedWidth= bd->width; - maxAllowed=(grib_power(modifiedWidth,2)+modifiedReference)*modifiedFactor; - minAllowed=modifiedReference*modifiedFactor; + descriptor_get_min_max(bd, modifiedWidth, modifiedReference, modifiedFactor, &minAllowed, &maxAllowed); nvals=grib_iarray_used_size(self->iss_list); if (nvals<=0) return GRIB_NO_VALUES; @@ -903,8 +915,8 @@ static int encode_double_value(grib_context* c,grib_buffer* buff,long* pos,bufr_ modifiedReference= bd->reference; modifiedFactor= bd->factor; modifiedWidth= bd->width; - maxAllowed=(grib_power(modifiedWidth,2)+modifiedReference)*modifiedFactor; - minAllowed=modifiedReference*modifiedFactor; + + descriptor_get_min_max(bd, modifiedWidth, modifiedReference, modifiedFactor, &minAllowed, &maxAllowed); grib_buffer_set_ulength_bits(c,buff,buff->ulength_bits+modifiedWidth); if (value==GRIB_MISSING_DOUBLE) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9382d5acb..298972a60 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -88,6 +88,7 @@ list( APPEND tests_data_reqd bufr_ecc-379 bufr_ecc-393 bufr_ecc-433 + bufr_ecc-750 grib_ecc-490 bufr_ecc-556 gts_get diff --git a/tests/bufr_ecc-750.sh b/tests/bufr_ecc-750.sh new file mode 100755 index 000000000..f6cba0cd9 --- /dev/null +++ b/tests/bufr_ecc-750.sh @@ -0,0 +1,87 @@ +#!/bin/sh +# Copyright 2005-2018 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.sh + +# --------------------------------------------------------- +# This is the test for the JIRA issue ECC-750. +# Encoding values which exceed the maximum allowed +# --------------------------------------------------------- +label="bufr_ecc-750-test" +tempRules=temp.${label}.filter +tempBufr=temp.${label}.bufr + +# -------------------------------- +# Case 1: airTemperature, width=12 +# -------------------------------- +input=${data_dir}/bufr/temp_101.bufr +cat > $tempRules < $tempRules < $tempRules <