Skip to content

Commit

Permalink
GRIB message validity checker: Parameter info
Browse files Browse the repository at this point in the history
  • Loading branch information
shahramn committed Feb 1, 2025
1 parent d1bf0e9 commit 9341df8
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
49 changes: 47 additions & 2 deletions src/accessor/grib_accessor_class_message_is_valid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
grib_accessor_message_is_valid_t _grib_accessor_message_is_valid{};
grib_accessor* grib_accessor_message_is_valid = &_grib_accessor_message_is_valid;

static const char* TITLE = "Message Validity Checks";
static const char* TITLE = "Message validity checks";

void grib_accessor_message_is_valid_t::init(const long l, grib_arguments* arg)
{
Expand Down Expand Up @@ -130,6 +130,7 @@ int grib_accessor_message_is_valid_t::check_7777()

int grib_accessor_message_is_valid_t::check_surface_keys()
{
// printf("DEBUG %s \n", __func__);
int err = 0;
const grib_context* c = handle_->context;
if (edition_ != 2) return GRIB_SUCCESS;
Expand All @@ -149,6 +150,17 @@ int grib_accessor_message_is_valid_t::check_surface_keys()
grib_context_log(c, GRIB_LOG_ERROR, "%s: First fixed surface: If the scale factor is missing so should the scaled value and vice versa", TITLE);
return GRIB_INVALID_KEY_VALUE;
}
if (stype != 255) {
int required_val = codes_grib_surface_type_requires_value(edition_, stype, &err);
if (err) return err;
if (required_val && (sfac_missing || sval_missing)) {
char name[128] = {0,};
size_t len = 128;
grib_get_string(handle_, "nameOfFirstFixedSurface", name, &len);
grib_context_log(c, GRIB_LOG_ERROR, "%s: First fixed surface: Type %ld (%s) requires a level", TITLE, stype, name);
return GRIB_INVALID_KEY_VALUE;
}
}

grib_get_long_internal(handle_, "typeOfSecondFixedSurface", &stype);
sfac_missing = grib_is_missing(handle_, "scaleFactorOfSecondFixedSurface", &err);
Expand All @@ -161,6 +173,17 @@ int grib_accessor_message_is_valid_t::check_surface_keys()
grib_context_log(c, GRIB_LOG_ERROR, "%s: Second fixed surface: If the scale factor is missing so should the scaled value and vice versa", TITLE);
return GRIB_INVALID_KEY_VALUE;
}
if (stype != 255) {
int required_val = codes_grib_surface_type_requires_value(edition_, stype, &err);
if (err) return err;
if (required_val && (sfac_missing || sval_missing)) {
char name[128] = {0,};
size_t len = 128;
grib_get_string(handle_, "nameOfSecondFixedSurface", name, &len);
grib_context_log(c, GRIB_LOG_ERROR, "%s: Second fixed surface: Type %ld (%s) requires a level", TITLE, stype, name);
return GRIB_INVALID_KEY_VALUE;
}
}

return GRIB_SUCCESS;
}
Expand Down Expand Up @@ -243,6 +266,27 @@ int grib_accessor_message_is_valid_t::check_sections()
return GRIB_SUCCESS;
}

int grib_accessor_message_is_valid_t::check_parameter()
{
//printf("DEBUG %s \n", __func__);
int err = 0;
long centre = 0;
err = grib_get_long_internal(handle_, "centre", &centre);
if (err) return err;
if (centre == 98) {
// make sure paramId is valid for ECMWF data
long paramId = 0;
err = grib_get_long_internal(handle_, "paramId", &paramId);
if (err) return err;
if (paramId == 0) {
grib_context_log(handle_->context, GRIB_LOG_ERROR, "%s: Key paramId is 0 (parameter is not mapped)", TITLE);
return GRIB_INVALID_MESSAGE;
}
}

return err;
}

int grib_accessor_message_is_valid_t::unpack_long(long* val, size_t* len)
{
typedef int (grib_accessor_message_is_valid_t::*check_func)();
Expand All @@ -254,7 +298,8 @@ int grib_accessor_message_is_valid_t::unpack_long(long* val, size_t* len)
&grib_accessor_message_is_valid_t::check_steps,
&grib_accessor_message_is_valid_t::check_7777,
&grib_accessor_message_is_valid_t::check_namespace_keys,
&grib_accessor_message_is_valid_t::check_surface_keys
&grib_accessor_message_is_valid_t::check_surface_keys,
&grib_accessor_message_is_valid_t::check_parameter
};

int err = 0;
Expand Down
1 change: 1 addition & 0 deletions src/accessor/grib_accessor_class_message_is_valid.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class grib_accessor_message_is_valid_t : public grib_accessor_long_t
int check_section_numbers(const int* sec_nums, size_t N);
int check_namespace_keys();
int check_sections();
int check_parameter();

private:
const char* product_ = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/grib_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2155,7 +2155,7 @@ int codes_grib_surface_type_requires_value(int edition, int type_of_surface_code
}

// Surface type keys are 1 octet and cannot be -ve
if (type_of_surface_code < 0 || type_of_surface_code > 254) {
if (type_of_surface_code < 0 || type_of_surface_code > 255) {
*err = GRIB_INVALID_ARGUMENT;
return 0;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/grib_check_message_validity.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ result=$( ${tools_dir}/grib_get -w count=1 -p isMessageValid $input 2>$tempText
[ $result -eq 0 ]
grep -q "Second fixed surface: If the type of surface is missing so should its scaled keys" $tempText

# Some surface types require sv/sf to be set (not missing)
${tools_dir}/grib_set -s typeOfFirstFixedSurface=160 $sample $tempGrib
grib_check_key_equals $tempGrib scaledValueOfFirstFixedSurface MISSING
grib_check_key_equals $tempGrib scaleFactorOfFirstFixedSurface MISSING
grib_check_key_equals $tempGrib isMessageValid 0 2>$tempText
grep -q "First fixed surface: Type 160 .Depth below sea level. requires a level" $tempText


# Check paramId is mapped
# ------------------------------
input=$data_dir/tigge_cf_ecmwf.grib2
# Message 43 in this file has a deprecated mapping (wilting point)
result=$( ${tools_dir}/grib_get -w count=43 -p isMessageValid $input 2>$tempText )
[ $result -eq 0 ]
grep -q "parameter is not mapped" $tempText


# Check steps
# ------------------------------
Expand Down
2 changes: 0 additions & 2 deletions tests/grib_dump_debug.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ spectral_complex.grib1
spherical_model_level.grib1
spherical_pressure_level.grib1
constant_field.grib2
multi_created.grib2
reduced_gaussian_model_level.grib2
reduced_gaussian_pressure_level.grib2
reduced_gaussian_pressure_level_constant.grib2
Expand All @@ -58,7 +57,6 @@ regular_gaussian_pressure_level_constant.grib2
regular_gaussian_surface.grib2
regular_latlon_surface.grib2
regular_latlon_surface_constant.grib2
sample.grib2
spherical_model_level.grib2
spherical_pressure_level.grib2
tigge_af_ecmwf.grib2
Expand Down

0 comments on commit 9341df8

Please sign in to comment.