From a3eb4449f66753ba7616d6b15ff3c07c2c4db291 Mon Sep 17 00:00:00 2001
From: Uwe Schulzweida <uwe.schulzweida@mpimet.mpg.de>
Date: Mon, 23 Oct 2006 10:40:00 +0000
Subject: [PATCH] add SIGMA level support

---
 ChangeLog        |  1 +
 src/cdi.h        |  1 +
 src/griblib.c    | 34 ++++++++++++++++++----------------
 src/stream_grb.c | 16 ++++++++++++++++
 src/zaxis.c      |  1 +
 5 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 2f158a702..9818f7873 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,7 @@
 
         * using GRIB library version 1.0.3
 	* using EXSE library version 1.0.1
+	* add SIGMA level support
 	* gridPrint: extented
 	* Version 1.0.3 released
 
diff --git a/src/cdi.h b/src/cdi.h
index 1f0188891..68bfbb8ff 100644
--- a/src/cdi.h
+++ b/src/cdi.h
@@ -104,6 +104,7 @@ extern "C" {
 #define  ZAXIS_ISENTROPIC         8
 #define  ZAXIS_TRAJECTORY         9
 #define  ZAXIS_ALTITUDE          10
+#define  ZAXIS_SIGMA             11
 
 /* TAXIS types */
 
diff --git a/src/griblib.c b/src/griblib.c
index 6236deaea..7f431876d 100644
--- a/src/griblib.c
+++ b/src/griblib.c
@@ -1,5 +1,5 @@
 
-/* Generated automatically from m214003 on Wed Oct 11 10:35:23 CEST 2006 */
+/* Generated automatically from m214003 on Mon Oct 23 12:36:43 CEST 2006 */
 
 #if defined (HAVE_CONFIG_H)
 #  include "config.h"
@@ -3100,6 +3100,12 @@ int encodeBDS(GRIBPACK *lGrib, int *gribLen, int decscale, int *isec2, int *isec
       Message(func, "complex packing of spectral data unsupported, using simple packing!");
     }
 
+  if ( decscale )
+    {
+      double scale = pow(10.0, (double) decscale);
+      for ( i = 0; i < datasize; ++i ) data[i] *= scale;
+    }
+
   if ( ISEC2_GridType == GTYPE_SPECTRAL )
     {
       *datstart   = 15;
@@ -3124,12 +3130,6 @@ int encodeBDS(GRIBPACK *lGrib, int *gribLen, int decscale, int *isec2, int *isec
 
   Flag += unused_bits;
 
-  if ( decscale )
-    {
-      double scale = pow(10.0, (double) decscale);
-      for ( i = PackStart; i < datasize; ++i ) data[i] *= scale;
-    }
-
   fmin = fmax = data[PackStart];
 
 #if   defined (CRAY)
@@ -3360,6 +3360,9 @@ int encodeBDS(GRIBPACK *lGrib, int *gribLen, int decscale, int *isec2, int *isec
 	}
       if (cbits != 8) lGrib[z++] = c << cbits;
     }
+  else if ( ISEC4_NumBits == 0 )
+    {
+    }
   else
     {
       Error(func, "Unimplemented packing factor %d", ISEC4_NumBits);
@@ -4162,14 +4165,6 @@ static int decodeBDS(int decscale, unsigned char *bds, int *isec2, int *isec4, d
 	zscale = 1.0/intpow2(-jscale);
       else
 	zscale = intpow2(jscale);
-
-      if ( decscale )
-	{
-	  double scale;
-	  scale = pow(10.0, (double)-decscale);
-	  fmin   *= scale;
-	  zscale *= scale;
-	}
     }
 
   /* get number of bits in each data value. */
@@ -4424,6 +4419,13 @@ static int decodeBDS(int decscale, unsigned char *bds, int *isec2, int *isec4, d
       scaleComplex(fsec4, pcStart, pcScale, ISEC2_PentaJ);
     }
 
+
+  if ( decscale )
+    {
+      double scale = pow(10.0, (double)-decscale);
+      for ( i = 0; i < ISEC4_NumValues; i++ ) fsec4[i] *= scale;
+    }
+
   return (bdsLen);
 }
 
@@ -8041,7 +8043,7 @@ int  gribUnzip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbu
 
   return (gribLen);
 }
-static const char grb_libvers[] = "1.0.3" " of ""Oct 11 2006"" ""10:35:23";
+static const char grb_libvers[] = "1.0.3" " of ""Oct 23 2006"" ""12:36:44";
 
 
 
diff --git a/src/stream_grb.c b/src/stream_grb.c
index c0660d885..458a1e65a 100644
--- a/src/stream_grb.c
+++ b/src/stream_grb.c
@@ -341,6 +341,11 @@ int gribGetZaxisType(int grbleveltype)
 	leveltype = ZAXIS_ALTITUDE;
 	break;
       }
+    case LTYPE_SIGMA:
+      {
+	leveltype = ZAXIS_SIGMA;
+	break;
+      }
     case LTYPE_HYBRID:
     case LTYPE_HYBRID_LAYER:
       {
@@ -1876,6 +1881,17 @@ void grbDefLevel(int *isec1, int *isec2, double *fsec2, int zaxisID, int levelID
 	ISEC1_Level1    = ilevel;
 	ISEC1_Level2    = 0;
 
+	break;
+      }
+    case ZAXIS_SIGMA:
+      {
+	level = zaxisInqLevel(zaxisID, levelID);
+
+	ilevel = (int) level;
+	ISEC1_LevelType = LTYPE_SIGMA;
+	ISEC1_Level1    = ilevel;
+	ISEC1_Level2    = 0;
+
 	break;
       }
     case ZAXIS_DEPTH_BELOW_LAND:
diff --git a/src/zaxis.c b/src/zaxis.c
index 019e9e229..f552e93c2 100644
--- a/src/zaxis.c
+++ b/src/zaxis.c
@@ -29,6 +29,7 @@ LevelTypeEntry[] = {
   {"lev",     "isentropic",               "K"},
   {"lev",     "trajectory",               ""},
   {"alt",     "altitude",                 "m"},
+  {"lev",     "sigma",                    "level"},
 };
 
 static int CDI_MaxLeveltype = sizeof(LevelTypeEntry) / sizeof(LevelTypeEntry[0]);
-- 
GitLab