From 9ae756186506d922477dc195702d4f0e0bc24891 Mon Sep 17 00:00:00 2001
From: Uwe Schulzweida <uwe.schulzweida@mpimet.mpg.de>
Date: Mon, 30 Mar 2015 09:12:49 +0000
Subject: [PATCH] gribapiDefLevel: use function grib2DefLevel() to write grib2
 levels

---
 ChangeLog            |  4 +++
 src/stream_cdf.c     | 10 ++++--
 src/stream_gribapi.c | 74 +++++++++++++++++++++-----------------------
 3 files changed, 47 insertions(+), 41 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index ff73a853d..d7ac5cc67 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2015-03-30  Uwe Schulzweida
+
+	* gribapiDefLevel: use function grib2DefLevel() to write grib2 levels
+
 2015-03-29  Uwe Schulzweida
 
 	* vlistMerge: compare size of grids (bug fix)
diff --git a/src/stream_cdf.c b/src/stream_cdf.c
index f14546a75..81d00bdb6 100644
--- a/src/stream_cdf.c
+++ b/src/stream_cdf.c
@@ -4789,6 +4789,12 @@ int isDBLAxis(/*const char *units,*/ const char *longname)
   return (status);
 }
 
+static
+int unitsIsMeter(const char *units)
+{
+  return (*units == 'm' && (strcmp(units, "m") == 0 || strncmp(units, "meter", 5) == 0));
+}
+
 static
 int isDepthAxis(const char *stdname, const char *longname)
 {
@@ -5775,7 +5781,7 @@ void verify_coordinate_vars_1(int ndims, ncdim_t *ncdims, ncvar_t *ncvars, int t
                 {
                   ncvars[ncvarid].zaxistype = ZAXIS_DEPTH_BELOW_LAND;
 		}
-	      else if ( strcmp(ncvars[ncvarid].units, "m")   == 0 )
+	      else if ( unitsIsMeter(ncvars[ncvarid].units) )
 		{
 		  if ( isDepthAxis(ncvars[ncvarid].stdname, ncvars[ncvarid].longname) )
 		    ncvars[ncvarid].zaxistype = ZAXIS_DEPTH_BELOW_SEA;
@@ -5862,7 +5868,7 @@ void verify_coordinate_vars_2(int nvars, ncvar_t *ncvars)
                   ncvars[ncvarid].zaxistype = ZAXIS_DEPTH_BELOW_LAND;
 		  continue;
 		}
-	      else if ( strcmp(ncvars[ncvarid].units, "m")   == 0 )
+	      else if ( unitsIsMeter(ncvars[ncvarid].units) )
 		{
 		  if ( isDepthAxis(ncvars[ncvarid].stdname, ncvars[ncvarid].longname) )
 		    ncvars[ncvarid].zaxistype = ZAXIS_DEPTH_BELOW_SEA;
diff --git a/src/stream_gribapi.c b/src/stream_gribapi.c
index e158f6de2..e4faeeab0 100644
--- a/src/stream_gribapi.c
+++ b/src/stream_gribapi.c
@@ -2281,6 +2281,8 @@ void gribapiDefLevel(int editionNumber, grib_handle *gh, int param, int zaxisID,
       zaxisDefUnits(zaxisID, "Pa");
     }
 
+  int grib2ltype = zaxisTypeToGrib2ltype(zaxistype);
+
   switch (zaxistype)
     {
     case ZAXIS_SURFACE:
@@ -2292,11 +2294,14 @@ void gribapiDefLevel(int editionNumber, grib_handle *gh, int param, int zaxisID,
     case ZAXIS_ISENTROPIC:
       {
 	if ( editionNumber <= 1 )
-          gribapiDefLevelType(gh, gcinit, "indicatorOfTypeOfLevel", zaxisTypeToGrib1ltype(zaxistype));
+          {
+            gribapiDefLevelType(gh, gcinit, "indicatorOfTypeOfLevel", zaxisTypeToGrib1ltype(zaxistype));
+            GRIB_CHECK(my_grib_set_long(gh, "level", (long)level), 0);
+          }
         else
-          gribapiDefLevelType(gh, gcinit, "typeOfFirstFixedSurface", zaxisTypeToGrib2ltype(zaxistype));
-
-        GRIB_CHECK(my_grib_set_long(gh, "level", (long)level), 0);
+          {
+            grib2DefLevel(gh, gcinit, grib2ltype, grib2ltype, lbounds, level, dlevel1, dlevel2);
+          }
 
 	break;
       }
@@ -2312,27 +2317,22 @@ void gribapiDefLevel(int editionNumber, grib_handle *gh, int param, int zaxisID,
     case ZAXIS_MIX_LAYER:
     case ZAXIS_ATMOSPHERE:
       {
-        if ( lbounds )
+        if ( editionNumber <= 1 )
           {
-            if ( editionNumber <= 1 )
-              gribapiDefLevelType(gh, gcinit, "indicatorOfTypeOfLevel", zaxisTypeToGrib1ltype(zaxistype));
+            gribapiDefLevelType(gh, gcinit, "indicatorOfTypeOfLevel", zaxisTypeToGrib1ltype(zaxistype));
+            if ( lbounds )
+              {
+                GRIB_CHECK(my_grib_set_long(gh, "topLevel", (long) dlevel1), 0);
+                GRIB_CHECK(my_grib_set_long(gh, "bottomLevel", (long) dlevel2), 0);
+              }
             else
               {
-                gribapiDefLevelType(gh, gcinit, "typeOfFirstFixedSurface", zaxisTypeToGrib2ltype(zaxistype));
-                gribapiDefLevelType(gh, gcinit, "typeOfSecondFixedSurface", zaxisTypeToGrib2ltype(zaxistype));
+                GRIB_CHECK(my_grib_set_long(gh, "level", (long) level), 0);
               }
-
-            GRIB_CHECK(my_grib_set_long(gh, "topLevel", (long) dlevel1), 0);
-            GRIB_CHECK(my_grib_set_long(gh, "bottomLevel", (long) dlevel2), 0);
           }
         else
           {
-            if ( editionNumber <= 1 )
-              gribapiDefLevelType(gh, gcinit, "indicatorOfTypeOfLevel", zaxisTypeToGrib1ltype(zaxistype));
-            else
-              gribapiDefLevelType(gh, gcinit, "typeOfFirstFixedSurface", zaxisTypeToGrib2ltype(zaxistype));
-
-            GRIB_CHECK(my_grib_set_long(gh, "level", (long) level), 0);
+            grib2DefLevel(gh, gcinit, grib2ltype, grib2ltype, lbounds, level, dlevel1, dlevel2);
           }
 
         break;
@@ -2340,28 +2340,24 @@ void gribapiDefLevel(int editionNumber, grib_handle *gh, int param, int zaxisID,
     case ZAXIS_HYBRID:
     case ZAXIS_HYBRID_HALF:
       {
-	if ( lbounds )
-	  {
-	    if ( editionNumber <= 1 )
-	      gribapiDefLevelType(gh, gcinit, "indicatorOfTypeOfLevel", GRIB1_LTYPE_HYBRID_LAYER);
-            else
-	      {
-		gribapiDefLevelType(gh, gcinit, "typeOfFirstFixedSurface", GRIB2_LTYPE_HYBRID);
-		gribapiDefLevelType(gh, gcinit, "typeOfSecondFixedSurface", GRIB2_LTYPE_HYBRID);
-	      }
-
-	    GRIB_CHECK(my_grib_set_long(gh, "topLevel", (long) dlevel1), 0);
-	    GRIB_CHECK(my_grib_set_long(gh, "bottomLevel", (long) dlevel2), 0);
-	  }
-	else
-	  {
-	    if ( editionNumber <= 1 )
-              gribapiDefLevelType(gh, gcinit, "indicatorOfTypeOfLevel", GRIB1_LTYPE_HYBRID);
+        if ( editionNumber <= 1 )
+          {
+            if ( lbounds )
+              {
+                gribapiDefLevelType(gh, gcinit, "indicatorOfTypeOfLevel", GRIB1_LTYPE_HYBRID_LAYER);
+                GRIB_CHECK(my_grib_set_long(gh, "topLevel", (long) dlevel1), 0);
+                GRIB_CHECK(my_grib_set_long(gh, "bottomLevel", (long) dlevel2), 0);
+              }
             else
-              gribapiDefLevelType(gh, gcinit, "typeOfFirstFixedSurface", GRIB2_LTYPE_HYBRID);
-
-	    GRIB_CHECK(my_grib_set_long(gh, "level", (long) level), 0);
-	  }
+              {
+                gribapiDefLevelType(gh, gcinit, "indicatorOfTypeOfLevel", GRIB1_LTYPE_HYBRID);
+                GRIB_CHECK(my_grib_set_long(gh, "level", (long) level), 0);
+              }
+          }
+        else
+          {
+            grib2DefLevel(gh, gcinit, GRIB2_LTYPE_HYBRID, GRIB2_LTYPE_HYBRID, lbounds, level, dlevel1, dlevel2);
+          }
 
         if ( !gcinit )
           {
-- 
GitLab