@@ -68,12 +68,8 @@ int grib_accessor_message_is_valid_t::check_grid_pl_array()
6868
6969 char gridType[128 ] = {0 ,};
7070 size_t len = 128 ;
71- ret = grib_get_string (handle_, " gridType" , gridType, &len);
72- if (ret == GRIB_SUCCESS && STR_EQUAL (gridType, " reduced_ll" )) {
73- // Unfortunately in our archive we have such grids with zeroes in the pl!
74- return GRIB_SUCCESS;
75- }
76-
71+ ret = grib_get_string_internal (handle_, " gridType" , gridType, &len);
72+ if (ret != GRIB_SUCCESS) return ret;
7773
7874 if ((ret = grib_get_size (handle_, " pl" , &plsize)) != GRIB_SUCCESS)
7975 return ret;
@@ -90,16 +86,42 @@ int grib_accessor_message_is_valid_t::check_grid_pl_array()
9086
9187 pl = (long *)grib_context_malloc_clear (c, sizeof (long ) * plsize);
9288 if (!pl) return GRIB_OUT_OF_MEMORY;
93- if ((ret = grib_get_long_array (handle_, " pl" , pl, &plsize)) != GRIB_SUCCESS)
89+ if ((ret = grib_get_long_array_internal (handle_, " pl" , pl, &plsize)) != GRIB_SUCCESS)
9490 return ret;
9591
96- for (size_t j = 0 ; j < plsize; j++) {
97- if (pl[j] == 0 ) {
98- grib_context_log (c, GRIB_LOG_ERROR, " %s: Invalid PL array: entry at index=%zu is zero" , TITLE, j);
92+ long numberOfDataPoints = 0 ;
93+ if ((ret = grib_get_long_internal (handle_, " numberOfDataPoints" , &numberOfDataPoints)) != GRIB_SUCCESS)
94+ return ret;
95+ size_t sum_pl = 0 ;
96+ for (size_t j = 0 ; j < plsize; j++) sum_pl += pl[j];
97+
98+ if (STR_EQUAL (gridType, " reduced_ll" )) {
99+ // For reduced_ll grids, sum(pl) must equal numberOfDataPoints in every situation
100+ if (sum_pl != (size_t )numberOfDataPoints) {
101+ grib_context_log (c, GRIB_LOG_ERROR, " %s: Sum of PL array (=%zu) must equal numberOfDataPoints (=%ld)" ,
102+ TITLE, sum_pl, numberOfDataPoints);
103+ grib_context_free (c, pl);
104+ return GRIB_WRONG_GRID;
105+ }
106+ }
107+ else {
108+ // Other reduced grids
109+ // Unfortunately in our archive we have reduced_ll grids with zeroes in the pl!
110+ for (size_t j = 0 ; j < plsize; j++) {
111+ if (pl[j] == 0 ) {
112+ grib_context_log (c, GRIB_LOG_ERROR, " %s: Invalid PL array: entry at index=%zu is zero" , TITLE, j);
113+ grib_context_free (c, pl);
114+ return GRIB_WRONG_GRID;
115+ }
116+ }
117+ if (sum_pl < (size_t )numberOfDataPoints) {
118+ grib_context_log (c, GRIB_LOG_ERROR, " %s: Sum of PL array (=%zu) cannot be less than numberOfDataPoints (=%ld)" ,
119+ TITLE, sum_pl, numberOfDataPoints);
99120 grib_context_free (c, pl);
100121 return GRIB_WRONG_GRID;
101122 }
102123 }
124+
103125 grib_context_free (c, pl);
104126
105127 return GRIB_SUCCESS;
0 commit comments