Skip to content

Commit

Permalink
GRIB message validity checker: Check sections for both editions
Browse files Browse the repository at this point in the history
  • Loading branch information
shahramn committed Dec 23, 2024
1 parent 2d9732f commit aba80c4
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions src/accessor/grib_accessor_class_message_is_valid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,37 +104,39 @@ static int check_geoiterator(grib_handle* h)
return err;
}


static int check_section_numbers(grib_handle* h, long edition, const int* sec_nums, size_t N)
{
for (size_t i = 0; i < N; ++i) {
char sname[16] = {0,};
const int sec_num = sec_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;
}
}
return GRIB_SUCCESS;
}

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;
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 int grib1_section_nums[] = {1, 2, 4};
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,};
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, "GRIB%ld: Section %d is missing!", edition, sec_num);
return GRIB_INVALID_MESSAGE;
}
}
err = check_section_numbers(h, edition, grib1_section_nums, N);
if (err) return err;
}
else if (edition == 2) {
const int grib2_section_nums[] = {1, 3, 4, 5, 6, 7, 8}; // section 2 is optional
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;
}
}
err = check_section_numbers(h, edition, grib2_section_nums, N);
if (err) return err;
}
return GRIB_SUCCESS;
}
Expand Down

0 comments on commit aba80c4

Please sign in to comment.