GRIB-23: wrong value for longitudeOfSouthernPole. Do not allow -ve numbers for unsigned keys

This commit is contained in:
Shahram Najm 2013-08-02 15:28:57 +01:00
parent f8670c58bd
commit 4a10ee2630
2 changed files with 34 additions and 7 deletions

View File

@ -29,6 +29,7 @@ int main(int argc, char** argv) {
long numberOfContributingSpectralBands; long numberOfContributingSpectralBands;
long values[1024]; long values[1024];
long new_values[1024];
FILE* in = NULL; FILE* in = NULL;
char* filename = "../../data/satellite.grib"; char* filename = "../../data/satellite.grib";
@ -36,7 +37,7 @@ int main(int argc, char** argv) {
in = fopen(filename,"r"); in = fopen(filename,"r");
if(!in) { if(!in) {
printf("ERROR: unable to open file %s\n",filename); printf("ERROR: unable to open input file %s\n",filename);
return 1; return 1;
} }
@ -45,10 +46,15 @@ int main(int argc, char** argv) {
if (h == NULL) { if (h == NULL) {
printf("Error: unable to create handle from file %s\n",filename); printf("Error: unable to create handle from file %s\n",filename);
} }
GRIB_CHECK(grib_get_long(h,"numberOfContributingSpectralBands",&numberOfContributingSpectralBands),0);
assert(numberOfContributingSpectralBands == 3);
/* Shrink NB to 2 */
numberOfContributingSpectralBands = 2; numberOfContributingSpectralBands = 2;
GRIB_CHECK(grib_set_long(h,"numberOfContributingSpectralBands",numberOfContributingSpectralBands),0); GRIB_CHECK(grib_set_long(h,"numberOfContributingSpectralBands",numberOfContributingSpectralBands),0);
/* Expand NB to 9 */
numberOfContributingSpectralBands = 9; numberOfContributingSpectralBands = 9;
GRIB_CHECK(grib_set_long(h,"numberOfContributingSpectralBands",numberOfContributingSpectralBands),0); GRIB_CHECK(grib_set_long(h,"numberOfContributingSpectralBands",numberOfContributingSpectralBands),0);
@ -66,16 +72,27 @@ int main(int argc, char** argv) {
GRIB_CHECK(grib_get_long_array(h,"scaledValueOfCentralWaveNumber",values,&size),0); GRIB_CHECK(grib_get_long_array(h,"scaledValueOfCentralWaveNumber",values,&size),0);
assert(size == count); assert(size == count);
for(i=0;i<count;i++) for(i=0;i<count;i++) {
printf("scaledValueOfCentralWaveNumber %lu = %ld\n",(unsigned long)i,values[i]); printf("scaledValueOfCentralWaveNumber %lu = %ld\n",(unsigned long)i,values[i]);
if (i == 0) assert( values[i] == 26870 );
if (i == 1) assert( values[i] == 9272 );
}
for(i=0;i<count;i++) for(i=0;i<count;i++)
values[i] = -values[i]; values[i] = i+1000;
size = count; size = count;
/* size--; */ /* size--; */
GRIB_CHECK(grib_set_long_array(h,"scaledValueOfCentralWaveNumber",values,size),0); GRIB_CHECK(grib_set_long_array(h,"scaledValueOfCentralWaveNumber",values,size),0);
assert(size == count); assert(size == count);
/* check what we set */
GRIB_CHECK(grib_get_long_array(h,"scaledValueOfCentralWaveNumber",new_values,&size),0);
assert(size == count);
for(i=0;i<count;i++) {
printf("Now scaledValueOfCentralWaveNumber %lu = %ld\n",(unsigned long)i,new_values[i]);
assert( new_values[i] == (i+1000) );
}
grib_handle_delete(h); grib_handle_delete(h);

View File

@ -214,6 +214,11 @@ int pack_long_unsigned_helper(grib_accessor* a, const long* val, size_t *len, in
/* Check if value fits into number of bits */ /* Check if value fits into number of bits */
if (check) { if (check) {
const long nbits = self->nbytes*8; const long nbits = self->nbytes*8;
if (v<0) {
grib_context_log(a->parent->h->context, GRIB_LOG_ERROR,
"Key \"%s\": Trying to encode a negative value of %ld for key of type unsigned\n", a->name, v);
return GRIB_ENCODING_ERROR;
}
if ( nbits < 32 && v != GRIB_MISSING_LONG ) { if ( nbits < 32 && v != GRIB_MISSING_LONG ) {
unsigned long maxval = (1 << nbits)-1; unsigned long maxval = (1 << nbits)-1;
if (v > maxval) { if (v > maxval) {
@ -296,7 +301,8 @@ static int pack_long(grib_accessor* a, const long* val, size_t *len)
return pack_long_unsigned_helper(a,val,len, /*check=*/1); return pack_long_unsigned_helper(a,val,len, /*check=*/1);
} }
static long byte_count(grib_accessor* a){ static long byte_count(grib_accessor* a)
{
return a->length; return a->length;
} }
@ -311,19 +317,23 @@ static long value_count(grib_accessor* a)
return 1; return 1;
} }
static long byte_offset(grib_accessor* a){ static long byte_offset(grib_accessor* a)
{
return a->offset; return a->offset;
} }
static void update_size(grib_accessor* a,size_t s) static void update_size(grib_accessor* a,size_t s)
{ {
a->length = s; a->length = s;
} }
static long next_offset(grib_accessor* a){ static long next_offset(grib_accessor* a)
{
return grib_byte_offset(a)+grib_byte_count(a); return grib_byte_offset(a)+grib_byte_count(a);
} }
static int is_missing(grib_accessor* a){ static int is_missing(grib_accessor* a)
{
int i=0; int i=0;
unsigned char ff=0xff; unsigned char ff=0xff;
unsigned long offset=a->offset; unsigned long offset=a->offset;