2015-07-21 13:24:02 +00:00
|
|
|
/*
|
2020-01-28 14:32:34 +00:00
|
|
|
* (C) Copyright 2005- ECMWF.
|
2015-07-21 13:24:02 +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.
|
|
|
|
*/
|
2016-10-10 18:14:34 +00:00
|
|
|
|
|
|
|
#include "grib_api_internal.h"
|
2015-07-21 13:24:02 +00:00
|
|
|
#include "eccodes.h"
|
|
|
|
|
2017-09-01 16:36:16 +00:00
|
|
|
static int get_packing_type_code(const char* packingType)
|
2016-10-10 18:14:34 +00:00
|
|
|
{
|
|
|
|
int result = GRIB_UTIL_PACKING_TYPE_GRID_SIMPLE;
|
2020-01-29 12:05:02 +00:00
|
|
|
if (packingType == NULL)
|
2023-02-16 18:29:32 +00:00
|
|
|
return GRIB_UTIL_PACKING_TYPE_SAME_AS_INPUT;
|
2016-10-10 18:14:34 +00:00
|
|
|
|
|
|
|
if (STR_EQUAL(packingType, "grid_jpeg"))
|
2023-02-16 19:42:37 +00:00
|
|
|
return GRIB_UTIL_PACKING_TYPE_JPEG;
|
2023-02-16 18:29:32 +00:00
|
|
|
else if (STR_EQUAL(packingType, "grid_ccsds"))
|
2023-02-16 19:42:37 +00:00
|
|
|
return GRIB_UTIL_PACKING_TYPE_CCSDS;
|
2016-10-10 18:14:34 +00:00
|
|
|
else if (STR_EQUAL(packingType, "grid_simple"))
|
2023-02-16 19:42:37 +00:00
|
|
|
return GRIB_UTIL_PACKING_TYPE_GRID_SIMPLE;
|
2016-10-10 18:14:34 +00:00
|
|
|
else if (STR_EQUAL(packingType, "grid_second_order"))
|
2023-02-16 19:42:37 +00:00
|
|
|
return GRIB_UTIL_PACKING_TYPE_GRID_SECOND_ORDER;
|
2020-07-14 13:35:04 +00:00
|
|
|
else if (STR_EQUAL(packingType, "grid_ieee"))
|
2023-02-16 19:42:37 +00:00
|
|
|
return GRIB_UTIL_PACKING_TYPE_IEEE;
|
2024-01-14 14:39:46 +00:00
|
|
|
else if (STR_EQUAL(packingType, "grid_complex"))
|
|
|
|
return GRIB_UTIL_PACKING_TYPE_GRID_COMPLEX;
|
2016-10-10 18:14:34 +00:00
|
|
|
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(!"Invalid packingType");
|
2016-10-10 18:14:34 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-09-01 16:36:16 +00:00
|
|
|
static void test_reduced_gg(int remove_local_def, int edition, const char* packingType,
|
2020-01-29 12:05:02 +00:00
|
|
|
const char* input_filename, const char* output_filename)
|
2015-09-23 14:30:30 +00:00
|
|
|
{
|
|
|
|
/* based on copy_spec_from_ksec */
|
2020-01-29 12:05:02 +00:00
|
|
|
int err = 0;
|
2015-09-23 14:30:30 +00:00
|
|
|
size_t slen = 32, inlen = 0, outlen = 0;
|
2020-01-29 12:05:02 +00:00
|
|
|
size_t i = 0, size = 0;
|
|
|
|
int set_spec_flags = 0;
|
|
|
|
double* values = NULL;
|
|
|
|
FILE* in = NULL;
|
|
|
|
FILE* out = NULL;
|
2015-09-23 14:30:30 +00:00
|
|
|
const void* buffer = NULL;
|
|
|
|
char gridType[128] = {0,};
|
|
|
|
|
2023-02-16 19:42:37 +00:00
|
|
|
grib_handle* handle = 0;
|
|
|
|
grib_handle* finalh = 0;
|
2020-01-29 12:05:02 +00:00
|
|
|
grib_util_grid_spec spec = {0,};
|
|
|
|
grib_util_packing_spec packing_spec = {0,};
|
2015-09-23 14:30:30 +00:00
|
|
|
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(input_filename);
|
2020-01-29 12:05:02 +00:00
|
|
|
in = fopen(input_filename, "rb");
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(in);
|
2020-01-29 12:05:02 +00:00
|
|
|
handle = grib_handle_new_from_file(0, in, &err);
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(handle);
|
2015-09-23 14:30:30 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
CODES_CHECK(grib_get_string(handle, "gridType", gridType, &slen), 0);
|
2015-09-23 14:30:30 +00:00
|
|
|
if (!STR_EQUAL(gridType, "reduced_gg")) {
|
|
|
|
grib_handle_delete(handle);
|
|
|
|
return;
|
|
|
|
}
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(output_filename);
|
2020-01-29 12:05:02 +00:00
|
|
|
out = fopen(output_filename, "wb");
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(out);
|
2015-09-23 14:30:30 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
CODES_CHECK(grib_get_size(handle, "values", &inlen), 0);
|
|
|
|
values = (double*)malloc(sizeof(double) * inlen);
|
|
|
|
CODES_CHECK(grib_get_double_array(handle, "values", values, &inlen), 0);
|
2023-02-16 19:42:37 +00:00
|
|
|
// make sure values are not constant
|
|
|
|
values[0] = 4.4;
|
|
|
|
values[1] = 5.5;
|
2020-01-29 12:05:02 +00:00
|
|
|
for (i = 0; i < inlen; ++i) {
|
2016-08-31 16:38:31 +00:00
|
|
|
values[i] *= 1.10;
|
|
|
|
}
|
2015-09-23 14:30:30 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
spec.grid_type = GRIB_UTIL_GRID_SPEC_REDUCED_GG;
|
|
|
|
spec.N = 32; /* hardcoded for now */
|
|
|
|
spec.Nj = 2 * spec.N;
|
|
|
|
outlen = inlen;
|
|
|
|
spec.iDirectionIncrementInDegrees = 1.5;
|
|
|
|
spec.jDirectionIncrementInDegrees = 1.5;
|
2018-02-05 16:05:31 +00:00
|
|
|
spec.latitudeOfFirstGridPointInDegrees = 87.8637991;
|
2015-09-23 14:30:30 +00:00
|
|
|
spec.longitudeOfFirstGridPointInDegrees = 0.0;
|
2016-08-31 16:38:31 +00:00
|
|
|
spec.latitudeOfLastGridPointInDegrees = -spec.latitudeOfFirstGridPointInDegrees;
|
|
|
|
spec.longitudeOfLastGridPointInDegrees = 357.187500;
|
2020-01-29 12:05:02 +00:00
|
|
|
spec.bitmapPresent = 0;
|
2015-09-23 14:30:30 +00:00
|
|
|
|
2016-10-10 18:14:34 +00:00
|
|
|
/*packing_spec.packing_type = GRIB_UTIL_PACKING_TYPE_GRID_SECOND_ORDER;*/
|
|
|
|
packing_spec.packing_type = get_packing_type_code(packingType);
|
2015-09-23 14:30:30 +00:00
|
|
|
packing_spec.bitsPerValue = 24;
|
2020-01-29 12:05:02 +00:00
|
|
|
packing_spec.accuracy = GRIB_UTIL_ACCURACY_USE_PROVIDED_BITS_PER_VALUES;
|
2023-02-16 18:29:32 +00:00
|
|
|
if (packingType)
|
|
|
|
packing_spec.packing = GRIB_UTIL_PACKING_USE_PROVIDED;
|
|
|
|
else
|
|
|
|
packing_spec.packing = GRIB_UTIL_PACKING_SAME_AS_INPUT;
|
2020-01-29 12:05:02 +00:00
|
|
|
|
2018-02-05 16:05:31 +00:00
|
|
|
/*Extra settings
|
|
|
|
packing_spec.extra_settings_count++;
|
|
|
|
packing_spec.extra_settings[0].type = GRIB_TYPE_LONG;
|
|
|
|
packing_spec.extra_settings[0].name = "expandBoundingBox";
|
|
|
|
packing_spec.extra_settings[0].long_value = 1;
|
|
|
|
*/
|
2015-09-23 14:30:30 +00:00
|
|
|
|
|
|
|
finalh = grib_util_set_spec(
|
2020-01-29 12:05:02 +00:00
|
|
|
handle,
|
|
|
|
&spec,
|
|
|
|
&packing_spec,
|
|
|
|
set_spec_flags,
|
|
|
|
values,
|
|
|
|
outlen,
|
|
|
|
&err);
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(finalh);
|
|
|
|
ECCODES_ASSERT(err == 0);
|
2015-09-23 14:30:30 +00:00
|
|
|
|
2018-06-06 14:50:47 +00:00
|
|
|
/* Try some invalid inputs and check it is handled */
|
|
|
|
{
|
2023-02-16 19:42:37 +00:00
|
|
|
grib_handle* h2 = 0;
|
2020-01-29 12:05:02 +00:00
|
|
|
packing_spec.accuracy = 999;
|
|
|
|
h2 = grib_util_set_spec(handle, &spec, &packing_spec, set_spec_flags, values, outlen, &err);
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(err == GRIB_INTERNAL_ERROR);
|
|
|
|
ECCODES_ASSERT(!h2);
|
2020-02-29 13:10:58 +00:00
|
|
|
if (h2) exit(1);
|
2018-11-15 14:25:35 +00:00
|
|
|
#ifdef INFINITY
|
2020-01-29 12:05:02 +00:00
|
|
|
packing_spec.accuracy = GRIB_UTIL_ACCURACY_USE_PROVIDED_BITS_PER_VALUES;
|
|
|
|
values[0] = INFINITY;
|
|
|
|
h2 = grib_util_set_spec(handle, &spec, &packing_spec, set_spec_flags, values, outlen, &err);
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(err == GRIB_ENCODING_ERROR);
|
|
|
|
ECCODES_ASSERT(!h2);
|
2024-03-13 16:47:58 +00:00
|
|
|
if (h2) exit(1);
|
|
|
|
|
|
|
|
values[0] = -INFINITY;
|
|
|
|
h2 = grib_util_set_spec(handle, &spec, &packing_spec, set_spec_flags, values, outlen, &err);
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(err == GRIB_ENCODING_ERROR);
|
|
|
|
ECCODES_ASSERT(!h2);
|
2020-02-29 13:10:58 +00:00
|
|
|
if (h2) exit(1);
|
2018-11-15 14:25:35 +00:00
|
|
|
#endif
|
2018-06-06 14:50:47 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 14:30:30 +00:00
|
|
|
/* Write out the message to the output file */
|
2020-01-29 12:05:02 +00:00
|
|
|
CODES_CHECK(grib_get_message(finalh, &buffer, &size), 0);
|
|
|
|
CODES_CHECK(codes_check_message_header(buffer, size, PRODUCT_GRIB), 0);
|
|
|
|
CODES_CHECK(codes_check_message_footer(buffer, size, PRODUCT_GRIB), 0);
|
|
|
|
if (fwrite(buffer, 1, size, out) != size) {
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(0);
|
2015-09-23 14:30:30 +00:00
|
|
|
}
|
|
|
|
grib_handle_delete(handle);
|
|
|
|
grib_handle_delete(finalh);
|
|
|
|
fclose(in);
|
|
|
|
fclose(out);
|
2018-09-06 13:56:41 +00:00
|
|
|
free(values);
|
2018-02-05 16:05:31 +00:00
|
|
|
/*printf("ALL OK: %s\n", __func__);*/
|
2015-09-23 14:30:30 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 16:36:16 +00:00
|
|
|
static void test_regular_ll(int remove_local_def, int edition, const char* packingType,
|
2020-01-29 12:05:02 +00:00
|
|
|
const char* input_filename, const char* output_filename)
|
2015-07-21 13:24:02 +00:00
|
|
|
{
|
|
|
|
/* based on copy_spec_from_ksec */
|
2020-01-29 12:05:02 +00:00
|
|
|
int err = 0;
|
2015-09-23 14:30:30 +00:00
|
|
|
size_t slen = 32, inlen = 0, outlen = 0;
|
2020-01-29 12:05:02 +00:00
|
|
|
size_t size = 0;
|
|
|
|
int set_spec_flags = 0;
|
|
|
|
double* values = NULL;
|
|
|
|
FILE* in = NULL;
|
|
|
|
FILE* out = NULL;
|
2015-07-21 13:24:02 +00:00
|
|
|
const void* buffer = NULL;
|
2015-09-23 14:30:30 +00:00
|
|
|
char gridType[128] = {0,};
|
2018-04-20 15:43:16 +00:00
|
|
|
long input_edition = 0;
|
2015-07-21 13:24:02 +00:00
|
|
|
|
2023-02-16 19:42:37 +00:00
|
|
|
grib_handle* handle = 0;
|
|
|
|
grib_handle* finalh = 0;
|
2020-01-29 12:05:02 +00:00
|
|
|
grib_util_grid_spec spec = {0,};
|
|
|
|
grib_util_packing_spec packing_spec = {0,};
|
2015-07-21 13:24:02 +00:00
|
|
|
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(input_filename);
|
2020-01-29 12:05:02 +00:00
|
|
|
in = fopen(input_filename, "rb");
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(in);
|
2023-02-16 19:42:37 +00:00
|
|
|
handle = grib_handle_new_from_file(0, in, &err);
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(handle);
|
2020-01-29 12:05:02 +00:00
|
|
|
|
2023-02-16 19:42:37 +00:00
|
|
|
CODES_CHECK(grib_get_long(handle, "edition", &input_edition), 0);
|
2015-09-23 14:30:30 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
CODES_CHECK(grib_get_string(handle, "gridType", gridType, &slen), 0);
|
2015-09-23 14:30:30 +00:00
|
|
|
if (!STR_EQUAL(gridType, "regular_ll")) {
|
|
|
|
grib_handle_delete(handle);
|
|
|
|
return;
|
|
|
|
}
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(output_filename);
|
2020-01-29 12:05:02 +00:00
|
|
|
out = fopen(output_filename, "wb");
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(out);
|
2015-07-21 13:24:02 +00:00
|
|
|
|
2023-02-16 19:42:37 +00:00
|
|
|
CODES_CHECK(grib_get_size(handle, "values", &inlen), 0);
|
2020-01-29 12:05:02 +00:00
|
|
|
values = (double*)malloc(sizeof(double) * inlen);
|
2023-02-16 19:42:37 +00:00
|
|
|
CODES_CHECK(grib_get_double_array(handle, "values", values, &inlen), 0);
|
|
|
|
// make sure values are not constant
|
|
|
|
values[0] = 4.4;
|
|
|
|
values[1] = 5.5;
|
2015-07-21 13:24:02 +00:00
|
|
|
|
|
|
|
spec.grid_type = GRIB_UTIL_GRID_SPEC_REGULAR_LL;
|
2020-01-29 12:05:02 +00:00
|
|
|
spec.Nj = 14;
|
|
|
|
spec.Ni = 17;
|
|
|
|
outlen = spec.Nj * spec.Ni;
|
2019-10-01 11:12:18 +00:00
|
|
|
/* outlen = inlen; */
|
2020-01-29 12:05:02 +00:00
|
|
|
spec.iDirectionIncrementInDegrees = 1.5;
|
|
|
|
spec.jDirectionIncrementInDegrees = 1.5;
|
2018-04-20 15:43:16 +00:00
|
|
|
spec.latitudeOfFirstGridPointInDegrees = 60.0001;
|
2015-07-21 13:24:02 +00:00
|
|
|
spec.longitudeOfFirstGridPointInDegrees = -9.0;
|
|
|
|
spec.latitudeOfLastGridPointInDegrees = 40.5;
|
|
|
|
spec.longitudeOfLastGridPointInDegrees = 15.0;
|
2020-01-29 12:05:02 +00:00
|
|
|
spec.bitmapPresent = 0;
|
2015-07-21 13:24:02 +00:00
|
|
|
|
2016-10-10 18:14:34 +00:00
|
|
|
/*packing_spec.packing_type = GRIB_UTIL_PACKING_TYPE_GRID_SIMPLE;*/
|
|
|
|
packing_spec.packing_type = get_packing_type_code(packingType);
|
2015-07-21 13:24:02 +00:00
|
|
|
packing_spec.bitsPerValue = 24;
|
2020-01-29 12:05:02 +00:00
|
|
|
packing_spec.accuracy = GRIB_UTIL_ACCURACY_USE_PROVIDED_BITS_PER_VALUES;
|
2023-02-16 18:29:32 +00:00
|
|
|
if (packingType)
|
|
|
|
packing_spec.packing = GRIB_UTIL_PACKING_USE_PROVIDED;
|
|
|
|
else
|
|
|
|
packing_spec.packing = GRIB_UTIL_PACKING_SAME_AS_INPUT;
|
2016-10-10 18:14:34 +00:00
|
|
|
|
2018-04-20 15:43:16 +00:00
|
|
|
packing_spec.extra_settings_count++;
|
2020-01-29 12:05:02 +00:00
|
|
|
packing_spec.extra_settings[0].type = GRIB_TYPE_LONG;
|
|
|
|
packing_spec.extra_settings[0].name = "expandBoundingBox";
|
2018-04-20 15:43:16 +00:00
|
|
|
packing_spec.extra_settings[0].long_value = 1;
|
|
|
|
|
2016-10-10 18:14:34 +00:00
|
|
|
if (edition != 0) {
|
|
|
|
packing_spec.editionNumber = edition;
|
|
|
|
}
|
|
|
|
if (remove_local_def) {
|
|
|
|
packing_spec.deleteLocalDefinition = 1;
|
|
|
|
}
|
2015-07-21 13:24:02 +00:00
|
|
|
|
2023-02-16 19:42:37 +00:00
|
|
|
finalh = grib_util_set_spec(
|
2020-01-29 12:05:02 +00:00
|
|
|
handle,
|
|
|
|
&spec,
|
|
|
|
&packing_spec,
|
|
|
|
set_spec_flags,
|
|
|
|
values,
|
|
|
|
outlen,
|
|
|
|
&err);
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(finalh);
|
|
|
|
ECCODES_ASSERT(err == 0);
|
2015-07-21 13:24:02 +00:00
|
|
|
|
2018-04-20 15:43:16 +00:00
|
|
|
/* Check expand_bounding_box worked.
|
|
|
|
* Specified latitudeOfFirstGridPointInDegrees cannot be encoded in GRIB1
|
|
|
|
* so it is rounded up to nearest millidegree */
|
|
|
|
if (input_edition == 1) {
|
|
|
|
const double expected_lat1 = 60.001;
|
2020-01-29 12:05:02 +00:00
|
|
|
double lat1 = 0;
|
2023-02-16 19:42:37 +00:00
|
|
|
CODES_CHECK(grib_get_double(finalh, "latitudeOfFirstGridPointInDegrees", &lat1), 0);
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(fabs(lat1 - expected_lat1) < 1e-10);
|
2018-04-20 15:43:16 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 13:24:02 +00:00
|
|
|
/* Write out the message to the output file */
|
2023-02-16 19:42:37 +00:00
|
|
|
CODES_CHECK(grib_get_message(finalh, &buffer, &size), 0);
|
2020-01-29 12:05:02 +00:00
|
|
|
CODES_CHECK(codes_check_message_header(buffer, size, PRODUCT_GRIB), 0);
|
|
|
|
CODES_CHECK(codes_check_message_footer(buffer, size, PRODUCT_GRIB), 0);
|
|
|
|
if (fwrite(buffer, 1, size, out) != size) {
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(0);
|
2015-07-21 13:24:02 +00:00
|
|
|
}
|
2023-02-16 19:42:37 +00:00
|
|
|
grib_handle_delete(handle);
|
|
|
|
grib_handle_delete(finalh);
|
2020-02-10 10:58:53 +00:00
|
|
|
free(values);
|
2015-09-23 14:30:30 +00:00
|
|
|
fclose(in);
|
|
|
|
fclose(out);
|
2015-07-21 13:24:02 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 16:36:16 +00:00
|
|
|
#if 0
|
|
|
|
static void test_grid_complex_spatial_differencing(int remove_local_def, int edition, const char* packingType,
|
2017-08-02 14:18:38 +00:00
|
|
|
const char* input_filename, const char* output_filename)
|
|
|
|
{
|
|
|
|
/* based on copy_spec_from_ksec */
|
|
|
|
int err = 0;
|
|
|
|
size_t slen = 34, inlen = 0, outlen = 0;
|
|
|
|
size_t size=0;
|
|
|
|
int set_spec_flags=0;
|
|
|
|
double* values = NULL;
|
|
|
|
FILE* in = NULL;
|
|
|
|
FILE* out = NULL;
|
|
|
|
const void* buffer = NULL;
|
|
|
|
char gridType[128] = {0,};
|
|
|
|
double theMax,theMin,theAverage;
|
|
|
|
|
2023-02-16 19:42:37 +00:00
|
|
|
grib_handle *handle = 0;
|
|
|
|
grib_handle *finalh = 0;
|
2017-08-02 14:18:38 +00:00
|
|
|
grib_util_grid_spec spec={0,};
|
|
|
|
grib_util_packing_spec packing_spec={0,};
|
|
|
|
|
2024-12-20 12:58:07 +00:00
|
|
|
in = fopen(input_filename,"rb"); ECCODES_ASSERT(in);
|
|
|
|
handle = grib_handle_new_from_file(0, in, &err); ECCODES_ASSERT(handle);
|
2017-08-02 14:18:38 +00:00
|
|
|
|
|
|
|
CODES_CHECK(grib_get_string(handle, "packingType", gridType, &slen),0);
|
|
|
|
if (!STR_EQUAL(gridType, "grid_complex_spatial_differencing")) {
|
|
|
|
grib_handle_delete(handle);
|
|
|
|
return;
|
|
|
|
}
|
2024-12-20 12:58:07 +00:00
|
|
|
out = fopen(output_filename,"wb"); ECCODES_ASSERT(out);
|
2017-08-02 14:18:38 +00:00
|
|
|
|
2023-02-16 19:42:37 +00:00
|
|
|
CODES_CHECK(grib_get_size(handle,"values",&inlen), 0);
|
2017-08-02 14:18:38 +00:00
|
|
|
values = (double*)malloc(sizeof(double)*inlen);
|
2023-02-16 19:42:37 +00:00
|
|
|
CODES_CHECK(grib_get_double_array(handle, "values", values,&inlen), 0);
|
2022-12-26 14:37:01 +00:00
|
|
|
|
2023-02-16 19:42:37 +00:00
|
|
|
CODES_CHECK(grib_get_double(handle, "max", &theMax),0);
|
|
|
|
CODES_CHECK(grib_get_double(handle, "min", &theMin),0);
|
|
|
|
CODES_CHECK(grib_get_double(handle, "average",&theAverage),0);
|
2017-09-01 16:36:16 +00:00
|
|
|
printf("inlen=%lu \t max=%g \t min=%g \t avg=%g\n", inlen, theMax, theMin, theAverage);
|
2017-08-02 14:18:38 +00:00
|
|
|
|
|
|
|
spec.grid_type = GRIB_UTIL_GRID_SPEC_REGULAR_LL;
|
|
|
|
spec.Nj = 721;
|
|
|
|
spec.Ni = 1440;
|
|
|
|
outlen = spec.Nj * spec.Ni;
|
|
|
|
spec.iDirectionIncrementInDegrees = 0.25;
|
|
|
|
spec.jDirectionIncrementInDegrees = 0.25;
|
|
|
|
spec.latitudeOfFirstGridPointInDegrees = 90.0;
|
|
|
|
spec.longitudeOfFirstGridPointInDegrees = 0.0;
|
|
|
|
spec.latitudeOfLastGridPointInDegrees = -90.0;
|
|
|
|
spec.longitudeOfLastGridPointInDegrees = 359.75;
|
|
|
|
spec.bitmapPresent = 1; /* there are missing values inside the data section! */
|
|
|
|
spec.missingValue = 9999;
|
|
|
|
|
2017-08-03 13:14:17 +00:00
|
|
|
packing_spec.packing_type = GRIB_UTIL_PACKING_TYPE_GRID_SIMPLE; /*Convert to Grid Simple Packing*/
|
2017-08-02 14:18:38 +00:00
|
|
|
packing_spec.bitsPerValue = 11;
|
|
|
|
packing_spec.accuracy=GRIB_UTIL_ACCURACY_USE_PROVIDED_BITS_PER_VALUES;
|
2017-08-03 13:14:17 +00:00
|
|
|
/*packing_spec.packing=GRIB_UTIL_PACKING_USE_PROVIDED;*/
|
2017-08-02 14:18:38 +00:00
|
|
|
|
|
|
|
if (edition != 0) {
|
|
|
|
packing_spec.editionNumber = edition;
|
|
|
|
}
|
|
|
|
if (remove_local_def) {
|
|
|
|
packing_spec.deleteLocalDefinition = 1;
|
|
|
|
}
|
|
|
|
|
2023-02-16 19:42:37 +00:00
|
|
|
finalh = grib_util_set_spec(
|
2017-08-02 14:18:38 +00:00
|
|
|
handle,
|
|
|
|
&spec,
|
|
|
|
&packing_spec,
|
|
|
|
set_spec_flags,
|
|
|
|
values,
|
|
|
|
outlen,
|
|
|
|
&err);
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(finalh);
|
|
|
|
ECCODES_ASSERT(err == 0);
|
2017-08-02 14:18:38 +00:00
|
|
|
|
|
|
|
/* Write out the message to the output file */
|
2023-02-16 19:42:37 +00:00
|
|
|
CODES_CHECK(grib_get_message(finalh, &buffer, &size),0);
|
2017-08-02 14:18:38 +00:00
|
|
|
if(fwrite(buffer,1,size,out) != size) {
|
2024-12-20 12:58:07 +00:00
|
|
|
ECCODES_ASSERT(0);
|
2017-08-02 14:18:38 +00:00
|
|
|
}
|
2023-02-16 19:42:37 +00:00
|
|
|
grib_handle_delete(handle);
|
|
|
|
grib_handle_delete(finalh);
|
2017-08-02 14:18:38 +00:00
|
|
|
fclose(in);
|
|
|
|
fclose(out);
|
|
|
|
}
|
2017-09-01 16:36:16 +00:00
|
|
|
#endif
|
2017-08-02 14:18:38 +00:00
|
|
|
|
2024-08-25 15:18:10 +00:00
|
|
|
// static void usage(const char* prog)
|
|
|
|
// {
|
|
|
|
// fprintf(stderr, "%s: [-p packingType] [-r] [-e edition] in.grib out.grib\n", prog);
|
|
|
|
// fprintf(stderr, "-p packingType: one of grid_jpeg, grid_ccsds, grid_second_order or grid_simple\n");
|
|
|
|
// fprintf(stderr, "-r remove local definition\n");
|
|
|
|
// fprintf(stderr, "-e edition: 1 or 2\n");
|
|
|
|
// exit(1);
|
|
|
|
// }
|
2016-10-10 18:14:34 +00:00
|
|
|
|
2020-01-29 12:05:02 +00:00
|
|
|
int main(int argc, char* argv[])
|
2015-07-21 13:24:02 +00:00
|
|
|
{
|
2018-12-10 17:23:06 +00:00
|
|
|
int i = 0, remove_local_def = 0;
|
2020-01-29 12:05:02 +00:00
|
|
|
int edition = 0;
|
|
|
|
char* packingType = NULL;
|
|
|
|
char* infile_name = NULL;
|
2016-10-10 18:14:34 +00:00
|
|
|
char* outfile_name = NULL;
|
2020-01-29 12:05:02 +00:00
|
|
|
|
2024-08-25 15:18:10 +00:00
|
|
|
if (argc == 1 || argc > 8) return 1;// see usage
|
2018-12-10 17:23:06 +00:00
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
2020-01-29 12:05:02 +00:00
|
|
|
if (strcmp(argv[i], "-p") == 0) {
|
|
|
|
packingType = argv[i + 1];
|
2018-12-10 17:23:06 +00:00
|
|
|
++i;
|
2020-01-29 12:05:02 +00:00
|
|
|
}
|
|
|
|
else if (strcmp(argv[i], "-e") == 0) {
|
|
|
|
edition = atoi(argv[i + 1]);
|
2018-12-10 17:23:06 +00:00
|
|
|
++i;
|
2020-01-29 12:05:02 +00:00
|
|
|
}
|
|
|
|
else if (strcmp(argv[i], "-r") == 0) {
|
2018-12-10 17:23:06 +00:00
|
|
|
remove_local_def = 1;
|
2020-01-29 12:05:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-12-10 17:23:06 +00:00
|
|
|
/* Expect 2 filenames */
|
2020-01-29 12:05:02 +00:00
|
|
|
infile_name = argv[i];
|
|
|
|
outfile_name = argv[i + 1];
|
2018-12-10 17:23:06 +00:00
|
|
|
break;
|
2016-10-10 18:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-10 17:23:06 +00:00
|
|
|
#if 0
|
|
|
|
printf("DEBUG remove_local_def = %d\n", remove_local_def);
|
|
|
|
printf("DEBUG edition = %d\n", edition);
|
|
|
|
printf("DEBUG packingType = %s\n", packingType);
|
|
|
|
printf("DEBUG infile_name = %s\n", infile_name);
|
|
|
|
printf("DEBUG outfile_name = %s\n", outfile_name);
|
|
|
|
#endif
|
2016-10-10 18:14:34 +00:00
|
|
|
test_regular_ll(remove_local_def, edition, packingType, infile_name, outfile_name);
|
|
|
|
test_reduced_gg(remove_local_def, edition, packingType, infile_name, outfile_name);
|
2017-08-03 13:14:17 +00:00
|
|
|
/*test_grid_complex_spatial_differencing(remove_local_def, edition, packingType, infile_name, outfile_name);*/
|
2015-07-21 13:24:02 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|