ECC-979: grib_util_set_spec: Allow encoding of double values on scaledValueOf/scaleFactorOf keys

This commit is contained in:
Shahram Najm 2019-08-27 15:45:21 +01:00
commit 29441c8167
5 changed files with 168 additions and 36 deletions

View File

@ -69,8 +69,14 @@ _if (shapeOfTheEarth == 3){
scaleFactorOfEarthMajorAxis, scaledValueOfEarthMajorAxis);
meta earthMinorAxis from_scale_factor_scaled_value(
scaleFactorOfEarthMinorAxis, scaledValueOfEarthMinorAxis);
meta earthMajorAxisInMetres scale(earthMajorAxis, thousand, one, zero);
meta earthMinorAxisInMetres scale(earthMinorAxis, thousand, one, zero);
# The 'scale' accessor works with integers so rounds its first argument
# which is not what we want because the inputs are doubles with decimal
# expansions. So use the trick of dividing by 0.001 to multiply by 1000
# meta earthMajorAxisInMetres scale(earthMajorAxis, thousand, one, zero);
# meta earthMinorAxisInMetres scale(earthMinorAxis, thousand, one, zero);
meta earthMajorAxisInMetres divdouble(earthMajorAxis, 0.001);
meta earthMinorAxisInMetres divdouble(earthMinorAxis, 0.001);
}
_if (shapeOfTheEarth == 7){
# Major and minor axes specified (in m) by data producer

View File

@ -138,38 +138,34 @@ static void init_class(grib_accessor_class* c)
static void init(grib_accessor* a,const long l, grib_arguments* c)
{
grib_accessor_divdouble* self = (grib_accessor_divdouble*)a;
int n = 0;
grib_accessor_divdouble* self = (grib_accessor_divdouble*)a;
int n = 0;
self->val = grib_arguments_get_name(grib_handle_of_accessor(a),c,n++);
self->divisor = grib_arguments_get_double(grib_handle_of_accessor(a),c,n++);
self->val = grib_arguments_get_name(grib_handle_of_accessor(a),c,n++);
self->divisor = grib_arguments_get_double(grib_handle_of_accessor(a),c,n++);
}
static int unpack_double (grib_accessor* a, double* val, size_t *len)
static int unpack_double(grib_accessor* a, double* val, size_t *len)
{
grib_accessor_divdouble* self = (grib_accessor_divdouble*)a;
int ret = GRIB_SUCCESS;
long ivalue = 0;
double value = 0;
grib_accessor_divdouble* self = (grib_accessor_divdouble*)a;
int ret = GRIB_SUCCESS;
double value = 0;
if(*len < 1)
{
*len = 1;
return GRIB_ARRAY_TOO_SMALL;
}
if(*len < 1)
{
*len = 1;
return GRIB_ARRAY_TOO_SMALL;
}
ret = grib_get_double_internal(grib_handle_of_accessor(a), self->val, &value);
ret = grib_get_long_internal(grib_handle_of_accessor(a), self->val, &ivalue);
if(ret != GRIB_SUCCESS )
return ret;
value = ivalue;
/* fprintf(stdout,"\nname %s %s %g/%g\n",a->name ,self->val,value,divisor);*/
Assert(self->divisor!=0);
*val = value/self->divisor;
if(ret != GRIB_SUCCESS )
return ret;
/* fprintf(stdout,"\nname %s %s %g/%g\n",a->name ,self->val,value,divisor);*/
*val = value/self->divisor;
*len = 1;
return GRIB_SUCCESS;
*len = 1;
return GRIB_SUCCESS;
}

View File

@ -14,13 +14,14 @@
#include "grib_api_internal.h"
#include <math.h>
/*
This is used by make_class.pl
START_CLASS_DEF
CLASS = accessor
SUPER = grib_accessor_class_double
IMPLEMENTS = unpack_double;is_missing
IMPLEMENTS = unpack_double;pack_double;is_missing
IMPLEMENTS = init
MEMBERS=const char* scaleFactor
MEMBERS=const char* scaledValue
@ -39,8 +40,9 @@ or edit "accessor.class" and rerun ./make_class.pl
*/
static int is_missing(grib_accessor*);
static int unpack_double(grib_accessor*, double* val,size_t *len);
static void init(grib_accessor*,const long, grib_arguments* );
static int pack_double(grib_accessor*, const double* val, size_t *len);
static int unpack_double(grib_accessor*, double* val, size_t *len);
static void init(grib_accessor*, const long, grib_arguments*);
static void init_class(grib_accessor_class*);
typedef struct grib_accessor_from_scale_factor_scaled_value {
@ -75,7 +77,7 @@ static grib_accessor_class _grib_accessor_class_from_scale_factor_scaled_value =
&is_missing, /* grib_pack procedures long */
0, /* grib_pack procedures long */
0, /* grib_unpack procedures long */
0, /* grib_pack procedures double */
&pack_double, /* grib_pack procedures double */
&unpack_double, /* grib_unpack procedures double */
0, /* grib_pack procedures string */
0, /* grib_unpack procedures string */
@ -114,7 +116,6 @@ static void init_class(grib_accessor_class* c)
c->pack_missing = (*(c->super))->pack_missing;
c->pack_long = (*(c->super))->pack_long;
c->unpack_long = (*(c->super))->unpack_long;
c->pack_double = (*(c->super))->pack_double;
c->pack_string = (*(c->super))->pack_string;
c->unpack_string = (*(c->super))->unpack_string;
c->pack_string_array = (*(c->super))->pack_string_array;
@ -137,16 +138,92 @@ static void init_class(grib_accessor_class* c)
/* END_CLASS_IMP */
static void init(grib_accessor* a,const long l, grib_arguments* c)
static void init(grib_accessor* a, const long l, grib_arguments* c)
{
grib_accessor_from_scale_factor_scaled_value* self = (grib_accessor_from_scale_factor_scaled_value*)a;
int n = 0;
grib_handle* hand = grib_handle_of_accessor(a);
self->scaleFactor = grib_arguments_get_name(hand,c,n++);
self->scaledValue = grib_arguments_get_name(hand,c,n++);
self->scaleFactor = grib_arguments_get_name(hand, c, n++);
self->scaledValue = grib_arguments_get_name(hand, c, n++);
a->flags |= GRIB_ACCESSOR_FLAG_READ_ONLY;
/* ECC-979: Allow user to encode */
/* a->flags |= GRIB_ACCESSOR_FLAG_READ_ONLY; */
}
static float float_epsilon(void)
{
float floatEps = 1.0;
while (1 + floatEps / 2 != 1)
floatEps /= 2;
return floatEps;
}
static int is_approximately_equal(double a, double b, double epsilon)
{
if (a == b) return 1;
if (fabs(a - b) <= epsilon)
return 1;
return 0;
}
static double eval_value_factor(long value, long factor)
{
return (double)value * pow(10.0, -factor);
}
static int pack_double(grib_accessor* a, const double* val, size_t *len)
{
/* Evaluate self->scaleFactor and self->scaledValue from input double '*val' */
grib_accessor_from_scale_factor_scaled_value* self = (grib_accessor_from_scale_factor_scaled_value*)a;
grib_handle* hand = grib_handle_of_accessor(a);
int ret = 0;
long factor=0, prev_factor=0;
long value=0, prev_value=0;
double exact = *val; /*the input*/
const float epsilon = float_epsilon();
unsigned long maxval_value, maxval_factor; /*maximum allowable values*/
grib_accessor *accessor_factor, *accessor_value;
if (exact == 0) {
if ((ret = grib_set_long_internal(hand, self->scaleFactor, 0)) != GRIB_SUCCESS) return ret;
if ((ret = grib_set_long_internal(hand, self->scaledValue, 0)) != GRIB_SUCCESS) return ret;
return GRIB_SUCCESS;
}
accessor_factor = grib_find_accessor(hand, self->scaleFactor);
accessor_value = grib_find_accessor(hand, self->scaledValue);
if (!accessor_factor || !accessor_value) {
grib_context_log(a->context, GRIB_LOG_ERROR, "Could not access keys %s and %s", self->scaleFactor, self->scaledValue);
return GRIB_ENCODING_ERROR;
}
maxval_value = (1UL << (accessor_value->length*8))-2; /* exclude missing */
maxval_factor = (1UL << (accessor_factor->length*8))-2; /* exclude missing */
Assert(exact > 0);
/* Loop until we find a close enough approximation. Keep the last good values */
factor = prev_factor = 0;
value = prev_value = round(exact);
while (!is_approximately_equal(exact, eval_value_factor(value, factor), epsilon) &&
value < maxval_value &&
factor < maxval_factor)
{
value = round(exact * pow(10., ++factor));
if (value > maxval_value || factor > maxval_factor) {
/* One or more maxima exceeded. So stop and use the previous values */
value = prev_value;
factor = prev_factor;
break;
}
prev_factor = factor;
prev_value = value;
}
if ((ret = grib_set_long_internal(hand, self->scaleFactor, factor)) != GRIB_SUCCESS) return ret;
if ((ret = grib_set_long_internal(hand, self->scaledValue, value)) != GRIB_SUCCESS) return ret;
return GRIB_SUCCESS;
}
static int unpack_double(grib_accessor* a, double* val, size_t *len)

View File

@ -56,6 +56,7 @@ list( APPEND tests_no_data_reqd
grib_2nd_order_numValues
grib_ecc-136
grib_ecc-967
grib_ecc-979
julian
bufr_dump_samples
bufr_json_samples

52
tests/grib_ecc-979.sh Executable file
View File

@ -0,0 +1,52 @@
#!/bin/sh
# Copyright 2005-2019 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
set -u
# ---------------------------------------------------------
# This is the test for the JIRA issue ECC-979.
# Directly setting keys that use the accessor
# from_scale_factor_scaled_value
# ---------------------------------------------------------
label="grib_ecc-979-test"
temp=temp.${label}
sample2=$ECCODES_SAMPLES_PATH/GRIB2.tmpl
${tools_dir}/grib_set -s shapeOfTheEarth=1,radius=6371229 $sample2 $temp
grib_check_key_equals $temp scaledValueOfRadiusOfSphericalEarth,scaleFactorOfRadiusOfSphericalEarth '6371229 0'
${tools_dir}/grib_set -s shapeOfTheEarth=1,radius=6371229.0 $sample2 $temp
grib_check_key_equals $temp scaledValueOfRadiusOfSphericalEarth,scaleFactorOfRadiusOfSphericalEarth '6371229 0'
# Oblate
${tools_dir}/grib_set -s shapeOfTheEarth=7,earthMajorAxis=6378137.0,earthMinorAxis=6356752.3142 $sample2 $temp
grib_check_key_equals $temp scaleFactorOfEarthMajorAxis,scaledValueOfEarthMajorAxis '0 6378137'
grib_check_key_equals $temp scaleFactorOfEarthMinorAxis,scaledValueOfEarthMinorAxis '2 635675231'
# Test edge case
${tools_dir}/grib_set -s shapeOfTheEarth=1,radius=0 $sample2 $temp
grib_check_key_equals $temp scaledValueOfRadiusOfSphericalEarth,scaleFactorOfRadiusOfSphericalEarth '0 0'
# Check larger factor
${tools_dir}/grib_set -s shapeOfTheEarth=1,radius=678.1234 $sample2 $temp
grib_check_key_equals $temp scaledValueOfRadiusOfSphericalEarth,scaleFactorOfRadiusOfSphericalEarth '6781234 4'
# ----------------------------------------------------------
# Also the conversion to metres should preserve the decimals
${tools_dir}/grib_set -s \
shapeOfTheEarth=3,earthMajorAxis=6378.137,earthMinorAxis=6356.75231425 \
$sample2 $temp
r=`${tools_dir}/grib_get -F%.5f -p earthMajorAxis,earthMajorAxisInMetres,earthMinorAxis,earthMinorAxisInMetres $temp`
[ "$r" = '6378.13700 6378137.00000 6356.75231 6356752.31000' ]
# Clean up
rm -f $temp