GRIB2 encoding: longitude values should be 0 to 360
@@ -173,7 +173,6 @@ static int unpack_double (grib_accessor* a, double* val, size_t *len)
return GRIB_SUCCESS;
}
-
if((ret = grib_get_double_array_internal(grib_handle_of_accessor(a), self->grid,grid,&size)) != GRIB_SUCCESS)
return ret;
@@ -182,6 +181,13 @@ static int unpack_double (grib_accessor* a, double* val, size_t *len)
return GRIB_SUCCESS;
}
+static double normalise_longitude(double a_lon)
+{
+ /* WMO regulation: The longitude values shall be limited to the range 0 to 360 degrees inclusive */
+ while (a_lon<0) a_lon += 360;
+ while (a_lon>360) a_lon -= 360;
+ return a_lon;
+}
static int pack_double(grib_accessor* a, const double* val, size_t *len)
{
@@ -190,6 +196,7 @@ static int pack_double(grib_accessor* a, const double* val, size_t *len)
double grid[6];
size_t size = 6;
+ double new_val = *val;
if(*len < 1){
ret = GRIB_ARRAY_TOO_SMALL;
@@ -203,12 +210,16 @@ static int pack_double(grib_accessor* a, const double* val, size_t *len)
return ret;
}
-
if((ret = grib_get_double_array_internal(grib_handle_of_accessor(a), self->grid,grid,&size)) != GRIB_SUCCESS)
return ret;
- if ( (self->index == 1 || self->index == 3) && *val < 0 ) grid[self->index] = 360+*val;
- else grid[self->index] = *val;
+ /* index 1 is longitudeOfFirstGridPointInDegrees
+ * index 3 is longitudeOfLastGridPointInDegrees
+ */
+ if ( (self->index == 1 || self->index == 3) ) {
+ new_val = normalise_longitude(*val);
+ }
+ grid[self->index] = new_val;
return grib_set_double_array_internal(grib_handle_of_accessor(a), self->grid,grid,size);
}
@@ -236,5 +247,3 @@ static int is_missing(grib_accessor* a)
return !given;
}
-
-
