From ef506078b533f9feeaa1da0d6630e2bc893658d8 Mon Sep 17 00:00:00 2001 From: shahramn Date: Mon, 23 Dec 2024 11:58:06 +0000 Subject: [PATCH] GRIB message validity checker: Sections --- .../grib_accessor_class_message_is_valid.cc | 23 +++++++++++++++++++ tests/grib_check_message_validity.sh | 17 ++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/accessor/grib_accessor_class_message_is_valid.cc b/src/accessor/grib_accessor_class_message_is_valid.cc index 9a0bdbea7..a937502e7 100644 --- a/src/accessor/grib_accessor_class_message_is_valid.cc +++ b/src/accessor/grib_accessor_class_message_is_valid.cc @@ -9,6 +9,7 @@ */ #include "grib_accessor_class_message_is_valid.h" +#include grib_accessor_message_is_valid_t _grib_accessor_message_is_valid{}; grib_accessor* grib_accessor_message_is_valid = &_grib_accessor_message_is_valid; @@ -103,8 +104,30 @@ static int check_geoiterator(grib_handle* h) return err; } +static int check_sections(grib_handle* h) +{ + int err = 0; + long edition = 0; + err = grib_get_long_internal(h, "edition", &edition); + if (err) return err; + if (edition == 2) { + // sections 3 thru to 8 should exist + // section 2 is optional + for (size_t i=3; i<9; ++i) { + char sname[16] = {0,}; + snprintf(sname, sizeof(sname), "section_%zu", i); + if (!grib_is_defined(h, sname)) { + grib_context_log(h->context, GRIB_LOG_ERROR, "Section %zu is missing!", i); + return GRIB_INVALID_MESSAGE; + } + } + } + return GRIB_SUCCESS; +} + typedef int (*proj_func)(grib_handle*); static proj_func check_functions[] = { + check_sections, check_field_values, check_grid_pl_array, check_geoiterator diff --git a/tests/grib_check_message_validity.sh b/tests/grib_check_message_validity.sh index 828ade584..d23df8d6c 100755 --- a/tests/grib_check_message_validity.sh +++ b/tests/grib_check_message_validity.sh @@ -14,8 +14,18 @@ tempGrib=temp.$label.grib tempText=temp.$label.txt tempFilt=temp.$label.filt -sample=$ECCODES_SAMPLES_PATH/reduced_gg_pl_32_grib2.tmpl -grib_check_key_equals $sample isMessageValid 1 +grib_check_key_equals $ECCODES_SAMPLES_PATH/reduced_gg_pl_32_grib2.tmpl isMessageValid 1 +grib_check_key_equals $ECCODES_SAMPLES_PATH/GRIB2.tmpl isMessageValid 1 + + +# Bad sections +# --------------- +# Correct key order: NV=6,PVPresent=1 +${tools_dir}/grib_set -s PVPresent=1,NV=6 $ECCODES_SAMPLES_PATH/reduced_gg_pl_128_grib2.tmpl $tempGrib +grib_check_key_equals $tempGrib isMessageValid 0 2>$tempText +grep -q "Section 5 is missing" $tempText +grep -q "Error instantiating iterator gaussian_reduced" $tempText + # Check regular lat/lon # ------------------------------ @@ -26,6 +36,7 @@ grep -q "Regular grid Geoiterator" $tempText # Check reduced Gaussian grid Ni # ------------------------------ +sample=$ECCODES_SAMPLES_PATH/reduced_gg_pl_32_grib2.tmpl cat >$tempFilt<$tempFilt<