From bca8f6cfc30a90c460f2f52100fdb84a7341dc5d Mon Sep 17 00:00:00 2001
From: Uwe Schulzweida <uwe.schulzweida@mpimet.mpg.de>
Date: Tue, 27 Jul 2021 15:05:59 +0200
Subject: [PATCH] Moved gribContainers stuff to grbEncode().

---
 src/cdi_int.h   |  4 ----
 src/grb_write.c | 58 +++++++++++++++++++------------------------------
 src/gribapi.c   |  2 +-
 src/stream.c    |  2 +-
 4 files changed, 24 insertions(+), 42 deletions(-)

diff --git a/src/cdi_int.h b/src/cdi_int.h
index 7f7afffb1..0cf232ff8 100644
--- a/src/cdi_int.h
+++ b/src/cdi_int.h
@@ -300,11 +300,7 @@ typedef struct {
   int         complevel;     // compression level
   bool        sortname;
   bool        sortparam;
-#ifdef GRIBCONTAINER2D
-  void      **gribContainers;
-#else
   void       *gribContainers;
-#endif
 
   int         numWorker;
   int         nextRecID;
diff --git a/src/grb_write.c b/src/grb_write.c
index 7f8954b3c..0d960cc48 100644
--- a/src/grb_write.c
+++ b/src/grb_write.c
@@ -24,7 +24,7 @@ static
 size_t grbEncode(int filetype, int memtype, int varID, int levelID, int vlistID, int gridID, int zaxisID,
 		 int date, int time, int tsteptype, int numavg,
 		 size_t datasize, const void *data, size_t nmiss, void **gribbuffer,
-		 int comptype, void *gribContainer)
+		 int comptype, void *gribContainers)
 {
   size_t nbytes = 0;
 
@@ -42,6 +42,11 @@ size_t grbEncode(int filetype, int memtype, int varID, int levelID, int vlistID,
 #endif
 #ifdef HAVE_LIBGRIB_API
     {
+#ifdef GRIBCONTAINER2D
+      void *gribContainer = (void *) &((gribContainer_t **)gribContainers)[varID][levelID];
+#else
+      void *gribContainer = (void *) &((gribContainer_t *)gribContainers)[varID];
+#endif
       const void *datap = data;
       if ( memtype == MEMTYPE_FLOAT )
         {
@@ -62,7 +67,7 @@ size_t grbEncode(int filetype, int memtype, int varID, int levelID, int vlistID,
 #else
     {
       Error("ecCodes support not compiled in!");
-      (void)gribContainer;
+      (void)gribContainers;
       (void)comptype;
     }
 #endif
@@ -314,9 +319,6 @@ void grbCopyRecord(stream_t *streamptr2, stream_t *streamptr1)
 
 void grb_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, const void *data, size_t nmiss)
 {
-  void *gribbuffer = NULL;
-  void *gc = NULL;
-
   const int filetype = streamptr->filetype;
   const int fileID = streamptr->fileID;
   const int vlistID = streamptr->vlistID;
@@ -331,37 +333,22 @@ void grb_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtyp
 
   if (CDI_Debug) Message("gridID = %d zaxisID = %d", gridID, zaxisID);
 
-  size_t datasize = gridInqSize(gridID);
+  const size_t datasize = gridInqSize(gridID);
 
-#ifdef HAVE_LIBCGRIBEX
-  if (filetype == CDI_FILETYPE_GRB && !CDI_gribapi_grib1)
-    {
-    }
-  else
-#endif
-    {
-#ifdef GRIBCONTAINER2D
-      gribContainer_t **gribContainers =  (gribContainer_t **) streamptr->gribContainers;
-      gc = (void *) &gribContainers[varID][levelID];
-#else
-      gribContainer_t *gribContainers =  (gribContainer_t *) streamptr->gribContainers;
-      gc = (void *) &gribContainers[varID];
-#endif
-    }
-
-  if ( comptype != CDI_COMPRESS_JPEG && comptype != CDI_COMPRESS_SZIP && comptype != CDI_COMPRESS_AEC ) comptype = CDI_COMPRESS_NONE;
+  if (comptype != CDI_COMPRESS_JPEG && comptype != CDI_COMPRESS_SZIP && comptype != CDI_COMPRESS_AEC) comptype = CDI_COMPRESS_NONE;
 
-  if ( filetype == CDI_FILETYPE_GRB && !CDI_gribapi_grib1 && comptype == CDI_COMPRESS_JPEG )
+  if (filetype == CDI_FILETYPE_GRB && !CDI_gribapi_grib1 && comptype == CDI_COMPRESS_JPEG)
     {
-      static int ljpeg_warn = 1;
-      if ( ljpeg_warn ) Warning("JPEG compression of GRIB1 records not available!");
-      ljpeg_warn = 0;
+      static bool ljpeg_warn = true;
+      if (ljpeg_warn) Warning("JPEG compression of GRIB1 records not available!");
+      ljpeg_warn = false;
     }
 
+  void *gribbuffer = NULL;
   size_t nbytes = grbEncode(filetype, memtype, varID, levelID, vlistID, gridID, zaxisID, date, time, tsteptype, numavg,
-                            datasize, data, nmiss, &gribbuffer, comptype, gc);
+                            datasize, data, nmiss, &gribbuffer, comptype, streamptr->gribContainers);
 
-  if ( filetype == CDI_FILETYPE_GRB && (comptype == CDI_COMPRESS_SZIP || comptype == CDI_COMPRESS_AEC) )
+  if (filetype == CDI_FILETYPE_GRB && (comptype == CDI_COMPRESS_SZIP || comptype == CDI_COMPRESS_AEC))
     nbytes = grbSzip(filetype, gribbuffer, nbytes);
 
   if (streamptr->fdbStore)
@@ -401,14 +388,14 @@ void grb_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtyp
 
 void grb_write_var(stream_t *streamptr, int varID, int memtype, const void *data, size_t nmiss)
 {
-  int vlistID  = streamptr->vlistID,
+  const int vlistID  = streamptr->vlistID,
     gridID   = vlistInqVarGrid(vlistID, varID),
     zaxisID  = vlistInqVarZaxis(vlistID, varID),
     nlevs    = zaxisInqSize(zaxisID);
-  size_t gridsize = gridInqSize(gridID);
-  double missval = vlistInqVarMissval(vlistID, varID);
+  const size_t gridsize = gridInqSize(gridID);
+  const double missval = vlistInqVarMissval(vlistID, varID);
 
-  size_t chunkLen = gridsize;
+  const size_t chunkLen = gridsize;
   if ( memtype == MEMTYPE_FLOAT )
     for ( int levelID = 0; levelID < nlevs; levelID++ )
       {
@@ -438,9 +425,8 @@ void grb_write_var(stream_t *streamptr, int varID, int memtype, const void *data
 
 void grb_write_record(stream_t *streamptr, int memtype, const void *data, size_t nmiss)
 {
-  int varID   = streamptr->record->varID;
-  int levelID = streamptr->record->levelID;
-
+  const int varID   = streamptr->record->varID;
+  const int levelID = streamptr->record->levelID;
   grb_write_var_slice(streamptr, varID, levelID, memtype, data, nmiss);
 }
 
diff --git a/src/gribapi.c b/src/gribapi.c
index 488dc9354..8268534fa 100644
--- a/src/gribapi.c
+++ b/src/gribapi.c
@@ -122,7 +122,7 @@ void gribContainersNew(stream_t * streamptr)
             }
 	}
 
-      streamptr->gribContainers = (void **) gribContainers;
+      streamptr->gribContainers = (void *) gribContainers;
 #else
       gribContainer_t *gribContainers
         = (gribContainer_t *) Malloc((size_t)nvars*sizeof(gribContainer_t));
diff --git a/src/stream.c b/src/stream.c
index 40ff8f076..593d49a3b 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -706,7 +706,7 @@ int streamOpenA(const char *filename, const char *filemode, int filetype)
 #endif
       {
         fileID = gribOpen(filename, filemode);
-        if ( fileID != CDI_UNDEFID ) gribContainersNew(streamptr);
+        if (fileID != CDI_UNDEFID) gribContainersNew(streamptr);
 	break;
       }
 #endif
-- 
GitLab