GRIB message validity checker: Check sections for both editions

This commit is contained in:
shahramn 2024-12-23 13:21:55 +00:00
parent ef506078b5
commit 2d9732f009
1 changed files with 20 additions and 6 deletions

View File

@ -110,14 +110,28 @@ static int check_sections(grib_handle* h)
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) {
const int grib1_section_nums[] = {1, 2, 4};
const int grib2_section_nums[] = {1, 3, 4, 5, 6, 7, 8}; // section 2 is optional
if (edition == 1) {
const size_t N = sizeof(grib1_section_nums) / sizeof(grib1_section_nums[0]);
for (size_t i=0; i<N; ++i) {
char sname[16] = {0,};
snprintf(sname, sizeof(sname), "section_%zu", i);
const int sec_num = grib1_section_nums[i];
snprintf(sname, sizeof(sname), "section_%d", sec_num);
if (!grib_is_defined(h, sname)) {
grib_context_log(h->context, GRIB_LOG_ERROR, "Section %zu is missing!", i);
grib_context_log(h->context, GRIB_LOG_ERROR, "GRIB%ld: Section %d is missing!", edition, sec_num);
return GRIB_INVALID_MESSAGE;
}
}
}
else if (edition == 2) {
const size_t N = sizeof(grib2_section_nums) / sizeof(grib2_section_nums[0]);
for (size_t i=0; i<N; ++i) {
char sname[16] = {0,};
const int sec_num = grib2_section_nums[i];
snprintf(sname, sizeof(sname), "section_%d", sec_num);
if (!grib_is_defined(h, sname)) {
grib_context_log(h->context, GRIB_LOG_ERROR, "GRIB%ld: Section %d is missing!", edition, sec_num);
return GRIB_INVALID_MESSAGE;
}
}