From 4752afb09e63a0e0831d442c4822979c53c7c59c Mon Sep 17 00:00:00 2001 From: shahramn Date: Fri, 8 Mar 2024 12:07:15 +0000 Subject: [PATCH 01/13] ECC-1783: grib_to_netcdf: No error message when input file has invalid fields --- tools/grib_to_netcdf.cc | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tools/grib_to_netcdf.cc b/tools/grib_to_netcdf.cc index c1e09d7cc..0dad044ab 100644 --- a/tools/grib_to_netcdf.cc +++ b/tools/grib_to_netcdf.cc @@ -4225,7 +4225,15 @@ int grib_tool_new_filename_action(grib_runtime_options* options, const char* fil grib_handle_delete(h); } - grib_file_close(file->name, 0, &e); + if (e != GRIB_SUCCESS) { + grib_context_log(ctx, GRIB_LOG_ERROR, "%s (message %d)", grib_get_error_message(e), i); + } + + int e2 = 0; + grib_file_close(file->name, 0, &e2); + if (e2 != GRIB_SUCCESS) { + grib_context_log(ctx, GRIB_LOG_ERROR, "Failed to close file %s (%s)", file->name, grib_get_error_message(e2)); + } { /* Now do some checks */ From 73a147286f95b29fec0276fe6f6da3651d68836b Mon Sep 17 00:00:00 2001 From: shahramn Date: Fri, 8 Mar 2024 12:07:53 +0000 Subject: [PATCH 02/13] Examples: Correct arguments to perror --- examples/C/grib_clone.c | 2 +- examples/C/grib_precision.c | 2 +- examples/C/grib_set_keys.c | 2 +- examples/C/grib_set_missing.c | 2 +- examples/C/grib_set_pv.c | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/C/grib_clone.c b/examples/C/grib_clone.c index d54ab4440..c2964b54a 100644 --- a/examples/C/grib_clone.c +++ b/examples/C/grib_clone.c @@ -68,7 +68,7 @@ int main(int argc, char* argv[]) CODES_CHECK(codes_get_message(clone_handle, &buffer, &size), 0); /* write the buffer to a file */ if (fwrite(buffer, 1, size, out) != size) { - perror(argv[1]); + perror(argv[2]); return 1; } codes_handle_delete(clone_handle); diff --git a/examples/C/grib_precision.c b/examples/C/grib_precision.c index ae39318b2..4f3657610 100644 --- a/examples/C/grib_precision.c +++ b/examples/C/grib_precision.c @@ -116,7 +116,7 @@ int main(int argc, char** argv) /* write the buffer in a file*/ if (fwrite(buffer, 1, size, out) != size) { - perror(argv[1]); + perror(outfile); exit(1); } diff --git a/examples/C/grib_set_keys.c b/examples/C/grib_set_keys.c index feea6731a..50aeff372 100644 --- a/examples/C/grib_set_keys.c +++ b/examples/C/grib_set_keys.c @@ -78,7 +78,7 @@ int main(int argc, char** argv) /* write the buffer in a file*/ if (fwrite(buffer, 1, size, out) != size) { - perror(argv[1]); + perror(outfile); exit(1); } diff --git a/examples/C/grib_set_missing.c b/examples/C/grib_set_missing.c index 82f698975..98395c120 100644 --- a/examples/C/grib_set_missing.c +++ b/examples/C/grib_set_missing.c @@ -77,7 +77,7 @@ int main(int argc, char** argv) /* write the buffer in a file*/ if (fwrite(buffer, 1, size, out) != size) { - perror(argv[1]); + perror(outfile); exit(1); } diff --git a/examples/C/grib_set_pv.c b/examples/C/grib_set_pv.c index 7ca4c32fe..e279aec8b 100644 --- a/examples/C/grib_set_pv.c +++ b/examples/C/grib_set_pv.c @@ -78,7 +78,7 @@ int main(int argc, char** argv) /* write the buffer in a file*/ if (fwrite(buffer, 1, size, out) != size) { - perror(argv[1]); + perror(outfile); exit(1); } From 626aee752d82df41563bc6d064501d63205056e8 Mon Sep 17 00:00:00 2001 From: shahramn Date: Fri, 8 Mar 2024 12:08:39 +0000 Subject: [PATCH 03/13] Refactoring: removal of duplicated code (dump) --- src/grib_accessor_class_unexpanded_descriptors.cc | 10 ++-------- src/grib_accessor_class_unsigned_bits.cc | 10 ++-------- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/src/grib_accessor_class_unexpanded_descriptors.cc b/src/grib_accessor_class_unexpanded_descriptors.cc index b583ce5cd..a026388f2 100644 --- a/src/grib_accessor_class_unexpanded_descriptors.cc +++ b/src/grib_accessor_class_unexpanded_descriptors.cc @@ -16,7 +16,7 @@ CLASS = accessor SUPER = grib_accessor_class_long IMPLEMENTS = unpack_long;pack_long - IMPLEMENTS = init;dump + IMPLEMENTS = init IMPLEMENTS = next_offset IMPLEMENTS = value_count IMPLEMENTS = byte_offset @@ -43,7 +43,6 @@ static int unpack_long(grib_accessor*, long* val, size_t* len); static long byte_offset(grib_accessor*); static long next_offset(grib_accessor*); static int value_count(grib_accessor*, long*); -static void dump(grib_accessor*, grib_dumper*); static void init(grib_accessor*, const long, grib_arguments*); static void update_size(grib_accessor*, size_t); @@ -68,7 +67,7 @@ static grib_accessor_class _grib_accessor_class_unexpanded_descriptors = { &init, /* init */ 0, /* post_init */ 0, /* destroy */ - &dump, /* dump */ + 0, /* dump */ &next_offset, /* next_offset */ 0, /* get length of string */ &value_count, /* get number of values */ @@ -122,11 +121,6 @@ static void init(grib_accessor* a, const long len, grib_arguments* args) a->length = 0; } -static void dump(grib_accessor* a, grib_dumper* dumper) -{ - grib_dump_long(dumper, a, NULL); -} - static int unpack_long(grib_accessor* a, long* val, size_t* len) { grib_accessor_unexpanded_descriptors* self = (grib_accessor_unexpanded_descriptors*)a; diff --git a/src/grib_accessor_class_unsigned_bits.cc b/src/grib_accessor_class_unsigned_bits.cc index 026d4ff67..53824a994 100644 --- a/src/grib_accessor_class_unsigned_bits.cc +++ b/src/grib_accessor_class_unsigned_bits.cc @@ -16,7 +16,7 @@ CLASS = accessor SUPER = grib_accessor_class_long IMPLEMENTS = unpack_long;pack_long - IMPLEMENTS = init;dump + IMPLEMENTS = init IMPLEMENTS = next_offset IMPLEMENTS = byte_count IMPLEMENTS = value_count @@ -45,7 +45,6 @@ static long byte_count(grib_accessor*); static long byte_offset(grib_accessor*); static long next_offset(grib_accessor*); static int value_count(grib_accessor*, long*); -static void dump(grib_accessor*, grib_dumper*); static void init(grib_accessor*, const long, grib_arguments*); static void update_size(grib_accessor*, size_t); @@ -70,7 +69,7 @@ static grib_accessor_class _grib_accessor_class_unsigned_bits = { &init, /* init */ 0, /* post_init */ 0, /* destroy */ - &dump, /* dump */ + 0, /* dump */ &next_offset, /* next_offset */ 0, /* get length of string */ &value_count, /* get number of values */ @@ -149,11 +148,6 @@ static void init(grib_accessor* a, const long len, grib_arguments* args) a->length = compute_byte_count(a); } -static void dump(grib_accessor* a, grib_dumper* dumper) -{ - grib_dump_long(dumper, a, NULL); -} - static int unpack_long(grib_accessor* a, long* val, size_t* len) { grib_accessor_unsigned_bits* self = (grib_accessor_unsigned_bits*)a; From b4aa46878cc35cdbfffd1a22ff08f1488962da27 Mon Sep 17 00:00:00 2001 From: shahramn Date: Fri, 8 Mar 2024 12:21:39 +0000 Subject: [PATCH 04/13] ECC-1783: Test --- tests/grib_to_netcdf.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/grib_to_netcdf.sh b/tests/grib_to_netcdf.sh index c07dc89e9..f13d8475b 100755 --- a/tests/grib_to_netcdf.sh +++ b/tests/grib_to_netcdf.sh @@ -148,7 +148,9 @@ set -e [ $status -ne 0 ] grep -q "Wrong number of fields" $tempText -# Not regular grid + +echo "Not a regular grid ..." +# -------------------------- input=${data_dir}/reduced_gaussian_pressure_level.grib2 set +e ${tools_dir}/grib_to_netcdf -o $tempNetcdf $input > $tempText 2>&1 @@ -158,6 +160,17 @@ set -e grep -q "not on a regular lat/lon grid or on a regular Gaussian grid" $tempText +# ECC-1783: No error message when input file has invalid fields +input=$data_dir/bad.grib +set +e +${tools_dir}/grib_to_netcdf -o $tempNetcdf $input > $tempText 2>&1 +status=$? +set -e +[ $status -ne 0 ] +grep -q "Wrong message length" $tempText + + +# Validity time check export GRIB_TO_NETCDF_CHECKVALIDTIME=0 ${tools_dir}/grib_to_netcdf -o $tempNetcdf $tempGrib [ -f "$tempNetcdf" ] From 8dc64c35c6b13b168bfc2855fa3e2b05922f7981 Mon Sep 17 00:00:00 2001 From: shahramn Date: Fri, 8 Mar 2024 15:13:17 +0000 Subject: [PATCH 05/13] Testing: BUFR headers and key comparisons --- tests/bufr_extract_headers.sh | 34 ++++++++++++++++++++++++++++++++++ tests/codes_compare_keys.cc | 2 ++ tests/codes_compare_keys.sh | 2 ++ 3 files changed, 38 insertions(+) diff --git a/tests/bufr_extract_headers.sh b/tests/bufr_extract_headers.sh index a20602391..0d518d732 100755 --- a/tests/bufr_extract_headers.sh +++ b/tests/bufr_extract_headers.sh @@ -199,4 +199,38 @@ status=$? set -e [ $status -ne 0 ] + +echo "Test with nonexistent file..." +# --------------------------------- +set +e +$EXEC ${test_dir}/bufr_extract_headers centre nosuchfile > $temp1 2>&1 +status=$? +set -e +[ $status -ne 0 ] +grep -q "Unable to read file" $temp1 + + +echo "Test with bad BUFR file..." +# --------------------------------- +echo BUFR > $temp1 +set +e +$EXEC ${test_dir}/bufr_extract_headers centre $temp1 > $temp2 2>&1 +status=$? +set -e +[ $status -ne 0 ] +grep -q "Unable to count BUFR messages" $temp2 + + +echo "Test with GRIB file..." +# --------------------------------- +input=${data_dir}/sample.grib2 +set +e +$EXEC ${test_dir}/bufr_extract_headers centre $input > $temp2 2>&1 +status=$? +set -e +[ $status -ne 0 ] +grep -q "No BUFR messages in file" $temp2 + + +# Clean up rm -f $temp1 $temp2 diff --git a/tests/codes_compare_keys.cc b/tests/codes_compare_keys.cc index 116998de7..2b930ea11 100644 --- a/tests/codes_compare_keys.cc +++ b/tests/codes_compare_keys.cc @@ -54,6 +54,8 @@ int main(int argc, char* argv[]) codes_compare_key(h1, h2, "computeStatistics", 0); // statistics codes_compare_key(h1, h2, "paramId", 0); // concept codes_compare_key(h1, h2, "identifier", 0); // ascii + err = codes_compare_key(h1, h2, "abcdefghij", 0); // no such key + Assert(err == GRIB_NOT_FOUND); if (list_provided_keys) { for (i = 0; list_provided_keys[i] != NULL; ++i) { diff --git a/tests/codes_compare_keys.sh b/tests/codes_compare_keys.sh index 1a678ede8..b07e6edc0 100755 --- a/tests/codes_compare_keys.sh +++ b/tests/codes_compare_keys.sh @@ -37,6 +37,7 @@ key: hoursAfterDataCutoff (Long values are different) key: minutesAfterDataCutoff (Long values are different) key: numberOfValues (Long values are different) key: referenceValue (Double values are different) +ECCODES ERROR : Key abcdefghij not found in first message Comparison failed: 13 differences EOF @@ -57,6 +58,7 @@ cat $tempLog cat > $tempRef < Date: Fri, 8 Mar 2024 16:05:13 +0000 Subject: [PATCH 06/13] Testing: Dump output --- tests/grib_ccsds.sh | 5 +++++ tests/grib_ecc-1212.sh | 2 ++ 2 files changed, 7 insertions(+) diff --git a/tests/grib_ccsds.sh b/tests/grib_ccsds.sh index bb37f060c..13533ab90 100755 --- a/tests/grib_ccsds.sh +++ b/tests/grib_ccsds.sh @@ -22,6 +22,11 @@ logfile=temp.$label.log rm -f $outfile1 $outfile2 +# Debug info +ECCODES_DEBUG=-1 ${tools_dir}/grib_copy -r $infile $outfile1 > $logfile 2>&1 +grep -q "ECCODES DEBUG CCSDS .* aec_stream.flags" $logfile + + # Convert a non-constant grib2 file (Note: not using -r) grib2_sample=$ECCODES_SAMPLES_PATH/gg_sfc_grib2.tmpl ${tools_dir}/grib_set -s packingType=grid_ccsds $grib2_sample $outfile1 diff --git a/tests/grib_ecc-1212.sh b/tests/grib_ecc-1212.sh index e1f32e596..7ff7c2ead 100755 --- a/tests/grib_ecc-1212.sh +++ b/tests/grib_ecc-1212.sh @@ -112,6 +112,8 @@ ${tools_dir}/grib_filter -o $tempGrib $tempFilt $sample_grib2 grib_check_key_equals $tempGrib selectedFcIndex,step '1 4' grib_check_key_equals $tempGrib mars.date,mars.time '20220607 300' +${tools_dir}/grib_dump -Dat $tempGrib > $tempOut + for pdtn in 88 92 93 94 95 96 97 98; do ${tools_dir}/grib_set -s \ tablesVersion=30,productDefinitionTemplateNumber=$pdtn,numberOfForecastsUsedInLocalTime=1 \ From 7b0a7798902d887af63b2947a13acd4baac5f552 Mon Sep 17 00:00:00 2001 From: shahramn Date: Fri, 8 Mar 2024 17:55:13 +0000 Subject: [PATCH 07/13] Testing: subarray unpack --- src/grib_accessor_class_bit.cc | 29 +++++++++++++---------------- src/grib_accessor_class_bitmap.cc | 30 ++++++++++++------------------ tests/grib_unpack_subarray.sh | 3 +++ 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/grib_accessor_class_bit.cc b/src/grib_accessor_class_bit.cc index e3099d259..ab81eecda 100644 --- a/src/grib_accessor_class_bit.cc +++ b/src/grib_accessor_class_bit.cc @@ -106,22 +106,20 @@ grib_accessor_class* grib_accessor_class_bit = &_grib_accessor_class_bit; static void init(grib_accessor* a, const long len, grib_arguments* arg) { grib_accessor_bit* self = (grib_accessor_bit*)a; - a->length = 0; - self->owner = grib_arguments_get_name(grib_handle_of_accessor(a), arg, 0); - self->bit_index = grib_arguments_get_long(grib_handle_of_accessor(a), arg, 1); + a->length = 0; + self->owner = grib_arguments_get_name(grib_handle_of_accessor(a), arg, 0); + self->bit_index = grib_arguments_get_long(grib_handle_of_accessor(a), arg, 1); } - static int unpack_long(grib_accessor* a, long* val, size_t* len) { grib_accessor_bit* self = (grib_accessor_bit*)a; - int ret = 0; - + int ret = 0; long data = 0; if (*len < 1) { - grib_context_log(a->context, GRIB_LOG_ERROR, "grib_accessor_bit : unpack_long : Wrong size for %s it contains %d values ", a->name, 1); - *len = 0; + grib_context_log(a->context, GRIB_LOG_ERROR, "grib_accessor_bit: unpack_long: Wrong size for %s, it contains %d values ", a->name, 1); + *len = 1; return GRIB_ARRAY_TOO_SMALL; } @@ -142,23 +140,22 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len) static int pack_long(grib_accessor* a, const long* val, size_t* len) { grib_accessor_bit* self = (grib_accessor_bit*)a; - grib_accessor* owner = NULL; - unsigned char* mdata = 0; + if (*len < 1) { - grib_context_log(a->context, GRIB_LOG_ERROR, "grib_accessor_bit : pack_long : At least one value to pack for %s", a->name); - *len = 0; + grib_context_log(a->context, GRIB_LOG_ERROR, "grib_accessor_bit: pack_long: At least one value to pack for %s", a->name); + *len = 1; return GRIB_ARRAY_TOO_SMALL; } - owner = grib_find_accessor(grib_handle_of_accessor(a), self->owner); - + grib_accessor* owner = grib_find_accessor(grib_handle_of_accessor(a), self->owner); if (!owner) { - grib_context_log(a->context, GRIB_LOG_ERROR, "grib_accessor_bit : Cannot get the owner %s for computing the bit value of %s ", self->owner, a->name); + grib_context_log(a->context, GRIB_LOG_ERROR, "grib_accessor_bit: Cannot get the owner %s for computing the bit value of %s", + self->owner, a->name); *len = 0; return GRIB_NOT_FOUND; } - mdata = grib_handle_of_accessor(a)->buffer->data; + unsigned char* mdata = grib_handle_of_accessor(a)->buffer->data; mdata += grib_byte_offset(owner); /* Note: In the definitions, flagbit numbers go from 7 to 0 (the bit_index), while WMO convention is from 1 to 8 */ diff --git a/src/grib_accessor_class_bitmap.cc b/src/grib_accessor_class_bitmap.cc index bd52d16cd..b86b34332 100644 --- a/src/grib_accessor_class_bitmap.cc +++ b/src/grib_accessor_class_bitmap.cc @@ -161,8 +161,8 @@ static void compute_size(grib_accessor* a) static void init(grib_accessor* a, const long len, grib_arguments* arg) { grib_accessor_bitmap* self = (grib_accessor_bitmap*)a; - grib_handle* hand = grib_handle_of_accessor(a); - int n = 0; + grib_handle* hand = grib_handle_of_accessor(a); + int n = 0; self->tableReference = grib_arguments_get_name(hand, arg, n++); self->missing_value = grib_arguments_get_name(hand, arg, n++); @@ -190,23 +190,21 @@ static void dump(grib_accessor* a, grib_dumper* dumper) static int unpack_long(grib_accessor* a, long* val, size_t* len) { - long pos = a->offset * 8; - long tlen = 0; - long i = 0; - int err = 0; + long pos = a->offset * 8; + long tlen = 0; const grib_handle* hand = grib_handle_of_accessor(a); - err = grib_value_count(a, &tlen); + int err = grib_value_count(a, &tlen); if (err) return err; if (*len < tlen) { grib_context_log(a->context, GRIB_LOG_ERROR, "Wrong size for %s, it contains %ld values", a->name, tlen); - *len = 0; + *len = tlen; return GRIB_ARRAY_TOO_SMALL; } - for (i = 0; i < tlen; i++) { + for (long i = 0; i < tlen; i++) { val[i] = (long)grib_decode_unsigned_long(hand->buffer->data, &pos, 1); } *len = tlen; @@ -219,21 +217,19 @@ static int unpack(grib_accessor* a, T* val, size_t* len) static_assert(std::is_floating_point::value, "Requires floating points numbers"); long pos = a->offset * 8; long tlen; - long i; - int err = 0; grib_handle* hand = grib_handle_of_accessor(a); - err = grib_value_count(a, &tlen); + int err = grib_value_count(a, &tlen); if (err) return err; if (*len < tlen) { grib_context_log(a->context, GRIB_LOG_ERROR, "Wrong size for %s, it contains %ld values", a->name, tlen); - *len = 0; + *len = tlen; return GRIB_ARRAY_TOO_SMALL; } - for (i = 0; i < tlen; i++) { + for (long i = 0; i < tlen; i++) { val[i] = (T)grib_decode_unsigned_long(hand->buffer->data, &pos, 1); } *len = tlen; @@ -261,8 +257,7 @@ static int unpack_double_element(grib_accessor* a, size_t idx, double* val) } static int unpack_double_element_set(grib_accessor* a, const size_t* index_array, size_t len, double* val_array) { - size_t i = 0; - for (i=0; ilength; @@ -293,7 +287,7 @@ static int unpack_string(grib_accessor* a, char* val, size_t* len) return GRIB_BUFFER_TOO_SMALL; } - for (i = 0; i < a->length; i++) { + for (long i = 0; i < a->length; i++) { val[i] = hand->buffer->data[a->offset + i]; } diff --git a/tests/grib_unpack_subarray.sh b/tests/grib_unpack_subarray.sh index 43bc730d8..3ac3b2530 100755 --- a/tests/grib_unpack_subarray.sh +++ b/tests/grib_unpack_subarray.sh @@ -13,6 +13,9 @@ label="grib_unpack_subarray_test" temp=${label}".grib.tmp" +infile=$data_dir/constant_field.grib2 +$EXEC ${test_dir}/grib_unpack_subarray $infile + infile=$data_dir/sample.grib2 $EXEC ${test_dir}/grib_unpack_subarray $infile From 87d14a89b80ed2986350a9a1482c315e665c27a6 Mon Sep 17 00:00:00 2001 From: shahramn Date: Fri, 8 Mar 2024 21:15:31 +0000 Subject: [PATCH 08/13] Tools: Check overflow/underflow (Fix) --- src/grib_util.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/grib_util.cc b/src/grib_util.cc index d2c1af775..5a4241fef 100644 --- a/src/grib_util.cc +++ b/src/grib_util.cc @@ -1749,8 +1749,7 @@ static void set_value(grib_values* value, char* str, int equal) value->long_value = strtol(buf, &p, 10); if (*p == 0) { // check the conversion from string to long - if ((errno == ERANGE && (value->long_value == LONG_MAX || value->long_value == LONG_MIN)) || - (errno != 0 && value->long_value == 0)) { + if (errno == ERANGE && (value->long_value == LONG_MAX || value->long_value == LONG_MIN)) { fprintf(stderr, "ECCODES WARNING : Setting %s=%s causes overflow/underflow\n", value->name, buf); fprintf(stderr, "ECCODES WARNING : Value adjusted to %ld\n", value->long_value); //perror("strtol"); From 85daceab7f02c2d974fc780d597d8fe2b6603201 Mon Sep 17 00:00:00 2001 From: shahramn Date: Fri, 8 Mar 2024 21:15:54 +0000 Subject: [PATCH 09/13] Testing: GRIB2 product def templates --- tests/grib2_templates.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/grib2_templates.sh b/tests/grib2_templates.sh index 024dd26dd..64b43e4ad 100755 --- a/tests/grib2_templates.sh +++ b/tests/grib2_templates.sh @@ -195,6 +195,46 @@ grib_check_key_equals $temp isTemplateDeprecated,isTemplateExperimental '0 1' $tools_dir/grib_set -s gridType=time_section $sample2 $temp grib_check_key_equals $temp isTemplateDeprecated,isTemplateExperimental '0 1' +# Use of eps key (for local section) +# ----------------------------------- +input=$ECCODES_SAMPLES_PATH/reduced_gg_pl_32_grib2.tmpl +$tools_dir/grib_set -s stepType=accum,eps=1 $input $temp +grib_check_key_equals $temp productDefinitionTemplateNumber '11' + +#17 Ensemble mean +#18 Ensemble standard deviation +$tools_dir/grib_set -s type=17,stepType=accum,eps=1 $input $temp +grib_check_key_equals $temp productDefinitionTemplateNumber,derivedForecast '12 0' + +$tools_dir/grib_set -s type=18,stepType=accum,eps=1 $input $temp +grib_check_key_equals $temp productDefinitionTemplateNumber,derivedForecast '12 4' + +# Chemicals +$tools_dir/grib_set -s paramId=217019,stepType=instant,eps=0 $input $temp +grib_check_key_equals $temp productDefinitionTemplateNumber '40' + +$tools_dir/grib_set -s paramId=217019,stepType=instant,eps=1 $input $temp +grib_check_key_equals $temp productDefinitionTemplateNumber '41' + +$tools_dir/grib_set -s paramId=217019,stepType=accum,eps=0 $input $temp +grib_check_key_equals $temp productDefinitionTemplateNumber '42' + +$tools_dir/grib_set -s paramId=217019,stepType=accum,eps=1 $input $temp +grib_check_key_equals $temp productDefinitionTemplateNumber '43' + +# Aerosol 210072 +$tools_dir/grib_set -s paramId=210072,stepType=instant,eps=0 $input $temp +grib_check_key_equals $temp productDefinitionTemplateNumber '48' + +$tools_dir/grib_set -s paramId=210072,stepType=instant,eps=1 $input $temp +grib_check_key_equals $temp productDefinitionTemplateNumber '45' + +#$tools_dir/grib_set -s paramId=210072,stepType=accum,eps=0 $input $temp +#grib_check_key_equals $temp productDefinitionTemplateNumber '42' + +#$tools_dir/grib_set -s paramId=210072,stepType=accum,eps=1 $input $temp +#grib_check_key_equals $temp productDefinitionTemplateNumber '43' + # Clean up rm -f $temp $temp1 $temp2 $tempFilt $tempText From 6f710bdcbf4cdf325caa9fab2b8e5be01be244c0 Mon Sep 17 00:00:00 2001 From: shahramn Date: Sat, 9 Mar 2024 12:47:54 +0000 Subject: [PATCH 10/13] Must clear errno before calling strtol --- src/grib_util.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/grib_util.cc b/src/grib_util.cc index 5a4241fef..546fb726d 100644 --- a/src/grib_util.cc +++ b/src/grib_util.cc @@ -1723,6 +1723,7 @@ static void set_value(grib_values* value, char* str, int equal) } break; case GRIB_TYPE_LONG: + errno = 0; // must clear errno before calling strtol value->long_value = strtol(buf, &p, 10); if (*p != 0) value->has_value = 1; @@ -1746,6 +1747,7 @@ static void set_value(grib_values* value, char* str, int equal) } break; case GRIB_TYPE_UNDEFINED: + errno = 0; // must clear errno before calling strtol value->long_value = strtol(buf, &p, 10); if (*p == 0) { // check the conversion from string to long From 9cda220036bddc6ec61369ef98b16f45b82e9067 Mon Sep 17 00:00:00 2001 From: shahramn Date: Sat, 9 Mar 2024 18:50:28 +0000 Subject: [PATCH 11/13] Accessors: Return correct array length --- src/grib_accessor_class_codetable.cc | 4 ++-- src/grib_accessor_class_spd.cc | 4 ++-- src/grib_accessor_class_unsigned_bits.cc | 2 +- src/grib_accessor_class_variable.cc | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/grib_accessor_class_codetable.cc b/src/grib_accessor_class_codetable.cc index 0d97efdfb..9e39ed633 100644 --- a/src/grib_accessor_class_codetable.cc +++ b/src/grib_accessor_class_codetable.cc @@ -908,9 +908,9 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len) } if (*len < rlen) { - grib_context_log(a->context, GRIB_LOG_ERROR, "Wrong size (%lu) for %s, it contains %ld values", + grib_context_log(a->context, GRIB_LOG_ERROR, "Wrong size (%zu) for %s, it contains %ld values", *len, a->name, rlen); - *len = 0; + *len = rlen; return GRIB_ARRAY_TOO_SMALL; } diff --git a/src/grib_accessor_class_spd.cc b/src/grib_accessor_class_spd.cc index 734c0bc89..e0f4a20a7 100644 --- a/src/grib_accessor_class_spd.cc +++ b/src/grib_accessor_class_spd.cc @@ -167,8 +167,8 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len) if (*len < rlen) { grib_context_log(a->context, GRIB_LOG_ERROR, - "Wrong size (%ld) for %s it contains %ld values", *len, a->name, rlen); - *len = 0; + "Wrong size (%zu) for %s, it contains %ld values", *len, a->name, rlen); + *len = rlen; return GRIB_ARRAY_TOO_SMALL; } diff --git a/src/grib_accessor_class_unsigned_bits.cc b/src/grib_accessor_class_unsigned_bits.cc index 53824a994..38c3425cf 100644 --- a/src/grib_accessor_class_unsigned_bits.cc +++ b/src/grib_accessor_class_unsigned_bits.cc @@ -163,7 +163,7 @@ static int unpack_long(grib_accessor* a, long* val, size_t* len) if (*len < rlen) { grib_context_log(a->context, GRIB_LOG_ERROR, "Wrong size (%ld) for %s, it contains %ld values", *len, a->name, rlen); - *len = 0; + *len = rlen; return GRIB_ARRAY_TOO_SMALL; } diff --git a/src/grib_accessor_class_variable.cc b/src/grib_accessor_class_variable.cc index 1eb04f897..8c5bbe7b0 100644 --- a/src/grib_accessor_class_variable.cc +++ b/src/grib_accessor_class_variable.cc @@ -208,7 +208,7 @@ static int pack_double(grib_accessor* a, const double* val, size_t* len) const double dval = *val; if (*len != 1) { - grib_context_log(a->context, GRIB_LOG_ERROR, "Wrong size for %s, it contains %d values", a->name, 1); + grib_context_log(a->context, GRIB_LOG_ERROR, "Wrong size for %s, it contains 1 value", a->name); *len = 1; return GRIB_ARRAY_TOO_SMALL; } @@ -233,7 +233,7 @@ static int pack_float(grib_accessor* a, const float* val, size_t* len) const double fval = *val; if (*len != 1) { - grib_context_log(a->context, GRIB_LOG_ERROR, "Wrong size for %s, it contains %d values", a->name, 1); + grib_context_log(a->context, GRIB_LOG_ERROR, "Wrong size for %s, it contains 1 value", a->name); *len = 1; return GRIB_ARRAY_TOO_SMALL; } @@ -252,7 +252,7 @@ static int pack_long(grib_accessor* a, const long* val, size_t* len) grib_accessor_variable* self = (grib_accessor_variable*)a; if (*len != 1) { - grib_context_log(a->context, GRIB_LOG_ERROR, "Wrong size for %s it contains %d values ", a->name, 1); + grib_context_log(a->context, GRIB_LOG_ERROR, "Wrong size for %s it contains 1 value", a->name); *len = 1; return GRIB_ARRAY_TOO_SMALL; } From 9c5df40768641bf48e0e4c24e30e8563a1f46fab Mon Sep 17 00:00:00 2001 From: shahramn Date: Sat, 9 Mar 2024 19:47:41 +0000 Subject: [PATCH 12/13] Testing: bitsPerValue --- tests/grib_bpv_limit.cc | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/tests/grib_bpv_limit.cc b/tests/grib_bpv_limit.cc index 97be2e39c..896e0cff7 100644 --- a/tests/grib_bpv_limit.cc +++ b/tests/grib_bpv_limit.cc @@ -34,28 +34,24 @@ static int check_error_code(int err) int main(int argc, char** argv) { - int err = 0, i; + int err = 0, i; size_t values_len = 0; - double* values = NULL; - char error_msg[100]; const double tolerance = 1e-5; - size_t slong = sizeof(long) * 8; - grib_handle* h; - char* filename; + size_t slong = sizeof(long) * 8; Assert(argc == 2); - filename = argv[1]; + char* filename = argv[1]; for (i = 0; i < 255; i++) { FILE* in = fopen(filename, "rb"); Assert(in); - h = grib_handle_new_from_file(0, in, &err); + grib_handle* h = grib_handle_new_from_file(0, in, &err); Assert(h); /* get the size of the values array*/ GRIB_CHECK(grib_get_size(h, "values", &values_len), 0); - values = (double*)malloc(values_len * sizeof(double)); + double* values = (double*)malloc(values_len * sizeof(double)); err = grib_get_double_array(h, "values", values, &values_len); if (compare_double_absolute(values[0], 2.7900000000e+02, tolerance) != 0) @@ -82,9 +78,8 @@ int main(int argc, char** argv) /* do nothing */ } else { - snprintf(error_msg, sizeof(error_msg), "Error decoding when bpv=%d. Error message:%s", i, grib_get_error_message(err)); - perror(error_msg); - exit(1); + fprintf(stderr, "Error decoding when bpv=%d. Error message:%s\n", i, grib_get_error_message(err)); + return 1; } values = (double*)malloc(values_len * sizeof(double)); @@ -102,9 +97,8 @@ int main(int argc, char** argv) /* do nothing */ } else { - snprintf(error_msg, sizeof(error_msg), "Error decoding when bpv=%d. Error message:%s", i, grib_get_error_message(err)); - perror(error_msg); - exit(1); + fprintf(stderr, "Error decoding when bpv=%d. Error message:%s\n", i, grib_get_error_message(err)); + return 1; } free(values); From 9ce9b1f41c399636ac08b81c87e1880ec24afc60 Mon Sep 17 00:00:00 2001 From: shahramn Date: Sat, 9 Mar 2024 19:55:22 +0000 Subject: [PATCH 13/13] Dead code removal --- src/eccodes_prototypes.h | 1 - src/grib_filepool.cc | 8 ++++---- tests/bufr_ecc-517.cc | 22 +++++++--------------- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/eccodes_prototypes.h b/src/eccodes_prototypes.h index abd512a9f..6ae285fd2 100644 --- a/src/eccodes_prototypes.h +++ b/src/eccodes_prototypes.h @@ -918,7 +918,6 @@ grib_handle* grib_fieldset_retrieve(grib_fieldset* set, int i, int* err); /* grib_filepool.cc*/ void grib_file_pool_clean(void); -grib_file* grib_file_pool_get_files(void); grib_file* grib_file_open(const char* filename, const char* mode, int* err); void grib_file_pool_delete_file(grib_file* file); void grib_file_close(const char* filename, int force, int* err); diff --git a/src/grib_filepool.cc b/src/grib_filepool.cc index 8d55f3d3d..28606e8c9 100644 --- a/src/grib_filepool.cc +++ b/src/grib_filepool.cc @@ -132,10 +132,10 @@ void grib_file_pool_clean() // return grib_write_file(fh, file->next); // } -grib_file* grib_file_pool_get_files() -{ - return file_pool.first; -} +// grib_file* grib_file_pool_get_files() +// { +// return file_pool.first; +// } // int grib_file_pool_read(grib_context* c, FILE* fh) // { diff --git a/tests/bufr_ecc-517.cc b/tests/bufr_ecc-517.cc index 37c538fde..2ad8d7ab4 100644 --- a/tests/bufr_ecc-517.cc +++ b/tests/bufr_ecc-517.cc @@ -20,10 +20,9 @@ int main(int argc, char** argv) codes_handle* h = NULL; long* ivalues = NULL; const char* sampleName = "BUFR3_local"; - char* outfilename = NULL; assert(argc == 2); - outfilename = argv[1]; + char* outfilename = argv[1]; // Test non-existent sample file. Should fail h = codes_bufr_handle_new_from_samples(NULL, "some rubbish"); @@ -33,10 +32,8 @@ int main(int argc, char** argv) assert(h); ivalues = (long*)malloc(1 * sizeof(long)); - if (!ivalues) { - fprintf(stderr, "Failed to allocate memory (ivalues).\n"); - return 1; - } + assert(ivalues); + size = 1; ivalues[0] = 1; CODES_CHECK(codes_set_long_array(h, "inputDelayedDescriptorReplicationFactor", ivalues, size), 0); @@ -89,10 +86,8 @@ int main(int argc, char** argv) free(ivalues); ivalues = (long*)malloc(9 * sizeof(long)); - if (!ivalues) { - fprintf(stderr, "Failed to allocate memory (ivalues).\n"); - return 1; - } + assert(ivalues); + size = 4; ivalues[0] = 309052; ivalues[1] = 5001; @@ -104,11 +99,8 @@ int main(int argc, char** argv) CODES_CHECK(codes_set_long(h, "pack", 1), 0); fout = fopen(outfilename, "wb"); - if (!fout) { - fprintf(stderr, "Failed to open (create) output file.\n"); - free(ivalues); - return 1; - } + assert(fout); + CODES_CHECK(codes_get_message(h, &buffer, &size), 0); CODES_CHECK(codes_check_message_header(buffer, size, PRODUCT_BUFR), 0); CODES_CHECK(codes_check_message_footer(buffer, size, PRODUCT_BUFR), 0);