diff --git a/ChangeLog b/ChangeLog
index b8a7bc2c2168f89def3bbc7d9bebb78b56ba708d..147e1a2b1dc672dd815fd58ef937e2a8f4e557c4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,24 @@
+2016-11-04  Uwe Schulzweida
+
+	* Version 1.8.0rc4 released
+
+2016-11-04  Uwe Schulzweida
+
+	* cgribexVarCompare: bug fix for different kind of instant step types
+
+2016-10-10  Uwe Schulzweida
+
+	* NetCDF: added support for grid datatype integer
+	* NetCDF: added support for proj coordinate without mapping attribute
+
+2016-10-04  Uwe Schulzweida
+
+	* Version 1.8.0rc3 released
+
+2016-10-04  Uwe Schulzweida
+
+	* Changed default name of pressure levels to plev
+
 2016-09-26  Uwe Schulzweida
 
 	* Added read support for hybrid sigma pressure coordinate with formula term P0
diff --git a/NEWS b/NEWS
index 148b2e321acd7ef3f50c8a4c2cb02d8e051c6d2e..487f9727fb9c78fa975780e9ec369fbbafc9b551 100644
--- a/NEWS
+++ b/NEWS
@@ -1,7 +1,15 @@
 CDI NEWS
 --------
 
+Version 1.8.0 (26 October 2016):
 
+   New features:
+     * Refactor horizontal and vertical grid module
+     * netCDF4: added support for attribute type NC_BYTE, NC_UBYTE, NC_USHORT and NC_UINT
+   Fixed bugs:
+     * iegWriteVarSliceDP: does not work (bug fix)
+     * streamOpenAppend: call to gribContainersNew() missing (bug fix) [Bug #6944]
+        
 Version 1.7.0 (27 October 2015):
 
    New features:
diff --git a/configure b/configure
index 341a56f937cab1a7b80e279ba28c22f06e71bebc..eb2ea8246391faa006ae1970f805ac02ee7b58cb 100755
--- a/configure
+++ b/configure
@@ -1,6 +1,6 @@
 #! /bin/sh
 # Guess values for system-dependent variables and create Makefiles.
-# Generated by GNU Autoconf 2.68 for cdi 1.8.0rc3.
+# Generated by GNU Autoconf 2.68 for cdi 1.8.0rc4.
 #
 # Report bugs to <http://mpimet.mpg.de/cdi>.
 #
@@ -570,8 +570,8 @@ MAKEFLAGS=
 # Identity of this package.
 PACKAGE_NAME='cdi'
 PACKAGE_TARNAME='cdi'
-PACKAGE_VERSION='1.8.0rc3'
-PACKAGE_STRING='cdi 1.8.0rc3'
+PACKAGE_VERSION='1.8.0rc4'
+PACKAGE_STRING='cdi 1.8.0rc4'
 PACKAGE_BUGREPORT='http://mpimet.mpg.de/cdi'
 PACKAGE_URL=''
 
@@ -1460,7 +1460,7 @@ if test "$ac_init_help" = "long"; then
   # Omit some internal or obsolete options to make the list less imposing.
   # This message is too long to be a string in the A/UX 3.1 sh.
   cat <<_ACEOF
-\`configure' configures cdi 1.8.0rc3 to adapt to many kinds of systems.
+\`configure' configures cdi 1.8.0rc4 to adapt to many kinds of systems.
 
 Usage: $0 [OPTION]... [VAR=VALUE]...
 
@@ -1530,7 +1530,7 @@ fi
 
 if test -n "$ac_init_help"; then
   case $ac_init_help in
-     short | recursive ) echo "Configuration of cdi 1.8.0rc3:";;
+     short | recursive ) echo "Configuration of cdi 1.8.0rc4:";;
    esac
   cat <<\_ACEOF
 
@@ -1723,7 +1723,7 @@ fi
 test -n "$ac_init_help" && exit $ac_status
 if $ac_init_version; then
   cat <<\_ACEOF
-cdi configure 1.8.0rc3
+cdi configure 1.8.0rc4
 generated by GNU Autoconf 2.68
 
 Copyright (C) 2010 Free Software Foundation, Inc.
@@ -2568,7 +2568,7 @@ cat >config.log <<_ACEOF
 This file contains any messages produced by compilers while
 running configure, to aid debugging if configure makes a mistake.
 
-It was created by cdi $as_me 1.8.0rc3, which was
+It was created by cdi $as_me 1.8.0rc4, which was
 generated by GNU Autoconf 2.68.  Invocation command line was
 
   $ $0 $@
@@ -3520,7 +3520,7 @@ fi
 
 # Define the identity of the package.
  PACKAGE='cdi'
- VERSION='1.8.0rc3'
+ VERSION='1.8.0rc4'
 
 
 cat >>confdefs.h <<_ACEOF
@@ -23389,7 +23389,7 @@ Usage: $0 [OPTIONS]
 Report bugs to <bug-libtool@gnu.org>."
 
 lt_cl_version="\
-cdi config.lt 1.8.0rc3
+cdi config.lt 1.8.0rc4
 configured by $0, generated by GNU Autoconf 2.68.
 
 Copyright (C) 2011 Free Software Foundation, Inc.
@@ -30522,7 +30522,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 # report actual input values of CONFIG_FILES etc. instead of their
 # values after options handling.
 ac_log="
-This file was extended by cdi $as_me 1.8.0rc3, which was
+This file was extended by cdi $as_me 1.8.0rc4, which was
 generated by GNU Autoconf 2.68.  Invocation command line was
 
   CONFIG_FILES    = $CONFIG_FILES
@@ -30588,7 +30588,7 @@ _ACEOF
 cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
 ac_cs_version="\\
-cdi config.status 1.8.0rc3
+cdi config.status 1.8.0rc4
 configured by $0, generated by GNU Autoconf 2.68,
   with options \\"\$ac_cs_config\\"
 
diff --git a/configure.ac b/configure.ac
index 3ba7700cef8e052ede63899580918f70389a67ec..6c575ac04af93c601ba635650c1f9ecd4d8cddd4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -4,7 +4,7 @@
 #  autoconf 2.68
 #  libtool  2.4.2
 
-AC_INIT([cdi], [1.8.0rc3], [http://mpimet.mpg.de/cdi])
+AC_INIT([cdi], [1.8.0rc4], [http://mpimet.mpg.de/cdi])
 
 AC_DEFINE_UNQUOTED(CDI, ["$PACKAGE_VERSION"], [CDI version])
 
diff --git a/doc/tex/c_quick_ref.tex b/doc/tex/c_quick_ref.tex
index 4b3c46f27f609666bc5f7a5694fc51d701dfa6c8..f84540a742de35270ba3c94c97528d939cf120b9 100644
--- a/doc/tex/c_quick_ref.tex
+++ b/doc/tex/c_quick_ref.tex
@@ -162,15 +162,6 @@ Define the precision of a Grid.
 Define the projection ID of a Grid.
 
 
-\section*{\tt \htmlref{gridDefProjType}{gridDefProjType}}
-
-\begin{verbatim}
-    void gridDefProjType (int gridID, int projtype);
-\end{verbatim}
-
-Define the projection type.
-
-
 \section*{\tt \htmlref{gridDefReference}{gridDefReference}}
 
 \begin{verbatim}
diff --git a/doc/tex/f_quick_ref.tex b/doc/tex/f_quick_ref.tex
index e54fc2443b5f687f9803483c7a4bbc7e3a1767d2..c43127e1ae6c7c1fc4194c1005ba1ac3d43177db 100644
--- a/doc/tex/f_quick_ref.tex
+++ b/doc/tex/f_quick_ref.tex
@@ -164,15 +164,6 @@ Define the precision of a Grid.
 Define the projection ID of a Grid.
 
 
-\section*{\tt \htmlref{gridDefProjType}{gridDefProjType}}
-
-\begin{verbatim}
-    SUBROUTINE gridDefProjType (INTEGER gridID, INTEGER projtype)
-\end{verbatim}
-
-Define the projection type.
-
-
 \section*{\tt \htmlref{gridDefReference}{gridDefReference}}
 
 \begin{verbatim}
diff --git a/src/Makefile.in b/src/Makefile.in
index be115a80ebafb926b136c7920686d5963e4ee8e5..234a5d46f45d3ac46d56f25594a163057b0e4ff5 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -941,8 +941,8 @@ distclean-generic:
 maintainer-clean-generic:
 	@echo "This command is intended for maintainers to use"
 	@echo "it deletes files that may require special tools to rebuild."
-@ENABLE_CDI_LIB_FALSE@uninstall-local:
 @ENABLE_CDI_LIB_FALSE@install-exec-local:
+@ENABLE_CDI_LIB_FALSE@uninstall-local:
 clean: clean-am
 
 clean-am: clean-generic clean-libLTLIBRARIES clean-libtool \
diff --git a/src/basetime.c b/src/basetime.c
index ff4cd8aad7fb405d0cd3e736f4671007a9fae9b8..2efc45d3a1782f66d47ddaaae5b824f494c2b399 100644
--- a/src/basetime.c
+++ b/src/basetime.c
@@ -9,18 +9,16 @@
 #include "cdi.h"
 #include "basetime.h"
 
-#undef  UNDEFID
-#define UNDEFID  CDI_UNDEFID
 
 void basetimeInit(basetime_t *basetime)
 {
   if ( basetime == NULL )
     Error("Internal problem! Basetime not allocated.");
 
-  basetime->ncvarid       = UNDEFID;
-  basetime->ncdimid       = UNDEFID;
-  basetime->ncvarboundsid = UNDEFID;
-  basetime->leadtimeid    = UNDEFID;
+  basetime->ncvarid       = CDI_UNDEFID;
+  basetime->ncdimid       = CDI_UNDEFID;
+  basetime->ncvarboundsid = CDI_UNDEFID;
+  basetime->leadtimeid    = CDI_UNDEFID;
   basetime->lwrf          = false;
   basetime->timevar_cache = NULL;
 }
diff --git a/src/cdf_lazy_grid.c b/src/cdf_lazy_grid.c
index fa5c6b0514b3b961a84074d8bf1ac8c28c5e3cd2..37bdabd67fbbc5a689a97e4f8dd64882769bb668 100644
--- a/src/cdf_lazy_grid.c
+++ b/src/cdf_lazy_grid.c
@@ -1,3 +1,7 @@
+#if defined (HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
 #include "stream_cdf.h"
 #include "cdf_lazy_grid.h"
 
@@ -24,6 +28,8 @@ static bool cdfLazyInitialized;
 #endif
 
 
+#ifdef HAVE_LIBNETCDF
+
 void cdfLazyGridDestroy(struct cdfLazyGrid *lazyGrid)
 {
   if (lazyGrid->base.area == cdfPendingLoad)  lazyGrid->base.area = NULL;
@@ -515,6 +521,9 @@ void cdfBaseGridRenew(struct cdfLazyGrid *restrict *restrict gridpptr, int gridt
     *gridpptr = grid = (struct cdfLazyGrid *)Malloc(sizeof (grid_t));
   cdfBaseGridInit((grid_t*)grid, gridtype);
 }
+
+#endif
+
 /*
  * Local Variables:
  * c-file-style: "Java"
diff --git a/src/cdf_read.c b/src/cdf_read.c
index 66eabc2fca9c943d04b9b889535d5e33f50c2724..ba33aad3674633772aff70d745508486c96e59d9 100644
--- a/src/cdf_read.c
+++ b/src/cdf_read.c
@@ -16,10 +16,6 @@
 #include "vlist.h"
 
 
-#undef  UNDEFID
-#define UNDEFID  CDI_UNDEFID
-
-
 static
 void cdfReadGridTraj(stream_t *streamptr, int gridID)
 {
@@ -27,8 +23,8 @@ void cdfReadGridTraj(stream_t *streamptr, int gridID)
   int fileID  = streamptr->fileID;
 
   int gridindex = vlistGridIndex(vlistID, gridID);
-  int lonID = streamptr->xdimID[gridindex];
-  int latID = streamptr->ydimID[gridindex];
+  int lonID = streamptr->ncgrid[gridindex].xdimID;
+  int latID = streamptr->ncgrid[gridindex].ydimID;
 
   int tsID = streamptr->curTsID;
   size_t index = (size_t)tsID;
@@ -53,15 +49,15 @@ void cdfGetSlapDescription(stream_t *streamptr, int varID, size_t (*start)[4], s
 
   if ( CDI_Debug ) Message("tsID = %d", tsID);
 
-  int xid = UNDEFID, yid = UNDEFID;
+  int xid = CDI_UNDEFID, yid = CDI_UNDEFID;
   if ( gridInqType(gridID) == GRID_TRAJECTORY )
     {
       cdfReadGridTraj(streamptr, gridID);
     }
   else
     {
-      xid = streamptr->xdimID[gridindex];
-      yid = streamptr->ydimID[gridindex];
+      xid = streamptr->ncgrid[gridindex].xdimID;
+      yid = streamptr->ncgrid[gridindex].ydimID;
     }
   int zaxisindex = vlistZaxisIndex(vlistID, zaxisID);
   int zid = streamptr->zaxisID[zaxisindex];
@@ -74,9 +70,9 @@ void cdfGetSlapDescription(stream_t *streamptr, int varID, size_t (*start)[4], s
       ndims++; \
     } while(0)
   if ( tsteptype != TSTEP_CONSTANT ) addDimension((size_t)tsID, 1);
-  if ( zid != UNDEFID ) addDimension(0, (size_t)zaxisInqSize(zaxisID));
-  if ( yid != UNDEFID ) addDimension(0, (size_t)gridInqYsize(gridID));
-  if ( xid != UNDEFID ) addDimension(0, (size_t)gridInqXsize(gridID));
+  if ( zid != CDI_UNDEFID ) addDimension(0, (size_t)zaxisInqSize(zaxisID));
+  if ( yid != CDI_UNDEFID ) addDimension(0, (size_t)gridInqYsize(gridID));
+  if ( xid != CDI_UNDEFID ) addDimension(0, (size_t)gridInqXsize(gridID));
 #undef addDimension
 
   assert(ndims <= (int)(sizeof(*start)/sizeof(**start)));
@@ -365,7 +361,7 @@ void cdfInqDimIds(stream_t *streamptr, int varId, int (*outDimIds)[3])
   int gridId = vlistInqVarGrid(streamptr->vlistID, varId);
   int gridindex = vlistGridIndex(streamptr->vlistID, gridId);
 
-  (*outDimIds)[0] = (*outDimIds)[1] = (*outDimIds)[2] = UNDEFID;
+  (*outDimIds)[0] = (*outDimIds)[1] = (*outDimIds)[2] = CDI_UNDEFID;
   switch ( gridInqType(gridId) )
     {
       case GRID_TRAJECTORY:
@@ -373,12 +369,12 @@ void cdfInqDimIds(stream_t *streamptr, int varId, int (*outDimIds)[3])
         break;
 
       case GRID_UNSTRUCTURED:
-        (*outDimIds)[0] = streamptr->xdimID[gridindex];
+        (*outDimIds)[0] = streamptr->ncgrid[gridindex].xdimID;
         break;
 
       default:
-        (*outDimIds)[0] = streamptr->xdimID[gridindex];
-        (*outDimIds)[1] = streamptr->ydimID[gridindex];
+        (*outDimIds)[0] = streamptr->ncgrid[gridindex].xdimID;
+        (*outDimIds)[1] = streamptr->ncgrid[gridindex].ydimID;
         break;
     }
 
@@ -390,8 +386,8 @@ void cdfInqDimIds(stream_t *streamptr, int varId, int (*outDimIds)[3])
 static
 int cdfGetSkipDim(int fileId, int ncvarid, int (*dimIds)[3])
 {
-  if((*dimIds)[0] != UNDEFID) return 0;
-  if((*dimIds)[1] != UNDEFID) return 0;
+  if((*dimIds)[0] != CDI_UNDEFID) return 0;
+  if((*dimIds)[1] != CDI_UNDEFID) return 0;
   int nvdims;
   cdf_inq_varndims(fileId, ncvarid, &nvdims);
   if(nvdims != 3) return 0;
@@ -436,7 +432,7 @@ void cdfGetSliceSlapDescription(stream_t *streamptr, int varId, int levelId, boo
   int dimorder[3];
   vlistInqVarDimorder(vlistId, varId, &dimorder);
 
-  *outSwapXY = (dimorder[2] == 2 || dimorder[0] == 1) && dimIds[0] != UNDEFID && dimIds[1] != UNDEFID ;
+  *outSwapXY = (dimorder[2] == 2 || dimorder[0] == 1) && dimIds[0] != CDI_UNDEFID && dimIds[1] != CDI_UNDEFID ;
 
   int ndims = 0;
 
@@ -453,7 +449,7 @@ void cdfGetSliceSlapDescription(stream_t *streamptr, int varId, int levelId, boo
     {
       size_t size;
       int curDimId = dimIds[dimorder[id]-1];
-      if ( curDimId == UNDEFID ) continue;
+      if ( curDimId == CDI_UNDEFID ) continue;
       switch ( dimorder[id] )
         {
           Error("Internal errror: Malformed dimension order encountered. Please report this bug.\n");
diff --git a/src/cdf_write.c b/src/cdf_write.c
index ca981748ac727ec1d2c076ca5aa68b91eba32beb..8e0d3b8db8fceb6d8e36f702d94e92d72e95718b 100644
--- a/src/cdf_write.c
+++ b/src/cdf_write.c
@@ -13,10 +13,6 @@
 #include "vlist.h"
 
 
-#undef  UNDEFID
-#define UNDEFID  CDI_UNDEFID
-
-
 void cdfDefVarDeflate(int ncid, int ncvarid, int deflate_level)
 {
 #if  defined(HAVE_NETCDF4)
@@ -41,7 +37,7 @@ void cdfDefVarDeflate(int ncid, int ncvarid, int deflate_level)
 #endif
 }
 
-static
+
 int cdfDefDatatype(int datatype, int filetype)
 {
   int xtype = NC_FLOAT;
@@ -64,7 +60,7 @@ int cdfDefDatatype(int datatype, int filetype)
       else if ( datatype == CDI_DATATYPE_UINT32 ) xtype = NC_INT;
 #endif
       else if ( datatype == CDI_DATATYPE_FLT64  ) xtype = NC_DOUBLE;
-      else                                    xtype = NC_FLOAT;
+      else                                        xtype = NC_FLOAT;
     }
   else
     {
@@ -75,7 +71,7 @@ int cdfDefDatatype(int datatype, int filetype)
       else if ( datatype == CDI_DATATYPE_UINT16 ) xtype = NC_INT;
       else if ( datatype == CDI_DATATYPE_UINT32 ) xtype = NC_INT;
       else if ( datatype == CDI_DATATYPE_FLT64  ) xtype = NC_DOUBLE;
-      else                                    xtype = NC_FLOAT;
+      else                                        xtype = NC_FLOAT;
     }
 
   return xtype;
@@ -113,7 +109,7 @@ void cdfDefInstitut(stream_t *streamptr)
   int fileID  = streamptr->fileID;
   int instID  = vlistInqInstitut(vlistID);
 
-  if ( instID != UNDEFID )
+  if ( instID != CDI_UNDEFID )
     {
       const char *longname = institutInqLongnamePtr(instID);
       if ( longname )
@@ -136,7 +132,7 @@ void cdfDefSource(stream_t *streamptr)
   int fileID  = streamptr->fileID;
   int modelID = vlistInqModel(vlistID);
 
-  if ( modelID != UNDEFID )
+  if ( modelID != CDI_UNDEFID )
     {
       const char *longname = modelInqNamePtr(modelID);
       if ( longname )
@@ -257,7 +253,7 @@ void cdfDefLocalAtts(stream_t *streamptr)
   int fileID  = streamptr->fileID;
 
   if ( streamptr->localatts ) return;
-  if ( vlistInqInstitut(vlistID) != UNDEFID ) return;
+  if ( vlistInqInstitut(vlistID) != CDI_UNDEFID ) return;
 
   streamptr->localatts = 1;
 
@@ -266,7 +262,7 @@ void cdfDefLocalAtts(stream_t *streamptr)
   for ( int varID = 0; varID < streamptr->nvars; varID++ )
     {
       int instID = vlistInqVarInstitut(vlistID, varID);
-      if ( instID != UNDEFID )
+      if ( instID != CDI_UNDEFID )
 	{
           int ncvarid = streamptr->vars[varID].ncvarid;
   	  const char *name = institutInqNamePtr(instID);
@@ -320,7 +316,7 @@ static
 int cdfDefVar(stream_t *streamptr, int varID)
 {
   int ncvarid = -1;
-  int xid = UNDEFID, yid = UNDEFID;
+  int xid = CDI_UNDEFID, yid = CDI_UNDEFID;
   size_t xsize = 0, ysize = 0;
   char varname[CDI_MAX_NAME];
   int dims[4];
@@ -338,7 +334,7 @@ int cdfDefVar(stream_t *streamptr, int varID)
   if ( CDI_Debug )
     Message("streamID = %d, fileID = %d, varID = %d", streamptr->self, fileID, varID);
 
-  if ( streamptr->vars[varID].ncvarid != UNDEFID )
+  if ( streamptr->vars[varID].ncvarid != CDI_UNDEFID )
     return streamptr->vars[varID].ncvarid;
 
   int vlistID   = streamptr->vlistID;
@@ -362,18 +358,18 @@ int cdfDefVar(stream_t *streamptr, int varID)
     {
       xid = streamptr->ncgrid[gridindex].xdimID;
       yid = streamptr->ncgrid[gridindex].ydimID;
-      if ( xid != UNDEFID ) cdf_inq_dimlen(fileID, xid, &xsize);
-      if ( yid != UNDEFID ) cdf_inq_dimlen(fileID, yid, &ysize);
+      if ( xid != CDI_UNDEFID ) cdf_inq_dimlen(fileID, xid, &xsize);
+      if ( yid != CDI_UNDEFID ) cdf_inq_dimlen(fileID, yid, &ysize);
     }
 
   int zaxisindex = vlistZaxisIndex(vlistID, zaxisID);
   int zid = streamptr->zaxisID[zaxisindex];
   bool zaxis_is_scalar = false;
-  if ( zid == UNDEFID ) zaxis_is_scalar = zaxisInqScalar(zaxisID) > 0;
+  if ( zid == CDI_UNDEFID ) zaxis_is_scalar = zaxisInqScalar(zaxisID) > 0;
 
   if ( dimorder[0] != 3 ) lchunk = false; /* ZYX and ZXY */
 
-  if ( ((dimorder[0]>0)+(dimorder[1]>0)+(dimorder[2]>0)) < ((xid!=UNDEFID)+(yid!=UNDEFID)+(zid!=UNDEFID)) )
+  if ( ((dimorder[0]>0)+(dimorder[1]>0)+(dimorder[2]>0)) < ((xid!=CDI_UNDEFID)+(yid!=CDI_UNDEFID)+(zid!=CDI_UNDEFID)) )
     {
       printf("zid=%d  yid=%d  xid=%d\n", zid, yid, xid);
       Error("Internal problem, dimension order missing!");
@@ -383,32 +379,32 @@ int cdfDefVar(stream_t *streamptr, int varID)
 
   if ( tsteptype != TSTEP_CONSTANT )
     {
-      if ( tid == UNDEFID ) Error("Internal problem, time undefined!");
+      if ( tid == CDI_UNDEFID ) Error("Internal problem, time undefined!");
       chunks[ndims] = 1;
       dims[ndims++] = tid;
       axis[iax++] = 'T';
     }
   /*
-  if ( zid != UNDEFID ) axis[iax++] = 'Z';
-  if ( zid != UNDEFID ) chunks[ndims] = 1;
-  if ( zid != UNDEFID ) dims[ndims++] = zid;
+  if ( zid != CDI_UNDEFID ) axis[iax++] = 'Z';
+  if ( zid != CDI_UNDEFID ) chunks[ndims] = 1;
+  if ( zid != CDI_UNDEFID ) dims[ndims++] = zid;
 
-  if ( yid != UNDEFID ) chunks[ndims] = ysize;
-  if ( yid != UNDEFID ) dims[ndims++] = yid;
+  if ( yid != CDI_UNDEFID ) chunks[ndims] = ysize;
+  if ( yid != CDI_UNDEFID ) dims[ndims++] = yid;
 
-  if ( xid != UNDEFID ) chunks[ndims] = xsize;
-  if ( xid != UNDEFID ) dims[ndims++] = xid;
+  if ( xid != CDI_UNDEFID ) chunks[ndims] = xsize;
+  if ( xid != CDI_UNDEFID ) dims[ndims++] = xid;
   */
   for ( int id = 0; id < 3; ++id )
     {
-      if ( dimorder[id] == 3 && zid != UNDEFID )
+      if ( dimorder[id] == 3 && zid != CDI_UNDEFID )
         {
           axis[iax++] = 'Z';
           chunks[ndims] = 1;
           dims[ndims] = zid;
           ndims++;
         }
-      else if ( dimorder[id] == 2 && yid != UNDEFID )
+      else if ( dimorder[id] == 2 && yid != CDI_UNDEFID )
         {
           if ( chunktype == CDI_CHUNK_LINES )
             chunks[ndims] = 1;
@@ -417,7 +413,7 @@ int cdfDefVar(stream_t *streamptr, int varID)
           dims[ndims] = yid;
           ndims++;
         }
-      else if ( dimorder[id] == 1 && xid != UNDEFID )
+      else if ( dimorder[id] == 1 && xid != CDI_UNDEFID )
         {
           chunks[ndims] = xsize;
           dims[ndims] = xid;
@@ -583,7 +579,7 @@ int cdfDefVar(stream_t *streamptr, int varID)
       cdf_put_att_text(fileID, ncvarid, "param", strlen(paramstr), paramstr);
     }
 
-  if ( tableID != UNDEFID )
+  if ( tableID != CDI_UNDEFID )
     {
       tablenum = tableInqNum(tableID);
       if ( tablenum > 0 )
@@ -621,7 +617,7 @@ int cdfDefVar(stream_t *streamptr, int varID)
     {
       cdf_put_att_text(fileID, ncvarid, "coordinates", 9, "tlon tlat" );
     }
-  else if ( gridtype == GRID_LONLAT && xid == UNDEFID && yid == UNDEFID && gridsize == 1 )
+  else if ( gridtype == GRID_LONLAT && xid == CDI_UNDEFID && yid == CDI_UNDEFID && gridsize == 1 )
     {
       int ncxvarID = streamptr->ncgrid[gridindex].xvarID;
       int ncyvarID = streamptr->ncgrid[gridindex].yvarID;
@@ -1009,7 +1005,7 @@ void cdf_write_var(stream_t *streamptr, int varID, int memtype, const void *data
   int zaxisID   = vlistInqVarZaxis(vlistID, varID);
   int tsteptype = vlistInqVarTsteptype(vlistID, varID);
 
-  int xid = UNDEFID, yid = UNDEFID;
+  int xid = CDI_UNDEFID, yid = CDI_UNDEFID;
   if ( gridInqType(gridID) == GRID_TRAJECTORY )
     {
       cdfWriteGridTraj(streamptr, gridID);
@@ -1031,14 +1027,14 @@ void cdf_write_var(stream_t *streamptr, int varID, int memtype, const void *data
       ndims++;
     }
 
-  if ( zid != UNDEFID )
+  if ( zid != CDI_UNDEFID )
     {
       start[ndims] = 0;
       count[ndims] = (size_t)zaxisInqSize(zaxisID);
       ndims++;
     }
 
-  if ( yid != UNDEFID )
+  if ( yid != CDI_UNDEFID )
     {
       start[ndims] = 0;
       cdf_inq_dimlen(fileID, yid, &size);
@@ -1047,7 +1043,7 @@ void cdf_write_var(stream_t *streamptr, int varID, int memtype, const void *data
       ndims++;
     }
 
-  if ( xid != UNDEFID )
+  if ( xid != CDI_UNDEFID )
     {
       start[ndims] = 0;
       cdf_inq_dimlen(fileID, xid, &size);
@@ -1081,7 +1077,7 @@ void cdf_write_var_chunk(stream_t *streamptr, int varID, int memtype,
 {
   if ( streamptr->accessmode == 0 ) cdfEndDef(streamptr);
 
-  int xid = UNDEFID, yid = UNDEFID;
+  int xid = CDI_UNDEFID, yid = CDI_UNDEFID;
   size_t xsize = 0, ysize = 0;
   size_t start[5];
   size_t count[5];
@@ -1128,7 +1124,7 @@ void cdf_write_var_chunk(stream_t *streamptr, int varID, int memtype,
       count[ndims] = 1;
       ndims++;
     }
-  if ( zid != UNDEFID )
+  if ( zid != CDI_UNDEFID )
     {
       int size = zaxisInqSize(zaxisID);
       xassert(rect[2][0] >= 0 && rect[2][0] <= rect[2][1]
@@ -1137,7 +1133,7 @@ void cdf_write_var_chunk(stream_t *streamptr, int varID, int memtype,
       count[ndims] = (size_t)rect[2][1] - (size_t)rect[2][0] + 1;
       ndims++;
     }
-  if ( yid != UNDEFID )
+  if ( yid != CDI_UNDEFID )
     {
       size_t size;
       cdf_inq_dimlen(fileID, yid, &size);
@@ -1147,7 +1143,7 @@ void cdf_write_var_chunk(stream_t *streamptr, int varID, int memtype,
       count[ndims] = (size_t)rect[1][1] - (size_t)rect[1][0] + 1;
       ndims++;
     }
-  if ( xid != UNDEFID )
+  if ( xid != CDI_UNDEFID )
     {
       size_t size;
       cdf_inq_dimlen(fileID, xid, &size);
@@ -1187,7 +1183,7 @@ void cdf_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtyp
   size_t start[5];
   size_t count[5];
   int dimorder[3];
-  int xid = UNDEFID, yid = UNDEFID;
+  int xid = CDI_UNDEFID, yid = CDI_UNDEFID;
 
   if ( CDI_Debug ) Message("streamID = %d  varID = %d", streamptr->self, varID);
 
@@ -1220,7 +1216,7 @@ void cdf_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtyp
   int zaxisindex = vlistZaxisIndex(vlistID, zaxisID);
   int zid = streamptr->zaxisID[zaxisindex];
 
-  bool swapxy = (dimorder[2] == 2 || dimorder[0] == 1) && xid != UNDEFID && yid != UNDEFID;
+  bool swapxy = (dimorder[2] == 2 || dimorder[0] == 1) && xid != CDI_UNDEFID && yid != CDI_UNDEFID;
 
   size_t ndims = 0;
   if ( tsteptype != TSTEP_CONSTANT )
@@ -1232,20 +1228,20 @@ void cdf_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtyp
 
   for ( int id = 0; id < 3; ++id )
     {
-      if ( dimorder[id] == 3 && zid != UNDEFID )
+      if ( dimorder[id] == 3 && zid != CDI_UNDEFID )
         {
           start[ndims] = (size_t)levelID;
           count[ndims] = 1;
           ndims++;
         }
-      else if ( dimorder[id] == 2 && yid != UNDEFID )
+      else if ( dimorder[id] == 2 && yid != CDI_UNDEFID )
         {
           start[ndims] = 0;
           cdf_inq_dimlen(fileID, yid, &ysize);
           count[ndims] = ysize;
           ndims++;
         }
-      else if ( dimorder[id] == 1 && xid != UNDEFID )
+      else if ( dimorder[id] == 1 && xid != CDI_UNDEFID )
         {
           start[ndims] = 0;
           cdf_inq_dimlen(fileID, xid, &xsize);
diff --git a/src/cdi.h b/src/cdi.h
index a968cbb6ddd3854ed2fee92e2ea682f7f996d0b6..8b97d62f4a9b7e523443ea9fd5fa1134258bd736 100644
--- a/src/cdi.h
+++ b/src/cdi.h
@@ -752,9 +752,6 @@ void    gridDefProj(int gridID, int projID);
 /*      gridInqProj: Get the projection ID of a Grid */
 int     gridInqProj(int gridID);
 
-/*      gridDefProjType: Define the projection type */
-void    gridDefProjType(int gridID, int projtype);
-
 /*      gridInqProjType: Get the projection type */
 int     gridInqProjType(int gridID);
 
diff --git a/src/cdi.inc b/src/cdi.inc
index 8aa8082322b828e3b37fb706ab2ae6aaf46a92ca..8bb57c03bdf77df1589015942f99fd9edc7c1f9d 100644
--- a/src/cdi.inc
+++ b/src/cdi.inc
@@ -4,7 +4,7 @@
 !
 ! Author:
 ! -------
-! Uwe Schulzweida, MPI-MET, Hamburg,   September 2016
+! Uwe Schulzweida, MPI-MET, Hamburg,   November 2016
 !
 
       INTEGER    CDI_MAX_NAME          
@@ -1521,11 +1521,6 @@
 !                                    (INTEGER         gridID)
       EXTERNAL        gridInqProj
 
-!                     gridDefProjType
-!                                    (INTEGER         gridID,
-!                                     INTEGER         projtype)
-      EXTERNAL        gridDefProjType
-
       INTEGER         gridInqProjType
 !                                    (INTEGER         gridID)
       EXTERNAL        gridInqProjType
diff --git a/src/cdiFortran.c b/src/cdiFortran.c
index 2ca980b59da27cc94af875e8c730bf09466e58b3..a983d1598383af4d65470299f66b0c820cd0d310 100644
--- a/src/cdiFortran.c
+++ b/src/cdiFortran.c
@@ -314,7 +314,6 @@ FCALLSCSUB1 (gridDestroy, GRIDDESTROY, griddestroy, INT)
 FCALLSCFUN1 (INT, gridDuplicate, GRIDDUPLICATE, gridduplicate, INT)
 FCALLSCSUB2 (gridDefProj, GRIDDEFPROJ, griddefproj, INT, INT)
 FCALLSCFUN1 (INT, gridInqProj, GRIDINQPROJ, gridinqproj, INT)
-FCALLSCSUB2 (gridDefProjType, GRIDDEFPROJTYPE, griddefprojtype, INT, INT)
 FCALLSCFUN1 (INT, gridInqProjType, GRIDINQPROJTYPE, gridinqprojtype, INT)
 FCALLSCFUN1 (INT, gridInqType, GRIDINQTYPE, gridinqtype, INT)
 FCALLSCFUN1 (INT, gridInqSize, GRIDINQSIZE, gridinqsize, INT)
diff --git a/src/cdi_int.h b/src/cdi_int.h
index c7cca2ee042953fb7b1c18a85ac0a8e7756fa808..d2f9c36253026fa6ffc4e556447e575dcc7553e3 100644
--- a/src/cdi_int.h
+++ b/src/cdi_int.h
@@ -245,9 +245,7 @@ typedef struct {
   int         ncmode;
   int         vlistID;
 #ifdef HAVE_LIBNETCDF
-  ncgrid_t   *ncgrid;
-  int         xdimID[MAX_GRIDS_PS];
-  int         ydimID[MAX_GRIDS_PS];
+  ncgrid_t    ncgrid[MAX_GRIDS_PS];
   int         zaxisID[MAX_ZAXES_PS];	//Warning: synchronous array to vlist_to_pointer(vlistID)->zaxisIDs
   int         nczvarID[MAX_ZAXES_PS];
   VCT         vct;
diff --git a/src/grid.c b/src/grid.c
index 98d02428d0fc96f6bff2b5832194f1bc13e5dc8b..63bb9355810267e763f482fb90b3b3f062f8f254 100644
--- a/src/grid.c
+++ b/src/grid.c
@@ -20,8 +20,6 @@
 #include "serialize.h"
 #include "vlist.h"
 
-#undef  UNDEFID
-#define UNDEFID -1
 
 /* the value in the second pair of brackets must match the length of
  * the longest string (including terminating NUL) */
@@ -1186,7 +1184,6 @@ void gridDefPrec(int gridID, int prec)
 int gridInqPrec(int gridID)
 {
   grid_t *gridptr = grid_to_pointer(gridID);
-
   return gridptr->prec;
 }
 
@@ -1209,7 +1206,6 @@ The function @func{gridInqXsize} returns the number of values of a X-axis.
 int gridInqXsize(int gridID)
 {
   grid_t *gridptr = grid_to_pointer(gridID);
-
   return gridptr->x.size;
 }
 
@@ -1274,7 +1270,6 @@ The function @func{gridInqYsize} returns the number of values of a Y-axis.
 int gridInqYsize(int gridID)
 {
   grid_t *gridptr = grid_to_pointer(gridID);
-
   return gridptr->y.size;
 }
 
@@ -1324,7 +1319,6 @@ of a Gaussian grid.
 int gridInqNP(int gridID)
 {
   grid_t *gridptr = grid_to_pointer(gridID);
-
   return gridptr->np;
 }
 
@@ -2049,23 +2043,22 @@ static
 bool compareXYvals2(grid_t *gridRef, grid_t *gridTest)
 {
   int gridsize = gridTest->size;
-  bool differ
-    = ((gridTest->x.vals == NULL) ^ (gridRef->x.vals == NULL))
-    || ((gridTest->y.vals == NULL) ^ (gridRef->y.vals == NULL));
+  bool differ = ((gridTest->x.vals == NULL) ^ (gridRef->x.vals == NULL))
+             || ((gridTest->y.vals == NULL) ^ (gridRef->y.vals == NULL));
 
   typedef double (*inqVal)(grid_t *grid, int index);
   inqVal inqXValRef = gridRef->vtable->inqXVal,
-    inqYValRef = gridRef->vtable->inqXVal,
-    inqXValTest = gridTest->vtable->inqXVal,
-    inqYValTest = gridTest->vtable->inqYVal;
+         inqYValRef = gridRef->vtable->inqYVal,
+         inqXValTest = gridTest->vtable->inqXVal,
+         inqYValTest = gridTest->vtable->inqYVal;
 
   if ( !differ && gridTest->x.vals )
     differ = fabs(inqXValTest(gridTest, 0) - inqXValRef(gridRef, 0)) > 1.e-9
-      || fabs(inqXValTest(gridTest, gridsize-1) - inqXValRef(gridRef, gridsize-1)) > 1.e-9;
+          || fabs(inqXValTest(gridTest, gridsize-1) - inqXValRef(gridRef, gridsize-1)) > 1.e-9;
 
   if ( !differ && gridTest->y.vals )
     differ = fabs(inqYValTest(gridTest, 0) - inqYValRef(gridRef, 0)) > 1.e-9
-      || fabs(inqYValTest(gridTest, gridsize-1) - inqYValRef(gridRef, gridsize-1)) > 1.e-9;
+          || fabs(inqYValTest(gridTest, gridsize-1) - inqYValRef(gridRef, gridsize-1)) > 1.e-9;
 
   return differ;
 }
@@ -2195,7 +2188,7 @@ bool gridCompare(int gridID, const grid_t *grid, bool coord_compare)
               else
                 {
                   /* FIXME: not octet 0 but octet 7 is guaranteed  non-zero for any non-NULL UUID */
-                  differ = differ || ( gridRef->uuid[0] && grid->uuid[0] && memcmp(gridRef->uuid, grid->uuid, CDI_UUID_SIZE) != 0 );
+                  differ = differ || (gridRef->uuid[0] && grid->uuid[0] && memcmp(gridRef->uuid, grid->uuid, CDI_UUID_SIZE) != 0);
 
                   if ( !differ &&
                        ((grid->x.vals == NULL) ^ (gridRef->x.vals == NULL)) &&
@@ -2896,7 +2889,6 @@ void gridDefNvertex(int gridID, int nvertex)
 int gridInqNvertex(int gridID)
 {
   grid_t *gridptr = grid_to_pointer(gridID);
-
   return gridptr->nvertex;
 }
 
@@ -4515,7 +4507,7 @@ struct addIfNewRes cdiVlistAddGridIfNew(int vlistID, grid_t *grid, int mode)
   if ( mode == 0 )
     for ( unsigned index = 0; index < ngrids; index++ )
       {
-	if ( (gridID = vlistptr->gridIDs[index]) != UNDEFID )
+	if ( (gridID = vlistptr->gridIDs[index]) != CDI_UNDEFID )
           {
             if ( gridCompare(gridID, grid, false) == false )
               {
diff --git a/src/institution.c b/src/institution.c
index 3e973a732f5b64f3da05519c6bc1e0972fb61b5f..eb1ef6bb7b732f2a80e2e65f95eb2ccc1f2c5498 100644
--- a/src/institution.c
+++ b/src/institution.c
@@ -15,12 +15,10 @@
 #include "serialize.h"
 #include "institution.h"
 
-#undef  UNDEFID
-#define UNDEFID  -1
 
-static int ECMWF  = UNDEFID,
-  MPIMET = UNDEFID,
-  MCH    = UNDEFID;
+static int ECMWF  = CDI_UNDEFID,
+  MPIMET = CDI_UNDEFID,
+  MCH    = CDI_UNDEFID;
 
 typedef struct
 {
@@ -53,10 +51,10 @@ static const resOps instituteOps = {
 static
 void instituteDefaultValue ( institute_t * instituteptr )
 {
-  instituteptr->self       = UNDEFID;
+  instituteptr->self       = CDI_UNDEFID;
   instituteptr->used       = 0;
-  instituteptr->center     = UNDEFID;
-  instituteptr->subcenter  = UNDEFID;
+  instituteptr->center     = CDI_UNDEFID;
+  instituteptr->subcenter  = CDI_UNDEFID;
   instituteptr->name       = NULL;
   instituteptr->longname   = NULL;
 }
@@ -151,14 +149,14 @@ findInstitute(int id, void *res, void *data)
 int institutInq(int center, int subcenter, const char *name, const char *longname)
 {
   institute_t * ip_ref = (institute_t *) Malloc(sizeof (*ip_ref));
-  ip_ref->self       = UNDEFID;
+  ip_ref->self       = CDI_UNDEFID;
   ip_ref->used       = 0;
   ip_ref->center     = center;
   ip_ref->subcenter  = subcenter;
   ip_ref->name       = name && name[0] ? (char *)name : NULL;
   ip_ref->longname   = longname && longname[0] ? (char *)longname : NULL;
 
-  struct instLoc state = { .ip = ip_ref, .id = UNDEFID };
+  struct instLoc state = { .ip = ip_ref, .id = CDI_UNDEFID };
   cdiResHFilterApply(&instituteOps, findInstitute, &state);
 
   Free(ip_ref);
@@ -202,10 +200,10 @@ int institutInqCenter(int instID)
 {
   institute_t * instituteptr = NULL;
 
-  if ( instID != UNDEFID )
+  if ( instID != CDI_UNDEFID )
     instituteptr = ( institute_t * ) reshGetVal ( instID, &instituteOps );
 
-  return  instituteptr ? instituteptr->center : UNDEFID;
+  return  instituteptr ? instituteptr->center : CDI_UNDEFID;
 }
 
 
@@ -213,10 +211,10 @@ int institutInqSubcenter(int instID)
 {
   institute_t * instituteptr = NULL;
 
-  if ( instID != UNDEFID )
+  if ( instID != CDI_UNDEFID )
     instituteptr = ( institute_t * ) reshGetVal ( instID, &instituteOps );
 
-  return instituteptr ? instituteptr->subcenter: UNDEFID;
+  return instituteptr ? instituteptr->subcenter: CDI_UNDEFID;
 }
 
 
@@ -224,7 +222,7 @@ const char *institutInqNamePtr(int instID)
 {
   institute_t * instituteptr = NULL;
 
-  if ( instID != UNDEFID )
+  if ( instID != CDI_UNDEFID )
     instituteptr = ( institute_t * ) reshGetVal ( instID, &instituteOps );
 
   return instituteptr ? instituteptr->name : NULL;
@@ -235,7 +233,7 @@ const char *institutInqLongnamePtr(int instID)
 {
   institute_t * instituteptr = NULL;
 
-  if ( instID != UNDEFID )
+  if ( instID != CDI_UNDEFID )
     instituteptr = ( institute_t * ) reshGetVal ( instID, &instituteOps );
 
   return instituteptr ? instituteptr->longname : NULL;
diff --git a/src/model.c b/src/model.c
index f7ee147ff5c456167e483ad6c01949c9489d7f70..7b8b0470095b61b9ffca77bc2ae2088435e330d4 100644
--- a/src/model.c
+++ b/src/model.c
@@ -13,12 +13,12 @@
 #include "resource_unpack.h"
 #include "serialize.h"
 
-#undef  UNDEFID
-#define UNDEFID -1
+#undef  CDI_UNDEFID
+#define CDI_UNDEFID -1
 
-static int ECHAM4 = UNDEFID,
-  ECHAM5 = UNDEFID,
-  COSMO  = UNDEFID;
+static int ECHAM4 = CDI_UNDEFID,
+  ECHAM5 = CDI_UNDEFID,
+  COSMO  = CDI_UNDEFID;
 
 typedef struct
 {
@@ -56,10 +56,10 @@ static const resOps modelOps = {
 static
 void modelDefaultValue ( model_t *modelptr )
 {
-  modelptr->self        = UNDEFID;
+  modelptr->self        = CDI_UNDEFID;
   modelptr->used        = 0;
-  modelptr->instID      = UNDEFID;
-  modelptr->modelgribID = UNDEFID;
+  modelptr->instID      = CDI_UNDEFID;
+  modelptr->modelgribID = CDI_UNDEFID;
   modelptr->name        = NULL;
 }
 
@@ -186,7 +186,7 @@ int modelInq(int instID, int modelgribID, const char *name)
 
   struct modelLoc searchState = { .name = name, .instID = instID,
                                   .modelgribID = modelgribID,
-                                  .resID = UNDEFID };
+                                  .resID = CDI_UNDEFID };
   if (name && *name)
     cdiResHFilterApply(&modelOps, findModelByName, &searchState);
   else
@@ -213,10 +213,10 @@ int modelInqInstitut(int modelID)
 
   modelInit ();
 
-  if ( modelID != UNDEFID )
+  if ( modelID != CDI_UNDEFID )
     modelptr = ( model_t * ) reshGetVal ( modelID, &modelOps );
 
-  return modelptr ? modelptr->instID : UNDEFID;
+  return modelptr ? modelptr->instID : CDI_UNDEFID;
 }
 
 
@@ -226,10 +226,10 @@ int modelInqGribID(int modelID)
 
   modelInit ();
 
-  if ( modelID != UNDEFID )
+  if ( modelID != CDI_UNDEFID )
     modelptr = ( model_t * ) reshGetVal ( modelID, &modelOps );
 
-  return modelptr ? modelptr->modelgribID : UNDEFID;
+  return modelptr ? modelptr->modelgribID : CDI_UNDEFID;
 }
 
 
@@ -239,7 +239,7 @@ const char *modelInqNamePtr(int modelID)
 
   modelInit ();
 
-  if ( modelID != UNDEFID )
+  if ( modelID != CDI_UNDEFID )
     modelptr = ( model_t * ) reshGetVal ( modelID, &modelOps );
 
   return modelptr ? modelptr->name : NULL;
diff --git a/src/stream.c b/src/stream.c
index e41a00bd78bea497e73cc96adbe0cc2744f2e5b7..85d536145697909d2bc25b91d26ae8f7026a9635 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -637,7 +637,7 @@ int streamOpenA(const char *filename, const char *filemode, int filetype)
   if ( status < 0 ) return status;
   vlist_t *vlistptr = vlist_to_pointer(streamptr->vlistID);
   vlistptr->ntsteps = (int)cdiInqTimeSize(streamID);
-  if(!strcmp(filemode, "r")) cdiVlistMakeImmutable(streamptr->vlistID);
+  if ( !strcmp(filemode, "r") ) cdiVlistMakeImmutable(streamptr->vlistID);
 
   {
     void (*streamCloseDelegate)(stream_t *streamptr, int recordBufIsToBeDeleted)
@@ -880,13 +880,18 @@ void streamDefaultValue ( stream_t * streamptr )
   basetimeInit(&streamptr->basetime);
 
 #ifdef HAVE_LIBNETCDF
-  int i;
-  for ( i = 0; i < MAX_GRIDS_PS; i++ ) streamptr->xdimID[i]  = CDI_UNDEFID;
-  for ( i = 0; i < MAX_GRIDS_PS; i++ ) streamptr->ydimID[i]  = CDI_UNDEFID;
-  for ( i = 0; i < MAX_ZAXES_PS; i++ ) streamptr->zaxisID[i]  = CDI_UNDEFID;
-  for ( i = 0; i < MAX_ZAXES_PS; i++ ) streamptr->nczvarID[i] = CDI_UNDEFID;
+  for ( int i = 0; i < MAX_ZAXES_PS; i++ ) streamptr->zaxisID[i]  = CDI_UNDEFID;
+  for ( int i = 0; i < MAX_ZAXES_PS; i++ ) streamptr->nczvarID[i] = CDI_UNDEFID;
 
-  streamptr->ncgrid            = NULL;
+  for ( int i = 0; i < MAX_GRIDS_PS; i++ )
+    {
+      streamptr->ncgrid[i].gridID = CDI_UNDEFID;
+      streamptr->ncgrid[i].xdimID = CDI_UNDEFID;
+      streamptr->ncgrid[i].ydimID = CDI_UNDEFID;
+      streamptr->ncgrid[i].xvarID = CDI_UNDEFID;
+      streamptr->ncgrid[i].yvarID = CDI_UNDEFID;
+      streamptr->ncgrid[i].avarID = CDI_UNDEFID;
+    }
 
   streamptr->vct.ilev          = 0;
   streamptr->vct.mlev          = 0;
@@ -966,7 +971,6 @@ void cdiStreamCloseDefaultDelegate(stream_t *streamptr, int recordBufIsToBeDelet
       case CDI_FILETYPE_NC4C:
         {
           cdfClose(fileID);
-          if ( streamptr->ncgrid ) { Free(streamptr->ncgrid); streamptr->ncgrid = NULL; }
           break;
         }
 #endif
diff --git a/src/stream_cdf.h b/src/stream_cdf.h
index ad61c1ed6e06c0f7f8e5fed32f810771099bdd57..8056b6e4db220fbbaf0ee3e08aa6b2a02effc83c 100644
--- a/src/stream_cdf.h
+++ b/src/stream_cdf.h
@@ -35,6 +35,8 @@ void cdfDefTime(stream_t* streamptr);
 
 void cdf_scale_add(size_t size, double *data, double addoffset, double scalefactor);
 
+int cdfDefDatatype(int datatype, int filetype);
+
 #endif
 /*
  * Local Variables:
diff --git a/src/stream_cdf_i.c b/src/stream_cdf_i.c
index a7c2c9c54d71553a92649376205d2be259ccf809..f838e2d2015ee27cc2648f36d16ce1ad3ea3e7ff 100644
--- a/src/stream_cdf_i.c
+++ b/src/stream_cdf_i.c
@@ -19,8 +19,6 @@
 #include "cdf_util.h"
 #include "cdf_lazy_grid.h"
 
-#undef  UNDEFID
-#define UNDEFID  CDI_UNDEFID
 
 #define  X_AXIS  1
 #define  Y_AXIS  2
@@ -500,7 +498,7 @@ void cdfCreateRecords(stream_t *streamptr, int tsID)
       destTstep->nrecs      = nrecs;
       destTstep->nallrecs   = nrecs;
       destTstep->recordSize = nrecs;
-      destTstep->curRecID   = UNDEFID;
+      destTstep->curRecID   = CDI_UNDEFID;
       destTstep->recIDs     = (int *) Malloc((size_t)nvrecs*sizeof (int));;
       for ( int recID = 0; recID < nvrecs; recID++ ) destTstep->recIDs[recID] = recID;
 
@@ -537,7 +535,7 @@ void cdfCreateRecords(stream_t *streamptr, int tsID)
       destTstep->nrecs      = nvrecs;
       destTstep->nallrecs   = nrecs;
       destTstep->recordSize = nrecs;
-      destTstep->curRecID   = UNDEFID;
+      destTstep->curRecID   = CDI_UNDEFID;
 
       memcpy(destTstep->records, sourceTstep->records, (size_t)nrecs*sizeof(record_t));
 
@@ -566,7 +564,7 @@ void cdfCreateRecords(stream_t *streamptr, int tsID)
       destTstep->nrecs      = nvrecs;
       destTstep->nallrecs   = nrecs;
       destTstep->recordSize = nrecs;
-      destTstep->curRecID   = UNDEFID;
+      destTstep->curRecID   = CDI_UNDEFID;
 
       memcpy(destTstep->records, sourceTstep->records, (size_t)nrecs*sizeof(record_t));
 
@@ -610,7 +608,7 @@ int cdf_time_dimid(int fileID, int ndims, int nvars)
         }
     }
 
-  return UNDEFID;
+  return CDI_UNDEFID;
 }
 
 static
@@ -618,8 +616,8 @@ void init_ncdims(long ndims, ncdim_t *ncdims)
 {
   for ( long ncdimid = 0; ncdimid < ndims; ncdimid++ )
     {
-      ncdims[ncdimid].ncvarid      = UNDEFID;
-      ncdims[ncdimid].dimtype      = UNDEFID;
+      ncdims[ncdimid].ncvarid      = CDI_UNDEFID;
+      ncdims[ncdimid].dimtype      = CDI_UNDEFID;
       ncdims[ncdimid].len          = 0;
       ncdims[ncdimid].name[0]      = 0;
     }
@@ -630,8 +628,8 @@ void init_ncvars(long nvars, ncvar_t *ncvars)
 {
   for ( long ncvarid = 0; ncvarid < nvars; ++ncvarid )
     {
-      ncvars[ncvarid].ncid            = UNDEFID;
-      ncvars[ncvarid].isvar           = UNDEFID;
+      ncvars[ncvarid].ncid            = CDI_UNDEFID;
+      ncvars[ncvarid].isvar           = CDI_UNDEFID;
       ncvars[ncvarid].ignore          = false;
       ncvars[ncvarid].isx             = false;
       ncvars[ncvarid].isy             = false;
@@ -644,41 +642,41 @@ void init_ncvars(long nvars, ncvar_t *ncvars)
       ncvars[ncvarid].climatology     = false;
       ncvars[ncvarid].lformulaterms   = false;
       ncvars[ncvarid].tsteptype       = TSTEP_CONSTANT;
-      ncvars[ncvarid].param           = UNDEFID;
-      ncvars[ncvarid].code            = UNDEFID;
+      ncvars[ncvarid].param           = CDI_UNDEFID;
+      ncvars[ncvarid].code            = CDI_UNDEFID;
       ncvars[ncvarid].tabnum          = 0;
-      ncvars[ncvarid].bounds          = UNDEFID;
-      ncvars[ncvarid].gridID          = UNDEFID;
-      ncvars[ncvarid].zaxisID         = UNDEFID;
-      ncvars[ncvarid].gridtype        = UNDEFID;
-      ncvars[ncvarid].zaxistype       = UNDEFID;
-      ncvars[ncvarid].xdim            = UNDEFID;
-      ncvars[ncvarid].ydim            = UNDEFID;
-      ncvars[ncvarid].zdim            = UNDEFID;
-      ncvars[ncvarid].xvarid          = UNDEFID;
-      ncvars[ncvarid].yvarid          = UNDEFID;
-      ncvars[ncvarid].zvarid          = UNDEFID;
-      ncvars[ncvarid].tvarid          = UNDEFID;
-      ncvars[ncvarid].psvarid         = UNDEFID;
-      ncvars[ncvarid].p0varid         = UNDEFID;
+      ncvars[ncvarid].bounds          = CDI_UNDEFID;
+      ncvars[ncvarid].gridID          = CDI_UNDEFID;
+      ncvars[ncvarid].zaxisID         = CDI_UNDEFID;
+      ncvars[ncvarid].gridtype        = CDI_UNDEFID;
+      ncvars[ncvarid].zaxistype       = CDI_UNDEFID;
+      ncvars[ncvarid].xdim            = CDI_UNDEFID;
+      ncvars[ncvarid].ydim            = CDI_UNDEFID;
+      ncvars[ncvarid].zdim            = CDI_UNDEFID;
+      ncvars[ncvarid].xvarid          = CDI_UNDEFID;
+      ncvars[ncvarid].yvarid          = CDI_UNDEFID;
+      ncvars[ncvarid].zvarid          = CDI_UNDEFID;
+      ncvars[ncvarid].tvarid          = CDI_UNDEFID;
+      ncvars[ncvarid].psvarid         = CDI_UNDEFID;
+      ncvars[ncvarid].p0varid         = CDI_UNDEFID;
       ncvars[ncvarid].ncoordvars      = 0;
       for ( int i = 0; i < MAX_COORDVARS; ++i )
-        ncvars[ncvarid].coordvarids[i]  = UNDEFID;
+        ncvars[ncvarid].coordvarids[i]  = CDI_UNDEFID;
       ncvars[ncvarid].nauxvars      = 0;
       for ( int i = 0; i < MAX_AUXVARS; ++i )
-        ncvars[ncvarid].auxvarids[i]  = UNDEFID;
-      ncvars[ncvarid].cellarea        = UNDEFID;
-      ncvars[ncvarid].tableID         = UNDEFID;
+        ncvars[ncvarid].auxvarids[i]  = CDI_UNDEFID;
+      ncvars[ncvarid].cellarea        = CDI_UNDEFID;
+      ncvars[ncvarid].tableID         = CDI_UNDEFID;
       ncvars[ncvarid].xtype           = 0;
       ncvars[ncvarid].ndims           = 0;
-      ncvars[ncvarid].gmapid          = UNDEFID;
+      ncvars[ncvarid].gmapid          = CDI_UNDEFID;
       ncvars[ncvarid].vctsize         = 0;
       ncvars[ncvarid].vct             = NULL;
       ncvars[ncvarid].truncation      = 0;
       ncvars[ncvarid].position        = 0;
       ncvars[ncvarid].positive        = 0;
       ncvars[ncvarid].chunked         = 0;
-      ncvars[ncvarid].chunktype       = UNDEFID;
+      ncvars[ncvarid].chunktype       = CDI_UNDEFID;
       ncvars[ncvarid].defmissval      = false;
       ncvars[ncvarid].deffillval      = false;
       ncvars[ncvarid].missval         = 0;
@@ -704,7 +702,7 @@ void init_ncvars(long nvars, ncvar_t *ncvars)
 static
 void cdf_set_var(ncvar_t *ncvars, int ncvarid, short isvar)
 {
-  if ( ncvars[ncvarid].isvar != UNDEFID &&
+  if ( ncvars[ncvarid].isvar != CDI_UNDEFID &&
        ncvars[ncvarid].isvar != isvar   &&
        ncvars[ncvarid].warn  == false )
     {
@@ -721,7 +719,7 @@ void cdf_set_var(ncvar_t *ncvars, int ncvarid, short isvar)
 static
 void cdf_set_dim(ncvar_t *ncvars, int ncvarid, int dimid, int dimtype)
 {
-  if ( ncvars[ncvarid].dimtype[dimid] != UNDEFID &&
+  if ( ncvars[ncvarid].dimtype[dimid] != CDI_UNDEFID &&
        ncvars[ncvarid].dimtype[dimid] != dimtype )
     {
       Warning("Inconsistent dimension definition for %s! dimid = %d;  type = %d;  newtype = %d",
@@ -804,7 +802,7 @@ bool isHybridSigmaPressureCoordinate(int ncid, int ncvarid, ncvar_t *ncvars, con
       if ( psvarid1 != -1 ) ncvar->psvarid = psvarid1;
       if ( p0varid1 != -1 ) ncvar->p0varid = p0varid1;
 
-      if ( ncvar->bounds != UNDEFID && ncvars[ncvar->bounds].lformulaterms )
+      if ( ncvar->bounds != CDI_UNDEFID && ncvars[ncvar->bounds].lformulaterms )
         {
           ncfvarid = ncvar->bounds;
           int avarid2 = -1, bvarid2 = -1, psvarid2 = -1, p0varid2 = -1;
@@ -1018,7 +1016,7 @@ void cdf_scan_attr_axis(ncvar_t *ncvars, ncdim_t *ncdims, int ncvarid, const cha
 static
 int cdf_get_cell_varid(char *attstring, int ncid)
 {
-  int nc_cell_id = UNDEFID;
+  int nc_cell_id = CDI_UNDEFID;
 
   char *pstring = attstring;
   while ( isspace((int) *pstring) ) pstring++;
@@ -1140,7 +1138,7 @@ void cdf_scan_var_attr(int nvars, ncvar_t *ncvars, ncdim_t *ncdims, int timedimi
 
       for ( iatt = 0; iatt < nvatts; iatt++ )
         {
-          int nc_cell_id = UNDEFID;
+          int nc_cell_id = CDI_UNDEFID;
 
           cdf_inq_attname(ncid, ncvarid, iatt, attname);
           cdf_inq_atttype(ncid, ncvarid, attname, &atttype);
@@ -1276,7 +1274,7 @@ void cdf_scan_var_attr(int nvars, ncvar_t *ncvars, ncdim_t *ncdims, int timedimi
             {
               ncvars[ncvarid].lformulaterms = true;
             }
-          else if ( isText && strcmp(attname, "cell_measures") == 0 && (nc_cell_id=cdf_get_cell_varid(attstring, ncid)) != UNDEFID )
+          else if ( isText && strcmp(attname, "cell_measures") == 0 && (nc_cell_id=cdf_get_cell_varid(attstring, ncid)) != CDI_UNDEFID )
             {
               ncvars[ncvarid].cellarea = nc_cell_id;
               ncvars[nc_cell_id].isvar = FALSE;
@@ -1620,12 +1618,12 @@ void cdf_set_dimtype(int nvars, ncvar_t *ncvars, ncdim_t *ncdims)
 	      /* else if ( ncvars[ncvarid].dimtype[i] == T_AXIS ) ltdim = true; */
 	    }
 
-          if ( !lxdim && ncvars[ncvarid].xvarid != UNDEFID )
+          if ( !lxdim && ncvars[ncvarid].xvarid != CDI_UNDEFID )
             {
               if ( ncvars[ncvars[ncvarid].xvarid].ndims == 0 ) lxdim = true;
             }
 
-          if ( !lydim && ncvars[ncvarid].yvarid != UNDEFID )
+          if ( !lydim && ncvars[ncvarid].yvarid != CDI_UNDEFID )
             {
               if ( ncvars[ncvars[ncvarid].yvarid].ndims == 0 ) lydim = true;
             }
@@ -1640,7 +1638,7 @@ void cdf_set_dimtype(int nvars, ncvar_t *ncvars, ncdim_t *ncdims)
                         cdf_set_dim(ncvars, ncvarid, i, Z_AXIS);
                         lzdim = true;
                         int ncdimid = ncvars[ncvarid].dimids[i];
-                        if ( ncdims[ncdimid].dimtype == UNDEFID )
+                        if ( ncdims[ncdimid].dimtype == CDI_UNDEFID )
                           ncdims[ncdimid].dimtype = Z_AXIS;
                       }
                   }
@@ -1653,7 +1651,7 @@ void cdf_set_dimtype(int nvars, ncvar_t *ncvars, ncdim_t *ncdims)
       int ndims = ncvars[ncvarid].ndims;
       for ( int i = 0; i < ndims; i++ )
         {
-          if ( ncvars[ncvarid].dimtype[i] == UNDEFID )
+          if ( ncvars[ncvarid].dimtype[i] == CDI_UNDEFID )
             {
               int ncdimid = ncvars[ncvarid].dimids[i];
               if ( ncdims[ncdimid].dimtype == Z_AXIS )
@@ -1679,12 +1677,12 @@ void cdf_set_dimtype(int nvars, ncvar_t *ncvars, ncdim_t *ncdims)
 	      /* else if ( ncvars[ncvarid].dimtype[i] == T_AXIS ) ltdim = true; */
 	    }
 
-          if ( !lxdim && ncvars[ncvarid].xvarid != UNDEFID )
+          if ( !lxdim && ncvars[ncvarid].xvarid != CDI_UNDEFID )
             {
               if ( ncvars[ncvars[ncvarid].xvarid].ndims == 0 ) lxdim = true;
             }
 
-          if ( !lydim && ncvars[ncvarid].yvarid != UNDEFID )
+          if ( !lydim && ncvars[ncvarid].yvarid != CDI_UNDEFID )
             {
               if ( ncvars[ncvars[ncvarid].yvarid].ndims == 0 ) lydim = true;
             }
@@ -1825,7 +1823,7 @@ void verify_coordinate_vars_1(int ncid, int ndims, ncdim_t *ncdims, ncvar_t *ncv
 		}
 	    }
 
-	  if ( ncvars[ncvarid].zaxistype != UNDEFID )
+	  if ( ncvars[ncvarid].zaxistype != CDI_UNDEFID )
 	    {
               ncvars[ncvarid].islev = true;
 	      cdf_set_var(ncvars, ncvarid, FALSE);
@@ -1880,7 +1878,7 @@ void verify_coordinate_vars_2(int nvars, ncvar_t *ncvars)
 		    ncvars[ncvarid].zaxistype = ZAXIS_GENERIC;
 		  continue;
 		}
-	      else if ( ncvars[ncvarid].zaxistype == UNDEFID && is_pressure_units(ncvars[ncvarid].units) )
+	      else if ( ncvars[ncvarid].zaxistype == CDI_UNDEFID && is_pressure_units(ncvars[ncvarid].units) )
 		{
 		  ncvars[ncvarid].zaxistype = ZAXIS_PRESSURE;
 		  continue;
@@ -2129,7 +2127,8 @@ bool cdf_read_xcoord(struct cdfLazyGrid *restrict lazyGrid, ncdim_t *ncdims, ncv
       return true;
     }
 
-  if ( axisvar->xtype == NC_FLOAT ) grid->prec = CDI_DATATYPE_FLT32;
+  int prec = cdfInqDatatype(axisvar->xtype, axisvar->lunsigned);
+  if ( prec != -1 )  grid->prec = prec;
 
   cdf_load_vals(size, ndims, xvarid, axisvar, &grid->x.vals, &lazyGrid->xValsGet, ntdims, start, count);
 
@@ -2180,10 +2179,10 @@ bool cdf_read_ycoord(struct cdfLazyGrid *restrict lazyGrid, ncdim_t *ncdims, ncv
       return true;
     }
 
-  if ( axisvar->xtype == NC_FLOAT ) grid->prec = CDI_DATATYPE_FLT32;
+  int prec = cdfInqDatatype(axisvar->xtype, axisvar->lunsigned);
+  if ( prec != -1 )  grid->prec = prec;
 
-  cdf_load_vals(size, ndims, yvarid, axisvar, &grid->y.vals, &lazyGrid->yValsGet,
-                ntdims, start, count);
+  cdf_load_vals(size, ndims, yvarid, axisvar, &grid->y.vals, &lazyGrid->yValsGet, ntdims, start, count);
 
   cdf_copy_axis_attr(axisvar, &grid->y);
 
@@ -2201,8 +2200,8 @@ bool cdf_read_coordinates(struct cdfLazyGrid *restrict lazyGrid, ncvar_t *ncvar,
 
   if ( ncvar->gridtype == GRID_TRAJECTORY )
     {
-      if ( ncvar->xvarid == UNDEFID ) Error("Longitude coordinate undefined for %s!", ncvar->name);
-      if ( ncvar->yvarid == UNDEFID ) Error("Latitude coordinate undefined for %s!", ncvar->name);
+      if ( ncvar->xvarid == CDI_UNDEFID ) Error("Longitude coordinate undefined for %s!", ncvar->name);
+      if ( ncvar->yvarid == CDI_UNDEFID ) Error("Latitude coordinate undefined for %s!", ncvar->name);
     }
   else
     {
@@ -2210,14 +2209,14 @@ bool cdf_read_coordinates(struct cdfLazyGrid *restrict lazyGrid, ncvar_t *ncvar,
       size_t start[3], count[3];
       int ntdims = 0;
 
-      if ( xvarid != UNDEFID && yvarid != UNDEFID )
+      if ( xvarid != CDI_UNDEFID && yvarid != CDI_UNDEFID )
         {
           int ndims = ncvars[xvarid].ndims;
           if ( ndims != ncvars[yvarid].ndims )
             {
               Warning("Inconsistent grid structure for variable %s!", ncvar->name);
-              ncvar->xvarid = xvarid = UNDEFID;
-              ncvar->yvarid = yvarid = UNDEFID;
+              ncvar->xvarid = xvarid = CDI_UNDEFID;
+              ncvar->yvarid = yvarid = CDI_UNDEFID;
             }
           if ( ndims > 1 )
             {
@@ -2237,40 +2236,40 @@ bool cdf_read_coordinates(struct cdfLazyGrid *restrict lazyGrid, ncvar_t *ncvar,
               else
                 {
                   Warning("Unsupported grid structure for variable %s (grid dims > 2)!", ncvar->name);
-                  ncvar->xvarid = xvarid = UNDEFID;
-                  ncvar->yvarid = yvarid = UNDEFID;
+                  ncvar->xvarid = xvarid = CDI_UNDEFID;
+                  ncvar->yvarid = yvarid = CDI_UNDEFID;
                 }
             }
         }
 
-      if ( xvarid != UNDEFID )
+      if ( xvarid != CDI_UNDEFID )
         {
           if ( (ncvars[xvarid].ndims - ntdims) > 2 )
             {
               Warning("Coordinate variable %s has to many dimensions (%d), skipped!", ncvars[xvarid].name, ncvars[xvarid].ndims);
-              //ncvar->xvarid = UNDEFID;
-              xvarid = UNDEFID;
+              //ncvar->xvarid = CDI_UNDEFID;
+              xvarid = CDI_UNDEFID;
             }
         }
 
-      if ( yvarid != UNDEFID )
+      if ( yvarid != CDI_UNDEFID )
         {
           if ( (ncvars[yvarid].ndims - ntdims) > 2 )
             {
               Warning("Coordinate variable %s has to many dimensions (%d), skipped!", ncvars[yvarid].name, ncvars[yvarid].ndims);
-              //ncvar->yvarid = UNDEFID;
-              yvarid = UNDEFID;
+              //ncvar->yvarid = CDI_UNDEFID;
+              yvarid = CDI_UNDEFID;
             }
         }
 
       bool islon = false, islat = false;
 
-      if ( xvarid != UNDEFID )
+      if ( xvarid != CDI_UNDEFID )
         if ( cdf_read_xcoord(lazyGrid, ncdims, ncvar, xvarid, &ncvars[xvarid],
                              &xsize, ysize, ntdims, start, count, &islon) )
           return true;
 
-      if ( yvarid != UNDEFID )
+      if ( yvarid != CDI_UNDEFID )
         if ( cdf_read_ycoord(lazyGrid, ncdims, ncvar, yvarid, &ncvars[yvarid],
                              xsize, &ysize, ntdims, start, count, &islat) )
           return true;
@@ -2280,7 +2279,7 @@ bool cdf_read_coordinates(struct cdfLazyGrid *restrict lazyGrid, ncvar_t *ncvar,
       else if ( ncvar->gridtype == GRID_UNSTRUCTURED ) size = xsize;
       else                         size = xsize*ysize;
 
-      if ( ncvar->gridtype == UNDEFID || ncvar->gridtype == GRID_GENERIC )
+      if ( ncvar->gridtype == CDI_UNDEFID || ncvar->gridtype == GRID_GENERIC )
         cdf_check_gridtype(&ncvar->gridtype, islon, islat, xsize, ysize, grid);
     }
 
@@ -2289,7 +2288,7 @@ bool cdf_read_coordinates(struct cdfLazyGrid *restrict lazyGrid, ncvar_t *ncvar,
   else if ( gridtype == GRID_PROJECTION && ncvar->gridtype == GRID_LONLAT )
     {
       int gmapvarid = ncvar->gmapid;
-      if ( gmapvarid != UNDEFID && cdfCheckAttText(ncvar->ncid, gmapvarid, "grid_mapping_name") )
+      if ( gmapvarid != CDI_UNDEFID && cdfCheckAttText(ncvar->ncid, gmapvarid, "grid_mapping_name") )
         {
           char attstring[CDI_MAX_NAME];
           cdfGetAttText(ncvar->ncid, gmapvarid, "grid_mapping_name", CDI_MAX_NAME, attstring);
@@ -2309,11 +2308,11 @@ bool cdf_read_coordinates(struct cdfLazyGrid *restrict lazyGrid, ncvar_t *ncvar,
         grid->size  = (int)size;
         grid->x.size = (int)xsize;
         grid->y.size = (int)ysize;
-        if ( xvarid != UNDEFID )
+        if ( xvarid != CDI_UNDEFID )
           {
             grid->x.flag = 1;
             int bvarid = ncvars[xvarid].bounds;
-            if ( bvarid != UNDEFID )
+            if ( bvarid != CDI_UNDEFID )
               {
                 int nbdims = ncvars[bvarid].ndims;
                 if ( nbdims == 2 || nbdims == 3 )
@@ -2324,16 +2323,16 @@ bool cdf_read_coordinates(struct cdfLazyGrid *restrict lazyGrid, ncvar_t *ncvar,
                   }
               }
           }
-        if ( yvarid != UNDEFID )
+        if ( yvarid != CDI_UNDEFID )
           {
             grid->y.flag = 1;
             int bvarid = ncvars[yvarid].bounds;
-            if ( bvarid != UNDEFID )
+            if ( bvarid != CDI_UNDEFID )
               {
                 int nbdims = ncvars[bvarid].ndims;
                 if ( nbdims == 2 || nbdims == 3 )
                   {
-                    if ( *vdimid == UNDEFID )
+                    if ( *vdimid == CDI_UNDEFID )
                       {
                         *vdimid = ncvars[bvarid].dimids[nbdims-1];
                         grid->nvertex = (int)ncdims[*vdimid].len;
@@ -2343,7 +2342,7 @@ bool cdf_read_coordinates(struct cdfLazyGrid *restrict lazyGrid, ncvar_t *ncvar,
               }
           }
 
-        if ( ncvar->cellarea != UNDEFID )
+        if ( ncvar->cellarea != CDI_UNDEFID )
           cdf_load_cellarea(size, ncvar, &grid->area, &lazyGrid->cellAreaGet);
 
         break;
@@ -2401,13 +2400,13 @@ bool cdf_read_coordinates(struct cdfLazyGrid *restrict lazyGrid, ncvar_t *ncvar,
 }
 
 static
-bool cdf_set_unstructured_par(ncvar_t *ncvar, grid_t *grid, int xdimid, int ydimid, int number_of_grid_used, unsigned char *uuidOfHGrid)
+bool cdf_set_unstructured_par(ncvar_t *ncvar, grid_t *grid, int *xdimid, int *ydimid, int number_of_grid_used, unsigned char *uuidOfHGrid)
 {
   int ndims = ncvar->ndims;
   int *dimtype = ncvar->dimtype;
 
-  int zdimid = UNDEFID;
-  int xdimidx = UNDEFID, ydimidx = UNDEFID;
+  int zdimid = CDI_UNDEFID;
+  int xdimidx = CDI_UNDEFID, ydimidx = CDI_UNDEFID;
 
   for ( int i = 0; i < ndims; i++ )
     {
@@ -2416,20 +2415,20 @@ bool cdf_set_unstructured_par(ncvar_t *ncvar, grid_t *grid, int xdimid, int ydim
       else if ( dimtype[i] == Z_AXIS ) zdimid = ncvar->dimids[i];
     }
 
-  if ( xdimid != UNDEFID && ydimid != UNDEFID && zdimid == UNDEFID )
+  if ( *xdimid != CDI_UNDEFID && *ydimid != CDI_UNDEFID && zdimid == CDI_UNDEFID )
     {
       if ( grid->x.size > grid->y.size && grid->y.size < 1000 )
         {
           dimtype[ydimidx] = Z_AXIS;
-          ydimid = UNDEFID;
+          *ydimid = CDI_UNDEFID;
           grid->size  = grid->x.size;
           grid->y.size = 0;
         }
       else if ( grid->y.size > grid->x.size && grid->x.size < 1000 )
         {
           dimtype[xdimidx] = Z_AXIS;
-          xdimid = ydimid;
-          ydimid = UNDEFID;
+          *xdimid = *ydimid;
+          *ydimid = CDI_UNDEFID;
           grid->size  = grid->y.size;
           grid->x.size = grid->y.size;
           grid->y.size = 0;
@@ -2443,7 +2442,7 @@ bool cdf_set_unstructured_par(ncvar_t *ncvar, grid_t *grid, int xdimid, int ydim
       return true;
     }
 
-  if ( number_of_grid_used != UNDEFID ) grid->number = number_of_grid_used;
+  if ( number_of_grid_used != CDI_UNDEFID ) grid->number = number_of_grid_used;
   if ( ncvar->position > 0 ) grid->position = ncvar->position;
   if ( uuidOfHGrid[0] != 0 ) memcpy(grid->uuid, uuidOfHGrid, 16);
 
@@ -2473,48 +2472,47 @@ void cdf_read_mapping_atts(int ncid, int gmapvarid, int projID, const char *varn
 static
 void cdf_set_grid_to_similar_vars(ncvar_t *ncvar1, ncvar_t *ncvar2, int gridtype, int xdimid, int ydimid)
 {
-  if ( ncvar2->isvar == TRUE && ncvar2->gridID == UNDEFID )
+  if ( ncvar2->isvar == TRUE && ncvar2->gridID == CDI_UNDEFID )
     {
-      int xdimid2 = UNDEFID, ydimid2 = UNDEFID, zdimid2 = UNDEFID;
-      int xdimidx = UNDEFID, ydimidx = UNDEFID;
+      int xdimid2 = CDI_UNDEFID, ydimid2 = CDI_UNDEFID, zdimid2 = CDI_UNDEFID;
+      int xdimidx = CDI_UNDEFID, ydimidx = CDI_UNDEFID;
       int ndims2 = ncvar2->ndims;
 
+      int *dimtype2 = ncvar2->dimtype;
+      int *dimids2 = ncvar2->dimids;
       for ( int i = 0; i < ndims2; i++ )
         {
-          int *dimtype2 = ncvar2->dimtype;
-          int *dimids2 = ncvar2->dimids;
           if      ( dimtype2[i] == X_AXIS ) { xdimid2 = dimids2[i]; xdimidx = i; }
           else if ( dimtype2[i] == Y_AXIS ) { ydimid2 = dimids2[i]; ydimidx = i; }
           else if ( dimtype2[i] == Z_AXIS ) { zdimid2 = dimids2[i]; }
         }
 
-      if ( ncvar2->gridtype == UNDEFID && gridtype == GRID_UNSTRUCTURED )
+      if ( ncvar2->gridtype == CDI_UNDEFID && gridtype == GRID_UNSTRUCTURED )
         {
-          if ( xdimid == xdimid2 && ydimid2 != UNDEFID && zdimid2 == UNDEFID )
+          if ( xdimid == xdimid2 && ydimid2 != CDI_UNDEFID && zdimid2 == CDI_UNDEFID )
             {
               ncvar2->dimtype[ydimidx] = Z_AXIS;
-              ydimid2 = UNDEFID;
+              ydimid2 = CDI_UNDEFID;
             }
 
-          if ( xdimid == ydimid2 && xdimid2 != UNDEFID && zdimid2 == UNDEFID )
+          if ( xdimid == ydimid2 && xdimid2 != CDI_UNDEFID && zdimid2 == CDI_UNDEFID )
             {
               ncvar2->dimtype[xdimidx] = Z_AXIS;
               xdimid2 = ydimid2;
-              ydimid2 = UNDEFID;
+              ydimid2 = CDI_UNDEFID;
             }
         }
 
-      if ( xdimid == xdimid2 &&
-           (ydimid == ydimid2 || (xdimid == ydimid && ydimid2 == UNDEFID)) )
+      if ( xdimid == xdimid2 && (ydimid == ydimid2 || (xdimid == ydimid && ydimid2 == CDI_UNDEFID)) )
         {
           bool same_grid = ncvar1->xvarid == ncvar2->xvarid
-            && ncvar1->yvarid == ncvar2->yvarid
-            && ncvar1->position == ncvar2->position;
+                        && ncvar1->yvarid == ncvar2->yvarid
+                        && ncvar1->position == ncvar2->position;
           /*
-            if ( xvarid != -1 && ncvar2->xvarid != UNDEFID &&
+            if ( xvarid != -1 && ncvar2->xvarid != CDI_UNDEFID &&
             xvarid != ncvar2->xvarid ) same_grid = false;
 
-            if ( yvarid != -1 && ncvar2->yvarid != UNDEFID &&
+            if ( yvarid != -1 && ncvar2->yvarid != CDI_UNDEFID &&
             yvarid != ncvar2->yvarid ) same_grid = false;
           */
 
@@ -2529,34 +2527,31 @@ void cdf_set_grid_to_similar_vars(ncvar_t *ncvar1, ncvar_t *ncvar2, int gridtype
 }
 
 static
-void cdf_define_all_grids(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int nvars, ncvar_t *ncvars,
+void cdf_define_all_grids(ncgrid_t *ncgrid, int vlistID, ncdim_t *ncdims, int nvars, ncvar_t *ncvars,
                           int timedimid, unsigned char *uuidOfHGrid, char *gridfile, int number_of_grid_used)
 {
   for ( int ncvarid = 0; ncvarid < nvars; ++ncvarid )
     {
       ncvar_t *ncvar = &ncvars[ncvarid];
-      if ( ncvar->isvar && ncvar->gridID == UNDEFID )
+      if ( ncvar->isvar && ncvar->gridID == CDI_UNDEFID )
 	{
-          int gmapvarid = ncvar->gmapid;
-          bool lproj = gmapvarid != UNDEFID;
-          bool lgrid = !(lproj && ncvar->xvarid == UNDEFID);
           int ndims = ncvar->ndims;
           int *dimtype = ncvar->dimtype;
-          int vdimid = UNDEFID;
+          int vdimid = CDI_UNDEFID;
           struct addIfNewRes projAdded = { .Id = CDI_UNDEFID, .isNew = 0 },
                              gridAdded = { .Id = CDI_UNDEFID, .isNew = 0 };
-	  int xdimid = UNDEFID, ydimid = UNDEFID;
+	  int xdimid = CDI_UNDEFID, ydimid = CDI_UNDEFID;
           int nydims = cdf_get_xydimid(ndims, ncvar->dimids, dimtype, &xdimid, &ydimid);
 
-          int xaxisid = (xdimid != UNDEFID) ? ncdims[xdimid].ncvarid : UNDEFID;
-          int yaxisid = (ydimid != UNDEFID) ? ncdims[ydimid].ncvarid : UNDEFID;
-          int xvarid = (ncvar->xvarid != UNDEFID) ? ncvar->xvarid : xaxisid;
-          int yvarid = (ncvar->yvarid != UNDEFID) ? ncvar->yvarid : yaxisid;
+          int xaxisid = (xdimid != CDI_UNDEFID) ? ncdims[xdimid].ncvarid : CDI_UNDEFID;
+          int yaxisid = (ydimid != CDI_UNDEFID) ? ncdims[ydimid].ncvarid : CDI_UNDEFID;
+          int xvarid = (ncvar->xvarid != CDI_UNDEFID) ? ncvar->xvarid : xaxisid;
+          int yvarid = (ncvar->yvarid != CDI_UNDEFID) ? ncvar->yvarid : yaxisid;
 
-	  size_t xsize = (xdimid != UNDEFID) ? ncdims[xdimid].len : 0;
-	  size_t ysize = (ydimid != UNDEFID) ? ncdims[ydimid].len : 0;
+	  size_t xsize = (xdimid != CDI_UNDEFID) ? ncdims[xdimid].len : 0;
+	  size_t ysize = (ydimid != CDI_UNDEFID) ? ncdims[ydimid].len : 0;
 
-	  if ( ydimid == UNDEFID && yvarid != UNDEFID )
+	  if ( ydimid == CDI_UNDEFID && yvarid != CDI_UNDEFID )
 	    {
 	      if ( ncvars[yvarid].ndims == 1 )
 		{
@@ -2565,8 +2560,18 @@ void cdf_define_all_grids(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
 		}
 	    }
 
-          bool lunstructured = xdimid != UNDEFID && xdimid == ydimid && nydims == 0;
-	  if ( (ncvar->gridtype == UNDEFID || ncvar->gridtype == GRID_GENERIC) && lunstructured )
+          int gmapvarid = ncvar->gmapid;
+          bool lproj = gmapvarid != CDI_UNDEFID;
+
+          if ( !lproj && xaxisid != CDI_UNDEFID && xaxisid != xvarid && yaxisid != CDI_UNDEFID && yaxisid != yvarid )
+            {
+              lproj = true;
+            }
+
+          bool lgrid = !(lproj && ncvar->xvarid == CDI_UNDEFID);
+
+          bool lunstructured = xdimid != CDI_UNDEFID && xdimid == ydimid && nydims == 0;
+	  if ( (ncvar->gridtype == CDI_UNDEFID || ncvar->gridtype == GRID_GENERIC) && lunstructured )
             ncvar->gridtype = GRID_UNSTRUCTURED;
 
           struct cdfLazyGrid *restrict lazyGrid = NULL, *restrict lazyProj = NULL;
@@ -2587,19 +2592,21 @@ void cdf_define_all_grids(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
           grid_t *grid = &lazyGrid->base;
           grid_t *proj = ( lgrid && lproj ) ? &lazyProj->base : NULL;
 
-          xaxisid = (xdimid != UNDEFID) ? ncdims[xdimid].ncvarid : UNDEFID;
-          yaxisid = (ydimid != UNDEFID) ? ncdims[ydimid].ncvarid : UNDEFID;
+          xaxisid = (xdimid != CDI_UNDEFID) ? ncdims[xdimid].ncvarid : CDI_UNDEFID;
+          yaxisid = (ydimid != CDI_UNDEFID) ? ncdims[ydimid].ncvarid : CDI_UNDEFID;
 
 
           if ( cdf_read_coordinates(lazyGrid, ncvar, ncvars, ncdims,
                                     timedimid, xvarid, yvarid, xsize, ysize, &vdimid) )
             continue;
 
-	  if ( number_of_grid_used != UNDEFID && (grid->type == UNDEFID || grid->type == GRID_GENERIC) )
+	  if ( number_of_grid_used != CDI_UNDEFID &&
+               (grid->type == CDI_UNDEFID || grid->type == GRID_GENERIC) &&
+               xdimid != CDI_UNDEFID && xsize > 9999 )
             grid->type = GRID_UNSTRUCTURED;
 
           if ( grid->type == GRID_UNSTRUCTURED )
-            if ( cdf_set_unstructured_par(ncvar, grid, xdimid, ydimid, number_of_grid_used, uuidOfHGrid) )
+            if ( cdf_set_unstructured_par(ncvar, grid, &xdimid, &ydimid, number_of_grid_used, uuidOfHGrid) )
               continue;
 
           if ( lproj && lgrid )
@@ -2630,7 +2637,7 @@ void cdf_define_all_grids(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
 
           int gridID = ncvar->gridID;
 
-          if ( lproj )
+          if ( lproj && gmapvarid != CDI_UNDEFID )
             {
               int projID = lgrid ? grid->proj : gridID;
               int ncid = ncvars[gmapvarid].ncid;
@@ -2646,14 +2653,16 @@ void cdf_define_all_grids(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
           if ( ncvar->chunked ) grid_set_chunktype(grid, ncvar);
 
 	  int gridindex = vlistGridIndex(vlistID, gridID);
-	  streamptr->xdimID[gridindex] = xdimid;
-	  streamptr->ydimID[gridindex] = ydimid;
-          if ( xdimid == UNDEFID && ydimid == UNDEFID && grid->size == 1 )
+          ncgrid[gridindex].gridID = gridID;
+          ncgrid[gridindex].xdimID = xdimid;
+          ncgrid[gridindex].ydimID = ydimid;
+
+          if ( xdimid == CDI_UNDEFID && ydimid == CDI_UNDEFID && grid->size == 1 )
             gridDefHasDims(gridID, FALSE);
 
-          if ( xdimid != UNDEFID ) cdiGridDefKeyStr(gridID, CDI_KEY_XDIMNAME, (int)(strlen(ncdims[xdimid].name)+1), ncdims[xdimid].name);
-          if ( ydimid != UNDEFID ) cdiGridDefKeyStr(gridID, CDI_KEY_YDIMNAME, (int)(strlen(ncdims[ydimid].name)+1), ncdims[ydimid].name);
-          if ( vdimid != UNDEFID ) cdiGridDefKeyStr(gridID, CDI_KEY_VDIMNAME, (int)(strlen(ncdims[vdimid].name)+1), ncdims[vdimid].name);
+          if ( xdimid != CDI_UNDEFID ) cdiGridDefKeyStr(gridID, CDI_KEY_XDIMNAME, (int)(strlen(ncdims[xdimid].name)+1), ncdims[xdimid].name);
+          if ( ydimid != CDI_UNDEFID ) cdiGridDefKeyStr(gridID, CDI_KEY_YDIMNAME, (int)(strlen(ncdims[ydimid].name)+1), ncdims[ydimid].name);
+          if ( vdimid != CDI_UNDEFID ) cdiGridDefKeyStr(gridID, CDI_KEY_VDIMNAME, (int)(strlen(ncdims[vdimid].name)+1), ncdims[vdimid].name);
 
 	  if ( CDI_Debug ) Message("gridID %d %d %s", gridID, ncvarid, ncvar->name);
 
@@ -2690,12 +2699,12 @@ void cdf_define_all_zaxes(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
   for ( int ncvarid = 0; ncvarid < nvars; ncvarid++ )
     {
       ncvar_t *ncvar = &ncvars[ncvarid];
-      if ( ncvar->isvar == TRUE && ncvar->zaxisID == UNDEFID )
+      if ( ncvar->isvar == TRUE && ncvar->zaxisID == CDI_UNDEFID )
 	{
           bool is_scalar = false;
 	  bool with_bounds = false;
-	  int zdimid = UNDEFID;
-	  int zvarid = UNDEFID;
+	  int zdimid = CDI_UNDEFID;
+	  int zvarid = CDI_UNDEFID;
 	  int zsize = 1;
           int psvarid = -1;
           int p0varid = -1;
@@ -2718,7 +2727,7 @@ void cdf_define_all_zaxes(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
                     zdimid = ncvar->dimids[i];
                 }
 
-              if ( zdimid != UNDEFID )
+              if ( zdimid != CDI_UNDEFID )
                 {
                   zvarid = ncdims[zdimid].ncvarid;
                   zsize  = (int)ncdims[zdimid].len;
@@ -2729,13 +2738,13 @@ void cdf_define_all_zaxes(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
 
 	  double *zvar = NULL;
 
-	  int zaxisType = UNDEFID;
-	  if ( zvarid != UNDEFID ) zaxisType = ncvars[zvarid].zaxistype;
-	  if ( zaxisType == UNDEFID ) zaxisType = ZAXIS_GENERIC;
+	  int zaxisType = CDI_UNDEFID;
+	  if ( zvarid != CDI_UNDEFID ) zaxisType = ncvars[zvarid].zaxistype;
+	  if ( zaxisType == CDI_UNDEFID ) zaxisType = ZAXIS_GENERIC;
 
 	  int zprec = CDI_DATATYPE_FLT64;
 
-	  if ( zvarid != UNDEFID )
+	  if ( zvarid != CDI_UNDEFID )
 	    {
 	      positive  = ncvars[zvarid].positive;
 	      pname     = ncvars[zvarid].name;
@@ -2760,7 +2769,7 @@ void cdf_define_all_zaxes(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
               zvar = (double*) Malloc((size_t)zsize*sizeof(double));
 	      cdf_get_var_double(ncvars[zvarid].ncid, zvarid, zvar);
 
-	      if ( ncvars[zvarid].bounds != UNDEFID )
+	      if ( ncvars[zvarid].bounds != CDI_UNDEFID )
 		{
 		  int nbdims = ncvars[ncvars[zvarid].bounds].ndims;
 		  if ( nbdims == 2 )
@@ -2785,16 +2794,19 @@ void cdf_define_all_zaxes(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
 	    }
 	  else
 	    {
-              pname     = (zdimid != UNDEFID) ? ncdims[zdimid].name : NULL;
+              pname     = (zdimid != CDI_UNDEFID) ? ncdims[zdimid].name : NULL;
 	      plongname = NULL;
 	      punits    = NULL;
 
-	      if ( zsize == 1 && zdimid == UNDEFID )
+	      if ( zsize == 1 && zdimid == CDI_UNDEFID )
 		{
-                  zaxisType = (ncvar->zaxistype != UNDEFID) ? ncvar->zaxistype : ZAXIS_SURFACE;
-                  zvar = (double*) Malloc(sizeof(double));
-		  zvar[0] = 0;
-		}
+                  zaxisType = (ncvar->zaxistype != CDI_UNDEFID) ? ncvar->zaxistype : ZAXIS_SURFACE;
+                  // if ( pname )
+                    {
+                      zvar = (double*) Malloc(sizeof(double));
+                      zvar[0] = 0;
+                    }
+                }
 	    }
 
       	  ncvar->zaxisID = varDefZaxis(vlistID, zaxisType, (int) zsize, zvar, with_bounds, lbounds, ubounds,
@@ -2823,7 +2835,7 @@ void cdf_define_all_zaxes(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
           if ( positive > 0 ) zaxisDefPositive(zaxisID, positive);
           if ( is_scalar ) zaxisDefScalar(zaxisID);
 
-          if ( zdimid != UNDEFID )
+          if ( zdimid != CDI_UNDEFID )
             cdiZaxisDefKeyStr(zaxisID, CDI_KEY_DIMNAME, (int)(strlen(ncdims[zdimid].name)+1), ncdims[zdimid].name);
           /*
           if ( vdimid != -1 )
@@ -2833,7 +2845,7 @@ void cdf_define_all_zaxes(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
 	  if ( lbounds ) Free(lbounds);
 	  if ( ubounds ) Free(ubounds);
 
-          if ( zvarid != UNDEFID )
+          if ( zvarid != CDI_UNDEFID )
             {
               int ncid = ncvars[zvarid].ncid;
               int nvatts = ncvars[zvarid].natts;
@@ -2851,13 +2863,13 @@ void cdf_define_all_zaxes(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
 	    Message("zaxisID %d %d %s", zaxisID, ncvarid, ncvar->name);
 
 	  for ( int ncvarid2 = ncvarid+1; ncvarid2 < nvars; ncvarid2++ )
-	    if ( ncvars[ncvarid2].isvar == TRUE && ncvars[ncvarid2].zaxisID == UNDEFID /*&& ncvars[ncvarid2].zaxistype == UNDEFID*/ )
+	    if ( ncvars[ncvarid2].isvar == TRUE && ncvars[ncvarid2].zaxisID == CDI_UNDEFID /*&& ncvars[ncvarid2].zaxistype == CDI_UNDEFID*/ )
 	      {
-                int zvarid2 = UNDEFID;
-                if ( ncvars[ncvarid2].zvarid != UNDEFID && ncvars[ncvars[ncvarid2].zvarid].ndims == 0 )
+                int zvarid2 = CDI_UNDEFID;
+                if ( ncvars[ncvarid2].zvarid != CDI_UNDEFID && ncvars[ncvars[ncvarid2].zvarid].ndims == 0 )
                   zvarid2 = ncvars[ncvarid2].zvarid;
 
-		int zdimid2 = UNDEFID;
+		int zdimid2 = CDI_UNDEFID;
 		ndims = ncvars[ncvarid2].ndims;
 		for ( int i = 0; i < ndims; i++ )
 		  {
@@ -2867,10 +2879,10 @@ void cdf_define_all_zaxes(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int
 
 		if ( zdimid == zdimid2 /* && zvarid == zvarid2 */)
 		  {
-                    if ( (zdimid != UNDEFID && ncvars[ncvarid2].zaxistype == UNDEFID) ||
-                         (zdimid == UNDEFID && zvarid != UNDEFID && zvarid == zvarid2) ||
-                         (zdimid == UNDEFID && zaxisType == ncvars[ncvarid2].zaxistype) ||
-                         (zdimid == UNDEFID && zvarid2 == UNDEFID && ncvars[ncvarid2].zaxistype == UNDEFID) )
+                    if ( (zdimid != CDI_UNDEFID && ncvars[ncvarid2].zaxistype == CDI_UNDEFID) ||
+                         (zdimid == CDI_UNDEFID && zvarid != CDI_UNDEFID && zvarid == zvarid2) ||
+                         (zdimid == CDI_UNDEFID && zaxisType == ncvars[ncvarid2].zaxistype) ||
+                         (zdimid == CDI_UNDEFID && zvarid2 == CDI_UNDEFID && ncvars[ncvarid2].zaxistype == CDI_UNDEFID) )
                       {
                         if ( CDI_Debug )
                           Message("zaxisID %d %d %s", zaxisID, ncvarid2, ncvars[ncvarid2].name);
@@ -2937,7 +2949,7 @@ void cdf_define_all_vars(stream_t *streamptr, int vlistID, int instID, int model
       if ( ncvars[ncvarid].deflate )
 	vlistDefVarCompType(vlistID, varID, CDI_COMPRESS_ZIP);
 
-      if ( ncvars[ncvarid].chunked && ncvars[ncvarid].chunktype != UNDEFID )
+      if ( ncvars[ncvarid].chunked && ncvars[ncvarid].chunktype != CDI_UNDEFID )
         vlistDefVarChunkType(vlistID, varID, ncvars[ncvarid].chunktype);
 #endif
 
@@ -2945,9 +2957,9 @@ void cdf_define_all_vars(stream_t *streamptr, int vlistID, int instID, int model
       streamptr->vars[varID1].ncvarid = ncvarid;
 
       vlistDefVarName(vlistID, varID, ncvars[ncvarid].name);
-      if ( ncvars[ncvarid].param != UNDEFID ) vlistDefVarParam(vlistID, varID, ncvars[ncvarid].param);
-      if ( ncvars[ncvarid].code != UNDEFID )  vlistDefVarCode(vlistID, varID, ncvars[ncvarid].code);
-      if ( ncvars[ncvarid].code != UNDEFID )
+      if ( ncvars[ncvarid].param != CDI_UNDEFID ) vlistDefVarParam(vlistID, varID, ncvars[ncvarid].param);
+      if ( ncvars[ncvarid].code != CDI_UNDEFID )  vlistDefVarCode(vlistID, varID, ncvars[ncvarid].code);
+      if ( ncvars[ncvarid].code != CDI_UNDEFID )
 	{
 	  int param = cdiEncodeParam(ncvars[ncvarid].code, ncvars[ncvarid].tabnum, 255);
 	  vlistDefVarParam(vlistID, varID, param);
@@ -2968,7 +2980,7 @@ void cdf_define_all_vars(stream_t *streamptr, int vlistID, int instID, int model
 
       vlistDefVarInstitut(vlistID, varID, instID);
       vlistDefVarModel(vlistID, varID, modelID);
-      if ( ncvars[ncvarid].tableID != UNDEFID )
+      if ( ncvars[ncvarid].tableID != CDI_UNDEFID )
 	vlistDefVarTable(vlistID, varID, ncvars[ncvarid].tableID);
 
       if ( ncvars[ncvarid].deffillval == false && ncvars[ncvarid].defmissval )
@@ -2985,8 +2997,8 @@ void cdf_define_all_vars(stream_t *streamptr, int vlistID, int instID, int model
 		vlistInqVarGrid(vlistID, varID), vlistInqVarZaxis(vlistID, varID));
 
       int gridindex = vlistGridIndex(vlistID, gridID);
-      int xdimid = streamptr->xdimID[gridindex];
-      int ydimid = streamptr->ydimID[gridindex];
+      int xdimid = streamptr->ncgrid[gridindex].xdimID;
+      int ydimid = streamptr->ncgrid[gridindex].ydimID;
 
       int zaxisindex = vlistZaxisIndex(vlistID, zaxisID);
       int zdimid = streamptr->zaxisID[zaxisindex];
@@ -3109,7 +3121,7 @@ void cdf_define_all_vars(stream_t *streamptr, int vlistID, int instID, int model
       int varModelID = vlistInqVarModel(vlistID, varID);
       int varTableID = vlistInqVarTable(vlistID, varID);
       int code = vlistInqVarCode(vlistID, varID);
-      if ( cdiDefaultTableID != UNDEFID )
+      if ( cdiDefaultTableID != CDI_UNDEFID )
 	{
 	  if ( tableInqParNamePtr(cdiDefaultTableID, code) )
 	    {
@@ -3117,7 +3129,7 @@ void cdf_define_all_vars(stream_t *streamptr, int vlistID, int instID, int model
 	      vlistDestroyVarLongname(vlistID, varID);
 	      vlistDestroyVarUnits(vlistID, varID);
 
-	      if ( varTableID != UNDEFID )
+	      if ( varTableID != CDI_UNDEFID )
 		{
 		  vlistDefVarName(vlistID, varID, tableInqParNamePtr(cdiDefaultTableID, code));
 		  if ( tableInqParLongnamePtr(cdiDefaultTableID, code) )
@@ -3131,12 +3143,12 @@ void cdf_define_all_vars(stream_t *streamptr, int vlistID, int instID, int model
 		}
 	    }
 
-	  if ( cdiDefaultModelID != UNDEFID ) varModelID = cdiDefaultModelID;
-	  if ( cdiDefaultInstID  != UNDEFID ) varInstID  = cdiDefaultInstID;
+	  if ( cdiDefaultModelID != CDI_UNDEFID ) varModelID = cdiDefaultModelID;
+	  if ( cdiDefaultInstID  != CDI_UNDEFID ) varInstID  = cdiDefaultInstID;
 	}
-      if ( varInstID  != UNDEFID ) vlistDefVarInstitut(vlistID, varID, varInstID);
-      if ( varModelID != UNDEFID ) vlistDefVarModel(vlistID, varID, varModelID);
-      if ( varTableID != UNDEFID ) vlistDefVarTable(vlistID, varID, varTableID);
+      if ( varInstID  != CDI_UNDEFID ) vlistDefVarInstitut(vlistID, varID, varInstID);
+      if ( varModelID != CDI_UNDEFID ) vlistDefVarModel(vlistID, varID, varModelID);
+      if ( varTableID != CDI_UNDEFID ) vlistDefVarTable(vlistID, varID, varTableID);
     }
 }
 
@@ -3169,13 +3181,13 @@ void cdf_scan_global_attr(int fileID, int vlistID, stream_t *streamptr, int ngat
 	      else if ( strcmp(attname, "institution") == 0 )
 		{
 		  *instID = institutInq(0, 0, NULL, attstring);
-		  if ( *instID == UNDEFID )
+		  if ( *instID == CDI_UNDEFID )
 		    *instID = institutDef(0, 0, NULL, attstring);
 		}
 	      else if ( strcmp(attname, "source") == 0 )
 		{
 		  *modelID = modelInq(-1, 0, attstring);
-		  if ( *modelID == UNDEFID )
+		  if ( *modelID == CDI_UNDEFID )
 		    *modelID = modelDef(-1, 0, attstring);
 		}
 	      else if ( strcmp(attname, "Source") == 0 )
@@ -3228,7 +3240,7 @@ void cdf_scan_global_attr(int fileID, int vlistID, stream_t *streamptr, int ngat
 	{
 	  if ( strcmp(attname, "number_of_grid_used") == 0 )
 	    {
-	      (*number_of_grid_used) = UNDEFID;
+	      (*number_of_grid_used) = CDI_UNDEFID;
 	      cdfGetAttInt(fileID, NC_GLOBAL, attname, 1, number_of_grid_used);
 	    }
  	  else
@@ -3252,7 +3264,7 @@ void cdf_scan_global_attr(int fileID, int vlistID, stream_t *streamptr, int ngat
 static
 int find_leadtime(int nvars, ncvar_t *ncvars)
 {
-  int leadtime_id = UNDEFID;
+  int leadtime_id = CDI_UNDEFID;
 
   for ( int ncvarid = 0; ncvarid < nvars; ncvarid++ )
     {
@@ -3272,7 +3284,7 @@ void find_time_vars(int nvars, ncvar_t *ncvars, ncdim_t *ncdims, int timedimid,
 {
   int ncvarid;
 
-  if ( timedimid == UNDEFID )
+  if ( timedimid == CDI_UNDEFID )
     {
       char timeunits[CDI_MAX_NAME];
 
@@ -3298,7 +3310,7 @@ void find_time_vars(int nvars, ncvar_t *ncvars, ncdim_t *ncdims, int timedimid,
     {
       bool ltimevar = false;
 
-      if ( ncdims[timedimid].ncvarid != UNDEFID )
+      if ( ncdims[timedimid].ncvarid != CDI_UNDEFID )
         {
           streamptr->basetime.ncvarid = ncdims[timedimid].ncvarid;
           ltimevar = true;
@@ -3344,7 +3356,7 @@ void find_time_vars(int nvars, ncvar_t *ncvars, ncdim_t *ncdims, int timedimid,
       /* time varID */
       ncvarid = streamptr->basetime.ncvarid;
 
-      if ( ncvarid == UNDEFID )
+      if ( ncvarid == CDI_UNDEFID )
         {
           Warning("Time variable >%s< not found!", ncdims[timedimid].name);
         }
@@ -3353,11 +3365,11 @@ void find_time_vars(int nvars, ncvar_t *ncvars, ncdim_t *ncdims, int timedimid,
   /* time varID */
   ncvarid = streamptr->basetime.ncvarid;
 
-  if ( ncvarid != UNDEFID && streamptr->basetime.lwrf == false )
+  if ( ncvarid != CDI_UNDEFID && streamptr->basetime.lwrf == false )
     {
       if ( ncvars[ncvarid].units[0] != 0 ) *time_has_units = true;
 
-      if ( ncvars[ncvarid].bounds != UNDEFID )
+      if ( ncvars[ncvarid].bounds != CDI_UNDEFID )
         {
           int nbdims = ncvars[ncvars[ncvarid].bounds].ndims;
           if ( nbdims == 2 )
@@ -3378,7 +3390,7 @@ static
 void read_vct_echam(int fileID, int nvars, ncvar_t *ncvars, ncdim_t *ncdims, double **vct, size_t *pvctsize)
 {
   /* find ECHAM VCT */
-  int nvcth_id = UNDEFID, vcta_id = UNDEFID, vctb_id = UNDEFID;
+  int nvcth_id = CDI_UNDEFID, vcta_id = CDI_UNDEFID, vctb_id = CDI_UNDEFID;
 
   for ( int ncvarid = 0; ncvarid < nvars; ncvarid++ )
     {
@@ -3408,7 +3420,7 @@ void read_vct_echam(int fileID, int nvars, ncvar_t *ncvars, ncdim_t *ncdims, dou
     }
 
   /* read VCT */
-  if ( nvcth_id != UNDEFID && vcta_id != UNDEFID && vctb_id != UNDEFID )
+  if ( nvcth_id != CDI_UNDEFID && vcta_id != CDI_UNDEFID && vctb_id != CDI_UNDEFID )
     {
       size_t vctsize = ncdims[nvcth_id].len;
       vctsize *= 2;
@@ -3427,7 +3439,7 @@ void cdf_set_ucla_dimtype(int ndims, ncdim_t *ncdims, ncvar_t *ncvars)
       int ncvarid = ncdims[ncdimid].ncvarid;
       if ( ncvarid != -1 )
         {
-          if ( ncdims[ncdimid].dimtype == UNDEFID && ncvars[ncvarid].units[0] == 'm' )
+          if ( ncdims[ncdimid].dimtype == CDI_UNDEFID && ncvars[ncvarid].units[0] == 'm' )
             {
               if      ( ncvars[ncvarid].name[0] == 'x' ) ncdims[ncdimid].dimtype = X_AXIS;
               else if ( ncvars[ncvarid].name[0] == 'y' ) ncdims[ncdimid].dimtype = Y_AXIS;
@@ -3442,7 +3454,7 @@ int cdf_check_vars(int nvars, ncvar_t *ncvars, int ntsteps, int timedimid)
 {
   for ( int ncvarid = 0; ncvarid < nvars; ncvarid++ )
     {
-      if ( timedimid != UNDEFID )
+      if ( timedimid != CDI_UNDEFID )
 	if ( ncvars[ncvarid].isvar == -1 &&
 	     ncvars[ncvarid].ndims > 1   &&
 	     timedimid == ncvars[ncvarid].dimids[0] )
@@ -3470,7 +3482,7 @@ int cdf_check_vars(int nvars, ncvar_t *ncvars, int ntsteps, int timedimid)
 	  continue;
 	}
 
-      if ( ncvars[ncvarid].ndims == 4 && timedimid == UNDEFID )
+      if ( ncvars[ncvarid].ndims == 4 && timedimid == CDI_UNDEFID )
 	{
 	  ncvars[ncvarid].isvar = 0;
 	  Warning("%d dimensional variables without time dimension are not supported, skipped variable %s!",
@@ -3491,7 +3503,7 @@ int cdf_check_vars(int nvars, ncvar_t *ncvars, int ntsteps, int timedimid)
 	  continue;
 	}
 
-      if ( timedimid != UNDEFID && ntsteps == 0 && ncvars[ncvarid].ndims > 0 )
+      if ( timedimid != CDI_UNDEFID && ntsteps == 0 && ncvars[ncvarid].ndims > 0 )
 	{
 	  if ( timedimid == ncvars[ncvarid].dimids[0] )
 	    {
@@ -3517,16 +3529,16 @@ int cdfInqContents(stream_t *streamptr)
   bool time_has_units = false;
   bool time_has_bounds = false;
   bool time_climatology = false;
-  int leadtime_id = UNDEFID;
+  int leadtime_id = CDI_UNDEFID;
   int nvars_data;
-  int instID  = UNDEFID;
-  int modelID = UNDEFID;
-  int calendar = UNDEFID;
+  int instID  = CDI_UNDEFID;
+  int modelID = CDI_UNDEFID;
+  int calendar = CDI_UNDEFID;
   int format = 0;
   bool ucla_les = false;
   char gridfile[8912];
   char fcreftime[CDI_MAX_NAME];
-  int number_of_grid_used = UNDEFID;
+  int number_of_grid_used = CDI_UNDEFID;
 
   unsigned char uuidOfHGrid[CDI_UUID_SIZE];
   unsigned char uuidOfVGrid[CDI_UUID_SIZE];
@@ -3612,7 +3624,7 @@ int cdfInqContents(stream_t *streamptr)
   streamptr->basetime.ncdimid = timedimid;
 
   size_t ntsteps = 0;
-  if ( timedimid != UNDEFID ) cdf_inq_dimlen(fileID, timedimid, &ntsteps);
+  if ( timedimid != CDI_UNDEFID ) cdf_inq_dimlen(fileID, timedimid, &ntsteps);
 
   if ( CDI_Debug ) Message("Number of timesteps = %d", ntsteps);
   if ( CDI_Debug ) Message("Time dimid = %d", streamptr->basetime.ncdimid);
@@ -3641,7 +3653,7 @@ int cdfInqContents(stream_t *streamptr)
 	{
 	  if ( ncvars[ncvarid].ndims == 1 )
 	    {
-	      if ( timedimid != UNDEFID && timedimid == ncvars[ncvarid].dimids[0] )
+	      if ( timedimid != CDI_UNDEFID && timedimid == ncvars[ncvarid].dimids[0] )
 		{
 		  if ( ncvars[ncvarid].isvar != FALSE ) cdf_set_var(ncvars, ncvarid, TRUE);
 		}
@@ -3651,7 +3663,7 @@ int cdfInqContents(stream_t *streamptr)
 		}
 	      // if ( ncvars[ncvarid].isvar != TRUE ) cdf_set_var(ncvars, ncvarid, FALSE);
 
-	      if ( ncdimid == ncvars[ncvarid].dimids[0] && ncdims[ncdimid].ncvarid == UNDEFID )
+	      if ( ncdimid == ncvars[ncvarid].dimids[0] && ncdims[ncdimid].ncvarid == CDI_UNDEFID )
 		if ( strcmp(ncvars[ncvarid].name, ncdims[ncdimid].name) == 0 )
 		  {
 		    ncdims[ncdimid].ncvarid = ncvarid;
@@ -3665,7 +3677,7 @@ int cdfInqContents(stream_t *streamptr)
   find_time_vars(nvars, ncvars, ncdims, timedimid, streamptr, &time_has_units, &time_has_bounds, &time_climatology);
 
   leadtime_id = find_leadtime(nvars, ncvars);
-  if ( leadtime_id != UNDEFID ) ncvars[leadtime_id].isvar = FALSE;
+  if ( leadtime_id != CDI_UNDEFID ) ncvars[leadtime_id].isvar = FALSE;
 
   /* check ncvars */
   timedimid = cdf_check_vars(nvars, ncvars, ntsteps, timedimid);
@@ -3737,7 +3749,7 @@ int cdfInqContents(stream_t *streamptr)
   if ( CDI_Debug ) cdf_print_vars(ncvars, nvars, "cdf_define_all_grids");
 
   /* define all grids */
-  cdf_define_all_grids(streamptr, vlistID, ncdims, nvars, ncvars, timedimid, uuidOfHGrid, gridfile, number_of_grid_used);
+  cdf_define_all_grids(streamptr->ncgrid, vlistID, ncdims, nvars, ncvars, timedimid, uuidOfHGrid, gridfile, number_of_grid_used);
 
 
   /* define all zaxes */
@@ -3764,7 +3776,7 @@ int cdfInqContents(stream_t *streamptr)
       return CDI_EUFSTRUCT;
     }
 
-  if ( ntsteps == 0 && streamptr->basetime.ncdimid == UNDEFID && streamptr->basetime.ncvarid != UNDEFID )
+  if ( ntsteps == 0 && streamptr->basetime.ncdimid == CDI_UNDEFID && streamptr->basetime.ncvarid != CDI_UNDEFID )
     ntsteps = 1;
 
   streamptr->ntsteps = (long)ntsteps;
@@ -3784,11 +3796,11 @@ int cdfInqContents(stream_t *streamptr)
 
       if ( setBaseTime(ncvars[nctimevarid].units, taxis) == 1 )
         {
-          nctimevarid = UNDEFID;
-          streamptr->basetime.ncvarid = UNDEFID;
+          nctimevarid = CDI_UNDEFID;
+          streamptr->basetime.ncvarid = CDI_UNDEFID;
         }
 
-      if ( leadtime_id != UNDEFID && taxis->type == TAXIS_RELATIVE )
+      if ( leadtime_id != CDI_UNDEFID && taxis->type == TAXIS_RELATIVE )
         {
           streamptr->basetime.leadtimeid = leadtime_id;
           taxis->type = TAXIS_FORECAST;
@@ -3808,7 +3820,7 @@ int cdfInqContents(stream_t *streamptr)
       if ( time_climatology ) streamptr->tsteps[0].taxis.climatology = true;
     }
 
-  if ( nctimevarid != UNDEFID )
+  if ( nctimevarid != CDI_UNDEFID )
     {
       taxis_t *taxis = &streamptr->tsteps[0].taxis;
       ptaxisDefName(taxis, ncvars[nctimevarid].name);
@@ -3820,7 +3832,7 @@ int cdfInqContents(stream_t *streamptr)
       ptaxisDefDatatype(taxis, datatype);
     }
 
-  if ( nctimevarid != UNDEFID )
+  if ( nctimevarid != CDI_UNDEFID )
     if ( ncvars[nctimevarid].calendar == true )
       {
         char attstring[1024];
@@ -3849,7 +3861,7 @@ int cdfInqContents(stream_t *streamptr)
     }
 
 
-  if ( calendar == UNDEFID && streamptr->tsteps[0].taxis.type != TAXIS_ABSOLUTE )
+  if ( calendar == CDI_UNDEFID && streamptr->tsteps[0].taxis.type != TAXIS_ABSOLUTE )
     {
       calendar = CALENDAR_STANDARD;
     }
@@ -3858,7 +3870,7 @@ int cdfInqContents(stream_t *streamptr)
 #pragma GCC diagnostic push
 #pragma GCC diagnostic warning "-Wstrict-overflow"
 #endif
-  if ( calendar != UNDEFID )
+  if ( calendar != CDI_UNDEFID )
     {
       taxis_t *taxis = &streamptr->tsteps[0].taxis;
       taxis->calendar = calendar;
@@ -3960,7 +3972,7 @@ int cdfInqTimestep(stream_t * streamptr, int tsID)
       double timevalue = tsID;
 
       int nctimevarid = streamptr->basetime.ncvarid;
-      if ( nctimevarid != UNDEFID )
+      if ( nctimevarid != CDI_UNDEFID )
 	{
 	  int fileID = streamptr->fileID;
 	  size_t index  = (size_t)tsID;
@@ -3984,7 +3996,7 @@ int cdfInqTimestep(stream_t * streamptr, int tsID)
 	    }
 
 	  int nctimeboundsid = streamptr->basetime.ncvarboundsid;
-	  if ( nctimeboundsid != UNDEFID )
+	  if ( nctimeboundsid != CDI_UNDEFID )
 	    {
 	      size_t start[2], count[2];
               start[0] = index; count[0] = 1; start[1] = 0; count[1] = 1;
@@ -4001,7 +4013,7 @@ int cdfInqTimestep(stream_t * streamptr, int tsID)
 	    }
 
           int leadtimeid = streamptr->basetime.leadtimeid;
-          if ( leadtimeid != UNDEFID )
+          if ( leadtimeid != CDI_UNDEFID )
             {
               timevalue = get_timevalue(fileID, leadtimeid, tsID, NULL);
               cdiSetForecastPeriod(timevalue, taxis);
@@ -4020,7 +4032,7 @@ int cdfInqHistorySize(stream_t *streamptr)
 {
   size_t size = 0;
   int ncid = streamptr->fileID;
-  if ( streamptr->historyID != UNDEFID )
+  if ( streamptr->historyID != CDI_UNDEFID )
     cdf_inq_attlen(ncid, NC_GLOBAL, "history", &size);
 
   return (int) size;
@@ -4030,7 +4042,7 @@ int cdfInqHistorySize(stream_t *streamptr)
 void cdfInqHistoryString(stream_t *streamptr, char *history)
 {
   int ncid = streamptr->fileID;
-  if ( streamptr->historyID != UNDEFID )
+  if ( streamptr->historyID != CDI_UNDEFID )
     {
       nc_type atttype;
       cdf_inq_atttype(ncid, NC_GLOBAL, "history", &atttype);
diff --git a/src/stream_cdf_o.c b/src/stream_cdf_o.c
index 32aac36c2e578eec2596510fae70a42287f85bef..4d565f1bd9b18b7114511a15fa9d79af988de49d 100644
--- a/src/stream_cdf_o.c
+++ b/src/stream_cdf_o.c
@@ -14,9 +14,6 @@
 #include "zaxis.h"
 
 
-#undef  UNDEFID
-#define UNDEFID  CDI_UNDEFID
-
 #define  POSITIVE_UP    1
 #define  POSITIVE_DOWN  2
 
@@ -92,7 +89,7 @@ void cdfDefTimeValue(stream_t *streamptr, int tsID)
     }
 
   ncvarid = streamptr->basetime.leadtimeid;
-  if ( taxis->type == TAXIS_FORECAST && ncvarid != UNDEFID )
+  if ( taxis->type == TAXIS_FORECAST && ncvarid != CDI_UNDEFID )
     {
       timevalue = taxis->fc_period;
       cdf_put_var1_double(fileID, ncvarid, &index, &timevalue);
@@ -232,7 +229,7 @@ void cdfDefTime(stream_t* streamptr)
   int time_bndsid = -1;
   static const char default_name[] = "time";
 
-  if ( streamptr->basetime.ncvarid != UNDEFID ) return;
+  if ( streamptr->basetime.ncvarid != CDI_UNDEFID ) return;
 
   int fileID = streamptr->fileID;
 
@@ -334,13 +331,13 @@ void cdfDefTimestep(stream_t *streamptr, int tsID)
 static
 void cdfDefComplex(stream_t *streamptr, int gridID, int gridindex)
 {
-  int dimID = UNDEFID;
+  int dimID = CDI_UNDEFID;
   int fileID  = streamptr->fileID;
   ncgrid_t *ncgrid = streamptr->ncgrid;
 
   for ( int index = 0; index < gridindex; ++index )
     {
-      if ( ncgrid[index].xdimID != UNDEFID )
+      if ( ncgrid[index].xdimID != CDI_UNDEFID )
         {
           int gridID0 = ncgrid[index].gridID;
           int gridtype0 = gridInqType(gridID0);
@@ -352,7 +349,7 @@ void cdfDefComplex(stream_t *streamptr, int gridID, int gridindex)
         }
     }
 
-  if ( dimID == UNDEFID )
+  if ( dimID == CDI_UNDEFID )
     {
       static const char axisname[] = "nc2";
       size_t dimlen = 2;
@@ -373,14 +370,14 @@ cdfDefSPorFC(stream_t *streamptr, int gridID, int gridindex,
              char *restrict axisname, int gridRefType)
 {
   int iz = 0;
-  int dimID = UNDEFID;
+  int dimID = CDI_UNDEFID;
   ncgrid_t *ncgrid = streamptr->ncgrid;
 
   size_t dimlen = (size_t)gridInqSize(gridID)/2;
 
   for ( int index = 0; index < gridindex; index++ )
     {
-      if ( ncgrid[index].ydimID != UNDEFID )
+      if ( ncgrid[index].ydimID != CDI_UNDEFID )
         {
           int gridID0 = ncgrid[index].gridID;
           int gridtype0 = gridInqType(gridID0);
@@ -398,7 +395,7 @@ cdfDefSPorFC(stream_t *streamptr, int gridID, int gridindex,
         }
     }
 
-  if ( dimID == UNDEFID )
+  if ( dimID == CDI_UNDEFID )
     {
       int fileID  = streamptr->fileID;
       if ( iz == 0 ) axisname[3] = '\0';
@@ -500,13 +497,13 @@ cdfDefTrajLatLon(stream_t *streamptr, int gridID, int gridindex,
   if ( dimlen != 1 )
     Error("%c size isn't 1 for %s grid!", dimtype, gridNamePtr(gridInqType(gridID)));
 
-  int ncvarid = UNDEFID;
+  int ncvarid = CDI_UNDEFID;
   if ( dimtype == 'X' )
     ncvarid = ncgrid[gridindex].xdimID;
   else
     ncvarid = ncgrid[gridindex].ydimID;
 
-  if ( ncvarid == UNDEFID )
+  if ( ncvarid == CDI_UNDEFID )
     {
       int dimNcID = streamptr->basetime.ncvarid;
       int fileID  = streamptr->fileID;
@@ -546,7 +543,7 @@ int checkDimName(int fileID, size_t dimlen, char *dimname)
 {
   /* check whether the dimenion name is already defined with the same length */
   unsigned iz = 0;
-  int dimid = UNDEFID;
+  int dimid = CDI_UNDEFID;
   char name[CDI_MAX_NAME];
 
   size_t len = strlen(dimname);
@@ -650,18 +647,19 @@ cdfDefAxisCommon(stream_t *streamptr, int gridID, int gridindex, int ndims,
                  const struct cdfDefGridAxisInqs *gridAxisInq, int dimKey, char axisLetter,
                  void (*finishCyclicBounds)(double *pbounds, size_t dimlen, const double *pvals))
 {
-  int dimID = UNDEFID;
-  int ncvarid = UNDEFID, ncbvarid = UNDEFID;
-  int nvdimID = UNDEFID;
+  int dimID = CDI_UNDEFID;
+  int ncvarid = CDI_UNDEFID, ncbvarid = CDI_UNDEFID;
+  int nvdimID = CDI_UNDEFID;
   int fileID  = streamptr->fileID;
   size_t dimlen = (size_t)gridAxisInq->axisSize(gridID);
-  nc_type xtype = (gridInqPrec(gridID) == CDI_DATATYPE_FLT32) ? NC_FLOAT : NC_DOUBLE;
+  nc_type xtype = (nc_type)cdfDefDatatype(gridInqPrec(gridID), streamptr->filetype);
+
   ncgrid_t *ncgrid = streamptr->ncgrid;
 
   for ( int index = 0; index < gridindex; ++index )
     {
       int gridID0 = ncgrid[index].gridID;
-      assert(gridID0 != UNDEFID);
+      assert(gridID0 != CDI_UNDEFID);
       int gridtype0 = gridInqType(gridID0);
       if ( gridtype0 == GRID_GAUSSIAN    ||
            gridtype0 == GRID_LONLAT      ||
@@ -686,7 +684,7 @@ cdfDefAxisCommon(stream_t *streamptr, int gridID, int gridindex, int ndims,
         }
     }
 
-  if ( dimID == UNDEFID )
+  if ( dimID == CDI_UNDEFID )
     {
       const double *pvals = gridAxisInq->axisValsPtr(gridID);
 
@@ -714,7 +712,7 @@ cdfDefAxisCommon(stream_t *streamptr, int gridID, int gridindex, int ndims,
           if ( dimname[0] == 0 ) strcpy(dimname, extendedAxisname);
           dimID = checkDimName(fileID, dimlen, dimname);
 
-          if ( dimID == UNDEFID ) cdf_def_dim(fileID, dimname, dimlen, &dimID);
+          if ( dimID == CDI_UNDEFID ) cdf_def_dim(fileID, dimname, dimlen, &dimID);
         }
 
       bool gen_bounds = false;
@@ -749,7 +747,7 @@ cdfDefAxisCommon(stream_t *streamptr, int gridID, int gridindex, int ndims,
               if ( nc_inq_dimid(fileID, bndsName, &nvdimID) != NC_NOERR )
                 cdf_def_dim(fileID, bndsName, nvertex, &nvdimID);
             }
-          if ( pbounds && nvdimID != UNDEFID )
+          if ( pbounds && nvdimID != CDI_UNDEFID )
             {
               char boundsname[extendedAxisnameLen + 1 + sizeof (bndsName)];
               memcpy(boundsname, axisname, extendedAxisnameLen);
@@ -764,8 +762,8 @@ cdfDefAxisCommon(stream_t *streamptr, int gridID, int gridindex, int ndims,
       cdf_enddef(fileID);
       streamptr->ncmode = 2;
 
-      if ( ncvarid  != UNDEFID ) cdf_put_var_double(fileID, ncvarid, pvals);
-      if ( ncbvarid != UNDEFID ) cdf_put_var_double(fileID, ncbvarid, pbounds);
+      if ( ncvarid  != CDI_UNDEFID ) cdf_put_var_double(fileID, ncvarid, pvals);
+      if ( ncbvarid != CDI_UNDEFID ) cdf_put_var_double(fileID, ncbvarid, pbounds);
       if ( gen_bounds ) Free(pbounds);
 
       if ( ndims == 0 )
@@ -827,11 +825,11 @@ void cdfGridCompress(int fileID, int ncvarid, int gridsize, int filetype, int co
 static
 void cdfDefCurvilinear(stream_t *streamptr, int gridID, int gridindex)
 {
-  int xdimID = UNDEFID;
-  int ydimID = UNDEFID;
-  int ncxvarid = UNDEFID, ncyvarid = UNDEFID;
-  int ncbxvarid = UNDEFID, ncbyvarid = UNDEFID, ncavarid = UNDEFID;
-  nc_type xtype = (gridInqPrec(gridID) == CDI_DATATYPE_FLT32) ? NC_FLOAT : NC_DOUBLE;
+  int xdimID = CDI_UNDEFID;
+  int ydimID = CDI_UNDEFID;
+  int ncxvarid = CDI_UNDEFID, ncyvarid = CDI_UNDEFID;
+  int ncbxvarid = CDI_UNDEFID, ncbyvarid = CDI_UNDEFID, ncavarid = CDI_UNDEFID;
+  nc_type xtype = (nc_type)cdfDefDatatype(gridInqPrec(gridID), streamptr->filetype);
   ncgrid_t *ncgrid = streamptr->ncgrid;
 
   int fileID  = streamptr->fileID;
@@ -842,7 +840,7 @@ void cdfDefCurvilinear(stream_t *streamptr, int gridID, int gridindex)
 
   for ( int index = 0; index < gridindex; index++ )
     {
-      if ( ncgrid[index].xdimID != UNDEFID )
+      if ( ncgrid[index].xdimID != CDI_UNDEFID )
         {
           int gridID0 = ncgrid[index].gridID;
           int gridtype0 = gridInqType(gridID0);
@@ -865,7 +863,7 @@ void cdfDefCurvilinear(stream_t *streamptr, int gridID, int gridindex)
         }
     }
 
-  if ( xdimID == UNDEFID || ydimID == UNDEFID )
+  if ( xdimID == CDI_UNDEFID || ydimID == CDI_UNDEFID )
     {
       if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
       {
@@ -874,7 +872,7 @@ void cdfDefCurvilinear(stream_t *streamptr, int gridID, int gridindex)
         cdiGridInqKeyStr(gridID, CDI_KEY_XDIMNAME, CDI_MAX_NAME, xdimname);
         if ( xdimname[0] == 0 ) { xdimname[0] = 'x'; xdimname[1] = 0; }
         xdimID = checkDimName(fileID, xdimlen, xdimname);
-        if ( xdimID == UNDEFID ) cdf_def_dim(fileID, xdimname, xdimlen, &xdimID);
+        if ( xdimID == CDI_UNDEFID ) cdf_def_dim(fileID, xdimname, xdimlen, &xdimID);
       }
       {
         char ydimname[CDI_MAX_NAME+3];
@@ -882,10 +880,10 @@ void cdfDefCurvilinear(stream_t *streamptr, int gridID, int gridindex)
         cdiGridInqKeyStr(gridID, CDI_KEY_YDIMNAME, CDI_MAX_NAME, ydimname);
         if ( ydimname[0] == 0 ) { ydimname[0] = 'y'; ydimname[1] = 0; }
         ydimID = checkDimName(fileID, ydimlen, ydimname);
-        if ( ydimID == UNDEFID ) cdf_def_dim(fileID, ydimname, ydimlen, &ydimID);
+        if ( ydimID == CDI_UNDEFID ) cdf_def_dim(fileID, ydimname, ydimlen, &ydimID);
       }
 
-      int nvdimID = UNDEFID;
+      int nvdimID = CDI_UNDEFID;
       int dimIDs[3];
       if ( gridInqXboundsPtr(gridID) || gridInqYboundsPtr(gridID) )
         {
@@ -894,7 +892,7 @@ void cdfDefCurvilinear(stream_t *streamptr, int gridID, int gridindex)
           if ( vdimname[0] == 0 ) strcpy(vdimname, "nv4");
           size_t nvertex = 4;
           nvdimID = checkDimName(fileID, nvertex, vdimname);
-          if ( nvdimID == UNDEFID ) cdf_def_dim(fileID, vdimname, nvertex, &nvdimID);
+          if ( nvdimID == CDI_UNDEFID ) cdf_def_dim(fileID, vdimname, nvertex, &nvdimID);
         }
 
       dimIDs[0] = ydimID;
@@ -915,7 +913,7 @@ void cdfDefCurvilinear(stream_t *streamptr, int gridID, int gridindex)
           /* attribute for Panoply */
           cdf_put_att_text(fileID, ncxvarid, "_CoordinateAxisType", 3, "Lon");
 
-          if ( gridInqXboundsPtr(gridID) && nvdimID != UNDEFID )
+          if ( gridInqXboundsPtr(gridID) && nvdimID != CDI_UNDEFID )
             {
               size_t xaxisnameLen = strlen(xaxisname);
               xaxisname[xaxisnameLen] = '_';
@@ -941,7 +939,7 @@ void cdfDefCurvilinear(stream_t *streamptr, int gridID, int gridindex)
           /* attribute for Panoply */
           cdf_put_att_text(fileID, ncyvarid, "_CoordinateAxisType", 3, "Lat");
 
-          if ( gridInqYboundsPtr(gridID) && nvdimID != UNDEFID )
+          if ( gridInqYboundsPtr(gridID) && nvdimID != CDI_UNDEFID )
             {
               size_t yaxisnameLen = strlen(yaxisname);
               yaxisname[yaxisnameLen] = '_';
@@ -970,11 +968,11 @@ void cdfDefCurvilinear(stream_t *streamptr, int gridID, int gridindex)
       cdf_enddef(fileID);
       streamptr->ncmode = 2;
 
-      if ( ncxvarid  != UNDEFID ) cdf_put_var_double(fileID, ncxvarid,  gridInqXvalsPtr(gridID));
-      if ( ncbxvarid != UNDEFID ) cdf_put_var_double(fileID, ncbxvarid, gridInqXboundsPtr(gridID));
-      if ( ncyvarid  != UNDEFID ) cdf_put_var_double(fileID, ncyvarid,  gridInqYvalsPtr(gridID));
-      if ( ncbyvarid != UNDEFID ) cdf_put_var_double(fileID, ncbyvarid, gridInqYboundsPtr(gridID));
-      if ( ncavarid  != UNDEFID ) cdf_put_var_double(fileID, ncavarid,  gridInqAreaPtr(gridID));
+      if ( ncxvarid  != CDI_UNDEFID ) cdf_put_var_double(fileID, ncxvarid,  gridInqXvalsPtr(gridID));
+      if ( ncbxvarid != CDI_UNDEFID ) cdf_put_var_double(fileID, ncbxvarid, gridInqXboundsPtr(gridID));
+      if ( ncyvarid  != CDI_UNDEFID ) cdf_put_var_double(fileID, ncyvarid,  gridInqYvalsPtr(gridID));
+      if ( ncbyvarid != CDI_UNDEFID ) cdf_put_var_double(fileID, ncbyvarid, gridInqYboundsPtr(gridID));
+      if ( ncavarid  != CDI_UNDEFID ) cdf_put_var_double(fileID, ncavarid,  gridInqAreaPtr(gridID));
     }
 
   ncgrid[gridindex].gridID = gridID;
@@ -989,14 +987,14 @@ static
 void cdfDefRgrid(stream_t *streamptr, int gridID, int gridindex)
 {
   ncgrid_t *ncgrid = streamptr->ncgrid;
-  int dimID = UNDEFID;
+  int dimID = CDI_UNDEFID;
 
   size_t dimlen = (size_t)gridInqSize(gridID);
 
   int iz = 0;
   for ( int index = 0; index < gridindex; index++ )
     {
-      if ( ncgrid[index].xdimID != UNDEFID )
+      if ( ncgrid[index].xdimID != CDI_UNDEFID )
         {
           int gridID0 = ncgrid[index].gridID;
           int gridtype0 = gridInqType(gridID0);
@@ -1014,7 +1012,7 @@ void cdfDefRgrid(stream_t *streamptr, int gridID, int gridindex)
         }
     }
 
-  if ( dimID == UNDEFID )
+  if ( dimID == CDI_UNDEFID )
     {
       int fileID  = streamptr->fileID;
       static bool lwarn = true;
@@ -1046,14 +1044,14 @@ void cdfDefGdim(stream_t *streamptr, int gridID, int gridindex)
 {
   ncgrid_t *ncgrid = streamptr->ncgrid;
   int iz = 0;
-  int dimID = UNDEFID;
+  int dimID = CDI_UNDEFID;
 
   size_t dimlen = (size_t)gridInqSize(gridID);
 
   if ( gridInqYsize(gridID) == 0 )
     for ( int index = 0; index < gridindex; index++ )
       {
-        if ( ncgrid[index].xdimID != UNDEFID )
+        if ( ncgrid[index].xdimID != CDI_UNDEFID )
           {
             int gridID0 = ncgrid[index].gridID;
             int gridtype0 = gridInqType(gridID0);
@@ -1074,7 +1072,7 @@ void cdfDefGdim(stream_t *streamptr, int gridID, int gridindex)
   if ( gridInqXsize(gridID) == 0 )
     for ( int index = 0; index < gridindex; index++ )
       {
-        if ( ncgrid[index].ydimID != UNDEFID )
+        if ( ncgrid[index].ydimID != CDI_UNDEFID )
           {
             int gridID0 = ncgrid[index].gridID;
             int gridtype0 = gridInqType(gridID0);
@@ -1092,7 +1090,7 @@ void cdfDefGdim(stream_t *streamptr, int gridID, int gridindex)
           }
       }
 
-  if ( dimID == UNDEFID )
+  if ( dimID == CDI_UNDEFID )
     {
       int fileID  = streamptr->fileID;
       char dimname[CDI_MAX_NAME];
@@ -1102,7 +1100,7 @@ void cdfDefGdim(stream_t *streamptr, int gridID, int gridindex)
 
       if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
 
-      if ( dimID == UNDEFID ) cdf_def_dim(fileID, dimname, dimlen, &dimID);
+      if ( dimID == CDI_UNDEFID ) cdf_def_dim(fileID, dimname, dimlen, &dimID);
 
       cdf_enddef(fileID);
       streamptr->ncmode = 2;
@@ -1168,11 +1166,11 @@ void cdfDefZaxisUUID(stream_t *streamptr, int zaxisID)
 static
 void cdfDefUnstructured(stream_t *streamptr, int gridID, int gridindex)
 {
-  int dimID = UNDEFID;
-  int ncxvarid = UNDEFID, ncyvarid = UNDEFID;
-  int ncbxvarid = UNDEFID, ncbyvarid = UNDEFID, ncavarid = UNDEFID;
-  int nvdimID = UNDEFID;
-  nc_type xtype = (gridInqPrec(gridID) == CDI_DATATYPE_FLT32) ? NC_FLOAT : NC_DOUBLE;
+  int dimID = CDI_UNDEFID;
+  int ncxvarid = CDI_UNDEFID, ncyvarid = CDI_UNDEFID;
+  int ncbxvarid = CDI_UNDEFID, ncbyvarid = CDI_UNDEFID, ncavarid = CDI_UNDEFID;
+  int nvdimID = CDI_UNDEFID;
+  nc_type xtype = (nc_type)cdfDefDatatype(gridInqPrec(gridID), streamptr->filetype);
   ncgrid_t *ncgrid = streamptr->ncgrid;
 
   int fileID  = streamptr->fileID;
@@ -1181,7 +1179,7 @@ void cdfDefUnstructured(stream_t *streamptr, int gridID, int gridindex)
 
   for ( int index = 0; index < gridindex; index++ )
     {
-      if ( ncgrid[index].xdimID != UNDEFID )
+      if ( ncgrid[index].xdimID != CDI_UNDEFID )
         {
           int gridID0 = ncgrid[index].gridID;
           int gridtype0 = gridInqType(gridID0);
@@ -1205,7 +1203,7 @@ void cdfDefUnstructured(stream_t *streamptr, int gridID, int gridindex)
         }
     }
 
-  if ( dimID == UNDEFID )
+  if ( dimID == CDI_UNDEFID )
     {
       if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
       {
@@ -1214,7 +1212,7 @@ void cdfDefUnstructured(stream_t *streamptr, int gridID, int gridindex)
         cdiGridInqKeyStr(gridID, CDI_KEY_XDIMNAME, CDI_MAX_NAME, xdimname);
         if ( xdimname[0] == 0 ) strcpy(xdimname, "ncells");
         dimID = checkDimName(fileID, dimlen, xdimname);
-        if ( dimID == UNDEFID ) cdf_def_dim(fileID, xdimname, dimlen, &dimID);
+        if ( dimID == CDI_UNDEFID ) cdf_def_dim(fileID, xdimname, dimlen, &dimID);
       }
 
       size_t nvertex = (size_t)gridInqNvertex(gridID);
@@ -1225,7 +1223,7 @@ void cdfDefUnstructured(stream_t *streamptr, int gridID, int gridindex)
           cdiGridInqKeyStr(gridID, CDI_KEY_VDIMNAME, CDI_MAX_NAME, vdimname);
           if ( vdimname[0] == 0 ) strcpy(vdimname, "vertices");
           nvdimID = checkDimName(fileID, nvertex, vdimname);
-          if ( nvdimID == UNDEFID ) cdf_def_dim(fileID, vdimname, nvertex, &nvdimID);
+          if ( nvdimID == CDI_UNDEFID ) cdf_def_dim(fileID, vdimname, nvertex, &nvdimID);
         }
 
       cdfDefGridReference(streamptr, gridID);
@@ -1242,7 +1240,7 @@ void cdfDefUnstructured(stream_t *streamptr, int gridID, int gridindex)
 
           cdfPutGridStdAtts(fileID, ncxvarid, gridID, 'X', &gridInqsX);
 
-          if ( gridInqXboundsPtr(gridID) && nvdimID != UNDEFID )
+          if ( gridInqXboundsPtr(gridID) && nvdimID != CDI_UNDEFID )
             {
               int dimIDs[2] = { dimID, nvdimID };
               size_t xaxisnameLen = strlen(xaxisname);
@@ -1265,7 +1263,7 @@ void cdfDefUnstructured(stream_t *streamptr, int gridID, int gridindex)
 
           cdfPutGridStdAtts(fileID, ncyvarid, gridID, 'Y', &gridInqsY);
 
-          if ( gridInqYboundsPtr(gridID) && nvdimID != UNDEFID )
+          if ( gridInqYboundsPtr(gridID) && nvdimID != CDI_UNDEFID )
             {
               int dimIDs[2] = { dimID, nvdimID };
               size_t yaxisnameLen = strlen(yaxisname);
@@ -1295,11 +1293,11 @@ void cdfDefUnstructured(stream_t *streamptr, int gridID, int gridindex)
       cdf_enddef(fileID);
       streamptr->ncmode = 2;
 
-      if ( ncxvarid  != UNDEFID ) cdf_put_var_double(fileID, ncxvarid,  gridInqXvalsPtr(gridID));
-      if ( ncbxvarid != UNDEFID ) cdf_put_var_double(fileID, ncbxvarid, gridInqXboundsPtr(gridID));
-      if ( ncyvarid  != UNDEFID ) cdf_put_var_double(fileID, ncyvarid,  gridInqYvalsPtr(gridID));
-      if ( ncbyvarid != UNDEFID ) cdf_put_var_double(fileID, ncbyvarid, gridInqYboundsPtr(gridID));
-      if ( ncavarid  != UNDEFID ) cdf_put_var_double(fileID, ncavarid,  gridInqAreaPtr(gridID));
+      if ( ncxvarid  != CDI_UNDEFID ) cdf_put_var_double(fileID, ncxvarid,  gridInqXvalsPtr(gridID));
+      if ( ncbxvarid != CDI_UNDEFID ) cdf_put_var_double(fileID, ncbxvarid, gridInqXboundsPtr(gridID));
+      if ( ncyvarid  != CDI_UNDEFID ) cdf_put_var_double(fileID, ncyvarid,  gridInqYvalsPtr(gridID));
+      if ( ncbyvarid != CDI_UNDEFID ) cdf_put_var_double(fileID, ncbyvarid, gridInqYboundsPtr(gridID));
+      if ( ncavarid  != CDI_UNDEFID ) cdf_put_var_double(fileID, ncavarid,  gridInqAreaPtr(gridID));
     }
 
   ncgrid[gridindex].gridID = gridID;
@@ -1590,7 +1588,7 @@ void cdf_def_zaxis_hybrid_echam(stream_t *streamptr, int type, int *ncvaridp, in
 
   cdf_def_vct_echam(streamptr, zaxisID);
 
-  if ( *dimID == UNDEFID )
+  if ( *dimID == CDI_UNDEFID )
     {
       if ( type == ZAXIS_HYBRID )
         streamptr->zaxisID[zaxisindex] = streamptr->vct.mlevID;
@@ -1611,7 +1609,7 @@ void cdf_def_zaxis_hybrid_cf(stream_t *streamptr, int type, int *ncvaridp, int z
 
   char p0name[CDI_MAX_NAME]; p0name[0] = 0;
   double p0value = 1;
-  int p0varid = UNDEFID;
+  int p0varid = CDI_UNDEFID;
   int p0status = cdiZaxisInqKeyFlt(zaxisID, CDI_KEY_P0VALUE, &p0value);
   if ( p0status == 0 )
     {
@@ -1667,8 +1665,8 @@ void cdf_def_zaxis_hybrid_cf(stream_t *streamptr, int type, int *ncvaridp, int z
     len = (size_t)(sprintf(txt, "%s%s", "ap: ap b: b ps: ", psname));
   cdf_put_att_text(fileID, ncvarid, "formula_terms", len, txt);
 
-  int ncbvarid = UNDEFID;
-  int nvdimID = UNDEFID;
+  int ncbvarid = CDI_UNDEFID;
+  int nvdimID = CDI_UNDEFID;
 
   double lbounds[dimlen], ubounds[dimlen], levels[dimlen];
 
@@ -1695,7 +1693,7 @@ void cdf_def_zaxis_hybrid_cf(stream_t *streamptr, int type, int *ncvaridp, int z
       if ( nc_inq_dimid(fileID, bndsName, &nvdimID) != NC_NOERR )
         cdf_def_dim(fileID, bndsName, nvertex, &nvdimID);
 
-      if ( nvdimID != UNDEFID )
+      if ( nvdimID != CDI_UNDEFID )
         {
           size_t axisnameLen = strlen(axisname);
           axisname[axisnameLen] = '_';
@@ -1729,9 +1727,9 @@ void cdf_def_zaxis_hybrid_cf(stream_t *streamptr, int type, int *ncvaridp, int z
 
   cdf_put_var_double(fileID, ncvarid, levels);
 
-  if ( p0varid != UNDEFID ) cdf_put_var_double(fileID, p0varid, &p0value);
+  if ( p0varid != CDI_UNDEFID ) cdf_put_var_double(fileID, p0varid, &p0value);
 
-  if ( ncbvarid != UNDEFID )
+  if ( ncbvarid != CDI_UNDEFID )
     {
       double zbounds[2*dimlen];
       for ( size_t i = 0; i < dimlen; ++i )
@@ -1744,7 +1742,7 @@ void cdf_def_zaxis_hybrid_cf(stream_t *streamptr, int type, int *ncvaridp, int z
 
   cdf_def_vct_cf(streamptr, zaxisID, *dimID, nvdimID, p0status, p0value);
 
-  if ( *dimID == UNDEFID )
+  if ( *dimID == CDI_UNDEFID )
     {
       if ( type == ZAXIS_HYBRID )
         streamptr->zaxisID[zaxisindex] = streamptr->vct.mlevID;
@@ -1767,10 +1765,10 @@ void cdfDefZaxis(stream_t *streamptr, int zaxisID)
 {
   /*  char zaxisname0[CDI_MAX_NAME]; */
   char axisname[CDI_MAX_NAME];
-  int dimID = UNDEFID;
+  int dimID = CDI_UNDEFID;
   int dimIDs[2];
-  int ncvarid = UNDEFID, ncbvarid = UNDEFID;
-  int nvdimID = UNDEFID;
+  int ncvarid = CDI_UNDEFID, ncbvarid = CDI_UNDEFID;
+  int nvdimID = CDI_UNDEFID;
   int xtype = NC_DOUBLE;
 
   if ( zaxisInqPrec(zaxisID) == CDI_DATATYPE_FLT32 ) xtype = NC_FLOAT;
@@ -1820,7 +1818,7 @@ void cdfDefZaxis(stream_t *streamptr, int zaxisID)
 
   zaxisInqName(zaxisID, axisname);
 
-  if ( dimID == UNDEFID )
+  if ( dimID == CDI_UNDEFID )
     {
       checkZaxisName(axisname, fileID, vlistID, zaxisID, nzaxis);
 
@@ -1846,7 +1844,7 @@ void cdfDefZaxis(stream_t *streamptr, int zaxisID)
 
           if ( streamptr->ncmode == 2 ) cdf_redef(fileID);
 
-          if ( ndims && dimID == UNDEFID ) cdf_def_dim(fileID, dimname, dimlen, &dimID);
+          if ( ndims && dimID == CDI_UNDEFID ) cdf_def_dim(fileID, dimname, dimlen, &dimID);
 
           if ( zaxisInqLevels(zaxisID, NULL) )
             {
@@ -1876,7 +1874,7 @@ void cdfDefZaxis(stream_t *streamptr, int zaxisID)
                   if ( nc_inq_dimid(fileID, bndsName, &nvdimID) != NC_NOERR )
                     cdf_def_dim(fileID, bndsName, nvertex, &nvdimID);
 
-                  if ( nvdimID != UNDEFID )
+                  if ( nvdimID != CDI_UNDEFID )
                     {
                       size_t axisnameLen = strlen(axisname);
                       axisname[axisnameLen] = '_';
@@ -1898,7 +1896,7 @@ void cdfDefZaxis(stream_t *streamptr, int zaxisID)
             {
               cdf_put_var_double(fileID, ncvarid, zaxisInqLevelsPtr(zaxisID));
 
-              if ( ncbvarid != UNDEFID )
+              if ( ncbvarid != CDI_UNDEFID )
                 {
                   double lbounds[dimlen], ubounds[dimlen], zbounds[2*dimlen];
                   zaxisInqLbounds(zaxisID, lbounds);
@@ -1917,7 +1915,7 @@ void cdfDefZaxis(stream_t *streamptr, int zaxisID)
         }
     }
 
-  if ( dimID != UNDEFID )
+  if ( dimID != CDI_UNDEFID )
     streamptr->zaxisID[zaxisindex] = dimID;
 }
 
@@ -1952,7 +1950,7 @@ void cdf_def_mapping(stream_t *streamptr, int gridID)
 static
 void cdfDefGrid(stream_t *streamptr, int gridID, int gridindex)
 {
-  if ( streamptr->ncgrid[gridindex].xdimID != UNDEFID ) return;
+  if ( streamptr->ncgrid[gridindex].xdimID != CDI_UNDEFID ) return;
 
   int gridtype = gridInqType(gridID);
   int size     = gridInqSize(gridID);
@@ -2053,19 +2051,19 @@ void cdfDefVars(stream_t *streamptr)
 {
   int index = 0;
   int vlistID = streamptr->vlistID;
-  if ( vlistID == UNDEFID )
+  if ( vlistID == CDI_UNDEFID )
     Error("Internal problem! vlist undefined for streamptr %p", streamptr);
 
   int ngrids = vlistNgrids(vlistID);
-  streamptr->ncgrid = (ncgrid_t*) Malloc(2*ngrids*sizeof(ncgrid_t));
+  if ( 2*ngrids > MAX_GRIDS_PS ) Error("Internaal problem! Too many grids per stream (max=%d)\n", MAX_GRIDS_PS);
   for ( index = 0; index < 2*ngrids; ++index )
     {
-      streamptr->ncgrid[index].gridID = UNDEFID;
-      streamptr->ncgrid[index].xdimID = UNDEFID;
-      streamptr->ncgrid[index].ydimID = UNDEFID;
-      streamptr->ncgrid[index].xvarID = UNDEFID;
-      streamptr->ncgrid[index].yvarID = UNDEFID;
-      streamptr->ncgrid[index].avarID = UNDEFID;
+      streamptr->ncgrid[index].gridID = CDI_UNDEFID;
+      streamptr->ncgrid[index].xdimID = CDI_UNDEFID;
+      streamptr->ncgrid[index].ydimID = CDI_UNDEFID;
+      streamptr->ncgrid[index].xvarID = CDI_UNDEFID;
+      streamptr->ncgrid[index].yvarID = CDI_UNDEFID;
+      streamptr->ncgrid[index].avarID = CDI_UNDEFID;
     }
 
   for ( index = 0; index < ngrids; ++index )
@@ -2085,7 +2083,7 @@ void cdfDefVars(stream_t *streamptr)
   for ( int index = 0; index < nzaxis; ++index )
     {
       int zaxisID = vlistZaxis(vlistID, index);
-      if ( streamptr->zaxisID[index] == UNDEFID ) cdfDefZaxis(streamptr, zaxisID);
+      if ( streamptr->zaxisID[index] == CDI_UNDEFID ) cdfDefZaxis(streamptr, zaxisID);
     }
 }
 
diff --git a/src/stream_cgribex.c b/src/stream_cgribex.c
index f96c1b96f6fca324004c882293ffc3c0d5881ff3..02dcb07d776bf496274fbe2444ffdf9c089c277d 100644
--- a/src/stream_cgribex.c
+++ b/src/stream_cgribex.c
@@ -514,11 +514,10 @@ compvar_t cgribexVarSet(int param, int level1, int level2, int leveltype, int tr
 static inline int
 cgribexVarCompare(compvar_t compVar, record_t record, int flag)
 {
-  int tstepDiff = (!((flag == 0) & (((compVar.tsteptype == TSTEP_INSTANT)
-                                     & (record.tsteptype == TSTEP_INSTANT3))
-                                    |((compVar.tsteptype == TSTEP_INSTANT3)
-                                      & (record.tsteptype == TSTEP_INSTANT)))))
-    & (compVar.tsteptype != record.tsteptype);
+  bool vinst = compVar.tsteptype == TSTEP_INSTANT || compVar.tsteptype == TSTEP_INSTANT2 || compVar.tsteptype == TSTEP_INSTANT3;
+  bool rinst = record.tsteptype == TSTEP_INSTANT || record.tsteptype == TSTEP_INSTANT2 || record.tsteptype == TSTEP_INSTANT3;
+  int tstepDiff = (!((flag == 0) & (vinst && rinst)))
+                & (compVar.tsteptype != record.tsteptype);
   int rstatus = (compVar.param != record.param)
     |           (compVar.level1 != record.ilevel)
     |           (compVar.level2 != record.ilevel2)
@@ -663,8 +662,8 @@ int cgribexScanTimestep1(stream_t * streamptr)
 
       if ( ISEC1_LevelType == 100 ) ISEC1_Level1 *= 100;
       if ( ISEC1_LevelType ==  99 ) ISEC1_LevelType = 100;
-      level1   = ISEC1_Level1;
-      level2   = ISEC1_Level2;
+      level1 = ISEC1_Level1;
+      level2 = ISEC1_Level2;
 
       gribDateTime(isec1, &vdate, &vtime);
 
@@ -684,8 +683,8 @@ int cgribexScanTimestep1(stream_t * streamptr)
 	}
       else
 	{
-	  datetime.date  = vdate;
-	  datetime.time  = vtime;
+	  datetime.date = vdate;
+	  datetime.time = vtime;
 	  compvar_t compVar = cgribexVarSet(param, level1, level2, ISEC1_LevelType, ISEC1_TimeRange);
 	  for ( recID = 0; recID < nrecs; recID++ )
 	    {
diff --git a/src/stream_ext.c b/src/stream_ext.c
index b8db98e0336ef82eae72776edd318fe7ae8a88a0..8035949258edcb87975778193e2f4b2ac9b11a46 100644
--- a/src/stream_ext.c
+++ b/src/stream_ext.c
@@ -20,13 +20,8 @@
 #include "exse.h"
 
 
-#undef  UNDEFID
-#define UNDEFID  CDI_UNDEFID
-
-
 #if defined (HAVE_LIBEXTRA)
 
-
 typedef struct {
   int param;
   int level;
@@ -85,7 +80,7 @@ int extInqRecord(stream_t *streamptr, int *varID, int *levelID)
 
   *varID = vlistInqVarID(vlistID, icode);
 
-  if ( *varID == UNDEFID ) Error("Code %d undefined", icode);
+  if ( *varID == CDI_UNDEFID ) Error("Code %d undefined", icode);
 
   zaxisID = vlistInqVarZaxis(vlistID, *varID);
 
@@ -630,8 +625,8 @@ int extInqTimestep(stream_t *streamptr, int tsID)
   if ( CDI_Debug )
     Message("tsID = %d rtsteps = %d", tsID, streamptr->rtsteps);
 
-  long ntsteps = UNDEFID;
-  while ( ( tsID + 1 ) > streamptr->rtsteps && ntsteps == UNDEFID )
+  long ntsteps = CDI_UNDEFID;
+  while ( ( tsID + 1 ) > streamptr->rtsteps && ntsteps == CDI_UNDEFID )
     ntsteps = extScanTimestep(streamptr);
 
   int nrecs = 0;
diff --git a/src/stream_ieg.c b/src/stream_ieg.c
index af098ca29c6993cb650f253aef17723b48f9975c..df6a6df9a4da1aeb360315ff21b69380afca6dd3 100644
--- a/src/stream_ieg.c
+++ b/src/stream_ieg.c
@@ -24,13 +24,8 @@
 #include "exse.h"
 
 
-#undef  UNDEFID
-#define UNDEFID  CDI_UNDEFID
-
-
 #if defined (HAVE_LIBIEG)
 
-
 typedef struct {
   int param;
   int level;
@@ -82,7 +77,7 @@ int iegInqRecord(stream_t *streamptr, int *varID, int *levelID)
 
   *varID = vlistInqVarID(vlistID, icode);
 
-  if ( *varID == UNDEFID ) Error("Code %d undefined", icode);
+  if ( *varID == CDI_UNDEFID ) Error("Code %d undefined", icode);
 
   zaxisID = vlistInqVarZaxis(vlistID, *varID);
 
@@ -1156,8 +1151,8 @@ int iegInqTimestep(stream_t *streamptr, int tsID)
   if ( CDI_Debug )
     Message("tsID = %d rtsteps = %d", tsID, streamptr->rtsteps);
 
-  long ntsteps = UNDEFID;
-  while ( ( tsID + 1 ) > streamptr->rtsteps && ntsteps == UNDEFID )
+  long ntsteps = CDI_UNDEFID;
+  while ( ( tsID + 1 ) > streamptr->rtsteps && ntsteps == CDI_UNDEFID )
     ntsteps = iegScanTimestep(streamptr);
 
   int nrecs = 0;
diff --git a/src/stream_srv.c b/src/stream_srv.c
index dff4171ac60e35e551ed29c1226df7a16e48eba9..3aa74f006c4dccb8a7e891162a9e1abf7377dbe1 100644
--- a/src/stream_srv.c
+++ b/src/stream_srv.c
@@ -21,13 +21,8 @@
 #include "exse.h"
 
 
-#undef  UNDEFID
-#define UNDEFID  CDI_UNDEFID
-
-
 #if defined (HAVE_LIBSERVICE)
 
-
 typedef struct {
   int param;
   int level;
@@ -79,7 +74,7 @@ int srvInqRecord(stream_t *streamptr, int *varID, int *levelID)
 
   *varID = vlistInqVarID(vlistID, icode);
 
-  if ( *varID == UNDEFID ) Error("Code %d undefined", icode);
+  if ( *varID == CDI_UNDEFID ) Error("Code %d undefined", icode);
 
   zaxisID = vlistInqVarZaxis(vlistID, *varID);
 
@@ -626,12 +621,12 @@ int srvInqTimestep(stream_t *streamptr, int tsID)
   if ( CDI_Debug )
     Message("tsID = %d rtsteps = %d", tsID, streamptr->rtsteps);
 
-  long ntsteps = UNDEFID;
-  while ( ( tsID + 1 ) > streamptr->rtsteps && ntsteps == UNDEFID )
+  long ntsteps = CDI_UNDEFID;
+  while ( ( tsID + 1 ) > streamptr->rtsteps && ntsteps == CDI_UNDEFID )
     ntsteps = srvScanTimestep(streamptr);
 
   int nrecs = 0;
-  if ( !(tsID >= streamptr->ntsteps && streamptr->ntsteps != UNDEFID) )
+  if ( !(tsID >= streamptr->ntsteps && streamptr->ntsteps != CDI_UNDEFID) )
     {
       streamptr->curTsID = tsID;
       nrecs = streamptr->tsteps[tsID].nrecs;
diff --git a/src/table.c b/src/table.c
index a3eb9913e6fcf977f00625cf4fa477511e87bd2a..387afc7288350f84448b4ff4607fa47f830398de 100644
--- a/src/table.c
+++ b/src/table.c
@@ -10,8 +10,6 @@
 #include "cdi.h"
 #include "cdi_int.h"
 
-#undef  UNDEFID
-#define UNDEFID -1
 
 /*int TableDefine = 0; */ /* Define new table also if the entry already exist */
                           /* This is needed for createtable */
@@ -96,8 +94,8 @@ static void parTableInitEntry(int tableID)
   parTable[tableID].used    = 0;
   parTable[tableID].pars    = NULL;
   parTable[tableID].npars   = 0;
-  parTable[tableID].modelID = UNDEFID;
-  parTable[tableID].number  = UNDEFID;
+  parTable[tableID].modelID = CDI_UNDEFID;
+  parTable[tableID].number  = CDI_UNDEFID;
   parTable[tableID].name    = NULL;
 }
 
@@ -325,7 +323,7 @@ int tableRead(const char *tablefile)
   int lnr = 0;
   int id;
   char name[256], longname[256], units[256];
-  int tableID = UNDEFID;
+  int tableID = CDI_UNDEFID;
   int err;
   char *tablename;
   FILE *tablefp;
@@ -379,7 +377,7 @@ int tableRead(const char *tablefile)
 
 static int tableFromEnv(int modelID, int tablenum)
 {
-  int tableID = UNDEFID;
+  int tableID = CDI_UNDEFID;
   char tablename[256] = {'\0'};
   int tablenamefound = 0;
 
@@ -397,7 +395,7 @@ static int tableFromEnv(int modelID, int tablenum)
   else
     {
       int instID = modelInqInstitut(modelID);
-      if ( instID != UNDEFID )
+      if ( instID != CDI_UNDEFID )
 	{
           const char *instName;
 	  if ( (instName = institutInqNamePtr(instID)) )
@@ -434,7 +432,7 @@ static int tableFromEnv(int modelID, int tablenum)
       /* if (tablefile) printf("tableFile = %s\n", tablefile); */
 
       tableID = tableRead(tablefile);
-      if ( tableID != UNDEFID )
+      if ( tableID != CDI_UNDEFID )
 	{
 	  tableDefModelID(tableID, modelID);
 	  tableDefNum(tableID, tablenum);
@@ -449,8 +447,8 @@ static int tableFromEnv(int modelID, int tablenum)
 
 int tableInq(int modelID, int tablenum, const char *tablename)
 {
-  int tableID = UNDEFID;
-  int modelID2 = UNDEFID;
+  int tableID = CDI_UNDEFID;
+  int modelID2 = CDI_UNDEFID;
   char tablefile[256] = {'\0'};
 
   if ( ! ParTableInit ) parTableInit();
@@ -472,7 +470,7 @@ int tableInq(int modelID, int tablenum, const char *tablename)
 	      if ( memcmp(parTable[tableID].name, tablename, len) == 0 ) break;
 	    }
 	}
-      if ( tableID == MAX_TABLE ) tableID = UNDEFID;
+      if ( tableID == MAX_TABLE ) tableID = CDI_UNDEFID;
       if ( CDI_Debug )
 	Message("tableID = %d tablename = %s", tableID, tablename);
     }
@@ -487,11 +485,11 @@ int tableInq(int modelID, int tablenum, const char *tablename)
 	    }
 	}
 
-      if ( tableID == MAX_TABLE ) tableID = UNDEFID;
+      if ( tableID == MAX_TABLE ) tableID = CDI_UNDEFID;
 
-      if ( tableID == UNDEFID )
+      if ( tableID == CDI_UNDEFID )
 	{
-	  if ( modelID != UNDEFID )
+	  if ( modelID != CDI_UNDEFID )
 	    {
               const char *modelName;
 	      if ( (modelName = modelInqNamePtr(modelID)) )
@@ -503,7 +501,7 @@ int tableInq(int modelID, int tablenum, const char *tablename)
 		  modelID2 = modelInq(-1, 0, tablefile);
 		}
 	    }
-	  if ( modelID2 != UNDEFID )
+	  if ( modelID2 != CDI_UNDEFID )
 	    for ( tableID = 0; tableID < MAX_TABLE; tableID++ )
 	      {
 		if ( parTable[tableID].used )
@@ -514,9 +512,9 @@ int tableInq(int modelID, int tablenum, const char *tablename)
 	      }
 	}
 
-      if ( tableID == MAX_TABLE ) tableID = UNDEFID;
+      if ( tableID == MAX_TABLE ) tableID = CDI_UNDEFID;
 
-      if ( tableID == UNDEFID && modelID != UNDEFID )
+      if ( tableID == CDI_UNDEFID && modelID != CDI_UNDEFID )
 	tableID = tableFromEnv(modelID, tablenum);
 
       if ( CDI_Debug )
@@ -529,14 +527,14 @@ int tableInq(int modelID, int tablenum, const char *tablename)
 
 int tableDef(int modelID, int tablenum, const char *tablename)
 {
-  int tableID = UNDEFID;
+  int tableID = CDI_UNDEFID;
 
   if ( ! ParTableInit ) parTableInit();
   /*
-  if ( ! (modelID == UNDEFID && tablenum == 0) )
+  if ( ! (modelID == CDI_UNDEFID && tablenum == 0) )
     tableID = tableInq(modelID, tablenum, tablename);
     */
-  if ( tableID == UNDEFID )
+  if ( tableID == CDI_UNDEFID )
     {
       tableID = tableNewEntry();
 
@@ -618,7 +616,7 @@ void tableWrite(const char *ptfile, int tableID)
   if ( CDI_Debug )
     Message("write parameter table %d to %s", tableID, ptfile);
 
-  if ( tableID == UNDEFID )
+  if ( tableID == CDI_UNDEFID )
     {
       Warning("parameter table ID undefined");
       return;
@@ -730,7 +728,7 @@ void tableFWriteC(FILE *ptfp, int tableID)
   char tablename[256];
 
 
-  if ( tableID == UNDEFID )
+  if ( tableID == CDI_UNDEFID )
     {
       Warning("parameter table ID undefined");
       return;
@@ -798,7 +796,7 @@ int tableInqParCode(int tableID, char *varname, int *code)
 {
   int err = 1;
 
-  if ( tableID != UNDEFID && varname != NULL )
+  if ( tableID != CDI_UNDEFID && varname != NULL )
     {
       int npars = parTable[tableID].npars;
       for ( int item = 0; item < npars; item++ )
@@ -835,7 +833,7 @@ int tableInqParName(int tableID, int code, char *varname)
 	    }
 	}
     }
-  else if ( tableID == UNDEFID )
+  else if ( tableID == CDI_UNDEFID )
     { }
   else
     Error("Invalid table ID %d", tableID);
@@ -848,7 +846,7 @@ const char *tableInqParNamePtr(int tableID, int code)
 {
   const char *name = NULL;
 
-  if ( tableID != UNDEFID )
+  if ( tableID != CDI_UNDEFID )
     {
       int npars = parTable[tableID].npars;
       for ( int item = 0; item < npars; item++ )
@@ -869,7 +867,7 @@ const char *tableInqParLongnamePtr(int tableID, int code)
 {
   const char *longname = NULL;
 
-  if ( tableID != UNDEFID )
+  if ( tableID != CDI_UNDEFID )
     {
       int npars = parTable[tableID].npars;
       for ( int item = 0; item < npars; item++ )
@@ -890,7 +888,7 @@ const char *tableInqParUnitsPtr(int tableID, int code)
 {
   const char *units = NULL;
 
-  if ( tableID != UNDEFID )
+  if ( tableID != CDI_UNDEFID )
     {
       int npars = parTable[tableID].npars;
       for ( int item = 0; item < npars; item++ )
@@ -909,12 +907,12 @@ const char *tableInqParUnitsPtr(int tableID, int code)
 
 int tableInqParLongname(int tableID, int code, char *longname)
 {
-  if ( ((tableID >= 0) & (tableID < MAX_TABLE)) | (tableID == UNDEFID) ) { } else
+  if ( ((tableID >= 0) & (tableID < MAX_TABLE)) | (tableID == CDI_UNDEFID) ) { } else
     Error("Invalid table ID %d", tableID);
 
   int err = 1;
 
-  if ( tableID != UNDEFID )
+  if ( tableID != CDI_UNDEFID )
     {
       int npars = parTable[tableID].npars;
       for ( int item = 0; item < npars; item++ )
@@ -936,12 +934,12 @@ int tableInqParLongname(int tableID, int code, char *longname)
 int tableInqParUnits(int tableID, int code, char *units)
 {
 
-  if ( ((tableID >= 0) & (tableID < MAX_TABLE)) | (tableID == UNDEFID) ) { } else
+  if ( ((tableID >= 0) & (tableID < MAX_TABLE)) | (tableID == CDI_UNDEFID) ) { } else
     Error("Invalid table ID %d", tableID);
 
   int err = 1;
 
-  if ( tableID != UNDEFID )
+  if ( tableID != CDI_UNDEFID )
     {
       int npars = parTable[tableID].npars;
       for ( int item = 0; item < npars; item++ )
@@ -963,7 +961,7 @@ int tableInqParUnits(int tableID, int code, char *units)
 void tableInqPar(int tableID, int code, char *name, char *longname, char *units)
 {
 
-  if ( ((tableID >= 0) & (tableID < MAX_TABLE)) | (tableID == UNDEFID) ) { } else
+  if ( ((tableID >= 0) & (tableID < MAX_TABLE)) | (tableID == CDI_UNDEFID) ) { } else
     Error("Invalid table ID %d", tableID);
 
   int npars = parTable[tableID].npars;
diff --git a/src/varscan.c b/src/varscan.c
index b76c8d95916bd728446fa1cf3517a9b011a81e9e..6b3d99a66e3358310fbfc8f2ade5ee06654f271b 100644
--- a/src/varscan.c
+++ b/src/varscan.c
@@ -2,9 +2,6 @@
 #  include "config.h"
 #endif
 
-#include <stdbool.h>
-#include <string.h>
-#include <math.h>
 
 #include "cdi.h"
 #include "cdi_int.h"
@@ -18,9 +15,6 @@
 #include "subtype.h"
 
 
-#undef  UNDEFID
-#define UNDEFID -1
-
 static size_t Vctsize = 0;
 static double *Vct = NULL;
 
@@ -74,7 +68,7 @@ typedef struct
   int            comptype;       // compression type
   int            complevel;      // compression level
   short          timave;
-  short          lmissval;
+  bool           lmissval;
   double         missval;
   char          *name;
   char          *stdname;
@@ -98,16 +92,15 @@ static vartable_t *vartable;
 static unsigned varTablesize = 0;
 static unsigned nvars = 0;
 
-
-static void
-paramInitEntry(unsigned varID, int param)
+static
+void paramInitEntry(unsigned varID, int param)
 {
   vartable[varID].param          = param;
   vartable[varID].prec           = 0;
   vartable[varID].tsteptype      = TSTEP_INSTANT;
   vartable[varID].timave         = 0;
   vartable[varID].timaccu        = 0;
-  vartable[varID].gridID         = UNDEFID;
+  vartable[varID].gridID         = CDI_UNDEFID;
   vartable[varID].zaxistype      = 0;
   vartable[varID].ltype1         = 0;
   vartable[varID].ltype2         = -1;
@@ -117,14 +110,14 @@ paramInitEntry(unsigned varID, int param)
   vartable[varID].recordTable    = NULL;
   vartable[varID].nsubtypes_alloc= 0;
   vartable[varID].nsubtypes      = 0;
-  vartable[varID].instID         = UNDEFID;
-  vartable[varID].modelID        = UNDEFID;
-  vartable[varID].tableID        = UNDEFID;
-  vartable[varID].typeOfGeneratingProcess   = UNDEFID;
-  vartable[varID].productDefinitionTemplate = UNDEFID;
+  vartable[varID].instID         = CDI_UNDEFID;
+  vartable[varID].modelID        = CDI_UNDEFID;
+  vartable[varID].tableID        = CDI_UNDEFID;
+  vartable[varID].typeOfGeneratingProcess   = CDI_UNDEFID;
+  vartable[varID].productDefinitionTemplate = CDI_UNDEFID;
   vartable[varID].comptype       = CDI_COMPRESS_NONE;
   vartable[varID].complevel      = 1;
-  vartable[varID].lmissval       = 0;
+  vartable[varID].lmissval       = false;
   vartable[varID].missval        = 0;
   vartable[varID].name           = NULL;
   vartable[varID].stdname        = NULL;
@@ -143,7 +136,7 @@ varGetEntry(int param, int zaxistype, int ltype1, int tsteptype, const char *nam
     {
       /* testing for "param" implicitly checks if we are beyond the
        * current vartable size: */
-      if (vartable[varID].param == param)
+      if ( vartable[varID].param == param )
         {
           int no_of_tiles = -1;
           if ( tiles ) no_of_tiles = tiles->numberOfTiles;
@@ -298,24 +291,24 @@ static int levelNewEntry(unsigned varID, int level1, int level2, int tileID)
       levelTable = (leveltable_t *) Malloc((size_t)levelTableSize
                                            * sizeof (leveltable_t));
       for ( int i = 0; i < levelTableSize; i++ )
-        levelTable[i].recID = UNDEFID;
+        levelTable[i].recID = CDI_UNDEFID;
     }
   else
     {
       while( levelID < levelTableSize
-             && levelTable[levelID].recID != UNDEFID )
+             && levelTable[levelID].recID != CDI_UNDEFID )
         ++levelID;
     }
   /*
     If the table overflows, double its size.
   */
-  if( levelID == levelTableSize )
+  if ( levelID == levelTableSize )
     {
       levelTable = (leveltable_t *) Realloc(levelTable,
                                             (size_t)(levelTableSize *= 2)
                                             * sizeof (leveltable_t));
       for( int i = levelID; i < levelTableSize; i++ )
-        levelTable[i].recID = UNDEFID;
+        levelTable[i].recID = CDI_UNDEFID;
     }
 
   levelTable[levelID].level1   = level1;
@@ -374,7 +367,7 @@ paramNewEntry(int param)
     {
       vartable = (vartable_t *) Realloc(vartable, (size_t)(varTablesize *= 2)
                                         * sizeof (vartable_t));
-      for( unsigned i = varID; i < varTablesize; i++ )
+      for ( unsigned i = varID; i < varTablesize; i++ )
         {
           vartable[i].param = UNDEF_PARAM;
           vartable[i].opt_grib_kvpair      = NULL;
@@ -422,6 +415,7 @@ int varInsertTileSubtype(vartable_t *vptr, const var_tile_t *tiles)
     subtypeDestroyPtr(subtype_ptr);
     return vptr->tiles->nentries - 1;
   }
+
   return CDI_UNDEFID;
 }
 
@@ -433,9 +427,9 @@ void varAddRecord(int recID, int param, int gridID, int zaxistype, int lbounds,
                   const var_tile_t *tiles, int *tile_index)
 {
   unsigned varID = (cdiSplitLtype105 != 1 || zaxistype != ZAXIS_HEIGHT) ?
-    varGetEntry(param, zaxistype, ltype1, tsteptype, name, tiles) : (unsigned)UNDEFID;
+    varGetEntry(param, zaxistype, ltype1, tsteptype, name, tiles) : (unsigned) CDI_UNDEFID;
 
-  if ( varID == (unsigned)UNDEFID )
+  if ( varID == (unsigned) CDI_UNDEFID )
     {
       nvars++;
       varID = paramNewEntry(param);
@@ -478,11 +472,12 @@ void varAddRecord(int recID, int param, int gridID, int zaxistype, int lbounds,
   int this_tile = varInsertTileSubtype(&vartable[varID], tiles);
   int tileID = tileGetEntry(varID, this_tile);
   if ( tile_index ) (*tile_index) = this_tile;
-  if (tileID == CDI_UNDEFID) {
-    tileID = tileNewEntry((int)varID);
-    vartable[varID].recordTable[tileID].subtypeIndex = this_tile;
-    vartable[varID].nsubtypes++;
-  }
+  if ( tileID == CDI_UNDEFID )
+    {
+      tileID = tileNewEntry((int)varID);
+      vartable[varID].recordTable[tileID].subtypeIndex = this_tile;
+      vartable[varID].nsubtypes++;
+    }
 
   /* append current level to level table info */
   int levelID = levelNewEntry(varID, level1, level2, tileID);
@@ -745,10 +740,10 @@ void cdi_generate_vars(stream_t *streamptr)
       vlistDefVarTimaccu(vlistID, varID, timaccu);
       vlistDefVarCompType(vlistID, varID, comptype);
 
-      if ( vartable[varid].typeOfGeneratingProcess != UNDEFID )
+      if ( vartable[varid].typeOfGeneratingProcess != CDI_UNDEFID )
         vlistDefVarTypeOfGeneratingProcess(vlistID, varID, vartable[varid].typeOfGeneratingProcess);
 
-      if ( vartable[varid].productDefinitionTemplate != UNDEFID )
+      if ( vartable[varid].productDefinitionTemplate != CDI_UNDEFID )
         vlistDefVarProductDefinitionTemplate(vlistID, varID, vartable[varid].productDefinitionTemplate);
 
       if ( vartable[varid].lmissval ) vlistDefVarMissval(vlistID, varID, vartable[varid].missval);
@@ -777,13 +772,13 @@ void cdi_generate_vars(stream_t *streamptr)
         }
       /* note: if the key is not defined, we do not throw an error! */
 
-      if ( cdiDefaultTableID != UNDEFID )
+      if ( cdiDefaultTableID != CDI_UNDEFID )
 	{
 	  int pdis, pcat, pnum;
 	  cdiDecodeParam(param, &pnum, &pcat, &pdis);
 	  if ( tableInqParNamePtr(cdiDefaultTableID, pnum) )
 	    {
-	      if ( tableID != UNDEFID )
+	      if ( tableID != CDI_UNDEFID )
 		{
 		  strcpy(name, tableInqParNamePtr(cdiDefaultTableID, pnum));
 		  vlistDefVarName(vlistID, varID, name);
@@ -801,13 +796,13 @@ void cdi_generate_vars(stream_t *streamptr)
 	      else
 		tableID = cdiDefaultTableID;
 	    }
-	  if ( cdiDefaultModelID != UNDEFID ) modelID = cdiDefaultModelID;
-	  if ( cdiDefaultInstID  != UNDEFID )  instID = cdiDefaultInstID;
+	  if ( cdiDefaultModelID != CDI_UNDEFID ) modelID = cdiDefaultModelID;
+	  if ( cdiDefaultInstID  != CDI_UNDEFID )  instID = cdiDefaultInstID;
 	}
 
-      if ( instID  != UNDEFID ) vlistDefVarInstitut(vlistID, varID, instID);
-      if ( modelID != UNDEFID ) vlistDefVarModel(vlistID, varID, modelID);
-      if ( tableID != UNDEFID ) vlistDefVarTable(vlistID, varID, tableID);
+      if ( instID  != CDI_UNDEFID ) vlistDefVarInstitut(vlistID, varID, instID);
+      if ( modelID != CDI_UNDEFID ) vlistDefVarModel(vlistID, varID, modelID);
+      if ( tableID != CDI_UNDEFID ) vlistDefVarTable(vlistID, varID, tableID);
     }
 
   for ( unsigned index = 0; index < nvars; index++ )
@@ -864,21 +859,16 @@ void varDefZAxisReference(int nhlev, int nvgrid, unsigned char uuid[CDI_UUID_SIZ
 bool zaxisCompare(int zaxisID, int zaxistype, int nlevels, bool lbounds, const double *levels, const char *longname, const char *units, int ltype1)
 {
   bool differ = true;
-  bool zlbounds = false;
-  bool ltype_is_equal = false;
 
-  if ( ltype1 == zaxisInqLtype(zaxisID) ) ltype_is_equal = true;
+  bool ltype_is_equal = (ltype1 == zaxisInqLtype(zaxisID));
 
   if ( ltype_is_equal && (zaxistype == zaxisInqType(zaxisID) || zaxistype == ZAXIS_GENERIC) )
     {
-      if ( zaxisInqLbounds(zaxisID, NULL) > 0 ) zlbounds = true;
+      bool zlbounds = (zaxisInqLbounds(zaxisID, NULL) > 0);
       if ( nlevels == zaxisInqSize(zaxisID) && zlbounds == lbounds )
 	{
-	  char zlongname[CDI_MAX_NAME];
-	  char zunits[CDI_MAX_NAME];
-
 	  const double *dlevels = zaxisInqLevelsPtr(zaxisID);
-          if ( dlevels )
+          if ( dlevels && levels )
             {
               int levelID;
               for ( levelID = 0; levelID < nlevels; levelID++ )
@@ -886,23 +876,24 @@ bool zaxisCompare(int zaxisID, int zaxistype, int nlevels, bool lbounds, const d
                   if ( fabs(dlevels[levelID] - levels[levelID]) > 1.e-9 )
                     break;
                 }
-
               if ( levelID == nlevels ) differ = false;
             }
 
 	  if ( ! differ )
 	    {
-	      zaxisInqLongname(zaxisID, zlongname);
-	      zaxisInqUnits(zaxisID, zunits);
-	      if ( longname && zlongname[0] )
-		{
-		  if ( strcmp(longname, zlongname) != 0 ) differ = true;
-		}
-	      if ( units && zunits[0] )
-		{
-		  if ( strcmp(units, zunits) != 0 ) differ = true;
-		}
-	    }
+              if ( longname )
+                {
+                  char zlongname[CDI_MAX_NAME];
+                  zaxisInqLongname(zaxisID, zlongname);
+                  if ( zlongname[0] && strcmp(longname, zlongname) != 0 ) differ = true;
+                }
+              if ( units )
+                {
+                  char zunits[CDI_MAX_NAME];
+                  zaxisInqUnits(zaxisID, zunits);
+                  if ( zunits[0] && strcmp(units, zunits) != 0 ) differ = true;
+                }
+            }
 	}
     }
 
@@ -946,7 +937,7 @@ int varDefZaxis(int vlistID, int zaxistype, int nlevels, const double *levels, b
     mode: 0 search in vlist and zaxis table
           1 search in zaxis table
    */
-  int zaxisID = UNDEFID;
+  int zaxisID = CDI_UNDEFID;
   bool zaxisdefined = false;
   bool zaxisglobdefined = false;
   vlist_t *vlistptr = vlist_to_pointer(vlistID);
@@ -957,7 +948,7 @@ int varDefZaxis(int vlistID, int zaxistype, int nlevels, const double *levels, b
       {
 	zaxisID = vlistptr->zaxisIDs[index];
 
-	if ( zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, longname, units, ltype1) == false )
+	if ( !zaxisCompare(zaxisID, zaxistype, nlevels, lbounds, levels, longname, units, ltype1) )
 	  {
 	    zaxisdefined = true;
 	    break;
@@ -1021,7 +1012,7 @@ int varDefZaxis(int vlistID, int zaxistype, int nlevels, const double *levels, b
 
 void varDefMissval(int varID, double missval)
 {
-  vartable[varID].lmissval = 1;
+  vartable[varID].lmissval = true;
   vartable[varID].missval = missval;
 }
 
diff --git a/src/zaxis.c b/src/zaxis.c
index caaa81f322278454d7c11a718f0ca74c1c36f51a..9ea9c50846a67d36be7ee792e0f00571c9bf37fd 100644
--- a/src/zaxis.c
+++ b/src/zaxis.c
@@ -35,7 +35,7 @@ ZaxistypeEntry[] = {
   { /*  1 */ 0, "lev",               "generic",                "",               ""},
   { /*  2 */ 2, "lev",               "hybrid",                 "",               "level"},
   { /*  3 */ 2, "lev",               "hybrid_half",            "",               "level"},
-  { /*  4 */ 2, "lev",               "pressure",               "air_pressure",   "Pa"},
+  { /*  4 */ 2, "plev",              "pressure",               "air_pressure",   "Pa"},
   { /*  5 */ 1, "height",            "height",                 "height",         "m"},
   { /*  6 */ 2, "depth",             "depth_below_sea",        "depth",          "m"},
   { /*  7 */ 2, "depth",             "depth_below_land",       "",               "cm"},