From c09a551007162a486f238bec503c06feb43d845d Mon Sep 17 00:00:00 2001
From: Uwe Schulzweida <uwe.schulzweida@mpimet.mpg.de>
Date: Fri, 3 Feb 2017 16:10:47 +0100
Subject: [PATCH] GRIB: Check Curvilinear grid for projection.

---
 src/stream_cgribex.c | 25 +++++++++++++++++--------
 src/stream_gribapi.c | 31 ++++++++++++++++++++-----------
 2 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/src/stream_cgribex.c b/src/stream_cgribex.c
index 9263609d1..d7afade35 100644
--- a/src/stream_cgribex.c
+++ b/src/stream_cgribex.c
@@ -1636,14 +1636,23 @@ void cgribexDefGrid(int *isec1, int *isec2, double *fsec2, int *isec4, int gridI
     }
   else if ( gridtype == GRID_CURVILINEAR )
     {
-      static bool lwarning = true;
-      if ( lwarning && gridInqSize(gridID) > 1 )
-	{
-	  lwarning = false;
-	  Warning("Curvilinear grids are unsupported in GRIB1! Created wrong Grid Description Section!");
-	}
-      gridtype = GRID_LONLAT;
-      lcurvi = true;
+      int projID = gridInqProj(gridID);
+      if ( projID != CDI_UNDEFID && gridInqType(projID) == GRID_PROJECTION )
+        {
+          gridID = projID;
+          gridtype = GRID_PROJECTION;
+        }
+      else
+        {
+          static bool lwarning = true;
+          if ( lwarning && gridInqSize(gridID) > 1 )
+            {
+              lwarning = false;
+              Warning("Curvilinear grid is unsupported in GRIB1! Created wrong Grid Description Section!");
+            }
+          lcurvi = true;
+          gridtype = GRID_LONLAT;
+        }
     }
 
   if ( gridtype == GRID_PROJECTION && gridInqProjType(gridID) == CDI_PROJ_RLL )
diff --git a/src/stream_gribapi.c b/src/stream_gribapi.c
index 759b85452..8dd6a920b 100644
--- a/src/stream_gribapi.c
+++ b/src/stream_gribapi.c
@@ -20,10 +20,10 @@
 #include "subtype.h"
 
 
-#  include "cgribex.h"      /* gribGetSize, gribRead, gribGetZip, GRIB1_LTYPE_99 */
-#  include "gribapi.h"
+#include "cgribex.h"      /* gribGetSize, gribRead, gribGetZip, GRIB1_LTYPE_99 */
+#include "gribapi.h"
 
-#  include <grib_api.h>
+#include <grib_api.h>
 
 extern int cdiInventoryMode;
 
@@ -1918,14 +1918,23 @@ void gribapiDefGrid(int editionNumber, grib_handle *gh, int gridID, int comptype
     }
   else if ( gridtype == GRID_CURVILINEAR )
     {
-      static bool lwarning = true;
-      if ( lwarning && gridsize > 1 )
-	{
-	  lwarning = false;
-	  Warning("Curvilinear grids are unsupported in GRIB format! Created wrong Grid Description Section!");
-	}
-      gridtype = GRID_LONLAT;
-      lcurvi = true;
+      int projID = gridInqProj(gridID);
+      if ( projID != CDI_UNDEFID && gridInqType(projID) == GRID_PROJECTION )
+        {
+          gridID = projID;
+          gridtype = GRID_PROJECTION;
+        }
+      else
+        {
+          static bool lwarning = true;
+          if ( lwarning && gridsize > 1 )
+            {
+              lwarning = false;
+              Warning("Curvilinear grid is unsupported in GRIB format! Created wrong Grid Description Section!");
+            }
+          lcurvi = true;
+          gridtype = GRID_LONLAT;
+        }
     }
 
   if ( gridtype == GRID_PROJECTION && gridInqProjType(gridID) == CDI_PROJ_RLL )
-- 
GitLab