diff --git a/src/grib_accessor_class_data_g1simple_packing.cc b/src/grib_accessor_class_data_g1simple_packing.cc index e9f4aefdc..82c72c64c 100644 --- a/src/grib_accessor_class_data_g1simple_packing.cc +++ b/src/grib_accessor_class_data_g1simple_packing.cc @@ -194,16 +194,22 @@ static int pack_double(grib_accessor* a, const double* cval, size_t* len) } if (units_factor != 1.0) { - if (units_bias != 0.0) - for (i = 0; i < n_vals; i++) + if (units_bias != 0.0) { + for (i = 0; i < n_vals; i++) { val[i] = val[i] * units_factor + units_bias; - else - for (i = 0; i < n_vals; i++) + } + } + else { + for (i = 0; i < n_vals; i++) { val[i] *= units_factor; + } + } } - else if (units_bias != 0.0) - for (i = 0; i < n_vals; i++) + else if (units_bias != 0.0) { + for (i = 0; i < n_vals; i++) { val[i] += units_bias; + } + } if (c->ieee_packing && self->ieee_packing) { long precision = 0; /* Either 1(=32 bits) or 2(=64 bits) */ diff --git a/src/grib_accessor_class_data_g2simple_packing.cc b/src/grib_accessor_class_data_g2simple_packing.cc index 69e1bdbc9..56441fc96 100644 --- a/src/grib_accessor_class_data_g2simple_packing.cc +++ b/src/grib_accessor_class_data_g2simple_packing.cc @@ -177,16 +177,21 @@ static int pack_double(grib_accessor* a, const double* cval, size_t* len) } if (units_factor != 1.0) { - if (units_bias != 0.0) - for (i = 0; i < n_vals; i++) + if (units_bias != 0.0) { + for (i = 0; i < n_vals; i++) { val[i] = val[i] * units_factor + units_bias; - else - for (i = 0; i < n_vals; i++) + } + } else { + for (i = 0; i < n_vals; i++) { val[i] *= units_factor; + } + } } - else if (units_bias != 0.0) - for (i = 0; i < n_vals; i++) + else if (units_bias != 0.0) { + for (i = 0; i < n_vals; i++) { val[i] += units_bias; + } + } /* IEEE packing */ if (c->ieee_packing) { diff --git a/src/grib_accessor_class_data_jpeg2000_packing.cc b/src/grib_accessor_class_data_jpeg2000_packing.cc index 66d6f6767..b00160214 100644 --- a/src/grib_accessor_class_data_jpeg2000_packing.cc +++ b/src/grib_accessor_class_data_jpeg2000_packing.cc @@ -225,7 +225,7 @@ static int unpack_double(grib_accessor* a, double* val, size_t* len) grib_accessor_data_jpeg2000_packing* self = (grib_accessor_data_jpeg2000_packing*)a; int err = GRIB_SUCCESS; - int i; + size_t i = 0; size_t buflen = grib_byte_count(a); double bscale = 0; @@ -304,16 +304,21 @@ static int unpack_double(grib_accessor* a, double* val, size_t* len) val[i] = (val[i] * bscale + reference_value) * dscale; } if (units_factor != 1.0) { - if (units_bias != 0.0) - for (i = 0; i < n_vals; i++) + if (units_bias != 0.0) { + for (i = 0; i < n_vals; i++) { val[i] = val[i] * units_factor + units_bias; - else - for (i = 0; i < n_vals; i++) + } + } else { + for (i = 0; i < n_vals; i++) { val[i] *= units_factor; + } + } } - else if (units_bias != 0.0) - for (i = 0; i < n_vals; i++) + else if (units_bias != 0.0) { + for (i = 0; i < n_vals; i++) { val[i] += units_bias; + } + } return err; } diff --git a/src/grib_accessor_class_data_simple_packing.cc b/src/grib_accessor_class_data_simple_packing.cc index 0d44acd7f..e78bda53c 100644 --- a/src/grib_accessor_class_data_simple_packing.cc +++ b/src/grib_accessor_class_data_simple_packing.cc @@ -406,16 +406,21 @@ static int unpack(grib_accessor* a, T* val, size_t* len) *len = (long)n_vals; if (units_factor != 1.0) { - if (units_bias != 0.0) - for (i = 0; i < n_vals; i++) + if (units_bias != 0.0) { + for (i = 0; i < n_vals; i++) { val[i] = val[i] * units_factor + units_bias; - else - for (i = 0; i < n_vals; i++) + } + } else { + for (i = 0; i < n_vals; i++) { val[i] *= units_factor; + } + } } - else if (units_bias != 0.0) - for (i = 0; i < n_vals; i++) + else if (units_bias != 0.0) { + for (i = 0; i < n_vals; i++) { val[i] += units_bias; + } + } return err; } diff --git a/tests/grib_units_bias_factor.sh b/tests/grib_units_bias_factor.sh index e10662d7a..4dbc9db81 100755 --- a/tests/grib_units_bias_factor.sh +++ b/tests/grib_units_bias_factor.sh @@ -13,22 +13,31 @@ label="grib_units_bias_factor_test" outfile=temp.$label.grib -infile=${data_dir}/sample.grib2 -max=`${tools_dir}/grib_get -F%.3f -p max $infile` -[ "$max" = "311.099" ] +infiles="${data_dir}/sample.grib2 ${data_dir}/regular_latlon_surface.grib1" +for infile in $infiles; do + #infile=${data_dir}/sample.grib2 + max=`${tools_dir}/grib_get -F%.3f -p max $infile` + [ "$max" = "311.099" ] -${tools_dir}/grib_set -rs unitsFactor=1.1,unitsBias=4 $infile $outfile -max=`${tools_dir}/grib_get -F%.3f -p max $outfile` -[ "$max" = "346.209" ] + ${tools_dir}/grib_set -rs unitsFactor=1.1,unitsBias=4 $infile $outfile + max=`${tools_dir}/grib_get -F%.3f -p max $outfile` + [ "$max" = "346.209" ] -${tools_dir}/grib_set -rs unitsFactor=2 $infile $outfile -max=`${tools_dir}/grib_get -F%.3f -p max $outfile` -[ "$max" = "622.197" ] + ${tools_dir}/grib_set -rs unitsFactor=2 $infile $outfile + max=`${tools_dir}/grib_get -F%.3f -p max $outfile` + [ "$max" = "622.197" ] -${tools_dir}/grib_set -rs unitsBias=4 $infile $outfile -max=`${tools_dir}/grib_get -F%.3f -p max $outfile` -[ "$max" = "315.099" ] + ${tools_dir}/grib_set -rs unitsBias=4 $infile $outfile + max=`${tools_dir}/grib_get -F%.3f -p max $outfile` + [ "$max" = "315.099" ] +done +if [ $HAVE_JPEG -eq 1 ]; then + infile=${data_dir}/jpeg.grib2 + ${tools_dir}/grib_get -p max -s unitsBias=1.5 $infile $outfile + ${tools_dir}/grib_get -p min -s unitsFactor=2 $infile $outfile + ${tools_dir}/grib_get -p avg -s unitsFactor=1.1,unitsBias=100 $infile $outfile +fi # Clean up rm -f $outfile