ECC-1813: stepUnits (without step) change low_level_keys

This commit is contained in:
Eugen Betke 2024-04-24 17:17:54 +00:00
parent 453122aee8
commit 283b69596b
3 changed files with 62 additions and 4 deletions

View File

@ -262,9 +262,13 @@ static int unpack_one_time_range_long_(grib_accessor* a, long* val, size_t* len)
}
if (add_time_range) {
*val = start_step_value + time_range_value;
if ((err = grib_set_long_internal(h, "endStepUnit", step_units)) != GRIB_SUCCESS)
return err;
}
else {
*val = start_step_value;
if ((err = grib_set_long_internal(h, "endStepUnit", step_units)) != GRIB_SUCCESS)
return err;
}
return GRIB_SUCCESS;
@ -310,9 +314,13 @@ static int unpack_one_time_range_double_(grib_accessor* a, double *val , size_t*
}
if (add_time_range) {
*val = (start_step + time_range).value<double>(eccodes::Unit(step_units));
if ((err = grib_set_long_internal(h, "endStepUnit", step_units)) != GRIB_SUCCESS)
return err;
}
else {
*val = start_step.value<double>(eccodes::Unit(start_step_unit));
if ((err = grib_set_long_internal(h, "endStepUnit", start_step_unit)) != GRIB_SUCCESS)
return err;
}
return GRIB_SUCCESS;
@ -441,14 +449,19 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len)
grib_handle* h = grib_handle_of_accessor(a);
int ret = 0;
long start_step_value;
long start_step_unit;
long numberOfTimeRange;
if ((ret = grib_get_long_internal(h, self->start_step_value, &start_step_value)))
return ret;
if ((ret = grib_get_long_internal(h, "startStepUnit", &start_step_unit)))
return ret;
/* point in time */
if (self->year == NULL) {
*val = start_step_value;
if ((ret = grib_set_long_internal(h, "endStepUnit", start_step_unit)))
return ret;
return 0;
}
@ -479,14 +492,19 @@ static int unpack_double(grib_accessor* a, double* val, size_t* len)
grib_handle* h = grib_handle_of_accessor(a);
int ret = 0;
long start_step_value;
long start_step_unit;
long numberOfTimeRange;
if ((ret = grib_get_long_internal(h, self->start_step_value, &start_step_value)))
return ret;
if ((ret = grib_get_long_internal(h, "startStepUnit", &start_step_unit)))
return ret;
/* point in time */
if (self->year == NULL) {
*val = start_step_value;
if ((ret = grib_set_long_internal(h, "endStepUnit", start_step_unit)))
return ret;
return 0;
}
@ -542,7 +560,8 @@ static int pack_long_(grib_accessor* a, const long end_step_value, const long en
/*point in time */
if (self->year == NULL) {
err = grib_set_long_internal(h, "startStepUnit", end_step.unit().value<long>());
if ((err = grib_set_long_internal(h, "startStepUnit", end_step.unit().value<long>())) != GRIB_SUCCESS)
return err;
err = grib_set_long_internal(h, self->start_step_value, end_step.value<long>());
return err;
}

View File

@ -178,6 +178,12 @@ static int pack_long(grib_accessor* a, const long* val, size_t* len)
grib_handle* h = grib_handle_of_accessor(a);
grib_accessor_optimal_step_units* self = (grib_accessor_optimal_step_units*)a;
long start_step;
long start_step_unit;
long end_step;
long end_step_unit;
int ret;
auto supported_units = eccodes::Unit::list_supported_units();
try {
eccodes::Unit unit{*val}; // throws if not supported
@ -198,11 +204,35 @@ static int pack_long(grib_accessor* a, const long* val, size_t* len)
return GRIB_INVALID_ARGUMENT;
}
int ret;
self->overwriteStepUnits = *val;
if ((ret = grib_set_long_internal(h, "forceStepUnits", *val)) != GRIB_SUCCESS) {
if ((ret = grib_set_long_internal(h, "forceStepUnits", *val)) != GRIB_SUCCESS)
return ret;
if ((ret = grib_get_long_internal(h, "startStep", &start_step)) != GRIB_SUCCESS)
return ret;
if ((ret = grib_get_long_internal(h, "startStepUnit", &start_step_unit)) != GRIB_SUCCESS)
return ret;
if ((ret = grib_get_long_internal(h, "endStep", &end_step)) != GRIB_SUCCESS)
return ret;
if ((ret = grib_get_long_internal(h, "endStepUnit", &end_step_unit)) != GRIB_SUCCESS)
return ret;
//printf("start_step: %ld, start_step_unit: %ld, end_step: %ld, end_step_unit: %ld\n", start_step, start_step_unit, end_step, end_step_unit);
eccodes::Step start{start_step, start_step_unit};
start = start.set_unit(*val);
eccodes::Step end{end_step, end_step_unit};
end = end.set_unit(*val);
if ((ret = grib_set_long_internal(h, "startStepUnit", start.unit().value<long>())) != GRIB_SUCCESS)
return ret;
if ((ret = grib_set_long_internal(h, "startStep", start.value<long>())) != GRIB_SUCCESS)
return ret;
if ((ret = grib_set_long_internal(h, "endStepUnit", end.unit().value<long>())) != GRIB_SUCCESS)
return ret;
if ((ret = grib_set_long_internal(h, "endStep", end.value<long>())) != GRIB_SUCCESS)
return ret;
}
return GRIB_SUCCESS;
}

View File

@ -62,6 +62,9 @@ fi
instantaneous_field=$data_dir/reduced_gaussian_surface.grib2
accumulated_field=$data_dir/reduced_gaussian_sub_area.grib2
# Check the lowercase alias 'stepunits' for a variety of step types (instant, accum etc)
${tools_dir}/grib_get -p stepunits $data_dir/tigge_cf_ecmwf.grib2
@ -89,6 +92,12 @@ grib_check_key_equals $temp "-p $keys_step_range" "59m 59m 59 59 m"
grib_check_key_equals $temp "-p $keys_start_step" "59m 59m 59 59 m"
grib_check_key_equals $temp "-p $keys_end_step" "59m 59m 59 59 m"
${tools_dir}/grib_set -s stepUnits=m,step=60 $fn $temp
grib_check_key_equals $temp "-p $low_level_keys" "60 m"
${tools_dir}/grib_set -s stepUnits=s $temp $temp2
grib_check_key_equals $temp2 "-p $low_level_keys" "3600 s"
${tools_dir}/grib_set -s stepUnits=h $temp $temp2
grib_check_key_equals $temp2 "-p $low_level_keys" "1 h"
#### stepUnits overrides the units in the low level keys
# if stepUnits=UNIT is set, then set the low level keys to UNIT