GRIB2: Order of setting keys creates a failure
@@ -294,24 +294,26 @@ static int pack_long(grib_accessor* a, const long* val, size_t *len)
size_t pressure_units_len=10;
grib_accessor_g2level* self = (grib_accessor_g2level*)a;
+ grib_handle* hand = grib_handle_of_accessor(a);
+ int change_scale_and_value = 1;
if(*len != 1)
return GRIB_WRONG_ARRAY_SIZE;
/*Not sure if this is necessary
* if (value_first == GRIB_MISSING_LONG) {
- * if ((ret=grib_set_missing_internal(grib_handle_of_accessor(a), self->scale_first)) != GRIB_SUCCESS)
+ * if ((ret=grib_set_missing_internal(hand, self->scale_first)) != GRIB_SUCCESS)
* return ret;
- * if ((ret=grib_set_missing_internal(grib_handle_of_accessor(a), self->value_first)) != GRIB_SUCCESS)
+ * if ((ret=grib_set_missing_internal(hand, self->value_first)) != GRIB_SUCCESS)
* return ret;
* return GRIB_SUCCESS;
* }
*/
- if((ret = grib_get_long_internal(grib_handle_of_accessor(a), self->type_first,&type_first))
+ if((ret = grib_get_long_internal(hand, self->type_first,&type_first))
!=GRIB_SUCCESS) return ret;
- if((ret = grib_get_string_internal(grib_handle_of_accessor(a), self->pressure_units,pressure_units,&pressure_units_len))
+ if((ret = grib_get_string_internal(hand, self->pressure_units,pressure_units,&pressure_units_len))
!=GRIB_SUCCESS) return ret;
switch(type_first)
@@ -326,12 +328,29 @@ static int pack_long(grib_accessor* a, const long* val, size_t *len)
break;
}
+ /* ECC-530:
+ * The pack_long function can get called when key "typeOfSecondFixedSurface" is
+ * changed (via the trigger rule in the definitions). That can have an undesired
+ * side-effect that it sets the scale factor and scaled value keys
+ * (e.g. scaleFactorOfFirstFixedSurface, scaledValueOfFirstFixedSurface)
+ * overwriting their previous values.
+ * In this scenario we do not want to change the scale/value.
+ * However when the user directly sets the level or when we are changing edition, then
+ * we do want to change the scale/value.
+ */
+ if (hand->loader && hand->loader->changing_edition==0) {
+ change_scale_and_value = 0;
+ }
+
+ if (change_scale_and_value) {
if (type_first>9) {
- if((ret = grib_set_long_internal(grib_handle_of_accessor(a), self->scale_first,scale_first))
+ if((ret = grib_set_long_internal(hand, self->scale_first,scale_first))
!=GRIB_SUCCESS) return ret;
- if((ret = grib_set_long_internal(grib_handle_of_accessor(a), self->value_first,value_first))
+ if((ret = grib_set_long_internal(hand, self->value_first,value_first))
!=GRIB_SUCCESS) return ret;
}
+ }
+
return GRIB_SUCCESS;
}
