From d0d13cdddaf4b41ea60231a65acfbf5c910f4c93 Mon Sep 17 00:00:00 2001
From: Uwe Schulzweida <uwe.schulzweida@mpimet.mpg.de>
Date: Fri, 10 Feb 2017 15:01:25 +0100
Subject: [PATCH] Added cdiSortParam.

---
 src/cdi_int.c      |  5 +++++
 src/cdi_int.h      |  4 +++-
 src/stream.c       |  3 ++-
 src/stream_cdf_i.c |  1 +
 src/varscan.c      | 20 ++++++++------------
 5 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/src/cdi_int.c b/src/cdi_int.c
index 1ed0932e0..0894198fd 100644
--- a/src/cdi_int.c
+++ b/src/cdi_int.c
@@ -67,6 +67,7 @@ int cdiGribApiDebug     = 0;
 int cdiDefaultLeveltype = -1;
 int cdiDataUnreduced = 0;
 int cdiSortName = 0;
+int cdiSortParam = 0;
 int cdiHaveMissval = 0;
 
 
@@ -297,6 +298,9 @@ void cdiInitialize(void)
       value = cdiGetenvInt("CDI_SORTNAME");
       if ( value >= 0 ) cdiSortName = (int) value;
 
+      value = cdiGetenvInt("CDI_SORTPARAM");
+      if ( value >= 0 ) cdiSortParam = (int) value;
+
       value = cdiGetenvInt("CDI_HAVE_MISSVAL");
       if ( value >= 0 ) cdiHaveMissval = (int) value;
 
@@ -420,6 +424,7 @@ void cdiDefGlobal(const char *string, int val)
   if      ( strcmp(string, "REGULARGRID")      == 0 ) cdiDataUnreduced = val;
   else if ( strcmp(string, "GRIBAPI_DEBUG")    == 0 ) cdiGribApiDebug = val;
   else if ( strcmp(string, "SORTNAME")         == 0 ) cdiSortName = val;
+  else if ( strcmp(string, "SORTPARAM")        == 0 ) cdiSortParam = val;
   else if ( strcmp(string, "HAVE_MISSVAL")     == 0 ) cdiHaveMissval = val;
   else if ( strcmp(string, "NC_CHUNKSIZEHINT") == 0 ) cdiNcChunksizehint = val;
   else if ( strcmp(string, "CMOR_MODE")        == 0 ) CDI_cmor_mode = val;
diff --git a/src/cdi_int.h b/src/cdi_int.h
index 3df41a5db..f01011e8e 100644
--- a/src/cdi_int.h
+++ b/src/cdi_int.h
@@ -254,10 +254,11 @@ typedef struct {
   int         globalatts;
   int         localatts;
   int         unreduced;
-  int         sortname;
   int         have_missval;
   int         comptype;      // compression type
   int         complevel;     // compression level
+  bool        sortname;
+  bool        sortparam;
 #if defined (GRIBCONTAINER2D)
   void      **gribContainers;
 #else
@@ -317,6 +318,7 @@ extern int cdiChunkType;
 extern int cdiSplitLtype105;
 extern int cdiDataUnreduced;
 extern int cdiSortName;
+extern int cdiSortParam;
 extern int cdiHaveMissval;
 extern int cdiIgnoreAttCoordinates;
 extern int cdiIgnoreValidRange;
diff --git a/src/stream.c b/src/stream.c
index 85d536145..844d706e2 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -872,7 +872,8 @@ void streamDefaultValue ( stream_t * streamptr )
   streamptr->globalatts        = 0;
   streamptr->localatts         = 0;
   streamptr->unreduced         = cdiDataUnreduced;
-  streamptr->sortname          = cdiSortName;
+  streamptr->sortname          = cdiSortName > 0;
+  streamptr->sortparam         = cdiSortParam > 0;
   streamptr->have_missval      = cdiHaveMissval;
   streamptr->comptype          = CDI_COMPRESS_NONE;
   streamptr->complevel         = 0;
diff --git a/src/stream_cdf_i.c b/src/stream_cdf_i.c
index 5df70a449..c3fa8b00d 100644
--- a/src/stream_cdf_i.c
+++ b/src/stream_cdf_i.c
@@ -2894,6 +2894,7 @@ void cdf_define_all_zaxes(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
     }
 }
 
+
 struct varinfo
 {
   int      ncvarid;
diff --git a/src/varscan.c b/src/varscan.c
index 6b3d99a66..01942f05d 100644
--- a/src/varscan.c
+++ b/src/varscan.c
@@ -534,20 +534,16 @@ int cmpLevelTableInv(const void* s1, const void* s2)
 }
 
 
-typedef struct
+struct paraminfo
 {
-  int      varid;
-  int      param;
-  int      ltype;
-}
-param_t;
-
+  int varid, param, ltype;
+};
 
 static
 int cmpparam(const void* s1, const void* s2)
 {
-  const param_t *x = (const param_t*) s1;
-  const param_t *y = (const param_t*) s2;
+  const struct paraminfo *x = (const struct paraminfo*) s1;
+  const struct paraminfo *y = (const struct paraminfo*) s2;
 
   int cmp = (( x->param > y->param ) - ( x->param < y->param )) * 2
            + ( x->ltype > y->ltype ) - ( x->ltype < y->ltype );
@@ -564,9 +560,9 @@ void cdi_generate_vars(stream_t *streamptr)
   int *varids = (int *) Malloc(nvars*sizeof(int));
   for ( unsigned varID = 0; varID < nvars; varID++ ) varids[varID] = (int)varID;
 
-  if ( streamptr->sortname )
+  if ( streamptr->sortparam )
     {
-      param_t *varInfo = (param_t *) Malloc((size_t)nvars * sizeof (param_t));
+      struct paraminfo *varInfo = (struct paraminfo *) Malloc((size_t)nvars * sizeof(struct paraminfo));
 
       for ( unsigned varID = 0; varID < nvars; varID++ )
 	{
@@ -574,7 +570,7 @@ void cdi_generate_vars(stream_t *streamptr)
 	  varInfo[varID].param = vartable[varID].param;
 	  varInfo[varID].ltype = vartable[varID].ltype1;
 	}
-      qsort(varInfo, (size_t)nvars, sizeof(param_t), cmpparam);
+      qsort(varInfo, (size_t)nvars, sizeof(struct paraminfo), cmpparam);
       for ( unsigned varID = 0; varID < nvars; varID++ )
 	{
 	  varids[varID] = varInfo[varID].varid;
-- 
GitLab