Skip to content

Commit

Permalink
GRIB message validity checker: Add product kind as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
shahramn committed Dec 14, 2024
1 parent c6caca4 commit 541d380
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
4 changes: 2 additions & 2 deletions definitions/boot.def
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ constant definitionFilesVersion="2.0.0.0" : hidden;
constant internalVersion=30 : hidden;
meta checkInternalVersion check_internal_version(internalVersion) : hidden;

meta isMessageValid message_is_valid() : hidden;

# ECC-806: Local concepts precedence order
preferLocalConceptsEnvVar = getenv("ECCODES_GRIB_PREFER_LOCAL_CONCEPTS","0") : hidden;
transient preferLocalConcepts = preferLocalConceptsEnvVar : hidden;
Expand Down Expand Up @@ -126,3 +124,5 @@ if(kindOfProduct == `WRAP`){
constant WRAPstr="WRAP";
alias ls.identifier=WRAPstr;
}

meta isMessageValid message_is_valid(kindOfProduct) : hidden;
16 changes: 13 additions & 3 deletions src/accessor/grib_accessor_class_message_is_valid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
grib_accessor_message_is_valid_t _grib_accessor_message_is_valid{};
grib_accessor* grib_accessor_message_is_valid = &_grib_accessor_message_is_valid;

void grib_accessor_message_is_valid_t::init(const long l, grib_arguments* c)
void grib_accessor_message_is_valid_t::init(const long l, grib_arguments* arg)
{
grib_accessor_long_t::init(l, c);
grib_accessor_long_t::init(l, arg);

grib_handle* h = grib_handle_of_accessor(this);
product_ = arg->get_name(h, 0);

flags_ |= GRIB_ACCESSOR_FLAG_READ_ONLY;
length_ = 0;
}
Expand Down Expand Up @@ -96,10 +100,16 @@ int grib_accessor_message_is_valid_t::unpack_long(long* val, size_t* len)
{
int ret = 0;
grib_handle* h = grib_handle_of_accessor(this);

*len = 1;
*val = 1; // Assume message is valid

if (h->product_kind != PRODUCT_GRIB) {
char product[32] = {0,};
size_t size = sizeof(product) / sizeof(*product);
ret = grib_get_string(h, product_, product, &size);
if (ret) return ret;

if (!STR_EQUAL(product, "GRIB")) {
grib_context_log(h->context, GRIB_LOG_ERROR, "Validity checks only implemented for GRIB messages");
*val = 0;
return GRIB_NOT_IMPLEMENTED;
Expand Down
3 changes: 3 additions & 0 deletions src/accessor/grib_accessor_class_message_is_valid.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ class grib_accessor_message_is_valid_t : public grib_accessor_long_t
grib_accessor* create_empty_accessor() override { return new grib_accessor_message_is_valid_t{}; }
int unpack_long(long* val, size_t* len) override;
void init(const long, grib_arguments*) override;

private:
const char* product_ = nullptr;
};
11 changes: 6 additions & 5 deletions tests/grib_check_message_validity.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ cat >$tempFilt<<EOF
assert ( isMessageValid == 0 );
write;
EOF
${tools_dir}/grib_filter -o $tempGrib $tempFilt $sample

${tools_dir}/grib_filter -o $tempGrib $tempFilt $sample 2>$tempText
grep -q "Invalid Ni" $tempText
grib_check_key_equals $tempGrib isMessageValid 0
grib_check_key_equals $sample isMessageValid 1

Expand All @@ -37,15 +37,16 @@ cat >$tempFilt<<EOF
assert ( isMessageValid == 0 );
write;
EOF
${tools_dir}/grib_filter -o $tempGrib $tempFilt $sample

${tools_dir}/grib_filter -o $tempGrib $tempFilt $sample 2>$tempText
grep -q "Invalid PL array" $tempText
grib_check_key_equals $tempGrib isMessageValid 0


# Check data values
# ------------------------------
${tools_dir}/grib_set -s bitsPerValue=25 $data_dir/sample.grib2 $tempGrib
grib_check_key_equals $tempGrib isMessageValid 0
grib_check_key_equals $tempGrib isMessageValid 0 2>$tempText
grep -q "Data section size mismatch" $tempText


# Clean up
Expand Down

0 comments on commit 541d380

Please sign in to comment.