diff --git a/src/gribapi_utilities.c b/src/gribapi_utilities.c
index e073a7f250dd66121f841691fb2abf14af16432b..56a44e3de52e18a148cc9f4ae56d6dbb67d03450 100644
--- a/src/gribapi_utilities.c
+++ b/src/gribapi_utilities.c
@@ -18,7 +18,7 @@
 #define FAIL_ON_GRIB_ERROR(function, gribHandle, key, ...)                                                                 \
   do                                                                                                                       \
     {                                                                                                                      \
-      const int errorCode = (int) function(gribHandle, key, __VA_ARGS__);                                                  \
+      int errorCode = (int) function(gribHandle, key, __VA_ARGS__);                                                        \
       if (errorCode)                                                                                                       \
         {                                                                                                                  \
           fprintf(stderr, "%s:%d: Error in function `%s`: `%s` returned error code %d for key \"%s\"", __FILE__, __LINE__, \
@@ -573,7 +573,7 @@ has_ni(grib_handle *gh)
 int
 gribapiGetGridType(grib_handle *gh)
 {
-  const long gridDefinitionTemplateNumber = gribGetLongDefault(gh, "gridDefinitionTemplateNumber", -1);
+  long gridDefinitionTemplateNumber = gribGetLongDefault(gh, "gridDefinitionTemplateNumber", -1);
   switch (gridDefinitionTemplateNumber)
     {
     case GRIB2_GTYPE_LATLON: return has_ni(gh) ? GRID_LONLAT : GRID_GENERIC;
@@ -675,9 +675,9 @@ gribapiGetGridRegular(grib_handle *gh, grid_t *grid, int editionNumber, int grid
 {
   long lpar;
   FAIL_ON_GRIB_ERROR(grib_get_long, gh, "Ni", &lpar);
-  const size_t nlon = (size_t) lpar;
+  size_t nlon = (size_t) lpar;
   FAIL_ON_GRIB_ERROR(grib_get_long, gh, "Nj", &lpar);
-  const size_t nlat = (size_t) lpar;
+  size_t nlat = (size_t) lpar;
 
   if (gridtype == GRID_GAUSSIAN)
     {
@@ -756,9 +756,9 @@ gribapiGetGridProj(grib_handle *gh, grid_t *grid, size_t numberOfPoints)
 {
   long lpar;
   FAIL_ON_GRIB_ERROR(grib_get_long, gh, "Nx", &lpar);
-  const size_t nlon = (size_t) lpar;
+  size_t nlon = (size_t) lpar;
   FAIL_ON_GRIB_ERROR(grib_get_long, gh, "Ny", &lpar);
-  const size_t nlat = (size_t) lpar;
+  size_t nlat = (size_t) lpar;
 
   if (numberOfPoints != nlon * nlat) Error("numberOfPoints (%zu) and gridSize (%zu) differ!", numberOfPoints, nlon * nlat);
 
@@ -841,12 +841,12 @@ static void
 gribapiGetGridGeneric(grib_handle *gh, grid_t *grid, size_t numberOfPoints)
 {
   long lpar;
-  const size_t nlon = (grib_get_long(gh, "Ni", &lpar) == 0) ? (size_t) lpar : 0;
-  const size_t nlat = (grib_get_long(gh, "Nj", &lpar) == 0) ? (size_t) lpar : 0;
+  size_t nlon = (grib_get_long(gh, "Ni", &lpar) == 0) ? (size_t) lpar : 0;
+  size_t nlat = (grib_get_long(gh, "Nj", &lpar) == 0) ? (size_t) lpar : 0;
 
   grid->size = numberOfPoints;
 
-  const bool lgeneric = (nlon > 0 && nlat > 0 && nlon * nlat == numberOfPoints);
+  bool lgeneric = (nlon > 0 && nlat > 0 && nlon * nlat == numberOfPoints);
   grid->x.size = lgeneric ? nlon : 0;
   grid->y.size = lgeneric ? nlat : 0;
 }
@@ -856,7 +856,7 @@ bool
 gribapiGetGrid(grib_handle *gh, grid_t *grid)
 {
   bool uvRelativeToGrid = false;
-  const long editionNumber = gribEditionNumber(gh);
+  long editionNumber = gribEditionNumber(gh);
   int gridtype = gribapiGetGridType(gh);
   int projtype = (gridtype == GRID_PROJECTION && gribapiGetIsRotated(gh)) ? CDI_PROJ_RLL : CDI_UNDEFID;
   if (gridtype == CDI_PROJ_LCC || gridtype == CDI_PROJ_STERE)
@@ -920,7 +920,7 @@ gribapiGetGrid(grib_handle *gh, grid_t *grid)
 
   if (gridtype == GRID_GAUSSIAN || gridtype == GRID_LONLAT || projtype == CDI_PROJ_RLL || projtype == CDI_PROJ_LCC)
     {
-      long temp;
+      long temp = 0;
       GRIB_CHECK(grib_get_long(gh, "uvRelativeToGrid", &temp), 0);
       assert(temp == 0 || temp == 1);
       uvRelativeToGrid = (bool) temp;
diff --git a/src/stream_gribapi.c b/src/stream_gribapi.c
index 3881f553d52331111d7f8ccf901b0eafb5036923..4b4c897f3c2fc4673c0358158328fdba7ee21d82 100644
--- a/src/stream_gribapi.c
+++ b/src/stream_gribapi.c
@@ -2217,10 +2217,10 @@ gribapiDefGridRegular(grib_handle *gh, int gridID, int gridtype, bool gridIsRota
   GRIB_CHECK(my_grib_set_double(gh, "latitudeOfFirstGridPointInDegrees", yfirst), 0);
   GRIB_CHECK(my_grib_set_double(gh, "latitudeOfLastGridPointInDegrees", ylast), 0);
 
-  long xscan = xfirst > xlast;
-  GRIB_CHECK(my_grib_set_long(gh, "iScansNegatively", xscan), 0);
-  xscan = yfirst < ylast;
-  GRIB_CHECK(my_grib_set_long(gh, "jScansPositively", xscan), 0);
+  if (uvRelativeToGrid >= 0) GRIB_CHECK(my_grib_set_long(gh, "uvRelativeToGrid", uvRelativeToGrid), 0);
+
+  GRIB_CHECK(my_grib_set_long(gh, "iScansNegatively", (xfirst > xlast)), 0);
+  GRIB_CHECK(my_grib_set_long(gh, "jScansPositively", (yfirst < ylast)), 0);
 
   if (gridtype == GRID_GAUSSIAN || gridtype == GRID_GAUSSIAN_REDUCED)
     {
@@ -2245,8 +2245,6 @@ gribapiDefGridRegular(grib_handle *gh, int gridID, int gridtype, bool gridIsRota
       GRIB_CHECK(my_grib_set_double(gh, "longitudeOfSouthernPoleInDegrees", xpole), 0);
       GRIB_CHECK(my_grib_set_double(gh, "angleOfRotation", angle), 0);
     }
-
-  if (uvRelativeToGrid >= 0) GRIB_CHECK(my_grib_set_long(gh, "uvRelativeToGrid", uvRelativeToGrid), 0);
 }
 
 static int
@@ -2322,18 +2320,17 @@ gribapiDefGridLCC(grib_handle *gh, int editionNumber, int gridID, int uvRelative
   if (shapeOfTheEarth) GRIB_CHECK(my_grib_set_long(gh, "shapeOfTheEarth", shapeOfTheEarth), 0);
   if (shapeOfTheEarth == 1) GRIB_CHECK(my_grib_set_long(gh, "radiusOfTheEarth", gpp.a), 0);
 
-  if (uvRelativeToGrid >= 0) GRIB_CHECK(my_grib_set_long(gh, "uvRelativeToGrid", uvRelativeToGrid), 0);
-
-  long earthIsOblate = (IS_EQUAL(gpp.a, 6378160.0) && IS_EQUAL(gpp.rf, 297.0));
+  long earthIsOblate = (shapeOfTheEarth == 2 || shapeOfTheEarth == 3 || shapeOfTheEarth == 4);
   if (earthIsOblate) GRIB_CHECK(my_grib_set_long(gh, "earthIsOblate", earthIsOblate), 0);
 
-  int scanflag = 0;
-  gribbyte_set_bit(&scanflag, 2);
-  if (editionNumber <= 1) GRIB_CHECK(my_grib_set_long(gh, "scanningMode", (long) scanflag), 0);
+  if (uvRelativeToGrid >= 0) GRIB_CHECK(my_grib_set_long(gh, "uvRelativeToGrid", uvRelativeToGrid), 0);
+
+  GRIB_CHECK(my_grib_set_long(gh, "iScansNegatively", (xinc < 0)), 0);
+  GRIB_CHECK(my_grib_set_long(gh, "jScansPositively", (yinc > 0)), 0);
 }
 
 static void
-gribapiDefGridSTERE(grib_handle *gh, int gridID)
+gribapiDefGridSTERE(grib_handle *gh, int gridID, int uvRelativeToGrid)
 {
   long xsize = (long) gridInqXsize(gridID);
   long ysize = (long) gridInqYsize(gridID);
@@ -2367,9 +2364,13 @@ gribapiDefGridSTERE(grib_handle *gh, int gridID)
   if (shapeOfTheEarth) GRIB_CHECK(my_grib_set_long(gh, "shapeOfTheEarth", shapeOfTheEarth), 0);
   if (shapeOfTheEarth == 1) GRIB_CHECK(my_grib_set_long(gh, "radiusOfTheEarth", gpp.a), 0);
 
-  GRIB_CHECK(my_grib_set_long(gh, "resolutionAndComponentFlags", 8), 0);
-  GRIB_CHECK(my_grib_set_long(gh, "iScansNegatively", 0), 0);
-  GRIB_CHECK(my_grib_set_long(gh, "jScansPositively", 1), 0);
+  long earthIsOblate = (shapeOfTheEarth == 2 || shapeOfTheEarth == 3 || shapeOfTheEarth == 4);
+  if (earthIsOblate) GRIB_CHECK(my_grib_set_long(gh, "earthIsOblate", earthIsOblate), 0);
+
+  if (uvRelativeToGrid >= 0) GRIB_CHECK(my_grib_set_long(gh, "uvRelativeToGrid", uvRelativeToGrid), 0);
+
+  GRIB_CHECK(my_grib_set_long(gh, "iScansNegatively", (xinc < 0)), 0);
+  GRIB_CHECK(my_grib_set_long(gh, "jScansPositively", (yinc > 0)), 0);
 }
 
 static void
@@ -2529,7 +2530,7 @@ gribapiDefGrid(int editionNumber, grib_handle *gh, int gridID, int comptype, int
       }
     case CDI_PROJ_STERE:
       {
-        gribapiDefGridSTERE(gh, gridID);
+        gribapiDefGridSTERE(gh, gridID, uvRelativeToGrid);
         break;
       }
     case GRID_SPECTRAL: