From bfd97b3e8c57659cb6cb7c8f62fcae28e75e0d88 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Sun, 24 Oct 2021 16:44:53 +0100 Subject: [PATCH 01/10] Fix cppcheck warnings --- src/grib_accessor_class_ascii.c | 2 +- ...data_g1second_order_general_extended_packing.c | 4 ++-- src/grib_accessor_class_data_raw_packing.c | 13 +++++++------ src/grib_accessor_class_data_simple_packing.c | 2 +- src/grib_accessor_class_group.c | 2 +- src/grib_accessor_class_non_alpha.c | 2 +- src/grib_accessor_class_padtoeven.c | 2 +- src/grib_bits.c | 9 ++++----- src/grib_bits_fast_big_endian_omp.c | 4 ++-- src/grib_dependency.c | 15 ++++++++++----- src/grib_dumper_class_bufr_encode_filter.c | 2 ++ src/grib_dumper_class_bufr_encode_fortran.c | 8 +++----- src/grib_dumper_class_bufr_encode_python.c | 8 +++----- src/grib_fieldset.c | 8 ++++---- src/grib_ieeefloat.c | 2 ++ 15 files changed, 44 insertions(+), 39 deletions(-) diff --git a/src/grib_accessor_class_ascii.c b/src/grib_accessor_class_ascii.c index f15a3e524..7680cff74 100644 --- a/src/grib_accessor_class_ascii.c +++ b/src/grib_accessor_class_ascii.c @@ -225,7 +225,7 @@ static int unpack_long(grib_accessor* a, long* v, size_t* len) return err; i = 0; - while (val[i] == ' ' && val[i] != 0 && i < l - 1) + while (i < l - 1 && val[i] == ' ') i++; if (val[i] == 0) { diff --git a/src/grib_accessor_class_data_g1second_order_general_extended_packing.c b/src/grib_accessor_class_data_g1second_order_general_extended_packing.c index 15afcf7a7..7293d26b3 100644 --- a/src/grib_accessor_class_data_g1second_order_general_extended_packing.c +++ b/src/grib_accessor_class_data_g1second_order_general_extended_packing.c @@ -1838,8 +1838,8 @@ static int pack_double(grib_accessor* a, const double* val, size_t* len) size = (lengthOfSecondOrderValues + 7) / 8; sizebits = lengthOfSecondOrderValues; - /* padding section 4 to an even number of octets*/ - size = (size + offsetBeforeData - offsetSection4) % 2 ? size + 1 : size; + /* padding section 4 to an even number of octets */ + size = ((size + offsetBeforeData - offsetSection4) % 2) ? size + 1 : size; half_byte = 8 * size - sizebits; if ((ret = grib_set_long_internal(handle, self->half_byte, half_byte)) != GRIB_SUCCESS) return ret; diff --git a/src/grib_accessor_class_data_raw_packing.c b/src/grib_accessor_class_data_raw_packing.c index 147f0a3a3..28da01f9b 100644 --- a/src/grib_accessor_class_data_raw_packing.c +++ b/src/grib_accessor_class_data_raw_packing.c @@ -219,8 +219,8 @@ static int pack_double(grib_accessor* a, const double* val, size_t* len) double* values = (double*)val; size_t inlen = *len; - int free_buffer = 0; - int free_values = 0; + /*int free_buffer = 0; + *int free_values = 0;*/ int code = GRIB_SUCCESS; @@ -261,10 +261,11 @@ static int pack_double(grib_accessor* a, const double* val, size_t* len) code = grib_ieee_encode_array(a->context, values, inlen, bytes, buffer); clean_up: - if (free_buffer) - free(buffer); - if (free_values) - free(values); + /*if (free_buffer) + * free(buffer); + * if (free_values) + * free(values); + */ grib_buffer_replace(a, buffer, bufsize, 1, 1); diff --git a/src/grib_accessor_class_data_simple_packing.c b/src/grib_accessor_class_data_simple_packing.c index e49162ab6..3a067de28 100644 --- a/src/grib_accessor_class_data_simple_packing.c +++ b/src/grib_accessor_class_data_simple_packing.c @@ -532,7 +532,7 @@ static int pack_double(grib_accessor* a, const double* val, size_t* len) grib_accessor_data_simple_packing* self = (grib_accessor_data_simple_packing*)a; grib_handle* gh = grib_handle_of_accessor(a); - size_t i; + size_t i = 0; size_t n_vals = *len; int err = 0; int last; diff --git a/src/grib_accessor_class_group.c b/src/grib_accessor_class_group.c index 38cc9c67f..7d99ed408 100644 --- a/src/grib_accessor_class_group.c +++ b/src/grib_accessor_class_group.c @@ -248,7 +248,7 @@ static int unpack_long(grib_accessor* a, long* v, size_t* len) return err; i = 0; - while (val[i] == ' ' && val[i] != 0 && i < l - 1) + while (i < l - 1 && val[i] == ' ') i++; if (val[i] == 0) { diff --git a/src/grib_accessor_class_non_alpha.c b/src/grib_accessor_class_non_alpha.c index 9c5b981e4..9fbf0141b 100644 --- a/src/grib_accessor_class_non_alpha.c +++ b/src/grib_accessor_class_non_alpha.c @@ -223,7 +223,7 @@ static int unpack_long(grib_accessor* a, long* v, size_t* len) return err; i = 0; - while (i < l - 1 && val[i] == ' ' && val[i] != 0) + while (i < l - 1 && val[i] == ' ') i++; if (val[i] == 0) { diff --git a/src/grib_accessor_class_padtoeven.c b/src/grib_accessor_class_padtoeven.c index 13e9dc857..4daaab023 100644 --- a/src/grib_accessor_class_padtoeven.c +++ b/src/grib_accessor_class_padtoeven.c @@ -160,7 +160,7 @@ static size_t preferred_size(grib_accessor* a, int from_handle) /* printf("EVEN %ld %ld\n",(long) a->offset,(long) offset);*/ seclen = a->offset - offset; - return seclen % 2 ? 1 : 0; + return (seclen % 2) ? 1 : 0; } static void init(grib_accessor* a, const long len, grib_arguments* args) diff --git a/src/grib_bits.c b/src/grib_bits.c index c50c5fbf5..b99f8e033 100644 --- a/src/grib_bits.c +++ b/src/grib_bits.c @@ -176,14 +176,13 @@ int grib_encode_signed_longb(unsigned char* p, long val, long* bitp, long nb) Assert(nb <= max_nbits); - if (sign) + if (sign) { val = -val; - - if (sign) grib_set_bit_on(p, bitp); - else + } + else { grib_set_bit_off(p, bitp); - + } return grib_encode_unsigned_longb(p, val, bitp, nb - 1); } diff --git a/src/grib_bits_fast_big_endian_omp.c b/src/grib_bits_fast_big_endian_omp.c index 6bdb787d9..ec229e5a0 100644 --- a/src/grib_bits_fast_big_endian_omp.c +++ b/src/grib_bits_fast_big_endian_omp.c @@ -17,7 +17,7 @@ int grib_decode_long_array(const unsigned char* p, long* bitp, long nbits, size_ { long i = 0; long countOfLeftmostBits = 0, leftmostBits = 0; - long startBit, startByte; + long startBit; long remainingBits = nbits; long* pp = (long*)p; int inited = 0; @@ -84,7 +84,7 @@ int grib_decode_double_array(const unsigned char* p, long* bitp, long nbits, dou { long i = 0; long countOfLeftmostBits = 0, leftmostBits = 0; - long startBit, startByte; + long startBit; long remainingBits = nbits; long* pp = (long*)p; int inited = 0; diff --git a/src/grib_dependency.c b/src/grib_dependency.c index 932c65055..5f25460d6 100644 --- a/src/grib_dependency.c +++ b/src/grib_dependency.c @@ -44,9 +44,9 @@ static grib_handle* handle_of(grib_accessor* observed) void grib_dependency_add(grib_accessor* observer, grib_accessor* observed) { - grib_handle* h = handle_of(observed); - grib_dependency* d = h->dependencies; - grib_dependency* last = 0; + grib_handle* h = NULL; + grib_dependency* d = NULL; + grib_dependency* last = NULL; /*printf("grib_dependency_add: observe %p %p observed=%s observer=%s\n", (void*)observed, (void*)observer, @@ -56,6 +56,8 @@ void grib_dependency_add(grib_accessor* observer, grib_accessor* observed) if (!observer || !observed) { return; } + h = handle_of(observed); + d = h->dependencies; /* Assert(h == handle_of(observer)); */ @@ -164,12 +166,15 @@ int _grib_dependency_notify_change(grib_handle* h, grib_accessor* observed) void grib_dependency_remove_observer(grib_accessor* observer) { - grib_handle* h = handle_of(observer); - grib_dependency* d = h->dependencies; + grib_handle* h = NULL; + grib_dependency* d = NULL; if (!observer) return; + h = handle_of(observer); + d = h->dependencies; + while (d) { if (d->observer == observer) { d->observer = 0; diff --git a/src/grib_dumper_class_bufr_encode_filter.c b/src/grib_dumper_class_bufr_encode_filter.c index 4e3e4aa20..f68e9f397 100644 --- a/src/grib_dumper_class_bufr_encode_filter.c +++ b/src/grib_dumper_class_bufr_encode_filter.c @@ -110,6 +110,7 @@ GRIB_INLINE static int grib_inline_strcmp(const char* a, const char* b) return (*a == 0 && *b == 0) ? 0 : 1; } +/* typedef struct string_count string_count; struct string_count { @@ -117,6 +118,7 @@ struct string_count int count; string_count* next; }; +*/ static int depth = 0; diff --git a/src/grib_dumper_class_bufr_encode_fortran.c b/src/grib_dumper_class_bufr_encode_fortran.c index 1f17ead15..0f99793dc 100644 --- a/src/grib_dumper_class_bufr_encode_fortran.c +++ b/src/grib_dumper_class_bufr_encode_fortran.c @@ -704,15 +704,13 @@ static void dump_string_array(grib_dumper* d, grib_accessor* a, const char* comm fprintf(self->dumper.out, " \"%s\" /)\n", values[size - 1]); if (self->isLeaf == 0) { + char* prefix; + int dofree = 0; + if ((r = compute_bufr_key_rank(h, self->keys, a->name)) != 0) fprintf(self->dumper.out, " call codes_set_string_array(ibufr,'#%d#%s',svalues)\n", r, a->name); else fprintf(self->dumper.out, " call codes_set_string_array(ibufr,'%s',svalues)\n", a->name); - } - - if (self->isLeaf == 0) { - char* prefix; - int dofree = 0; if (r != 0) { prefix = (char*)grib_context_malloc_clear(c, sizeof(char) * (strlen(a->name) + 10)); diff --git a/src/grib_dumper_class_bufr_encode_python.c b/src/grib_dumper_class_bufr_encode_python.c index 9abf28a85..301e1eb70 100644 --- a/src/grib_dumper_class_bufr_encode_python.c +++ b/src/grib_dumper_class_bufr_encode_python.c @@ -648,15 +648,13 @@ static void dump_string_array(grib_dumper* d, grib_accessor* a, const char* comm fprintf(self->dumper.out, " \"%s\", )\n", values[i]); if (self->isLeaf == 0) { + char* prefix; + int dofree = 0; + if ((r = compute_bufr_key_rank(h, self->keys, a->name)) != 0) fprintf(self->dumper.out, " codes_set_array(ibufr, '#%d#%s', svalues)\n", r, a->name); else fprintf(self->dumper.out, " codes_set_array(ibufr, '%s', svalues)\n", a->name); - } - - if (self->isLeaf == 0) { - char* prefix; - int dofree = 0; if (r != 0) { prefix = (char*)grib_context_malloc_clear(c, sizeof(char) * (strlen(a->name) + 10)); diff --git a/src/grib_fieldset.c b/src/grib_fieldset.c index 91e85929d..465b0b7fb 100644 --- a/src/grib_fieldset.c +++ b/src/grib_fieldset.c @@ -593,7 +593,7 @@ static grib_order_by* grib_fieldset_new_order_by(grib_context* c, const char* ob p++; mode = mode_default; if (p != t2) { - while (*p == ' ' && *p != '\0') + while (*p == ' ') p++; if (*p != '\0') { *(p - 1) = '\0'; @@ -683,12 +683,12 @@ int grib_fieldset_add(grib_fieldset* set, char* filename) return ret; } offset = 0; - ret = grib_get_double(h, "offset", &offset); + grib_get_double(h, "offset", &offset); set->fields[set->size] = (grib_field*)grib_context_malloc_clear(c, sizeof(grib_field)); set->fields[set->size]->file = file; file->refcount++; set->fields[set->size]->offset = (off_t)offset; - ret = grib_get_long(h, "totalLength", &length); + grib_get_long(h, "totalLength", &length); set->fields[set->size]->length = length; set->filter->el[set->size] = set->size; set->order->el[set->size] = set->size; @@ -897,7 +897,7 @@ static void grib_fieldset_delete_fields(grib_fieldset* set) static void grib_trim(char** x) { char* p = 0; - while (**x == ' ' && **x != '\0') + while (**x == ' ') (*x)++; if (**x == '\0') return; diff --git a/src/grib_ieeefloat.c b/src/grib_ieeefloat.c index 3c393110b..d7b713a24 100644 --- a/src/grib_ieeefloat.c +++ b/src/grib_ieeefloat.c @@ -522,8 +522,10 @@ int grib_ieee_encode_array(grib_context* c, double* val, size_t nvals, int bytes unsigned char* buf) { int err = 0, i = 0, j = 0; +#if IEEE_LE unsigned char s4[4]; unsigned char s8[8]; +#endif float fval = 0; double* pval = val; From 98078a2eb650fad67ec929aaa784bb673f794066 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Mon, 25 Oct 2021 14:07:49 +0100 Subject: [PATCH 02/10] Fix cppcheck warnings --- src/encode_double_array.c | 2 +- src/grib_accessor.c | 2 +- src/grib_accessor_class_apply_operators.c | 2 +- src/grib_accessor_class_g1end_of_interval_monthly.c | 2 +- src/grib_api_prototypes.h | 4 ++-- src/grib_bits_any_endian.c | 2 +- src/grib_db.c | 4 ++-- src/grib_dumper_class_bufr_decode_filter.c | 4 ++-- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/encode_double_array.c b/src/encode_double_array.c index 4854e544c..b822ce5e5 100644 --- a/src/encode_double_array.c +++ b/src/encode_double_array.c @@ -21,7 +21,7 @@ static void encode_double_array_common(int numBits, long packStart, long datasiz unsigned int ival; int cbits, jbits; unsigned int c; - static unsigned int mask[] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 }; + static const unsigned int mask[] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 }; /* code from gribw routine flist2bitstream */ diff --git a/src/grib_accessor.c b/src/grib_accessor.c index eb841cb20..e0541b766 100644 --- a/src/grib_accessor.c +++ b/src/grib_accessor.c @@ -789,7 +789,7 @@ grib_accessors_list* grib_accessors_list_last(grib_accessors_list* al) return al->last; } -grib_accessors_list* grib_accessors_list_find(grib_accessors_list* al, grib_accessor* a) +grib_accessors_list* grib_accessors_list_find(grib_accessors_list* al, const grib_accessor* a) { grib_accessors_list* last = al; grib_accessors_list* next = al->next; diff --git a/src/grib_accessor_class_apply_operators.c b/src/grib_accessor_class_apply_operators.c index 9a1685e1c..eb1fa5e4c 100644 --- a/src/grib_accessor_class_apply_operators.c +++ b/src/grib_accessor_class_apply_operators.c @@ -259,7 +259,7 @@ static int get_native_type(grib_accessor* a) return type; } -size_t compute_size_AO(long* descriptors, size_t numberOfDescriptors) +size_t compute_size_AO(const long* descriptors, size_t numberOfDescriptors) { int i = 0; size_t sizeAO = numberOfDescriptors; diff --git a/src/grib_accessor_class_g1end_of_interval_monthly.c b/src/grib_accessor_class_g1end_of_interval_monthly.c index e4aadbc44..34aae24ce 100644 --- a/src/grib_accessor_class_g1end_of_interval_monthly.c +++ b/src/grib_accessor_class_g1end_of_interval_monthly.c @@ -161,7 +161,7 @@ static int unpack_double(grib_accessor* a, double* val, size_t* len) char verifyingMonth[7] = {0,}; size_t slen = 7; long year = 0, month = 0, date = 0; - long mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + const long mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; long days = 0; if (!a->dirty) diff --git a/src/grib_api_prototypes.h b/src/grib_api_prototypes.h index 66ec2432e..fe6d9ec71 100644 --- a/src/grib_api_prototypes.h +++ b/src/grib_api_prototypes.h @@ -156,7 +156,7 @@ grib_accessor* grib_accessor_get_attribute(grib_accessor* a, const char* name); grib_accessors_list* grib_accessors_list_create(grib_context* c); void grib_accessors_list_push(grib_accessors_list* al, grib_accessor* a, int rank); grib_accessors_list* grib_accessors_list_last(grib_accessors_list* al); -grib_accessors_list* grib_accessors_list_find(grib_accessors_list* al, grib_accessor* a); +grib_accessors_list* grib_accessors_list_find(grib_accessors_list* al, const grib_accessor* a); void grib_accessors_list_delete(grib_context* c, grib_accessors_list* al); /* grib_concept.c */ @@ -314,7 +314,7 @@ bufr_descriptor* accessor_bufr_elements_table_get_descriptor(grib_accessor* a, i /* grib_accessor_class_bufr_has_delayed_replication.c */ /* grib_accessor_class_apply_operators.c */ -size_t compute_size_AO(long* descriptors, size_t numberOfDescriptors); +size_t compute_size_AO(const long* descriptors, size_t numberOfDescriptors); /* grib_accessor_class_non_alpha.c */ diff --git a/src/grib_bits_any_endian.c b/src/grib_bits_any_endian.c index c614d0a80..364a2842e 100644 --- a/src/grib_bits_any_endian.c +++ b/src/grib_bits_any_endian.c @@ -135,7 +135,7 @@ char* grib_decode_string(const unsigned char* bitStream, long* bitOffset, size_t unsigned char c; unsigned char* p; char* s = string; - unsigned char mask[] = { 0, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE }; + const unsigned char mask[] = { 0, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE }; int remainderComplement = 8 - remainder; if (numberOfCharacters == 0) diff --git a/src/grib_db.c b/src/grib_db.c index 91b2fa82c..d6f4924a7 100644 --- a/src/grib_db.c +++ b/src/grib_db.c @@ -638,7 +638,7 @@ static grib_order_by* grib_db_new_order_by(grib_context* c, char* obstr) p++; mode = mode_default; if (p != t2) { - while (*p == ' ' && *p != '\0') + while (*p == ' ') p++; if (*p != '\0') { *(p - 1) = '\0'; @@ -906,7 +906,7 @@ static void grib_db_delete_fields(grib_fieldset* set) static void grib_trim(char** x) { char* p = 0; - while (**x == ' ' && **x != '\0') + while (**x == ' ') (*x)++; if (**x == '\0') return; diff --git a/src/grib_dumper_class_bufr_decode_filter.c b/src/grib_dumper_class_bufr_decode_filter.c index 1434efc23..b07dbaae4 100644 --- a/src/grib_dumper_class_bufr_decode_filter.c +++ b/src/grib_dumper_class_bufr_decode_filter.c @@ -107,7 +107,7 @@ GRIB_INLINE static int grib_inline_strcmp(const char* a, const char* b) } return (*a == 0 && *b == 0) ? 0 : 1; } - +/* typedef struct string_count string_count; struct string_count { @@ -115,7 +115,7 @@ struct string_count int count; string_count* next; }; - +*/ static int depth = 0; static void init_class(grib_dumper_class* c) {} From f575a0468c2b43f0cab66c11f2f04491ae72025b Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Sat, 6 Nov 2021 12:35:12 +0000 Subject: [PATCH 03/10] GRIB: nearest function on oblate spheroid --- src/grib_api_prototypes.h | 1 + src/grib_nearest.c | 50 +++++++++++++++++-------- src/grib_nearest_class_latlon_reduced.c | 13 ++----- src/grib_nearest_class_reduced.c | 13 ++----- src/grib_nearest_class_regular.c | 32 +++------------- tests/grib_nearest_test.sh | 25 +++++++++++++ 6 files changed, 72 insertions(+), 62 deletions(-) diff --git a/src/grib_api_prototypes.h b/src/grib_api_prototypes.h index a10dabe40..4e4cfa376 100644 --- a/src/grib_api_prototypes.h +++ b/src/grib_api_prototypes.h @@ -1380,6 +1380,7 @@ grib_box* grib_box_factory(grib_handle* h, grib_arguments* args); int grib_nearest_find(grib_nearest* nearest, const grib_handle* h, double inlat, double inlon, unsigned long flags, double* outlats, double* outlons, double* values, double* distances, int* indexes, size_t* len); int grib_nearest_init(grib_nearest* i, grib_handle* h, grib_arguments* args); int grib_nearest_delete(grib_nearest* i); +int grib_nearest_get_radius(grib_handle* h, double* radiusInKm); void grib_binary_search(double xx[], const unsigned long n, double x, int* ju, int* jl); int grib_nearest_find_multiple(const grib_handle* h, int is_lsm, const double* inlats, const double* inlons, long npoints, double* outlats, double* outlons, double* values, double* distances, int* indexes); int grib_nearest_find_generic(grib_nearest* nearest, grib_handle* h, double inlat, double inlon, unsigned long flags, diff --git a/src/grib_nearest.c b/src/grib_nearest.c index a07ab5904..f389c60b2 100644 --- a/src/grib_nearest.c +++ b/src/grib_nearest.c @@ -97,6 +97,37 @@ int grib_nearest_delete(grib_nearest* i) return 0; } +/* Get the radius in kilometres for nearest neighbour distance calculations */ +/* For an ellipsoid, approximate the radius using the average of the semimajor and semiminor axes */ +int grib_nearest_get_radius(grib_handle* h, double* radiusInKm) +{ + int err = 0; + long lRadiusInMetres; + double result = 0; + const char* s_radius = "radius"; + const char* s_minor = "earthMinorAxisInMetres"; + const char* s_major = "earthMajorAxisInMetres"; + + if ((err = grib_get_long(h, s_radius, &lRadiusInMetres)) == GRIB_SUCCESS) { + if (grib_is_missing(h, s_radius, &err) || lRadiusInMetres == GRIB_MISSING_LONG) { + grib_context_log(h->context, GRIB_LOG_DEBUG, "Key 'radius' is missing"); + return GRIB_GEOCALCULUS_PROBLEM; + } + result = ((double)lRadiusInMetres) / 1000.0; + } + else { + double minor = 0, major = 0; + if ((err = grib_get_double_internal(h, s_minor, &minor)) != GRIB_SUCCESS) return err; + if ((err = grib_get_double_internal(h, s_major, &major)) != GRIB_SUCCESS) return err; + if (grib_is_missing(h, s_minor, &err)) return GRIB_GEOCALCULUS_PROBLEM; + if (grib_is_missing(h, s_major, &err)) return GRIB_GEOCALCULUS_PROBLEM; + result = (major + minor) / 2.0; + result = result / 1000.0; + } + *radiusInKm = result; + return GRIB_SUCCESS; +} + void grib_binary_search(double xx[], const unsigned long n, double x, int* ju, int* jl) { @@ -278,7 +309,7 @@ int grib_nearest_find_generic( { int ret = 0, i = 0; size_t nvalues = 0, nneighbours = 0; - double radiusInMetres, radiusInKm; + double radiusInKm; grib_iterator* iter = NULL; double lat = 0, lon = 0; @@ -291,21 +322,8 @@ int grib_nearest_find_generic( return ret; nearest->values_count = nvalues; - /* We need the radius to calculate the nearest distance. For an oblate earth - approximate this using the average of the semimajor and semiminor axes */ - if ((ret = grib_get_double(h, radius_keyname, &radiusInMetres)) == GRIB_SUCCESS && - !grib_is_missing(h, radius_keyname, &ret)) { - radiusInKm = radiusInMetres / 1000.0; - } - else { - double minor = 0, major = 0; - if ((ret = grib_get_double_internal(h, "earthMinorAxisInMetres", &minor)) != GRIB_SUCCESS) return ret; - if ((ret = grib_get_double_internal(h, "earthMajorAxisInMetres", &major)) != GRIB_SUCCESS) return ret; - if (grib_is_missing(h, "earthMinorAxisInMetres", &ret)) return GRIB_GEOCALCULUS_PROBLEM; - if (grib_is_missing(h, "earthMajorAxisInMetres", &ret)) return GRIB_GEOCALCULUS_PROBLEM; - radiusInMetres = (major + minor) / 2; - radiusInKm = radiusInMetres / 1000.0; - } + if ((ret = grib_nearest_get_radius(h, &radiusInKm)) != GRIB_SUCCESS) + return ret; neighbours = (PointStore*)grib_context_malloc(nearest->context, nvalues * sizeof(PointStore)); for (i = 0; i < nvalues; ++i) { diff --git a/src/grib_nearest_class_latlon_reduced.c b/src/grib_nearest_class_latlon_reduced.c index 5c4243718..02e31f6e8 100644 --- a/src/grib_nearest_class_latlon_reduced.c +++ b/src/grib_nearest_class_latlon_reduced.c @@ -119,22 +119,15 @@ static int find(grib_nearest* nearest, grib_handle* h, size_t nvalues = 0; grib_iterator* iter = NULL; double lat = 0, lon = 0; - long iradius; - double radius; + double radiusInKm; int ilat = 0, ilon = 0; if ((ret = grib_get_size(h, self->values_key, &nvalues)) != GRIB_SUCCESS) return ret; nearest->values_count = nvalues; - if (grib_is_missing(h, self->radius, &ret)) { - grib_context_log(h->context, GRIB_LOG_DEBUG, "Key '%s' is missing", self->radius); - return ret ? ret : GRIB_GEOCALCULUS_PROBLEM; - } - - if ((ret = grib_get_long(h, self->radius, &iradius)) != GRIB_SUCCESS) + if ((ret = grib_nearest_get_radius(h, &radiusInKm)) != GRIB_SUCCESS) return ret; - radius = ((double)iradius) / 1000.0; /* Compute lat/lon info, create iterator etc if it's the 1st time or different grid. * This is for performance: if the grid has not changed, we only do this once @@ -352,7 +345,7 @@ static int find(grib_nearest* nearest, grib_handle* h, kk = 0; for (jj = 0; jj < 2; jj++) { for (ii = 0; ii < 2; ii++) { - self->distances[kk] = geographic_distance_spherical(radius, inlon, inlat, + self->distances[kk] = geographic_distance_spherical(radiusInKm, inlon, inlat, self->lons[self->k[kk]], self->lats[self->j[jj]]); kk++; } diff --git a/src/grib_nearest_class_reduced.c b/src/grib_nearest_class_reduced.c index 2a2235d1e..7fb2232f8 100644 --- a/src/grib_nearest_class_reduced.c +++ b/src/grib_nearest_class_reduced.c @@ -148,8 +148,7 @@ static int find(grib_nearest* nearest, grib_handle* h, size_t nvalues = 0; grib_iterator* iter = NULL; double lat = 0, lon = 0; - long iradius; - double radius; + double radiusInKm; int ilat = 0, ilon = 0; get_reduced_row_proc get_reduced_row_func = &grib_get_reduced_row; @@ -164,14 +163,8 @@ static int find(grib_nearest* nearest, grib_handle* h, return ret; nearest->values_count = nvalues; - if (grib_is_missing(h, self->radius, &ret)) { - grib_context_log(h->context, GRIB_LOG_DEBUG, "Key '%s' is missing", self->radius); - return ret ? ret : GRIB_GEOCALCULUS_PROBLEM; - } - - if ((ret = grib_get_long(h, self->radius, &iradius)) != GRIB_SUCCESS) + if ((ret = grib_nearest_get_radius(h, &radiusInKm)) != GRIB_SUCCESS) return ret; - radius = ((double)iradius) / 1000.0; /* Compute lat/lon info, create iterator etc if it's the 1st time or different grid. * This is for performance: if the grid has not changed, we only do this once @@ -421,7 +414,7 @@ static int find(grib_nearest* nearest, grib_handle* h, kk = 0; for (jj = 0; jj < 2; jj++) { for (ii = 0; ii < 2; ii++) { - self->distances[kk] = geographic_distance_spherical(radius, inlon, inlat, + self->distances[kk] = geographic_distance_spherical(radiusInKm, inlon, inlat, self->lons[self->k[kk]], self->lats[self->j[jj]]); kk++; } diff --git a/src/grib_nearest_class_regular.c b/src/grib_nearest_class_regular.c index 6849294cc..deea985d4 100644 --- a/src/grib_nearest_class_regular.c +++ b/src/grib_nearest_class_regular.c @@ -108,13 +108,10 @@ static int find(grib_nearest* nearest, grib_handle* h, grib_nearest_regular* self = (grib_nearest_regular*) nearest; int ret=0,kk=0,ii=0,jj=0; size_t nvalues=0; + double radiusInKm; - long iradius; - double radius; - - if( (ret = grib_get_long(h,self->radius,&iradius))!= GRIB_SUCCESS) + if ((ret = grib_nearest_get_radius(h, &radiusInKm)) != GRIB_SUCCESS) return ret; - radius=((double)iradius)/1000.0; if (!nearest->h || (flags & GRIB_NEAREST_SAME_DATA)==0 || nearest->h!=h) { grib_iterator* iter=NULL; @@ -231,8 +228,7 @@ static int find(grib_nearest* nearest, grib_handle* h, grib_nearest_regular* self = (grib_nearest_regular*)nearest; int ret = 0, kk = 0, ii = 0, jj = 0; size_t nvalues = 0; - long iradius; - double radius; /* radius in km */ + double radiusInKm; grib_iterator* iter = NULL; double lat = 0, lon = 0; @@ -248,24 +244,8 @@ static int find(grib_nearest* nearest, grib_handle* h, return ret; nearest->values_count = nvalues; - /* We need the radius to calculate the nearest distance. For an oblate earth - approximate this using the average of the semimajor and semiminor axes */ - if ((ret = grib_get_long(h, self->radius, &iradius)) == GRIB_SUCCESS) { - if (grib_is_missing(h, self->radius, &ret) || iradius == GRIB_MISSING_LONG) { - grib_context_log(h->context, GRIB_LOG_DEBUG, "Key '%s' is missing", self->radius); - return GRIB_GEOCALCULUS_PROBLEM; - } - radius = ((double)iradius) / 1000.0; - } - else { - double minor = 0, major = 0; - if ((ret = grib_get_double_internal(h, "earthMinorAxisInMetres", &minor)) != GRIB_SUCCESS) return ret; - if ((ret = grib_get_double_internal(h, "earthMajorAxisInMetres", &major)) != GRIB_SUCCESS) return ret; - if (grib_is_missing(h, "earthMinorAxisInMetres", &ret)) return GRIB_GEOCALCULUS_PROBLEM; - if (grib_is_missing(h, "earthMajorAxisInMetres", &ret)) return GRIB_GEOCALCULUS_PROBLEM; - radius = (major + minor) / 2.0; - radius = radius / 1000.0; - } + if ((ret = grib_nearest_get_radius(h, &radiusInKm)) != GRIB_SUCCESS) + return ret; /* Compute lat/lon info, create iterator etc if it's the 1st time or different grid. * This is for performance: if the grid has not changed, we only do this once @@ -424,7 +404,7 @@ static int find(grib_nearest* nearest, grib_handle* h, for (jj = 0; jj < 2; jj++) { for (ii = 0; ii < 2; ii++) { self->k[kk] = self->i[ii] + self->lons_count * self->j[jj]; - self->distances[kk] = geographic_distance_spherical(radius, inlon, inlat, + self->distances[kk] = geographic_distance_spherical(radiusInKm, inlon, inlat, self->lons[self->i[ii]], self->lats[self->j[jj]]); kk++; } diff --git a/tests/grib_nearest_test.sh b/tests/grib_nearest_test.sh index 03a35aa49..33bc2c16f 100755 --- a/tests/grib_nearest_test.sh +++ b/tests/grib_nearest_test.sh @@ -54,10 +54,35 @@ diff $tempRef $temp # ECC-1295: regular lat/lon on ellipsoid # ---------------------------------------- sample2=$ECCODES_SAMPLES_PATH/GRIB2.tmpl +${tools_dir}/grib_set -s shapeOfTheEarth=2 $sample2 $temp +grib_check_key_equals $sample2 earthIsOblate 0 +grib_check_key_equals $temp earthIsOblate 1 +${tools_dir}/grib_ls -l 0,0 $temp + +# reduced lat/lon on ellipsoid +# ---------------------------------------- +sample2=$ECCODES_SAMPLES_PATH/reduced_ll_sfc_grib2.tmpl +${tools_dir}/grib_set -s shapeOfTheEarth=4 $sample2 $temp +grib_check_key_equals $sample2 earthIsOblate 0 +grib_check_key_equals $temp earthIsOblate 1 +${tools_dir}/grib_ls -l 0,0 $temp + +# regular gaussian on ellipsoid +# ---------------------------------------- +sample2=$ECCODES_SAMPLES_PATH/regular_gg_pl_grib2.tmpl ${tools_dir}/grib_set -s shapeOfTheEarth=5 $sample2 $temp grib_check_key_equals $sample2 earthIsOblate 0 grib_check_key_equals $temp earthIsOblate 1 ${tools_dir}/grib_ls -l 0,0 $temp +# reduced gaussian on ellipsoid +# ---------------------------------------- +sample2=$ECCODES_SAMPLES_PATH/reduced_gg_pl_48_grib2.tmpl +${tools_dir}/grib_set -s shapeOfTheEarth=5 $sample2 $temp +grib_check_key_equals $sample2 earthIsOblate 0 +grib_check_key_equals $temp earthIsOblate 1 +${tools_dir}/grib_ls -l 0,0 $temp + + # Clean up rm -f $temp $tempRef From adbb4e5f5843ed54d9bc641117379cc0438bc8f1 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Sat, 6 Nov 2021 13:58:39 +0000 Subject: [PATCH 04/10] Clean up --- src/grib_api_prototypes.h | 3 +-- src/grib_nearest.c | 1 - src/grib_nearest_class_lambert_azimuthal_equal_area.c | 1 - src/grib_nearest_class_lambert_conformal.c | 1 - src/grib_nearest_class_mercator.c | 1 - src/grib_nearest_class_polar_stereographic.c | 1 - src/grib_nearest_class_space_view.c | 1 - 7 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/grib_api_prototypes.h b/src/grib_api_prototypes.h index 4e4cfa376..dbf31a1cb 100644 --- a/src/grib_api_prototypes.h +++ b/src/grib_api_prototypes.h @@ -1384,8 +1384,7 @@ int grib_nearest_get_radius(grib_handle* h, double* radiusInKm); void grib_binary_search(double xx[], const unsigned long n, double x, int* ju, int* jl); int grib_nearest_find_multiple(const grib_handle* h, int is_lsm, const double* inlats, const double* inlons, long npoints, double* outlats, double* outlons, double* values, double* distances, int* indexes); int grib_nearest_find_generic(grib_nearest* nearest, grib_handle* h, double inlat, double inlon, unsigned long flags, - const char* values_keyname, const char* radius_keyname, - const char* Ni_keyname, const char* Nj_keyname, + const char* values_keyname, const char* Ni_keyname, const char* Nj_keyname, double** out_lats, int* out_lats_count, double** out_lons, diff --git a/src/grib_nearest.c b/src/grib_nearest.c index f389c60b2..5d62c8340 100644 --- a/src/grib_nearest.c +++ b/src/grib_nearest.c @@ -295,7 +295,6 @@ int grib_nearest_find_generic( double inlat, double inlon, unsigned long flags, const char* values_keyname, - const char* radius_keyname, const char* Ni_keyname, const char* Nj_keyname, double** out_lats, diff --git a/src/grib_nearest_class_lambert_azimuthal_equal_area.c b/src/grib_nearest_class_lambert_azimuthal_equal_area.c index 70715f472..a2d433b44 100644 --- a/src/grib_nearest_class_lambert_azimuthal_equal_area.c +++ b/src/grib_nearest_class_lambert_azimuthal_equal_area.c @@ -110,7 +110,6 @@ static int find(grib_nearest* nearest, grib_handle* h, nearest, h, inlat, inlon, flags, /* inputs */ self->values_key, /* outputs to set the 'self' object */ - self->radius, self->Ni, self->Nj, &(self->lats), diff --git a/src/grib_nearest_class_lambert_conformal.c b/src/grib_nearest_class_lambert_conformal.c index 0fc5ed54a..241a6b2c3 100644 --- a/src/grib_nearest_class_lambert_conformal.c +++ b/src/grib_nearest_class_lambert_conformal.c @@ -110,7 +110,6 @@ static int find(grib_nearest* nearest, grib_handle* h, nearest, h, inlat, inlon, flags, /* inputs */ self->values_key, /* outputs to set the 'self' object */ - self->radius, self->Ni, self->Nj, &(self->lats), diff --git a/src/grib_nearest_class_mercator.c b/src/grib_nearest_class_mercator.c index f447e9d2f..03737e45c 100644 --- a/src/grib_nearest_class_mercator.c +++ b/src/grib_nearest_class_mercator.c @@ -112,7 +112,6 @@ static int find(grib_nearest* nearest, grib_handle* h, nearest, h, inlat, inlon, flags, /* inputs */ self->values_key, /* outputs to set the 'self' object */ - self->radius, self->Ni, self->Nj, &(self->lats), diff --git a/src/grib_nearest_class_polar_stereographic.c b/src/grib_nearest_class_polar_stereographic.c index 4796c3ea6..e4568e3f0 100644 --- a/src/grib_nearest_class_polar_stereographic.c +++ b/src/grib_nearest_class_polar_stereographic.c @@ -110,7 +110,6 @@ static int find(grib_nearest* nearest, grib_handle* h, nearest, h, inlat, inlon, flags, /* inputs */ self->values_key, /* outputs to set the 'self' object */ - self->radius, self->Ni, self->Nj, &(self->lats), diff --git a/src/grib_nearest_class_space_view.c b/src/grib_nearest_class_space_view.c index 874670f67..2d063066f 100644 --- a/src/grib_nearest_class_space_view.c +++ b/src/grib_nearest_class_space_view.c @@ -110,7 +110,6 @@ static int find(grib_nearest* nearest, grib_handle* h, nearest, h, inlat, inlon, flags, /* inputs */ self->values_key, /* outputs to set the 'self' object */ - self->radius, self->Ni, self->Nj, &(self->lats), From 6b2c1348b34fbb9825c0f8a9db57e8a71c99bc6e Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Sat, 6 Nov 2021 14:18:48 +0000 Subject: [PATCH 05/10] Cppcheck warnings: Const correctness --- tools/bufr_compare.c | 6 +++--- tools/grib_compare.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tools/bufr_compare.c b/tools/bufr_compare.c index b3f384a69..e26933429 100644 --- a/tools/bufr_compare.c +++ b/tools/bufr_compare.c @@ -59,7 +59,7 @@ GRIB_INLINE static int grib_inline_strcmp(const char* a, const char* b) return (*a == 0 && *b == 0) ? 0 : 1; } -typedef double (*compare_double_proc)(double*, double*, double*); +typedef double (*compare_double_proc)(const double*, const double*, const double*); typedef struct grib_error grib_error; struct grib_error @@ -136,7 +136,7 @@ static void release_keys_list() } } -GRIB_INLINE static double compare_double_absolute(double* a, double* b, double* err) +GRIB_INLINE static double compare_double_absolute(const double* a, const double* b, const double* err) { double ret = 0; double d = fabs(*a - *b); @@ -183,7 +183,7 @@ static void write_messages(grib_handle* handle1, grib_handle* handle2) write_message(handle2, "error2"); } -static double compare_double_relative(double* a, double* b, double* err) +static double compare_double_relative(const double* a, const double* b, const double* err) { double relativeError; diff --git a/tools/grib_compare.c b/tools/grib_compare.c index bef41172d..2df935d52 100644 --- a/tools/grib_compare.c +++ b/tools/grib_compare.c @@ -46,7 +46,7 @@ const char* tool_description = const char* tool_name = "grib_compare"; const char* tool_usage = "[options] grib_file1 grib_file2"; -typedef double (*compare_double_proc)(double*, double*, double); +typedef double (*compare_double_proc)(const double*, const double*, double); static int compare_handles(grib_handle* h1, grib_handle* h2, grib_runtime_options* options); typedef struct grib_error grib_error; @@ -124,7 +124,7 @@ GRIB_INLINE static int grib_inline_rstrcmp(const char* a, const char* b) } /* Returns 0 when the values are considered the same */ -static double compare_double_absolute(double* a, double* b, double tolerance) +static double compare_double_absolute(const double* a, const double* b, double tolerance) { double ret = 0; double d = fabs(*a - *b); @@ -136,7 +136,7 @@ static double compare_double_absolute(double* a, double* b, double tolerance) } /* Returns 0 when the values are considered the same */ -static double compare_double_relative(double* a, double* b, double tolerance) +static double compare_double_relative(const double* a, const double* b, double tolerance) { double relativeError; From 08fa1ee919a6f637c099117156b9180f790886ed Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Sat, 6 Nov 2021 14:21:32 +0000 Subject: [PATCH 06/10] Cppcheck warnings: Const correctness --- tools/metar_compare.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/metar_compare.c b/tools/metar_compare.c index aac50cb4e..fdbb46d05 100644 --- a/tools/metar_compare.c +++ b/tools/metar_compare.c @@ -60,7 +60,7 @@ GRIB_INLINE static int grib_inline_strcmp(const char* a, const char* b) return (*a == 0 && *b == 0) ? 0 : 1; } -typedef double (*compare_double_proc)(double*, double*, double*); +typedef double (*compare_double_proc)(const double*, const double*, const double*); typedef struct grib_error grib_error; struct grib_error @@ -97,7 +97,7 @@ static int start = -1; static int end = -1; static int write_count = 0; -GRIB_INLINE static double compare_double_absolute(double* a, double* b, double* err) +GRIB_INLINE static double compare_double_absolute(const double* a, const double* b, const double* err) { double ret = 0; double d = fabs(*a - *b); @@ -144,7 +144,7 @@ static void write_messages(grib_handle* h1, grib_handle* h2) write_message(h2, "error2"); } -static double compare_double_relative(double* a, double* b, double* err) +static double compare_double_relative(const double* a, const double* b, const double* err) { double relativeError; From 87acf45bb5db75006eb94f1ef00359d199df1f93 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Sat, 6 Nov 2021 17:56:21 +0000 Subject: [PATCH 07/10] Cppcheck warnings: Const correctness --- src/grib_bits_any_endian.c | 2 +- src/grib_bits_fast_big_endian.c | 2 +- src/grib_iterator_class_lambert_azimuthal_equal_area.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/grib_bits_any_endian.c b/src/grib_bits_any_endian.c index 362f11fe2..411d902bc 100644 --- a/src/grib_bits_any_endian.c +++ b/src/grib_bits_any_endian.c @@ -97,7 +97,7 @@ int grib_encode_string(unsigned char* bitStream, long* bitOffset, size_t numberO int remainder = *bitOffset % 8; unsigned char c; unsigned char* p; - unsigned char mask[] = { 0, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE }; + const unsigned char mask[] = { 0, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE }; int remainderComplement = 8 - remainder; char str[512] = {0,}; char* s = str; diff --git a/src/grib_bits_fast_big_endian.c b/src/grib_bits_fast_big_endian.c index 4a50349dc..b47dcfcc7 100644 --- a/src/grib_bits_fast_big_endian.c +++ b/src/grib_bits_fast_big_endian.c @@ -90,7 +90,7 @@ char* grib_decode_string(const unsigned char* bitStream, long* bitOffset, size_t unsigned char c; unsigned char* p; char* s = string; - unsigned char mask[] = { 0, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE }; + const unsigned char mask[] = { 0, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE }; int remainderComplement = 8 - remainder; if (numberOfCharacters == 0) diff --git a/src/grib_iterator_class_lambert_azimuthal_equal_area.c b/src/grib_iterator_class_lambert_azimuthal_equal_area.c index 0ba3be713..f105094ae 100644 --- a/src/grib_iterator_class_lambert_azimuthal_equal_area.c +++ b/src/grib_iterator_class_lambert_azimuthal_equal_area.c @@ -131,7 +131,7 @@ static void pj_authset(double es, double* APA) APA[1] += t * P11; APA[2] = t * P20; } -static double pj_authlat(double beta, double* APA) +static double pj_authlat(double beta, const double* APA) { double t = beta + beta; return (beta + APA[0] * sin(t) + APA[1] * sin(t + t) + APA[2] * sin(t + t + t)); From 6ae358e67eb06c1b7641032ba0328361853108f5 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Sat, 6 Nov 2021 19:29:01 +0000 Subject: [PATCH 08/10] Cppcheck warnings: unused struct --- src/grib_dumper_class_bufr_decode_C.c | 8 -------- src/grib_dumper_class_bufr_decode_filter.c | 10 +--------- src/grib_dumper_class_bufr_decode_fortran.c | 8 -------- src/grib_dumper_class_bufr_decode_python.c | 8 -------- src/grib_dumper_class_bufr_encode_C.c | 8 -------- src/grib_dumper_class_bufr_encode_filter.c | 10 ---------- src/grib_dumper_class_bufr_encode_fortran.c | 8 -------- src/grib_dumper_class_bufr_encode_python.c | 8 -------- src/grib_dumper_class_bufr_simple.c | 8 -------- 9 files changed, 1 insertion(+), 75 deletions(-) diff --git a/src/grib_dumper_class_bufr_decode_C.c b/src/grib_dumper_class_bufr_decode_C.c index b95dd4341..a2932cc09 100644 --- a/src/grib_dumper_class_bufr_decode_C.c +++ b/src/grib_dumper_class_bufr_decode_C.c @@ -109,14 +109,6 @@ GRIB_INLINE static int grib_inline_strcmp(const char* a, const char* b) return (*a == 0 && *b == 0) ? 0 : 1; } -typedef struct string_count string_count; -struct string_count -{ - char* value; - int count; - string_count* next; -}; - static int depth = 0; static void init_class(grib_dumper_class* c) {} diff --git a/src/grib_dumper_class_bufr_decode_filter.c b/src/grib_dumper_class_bufr_decode_filter.c index b07dbaae4..a61c985e4 100644 --- a/src/grib_dumper_class_bufr_decode_filter.c +++ b/src/grib_dumper_class_bufr_decode_filter.c @@ -107,15 +107,7 @@ GRIB_INLINE static int grib_inline_strcmp(const char* a, const char* b) } return (*a == 0 && *b == 0) ? 0 : 1; } -/* -typedef struct string_count string_count; -struct string_count -{ - char* value; - int count; - string_count* next; -}; -*/ + static int depth = 0; static void init_class(grib_dumper_class* c) {} diff --git a/src/grib_dumper_class_bufr_decode_fortran.c b/src/grib_dumper_class_bufr_decode_fortran.c index 0089d35b2..c8ff95ee5 100644 --- a/src/grib_dumper_class_bufr_decode_fortran.c +++ b/src/grib_dumper_class_bufr_decode_fortran.c @@ -109,14 +109,6 @@ GRIB_INLINE static int grib_inline_strcmp(const char* a, const char* b) return (*a == 0 && *b == 0) ? 0 : 1; } -typedef struct string_count string_count; -struct string_count -{ - char* value; - int count; - string_count* next; -}; - static int depth = 0; static void init_class(grib_dumper_class* c) {} diff --git a/src/grib_dumper_class_bufr_decode_python.c b/src/grib_dumper_class_bufr_decode_python.c index 18dc5846c..38895fba3 100644 --- a/src/grib_dumper_class_bufr_decode_python.c +++ b/src/grib_dumper_class_bufr_decode_python.c @@ -109,14 +109,6 @@ GRIB_INLINE static int grib_inline_strcmp(const char* a, const char* b) return (*a == 0 && *b == 0) ? 0 : 1; } -typedef struct string_count string_count; -struct string_count -{ - char* value; - int count; - string_count* next; -}; - static int depth = 0; static void init_class(grib_dumper_class* c) {} diff --git a/src/grib_dumper_class_bufr_encode_C.c b/src/grib_dumper_class_bufr_encode_C.c index f82cc960c..718792238 100644 --- a/src/grib_dumper_class_bufr_encode_C.c +++ b/src/grib_dumper_class_bufr_encode_C.c @@ -109,14 +109,6 @@ GRIB_INLINE static int grib_inline_strcmp(const char* a, const char* b) return (*a == 0 && *b == 0) ? 0 : 1; } -typedef struct string_count string_count; -struct string_count -{ - char* value; - int count; - string_count* next; -}; - static int depth = 0; static void init_class(grib_dumper_class* c) {} diff --git a/src/grib_dumper_class_bufr_encode_filter.c b/src/grib_dumper_class_bufr_encode_filter.c index f68e9f397..a481614b2 100644 --- a/src/grib_dumper_class_bufr_encode_filter.c +++ b/src/grib_dumper_class_bufr_encode_filter.c @@ -110,16 +110,6 @@ GRIB_INLINE static int grib_inline_strcmp(const char* a, const char* b) return (*a == 0 && *b == 0) ? 0 : 1; } -/* -typedef struct string_count string_count; -struct string_count -{ - char* value; - int count; - string_count* next; -}; -*/ - static int depth = 0; static void init_class(grib_dumper_class* c) {} diff --git a/src/grib_dumper_class_bufr_encode_fortran.c b/src/grib_dumper_class_bufr_encode_fortran.c index 0f99793dc..78ec92f52 100644 --- a/src/grib_dumper_class_bufr_encode_fortran.c +++ b/src/grib_dumper_class_bufr_encode_fortran.c @@ -109,14 +109,6 @@ GRIB_INLINE static int grib_inline_strcmp(const char* a, const char* b) return (*a == 0 && *b == 0) ? 0 : 1; } -typedef struct string_count string_count; -struct string_count -{ - char* value; - int count; - string_count* next; -}; - static int depth = 0; static void init_class(grib_dumper_class* c) {} diff --git a/src/grib_dumper_class_bufr_encode_python.c b/src/grib_dumper_class_bufr_encode_python.c index 301e1eb70..fba839495 100644 --- a/src/grib_dumper_class_bufr_encode_python.c +++ b/src/grib_dumper_class_bufr_encode_python.c @@ -109,14 +109,6 @@ GRIB_INLINE static int grib_inline_strcmp(const char* a, const char* b) return (*a == 0 && *b == 0) ? 0 : 1; } -typedef struct string_count string_count; -struct string_count -{ - char* value; - int count; - string_count* next; -}; - static int depth = 0; static void init_class(grib_dumper_class* c) {} diff --git a/src/grib_dumper_class_bufr_simple.c b/src/grib_dumper_class_bufr_simple.c index 785b41c2e..2aeeeb433 100644 --- a/src/grib_dumper_class_bufr_simple.c +++ b/src/grib_dumper_class_bufr_simple.c @@ -108,14 +108,6 @@ GRIB_INLINE static int grib_inline_strcmp(const char* a, const char* b) return (*a == 0 && *b == 0) ? 0 : 1; } -typedef struct string_count string_count; -struct string_count -{ - char* value; - int count; - string_count* next; -}; - static void init_class(grib_dumper_class* c) {} static int init(grib_dumper* d) From a5f44eebcaa08a3e73ecb1d1c7200642a2137e08 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Sat, 6 Nov 2021 19:33:00 +0000 Subject: [PATCH 09/10] Cppcheck warnings: Const correctness --- src/grib_ibmfloat.c | 2 +- src/grib_ieeefloat.c | 2 +- src/grib_iterator_class_gaussian.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/grib_ibmfloat.c b/src/grib_ibmfloat.c index fd62861e9..1b11ed3bf 100644 --- a/src/grib_ibmfloat.c +++ b/src/grib_ibmfloat.c @@ -89,7 +89,7 @@ static void init_table_if_needed() GRIB_MUTEX_UNLOCK(&mutex) } -static void binary_search(double xx[], const unsigned long n, double x, unsigned long* j) +static void binary_search(const double xx[], const unsigned long n, double x, unsigned long* j) { /*These routine works only on ascending ordered arrays*/ unsigned long ju, jm, jl; diff --git a/src/grib_ieeefloat.c b/src/grib_ieeefloat.c index d7b713a24..82f20349f 100644 --- a/src/grib_ieeefloat.c +++ b/src/grib_ieeefloat.c @@ -95,7 +95,7 @@ static void init_table_if_needed() GRIB_MUTEX_UNLOCK(&mutex) } -static void binary_search(double xx[], const unsigned long n, double x, unsigned long* j) +static void binary_search(const double xx[], const unsigned long n, double x, unsigned long* j) { /*These routine works only on ascending ordered arrays*/ unsigned long ju, jm, jl; diff --git a/src/grib_iterator_class_gaussian.c b/src/grib_iterator_class_gaussian.c index 06fb66f8f..d0f659ceb 100644 --- a/src/grib_iterator_class_gaussian.c +++ b/src/grib_iterator_class_gaussian.c @@ -91,7 +91,7 @@ static void init_class(grib_iterator_class* c) } /* END_CLASS_IMP */ -static void binary_search(double xx[], const unsigned long n, double x, unsigned long* j); +static void binary_search(const double xx[], const unsigned long n, double x, unsigned long* j); static int init(grib_iterator* i, grib_handle* h, grib_arguments* args) { @@ -164,7 +164,7 @@ static int init(grib_iterator* i, grib_handle* h, grib_arguments* args) return ret; } -static void binary_search(double xx[], const unsigned long n, double x, unsigned long* j) +static void binary_search(const double xx[], const unsigned long n, double x, unsigned long* j) { /*This routine works only on descending ordered arrays*/ #define EPSILON 1e-3 From 5a8885fe119f947fbb3341b1a9dcd48d16b105ab Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Sat, 6 Nov 2021 21:46:06 +0000 Subject: [PATCH 10/10] Cppcheck warnings: examples --- examples/C/bufr_copy_data.c | 6 +----- examples/C/bufr_expanded.c | 2 +- examples/C/bufr_keys_iterator.c | 2 +- examples/C/bufr_read_temp.c | 2 +- examples/C/grib_clone.c | 10 ++++++---- examples/C/grib_ensemble_index.c | 4 ++-- examples/C/grib_iterator_bitmap.c | 2 +- examples/C/grib_keys_iterator.c | 4 ---- examples/C/grib_precipitation.c | 1 - examples/C/multi2.c | 2 +- examples/C/new_sample.c | 2 +- 11 files changed, 15 insertions(+), 22 deletions(-) diff --git a/examples/C/bufr_copy_data.c b/examples/C/bufr_copy_data.c index eb816163f..589a7de1b 100644 --- a/examples/C/bufr_copy_data.c +++ b/examples/C/bufr_copy_data.c @@ -79,10 +79,6 @@ int main(int argc, char* argv[]) codes_set_long_array(ho, "unexpandedDescriptors", ud, size); while ((h = codes_handle_new_from_file(0, f, PRODUCT_BUFR, &err)) != NULL) { - if (!h) { - fprintf(stderr, "Error: Unable to create BUFR handle\n"); - return 1; - } /* codes_copy_key(h,ho,"unexpandedDescriptors",0); */ err = codes_set_long(h, "unpack", 1); if (err) { @@ -97,7 +93,7 @@ int main(int argc, char* argv[]) printf("Copied %s\n", keys[i]); free(keys[i]); } - printf("Total number of copied keys = %lu\n", nkeys); + printf("Total number of copied keys = %lu\n", (unsigned long)nkeys); free(keys); codes_handle_delete(h); diff --git a/examples/C/bufr_expanded.c b/examples/C/bufr_expanded.c index a18d1040c..46ee17b0a 100644 --- a/examples/C/bufr_expanded.c +++ b/examples/C/bufr_expanded.c @@ -57,7 +57,7 @@ int main(int argc, char* argv[]) /* get the size of the values array*/ CODES_CHECK(codes_get_size(h, "numericValues", &values_len), 0); - printf(" number of expanded values: %lu\n", values_len); + printf(" number of expanded values: %lu\n", (unsigned long)values_len); /* allocate array for data values */ values = (double*)malloc(values_len * sizeof(double)); diff --git a/examples/C/bufr_keys_iterator.c b/examples/C/bufr_keys_iterator.c index d3544873a..1db53101e 100644 --- a/examples/C/bufr_keys_iterator.c +++ b/examples/C/bufr_keys_iterator.c @@ -92,7 +92,7 @@ int main(int argc, char* argv[]) } else { /* for arrays */ - printf("(array of %lu)\n", klen); + printf("(array of %lu)\n", (unsigned long)klen); } } diff --git a/examples/C/bufr_read_temp.c b/examples/C/bufr_read_temp.c index 7a3610671..90772d772 100644 --- a/examples/C/bufr_read_temp.c +++ b/examples/C/bufr_read_temp.c @@ -140,7 +140,7 @@ int main(int argc, char* argv[]) for (i = 0; i < sigt_len; i++) { printf("%3lu %6.0f %6.0f %.1f %.1f\n", - i + 1, sigt_pres[i], sigt_geo[i], sigt_t[i], sigt_td[i]); + (unsigned long)(i + 1), sigt_pres[i], sigt_geo[i], sigt_t[i], sigt_td[i]); } /* Delete handle */ diff --git a/examples/C/grib_clone.c b/examples/C/grib_clone.c index cecc5f3d3..07e137c8f 100644 --- a/examples/C/grib_clone.c +++ b/examples/C/grib_clone.c @@ -38,11 +38,13 @@ int main(int argc, char* argv[]) } in = fopen(argv[1], "rb"); + if (!in) { + perror("ERROR: unable to open input file"); + return 1; + } out = fopen(argv[2], "wb"); - - if (!in || !out) { - perror("ERROR: unable to open files"); - fclose(out); + if (!out) { + perror("ERROR: unable to open output file"); fclose(in); return 1; } diff --git a/examples/C/grib_ensemble_index.c b/examples/C/grib_ensemble_index.c index 0de9fc260..aab96f64d 100644 --- a/examples/C/grib_ensemble_index.c +++ b/examples/C/grib_ensemble_index.c @@ -39,7 +39,7 @@ int main(int argc, char* argv[]) /* get size of "paramId" list */ CODES_CHECK(codes_index_get_size(index, "paramId", ¶mIdSize), 0); - printf("grib contains %lu different parameters\n", paramIdSize); + printf("grib contains %lu different parameters\n", (unsigned long)paramIdSize); /* allocate memory for "paramId" list */ paramId = (char**)malloc(paramIdSize * sizeof(char*)); /* get list of "paramId" */ @@ -47,7 +47,7 @@ int main(int argc, char* argv[]) /* get size of ensemble number list */ CODES_CHECK(codes_index_get_size(index, "number", &numberSize), 0); - printf("GRIB contains %lu different ensemble members\n", numberSize); + printf("GRIB contains %lu different ensemble members\n", (unsigned long)numberSize); /* allocate memory for ensemble number list */ number = (long*)malloc(numberSize * sizeof(long)); /* get list of ensemble numbers */ diff --git a/examples/C/grib_iterator_bitmap.c b/examples/C/grib_iterator_bitmap.c index 53bc18127..9871f7f91 100644 --- a/examples/C/grib_iterator_bitmap.c +++ b/examples/C/grib_iterator_bitmap.c @@ -55,7 +55,7 @@ int main(int argc, char** argv) CODES_CHECK(codes_get_size(h, "bitmap", &bmp_len), 0); bitmap = (long*)malloc(bmp_len * sizeof(long)); CODES_CHECK(codes_get_long_array(h, "bitmap", bitmap, &bmp_len), 0); - printf("Bitmap is present. Num = %lu\n", bmp_len); + printf("Bitmap is present. Num = %lu\n", (unsigned long)bmp_len); } /* Sanity check. Number of values must match number in bitmap */ CODES_CHECK(codes_get_size(h, "values", &values_len), 0); diff --git a/examples/C/grib_keys_iterator.c b/examples/C/grib_keys_iterator.c index c4de118b2..ca550238a 100644 --- a/examples/C/grib_keys_iterator.c +++ b/examples/C/grib_keys_iterator.c @@ -65,10 +65,6 @@ int main(int argc, char* argv[]) codes_keys_iterator* kiter = NULL; msg_count++; printf("-- GRIB N. %d --\n", msg_count); - if (!h) { - fprintf(stderr, "Error: Unable to create grib handle\n"); - exit(1); - } kiter = codes_keys_iterator_new(h, key_iterator_filter_flags, name_space); if (!kiter) { diff --git a/examples/C/grib_precipitation.c b/examples/C/grib_precipitation.c index 5e6297364..51214da9d 100644 --- a/examples/C/grib_precipitation.c +++ b/examples/C/grib_precipitation.c @@ -35,7 +35,6 @@ int main(int argc, char** argv) { int err = 0, i; double* values = NULL; - double max, min, average; size_t values_len = 0; FILE* in = NULL; diff --git a/examples/C/multi2.c b/examples/C/multi2.c index 058ec0ff7..c680cea58 100644 --- a/examples/C/multi2.c +++ b/examples/C/multi2.c @@ -35,7 +35,7 @@ static void read_data(FILE* fp, int num_msgs) long stepRange = 0; codes_handle* h = NULL; - printf("File offset start = %lu\n", ftello(fp)); + printf("File offset start = %ld\n", ftello(fp)); for (i = 0; i < num_msgs; ++i) { h = codes_handle_new_from_file(0, fp, PRODUCT_GRIB, &err); CODES_CHECK(err, 0); diff --git a/examples/C/new_sample.c b/examples/C/new_sample.c index cc08929d1..c89ed7c21 100644 --- a/examples/C/new_sample.c +++ b/examples/C/new_sample.c @@ -232,7 +232,7 @@ int main(int argc, char** argv) size = 496; vdouble = (double*)calloc(size, sizeof(double)); if (!vdouble) { - fprintf(stderr, "failed to allocate %lu bytes\n", size * sizeof(double)); + fprintf(stderr, "failed to allocate %lu bytes\n", (unsigned long)(size * sizeof(double))); exit(1); }