2016-11-14 10:05:40 +00:00
|
|
|
/*
|
2020-01-28 14:32:34 +00:00
|
|
|
* (C) Copyright 2005- ECMWF.
|
2016-11-14 10:05:40 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2023-01-06 13:02:12 +00:00
|
|
|
#include "grib_api_internal.h"
|
2016-11-14 10:05:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Test the use of optimizeScaleFactor; on the following field, the packing error
|
2022-06-25 12:01:00 +00:00
|
|
|
* is reduced by almost a factor of 2;
|
2016-11-14 10:05:40 +00:00
|
|
|
* Test that second order packing gives the same result as simple packing with optimizeScaleFactor=1
|
|
|
|
* philippe.marguinaud@meteo.fr, 2016/02
|
|
|
|
*/
|
2023-12-09 14:32:38 +00:00
|
|
|
#include "grib_gridded_values.h" // array 'values' defined here
|
2016-11-14 10:05:40 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
#define NLON 81
|
2016-11-14 10:05:40 +00:00
|
|
|
#define NLAT 166
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
static void encode_decode(double* zval, const char* packingType, const int bitsPerValue, const int ioptimizeScaleFactor)
|
2016-11-14 10:05:40 +00:00
|
|
|
{
|
|
|
|
size_t len, slen;
|
2020-01-29 12:05:02 +00:00
|
|
|
grib_handle* h;
|
2016-11-14 10:05:40 +00:00
|
|
|
|
2023-12-09 14:32:38 +00:00
|
|
|
printf("encode_decode: packingType=%s bitsPerValue=%d opt=%d\n", packingType, bitsPerValue, ioptimizeScaleFactor);
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(((h = grib_handle_new_from_samples(NULL, "regular_ll_pl_grib2")) == NULL), 0);
|
|
|
|
GRIB_CHECK(grib_set_long(h, "Ni", NLON), 0);
|
|
|
|
GRIB_CHECK(grib_set_long(h, "Nj", NLAT), 0);
|
|
|
|
slen = strlen(packingType);
|
|
|
|
GRIB_CHECK(grib_set_string(h, "packingType", packingType, &slen), 0);
|
|
|
|
GRIB_CHECK(grib_set_long(h, "bitsPerValue", bitsPerValue), 0);
|
|
|
|
GRIB_CHECK(grib_set_long(h, "optimizeScaleFactor", ioptimizeScaleFactor), 0);
|
2016-11-14 10:05:40 +00:00
|
|
|
|
|
|
|
len = NLON * NLAT;
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(grib_set_double_array(h, "values", values, len), 0);
|
|
|
|
GRIB_CHECK(grib_get_double_array(h, "values", zval, &len), 0);
|
2016-11-14 10:05:40 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
GRIB_CHECK(grib_handle_delete(h), 0);
|
2016-11-14 10:05:40 +00:00
|
|
|
}
|
|
|
|
|
2023-12-09 14:50:06 +00:00
|
|
|
static double calc_error(const double* zval1, const double* zval2)
|
2016-11-14 10:05:40 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
double zerr = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < NLON * NLAT; i++)
|
|
|
|
zerr += (zval2[i] - zval1[i]) * (zval2[i] - zval1[i]);
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
return sqrt(zerr);
|
2016-11-14 10:05:40 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
int main(int argc, char* argv[])
|
2016-11-14 10:05:40 +00:00
|
|
|
{
|
2023-12-09 14:50:06 +00:00
|
|
|
const int bitsPerValue[11] = { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 };
|
2016-11-14 10:05:40 +00:00
|
|
|
int ipackingType, ibitsPerValue, ioptimizeScaleFactor;
|
|
|
|
|
2022-06-25 12:01:00 +00:00
|
|
|
for (ibitsPerValue = 0; ibitsPerValue < 11; ibitsPerValue++) {
|
2020-01-29 12:05:02 +00:00
|
|
|
for (ipackingType = 0; ipackingType < 2; ipackingType++) {
|
2016-11-14 10:05:40 +00:00
|
|
|
double zval_simple[NLAT * NLON];
|
|
|
|
double zval_second[NLAT * NLON];
|
|
|
|
double zerr;
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
encode_decode(zval_simple, "grid_simple", bitsPerValue[ibitsPerValue], 1);
|
|
|
|
encode_decode(zval_second, "grid_second_order", bitsPerValue[ibitsPerValue], 1);
|
2016-11-14 10:05:40 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
zerr = calc_error(zval_simple, zval_second);
|
2016-11-14 10:05:40 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
if (zerr > 0.) {
|
2023-12-09 14:32:38 +00:00
|
|
|
fprintf(stderr, "Error: Second order packing does not reproduce simple packing with optimizeScaleFactor=1\n");
|
2016-11-14 10:05:40 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2022-06-25 12:01:00 +00:00
|
|
|
}
|
2020-01-29 12:05:02 +00:00
|
|
|
printf(" %30s %30s %30s %s\n", "bitsPerValue", "error(optimizeScaleFactor=0)", "error(optimizeScaleFactor=1)", "ratio");
|
|
|
|
for (ibitsPerValue = 0; ibitsPerValue < 11; ibitsPerValue++) {
|
2016-11-14 10:05:40 +00:00
|
|
|
double zerr[2];
|
2019-12-12 12:28:45 +00:00
|
|
|
double ratio = 0;
|
2020-01-29 12:05:02 +00:00
|
|
|
for (ioptimizeScaleFactor = 0; ioptimizeScaleFactor < 2; ioptimizeScaleFactor++) {
|
2016-11-14 10:05:40 +00:00
|
|
|
double zval[NLAT * NLON];
|
2020-01-29 12:05:02 +00:00
|
|
|
encode_decode(zval, "grid_simple", bitsPerValue[ibitsPerValue], ioptimizeScaleFactor);
|
|
|
|
zerr[ioptimizeScaleFactor] = calc_error(zval, values);
|
2016-11-14 10:05:40 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
ratio = zerr[0] / zerr[1];
|
|
|
|
printf(" %30d %30.9e %30.9e %g\n", bitsPerValue[ibitsPerValue], zerr[0], zerr[1], ratio);
|
2016-11-14 10:05:40 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
if ((ratio < 1.90) || (ratio > 2.00)) {
|
2023-12-09 14:32:38 +00:00
|
|
|
fprintf(stderr, "Error: Packing error too big: optimizeScaleFactor appears to be broken (zerr[0]/zerr[1]=%g)\n", ratio);
|
2016-11-14 10:05:40 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|