From 48fc59c38362ad3d1ff375834e9ba5e3d48a02cd Mon Sep 17 00:00:00 2001
From: Uwe Schulzweida <uwe.schulzweida@mpimet.mpg.de>
Date: Fri, 18 Nov 2022 12:08:16 +0100
Subject: [PATCH] grid module: removed initialization of CDI_KEY_SCANNINGMODE
 (bug fix)

---
 ChangeLog               |  8 ++++++++
 src/gribapi_utilities.c |  2 +-
 src/grid.c              |  1 -
 src/stream_gribapi.c    | 13 ++++++++-----
 4 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3097a0e18..ec93ffb37 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2022-12-06  Uwe Schulzweida
+
+	* Version 2.1.1 released
+
+2022-11-18  Uwe Schulzweida
+
+	* grid module: removed initialization of CDI_KEY_SCANNINGMODE (bug fix)
+
 2022-11-16  Uwe Schulzweida
 
 	* cdf_read_xcoord: skip char coordinates only if ndims=1 (bug fix)
diff --git a/src/gribapi_utilities.c b/src/gribapi_utilities.c
index c87c92bf3..e72476be7 100644
--- a/src/gribapi_utilities.c
+++ b/src/gribapi_utilities.c
@@ -942,7 +942,7 @@ gribapiGetGrid(grib_handle *gh, grid_t *grid)
          Default / implicit scanning mode is 64:
                             i and j scan positively, i points are consecutive (row-major)        */
 #ifdef HIRLAM_EXTENSIONS
-      if (cdiDebugExt >= 30)
+      if (cdiDebugExt >= 30 && gribEditionNumber(gh) <= 1)
         {
           //  indicatorOfParameter=33,indicatorOfTypeOfLevel=105,level
           long paramId, levelTypeId, levelId;
diff --git a/src/grid.c b/src/grid.c
index a459c1b5d..24767ac61 100644
--- a/src/grid.c
+++ b/src/grid.c
@@ -156,7 +156,6 @@ grid_init(grid_t *gridptr)
   gridptr->atts.nelems = 0;
 
   cdiDefVarKeyInt(&gridptr->keys, CDI_KEY_DATATYPE, CDI_DATATYPE_FLT64);
-  cdiDefVarKeyInt(&gridptr->keys, CDI_KEY_SCANNINGMODE, 64);
 }
 
 static void
diff --git a/src/stream_gribapi.c b/src/stream_gribapi.c
index b9b5b802c..b38651760 100644
--- a/src/stream_gribapi.c
+++ b/src/stream_gribapi.c
@@ -2805,7 +2805,7 @@ gribapiSetScanningMode(grib_handle *gh, int scanningMode)
   const long jScansPositively = (scanningMode & 64) / 64;
   const long jPointsAreConsecutive = (scanningMode & 32) / 32;
 
-  if (cdiDebugExt >= 30)
+  if (cdiDebugExt >= 30 && gribEditionNumber(gh) <= 1)
     {
       long paramId, levelTypeId, levelId, uvRelativeToGrid;
       GRIB_CHECK(grib_get_long(gh, "uvRelativeToGrid", &uvRelativeToGrid), 0);
@@ -3149,7 +3149,7 @@ gribapiSetExtMode(grib_handle *gh, int gridID, size_t datasize, const void *data
       if (cdiDebugExt >= 100)
         {
           size_t gridsize = gridInqSize(gridID);
-          Message("(scanModeIN=%d; gridsize=%zu", scanModeIN, gridsize);
+          Message("scanModeIN=%d; gridsize=%zu", scanModeIN, gridsize);
         }
 
       if (cdiGribDataScanningMode.active)  // allowed modes: <0, 64, 96>; Default is 64
@@ -3411,9 +3411,12 @@ void
 gribapiChangeParameterIdentification(grib_handle *gh, int code, int ltype, int level)
 {
   //  timeRangeIndicator: could be included later
-  if (code != -1) GRIB_CHECK(my_grib_set_long(gh, "indicatorOfParameter", code), 0);
-  if (ltype != -1) GRIB_CHECK(my_grib_set_long(gh, "indicatorOfTypeOfLevel", ltype), 0);
-  if (level != -1) GRIB_CHECK(my_grib_set_long(gh, "level", level), 0);
+  if (gribEditionNumber(gh) <= 1)
+    {
+      if (code != -1) GRIB_CHECK(my_grib_set_long(gh, "indicatorOfParameter", code), 0);
+      if (ltype != -1) GRIB_CHECK(my_grib_set_long(gh, "indicatorOfTypeOfLevel", ltype), 0);
+      if (level != -1) GRIB_CHECK(my_grib_set_long(gh, "level", level), 0);
+    }
 }
 
 #endif
-- 
GitLab