diff --git a/.gitignore b/.gitignore
index cdea57a8cb334212bdaf9692e9455363521629f5..dceaeaa1c4bd742c9511cf379e5fa73c17f5186d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
 # Autoreconf stage files:
 /autom4te.cache
+/configure~
 /src/config.h.in~
 
 # Distributed autoreconf stage files:
@@ -24,22 +25,20 @@ Makefile.in
 
 # Configure stage files:
 .deps
-/cdi.settings
-/config.log
-/config.lt
-/config.status
-/examples/pio/collectData
-/examples/pio/collectData.parallel
-/interfaces/*.run
-/libtool
-/src/cmake/cdi/*.cmake
-/src/config.h
-/src/pkgconfig/*.pc
-/src/stamp-h1
-/tables/gen_tableheaderfile
-/tests/*.run
-/tests/test_cksum_base
+/**/interfaces/*.run
+/**/src/cmake/cdi/*.cmake
+/**/src/config.h
+/**/src/pkgconfig/*.pc
+/**/src/stamp-h1
+/**/tables/gen_tableheaderfile
+/**/tests/*.run
+/**/tests/test_cksum_base
 Makefile
+cdi.settings
+config.log
+config.lt
+config.status
+libtool
 
 # Build stage files:
 *.L
@@ -48,88 +47,90 @@ Makefile
 *.mod
 *.o
 .dirstamp
-/*/**/.libs
-/app/cdi
-/app/createtable
+/**/.libs
+/**/app/cdi
+/**/app/createtable
+/**/examples/cdi_append
+/**/examples/cdi_copy
+/**/examples/cdi_read
+/**/examples/cdi_read_f2003
+/**/examples/cdi_write
+/**/examples/cdi_write_const
+/**/examples/cdi_write_ens
+/**/examples/cdi_write_f2003
+/**/examples/cdi_write_hybrid
+/**/examples/cdi_write_local
+/**/examples/cdi_write_relativ
+/**/examples/pio/*.parallel.F90
+/**/examples/pio/*.parallel.c
+/**/examples/pio/collectData
+/**/examples/pio/collectData.parallel
+/**/examples/pio/collectData2003
+/**/examples/pio/collectData2003.parallel
+/**/examples/pio/collectDataNStreams
+/**/examples/pio/collectDataNStreams.parallel
+/**/examples/pio/compareResourcesArray
+/**/interfaces/CdiInfo
+/**/interfaces/python/Cdi.py
+/**/interfaces/python/cdi_wrapper.cpp
+/**/interfaces/ruby/cdi_wrapper.cpp
+/**/src/cdi.inc
+/**/src/cdiFortran.c
+/**/src/cdipio.inc
+/**/src/cdipioFortran.c
+/**/src/make_fint
+/**/src/mo_cdi.f90
+/**/tests/*.parallel.c
+/**/tests/calendar_test1
+/**/tests/cksum_read
+/**/tests/cksum_verify
+/**/tests/cksum_write
+/**/tests/cksum_write_chunk
+/**/tests/pio_write
+/**/tests/pio_write.parallel
+/**/tests/pio_write_deco2d
+/**/tests/pio_write_deco2d.parallel
+/**/tests/test_byteswap
+/**/tests/test_cdf_read
+/**/tests/test_cdf_write
+/**/tests/test_grib
+/**/tests/test_month_adjust
+/**/tests/test_resource_copy
+/**/tests/test_resource_copy.parallel
+/**/tests/test_table
 /build
-/examples/cdi_append
-/examples/cdi_copy
-/examples/cdi_read
-/examples/cdi_read_f2003
-/examples/cdi_write
-/examples/cdi_write_const
-/examples/cdi_write_ens
-/examples/cdi_write_f2003
-/examples/cdi_write_hybrid
-/examples/cdi_write_local
-/examples/cdi_write_relativ
-/examples/pio/*.parallel.F90
-/examples/pio/*.parallel.c
-/examples/pio/collectData2003
-/examples/pio/collectData2003.parallel
-/examples/pio/collectDataNStreams
-/examples/pio/collectDataNStreams.parallel
-/examples/pio/compareResourcesArray
-/interfaces/CdiInfo
-/interfaces/python/Cdi.py
-/interfaces/python/cdi_wrapper.cpp
-/interfaces/ruby/cdi_wrapper.cpp
-/src/cdi.inc
-/src/cdiFortran.c
-/src/cdipio.inc
-/src/cdipioFortran.c
-/src/make_fint
-/src/mo_cdi.f90
-/tests/*.parallel.c
-/tests/calendar_test1
-/tests/cksum_read
-/tests/cksum_verify
-/tests/cksum_write
-/tests/cksum_write_chunk
-/tests/pio_write
-/tests/pio_write.parallel
-/tests/pio_write_deco2d
-/tests/pio_write_deco2d.parallel
-/tests/test_byteswap
-/tests/test_cdf_read
-/tests/test_cdf_write
-/tests/test_grib
-/tests/test_month_adjust
-/tests/test_resource_copy
-/tests/test_resource_copy.parallel
-/tests/test_table
 
 # Test stage files:
-/examples/**/*.grb
-/examples/**/*.grb2
-/examples/**/*.nc
-/interfaces/*.log
-/interfaces/*.trs
-/interfaces/python/*.pyc
-/interfaces/python/*.pyo
-/interfaces/python/__pycache__
-/tests/*.cksum
-/tests/*.ext
-/tests/*.grb
-/tests/*.grb2
-/tests/*.ieg
-/tests/*.log
-/tests/*.nc
-/tests/*.nc2
-/tests/*.nc4
-/tests/*.srv
-/tests/*.stderr
-/tests/*.stdout
-/tests/*.trs
+/**/examples/**/*.grb
+/**/examples/**/*.grb2
+/**/examples/**/*.nc
+/**/interfaces/*.log
+/**/interfaces/*.trs
+/**/interfaces/python/*.pyc
+/**/interfaces/python/*.pyo
+/**/interfaces/python/__pycache__
+/**/tests/*.cksum
+/**/tests/*.ext
+/**/tests/*.grb
+/**/tests/*.grb2
+/**/tests/*.ieg
+/**/tests/*.log
+/**/tests/*.nc
+/**/tests/*.nc2
+/**/tests/*.nc4
+/**/tests/*.srv
+/**/tests/*.stderr
+/**/tests/*.stdout
+/**/tests/*.trs
 
 # Documentation stage files:
 /doc/cdi_cman.pdf
 /doc/cdi_fman.pdf
 
 # Dist stage files:
-/cdi-[0-9].[0-9].[0-9]*
-/cdi-[0-9].[0-9][0-9].[0-9]*
-/src/cdilib.c
+/**/cdi-[0-9].[0-9].[0-9]*
+/**/cdi-[0-9].[0-9][0-9].[0-9]*
+/**/src/cdilib.c
 
 # Mac OS files:
 *.dSYM
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000000000000000000000000000000000000..71c43b56eb83dde30767073a4375a64f8d883746
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,29 @@
+# cmake options:
+#
+#       -DCMAKE_INSTALL_PREFIX=/path/to/install
+
+cmake_minimum_required( VERSION 3.12 FATAL_ERROR )
+
+project( cdi VERSION 2.4.0 LANGUAGES C )
+set(CMAKE_C_STANDARD 11)
+
+configure_file (
+  "${PROJECT_SOURCE_DIR}/cmake/cdi_config.h.in"
+  "${PROJECT_BINARY_DIR}/config.h"
+  )
+
+include_directories("${PROJECT_BINARY_DIR}")
+
+list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
+
+find_package(NetCDF)
+
+set(HAVE_NETCDF ${netCDF_FOUND} )
+
+if (netCDF_FOUND)
+  set(HAVE_NETCDF 1)
+endif ()
+
+add_subdirectory( src )
+add_subdirectory( app )
+#target_link_libraries(cdi PRIVATE NetCDF)
diff --git a/ChangeLog b/ChangeLog
index 651e6e7c53e6834a2846a61c3b0b4e24205df5b9..dc90f14dc0928de9b6fa524179d596fa8ad3b523 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,47 @@
+2024-02-22  Uwe Schulzweida
+
+	* Version 2.4.0 released
+
+2024-02-19  Uwe Schulzweida
+
+	* netCDF: use timevar buffer
+
+2024-02-05  Uwe Schulzweida
+
+	* fdb5 interface: update to release 5.11.29
+
+2024-02-02  Uwe Schulzweida
+
+	* improve support for time units second [Bug #11740]
+
+2024-01-10  Uwe Schulzweida
+
+	* cgribex: check gridsize when creating the inventory
+
+2024-01-04  Uwe Schulzweida
+
+	* define SSIZE_MAX to LONG_MAX, if undefined
+
+2023-12-29  Uwe Schulzweida
+
+	* Add global option CDI_Threadsafe to lock non thread-safe netCDF4/HDF5 calls
+
+2023-12-28  Uwe Schulzweida
+
+	* Add environment variable CDI_LOCK_IO to lock IO access
+
+2023-11-22  Uwe Schulzweida
+
+	* calc_chunk_cache_size: wrong result for 3D data (bug fix)
+
+2023-11-21  Uwe Schulzweida
+
+	* vlist_generate_zaxis: init query.lbounds and query.ubounds (bug fix)
+
+2023-11-16  Uwe Schulzweida
+
+	* netCDF: write of y-coordinates failed for Gaussian reduced grids (bug fix)
+
 2023-10-18  Uwe Schulzweida
 
 	* Version 2.3.0 released
@@ -171,13 +215,13 @@
 
 2022-10-17  Uwe Schulzweida
 
-        * Added environment variable CDI_SHUFFLE to set shuffle option to NetCDF4 deflation compression
+        * Add environment variable CDI_SHUFFLE to set shuffle option to NetCDF4 deflation compression
 
 2022-10-16  Uwe Schulzweida
 
-	* Improved read performance of temporal chunked NetCDF4 data
-        * Added environment variable CDI_CHUNK_CACHE to set the NetCDF4 chunk cache size
-        * Added environment variable CDI_CHUNK_CACHE_MAX to set the maximum chunk cache size
+	* Improve read performance of temporal chunked NetCDF4 data
+        * Add environment variable CDI_CHUNK_CACHE to set the NetCDF4 chunk cache size
+        * Add environment variable CDI_CHUNK_CACHE_MAX to set the maximum chunk cache size
 
 2022-10-14  Uwe Schulzweida
 
diff --git a/NEWS b/NEWS
index 66f83c2a8f9091e081aa6159899bb738c87f91a9..dffb7bc9ac9a876f2e0de483f963435f0dbee855 100644
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,16 @@
 CDI NEWS
 --------
 
+Version 2.4.0 (22 February 2024):
+
+   New features:
+     * Add experimental interface to FDB5
+     * Add global option CDI_Threadsafe to lock non thread-safe netCDF4/HDF5 calls
+   Fixed bugs:
+     * improve support for time units second [Bug #11740]
+     * calc_chunk_cache_size: wrong result for 3D data (bug fix)
+     * netCDF: write of y-coordinates failed for Gaussian reduced grids (bug fix)
+
 Version 2.3.0 (18 October 2023):
 
    New features:
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
new file mode 100644
index 0000000000000000000000000000000000000000..22bca89f702b3c330d8e9cfdf9f2ff7d493f6f4c
--- /dev/null
+++ b/app/CMakeLists.txt
@@ -0,0 +1,12 @@
+add_executable(cdi
+  cdi.c printinfo.c
+)
+
+target_include_directories(cdi PUBLIC
+  $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>
+  $<INSTALL_INTERFACE:include
+)
+
+target_link_libraries(cdi PUBLIC cdilib)
+
+target_compile_definitions(cdi PUBLIC HAVE_CONFIG_H)
diff --git a/app/cdi.c b/app/cdi.c
index 77fd6c623dae498e31d58c827bbd1c4690c11081..2bea675a990c0562aae5d0c424a064e049a10b67 100644
--- a/app/cdi.c
+++ b/app/cdi.c
@@ -1,5 +1,5 @@
 #ifdef HAVE_CONFIG_H
-#include "../src/config.h"
+#include "config.h"
 #endif
 
 #include <inttypes.h>
@@ -432,7 +432,7 @@ print_short_info(int streamID, int vlistID, int vardis)
       int datatype = vlistInqVarDatatype(vlistID, varID);
       // clang-format off
       if      (datatype == CDI_DATATYPE_PACK  ) strcpy(pstr, "P0");
-      else if (datatype > 0 && datatype <= 32 ) sprintf(pstr, "P%d", datatype);
+      else if (datatype > 0 && datatype <= 32 ) snprintf(pstr, sizeof(pstr), "P%d", datatype);
       else if (datatype == CDI_DATATYPE_CPX32 ) strcpy(pstr, "C32");
       else if (datatype == CDI_DATATYPE_CPX64 ) strcpy(pstr, "C64");
       else if (datatype == CDI_DATATYPE_FLT32 ) strcpy(pstr, "F32");
diff --git a/cmake/FindNetCDF.cmake b/cmake/FindNetCDF.cmake
new file mode 100644
index 0000000000000000000000000000000000000000..194b1a0d32a629aefc0841582c1df02f0b681083
--- /dev/null
+++ b/cmake/FindNetCDF.cmake
@@ -0,0 +1,131 @@
+#[==[
+Provides the following variables:
+
+  * `NetCDF_FOUND`: Whether NetCDF was found or not.
+  * `NetCDF_INCLUDE_DIRS`: Include directories necessary to use NetCDF.
+  * `NetCDF_LIBRARIES`: Libraries necessary to use NetCDF.
+  * `NetCDF_VERSION`: The version of NetCDF found.
+  * `NetCDF::NetCDF`: A target to use with `target_link_libraries`.
+  * `NetCDF_HAS_PARALLEL`: Whether or not NetCDF was found with parallel IO support.
+#]==]
+
+function(FindNetCDF_get_is_parallel_aware include_dir)
+  file(STRINGS "${include_dir}/netcdf_meta.h" _netcdf_lines
+    REGEX "#define[ \t]+NC_HAS_PARALLEL[ \t]")
+  string(REGEX REPLACE ".*NC_HAS_PARALLEL[ \t]*([0-1]+).*" "\\1" _netcdf_has_parallel "${_netcdf_lines}")
+  if (_netcdf_has_parallel)
+    set(NetCDF_HAS_PARALLEL TRUE PARENT_SCOPE)
+  else()
+    set(NetCDF_HAS_PARALLEL FALSE PARENT_SCOPE)
+  endif()
+endfunction()
+
+# Try to find a CMake-built NetCDF.
+find_package(netCDF CONFIG QUIET)
+if (netCDF_FOUND)
+  # Forward the variables in a consistent way.
+  set(NetCDF_FOUND "${netCDF_FOUND}")
+  set(NetCDF_INCLUDE_DIRS "${netCDF_INCLUDE_DIR}")
+  set(NetCDF_LIBRARIES "${netCDF_LIBRARIES}")
+  set(NetCDF_VERSION "${NetCDFVersion}")
+
+  include(FindPackageHandleStandardArgs)
+  find_package_handle_standard_args(NetCDF
+    REQUIRED_VARS NetCDF_INCLUDE_DIRS NetCDF_LIBRARIES
+    VERSION_VAR NetCDF_VERSION)
+
+  if (NOT TARGET NetCDF::NetCDF)
+    add_library(NetCDF::NetCDF INTERFACE IMPORTED)
+    if (TARGET "netCDF::netcdf")
+      # 4.7.3
+      set_target_properties(NetCDF::NetCDF PROPERTIES
+        INTERFACE_LINK_LIBRARIES "netCDF::netcdf")
+    elseif (TARGET "netcdf")
+      set_target_properties(NetCDF::NetCDF PROPERTIES
+        INTERFACE_LINK_LIBRARIES "netcdf")
+    else ()
+      set_target_properties(NetCDF::NetCDF PROPERTIES
+        INTERFACE_LINK_LIBRARIES "${netCDF_LIBRARIES}")
+    endif ()
+  endif ()
+
+  FindNetCDF_get_is_parallel_aware("${NetCDF_INCLUDE_DIRS}")
+  # Skip the rest of the logic in this file.
+  return ()
+endif ()
+
+find_package(PkgConfig QUIET)
+if (PkgConfig_FOUND)
+  pkg_check_modules(_NetCDF QUIET netcdf IMPORTED_TARGET)
+  if (_NetCDF_FOUND)
+    # Forward the variables in a consistent way.
+    set(NetCDF_FOUND "${_NetCDF_FOUND}")
+    set(NetCDF_INCLUDE_DIRS "${_NetCDF_INCLUDE_DIRS}")
+    set(NetCDF_LIBRARIES "${_NetCDF_LIBRARIES}")
+    set(NetCDF_VERSION "${_NetCDF_VERSION}")
+
+    include(FindPackageHandleStandardArgs)
+    find_package_handle_standard_args(NetCDF
+      REQUIRED_VARS NetCDF_LIBRARIES
+      # This is not required because system-default include paths are not
+      # reported by `FindPkgConfig`, so this might be empty. Assume that if we
+      # have a library, the include directories are fine (if any) since
+      # PkgConfig reported that the package was found.
+      # NetCDF_INCLUDE_DIRS
+      VERSION_VAR NetCDF_VERSION)
+
+    if (NOT TARGET NetCDF::NetCDF)
+      add_library(NetCDF::NetCDF INTERFACE IMPORTED)
+      set_target_properties(NetCDF::NetCDF PROPERTIES
+        INTERFACE_LINK_LIBRARIES "PkgConfig::_NetCDF")
+    endif ()
+
+    FindNetCDF_get_is_parallel_aware("${_NetCDF_INCLUDEDIR}")
+    # Skip the rest of the logic in this file.
+    return ()
+  endif ()
+endif ()
+
+find_path(NetCDF_INCLUDE_DIR
+  NAMES netcdf.h
+  DOC "netcdf include directories")
+mark_as_advanced(NetCDF_INCLUDE_DIR)
+
+find_library(NetCDF_LIBRARY
+  NAMES netcdf
+  DOC "netcdf library")
+mark_as_advanced(NetCDF_LIBRARY)
+
+if (NetCDF_INCLUDE_DIR)
+  file(STRINGS "${NetCDF_INCLUDE_DIR}/netcdf_meta.h" _netcdf_version_lines
+    REGEX "#define[ \t]+NC_VERSION_(MAJOR|MINOR|PATCH|NOTE)")
+  string(REGEX REPLACE ".*NC_VERSION_MAJOR *\([0-9]*\).*" "\\1" _netcdf_version_major "${_netcdf_version_lines}")
+  string(REGEX REPLACE ".*NC_VERSION_MINOR *\([0-9]*\).*" "\\1" _netcdf_version_minor "${_netcdf_version_lines}")
+  string(REGEX REPLACE ".*NC_VERSION_PATCH *\([0-9]*\).*" "\\1" _netcdf_version_patch "${_netcdf_version_lines}")
+  string(REGEX REPLACE ".*NC_VERSION_NOTE *\"\([^\"]*\)\".*" "\\1" _netcdf_version_note "${_netcdf_version_lines}")
+  set(NetCDF_VERSION "${_netcdf_version_major}.${_netcdf_version_minor}.${_netcdf_version_patch}${_netcdf_version_note}")
+  unset(_netcdf_version_major)
+  unset(_netcdf_version_minor)
+  unset(_netcdf_version_patch)
+  unset(_netcdf_version_note)
+  unset(_netcdf_version_lines)
+
+  FindNetCDF_get_is_parallel_aware("${NetCDF_INCLUDE_DIR}")
+endif ()
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(NetCDF
+  REQUIRED_VARS NetCDF_LIBRARY NetCDF_INCLUDE_DIR
+  VERSION_VAR NetCDF_VERSION)
+
+if (NetCDF_FOUND)
+  set(NetCDF_INCLUDE_DIRS "${NetCDF_INCLUDE_DIR}")
+  set(NetCDF_LIBRARIES "${NetCDF_LIBRARY}")
+
+  if (NOT TARGET NetCDF::NetCDF)
+    add_library(NetCDF::NetCDF UNKNOWN IMPORTED)
+    set_target_properties(NetCDF::NetCDF PROPERTIES
+      IMPORTED_LOCATION "${NetCDF_LIBRARY}"
+      INTERFACE_INCLUDE_DIRECTORIES "${NetCDF_INCLUDE_DIR}")
+  endif ()
+endif ()
diff --git a/cmake/cdi_config.h.in b/cmake/cdi_config.h.in
new file mode 100644
index 0000000000000000000000000000000000000000..e3bfd3225b75696f7fcb8e82af0f6f5b8acccba8
--- /dev/null
+++ b/cmake/cdi_config.h.in
@@ -0,0 +1,20 @@
+#ifndef cdi_config_h
+#define cdi_config_h
+
+#define PACKAGE_NAME      "@PROJECT_NAME@"
+
+#define VERSION           "@PROJECT_VERSION@"
+
+#define CDI               1
+
+#define HAVE_LIBGRIB      1
+#define HAVE_LIBCGRIBEX   1
+#define HAVE_LIBEXTRA     1
+#define HAVE_LIBSERVICE   1
+#define HAVE_LIBIEG       1
+
+#define HAVE_NETCDF       @HAVE_NETCDF@
+#define HAVE_NETCDF_NC4   @netCDF_HAS_NC4@
+#define NETCDF_FOUND      @NetCDF_FOUND@
+
+#endif /* cdi_config_h */
diff --git a/config/default b/config/default
index 238a6978cb297034ac71e7e7fd4c22e3e79188d5..81986338b547afbb70f0afec1d624ef535d141b5 100755
--- a/config/default
+++ b/config/default
@@ -63,14 +63,23 @@ case "${HOSTNAME}" in
 	            CC=gcc CFLAGS="-g -pipe -D_REENTRANT -Wall -W -Wfloat-equal -pedantic -O2"
 	;;
     hama*)
-        CDILIBS="--disable-iso-c-interface \
+        CDILIBS="--enable-iso-c-interface \
                  --enable-maintainer-mode \
+                 --enable-swig \
+                 --enable-python \
                  --with-szlib=/opt/local/lib/libaec \
-                 --with-eccodes=$HOME/local/eccodes-2.32.0 \
-                 --with-netcdf=$HOME/local/netcdf-c-4.9.1"
+                 --with-eccodes=/opt/local \
+                 --with-netcdf=/opt/local"
         PREFIX="--prefix=$HOME/local/cdi"
-        LD_ADD="-Wl,-rpath,$HOME/local/eccodes-2.32.0/lib"
-        if  test "$COMP" = clang ; then
+        # LD_ADD="-Wl,-rpath,$HOME/local/eccodes-2.32.0/lib"
+        if  test "$COMP" = mpicc ; then
+          MPI_LAUNCH="$(which mpirun)"
+          YAXT_ROOT='/Users/m214003/local/yaxt-0.10.0'
+          ${CONFPATH}configure --enable-mpi $CONFIG_OPTS $CDILIBS LDFLAGS="$LD_ADD $LDFLAGS" \
+	             CC=mpicc CFLAGS="-g -pipe -D_REENTRANT -Wall -Wwrite-strings -W -Wfloat-equal -pedantic -O3" \
+                     MPI_LAUNCH="${MPI_LAUNCH}" \
+                     PKG_CONFIG_PATH="${YAXT_ROOT}/lib/pkgconfig"
+        elif  test "$COMP" = clang ; then
           ${CONFPATH}configure $CONFIG_OPTS $PREFIX $CDILIBS LDFLAGS="$LD_ADD $LDFLAGS" \
 	             CC=clang CFLAGS="-g -pipe -D_REENTRANT -Wall -Wwrite-strings -W -Wfloat-equal -pedantic -O3"
 	else
diff --git a/configure.ac b/configure.ac
index 3d986890f58e4071bd6af210709829821a666534..0723a979d5a61bec35377b01527438f7cca5c46e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -7,7 +7,7 @@
 AC_PREREQ([2.69])
 LT_PREREQ([2.4.6])
 
-AC_INIT([cdi],[2.3.0],[https://mpimet.mpg.de/cdi])
+AC_INIT([cdi],[2.4.0],[https://mpimet.mpg.de/cdi])
 AC_DEFINE_UNQUOTED(CDI, ["$PACKAGE_VERSION"], [CDI version])
 
 AC_CONFIG_AUX_DIR([config])
@@ -21,11 +21,26 @@ AM_INIT_AUTOMAKE([1.16.1 foreign])
 AM_MAINTAINER_MODE([enable])
 AM_EXTRA_RECURSIVE_TARGETS([examples])
 
-m4_pushdef([_AC_PROG_CC_C89],
-  [rm -rf conftest.dSYM # needed when configured on MacOS with CFLAGS='-g'
-   _AC_PROG_CC_C99([], [AC_MSG_FAILURE([C compiler does not support ISO C99])])])dnl
-AC_PROG_CC
-m4_popdef([_AC_PROG_CC_C89])dnl
+dnl Tests for the C compiler:
+dnl   Make sure conftest.dSYM is removed when configured on MacOS with
+dnl   CFLAGS='-g':
+m4_pushdef([_AM_PROG_CC_C_O], m4_bpatsubst(m4_dquote(m4_defn([_AM_PROG_CC_C_O])),
+  [rm -f\(.* conftest\*.*\)], [rm -rf\1]))dnl
+dnl   Check for ISO C99 support
+dnl   (Autoconf 2.69- and 2.70+ have different documented ways for this):
+m4_case(m4_version_compare(AC_AUTOCONF_VERSION,[2.70]),
+  [-1], [AC_PROG_CC_C99],
+  [0],  [dnl
+dnl   Fix a bug in Autoconf 2.70 (see https://savannah.gnu.org/support/?110396):
+         m4_pushdef([_AC_PROG_CC_C99],
+         m4_bpatsubst(m4_dquote(m4_defn([_AC_PROG_CC_C99])),
+           [\[ac_c_conftest_c89_program\]], [[ac_c_conftest_c99_program]]))dnl
+         AC_PROG_CC
+         m4_popdef([_AC_PROG_CC_C99])],
+  [1],  [AC_PROG_CC])
+AS_VAR_IF([ac_cv_prog_cc_c99], [no],
+  [AC_MSG_FAILURE([unable to detect C compiler flag needed to accept ISO C99])])
+m4_popdef([AC_PROG_CC_C_O])dnl
 ACX_PROG_CC_POSIX([2001])
 AC_C_RESTRICT
 
@@ -234,7 +249,7 @@ AC_FUNC_MMAP
 dnl Check for structures:
 AC_CHECK_MEMBERS([struct stat.st_blksize])
 dnl Checks for header files:
-AC_CHECK_HEADERS([malloc.h unistd.h sys/time.h],,,[AC_INCLUDES_DEFAULT])
+AC_CHECK_HEADERS([malloc.h sys/time.h],,,[AC_INCLUDES_DEFAULT])
 AX_EXECINFO
 dnl Checks for the availability of functions:
 AC_CHECK_FUNCS([mallinfo pwrite])
diff --git a/contrib/00nagfor-libtool-patch/nagfor-libtool-2.4.7.patch b/contrib/00nagfor-libtool-patch/nagfor-libtool-2.4.7.patch
new file mode 100644
index 0000000000000000000000000000000000000000..9af416d050b9e93eb261679c5bf4085283e18939
--- /dev/null
+++ b/contrib/00nagfor-libtool-patch/nagfor-libtool-2.4.7.patch
@@ -0,0 +1,122 @@
+--- a/config/ltmain.sh	2012-06-12 00:20:43.000000000 +0200
++++ b/config/ltmain.sh	2014-08-19 16:34:48.161235251 +0200
+@@ -3217,6 +3217,25 @@
+ }
+ # end func_convert_path_msys_to_cygwin
+ 
++func_add2xrpath ()
++{
++  dir="$1"
++  # We need an absolute path.
++  case $dir in
++    [\\/]* | [A-Za-z]:[\\/]*) ;;
++    =*)
++    func_stripname '=' '' "$dir"
++    dir=$lt_sysroot$func_stripname_result
++      ;;
++    *)
++      func_fatal_error "only absolute run-paths are allowed"
++      ;;
++  esac
++  case "$xrpath " in
++    *" $dir "*) ;;
++    *) func_append xrpath " $dir" ;;
++  esac
++}
+ 
+ # func_convert_path_nix_to_cygwin ARG
+ # Convert path ARG from *nix to Cygwin format.  Requires Cygwin installed in a
+@@ -7147,20 +7166,7 @@
+ 	func_stripname '-R' '' "$arg"
+ 	dir=$func_stripname_result
+ 	# We need an absolute path.
+-	case $dir in
+-	[\\/]* | [A-Za-z]:[\\/]*) ;;
+-	=*)
+-	  func_stripname '=' '' "$dir"
+-	  dir=$lt_sysroot$func_stripname_result
+-	  ;;
+-	*)
+-	  func_fatal_error "only absolute run-paths are allowed"
+-	  ;;
+-	esac
+-	case "$xrpath " in
+-	*" $dir "*) ;;
+-	*) func_append xrpath " $dir" ;;
+-	esac
++	func_add2xrpath "$dir"
+ 	continue
+ 	;;
+ 
+@@ -7221,6 +7227,49 @@
+ 	;;
+ 
+       -Wl,*)
++	wlprefix=`expr x"$wl" : '.*' - 1`
++	wlprefix=`expr x"$arg" : 'x\(.\{'"$wlprefix"'\}\)'`
++	if test x"$wlprefix" = x"$wl" -a x"$wl" != x"-Wl,"; then
++	  func_append compiler_flags " $arg"
++	  func_append compile_command " $arg"
++	  func_append finalize_command " $arg"
++	  if expr "x$arg" : '^x'"$wl"'-rpath,,' >/dev/null ; then
++	    func_stripname "$wl"'-rpath,,' '' "$arg"
++	    dir=$func_stripname_result
++	    func_add2xrpath "$dir"
++	  elif expr "x$arg" : '^x'"$wl"'-rpath$' >/dev/null ; then
++	    func_stripname "$wl" '' "$1"
++	    dir=$func_stripname_result
++	    if ! expr "x$1" : '^x'"$wl" >/dev/null \
++	    || ! test -d "$dir" ; then
++	    func_fatal_error "$wl"'-rpath must be followed by '"$wl"'-escaped directory'
++	    fi
++	    shift
++	    func_add2xrpath "$dir"
++	    continue
++	  else
++	    func_stripname "$wl" '' "$arg"
++	    func_append new_inherited_linker_flags " -Wl,$func_stripname_result"
++	  fi
++	  continue
++	elif expr "x$arg" : '^x-Wl,-rpath,' >/dev/null ; then
++	  func_stripname '-Wl,-rpath,' '' "$arg"
++	  dir=$func_stripname_result
++	  func_add2xrpath "$dir"
++	  continue
++	elif expr "x$arg" : '^x-Wl,-rpath$' >/dev/null ; then
++	  func_stripname '-Wl,' '' "$1"
++	  dir=$func_stripname_result
++	  if ! expr "x$1" : '^x-Wl,' >/dev/null \
++	    || ! test -d "$dir" ; then
++	    func_fatal_error '-Wl,-rpath must be followed by -Wl-escaped directory'
++	  fi
++	  func_quote eval "$1"
++	  func_append libtool_args " $func_quote_result"
++	  shift
++	  func_add2xrpath "$dir"
++	  continue
++	fi
+ 	func_stripname '-Wl,' '' "$arg"
+ 	args=$func_stripname_result
+ 	arg=
+@@ -9515,6 +9562,10 @@
+ 	  *) func_append new_libs " $deplib" ;;
+ 	  esac
+ 	  ;;
++	-Wl,*)
++	  func_stripname -Wl, '' "$deplib"
++	  func_append new_libs " $wl$func_stripname_result"
++	  ;;
+ 	*) func_append new_libs " $deplib" ;;
+ 	esac
+       done
+@@ -10247,6 +10298,10 @@
+ 	  *) func_append new_libs " $deplib" ;;
+ 	  esac
+ 	  ;;
++	-Wl,*)
++	  func_stripname -Wl, '' "$deplib"
++	  func_append new_libs " $wl$func_stripname_result"
++	  ;;
+ 	*) func_append new_libs " $deplib" ;;
+ 	esac
+       done
diff --git a/contrib/06ltmain_nag_pthread-patch/ltmain_nag_pthread.patch b/contrib/06ltmain_nag_pthread-patch/ltmain_nag_pthread.patch
index e6c618e15ecea2ddfab5dd465e50f8b30cd50323..ff79d7e555257db760a14c4cb59e5702bbc7bb8f 100644
--- a/contrib/06ltmain_nag_pthread-patch/ltmain_nag_pthread.patch
+++ b/contrib/06ltmain_nag_pthread-patch/ltmain_nag_pthread.patch
@@ -7,7 +7,7 @@
 +
 +	  # Additionally convert " -pthread" to " -Wl,-pthread" for nagfor
 +	  if test yes = "$with_nagfor" ; then
-+	    tmp_inherited_linker_flags=`$ECHO " $tmp_inherited_linker_flags" | $SED 's/ -pthread\b/ -Wl,-pthread/g'`
++	    tmp_inherited_linker_flags=`$ECHO " $tmp_inherited_linker_flags " | $SED 's/ -pthread[[:blank:]]/ -Wl,-pthread /g'`
 +	  fi
 +
  	  for tmp_inherited_linker_flag in $tmp_inherited_linker_flags; do
@@ -28,7 +28,7 @@
 +      # lines.
 +
 +      if test yes = "$with_nagfor" ; then
-+        new_inherited_linker_flags=`$ECHO " $new_inherited_linker_flags" | $SED 's/ -Wl,-pthread\b/ -pthread/g;s/^ *//'`
++        new_inherited_linker_flags=`$ECHO " $new_inherited_linker_flags " | $SED 's/ -Wl,-pthread / -pthread /g;s/^ *//;s/ *$//'`
 +      fi
 +
        # move library search paths that coincide with paths to not yet
diff --git a/doc/tex/c_ref.tex b/doc/tex/c_ref.tex
index d5b858f3d378b4cbbd6c126bbb4c932824460bd8..9e00ec8e279e5a9b1286fa5733f8385302f700f2 100644
--- a/doc/tex/c_ref.tex
+++ b/doc/tex/c_ref.tex
@@ -701,7 +701,7 @@ streamReadVar
 \fi
 }}
 \begin{verbatim}
-    void streamReadVar(int streamID, int varID, double *data, SizeType *nmiss);
+    void streamReadVar(int streamID, int varID, double *data, SizeType *numMissVals);
 \end{verbatim}
 
 Read a variable
@@ -716,7 +716,7 @@ streamReadVarF
 \fi
 }}
 \begin{verbatim}
-    void streamReadVar(int streamID, int varID, float *data, SizeType *nmiss);
+    void streamReadVar(int streamID, int varID, float *data, SizeType *numMissVals);
 \end{verbatim}
 
 Read a variable
@@ -732,7 +732,7 @@ streamReadVarSlice
 }}
 \begin{verbatim}
     void streamReadVarSlice(int streamID, int varID, int levelID, double *data, 
-                            SizeType *nmiss);
+                            SizeType *numMissVals);
 \end{verbatim}
 
 Read a horizontal slice of a variable
@@ -748,7 +748,7 @@ streamReadVarSliceF
 }}
 \begin{verbatim}
     void streamReadVarSliceF(int streamID, int varID, int levelID, float *data, 
-                             SizeType *nmiss);
+                             SizeType *numMissVals);
 \end{verbatim}
 
 Read a horizontal slice of a variable
@@ -763,7 +763,8 @@ streamWriteVar
 \fi
 }}
 \begin{verbatim}
-    void streamWriteVar(int streamID, int varID, const double *data, SizeType nmiss);
+    void streamWriteVar(int streamID, int varID, const double *data, 
+                        SizeType numMissVals);
 \end{verbatim}
 
 Write a variable
@@ -778,7 +779,8 @@ streamWriteVarF
 \fi
 }}
 \begin{verbatim}
-    void streamWriteVarF(int streamID, int varID, const float *data, SizeType nmiss);
+    void streamWriteVarF(int streamID, int varID, const float *data, 
+                         SizeType numMissVals);
 \end{verbatim}
 
 Write a variable
@@ -794,7 +796,7 @@ streamWriteVarSlice
 }}
 \begin{verbatim}
     void streamWriteVarSlice(int streamID, int varID, int levelID, const double *data, 
-                             SizeType nmiss);
+                             SizeType numMissVals);
 \end{verbatim}
 
 Write a horizontal slice of a variable
@@ -810,7 +812,7 @@ streamWriteVarSliceF
 }}
 \begin{verbatim}
     void streamWriteVarSliceF(int streamID, int varID, int levelID, const float *data, 
-                              SizeType nmiss);
+                              SizeType numMissVals);
 \end{verbatim}
 
 Write a horizontal slice of a variable
diff --git a/doc/tex/c_stream_read.tex b/doc/tex/c_stream_read.tex
index de2a92f365a889bcd96dc49bd29fcb4ccb165b37..6ab71620e9bd2c5a378ae234452e95e174842c2c 100644
--- a/doc/tex/c_stream_read.tex
+++ b/doc/tex/c_stream_read.tex
@@ -9,11 +9,11 @@ from an open dataset.
 \subsubsection*{Usage}
 
 \begin{verbatim}
-    void streamReadVar(int streamID, int varID, double *data, SizeType *nmiss);
+    void streamReadVar(int streamID, int varID, double *data, SizeType *numMissVals);
 \end{verbatim}
 
 \hspace*{4mm}\begin{minipage}[]{15cm}
-\begin{deflist}{\texttt{streamID}\ }
+\begin{deflist}{\texttt{numMissVals}\ }
 \item[\texttt{streamID}]
 Stream ID, from a previous call to {\htmlref{\texttt{streamOpenRead}}{streamOpenRead}}.
 \item[\texttt{varID}]
@@ -21,7 +21,7 @@ Variable identifier.
 \item[\texttt{data}]
 Pointer to the location into which the data values are read.
                      The caller must allocate space for the returned values.
-\item[\texttt{nmiss}]
+\item[\texttt{numMissVals}]
 Number of missing values.
 
 \end{deflist}
@@ -37,11 +37,11 @@ from an open dataset.
 \subsubsection*{Usage}
 
 \begin{verbatim}
-    void streamReadVar(int streamID, int varID, float *data, SizeType *nmiss);
+    void streamReadVar(int streamID, int varID, float *data, SizeType *numMissVals);
 \end{verbatim}
 
 \hspace*{4mm}\begin{minipage}[]{15cm}
-\begin{deflist}{\texttt{streamID}\ }
+\begin{deflist}{\texttt{numMissVals}\ }
 \item[\texttt{streamID}]
 Stream ID, from a previous call to {\htmlref{\texttt{streamOpenRead}}{streamOpenRead}}.
 \item[\texttt{varID}]
@@ -49,7 +49,7 @@ Variable identifier.
 \item[\texttt{data}]
 Pointer to the location into which the data values are read.
                      The caller must allocate space for the returned values.
-\item[\texttt{nmiss}]
+\item[\texttt{numMissVals}]
 Number of missing values.
 
 \end{deflist}
@@ -66,11 +66,11 @@ from an open dataset.
 
 \begin{verbatim}
     void streamReadVarSlice(int streamID, int varID, int levelID, double *data, 
-                            SizeType *nmiss);
+                            SizeType *numMissVals);
 \end{verbatim}
 
 \hspace*{4mm}\begin{minipage}[]{15cm}
-\begin{deflist}{\texttt{streamID}\ }
+\begin{deflist}{\texttt{numMissVals}\ }
 \item[\texttt{streamID}]
 Stream ID, from a previous call to {\htmlref{\texttt{streamOpenRead}}{streamOpenRead}}.
 \item[\texttt{varID}]
@@ -80,7 +80,7 @@ Level identifier.
 \item[\texttt{data}]
 Pointer to the location into which the data values are read.
                      The caller must allocate space for the returned values.
-\item[\texttt{nmiss}]
+\item[\texttt{numMissVals}]
 Number of missing values.
 
 \end{deflist}
@@ -97,11 +97,11 @@ from an open dataset.
 
 \begin{verbatim}
     void streamReadVarSliceF(int streamID, int varID, int levelID, float *data, 
-                             SizeType *nmiss);
+                             SizeType *numMissVals);
 \end{verbatim}
 
 \hspace*{4mm}\begin{minipage}[]{15cm}
-\begin{deflist}{\texttt{streamID}\ }
+\begin{deflist}{\texttt{numMissVals}\ }
 \item[\texttt{streamID}]
 Stream ID, from a previous call to {\htmlref{\texttt{streamOpenRead}}{streamOpenRead}}.
 \item[\texttt{varID}]
@@ -111,7 +111,7 @@ Level identifier.
 \item[\texttt{data}]
 Pointer to the location into which the data values are read.
                      The caller must allocate space for the returned values.
-\item[\texttt{nmiss}]
+\item[\texttt{numMissVals}]
 Number of missing values.
 
 \end{deflist}
diff --git a/doc/tex/c_stream_write.tex b/doc/tex/c_stream_write.tex
index 12c4ee9d516be190782d22b0030bee445282c6cd..8d7e37784ae7ca8fafd77f8b58d097fd56eebe85 100644
--- a/doc/tex/c_stream_write.tex
+++ b/doc/tex/c_stream_write.tex
@@ -9,18 +9,19 @@ The values are converted to the external data type of the variable, if necessary
 \subsubsection*{Usage}
 
 \begin{verbatim}
-    void streamWriteVar(int streamID, int varID, const double *data, SizeType nmiss);
+    void streamWriteVar(int streamID, int varID, const double *data, 
+                        SizeType numMissVals);
 \end{verbatim}
 
 \hspace*{4mm}\begin{minipage}[]{15cm}
-\begin{deflist}{\texttt{streamID}\ }
+\begin{deflist}{\texttt{numMissVals}\ }
 \item[\texttt{streamID}]
 Stream ID, from a previous call to {\htmlref{\texttt{streamOpenWrite}}{streamOpenWrite}}.
 \item[\texttt{varID}]
 Variable identifier.
 \item[\texttt{data}]
 Pointer to a block of double precision floating point data values to be written.
-\item[\texttt{nmiss}]
+\item[\texttt{numMissVals}]
 Number of missing values.
 
 \end{deflist}
@@ -36,18 +37,19 @@ The values are converted to the external data type of the variable, if necessary
 \subsubsection*{Usage}
 
 \begin{verbatim}
-    void streamWriteVarF(int streamID, int varID, const float *data, SizeType nmiss);
+    void streamWriteVarF(int streamID, int varID, const float *data, 
+                         SizeType numMissVals);
 \end{verbatim}
 
 \hspace*{4mm}\begin{minipage}[]{15cm}
-\begin{deflist}{\texttt{streamID}\ }
+\begin{deflist}{\texttt{numMissVals}\ }
 \item[\texttt{streamID}]
 Stream ID, from a previous call to {\htmlref{\texttt{streamOpenWrite}}{streamOpenWrite}}.
 \item[\texttt{varID}]
 Variable identifier.
 \item[\texttt{data}]
 Pointer to a block of single precision floating point data values to be written.
-\item[\texttt{nmiss}]
+\item[\texttt{numMissVals}]
 Number of missing values.
 
 \end{deflist}
@@ -64,11 +66,11 @@ The values are converted to the external data type of the variable, if necessary
 
 \begin{verbatim}
     void streamWriteVarSlice(int streamID, int varID, int levelID, const double *data, 
-                             SizeType nmiss);
+                             SizeType numMissVals);
 \end{verbatim}
 
 \hspace*{4mm}\begin{minipage}[]{15cm}
-\begin{deflist}{\texttt{streamID}\ }
+\begin{deflist}{\texttt{numMissVals}\ }
 \item[\texttt{streamID}]
 Stream ID, from a previous call to {\htmlref{\texttt{streamOpenWrite}}{streamOpenWrite}}.
 \item[\texttt{varID}]
@@ -77,7 +79,7 @@ Variable identifier.
 Level identifier.
 \item[\texttt{data}]
 Pointer to a block of double precision floating point data values to be written.
-\item[\texttt{nmiss}]
+\item[\texttt{numMissVals}]
 Number of missing values.
 
 \end{deflist}
@@ -94,11 +96,11 @@ The values are converted to the external data type of the variable, if necessary
 
 \begin{verbatim}
     void streamWriteVarSliceF(int streamID, int varID, int levelID, const float *data, 
-                              SizeType nmiss);
+                              SizeType numMissVals);
 \end{verbatim}
 
 \hspace*{4mm}\begin{minipage}[]{15cm}
-\begin{deflist}{\texttt{streamID}\ }
+\begin{deflist}{\texttt{numMissVals}\ }
 \item[\texttt{streamID}]
 Stream ID, from a previous call to {\htmlref{\texttt{streamOpenWrite}}{streamOpenWrite}}.
 \item[\texttt{varID}]
@@ -107,7 +109,7 @@ Variable identifier.
 Level identifier.
 \item[\texttt{data}]
 Pointer to a block of single precision floating point data values to be written.
-\item[\texttt{nmiss}]
+\item[\texttt{numMissVals}]
 Number of missing values.
 
 \end{deflist}
diff --git a/doc/tex/cdi_cman.tex b/doc/tex/cdi_cman.tex
index 308ae850222f8e911542cb8b9e9f4b9ab7db7e5a..d33aa7943e9840dc49d90f1803982a6c8cbc88d6 100644
--- a/doc/tex/cdi_cman.tex
+++ b/doc/tex/cdi_cman.tex
@@ -136,7 +136,7 @@
 \end{picture}
 
 \begin{flushright}
-{\large\bfseries Climate Data Interface \\ Version 2.3.0 \\ October 2023}
+{\large\bfseries Climate Data Interface \\ Version 2.4.0 \\ February 2024}
 \end{flushright}
 
 \vfill
diff --git a/doc/tex/cdi_fman.tex b/doc/tex/cdi_fman.tex
index 389fc4f2eb67ee961c7a432ccac0dee5b39511f7..ef3d9edbc92473b690ff9cb4b1a40cfd6379cae0 100644
--- a/doc/tex/cdi_fman.tex
+++ b/doc/tex/cdi_fman.tex
@@ -133,7 +133,7 @@
 \end{picture}
 
 \begin{flushright}
-{\large\bfseries Climate Data Interface \\ Version 2.3.0 \\ October 2023}
+{\large\bfseries Climate Data Interface \\ Version 2.4.0 \\ February 2024}
 \end{flushright}
 
 \vfill
diff --git a/doc/tex/f_ref.tex b/doc/tex/f_ref.tex
index 7f742955864c7f3c5a16b5755558dea8500f1b83..6c5302257bbd415e34b02aa52c508630c2a0d993 100644
--- a/doc/tex/f_ref.tex
+++ b/doc/tex/f_ref.tex
@@ -714,7 +714,7 @@ streamReadVar
 }}
 \begin{verbatim}
     SUBROUTINE streamReadVar(INTEGER streamID, INTEGER varID, REAL*8 data, 
-                             INTEGER nmiss)
+                             INTEGER numMissVals)
 \end{verbatim}
 
 Read a variable
@@ -730,7 +730,7 @@ streamReadVarF
 }}
 \begin{verbatim}
     SUBROUTINE streamReadVar(INTEGER streamID, INTEGER varID, REAL*4 data, 
-                             INTEGER nmiss)
+                             INTEGER numMissVals)
 \end{verbatim}
 
 Read a variable
@@ -746,7 +746,7 @@ streamReadVarSlice
 }}
 \begin{verbatim}
     SUBROUTINE streamReadVarSlice(INTEGER streamID, INTEGER varID, INTEGER levelID, 
-                                  REAL*8 data, INTEGER nmiss)
+                                  REAL*8 data, INTEGER numMissVals)
 \end{verbatim}
 
 Read a horizontal slice of a variable
@@ -762,7 +762,7 @@ streamReadVarSliceF
 }}
 \begin{verbatim}
     SUBROUTINE streamReadVarSliceF(INTEGER streamID, INTEGER varID, INTEGER levelID, 
-                                   REAL*4 data, INTEGER nmiss)
+                                   REAL*4 data, INTEGER numMissVals)
 \end{verbatim}
 
 Read a horizontal slice of a variable
@@ -778,7 +778,7 @@ streamWriteVar
 }}
 \begin{verbatim}
     SUBROUTINE streamWriteVar(INTEGER streamID, INTEGER varID, REAL*8 data, 
-                              INTEGER nmiss)
+                              INTEGER numMissVals)
 \end{verbatim}
 
 Write a variable
@@ -794,7 +794,7 @@ streamWriteVarF
 }}
 \begin{verbatim}
     SUBROUTINE streamWriteVarF(INTEGER streamID, INTEGER varID, REAL*4 data, 
-                               INTEGER nmiss)
+                               INTEGER numMissVals)
 \end{verbatim}
 
 Write a variable
@@ -810,7 +810,7 @@ streamWriteVarSlice
 }}
 \begin{verbatim}
     SUBROUTINE streamWriteVarSlice(INTEGER streamID, INTEGER varID, INTEGER levelID, 
-                                   REAL*8 data, INTEGER nmiss)
+                                   REAL*8 data, INTEGER numMissVals)
 \end{verbatim}
 
 Write a horizontal slice of a variable
@@ -826,7 +826,7 @@ streamWriteVarSliceF
 }}
 \begin{verbatim}
     SUBROUTINE streamWriteVarSliceF(INTEGER streamID, INTEGER varID, INTEGER levelID, 
-                                    REAL*4 data, INTEGER nmiss)
+                                    REAL*4 data, INTEGER numMissVals)
 \end{verbatim}
 
 Write a horizontal slice of a variable
diff --git a/doc/tex/f_stream_read.tex b/doc/tex/f_stream_read.tex
index 3885aaa1a0d7c0ffc529d6506049fc996d370bc4..fbb9671601c7be3bc61ac06a8b552e126dc698b9 100644
--- a/doc/tex/f_stream_read.tex
+++ b/doc/tex/f_stream_read.tex
@@ -10,11 +10,11 @@ from an open dataset.
 
 \begin{verbatim}
     SUBROUTINE streamReadVar(INTEGER streamID, INTEGER varID, REAL*8 data, 
-                             INTEGER nmiss)
+                             INTEGER numMissVals)
 \end{verbatim}
 
 \hspace*{4mm}\begin{minipage}[]{15cm}
-\begin{deflist}{\texttt{streamID}\ }
+\begin{deflist}{\texttt{numMissVals}\ }
 \item[\texttt{streamID}]
 Stream ID, from a previous call to {\htmlref{\texttt{streamOpenRead}}{streamOpenRead}}.
 \item[\texttt{varID}]
@@ -22,7 +22,7 @@ Variable identifier.
 \item[\texttt{data}]
 Pointer to the location into which the data values are read.
                      The caller must allocate space for the returned values.
-\item[\texttt{nmiss}]
+\item[\texttt{numMissVals}]
 Number of missing values.
 
 \end{deflist}
@@ -39,11 +39,11 @@ from an open dataset.
 
 \begin{verbatim}
     SUBROUTINE streamReadVar(INTEGER streamID, INTEGER varID, REAL*4 data, 
-                             INTEGER nmiss)
+                             INTEGER numMissVals)
 \end{verbatim}
 
 \hspace*{4mm}\begin{minipage}[]{15cm}
-\begin{deflist}{\texttt{streamID}\ }
+\begin{deflist}{\texttt{numMissVals}\ }
 \item[\texttt{streamID}]
 Stream ID, from a previous call to {\htmlref{\texttt{streamOpenRead}}{streamOpenRead}}.
 \item[\texttt{varID}]
@@ -51,7 +51,7 @@ Variable identifier.
 \item[\texttt{data}]
 Pointer to the location into which the data values are read.
                      The caller must allocate space for the returned values.
-\item[\texttt{nmiss}]
+\item[\texttt{numMissVals}]
 Number of missing values.
 
 \end{deflist}
@@ -68,11 +68,11 @@ from an open dataset.
 
 \begin{verbatim}
     SUBROUTINE streamReadVarSlice(INTEGER streamID, INTEGER varID, INTEGER levelID, 
-                                  REAL*8 data, INTEGER nmiss)
+                                  REAL*8 data, INTEGER numMissVals)
 \end{verbatim}
 
 \hspace*{4mm}\begin{minipage}[]{15cm}
-\begin{deflist}{\texttt{streamID}\ }
+\begin{deflist}{\texttt{numMissVals}\ }
 \item[\texttt{streamID}]
 Stream ID, from a previous call to {\htmlref{\texttt{streamOpenRead}}{streamOpenRead}}.
 \item[\texttt{varID}]
@@ -82,7 +82,7 @@ Level identifier.
 \item[\texttt{data}]
 Pointer to the location into which the data values are read.
                      The caller must allocate space for the returned values.
-\item[\texttt{nmiss}]
+\item[\texttt{numMissVals}]
 Number of missing values.
 
 \end{deflist}
@@ -99,11 +99,11 @@ from an open dataset.
 
 \begin{verbatim}
     SUBROUTINE streamReadVarSliceF(INTEGER streamID, INTEGER varID, INTEGER levelID, 
-                                   REAL*4 data, INTEGER nmiss)
+                                   REAL*4 data, INTEGER numMissVals)
 \end{verbatim}
 
 \hspace*{4mm}\begin{minipage}[]{15cm}
-\begin{deflist}{\texttt{streamID}\ }
+\begin{deflist}{\texttt{numMissVals}\ }
 \item[\texttt{streamID}]
 Stream ID, from a previous call to {\htmlref{\texttt{streamOpenRead}}{streamOpenRead}}.
 \item[\texttt{varID}]
@@ -113,7 +113,7 @@ Level identifier.
 \item[\texttt{data}]
 Pointer to the location into which the data values are read.
                      The caller must allocate space for the returned values.
-\item[\texttt{nmiss}]
+\item[\texttt{numMissVals}]
 Number of missing values.
 
 \end{deflist}
diff --git a/doc/tex/f_stream_write.tex b/doc/tex/f_stream_write.tex
index 0f798e1cd45e7a52dc6c25fd50c265bfed11618a..2be68c835dcd9ac98a590585dee6cbe9a02af18d 100644
--- a/doc/tex/f_stream_write.tex
+++ b/doc/tex/f_stream_write.tex
@@ -10,18 +10,18 @@ The values are converted to the external data type of the variable, if necessary
 
 \begin{verbatim}
     SUBROUTINE streamWriteVar(INTEGER streamID, INTEGER varID, REAL*8 data, 
-                              INTEGER nmiss)
+                              INTEGER numMissVals)
 \end{verbatim}
 
 \hspace*{4mm}\begin{minipage}[]{15cm}
-\begin{deflist}{\texttt{streamID}\ }
+\begin{deflist}{\texttt{numMissVals}\ }
 \item[\texttt{streamID}]
 Stream ID, from a previous call to {\htmlref{\texttt{streamOpenWrite}}{streamOpenWrite}}.
 \item[\texttt{varID}]
 Variable identifier.
 \item[\texttt{data}]
 Pointer to a block of double precision floating point data values to be written.
-\item[\texttt{nmiss}]
+\item[\texttt{numMissVals}]
 Number of missing values.
 
 \end{deflist}
@@ -38,18 +38,18 @@ The values are converted to the external data type of the variable, if necessary
 
 \begin{verbatim}
     SUBROUTINE streamWriteVarF(INTEGER streamID, INTEGER varID, REAL*4 data, 
-                               INTEGER nmiss)
+                               INTEGER numMissVals)
 \end{verbatim}
 
 \hspace*{4mm}\begin{minipage}[]{15cm}
-\begin{deflist}{\texttt{streamID}\ }
+\begin{deflist}{\texttt{numMissVals}\ }
 \item[\texttt{streamID}]
 Stream ID, from a previous call to {\htmlref{\texttt{streamOpenWrite}}{streamOpenWrite}}.
 \item[\texttt{varID}]
 Variable identifier.
 \item[\texttt{data}]
 Pointer to a block of single precision floating point data values to be written.
-\item[\texttt{nmiss}]
+\item[\texttt{numMissVals}]
 Number of missing values.
 
 \end{deflist}
@@ -66,11 +66,11 @@ The values are converted to the external data type of the variable, if necessary
 
 \begin{verbatim}
     SUBROUTINE streamWriteVarSlice(INTEGER streamID, INTEGER varID, INTEGER levelID, 
-                                   REAL*8 data, INTEGER nmiss)
+                                   REAL*8 data, INTEGER numMissVals)
 \end{verbatim}
 
 \hspace*{4mm}\begin{minipage}[]{15cm}
-\begin{deflist}{\texttt{streamID}\ }
+\begin{deflist}{\texttt{numMissVals}\ }
 \item[\texttt{streamID}]
 Stream ID, from a previous call to {\htmlref{\texttt{streamOpenWrite}}{streamOpenWrite}}.
 \item[\texttt{varID}]
@@ -79,7 +79,7 @@ Variable identifier.
 Level identifier.
 \item[\texttt{data}]
 Pointer to a block of double precision floating point data values to be written.
-\item[\texttt{nmiss}]
+\item[\texttt{numMissVals}]
 Number of missing values.
 
 \end{deflist}
@@ -96,11 +96,11 @@ The values are converted to the external data type of the variable, if necessary
 
 \begin{verbatim}
     SUBROUTINE streamWriteVarSliceF(INTEGER streamID, INTEGER varID, INTEGER levelID, 
-                                    REAL*4 data, INTEGER nmiss)
+                                    REAL*4 data, INTEGER numMissVals)
 \end{verbatim}
 
 \hspace*{4mm}\begin{minipage}[]{15cm}
-\begin{deflist}{\texttt{streamID}\ }
+\begin{deflist}{\texttt{numMissVals}\ }
 \item[\texttt{streamID}]
 Stream ID, from a previous call to {\htmlref{\texttt{streamOpenWrite}}{streamOpenWrite}}.
 \item[\texttt{varID}]
@@ -109,7 +109,7 @@ Variable identifier.
 Level identifier.
 \item[\texttt{data}]
 Pointer to a block of single precision floating point data values to be written.
-\item[\texttt{nmiss}]
+\item[\texttt{numMissVals}]
 Number of missing values.
 
 \end{deflist}
diff --git a/examples/cdi_append.c b/examples/cdi_append.c
index 31719366f4e9767b8554083e290534d135116f5b..b583f99929afa24969c82007e82f44db3e27e7af 100644
--- a/examples/cdi_append.c
+++ b/examples/cdi_append.c
@@ -11,7 +11,7 @@ main(void)
     nlev = 5,   // Number of levels
     nts = 3,    // Number of time steps
   };
-  SizeType nmiss = 0;
+  SizeType numMissVals = 0;
   double var1[nlon * nlat];
   double var2[nlon * nlat * nlev];
 
@@ -70,8 +70,8 @@ main(void)
       for (size_t i = 0; i < nlon * nlat * nlev; i++) var2[i] = 2.2;
 
       // Write var1 and var2
-      streamWriteVar(streamID, varID1, var1, nmiss);
-      streamWriteVar(streamID, varID2, var2, nmiss);
+      streamWriteVar(streamID, varID1, var1, numMissVals);
+      streamWriteVar(streamID, varID2, var2, numMissVals);
     }
 
   // Close the output stream
diff --git a/examples/cdi_copy.c b/examples/cdi_copy.c
index 33c3dee245799cea0800f22964a2afa2e9963bc2..14ae4a55a540d9ada59496c459dbc377cad854ca 100644
--- a/examples/cdi_copy.c
+++ b/examples/cdi_copy.c
@@ -5,7 +5,6 @@
 int
 main(void)
 {
-
   // Open the input dataset
   int streamID1 = streamOpenRead("example.nc");
   if (streamID1 < 0)
@@ -17,11 +16,11 @@ main(void)
   // Get the variable list of the dataset
   int vlistID1 = streamInqVlist(streamID1);
 
-  int nvars = vlistNvars(vlistID1);
+  int numVars = vlistNvars(vlistID1);
 
   int varDataSize = 0;
   double *varData = NULL;
-  for (int varID = 0; varID < nvars; ++varID)
+  for (int varID = 0; varID < numVars; ++varID)
     {
       int varSize = vlistInqVarSize(vlistID1, varID);
       varDataSize = varSize > varDataSize ? varSize : varDataSize;
@@ -52,13 +51,13 @@ main(void)
       // Define the output time step
       streamDefTimestep(streamID2, tsID);
 
-      for (int varID = 0; varID < nvars; ++varID)
+      for (int varID = 0; varID < numVars; ++varID)
         {
-          SizeType nmiss;
+          SizeType numMissVals;
           // Read var
-          streamReadVar(streamID1, varID, varData, &nmiss);
+          streamReadVar(streamID1, varID, varData, &numMissVals);
           // Write var
-          streamWriteVar(streamID2, varID, varData, nmiss);
+          streamWriteVar(streamID2, varID, varData, numMissVals);
         }
       ++tsID;
     }
diff --git a/examples/cdi_copy_file.c b/examples/cdi_copy_file.c
index 7cd915ee2287becb4cbdc64804c476d68b21619a..4f26a2868e95380b8b215e3487ae76c7908d30b7 100644
--- a/examples/cdi_copy_file.c
+++ b/examples/cdi_copy_file.c
@@ -12,7 +12,7 @@ main(int argc, char *argv[])
 {
   int linfo = 0;
   int vlistID1, vlistID2, varID, streamID1, streamID2;
-  size_t nmiss;
+  size_t numMissVals;
   int nvars, status;
   double *vardata = NULL;
   const char *ifile, *ofile;
@@ -63,7 +63,7 @@ main(int argc, char *argv[])
       for (varID = 0; varID < nvars; ++varID)
         {
           /* Read variable  */
-          streamReadVar(streamID1, varID, vardata, &nmiss);
+          streamReadVar(streamID1, varID, vardata, &numMissVals);
 
           if (linfo)
             {
@@ -84,7 +84,7 @@ main(int argc, char *argv[])
             }
 
           /* Write variable */
-          streamWriteVar(streamID2, varID, vardata, nmiss);
+          streamWriteVar(streamID2, varID, vardata, numMissVals);
         }
 
       tsID++;
diff --git a/examples/cdi_read.c b/examples/cdi_read.c
index f957f23476a27d155b51667e49db2a79355eae5f..13fc6e9273c1dc160263f2227a3f38a7c6b0ca38 100644
--- a/examples/cdi_read.c
+++ b/examples/cdi_read.c
@@ -11,7 +11,7 @@ main(void)
     nlev = 5,   // Number of levels
     nts = 3,    // Number of time steps
   };
-  SizeType nmiss;
+  SizeType numMissVals;
   double var1[nlon * nlat];
   double var2[nlon * nlat * nlev];
 
@@ -45,8 +45,8 @@ main(void)
       printf("read timestep %d:  date=%d  time=%d\n", tsID + 1, vdate, vtime);
 
       // Read var1 and var2
-      streamReadVar(streamID, varID1, var1, &nmiss);
-      streamReadVar(streamID, varID2, var2, &nmiss);
+      streamReadVar(streamID, varID1, var1, &numMissVals);
+      streamReadVar(streamID, varID2, var2, &numMissVals);
     }
 
   // Close the input stream
diff --git a/examples/cdi_read_atts.c b/examples/cdi_read_atts.c
index f37f6c9e8c5e4550402acc5dac219d678f856db0..89cb1717dbd7700e5c71de79f7716dd1966aa4b3 100644
--- a/examples/cdi_read_atts.c
+++ b/examples/cdi_read_atts.c
@@ -10,7 +10,7 @@ int
 main(void)
 {
   int taxisID, vlistID, varID1, varID2, streamID;
-  size_t nmiss;
+  size_t numMissVals;
   double var1[nlon * nlat];
   double var2[nlon * nlat * nlev];
 
@@ -63,8 +63,8 @@ main(void)
       int vtime = taxisInqVtime(taxisID);
 
       // Read var1 and var2
-      streamReadVar(streamID, varID1, var1, &nmiss);
-      streamReadVar(streamID, varID2, var2, &nmiss);
+      streamReadVar(streamID, varID1, var1, &numMissVals);
+      streamReadVar(streamID, varID2, var2, &numMissVals);
     }
 
   // Close the input stream
diff --git a/examples/cdi_read_float.c b/examples/cdi_read_float.c
index 41267501d2244786bad447fe3ebac1c552fdd184..26914a7e39f6aef284f3668b567628c9e11c6916 100644
--- a/examples/cdi_read_float.c
+++ b/examples/cdi_read_float.c
@@ -10,7 +10,7 @@ int
 main(void)
 {
   int taxisID, vlistID, varID1, varID2, streamID;
-  size_t nmiss;
+  size_t numMissVals;
   float var1[nlon * nlat];
   float var2[nlon * nlat * nlev];
 
@@ -43,8 +43,8 @@ main(void)
       int vtime = taxisInqVtime(taxisID);
 
       // Read var1 and var2
-      streamReadVarF(streamID, varID1, var1, &nmiss);
-      streamReadVarF(streamID, varID2, var2, &nmiss);
+      streamReadVarF(streamID, varID1, var1, &numMissVals);
+      streamReadVarF(streamID, varID2, var2, &numMissVals);
       printf("tsID %d %d %d %g %g\n", tsID, vdate, vtime, var1[0], var2[0]);
     }
 
diff --git a/examples/cdi_read_records.c b/examples/cdi_read_records.c
index af891d07113e0259f601519d56f70eacba8f2eb7..74944638c280fb5c78b633959b36efa83cbf63ff 100644
--- a/examples/cdi_read_records.c
+++ b/examples/cdi_read_records.c
@@ -17,14 +17,14 @@ typedef struct
 {
   int streamID;
   int *varID, *levelID;
-  size_t *nmiss;
+  size_t *numMissVals;
   double *array;
 } read_arg_t;
 
 typedef struct
 {
   int varID, levelID;
-  size_t nmiss;
+  size_t numMissVals;
   double *array;
   int array_size;
   int recID, nrecs;
@@ -41,7 +41,7 @@ typedef struct work_st
 typedef struct
 {
   int varID, levelID;
-  size_t nmiss;
+  size_t numMissVals;
   double *array;
   int array_size;
   int recID, nrecs;
@@ -63,17 +63,17 @@ readRecord(void *arg)
   read_arg_t *read_arg = (read_arg_t *) arg;
   int streamID;
   int *varID, *levelID;
-  size_t *nmiss;
+  size_t *numMissVals;
   double *array;
 
   streamID = read_arg->streamID;
   varID = read_arg->varID;
   levelID = read_arg->levelID;
-  nmiss = read_arg->nmiss;
+  numMissVals = read_arg->numMissVals;
   array = read_arg->array;
 
   streamInqRecord(streamID, varID, levelID);
-  streamReadRecord(streamID, array, nmiss);
+  streamReadRecord(streamID, array, numMissVals);
 }
 
 #if defined(HAVE_LIBPTHREAD)
@@ -253,7 +253,7 @@ destroy_iothread(void *p)
 #endif
 
 void
-stream_read_record_par(int streamID, int *varID, int *levelID, double *array, size_t *nmiss, par_io_t *parIO)
+stream_read_record_par(int streamID, int *varID, int *levelID, double *array, size_t *numMissVals, par_io_t *parIO)
 {
   int lpario = 0;
   int recID = 0, nrecs = 0;
@@ -273,7 +273,7 @@ stream_read_record_par(int streamID, int *varID, int *levelID, double *array, si
       read_arg.streamID = streamID;
       read_arg.varID = varID;
       read_arg.levelID = levelID;
-      read_arg.nmiss = nmiss;
+      read_arg.numMissVals = numMissVals;
       read_arg.array = array;
 
       readRecord(&read_arg);
@@ -285,7 +285,7 @@ stream_read_record_par(int streamID, int *varID, int *levelID, double *array, si
 
       *varID = parIO->varID;
       *levelID = parIO->levelID;
-      *nmiss = parIO->nmiss;
+      *numMissVals = parIO->numMissVals;
 
       memcpy(array, parIO->array, parIO->array_size * sizeof(double));
     }
@@ -301,7 +301,7 @@ stream_read_record_par(int streamID, int *varID, int *levelID, double *array, si
           read_arg->streamID = streamID;
           read_arg->varID = &parIO->varID;
           read_arg->levelID = &parIO->levelID;
-          read_arg->nmiss = &parIO->nmiss;
+          read_arg->numMissVals = &parIO->numMissVals;
           read_arg->array = parIO->array;
 
           dispatch(parIO->iothread, readRecord, read_arg);
@@ -315,17 +315,17 @@ stream_read_record_par(int streamID, int *varID, int *levelID, double *array, si
 }
 
 void
-stream_read_record(int streamID, int *varID, int *levelID, double *data, size_t *nmiss)
+stream_read_record(int streamID, int *varID, int *levelID, double *data, size_t *numMissVals)
 {
   streamInqRecord(streamID, varID, levelID);
-  streamReadRecord(streamID, data, nmiss);
+  streamReadRecord(streamID, data, numMissVals);
 }
 
 int
 main(int argc, char *argv[])
 {
   int taxisID, vlistID, varID, levelID, streamID, tsID;
-  size_t nmiss;
+  size_t numMissVals;
   int vdate, vtime;
   int nrecs, recID, code;
   int gridsize, i;
@@ -377,9 +377,9 @@ main(int argc, char *argv[])
           parIO.recID = recID;
           parIO.nrecs = nrecs;
 #ifdef PARIO
-          stream_read_record_par(streamID, &varID, &levelID, data, &nmiss, &parIO);
+          stream_read_record_par(streamID, &varID, &levelID, data, &numMissVals, &parIO);
 #else
-          stream_read_record(streamID, &varID, &levelID, data, &nmiss);
+          stream_read_record(streamID, &varID, &levelID, data, &numMissVals);
 #endif
           code = vlistInqVarCode(vlistID, varID);
           gridsize = gridInqSize(vlistInqVarGrid(vlistID, varID));
diff --git a/examples/cdi_read_time.c b/examples/cdi_read_time.c
index d88c8bd88fced1e9662ee001cbedca424ec41d33..1058cee59ca5b58dc23be9c10d22c6200aad1845 100644
--- a/examples/cdi_read_time.c
+++ b/examples/cdi_read_time.c
@@ -8,7 +8,7 @@ main(void)
   const int nlat = 6;   // Number of latitudes
   const int nlev = 5;   // Number of levels
   const int nts = 20;   // Number of time steps
-  size_t nmiss;
+  size_t numMissVals;
   double var1[nlon * nlat];
   double var2[nlon * nlat * nlev];
 
@@ -42,8 +42,8 @@ main(void)
       printf("read timestep %d:  date=%d  time=%d\n", tsID + 1, vdate, vtime);
 
       // Read var1 and var2
-      // streamReadVar(streamID, varID1, var1, &nmiss);
-      // streamReadVar(streamID, varID2, var2, &nmiss);
+      // streamReadVar(streamID, varID1, var1, &numMissVals);
+      // streamReadVar(streamID, varID2, var2, &numMissVals);
     }
 
   // Close the input stream
diff --git a/examples/cdi_write.c b/examples/cdi_write.c
index deceff1e94f790a0e74f0dfd69f3dbeba4e3797a..3426e112dc8877ee6d65e22c85e4b2e9972a14cd 100644
--- a/examples/cdi_write.c
+++ b/examples/cdi_write.c
@@ -11,10 +11,10 @@ main(void)
     nlev = 5,   // Number of levels
     nts = 3,    // Number of time steps
   };
-  SizeType nmiss = 0;
+  SizeType numMissVals = 0;
   double lons[] = { 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330 };
   double lats[] = { -75, -45, -15, 15, 45, 75 };
-  double levs[] = { 101300, 92500, 85000, 50000, 20000 };
+  double levels[] = { 101300, 92500, 85000, 50000, 20000 };
   double var1[nlon * nlat];
   double var2[nlon * nlat * nlev];
 
@@ -30,7 +30,7 @@ main(void)
 
   // Create a pressure level Z-axis
   int zaxisID2 = zaxisCreate(ZAXIS_PRESSURE, nlev);
-  zaxisDefLevels(zaxisID2, levs);
+  zaxisDefLevels(zaxisID2, levels);
 
   // Create a variable list
   int vlistID = vlistCreate();
@@ -75,8 +75,8 @@ main(void)
       for (size_t i = 0; i < nlon * nlat * nlev; i++) var2[i] = 2.2;
 
       // Write var1 and var2
-      streamWriteVar(streamID, varID1, var1, nmiss);
-      streamWriteVar(streamID, varID2, var2, nmiss);
+      streamWriteVar(streamID, varID1, var1, numMissVals);
+      streamWriteVar(streamID, varID2, var2, numMissVals);
     }
 
   // Close the output stream
diff --git a/examples/cdi_write_const.c b/examples/cdi_write_const.c
index 920dac182434471342a16c14355ce3530bbcb929..d5d42156faac22c2d4ec165049c2d794042b1092 100644
--- a/examples/cdi_write_const.c
+++ b/examples/cdi_write_const.c
@@ -10,7 +10,7 @@ main(void)
 {
   int gridID, zaxisID1, zaxisID2;
   int vlistID, varID1, varID2, streamID;
-  size_t i, nmiss = 0;
+  size_t i, numMissVals = 0;
   double lons[nlon] = { 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330 };
   double lats[nlat] = { -75, -45, -15, 15, 45, 75 };
   double levs[nlev] = { 101300, 92500, 85000, 50000, 20000 };
@@ -71,8 +71,8 @@ main(void)
   for (i = 0; i < nlon * nlat * nlev; i++) var2[i] = 2.2;
 
   // Write var1 and var2
-  streamWriteVar(streamID, varID1, var1, nmiss);
-  streamWriteVar(streamID, varID2, var2, nmiss);
+  streamWriteVar(streamID, varID1, var1, numMissVals);
+  streamWriteVar(streamID, varID2, var2, numMissVals);
 
   // Close the output stream
   streamClose(streamID);
diff --git a/examples/cdi_write_curv_rll.c b/examples/cdi_write_curv_rll.c
index b4d5984aa1e8ade1a2f1cf368983764ac1f48522..1699020bbe7f7ce80550757597d8789a3c208551 100644
--- a/examples/cdi_write_curv_rll.c
+++ b/examples/cdi_write_curv_rll.c
@@ -8,7 +8,7 @@ main(void)
   const int nlat = 6;   // Number of latitudes
   const int nlev = 5;   // Number of levels
   const int nts = 3;    // Number of time steps
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
   double levs[] = { 101300, 92500, 85000, 50000, 20000 };
   double var1[nlon * nlat];
   double var2[nlon * nlat * nlev];
@@ -105,8 +105,8 @@ main(void)
       for (size_t i = 0; i < nlon * nlat * nlev; i++) var2[i] = 2.2;
 
       // Write var1 and var2
-      streamWriteVar(streamID, varID1, var1, nmiss);
-      streamWriteVar(streamID, varID2, var2, nmiss);
+      streamWriteVar(streamID, varID1, var1, numMissVals);
+      streamWriteVar(streamID, varID2, var2, numMissVals);
     }
 
   // Close the output stream
diff --git a/examples/cdi_write_dbl.c b/examples/cdi_write_dbl.c
index 9a48e7bd2cf775ed9f15802877a36a6de4d29e90..6eb4a4e2107e64d0f34530980e384c6a3f5f46c1 100644
--- a/examples/cdi_write_dbl.c
+++ b/examples/cdi_write_dbl.c
@@ -11,7 +11,7 @@ main(void)
 {
   int gridID, zaxisID1, zaxisID2, taxisID;
   int vlistID, varID1, varID2, streamID, tsID;
-  size_t i, nmiss = 0;
+  size_t i, numMissVals = 0;
   double lons[nlon] = { 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330 };
   double lats[nlat] = { -75, -45, -15, 15, 45, 75 };
   double levs[nlev] = { 5, 10, 50, 100, 500 };
@@ -76,8 +76,8 @@ main(void)
       for (i = 0; i < nlon * nlat * nlev; i++) var2[i] = 2.2;
 
       // Write var1 and var2
-      streamWriteVar(streamID, varID1, var1, nmiss);
-      streamWriteVar(streamID, varID2, var2, nmiss);
+      streamWriteVar(streamID, varID1, var1, numMissVals);
+      streamWriteVar(streamID, varID2, var2, numMissVals);
     }
 
   // Close the output stream
diff --git a/examples/cdi_write_ens.c b/examples/cdi_write_ens.c
index 1db9d139386e3a12f5fd83308f1ef3e4bd496aa2..cabeeda8b47cf90c9e4ebd644435c023e3ce7447 100644
--- a/examples/cdi_write_ens.c
+++ b/examples/cdi_write_ens.c
@@ -19,7 +19,7 @@ main(void)
   int tsID;
   int levelID;
   int vlistID, taxisID;
-  SizeType nmiss;
+  SizeType numMissVals;
 
   int instID;
 
@@ -111,7 +111,7 @@ main(void)
 
       for (varID = 0; varID < nvars; varID++)
         {
-          streamReadVar(streamID1, varID, data, &nmiss);
+          streamReadVar(streamID1, varID, data, &numMissVals);
 
           /* int code     =  */ vlistInqVarCode(vlistID, varID);
           gridID = vlistInqVarGrid(vlistID, varID);
@@ -127,7 +127,7 @@ main(void)
               /* int offset = gridsize*levelID; */
             }
 
-          streamWriteVar(streamID2, varID, data, nmiss);
+          streamWriteVar(streamID2, varID, data, numMissVals);
         }
       tsID++;
     }
diff --git a/examples/cdi_write_float.c b/examples/cdi_write_float.c
index 6ffbd1b4a5ee6910de4201e7eb6d62d45dfff38d..75d9e904b0152511b9f96641b4132f6fb6f500dc 100644
--- a/examples/cdi_write_float.c
+++ b/examples/cdi_write_float.c
@@ -11,7 +11,7 @@ main(void)
 {
   int gridID, zaxisID1, zaxisID2, taxisID;
   int vlistID, varID1, varID2, streamID, tsID;
-  size_t i, nmiss = 0;
+  size_t i, numMissVals = 0;
   double lons[nlon] = { 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330 };
   double lats[nlat] = { -75, -45, -15, 15, 45, 75 };
   double levs[nlev] = { 101300, 92500, 85000, 50000, 20000 };
@@ -75,8 +75,8 @@ main(void)
       for (i = 0; i < nlon * nlat * nlev; i++) var2[i] = 2.2;
 
       // Write var1 and var2
-      streamWriteVarF(streamID, varID1, var1, nmiss);
-      streamWriteVarF(streamID, varID2, var2, nmiss);
+      streamWriteVarF(streamID, varID1, var1, numMissVals);
+      streamWriteVarF(streamID, varID2, var2, numMissVals);
     }
 
   // Close the output stream
diff --git a/examples/cdi_write_forecast.c b/examples/cdi_write_forecast.c
index 658d8b5ce44f63d02be7750312ae78db586b5aa9..19acbfbfd81728073ee3e987928c0af83a624f6d 100644
--- a/examples/cdi_write_forecast.c
+++ b/examples/cdi_write_forecast.c
@@ -11,7 +11,7 @@ main(void)
 {
   int gridID, zaxisID1, zaxisID2, taxisID;
   int vlistID, varID1, varID2, streamID, tsID;
-  size_t i, nmiss = 0;
+  size_t i, numMissVals = 0;
   double lons[nlon] = { 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330 };
   double lats[nlat] = { -75, -45, -15, 15, 45, 75 };
   double levs[nlev] = { 101300 };
@@ -81,8 +81,8 @@ main(void)
       for (i = 0; i < nlon * nlat * nlev; i++) var2[i] = 2.2;
 
       // Write var1 and var2
-      streamWriteVar(streamID, varID1, var1, nmiss);
-      streamWriteVar(streamID, varID2, var2, nmiss);
+      streamWriteVar(streamID, varID1, var1, numMissVals);
+      streamWriteVar(streamID, varID2, var2, numMissVals);
     }
 
   // Close the output stream
diff --git a/examples/cdi_write_gribkey.c b/examples/cdi_write_gribkey.c
index be992754d8c07e2be23bec22d7543aa1fa3b46a0..32ec6daaa83ba21ed5c819e4f8d809b8855bdd73 100644
--- a/examples/cdi_write_gribkey.c
+++ b/examples/cdi_write_gribkey.c
@@ -11,7 +11,7 @@ main(void)
 {
   int gridID, zaxisID1, zaxisID2, taxisID;
   int vlistID, varID1, varID2, streamID;
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
   double lons[nlon] = { 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330 };
   double lats[nlat] = { -75, -45, -15, 15, 45, 75 };
   double levs[nlev] = { 101300, 92500, 85000, 50000, 20000 };
@@ -83,8 +83,8 @@ main(void)
       for (size_t i = 0; i < nlon * nlat * nlev; i++) var2[i] = 2.2;
 
       // Write var1 and var2
-      streamWriteVar(streamID, varID1, var1, nmiss);
-      streamWriteVar(streamID, varID2, var2, nmiss);
+      streamWriteVar(streamID, varID1, var1, numMissVals);
+      streamWriteVar(streamID, varID2, var2, numMissVals);
     }
 
   // Close the output stream
diff --git a/examples/cdi_write_hybrid.c b/examples/cdi_write_hybrid.c
index 5437f12e5fec9786ee4ab81d4c07889ac3aa6894..4488f059543eab16d978acdd15d5bed5e3cc1dc8 100644
--- a/examples/cdi_write_hybrid.c
+++ b/examples/cdi_write_hybrid.c
@@ -12,7 +12,7 @@ main(void)
 {
   int gridID, zaxisID1, zaxisID2, zaxisID3, zaxisID4, taxisID;
   int vlistID, varID1, varID2, varID3, varID4, streamID;
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
   double lons[nlon] = { 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330 };
   double lats[nlat] = { -75, -45, -15, 15, 45, 75 };
   double levs[nhlev] = { 1, 2, 3, 4, 5, 6 };
@@ -102,10 +102,10 @@ main(void)
         for (size_t i = 0; i < nlon * nlat; ++i) var4[i + k * nlon * nlat] = 100 + k;
 
       // Write var1 and var2
-      streamWriteVar(streamID, varID1, var1, nmiss);
-      streamWriteVar(streamID, varID2, var2, nmiss);
-      streamWriteVar(streamID, varID3, var3, nmiss);
-      streamWriteVar(streamID, varID4, var4, nmiss);
+      streamWriteVar(streamID, varID1, var1, numMissVals);
+      streamWriteVar(streamID, varID2, var2, numMissVals);
+      streamWriteVar(streamID, varID3, var3, numMissVals);
+      streamWriteVar(streamID, varID4, var4, numMissVals);
     }
 
   // Close the output stream
diff --git a/examples/cdi_write_local.c b/examples/cdi_write_local.c
index 35d88f92920fa08bb82e878e353b276eeb0a1c27..475ec0294b67e04a8cce1cc2a3386a732ebf20dc 100644
--- a/examples/cdi_write_local.c
+++ b/examples/cdi_write_local.c
@@ -11,7 +11,7 @@ main(void)
 {
   int gridID, zaxisID1, zaxisID2, taxisID;
   int vlistID, varID1, varID2, streamID;
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
   double lons[nlon] = { 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330 };
   double lats[nlat] = { -75, -45, -15, 15, 45, 75 };
   double levs[nlev] = { 101300, 92500, 85000, 50000, 20000 };
@@ -86,8 +86,8 @@ main(void)
       for (size_t i = 0; i < nlon * nlat * nlev; i++) var2[i] = 2.2;
 
       // Write var1 and var2
-      streamWriteVar(streamID, varID1, var1, nmiss);
-      streamWriteVar(streamID, varID2, var2, nmiss);
+      streamWriteVar(streamID, varID1, var1, numMissVals);
+      streamWriteVar(streamID, varID2, var2, numMissVals);
     }
 
   // Close the output stream
diff --git a/examples/cdi_write_pl.c b/examples/cdi_write_pl.c
index 4a3ff88a9e8fd40caab1595f14987b7bfb47eb24..f05b66ec58a95510492087d2d3ceda6219e6059e 100644
--- a/examples/cdi_write_pl.c
+++ b/examples/cdi_write_pl.c
@@ -11,7 +11,7 @@ main(void)
 {
   int gridID, zaxisID1, zaxisID2, taxisID;
   int vlistID, varID1, varID2, streamID;
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
   double lons[nlon] = { 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330 };
   double lats[nlat] = { -75, -45, -15, 15, 45, 75 };
   double levs[nlev] = { 100000.1, 10000.2, 1000.38, 100.4, 10.5 };
@@ -79,8 +79,8 @@ main(void)
         for (i = 0; i < nlon * nlat; i++) var2[i + k * nlon * nlat] = 2.2 + k;
 
       // Write var1 and var2
-      streamWriteVar(streamID, varID1, var1, nmiss);
-      streamWriteVar(streamID, varID2, var2, nmiss);
+      streamWriteVar(streamID, varID1, var1, numMissVals);
+      streamWriteVar(streamID, varID2, var2, numMissVals);
     }
 
   // Close the output stream
diff --git a/examples/cdi_write_point.c b/examples/cdi_write_point.c
index 84d9cdc72733d0660d3b1b19e7dd3d2fe272ade3..c4aa3e8576b96db03fc6b4506a5162bf058cc76d 100644
--- a/examples/cdi_write_point.c
+++ b/examples/cdi_write_point.c
@@ -10,7 +10,7 @@ main(void)
 {
   int gridID, zaxisID1, zaxisID2, taxisID;
   int vlistID, varID1, varID2, streamID;
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
   double levs[nlev] = { 101300, 92500, 85000, 50000, 20000 };
   double var1;
   double var2[nlev];
@@ -68,8 +68,8 @@ main(void)
       for (size_t i = 0; i < nlev; i++) var2[i] = 2.2;
 
       // Write var1 and var2
-      streamWriteVar(streamID, varID1, &var1, nmiss);
-      streamWriteVar(streamID, varID2, var2, nmiss);
+      streamWriteVar(streamID, varID1, &var1, numMissVals);
+      streamWriteVar(streamID, varID2, var2, numMissVals);
     }
 
   // Close the output stream
diff --git a/examples/cdi_write_refgrid.c b/examples/cdi_write_refgrid.c
index 9aa390199baf10d75ff76a414e5b175a4d2b63b5..d7a304906cfc45ef3886a5f0481a050bd4e768bc 100644
--- a/examples/cdi_write_refgrid.c
+++ b/examples/cdi_write_refgrid.c
@@ -11,7 +11,7 @@ main(void)
 {
   int gridID, zaxisID1, zaxisID2, taxisID;
   int vlistID, varID1, varID2, streamID;
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
   double levs[nlev] = { 101300, 92500, 85000, 50000, 20000 };
   double var1[nlon * nlat];
   double var2[nlon * nlat * nlev];
@@ -74,8 +74,8 @@ main(void)
       for (size_t i = 0; i < nlon * nlat * nlev; i++) var2[i] = 2.2;
 
       // Write var1 and var2
-      streamWriteVar(streamID, varID1, var1, nmiss);
-      streamWriteVar(streamID, varID2, var2, nmiss);
+      streamWriteVar(streamID, varID1, var1, numMissVals);
+      streamWriteVar(streamID, varID2, var2, numMissVals);
     }
 
   // Close the output stream
diff --git a/examples/cdi_write_refvgrid.c b/examples/cdi_write_refvgrid.c
index f9ee3c4f64379b35a270c620ce550abe490dfc6b..5aaf38aa5dfdbc5f6c454e6c96ae29d3073eff95 100644
--- a/examples/cdi_write_refvgrid.c
+++ b/examples/cdi_write_refvgrid.c
@@ -11,7 +11,7 @@ main(void)
 {
   int gridID, zaxisID1, zaxisID2, taxisID;
   int vlistID, varID1, varID2, streamID, tsID;
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
   double levs[nlev] = { 1, 2, 3, 4, 5 };
   double var1[nlon * nlat];
   double var2[nlon * nlat * nlev];
@@ -80,8 +80,8 @@ main(void)
       for (size_t i = 0; i < nlon * nlat * nlev; i++) var2[i] = 2.2;
 
       // Write var1 and var2
-      streamWriteVar(streamID, varID1, var1, nmiss);
-      streamWriteVar(streamID, varID2, var2, nmiss);
+      streamWriteVar(streamID, varID1, var1, numMissVals);
+      streamWriteVar(streamID, varID2, var2, numMissVals);
     }
 
   // Close the output stream
diff --git a/examples/cdi_write_relativ.c b/examples/cdi_write_relativ.c
index 62c62bb6337613f3758ddf1dfcf575a086537623..a145a1c1216038eff36cb2384c115cc8ad10ad75 100644
--- a/examples/cdi_write_relativ.c
+++ b/examples/cdi_write_relativ.c
@@ -11,7 +11,7 @@ main(void)
     nlev = 5,   // Number of levels
     nts = 3,    // Number of time steps
   };
-  int nmiss = 0;
+  int numMissVals = 0;
   double lons[] = { 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330 };
   double lats[] = { -75, -45, -15, 15, 45, 75 };
   double levs[] = { 101300, 92500, 85000, 50000, 20000 };
@@ -75,8 +75,8 @@ main(void)
       for (int i = 0; i < nlon * nlat * nlev; i++) var2[i] = 2.2;
 
       // Write var1 and var2
-      streamWriteVar(streamID, varID1, var1, nmiss);
-      streamWriteVar(streamID, varID2, var2, nmiss);
+      streamWriteVar(streamID, varID1, var1, numMissVals);
+      streamWriteVar(streamID, varID2, var2, numMissVals);
     }
 
   // Close the output stream
diff --git a/examples/cdi_write_reset.c b/examples/cdi_write_reset.c
index ce6da51e52399d47afa9a46a5ef3ad0aad29278d..d6080b54796d1f14ffe353189f20c687aa51cb68 100644
--- a/examples/cdi_write_reset.c
+++ b/examples/cdi_write_reset.c
@@ -12,7 +12,7 @@ run(int irun)
 {
   int gridID, zaxisID1, zaxisID2, taxisID;
   int vlistID, varID1, varID2, streamID;
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
   double lons[nlon] = { 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330 };
   double lats[nlat] = { -75, -45, -15, 15, 45, 75 };
   double levs[nlev] = { 101300, 92500, 85000, 50000, 20000 };
@@ -76,8 +76,8 @@ run(int irun)
       for (size_t i = 0; i < nlon * nlat * nlev; i++) var2[i] = 2.2;
 
       // Write var1 and var2
-      streamWriteVar(streamID, varID1, var1, nmiss);
-      streamWriteVar(streamID, varID2, var2, nmiss);
+      streamWriteVar(streamID, varID1, var1, numMissVals);
+      streamWriteVar(streamID, varID2, var2, numMissVals);
     }
 
   fprintf(stderr, "%d gridID=%d, zaxisID1=%d, vlistID=%d, streamID=%d\n", irun, gridID, zaxisID1, vlistID, streamID);
diff --git a/examples/cdi_write_rll.c b/examples/cdi_write_rll.c
index b936c39103aa288941e0b2d0686dde74f3e488b0..f8af829494599355c83f0cbdfb85b628ee7d5de0 100644
--- a/examples/cdi_write_rll.c
+++ b/examples/cdi_write_rll.c
@@ -8,7 +8,7 @@ main(void)
   const int nlat = 6;   // Number of latitudes
   const int nlev = 5;   // Number of levels
   const int nts = 3;    // Number of time steps
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
   double lons[] = { 0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330 };
   double lats[] = { -75, -45, -15, 15, 45, 75 };
   double levs[] = { 101300, 92500, 85000, 50000, 20000 };
@@ -76,8 +76,8 @@ main(void)
       for (size_t i = 0; i < nlon * nlat * nlev; i++) var2[i] = 2.2;
 
       // Write var1 and var2
-      streamWriteVar(streamID, varID1, var1, nmiss);
-      streamWriteVar(streamID, varID2, var2, nmiss);
+      streamWriteVar(streamID, varID1, var1, numMissVals);
+      streamWriteVar(streamID, varID2, var2, numMissVals);
     }
 
   // Close the output stream
diff --git a/examples/pio/collectData.c b/examples/pio/collectData.c
index f23b5550a30618f7a8d2824a8a79fd81df0587d9..a2ce0526c317ea9770e1eae25abbcba43d98e5e3 100644
--- a/examples/pio/collectData.c
+++ b/examples/pio/collectData.c
@@ -99,7 +99,7 @@ modelRun(MPI_Comm commModel)
   taxisID = taxisCreate(TAXIS_ABSOLUTE);
   vlistDefTaxis(vlistID, taxisID);
 
-  sprintf(&filename[0], "%s_%d.grb", name, tfID);
+  snprintf(&filename[0], sizeof(filename), "%s_%d.grb", name, tfID);
   streamID = streamOpenWrite(filename, filetype);
   xassert(streamID >= 0);
   streamDefVlist(streamID, vlistID);
@@ -112,7 +112,7 @@ modelRun(MPI_Comm commModel)
       /* if ( tfID > 0 ) */
       {
         streamClose(streamID);
-        sprintf(&filename[0], "%s_%d.grb", name, tfID);
+        snprintf(&filename[0], sizeof(filename), "%s_%d.grb", name, tfID);
         streamID = streamOpenWrite(filename, filetype);
         xassert(streamID >= 0);
         streamDefVlist(streamID, vlistID);
diff --git a/examples/pio/collectDataNStreams.c b/examples/pio/collectDataNStreams.c
index 263023720de4d0ff46875d4a14b2d0ae1f7b1a9b..f92cb38e77cf8f450bce5d71c5bfd5d935a2de0d 100644
--- a/examples/pio/collectDataNStreams.c
+++ b/examples/pio/collectDataNStreams.c
@@ -134,7 +134,7 @@ modelRun(MPI_Comm commModel)
   for (i = 0; i < nStreams; i++)
     {
       memset(filename, 0, 1024);
-      sprintf(&filename[0], "%s%c_%d.grb", nameExp, asciiA + i, tfID);
+      snprintf(&filename[0], sizeof(filename), "%s%c_%d.grb", nameExp, asciiA + i, tfID);
       streamID[i] = streamOpenWrite(filename, filetype);
       xassert(streamID[i] >= 0);
       streamDefVlist(streamID[i], vlistID[i]);
@@ -151,7 +151,7 @@ modelRun(MPI_Comm commModel)
           for (i = 0; i < nStreams; i++)
             {
               streamClose(streamID[i]);
-              sprintf(&filename[0], "%s%c_%d.grb", nameExp, asciiA + i, tfID);
+              snprintf(&filename[0], sizeof(filename), "%s%c_%d.grb", nameExp, asciiA + i, tfID);
               streamID[i] = streamOpenWrite(filename, filetype);
               xassert(streamID[i] >= 0);
               streamDefVlist(streamID[i], vlistID[i]);
diff --git a/m4/acx_assert_lang_is_fortran_variant.m4 b/m4/acx_assert_lang_is_fortran_variant.m4
index 1d857263b105440a5efb4d5d8458bfc3d52e59aa..d7bd3c059b5a5e7d6511b52a62251dd6cf537652 100644
--- a/m4/acx_assert_lang_is_fortran_variant.m4
+++ b/m4/acx_assert_lang_is_fortran_variant.m4
@@ -6,7 +6,7 @@ dnl Version: 1.0
 dnl Keywords: fortran language version assertion
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -51,6 +51,6 @@ dnl
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/acx_c_package.m4 b/m4/acx_c_package.m4
index c9ec79d07a2225c0d7720ef8e8cecd15a825432f..ea07c0895bd07cc28cb2cd13f8ccc8a6b85bf729 100644
--- a/m4/acx_c_package.m4
+++ b/m4/acx_c_package.m4
@@ -5,7 +5,7 @@ dnl Version: 1.0
 dnl Keywords: configure configure.ac autotools
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -46,7 +46,7 @@ dnl    [INCLUDE], [EXTRA-INCLUDES], [EXTRA-INCLUDEFLAGS],
 dnl       [ACTION-IF_HEADER-NOT-FOUND],
 dnl    [FUNCTION], [LIB-CANDIDATES], [EXTRA-LIBS], [EXTRA-LIBFLAGS],
 dnl      [ACTION-IF-LIB-NOT-FOUND],
-dnl    [DEFAULT-ROOT], [EXTRA-INCLUDE-PATHS-TO-TRY])
+dnl    [DEFAULT-ROOT])
 dnl -------------------------------------------------------------------
 dnl Check whether INCLUDE can be compiled and FUNCTION is found in
 dnl LIB-CANDIDATES. Sets PACKAGE_C_LIB and PACKAGE_C_INCLUDE variables to
@@ -57,7 +57,7 @@ dnl Also defines configure --with arguments for PACKAGEROOT,
 dnl PACKAGE-LIB and PACKAGE-INCLUDE.
 AC_DEFUN([ACX_C_PACKAGE],
   [AC_LANG_PUSH([C])
-   ACX_GENERIC_PACKAGE([$1],[$2],[-I],[$3],[$4],[$5],[$6],[-L],[$7],[$8],[$9],[$10],[$11],[$12],[$13])
+   ACX_GENERIC_PACKAGE([$1],[$2],[-I],[$3],[$4],[$5],[$6],[-L],[$7],[$8],[$9],[$10],[$11],[$12])
    AC_LANG_POP([C])dnl
   ])
 dnl
diff --git a/m4/acx_cfortran_flags.m4 b/m4/acx_cfortran_flags.m4
index 8f09ba15748cdebcf8b258eaeec2c8e83310adfc..595d129e4c85d7eb82f83cf94cdc53e7f89d84d2 100644
--- a/m4/acx_cfortran_flags.m4
+++ b/m4/acx_cfortran_flags.m4
@@ -1,13 +1,13 @@
-dnl acx_fc_c_link.m4 --- transform library c flags into version
-dnl                      that suits the fortran compiler
+dnl acx_cfortran_flags.m4 --- determine flags for C compiler to build external
+dnl                           functions for F77/FC with cfortran.h
 dnl
-dnl Copyright  (C)  2011  Thomas Jahns <jahns@dkrz.de>
+dnl Copyright  (C)  2020  Thomas Jahns <jahns@dkrz.de>
 dnl
 dnl Version: 1.0
 dnl Keywords:
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -44,7 +44,7 @@ AC_DEFUN([ACX_XLF_QEXTNAME_ADD_APPENDUS],
   [AS_CASE([$host],
      [*-ibm-aix*|powerpc64-*-linux-*|powerpc-*-linux-*],
      [AC_MSG_CHECKING([if -Dappendus needs to be added to CPPFLAGS for cfortran.h])
-      AS_IF([$CC -qversion 2>&1 | sed -n 1,5p | grep '^IBM XL C' >/dev/null],
+      AS_IF([$CC -qversion 2>&1 | sed 5q | grep '^IBM XL C' >/dev/null],
         [acx_temp_qextname_f77flags=`echo "$FFLAGS" | sed -n '/-qextname/{ s/^\(.* \)*-qextname\( .*\)*$/-qextname/;p;}'`
          acx_temp_qextname_fcflags=`echo "$FCFLAGS" | sed -n '/-qextname/{ s/^\(.* \)*-qextname\( .*\)*$/-qextname/;p;}'`
       dnl pretend the same option as for FC was used if F77 isn't used at all
@@ -80,30 +80,31 @@ AC_DEFUN([_ACX_FIND_CFORTRAN_DEF],
    AS_VAR_PUSHDEF([acx_FCFLAGS],[AC_LANG_CASE([Fortran],[FCFLAGS],[Fortran 77],[FFLAGS])])dnl
    AS_CASE([$host],
      [x86_64-*-linux-*|i*86-*-linux-*|*-apple-darwin*|ia64-*-linux-*|x86_64-*-freebsd*|i*86-*-freebsd*],
-     [acx_temp=`$acx_FC -V 2>&1 | sed -n 1,5p`
-      AS_IF([echo "$acx_temp" | grep '^Copyright.*\(The Portland Group\|NVIDIA CORPORATION\)' >/dev/null],
-        [AS_VAR_SET([acx_cf_flag],[-DgFortran])],
-        [echo "$acx_temp" | grep '^NAG Fortran Compiler Release' >/dev/null],
+     [AS_CASE([`$acx_FC -V 2>&1 | sed -n 1,10p`],
+        [*NAG\ Fortran\ Compiler\ Release*],
         [AS_VAR_SET([acx_cf_flag],[-DNAGf90Fortran])],
-        [echo "$acx_temp" | grep '^Intel(R) Fortran.*Compiler' >/dev/null],
-        [AS_VAR_SET([acx_cf_flag],[-DgFortran])],
-        [echo "$acx_temp" | grep '^Cray Fortran' >/dev/null],
+        [*Copyright*The\ Portland\ Group*|*Copyright*NVIDIA\ CORPORATION*|*Intel\(R\)\ Fortran*Compiler*|*Cray\ Fortran*],
         [AS_VAR_SET([acx_cf_flag],[-DgFortran])],
-        [acx_temp=`$acx_FC --version 2>&1 | sed -n 1,5p` \
-         && echo $acx_temp | grep '^GNU Fortran' >/dev/null],
-        [AS_IF([echo $acx_temp | grep g77 >/dev/null],
+        [AS_CASE([`$acx_FC --version 2>&1 | sed -n 1,5p`],
+           [*G95*],
+           [AS_VAR_SET([acx_cf_flag],[-DNAGf90Fortran])],
+           [*GNU\ Fortran*g77*],
            [AS_VAR_SET([acx_cf_flag],[-Dg77Fortran])],
+           [*GNU\ Fortran*],
            [dnl assume gfortran
 dnl check if compiling with f2c bindings or with default bindings
-            AS_IF([echo "$acx_FCFLAGS" | grep '^\(.* \)*-ff2c\( .*\)*$' >/dev/null],
+            AS_CASE([" $acx_FCFLAGS "],
+              [*\ -ff2c\ *],
               [AS_VAR_SET([acx_cf_flag],[-Df2cFortran])],
-              [AS_VAR_SET([acx_cf_flag],[-DgFortran])])])],
-        [echo $acx_temp | grep '^G95' >/dev/null],
-        [AS_VAR_SET([acx_cf_flag],[-DNAGf90Fortran])],
-        [$acx_FC -v 2>&1 | sed -n 1,5p | grep '^f2c' >/dev/null],
-        [AS_VAR_SET([acx_cf_flag],[-Df2cFortran])])],
+              [AS_VAR_SET([acx_cf_flag],[-DgFortran])
+               acx_temp=`$acx_FC --version 2>&1 | sed -n -e '/^GNU Fortran/{' \
+                  -e 's/^GNU Fortran\@{:@ @{:@@<:@^@:}@@:>@*@:}@\)\{0,1\} \(@<:@0-9.@:>@*\)\( .*\)\{0,1\}/\2/;p;}'`
+               AS_VERSION_COMPARE([$acx_temp],[7],,,[AS_VAR_SET([acx_cf_flag],["-DgFortran -DgFortran8"])])])],
+           [AS_CASE([`$acx_FC -v 2>&1 | sed -n 1,5p`],
+              [*f2c*],
+              [AS_VAR_SET([acx_cf_flag],[-Df2cFortran])])])])],
      [powerpc64-*-linux-*|powerpc-*-linux-*],
-     [AS_IF([$acx_FC -qversion 2>&1 | sed -n 1,5p | grep '^IBM XL Fortran' >/dev/null],
+     [AS_IF([$acx_FC -qversion 2>&1 | sed 5q | grep '^IBM XL Fortran' >/dev/null],
         [AS_VAR_SET([acx_cf_flag],[-DIBMR2Fortran])])],
      [*-ibm-aix*],
      [dnl xlc set _IBMR2 so nothing needs to be done
@@ -124,19 +125,21 @@ AC_DEFUN([ACX_FIND_CFORTRAN_DEF],
      [acx_cv_cf_flag],
      [acx_cv_cf_flag=''
 dnl test if user already provided a flag
-      AS_FOR([MACRO],[macro],[pgiFortran NAGf90Fortran f2cFortran hpuxFortran apolloFortran sunFortran IBMR2Fortran CRAYFortran PATHSCALE_COMPILER gFortran mipsFortran DECFortran vmsFortran CONVEXFortran PowerStationFortran AbsoftUNIXFortran AbsoftProFortran SXFortran],
-        [acx_temp=`echo "$CPPFLAGS $CFLAGS" | sed -n 's/^\(.* \)*-D\('"MACRO"'\)\( .*\)*$/\2/;t print
-b
-: print
+      acx_temp=`echo " $CPPFLAGS $CFLAGS " | sed -n 'm4_foreach_w([MACRO],[pgiFortran NAGf90Fortran f2cFortran hpuxFortran apolloFortran sunFortran IBMR2Fortran CRAYFortran PATHSCALE_COMPILER gFortran mipsFortran DECFortran vmsFortran CONVEXFortran PowerStationFortran AbsoftUNIXFortran AbsoftProFortran SXFortran],[/ -D[]MACRO/{
+x
+s/$/ -D[]MACRO/
+x
+}
+])x
+s/^ //
 p'`
-         AS_IF([test x"$acx_temp" != x],
-           [AS_IF([test x"$acx_cv_cf_flag" = x],
-              [acx_cv_cf_flag="$acx_temp (user-specified)"],
-              [acx_failure_msg="multiple specification of cfortran.h flags: "`echo "$acx_cv_cf_flag" | sed 's/ (user-specified)$//'`" $acx_temp"
-               acx_cv_cf_flag='error'
-               break])])])
+      AS_CASE([$acx_temp],
+        [-D*\ -D*],
+        [acx_failure_msg="multiple specification of cfortran.h flags: $acx_temp"
+         acx_cv_cf_flag='error'],
+        [-D*],
+        [acx_cv_cf_flag="$acx_temp (user-specified)"],
 dnl find automatically from machine/compiler
-      AS_IF([test x"$acx_cv_cf_flag" = x],
         [AC_PROVIDE_IFELSE([AC_PROG_FC],
            [AS_IF([test -n "$FC" -a X"$FC" != Xno],
               [AC_LANG_PUSH([Fortran])
@@ -155,26 +158,27 @@ dnl check f77 flag matches fc flag
          AC_PROVIDE_IFELSE([AC_PROG_F77],
            [AC_PROVIDE_IFELSE([AC_PROG_FC],
               [dnl both FC and F77 are configured
-               AS_IF([test -z "$FC" -o X"$FC" != Xno],
-                 [acx_cv_cf_flag="$acx_cv_fc_cf_flag (probed)"],
-                 [test -z "$F77" -o X"$F77" != Xno],
+               AS_IF([test -z "$FC" -o X"$FC" = Xno],
                  [acx_cv_cf_flag="$acx_cv_f77_cf_flag (probed)"],
+                 [test -z "$F77" -o X"$F77" = Xno],
+                 [acx_cv_cf_flag="$acx_cv_fc_cf_flag (probed)"],
                  [AS_IF([test x"$acx_cv_f77_cf_flag" = x"$acx_cv_fc_cf_flag"],
                     [acx_cv_cf_flag="$acx_cv_f77_cf_flag (probed)"],
-                    [acx_failure_msg="cfortran.h flag for $F77 does not match the flag for $FC.
+                    [acx_failure_msg="cfortran.h flag for $F77 ($acx_cv_f77_cf_flag) does not match the flag for $FC ($acx_cv_fc_cf_flag).
 Did you configure compatible compilers?"
                      acx_cv_cf_flag='error'])])])],
            [acx_cv_cf_flag="$acx_cv_fc_cf_flag (probed)"])])])
-   AS_IF([test x"$acx_cv_cf_flag" = xerror],
+   AS_CASE([$acx_cv_cf_flag],
+     [error],
      [m4_default([$1],
-        [AC_MSG_ERROR([$acx_failure_msg])])])
+        [AC_MSG_ERROR([$acx_failure_msg])])],
 dnl now that flag is established, add (probed) defines to CPPFLAGS
-   AS_IF([echo "$acx_cv_cf_flag" | grep ' (probed)$' >/dev/null],
+     [*\ \(probed\)],
      [CPPFLAGS="${CPPFLAGS+$CPPFLAGS }"`echo "$acx_cv_cf_flag" | sed 's/ (probed)$//'`])
   ])
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/acx_check_cfortran.m4 b/m4/acx_check_cfortran.m4
index a8dc4468b80d00eb05603f109ad2351aa95c7689..536260229e4c4b16a7d40c7fa360c9d448dd74d3 100644
--- a/m4/acx_check_cfortran.m4
+++ b/m4/acx_check_cfortran.m4
@@ -8,7 +8,7 @@ dnl Version: 1.0
 dnl Keywords:
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -260,7 +260,9 @@ FCALLSCSUB0(errExit,ERR_EXIT,err_exit)
               [_ACX_CHECK_CFORTRAN_FC],
               [acx_cv_cfortran_works=${acx_cv_cfortran_works-yes}])])
          AC_PROVIDE_IFELSE([AC_PROG_F77],
-           [AS_IF([test -n "$F77" -a X"$F77" != Xno AC_PROVIDE_IFELSE([AC_PROG_FC],[-a \( x"$acx_cv_cfortran_works" = xyes -o -z "$FC" -o X"$FC" = Xno \)])],[_ACX_CHECK_CFORTRAN_F77])])
+           [AS_IF([test -n "$F77" -a X"$F77" != Xno AC_PROVIDE_IFELSE(
+              [AC_PROG_FC],[-a \( x"$acx_cv_cfortran_works" = xyes dnl
+-o -z "$FC" -o X"$FC" = Xno \)])],[_ACX_CHECK_CFORTRAN_F77])])
          rm -f "conftest_c.$ac_objext" "conftest_c.$OBJEXT"
         ],
         [acx_cv_cfortran_works="compiling with cfortran.h failed"])
@@ -282,6 +284,6 @@ FCALLSCSUB0(errExit,ERR_EXIT,err_exit)
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/acx_fortran_check_include.m4 b/m4/acx_fortran_check_include.m4
index df5a5b0bf06b16a2d607320a6698153ea417eae3..bf38c48078057079eee877442c21bf8ceec76316 100644
--- a/m4/acx_fortran_check_include.m4
+++ b/m4/acx_fortran_check_include.m4
@@ -6,7 +6,7 @@ dnl Version: 1.0
 dnl Keywords:
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -87,6 +87,6 @@ AC_DEFUN([ACX_FORTRAN_CHECK_MOD_PATHS_IFELSE],
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/acx_fortran_package.m4 b/m4/acx_fortran_package.m4
index 42ff00151cd6df1c99c4b5f62162c50bcee83f41..7de4addf74184d3d5fe7475d94057ff526f9cca3 100644
--- a/m4/acx_fortran_package.m4
+++ b/m4/acx_fortran_package.m4
@@ -8,7 +8,7 @@ dnl Version: 1.0
 dnl Keywords: fortran package availability check
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -158,6 +158,6 @@ AC_DEFUN([ACX_F90_PACKAGE],
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/acx_lang_c_check_include.m4 b/m4/acx_lang_c_check_include.m4
index f3578992906d42f2a483f0184b3bd4305baa74fa..5352f0357280fbb9786cada62a373665e08034e8 100644
--- a/m4/acx_lang_c_check_include.m4
+++ b/m4/acx_lang_c_check_include.m4
@@ -8,7 +8,7 @@ dnl Version: 1.0
 dnl Keywords: c header check -I path
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -57,6 +57,6 @@ m4_define([ACX_LANG_INCLUDE_PROGRAM(C)],
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/acx_lang_check_include.m4 b/m4/acx_lang_check_include.m4
index 0228bdd2a89a6079a1afea61cbc1de38147935f5..110ecfe72b36ce58144e8fb9014b073b9e65d0f0 100644
--- a/m4/acx_lang_check_include.m4
+++ b/m4/acx_lang_check_include.m4
@@ -6,7 +6,7 @@ dnl Version: 1.0
 dnl Keywords: package include check
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -125,6 +125,6 @@ AC_DEFUN([ACX_GENERIC_CHECK_INCLUDE_PATHS_IFELSE],
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/acx_lang_fortran_check_include.m4 b/m4/acx_lang_fortran_check_include.m4
index 5ed73283b8654fcff9ef6f99dd1e9706b003afbe..352cb2a40949dd24f0bc5bff13d6cb8b34ea375c 100644
--- a/m4/acx_lang_fortran_check_include.m4
+++ b/m4/acx_lang_fortran_check_include.m4
@@ -7,7 +7,7 @@ dnl Version: 1.0
 dnl Keywords:
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -61,6 +61,6 @@ AC_DEFUN([ACX_LANG_INCLUDE_PROGRAM(Fortran)],
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/acx_lang_package.m4 b/m4/acx_lang_package.m4
index e18889f8e116c16de93f2c970cfe3af02d6c4030..6f099808369da96f8f44f66d84ab4b403487ee33 100644
--- a/m4/acx_lang_package.m4
+++ b/m4/acx_lang_package.m4
@@ -10,7 +10,7 @@ dnl Version: 1.0
 dnl Keywords:
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -49,7 +49,7 @@ dnl
 dnl ACX_GENERIC_PACKAGE(PACKAGE, [INCLUDE], INC_FLAG, [EXTRA-INCLUDES],
 dnl [EXTRA-INCLUDEFLAGS], [ACTION-IF_HEADER-NOT-FOUND], [FUNCTION],
 dnl LIBFLAG, [LIB-CANDIDATES], [EXTRA-LIBS], [EXTRA-LIBFLAGS],
-dnl [ACTION-IF-LIB-NOT-FOUND], [DEFAULT-ROOT], [EXTRA-INCLUDE-DIRS-TO-TRY])
+dnl [ACTION-IF-LIB-NOT-FOUND], [DEFAULT-ROOT])
 dnl -------------------------------------------------------------------
 dnl Check whether INCLUDE can be compiled and FUNCTION is found in
 dnl LIB-CANDIDATES with current language compiler. Sets PACKAGE_LANG_LIB
@@ -63,15 +63,15 @@ dnl to yes if all requested tests succeeded, to no if any test failed.
 dnl The library check will not run if the header check failed.
 AC_DEFUN([ACX_GENERIC_PACKAGE],
   [AC_REQUIRE([_ASX_TR_ARG_PREPARE])
-   AS_VAR_PUSHDEF([acx_pkg_root],[AS_TR_CPP([$1][root])])
-   AS_VAR_PUSHDEF([acx_pkg_lib],[AS_TR_CPP([$1_]_AC_LANG_ABBREV[_LIB])])
-   AS_VAR_PUSHDEF([acx_pkg_inc],[AS_TR_CPP([$1_]_AC_LANG_ABBREV[_INCLUDE])])
+   AS_VAR_PUSHDEF([acx_pkg_root],[AS_TR_CPP([$1][root])])dnl
+   AS_VAR_PUSHDEF([acx_pkg_lib],[AS_TR_CPP([$1_]_AC_LANG_ABBREV[_LIB])])dnl
+   AS_VAR_PUSHDEF([acx_pkg_inc],[AS_TR_CPP([$1_]_AC_LANG_ABBREV[_INCLUDE])])dnl
    AS_VAR_PUSHDEF([acx_cv_pkg_inc],
-     [AS_TR_SH([acx_cv_]_AC_LANG_ABBREV[_include_]$2)])
+     [AS_TR_SH([acx_cv_]_AC_LANG_ABBREV[_include_]$2)])dnl
    AS_VAR_PUSHDEF([acx_pkg_bindings],
-     [AS_TR_SH([have_][$1_]_AC_LANG_ABBREV[_bindings])])
+     [AS_TR_SH([have_][$1_]_AC_LANG_ABBREV[_bindings])])dnl
    AS_VAR_SET([acx_pkg_bindings],[yes])
-   AC_SUBST(acx_pkg_root)
+   AC_SUBST(acx_pkg_root)dnl
    AC_ARG_WITH(ASX_TR_ARG([$1-root]),
      [AS_HELP_STRING([--with-]ASX_TR_ARG([$1])[-root],
         [set directory to search for $1 headers and library]m4_ifval([$13],[, @<:@default=$13@:>@]))],
@@ -96,7 +96,7 @@ AC_DEFUN([ACX_GENERIC_PACKAGE],
         [AS_VAR_SET([acx_pkg_inc],["$3$withval"])])
       AC_ARG_VAR(acx_pkg_inc,
 	[specifically set flags to use when compiling sources using $1 includes.])
-      ACX_LANG_CHECK_INCLUDE_PATHS_IFELSE([$2],[$14],
+      ACX_LANG_CHECK_INCLUDE_PATHS_IFELSE([$2],[],
         [AS_LITERAL_IF([acx_cv_pkg_inc],
            [AS_VAR_SET([acx_pkg_inc],[`echo "$]acx_cv_pkg_inc[" | sed -e 's/^ *//;s/ *$//'`])],
            [AS_VAR_COPY([acx_temp],[acx_cv_pkg_inc])
@@ -124,16 +124,15 @@ AC_DEFUN([ACX_GENERIC_PACKAGE],
            [AS_VAR_COPY([acx_temp],[ac_Search])
             acx_temp=`echo "$acx_temp" | sed -e 's/^ *//;s/ *$//'`
             AS_VAR_COPY([acx_pkg_lib],[acx_temp])])
-         AS_VAR_POPDEF([ac_Search])])])
+         AS_VAR_POPDEF([ac_Search])])])dnl
    AS_VAR_POPDEF([acx_pkg_bindings])dnl
    AS_VAR_POPDEF([acx_cv_pkg_inc])dnl
    AS_VAR_POPDEF([acx_pkg_inc])dnl
    AS_VAR_POPDEF([acx_pkg_lib])dnl
-   AS_VAR_POPDEF([acx_pkg_root])dnl
-  ])
+   AS_VAR_POPDEF([acx_pkg_root])])
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/acx_lt_problems.m4 b/m4/acx_lt_problems.m4
index 99da19b3aaa8d256f77cb617a266f262fdf863bc..fa7be119f5bbcd28f2e2267b6356123d806c97c0 100644
--- a/m4/acx_lt_problems.m4
+++ b/m4/acx_lt_problems.m4
@@ -6,7 +6,7 @@ dnl Version: 1.0
 dnl Keywords:
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -94,6 +94,6 @@ AC_DEFUN([ACX_LT_PROBLEMS],
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/acx_m4_list_to_quoted_strings.m4 b/m4/acx_m4_list_to_quoted_strings.m4
index a3a87fa8ceb54e51ef6efaeafb2c0243677b8973..8ae8f66926de4137eee6e81d5860f2e7d5581c56 100644
--- a/m4/acx_m4_list_to_quoted_strings.m4
+++ b/m4/acx_m4_list_to_quoted_strings.m4
@@ -7,7 +7,7 @@ dnl Version: 1.0
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Keywords:
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -48,6 +48,6 @@ AC_DEFUN([ACX_M4_LIST_TO_QUOTED_STRINGS],
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/acx_mpirun.m4 b/m4/acx_mpirun.m4
index 5d2b7c1adc3d68fca4d40a889fe6c1a5e338b73f..e6cee887ae15bf8b550d801c33dfb02c0484a359 100644
--- a/m4/acx_mpirun.m4
+++ b/m4/acx_mpirun.m4
@@ -5,7 +5,7 @@ dnl
 dnl Keywords: configure configure.ac autoconf MPI mpirun mpiexec
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -99,6 +99,6 @@ main(int argc, char **argv)
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/acx_mv_obj.m4 b/m4/acx_mv_obj.m4
index 369db2836db867b16e3572ffc1bca085b3abd0e9..452632848e824f44640250e8811cdb7bb2b97fbb 100644
--- a/m4/acx_mv_obj.m4
+++ b/m4/acx_mv_obj.m4
@@ -1,4 +1,4 @@
-dnl acx_mv_lo.m4 --- shell function to rename a libtool .lo file
+dnl acx_mv_obj.m4 --- shell function to rename a libtool .lo file
 dnl
 dnl Copyright  (C)  2019  Thomas Jahns <jahns@dkrz.de>
 dnl
@@ -6,7 +6,7 @@ dnl Version: 1.0
 dnl Keywords:
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -43,16 +43,21 @@ dnl
 AC_DEFUN([ACX_MV_OBJ],
   [AC_REQUIRE_SHELL_FN([acx_fn_mv_obj],
      [AS_FUNCTION_DESCRIBE([ac_fn_mv_obj], [LINENO FROM TO],
-       [Rename FROM.$ac_ext to TO.$ac_ext, and return whether this succeeded.])],
+       [Rename FROM.$ac_objext to TO.$ac_objext, and return whether this succeeded.])],
      [AS_LINENO_PUSH([$[]1])
       acx_path_from=`echo "$[]2" | sed -e 's!/\{0,1\}@<:@^/@:>@*$[]!!'`
-      test -z "$acx_path_from" && ASX_VAR_UNSET([acx_path_from])
+      test -z "$acx_path_from" && acx_path_from=.
       acx_fn_from=`echo "$[]2" | sed -e 's@^.*/@@'`
       acx_path_to=`echo "$[]3" | sed -e 's!/\{0,1\}@<:@^/@:>@*$[]!!'`
-      test -z "$acx_path_to" && ASX_VAR_UNSET([acx_path_to])
+      test -z "$acx_path_to" && acx_path_to=.
       acx_fn_to=`echo "$[]3" | sed -e 's@^.*/@@'`
       AS_IF([expr "$ac_compile" : '.*/libtool --mode=compile' >/dev/null],
-        [_AC_RUN_LOG([sed 's@\(pic_object='"'\)"'\(\(.libs/\)\{0,1\}\)'"$acx_fn_from"'\.o'"'"'@\1\2'"$acx_fn_to"'.o'"'"'@' "$][2.$ac_objext" >"$][3.$ac_objext" && rm "$][2.$ac_objext" && if test -f "$][2.$OBJEXT" ; then mv "$][2.$OBJEXT" "$][3.$OBJEXT" ; fi && if test -f "${acx_path_from+$acx_path_from/}.libs/$acx_fn_from.$OBJEXT" ; then as_dir="${acx_path_to-.}/.libs" as_fn_mkdir_p ; mv "${acx_path_from+$acx_path_from/}.libs/$acx_fn_from.$OBJEXT" "${acx_path_to+$acx_path_to/}.libs/$acx_fn_to.$OBJEXT" ; fi],
+        [_AC_RUN_LOG([sed 's@\(pic_object='"'\)"'\(\(.libs/\)\{0,1\}\)'"$acx_fn_from"'\.o'"'"'@\1\2'"$acx_fn_to"'.o'"'"'@' "$][2.$ac_objext" >"$][3.$ac_objext" && rm "$][2.$ac_objext" \
+        && if test -f "$][2.$OBJEXT" ; then mv "$][2.$OBJEXT" "$][3.$OBJEXT" ; fi \
+        && if test -f "$acx_path_from/.libs/$acx_fn_from.$OBJEXT" ; then \
+          as_dir="$acx_path_to/.libs" as_fn_mkdir_p ; \
+          mv "$acx_path_from/.libs/$acx_fn_from.$OBJEXT" \
+             "$acx_path_to/.libs/$acx_fn_to.$OBJEXT" ; fi],
             [_AS_ECHO_LOG([Renaming object file $][2.$ac_objext to $][3.$ac_objext.])])],
          [_AC_RUN_LOG([mv "$][2.$ac_objext" "$][3.$ac_objext"],
             [_AS_ECHO_LOG([Renaming object file $][2.$ac_objext to $][3.$ac_objext.])])])
@@ -64,7 +69,7 @@ dnl
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
 dnl
diff --git a/m4/acx_option_search_libs.m4 b/m4/acx_option_search_libs.m4
index 0e55de126fb37576347aa220dc8ca1bcacba27f2..e6fd0b74d85c0e7221bd48b8e3ee978cc1e0caef 100644
--- a/m4/acx_option_search_libs.m4
+++ b/m4/acx_option_search_libs.m4
@@ -9,7 +9,7 @@ dnl Version: 1.0
 dnl Keywords:
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -125,6 +125,6 @@ AC_DEFUN([ACX_OPTION_SEARCH_LIBS_MULTI],
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/acx_options.m4 b/m4/acx_options.m4
index 9db3fe5a25e02f434da744a9e12389e308df20dd..d35722fc3785f32c74abf6f44c52e255ff85c113 100644
--- a/m4/acx_options.m4
+++ b/m4/acx_options.m4
@@ -3,7 +3,7 @@ AC_DEFUN([ACX_CDI_OPTIONS],
 #  ----------------------------------------------------------------------
 #  Checks for multithreaded compiling + linking
 AC_ARG_WITH([threads],
-            [AC_HELP_STRING([--with-threads=<yes/no/directory>],
+            [AS_HELP_STRING([--with-threads=<yes/no/directory>],
                             [Compile + link for multithreading [default=yes]])],
             [],
             [with_threads=yes])
@@ -318,7 +318,7 @@ AC_SUBST([ENABLE_ACROSS],[$enable_across])
 #  Compile interface with internal CGRIBEX library
 AC_MSG_CHECKING([for CGRIBEX support])
 AC_ARG_ENABLE([cgribex],
-              [AC_HELP_STRING([--enable-cgribex],[Use the CGRIBEX library [default=yes]])],
+              [AS_HELP_STRING([--enable-cgribex],[Use the CGRIBEX library [default=yes]])],
               [AS_IF([test "x$enable_cgribex" != 'xno'],
                      [AC_DEFINE(HAVE_LIBCGRIBEX,[1],[Define to 1 for GRIB1 decoding/encoding with cgribex])
                       enable_cgribex=yes])],
@@ -330,7 +330,7 @@ AC_SUBST([ENABLE_CGRIBEX],[$enable_cgribex])
 #  Compile interface with internal SERVICE library
 AC_MSG_CHECKING([for SERVICE support])
 AC_ARG_ENABLE([service],
-              [AC_HELP_STRING([--enable-service],[Use the service library [default=yes]])],
+              [AS_HELP_STRING([--enable-service],[Use the service library [default=yes]])],
               [AS_IF([test "x$enable_service" != 'xno'],
                      [AC_DEFINE(HAVE_LIBSERVICE,[1],[Define to 1 for SERVICE interface])
                       enable_service=yes])],
@@ -342,7 +342,7 @@ AC_SUBST([ENABLE_SERVICE],[$enable_service])
 #  Compile interface with internal EXTRA library
 AC_MSG_CHECKING([for EXTRA support])
 AC_ARG_ENABLE([extra],
-              [AC_HELP_STRING([--enable-extra],[Use the extra library [default=yes]])],
+              [AS_HELP_STRING([--enable-extra],[Use the extra library [default=yes]])],
               [AS_IF([test "x$enable_extra" != 'xno'],
                      [AC_DEFINE(HAVE_LIBEXTRA,[1],[Define to 1 for EXTRA interface])
                       enable_extra=yes])],
@@ -354,7 +354,7 @@ AC_SUBST([ENABLE_EXTRA],[$enable_extra])
 #  Compile interface with internal IEG library
 AC_MSG_CHECKING([for IEG support])
 AC_ARG_ENABLE([ieg],
-              [AC_HELP_STRING([--enable-ieg],[Use the ieg library [default=yes]])],
+              [AS_HELP_STRING([--enable-ieg],[Use the ieg library [default=yes]])],
               [AS_IF([test "x$enable_ieg" != 'xno'],
                      [AC_DEFINE(HAVE_LIBIEG,[1],[Define to 1 for IEG interface])
                       enable_ieg=yes])],
diff --git a/m4/acx_sl_fc_mod_path_flag.m4 b/m4/acx_sl_fc_mod_path_flag.m4
index ad384f2e05ae6ce1bd6e2588fbaeaa88166e09ae..cce2fc6cc9f4148cbb09daf5b35354d6cc236fd5 100644
--- a/m4/acx_sl_fc_mod_path_flag.m4
+++ b/m4/acx_sl_fc_mod_path_flag.m4
@@ -6,7 +6,7 @@ dnl Version: 1.0
 dnl Keywords:
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -91,6 +91,6 @@ AC_DEFUN([ACX_SL_FC_CHECK_MOD_PATH_FLAG],dnl
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/acx_sl_mod_suffix.m4 b/m4/acx_sl_mod_suffix.m4
index d3cd596ab86941c24a0c5d95a45173ea42c94581..27a6fdc463f40668a0b38ed14f2a018ccf0d9e0e 100644
--- a/m4/acx_sl_mod_suffix.m4
+++ b/m4/acx_sl_mod_suffix.m4
@@ -6,7 +6,7 @@ dnl Version: 1.0
 dnl Keywords:
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -108,6 +108,6 @@ dnl
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/acx_use_libtool_configuration.m4 b/m4/acx_use_libtool_configuration.m4
index 687acd1cc3f93a0055a2398afe5897831c93ba6f..bc3ff26b6ddf1284a08f9441fe7a8b988f9c794f 100644
--- a/m4/acx_use_libtool_configuration.m4
+++ b/m4/acx_use_libtool_configuration.m4
@@ -6,7 +6,7 @@ dnl Version: 1.0
 dnl Keywords:
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -73,9 +73,12 @@ AC_DEFUN([ACX_USE_LIBTOOL_CONFIGURATION],
   [dnl before switching on libtool, identify compilers that prevent us from
    dnl certain build configurations
    ACX_LT_PROBLEMS
+   m4_if(m4_cmp(m4_version_compare(LT_PACKAGE_VERSION,[2.4.6]),1),-1,
+     [m4_pushdef([acx_lt_sed],[sed])],
+     [m4_pushdef([acx_lt_sed],[SED])])
 dnl add some monkey patching for older libtool versions that don't handle
 dnl newer PGI or NAG configurations particularly well
-   m4_if(m4_cmp(m4_version_compare(LT_PACKAGE_VERSION,[2.4.6]),1),-1,
+   m4_if(m4_cmp(m4_version_compare(LT_PACKAGE_VERSION,[2.4.7]),1),-1,
      [m4_pushdef([_LT_COMPILER_PIC],
        m4_bpatsubst(m4_dquote(
        m4_bpatsubst(m4_dquote(
@@ -86,12 +89,12 @@ dnl newer PGI or NAG configurations particularly well
 	  _LT_TAGVAR(lt_prog_compiler_pic, $][1)='-PIC'
 	  _LT_TAGVAR(lt_prog_compiler_static, $][1)='-Bstatic'
 	  ;;
-	*PGI\\ Compilers\\ and\\ Tools*|*NVIDIA\\ Compilers\\ and\\ Tools*|*Port][land\\ ])),[sed 5q`],[sed -n 1,5p`])),[pgcpp\*],[pgcpp* | pgc++* ]))dnl
+	*PGI\\ Compilers\\ and\\ Tools*|*NVIDIA\\ Compilers\\ and\\ Tools*|*Port][land\\ ])),acx_lt_sed[ 5q`],acx_lt_sed[ -n 1,5p`])),[pgcpp\*],[pgcpp* | pgc++* ]))dnl
       m4_pushdef([_LT_LANG_CXX_CONFIG],
         m4_bpatsubst(m4_dquote(
         m4_bpatsubst(m4_dquote(
         m4_bpatsubst(m4_dquote(m4_defn([_LT_LANG_CXX_CONFIG])),
-          [sed 5q`],[sed -n 1,5p`])),
+          acx_lt_sed[ 5q`],acx_lt_sed[ -n 1,5p`])),
           [\$CC\( \$pic_flag\)? -shared -nostdlib \$predep_objects \$libobjs \$deplibs \$postdep_objects ],
           [$CC\1 -shared $libobjs $deplibs ])),
           [pgcpp\*],[pgcpp* | pgc++* ]))dnl
@@ -105,13 +108,13 @@ dnl newer PGI or NAG configurations particularly well
         [$CC '"$tmp_sharedflag""$tmp_addflag"' $libobjs $deplibs '"$tmp_compiler_flags"' $wl-soname])),
         [  tmp_sharedflag='-Wl,-shared'],
         [  tmp_sharedflag='-Wl,-shared'
-	  tmp_compiler_flags='`echo \$compiler_flags | sed -e '"'"'s/ -W@<:@cl@:>@,-no-pie\\b//g'"'"'`'])),
+	  tmp_compiler_flags='`echo " \$compiler_flags " | sed -E -e '"'"': start'"'"' -e '"'"'s/ -W@<:@cl@:>@,-no-pie@<:@@<:@:blank:@:>@@:>@/ /g;t start'"'"' -e '"'"'s/^ //;s/ $//'"'"'`'])),
         [\*Sun\\ F\*\(.\)[ 	]*# Sun Fortran 8\.3
 [ 	]*tmp_sharedflag='-G' ;;
 ],[\&	*NAG\\ Fortran\\ Compiler*\1
 	  tmp_sharedflag='-Wl,-shared'
-	  tmp_compiler_flags='`echo \$compiler_flags | sed -e '"'"'s/ -W@<:@cl@:>@,-no-pie\\b//g'"'"'`' ;;
-])),[sed 5q`],[sed -n 1,5p`]))dnl
+	  tmp_compiler_flags='`echo " \$compiler_flags " | sed -E -e '"'"': start'"'"' -e '"'"'s/ -W@<:@cl@:>@,-no-pie@<:@@<:@:blank:@:>@@:>@/ /g;t start'"'"' -e '"'"'s/^ //;s/ $//'"'"'`' ;;
+])),acx_lt_sed[ 5q`],acx_lt_sed[ -n 1,5p`]))dnl
       m4_pushdef([_LT_SYS_HIDDEN_LIBDEPS],[AS_UNSET([output_verbose_link_cmd])]
         m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(
           m4_defn([_LT_SYS_HIDDEN_LIBDEPS])),[test x-\([LR]\) = "\$p"],
@@ -146,34 +149,46 @@ dnl substitute -shared-intel if present
      [AS_IF([eval test x\$\{acx_flag_var+set\} = xset],
         [eval acx_temp="\" \$$acx_flag_var_ \""
          AS_CASE([$acx_temp],[*\ -shared-intel\ *|*\ -static-intel\ *],
-           [acx_temp=`echo "$acx_temp" | sed -e 's/ \(-\(shared\|static\)-intel\)\b/ -Xcompiler \1 -XCClinker \1/'`])
+           [acx_temp=`echo "$acx_temp" | sed -E -e 's/ (-shared-intel|-static-intel) / -Xcompiler \1 -XCClinker \1 /g'`])
+         AS_CASE([$acx_temp],[*\ -Qlocation,*\ *],
+           [acx_temp=`echo "$acx_temp" | sed -E -e 's/ (-Qlocation,@<:@^, @:>@*,@<:@^@<:@:blank:@:>@@:>@*)/ -Xcompiler \1 -XCClinker \1/g'`])
 dnl take care of ifort/icc/icpc two-part options
-         eval acx_flag_var=\"`echo "$acx_temp" | sed -e 's/ -\(align\|allow\|assume\|ccdefault\|check\|convert\|debug\|debug-parameters\|diag-type\|diag-enable\|diag-disable\|double-size\|dynamic-linker\|dyncom\|export-dir\|extend-source\|fp-model\|fpscomp\|gen-interfaces\|heap-arrays\|imacros\|integer-size\|iprefix\|iquote\|iwithprefixbefore\|module\|names\|opt-report\|opt-streaming-stores\|pch-dir\|pch-use\|prof-dir\|prof-file\|real-size\|reentrancy\|stand\|tcollect-filter\|tune\|warn\|watch\) \(@<:@^-@:>@@<:@^ @:>@*\)\b/ -Xcompiler -\1 -Xcompiler \2/g' -e 's/^ //;s/ $//'`\"])])
-   AC_PROVIDE_IFELSE([AC_PROG_CC],
-     [AS_IF([test -n "$CC" -a X"$CC" != Xno],
-        [AC_LANG_PUSH([C])
-         _KPSE_CHECK_LIBTOOL
-         AC_LANG_POP([C])])])
-   AC_PROVIDE_IFELSE([AC_PROG_FC],
-     [AS_IF([test -n "$FC" -a X"$FC" != Xno],
-        [AC_LANG_PUSH([Fortran])
-         _ACX_LT_FORT_FLAGS_MANGLE
-         _KPSE_CHECK_LIBTOOL
-         AC_LANG_POP([Fortran])])])
-   AC_PROVIDE_IFELSE([AC_PROG_F77],
-     [AS_IF([test -n "$F77" -a X"$F77" != Xno],
-        [AC_LANG_PUSH([Fortran 77])
-         _ACX_LT_FORT_FLAGS_MANGLE
-         _KPSE_CHECK_LIBTOOL
-         AC_LANG_POP([Fortran 77])])])
-   AC_PROVIDE_IFELSE([AC_PROG_CXX],
-     [AS_IF([test -n "$CXX" -a X"$CXX" != Xno],
-        [AC_LANG_PUSH([C++])
+         eval acx_flag_var=\"`echo "$acx_temp" | sed -E -e 's/ -(align|allow|assume|ccdefault|check|convert|debug|debug-parameters|diag-type|diag-enable|diag-disable|double-size|dynamic-linker|dyncom|export-dir|extend-source|fp-model|fpscomp|gen-interfaces|heap-arrays|imacros|integer-size|iprefix|iquote|iwithprefixbefore|module|names|opt-report|opt-streaming-stores|pch-dir|pch-use|prof-dir|prof-file|real-size|reentrancy|stand|tcollect-filter|tune|warn|watch) (@<:@^-@:>@@<:@^@<:@:blank:@:>@@:>@*)/ -Xcompiler -\1 -Xcompiler \2/g' -e 's/^ //;s/ $//'`\"])])
+dnl NAG Fortran injects several object files into shared objects which
+dnl are only needed and helpful when calling the Fortran code from
+dnl C/C++ without explicit initialization of the NAG Fortran
+dnl RTL. Since these libraries are meant to be linked into Fortran
+dnl programs compiled with the same compiler or a newer version, that
+dnl only causes overhead and potential problems.
+   m4_pushdef([fix_nag_shared_link],
+     [AC_LANG_CASE([Fortran],[m4_pushdef([acx_flags_var],[FCFLAGS])m4_pushdef([acx_lt_tag],[FC])],
+        [Fortran 77],[m4_pushdef([acx_flags_var],[FFLAGS])m4_pushdef([acx_lt_tag],[F77])],
+        [C],[m4_pushdef([acx_flags_var],[CFLAGS])m4_pushdef([acx_lt_tag],[])])dnl
+      AS_IF([test x"$_LT_TAGVAR([acx_is_nagfor],m4_defn([acx_lt_tag]))" = xyes],
+        [AS_CASE([" $acx_flags_var $LDFLAGS "],
+           [*\ -B*\ *],
+           [extrasub="${extrasub+$extrasub$as_nl}/^\# skip internal symbols for stubs created by xlf/i \\\\${as_nl}\# ignore symbols nagfor injects into our shared objects\\\\${as_nl}/"'^${exp_sym_prefix}'"${acx_symprfx}"'(__NAGf90_|f90_)/b'
+],
+           [acx_flags_var="$acx_flags_var -XCClinker -Wl,-B${ac_abs_confdir}/util/nagdynlib"])])])dnl
+dnl
+   m4_foreach([lng],[[C],[Fortran],[Fortran 77],[C++]],
+  [m4_define([lCC],[_AC_LANG_DISPATCH([_AC_CC],m4_defn([lng]))])dnl
+   AC_PROVIDE_IFELSE([AC_PROG_]_AC_LANG_DISPATCH([_AC_CC],m4_defn([lng])),
+     [AS_IF([test -n "$]m4_defn([lCC])[" -a X"$lCC" != Xno],
+        [AC_LANG_PUSH(m4_defn([lng]))
+         m4_if(m4_if(m4_defn([lng]),[Fortran],[1],m4_defn([lng]),[Fortran 77],[1],[2]),[1],
+           [_ACX_LT_FORT_FLAGS_MANGLE])
          _KPSE_CHECK_LIBTOOL
-         AC_LANG_POP([C++])])])])dnl
+         fix_nag_shared_link
+         AC_LANG_POP(m4_defn([lng]))])
+])])
+   dnl adjust libobjs handling to deal with $ac_objext being .lo
+   m4_pushdef([_AC_LIBOBJS_NORMALIZE],m4_bpatsubst(m4_dquote(m4_defn([_AC_LIBOBJS_NORMALIZE])),
+     [s/\\\.obj],[s/.lo\$//;\&]))dnl
+   m4_popdef([fix_nag_shared_link])])dnl
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/asx_tr_arg.m4 b/m4/asx_tr_arg.m4
index a65cf790a6cf5a4302d10234c15c94c6b144e334..f23223c683ff5958906c848a9b176e6a250bd5b6 100644
--- a/m4/asx_tr_arg.m4
+++ b/m4/asx_tr_arg.m4
@@ -6,7 +6,7 @@ dnl Version: 1.0
 dnl Keywords:
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -64,6 +64,6 @@ AS_LITERAL_IF([$1],
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/asx_unset.m4 b/m4/asx_unset.m4
index 2665953b59edccad1463a1d1d80e0d5a8b2a0906..46f01ba9dd264bdcaa97d9da89830a96ac2f1927 100644
--- a/m4/asx_unset.m4
+++ b/m4/asx_unset.m4
@@ -6,7 +6,7 @@ dnl Version: 1.0
 dnl Keywords:
 dnl Author: Thomas Jahns <jahns@dkrz.de>
 dnl Maintainer: Thomas Jahns <jahns@dkrz.de>
-dnl URL: https://www.dkrz.de/redmine/projects/scales-ppm
+dnl URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
 dnl
 dnl Redistribution and use in source and binary forms, with or without
 dnl modification, are  permitted provided that the following conditions are
@@ -55,6 +55,6 @@ AC_DEFUN([ASX_VAR_UNSET],
 dnl
 dnl Local Variables:
 dnl mode: autoconf
-dnl license-project-url: "https://www.dkrz.de/redmine/projects/scales-ppm"
+dnl license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
 dnl license-default: "bsd"
 dnl End:
diff --git a/m4/ax_pthread.m4 b/m4/ax_pthread.m4
index 1598d077ff020f1f0be8388fded01c871c946354..9f35d139149f8d9bda17cddb730cd13bcf775465 100644
--- a/m4/ax_pthread.m4
+++ b/m4/ax_pthread.m4
@@ -14,20 +14,24 @@
 #   flags that are needed. (The user can also force certain compiler
 #   flags/libs to be tested by setting these environment variables.)
 #
-#   Also sets PTHREAD_CC to any special C compiler that is needed for
-#   multi-threaded programs (defaults to the value of CC otherwise). (This
-#   is necessary on AIX to use the special cc_r compiler alias.)
+#   Also sets PTHREAD_CC and PTHREAD_CXX to any special C compiler that is
+#   needed for multi-threaded programs (defaults to the value of CC
+#   respectively CXX otherwise). (This is necessary on e.g. AIX to use the
+#   special cc_r/CC_r compiler alias.)
 #
 #   NOTE: You are assumed to not only compile your program with these flags,
 #   but also to link with them as well. For example, you might link with
 #   $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS
+#   $PTHREAD_CXX $CXXFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS
 #
 #   If you are only building threaded programs, you may wish to use these
 #   variables in your default LIBS, CFLAGS, and CC:
 #
 #     LIBS="$PTHREAD_LIBS $LIBS"
 #     CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
+#     CXXFLAGS="$CXXFLAGS $PTHREAD_CFLAGS"
 #     CC="$PTHREAD_CC"
+#     CXX="$PTHREAD_CXX"
 #
 #   In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant
 #   has a nonstandard name, this macro defines PTHREAD_CREATE_JOINABLE to
@@ -83,7 +87,7 @@
 #   modified version of the Autoconf Macro, you may extend this special
 #   exception to the GPL to apply to your modified version as well.
 
-#serial 27
+#serial 31
 
 AU_ALIAS([ACX_PTHREAD], [AX_PTHREAD])
 AC_DEFUN([AX_PTHREAD], [
@@ -105,6 +109,7 @@ if test "x$PTHREAD_CFLAGS$PTHREAD_LIBS" != "x"; then
         ax_pthread_save_CFLAGS="$CFLAGS"
         ax_pthread_save_LIBS="$LIBS"
         AS_IF([test "x$PTHREAD_CC" != "x"], [CC="$PTHREAD_CC"])
+        AS_IF([test "x$PTHREAD_CXX" != "x"], [CXX="$PTHREAD_CXX"])
         CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
         LIBS="$PTHREAD_LIBS $LIBS"
         AC_MSG_CHECKING([for pthread_join using $CC $PTHREAD_CFLAGS $PTHREAD_LIBS])
@@ -386,7 +391,7 @@ if test "x$ax_pthread_clang" = "xyes"; then
              # step
              ax_pthread_save_ac_link="$ac_link"
              ax_pthread_sed='s/conftest\.\$ac_ext/conftest.$ac_objext/g'
-             ax_pthread_link_step=`$as_echo "$ac_link" | sed "$ax_pthread_sed"`
+             ax_pthread_link_step=`AS_ECHO(["$ac_link"]) | sed "$ax_pthread_sed"`
              ax_pthread_2step_ac_link="($ac_compile) && (echo ==== >&5) && ($ax_pthread_link_step)"
              ax_pthread_save_CFLAGS="$CFLAGS"
              for ax_pthread_try in '' -Qunused-arguments -Wno-unused-command-line-argument unknown; do
@@ -482,18 +487,28 @@ if test "x$ax_pthread_ok" = "xyes"; then
                     [#handle absolute path differently from PATH based program lookup
                      AS_CASE(["x$CC"],
                          [x/*],
-                         [AS_IF([AS_EXECUTABLE_P([${CC}_r])],[PTHREAD_CC="${CC}_r"])],
-                         [AC_CHECK_PROGS([PTHREAD_CC],[${CC}_r],[$CC])])])
+                         [
+			   AS_IF([AS_EXECUTABLE_P([${CC}_r])],[PTHREAD_CC="${CC}_r"])
+			   AS_IF([test "x${CXX}" != "x"], [AS_IF([AS_EXECUTABLE_P([${CXX}_r])],[PTHREAD_CXX="${CXX}_r"])])
+			 ],
+                         [
+			   AC_CHECK_PROGS([PTHREAD_CC],[${CC}_r],[$CC])
+			   AS_IF([test "x${CXX}" != "x"], [AC_CHECK_PROGS([PTHREAD_CXX],[${CXX}_r],[$CXX])])
+			 ]
+                     )
+                    ])
                 ;;
             esac
         fi
 fi
 
 test -n "$PTHREAD_CC" || PTHREAD_CC="$CC"
+test -n "$PTHREAD_CXX" || PTHREAD_CXX="$CXX"
 
 AC_SUBST([PTHREAD_LIBS])
 AC_SUBST([PTHREAD_CFLAGS])
 AC_SUBST([PTHREAD_CC])
+AC_SUBST([PTHREAD_CXX])
 
 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
 if test "x$ax_pthread_ok" = "xyes"; then
diff --git a/scripts/reconfigure b/scripts/reconfigure
index 9a5eb6577744753c9cc61548bb3af5d6b59461b2..289c27cc556685cde8c2242119e58e679e5b4bd2 100755
--- a/scripts/reconfigure
+++ b/scripts/reconfigure
@@ -1,15 +1,83 @@
-#! /bin/bash
+#! /usr/bin/env bash
 #
 # This script contains all the necessary steps to recreate the
 # autotools files to build CDI.
 #
-libtoolize --force --copy
-libtoolversion=$(libtoolize --version \
-                      | sed -e 's/^libtoolize \(([^)]*) \)\{0,1\}\([0-9.]*\)/\2/;q')
+# Copyright  (C)  2021  Thomas Jahns <jahns@dkrz.de>
+#
+# Version: 1.0
+# Author: Thomas Jahns <jahns@dkrz.de>
+# Keywords: autotools autogen autoconf libtool automake
+# Maintainer: Thomas Jahns <jahns@dkrz.de>
+# URL: https://swprojects.dkrz.de/redmine/projects/scales-ppm
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are  permitted provided that the following conditions are
+# met:
+#
+# Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# Neither the name of the DKRZ GmbH nor the names of its contributors
+# may be used to endorse or promote products derived from this software
+# without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+set -e
+
+# this should guarantee reproducible results from automake runs
+export PERL_HASH_SEED=0
+
+if [[ -z "${libtoolize:-}" ]]; then
+  case $(uname -s) in
+    Darwin)
+      libtoolize=glibtoolize
+      if [[ -z "${SED+set}" ]] && command -v gsed >/dev/null; then
+        SED=gsed
+      fi
+      ;;
+    *)
+      libtoolize=libtoolize
+      ;;
+  esac
+fi
+
+SED=${SED-sed}
+if sed_version=$($SED --version 2>/dev/null) && [[ $sed_version = *GNU\ sed* ]]; then
+  sed_inplace_options=('-i')
+elif $SED -E </dev/null 2>/dev/null >/dev/null ; then
+  # assume modern FreeBSD sed
+  sed_inplace_options=('-i' '')
+else
+  printf '%s\n' 'Cannot find sed able to operate in place.' \
+    'Please provide full path to GNU or FreeBSD sed in SED environment variable.' \
+    >&2
+  exit 1
+fi
+
+autoreconf -i --force
+$libtoolize --force --copy
+libtoolversion=$($libtoolize --version \
+                      | ${SED} -e 's/^'"${libtoolize##*/}"' \(([^)]*) \)\{0,1\}\([0-9.]*\)/\2/;q')
 declare -a patches
 case "$libtoolversion" in
-  2.4.6)
-    patches=(contrib/00nagfor-libtool-patch/nagfor-libtool-2.4.6.patch \
+  (2.4.6|2.4.7)
+    patches=(contrib/00nagfor-libtool-patch/nagfor-libtool-${libtoolversion}.patch \
       contrib/01aix-deplib-rpath-patch/aix-deplib-libtool.patch \
       contrib/03ltmain-ld-groups-patch/ltmain-ld-groups-libtool-2.4.6.patch \
       contrib/04ltmain-xlinker-patch/ltmain-xlinker-patch.patch \
@@ -25,7 +93,7 @@ case "$libtoolversion" in
     #   2) it is already applied to the Debian-provided libtool and the current
     #      way we apply our patches does not handle such cases.
     ;;
-  2.4.2)
+  (2.4.2)
     patches=(contrib/00nagfor-libtool-patch/nagfor-libtool.patch \
       contrib/01aix-deplib-rpath-patch/aix-deplib-libtool.patch \
       contrib/02nagfor53-shared-patch/nagfor53-shared.patch \
@@ -40,11 +108,19 @@ for patch in "${patches[@]}"; do
   patch -p1 <$patch
 done
 autoreconf -i
-find . -name Makefile.in -print0 | xargs -0 sed -i -e 's/[	 ][	 ]*$//'
+find . -name Makefile.in -exec $SED "${sed_inplace_options[@]}" \
+  -e 's/[	 ][	 ]*$//' \{\} +
 \rm -f config/ltmain.sh.orig m4/libtool.m4.orig
-wget -o /dev/null -O config/config.guess \
+
+if command -v wget >/dev/null ; then
+  DL_CMD=(wget -o /dev/null -O)
+elif command -v curl >/dev/null ; then
+  DL_CMD=(curl -s -o)
+fi
+
+"${DL_CMD[@]}" config/config.guess \
      'http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD'
-wget -o /dev/null -O config/config.sub \
+"${DL_CMD[@]}" config/config.sub \
      'http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD'
 
 #
@@ -53,3 +129,9 @@ wget -o /dev/null -O config/config.sub \
 # if a known good checkout is at orig/libcdi and a newly reconfigured
 # tree is at new/libcdi
 #
+# Local Variables:
+# mode: sh
+# license-project-url: "https://swprojects.dkrz.de/redmine/projects/scales-ppm"
+# license-default: "bsd"
+# End:
+#
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 0000000000000000000000000000000000000000..2504621b85e4069b57845e41519f3d33856cc7fb
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,145 @@
+list( APPEND cdi_src_files
+  async_worker.c
+  async_worker.h
+  basetime.c
+  basetime.h
+  binary.c
+  binary.h
+  calendar.c
+  calendar.h
+  cdf.c
+  cdf.h
+  cdf_config.h
+  cdf_int.c
+  cdf_int.h
+  cdf_lazy_grid.c
+  cdf_lazy_grid.h
+  cdf_read.c
+  cdf_records.c
+  cdf_util.c
+  cdf_util.h
+  cdf_write.c
+  cdi.h
+  cdi_across.c
+  cdi_across.h
+  cdi_att.c
+  cdi_att.h
+  cdi_cksum.c
+  cdi_cksum.h
+  cdi_datetime.c
+  cdi_datetime.h
+  cdi_error.c
+  cdi_fdb.c
+  cdi_fdb.h
+  cdi_int.c
+  cdi_int.h
+  cdi_key.c
+  cdi_key.h
+  cdi_limits.h
+  cdi_query.c
+  cdi_util.c
+  cdi_uuid.h
+  cgribex.h
+  cgribexlib.c
+  cksum.c
+  cksum.h
+  dmemory.c
+  dmemory.h
+  dtypes.h
+  error.c
+  error.h
+  exse.h
+  extra.h
+  extralib.c
+  file.c
+  file.h
+  gaussian_latitudes.c
+  get_num_missvals.c
+  get_num_missvals.h
+  grb_read.c
+  grb_write.c
+  gribapi.c
+  gribapi.h
+  gribapi_utilities.h
+  grid.c
+  grid.h
+  ieg.h
+  ieglib.c
+  input_file.c
+  input_file.h
+  institution.c
+  institution.h
+  iterator.c
+  iterator.h
+  iterator_fallback.c
+  iterator_fallback.h
+  iterator_grib.c
+  iterator_grib.h
+  julian_date.c
+  julian_date.h
+  model.c
+  model.h
+  namespace.c
+  namespace.h
+  normalize_month.h
+  referenceCounting.c
+  referenceCounting.h
+  resource_handle.c
+  resource_handle.h
+  serialize.c
+  serialize.h
+  service.h
+  servicelib.c
+  stream.c
+  stream_cdf.h
+  stream_cdf_i.c
+  stream_cdf_o.c
+  stream_cdf_postdef.h
+  stream_cdf_time.c
+  stream_cgribex.c
+  stream_cgribex.h
+  stream_ext.c
+  stream_ext.h
+  stream_grb.c
+  stream_grb.h
+  stream_gribapi.h
+  stream_ieg.c
+  stream_ieg.h
+  stream_read.c
+  stream_record.c
+  stream_scan.c
+  stream_scan.h
+  stream_srv.c
+  stream_srv.h
+  stream_var.c
+  stream_write.c
+  subtype.c
+  subtype.h
+  swap.c
+  swap.h
+  table.c
+  table.h
+  tablepar.h
+  taxis.c
+  taxis.h
+  tsteps.c
+  util.c
+  varscan.c
+  varscan.h
+  version.c
+  vlist.c
+  vlist.h
+  vlist_var.c
+  vlist_var.h
+  vlist_var_key.c
+  vlist_var_pack.c
+  zaxis.c
+  zaxis.h
+)
+
+add_library(cdilib
+  ${cdi_src_files}
+#  INSTALL_HEADERS_LIST  cdi.h
+)
+
+target_compile_definitions(cdilib PUBLIC HAVE_CONFIG_H)
diff --git a/src/calendar.c b/src/calendar.c
index e35a1a948dbead014cf462fbbe0884373d554ea6..6b84f04b8029bc2f210b5d285226ef8cbc785c67 100644
--- a/src/calendar.c
+++ b/src/calendar.c
@@ -19,7 +19,7 @@ get_dayspermonth_array(int daysPerYear)
 int
 days_per_month(int calendar, int year, int month)
 {
-  const int daysPerYear = calendar_dpy(calendar);
+  int daysPerYear = calendar_dpy(calendar);
   const int *daysPerMonthArray = (daysPerYear == 360) ? month_360 : ((daysPerYear == 365) ? month_365 : month_366);
 
   int daysPerMonth = (month >= 1 && month <= 12) ? daysPerMonthArray[month - 1] : 0;
diff --git a/src/calendar.h b/src/calendar.h
index 6e2a6757abacb5a1185a49c8ddf7d69b6d0de80a..26c073288df4f052f07e1990c996e64bc6216cae 100644
--- a/src/calendar.h
+++ b/src/calendar.h
@@ -6,8 +6,6 @@
 
 // clang-format off
 
-#include "cdi.h"
-
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/src/cdf_lazy_grid.c b/src/cdf_lazy_grid.c
index 4804117cf928e28d76fc81c2e08855138e0fbc1d..625972b971b49c72af166d47a0f7e2c68e7a1ea6 100644
--- a/src/cdf_lazy_grid.c
+++ b/src/cdf_lazy_grid.c
@@ -432,7 +432,7 @@ cdfLazyGridInitOnce(void)
 #else
   */
   cdfPendingLoad = (double *) &cdfPendingLoad;
-  //#endif
+  // #endif
   atexit(cdfLazyGridDestroyOnce);
 #ifndef HAVE_LIBPTHREAD
   cdfLazyInitialized = true;
diff --git a/src/cdf_read.c b/src/cdf_read.c
index 1ecd04ae56283e01405ad18f4f71eed0b3750ba8..654f761f0cf72c2eba5785879b07fed284b0b024 100644
--- a/src/cdf_read.c
+++ b/src/cdf_read.c
@@ -669,7 +669,7 @@ cdfReadDataSP(stream_t *streamptr, int varID, size_t length, size_t start[MAX_DI
 }
 
 static void
-cdfReadVarDP(stream_t *streamptr, int varID, double *data, size_t *nmiss)
+cdfReadVarDP(stream_t *streamptr, int varID, double *data, size_t *numMissVals)
 {
   if (CDI_Debug) Message("streamID = %d  varID = %d", streamptr->self, varID);
 
@@ -681,11 +681,11 @@ cdfReadVarDP(stream_t *streamptr, int varID, double *data, size_t *nmiss)
   size_t length = getSizeVar3D(vlistID, varID);
   cdfReadDataDP(streamptr, varID, length, start, count, data);
 
-  *nmiss = cdfDoInputDataTransformationDP(vlistID, varID, length, data);
+  *numMissVals = cdfDoInputDataTransformationDP(vlistID, varID, length, data);
 }
 
 static void
-cdfReadVarSP(stream_t *streamptr, int varID, float *data, size_t *nmiss)
+cdfReadVarSP(stream_t *streamptr, int varID, float *data, size_t *numMissVals)
 {
   if (CDI_Debug) Message("streamID = %d  varID = %d", streamptr->self, varID);
 
@@ -697,20 +697,20 @@ cdfReadVarSP(stream_t *streamptr, int varID, float *data, size_t *nmiss)
   size_t length = getSizeVar3D(vlistID, varID);
   cdfReadDataSP(streamptr, varID, length, start, count, data);
 
-  *nmiss = cdfDoInputDataTransformationSP(vlistID, varID, length, data);
+  *numMissVals = cdfDoInputDataTransformationSP(vlistID, varID, length, data);
 }
 
 void
-cdf_read_var(stream_t *streamptr, int varID, int memtype, void *data, size_t *nmiss)
+cdf_read_var(stream_t *streamptr, int varID, int memtype, void *data, size_t *numMissVals)
 {
   if (memtype == MEMTYPE_DOUBLE)
-    cdfReadVarDP(streamptr, varID, (double *) data, nmiss);
+    cdfReadVarDP(streamptr, varID, (double *) data, numMissVals);
   else
-    cdfReadVarSP(streamptr, varID, (float *) data, nmiss);
+    cdfReadVarSP(streamptr, varID, (float *) data, numMissVals);
 }
 
 static void
-cdf_read_var_slice_DP(stream_t *streamptr, int tsID, int varID, int levelID, double *data, size_t *nmiss)
+cdf_read_var_slice_DP(stream_t *streamptr, int tsID, int varID, int levelID, double *data, size_t *numMissVals)
 {
   if (CDI_Debug) Message("streamID=%d  tsID=%d varID=%d  levelID=%d", streamptr->self, tsID, varID, levelID);
 
@@ -725,17 +725,17 @@ cdf_read_var_slice_DP(stream_t *streamptr, int tsID, int varID, int levelID, dou
 
   if (swapxy) transpose2dArrayDP(gridId, data);
 
-  *nmiss = cdfDoInputDataTransformationDP(vlistID, varID, length, data);
+  *numMissVals = cdfDoInputDataTransformationDP(vlistID, varID, length, data);
 }
 
 static void
-cdfReadVarSliceDP(stream_t *streamptr, int varID, int levelID, double *data, size_t *nmiss)
+cdfReadVarSliceDP(stream_t *streamptr, int varID, int levelID, double *data, size_t *numMissVals)
 {
-  cdf_read_var_slice_DP(streamptr, streamptr->curTsID, varID, levelID, data, nmiss);
+  cdf_read_var_slice_DP(streamptr, streamptr->curTsID, varID, levelID, data, numMissVals);
 }
 
 static void
-cdf_read_var_slice_SP(stream_t *streamptr, int tsID, int varID, int levelID, float *data, size_t *nmiss)
+cdf_read_var_slice_SP(stream_t *streamptr, int tsID, int varID, int levelID, float *data, size_t *numMissVals)
 {
   if (CDI_Debug) Message("streamID=%d  tsID=%d varID=%d  levelID=%d", streamptr->self, tsID, varID, levelID);
 
@@ -750,22 +750,22 @@ cdf_read_var_slice_SP(stream_t *streamptr, int tsID, int varID, int levelID, flo
 
   if (swapxy) transpose2dArraySP(gridId, data);
 
-  *nmiss = cdfDoInputDataTransformationSP(vlistID, varID, length, data);
+  *numMissVals = cdfDoInputDataTransformationSP(vlistID, varID, length, data);
 }
 
 static void
-cdfReadVarSliceSP(stream_t *streamptr, int varID, int levelID, float *data, size_t *nmiss)
+cdfReadVarSliceSP(stream_t *streamptr, int varID, int levelID, float *data, size_t *numMissVals)
 {
-  cdf_read_var_slice_SP(streamptr, streamptr->curTsID, varID, levelID, data, nmiss);
+  cdf_read_var_slice_SP(streamptr, streamptr->curTsID, varID, levelID, data, numMissVals);
 }
 
 void
-cdf_read_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, void *data, size_t *nmiss)
+cdf_read_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, void *data, size_t *numMissVals)
 {
   if (memtype == MEMTYPE_DOUBLE)
-    cdfReadVarSliceDP(streamptr, varID, levelID, (double *) data, nmiss);
+    cdfReadVarSliceDP(streamptr, varID, levelID, (double *) data, numMissVals);
   else
-    cdfReadVarSliceSP(streamptr, varID, levelID, (float *) data, nmiss);
+    cdfReadVarSliceSP(streamptr, varID, levelID, (float *) data, numMissVals);
 }
 
 typedef struct JobArgs
@@ -774,7 +774,7 @@ typedef struct JobArgs
   int varID, levelID;
   int recID, tsID, memtype;
   void *data;
-  size_t gridsize, nmiss;
+  size_t gridsize, numMissVals;
 } JobArgs;
 
 static int
@@ -783,9 +783,9 @@ cdf_read_data_async(void *untypedArgs)
   JobArgs *args = (JobArgs *) untypedArgs;
 
   if (args->memtype == MEMTYPE_DOUBLE)
-    cdf_read_var_slice_DP(args->streamptr, args->tsID, args->varID, args->levelID, (double *) args->data, &args->nmiss);
+    cdf_read_var_slice_DP(args->streamptr, args->tsID, args->varID, args->levelID, (double *) args->data, &args->numMissVals);
   else
-    cdf_read_var_slice_SP(args->streamptr, args->tsID, args->varID, args->levelID, (float *) args->data, &args->nmiss);
+    cdf_read_var_slice_SP(args->streamptr, args->tsID, args->varID, args->levelID, (float *) args->data, &args->numMissVals);
 
   return 0;
 }
@@ -797,13 +797,13 @@ cdf_read_data(stream_t *streamptr, int recID, int memtype, void *data)
   int varID = streamptr->tsteps[tsID].records[recID].varID;
   int levelID = streamptr->tsteps[tsID].records[recID].levelID;
 
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
   if (memtype == MEMTYPE_DOUBLE)
-    cdf_read_var_slice_DP(streamptr, tsID, varID, levelID, (double *) data, &nmiss);
+    cdf_read_var_slice_DP(streamptr, tsID, varID, levelID, (double *) data, &numMissVals);
   else
-    cdf_read_var_slice_SP(streamptr, tsID, varID, levelID, (float *) data, &nmiss);
+    cdf_read_var_slice_SP(streamptr, tsID, varID, levelID, (float *) data, &numMissVals);
 
-  return nmiss;
+  return numMissVals;
 }
 
 typedef struct JobDescriptor
@@ -830,7 +830,7 @@ job_args_init(stream_t *streamptr, int tsID, int recID, int memtype, void *data)
     .memtype = memtype,
     .data = data,
     .gridsize = gridsize,
-    .nmiss = 0,
+    .numMissVals = 0,
   };
 }
 
@@ -843,11 +843,11 @@ JobDescriptor_startJob(AsyncManager *jobManager, JobDescriptor *me, stream_t *st
 }
 
 static void
-JobDescriptor_finishJob(AsyncManager *jobManager, JobDescriptor *me, void *data, size_t *nmiss)
+JobDescriptor_finishJob(AsyncManager *jobManager, JobDescriptor *me, void *data, size_t *numMissVals)
 {
   if (AsyncWorker_wait(jobManager, me->job)) xabort("error executing job in worker thread");
   memcpy(data, me->args.data, me->args.gridsize * ((me->args.memtype == MEMTYPE_FLOAT) ? sizeof(float) : sizeof(double)));
-  *nmiss = me->args.nmiss;
+  *numMissVals = me->args.numMissVals;
 
   Free(me->args.data);
   me->args.recID = -1;  // mark as inactive
@@ -897,7 +897,7 @@ read_next_record(AsyncManager *jobManager, JobDescriptor *jd, stream_t *streampt
 }
 
 static void
-cdf_read_next_record(stream_t *streamptr, int recID, int memtype, void *data, size_t *nmiss)
+cdf_read_next_record(stream_t *streamptr, int recID, int memtype, void *data, size_t *numMissVals)
 {
   bool jobFound = false;
 
@@ -934,24 +934,24 @@ cdf_read_next_record(stream_t *streamptr, int recID, int memtype, void *data, si
           if (jd->args.recID == recID && jd->args.tsID == tsID)
             {
               jobFound = true;
-              JobDescriptor_finishJob(jobManager, jd, data, nmiss);
+              JobDescriptor_finishJob(jobManager, jd, data, numMissVals);
               if (streamptr->nextGlobalRecId < streamptr->maxGlobalRecs) read_next_record(jobManager, jd, streamptr, memtype);
             }
         }
     }
 
   // perform the work synchronously if we didn't start a job for it yet
-  if (!jobFound) *nmiss = cdf_read_data(streamptr, recID, memtype, data);
+  if (!jobFound) *numMissVals = cdf_read_data(streamptr, recID, memtype, data);
 }
 
 void
-cdf_read_record(stream_t *streamptr, int memtype, void *data, size_t *nmiss)
+cdf_read_record(stream_t *streamptr, int memtype, void *data, size_t *numMissVals)
 {
   int tsID = streamptr->curTsID;
   int vrecID = streamptr->tsteps[tsID].curRecID;
   int recID = streamptr->tsteps[tsID].recIDs[vrecID];
 
-  cdf_read_next_record(streamptr, recID, memtype, data, nmiss);
+  cdf_read_next_record(streamptr, recID, memtype, data, numMissVals);
 }
 
 //----------------------------------------------------------------------------
@@ -960,7 +960,7 @@ cdf_read_record(stream_t *streamptr, int memtype, void *data, size_t *nmiss)
 
 void
 cdfReadVarSliceDPPart(stream_t *streamptr, int varID, int levelID, int varType, int startpoint, size_t length, double *data,
-                      size_t *nmiss)
+                      size_t *numMissVals)
 {
   (void) (varType);
 
@@ -986,12 +986,12 @@ cdfReadVarSliceDPPart(stream_t *streamptr, int varID, int levelID, int varType,
 
   if (swapxy) transpose2dArrayDP(gridId, data);
 
-  *nmiss = cdfDoInputDataTransformationDP(vlistID, varID, length, data);
+  *numMissVals = cdfDoInputDataTransformationDP(vlistID, varID, length, data);
 }
 
 void
 cdfReadVarSliceSPPart(stream_t *streamptr, int varID, int levelID, int varType, int startpoint, size_t length, float *data,
-                      size_t *nmiss)
+                      size_t *numMissVals)
 {
   (void) (varType);
 
@@ -1017,24 +1017,24 @@ cdfReadVarSliceSPPart(stream_t *streamptr, int varID, int levelID, int varType,
 
   if (swapxy) transpose2dArraySP(gridId, data);
 
-  *nmiss = cdfDoInputDataTransformationSP(vlistID, varID, length, data);
+  *numMissVals = cdfDoInputDataTransformationSP(vlistID, varID, length, data);
 }
 
 static int
 cdiStreamReadVarSlicePart(int streamID, int varID, int levelID, int varType, int start, size_t size, int memtype, void *data,
-                          size_t *nmiss)
+                          size_t *numMissVals)
 {
   int status = 0;
 
   if (CDI_Debug) Message("streamID = %d  varID = %d", streamID, varID);
 
   check_parg(data);
-  check_parg(nmiss);
+  check_parg(numMissVals);
 
   stream_t *streamptr = stream_to_pointer(streamID);
   int filetype = streamptr->filetype;
 
-  *nmiss = 0;
+  *numMissVals = 0;
 
   // currently we only care for netcdf data
   switch (cdiBaseFiletype(filetype))
@@ -1042,7 +1042,7 @@ cdiStreamReadVarSlicePart(int streamID, int varID, int levelID, int varType, int
 #ifdef HAVE_LIBGRIB
     case CDI_FILETYPE_GRIB:
       {
-        grb_read_var_slice(streamptr, varID, levelID, memtype, data, nmiss);
+        grb_read_var_slice(streamptr, varID, levelID, memtype, data, numMissVals);
         break;
       }
 #endif
@@ -1050,9 +1050,9 @@ cdiStreamReadVarSlicePart(int streamID, int varID, int levelID, int varType, int
     case CDI_FILETYPE_NETCDF:
       {
         if (memtype == MEMTYPE_FLOAT)
-          cdfReadVarSliceSPPart(streamptr, varID, levelID, varType, start, size, (float *) data, nmiss);
+          cdfReadVarSliceSPPart(streamptr, varID, levelID, varType, start, size, (float *) data, numMissVals);
         else
-          cdfReadVarSliceDPPart(streamptr, varID, levelID, varType, start, size, (double *) data, nmiss);
+          cdfReadVarSliceDPPart(streamptr, varID, levelID, varType, start, size, (double *) data, numMissVals);
         break;
       }
 #endif
@@ -1068,7 +1068,7 @@ cdiStreamReadVarSlicePart(int streamID, int varID, int levelID, int varType, int
 }
 
 void
-cdfReadVarDPPart(stream_t *streamptr, int varID, int varType, int startpoint, size_t length, double *data, size_t *nmiss)
+cdfReadVarDPPart(stream_t *streamptr, int varID, int varType, int startpoint, size_t length, double *data, size_t *numMissVals)
 {
   (void) (varType);
   if (CDI_Debug) Message("streamID = %d  varID = %d", streamptr->self, varID);
@@ -1085,11 +1085,11 @@ cdfReadVarDPPart(stream_t *streamptr, int varID, int varType, int startpoint, si
 
   cdf_get_vara_double(streamptr->fileID, ncvarid, start, count, data);
 
-  *nmiss = cdfDoInputDataTransformationDP(vlistID, varID, length, data);
+  *numMissVals = cdfDoInputDataTransformationDP(vlistID, varID, length, data);
 }
 
 void
-cdfReadVarSPPart(stream_t *streamptr, int varID, int varType, int startpoint, size_t length, float *data, size_t *nmiss)
+cdfReadVarSPPart(stream_t *streamptr, int varID, int varType, int startpoint, size_t length, float *data, size_t *numMissVals)
 {
   (void) (varType);
   if (CDI_Debug) Message("streamID = %d  varID = %d", streamptr->self, varID);
@@ -1106,22 +1106,22 @@ cdfReadVarSPPart(stream_t *streamptr, int varID, int varType, int startpoint, si
 
   cdf_get_vara_float(streamptr->fileID, ncvarid, start, count, data);
 
-  *nmiss = cdfDoInputDataTransformationSP(vlistID, varID, length, data);
+  *numMissVals = cdfDoInputDataTransformationSP(vlistID, varID, length, data);
 }
 
 static void
-cdiStreamReadVarPart(int streamID, int varID, int varType, int start, size_t size, int memtype, void *data, size_t *nmiss)
+cdiStreamReadVarPart(int streamID, int varID, int varType, int start, size_t size, int memtype, void *data, size_t *numMissVals)
 {
   (void) (varType);
   if (CDI_Debug) Message("streamID = %d  varID = %d", streamID, varID);
 
   check_parg(data);
-  check_parg(nmiss);
+  check_parg(numMissVals);
 
   stream_t *streamptr = stream_to_pointer(streamID);
   int filetype = streamptr->filetype;
 
-  *nmiss = 0;
+  *numMissVals = 0;
 
   // currently we only care for netcdf data
   switch (cdiBaseFiletype(filetype))
@@ -1129,7 +1129,7 @@ cdiStreamReadVarPart(int streamID, int varID, int varType, int start, size_t siz
 #ifdef HAVE_LIBGRIB
     case CDI_FILETYPE_GRIB:
       {
-        grb_read_var(streamptr, varID, memtype, data, nmiss);
+        grb_read_var(streamptr, varID, memtype, data, numMissVals);
         break;
       }
 #endif
@@ -1137,9 +1137,9 @@ cdiStreamReadVarPart(int streamID, int varID, int varType, int start, size_t siz
     case CDI_FILETYPE_NETCDF:
       {
         if (memtype == MEMTYPE_FLOAT)
-          cdfReadVarSPPart(streamptr, varID, varType, start, size, (float *) data, nmiss);
+          cdfReadVarSPPart(streamptr, varID, varType, start, size, (float *) data, numMissVals);
         else
-          cdfReadVarDPPart(streamptr, varID, varType, start, size, (double *) data, nmiss);
+          cdfReadVarDPPart(streamptr, varID, varType, start, size, (double *) data, numMissVals);
 
         break;
       }
@@ -1153,23 +1153,23 @@ cdiStreamReadVarPart(int streamID, int varID, int varType, int start, size_t siz
 }
 
 void
-streamReadVarSlicePart(int streamID, int varID, int levelID, int varType, int start, SizeType size, void *data, SizeType *nmiss,
-                       int memtype)
+streamReadVarSlicePart(int streamID, int varID, int levelID, int varType, int start, SizeType size, void *data,
+                       SizeType *numMissVals, int memtype)
 {
   size_t numMiss = 0;
   if (cdiStreamReadVarSlicePart(streamID, varID, levelID, varType, start, size, memtype, data, &numMiss))
     {
       Error("Unexpected error returned from cdiStreamReadVarSlicePart()!");
     }
-  *nmiss = (SizeType) numMiss;
+  *numMissVals = (SizeType) numMiss;
 }
 
 void
-streamReadVarPart(int streamID, int varID, int varType, int start, SizeType size, void *data, SizeType *nmiss, int memtype)
+streamReadVarPart(int streamID, int varID, int varType, int start, SizeType size, void *data, SizeType *numMissVals, int memtype)
 {
   size_t numMiss = 0;
   cdiStreamReadVarPart(streamID, varID, varType, start, size, memtype, data, &numMiss);
-  *nmiss = (SizeType) numMiss;
+  *numMissVals = (SizeType) numMiss;
 }
 
 #endif /* HAVE_LIBNETCDF */
diff --git a/src/cdf_write.c b/src/cdf_write.c
index cafd1f1f28ab8e060be398e45693b22e39722ac8..89f459aade4aaf937777e5ee50d061685d0a1404 100644
--- a/src/cdf_write.c
+++ b/src/cdf_write.c
@@ -855,12 +855,13 @@ cdfCheckVarname(int fileID, char name[CDI_MAX_NAME])
       int ncvarID;
       char varname[CDI_MAX_NAME];
       snprintf(varname, sizeof(varname), "%s", name);
-      char *varname2 = varname + strlen(varname);
+      size_t len = strlen(varname);
+      char *varname2 = varname + len;
       unsigned iz = 0;
 
       do
         {
-          if (iz) sprintf(varname2, "_%u", iz + 1);
+          if (iz) snprintf(varname2, sizeof(varname) - len, "_%u", iz + 1);
 
           if (nc_inq_varid(fileID, varname, &ncvarID) != NC_NOERR) break;
 
@@ -891,13 +892,14 @@ cdfGenVarname(int fileID, char name[CDI_MAX_NAME], int pnum, int pcat, int *pdis
   else
     snprintf(varname, sizeof(varname), "param%d.%d.%d", pnum, pcat, *pdis);
 
-  char *varname2 = varname + strlen(varname);
+  size_t len = strlen(varname);
+  char *varname2 = varname + len;
   int ncvarID;
   unsigned iz = 0;
 
   do
     {
-      if (iz) sprintf(varname2, "_%u", iz + 1);
+      if (iz) snprintf(varname2, sizeof(varname) - len, "_%u", iz + 1);
 
       if (nc_inq_varid(fileID, varname, &ncvarID) != NC_NOERR) break;
 
@@ -1109,7 +1111,7 @@ cdfWriteGridTraj(stream_t *streamptr, int gridID)
 
 static void
 cdf_write_var_data(int fileID, int vlistID, int varID, int ncvarID, int dtype, size_t nvals, size_t xsize, size_t ysize,
-                   bool swapxy, size_t *start, size_t *count, int memtype, const void *data, size_t nmiss)
+                   bool swapxy, size_t *start, size_t *count, int memtype, const void *data, size_t numMissVals)
 {
   const double *pdata_dp = (const double *) data;
   double *mdata_dp = NULL;
@@ -1133,7 +1135,7 @@ cdf_write_var_data(int fileID, int vlistID, int varID, int ncvarID, int dtype, s
             memcpy(mdata_sp, pdata_sp, nvals * sizeof(float));
             pdata_sp = mdata_sp;
 
-            if (nmiss > 0)
+            if (numMissVals > 0)
               {
                 for (size_t i = 0; i < nvals; ++i)
                   {
@@ -1163,7 +1165,7 @@ cdf_write_var_data(int fileID, int vlistID, int varID, int ncvarID, int dtype, s
             memcpy(mdata_dp, pdata_dp, nvals * sizeof(double));
             pdata_dp = mdata_dp;
 
-            if (nmiss > 0)
+            if (numMissVals > 0)
               {
                 for (size_t i = 0; i < nvals; ++i)
                   {
@@ -1261,7 +1263,7 @@ cdf_write_var_data(int fileID, int vlistID, int varID, int ncvarID, int dtype, s
               }
           }
 
-        Message("nvals = %zu, nmiss = %d, missval = %g, minval = %g, maxval = %g", nvals, nmiss, missval, fmin, fmax);
+        Message("nvals = %zu, numMissVals = %d, missval = %g, minval = %g, maxval = %g", nvals, numMissVals, missval, fmin, fmax);
       }
   }
 
@@ -1407,7 +1409,7 @@ cdfDefineStartAndCount(stream_t *streamptr, int varID, int xid, int yid, int zid
 }
 
 void
-cdf_write_var(stream_t *streamptr, int varID, int memtype, const void *data, size_t nmiss)
+cdf_write_var(stream_t *streamptr, int varID, int memtype, const void *data, size_t numMissVals)
 {
   if (streamptr->accessmode == 0) cdfEndDef(streamptr);
 
@@ -1436,12 +1438,12 @@ cdf_write_var(stream_t *streamptr, int varID, int memtype, const void *data, siz
 
   int dtype = vlistInqVarDatatype(vlistID, varID);
 
-  if (nmiss > 0) cdfDefVarMissval(streamptr, varID, dtype, 1);
+  if (numMissVals > 0) cdfDefVarMissval(streamptr, varID, dtype, 1);
 
   size_t nvals = gridInqSize(gridID) * (size_t) (zaxisInqSize(zaxisID));
 
   bool swapxy = false;
-  cdf_write_var_data(fileID, vlistID, varID, ncvarID, dtype, nvals, xsize, ysize, swapxy, start, count, memtype, data, nmiss);
+  cdf_write_var_data(fileID, vlistID, varID, ncvarID, dtype, nvals, xsize, ysize, swapxy, start, count, memtype, data, numMissVals);
 }
 
 static void
@@ -1502,7 +1504,7 @@ cdfDefineStartAndCountChunk(stream_t *streamptr, const int rect[][2], int varID,
 }
 
 void
-cdf_write_var_chunk(stream_t *streamptr, int varID, int memtype, const int rect[][2], const void *data, size_t nmiss)
+cdf_write_var_chunk(stream_t *streamptr, int varID, int memtype, const int rect[][2], const void *data, size_t numMissVals)
 {
   if (streamptr->accessmode == 0) cdfEndDef(streamptr);
 
@@ -1533,12 +1535,12 @@ cdf_write_var_chunk(stream_t *streamptr, int varID, int memtype, const int rect[
 
   int dtype = vlistInqVarDatatype(vlistID, varID);
 
-  if (nmiss > 0) cdfDefVarMissval(streamptr, varID, dtype, 1);
+  if (numMissVals > 0) cdfDefVarMissval(streamptr, varID, dtype, 1);
 
   size_t nvals = gridInqSize(gridID) * (size_t) (zaxisInqSize(zaxisID));
 
   bool swapxy = false;
-  cdf_write_var_data(fileID, vlistID, varID, ncvarID, dtype, nvals, xsize, ysize, swapxy, start, count, memtype, data, nmiss);
+  cdf_write_var_data(fileID, vlistID, varID, ncvarID, dtype, nvals, xsize, ysize, swapxy, start, count, memtype, data, numMissVals);
 }
 
 static void
@@ -1593,7 +1595,7 @@ cdfDefineStartAndCountSlice(stream_t *streamptr, int varID, int levelID, int dim
 }
 
 void
-cdf_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, const void *data, size_t nmiss)
+cdf_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, const void *data, size_t numMissVals)
 {
   if (streamptr->accessmode == 0) cdfEndDef(streamptr);
 
@@ -1620,19 +1622,19 @@ cdf_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, co
 
   int dtype = vlistInqVarDatatype(vlistID, varID);
 
-  if (nmiss > 0) cdfDefVarMissval(streamptr, varID, dtype, 1);
+  if (numMissVals > 0) cdfDefVarMissval(streamptr, varID, dtype, 1);
 
   size_t nvals = gridInqSize(gridID);
 
-  cdf_write_var_data(fileID, vlistID, varID, ncvarID, dtype, nvals, xsize, ysize, swapxy, start, count, memtype, data, nmiss);
+  cdf_write_var_data(fileID, vlistID, varID, ncvarID, dtype, nvals, xsize, ysize, swapxy, start, count, memtype, data, numMissVals);
 }
 
 void
-cdf_write_record(stream_t *streamptr, int memtype, const void *data, size_t nmiss)
+cdf_write_record(stream_t *streamptr, int memtype, const void *data, size_t numMissVals)
 {
   int varID = streamptr->record->varID;
   int levelID = streamptr->record->levelID;
-  cdf_write_var_slice(streamptr, varID, levelID, memtype, data, nmiss);
+  cdf_write_var_slice(streamptr, varID, levelID, memtype, data, numMissVals);
 }
 
 #endif
diff --git a/src/cdi.h b/src/cdi.h
index eed826b95864ee063d2c17525d1842a69ba6a9a0..9e4e48c311de2192b3875cd2ff00be71f36783bf 100644
--- a/src/cdi.h
+++ b/src/cdi.h
@@ -422,35 +422,35 @@ int     streamInqNvars(int streamID);
 // STREAM var I/O routines (random access)
 
 //      streamWriteVar: Write a variable
-void    streamWriteVar(int streamID, int varID, const double data[], SizeType nmiss);
-void    streamWriteVarF(int streamID, int varID, const float data[], SizeType nmiss);
+void    streamWriteVar(int streamID, int varID, const double data[], SizeType numMissVals);
+void    streamWriteVarF(int streamID, int varID, const float data[], SizeType numMissVals);
 
 //      streamReadVar: Read a variable
-void    streamReadVar(int streamID, int varID, double data[], SizeType *nmiss);
-void    streamReadVarF(int streamID, int varID, float data[], SizeType *nmiss);
-void    streamReadVarPart(int streamID, int varID, int varType, int start, SizeType size, void *data, SizeType *nmiss, int memtype);
+void    streamReadVar(int streamID, int varID, double data[], SizeType *numMissVals);
+void    streamReadVarF(int streamID, int varID, float data[], SizeType *numMissVals);
+void    streamReadVarPart(int streamID, int varID, int varType, int start, SizeType size, void *data, SizeType *numMissVals, int memtype);
 
 //      streamWriteVarSlice: Write a horizontal slice of a variable
-void    streamWriteVarSlice(int streamID, int varID, int levelID, const double data[], SizeType nmiss);
-void    streamWriteVarSliceF(int streamID, int varID, int levelID, const float data[], SizeType nmiss);
-void    streamReadVarSlicePart(int streamID, int varID, int levelID, int varType, int start, SizeType size, void *data, SizeType *nmiss, int memtype);
+void    streamWriteVarSlice(int streamID, int varID, int levelID, const double data[], SizeType numMissVals);
+void    streamWriteVarSliceF(int streamID, int varID, int levelID, const float data[], SizeType numMissVals);
+void    streamReadVarSlicePart(int streamID, int varID, int levelID, int varType, int start, SizeType size, void *data, SizeType *numMissVals, int memtype);
 
 //      streamReadVarSlice: Read a horizontal slice of a variable
-void    streamReadVarSlice(int streamID, int varID, int levelID, double data[], SizeType *nmiss);
-void    streamReadVarSliceF(int streamID, int varID, int levelID, float data[], SizeType *nmiss);
+void    streamReadVarSlice(int streamID, int varID, int levelID, double data[], SizeType *numMissVals);
+void    streamReadVarSliceF(int streamID, int varID, int levelID, float data[], SizeType *numMissVals);
 
-void    streamWriteVarChunk(int streamID, int varID, const int rect[][2], const double data[], SizeType nmiss);
-void    streamWriteVarChunkF(int streamID, int varID, const int rect[][2], const float data[], SizeType nmiss);
+void    streamWriteVarChunk(int streamID, int varID, const int rect[][2], const double data[], SizeType numMissVals);
+void    streamWriteVarChunkF(int streamID, int varID, const int rect[][2], const float data[], SizeType numMissVals);
 
 
 // STREAM record I/O routines (sequential access)
 
 void    streamDefRecord(int streamID, int  varID, int  levelID);
 void    streamInqRecord(int streamID, int *varID, int *levelID);
-void    streamWriteRecord(int streamID, const double data[], SizeType nmiss);
-void    streamWriteRecordF(int streamID, const float data[], SizeType nmiss);
-void    streamReadRecord(int streamID, double data[], SizeType *nmiss);
-void    streamReadRecordF(int streamID, float data[], SizeType *nmiss);
+void    streamWriteRecord(int streamID, const double data[], SizeType numMissVals);
+void    streamWriteRecordF(int streamID, const float data[], SizeType numMissVals);
+void    streamReadRecord(int streamID, double data[], SizeType *numMissVals);
+void    streamReadRecordF(int streamID, float data[], SizeType *numMissVals);
 void    streamCopyRecord(int streamIDdest, int streamIDsrc);
 
 void    streamInqGRIBinfo(int streamID, int *intnum, float *fltnum, off_t *bignum);
@@ -489,8 +489,8 @@ char *cdiIterator_inqVariableName(CdiIterator *me);     // The caller is respons
 int cdiIterator_inqGridId(CdiIterator *me);             // The returned id is only valid until the next call to cdiIteratorNextField().
 
 // Reading data
-void cdiIterator_readField(CdiIterator *me, double data[], SizeType *nmiss_optional);
-void cdiIterator_readFieldF(CdiIterator *me, float data[], SizeType *nmiss_optional);
+void cdiIterator_readField(CdiIterator *me, double data[], SizeType *numMissVals_optional);
+void cdiIterator_readFieldF(CdiIterator *me, float data[], SizeType *numMissVals_optional);
 // TODO[NH]: Add functions to read partial fields.
 
 
diff --git a/src/cdi_datetime.c b/src/cdi_datetime.c
index f873e7a2b9f768a09dde83b2c482c1a04a40a043..b85463627531c57994105d8d7ceee5a9b8929434 100644
--- a/src/cdi_datetime.c
+++ b/src/cdi_datetime.c
@@ -1,3 +1,15 @@
+/* DO NOT REMOVE the config.h include file under any circumstances,
+ * it's very much needed on some platforms */
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+/* DO NOT REMOVE the above config.h include file under any
+ * circumstances as long as it's the autoconf configuration header
+ * used to build this package. When it's missing on some platforms,
+ * some poor person has to do long, tedious debugging sessions, where
+ * struct offsets almost imperceptibly change from one file to the
+ * next to find out what happened */
+
 #include "cdi_datetime.h"
 #include <stdio.h>
 #include <stdlib.h>
@@ -56,12 +68,12 @@ CdiDate
 cdiDate_set(int64_t date)
 {
   int64_t iyear = date / 10000;
-  int year = iyear;
+  int year = (int) iyear;
   int64_t idate = date - iyear * 10000;
   if (idate < 0) idate = -idate;
   int64_t imonth = idate / 100;
-  int month = imonth;
-  int day = idate - imonth * 100;
+  int month = (int) imonth;
+  int day = (int) (idate - imonth * 100);
 
   CdiDate cdiDate;
   cdiDate.year = year;
@@ -81,7 +93,7 @@ cdiTime_set(int time)
   cdiTime.hour = (short) hour;
   cdiTime.minute = (short) minute;
   cdiTime.second = (short) second;
-  cdiTime.ms = ms;
+  cdiTime.ms = (short) ms;
 
   return cdiTime;
 }
@@ -117,8 +129,8 @@ cdiDate_encode(int year, int month, int day)
 {
   CdiDate cdiDate;
   cdiDate.year = year;
-  cdiDate.month = month;
-  cdiDate.day = day;
+  cdiDate.month = (short) month;
+  cdiDate.day = (short) day;
 
   return cdiDate;
 }
@@ -135,10 +147,10 @@ CdiTime
 cdiTime_encode(int hour, int minute, int second, int ms)
 {
   CdiTime cdiTime;
-  cdiTime.hour = hour;
-  cdiTime.minute = minute;
-  cdiTime.second = second;
-  cdiTime.ms = ms;
+  cdiTime.hour = (short) hour;
+  cdiTime.minute = (short) minute;
+  cdiTime.second = (short) second;
+  cdiTime.ms = (short) ms;
 
   return cdiTime;
 }
diff --git a/src/cdi_fdb.c b/src/cdi_fdb.c
index 6e0180316b1f1287a00bebb668b8f529035b5197..49a902263ac5912a42a189a1fd94c6b76f9f82f2 100644
--- a/src/cdi_fdb.c
+++ b/src/cdi_fdb.c
@@ -13,7 +13,28 @@ int cdi_fdb_dummy;
 void ensureBufferSize(size_t requiredSize, size_t *curSize, void **buffer);
 
 void
-decode_fdbitem(const char *fdbItem, KeyValueEntry *keyValue)
+check_fdb_error(int errorNum)
+{
+  // This routine provides a simple interface to FDB error message routine.
+  if (errorNum != FDB_SUCCESS) Error("%s", fdb_error_string(errorNum));
+}
+
+void
+cdi_fdb_delete_kvlist(int numItems, fdbKeyValueEntry *keyValueList)
+{
+  for (int i = 0; i < numItems; ++i)
+    {
+      for (int k = 0; k < keyValueList[i].numKeys; ++k)
+        {
+          if (keyValueList[i].keys[k]) free(keyValueList[i].keys[k]);
+          if (keyValueList[i].values[k]) free(keyValueList[i].values[k]);
+        }
+    }
+  free(keyValueList);
+}
+
+void
+decode_fdbitem(const char *fdbItem, KeyValueItem *keyValue)
 {
   keyValue->item = strdup(fdbItem);
   char *pItem = keyValue->item;
@@ -47,9 +68,10 @@ decode_fdbitem(const char *fdbItem, KeyValueEntry *keyValue)
         }
     }
 
-  keyValue->numKeys = numKeys;
+  // keyValue->numKeys = numKeys;
   // for (int i = 0; i < numKeys; i++)  printf("%d <%s>\n", i, itemKeys[i]);
 
+  int n = 0;
   for (int i = 0; i < numKeys; i++)
     {
       char *itemKey = itemKeys[i];
@@ -57,6 +79,7 @@ decode_fdbitem(const char *fdbItem, KeyValueEntry *keyValue)
       for (int k = 0; k < len; k++)
         if (itemKey[k] == '=')
           {
+            n++;
             itemKey[k] = 0;
             itemValues[i] = &itemKey[k + 1];
             break;
@@ -64,6 +87,8 @@ decode_fdbitem(const char *fdbItem, KeyValueEntry *keyValue)
 
       // printf("key <%s> value <%s>\n", itemKeys[i], itemValues[i]);
     }
+
+  keyValue->numKeys = n;
 }
 
 static int
@@ -73,90 +98,106 @@ fdb_request_add1(fdb_request_t *req, const char *param, const char *value)
 }
 
 static void
-fdbitem_to_request(const char *fdbItem, fdb_request_t *request)
+fdbitem_to_request(const fdbKeyValueEntry *keyValue, fdb_request_t *request)
 {
-  KeyValueEntry keyValue;
-  keyValue.item = NULL;
-  decode_fdbitem(fdbItem, &keyValue);
-
-  for (int i = 0; i < keyValue.numKeys; i++)
+  // printf("numKeys: %d\n", keyValue->numKeys);
+  for (int i = 0; i < keyValue->numKeys; i++)
     {
-      // printf("key <%s> value <%s>\n", keyValue.keys[i], keyValue.values[i]);
-      fdb_request_add1(request, keyValue.keys[i], keyValue.values[i]);
+      // printf("%d: key <%s> value <%s>\n", i, keyValue->keys[i], keyValue->values[i]);
+      check_fdb_error(fdb_request_add1(request, keyValue->keys[i], keyValue->values[i]));
     }
-
-  if (keyValue.item) free(keyValue.item);
 }
 
 int
-fdb_fill_itemlist(fdb_handle_t *fdb, fdb_request_t *request, char ***itemList)
+cdi_fdb_fill_kvlist(fdb_handle_t *fdb, fdb_request_t *request, fdbKeyValueEntry **pkeyValueList)
 {
-  const char **item = (const char **) malloc(sizeof(const char *));
-
   fdb_listiterator_t *it;
-  fdb_new_listiterator(&it);
 
-  fdb_list(fdb, request, it);
+  bool reportDuplicatedListElements = false;
+  check_fdb_error(fdb_list(fdb, request, &it, reportDuplicatedListElements));
 
   int numItems = 0;
   while (true)
     {
-      bool exist;
-      fdb_listiterator_next(it, &exist, item);
-      if (!exist) break;
+      int err = fdb_listiterator_next(it);
+      if (err != FDB_SUCCESS) break;
 
       numItems++;
     }
   if (CDI_Debug) Message("numItems = %d", numItems);
+  // Message("numItems = %d", numItems);
 
-  if (*itemList == NULL) *itemList = (char **) malloc(numItems * sizeof(char *));
+  if (*pkeyValueList == NULL) *pkeyValueList = (fdbKeyValueEntry *) malloc(numItems * sizeof(fdbKeyValueEntry));
 
-  fdb_list(fdb, request, it);
+  check_fdb_error(fdb_list(fdb, request, &it, reportDuplicatedListElements));
 
   int itemNum = 0;
   while (true)
     {
-      bool exist;
-      fdb_listiterator_next(it, &exist, item);
-      if (!exist) break;
+      int err = fdb_listiterator_next(it);
+      if (err != FDB_SUCCESS) break;
 
-      (*itemList)[itemNum++] = strdup(*item);
-    }
+      // const char *uri;
+      // size_t off, attr_len;
+      // fdb_listiterator_attrs(it, &uri, &off, &attr_len);
+      // printf("uri=%s, off=%zu, attr_len=%zu\n", uri, off, attr_len);
 
-  fdb_delete_listiterator(it);
+      fdb_split_key_t *sk = NULL;
+      check_fdb_error(fdb_new_splitkey(&sk));
+      check_fdb_error(fdb_listiterator_splitkey(it, sk));
 
-  free(item);
+      int keyNum = 0;
+      while (true)
+        {
+          const char *k;
+          const char *v;
+          size_t l;
+          bool checkLevel = true;
+          err = fdb_splitkey_next_metadata(sk, &k, &v, checkLevel ? &l : NULL);
+          if (err != FDB_SUCCESS) break;
+          // printf("k, v, l %s %s %zu\n", k, v, l);
+          if (keyNum < 32)
+            {
+              (*pkeyValueList)[itemNum].keys[keyNum] = strdup(k);
+              (*pkeyValueList)[itemNum].values[keyNum] = strdup(v);
+              keyNum++;
+            }
+        }
+
+      (*pkeyValueList)[itemNum++].numKeys = keyNum;
+
+      check_fdb_error(fdb_delete_splitkey(sk));
+    }
+
+  check_fdb_error(fdb_delete_listiterator(it));
 
   return numItems;
 }
 
 long
-fdb_read_record(fdb_handle_t *fdb, char *item, size_t *buffersize, void **gribbuffer)
+cdi_fdb_read_record(fdb_handle_t *fdb, const fdbKeyValueEntry *keyValue, size_t *buffersize, void **gribbuffer)
 {
-  // Message("%s", item);
-
   fdb_datareader_t *dataReader = NULL;
-  fdb_new_datareader(&dataReader);
+  check_fdb_error(fdb_new_datareader(&dataReader));
   fdb_request_t *singleRequest = NULL;
-  fdb_new_request(&singleRequest);
-  fdbitem_to_request(item, singleRequest);
-  int status = fdb_retrieve(fdb, singleRequest, dataReader);
-  fdb_delete_request(singleRequest);
-  if (status != FDB_SUCCESS) Error("fdb_retrieve failed!");
+  check_fdb_error(fdb_new_request(&singleRequest));
+  fdbitem_to_request(keyValue, singleRequest);
+  check_fdb_error(fdb_retrieve(fdb, singleRequest, dataReader));
+  check_fdb_error(fdb_delete_request(singleRequest));
 
   long recordSize = 0;
-  fdb_datareader_open(dataReader, &recordSize);
+  check_fdb_error(fdb_datareader_open(dataReader, &recordSize));
   if (recordSize == 0) Error("fdb_datareader empty!");
 
   ensureBufferSize(recordSize, buffersize, gribbuffer);
 
   long readSize = 0;
-  fdb_datareader_read(dataReader, *gribbuffer, recordSize, &readSize);
+  check_fdb_error(fdb_datareader_read(dataReader, *gribbuffer, recordSize, &readSize));
   // printf("fdb_datareader_read: size=%ld/%ld\n", recordSize, readSize);
   if (readSize != recordSize) Error("fdb_datareader_read failed!");
 
-  fdb_datareader_close(dataReader);
-  fdb_delete_datareader(dataReader);
+  check_fdb_error(fdb_datareader_close(dataReader));
+  check_fdb_error(fdb_delete_datareader(dataReader));
 
   return recordSize;
 }
@@ -166,33 +207,33 @@ check_numKey(const char *key, int numKeys, int numItems)
 {
   if (numKeys == 0)
     {
-      Warning("Key %s is missing in all of the FDB records!", key);
+      Warning("Key %s is missing in all FDB records!", key);
       return -1;
     }
   else if (numKeys < numItems)
     {
-      Warning("Key %s is missing in some of the FDB records!", key);
+      Warning("Key %s is missing in some FDB records!", key);
       return -2;
     }
 
   return 0;
 }
-
+/*
 int
-check_keyvalueList(int numItems, KeyValueEntry *keyValueList)
+check_keyvalueList(int numItems, fdbKeyValueEntry *keyValueList)
 {
-  const char *searchKeys[] = { "date", "time", "param", "levtype" };
-  const int numSearchKeys = sizeof(searchKeys) / sizeof(searchKeys[0]);
+  const char *searchKeys[] = { "param", "levtype", "date", "time" };
+  int numSearchKeys = sizeof(searchKeys) / sizeof(searchKeys[0]);
   int searchKeysCount[numSearchKeys];
   for (int k = 0; k < numSearchKeys; k++) searchKeysCount[k] = 0;
 
-  for (int i = 0; i < numItems; i++)
+  // for (int i = 0; i < numItems; i++)
+  for (int i = 0; i < 1; i++)
     {
       int numKeys = keyValueList[i].numKeys;
       char **itemKeys = keyValueList[i].keys;
       for (int k = 0; k < numSearchKeys; k++)
         {
-
           for (int j = 0; j < numKeys; j++)
             {
               if (str_is_equal(itemKeys[j], searchKeys[k]))
@@ -210,76 +251,123 @@ check_keyvalueList(int numItems, KeyValueEntry *keyValueList)
 
   return status;
 }
+*/
+void
+print_keyvalueList(int numItems, fdbKeyValueEntry *keyValueList)
+{
+  for (int i = 0; i < numItems; ++i)
+    {
+      printf("item=%d ", i + 1);
+      const fdbKeyValueEntry *e = &keyValueList[i];
+      for (int k = 0; k < e->numKeys; ++k) printf("%s%s=%s", (k > 0) ? "," : "", e->keys[k], e->values[k]);
+      printf("\n");
+    }
+}
+
+void
+print_keyvalueList_sorted(int numItems, fdbKeyValueEntry *keyValueList, RecordInfoEntry *recordInfoList)
+{
+  for (int i = 0; i < numItems; ++i)
+    {
+      int fdbIndex = recordInfoList[i].fdbIndex;
+      printf("item=%d ", fdbIndex + 1);
+      const fdbKeyValueEntry *e = &keyValueList[fdbIndex];
+      for (int k = 0; k < e->numKeys; ++k) printf("%s%s=%s", (k > 0) ? "," : "", e->keys[k], e->values[k]);
+      printf("\n");
+    }
+}
+
+static int
+cmp_datetime(const void *e1, const void *e2)
+{
+  const RecordInfoEntry *x = (const RecordInfoEntry *) e1, *y = (const RecordInfoEntry *) e2;
+  int64_t datetime1 = (int64_t) x->date * 100000 + x->time;
+  int64_t datetime2 = (int64_t) y->date * 100000 + y->time;
+
+  if (datetime1 < datetime2) return -1;
+  if (datetime1 > datetime2) return 1;
+  return 0;
+}
 
-typedef struct
+static bool
+isSorted_dateTime(int numItems, RecordInfoEntry *recordInfo)
 {
-  int date, time, param, levtype;
-  int ilevel;
-} CmpKeys;
+  int64_t datetime1 = (int64_t) recordInfo[0].date * 100000 + recordInfo[0].time;
+  for (int i = 1; i < numItems; ++i)
+    {
+      int64_t datetime2 = (int64_t) recordInfo[i].date * 100000 + recordInfo[i].time;
+      if (datetime1 > datetime2) return false;
+      datetime1 = datetime2;
+    }
+
+  return true;
+}
 
 void
+cdi_fdb_sort_datetime(int numItems, RecordInfoEntry *recordInfo)
+{
+  if (!isSorted_dateTime(numItems, recordInfo)) qsort(recordInfo, numItems, sizeof(recordInfo[0]), cmp_datetime);
+}
+
+static void
 record_info_entry_init(RecordInfoEntry *recordInfo)
 {
+  recordInfo->fdbIndex = -1;
   recordInfo->date = 0;
   recordInfo->time = 0;
   recordInfo->param = 0;
   recordInfo->levtype = 0;
   recordInfo->ilevel = 0;
 }
-
-static CmpKeys
-set_cmpkeys(RecordInfoEntry *recordInfo)
-{
-  CmpKeys cmpKeys;
-  cmpKeys.date = recordInfo->date;
-  cmpKeys.time = recordInfo->time;
-  cmpKeys.param = recordInfo->param;
-  cmpKeys.levtype = recordInfo->levtype;
-  cmpKeys.ilevel = recordInfo->ilevel;
-  return cmpKeys;
-}
-
+/*
 static int
-compare_cmpkeys(const CmpKeys *cmpKeys1, const CmpKeys *cmpKeys2)
+compare_record_info_entry(const RecordInfoEntry *r1, const RecordInfoEntry *r2)
 {
   // clang-format off
-  if (cmpKeys1->date == cmpKeys2->date &&
-      cmpKeys1->time == cmpKeys2->time &&
-      cmpKeys1->param == cmpKeys2->param &&
-      cmpKeys1->levtype == cmpKeys2->levtype &&
-      cmpKeys1->ilevel == cmpKeys2->ilevel)
+  if (r1->date    == r2->date &&
+      r1->time    == r2->time &&
+      r1->param   == r2->param &&
+      r1->levtype == r2->levtype &&
+      r1->ilevel  == r2->ilevel)
     return 0;
   // clang-format on
 
   return -1;
 }
-
+*/
 int
-get_num_records(int numItems, RecordInfoEntry *recordInfoList)
+get_num_records(int numItems, RecordInfoEntry *recordInfo)
 {
-  const int date = recordInfoList[0].date;
-  const int time = recordInfoList[0].time;
-
   int numRecords = 0;
   for (int i = 0; i < numItems; i++)
     {
-      if (date == recordInfoList[i].date && time == recordInfoList[i].time)
-        numRecords++;
-      else
-        break;
+      if (recordInfo[0].date != recordInfo[i].date || recordInfo[0].time != recordInfo[i].time) break;
+      numRecords++;
     }
 
-  CmpKeys cmpKeys0 = set_cmpkeys(&recordInfoList[0]);
+  int numTimesteps = numItems / numRecords;
+  if (numTimesteps * numRecords != numItems) return 0;
+
+  for (int k = 1; k < numTimesteps; ++k)
+    {
+      int date = recordInfo[k * numRecords].date;
+      int time = recordInfo[k * numRecords].time;
+      for (int i = 1; i < numRecords; i++)
+        {
+          int index = k * numRecords + i;
+          if (date != recordInfo[index].date || time != recordInfo[index].time) return 0;
+        }
+    }
+  /*
   for (int i = 1; i < numRecords; i++)
     {
-      CmpKeys cmpKeys = set_cmpkeys(&recordInfoList[i]);
-      if (compare_cmpkeys(&cmpKeys0, &cmpKeys) == 0)
+      if (compare_record_info_entry(&recordInfo[0], &recordInfo[i]) == 0)
         {
           numRecords = i;
           break;
         }
     }
-
+  */
   return numRecords;
 }
 
@@ -299,29 +387,53 @@ get_ilevtype(const char *levtype)
   // clang-format off
   if      (str_is_equal(levtype, "sfc")) ilevtype = levTypeSFC;
   else if (str_is_equal(levtype, "ml"))  ilevtype = levTypeML;
-  else if (str_is_equal(levtype, "pl"))  ilevtype = levTypeML;
+  else if (str_is_equal(levtype, "pl"))  ilevtype = levTypePL;
   // clang-format on
 
   return ilevtype;
 }
 
-void
-decode_keyvalue(KeyValueEntry *keyValue, RecordInfoEntry *recordInfo)
+int
+decode_keyvalue(int numItems, fdbKeyValueEntry *keyValueList, RecordInfoEntry *recordInfoList)
 {
-  char **itemKeys = keyValue->keys;
-  char **itemValues = keyValue->values;
-  int numKeys = keyValue->numKeys;
-  for (int i = 0; i < numKeys; i++)
+  int numKeyDate = 0;
+  int numKeyTime = 0;
+  int numKeyParam = 0;
+  int numKeyLtype = 0;
+  for (int i = 0; i < numItems; ++i)
     {
-      // printf("key <%s> value <%s>\n", itemKeys[i], itemValues[i]);
-      // clang-format off
-      if      (str_is_equal(itemKeys[i], "date"))     recordInfo->date = atoi(itemValues[i]);
-      else if (str_is_equal(itemKeys[i], "time"))     recordInfo->time = atoi(itemValues[i]);
-      else if (str_is_equal(itemKeys[i], "param"))    recordInfo->param = atoi(itemValues[i]);
-      else if (str_is_equal(itemKeys[i], "levtype"))  recordInfo->levtype = get_ilevtype(itemValues[i]);
-      else if (str_is_equal(itemKeys[i], "levelist")) recordInfo->ilevel = atoi(itemValues[i]);
-      // clang-format on
+      fdbKeyValueEntry *keyValue = &keyValueList[i];
+      RecordInfoEntry *rentry = &recordInfoList[i];
+      record_info_entry_init(rentry);
+      rentry->fdbIndex = i;
+      char **keys = keyValue->keys;
+      char **values = keyValue->values;
+      bool foundDate = false;
+      bool foundTime = false;
+      bool foundParam = false;
+      bool foundLtype = false;
+      bool foundLlist = false;
+      for (int i = 0; i < keyValue->numKeys; i++)
+        {
+          // printf("key <%s> value <%s>\n", itemKeys[i], itemValues[i]);
+          // clang-format off
+          if      (!foundDate  && str_is_equal(keys[i], "date"))     { foundDate = true;  numKeyDate++;  rentry->date = atoi(values[i]); }
+          else if (!foundTime  && str_is_equal(keys[i], "time"))     { foundTime = true;  numKeyTime++;  rentry->time = atoi(values[i]); }
+          else if (!foundParam && str_is_equal(keys[i], "param"))    { foundParam = true; numKeyParam++; rentry->param = atoi(values[i]); }
+          else if (!foundLtype && str_is_equal(keys[i], "levtype"))  { foundLtype = true; numKeyLtype++; rentry->levtype = get_ilevtype(values[i]); }
+          else if (!foundLlist && str_is_equal(keys[i], "levelist")) { foundLlist = true; rentry->ilevel = atoi(values[i]); }
+          // clang-format on
+          if (foundDate && foundTime && foundParam && foundLtype && foundLlist) break;
+        }
     }
+
+  int status = 0;
+  if (check_numKey("date", numKeyDate, numItems) != 0) status = -1;
+  if (check_numKey("time", numKeyTime, numItems) != 0) status = -1;
+  if (check_numKey("param", numKeyParam, numItems) != 0) status = -1;
+  if (check_numKey("levtype", numKeyLtype, numItems) != 0) status = -1;
+
+  return status;
 }
 
 int
@@ -337,7 +449,7 @@ remove_duplicate_timesteps(RecordInfoEntry *recordInfoList, int numRecords, int
       int k = 0;
       for (k = 0; k < numTimesteps; k++)
         {
-          const int index = (i + k) * numRecords;
+          int index = (i + k) * numRecords;
           if (date != recordInfoList[index].date || time != recordInfoList[index].time) break;
         }
 
@@ -364,51 +476,35 @@ remove_duplicate_timesteps(RecordInfoEntry *recordInfoList, int numRecords, int
 }
 
 fdb_request_t *
-create_fdb_request(const char *filename)
+cdi_create_fdb_request(const char *filename)
 {
   size_t len = strlen(filename);
-  if (len == 4) Error("Empty FDB request!");
+  if (len == 6) Error("Empty FDB request!");
 
-  KeyValueEntry keyValue;
+  KeyValueItem keyValue;
   keyValue.item = NULL;
-  decode_fdbitem(filename + 4, &keyValue);
+  decode_fdbitem(filename + 6, &keyValue);
 
   if (keyValue.numKeys == 0) Error("Empty FDB request!");
 
   fdb_request_t *request = NULL;
   fdb_new_request(&request);
 
-  bool classDefined = false;
-  bool streamDefined = false;
   bool expverDefined = false;
-  for (int i = 0; i < keyValue.numKeys; i++)
+  bool classDefined = false;
+  for (int k = 0; k < keyValue.numKeys; k++)
     {
       // clang-format off
-      if      (str_is_equal(keyValue.keys[i], "class"))  classDefined = true;
-      else if (str_is_equal(keyValue.keys[i], "stream")) streamDefined = true;
-      else if (str_is_equal(keyValue.keys[i], "expver")) expverDefined = true;
+      if      (!expverDefined && str_is_equal(keyValue.keys[k], "expver")) expverDefined = true;
+      else if (!classDefined  && str_is_equal(keyValue.keys[k], "class")) classDefined = true;
       // clang-format on
 
-      fdb_request_add1(request, keyValue.keys[i], keyValue.values[i]);
+      check_fdb_error(fdb_request_add1(request, keyValue.keys[k], keyValue.values[k]));
     }
 
-  if (!classDefined) Error("FDB parameter <class> undefined!");
-  if (!streamDefined) Error("FDB parameter <stream> undefined!");
   if (!expverDefined) Error("FDB parameter <expver> undefined!");
+  if (!classDefined) Error("FDB parameter <class> undefined!");
 
-  /*
-  fdb_request_add1(request, "class", "ea");
-  fdb_request_add1(request, "expver", "0001");
-  fdb_request_add1(request, "stream", "oper");
-  fdb_request_add1(request, "domain", "g");
-  fdb_request_add1(request, "date", "20180601");
-  // fdb_request_add1(request, "time", "1800");
-  fdb_request_add1(request, "type", "an");
-  fdb_request_add1(request, "levtype", "sfc");
-  fdb_request_add1(request, "step", "0");
-  fdb_request_add1(request, "param", "139");
-  // fdb_request_add1(request, "levelist", "300");
-  */
   if (keyValue.item) free(keyValue.item);
 
   return request;
diff --git a/src/cdi_fdb.h b/src/cdi_fdb.h
index a1a2d221032ea69b9ca510172cda870b126fd697..9baf530494deeef70c26ad2f78643cc96922f856 100644
--- a/src/cdi_fdb.h
+++ b/src/cdi_fdb.h
@@ -17,10 +17,18 @@ typedef struct
   char *keys[32];
   char *values[32];
   int numKeys;
-} KeyValueEntry;
+} KeyValueItem;
 
 typedef struct
 {
+  char *keys[32];
+  char *values[32];
+  int numKeys;
+} fdbKeyValueEntry;
+
+typedef struct
+{
+  int fdbIndex;
   int date;
   int time;
   int param;
@@ -28,15 +36,19 @@ typedef struct
   int ilevel;
 } RecordInfoEntry;
 
-void decode_fdbitem(const char *fdbItem, KeyValueEntry *keyValue);
-int fdb_fill_itemlist(fdb_handle_t *fdb, fdb_request_t *request, char ***itemList);
-long fdb_read_record(fdb_handle_t *fdb, char *item, size_t *buffersize, void **gribbuffer);
-int check_keyvalueList(int numItems, KeyValueEntry *keyValueList);
-void record_info_entry_init(RecordInfoEntry *recordInfo);
+void check_fdb_error(int errorNum);
+void cdi_fdb_delete_kvlist(int numItems, fdbKeyValueEntry *keyValueList);
+void decode_fdbitem(const char *fdbItem, KeyValueItem *keyValue);
+int cdi_fdb_fill_kvlist(fdb_handle_t *fdb, fdb_request_t *request, fdbKeyValueEntry **keyValueList);
+long cdi_fdb_read_record(fdb_handle_t *fdb, const fdbKeyValueEntry *keyValue, size_t *buffersize, void **gribbuffer);
+// int check_keyvalueList(int numItems, fdbKeyValueEntry *keyValueList);
+void print_keyvalueList(int numItems, fdbKeyValueEntry *keyValueList);
+void print_keyvalueList_sorted(int numItems, fdbKeyValueEntry *keyValueList, RecordInfoEntry *recordInfoList);
+void cdi_fdb_sort_datetime(int numItems, RecordInfoEntry *recordInfo);
 int get_num_records(int numItems, RecordInfoEntry *recordInfoList);
-void decode_keyvalue(KeyValueEntry *keyValue, RecordInfoEntry *recordInfo);
+int decode_keyvalue(int numItems, fdbKeyValueEntry *keyValueList, RecordInfoEntry *recordInfoList);
 int remove_duplicate_timesteps(RecordInfoEntry *recordInfoList, int numRecords, int numTimesteps, int *timestepRecordOffset);
-fdb_request_t *create_fdb_request(const char *filename);
+fdb_request_t *cdi_create_fdb_request(const char *filename);
 
 #endif
 
diff --git a/src/cdi_int.c b/src/cdi_int.c
index befd869165e4a4a5082947e8229dfb508ad38c6d..0f1bca01494d0eb0f1eadf3bceea3c8dcd7c2ac5 100644
--- a/src/cdi_int.c
+++ b/src/cdi_int.c
@@ -16,6 +16,11 @@
 #include "cgribex.h"
 #endif
 
+#ifdef HAVE_LIBPTHREAD
+#include <pthread.h>
+pthread_mutex_t CDI_IO_Mutex = PTHREAD_MUTEX_INITIALIZER;
+#endif
+
 int CDI_Default_Calendar = CALENDAR_PROLEPTIC;
 
 int CDI_Default_InstID = CDI_UNDEFID;
@@ -75,10 +80,11 @@ int CDI_Recopt = 0;
 
 bool CDI_gribapi_debug = false;
 bool CDI_gribapi_grib1 = false;
+bool CDI_Lock_IO = false;
+bool CDI_Threadsafe = false;
 int cdiDefaultLeveltype = -1;
 int cdiDataUnreduced = 0;
 int cdiSortName = 0;
-int cdiSortParam = 0;
 int cdiHaveMissval = 0;
 
 static long
@@ -337,6 +343,9 @@ cdiInitialize(void)
       value = cdi_getenv_int("CDI_ECCODES_GRIB1");
       if (value >= 0) cdiSetEccodesGrib1((bool) value);
 
+      value = cdi_getenv_int("CDI_LOCK_IO");
+      if (value >= 0) CDI_Lock_IO = (bool) value;
+
       value = cdi_getenv_int("CDI_READ_CELL_CORNERS");
       if (value >= 0) CDI_Read_Cell_Corners = (int) value;
 
@@ -349,9 +358,6 @@ cdiInitialize(void)
       value = cdi_getenv_int("CDI_SORTNAME");
       if (value >= 0) cdiSortName = (int) value;
 
-      value = cdi_getenv_int("CDI_SORTPARAM");
-      if (value >= 0) cdiSortParam = (int) value;
-
       value = cdi_getenv_int("CDI_HAVE_MISSVAL");
       if (value >= 0) cdiHaveMissval = (int) value;
 
@@ -430,7 +436,7 @@ cdiInitialize(void)
       envstr = getenv("CDI_QUERY_ABORT");
       if (envstr)
         {
-          const int ival = atoi(envstr);
+          int ival = atoi(envstr);
           if (ival == 0 || ival == 1)
             {
               CDI_Query_Abort = ival;
@@ -441,7 +447,7 @@ cdiInitialize(void)
       envstr = getenv("CDI_VERSION_INFO");
       if (envstr)
         {
-          const int ival = atoi(envstr);
+          int ival = atoi(envstr);
           if (ival == 0 || ival == 1)
             {
               CDI_Version_Info = ival;
@@ -452,7 +458,7 @@ cdiInitialize(void)
       envstr = getenv("CDI_CONVERT_CUBESPHERE");
       if (envstr)
         {
-          const int ival = atoi(envstr);
+          int ival = atoi(envstr);
           if (ival == 0 || ival == 1)
             {
               CDI_Convert_Cubesphere = ival;
@@ -489,7 +495,7 @@ cdiInitialize(void)
 const char *
 strfiletype(int filetype)
 {
-  const int size = (int) (sizeof(Filetypes) / sizeof(char *));
+  int size = (int) (sizeof(Filetypes) / sizeof(char *));
   return (filetype > 0 && filetype < size) ? Filetypes[filetype] : Filetypes[0];
 }
 
@@ -497,17 +503,18 @@ void
 cdiDefGlobal(const char *string, int value)
 {
   // clang-format off
-  if      (str_is_equal(string, "REGULARGRID")          ) cdiDataUnreduced = value;
-  else if (str_is_equal(string, "ECCODES_DEBUG")        ) CDI_gribapi_debug = (bool) value;
-  else if (str_is_equal(string, "ECCODES_GRIB1")        ) cdiSetEccodesGrib1((bool) value);
-  else if (str_is_equal(string, "SORTNAME")             ) cdiSortName = value;
-  else if (str_is_equal(string, "SORTPARAM")            ) cdiSortParam = value;
-  else if (str_is_equal(string, "HAVE_MISSVAL")         ) cdiHaveMissval = value;
-  else if (str_is_equal(string, "NC_CHUNKSIZEHINT")     ) CDI_Netcdf_Chunksizehint = value;
-  else if (str_is_equal(string, "READ_CELL_CORNERS")    ) CDI_Read_Cell_Corners = value;
-  else if (str_is_equal(string, "CMOR_MODE")            ) CDI_CMOR_Mode = value;
-  else if (str_is_equal(string, "REDUCE_DIM")           ) CDI_Reduce_Dim = value;
-  else if (str_is_equal(string, "NETCDF_HDR_PAD")       ) CDI_Netcdf_Hdr_Pad = (size_t) value;
+  if      (str_is_equal(string, "REGULARGRID"))           cdiDataUnreduced = value;
+  else if (str_is_equal(string, "LOCKIO"))                CDI_Lock_IO = (bool) value;
+  else if (str_is_equal(string, "THREADSAFE"))            CDI_Threadsafe = (bool) value;
+  else if (str_is_equal(string, "ECCODES_DEBUG"))         CDI_gribapi_debug = (bool) value;
+  else if (str_is_equal(string, "ECCODES_GRIB1"))         cdiSetEccodesGrib1((bool) value);
+  else if (str_is_equal(string, "SORTNAME"))              cdiSortName = value;
+  else if (str_is_equal(string, "HAVE_MISSVAL"))          cdiHaveMissval = value;
+  else if (str_is_equal(string, "NC_CHUNKSIZEHINT"))      CDI_Netcdf_Chunksizehint = value;
+  else if (str_is_equal(string, "READ_CELL_CORNERS"))     CDI_Read_Cell_Corners = value;
+  else if (str_is_equal(string, "CMOR_MODE"))             CDI_CMOR_Mode = value;
+  else if (str_is_equal(string, "REDUCE_DIM"))            CDI_Reduce_Dim = value;
+  else if (str_is_equal(string, "NETCDF_HDR_PAD"))        CDI_Netcdf_Hdr_Pad = (size_t) value;
   else if (str_is_equal(string, "NETCDF_LAZY_GRID_LOAD")) CDI_Netcdf_Lazy_Grid_Load = (bool) value;
   else Warning("Unsupported global key: %s", string);
   // clang-format on
diff --git a/src/cdi_int.h b/src/cdi_int.h
index c9ba92cc406cd2b29f37cbc3be8ea20519b87e21..c44667b7957901f48645fc4b96909659a5c493ca 100644
--- a/src/cdi_int.h
+++ b/src/cdi_int.h
@@ -1,10 +1,22 @@
 #ifndef CDI_INT_H
 #define CDI_INT_H
 
+// strdup() from string.h
+#ifdef __STDC_ALLOC_LIB__
+#define __STDC_WANT_LIB_EXT2__ 1
+#else
+#undef _POSIX_C_SOURCE
+#define _POSIX_C_SOURCE 200809L
+#endif
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
+#ifdef HAVE_LIBFDB5
+#include "cdi_fdb.h"
+#endif
+
 #include <assert.h>
 #include <stdio.h>
 #include <stdbool.h>
@@ -17,6 +29,16 @@
 #include "cdi.h"
 #include "cdf_config.h"
 
+#ifdef HAVE_LIBPTHREAD
+#include <pthread.h>
+extern pthread_mutex_t CDI_IO_Mutex;
+#define CDI_IO_LOCK() pthread_mutex_lock(&CDI_IO_Mutex)
+#define CDI_IO_UNLOCK() pthread_mutex_unlock(&CDI_IO_Mutex)
+#else
+#define CDI_IO_LOCK()
+#define CDI_IO_UNLOCK()
+#endif
+
 // Base file types
 
 #define CDI_FILETYPE_GRIB 100    // File type GRIB
@@ -27,22 +49,6 @@
 #define UNUSED(x) (void) x
 #endif
 
-#ifndef strdupx
-#ifndef strdup
-char *strdup(const char *s);
-#endif
-#define strdupx strdup
-/*
-#define strdupx(s)			          \
-({					      	  \
-   const char *__old = (s);			  \
-   size_t __len = strlen(__old) + 1;		  \
-   char *__new = (char *) Malloc(__len);	  \
-   (char *) memcpy(__new, __old, __len);	  \
-})
-*/
-#endif
-
 char *str_to_lower(char *str);
 bool strStartsWith(const char *vstr, const char *cstr);
 
@@ -183,7 +189,7 @@ typedef struct
   VarScanKeys scanKeys;
   var_tile_t tiles;  // tile-related meta-data, currently for GRIB-API only.
 #ifdef HAVE_LIBFDB5
-  void *fdbItem;
+  int fdbItemIndex;
 #endif
 } record_t;
 
@@ -311,7 +317,8 @@ typedef struct
   int comptype;   // compression type
   int complevel;  // compression level
   bool sortname;
-  bool sortparam;
+  bool lockIO;
+
   void *gribContainers;
 
   int numWorker;
@@ -322,6 +329,11 @@ typedef struct
 
   int protocol;
   void *protocolData;
+
+#ifdef HAVE_LIBFDB5
+  int fdbNumItems;
+  fdbKeyValueEntry *fdbKeyValueList;
+#endif
 } stream_t;
 
 // Length of optional keyword/value pair list
@@ -378,9 +390,10 @@ extern int CDI_Netcdf_Chunksizehint;
 extern int CDI_ChunkType;
 extern int CDI_Test;
 extern int CDI_Split_Ltype105;
+extern bool CDI_Lock_IO;
+extern bool CDI_Threadsafe;
 extern int cdiDataUnreduced;
 extern int cdiSortName;
-extern int cdiSortParam;
 extern int cdiHaveMissval;
 extern bool CDI_Ignore_Att_Coordinates;
 extern bool CDI_Coordinates_Lon_Lat;
@@ -492,9 +505,9 @@ int streamOpenID(const char *filename, char filemode, int filetype, int resH);
 
 void cdiStreamDefVlist_(int streamID, int vlistID);
 
-int cdiStreamWriteVar_(int streamID, int varID, int memtype, const void *data, SizeType nmiss);
+int cdiStreamWriteVar_(int streamID, int varID, int memtype, const void *data, SizeType numMissVals);
 
-void cdiStreamWriteVarChunk_(int streamID, int varID, int memtype, const int rect[][2], const void *data, SizeType nmiss);
+void cdiStreamWriteVarChunk_(int streamID, int varID, int memtype, const int rect[][2], const void *data, SizeType numMissVals);
 void cdiStreamCloseDefaultDelegate(stream_t *streamptr, int recordBufIsToBeDeleted);
 
 int cdiStreamDefTimestep_(stream_t *streamptr, int tsID);
diff --git a/src/cdi_util.c b/src/cdi_util.c
index b827b9cee4bdaa8d04a8b61e0884cc1256c5613f..187cf2c530db3c7c1d9229384cfcf5c050d74ca1 100644
--- a/src/cdi_util.c
+++ b/src/cdi_util.c
@@ -2,9 +2,7 @@
 #include "config.h"
 #endif
 
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif
 
 #include <limits.h>
 #include <stdio.h>
diff --git a/src/cdipio.h b/src/cdipio.h
index 72a5f9ac9a77531a532eac94e920239e74340f69..c1401954b50d34532c64686933ef115b9aceb2d6 100644
--- a/src/cdipio.h
+++ b/src/cdipio.h
@@ -80,14 +80,14 @@ void cdiPioStreamDefDecomposedVlist(int streamID, int vlistID, const Xt_idxlist
  * the corresponding array passed to streamWriteVar in the serial version.
  * The group of processes collectively calling streamWriteVarPart
  * must provide data for all indices or the behaviour is undefined. */
-void streamWriteVarPart(int streamID, int varID, const double *data, int nmiss, Xt_idxlist partDesc);
+void streamWriteVarPart(int streamID, int varID, const double *data, int numMissVals, Xt_idxlist partDesc);
 
 /* streamWriteVarPartF: Write part of the data making up variable
  * varID of stream streamID.
  *
  * Single-precision version of streamWriteVarPart.
  */
-void streamWriteVarPartF(int streamID, int varID, const float *data, int nmiss, Xt_idxlist partDesc);
+void streamWriteVarPartF(int streamID, int varID, const float *data, int numMissVals, Xt_idxlist partDesc);
 
 /* streamWriteScatteredVarPart: Write part of the data making up
  * variable varID of stream streamID.
@@ -97,7 +97,7 @@ void streamWriteVarPartF(int streamID, int varID, const float *data, int nmiss,
  * blocklengths[i] and starting displacements[i] each for i in [0,numBlocks)
  */
 void streamWriteScatteredVarPart(int streamID, int varID, const double *data, int numBlocks, const int blocklengths[],
-                                 const int displacements[], int nmiss, Xt_idxlist partDesc);
+                                 const int displacements[], int numMissVals, Xt_idxlist partDesc);
 
 /* streamWriteScatteredVarPartF: Write part of the data making up
  * variable varID of stream streamID.
@@ -105,7 +105,7 @@ void streamWriteScatteredVarPart(int streamID, int varID, const double *data, in
  * Single-precision version of streamWriteScatteredVarPart.
  */
 void streamWriteScatteredVarPartF(int streamID, int varID, const float *data, int numBlocks, const int blocklengths[],
-                                  const int displacements[], int nmiss, Xt_idxlist partDesc);
+                                  const int displacements[], int numMissVals, Xt_idxlist partDesc);
 /* cdiPioCSRLastN: return role codes appropriate to use the last
    \textit{nProcsIO} tasks as I/O servers */
 int cdiPioCSRLastN(MPI_Comm commSuper, int IOMode, int nProcsIO);
diff --git a/src/cgribexlib.c b/src/cgribexlib.c
index 47f8fe780c5071c30bdc1fe958f61d8b587d31d1..7ef1a4aec27e16af4a9fdc8991869f479741e655 100644
--- a/src/cgribexlib.c
+++ b/src/cgribexlib.c
@@ -1,11 +1,9 @@
 
-// Automatically generated by m214003 at 2023-06-12, do not edit
+// Automatically generated by m214003 at 2024-01-13, do not edit
 
-// CGRIBEXLIB_VERSION="2.1.1"
+// CGRIBEXLIB_VERSION="2.2.0"
 
-// clang-format off
-
-#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5) || defined (__clang__)
+#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5) || defined(__clang__)
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wconversion"
 #pragma GCC diagnostic ignored "-Wsign-conversion"
@@ -17,7 +15,7 @@
 #include <ppu_intrinsics.h>
 #endif
 
-#ifdef  HAVE_CONFIG_H
+#ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
@@ -28,7 +26,6 @@
 #include <sys/types.h>
 #include <inttypes.h>
 
-
 #include "file.h"
 #include "dmemory.h"
 #include "dtypes.h"
@@ -37,10 +34,12 @@
 #ifndef CGRIBEX_TEMPLATES_H
 #define CGRIBEX_TEMPLATES_H
 
+// clang-format off
 #define CAT(X,Y)      X##_##Y
 #define TEMPLATE(X,Y) CAT(X,Y)
+// clang-format on
 
-#endif 
+#endif
 #ifndef GRIB_INT_H
 #define GRIB_INT_H
 
@@ -55,6 +54,7 @@
 #include <math.h>
 #include <float.h>
 
+// clang-format off
 
 #ifndef  CGRIBEX_H
 #include "cgribex.h"
@@ -248,10 +248,14 @@ int grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **i
 }
 #endif
 
-#endif  /* GRIB_INT_H */
+// clang-format on
+
+#endif /* GRIB_INT_H */
 #ifndef GRIBDECODE_H
 #define GRIBDECODE_H
 
+// clang-format off
+
 #define  UNDEFINED          9.999e20
 
 
@@ -374,9 +378,9 @@ int grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **i
 #define  BDS_Len	    GET_UINT3(bds[0], bds[1], bds[2])
 #define  BDS_Flag	    (bds[3])
 #define  BDS_BinScale       GET_INT2(bds[ 4], bds[ 5])
-#define  BDS_RefValue       (decfp2((int)bds[ 6], GET_UINT3(bds[7], bds[8], bds[9])))
+#define  BDS_RefValue       (decfp2((int)bds[ 6], (int)(GET_UINT3(bds[7], bds[8], bds[9]))))
 #define  BDS_NumBits        ((int) bds[10])
-#define  BDS_RealCoef       (decfp2((int)bds[zoff+11], GET_UINT3(bds[zoff+12], bds[zoff+13], bds[zoff+14])))
+#define  BDS_RealCoef       (decfp2((int)bds[zoff+11], (int)(GET_UINT3(bds[zoff+12], bds[zoff+13], bds[zoff+14]))))
 #define  BDS_PackData       ((int) ((bds[zoff+11]<<8) + bds[zoff+12]))
 #define  BDS_Power          GET_INT2(bds[zoff+13], bds[zoff+14])
 #define  BDS_Z              (bds[13])
@@ -388,12 +392,16 @@ int grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **i
 #define  GRIB2_SECLEN(section)   (GET_UINT4(section[0], section[1], section[2], section[3]))
 #define  GRIB2_SECNUM(section)   (GET_UINT1(section[4]))
 
-#endif  /* GRIBDECODE_H */
+// clang-format on
+
+#endif /* GRIBDECODE_H */
 #ifndef CGRIBEX_GRIB_ENCODE_H
 #define CGRIBEX_GRIB_ENCODE_H
 
 #include <limits.h>
 
+// clang-format off
+
 #define PutnZero(n) \
 { \
   for ( size_t i___ = z >= 0 ? (size_t)z : 0; i___ < (size_t)(z+n); i___++ ) lGrib[i___] = 0; \
@@ -427,12 +435,14 @@ enum {
   Put3Byte(mantissa);            \
 }
 
-#endif  /* CGRIBEX_GRIB_ENCODE_H */
+// clang-format on
+
+#endif /* CGRIBEX_GRIB_ENCODE_H */
 #ifndef CODEC_COMMON_H
 #define CODEC_COMMON_H
-#define gribSwapByteOrder_uint16(ui16)  ((uint16_t)((ui16<<8) | (ui16>>8)))
-#endif  /* CODEC_COMMON_H */
-/* 
+#define gribSwapByteOrder_uint16(ui16) ((uint16_t) ((ui16 << 8) | (ui16 >> 8)))
+#endif /* CODEC_COMMON_H */
+/*
 icc -g -Wall -O3 -march=native -std=c99 -qopt-report=5 -DTEST_MINMAXVAL -qopenmp -DOMP_SIMD minmax_val.c
  result on hama2 (icc 16.0.0):
      float:
@@ -487,10 +497,10 @@ xlc_r -g -O3 -qhot -q64 -qarch=auto -qtune=auto -qreport -DTEST_MINMAXVAL minmax
 
 #include <stdlib.h>
 
-//#undef _GET_X86_COUNTER
-//#undef _GET_IBM_COUNTER
-//#undef _GET_MACH_COUNTER
-//#undef _ARCH_PWR6
+// #undef _GET_X86_COUNTER
+// #undef _GET_IBM_COUNTER
+// #undef _GET_MACH_COUNTER
+// #undef _ARCH_PWR6
 
 #if defined(_GET_IBM_COUNTER)
 #include <libhpc.h>
@@ -500,15 +510,15 @@ xlc_r -g -O3 -qhot -q64 -qarch=auto -qtune=auto -qreport -DTEST_MINMAXVAL minmax
 #include <mach/mach_time.h>
 #endif
 
-#if   defined(__GNUC__) && !defined(__ICC) && !defined(__clang__)
+#if defined(__GNUC__) && !defined(__ICC) && !defined(__clang__)
 #if (__GNUC__ >= 4) && (__GNUC_MINOR__ >= 4)
 #define GNUC_PUSH_POP
 #endif
 #endif
 
 #ifndef DISABLE_SIMD
-#if   defined(__GNUC__) && (__GNUC__ >= 4)
-#elif defined(__ICC)    && (__ICC >= 1100)
+#if defined(__GNUC__) && (__GNUC__ >= 4)
+#elif defined(__ICC) && (__ICC >= 1100)
 #elif defined(__clang__)
 #else
 #define DISABLE_SIMD
@@ -551,11 +561,10 @@ xlc_r -g -O3 -qhot -q64 -qarch=auto -qtune=auto -qreport -DTEST_MINMAXVAL minmax
 #include <emmintrin.h>
 #endif
 
-
 #if defined(_ENABLE_AVX)
 
-static
-void avx_minmax_val_double(const double *restrict buf, size_t nframes, double *min, double *max)
+static void
+avx_minmax_val_double(const double *restrict buf, size_t nframes, double *min, double *max)
 {
   double fmin[4], fmax[4];
   __m256d current_max, current_min, work;
@@ -565,79 +574,83 @@ void avx_minmax_val_double(const double *restrict buf, size_t nframes, double *m
   current_max = _mm256_set1_pd(*max);
 
   // Work input until "buf" reaches 32 byte alignment
-  while ( ((unsigned long)buf) % 32 != 0 && nframes > 0) {
-
-    // Load the next double into the work buffer
-    work = _mm256_set1_pd(*buf);
-    current_min = _mm256_min_pd(current_min, work);
-    current_max = _mm256_max_pd(current_max, work);
-    buf++;
-    nframes--;
-  }
+  while (((unsigned long) buf) % 32 != 0 && nframes > 0)
+    {
 
-  while (nframes >= 16) {
+      // Load the next double into the work buffer
+      work = _mm256_set1_pd(*buf);
+      current_min = _mm256_min_pd(current_min, work);
+      current_max = _mm256_max_pd(current_max, work);
+      buf++;
+      nframes--;
+    }
+
+  while (nframes >= 16)
+    {
 
-    (void) _mm_prefetch((const char *)(buf+8), _MM_HINT_NTA);
+      (void) _mm_prefetch((const char *) (buf + 8), _MM_HINT_NTA);
 
-    work = _mm256_load_pd(buf);
-    current_min = _mm256_min_pd(current_min, work);
-    current_max = _mm256_max_pd(current_max, work);
-    buf += 4;
+      work = _mm256_load_pd(buf);
+      current_min = _mm256_min_pd(current_min, work);
+      current_max = _mm256_max_pd(current_max, work);
+      buf += 4;
 
-    work = _mm256_load_pd(buf);
-    current_min = _mm256_min_pd(current_min, work);
-    current_max = _mm256_max_pd(current_max, work);
-    buf += 4;
+      work = _mm256_load_pd(buf);
+      current_min = _mm256_min_pd(current_min, work);
+      current_max = _mm256_max_pd(current_max, work);
+      buf += 4;
 
-    (void) _mm_prefetch((const char *)(buf+8), _MM_HINT_NTA);
+      (void) _mm_prefetch((const char *) (buf + 8), _MM_HINT_NTA);
 
-    work = _mm256_load_pd(buf);
-    current_min = _mm256_min_pd(current_min, work);
-    current_max = _mm256_max_pd(current_max, work);
-    buf += 4;
+      work = _mm256_load_pd(buf);
+      current_min = _mm256_min_pd(current_min, work);
+      current_max = _mm256_max_pd(current_max, work);
+      buf += 4;
 
-    work = _mm256_load_pd(buf);
-    current_min = _mm256_min_pd(current_min, work);
-    current_max = _mm256_max_pd(current_max, work);
-    buf += 4;
-    nframes -= 16;
-  }
+      work = _mm256_load_pd(buf);
+      current_min = _mm256_min_pd(current_min, work);
+      current_max = _mm256_max_pd(current_max, work);
+      buf += 4;
+      nframes -= 16;
+    }
 
   // work through aligned buffers
-  while (nframes >= 4) {
-    work = _mm256_load_pd(buf);
-    current_min = _mm256_min_pd(current_min, work);
-    current_max = _mm256_max_pd(current_max, work);
-    buf += 4;
-    nframes -= 4;
-  }
+  while (nframes >= 4)
+    {
+      work = _mm256_load_pd(buf);
+      current_min = _mm256_min_pd(current_min, work);
+      current_max = _mm256_max_pd(current_max, work);
+      buf += 4;
+      nframes -= 4;
+    }
 
   // work through the remainung values
-  while ( nframes > 0) {
-    work = _mm256_set1_pd(*buf);
-    current_min = _mm256_min_pd(current_min, work);
-    current_max = _mm256_max_pd(current_max, work);
-    buf++;
-    nframes--;
-  }
+  while (nframes > 0)
+    {
+      work = _mm256_set1_pd(*buf);
+      current_min = _mm256_min_pd(current_min, work);
+      current_max = _mm256_max_pd(current_max, work);
+      buf++;
+      nframes--;
+    }
 
   // find min & max value through shuffle tricks
 
   work = current_min;
   work = _mm256_shuffle_pd(work, work, 5);
-  work = _mm256_min_pd (work, current_min);
+  work = _mm256_min_pd(work, current_min);
   current_min = work;
   work = _mm256_permute2f128_pd(work, work, 1);
-  work = _mm256_min_pd (work, current_min);
+  work = _mm256_min_pd(work, current_min);
   _mm256_storeu_pd(fmin, work);
 
   work = current_max;
   work = current_max;
   work = _mm256_shuffle_pd(work, work, 5);
-  work = _mm256_max_pd (work, current_max);
+  work = _mm256_max_pd(work, current_max);
   current_max = work;
   work = _mm256_permute2f128_pd(work, work, 1);
-  work = _mm256_max_pd (work, current_max);
+  work = _mm256_max_pd(work, current_max);
   _mm256_storeu_pd(fmax, work);
 
   *min = fmin[0];
@@ -648,157 +661,158 @@ void avx_minmax_val_double(const double *restrict buf, size_t nframes, double *m
 
 #elif defined(_ENABLE_SSE2)
 
-static
-void sse2_minmax_val_double(const double *restrict buf, size_t nframes, double *min, double *max)
+static void
+sse2_minmax_val_double(const double *restrict buf, size_t nframes, double *min, double *max)
 {
   __m128d current_max, current_min, work;
-  
+
   // load starting max and min values into all slots of the XMM registers
   current_min = _mm_set1_pd(*min);
   current_max = _mm_set1_pd(*max);
-  
+
   // work on input until buf reaches 16 byte alignment
-  while ( ((unsigned long)buf) % 16 != 0 && nframes > 0) {
-    
-    // load one double and replicate
-    work = _mm_set1_pd(*buf);    
-    current_min = _mm_min_pd(current_min, work);
-    current_max = _mm_max_pd(current_max, work);    
-    buf++;
-    nframes--;
-  }
-  
-  while (nframes >= 8) {
-    // use 64 byte prefetch for double octetts
-    // __builtin_prefetch(buf+64,0,0); // for GCC 4.3.2 +
-
-    work = _mm_load_pd(buf);
-    current_min = _mm_min_pd(current_min, work);
-    current_max = _mm_max_pd(current_max, work);
-    buf += 2;
-    work = _mm_load_pd(buf);
-    current_min = _mm_min_pd(current_min, work);
-    current_max = _mm_max_pd(current_max, work);
-    buf += 2;
-    work = _mm_load_pd(buf);
-    current_min = _mm_min_pd(current_min, work);
-    current_max = _mm_max_pd(current_max, work);
-    buf += 2;
-    work = _mm_load_pd(buf);
-    current_min = _mm_min_pd(current_min, work);
-    current_max = _mm_max_pd(current_max, work);
-    buf += 2;
-    nframes -= 8;
-  }
+  while (((unsigned long) buf) % 16 != 0 && nframes > 0)
+    {
+
+      // load one double and replicate
+      work = _mm_set1_pd(*buf);
+      current_min = _mm_min_pd(current_min, work);
+      current_max = _mm_max_pd(current_max, work);
+      buf++;
+      nframes--;
+    }
+
+  while (nframes >= 8)
+    {
+      // use 64 byte prefetch for double octetts
+      // __builtin_prefetch(buf+64,0,0); // for GCC 4.3.2 +
+
+      work = _mm_load_pd(buf);
+      current_min = _mm_min_pd(current_min, work);
+      current_max = _mm_max_pd(current_max, work);
+      buf += 2;
+      work = _mm_load_pd(buf);
+      current_min = _mm_min_pd(current_min, work);
+      current_max = _mm_max_pd(current_max, work);
+      buf += 2;
+      work = _mm_load_pd(buf);
+      current_min = _mm_min_pd(current_min, work);
+      current_max = _mm_max_pd(current_max, work);
+      buf += 2;
+      work = _mm_load_pd(buf);
+      current_min = _mm_min_pd(current_min, work);
+      current_max = _mm_max_pd(current_max, work);
+      buf += 2;
+      nframes -= 8;
+    }
 
   // work through smaller chunks of aligned buffers without prefetching
-  while (nframes >= 2) {
-    work = _mm_load_pd(buf);
-    current_min = _mm_min_pd(current_min, work);
-    current_max = _mm_max_pd(current_max, work);
-    buf += 2;
-    nframes -= 2;
-  }
+  while (nframes >= 2)
+    {
+      work = _mm_load_pd(buf);
+      current_min = _mm_min_pd(current_min, work);
+      current_max = _mm_max_pd(current_max, work);
+      buf += 2;
+      nframes -= 2;
+    }
 
   // work through the remaining value
-  while ( nframes > 0) {
-    // load the last double and replicate
-    work = _mm_set1_pd(*buf);
-    current_min = _mm_min_pd(current_min, work);
-    current_max = _mm_max_pd(current_max, work);
-    buf++;
-    nframes--;
-  }
+  while (nframes > 0)
+    {
+      // load the last double and replicate
+      work = _mm_set1_pd(*buf);
+      current_min = _mm_min_pd(current_min, work);
+      current_max = _mm_max_pd(current_max, work);
+      buf++;
+      nframes--;
+    }
 
   // find final min and max value through shuffle tricks
   work = current_min;
   work = _mm_shuffle_pd(work, work, _MM_SHUFFLE2(0, 1));
-  work = _mm_min_pd (work, current_min);
+  work = _mm_min_pd(work, current_min);
   _mm_store_sd(min, work);
   work = current_max;
   work = _mm_shuffle_pd(work, work, _MM_SHUFFLE2(0, 1));
-  work = _mm_max_pd (work, current_max);
+  work = _mm_max_pd(work, current_max);
   _mm_store_sd(max, work);
 
   return;
 }
 
-#endif // SIMD
+#endif  // SIMD
 
 #if defined(_ARCH_PWR6)
-static
-void pwr6_minmax_val_double_unrolled6(const double *restrict data, size_t datasize, double *fmin, double *fmax)
+static void
+pwr6_minmax_val_double_unrolled6(const double *restrict data, size_t datasize, double *fmin, double *fmax)
 {
 #define __UNROLL_DEPTH_1 6
 
-  // to allow pipelining we have to unroll 
+  // to allow pipelining we have to unroll
 
   {
-    size_t residual =  datasize % __UNROLL_DEPTH_1;
+    size_t residual = datasize % __UNROLL_DEPTH_1;
     size_t ofs = datasize - residual;
     double dmin[__UNROLL_DEPTH_1];
     double dmax[__UNROLL_DEPTH_1];
 
-    for (size_t j = 0; j < __UNROLL_DEPTH_1; ++j) 
+    for (size_t j = 0; j < __UNROLL_DEPTH_1; ++j)
       {
-	dmin[j] = data[0];
-	dmax[j] = data[0];
+        dmin[j] = data[0];
+        dmax[j] = data[0];
       }
-    
-    for (size_t i = 0; i < datasize - residual; i += __UNROLL_DEPTH_1) 
+
+    for (size_t i = 0; i < datasize - residual; i += __UNROLL_DEPTH_1)
       {
-	for (size_t j = 0; j < __UNROLL_DEPTH_1; ++j) 
-	  {
-	    dmin[j] = __fsel(dmin[j] - data[i+j], data[i+j], dmin[j]);
-	    dmax[j] = __fsel(data[i+j] - dmax[j], data[i+j], dmax[j]);
-	  }
+        for (size_t j = 0; j < __UNROLL_DEPTH_1; ++j)
+          {
+            dmin[j] = __fsel(dmin[j] - data[i + j], data[i + j], dmin[j]);
+            dmax[j] = __fsel(data[i + j] - dmax[j], data[i + j], dmax[j]);
+          }
       }
 
-    for (size_t j = 0; j < residual; ++j) 
+    for (size_t j = 0; j < residual; ++j)
       {
-	dmin[j] = __fsel(dmin[j] - data[ofs+j], data[ofs+j], dmin[j]);
-	dmax[j] = __fsel(data[ofs+j] - dmax[j], data[ofs+j], dmax[j]);
+        dmin[j] = __fsel(dmin[j] - data[ofs + j], data[ofs + j], dmin[j]);
+        dmax[j] = __fsel(data[ofs + j] - dmax[j], data[ofs + j], dmax[j]);
       }
 
-    for (size_t j = 0; j < __UNROLL_DEPTH_1; ++j) 
+    for (size_t j = 0; j < __UNROLL_DEPTH_1; ++j)
       {
-	*fmin = __fsel(*fmin - dmin[j], dmin[j], *fmin);
-	*fmax = __fsel(dmax[j] - *fmax, dmax[j], *fmax);
+        *fmin = __fsel(*fmin - dmin[j], dmin[j], *fmin);
+        *fmax = __fsel(dmax[j] - *fmax, dmax[j], *fmax);
       }
   }
 #undef __UNROLL_DEPTH_1
 }
 #endif
 
+// clang-format off
 #if defined(TEST_MINMAXVAL) && defined(__GNUC__)
-static
-void minmax_val_double_orig(const double *restrict data, size_t datasize, double *fmin, double *fmax) __attribute__ ((noinline));
-static
-void minmax_val_double_simd(const double *restrict data, size_t datasize, double *fmin, double *fmax) __attribute__ ((noinline));
-static
-void minmax_val_double_omp(const double *restrict data, size_t datasize, double *fmin, double *fmax) __attribute__ ((noinline));
-static
-void minmax_val_float(const float *restrict data, long datasize, float *fmin, float *fmax) __attribute__ ((noinline));
-static
-void minmax_val_float_simd(const float *restrict data, size_t datasize, float *fmin, float *fmax) __attribute__ ((noinline));
+static void minmax_val_double_orig(const double *restrict data, size_t datasize, double *fmin, double *fmax) __attribute__((noinline));
+static void minmax_val_double_simd(const double *restrict data, size_t datasize, double *fmin, double *fmax) __attribute__((noinline));
+static void minmax_val_double_omp(const double *restrict data, size_t datasize, double *fmin, double *fmax) __attribute__((noinline));
+static void minmax_val_float(const float *restrict data, long datasize, float *fmin, float *fmax) __attribute__((noinline));
+static void minmax_val_float_simd(const float *restrict data, size_t datasize, float *fmin, float *fmax) __attribute__((noinline));
 #endif
+// clang-format on
 
 #if defined(GNUC_PUSH_POP)
 #pragma GCC push_options
-#pragma GCC optimize ("O3", "fast-math")
+#pragma GCC optimize("O3", "fast-math")
 #endif
-static
-void minmax_val_double_orig(const double *restrict data, size_t datasize, double *fmin, double *fmax)
+static void
+minmax_val_double_orig(const double *restrict data, size_t datasize, double *fmin, double *fmax)
 {
   double dmin = *fmin, dmax = *fmax;
 
-#if   defined(CRAY)
+#if defined(CRAY)
 #pragma _CRI ivdep
 #elif defined(SX)
 #pragma vdir nodep
 #elif defined(__uxp__)
 #pragma loop novrec
-#elif defined (__ICC)
+#elif defined(__ICC)
 #pragma ivdep
 #endif
   for (size_t i = 0; i < datasize; ++i)
@@ -811,19 +825,19 @@ void minmax_val_double_orig(const double *restrict data, size_t datasize, double
   *fmax = dmax;
 }
 
-static
-void minmax_val_float(const float *restrict data, long idatasize, float *fmin, float *fmax)
+static void
+minmax_val_float(const float *restrict data, long idatasize, float *fmin, float *fmax)
 {
-  size_t datasize = (size_t)idatasize;
+  size_t datasize = (size_t) idatasize;
   float dmin = *fmin, dmax = *fmax;
 
-#if   defined(CRAY)
+#if defined(CRAY)
 #pragma _CRI ivdep
 #elif defined(SX)
 #pragma vdir nodep
 #elif defined(__uxp__)
 #pragma loop novrec
-#elif defined (__ICC)
+#elif defined(__ICC)
 #pragma ivdep
 #endif
   for (size_t i = 0; i < datasize; ++i)
@@ -844,15 +858,15 @@ void minmax_val_float(const float *restrict data, long idatasize, float *fmin, f
 
 #if defined(GNUC_PUSH_POP)
 #pragma GCC push_options
-#pragma GCC optimize ("O3", "fast-math")
+#pragma GCC optimize("O3", "fast-math")
 #endif
-static
-void minmax_val_double_omp(const double *restrict data, size_t datasize, double *fmin, double *fmax)
+static void
+minmax_val_double_omp(const double *restrict data, size_t datasize, double *fmin, double *fmax)
 {
   double dmin = *fmin, dmax = *fmax;
 
 #if defined(_OPENMP)
-#pragma omp parallel for simd reduction(min:dmin) reduction(max:dmax)
+#pragma omp parallel for simd reduction(min : dmin) reduction(max : dmax)
 #endif
   for (size_t i = 0; i < datasize; ++i)
     {
@@ -864,13 +878,13 @@ void minmax_val_double_omp(const double *restrict data, size_t datasize, double
   *fmax = dmax;
 }
 
-static
-void minmax_val_double_simd(const double *restrict data, size_t datasize, double *fmin, double *fmax)
+static void
+minmax_val_double_simd(const double *restrict data, size_t datasize, double *fmin, double *fmax)
 {
   double dmin = *fmin, dmax = *fmax;
 
 #ifdef _OPENMP
-#pragma omp simd reduction(min:dmin) reduction(max:dmax)
+#pragma omp simd reduction(min : dmin) reduction(max : dmax)
 #endif
   for (size_t i = 0; i < datasize; ++i)
     {
@@ -882,13 +896,13 @@ void minmax_val_double_simd(const double *restrict data, size_t datasize, double
   *fmax = dmax;
 }
 
-static
-void minmax_val_float_simd(const float *restrict data, size_t datasize, float *fmin, float *fmax)
+static void
+minmax_val_float_simd(const float *restrict data, size_t datasize, float *fmin, float *fmax)
 {
   float dmin = *fmin, dmax = *fmax;
 
 #if defined(_OPENMP)
-#pragma omp simd reduction(min:dmin) reduction(max:dmax)
+#pragma omp simd reduction(min : dmin) reduction(max : dmax)
 #endif
   for (size_t i = 0; i < datasize; ++i)
     {
@@ -904,20 +918,23 @@ void minmax_val_float_simd(const float *restrict data, size_t datasize, float *f
 #endif
 #endif
 
-static
-void minmax_val_double(const double *restrict data, long idatasize, double *fmin, double *fmax)
+static void
+minmax_val_double(const double *restrict data, long idatasize, double *fmin, double *fmax)
 {
-#if defined(_GET_X86_COUNTER) || defined(_GET_MACH_COUNTER) 
+#if defined(_GET_X86_COUNTER) || defined(_GET_MACH_COUNTER)
   uint64_t start_minmax, end_minmax;
 #endif
-  size_t datasize = (size_t)idatasize;
+  size_t datasize = (size_t) idatasize;
 
-  if ( idatasize >= 1 ) ; else return;
+  if (idatasize >= 1)
+    ;
+  else
+    return;
 
-#if defined(_GET_X86_COUNTER) 
+#if defined(_GET_X86_COUNTER)
   start_minmax = _rdtsc();
 #endif
-#if defined(_GET_MACH_COUNTER) 
+#if defined(_GET_MACH_COUNTER)
   start_minmax = mach_absolute_time();
 #endif
 
@@ -934,7 +951,7 @@ void minmax_val_double(const double *restrict data, long idatasize, double *fmin
 #if defined(_ARCH_PWR6)
 #define __UNROLL_DEPTH_1 6
 
-  // to allow pipelining we have to unroll 
+    // to allow pipelining we have to unroll
 
 #if defined(_GET_IBM_COUNTER)
   hpmStart(1, "minmax fsel");
@@ -942,43 +959,43 @@ void minmax_val_double(const double *restrict data, long idatasize, double *fmin
 
   pwr6_minmax_val_double_unrolled6(data, datasize, fmin, fmax);
 
-#if defined(_GET_IBM_COUNTER) 
+#if defined(_GET_IBM_COUNTER)
   hpmStop(1);
 #endif
 
 #undef __UNROLL_DEPTH_1
 
-#else // original loop
+#else  // original loop
 
-#if defined(_GET_IBM_COUNTER) 
+#if defined(_GET_IBM_COUNTER)
   hpmStart(1, "minmax base");
 #endif
 
   minmax_val_double_orig(data, datasize, fmin, fmax);
 
-#if defined(_GET_IBM_COUNTER) 
+#if defined(_GET_IBM_COUNTER)
   hpmStop(1);
 #endif
 
-#endif // _ARCH_PWR6 && original loop
-#endif // SIMD
+#endif  // _ARCH_PWR6 && original loop
+#endif  // SIMD
 
 #if defined(_GET_X86_COUNTER) || defined(_GET_MACH_COUNTER)
-#if defined(_GET_X86_COUNTER) 
+#if defined(_GET_X86_COUNTER)
   end_minmax = _rdtsc();
 #endif
-#if defined(_GET_MACH_COUNTER) 
+#if defined(_GET_MACH_COUNTER)
   end_minmax = mach_absolute_time();
 #endif
 #if defined(_ENABLE_AVX)
-  printf("AVX minmax cycles:: %" PRIu64 "\n",  end_minmax-start_minmax);
-  fprintf (stderr, "AVX min: %lf max: %lf\n", *fmin, *fmax);
+  printf("AVX minmax cycles:: %" PRIu64 "\n", end_minmax - start_minmax);
+  fprintf(stderr, "AVX min: %lf max: %lf\n", *fmin, *fmax);
 #elif defined(_ENABLE_SSE2)
-  printf("SSE2 minmax cycles:: %" PRIu64 "\n", end_minmax-start_minmax);
-  fprintf (stderr, "SSE2 min: %lf max: %lf\n", *fmin, *fmax);
+  printf("SSE2 minmax cycles:: %" PRIu64 "\n", end_minmax - start_minmax);
+  fprintf(stderr, "SSE2 min: %lf max: %lf\n", *fmin, *fmax);
 #else
-  printf("loop minmax cycles:: %" PRIu64 "\n", end_minmax-start_minmax);
-  fprintf (stderr, "loop min: %lf max: %lf\n", *fmin, *fmax);
+  printf("loop minmax cycles:: %" PRIu64 "\n", end_minmax - start_minmax);
+  fprintf(stderr, "loop min: %lf max: %lf\n", *fmin, *fmax);
 #endif
 #endif
 
@@ -990,29 +1007,30 @@ void minmax_val_double(const double *restrict data, long idatasize, double *fmin
 #include <stdio.h>
 #include <sys/time.h>
 
-static
-double dtime()
+static double
+dtime()
 {
   double tseconds = 0.0;
   struct timeval mytime;
   gettimeofday(&mytime, NULL);
-  tseconds = (double) (mytime.tv_sec + (double)mytime.tv_usec*1.0e-6);
+  tseconds = (double) (mytime.tv_sec + (double) mytime.tv_usec * 1.0e-6);
   return (tseconds);
 }
 
 #define NRUN 10000
 
-int main(void)
+int
+main(void)
 {
   long datasize = 1000000;
   double t_begin, t_end;
 
   printf("datasize %ld\n", datasize);
-#if   defined(_OPENMP)
+#if defined(_OPENMP)
   printf("_OPENMP=%d\n", _OPENMP);
 #endif
 
-#if   defined(__ICC)
+#if defined(__ICC)
   printf("icc\n");
 #elif defined(__clang__)
   printf("clang\n");
@@ -1022,31 +1040,31 @@ int main(void)
 
   {
     float fmin, fmax;
-    float *data_sp = (float*) malloc(datasize*sizeof(float));
+    float *data_sp = (float *) malloc(datasize * sizeof(float));
 
-    for (long i = 0; i < datasize/2; ++i)        data_sp[i] = (float) (i);
-    for (long i = datasize/2; i < datasize; ++i) data_sp[i] = (float) (-datasize + i);
+    for (long i = 0; i < datasize / 2; ++i) data_sp[i] = (float) (i);
+    for (long i = datasize / 2; i < datasize; ++i) data_sp[i] = (float) (-datasize + i);
 
     printf("float:\n");
 
     t_begin = dtime();
     for (int i = 0; i < NRUN; ++i)
       {
-	fmin = fmax = data_sp[0];
-	minmax_val_float(data_sp, datasize, &fmin, &fmax);
+        fmin = fmax = data_sp[0];
+        minmax_val_float(data_sp, datasize, &fmin, &fmax);
       }
     t_end = dtime();
-    printf("minmax_val: fmin: %ld  fmax: %ld  time: %6.2fs\n", (long)fmin, (long) fmax, t_end-t_begin);
+    printf("minmax_val: fmin: %ld  fmax: %ld  time: %6.2fs\n", (long) fmin, (long) fmax, t_end - t_begin);
 
 #if defined(OMP_SIMD)
     t_begin = dtime();
     for (int i = 0; i < NRUN; ++i)
       {
-	fmin = fmax = data_sp[0];
-	minmax_val_float_simd(data_sp, datasize, &fmin, &fmax);
+        fmin = fmax = data_sp[0];
+        minmax_val_float_simd(data_sp, datasize, &fmin, &fmax);
       }
     t_end = dtime();
-    printf("simd      : fmin: %ld  fmax: %ld  time: %6.2fs\n", (long)fmin, (long) fmax, t_end-t_begin);
+    printf("simd      : fmin: %ld  fmax: %ld  time: %6.2fs\n", (long) fmin, (long) fmax, t_end - t_begin);
 #endif
 
     free(data_sp);
@@ -1054,87 +1072,87 @@ int main(void)
 
   {
     double fmin, fmax;
-    double *data_dp = (double*) malloc(datasize*sizeof(double));
+    double *data_dp = (double *) malloc(datasize * sizeof(double));
 
     // for (long i = datasize-1; i >= 0; --i) data[i] = (double) (-datasize/2 + i);
-    for (long i = 0; i < datasize/2; ++i)        data_dp[i] = (double) (i);
-    for (long i = datasize/2; i < datasize; ++i) data_dp[i] = (double) (-datasize + i);
+    for (long i = 0; i < datasize / 2; ++i) data_dp[i] = (double) (i);
+    for (long i = datasize / 2; i < datasize; ++i) data_dp[i] = (double) (-datasize + i);
 
     printf("double:\n");
 
     t_begin = dtime();
     for (int i = 0; i < NRUN; ++i)
       {
-	fmin = fmax = data_dp[0];
-	minmax_val_double(data_dp, datasize, &fmin, &fmax);
+        fmin = fmax = data_dp[0];
+        minmax_val_double(data_dp, datasize, &fmin, &fmax);
       }
     t_end = dtime();
-    printf("minmax_val: fmin: %ld  fmax: %ld  time: %6.2fs\n", (long)fmin, (long) fmax, t_end-t_begin);
+    printf("minmax_val: fmin: %ld  fmax: %ld  time: %6.2fs\n", (long) fmin, (long) fmax, t_end - t_begin);
 
     t_begin = dtime();
     for (int i = 0; i < NRUN; ++i)
       {
-	fmin = fmax = data_dp[0];
-	minmax_val_double_orig(data_dp, datasize, &fmin, &fmax);
+        fmin = fmax = data_dp[0];
+        minmax_val_double_orig(data_dp, datasize, &fmin, &fmax);
       }
     t_end = dtime();
-    printf("orig      : fmin: %ld  fmax: %ld  time: %6.2fs\n", (long)fmin, (long) fmax, t_end-t_begin);
+    printf("orig      : fmin: %ld  fmax: %ld  time: %6.2fs\n", (long) fmin, (long) fmax, t_end - t_begin);
 
 #if defined(OMP_SIMD)
     t_begin = dtime();
     for (int i = 0; i < NRUN; ++i)
       {
-	fmin = fmax = data_dp[0];
-	minmax_val_double_simd(data_dp, datasize, &fmin, &fmax);
+        fmin = fmax = data_dp[0];
+        minmax_val_double_simd(data_dp, datasize, &fmin, &fmax);
       }
     t_end = dtime();
-    printf("simd      : fmin: %ld  fmax: %ld  time: %6.2fs\n", (long)fmin, (long) fmax, t_end-t_begin);
+    printf("simd      : fmin: %ld  fmax: %ld  time: %6.2fs\n", (long) fmin, (long) fmax, t_end - t_begin);
 
     t_begin = dtime();
     for (int i = 0; i < NRUN; ++i)
       {
-	fmin = fmax = data_dp[0];
-	minmax_val_double_omp(data_dp, datasize, &fmin, &fmax);
+        fmin = fmax = data_dp[0];
+        minmax_val_double_omp(data_dp, datasize, &fmin, &fmax);
       }
     t_end = dtime();
-    printf("openmp %d  : fmin: %ld  fmax: %ld  time: %6.2fs\n", omp_get_max_threads(), (long)fmin, (long) fmax, t_end-t_begin);
+    printf("openmp %d  : fmin: %ld  fmax: %ld  time: %6.2fs\n", omp_get_max_threads(), (long) fmin, (long) fmax, t_end - t_begin);
 #endif
 
 #if defined(_ENABLE_AVX)
     t_begin = dtime();
     for (int i = 0; i < NRUN; ++i)
       {
-	fmin = fmax = data_dp[0];
-	avx_minmax_val_double(data_dp, datasize, &fmin, &fmax);
+        fmin = fmax = data_dp[0];
+        avx_minmax_val_double(data_dp, datasize, &fmin, &fmax);
       }
     t_end = dtime();
-    printf("avx       : fmin: %ld  fmax: %ld  time: %6.2fs\n", (long)fmin, (long) fmax, t_end-t_begin);
+    printf("avx       : fmin: %ld  fmax: %ld  time: %6.2fs\n", (long) fmin, (long) fmax, t_end - t_begin);
 #elif defined(_ENABLE_SSE2)
     t_begin = dtime();
     for (int i = 0; i < NRUN; ++i)
       {
-	fmin = fmax = data_dp[0];
-	sse2_minmax_val_double(data_dp, datasize, &fmin, &fmax);
+        fmin = fmax = data_dp[0];
+        sse2_minmax_val_double(data_dp, datasize, &fmin, &fmax);
       }
     t_end = dtime();
-    printf("sse2      : fmin: %ld  fmax: %ld  time: %6.2fs\n", (long)fmin, (long) fmax, t_end-t_begin);
+    printf("sse2      : fmin: %ld  fmax: %ld  time: %6.2fs\n", (long) fmin, (long) fmax, t_end - t_begin);
 #endif
 #if defined(_ARCH_PWR6)
     t_begin = dtime();
     for (int i = 0; i < NRUN; ++i)
       {
-	fmin = fmax = data_dp[0];
-	pwr6_minmax_val_double_unrolled6(data_dp, datasize, &fmin, &fmax);
+        fmin = fmax = data_dp[0];
+        pwr6_minmax_val_double_unrolled6(data_dp, datasize, &fmin, &fmax);
       }
     t_end = dtime();
-    printf("pwr6u6  : fmin: %ld  fmax: %ld  time: %6.2fs\n", (long)fmin, (long) fmax, t_end-t_begin);
+    printf("pwr6u6  : fmin: %ld  fmax: %ld  time: %6.2fs\n", (long) fmin, (long) fmax, t_end - t_begin);
 #endif
     free(data_dp);
   }
 
   return 0;
 }
-#endif // TEST_MINMAXVAL
+#endif  // TEST_MINMAXVAL
 
 #undef DISABLE_SIMD_MINMAXVAL
 #undef _ENABLE_AVX
@@ -1212,25 +1230,25 @@ xlc_r -g -O3 -qhot -q64 -qarch=auto -qtune=auto -qreport -DTEST_ENCODE encode_ar
 #ifdef TEST_ENCODE
 #include <stdio.h>
 #include <stdlib.h>
-#define  GRIBPACK     unsigned char
+#define GRIBPACK unsigned char
 
 #ifndef HOST_ENDIANNESS
 #ifdef __cplusplus
 static const uint32_t HOST_ENDIANNESS_temp[1] = { UINT32_C(0x00030201) };
-#define HOST_ENDIANNESS (((const unsigned char *)HOST_ENDIANNESS_temp)[0])
+#define HOST_ENDIANNESS (((const unsigned char *) HOST_ENDIANNESS_temp)[0])
 #else
-#define HOST_ENDIANNESS (((const unsigned char *)&(const uint32_t[1]){UINT32_C(0x00030201)})[0])
+#define HOST_ENDIANNESS (((const unsigned char *) &(const uint32_t[1]){ UINT32_C(0x00030201) })[0])
 #endif
 #endif
 
-#define  IS_BIGENDIAN()  (HOST_ENDIANNESS == 0)
-#define  Error(x,y)
+#define IS_BIGENDIAN() (HOST_ENDIANNESS == 0)
+#define Error(x, y)
 #endif
 
-//#undef _GET_X86_COUNTER
-//#undef _GET_MACH_COUNTER
-//#undef _GET_IBM_COUNTER
-//#undef _ARCH_PWR6
+// #undef _GET_X86_COUNTER
+// #undef _GET_MACH_COUNTER
+// #undef _GET_IBM_COUNTER
+// #undef _ARCH_PWR6
 
 #if defined _GET_IBM_COUNTER
 #include <libhpc.h>
@@ -1244,8 +1262,8 @@ static const uint32_t HOST_ENDIANNESS_temp[1] = { UINT32_C(0x00030201) };
 #include <math.h>
 
 #ifndef DISABLE_SIMD
-#if   defined(__GNUC__) && (__GNUC__ >= 4)
-#elif defined(__ICC)    && (__ICC >= 1100)
+#if defined(__GNUC__) && (__GNUC__ >= 4)
+#elif defined(__ICC) && (__ICC >= 1100)
 #elif defined(__clang__)
 #else
 #define DISABLE_SIMD
@@ -1256,24 +1274,24 @@ static const uint32_t HOST_ENDIANNESS_temp[1] = { UINT32_C(0x00030201) };
 #define DISABLE_SIMD_ENCODE
 #endif
 
-//#define DISABLE_SIMD_ENCODE
+// #define DISABLE_SIMD_ENCODE
 
 #ifdef DISABLE_SIMD_ENCODE
-# ifdef ENABLE_AVX
-#  define _ENABLE_AVX
-# endif
-# ifdef ENABLE_SSE4_1
-#  define _ENABLE_SSE4_1
-# endif
+#ifdef ENABLE_AVX
+#define _ENABLE_AVX
+#endif
+#ifdef ENABLE_SSE4_1
+#define _ENABLE_SSE4_1
+#endif
 #endif
 
 #ifndef DISABLE_SIMD_ENCODE
-# ifdef __AVX__
-#  define _ENABLE_AVX
-# endif
-# ifdef __SSE4_1__
-#  define _ENABLE_SSE4_1
-# endif
+#ifdef __AVX__
+#define _ENABLE_AVX
+#endif
+#ifdef __SSE4_1__
+#define _ENABLE_SSE4_1
+#endif
 #endif
 
 #if defined _ENABLE_AVX
@@ -1284,91 +1302,85 @@ static const uint32_t HOST_ENDIANNESS_temp[1] = { UINT32_C(0x00030201) };
 
 #if defined _ENABLE_AVX
 
-static
-void avx_encode_array_2byte_double(size_t datasize,
-				   unsigned char * restrict lGrib,
-				   const double * restrict data,
-				   double zref, double factor, size_t *gz)
-  __attribute__((optimize(2)));
-static
-void avx_encode_array_2byte_double(size_t datasize,
-				   unsigned char * restrict lGrib,
-				   const double * restrict data,
-				   double zref, double factor, size_t *gz) 
+static void avx_encode_array_2byte_double(size_t datasize, unsigned char *restrict lGrib, const double *restrict data, double zref,
+                                          double factor, size_t *gz) __attribute__((optimize(2)));
+static void
+avx_encode_array_2byte_double(size_t datasize, unsigned char *restrict lGrib, const double *restrict data, double zref,
+                              double factor, size_t *gz)
 {
   const double *dval = data;
-  __m128i *sgrib = (__m128i *) (lGrib+(*gz));
+  __m128i *sgrib = (__m128i *) (lGrib + (*gz));
 
   const __m128i swap = _mm_set_epi8(14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1);
 
   const __m256d c0 = _mm256_set1_pd(zref);
   const __m256d c1 = _mm256_set1_pd(factor);
   const __m256d c2 = _mm256_set1_pd(0.5);
-  
+
   __m256d d0, d3, d2, d1;
   __m128i i0, i1, i2, i3;
-  __m128i s0, s1;  
+  __m128i s0, s1;
 
   size_t residual = datasize % 16;
 
   size_t i;
-  for (i = 0; i < (datasize-residual); i += 16)
+  for (i = 0; i < (datasize - residual); i += 16)
     {
-      (void) _mm_prefetch((const char*)(dval+8), _MM_HINT_NTA);
+      (void) _mm_prefetch((const char *) (dval + 8), _MM_HINT_NTA);
       //_____________________________________________________________________________
 
-      d0 = _mm256_loadu_pd (dval);
-      d0 = _mm256_sub_pd (d0, c0);
-      d0 = _mm256_mul_pd (d0, c1);
-      d0 = _mm256_add_pd (d0, c2);
+      d0 = _mm256_loadu_pd(dval);
+      d0 = _mm256_sub_pd(d0, c0);
+      d0 = _mm256_mul_pd(d0, c1);
+      d0 = _mm256_add_pd(d0, c2);
+
+      i0 = _mm256_cvttpd_epi32(d0);
 
-      i0 = _mm256_cvttpd_epi32 (d0);
-      
       //_____________________________________________________________________________
-      
-      d1 = _mm256_loadu_pd (dval+4);
-      d1 = _mm256_sub_pd (d1, c0);
-      d1 = _mm256_mul_pd (d1, c1);
-      d1 = _mm256_add_pd (d1, c2);
-      
-      i1 = _mm256_cvttpd_epi32 (d1);
+
+      d1 = _mm256_loadu_pd(dval + 4);
+      d1 = _mm256_sub_pd(d1, c0);
+      d1 = _mm256_mul_pd(d1, c1);
+      d1 = _mm256_add_pd(d1, c2);
+
+      i1 = _mm256_cvttpd_epi32(d1);
 
       //_____________________________________________________________________________
 
       s0 = _mm_packus_epi32(i0, i1);
-      s0 = _mm_shuffle_epi8 (s0, swap);
-      (void) _mm_storeu_si128 (sgrib, s0);
+      s0 = _mm_shuffle_epi8(s0, swap);
+      (void) _mm_storeu_si128(sgrib, s0);
 
       //_____________________________________________________________________________
 
-      (void) _mm_prefetch((const char*)(dval+16), _MM_HINT_NTA);
+      (void) _mm_prefetch((const char *) (dval + 16), _MM_HINT_NTA);
 
       //_____________________________________________________________________________
-      
-      d2 = _mm256_loadu_pd (dval+8);
-      d2 = _mm256_sub_pd (d2, c0);
-      d2 = _mm256_mul_pd (d2, c1);
-      d2 = _mm256_add_pd (d2, c2);
-      
-      i2 = _mm256_cvttpd_epi32 (d2);
+
+      d2 = _mm256_loadu_pd(dval + 8);
+      d2 = _mm256_sub_pd(d2, c0);
+      d2 = _mm256_mul_pd(d2, c1);
+      d2 = _mm256_add_pd(d2, c2);
+
+      i2 = _mm256_cvttpd_epi32(d2);
 
       //_____________________________________________________________________________
-      
-      d3 = _mm256_loadu_pd (dval+12);
-      d3 = _mm256_sub_pd (d3, c0);
-      d3 = _mm256_mul_pd (d3, c1);
-      d3 = _mm256_add_pd (d3, c2);
-      
-      i3 = _mm256_cvttpd_epi32 (d3);
+
+      d3 = _mm256_loadu_pd(dval + 12);
+      d3 = _mm256_sub_pd(d3, c0);
+      d3 = _mm256_mul_pd(d3, c1);
+      d3 = _mm256_add_pd(d3, c2);
+
+      i3 = _mm256_cvttpd_epi32(d3);
 
       //_____________________________________________________________________________
 
       s1 = _mm_packus_epi32(i2, i3);
-      s1 = _mm_shuffle_epi8 (s1, swap);
-      (void) _mm_storeu_si128 (sgrib+1, s1);
+      s1 = _mm_shuffle_epi8(s1, swap);
+      (void) _mm_storeu_si128(sgrib + 1, s1);
 
       //_____________________________________________________________________________
-           
+
       dval += 16;
       sgrib += 2;
     }
@@ -1377,14 +1389,14 @@ void avx_encode_array_2byte_double(size_t datasize,
     {
       uint16_t ui16;
       for (size_t j = i; j < datasize; ++j)
-	{
-	  ui16 = (uint16_t) ((data[j] - zref) * factor + 0.5);
-	  lGrib[*gz+2*j  ] = ui16 >>  8;
-	  lGrib[*gz+2*j+1] = ui16;
-	}
+        {
+          ui16 = (uint16_t) ((data[j] - zref) * factor + 0.5);
+          lGrib[*gz + 2 * j] = ui16 >> 8;
+          lGrib[*gz + 2 * j + 1] = ui16;
+        }
     }
-  
-  *gz += 2*datasize;
+
+  *gz += 2 * datasize;
 
   return;
 }
@@ -1393,130 +1405,128 @@ void avx_encode_array_2byte_double(size_t datasize,
 
 #elif defined _ENABLE_SSE4_1
 
-static
-void sse41_encode_array_2byte_double(size_t datasize, 
-				     unsigned char * restrict lGrib,
-				     const double * restrict data, 
-				     double zref, double factor, size_t *gz) 
+static void
+sse41_encode_array_2byte_double(size_t datasize, unsigned char *restrict lGrib, const double *restrict data, double zref,
+                                double factor, size_t *gz)
 {
   const double *dval = data;
-  __m128i *sgrib = (__m128i *) (lGrib+(*gz));
+  __m128i *sgrib = (__m128i *) (lGrib + (*gz));
 
   const __m128i swap = _mm_set_epi8(14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1);
 
   const __m128d c0 = _mm_set1_pd(zref);
   const __m128d c1 = _mm_set1_pd(factor);
   const __m128d c2 = _mm_set1_pd(0.5);
-  
+
   __m128d d0, d4, d3, d2, d1;
   __m128i i0, i1, i2, i3, i4;
-  __m128i s0, s1;  
+  __m128i s0, s1;
 
   size_t residual = datasize % 16;
 
   size_t i;
-  for (i = 0; i < (datasize-residual); i += 16)
+  for (i = 0; i < (datasize - residual); i += 16)
     {
-      (void) _mm_prefetch((const char*)(dval+8), _MM_HINT_NTA);
+      (void) _mm_prefetch((const char *) (dval + 8), _MM_HINT_NTA);
       //_____________________________________________________________________________
 
-      d0 = _mm_loadu_pd (dval);
-      d0 = _mm_sub_pd (d0, c0);
-      d0 = _mm_mul_pd (d0, c1);
-      d0 = _mm_add_pd (d0, c2);
-      
-      d4 = _mm_loadu_pd (dval+2);
-      d4 = _mm_sub_pd (d4, c0);
-      d4 = _mm_mul_pd (d4, c1);
-      d4 = _mm_add_pd (d4, c2);
+      d0 = _mm_loadu_pd(dval);
+      d0 = _mm_sub_pd(d0, c0);
+      d0 = _mm_mul_pd(d0, c1);
+      d0 = _mm_add_pd(d0, c2);
+
+      d4 = _mm_loadu_pd(dval + 2);
+      d4 = _mm_sub_pd(d4, c0);
+      d4 = _mm_mul_pd(d4, c1);
+      d4 = _mm_add_pd(d4, c2);
 
-      i0 = _mm_cvttpd_epi32 (d0);
-      i4 = _mm_cvttpd_epi32 (d4);  
-      i0 = _mm_unpacklo_epi64 (i0, i4);
+      i0 = _mm_cvttpd_epi32(d0);
+      i4 = _mm_cvttpd_epi32(d4);
+      i0 = _mm_unpacklo_epi64(i0, i4);
 
       //_____________________________________________________________________________
-      
-      d1 = _mm_loadu_pd (dval+4);
-      d1 = _mm_sub_pd (d1, c0);
-      d1 = _mm_mul_pd (d1, c1);
-      d1 = _mm_add_pd (d1, c2);
-      
-      d4 = _mm_loadu_pd (dval+6);
-      d4 = _mm_sub_pd (d4, c0);
-      d4 = _mm_mul_pd (d4, c1);
-      d4 = _mm_add_pd (d4, c2);
-      
-      i1 = _mm_cvttpd_epi32 (d1);
-      i4 = _mm_cvttpd_epi32 (d4);  
-      i1 = _mm_unpacklo_epi64 (i1, i4);
+
+      d1 = _mm_loadu_pd(dval + 4);
+      d1 = _mm_sub_pd(d1, c0);
+      d1 = _mm_mul_pd(d1, c1);
+      d1 = _mm_add_pd(d1, c2);
+
+      d4 = _mm_loadu_pd(dval + 6);
+      d4 = _mm_sub_pd(d4, c0);
+      d4 = _mm_mul_pd(d4, c1);
+      d4 = _mm_add_pd(d4, c2);
+
+      i1 = _mm_cvttpd_epi32(d1);
+      i4 = _mm_cvttpd_epi32(d4);
+      i1 = _mm_unpacklo_epi64(i1, i4);
 
       //_____________________________________________________________________________
 
       s0 = _mm_packus_epi32(i0, i1);
-      s0 = _mm_shuffle_epi8 (s0, swap);
-      (void) _mm_storeu_si128 (sgrib, s0);
+      s0 = _mm_shuffle_epi8(s0, swap);
+      (void) _mm_storeu_si128(sgrib, s0);
 
       //_____________________________________________________________________________
 
-      (void) _mm_prefetch((const char*)(dval+16), _MM_HINT_NTA);
+      (void) _mm_prefetch((const char *) (dval + 16), _MM_HINT_NTA);
 
       //_____________________________________________________________________________
-      
-      d2 = _mm_loadu_pd (dval+8);
-      d2 = _mm_sub_pd (d2, c0);
-      d2 = _mm_mul_pd (d2, c1);
-      d2 = _mm_add_pd (d2, c2);
-      
-      d4 = _mm_loadu_pd (dval+10);
-      d4 = _mm_sub_pd (d4, c0);
-      d4 = _mm_mul_pd (d4, c1);
-      d4 = _mm_add_pd (d4, c2);
-      
-      i2 = _mm_cvttpd_epi32 (d2);
-      i4  = _mm_cvttpd_epi32 (d4);  
-      i2 = _mm_unpacklo_epi64 (i2, i4);
+
+      d2 = _mm_loadu_pd(dval + 8);
+      d2 = _mm_sub_pd(d2, c0);
+      d2 = _mm_mul_pd(d2, c1);
+      d2 = _mm_add_pd(d2, c2);
+
+      d4 = _mm_loadu_pd(dval + 10);
+      d4 = _mm_sub_pd(d4, c0);
+      d4 = _mm_mul_pd(d4, c1);
+      d4 = _mm_add_pd(d4, c2);
+
+      i2 = _mm_cvttpd_epi32(d2);
+      i4 = _mm_cvttpd_epi32(d4);
+      i2 = _mm_unpacklo_epi64(i2, i4);
 
       //_____________________________________________________________________________
-      
-      d3 = _mm_loadu_pd (dval+12);
-      d3 = _mm_sub_pd (d3, c0);
-      d3 = _mm_mul_pd (d3, c1);
-      d3 = _mm_add_pd (d3, c2);
-      
-      d4 = _mm_loadu_pd (dval+14);
-      d4 = _mm_sub_pd (d4, c0);
-      d4 = _mm_mul_pd (d4, c1);
-      d4 = _mm_add_pd (d4, c2);
-      
-      i3 = _mm_cvttpd_epi32 (d3);
-      i4 = _mm_cvttpd_epi32 (d4);  
-      i3 = _mm_unpacklo_epi64 (i3, i4);
+
+      d3 = _mm_loadu_pd(dval + 12);
+      d3 = _mm_sub_pd(d3, c0);
+      d3 = _mm_mul_pd(d3, c1);
+      d3 = _mm_add_pd(d3, c2);
+
+      d4 = _mm_loadu_pd(dval + 14);
+      d4 = _mm_sub_pd(d4, c0);
+      d4 = _mm_mul_pd(d4, c1);
+      d4 = _mm_add_pd(d4, c2);
+
+      i3 = _mm_cvttpd_epi32(d3);
+      i4 = _mm_cvttpd_epi32(d4);
+      i3 = _mm_unpacklo_epi64(i3, i4);
 
       //_____________________________________________________________________________
 
       s1 = _mm_packus_epi32(i2, i3);
-      s1 = _mm_shuffle_epi8 (s1, swap);
-      (void) _mm_storeu_si128 (sgrib+1, s1);
+      s1 = _mm_shuffle_epi8(s1, swap);
+      (void) _mm_storeu_si128(sgrib + 1, s1);
 
       //_____________________________________________________________________________
-           
+
       dval += 16;
       sgrib += 2;
     }
 
-  if (i != datasize) 
+  if (i != datasize)
     {
       uint16_t ui16;
       for (size_t j = i; j < datasize; ++j)
-	{
-	  ui16 = (uint16_t) ((data[j] - zref) * factor + 0.5);
-	  lGrib[*gz+2*j  ] = ui16 >>  8;
-	  lGrib[*gz+2*j+1] = ui16;
-	}
+        {
+          ui16 = (uint16_t) ((data[j] - zref) * factor + 0.5);
+          lGrib[*gz + 2 * j] = ui16 >> 8;
+          lGrib[*gz + 2 * j + 1] = ui16;
+        }
     }
 
-  *gz += 2*datasize;
-  
+  *gz += 2 * datasize;
+
   return;
 }
 
@@ -1526,11 +1536,11 @@ void sse41_encode_array_2byte_double(size_t datasize,
 
 #define grib_encode_array_2byte_double encode_array_2byte_double
 
-#endif // SIMD variants
-
+#endif  // SIMD variants
 
 #ifdef TEST_ENCODE
 
+// clang-format off
 #define CAT(X,Y)      X##_##Y
 #define TEMPLATE(X,Y) CAT(X,Y)
 
@@ -1543,43 +1553,44 @@ void sse41_encode_array_2byte_double(size_t datasize,
 #undef T
 #endif
 #define T float
-
+// clang-format on
 
 #include <sys/time.h>
 
-static
-double dtime()
+static double
+dtime()
 {
   double tseconds = 0.0;
   struct timeval mytime;
   gettimeofday(&mytime, NULL);
-  tseconds = (double) (mytime.tv_sec + (double)mytime.tv_usec*1.0e-6);
+  tseconds = (double) (mytime.tv_sec + (double) mytime.tv_usec * 1.0e-6);
   return (tseconds);
 }
 
-
-static
-void pout(char *name, int s, unsigned char *lgrib, long datasize, double tt)
+static void
+pout(char *name, int s, unsigned char *lgrib, long datasize, double tt)
 {
-  printf("%8s: val1: %d  val2: %d  val3: %d  valn: %d  time: %gs\n",
-         name, (int) lgrib[s*1+1], (int) lgrib[s*2+1], (int) lgrib[s*3+1], (int) lgrib[2*datasize-1], tt);
+  printf("%8s: val1: %d  val2: %d  val3: %d  valn: %d  time: %gs\n", name, (int) lgrib[s * 1 + 1], (int) lgrib[s * 2 + 1],
+         (int) lgrib[s * 3 + 1], (int) lgrib[2 * datasize - 1], tt);
 }
 
-int main(void)
+int
+main(void)
 {
-  enum {
+  enum
+  {
     datasize = 1000000,
     NRUN = 10000,
   };
 
   double t_begin, t_end;
 
-  float *dataf = (float*) malloc(datasize*sizeof(float));
-  double *data = (double*) malloc(datasize*sizeof(double));
-  unsigned char *lgrib = (unsigned char*) malloc(2*datasize*sizeof(unsigned char));
+  float *dataf = (float *) malloc(datasize * sizeof(float));
+  double *data = (double *) malloc(datasize * sizeof(double));
+  unsigned char *lgrib = (unsigned char *) malloc(2 * datasize * sizeof(unsigned char));
 
-  for (long i = 0; i < datasize; ++i) dataf[i] = (float) (-datasize/2 + i);
-  for (long i = 0; i < datasize; ++i) data[i] = (double) (-datasize/2 + i);
+  for (long i = 0; i < datasize; ++i) dataf[i] = (float) (-datasize / 2 + i);
+  for (long i = 0; i < datasize; ++i) data[i] = (double) (-datasize / 2 + i);
 
   int PackStart = 0;
   int nbpv = 16;
@@ -1588,13 +1599,13 @@ int main(void)
   double factor = 0.00390625;
   int s = 256;
 
-  if ( 0 )
+  if (0)
     {
       encode_array_float(0, 0, 0, NULL, NULL, 0, 0, NULL);
       encode_array_double(0, 0, 0, NULL, NULL, 0, 0, NULL);
     }
 
-#if   defined(__ICC)
+#if defined(__ICC)
   printf("icc\n");
 #elif defined(__clang__)
   printf("clang\n");
@@ -1608,19 +1619,19 @@ int main(void)
   for (int i = 0; i < NRUN; ++i)
     {
       z = 0;
-      encode_array_2byte_float(datasize, lgrib, dataf, (float)zref, (float)factor, &z);
+      encode_array_2byte_float(datasize, lgrib, dataf, (float) zref, (float) factor, &z);
     }
   t_end = dtime();
-  pout("orig", s, lgrib, datasize, t_end-t_begin);
+  pout("orig", s, lgrib, datasize, t_end - t_begin);
 
   t_begin = dtime();
   for (int i = 0; i < NRUN; ++i)
     {
       z = 0;
-      encode_array_unrolled_float(nbpv, PackStart, datasize, lgrib, dataf, (float)zref, (float)factor, &z);
+      encode_array_unrolled_float(nbpv, PackStart, datasize, lgrib, dataf, (float) zref, (float) factor, &z);
     }
   t_end = dtime();
-  pout("unrolled", s, lgrib, datasize, t_end-t_begin);
+  pout("unrolled", s, lgrib, datasize, t_end - t_begin);
 
   printf("double:\n");
 
@@ -1631,7 +1642,7 @@ int main(void)
       encode_array_2byte_double(datasize, lgrib, data, zref, factor, &z);
     }
   t_end = dtime();
-  pout("orig", s, lgrib, datasize, t_end-t_begin);
+  pout("orig", s, lgrib, datasize, t_end - t_begin);
 
   t_begin = dtime();
   for (int i = 0; i < NRUN; ++i)
@@ -1640,7 +1651,7 @@ int main(void)
       encode_array_unrolled_double(nbpv, PackStart, datasize, lgrib, data, zref, factor, &z);
     }
   t_end = dtime();
-  pout("unrolled", s, lgrib, datasize, t_end-t_begin);
+  pout("unrolled", s, lgrib, datasize, t_end - t_begin);
 
 #if defined _ENABLE_AVX
   t_begin = dtime();
@@ -1650,7 +1661,7 @@ int main(void)
       avx_encode_array_2byte_double(datasize, lgrib, data, zref, factor, &z);
     }
   t_end = dtime();
-  pout("avx", s, lgrib, datasize, t_end-t_begin);
+  pout("avx", s, lgrib, datasize, t_end - t_begin);
 #elif defined _ENABLE_SSE4_1
   t_begin = dtime();
   for (int i = 0; i < NRUN; ++i)
@@ -1659,12 +1670,12 @@ int main(void)
       sse41_encode_array_2byte_double(datasize, lgrib, data, zref, factor, &z);
     }
   t_end = dtime();
-  pout("sse41", s, lgrib, datasize, t_end-t_begin);
+  pout("sse41", s, lgrib, datasize, t_end - t_begin);
 #endif
 
   return 0;
 }
-#endif // TEST_ENCODE
+#endif  // TEST_ENCODE
 
 #undef DISABLE_SIMD_ENCODE
 #undef _ENABLE_AVX
@@ -1771,11 +1782,11 @@ confp3(double pval, int *kexp, int *kmant, int kbits, int kround)
   /*   Section 3 . Convert other values.                               */
   /* ----------------------------------------------------------------- */
   {
-    const double zeps = (kbits != 32) ? 1.0e-12 : 1.0e-8;
+    double zeps = (kbits != 32) ? 1.0e-12 : 1.0e-8;
     double zref = pval;
 
     // Sign of value.
-    const int isign = (zref >= 0.0) ? 0 : 128;
+    int isign = (zref >= 0.0) ? 0 : 128;
     zref = fabs(zref);
 
     // Exponent.
@@ -1878,8 +1889,8 @@ LABEL900:
 } /* confp3 */
 #include <math.h>
 
-
-double decfp2(int kexp, int kmant)
+double
+decfp2(int kexp, int kmant)
 {
   /*
 
@@ -1950,7 +1961,7 @@ double decfp2(int kexp, int kmant)
   /*   Section 1 . Convert value of 0.0. Ignore sign bit.              */
   /* ----------------------------------------------------------------- */
 
-  if ( (kexp == 128) || (kexp == 0) || (kexp == 255) ) return 0.0;
+  if ((kexp == 128) || (kexp == 0) || (kexp == 255)) return 0.0;
 
   /* ----------------------------------------------------------------- */
   /*   Section 2 . Convert other values.                               */
@@ -1959,7 +1970,7 @@ double decfp2(int kexp, int kmant)
   /*  Sign of value. */
 
   int iexp = kexp;
-  const int isign = (iexp < 128) * 2 - 1;
+  int isign = (iexp < 128) * 2 - 1;
 
   iexp -= iexp < 128 ? 0 : 128;
 
@@ -1969,7 +1980,7 @@ double decfp2(int kexp, int kmant)
 
   iexp -= 64;
 
-  const double pval = ldexp(1.0, 4 * iexp) * isign * POW_2_M24 * kmant;
+  double pval = ldexp(1.0, 4 * iexp) * isign * POW_2_M24 * kmant;
 
   /* ----------------------------------------------------------------- */
   /*   Section 9. Return to calling routine.                           */
@@ -1980,9 +1991,8 @@ double decfp2(int kexp, int kmant)
 #include <stdarg.h>
 #include <stdint.h>
 
-
-static
-void gribDecodeRefDate(const int *isec1, int *year, int *month, int *day)
+static void
+gribDecodeRefDate(const int *isec1, int *year, int *month, int *day)
 {
   int ryear = ISEC1_Year;
 
@@ -1998,16 +2008,16 @@ void gribDecodeRefDate(const int *isec1, int *year, int *month, int *day)
     {
       // if ( century != 0 )
       {
-        if ( ryear == 100 )
+        if (ryear == 100)
           {
             ryear = 0;
             century += 1;
           }
 
-        if ( ryear != 255 )
+        if (ryear != 255)
           {
-            ryear = century*100 + ryear;
-            if ( ISEC1_Century < 0 ) ryear = -ryear;
+            ryear = century * 100 + ryear;
+            if (ISEC1_Century < 0) ryear = -ryear;
           }
         else
           {
@@ -2016,41 +2026,41 @@ void gribDecodeRefDate(const int *isec1, int *year, int *month, int *day)
       }
     }
 
-  *year  = ryear;
+  *year = ryear;
   *month = ISEC1_Month;
-  *day   = ISEC1_Day;
+  *day = ISEC1_Day;
 }
 
-
-int gribRefDate(const int *isec1)
+int
+gribRefDate(const int *isec1)
 {
   int ryear, rmonth, rday;
   gribDecodeRefDate(isec1, &ryear, &rmonth, &rday);
-  return cdiEncodeDate(ryear, rmonth, rday);
+  return (int) cdiEncodeDate(ryear, rmonth, rday);
 }
 
-static
-void gribDecodeRefTime(const int *isec1, int *hour, int *minute, int *second)
+static void
+gribDecodeRefTime(const int *isec1, int *hour, int *minute, int *second)
 {
   *hour = ISEC1_Hour;
   *minute = ISEC1_Minute;
   *second = 0;
 }
 
-
-int gribRefTime(const int *isec1)
+int
+gribRefTime(const int *isec1)
 {
   int rhour, rminute, rsecond;
   gribDecodeRefTime(isec1, &rhour, &rminute, &rsecond);
   return cdiEncodeTime(rhour, rminute, rsecond);
 }
 
-
-bool gribTimeIsFC(const int *isec1)
+bool
+gribTimeIsFC(const int *isec1)
 {
   bool isFC = false;
 
-  const int time_period = (ISEC1_TimeRange == 10) ? (ISEC1_TimePeriod1<<8) + ISEC1_TimePeriod2 : ISEC1_TimePeriod1;
+  const int time_period = (ISEC1_TimeRange == 10) ? (ISEC1_TimePeriod1 << 8) + ISEC1_TimePeriod2 : ISEC1_TimePeriod1;
 
   if (time_period > 0 && ISEC1_Day > 0)
     {
@@ -2060,11 +2070,12 @@ bool gribTimeIsFC(const int *isec1)
   return isFC;
 }
 
-static
-int getTimeUnitFactor(int timeUnit)
+static int
+getTimeUnitFactor(int timeUnit)
 {
   static bool lprint = true;
-  switch ( timeUnit )
+  // clang-format off
+  switch (timeUnit)
     {
     case ISEC1_TABLE4_MINUTE:    return    60; break;
     case ISEC1_TABLE4_QUARTER:   return   900; break;
@@ -2075,19 +2086,20 @@ int getTimeUnitFactor(int timeUnit)
     case ISEC1_TABLE4_12HOURS:   return 43200; break;
     case ISEC1_TABLE4_DAY:       return 86400; break;
     default:
-      if ( lprint )
+      if (lprint)
         {
           gprintf(__func__, "Time unit %d unsupported", timeUnit);
           lprint = false;
         }
       break;
     }
+  // clang-format on
 
   return 0;
 }
 
-
-void gribDateTimeX(int *isec1, int *date, int *time, int *startDate, int *startTime)
+void
+gribDateTimeX(int *isec1, int *date, int *time, int *startDate, int *startTime)
 {
   *startDate = 0;
   *startTime = 0;
@@ -2096,8 +2108,8 @@ void gribDateTimeX(int *isec1, int *date, int *time, int *startDate, int *startT
 
   int64_t time_period = 0, time_period_x = 0;
   if (ISEC1_TimeRange == 10)
-    time_period = (ISEC1_TimePeriod1<<8) + ISEC1_TimePeriod2;
-  else if (ISEC1_TimeRange >=2 && ISEC1_TimeRange <= 5)
+    time_period = (ISEC1_TimePeriod1 << 8) + ISEC1_TimePeriod2;
+  else if (ISEC1_TimeRange >= 2 && ISEC1_TimeRange <= 5)
     {
       time_period_x = ISEC1_TimePeriod1;
       time_period = ISEC1_TimePeriod2;
@@ -2116,42 +2128,42 @@ void gribDateTimeX(int *isec1, int *date, int *time, int *startDate, int *startT
           JulianDate julianDate2 = julianDate_add_seconds(julianDate, timeUnitFactor * time_period_x);
           CdiDateTime sDateTime = julianDate_decode(CGRIBEX_grib_calendar, julianDate2);
           sDateTime.time.second = 0;
-          *startDate = (int)cdiDate_get(sDateTime.date);
+          *startDate = (int) cdiDate_get(sDateTime.date);
           *startTime = cdiTime_get(sDateTime.time);
         }
-      
+
       julianDate = julianDate_add_seconds(julianDate, timeUnitFactor * time_period);
       rDateTime = julianDate_decode(CGRIBEX_grib_calendar, julianDate);
     }
 
-  *date = (int)cdiDate_get(rDateTime.date);
+  *date = (int) cdiDate_get(rDateTime.date);
   *time = cdiTime_get(rDateTime.time);
 }
 
-
-void gribDateTime(int *isec1, int *date, int *time)
+void
+gribDateTime(int *isec1, int *date, int *time)
 {
   int sdate, stime;
   gribDateTimeX(isec1, date, time, &sdate, &stime);
 }
 
-
-void gprintf(const char *caller, const char *fmt, ...)
+void
+gprintf(const char *caller, const char *fmt, ...)
 {
   va_list args;
 
-  if ( grprsm == NULL ) Error("GRIBEX initialization missing!");
-	
+  if (grprsm == NULL) Error("GRIBEX initialization missing!");
+
   va_start(args, fmt);
 
-   fprintf(grprsm, "%-18s : ", caller);
+  fprintf(grprsm, "%-18s : ", caller);
   vfprintf(grprsm, fmt, args);
-   fputs("\n", grprsm);
+  fputs("\n", grprsm);
 
   va_end(args);
 }
 
-
+// clang-format off
 void
 gribExDP(int *isec0, int *isec1, int *isec2, double *fsec2, int *isec3,
 	 double *fsec3, int *isec4, double *fsec4, int klenp, int *kgrib,
@@ -2212,6 +2224,7 @@ gribExSP(int *isec0, int *isec1, int *isec2, float *fsec2, int *isec3,
       *kret=-9;
     }
 }
+// clang-format on
 
 int CGRIBEX_Fix_ZSE = 0; /* 1: Fix ZeroShiftError of simple packed spherical harmonics */
 int CGRIBEX_Const = 0;   /* 1: Don't pack constant fields on regular grids */
@@ -2267,9 +2280,8 @@ gribSetValueCheck(int vcheck)
 #include <string.h>
 #include <math.h>
 
-
-
-void gribPrintSec0(int *isec0)
+void
+gribPrintSec0(int *isec0)
 {
   /*
 
@@ -2296,15 +2308,15 @@ void gribPrintSec0(int *isec0)
   fprintf(grprsm, " GRIB Edition Number.                 %9d\n", ISEC0_GRIB_Version);
 }
 
-void gribPrintSec1(int *isec0, int *isec1)
+void
+gribPrintSec1(int *isec0, int *isec1)
 {
   /*
 
     Print the information in the Product Definition
     Section (Section 1) of decoded GRIB data.
 
-    Input Parameters:
-
+    Input Parameters
        isec0 - Array of decoded integers from Section 0
 
        isec1 - Array of decoded integers from Section 1
@@ -2370,7 +2382,7 @@ void gribPrintSec1(int *isec0, int *isec1)
       ENDIF
   */
 
-  if ( isec1[5] != 127 )
+  if (isec1[5] != 127)
     {
       fprintf(grprsm, " Type of level (Code Table 3).        %9d\n", isec1[6]);
       fprintf(grprsm, " Value 1 of level (Code Table 3).     %9d\n", isec1[7]);
@@ -2383,12 +2395,12 @@ void gribPrintSec1(int *isec0, int *isec1)
     }
 
   iyear = isec1[9];
-  if ( iyear != 255 )
+  if (iyear != 255)
     {
       int date, time;
       /* iyear  = ((isec1[20]-1)*100 + isec1[9]); */
       gribDateTime(isec1, &date, &time);
-      iyear = date/10000;
+      iyear = date / 10000;
       fprintf(grprsm, " Year of reference time of data.      %9d  (%4d)\n", isec1[9], iyear);
     }
   else
@@ -2410,7 +2422,7 @@ void gribPrintSec1(int *isec0, int *isec1)
      All ECMWF data in GRIB Editions before Edition 1 is decoded
      as 20th century data. Other centres are decoded as missing.
   */
-  if ( isec0[1] < 1 && isec1[1] != 98 )
+  if (isec0[1] < 1 && isec1[1] != 98)
     fprintf(grprsm, " Century of reference time of data.   Not given\n");
   else
     fprintf(grprsm, " Century of reference time of data.   %9d\n", isec1[20]);
@@ -2426,31 +2438,28 @@ void gribPrintSec1(int *isec0, int *isec1)
     Section 1 . Print local DWD information.
     -----------------------------------------------------------------
   */
-  if ( (ISEC1_CenterID == 78 || ISEC1_CenterID == 215 || ISEC1_CenterID == 250) &&
-       (isec1[36] == 253     || isec1[36] == 254) )
+  if ((ISEC1_CenterID == 78 || ISEC1_CenterID == 215 || ISEC1_CenterID == 250) && (isec1[36] == 253 || isec1[36] == 254))
     {
       fprintf(grprsm, " DWD local usage identifier.          %9d\n", isec1[36]);
-      if ( isec1[36] == 253 )
-	fprintf(grprsm, " (Database labelling and ensemble forecast)\n");
-      if ( isec1[36] == 254 )
-	fprintf(grprsm, " (Database labelling)\n");
+      if (isec1[36] == 253) fprintf(grprsm, " (Database labelling and ensemble forecast)\n");
+      if (isec1[36] == 254) fprintf(grprsm, " (Database labelling)\n");
 
-      fprintf(grprsm, " Year of database entry                     %3d  (%4d)\n", isec1[43], 1900+isec1[43]);
+      fprintf(grprsm, " Year of database entry                     %3d  (%4d)\n", isec1[43], 1900 + isec1[43]);
       fprintf(grprsm, " Month of database entry                    %3d\n", isec1[44]);
       fprintf(grprsm, " Day of database entry                      %3d\n", isec1[45]);
       fprintf(grprsm, " Hour of database entry                     %3d\n", isec1[46]);
       fprintf(grprsm, " Minute of database entry                   %3d\n", isec1[47]);
-      fprintf(grprsm, " DWD experiment number                %9d\n",isec1[48]);
-      fprintf(grprsm, " DWD run type                         %9d\n",isec1[49]);
-      if ( isec1[36] == 253 ) 
-	{
-	  fprintf(grprsm, " User id                              %9d\n",isec1[50]);
-	  fprintf(grprsm, " Experiment identifier                %9d\n",isec1[51]);
-	  fprintf(grprsm, " Ensemble identification type         %9d\n",isec1[52]);
-	  fprintf(grprsm, " Number of ensemble members           %9d\n",isec1[53]);
-	  fprintf(grprsm, " Actual number of ensemble member     %9d\n",isec1[54]);
-	  fprintf(grprsm, " Model version                            %2d.%2.2d\n",isec1[55],isec1[56]);
-	}
+      fprintf(grprsm, " DWD experiment number                %9d\n", isec1[48]);
+      fprintf(grprsm, " DWD run type                         %9d\n", isec1[49]);
+      if (isec1[36] == 253)
+        {
+          fprintf(grprsm, " User id                              %9d\n", isec1[50]);
+          fprintf(grprsm, " Experiment identifier                %9d\n", isec1[51]);
+          fprintf(grprsm, " Ensemble identification type         %9d\n", isec1[52]);
+          fprintf(grprsm, " Number of ensemble members           %9d\n", isec1[53]);
+          fprintf(grprsm, " Actual number of ensemble member     %9d\n", isec1[54]);
+          fprintf(grprsm, " Model version                            %2d.%2.2d\n", isec1[55], isec1[56]);
+        }
     }
 
   /*
@@ -2461,352 +2470,316 @@ void gribPrintSec1(int *isec0, int *isec1)
   /*
     Regular MARS labelling, or reformatted Washington EPS products.
   */
-  if ( (ISEC1_CenterID    == 98 && ISEC1_LocalFLag ==  1) ||
-       (ISEC1_SubCenterID == 98 && ISEC1_LocalFLag ==  1) ||
-       (ISEC1_CenterID    ==  7 && ISEC1_SubCenterID == 98) )
+  if ((ISEC1_CenterID == 98 && ISEC1_LocalFLag == 1) || (ISEC1_SubCenterID == 98 && ISEC1_LocalFLag == 1)
+      || (ISEC1_CenterID == 7 && ISEC1_SubCenterID == 98))
     {
       /*   Parameters common to all definitions.  */
 
       fprintf(grprsm, " ECMWF local usage identifier.        %9d\n", isec1[36]);
-      if ( isec1[36] == 1 )
-	fprintf(grprsm, " (Mars labelling or ensemble forecast)\n");
-      if ( isec1[36] == 2 )
-        fprintf(grprsm, " (Cluster means and standard deviations)\n");
-      if ( isec1[36] == 3 )
-        fprintf(grprsm, " (Satellite image data)\n");
-      if ( isec1[36] == 4 )
-        fprintf(grprsm, " (Ocean model data)\n");
-      if ( isec1[36] == 5 )
-        fprintf(grprsm, " (Forecast probability data)\n");
-      if ( isec1[36] == 6 )
-        fprintf(grprsm, " (Surface temperature data)\n");
-      if ( isec1[36] == 7 )
-        fprintf(grprsm, " (Sensitivity data)\n");
-      if ( isec1[36] == 8 )
-        fprintf(grprsm, " (ECMWF re-analysis data)\n");
-      if ( isec1[36] == 9 )
-        fprintf(grprsm, " (Singular vectors and ensemble perturbations)\n");
-      if ( isec1[36] == 10 )
-        fprintf(grprsm, " (EPS tubes)\n");
-      if ( isec1[36] == 11 )
-        fprintf(grprsm, " (Supplementary data used by analysis)\n");
-      if ( isec1[36] == 13 )
-        fprintf(grprsm, " (Wave 2D spectra direction and frequency)\n");
+      if (isec1[36] == 1) fprintf(grprsm, " (Mars labelling or ensemble forecast)\n");
+      if (isec1[36] == 2) fprintf(grprsm, " (Cluster means and standard deviations)\n");
+      if (isec1[36] == 3) fprintf(grprsm, " (Satellite image data)\n");
+      if (isec1[36] == 4) fprintf(grprsm, " (Ocean model data)\n");
+      if (isec1[36] == 5) fprintf(grprsm, " (Forecast probability data)\n");
+      if (isec1[36] == 6) fprintf(grprsm, " (Surface temperature data)\n");
+      if (isec1[36] == 7) fprintf(grprsm, " (Sensitivity data)\n");
+      if (isec1[36] == 8) fprintf(grprsm, " (ECMWF re-analysis data)\n");
+      if (isec1[36] == 9) fprintf(grprsm, " (Singular vectors and ensemble perturbations)\n");
+      if (isec1[36] == 10) fprintf(grprsm, " (EPS tubes)\n");
+      if (isec1[36] == 11) fprintf(grprsm, " (Supplementary data used by analysis)\n");
+      if (isec1[36] == 13) fprintf(grprsm, " (Wave 2D spectra direction and frequency)\n");
 
       fprintf(grprsm, " Class.                               %9d\n", isec1[37]);
       fprintf(grprsm, " Type.                                %9d\n", isec1[38]);
       fprintf(grprsm, " Stream.                              %9d\n", isec1[39]);
-      sprintf(hversion, "%4s", (char*)&isec1[40]); hversion[4] = 0;
+      sprintf(hversion, "%4s", (char *) &isec1[40]);
+      hversion[4] = 0;
       fprintf(grprsm, " Version number or Experiment identifier.  %4s\n", hversion);
       /*
-	ECMWF Local definition 1.
-	(MARS labelling or ensemble forecast data)
+        ECMWF Local definition 1.
+        (MARS labelling or ensemble forecast data)
       */
-      if ( isec1[36] == 1 )
-	{
-	  fprintf(grprsm, " Forecast number.                     %9d\n", isec1[41]);
-	  if ( isec1[39] != 1090 )
-	    fprintf(grprsm, " Total number of forecasts.           %9d\n", isec1[42]);
+      if (isec1[36] == 1)
+        {
+          fprintf(grprsm, " Forecast number.                     %9d\n", isec1[41]);
+          if (isec1[39] != 1090) fprintf(grprsm, " Total number of forecasts.           %9d\n", isec1[42]);
 
-	  return;
-	}
+          return;
+        }
       /*
-	ECMWF Local definition 2.
-	(Cluster means and standard deviations)
+        ECMWF Local definition 2.
+        (Cluster means and standard deviations)
       */
-      if ( isec1[36] == 2 )
-	{
-	  fprintf(grprsm, " Cluster number.                      %9d\n", isec1[41]);
-	  fprintf(grprsm, " Total number of clusters.            %9d\n", isec1[42]);
-	  fprintf(grprsm, " Clustering method.                   %9d\n", isec1[43]);
-	  fprintf(grprsm, " Start time step when clustering.     %9d\n", isec1[44]);
-	  fprintf(grprsm, " End time step when clustering.       %9d\n", isec1[45]);
-	  fprintf(grprsm, " Northern latitude of domain.         %9d\n", isec1[46]);
-	  fprintf(grprsm, " Western longitude of domain.         %9d\n", isec1[47]);
-	  fprintf(grprsm, " Southern latitude of domain.         %9d\n", isec1[48]);
-	  fprintf(grprsm, " Eastern longitude of domain.         %9d\n", isec1[49]);
-	  fprintf(grprsm, " Operational forecast in cluster      %9d\n", isec1[50]);
-	  fprintf(grprsm, " Control forecast in cluster          %9d\n", isec1[51]);
-	  fprintf(grprsm, " Number of forecasts in cluster.      %9d\n", isec1[52]);
-
-	  for (int jloop = 0; jloop < isec1[52]; jloop++)
-	    fprintf(grprsm, " Forecast number                      %9d\n", isec1[jloop+53]);
+      if (isec1[36] == 2)
+        {
+          fprintf(grprsm, " Cluster number.                      %9d\n", isec1[41]);
+          fprintf(grprsm, " Total number of clusters.            %9d\n", isec1[42]);
+          fprintf(grprsm, " Clustering method.                   %9d\n", isec1[43]);
+          fprintf(grprsm, " Start time step when clustering.     %9d\n", isec1[44]);
+          fprintf(grprsm, " End time step when clustering.       %9d\n", isec1[45]);
+          fprintf(grprsm, " Northern latitude of domain.         %9d\n", isec1[46]);
+          fprintf(grprsm, " Western longitude of domain.         %9d\n", isec1[47]);
+          fprintf(grprsm, " Southern latitude of domain.         %9d\n", isec1[48]);
+          fprintf(grprsm, " Eastern longitude of domain.         %9d\n", isec1[49]);
+          fprintf(grprsm, " Operational forecast in cluster      %9d\n", isec1[50]);
+          fprintf(grprsm, " Control forecast in cluster          %9d\n", isec1[51]);
+          fprintf(grprsm, " Number of forecasts in cluster.      %9d\n", isec1[52]);
+
+          for (int jloop = 0; jloop < isec1[52]; jloop++)
+            fprintf(grprsm, " Forecast number                      %9d\n", isec1[jloop + 53]);
+
+          return;
+        }
+      /*
+        ECMWF Local definition 3.
+        (Satellite image data)
+      */
+      if (isec1[36] == 3)
+        {
+          fprintf(grprsm, " Satellite spectral band.             %9d\n", isec1[41]);
+          fprintf(grprsm, " Function code.                       %9d\n", isec1[42]);
+          return;
+        }
+      /*
+        ECMWF Local definition 4.
+        (Ocean model data)
+      */
+      if (isec1[36] == 4)
+        {
+          fprintf(grprsm, " Satellite spectral band.             %9d\n", isec1[41]);
+          if (isec1[39] != 1090) fprintf(grprsm, " Function code.                       %9d\n", isec1[42]);
+          fprintf(grprsm, " Coordinate structure definition.\n");
+          fprintf(grprsm, " Fundamental spatial reference system.%9d\n", isec1[43]);
+          fprintf(grprsm, " Fundamental time reference.          %9d\n", isec1[44]);
+          fprintf(grprsm, " Space unit flag.                     %9d\n", isec1[45]);
+          fprintf(grprsm, " Vertical coordinate definition.      %9d\n", isec1[46]);
+          fprintf(grprsm, " Horizontal coordinate definition.    %9d\n", isec1[47]);
+          fprintf(grprsm, " Time unit flag.                      %9d\n", isec1[48]);
+          fprintf(grprsm, " Time coordinate definition.          %9d\n", isec1[49]);
+          fprintf(grprsm, " Position definition.     \n");
+          fprintf(grprsm, " Mixed coordinate field flag.         %9d\n", isec1[50]);
+          fprintf(grprsm, " Coordinate 1 flag.                   %9d\n", isec1[51]);
+          fprintf(grprsm, " Averaging flag.                      %9d\n", isec1[52]);
+          fprintf(grprsm, " Position of level 1.                 %9d\n", isec1[53]);
+          fprintf(grprsm, " Position of level 2.                 %9d\n", isec1[54]);
+          fprintf(grprsm, " Coordinate 2 flag.                   %9d\n", isec1[55]);
+          fprintf(grprsm, " Averaging flag.                      %9d\n", isec1[56]);
+          fprintf(grprsm, " Position of level 1.                 %9d\n", isec1[57]);
+          fprintf(grprsm, " Position of level 2.                 %9d\n", isec1[58]);
+          fprintf(grprsm, " Grid Definition.\n");
+          fprintf(grprsm, " Coordinate 3 flag (x-axis)           %9d\n", isec1[59]);
+          fprintf(grprsm, " Coordinate 4 flag (y-axis)           %9d\n", isec1[60]);
+          fprintf(grprsm, " Coordinate 4 of first grid point.    %9d\n", isec1[61]);
+          fprintf(grprsm, " Coordinate 3 of first grid point.    %9d\n", isec1[62]);
+          fprintf(grprsm, " Coordinate 4 of last grid point.     %9d\n", isec1[63]);
+          fprintf(grprsm, " Coordinate 3 of last grid point.     %9d\n", isec1[64]);
+          fprintf(grprsm, " i - increment.                       %9d\n", isec1[65]);
+          fprintf(grprsm, " j - increment.                       %9d\n", isec1[66]);
+          fprintf(grprsm, " Flag for irregular grid coordinates. %9d\n", isec1[67]);
+          fprintf(grprsm, " Flag for normal or staggered grids.  %9d\n", isec1[68]);
+          fprintf(grprsm, " Further information.\n");
+          fprintf(grprsm, " Further information flag.            %9d\n", isec1[69]);
+          fprintf(grprsm, " Auxiliary information.\n");
+          fprintf(grprsm, " No. entries in horizontal coordinate %9d\n", isec1[70]);
+          fprintf(grprsm, " No. entries in mixed coordinate defn.%9d\n", isec1[71]);
+          fprintf(grprsm, " No. entries in grid coordinate list. %9d\n", isec1[72]);
+          fprintf(grprsm, " No. entries in auxiliary array.      %9d\n", isec1[73]);
+          /*
+            Horizontal coordinate supplement.
+          */
+          fprintf(grprsm, " Horizontal coordinate supplement.\n");
+          if (isec1[70] == 0)
+            {
+              fprintf(grprsm, "(None).\n");
+            }
+          else
+            {
+              fprintf(grprsm, "Number of items = %d\n", isec1[70]);
+              for (int jloop = 0; jloop < isec1[70]; jloop++) fprintf(grprsm, "         %12d\n", isec1[74 + jloop]);
+            }
+          /*
+            Mixed coordinate definition.
+          */
+          fprintf(grprsm, " Mixed coordinate definition.\n");
+          if (isec1[71] == 0)
+            {
+              fprintf(grprsm, "(None).\n");
+            }
+          else
+            {
+              fprintf(grprsm, "Number of items = %d\n", isec1[71]);
+              ioffset = 74 + isec1[70];
+              for (int jloop = 0; jloop < isec1[71]; jloop++) fprintf(grprsm, "         %12d\n", isec1[ioffset + jloop]);
+            }
+          /*
+            Grid coordinate list.
+          */
+          fprintf(grprsm, " Grid coordinate list. \n");
+          if (isec1[72] == 0)
+            {
+              fprintf(grprsm, "(None).\n");
+            }
+          else
+            {
+              fprintf(grprsm, "Number of items = %d\n", isec1[72]);
+              ioffset = 74 + isec1[70] + isec1[71];
+              for (int jloop = 0; jloop < isec1[72]; jloop++) fprintf(grprsm, "         %12d\n", isec1[ioffset + jloop]);
+            }
+          /*
+            Auxiliary array.
+          */
+          fprintf(grprsm, " Auxiliary array.      \n");
+          if (isec1[73] == 0)
+            {
+              fprintf(grprsm, "(None).\n");
+            }
+          else
+            {
+              fprintf(grprsm, "Number of items = %d\n", isec1[73]);
+              ioffset = 74 + isec1[70] + isec1[71] + isec1[72];
+              for (int jloop = 0; jloop < isec1[73]; jloop++) fprintf(grprsm, "         %12d\n", isec1[ioffset + jloop]);
+            }
+          /*
+            Post-auxiliary array.
+          */
+          fprintf(grprsm, " Post-auxiliary array. \n");
+          ioffset = 74 + isec1[70] + isec1[71] + isec1[72] + isec1[73];
+          if (isec1[ioffset] == 0)
+            {
+              fprintf(grprsm, "(None).\n");
+            }
+          else
+            {
+              fprintf(grprsm, "Number of items = %d\n", isec1[ioffset]);
+              for (int jloop = 1; jloop < isec1[ioffset]; jloop++) fprintf(grprsm, "         %12d\n", isec1[ioffset + jloop]);
+            }
 
-	  return;
-	}
+          return;
+        }
       /*
-	ECMWF Local definition 3.
-	(Satellite image data)
+        ECMWF Local definition 5.
+        (Forecast probability data)
       */
-      if ( isec1[36] == 3 )
-	{
-	  fprintf(grprsm, " Satellite spectral band.             %9d\n", isec1[41]);
-	  fprintf(grprsm, " Function code.                       %9d\n", isec1[42]);
-	  return;
-	}
+      if (isec1[36] == 5)
+        {
+          fprintf(grprsm, " Forecast probability number          %9d\n", isec1[41]);
+          fprintf(grprsm, " Total number of forecast probabilities %7d\n", isec1[42]);
+          fprintf(grprsm, " Threshold units decimal scale factor %9d\n", isec1[43]);
+          fprintf(grprsm, " Threshold indicator(1=lower,2=upper,3=both) %2d\n", isec1[44]);
+          if (isec1[44] != 2) fprintf(grprsm, " Lower threshold value                %9d\n", isec1[45]);
+          if (isec1[44] != 1) fprintf(grprsm, " Upper threshold value                %9d\n", isec1[46]);
+          return;
+        }
       /*
-	ECMWF Local definition 4.
-	(Ocean model data)
+        ECMWF Local definition 6.
+        (Surface temperature data)
       */
-      if ( isec1[36] == 4 )
-	{
-	  fprintf(grprsm, " Satellite spectral band.             %9d\n", isec1[41]);
-	  if ( isec1[39] != 1090 )
-	    fprintf(grprsm, " Function code.                       %9d\n", isec1[42]);
-	  fprintf(grprsm, " Coordinate structure definition.\n");
-	  fprintf(grprsm, " Fundamental spatial reference system.%9d\n", isec1[43]);
-	  fprintf(grprsm, " Fundamental time reference.          %9d\n", isec1[44]);
-	  fprintf(grprsm, " Space unit flag.                     %9d\n", isec1[45]);
-	  fprintf(grprsm, " Vertical coordinate definition.      %9d\n", isec1[46]);
-	  fprintf(grprsm, " Horizontal coordinate definition.    %9d\n", isec1[47]);
-	  fprintf(grprsm, " Time unit flag.                      %9d\n", isec1[48]);
-	  fprintf(grprsm, " Time coordinate definition.          %9d\n", isec1[49]);
-	  fprintf(grprsm, " Position definition.     \n");
-	  fprintf(grprsm, " Mixed coordinate field flag.         %9d\n", isec1[50]);
-	  fprintf(grprsm, " Coordinate 1 flag.                   %9d\n", isec1[51]);
-	  fprintf(grprsm, " Averaging flag.                      %9d\n", isec1[52]);
-	  fprintf(grprsm, " Position of level 1.                 %9d\n", isec1[53]);
-	  fprintf(grprsm, " Position of level 2.                 %9d\n", isec1[54]);
-	  fprintf(grprsm, " Coordinate 2 flag.                   %9d\n", isec1[55]);
-	  fprintf(grprsm, " Averaging flag.                      %9d\n", isec1[56]);
-	  fprintf(grprsm, " Position of level 1.                 %9d\n", isec1[57]);
-	  fprintf(grprsm, " Position of level 2.                 %9d\n", isec1[58]);
-	  fprintf(grprsm, " Grid Definition.\n");
-	  fprintf(grprsm, " Coordinate 3 flag (x-axis)           %9d\n", isec1[59]);
-	  fprintf(grprsm, " Coordinate 4 flag (y-axis)           %9d\n", isec1[60]);
-	  fprintf(grprsm, " Coordinate 4 of first grid point.    %9d\n", isec1[61]);
-	  fprintf(grprsm, " Coordinate 3 of first grid point.    %9d\n", isec1[62]);
-	  fprintf(grprsm, " Coordinate 4 of last grid point.     %9d\n", isec1[63]);
-	  fprintf(grprsm, " Coordinate 3 of last grid point.     %9d\n", isec1[64]);
-	  fprintf(grprsm, " i - increment.                       %9d\n", isec1[65]);
-	  fprintf(grprsm, " j - increment.                       %9d\n", isec1[66]);
-	  fprintf(grprsm, " Flag for irregular grid coordinates. %9d\n", isec1[67]);
-	  fprintf(grprsm, " Flag for normal or staggered grids.  %9d\n", isec1[68]);
-	  fprintf(grprsm, " Further information.\n");
-	  fprintf(grprsm, " Further information flag.            %9d\n", isec1[69]);
-	  fprintf(grprsm, " Auxiliary information.\n");
-	  fprintf(grprsm, " No. entries in horizontal coordinate %9d\n", isec1[70]);
-	  fprintf(grprsm, " No. entries in mixed coordinate defn.%9d\n", isec1[71]);
-	  fprintf(grprsm, " No. entries in grid coordinate list. %9d\n", isec1[72]);
-	  fprintf(grprsm, " No. entries in auxiliary array.      %9d\n", isec1[73]);
-	  /*
-	    Horizontal coordinate supplement.
-	  */
-	  fprintf(grprsm, " Horizontal coordinate supplement.\n");
-	  if ( isec1[70] == 0 )
-	    {
-	      fprintf(grprsm, "(None).\n");
-	    }
-	  else
-	    {
-	      fprintf(grprsm, "Number of items = %d\n", isec1[70]);
-	      for (int jloop = 0; jloop < isec1[70]; jloop++)
-		fprintf(grprsm, "         %12d\n", isec1[74+jloop]);
-	    }
-	  /*
-	    Mixed coordinate definition.
-	  */
-	  fprintf(grprsm, " Mixed coordinate definition.\n");
-	  if ( isec1[71] == 0 )
-	    {
-	      fprintf(grprsm, "(None).\n");
-	    }
-	  else
-	    {
-	      fprintf(grprsm, "Number of items = %d\n", isec1[71]);
-	      ioffset = 74 + isec1[70];
-	      for (int jloop = 0; jloop < isec1[71]; jloop++)
-		fprintf(grprsm, "         %12d\n", isec1[ioffset+jloop]);
-	    }
-	  /*
-	    Grid coordinate list.
-	  */
-	  fprintf(grprsm, " Grid coordinate list. \n");
-	  if ( isec1[72] == 0 )
-	    {
-	      fprintf(grprsm, "(None).\n");
-	    }
-	  else
-	    {
-	      fprintf(grprsm, "Number of items = %d\n", isec1[72]);
-	      ioffset = 74 + isec1[70] + isec1[71];
-	      for (int jloop = 0; jloop < isec1[72]; jloop++)
-		fprintf(grprsm, "         %12d\n", isec1[ioffset+jloop]);
-	    }
-	  /*
-	    Auxiliary array.
-	  */
-	  fprintf(grprsm, " Auxiliary array.      \n");
-	  if ( isec1[73] == 0 )
-	    {
-	      fprintf(grprsm, "(None).\n");
-	    }
-	  else
-	    {
-	      fprintf(grprsm, "Number of items = %d\n", isec1[73]);
-	      ioffset = 74 + isec1[70] + isec1[71] + isec1[72];
-	      for (int jloop = 0; jloop < isec1[73]; jloop++)
-		fprintf(grprsm, "         %12d\n", isec1[ioffset+jloop]);
-	    }
-	  /*
-	    Post-auxiliary array.
-	  */
-	  fprintf(grprsm, " Post-auxiliary array. \n");
-	  ioffset = 74 + isec1[70] + isec1[71] + isec1[72] + isec1[73];
-	  if ( isec1[ioffset] == 0 )
-	    {
-	      fprintf(grprsm, "(None).\n");
-	    }
-	  else
-	    {
-	      fprintf(grprsm, "Number of items = %d\n", isec1[ioffset]);
-	      for (int jloop = 1; jloop < isec1[ioffset]; jloop++)
-		fprintf(grprsm, "         %12d\n", isec1[ioffset+jloop]);
-	    }
-
-	  return;
-	}
-      /*
-	ECMWF Local definition 5.
-	(Forecast probability data)
-      */
-      if ( isec1[36] == 5 )
-	{
-	  fprintf(grprsm, " Forecast probability number          %9d\n", isec1[41]);
-	  fprintf(grprsm, " Total number of forecast probabilities %7d\n", isec1[42]);
-	  fprintf(grprsm, " Threshold units decimal scale factor %9d\n", isec1[43]);
-	  fprintf(grprsm, " Threshold indicator(1=lower,2=upper,3=both) %2d\n", isec1[44]);
-	  if ( isec1[44]  !=  2 )
-	    fprintf(grprsm, " Lower threshold value                %9d\n", isec1[45]);
-	  if ( isec1[44]  !=  1 )
-	    fprintf(grprsm, " Upper threshold value                %9d\n", isec1[46]);
-	  return;
-	}
-      /*
-	ECMWF Local definition 6.
-	(Surface temperature data)
-      */
-      if ( isec1[36] == 6 )
-	{
-	  iyear = isec1[43];
-	  if ( iyear > 100 )
-	    {
-	      if ( iyear < 19000000 ) iyear = iyear + 19000000;
-	      fprintf(grprsm, " Date of SST field used               %9d\n", iyear);
-	    }
-	  else
-	    fprintf(grprsm, "Date of SST field used               Not given\n");
-	}
-      if ( isec1[44] == 0 )
-	fprintf(grprsm, " Type of SST field (= climatology)    %9d\n", isec1[44]);
-      if ( isec1[44] == 1 )
-	fprintf(grprsm, " Type of SST field (= 1/1 degree)     %9d\n", isec1[44]);
-      if ( isec1[44] == 2 )
-	fprintf(grprsm, " Type of SST field (= 2/2 degree)     %9d\n", isec1[44]);
+      if (isec1[36] == 6)
+        {
+          iyear = isec1[43];
+          if (iyear > 100)
+            {
+              if (iyear < 19000000) iyear = iyear + 19000000;
+              fprintf(grprsm, " Date of SST field used               %9d\n", iyear);
+            }
+          else
+            fprintf(grprsm, "Date of SST field used               Not given\n");
+        }
+      if (isec1[44] == 0) fprintf(grprsm, " Type of SST field (= climatology)    %9d\n", isec1[44]);
+      if (isec1[44] == 1) fprintf(grprsm, " Type of SST field (= 1/1 degree)     %9d\n", isec1[44]);
+      if (isec1[44] == 2) fprintf(grprsm, " Type of SST field (= 2/2 degree)     %9d\n", isec1[44]);
 
       fprintf(grprsm, " Number of ICE fields used:           %9d\n", isec1[45]);
 
       for (int jloop = 1; jloop <= isec1[45]; jloop++)
-	{
-	  iyear = isec1[44+(jloop*2)];
-	  if ( iyear > 100 )
-	    {
-              if ( iyear < 19000000 ) iyear = iyear + 19000000;
-	      fprintf(grprsm, " Date of ICE field%3d                 %9d\n", jloop, iyear);
-	      fprintf(grprsm, " Satellite number (ICE field%3d)      %9d\n", jloop,
-		     isec1[45+(jloop*2)]);
-	    }
-	  else
-	    fprintf(grprsm, "Date of SST field used               Not given\n");
-	}
+        {
+          iyear = isec1[44 + (jloop * 2)];
+          if (iyear > 100)
+            {
+              if (iyear < 19000000) iyear = iyear + 19000000;
+              fprintf(grprsm, " Date of ICE field%3d                 %9d\n", jloop, iyear);
+              fprintf(grprsm, " Satellite number (ICE field%3d)      %9d\n", jloop, isec1[45 + (jloop * 2)]);
+            }
+          else
+            fprintf(grprsm, "Date of SST field used               Not given\n");
+        }
       /*
-	ECMWF Local definition 7.
-	(Sensitivity data)
+        ECMWF Local definition 7.
+        (Sensitivity data)
       */
-      if ( isec1[36] == 7 )
-	{
-	  if ( isec1[38]  ==  51 )
-	    fprintf(grprsm, " Forecast number                      %9d\n", isec1[41]);
-	  if ( isec1[38]  !=  51 )
-	    fprintf(grprsm, " Iteration number                     %9d\n", isec1[41]);
-	  if ( isec1[38]  !=  52 )
-	    fprintf(grprsm, " Total number of diagnostics          %9d\n", isec1[42]);
-	  if ( isec1[38]  ==  52 )
-	    fprintf(grprsm, " No.interations in diag. minimisation %9d\n", isec1[42]);
-	  fprintf(grprsm, " Domain(0=Global,1=Europe,2=N.Hem.,3=S.Hem.) %2d\n", isec1[43]);
-	  fprintf(grprsm, " Diagnostic number                    %9d\n", isec1[44]);
-	}
+      if (isec1[36] == 7)
+        {
+          if (isec1[38] == 51) fprintf(grprsm, " Forecast number                      %9d\n", isec1[41]);
+          if (isec1[38] != 51) fprintf(grprsm, " Iteration number                     %9d\n", isec1[41]);
+          if (isec1[38] != 52) fprintf(grprsm, " Total number of diagnostics          %9d\n", isec1[42]);
+          if (isec1[38] == 52) fprintf(grprsm, " No.interations in diag. minimisation %9d\n", isec1[42]);
+          fprintf(grprsm, " Domain(0=Global,1=Europe,2=N.Hem.,3=S.Hem.) %2d\n", isec1[43]);
+          fprintf(grprsm, " Diagnostic number                    %9d\n", isec1[44]);
+        }
       /*
-	ECMWF Local definition 8.
-	(ECMWF re-analysis data)
+        ECMWF Local definition 8.
+        (ECMWF re-analysis data)
       */
-      if ( isec1[36] == 8 )
-	{
-	  if ( (isec1[39] == 1043) ||
-	       (isec1[39] == 1070) ||
-	       (isec1[39] == 1071) )
-	    {
-	      fprintf(grprsm, " Interval between reference times     %9d\n", isec1[41]);
-	      for (int jloop = 43; jloop <= 54; jloop++)
-		{
-		  jiloop = jloop + 8;
-		  fprintf(grprsm, " ERA section 1 octet %2d.              %9d\n",
-			 jiloop, isec1[jloop-1]);
-		}
-	    }
-	  else
-	    {
-	      for (int jloop = 42; jloop <= 54; jloop++)
-		{
-		  jiloop = jloop + 8;
-		  fprintf(grprsm, " ERA section 1 octet %2d.              %9d\n",
-			 jiloop, isec1[jloop-1]);
-		}
-	    }
-	  return;
-	}
+      if (isec1[36] == 8)
+        {
+          if ((isec1[39] == 1043) || (isec1[39] == 1070) || (isec1[39] == 1071))
+            {
+              fprintf(grprsm, " Interval between reference times     %9d\n", isec1[41]);
+              for (int jloop = 43; jloop <= 54; jloop++)
+                {
+                  jiloop = jloop + 8;
+                  fprintf(grprsm, " ERA section 1 octet %2d.              %9d\n", jiloop, isec1[jloop - 1]);
+                }
+            }
+          else
+            {
+              for (int jloop = 42; jloop <= 54; jloop++)
+                {
+                  jiloop = jloop + 8;
+                  fprintf(grprsm, " ERA section 1 octet %2d.              %9d\n", jiloop, isec1[jloop - 1]);
+                }
+            }
+          return;
+        }
 
-      if ( isec1[38] > 4  && isec1[38] < 9 )
-	{
-	  fprintf(grprsm, " Simulation number.                   %9d\n", isec1[41]);
-	  fprintf(grprsm, " Total number of simulations.         %9d\n", isec1[42]);
-	}
+      if (isec1[38] > 4 && isec1[38] < 9)
+        {
+          fprintf(grprsm, " Simulation number.                   %9d\n", isec1[41]);
+          fprintf(grprsm, " Total number of simulations.         %9d\n", isec1[42]);
+        }
       /*
-	ECMWF Local definition 9.
-	(Singular vectors and ensemble perturbations)
+        ECMWF Local definition 9.
+        (Singular vectors and ensemble perturbations)
       */
-      if ( isec1[36] == 9 )
-	{
-	  if ( isec1[38] == 60 )
-	    fprintf(grprsm, " Perturbed ensemble forecast number   %9d\n", isec1[41]);
-	  if ( isec1[38] == 61 )
-	    fprintf(grprsm, " Initial state perturbation number    %9d\n", isec1[41]);
-	  if ( isec1[38] == 62 )
-	    fprintf(grprsm, " Singular vector number               %9d\n", isec1[41]);
-	  if ( isec1[38] == 62 )
-	    {
-	      fprintf(grprsm, " Number of iterations                 %9d\n", isec1[42]);
-	      fprintf(grprsm, " Number of singular vectors computed  %9d\n", isec1[43]);
-	      fprintf(grprsm, " Norm used at initial time            %9d\n", isec1[44]);
-	      fprintf(grprsm, " Norm used at final time              %9d\n", isec1[45]);
-	      fprintf(grprsm, " Multiplication factor                %9d\n", isec1[46]);
-    	      fprintf(grprsm, " Latitude of north-west corner        %9d\n", isec1[47]);
-    	      fprintf(grprsm, " Longitude of north-west corner       %9d\n", isec1[48]);
-	      fprintf(grprsm, " Latitude of south-east corner        %9d\n", isec1[49]);
-	      fprintf(grprsm, " Longitude of south-east corner       %9d\n", isec1[50]);
-	      fprintf(grprsm, " Accuracy                             %9d\n", isec1[51]);
-	      fprintf(grprsm, " Number of singular vectors evolved   %9d\n", isec1[52]);
-	      fprintf(grprsm, " Ritz number one                      %9d\n", isec1[53]);
-	      fprintf(grprsm, " Ritz number two                      %9d\n", isec1[54]);
-	    }
-	}
+      if (isec1[36] == 9)
+        {
+          if (isec1[38] == 60) fprintf(grprsm, " Perturbed ensemble forecast number   %9d\n", isec1[41]);
+          if (isec1[38] == 61) fprintf(grprsm, " Initial state perturbation number    %9d\n", isec1[41]);
+          if (isec1[38] == 62) fprintf(grprsm, " Singular vector number               %9d\n", isec1[41]);
+          if (isec1[38] == 62)
+            {
+              fprintf(grprsm, " Number of iterations                 %9d\n", isec1[42]);
+              fprintf(grprsm, " Number of singular vectors computed  %9d\n", isec1[43]);
+              fprintf(grprsm, " Norm used at initial time            %9d\n", isec1[44]);
+              fprintf(grprsm, " Norm used at final time              %9d\n", isec1[45]);
+              fprintf(grprsm, " Multiplication factor                %9d\n", isec1[46]);
+              fprintf(grprsm, " Latitude of north-west corner        %9d\n", isec1[47]);
+              fprintf(grprsm, " Longitude of north-west corner       %9d\n", isec1[48]);
+              fprintf(grprsm, " Latitude of south-east corner        %9d\n", isec1[49]);
+              fprintf(grprsm, " Longitude of south-east corner       %9d\n", isec1[50]);
+              fprintf(grprsm, " Accuracy                             %9d\n", isec1[51]);
+              fprintf(grprsm, " Number of singular vectors evolved   %9d\n", isec1[52]);
+              fprintf(grprsm, " Ritz number one                      %9d\n", isec1[53]);
+              fprintf(grprsm, " Ritz number two                      %9d\n", isec1[54]);
+            }
+        }
       /*
-	ECMWF Local definition 10.
-	(EPS tubes)
+        ECMWF Local definition 10.
+        (EPS tubes)
       */
-      if ( isec1[36] == 10 )
-	{
-	  fprintf(grprsm, " Tube number                          %9d\n", isec1[41]);
+      if (isec1[36] == 10)
+        {
+          fprintf(grprsm, " Tube number                          %9d\n", isec1[41]);
           fprintf(grprsm, " Total number of tubes                %9d\n", isec1[42]);
           fprintf(grprsm, " Central cluster definition           %9d\n", isec1[43]);
           fprintf(grprsm, " Parameter                            %9d\n", isec1[44]);
@@ -2825,44 +2798,43 @@ void gribPrintSec1(int *isec0, int *isec1)
           fprintf(grprsm, " Number of forecasts in the tube      %9d\n", isec1[57]);
 
           fprintf(grprsm, " List of ensemble forecast numbers:\n");
-          for (int jloop = 1; jloop <=  isec1[57]; jloop++)
-	    fprintf(grprsm, "    %9d\n", isec1[57+jloop]);
-	}
+          for (int jloop = 1; jloop <= isec1[57]; jloop++) fprintf(grprsm, "    %9d\n", isec1[57 + jloop]);
+        }
       /*
-	ECMWF Local definition 11.
-	(Supplementary data used by the analysis)
+        ECMWF Local definition 11.
+        (Supplementary data used by the analysis)
       */
-      if ( isec1[36] == 11 )
-	{
-	  fprintf(grprsm, " Details of analysis which used the supplementary data:\n");
-	  fprintf(grprsm, "   Class                              %9d\n", isec1[41]);
-	  fprintf(grprsm, "   Type                               %9d\n", isec1[42]);
-	  fprintf(grprsm, "   Stream                             %9d\n", isec1[43]);
-	  /*
-	  sprintf(hversion, "%8d", isec1[44]);
-	  fprintf(grprsm, "   Version number/experiment identifier:   %4s\n", &hversion[4]);
-	  */
-	  iyear = isec1[45];
-	  if ( iyear > 50 )
-	    iyear = iyear + 1900;
-	  else
-	    iyear = iyear + 2000;
-
-	  fprintf(grprsm, "   Year                               %9d\n", iyear);
-	  fprintf(grprsm, "   Month                              %9d\n", isec1[46]);
-	  fprintf(grprsm, "   Day                                %9d\n", isec1[47]);
-	  fprintf(grprsm, "   Hour                               %9d\n", isec1[48]);
-	  fprintf(grprsm, "   Minute                             %9d\n", isec1[49]);
-	  fprintf(grprsm, "   Century                            %9d\n", isec1[50]);
-	  fprintf(grprsm, "   Originating centre                 %9d\n", isec1[51]);
-	  fprintf(grprsm, "   Sub-centre                         %9d\n", isec1[52]);
-	}
+      if (isec1[36] == 11)
+        {
+          fprintf(grprsm, " Details of analysis which used the supplementary data:\n");
+          fprintf(grprsm, "   Class                              %9d\n", isec1[41]);
+          fprintf(grprsm, "   Type                               %9d\n", isec1[42]);
+          fprintf(grprsm, "   Stream                             %9d\n", isec1[43]);
+          /*
+          sprintf(hversion, "%8d", isec1[44]);
+          fprintf(grprsm, "   Version number/experiment identifier:   %4s\n", &hversion[4]);
+          */
+          iyear = isec1[45];
+          if (iyear > 50)
+            iyear = iyear + 1900;
+          else
+            iyear = iyear + 2000;
+
+          fprintf(grprsm, "   Year                               %9d\n", iyear);
+          fprintf(grprsm, "   Month                              %9d\n", isec1[46]);
+          fprintf(grprsm, "   Day                                %9d\n", isec1[47]);
+          fprintf(grprsm, "   Hour                               %9d\n", isec1[48]);
+          fprintf(grprsm, "   Minute                             %9d\n", isec1[49]);
+          fprintf(grprsm, "   Century                            %9d\n", isec1[50]);
+          fprintf(grprsm, "   Originating centre                 %9d\n", isec1[51]);
+          fprintf(grprsm, "   Sub-centre                         %9d\n", isec1[52]);
+        }
       /*
-	ECMWF Local definition 12.
+        ECMWF Local definition 12.
       */
-      if ( isec1[36] == 12 )
-	{
-	  fprintf(grprsm, " (Mean, average, etc)\n");
+      if (isec1[36] == 12)
+        {
+          fprintf(grprsm, " (Mean, average, etc)\n");
           fprintf(grprsm, " Start date of the period              %8d\n", isec1[41]);
           fprintf(grprsm, " Start time of the period                  %4.4d\n", isec1[42]);
           fprintf(grprsm, " Finish date of the period             %8d\n", isec1[43]);
@@ -2872,151 +2844,138 @@ void gribPrintSec1(int *isec0, int *isec1)
           fprintf(grprsm, " Code showing method                   %8d\n", isec1[47]);
           fprintf(grprsm, " Number of different time intervals used  %5d\n", isec1[48]);
           fprintf(grprsm, " List of different time intervals used:\n");
-          iprev  = isec1[49];
+          iprev = isec1[49];
           unsigned icount = 0;
           for (int jloop = 1; jloop <= isec1[48]; jloop++)
-	    {
-	      icurr = isec1[48+jloop];
-	      if ( icurr != iprev )
-		{
-		  if ( icount == 1 )
-		    fprintf(grprsm, "  - interval %5.4d used       once\n", iprev);
-		  if ( icount == 2 )
-		    fprintf(grprsm, "  - interval %5.4d used       twice\n", iprev);
-		  if ( icount > 2 )
-		    fprintf(grprsm, "  - interval %5.4d used %5u times\n",  iprev, icount);
-		  iprev  = icurr;
-		  icount = 1;
-		}
-	      else
-		icount = icount + 1;
-	    }
-	  if ( icount == 1 )
-	    fprintf(grprsm, "  - interval %5.4d used       once\n", iprev);
-	  if ( icount == 2 )
-	    fprintf(grprsm, "  - interval %5.4d used       twice\n", iprev);
-	  if ( icount > 2 )
-	    fprintf(grprsm, "  - interval %5.4d used %5u times\n",  iprev, icount);
-	}
+            {
+              icurr = isec1[48 + jloop];
+              if (icurr != iprev)
+                {
+                  if (icount == 1) fprintf(grprsm, "  - interval %5.4d used       once\n", iprev);
+                  if (icount == 2) fprintf(grprsm, "  - interval %5.4d used       twice\n", iprev);
+                  if (icount > 2) fprintf(grprsm, "  - interval %5.4d used %5u times\n", iprev, icount);
+                  iprev = icurr;
+                  icount = 1;
+                }
+              else
+                icount = icount + 1;
+            }
+          if (icount == 1) fprintf(grprsm, "  - interval %5.4d used       once\n", iprev);
+          if (icount == 2) fprintf(grprsm, "  - interval %5.4d used       twice\n", iprev);
+          if (icount > 2) fprintf(grprsm, "  - interval %5.4d used %5u times\n", iprev, icount);
+        }
       /*
-	ECMWF Local definition 13.
-	(Wave 2D spectra direction and frequency)
+        ECMWF Local definition 13.
+        (Wave 2D spectra direction and frequency)
       */
-      if ( isec1[36] == 13 )
-	{
+      if (isec1[36] == 13)
+        {
           fprintf(grprsm, " Direction number                     %9d\n", isec1[43]);
-	  fprintf(grprsm, " Frequency number                     %9d\n", isec1[44]);
-	  fprintf(grprsm, " Total number of directions           %9d\n", isec1[45]);
-	  fprintf(grprsm, " Total number of frequencies          %9d\n", isec1[46]);
-	  fprintf(grprsm, " Scale factor applied to directions   %9d\n", isec1[47]);
-	  fprintf(grprsm, " Scale factor applied to frequencies  %9d\n", isec1[48]);
-	  fprintf(grprsm, " List of directions:\n");
+          fprintf(grprsm, " Frequency number                     %9d\n", isec1[44]);
+          fprintf(grprsm, " Total number of directions           %9d\n", isec1[45]);
+          fprintf(grprsm, " Total number of frequencies          %9d\n", isec1[46]);
+          fprintf(grprsm, " Scale factor applied to directions   %9d\n", isec1[47]);
+          fprintf(grprsm, " Scale factor applied to frequencies  %9d\n", isec1[48]);
+          fprintf(grprsm, " List of directions:\n");
           for (int jloop = 1; jloop <= isec1[45]; jloop++)
             {
-	      value = (float)(isec1[48+jloop])/(float)(isec1[47]);
-	      if ( isec1[43] == jloop )
-		fprintf(grprsm, " %2.2d:%15.7f   <-- this field value\n",  jloop, value);
-	      else
-		fprintf(grprsm, "%2.2d:%15.7f\n",  jloop, value);
+              value = (float) (isec1[48 + jloop]) / (float) (isec1[47]);
+              if (isec1[43] == jloop)
+                fprintf(grprsm, " %2.2d:%15.7f   <-- this field value\n", jloop, value);
+              else
+                fprintf(grprsm, "%2.2d:%15.7f\n", jloop, value);
             }
-	  fprintf(grprsm, " List of frequencies:\n");
+          fprintf(grprsm, " List of frequencies:\n");
           for (int jloop = 1; jloop <= isec1[46]; jloop++)
-	    {
-	      value = (float)(isec1[48+isec1[45]+jloop])/(float)(isec1[48]);
-	      if ( isec1[44] == jloop )
-		fprintf(grprsm, " %2.2d:%15.7f   <-- this field value\n",  jloop, value);
-	      else
-		fprintf(grprsm, "%2.2d:%15.7f\n",  jloop, value);
-
-	      if ( isec1[49+isec1[45]+isec1[46]] != 0 )
-		{
-		  fprintf(grprsm, " System number (65535 = missing)      %9d\n",
-			 isec1[49+isec1[45]+isec1[46]]);
-		  fprintf(grprsm, " Method number (65535 = missing)      %9d\n",
-			 isec1[50+isec1[45]+isec1[46]]);
-		}
-	    }
-	  /*
-	    ECMWF Local definition 14.
-	    (Brightness temperature)
-	  */
-	  if ( isec1[36] == 14 )
-	    {
-	      fprintf(grprsm, " Channel number                       %9d\n", isec1[43]);
-	      fprintf(grprsm, " Scale factor applied to frequencies  %9d\n", isec1[44]);
-	      fprintf(grprsm, " Total number of frequencies          %9d\n", isec1[45]);
-	      fprintf(grprsm, " List of frequencies:\n");
+            {
+              value = (float) (isec1[48 + isec1[45] + jloop]) / (float) (isec1[48]);
+              if (isec1[44] == jloop)
+                fprintf(grprsm, " %2.2d:%15.7f   <-- this field value\n", jloop, value);
+              else
+                fprintf(grprsm, "%2.2d:%15.7f\n", jloop, value);
+
+              if (isec1[49 + isec1[45] + isec1[46]] != 0)
+                {
+                  fprintf(grprsm, " System number (65535 = missing)      %9d\n", isec1[49 + isec1[45] + isec1[46]]);
+                  fprintf(grprsm, " Method number (65535 = missing)      %9d\n", isec1[50 + isec1[45] + isec1[46]]);
+                }
+            }
+          /*
+            ECMWF Local definition 14.
+            (Brightness temperature)
+          */
+          if (isec1[36] == 14)
+            {
+              fprintf(grprsm, " Channel number                       %9d\n", isec1[43]);
+              fprintf(grprsm, " Scale factor applied to frequencies  %9d\n", isec1[44]);
+              fprintf(grprsm, " Total number of frequencies          %9d\n", isec1[45]);
+              fprintf(grprsm, " List of frequencies:\n");
               for (int jloop = 1; jloop <= isec1[45]; jloop++)
-		{
-		  value = (float)(isec1[45+jloop])/(float)(isec1[44]);
-		  if ( isec1[43] == jloop )
-		    fprintf(grprsm, " %3d:%15.9f   <-- this channel\n", jloop, value);
-		  else
-		    fprintf(grprsm, " %3d:%15.9f\n", jloop, value);
-		}
-	    }
-	  /*
-	    ECMWF Local definition 15.
-	    (Ocean ensemble seasonal forecast)
-	  */
-	  if ( isec1[36] == 15 )
-	    {
-	      fprintf(grprsm, " Ensemble member number               %9d\n", isec1[41]);
-	      fprintf(grprsm, " System number                        %9d\n", isec1[42]);
-	      fprintf(grprsm, " Method number                        %9d\n", isec1[43]);
-	    }
-	  /*
-	    ECMWF Local definition 16.
-	    (Seasonal forecast monthly mean atmosphere data)
-	  */
-        if ( isec1[36] == 16 )
-	  {
-	    fprintf(grprsm, " Ensemble member number               %9d\n", isec1[41]);
-	    fprintf(grprsm, " System number                        %9d\n", isec1[43]);
-	    fprintf(grprsm, " Method number                        %9d\n", isec1[44]);
-	    fprintf(grprsm, " Verifying month                      %9d\n", isec1[45]);
-	    fprintf(grprsm, " Averaging period                     %9d\n", isec1[46]);
-	  }
-	/*
-	  ECMWF Local definition 17.
-	  (Sst or sea-ice used by analysis)
-	*/
-        if ( isec1[36] == 17 )
-	  {
-	    iyear = isec1[43];
-	    if ( iyear > 100 )
-	      {
-		if ( iyear < 19000000 ) iyear = iyear + 19000000;
-		fprintf(grprsm, " Date of sst/ice field used           %9d\n", iyear);
-	      }
-	    else
-              fprintf(grprsm, " Date of sst/ice field used           Not given\n");
-      
-	    if ( isec1[44] == 0 )
-	      fprintf(grprsm, " Type of sst/ice field (= climatology)%9d\n", isec1[44]);
-	    if ( isec1[44] == 1 )
-	      fprintf(grprsm, " Type of sst/ice field (= 1/1 degree) %9d\n", isec1[44]);
-	    if ( isec1[44] == 2 )
-	      fprintf(grprsm, " Type of sst/ice field (= 2/2 degree) %9d\n", isec1[44]);
-
-	    fprintf(grprsm, " Number of ICE fields used:           %9d\n", isec1[45]);
-
-	    for (int jloop = 1; jloop < isec1[45]; jloop++)
-	      {
-		iyear = isec1[44+(jloop*2)];
-		if ( iyear > 100 )
-		  {
-		    if ( iyear < 19000000 ) iyear = iyear + 19000000;
-		    fprintf(grprsm, " Date of ICE field%3d                 %9d\n", jloop,
-			   iyear);
-		    fprintf(grprsm, " Satellite number (ICE field%3d)      %9d\n", jloop,
-			   isec1[45+(jloop*2)]);
-		  }
-		else
-		  fprintf(grprsm, "Date of sst/ice field used           Not given\n");
-	      } 
-	  }
-	}
+                {
+                  value = (float) (isec1[45 + jloop]) / (float) (isec1[44]);
+                  if (isec1[43] == jloop)
+                    fprintf(grprsm, " %3d:%15.9f   <-- this channel\n", jloop, value);
+                  else
+                    fprintf(grprsm, " %3d:%15.9f\n", jloop, value);
+                }
+            }
+          /*
+            ECMWF Local definition 15.
+            (Ocean ensemble seasonal forecast)
+          */
+          if (isec1[36] == 15)
+            {
+              fprintf(grprsm, " Ensemble member number               %9d\n", isec1[41]);
+              fprintf(grprsm, " System number                        %9d\n", isec1[42]);
+              fprintf(grprsm, " Method number                        %9d\n", isec1[43]);
+            }
+          /*
+            ECMWF Local definition 16.
+            (Seasonal forecast monthly mean atmosphere data)
+          */
+          if (isec1[36] == 16)
+            {
+              fprintf(grprsm, " Ensemble member number               %9d\n", isec1[41]);
+              fprintf(grprsm, " System number                        %9d\n", isec1[43]);
+              fprintf(grprsm, " Method number                        %9d\n", isec1[44]);
+              fprintf(grprsm, " Verifying month                      %9d\n", isec1[45]);
+              fprintf(grprsm, " Averaging period                     %9d\n", isec1[46]);
+            }
+          /*
+            ECMWF Local definition 17.
+            (Sst or sea-ice used by analysis)
+          */
+          if (isec1[36] == 17)
+            {
+              iyear = isec1[43];
+              if (iyear > 100)
+                {
+                  if (iyear < 19000000) iyear = iyear + 19000000;
+                  fprintf(grprsm, " Date of sst/ice field used           %9d\n", iyear);
+                }
+              else
+                fprintf(grprsm, " Date of sst/ice field used           Not given\n");
+
+              if (isec1[44] == 0) fprintf(grprsm, " Type of sst/ice field (= climatology)%9d\n", isec1[44]);
+              if (isec1[44] == 1) fprintf(grprsm, " Type of sst/ice field (= 1/1 degree) %9d\n", isec1[44]);
+              if (isec1[44] == 2) fprintf(grprsm, " Type of sst/ice field (= 2/2 degree) %9d\n", isec1[44]);
+
+              fprintf(grprsm, " Number of ICE fields used:           %9d\n", isec1[45]);
+
+              for (int jloop = 1; jloop < isec1[45]; jloop++)
+                {
+                  iyear = isec1[44 + (jloop * 2)];
+                  if (iyear > 100)
+                    {
+                      if (iyear < 19000000) iyear = iyear + 19000000;
+                      fprintf(grprsm, " Date of ICE field%3d                 %9d\n", jloop, iyear);
+                      fprintf(grprsm, " Satellite number (ICE field%3d)      %9d\n", jloop, isec1[45 + (jloop * 2)]);
+                    }
+                  else
+                    fprintf(grprsm, "Date of sst/ice field used           Not given\n");
+                }
+            }
+        }
     }
   /*
     -----------------------------------------------------------------
@@ -3027,7 +2986,7 @@ void gribPrintSec1(int *isec0, int *isec1)
     Washington EPS products (but not reformatted Washington EPS
     products.
   */
-  if ( (isec1[1] == 7 && isec1[23] == 1) && (! (ISEC1_SubCenterID == 98)) )
+  if ((isec1[1] == 7 && isec1[23] == 1) && (!(ISEC1_SubCenterID == 98)))
     {
       /*   CALL KWPRS1 (iSEC0,iSEC1)*/
     }
@@ -3036,7 +2995,7 @@ void gribPrintSec1(int *isec0, int *isec1)
     Section 4 . Print local MPIM information.
     -----------------------------------------------------------------
   */
-  if (isec1[ 1] == 252 && isec1[36] == 1)
+  if (isec1[1] == 252 && isec1[36] == 1)
     {
       fprintf(grprsm, " MPIM local usage identifier.         %9d\n", isec1[36]);
       fprintf(grprsm, " Type of ensemble forecast            %9d\n", isec1[37]);
@@ -3045,7 +3004,8 @@ void gribPrintSec1(int *isec0, int *isec1)
     }
 }
 
-static void printQuasi(int *isec2)
+static void
+printQuasi(int *isec2)
 {
   /*
 
@@ -3078,15 +3038,15 @@ static void printQuasi(int *isec2)
   // See if scanning is north->south or south->north
   fprintf(grprsm, "  Number of points along a parallel varies.\n");
 
-  int ntos = ( fmod((double) isec2[10], 128.) < 64 );
+  int ntos = (fmod((double) isec2[10], 128.) < 64);
 
-  if ( ntos )
+  if (ntos)
     fprintf(grprsm, "  Number of points.   Parallel. (North to South)\n");
   else
     fprintf(grprsm, "  Number of points.   Parallel. (South to North)\n");
 
   // Display number of points for each latitude
-  int latcnt  = isec2[2];
+  int latcnt = isec2[2];
   int nextlat = 0;
   memset(yout, ' ', (size_t) 11);
 
@@ -3096,31 +3056,32 @@ static void printQuasi(int *isec2)
       sprintf(yout, "%4d", nextlat);
 
       // Finished?
-      if ( nextlat > latcnt ) break;
-      if ( nextlat == latcnt )
-	{
-	  fprintf(grprsm, " %5d                %-12s\n", isec2[nextlat+21], yout);
-	  break;
-	}
+      if (nextlat > latcnt) break;
+      if (nextlat == latcnt)
+        {
+          fprintf(grprsm, " %5d                %-12s\n", isec2[nextlat + 21], yout);
+          break;
+        }
       // Look for neighbouring latitudes with same number of points
       unsigned nrepeat = 0;
 
     LABEL110:
       // If neighbouring latitudes have same number of points increase the repeat count.
-      if ( isec2[nextlat+21+1] == isec2[nextlat+21] )
-	{
+      if (isec2[nextlat + 21 + 1] == isec2[nextlat + 21])
+        {
           nrepeat = nrepeat + 1;
           nextlat = nextlat + 1;
-	  if ( nextlat < latcnt ) goto LABEL110;
-	}
+          if (nextlat < latcnt) goto LABEL110;
+        }
       // Display neighbouring latitudes with same number of points as 'nn to mm'.
-      if ( nrepeat >= 1 ) sprintf(yout+4, "to %5d", nextlat);
-      fprintf(grprsm, " %5d                %-12s\n", isec2[nextlat+21], yout);
+      if (nrepeat >= 1) sprintf(yout + 4, "to %5d", nextlat);
+      fprintf(grprsm, " %5d                %-12s\n", isec2[nextlat + 21], yout);
       memset(yout, ' ', (size_t) 11);
     }
 }
 
-void gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
+void
+gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
 {
   /*
 
@@ -3165,8 +3126,7 @@ void gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
     Section 2 . Print spherical harmonic data.
     -----------------------------------------------------------------
   */
-  if ( isec2[0] == 50 || isec2[0] == 60 || 
-       isec2[0] == 70 || isec2[0] == 80 )
+  if (isec2[0] == 50 || isec2[0] == 60 || isec2[0] == 70 || isec2[0] == 80)
     {
       fprintf(grprsm, " Data represent type = spectral     (Table 6) %9d\n", isec2[0]);
       fprintf(grprsm, " J - Pentagonal resolution parameter.         %9d\n", isec2[1]);
@@ -3174,8 +3134,7 @@ void gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
       fprintf(grprsm, " M - Pentagonal resolution parameter.         %9d\n", isec2[3]);
       fprintf(grprsm, " Representation type (Table 9)                %9d\n", isec2[4]);
       fprintf(grprsm, " Representation mode (Table 10).              %9d\n", isec2[5]);
-      for (int i = 7; i <= 11; ++i)
-        fprintf(grprsm, " Not used.                                    %9d\n", isec2[i-1]);
+      for (int i = 7; i <= 11; ++i) fprintf(grprsm, " Not used.                                    %9d\n", isec2[i - 1]);
       fprintf(grprsm, " Number of vertical coordinate parameters.    %9d\n", isec2[11]);
       goto LABEL800;
     }
@@ -3184,18 +3143,17 @@ void gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
     Section 3 . Print Gaussian grid data.
     -----------------------------------------------------------------
   */
-  if ( isec2[0] ==  4 || isec2[0] == 14 || 
-       isec2[0] == 24 || isec2[0] == 34 )
+  if (isec2[0] == 4 || isec2[0] == 14 || isec2[0] == 24 || isec2[0] == 34)
     {
       fprintf(grprsm, " (Southern latitudes and Western longitudes are negative.)\n");
       fprintf(grprsm, " Data represent type = gaussian     (Table 6) %9d\n", isec2[0]);
       /*
-	Quasi-regular grids introduced in Edition 1.
+        Quasi-regular grids introduced in Edition 1.
       */
-      if ( isec2[16] == 0 || iedit < 1 )
-	fprintf(grprsm, " Number of points along a parallel.           %9d\n", isec2[1]);
+      if (isec2[16] == 0 || iedit < 1)
+        fprintf(grprsm, " Number of points along a parallel.           %9d\n", isec2[1]);
       else
-      	printQuasi(isec2);
+        printQuasi(isec2);
 
       fprintf(grprsm, " Number of points along a meridian.           %9d\n", isec2[2]);
       fprintf(grprsm, " Latitude of first grid point.                %9d\n", isec2[3]);
@@ -3209,12 +3167,12 @@ void gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
       fprintf(grprsm, " Latitude of last grid point.                 %9d\n", isec2[6]);
       fprintf(grprsm, " Longitude of last grid point.                %9d\n", isec2[7]);
       /*
-	Print increment if given.
+        Print increment if given.
       */
-      if ( isec2[5] == 128 )
-	fprintf(grprsm, " i direction (East-West) increment.           %9d\n", isec2[8]);
+      if (isec2[5] == 128)
+        fprintf(grprsm, " i direction (East-West) increment.           %9d\n", isec2[8]);
       else
-	fprintf(grprsm, " i direction (East-West) increment            Not given\n");
+        fprintf(grprsm, " i direction (East-West) increment            Not given\n");
 
       fprintf(grprsm, " Number of parallels between pole and equator.%9d\n", isec2[9]);
 
@@ -3230,16 +3188,15 @@ void gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
     Section 4 . Print Latitude / longitude grid data.
     -----------------------------------------------------------------
   */
-  if ( isec2[0] ==  0 || isec2[0] == 10 || 
-       isec2[0] == 20 || isec2[0] == 30 )
+  if (isec2[0] == 0 || isec2[0] == 10 || isec2[0] == 20 || isec2[0] == 30)
     {
       fprintf(grprsm, " (Southern latitudes and Western longitudes are negative.)\n");
       fprintf(grprsm, " Data represent type = lat/long     (Table 6) %9d\n", isec2[0]);
       /*
-	Quasi-regular lat/long grids also possible.
+        Quasi-regular lat/long grids also possible.
       */
-      if ( isec2[16] == 0 )
-	fprintf(grprsm, " Number of points along a parallel.           %9d\n", isec2[1]);
+      if (isec2[16] == 0)
+        fprintf(grprsm, " Number of points along a parallel.           %9d\n", isec2[1]);
       else
         printQuasi(isec2);
 
@@ -3255,18 +3212,18 @@ void gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
       fprintf(grprsm, " Latitude of last grid point.                 %9d\n", isec2[6]);
       fprintf(grprsm, " Longitude of last grid point.                %9d\n", isec2[7]);
       /*
-	Print increment if given.
+        Print increment if given.
       */
-      if ( isec2[8] < 0 )
-	fprintf(grprsm, " i direction (East-West) increment            Not given\n");
+      if (isec2[8] < 0)
+        fprintf(grprsm, " i direction (East-West) increment            Not given\n");
       else
-	fprintf(grprsm, " i direction (East-West) increment.           %9d\n", isec2[8]);
+        fprintf(grprsm, " i direction (East-West) increment.           %9d\n", isec2[8]);
 
-      if ( isec2[9] < 0 )
-	fprintf(grprsm, " j direction (North-South) increment          Not given\n");
+      if (isec2[9] < 0)
+        fprintf(grprsm, " j direction (North-South) increment          Not given\n");
       else
-	fprintf(grprsm, " j direction (North-South) increment.         %9d\n", isec2[9]);
-    
+        fprintf(grprsm, " j direction (North-South) increment.         %9d\n", isec2[9]);
+
       ibit = 8;
       prtbin(isec2[10], ibit, &iout, &ierr);
 
@@ -3279,7 +3236,7 @@ void gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
     Section 5 . Print polar stereographic data.
     -----------------------------------------------------------------
   */
-  if ( isec2[0] == 5 )
+  if (isec2[0] == 5)
     {
       fprintf(grprsm, " (Southern latitudes and Western longitudes are negative.)\n");
       fprintf(grprsm, " Data represent type = polar stereo (Table 6) %9d\n", isec2[0]);
@@ -3306,7 +3263,7 @@ void gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
     Section 6 . Print Lambert conformal data.
     -----------------------------------------------------------------
   */
-  if ( isec2[0] == 3 )
+  if (isec2[0] == 3)
     {
       fprintf(grprsm, " (Southern latitudes and Western longitudes are negative.)\n");
       fprintf(grprsm, " Data represent type = Lambert      (Table 6) %9d\n", isec2[0]);
@@ -3337,7 +3294,7 @@ void gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
     Section 7 . Print space view perspective or orthographic data.
     -----------------------------------------------------------------
   */
-  if ( isec2[0] == 90 )
+  if (isec2[0] == 90)
     {
       fprintf(grprsm, " (Southern latitudes and Western longitudes are negative.)\n");
       fprintf(grprsm, " Data represent type = space/ortho  (Table 6) %9d\n", isec2[0]);
@@ -3345,7 +3302,7 @@ void gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
       fprintf(grprsm, " Number of points along Y axis.               %9d\n", isec2[2]);
       fprintf(grprsm, " Latitude of sub-satellite point.             %9d\n", isec2[3]);
       fprintf(grprsm, " Longitude of sub-satellite point.            %9d\n", isec2[4]);
-      //iresol = isec2[17] + isec2[18];
+      // iresol = isec2[17] + isec2[18];
       fprintf(grprsm, " Diameter of the earth in x direction.        %9d\n", isec2[6]);
       fprintf(grprsm, " Y coordinate of sub-satellite point.         %9d\n", isec2[9]);
       ibit = 8;
@@ -3368,14 +3325,14 @@ void gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
     {
       fprintf(grprsm, " Data represent type = ECMWF ocean  (Table 6) %9d\n", isec2[0]);
       if ( isec2[1] ==  32767 )
-	fprintf(grprsm, " Number of points along the first axis.       Not used\n");
+        fprintf(grprsm, " Number of points along the first axis.       Not used\n");
       else
-	fprintf(grprsm, " Number of points along the first axis.       %9d\n", isec2[1]);
+        fprintf(grprsm, " Number of points along the first axis.       %9d\n", isec2[1]);
 
       if ( isec2[2] ==  32767 )
-	fprintf(grprsm, " Number of points along the second axis.      Not used\n");
+        fprintf(grprsm, " Number of points along the second axis.      Not used\n");
       else
-	fprintf(grprsm, " Number of points along the second axis.      %9d\n", isec2[2]);
+        fprintf(grprsm, " Number of points along the second axis.      %9d\n", isec2[2]);
 
       ibit = 8;
       prtbin(isec2[10], ibit, &iout, &ierr);
@@ -3388,7 +3345,7 @@ void gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
     Section 7.6 . Print triangular data
     -----------------------------------------------------------------
   */
-  if ( isec2[0] == 192 /* && ISEC1_CenterID == 78 */ )
+  if (isec2[0] == 192 /* && ISEC1_CenterID == 78 */)
     {
       fprintf(grprsm, " Data represent type = triangular   (Table 6) %9d\n", isec2[0]);
       fprintf(grprsm, " Number of factor 2 in factorisation of Ni.   %9d\n", isec2[1]);
@@ -3419,29 +3376,25 @@ void gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
                 stretched grid information, if any.
     -----------------------------------------------------------------
   */
- LABEL800:;
+LABEL800:;
   /*
     Vertical coordinate parameters ...
   */
-  if ( isec2[11] != 0 )
+  if (isec2[11] != 0)
     {
       fprintf(grprsm, " \n");
       fprintf(grprsm, " Vertical Coordinate Parameters.\n");
       fprintf(grprsm, " -------------------------------\n");
-      for (int i = 10; i < isec2[11]+10; ++i)
-	fprintf(grprsm, "    %20.12f\n", fsec2[i]);
+      for (int i = 10; i < isec2[11] + 10; ++i) fprintf(grprsm, "    %20.12f\n", fsec2[i]);
     }
   /*
     Rotated and stretched grids introduced in Edition 1.
   */
-  if ( iedit < 1 ) goto LABEL900;
+  if (iedit < 1) goto LABEL900;
   /*
     Rotated grid information ...
   */
-  if ( isec2[0] == 10 || isec2[0] == 30 || 
-       isec2[0] == 14 || isec2[0] == 34 || 
-       isec2[0] == 60 || isec2[0] == 80 || 
-       isec2[0] == 30 )
+  if (isec2[0] == 10 || isec2[0] == 30 || isec2[0] == 14 || isec2[0] == 34 || isec2[0] == 60 || isec2[0] == 80 || isec2[0] == 30)
     {
       fprintf(grprsm, " \n");
       fprintf(grprsm, " Latitude of southern pole of rotation.       %9d\n", isec2[12]);
@@ -3451,9 +3404,7 @@ void gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
   /*
     Stretched grid information ...
   */
-  if ( isec2[0] == 20 || isec2[0] == 30 || 
-       isec2[0] == 24 || isec2[0] == 34 || 
-       isec2[0] == 70 || isec2[0] == 80 )
+  if (isec2[0] == 20 || isec2[0] == 30 || isec2[0] == 24 || isec2[0] == 34 || isec2[0] == 70 || isec2[0] == 80)
     {
       fprintf(grprsm, " \n");
       fprintf(grprsm, " Latitude of pole of stretching.              %9d\n", isec2[14]);
@@ -3461,26 +3412,28 @@ void gribPrintSec2DP(int *isec0, int *isec2, double *fsec2)
       fprintf(grprsm, " Stretching factor.                     %20.10f\n", fsec2[1]);
     }
 
- LABEL900:;
+LABEL900:;
 
   return;
 }
 
-void gribPrintSec2SP(int *isec0, int *isec2, float  *fsec2sp)
+void
+gribPrintSec2SP(int *isec0, int *isec2, float *fsec2sp)
 {
   int inum = 10 + isec2[11];
 
-  double *fsec2 = (double*) Malloc((size_t)inum*sizeof(double));
-  if ( fsec2 == NULL ) SysError("No Memory!");
+  double *fsec2 = (double *) Malloc((size_t) inum * sizeof(double));
+  if (fsec2 == NULL) SysError("No Memory!");
 
   for (int j = 0; j < inum; ++j) fsec2[j] = fsec2sp[j];
-  
+
   gribPrintSec2DP(isec0, isec2, fsec2);
 
   Free(fsec2);
 }
 
-void gribPrintSec3DP(int *isec0, int *isec3, double *fsec3)
+void
+gribPrintSec3DP(int *isec0, int *isec3, double *fsec3)
 {
   /*
 
@@ -3510,7 +3463,7 @@ void gribPrintSec3DP(int *isec0, int *isec3, double *fsec3)
   fprintf(grprsm, " Section 3 - Bit-map Section.\n");
   fprintf(grprsm, " -------------------------------------\n");
 
-  if ( isec3[0] != 0 )
+  if (isec3[0] != 0)
     fprintf(grprsm, " Predetermined bit-map number.                %9d\n", isec3[0]);
   else
     fprintf(grprsm, " No predetermined bit-map.\n");
@@ -3520,17 +3473,19 @@ void gribPrintSec3DP(int *isec0, int *isec3, double *fsec3)
   fprintf(grprsm, " Missing data value for real data. %20.6g\n", fsec3[1]);
 }
 
-void gribPrintSec3SP(int *isec0, int *isec3, float  *fsec3sp)
+void
+gribPrintSec3SP(int *isec0, int *isec3, float *fsec3sp)
 {
   double fsec3[2];
 
   fsec3[0] = fsec3sp[0];
   fsec3[1] = fsec3sp[1];
-  
+
   gribPrintSec3DP(isec0, isec3, fsec3);
 }
 
-void gribPrintSec4DP(int *isec0, int *isec4, double *fsec4)
+void
+gribPrintSec4DP(int *isec0, int *isec4, double *fsec4)
 {
   /*
 
@@ -3579,33 +3534,32 @@ void gribPrintSec4DP(int *isec0, int *isec4, double *fsec4)
   /*
     If complex packing ..
   */
-  if ( isec4[3] == 64 )
+  if (isec4[3] == 64)
     {
-      if ( isec4[2] == 128 )
-	{
-	  fprintf(grprsm, " Byte offset of start of packed data (N).     %9d\n", isec4[15]);
-	  fprintf(grprsm, " Power (P * 1000).                            %9d\n", isec4[16]);
-	  fprintf(grprsm, " Pentagonal resolution parameter J for subset.%9d\n", isec4[17]);
-	  fprintf(grprsm, " Pentagonal resolution parameter K for subset.%9d\n", isec4[18]);
-	  fprintf(grprsm, " Pentagonal resolution parameter M for subset.%9d\n", isec4[19]);
-	}
+      if (isec4[2] == 128)
+        {
+          fprintf(grprsm, " Byte offset of start of packed data (N).     %9d\n", isec4[15]);
+          fprintf(grprsm, " Power (P * 1000).                            %9d\n", isec4[16]);
+          fprintf(grprsm, " Pentagonal resolution parameter J for subset.%9d\n", isec4[17]);
+          fprintf(grprsm, " Pentagonal resolution parameter K for subset.%9d\n", isec4[18]);
+          fprintf(grprsm, " Pentagonal resolution parameter M for subset.%9d\n", isec4[19]);
+        }
       else
-	{
-	  fprintf(grprsm, " Bits number of 2nd order values    (none=>0).%9d\n", isec4[10]);
-	  fprintf(grprsm, " General extend. 2-order packing (0=no,8=yes).%9d\n", isec4[11]);
-	  fprintf(grprsm, " Boustrophedonic ordering        (0=no,4=yes).%9d\n", isec4[12]);
-	  fprintf(grprsm, " Spatial differencing order          (0=none).%9d\n", isec4[13]+isec4[14]);
+        {
+          fprintf(grprsm, " Bits number of 2nd order values    (none=>0).%9d\n", isec4[10]);
+          fprintf(grprsm, " General extend. 2-order packing (0=no,8=yes).%9d\n", isec4[11]);
+          fprintf(grprsm, " Boustrophedonic ordering        (0=no,4=yes).%9d\n", isec4[12]);
+          fprintf(grprsm, " Spatial differencing order          (0=none).%9d\n", isec4[13] + isec4[14]);
         }
     }
   /*
     Number of non-missing values
   */
-  if ( isec4[20] != 0 )
-    fprintf(grprsm, " Number of non-missing values                 %9d\n", isec4[20]);
+  if (isec4[20] != 0) fprintf(grprsm, " Number of non-missing values                 %9d\n", isec4[20]);
   /*
     Information on matrix of values , if present.
   */
-  if ( isec4[7] == 64 )
+  if (isec4[7] == 64)
     {
       fprintf(grprsm, " First dimension (rows) of each matrix.       %9d\n", isec4[49]);
       fprintf(grprsm, " Second dimension (columns) of each matrix.   %9d\n", isec4[50]);
@@ -3625,36 +3579,36 @@ void gribPrintSec4DP(int *isec0, int *isec4, double *fsec4)
   */
 
   inum = isec4[0];
-  if ( inum <  0 ) inum = - inum;
-  if ( inum > 20 ) inum = 20;
+  if (inum < 0) inum = -inum;
+  if (inum > 20) inum = 20;
   /*
     Print first inum values.
   */
   fprintf(grprsm, " \n");
   fprintf(grprsm, " First %4d data values.\n", inum);
 
-  if ( isec4[4] == 0 )
+  if (isec4[4] == 0)
     {
       /*
-	Print real values ...
+        Print real values ...
       */
       for (int j = 0; j < inum; ++j)
-	{
-	  if ( fabs(fsec4[j]) > 0 )
-	    {
-	      if ( fabs(fsec4[j]) >= 0.1 && fabs(fsec4[j]) <= 1.e8 )
-		fprintf(grprsm, " %#16.8G    \n", fsec4[j]);
-	      else
-		fprintf(grprsm, " %#20.8E\n", fsec4[j]);
-	    }
-	  else
-	    fprintf(grprsm, " %#16.0f    \n", fabs(fsec4[j]));
-	}
+        {
+          if (fabs(fsec4[j]) > 0)
+            {
+              if (fabs(fsec4[j]) >= 0.1 && fabs(fsec4[j]) <= 1.e8)
+                fprintf(grprsm, " %#16.8G    \n", fsec4[j]);
+              else
+                fprintf(grprsm, " %#20.8E\n", fsec4[j]);
+            }
+          else
+            fprintf(grprsm, " %#16.0f    \n", fabs(fsec4[j]));
+        }
     }
   else
     {
       /*
-	Print integer values ...
+        Print integer values ...
       */
       fprintf(grprsm, " Print of integer values not supported\n");
       /*
@@ -3670,20 +3624,22 @@ void gribPrintSec4DP(int *isec0, int *isec4, double *fsec4)
     }
 }
 
-void gribPrintSec4SP(int *isec0, int *isec4, float  *fsec4sp)
+void
+gribPrintSec4SP(int *isec0, int *isec4, float *fsec4sp)
 {
   double fsec4[20];
 
   int inum = isec4[0];
-  if ( inum <  0 ) inum = -inum;
-  if ( inum > 20 ) inum = 20;
+  if (inum < 0) inum = -inum;
+  if (inum > 20) inum = 20;
 
   for (int j = 0; j < inum; ++j) fsec4[j] = fsec4sp[j];
-  
+
   gribPrintSec4DP(isec0, isec4, fsec4);
 }
 
-void gribPrintSec4Wave(int *isec4)
+void
+gribPrintSec4Wave(int *isec4)
 {
   /*
 
@@ -3705,7 +3661,7 @@ void gribPrintSec4Wave(int *isec4)
        Uwe Schulzweida   MPIfM   01/04/2001
 
   */
-  int    ntemp[100];
+  int ntemp[100];
   float *ptemp;
 
   grsdef();
@@ -3736,191 +3692,198 @@ void gribPrintSec4Wave(int *isec4)
 #include <string.h>
 #include <ctype.h>
 
-
-
-int gribOpen(const char *filename, const char *mode)
+int
+gribOpen(const char *filename, const char *mode)
 {
   int fileID = fileOpen(filename, mode);
 
-#if defined (__sun)
-  if ( fileID != FILE_UNDEFID && tolower(*mode) == 'r' )
+#if defined(__sun)
+  if (fileID != FILE_UNDEFID && tolower(*mode) == 'r')
     {
       fileSetBufferType(fileID, FILE_BUFTYPE_MMAP);
     }
 #endif
 
-  return fileID;  
+  return fileID;
 }
 
-
-void gribClose(int fileID)
+void
+gribClose(int fileID)
 {
   fileClose(fileID);
 }
 
-
-off_t gribGetPos(int fileID)
+off_t
+gribGetPos(int fileID)
 {
   return fileGetPos(fileID);
 }
 
-
-int gribCheckSeek(int fileID, long *offset, int *version)
+int
+gribCheckSeek(int fileID, long *offset, int *version)
 {
   int ierr = gribFileSeek(fileID, offset);
 
   *version = -1;
-  if ( !ierr )
+  if (!ierr)
     {
       char buffer[4];
-     if ( fileRead(fileID, buffer, 4) == 4 )
-	*version = buffer[3];
+      if (fileRead(fileID, buffer, 4) == 4) *version = buffer[3];
     }
 
   return ierr;
 }
 
-
-int gribFileSeek(int fileID, long *offset)
+int
+gribFileSeek(int fileID, long *offset)
 {
   /* position file pointer after GRIB */
   const long GRIB = 0x47524942;
   long code = 0;
   int ch;
-  int retry = 4096*4096;
+  int retry = 4096 * 4096;
 
   *offset = 0;
 
   void *fileptr = filePtr(fileID);
 
-  while ( retry-- )
+  while (retry--)
     {
       ch = filePtrGetc(fileptr);
-      if ( ch == EOF ) return -1;
-    
-      code = ( (code << 8) + ch ) & 0xFFFFFFFF;
-      if ( code == GRIB )
-	{
-	  if ( CGRIBEX_Debug ) Message("record offset = %ld", *offset);
-	  return 0;
-	}
+      if (ch == EOF) return -1;
+
+      code = ((code << 8) + ch) & 0xFFFFFFFF;
+      if (code == GRIB)
+        {
+          if (CGRIBEX_Debug) Message("record offset = %ld", *offset);
+          return 0;
+        }
 
       (*offset)++;
     }
 
-  if ( CGRIBEX_Debug ) Message("record offset = %ld", *offset);
+  if (CGRIBEX_Debug) Message("record offset = %ld", *offset);
 
   return 1;
 }
 
-static inline
-unsigned read3ByteMSBFirst(void *fileptr)
+static inline unsigned
+read3ByteMSBFirst(void *fileptr)
 {
-  unsigned b1 = (unsigned)(filePtrGetc(fileptr));
-  unsigned b2 = (unsigned)(filePtrGetc(fileptr));
-  unsigned b3 = (unsigned)(filePtrGetc(fileptr));
+  unsigned b1 = (unsigned) (filePtrGetc(fileptr));
+  unsigned b2 = (unsigned) (filePtrGetc(fileptr));
+  unsigned b3 = (unsigned) (filePtrGetc(fileptr));
   return GET_UINT3(b1, b2, b3);
 }
 
-
-size_t gribReadSize(int fileID)
+size_t
+gribReadSize(int fileID)
 {
   size_t rgribsize = 0;
   void *fileptr = filePtr(fileID);
-  off_t pos = fileGetPos(fileID); 
+  off_t pos = fileGetPos(fileID);
 
   unsigned gribsize = read3ByteMSBFirst(fileptr);
 
   int gribversion = filePtrGetc(fileptr);
 
-  if ( gribsize == 24 && gribversion != 1 && gribversion != 2 ) gribversion = 0;
+  if (gribsize == 24 && gribversion != 1 && gribversion != 2) gribversion = 0;
 
-  if ( CGRIBEX_Debug ) Message("gribversion = %d", gribversion);
+  if (CGRIBEX_Debug) Message("gribversion = %d", gribversion);
 
-  if ( gribversion == 0 )
+  if (gribversion == 0)
     {
       unsigned gdssize = 0, bmssize = 0;
       unsigned issize = 4, essize = 4;
 
       unsigned pdssize = gribsize;
       fileSetPos(fileID, (off_t) 3, SEEK_CUR);
-      if ( CGRIBEX_Debug ) Message("pdssize     = %u", pdssize);
+      if (CGRIBEX_Debug) Message("pdssize     = %u", pdssize);
       int flag = filePtrGetc(fileptr);
-      if ( CGRIBEX_Debug ) Message("flag        = %d", flag);
-  
-      fileSetPos(fileID, (off_t) pdssize-8, SEEK_CUR);
+      if (CGRIBEX_Debug) Message("flag        = %d", flag);
 
-      if ( flag & 128 )
-	{
-	  gdssize = read3ByteMSBFirst(fileptr);
-	  fileSetPos(fileID, (off_t) gdssize-3, SEEK_CUR);
-	  if ( CGRIBEX_Debug ) Message("gdssize     = %u", gdssize);
-	}
+      fileSetPos(fileID, (off_t) pdssize - 8, SEEK_CUR);
 
-      if ( flag & 64 )
-	{
-	  bmssize = read3ByteMSBFirst(fileptr);
-	  fileSetPos(fileID, (off_t) bmssize-3, SEEK_CUR);
-	  if ( CGRIBEX_Debug ) Message("bmssize     = %u", bmssize);
-	}
+      if (flag & 128)
+        {
+          gdssize = read3ByteMSBFirst(fileptr);
+          fileSetPos(fileID, (off_t) gdssize - 3, SEEK_CUR);
+          if (CGRIBEX_Debug) Message("gdssize     = %u", gdssize);
+        }
+
+      if (flag & 64)
+        {
+          bmssize = read3ByteMSBFirst(fileptr);
+          fileSetPos(fileID, (off_t) bmssize - 3, SEEK_CUR);
+          if (CGRIBEX_Debug) Message("bmssize     = %u", bmssize);
+        }
 
       unsigned bdssize = read3ByteMSBFirst(fileptr);
-      if ( CGRIBEX_Debug ) Message("bdssize     = %u", bdssize);
+      if (CGRIBEX_Debug) Message("bdssize     = %u", bdssize);
 
       gribsize = issize + pdssize + gdssize + bmssize + bdssize + essize;
       rgribsize = (size_t) gribsize;
     }
-  else if ( gribversion == 1 )
+  else if (gribversion == 1)
     {
-      if ( gribsize > JP23SET ) // Large GRIB record
-	{
-	  unsigned pdssize = read3ByteMSBFirst(fileptr);
-	  if ( CGRIBEX_Debug ) Message("pdssize     = %u", pdssize);
+      if (gribsize > JP23SET)  // Large GRIB record
+        {
+          unsigned pdssize = read3ByteMSBFirst(fileptr);
+          if (CGRIBEX_Debug) Message("pdssize     = %u", pdssize);
 
-	  int flag = 0;
-	  for (int i = 0; i < 5; ++i) flag = filePtrGetc(fileptr);
-	  if ( CGRIBEX_Debug ) Message("flag        = %d", flag);
-  
-	  fileSetPos(fileID, (off_t) pdssize-8, SEEK_CUR);
+          int flag = 0;
+          for (int i = 0; i < 5; ++i) flag = filePtrGetc(fileptr);
+          if (CGRIBEX_Debug) Message("flag        = %d", flag);
+
+          fileSetPos(fileID, (off_t) pdssize - 8, SEEK_CUR);
 
           unsigned gdssize = 0;
-	  if ( flag & 128 )
-	    {
-	      gdssize = read3ByteMSBFirst(fileptr);
-	      fileSetPos(fileID, (off_t) gdssize-3, SEEK_CUR);
-	      if ( CGRIBEX_Debug ) Message("gdssize     = %u", gdssize);
-	    }
+          if (flag & 128)
+            {
+              gdssize = read3ByteMSBFirst(fileptr);
+              fileSetPos(fileID, (off_t) gdssize - 3, SEEK_CUR);
+              if (CGRIBEX_Debug) Message("gdssize     = %u", gdssize);
+            }
 
           unsigned bmssize = 0;
-	  if ( flag & 64 )
-	    {
-	      bmssize = read3ByteMSBFirst(fileptr);
-	      fileSetPos(fileID, (off_t) bmssize-3, SEEK_CUR);
-	      if ( CGRIBEX_Debug ) Message("bmssize     = %u", bmssize);
-	    }
+          if (flag & 64)
+            {
+              bmssize = read3ByteMSBFirst(fileptr);
+              fileSetPos(fileID, (off_t) bmssize - 3, SEEK_CUR);
+              if (CGRIBEX_Debug) Message("bmssize     = %u", bmssize);
+            }
 
-	  unsigned bdssize = read3ByteMSBFirst(fileptr);
-	  if ( CGRIBEX_Debug ) Message("bdssize     = %u", bdssize);
-          if ( bdssize <= 120 )
+          unsigned bdssize = read3ByteMSBFirst(fileptr);
+          if (CGRIBEX_Debug) Message("bdssize     = %u", bdssize);
+          if (bdssize <= 120)
             {
-              const int issize = 4;
+              enum
+              {
+                issize = 4
+              };
               gribsize &= JP23SET;
               gribsize *= 120;
-              bdssize = correct_bdslen(bdssize, gribsize, issize+pdssize+gdssize+bmssize);
-              if ( CGRIBEX_Debug ) Message("bdssize     = %u", bdssize);
+              bdssize = correct_bdslen(bdssize, gribsize, issize + pdssize + gdssize + bmssize);
+              if (CGRIBEX_Debug) Message("bdssize     = %u", bdssize);
 
               gribsize = issize + pdssize + gdssize + bmssize + bdssize + 4;
             }
-	}
+        }
       rgribsize = (size_t) gribsize;
     }
-  else if ( gribversion == 2 )
+  else if (gribversion == 2)
     {
       /* we set gribsize the following way because it doesn't matter then
-	 whether int is 4 or 8 bytes long - we don't have to care if the size
-	 really fits: if it does not, the record can not be read at all */
+         whether int is 4 or 8 bytes long - we don't have to care if the size
+         really fits: if it does not, the record can not be read at all */
       rgribsize = 0;
-      for (int i = 0; i < 8; ++i) rgribsize = (rgribsize << 8) | filePtrGetc(fileptr);
+      enum
+      {
+        g2size_bytes = 8
+      };
+      unsigned char g2size[g2size_bytes];
+      filePtrRead(fileptr, g2size, g2size_bytes);
+      for (int i = 0; i < g2size_bytes; ++i) rgribsize = (rgribsize << 8) | g2size[i];
     }
   else
     {
@@ -3928,59 +3891,69 @@ size_t gribReadSize(int fileID)
       Warning("GRIB version %d unsupported!", gribversion);
     }
 
-  if ( filePtrEOF(fileptr) ) rgribsize = 0;
+  if (filePtrEOF(fileptr)) rgribsize = 0;
 
-  if ( CGRIBEX_Debug ) Message("gribsize = %zu", rgribsize);
+  if (CGRIBEX_Debug) Message("gribsize = %zu", rgribsize);
 
   fileSetPos(fileID, pos, SEEK_SET);
 
   return rgribsize;
 }
 
-
-size_t gribGetSize(int fileID)
+size_t
+gribGetSize(int fileID)
 {
   long offset;
-  int ierr = gribFileSeek(fileID, &offset); // position file pointer after GRIB
-  if ( ierr > 0 )
+  int ierr = gribFileSeek(fileID, &offset);  // position file pointer after GRIB
+  if (ierr > 0)
     {
       Warning("GRIB record not found!");
       return 0;
     }
 
-  if      ( ierr == -1 ) return 0;
-  else if ( ierr ==  1 ) return 0;
+  if (ierr == -1)
+    return 0;
+  else if (ierr == 1)
+    return 0;
 
   size_t recSize = gribReadSize(fileID);
 
-  if ( CGRIBEX_Debug ) Message("recsize = %zu", recSize);
+  if (CGRIBEX_Debug) Message("recsize = %zu", recSize);
 
   fileSetPos(fileID, (off_t) -4, SEEK_CUR);
 
   return recSize;
 }
 
-
-int gribRead(int fileID, void *buffer, size_t *buffersize)
+int
+gribRead(int fileID, void *buffer, size_t *buffersize)
 {
   long offset;
-  int ierr = gribFileSeek(fileID, &offset); // position file pointer after GRIB
-  if ( ierr > 0 )
+  int ierr = gribFileSeek(fileID, &offset);  // position file pointer after GRIB
+  if (ierr > 0)
     {
       Warning("GRIB record not found!");
       return -2;
     }
 
-  if      ( ierr == -1 ) { *buffersize = 0; return -1; }
-  else if ( ierr ==  1 ) { *buffersize = 0; return -2; }
+  if (ierr == -1)
+    {
+      *buffersize = 0;
+      return -1;
+    }
+  else if (ierr == 1)
+    {
+      *buffersize = 0;
+      return -2;
+    }
 
-  size_t recSize  = gribReadSize(fileID);
+  size_t recSize = gribReadSize(fileID);
   size_t readSize = recSize;
 
-  if ( readSize > *buffersize )
+  if (readSize > *buffersize)
     {
       readSize = *buffersize;
-      ierr = -3;          // Tell the caller that the buffer was insufficient.
+      ierr = -3;  // Tell the caller that the buffer was insufficient.
     }
 
   *buffersize = recSize;  // Inform the caller about the record size.
@@ -3990,17 +3963,17 @@ int gribRead(int fileID, void *buffer, size_t *buffersize)
 
   readSize -= 4;
   // Read the rest of the record into the buffer.
-  size_t nread = fileRead(fileID, (char *)buffer + 4, readSize);
+  size_t nread = fileRead(fileID, (char *) buffer + 4, readSize);
 
-  if ( nread != readSize ) ierr = 1;
+  if (nread != readSize) ierr = 1;
 
   return ierr;
 }
 
-
-int gribWrite(int fileID, void *buffer, size_t buffersize)
+int
+gribWrite(int fileID, void *buffer, size_t buffersize)
 {
-  int nwrite = (int)(fileWrite(fileID, buffer, buffersize));
+  int nwrite = (int) (fileWrite(fileID, buffer, buffersize));
   if (nwrite != (int) buffersize)
     {
       perror(__func__);
@@ -4012,18 +3985,17 @@ int gribWrite(int fileID, void *buffer, size_t buffersize)
 #include <string.h>
 #include <ctype.h>
 
-
 FILE *grprsm = NULL;
 int CGRIBEX_grib_calendar = -1;
 
-
-void gribSetCalendar(int calendar)
+void
+gribSetCalendar(int calendar)
 {
   CGRIBEX_grib_calendar = calendar;
 }
 
-
-void grsdef(void)
+void
+grsdef(void)
 {
   /*
 C---->
@@ -4097,7 +4069,7 @@ C     ----------------------------------------------------------------
   static bool lfirst = true;
   extern int CGRIBEX_Const;
 
-  if ( ! lfirst ) return;
+  if (!lfirst) return;
 
   /*
     ----------------------------------------------------------------
@@ -4110,34 +4082,34 @@ C     ----------------------------------------------------------------
   /*
     Set GRIB calendar.
   */
-  if ( CGRIBEX_grib_calendar == -1 )
+  if (CGRIBEX_grib_calendar == -1)
     {
       CGRIBEX_grib_calendar = CALENDAR_PROLEPTIC;
-  
+
       envString = getenv("GRIB_CALENDAR");
-      if ( envString )
-	{
-	  if      ( strncmp(envString, "standard", 8) == 0 )
-	    CGRIBEX_grib_calendar = CALENDAR_STANDARD;
-	  else if ( strncmp(envString, "proleptic", 9) == 0 )
-	    CGRIBEX_grib_calendar = CALENDAR_PROLEPTIC;
-	  else if ( strncmp(envString, "360days", 7) == 0 )
-	    CGRIBEX_grib_calendar = CALENDAR_360DAYS;
-	  else if ( strncmp(envString, "365days", 7) == 0 )
-	    CGRIBEX_grib_calendar = CALENDAR_365DAYS;
-	  else if ( strncmp(envString, "366days", 7) == 0 )
-	    CGRIBEX_grib_calendar = CALENDAR_366DAYS;
-	  else if ( strncmp(envString, "none", 4) == 0 )
-	    CGRIBEX_grib_calendar = CALENDAR_NONE;
-	}
+      if (envString)
+        {
+          if (strncmp(envString, "standard", 8) == 0)
+            CGRIBEX_grib_calendar = CALENDAR_STANDARD;
+          else if (strncmp(envString, "proleptic", 9) == 0)
+            CGRIBEX_grib_calendar = CALENDAR_PROLEPTIC;
+          else if (strncmp(envString, "360days", 7) == 0)
+            CGRIBEX_grib_calendar = CALENDAR_360DAYS;
+          else if (strncmp(envString, "365days", 7) == 0)
+            CGRIBEX_grib_calendar = CALENDAR_365DAYS;
+          else if (strncmp(envString, "366days", 7) == 0)
+            CGRIBEX_grib_calendar = CALENDAR_366DAYS;
+          else if (strncmp(envString, "none", 4) == 0)
+            CGRIBEX_grib_calendar = CALENDAR_NONE;
+        }
     }
   /*
     Set GRIBEX compatibility mode.
   */
   envString = getenv("GRIB_GRIBEX_MODE_ON");
-  if ( envString != NULL )
+  if (envString != NULL)
     {
-      if ( atoi(envString) == 1 ) CGRIBEX_Const = 0;
+      if (atoi(envString) == 1) CGRIBEX_Const = 0;
     }
 
   /*
@@ -4145,36 +4117,34 @@ C     ----------------------------------------------------------------
   */
   grprsm = stdout;
   env_stream = getenv("GRPRS_STREAM");
-  if ( env_stream )
-    {
-      if ( isdigit((int) env_stream[0]) )
-	{
-	  int unit;
-	  unit = atoi(env_stream);
-	  if ( unit < 1 || unit > 99 )
-	    Warning("Invalid number for GRPRS_STREAM: %d", unit);
-	  else if ( unit == 2 )
-	    grprsm = stderr;
-	  else if ( unit == 6 )
-	    grprsm = stdout;
-	  else
-	    {
-	      char filename[] = "unit.00";
-	      sprintf(filename, "%2.2d", unit);
-	      grprsm = fopen(filename, "w");
-	      if ( ! grprsm )
-		SysError("GRPRS_STREAM = %d", unit);
-	    }
-	}
+  if (env_stream)
+    {
+      if (isdigit((int) env_stream[0]))
+        {
+          int unit;
+          unit = atoi(env_stream);
+          if (unit < 1 || unit > 99)
+            Warning("Invalid number for GRPRS_STREAM: %d", unit);
+          else if (unit == 2)
+            grprsm = stderr;
+          else if (unit == 6)
+            grprsm = stdout;
+          else
+            {
+              char filename[] = "unit.00";
+              sprintf(filename, "%2.2d", unit);
+              grprsm = fopen(filename, "w");
+              if (!grprsm) SysError("GRPRS_STREAM = %d", unit);
+            }
+        }
       else
-	{
-	  if ( env_stream[0] )
-	    {
-	      grprsm = fopen(env_stream, "w");
-	      if ( ! grprsm )
-		SysError("GRPRS_STREAM = %s", env_stream);
-	    }
-	}
+        {
+          if (env_stream[0])
+            {
+              grprsm = fopen(env_stream, "w");
+              if (!grprsm) SysError("GRPRS_STREAM = %s", env_stream);
+            }
+        }
     }
   /*
     Mark common area values set by user.
@@ -4182,6 +4152,8 @@ C     ----------------------------------------------------------------
   lfirst = false;
 }
 
+// clang-format off
+
 /* pack 8-bit bytes from 64-bit words to a packed buffer */
 /* same as : for (int i = 0; i < bc; ++i) cp[i] = (unsigned char) up[i]; */
 
@@ -4581,10 +4553,12 @@ long unpackInt32(const unsigned char *cp, unsigned INT32 *up, long bc, long tc)
   return (bc);
 }
 #endif
-#include <stdio.h>
 
+// clang-format on
+#include <stdio.h>
 
-void prtbin(int kin, int knbit, int *kout, int *kerr)
+void
+prtbin(int kin, int knbit, int *kout, int *kerr)
 {
   /*
 
@@ -4595,7 +4569,7 @@ void prtbin(int kin, int knbit, int *kout, int *kerr)
 
 
     Input Parameters:
-    
+
        kin   - Integer variable containing binary number.
 
        knbit - Number of bits in binary number.
@@ -4604,11 +4578,11 @@ void prtbin(int kin, int knbit, int *kout, int *kerr)
 
        kout  - Integer variable containing decimal value
                with ones and zeroes corresponding to those of
-	       the input binary number.
+               the input binary number.
 
        kerr  - 0, If no error.
                1, Number of bits in binary number exceeds
-	          maximum allowed or is less than 1.
+                  maximum allowed or is less than 1.
 
 
     Converted from EMOS routine PRTBIN.
@@ -4625,7 +4599,7 @@ void prtbin(int kin, int knbit, int *kout, int *kerr)
     generated will fit in the computer word - in this case will
     it fit in a Cray 48 bit integer?
   */
-  if ( knbit < 1 || knbit > 14 )
+  if (knbit < 1 || knbit > 14)
     {
       *kerr = 1;
       printf(" prtbin : Error in binary number length - %3d bits.\n", knbit);
@@ -4639,15 +4613,15 @@ void prtbin(int kin, int knbit, int *kout, int *kerr)
     -----------------------------------------------------------------
   */
   *kout = 0;
-  ik    = kin;
-  idec  = 1;
+  ik = kin;
+  idec = 1;
 
   for (int j = 0; j < knbit; ++j)
     {
-      itemp = ik - ( (ik/2)*2 );
+      itemp = ik - ((ik / 2) * 2);
       *kout = (*kout) + itemp * idec;
-      ik    = ik / 2;
-      idec  = idec * 10;
+      ik = ik / 2;
+      idec = idec * 10;
     }
 
   return;
@@ -4769,8 +4743,8 @@ ref2ibm(double *pref, int kbits)
 #include <math.h>
 #include <string.h>
 
-
-unsigned correct_bdslen(unsigned bdslen, long recsize, long gribpos)
+unsigned
+correct_bdslen(unsigned bdslen, long recsize, long gribpos)
 {
   /*
     If a very large product, the section 4 length field holds
@@ -4780,13 +4754,13 @@ unsigned correct_bdslen(unsigned bdslen, long recsize, long gribpos)
     due to the count being only 24 bits. It is only possible because
     the (default) rounding for GRIB products is 120 bytes.
   */
-  if ( recsize > JP23SET && bdslen <= 120 ) bdslen = (unsigned)(recsize - gribpos - bdslen);
+  if (recsize > JP23SET && bdslen <= 120) bdslen = (unsigned) (recsize - gribpos - bdslen);
   return bdslen;
 }
 
-
-int grib1Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **pdsp,
-		  unsigned char **gdsp, unsigned char **bmsp, unsigned char **bdsp, long *gribrecsize)
+int
+grib1Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **pdsp, unsigned char **gdsp, unsigned char **bmsp,
+              unsigned char **bdsp, long *gribrecsize)
 {
   *gribrecsize = 0;
   *pdsp = NULL;
@@ -4796,17 +4770,16 @@ int grib1Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **p
 
   unsigned char *section = gribbuffer;
   unsigned char *is = gribbuffer;
-  if ( ! GRIB_START(section) )
+  if (!GRIB_START(section))
     {
-      fprintf(stderr, "Wrong GRIB indicator section: found >%c%c%c%c<\n",
-	      section[0], section[1], section[2], section[3]);
+      fprintf(stderr, "Wrong GRIB indicator section: found >%c%c%c%c<\n", section[0], section[1], section[2], section[3]);
       return -1;
     }
 
   unsigned recsize = GET_UINT3(section[4], section[5], section[6]);
 
   int gribversion = GRIB_EDITION(section);
-  if ( gribversion != 0 && gribversion != 1 )
+  if (gribversion != 0 && gribversion != 1)
     {
       fprintf(stderr, "Error while decoding GRIB1 sections: GRIB edition %d records not supported!\n", gribversion);
       return -1;
@@ -4819,7 +4792,7 @@ int grib1Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **p
   unsigned gribsize = 4 + grib1offset + PDS_Len;
 
   unsigned char *gds = NULL;
-  if ( PDS_HAS_GDS )
+  if (PDS_HAS_GDS)
     {
       gds = bufpointer;
       bufpointer += GDS_Len;
@@ -4827,7 +4800,7 @@ int grib1Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **p
     }
 
   unsigned char *bms = NULL;
-  if ( PDS_HAS_BMS )
+  if (PDS_HAS_BMS)
     {
       bms = bufpointer;
       bufpointer += BMS_Len;
@@ -4836,7 +4809,7 @@ int grib1Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **p
 
   unsigned char *bds = bufpointer;
   unsigned bdslen = BDS_Len;
-  if ( recsize > JP23SET && bdslen <= 120 )
+  if (recsize > JP23SET && bdslen <= 120)
     {
       recsize &= JP23SET;
       recsize *= 120;
@@ -4852,26 +4825,24 @@ int grib1Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **p
   *bdsp = bds;
 
   *gribrecsize = gribsize;
-  if ( gribbufsize < gribsize )
+  if (gribbufsize < gribsize)
     {
       fprintf(stderr, "Inconsistent length of GRIB message (grib_buffer_size=%ld < grib_record_size=%u)!\n", gribbufsize, gribsize);
       return 1;
     }
 
-  if ( !GRIB_FIN(bufpointer) ) // end section - "7777" in ASCII
+  if (!GRIB_FIN(bufpointer))  // end section - "7777" in ASCII
     {
-      fprintf(stderr, "Missing GRIB end section: found >%c%c%c%c<\n",
-	      bufpointer[0], bufpointer[1], bufpointer[2], bufpointer[3]);
+      fprintf(stderr, "Missing GRIB end section: found >%c%c%c%c<\n", bufpointer[0], bufpointer[1], bufpointer[2], bufpointer[3]);
       return -2;
     }
 
   return 0;
 }
 
-
-int grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **idsp,
-		  unsigned char **lusp, unsigned char **gdsp, unsigned char **pdsp,
-		  unsigned char **drsp, unsigned char **bmsp, unsigned char **bdsp)
+int
+grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **idsp, unsigned char **lusp, unsigned char **gdsp,
+              unsigned char **pdsp, unsigned char **drsp, unsigned char **bmsp, unsigned char **bdsp)
 {
   UNUSED(gribbufsize);
 
@@ -4886,32 +4857,31 @@ int grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **i
   unsigned char *section = gribbuffer;
   unsigned sec_len = 16;
 
-  if ( !GRIB_START(section) )
+  if (!GRIB_START(section))
     {
-      fprintf(stderr, "wrong indicator section >%c%c%c%c<\n",
-	      section[0], section[1], section[2], section[3]);
+      fprintf(stderr, "wrong indicator section >%c%c%c%c<\n", section[0], section[1], section[2], section[3]);
       return -1;
     }
 
   int gribversion = GRIB_EDITION(section);
-  if ( gribversion != 2 )
+  if (gribversion != 2)
     {
       fprintf(stderr, "wrong GRIB version %d\n", gribversion);
-      return -1;      
+      return -1;
     }
 
   unsigned gribsize = 0;
-  for (int i = 0; i < 8; ++i) gribsize = (gribsize << 8) | section[8+i];
+  for (int i = 0; i < 8; ++i) gribsize = (gribsize << 8) | section[8 + i];
 
   unsigned grib_len = sec_len;
-  section  += sec_len;
+  section += sec_len;
 
   /* section 1 */
   sec_len = GRIB2_SECLEN(section);
   int sec_num = GRIB2_SECNUM(section);
-  //fprintf(stderr, "ids %d %ld\n", sec_num, sec_len);
+  // fprintf(stderr, "ids %d %ld\n", sec_num, sec_len);
 
-  if ( sec_num != 1 )
+  if (sec_num != 1)
     {
       fprintf(stderr, "Unexpected section1 number %d\n", sec_num);
       return -1;
@@ -4920,28 +4890,28 @@ int grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **i
   *idsp = section;
 
   grib_len += sec_len;
-  section  += sec_len;
+  section += sec_len;
 
   /* section 2 and 3 */
   sec_len = GRIB2_SECLEN(section);
   sec_num = GRIB2_SECNUM(section);
-  //fprintf(stderr, "lus %d %ld\n", sec_num, sec_len);
+  // fprintf(stderr, "lus %d %ld\n", sec_num, sec_len);
 
-  if ( sec_num == 2 )
+  if (sec_num == 2)
     {
       *lusp = section;
 
       grib_len += sec_len;
-      section  += sec_len;
+      section += sec_len;
 
       /* section 3 */
       sec_len = GRIB2_SECLEN(section);
-      //sec_num = GRIB2_SECNUM(section);
-      //fprintf(stderr, "gds %d %ld\n", sec_num, sec_len);
+      // sec_num = GRIB2_SECNUM(section);
+      // fprintf(stderr, "gds %d %ld\n", sec_num, sec_len);
 
       *gdsp = section;
     }
-  else if ( sec_num == 3 )
+  else if (sec_num == 3)
     {
       *gdsp = section;
     }
@@ -4952,14 +4922,14 @@ int grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **i
     }
 
   grib_len += sec_len;
-  section  += sec_len;
+  section += sec_len;
 
   /* section 4 */
   sec_len = GRIB2_SECLEN(section);
   sec_num = GRIB2_SECNUM(section);
-  //fprintf(stderr, "pds %d %ld\n", sec_num, sec_len);
+  // fprintf(stderr, "pds %d %ld\n", sec_num, sec_len);
 
-  if ( sec_num != 4 )
+  if (sec_num != 4)
     {
       fprintf(stderr, "Unexpected section4 number %d\n", sec_num);
       return -1;
@@ -4968,14 +4938,14 @@ int grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **i
   *pdsp = section;
 
   grib_len += sec_len;
-  section  += sec_len;
+  section += sec_len;
 
   /* section 5 */
   sec_len = GRIB2_SECLEN(section);
   sec_num = GRIB2_SECNUM(section);
-  //fprintf(stderr, "drs %d %ld\n", sec_num, sec_len);
+  // fprintf(stderr, "drs %d %ld\n", sec_num, sec_len);
 
-  if ( sec_num != 5 )
+  if (sec_num != 5)
     {
       fprintf(stderr, "Unexpected section5 number %d\n", sec_num);
       return -1;
@@ -4984,14 +4954,14 @@ int grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **i
   *drsp = section;
 
   grib_len += sec_len;
-  section  += sec_len;
+  section += sec_len;
 
   /* section 6 */
   sec_len = GRIB2_SECLEN(section);
   sec_num = GRIB2_SECNUM(section);
-  //fprintf(stderr, "bms %d %ld\n", sec_num, sec_len);
+  // fprintf(stderr, "bms %d %ld\n", sec_num, sec_len);
 
-  if ( sec_num != 6 )
+  if (sec_num != 6)
     {
       fprintf(stderr, "Unexpected section6 number %d\n", sec_num);
       return -1;
@@ -5000,14 +4970,14 @@ int grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **i
   *bmsp = section;
 
   grib_len += sec_len;
-  section  += sec_len;
+  section += sec_len;
 
   /* section 7 */
   sec_len = GRIB2_SECLEN(section);
   sec_num = GRIB2_SECNUM(section);
-  //fprintf(stderr, "bds %d %ld\n", sec_num, sec_len);
+  // fprintf(stderr, "bds %d %ld\n", sec_num, sec_len);
 
-  if ( sec_num != 7 )
+  if (sec_num != 7)
     {
       fprintf(stderr, "Unexpected section7 number %d\n", sec_num);
       return -1;
@@ -5016,55 +4986,51 @@ int grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **i
   *bdsp = section;
 
   grib_len += sec_len;
-  section  += sec_len;
+  section += sec_len;
 
   /* skip multi GRIB sections */
   int msec = 1;
-  while ( !GRIB_FIN(section) )
+  while (!GRIB_FIN(section))
     {
       sec_len = GRIB2_SECLEN(section);
       sec_num = GRIB2_SECNUM(section);
 
-      if ( sec_num < 1 || sec_num > 7 ) break;
+      if (sec_num < 1 || sec_num > 7) break;
 
-      if ( sec_num == 7 )
-	fprintf(stderr, "Skipped unsupported multi GRIB section %d!\n", ++msec);
+      if (sec_num == 7) fprintf(stderr, "Skipped unsupported multi GRIB section %d!\n", ++msec);
 
-      if ( (grib_len + sec_len) > gribsize ) break;
+      if ((grib_len + sec_len) > gribsize) break;
 
       grib_len += sec_len;
-      section  += sec_len;
+      section += sec_len;
     }
 
   /* end section - "7777" in ASCII */
-  if ( !GRIB_FIN(section) )
+  if (!GRIB_FIN(section))
     {
-      fprintf(stderr, "Missing end section >%2x %2x %2x %2x<\n",
-	      section[0], section[1], section[2], section[3]);
+      fprintf(stderr, "Missing end section >%2x %2x %2x %2x<\n", section[0], section[1], section[2], section[3]);
       return -2;
     }
 
   return 0;
 }
 
-
-int grib_info_for_grads(off_t recpos, long recsize, unsigned char *gribbuffer,
-			int *intnum, float *fltnum, off_t *bignum)
+int
+grib_info_for_grads(off_t recpos, long recsize, unsigned char *gribbuffer, int *intnum, float *fltnum, off_t *bignum)
 {
   long gribsize = 0;
   off_t bpos = 0;
 
   unsigned char *section = gribbuffer;
   unsigned char *is = gribbuffer;
-  if ( ! GRIB_START(section) )
+  if (!GRIB_START(section))
     {
-      fprintf(stderr, "wrong indicator section >%c%c%c%c<\n",
-	      section[0], section[1], section[2], section[3]);
+      fprintf(stderr, "wrong indicator section >%c%c%c%c<\n", section[0], section[1], section[2], section[3]);
       return -1;
     }
 
   int gribversion = GRIB_EDITION(section);
-  if ( recsize == 24 && gribversion == 0 ) gribversion = 0;
+  if (recsize == 24 && gribversion == 0) gribversion = 0;
 
   unsigned grib1offset = (gribversion == 1) ? 4 : 0;
 
@@ -5073,7 +5039,7 @@ int grib_info_for_grads(off_t recpos, long recsize, unsigned char *gribbuffer,
   gribsize += 4 + grib1offset + PDS_Len;
 
   unsigned char *gds = NULL;
-  if ( PDS_HAS_GDS )
+  if (PDS_HAS_GDS)
     {
       gds = bufpointer;
       bufpointer += GDS_Len;
@@ -5081,7 +5047,7 @@ int grib_info_for_grads(off_t recpos, long recsize, unsigned char *gribbuffer,
     }
 
   unsigned char *bms = NULL;
-  if ( PDS_HAS_BMS )
+  if (PDS_HAS_BMS)
     {
       bms = bufpointer;
       bufpointer += BMS_Len;
@@ -5094,26 +5060,25 @@ int grib_info_for_grads(off_t recpos, long recsize, unsigned char *gribbuffer,
   off_t dpos = recpos + gribsize + 11;
 
   unsigned bdslen = BDS_Len;
-  bdslen = correct_bdslen(bdslen, recsize, bds-gribbuffer);
+  bdslen = correct_bdslen(bdslen, recsize, bds - gribbuffer);
   bufpointer += bdslen;
   gribsize += bdslen;
   gribsize += 4;
 
-  if ( gribsize > recsize )
+  if (gribsize > recsize)
     {
       fprintf(stderr, "GRIB buffer size %ld too small! Min size = %ld\n", recsize, gribsize);
       return 1;
     }
 
   /* end section - "7777" in ascii */
-  if ( !GRIB_FIN(bufpointer) )
+  if (!GRIB_FIN(bufpointer))
     {
-      fprintf(stderr, "Missing end section >%2x %2x %2x %2x<\n",
-	      bufpointer[0], bufpointer[1], bufpointer[2], bufpointer[3]);
+      fprintf(stderr, "Missing end section >%2x %2x %2x %2x<\n", bufpointer[0], bufpointer[1], bufpointer[2], bufpointer[3]);
     }
 
   int bs = BDS_BinScale;
-  if ( bs > 32767 ) bs = 32768-bs;
+  if (bs > 32767) bs = 32768 - bs;
   float bsf = ldexpf(1.0f, bs);
 
   bignum[0] = dpos;
@@ -5121,9 +5086,9 @@ int grib_info_for_grads(off_t recpos, long recsize, unsigned char *gribbuffer,
   intnum[0] = BDS_NumBits;
 
   /*  fltnum[0] = 1.0; */
-  fltnum[0] = powf(10.0f, (float)PDS_DecimalScale);
+  fltnum[0] = powf(10.0f, (float) PDS_DecimalScale);
   fltnum[1] = bsf;
-  fltnum[2] = (float)BDS_RefValue;
+  fltnum[2] = (float) BDS_RefValue;
   /*
   printf("intnum %d %d %d\n", intnum[0], intnum[1], intnum[2]);
   printf("fltnum %g %g %g\n", fltnum[0], fltnum[1], fltnum[2]);
@@ -5131,42 +5096,39 @@ int grib_info_for_grads(off_t recpos, long recsize, unsigned char *gribbuffer,
   return 0;
 }
 
-static
-int get_level(unsigned char *pds)
+static int
+get_level(unsigned char *pds)
 {
   int level = 0;
 
-  if ( PDS_LevelType == 100 )
-    level = PDS_Level * 100;
-  else if ( PDS_LevelType == 99 )
-    level = PDS_Level;
-  else if ( PDS_LevelType == 109 )
-    level = PDS_Level;
+  if (PDS_LevelType == 100)
+    level = (int) (PDS_Level) *100;
+  else if (PDS_LevelType == 99 || PDS_LevelType == 109)
+    level = (int) (PDS_Level);
   else
     level = PDS_Level1;
 
   return level;
 }
 
-static
-double get_cr(unsigned char *w1, unsigned char *w2)
+static double
+get_cr(unsigned char *w1, unsigned char *w2)
 {
   unsigned s1 = GET_UINT3(w1[0], w1[1], w1[2]);
   unsigned s2 = GET_UINT3(w2[0], w2[1], w2[2]);
-  return ((double)s1)/s2;
+  return ((double) s1) / s2;
 }
 
-
-static void grib1PrintALL(int nrec, long offset, long recpos, long recsize, unsigned char *gribbuffer)
+static void
+grib1PrintALL(int nrec, long offset, long recpos, long recsize, unsigned char *gribbuffer)
 {
   static bool header = true;
   unsigned char *is = NULL, *pds = NULL, *gds = NULL, *bms = NULL, *bds = NULL;
 
-  if ( header )
+  if (header)
     {
-      fprintf(stdout, 
-      "  Rec : Off Position   Size : V PDS  GDS    BMS    BDS : Code Level :  LType GType: CR LL\n");
-/*     ----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+ */
+      fprintf(stdout, "  Rec : Off Position   Size : V PDS  GDS    BMS    BDS : Code Level :  LType GType: CR LL\n");
+      //               ----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+
       header = false;
     }
 
@@ -5176,13 +5138,13 @@ static void grib1PrintALL(int nrec, long offset, long recpos, long recsize, unsi
 
   long gribrecsize;
   int nerr = grib1Sections(gribbuffer, recsize, &pds, &gds, &bms, &bds, &gribrecsize);
-  if ( nerr < 0 )
+  if (nerr < 0)
     {
       fprintf(stdout, "%5d :%4ld %8ld %6ld : GRIB message error\n", nrec, offset, recpos, recsize);
       return;
     }
 
-  int GridType = (gds == NULL) ? -1 : (int)GDS_GridType;
+  int GridType = (gds == NULL) ? -1 : (int) GDS_GridType;
 
   int level = get_level(pds);
 
@@ -5190,62 +5152,61 @@ static void grib1PrintALL(int nrec, long offset, long recpos, long recsize, unsi
 
   bool llarge = (gribsize > JP23SET && bdslen <= 120);
 
-  bdslen = correct_bdslen(bdslen, recsize, bds-gribbuffer);
+  bdslen = correct_bdslen(bdslen, recsize, bds - gribbuffer);
 
-  double cr = (((BDS_Flag >> 4)&1) && (BDS_Z == 128 || BDS_Z == 130)) ? get_cr(&bds[14], &gribbuffer[4]) : 1;
+  double cr = (((BDS_Flag >> 4) & 1) && (BDS_Z == 128 || BDS_Z == 130)) ? get_cr(&bds[14], &gribbuffer[4]) : 1;
 
-  fprintf(stdout, "%5d :%4ld %8ld %6ld :%2d%4d%5d %6d %6d : %3d %6d : %5d %5d %6.4g  %c",
-	  nrec, offset, recpos, recsize, GRIB_EDITION(is),
-	  PDS_Len, GDS_Len, BMS_Len, bdslen,
-	  PDS_Parameter, level, PDS_LevelType, GridType, cr, llarge?'T':'F');
+  fprintf(stdout, "%5d :%4ld %8ld %6ld :%2d%4d%5d %6d %6d : %3d %6d : %5d %5d %6.4g  %c", nrec, offset, recpos, recsize,
+          GRIB_EDITION(is), PDS_Len, GDS_Len, BMS_Len, bdslen, PDS_Parameter, level, PDS_LevelType, GridType, cr,
+          llarge ? 'T' : 'F');
 
-  if ( nerr > 0 ) fprintf(stdout, " <-- GRIB data corrupted!");
+  if (nerr > 0) fprintf(stdout, " <-- GRIB data corrupted!");
   fprintf(stdout, "\n");
 }
 
-
-static void grib2PrintALL(int nrec, long offset, long recpos, long recsize, unsigned char *gribbuffer)
+static void
+grib2PrintALL(int nrec, long offset, long recpos, long recsize, unsigned char *gribbuffer)
 {
   static bool header = true;
-  unsigned char *is  = NULL, *pds = NULL, *gds = NULL, *bms = NULL, *bds = NULL;
+  unsigned char *is = NULL, *pds = NULL, *gds = NULL, *bms = NULL, *bds = NULL;
   unsigned char *ids = NULL, *lus = NULL, *drs = NULL;
   long ids_len = 0, lus_len = 0, gds_len = 0, pds_len = 0, drs_len = 0, bms_len = 0, bds_len = 0;
   double cr = 1;
 
-  if ( header )
+  if (header)
     {
-      fprintf(stdout, 
-      "  Rec : Off Position   Size : V IDS LUS GDS PDS  DRS    BMS    BDS : Parameter   Level :  LType GType: CR\n");
-/*     ----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+ */
+      fprintf(stdout,
+              "  Rec : Off Position   Size : V IDS LUS GDS PDS  DRS    BMS    BDS : Parameter   Level :  LType GType: CR\n");
+      //       ----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+
       header = false;
     }
 
   is = gribbuffer;
 
   int nerr = grib2Sections(gribbuffer, recsize, &ids, &lus, &gds, &pds, &drs, &bms, &bds);
-  if ( nerr )
+  if (nerr)
     {
       fprintf(stdout, "%5d :%4ld %8ld %6ld : error\n", nrec, offset, recpos, recsize);
       return;
     }
 
-  if ( ids ) ids_len = GRIB2_SECLEN(ids);
-  if ( lus ) lus_len = GRIB2_SECLEN(lus);
-  if ( gds ) gds_len = GRIB2_SECLEN(gds);
-  if ( pds ) pds_len = GRIB2_SECLEN(pds);
-  if ( drs ) drs_len = GRIB2_SECLEN(drs);
-  if ( bms ) bms_len = GRIB2_SECLEN(bms);
-  if ( bds ) bds_len = GRIB2_SECLEN(bds);
+  if (ids) ids_len = GRIB2_SECLEN(ids);
+  if (lus) lus_len = GRIB2_SECLEN(lus);
+  if (gds) gds_len = GRIB2_SECLEN(gds);
+  if (pds) pds_len = GRIB2_SECLEN(pds);
+  if (drs) drs_len = GRIB2_SECLEN(drs);
+  if (bms) bms_len = GRIB2_SECLEN(bms);
+  if (bds) bds_len = GRIB2_SECLEN(bds);
 
   // double cr = (((BDS_Flag >> 4)&1) && (BDS_Z == 128 || BDS_Z == 130)) ? get_cr(&bds[14], &gribbuffer[4]) : 1;
 
-  int dis        = GET_UINT1(is[6]);
-  int gridtype   = GET_UINT2(gds[12],gds[13]);
-  int paramcat   = GET_UINT1(pds[9]);
-  int paramnum   = GET_UINT1(pds[10]);
+  int dis = GET_UINT1(is[6]);
+  int gridtype = (int) (GET_UINT2(gds[12], gds[13]));
+  int paramcat = GET_UINT1(pds[9]);
+  int paramnum = GET_UINT1(pds[10]);
   int level1type = GET_UINT1(pds[22]);
   /* level1sf   = GET_UINT1(pds[23]); */
-  int level1     = GET_UINT4(pds[24],pds[25],pds[26],pds[27]);
+  int level1 = (int) (GET_UINT4(pds[24], pds[25], pds[26], pds[27]));
   /* level2type = GET_UINT1(pds[28]); */
   /* level2sf   = GET_UINT1(pds[29]); */
   /* level2     = GET_UINT4(pds[30],pds[31],pds[32],pds[33]); */
@@ -5254,30 +5215,28 @@ static void grib2PrintALL(int nrec, long offset, long recpos, long recsize, unsi
   */
   char paramstr[16];
   sprintf(paramstr, "%d.%d.%d", paramnum, paramcat, dis);
-  fprintf(stdout, "%5d :%4ld %8ld %6ld :%2d %3ld %3ld %3ld %3ld %4ld %6ld %6ld : %-9s %7d : %5d %5d %6.4g\n",
-	  nrec, offset, recpos, recsize, GRIB_EDITION(is),
-	  ids_len, lus_len, gds_len, pds_len, drs_len, bms_len, bds_len,
-	  paramstr, level1, level1type, gridtype, cr);
+  fprintf(stdout, "%5d :%4ld %8ld %6ld :%2d %3ld %3ld %3ld %3ld %4ld %6ld %6ld : %-9s %7d : %5d %5d %6.4g\n", nrec, offset, recpos,
+          recsize, GRIB_EDITION(is), ids_len, lus_len, gds_len, pds_len, drs_len, bms_len, bds_len, paramstr, level1, level1type,
+          gridtype, cr);
 }
 
-
-void gribPrintALL(int nrec, long offset, long recpos, long recsize, unsigned char *gribbuffer)
+void
+gribPrintALL(int nrec, long offset, long recpos, long recsize, unsigned char *gribbuffer)
 {
-  int gribversion = gribVersion(gribbuffer, (size_t)recsize);
+  int gribversion = gribVersion(gribbuffer, (size_t) recsize);
 
-  if ( gribversion == 0 || gribversion == 1 )
+  if (gribversion == 0 || gribversion == 1)
     grib1PrintALL(nrec, offset, recpos, recsize, gribbuffer);
-  else if ( gribversion == 2 )
+  else if (gribversion == 2)
     grib2PrintALL(nrec, offset, recpos, recsize, gribbuffer);
   else
     {
-      fprintf(stdout, "%5d :%4ld%9ld%7ld : GRIB version %d unsupported\n",
-	      nrec, offset, recpos, recsize, gribversion); 
+      fprintf(stdout, "%5d :%4ld%9ld%7ld : GRIB version %d unsupported\n", nrec, offset, recpos, recsize, gribversion);
     }
 }
 
-
-static void grib1PrintPDS(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
+static void
+grib1PrintPDS(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
 {
   static int header = 1;
   unsigned char *is = NULL, *pds = NULL, *gds = NULL, *bms = NULL, *bds = NULL;
@@ -5287,11 +5246,10 @@ static void grib1PrintPDS(int nrec, long recpos, long recsize, unsigned char *gr
 
   UNUSED(recpos);
 
-  if ( header )
+  if (header)
     {
-      fprintf(stdout, 
-      "  Rec : PDS Tab Cen Sub Ver Grid Code LTyp Level1 Level2    Date  Time P1 P2 TU TR NAVE Scale FCnum CT\n");
-/*     ----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+ */
+      fprintf(stdout, "  Rec : PDS Tab Cen Sub Ver Grid Code LTyp Level1 Level2    Date  Time P1 P2 TU TR NAVE Scale FCnum CT\n");
+      //               ----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+
       header = 0;
     }
 
@@ -5299,94 +5257,85 @@ static void grib1PrintPDS(int nrec, long recpos, long recsize, unsigned char *gr
 
   long gribrecsize;
   int nerr = grib1Sections(gribbuffer, recsize, &pds, &gds, &bms, &bds, &gribrecsize);
-  if ( nerr < 0 )
+  if (nerr < 0)
     {
       fprintf(stdout, "%5d : GRIB message error\n", nrec);
       return;
     }
 
-  switch(GRIB_EDITION(is))
-    {   
+  switch (GRIB_EDITION(is))
+    {
     case 0:
-      year                = GET_UINT1(pds[12]);
-      century             = 1;
-      subcenter           = 0;
-      decimalscale        = 0;
+      year = GET_UINT1(pds[12]);
+      century = 1;
+      subcenter = 0;
+      decimalscale = 0;
       break;
     case 1:
-      year                = PDS_Year;
-      century             = PDS_Century;
-      subcenter           = PDS_Subcenter;
-      decimalscale        = PDS_DecimalScale;
+      year = PDS_Year;
+      century = PDS_Century;
+      subcenter = PDS_Subcenter;
+      decimalscale = PDS_DecimalScale;
       break;
-    default:
-      fprintf(stderr, "Grib version %d not supported!", GRIB_EDITION(is));
-      exit(EXIT_FAILURE);
+    default: fprintf(stderr, "Grib version %d not supported!", GRIB_EDITION(is)); exit(EXIT_FAILURE);
     }
 
-  if ( PDS_Len > 28 )
-    if ( PDS_CenterID    == 98 || PDS_Subcenter == 98 ||
-	(PDS_CenterID    ==  7 && PDS_Subcenter == 98) )
-      if ( pds[40] == 1 )
-	fc_num = GET_UINT1(pds[49]);
+  if (PDS_Len > 28)
+    if (PDS_CenterID == 98 || PDS_Subcenter == 98 || (PDS_CenterID == 7 && PDS_Subcenter == 98))
+      if (pds[40] == 1) fc_num = GET_UINT1(pds[49]);
 
-  if ( year < 0 )
+  if (year < 0)
     {
-      date = (-year)*10000+PDS_Month*100+PDS_Day;
+      date = (-year) * 10000 + (int) PDS_Month * 100 + (int) PDS_Day;
       century = -century;
     }
   else
     {
-      date =    year*10000+PDS_Month*100+PDS_Day;
+      date = year * 10000 + (int) PDS_Month * 100 + (int) PDS_Day;
     }
-      
-  fprintf(stdout, "%5d :%4d%4d%4d%4d%4d %4d %4d%4d%7d%7d %8d%6d%3d%3d%3d%3d%5d%6d%5d%4d", nrec,
-	  PDS_Len,  PDS_CodeTable,   PDS_CenterID, subcenter, PDS_ModelID,
-	  PDS_GridDefinition, PDS_Parameter, PDS_LevelType, PDS_Level1, PDS_Level2,
-	  date, PDS_Time, PDS_TimePeriod1, PDS_TimePeriod2, PDS_TimeUnit, PDS_TimeRange,
-	  PDS_AvgNum, decimalscale, fc_num, century);
 
-  if ( nerr > 0 ) fprintf(stdout, " <-- GRIB data corrupted!");
+  fprintf(stdout, "%5d :%4d%4d%4d%4d%4d %4d %4d%4d%7d%7d %8d%6d%3d%3d%3d%3d%5d%6d%5d%4d", nrec, PDS_Len, PDS_CodeTable,
+          PDS_CenterID, subcenter, PDS_ModelID, PDS_GridDefinition, PDS_Parameter, PDS_LevelType, PDS_Level1, PDS_Level2, date,
+          PDS_Time, PDS_TimePeriod1, PDS_TimePeriod2, PDS_TimeUnit, PDS_TimeRange, PDS_AvgNum, decimalscale, fc_num, century);
+
+  if (nerr > 0) fprintf(stdout, " <-- GRIB data corrupted!");
   fprintf(stdout, "\n");
 }
 
-
-void gribPrintPDS(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
+void
+gribPrintPDS(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
 {
-  int gribversion = gribVersion(gribbuffer, (size_t)recsize);
+  int gribversion = gribVersion(gribbuffer, (size_t) recsize);
 
-  if ( gribversion == 0 || gribversion == 1 )
-    grib1PrintPDS(nrec, recpos, recsize, gribbuffer);
+  if (gribversion == 0 || gribversion == 1) grib1PrintPDS(nrec, recpos, recsize, gribbuffer);
   /*
   else if ( gribversion == 2 )
     grib2PrintPDS(nrec, recpos, recsize, gribbuffer);
   */
   else
     {
-      fprintf(stdout, "%5d :%4ld%9ld%7ld : GRIB version %d unsupported\n",
-	      nrec, 0L, recpos, recsize, gribversion); 
+      fprintf(stdout, "%5d :%4ld%9ld%7ld : GRIB version %d unsupported\n", nrec, 0L, recpos, recsize, gribversion);
     }
 }
 
-
-static void grib1PrintGDS(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
+static void
+grib1PrintGDS(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
 {
   static int header = 1;
   unsigned char *pds = NULL, *gds = NULL, *bms = NULL, *bds = NULL;
 
   UNUSED(recpos);
 
-  if ( header )
+  if (header)
     {
-      fprintf(stdout, 
-      "  Rec : GDS  NV PVPL Typ : xsize ysize   Lat1   Lon1   Lat2   Lon2    dx    dy\n");
-/*     ----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+ */
+      fprintf(stdout, "  Rec : GDS  NV PVPL Typ : xsize ysize   Lat1   Lon1   Lat2   Lon2    dx    dy\n");
+      //               ----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+
       header = 0;
     }
 
   long gribrecsize;
   int nerr = grib1Sections(gribbuffer, recsize, &pds, &gds, &bms, &bds, &gribrecsize);
-  if ( nerr < 0 )
+  if (nerr < 0)
     {
       fprintf(stdout, "%5d : GRIB message error\n", nrec);
       return;
@@ -5394,57 +5343,50 @@ static void grib1PrintGDS(int nrec, long recpos, long recsize, unsigned char *gr
 
   fprintf(stdout, "%5d :", nrec);
 
-  if ( gds )
-    fprintf(stdout, "%4d%4d%4d %4d :%6d%6d%7d%7d%7d%7d%6d%6d",
-	    GDS_Len,  GDS_NV,   GDS_PVPL, GDS_GridType,
-	    GDS_NumLon,   GDS_NumLat,
-	    GDS_FirstLat, GDS_FirstLon,
-	    GDS_LastLat,  GDS_LastLon,
-	    GDS_LonIncr,  GDS_LatIncr);
+  if (gds)
+    fprintf(stdout, "%4d%4d%4d %4d :%6d%6d%7d%7d%7d%7d%6d%6d", GDS_Len, GDS_NV, GDS_PVPL, GDS_GridType, GDS_NumLon, GDS_NumLat,
+            GDS_FirstLat, GDS_FirstLon, GDS_LastLat, GDS_LastLon, GDS_LonIncr, GDS_LatIncr);
   else
     fprintf(stdout, " Grid Description Section not defined");
 
-  if ( nerr > 0 ) fprintf(stdout, " <-- GRIB data corrupted!");
+  if (nerr > 0) fprintf(stdout, " <-- GRIB data corrupted!");
   fprintf(stdout, "\n");
 }
 
-
-void gribPrintGDS(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
+void
+gribPrintGDS(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
 {
-  int gribversion = gribVersion(gribbuffer, (size_t)recsize);
+  int gribversion = gribVersion(gribbuffer, (size_t) recsize);
 
-  if ( gribversion == 0 || gribversion == 1 )
-    grib1PrintGDS(nrec, recpos, recsize, gribbuffer);
+  if (gribversion == 0 || gribversion == 1) grib1PrintGDS(nrec, recpos, recsize, gribbuffer);
   /*
   else if ( gribversion == 2 )
     grib2PrintGDS(nrec, recpos, recsize, gribbuffer);
   */
   else
     {
-      fprintf(stdout, "%5d :%4ld%9ld%7ld : GRIB version %d unsupported\n",
-	      nrec, 0L, recpos, recsize, gribversion); 
+      fprintf(stdout, "%5d :%4ld%9ld%7ld : GRIB version %d unsupported\n", nrec, 0L, recpos, recsize, gribversion);
     }
 }
 
-
-static void grib1PrintBMS(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
+static void
+grib1PrintBMS(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
 {
   static int header = 1;
   unsigned char *pds = NULL, *gds = NULL, *bms = NULL, *bds = NULL;
 
   UNUSED(recpos);
 
-  if ( header )
+  if (header)
     {
-      fprintf(stdout, 
-      "  Rec : Code Level     BMS    Size\n");
-/*     ----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+ */
+      fprintf(stdout, "  Rec : Code Level     BMS    Size\n");
+      //               ----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+
       header = 0;
     }
 
   long gribrecsize;
   int nerr = grib1Sections(gribbuffer, recsize, &pds, &gds, &bms, &bds, &gribrecsize);
-  if ( nerr < 0 )
+  if (nerr < 0)
     {
       fprintf(stdout, "%5d : GRIB message error\n", nrec);
       return;
@@ -5454,36 +5396,33 @@ static void grib1PrintBMS(int nrec, long recpos, long recsize, unsigned char *gr
 
   fprintf(stdout, "%5d :", nrec);
 
-  if ( bms )
-    fprintf(stdout, "%4d%7d %7d %7d",
-	    PDS_Parameter, level, BMS_Len, BMS_BitmapSize);
+  if (bms)
+    fprintf(stdout, "%4d%7d %7d %7d", PDS_Parameter, level, BMS_Len, BMS_BitmapSize);
   else
     fprintf(stdout, "%4d%7d Bit Map Section not defined", PDS_Parameter, level);
 
-  if ( nerr > 0 ) fprintf(stdout, " <-- GRIB data corrupted!");
+  if (nerr > 0) fprintf(stdout, " <-- GRIB data corrupted!");
   fprintf(stdout, "\n");
 }
 
-
-void gribPrintBMS(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
+void
+gribPrintBMS(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
 {
-  int gribversion = gribVersion(gribbuffer, (size_t)recsize);
+  int gribversion = gribVersion(gribbuffer, (size_t) recsize);
 
-  if ( gribversion == 0 || gribversion == 1 )
-    grib1PrintBMS(nrec, recpos, recsize, gribbuffer);
+  if (gribversion == 0 || gribversion == 1) grib1PrintBMS(nrec, recpos, recsize, gribbuffer);
   /*
   else if ( gribversion == 2 )
     grib2PrintBMS(nrec, recpos, recsize, gribbuffer);
   */
   else
     {
-      fprintf(stdout, "%5d :%4ld%9ld%7ld : GRIB version %d unsupported\n",
-	      nrec, 0L, recpos, recsize, gribversion); 
+      fprintf(stdout, "%5d :%4ld%9ld%7ld : GRIB version %d unsupported\n", nrec, 0L, recpos, recsize, gribversion);
     }
 }
 
-
-static void grib1PrintBDS(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
+static void
+grib1PrintBDS(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
 {
   static int header = 1;
   unsigned char *pds = NULL, *gds = NULL, *bms = NULL, *bds = NULL;
@@ -5491,17 +5430,16 @@ static void grib1PrintBDS(int nrec, long recpos, long recsize, unsigned char *gr
 
   UNUSED(recpos);
 
-  if ( header )
+  if (header)
     {
-      fprintf(stdout, 
-      "  Rec : Code Level     BDS Flag     Scale   RefValue Bits  CR\n");
-/*     ----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+ */
+      fprintf(stdout, "  Rec : Code Level     BDS Flag     Scale   RefValue Bits  CR\n");
+      //               ----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+
       header = 0;
     }
 
   long gribrecsize;
   int nerr = grib1Sections(gribbuffer, recsize, &pds, &gds, &bms, &bds, &gribrecsize);
-  if ( nerr < 0 )
+  if (nerr < 0)
     {
       fprintf(stdout, "%5d : GRIB message error\n", nrec);
       return;
@@ -5509,55 +5447,52 @@ static void grib1PrintBDS(int nrec, long recpos, long recsize, unsigned char *gr
 
   int level = get_level(pds);
 
-  double cr = (((BDS_Flag >> 4)&1) && BDS_Z == 128) ? get_cr(&bds[17], &bds[20]) : 1;
+  double cr = (((BDS_Flag >> 4) & 1) && BDS_Z == 128) ? get_cr(&bds[17], &bds[20]) : 1;
 
   double refval = BDS_RefValue;
 
-  if ( BDS_BinScale < 0 )
-    scale = 1.0/pow(2.0, (double) -BDS_BinScale);
+  if (BDS_BinScale < 0)
+    scale = 1.0 / pow(2.0, (double) -BDS_BinScale);
   else
     scale = pow(2.0, (double) BDS_BinScale);
 
-  if ( PDS_DecimalScale )
+  if (PDS_DecimalScale != 0)
     {
-      double decscale = pow(10.0, (double)-PDS_DecimalScale);
+      double decscale = pow(10.0, (double) -PDS_DecimalScale);
       refval *= decscale;
-      scale  *= decscale;
+      scale *= decscale;
     }
 
   fprintf(stdout, "%5d :", nrec);
 
-  if ( bds )
-    fprintf(stdout, "%4d%7d %7d %4d %8.5g %11.5g%4d %6.4g",
-	    PDS_Parameter, level,
-	    BDS_Len, BDS_Flag, scale, refval, BDS_NumBits, cr);
+  if (bds)
+    fprintf(stdout, "%4d%7d %7d %4d %8.5g %11.5g%4d %6.4g", PDS_Parameter, level, BDS_Len, BDS_Flag, scale, refval, BDS_NumBits,
+            cr);
   else
     fprintf(stdout, " Binary Data Section not defined");
 
-  if ( nerr > 0 ) fprintf(stdout, " <-- GRIB data corrupted!");
+  if (nerr > 0) fprintf(stdout, " <-- GRIB data corrupted!");
   fprintf(stdout, "\n");
 }
 
-
-void gribPrintBDS(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
+void
+gribPrintBDS(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
 {
-  int gribversion = gribVersion(gribbuffer, (size_t)recsize);
+  int gribversion = gribVersion(gribbuffer, (size_t) recsize);
 
-  if ( gribversion == 0 || gribversion == 1 )
-    grib1PrintBDS(nrec, recpos, recsize, gribbuffer);
+  if (gribversion == 0 || gribversion == 1) grib1PrintBDS(nrec, recpos, recsize, gribbuffer);
   /*
   else if ( gribversion == 2 )
     grib2PrintBDS(nrec, recpos, recsize, gribbuffer);
   */
   else
     {
-      fprintf(stdout, "%5d :%4ld%9ld%7ld : GRIB version %d unsupported\n",
-	      nrec, 0L, recpos, recsize, gribversion); 
+      fprintf(stdout, "%5d :%4ld%9ld%7ld : GRIB version %d unsupported\n", nrec, 0L, recpos, recsize, gribversion);
     }
 }
 
-
-void gribCheck1(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
+void
+gribCheck1(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
 {
   unsigned char *pds = NULL, *gds = NULL, *bms = NULL, *bds = NULL;
 
@@ -5565,13 +5500,13 @@ void gribCheck1(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
 
   long gribrecsize;
   int nerr = grib1Sections(gribbuffer, recsize, &pds, &gds, &bms, &bds, &gribrecsize);
-  if ( nerr < 0 )
+  if (nerr < 0)
     {
       fprintf(stdout, "%5d : GRIB message error\n", nrec);
       return;
     }
 
-  if ( nerr > 0 )
+  if (nerr > 0)
     {
       fprintf(stdout, "%5d : <-- GRIB data corrupted!\n", nrec);
       return;
@@ -5579,97 +5514,98 @@ void gribCheck1(int nrec, long recpos, long recsize, unsigned char *gribbuffer)
 
   int level = get_level(pds);
 
-  double cr = (((BDS_Flag >> 4)&1) && BDS_Z == 128) ? get_cr(&bds[17], &bds[20]) : 1;
+  double cr = (((BDS_Flag >> 4) & 1) && BDS_Z == 128) ? get_cr(&bds[17], &bds[20]) : 1;
 
-  if ( IS_EQUAL(cr, 1) && BDS_NumBits == 24 )
+  if (IS_EQUAL(cr, 1) && BDS_NumBits == 24)
     fprintf(stdout, "GRIB record %5d : code = %4d   level = %7d\n", nrec, PDS_Parameter, level);
 }
 
-
-static
-void repair1(unsigned char *gbuf, long gbufsize)
+static void
+repair1(unsigned char *gbuf, long gbufsize)
 {
   unsigned char *pds = NULL, *gds = NULL, *bms = NULL, *bds = NULL;
   /* int recLen; */
-  unsigned char *source;
-  size_t sourceLen;
-  int bds_nbits, bds_flag, lspherc, lcomplex /*, lcompress */;
-  int bds_head = 11;
-  int bds_ext = 0, bds_ubits;
-  int datstart = 0;
+  size_t bds_nbits;
+  int bds_flag, lspherc, lcomplex /*, lcompress */;
+  enum
+  {
+    bds_head = 11
+  };
+  size_t bds_ext = 0, bds_ubits;
 
   long gribrecsize;
   int nerr = grib1Sections(gbuf, gbufsize, &pds, &gds, &bms, &bds, &gribrecsize);
-  if ( nerr < 0 )
+  if (nerr < 0)
     {
       fprintf(stdout, "GRIB message error\n");
       return;
     }
 
-  if ( nerr > 0 )
+  if (nerr > 0)
     {
       fprintf(stdout, "GRIB data corrupted!\n");
       return;
     }
 
-  unsigned bds_len   = BDS_Len;
+  unsigned bds_len = BDS_Len;
   bds_nbits = BDS_NumBits;
-  bds_flag  = BDS_Flag;
-  bds_ubits = bds_flag & 15;
-  lspherc   =  bds_flag >> 7;
-  lcomplex  = (bds_flag >> 6)&1;
+  bds_flag = BDS_Flag;
+  bds_ubits = (size_t) bds_flag & 15;
+  lspherc = bds_flag >> 7;
+  lcomplex = (bds_flag >> 6) & 1;
   /* lcompress = (bds_flag >> 4)&1; */
 
-  if ( lspherc )
+  if (lspherc)
     {
-      if ( lcomplex  )
-	{
-	  int jup, ioff;
-	  jup  = bds[15];
-	  ioff = (jup+1)*(jup+2);
-	  bds_ext = 4 + 3 + 4*ioff;
-	}
+      if (lcomplex)
+        {
+          size_t jup, ioff;
+          jup = (size_t) bds[15];
+          ioff = (jup + 1) * (jup + 2);
+          bds_ext = 4 + 3 + 4 * ioff;
+        }
       else
-	{
-	  bds_ext = 4;
-	}
+        {
+          bds_ext = 4;
+        }
     }
 
-  datstart = bds_head + bds_ext;
+  size_t datstart = bds_head + bds_ext;
 
-  source = bds + datstart;
+  unsigned char *source = bds + datstart;
 
-  sourceLen = (size_t)(((((bds_len - datstart)*8-bds_ubits)/bds_nbits)*bds_nbits)/8);
+  size_t sourceLen = ((((bds_len - datstart) * 8 - bds_ubits) / bds_nbits) * bds_nbits) / 8;
 
-  if ( bds_nbits == 24 )
+  if (bds_nbits == 24)
     {
-      unsigned char *pbuf = (unsigned char*) Malloc(sourceLen);;
-      size_t nelem = sourceLen/3;
+      unsigned char *pbuf = (unsigned char *) Malloc(sourceLen);
+      ;
+      size_t nelem = sourceLen / 3;
       for (size_t i = 0; i < nelem; ++i)
-	{
-	  pbuf[3*i  ] = source[        i];
-	  pbuf[3*i+1] = source[  nelem+i];
-	  pbuf[3*i+2] = source[2*nelem+i];
-	}
+        {
+          pbuf[3 * i] = source[i];
+          pbuf[3 * i + 1] = source[nelem + i];
+          pbuf[3 * i + 2] = source[2 * nelem + i];
+        }
       memcpy(source, pbuf, sourceLen);
       Free(pbuf);
     }
 }
 
-
-void gribRepair1(int nrec, long recsize, unsigned char *gribbuffer)
+void
+gribRepair1(int nrec, long recsize, unsigned char *gribbuffer)
 {
   unsigned char *pds = NULL, *gds = NULL, *bms = NULL, *bds = NULL;
 
   long gribrecsize;
   int nerr = grib1Sections(gribbuffer, recsize, &pds, &gds, &bms, &bds, &gribrecsize);
-  if ( nerr < 0 )
+  if (nerr < 0)
     {
       fprintf(stdout, "%5d : GRIB message error\n", nrec);
       return;
     }
 
-  if ( nerr > 0 )
+  if (nerr > 0)
     {
       fprintf(stdout, "%5d : <-- GRIB data corrupted!\n", nrec);
       return;
@@ -5677,9 +5613,9 @@ void gribRepair1(int nrec, long recsize, unsigned char *gribbuffer)
 
   int level = get_level(pds);
 
-  double cr = (((BDS_Flag >> 4)&1) && BDS_Z == 128) ? get_cr(&bds[17], &bds[20]) : 1;
+  double cr = (((BDS_Flag >> 4) & 1) && BDS_Z == 128) ? get_cr(&bds[17], &bds[20]) : 1;
 
-  if ( IS_EQUAL(cr, 1) && BDS_NumBits == 24 )
+  if (IS_EQUAL(cr, 1) && BDS_NumBits == 24)
     {
       fprintf(stdout, "Repair GRIB record %5d : code = %4d   level = %7d\n", nrec, PDS_Parameter, level);
       repair1(gribbuffer, recsize);
@@ -5688,18 +5624,21 @@ void gribRepair1(int nrec, long recsize, unsigned char *gribbuffer)
 #include <stdio.h>
 #include <string.h>
 
-#if defined (HAVE_CONFIG_H)
+#if defined(HAVE_CONFIG_H)
 #endif
 
-#if  defined (HAVE_LIBSZ)
+#if defined(HAVE_LIBSZ)
 #if defined(__cplusplus)
-extern "C" {
+extern "C"
+{
 #endif
 #include <szlib.h>
-#ifdef  __cplusplus
+#ifdef __cplusplus
 }
 #endif
 
+// clang-format off
+
 #define OPTIONS_MASK        (SZ_RAW_OPTION_MASK | SZ_MSB_OPTION_MASK | SZ_NN_OPTION_MASK)
 
 #define PIXELS_PER_BLOCK    (8)
@@ -5721,24 +5660,27 @@ extern "C" {
 				     (var[offset+3] = 0xFF & (value      )))
 #endif
 
-int gribGetZip(size_t recsize, unsigned char *gribbuffer, size_t *urecsize)
+// clang-format on
+
+int
+gribGetZip(size_t recsize, unsigned char *gribbuffer, size_t *urecsize)
 {
   int compress = 0;
   unsigned char *pds = NULL, *gds = NULL, *bms = NULL, *bds = NULL;
 
   int gribversion = gribVersion(gribbuffer, recsize);
 
-  if ( gribversion == 2 ) return compress;
+  if (gribversion == 2) return compress;
 
   long gribrecsize;
-  int nerr = grib1Sections(gribbuffer, (long)recsize, &pds, &gds, &bms, &bds, &gribrecsize);
-  if ( nerr < 0 )
+  int nerr = grib1Sections(gribbuffer, (long) recsize, &pds, &gds, &bms, &bds, &gribrecsize);
+  if (nerr < 0)
     {
       fprintf(stdout, "GRIB message error\n");
       return compress;
     }
 
-  if ( nerr > 0 )
+  if (nerr > 0)
     {
       fprintf(stdout, "GRIB data corrupted!\n");
       return compress;
@@ -5746,16 +5688,16 @@ int gribGetZip(size_t recsize, unsigned char *gribbuffer, size_t *urecsize)
 
   /* bds_len   = BDS_Len; */
   /* bds_nbits = BDS_NumBits; */
-  int bds_flag  = BDS_Flag;
+  int bds_flag = BDS_Flag;
   /* lspherc   =  bds_flag >> 7; */
   /* lcomplex  = (bds_flag >> 6)&1; */
-  int lcompress = (bds_flag >> 4)&1;
+  int lcompress = (bds_flag >> 4) & 1;
 
   size_t gribsize = 0;
-  if ( lcompress )
+  if (lcompress)
     {
       compress = BDS_Z;
-      if ( compress == Z_SZIP ) gribsize = (size_t) GET_UINT3(bds[14], bds[15], bds[16]);
+      if (compress == Z_SZIP) gribsize = (size_t) GET_UINT3(bds[14], bds[15], bds[16]);
     }
 
   *urecsize = gribsize;
@@ -5763,10 +5705,10 @@ int gribGetZip(size_t recsize, unsigned char *gribbuffer, size_t *urecsize)
   return compress;
 }
 
-
-int gribZip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufsize)
+int
+gribZip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufsize)
 {
-#if ! defined(HAVE_LIBSZ)
+#if !defined(HAVE_LIBSZ)
   static int libszwarn = 1;
 #endif
   unsigned char *pds = NULL, *gds = NULL, *bms = NULL, *bds = NULL;
@@ -5774,37 +5716,37 @@ int gribZip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufsi
 
   unsigned gribLen = GET_UINT3(dbuf[4], dbuf[5], dbuf[6]);
 
-  int rec_len = gribLen;
+  int rec_len = (int) gribLen;
 
   long gribrecsize;
   int nerr = grib1Sections(dbuf, dbufsize, &pds, &gds, &bms, &bds, &gribrecsize);
-  if ( nerr < 0 )
+  if (nerr < 0)
     {
       fprintf(stdout, "GRIB message error\n");
-      return gribrecsize;
+      return (int) gribrecsize;
     }
 
-  if ( nerr > 0 )
+  if (nerr > 0)
     {
       fprintf(stdout, "GRIB data corrupted!\n");
-      return gribrecsize;
+      return (int) gribrecsize;
     }
 
-   int bds_zoffset = 12;
+  int bds_zoffset = 12;
+
+  unsigned bds_len = BDS_Len;
+  if (gribLen > JP23SET && bds_len <= 120)
+    {
+      gribLen &= JP23SET;
+      gribLen *= 120;
+      bds_len = correct_bdslen(bds_len, gribLen, bds - dbuf);
+      llarge = true;
+      bds_zoffset += 2;
+    }
 
-   int bds_len   = BDS_Len;
-   if ( gribLen > JP23SET && bds_len <= 120 )
-     {
-       gribLen &= JP23SET;
-       gribLen *= 120;
-       bds_len = correct_bdslen(bds_len, gribLen, bds-dbuf);
-       llarge = true;
-       bds_zoffset += 2;
-     }
+  if (gribLen > JP24SET || llarge) return (int) gribLen;
 
-   if ( gribLen > JP24SET || llarge ) return gribLen;
- 
-#if  defined(HAVE_LIBSZ)
+#if defined(HAVE_LIBSZ)
   {
     int bds_zstart = 14;
     unsigned gribLenOld = 0;
@@ -5813,317 +5755,318 @@ int gribZip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufsi
     unsigned char *pbuf = NULL;
 
     int bds_nbits = BDS_NumBits;
-    int bds_flag  = BDS_Flag;
+    int bds_flag = BDS_Flag;
     int bds_ubits = bds_flag & 15;
-    int lspherc   =  bds_flag >> 7;
-    int lcomplex  = (bds_flag >> 6)&1;
+    int lspherc = bds_flag >> 7;
+    int lcomplex = (bds_flag >> 6) & 1;
     /* lcompress = (bds_flag >> 4)&1; */
-    
-    if ( bds_nbits != 8 && bds_nbits != 16 && bds_nbits != 24 && bds_nbits != 32 )
+
+    if (bds_nbits != 8 && bds_nbits != 16 && bds_nbits != 24 && bds_nbits != 32)
       {
-	static bool linfo = true;
-	if ( linfo && bds_nbits != 0 )
-	  {
-	    linfo = false;
-	    fprintf(stderr, "GRIB szip supports only 8, 16, 24 and 32 bit data!\n");
-	  }
-	return rec_len;
+        static bool linfo = true;
+        if (linfo && bds_nbits != 0)
+          {
+            linfo = false;
+            fprintf(stderr, "GRIB szip supports only 8, 16, 24 and 32 bit data!\n");
+          }
+        return rec_len;
       }
 
     int bits_per_sample = (bds_nbits == 24) ? 8 : bds_nbits;
 
-    SZ_com_t sz_param;          /* szip parameter block */
-    sz_param.options_mask        = OPTIONS_MASK;
-    sz_param.bits_per_pixel      = bits_per_sample;
-    sz_param.pixels_per_block    = PIXELS_PER_BLOCK;
+    SZ_com_t sz_param; /* szip parameter block */
+    sz_param.options_mask = OPTIONS_MASK;
+    sz_param.bits_per_pixel = bits_per_sample;
+    sz_param.pixels_per_block = PIXELS_PER_BLOCK;
     sz_param.pixels_per_scanline = PIXELS_PER_SCANLINE;
 
-    if ( lspherc )
+    if (lspherc)
       {
         bds_ext = 4;
-	if ( lcomplex )
-	  {
-	    int jup  = bds[15];
-	    int ioff = (jup+1)*(jup+2);
-	    bds_ext += 3 + 4*ioff;
-	  }
+        if (lcomplex)
+          {
+            int jup = bds[15];
+            int ioff = (jup + 1) * (jup + 2);
+            bds_ext += 3 + 4 * ioff;
+          }
       }
 
     size_t datstart = bds_head + bds_ext;
 
-    size_t datsize = ((((bds_len - datstart)*8-bds_ubits)/bds_nbits)*bds_nbits)/8;
+    size_t datsize = ((((bds_len - datstart) * 8 - bds_ubits) / bds_nbits) * bds_nbits) / 8;
 
-    if ( datsize < MIN_SIZE ) return rec_len;
+    if (datsize < MIN_SIZE) return rec_len;
     /*
     fprintf(stderr, "%d %d %d %d\n", bds_len, datstart, bds_len - datstart, datsize);
     */
     size_t sourceLen = datsize;
-    size_t destLen   = sbufsize;
-    
+    size_t destLen = sbufsize;
+
     unsigned char *source = bds + datstart;
     unsigned char *dest = sbuf;
 
-    if ( bds_nbits == 24 )
+    if (bds_nbits == 24)
       {
-	long nelem = sourceLen/3;
-	pbuf = (unsigned char*) Malloc(sourceLen);
-	for (long i = 0; i < nelem; ++i)
-	  {
-	    pbuf[        i] = source[3*i  ];
-	    pbuf[  nelem+i] = source[3*i+1];
-	    pbuf[2*nelem+i] = source[3*i+2];
-	  }
-	source = pbuf;
+        long nelem = sourceLen / 3;
+        pbuf = (unsigned char *) Malloc(sourceLen);
+        for (long i = 0; i < nelem; ++i)
+          {
+            pbuf[i] = source[3 * i];
+            pbuf[nelem + i] = source[3 * i + 1];
+            pbuf[2 * nelem + i] = source[3 * i + 2];
+          }
+        source = pbuf;
       }
 
     int status = SZ_BufftoBuffCompress(dest, &destLen, source, sourceLen, &sz_param);
-    if ( status != SZ_OK )
+    if (status != SZ_OK)
       {
-	if ( status == SZ_NO_ENCODER_ERROR )
-	  Warning("SZ_NO_ENCODER_ERROR code %3d level %3d", PDS_Parameter, PDS_Level2);
-	else if ( status == SZ_PARAM_ERROR )
-	  Warning("SZ_PARAM_ERROR code %3d level %3d", PDS_Parameter, PDS_Level2);
-	else if ( status == SZ_MEM_ERROR )
-	  Warning("SZ_MEM_ERROR code %3d level %3d", PDS_Parameter, PDS_Level2);
-	else if ( status == SZ_OUTBUFF_FULL )
-	  /*Warning("SZ_OUTBUFF_FULL code %3d level %3d", PDS_Parameter, PDS_Level2)*/;
-	else
-	  Warning("SZ ERROR: %d code %3d level %3d", status, PDS_Parameter, PDS_Level2);
+        if (status == SZ_NO_ENCODER_ERROR)
+          Warning("SZ_NO_ENCODER_ERROR code %3d level %3d", PDS_Parameter, PDS_Level2);
+        else if (status == SZ_PARAM_ERROR)
+          Warning("SZ_PARAM_ERROR code %3d level %3d", PDS_Parameter, PDS_Level2);
+        else if (status == SZ_MEM_ERROR)
+          Warning("SZ_MEM_ERROR code %3d level %3d", PDS_Parameter, PDS_Level2);
+        else if (status == SZ_OUTBUFF_FULL)
+          /*Warning("SZ_OUTBUFF_FULL code %3d level %3d", PDS_Parameter, PDS_Level2)*/;
+        else
+          Warning("SZ ERROR: %d code %3d level %3d", status, PDS_Parameter, PDS_Level2);
       }
-    
-    if ( pbuf ) Free(pbuf);
+
+    if (pbuf) Free(pbuf);
     /*
     fprintf(stderr, "sourceLen, destLen %d %d\n", sourceLen, destLen);
     */
-    if ( destLen < MIN_COMPRESS*sourceLen )
+    if (destLen < MIN_COMPRESS * sourceLen)
       {
-	source = bds + datstart + bds_zoffset;
-	memcpy(source, dest, destLen);
-	
-	/* ----++++ number of unused bits at end of section) */
-
-	BDS_Flag -= bds_ubits;
-    
-	gribLenOld = gribLen;
-
-	if ( bds_ext )
-	  for (long i = bds_ext-1; i >= 0; --i)
-	    bds[bds_zoffset+bds_head+i] = bds[bds_head+i];
-
-	/*
-	fprintf(stderr, "destLen, datsize, datstart %d %d %d\n", destLen, datsize, datstart);
-	*/
-	/*	memcpy(bds + datstart + bds_zoffset, source, destLen); */
-	/*
-	  fprintf(stderr, "z>>> %d %d %d %d <<<\n", (int) bds[0+datstart+bds_zoffset],
-	    (int)bds[1+datstart+bds_zoffset], (int)bds[2+datstart+bds_zoffset], (int)bds[3+datstart+bds_zoffset]);
-	*/
-	if ( llarge )
-	  {
-	    if ( gribLenOld%120 )
-	      {
-		fprintf(stderr, "Internal problem, record length not multiple of 120!");
-		while ( gribLenOld%120 ) gribLenOld++;
-	      }
+        source = bds + datstart + bds_zoffset;
+        memcpy(source, dest, destLen);
+
+        /* ----++++ number of unused bits at end of section) */
+
+        BDS_Flag -= bds_ubits;
+
+        gribLenOld = gribLen;
+
+        if (bds_ext)
+          for (long i = bds_ext - 1; i >= 0; --i) bds[bds_zoffset + bds_head + i] = bds[bds_head + i];
+
+        /*
+        fprintf(stderr, "destLen, datsize, datstart %d %d %d\n", destLen, datsize, datstart);
+        */
+        /*	memcpy(bds + datstart + bds_zoffset, source, destLen); */
+        /*
+          fprintf(stderr, "z>>> %d %d %d %d <<<\n", (int) bds[0+datstart+bds_zoffset],
+            (int)bds[1+datstart+bds_zoffset], (int)bds[2+datstart+bds_zoffset], (int)bds[3+datstart+bds_zoffset]);
+        */
+        if (llarge)
+          {
+            if (gribLenOld % 120)
+              {
+                fprintf(stderr, "Internal problem, record length not multiple of 120!");
+                while (gribLenOld % 120) gribLenOld++;
+              }
             // gribLenOld = gribLenOld / (-120);
-	    // gribLenOld = JP23SET - gribLenOld + 1;
+            // gribLenOld = JP23SET - gribLenOld + 1;
 
-	    SetLen3(bds, bds_zstart, gribLenOld);
-	    SetLen4(bds, bds_zstart+3, sourceLen);
-	    SetLen4(bds, bds_zstart+7, destLen);
-	  }
-	else
-	  {
-	    SetLen3(bds, bds_zstart, gribLenOld);
-	    SetLen3(bds, bds_zstart+3, sourceLen);
-	    SetLen3(bds, bds_zstart+6, destLen);
-	  }
+            SetLen3(bds, bds_zstart, gribLenOld);
+            SetLen4(bds, bds_zstart + 3, sourceLen);
+            SetLen4(bds, bds_zstart + 7, destLen);
+          }
+        else
+          {
+            SetLen3(bds, bds_zstart, gribLenOld);
+            SetLen3(bds, bds_zstart + 3, sourceLen);
+            SetLen3(bds, bds_zstart + 6, destLen);
+          }
 
-	int bdsLen = datstart + bds_zoffset + destLen;
+        int bdsLen = datstart + bds_zoffset + destLen;
 
-	bds[11] = 0;
-	bds[12] = 0;
+        bds[11] = 0;
+        bds[12] = 0;
 
-	BDS_Z   = Z_SZIP;
+        BDS_Z = Z_SZIP;
 
-	BDS_Flag += 16;
-	if ( (bdsLen%2) == 1 )
-	  {
-	    BDS_Flag += 8;
-	    bds[bdsLen++] = 0;
-	  }
+        BDS_Flag += 16;
+        if ((bdsLen % 2) == 1)
+          {
+            BDS_Flag += 8;
+            bds[bdsLen++] = 0;
+          }
 
-	SetLen3(bds, 0, bdsLen);
+        SetLen3(bds, 0, bdsLen);
 
-	gribLen = (bds - dbuf) + bdsLen;
+        gribLen = (bds - dbuf) + bdsLen;
 
-	dbuf[gribLen++] = '7';
-	dbuf[gribLen++] = '7';
-	dbuf[gribLen++] = '7';
-	dbuf[gribLen++] = '7';
+        dbuf[gribLen++] = '7';
+        dbuf[gribLen++] = '7';
+        dbuf[gribLen++] = '7';
+        dbuf[gribLen++] = '7';
 
-	if ( llarge )
-	  {
-	    long bdslen = gribLen - 4;
+        if (llarge)
+          {
+            long bdslen = gribLen - 4;
 
-	    /*
-	      If a very large product, the section 4 length field holds
-	      the number of bytes in the product after section 4 upto
-	      the end of the padding bytes.
-	      This is a fixup to get round the restriction on product lengths
-	      due to the count being only 24 bits. It is only possible because
-	      the (default) rounding for GRIB products is 120 bytes.
-	    */
-	    while ( gribLen%120 ) dbuf[gribLen++] = 0;
+            /*
+              If a very large product, the section 4 length field holds
+              the number of bytes in the product after section 4 upto
+              the end of the padding bytes.
+              This is a fixup to get round the restriction on product lengths
+              due to the count being only 24 bits. It is only possible because
+              the (default) rounding for GRIB products is 120 bytes.
+            */
+            while (gribLen % 120) dbuf[gribLen++] = 0;
 
-	    long itemp = gribLen / (-120);
-	    itemp = JP23SET - itemp + 1;
+            long itemp = gribLen / (-120);
+            itemp = JP23SET - itemp + 1;
 
-	    SetLen3(dbuf, 4, itemp);
+            SetLen3(dbuf, 4, itemp);
 
-	    bdslen = gribLen - bdslen;
+            bdslen = gribLen - bdslen;
 
-	    SetLen3(bds, 0, bdslen);
-	  }
-	else
-	  {
-	    SetLen3(dbuf, 4, gribLen);
-	  }
+            SetLen3(bds, 0, bdslen);
+          }
+        else
+          {
+            SetLen3(dbuf, 4, gribLen);
+          }
       }
     else
       {
       }
     /*
     fprintf(stderr, "%3d %3d griblen in %6d  out %6d  CR %g   slen %6d dlen %6d  CR %g\n",
-	    PDS_Parameter, PDS_Level1, gribLenOld, gribLen,
-	    ((double)gribLenOld)/gribLen, sourceLen, destLen,
-	    ((double)sourceLen)/destLen);
+            PDS_Parameter, PDS_Level1, gribLenOld, gribLen,
+            ((double)gribLenOld)/gribLen, sourceLen, destLen,
+            ((double)sourceLen)/destLen);
     */
   }
 
 #else
-  
+
   UNUSED(sbuf);
   UNUSED(sbufsize);
 
-  if ( libszwarn )
+  if (libszwarn)
     {
       Warning("Compression disabled, szlib not available!");
       libszwarn = 0;
     }
 #endif
 
-  if ( llarge )
-    while ( gribLen%120 ) dbuf[gribLen++] = 0;
+  if (llarge)
+    while (gribLen % 120) dbuf[gribLen++] = 0;
   else
-    while ( gribLen & 7 ) dbuf[gribLen++] = 0;
+    while (gribLen & 7) dbuf[gribLen++] = 0;
 
-  rec_len = gribLen;
+  rec_len = (int) gribLen;
 
   return rec_len;
 }
 
-
-int  gribUnzip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufsize)
+int
+gribUnzip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbufsize)
 {
-#if ! defined(HAVE_LIBSZ)
+#if !defined(HAVE_LIBSZ)
   static int libszwarn = 1;
 #endif
   unsigned char *pds = NULL, *gds = NULL, *bms = NULL, *bds = NULL;
   size_t gribLen = 0;
   size_t destLen, sourceLen;
-  enum { bds_head = 11 };
+  enum
+  {
+    bds_head = 11
+  };
   int bds_ext = 0;
 
   UNUSED(dbufsize);
 
   long gribrecsize;
   int nerr = grib1Sections(sbuf, sbufsize, &pds, &gds, &bms, &bds, &gribrecsize);
-  if ( nerr < 0 )
+  if (nerr < 0)
     {
       fprintf(stdout, "GRIB message error\n");
       return 0;
     }
 
-  if ( nerr > 0 )
+  if (nerr > 0)
     {
       fprintf(stdout, "GRIB data corrupted!\n");
       return 0;
     }
 
-  //unsigned bds_len = BDS_Len;
+  // unsigned bds_len = BDS_Len;
   bool llarge = false;
 
   int bds_zoffset = 12;
-  if ( llarge ) bds_zoffset += 2;
+  if (llarge) bds_zoffset += 2;
 
   int bds_nbits = BDS_NumBits;
-  int bds_flag  = BDS_Flag;
-  int lspherc   =  bds_flag >> 7;
-  int lcomplex  = (bds_flag >> 6)&1;
+  int bds_flag = BDS_Flag;
+  int lspherc = bds_flag >> 7;
+  int lcomplex = (bds_flag >> 6) & 1;
   /* lcompress = (bds_flag >> 4)&1; */
 
-  if ( lspherc )
+  if (lspherc)
     {
-      if ( lcomplex  )
-	{
-	  int jup  = bds[bds_zoffset+15];
-	  int ioff = (jup+1)*(jup+2);
-	  bds_ext = 4 + 3 + 4*ioff;
-	}
+      if (lcomplex)
+        {
+          int jup = bds[bds_zoffset + 15];
+          int ioff = (jup + 1) * (jup + 2);
+          bds_ext = 4 + 3 + 4 * ioff;
+        }
       else
-	{
-	  bds_ext = 4;
-	}
+        {
+          bds_ext = 4;
+        }
     }
 
-  size_t datstart = bds_head + (size_t)bds_ext;
+  size_t datstart = bds_head + (size_t) bds_ext;
 
   unsigned char *source = bds + datstart + bds_zoffset;
-  if ( llarge )
-    sourceLen = ((size_t) ((bds[21]<<24)+(bds[22]<<16)+(bds[23]<<8)+bds[24]));
+  if (llarge)
+    sourceLen = ((size_t) ((bds[21] << 24) + (bds[22] << 16) + (bds[23] << 8) + bds[24]));
   else
-    sourceLen = ((size_t) ((bds[20]<<16)+(bds[21]<<8)+bds[22]));
+    sourceLen = ((size_t) ((bds[20] << 16) + (bds[21] << 8) + bds[22]));
 
   nerr = grib1Sections(dbuf, sbufsize, &pds, &gds, &bms, &bds, &gribrecsize);
-  if ( nerr < 0 )
+  if (nerr < 0)
     {
       fprintf(stdout, "GRIB message error\n");
       return 0;
     }
 
-  if ( nerr > 0 )
+  if (nerr > 0)
     {
       fprintf(stdout, "GRIB data corrupted!\n");
       return 0;
     }
 
   unsigned char *dest = bds + datstart;
-  if ( llarge )
-    destLen = ((size_t) ((bds[17]<<24)+(bds[18]<<16)+(bds[19]<<8)+bds[20]));
+  if (llarge)
+    destLen = ((size_t) ((bds[17] << 24) + (bds[18] << 16) + (bds[19] << 8) + bds[20]));
   else
-    destLen = ((size_t) ((bds[17]<<16)+(bds[18]<<8)+bds[19]));
+    destLen = ((size_t) ((bds[17] << 16) + (bds[18] << 8) + bds[19]));
 
-  BDS_Flag = (unsigned char)(BDS_Flag - 16);
+  BDS_Flag = (unsigned char) (BDS_Flag - 16);
 
   size_t bdsLen = datstart + destLen;
 
-#if  defined(HAVE_LIBSZ)
+#if defined(HAVE_LIBSZ)
   {
     int bds_zstart = 14;
-    unsigned recLen = GET_UINT3(bds[bds_zstart], bds[bds_zstart+1], bds[bds_zstart+2]);
+    unsigned recLen = GET_UINT3(bds[bds_zstart], bds[bds_zstart + 1], bds[bds_zstart + 2]);
 
     int bits_per_sample = (bds_nbits == 24) ? 8 : bds_nbits;
 
-    SZ_com_t sz_param;          /* szip parameter block */
-    sz_param.options_mask        = OPTIONS_MASK;
-    sz_param.bits_per_pixel      = bits_per_sample;
-    sz_param.pixels_per_block    = PIXELS_PER_BLOCK;
+    SZ_com_t sz_param; /* szip parameter block */
+    sz_param.options_mask = OPTIONS_MASK;
+    sz_param.bits_per_pixel = bits_per_sample;
+    sz_param.pixels_per_block = PIXELS_PER_BLOCK;
     sz_param.pixels_per_scanline = PIXELS_PER_SCANLINE;
 
-    if ( bds_ext )
-      for (long i = 0; i < bds_ext; ++i)
-	bds[bds_head+i] = bds[bds_zoffset+bds_head+i];
+    if (bds_ext)
+      for (long i = 0; i < bds_ext; ++i) bds[bds_head + i] = bds[bds_zoffset + bds_head + i];
 
     /*    fprintf(stderr, "gribUnzip: sourceLen %ld; destLen %ld\n", (long)sourceLen, (long)destLen);
     fprintf(stderr, "gribUnzip: sourceOff %d; destOff %d\n", bds[12], bds[11]);
@@ -6133,96 +6076,95 @@ int  gribUnzip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbu
     size_t tmpLen = destLen;
 
     int status = SZ_BufftoBuffDecompress(dest, &tmpLen, source, sourceLen, &sz_param);
-    if ( status != SZ_OK )
+    if (status != SZ_OK)
       {
-	if ( status == SZ_NO_ENCODER_ERROR )
-	  Warning("SZ_NO_ENCODER_ERROR code %3d level %3d", PDS_Parameter, PDS_Level2);
-	else if ( status == SZ_PARAM_ERROR )
-	  Warning("SZ_PARAM_ERROR code %3d level %3d", PDS_Parameter, PDS_Level2);
-	else if ( status == SZ_MEM_ERROR )
-	  Warning("SZ_MEM_ERROR code %3d level %3d", PDS_Parameter, PDS_Level2);
-	else if ( status == SZ_OUTBUFF_FULL )
-	  Warning("SZ_OUTBUFF_FULL code %3d level %3d", PDS_Parameter, PDS_Level2);
-	else
-	  Warning("SZ ERROR: %d code %3d level %3d", status, PDS_Parameter, PDS_Level2);
+        if (status == SZ_NO_ENCODER_ERROR)
+          Warning("SZ_NO_ENCODER_ERROR code %3d level %3d", PDS_Parameter, PDS_Level2);
+        else if (status == SZ_PARAM_ERROR)
+          Warning("SZ_PARAM_ERROR code %3d level %3d", PDS_Parameter, PDS_Level2);
+        else if (status == SZ_MEM_ERROR)
+          Warning("SZ_MEM_ERROR code %3d level %3d", PDS_Parameter, PDS_Level2);
+        else if (status == SZ_OUTBUFF_FULL)
+          Warning("SZ_OUTBUFF_FULL code %3d level %3d", PDS_Parameter, PDS_Level2);
+        else
+          Warning("SZ ERROR: %d code %3d level %3d", status, PDS_Parameter, PDS_Level2);
       }
     /*
     fprintf(stderr, "gribUnzip: sl = %ld  dl = %ld   tl = %ld\n",
-	    (long)sourceLen, (long)destLen,(long) tmpLen);
+            (long)sourceLen, (long)destLen,(long) tmpLen);
     */
-    if ( tmpLen != destLen )
-      Warning("unzip size differ: code %3d level %3d  ibuflen %ld ubuflen %ld",
-	      PDS_Parameter, PDS_Level2, (long) destLen, (long) tmpLen);
+    if (tmpLen != destLen)
+      Warning("unzip size differ: code %3d level %3d  ibuflen %ld ubuflen %ld", PDS_Parameter, PDS_Level2, (long) destLen,
+              (long) tmpLen);
 
-    if ( bds_nbits == 24 )
+    if (bds_nbits == 24)
       {
-	long nelem = tmpLen/3;
-	unsigned char *pbuf = (unsigned char*) Malloc(tmpLen);
-	for (long i = 0; i < nelem; ++i)
-	  {
-	    pbuf[3*i  ] = dest[        i];
-	    pbuf[3*i+1] = dest[  nelem+i];
-	    pbuf[3*i+2] = dest[2*nelem+i];
-	  }
-	memcpy(dest, pbuf, tmpLen);
-	Free(pbuf);
+        long nelem = tmpLen / 3;
+        unsigned char *pbuf = (unsigned char *) Malloc(tmpLen);
+        for (long i = 0; i < nelem; ++i)
+          {
+            pbuf[3 * i] = dest[i];
+            pbuf[3 * i + 1] = dest[nelem + i];
+            pbuf[3 * i + 2] = dest[2 * nelem + i];
+          }
+        memcpy(dest, pbuf, tmpLen);
+        Free(pbuf);
       }
 
     int bds_ubits = BDS_Flag & 15;
     BDS_Flag -= bds_ubits;
 
-    if ( (bdsLen%2) == 1 )
+    if ((bdsLen % 2) == 1)
       {
-	BDS_Flag += 8;
-	bds[bdsLen++] = 0;
+        BDS_Flag += 8;
+        bds[bdsLen++] = 0;
       }
 
     SetLen3(bds, 0, bdsLen);
 
     gribLen = (bds - dbuf) + bdsLen;
-    
+
     dbuf[gribLen++] = '7';
     dbuf[gribLen++] = '7';
     dbuf[gribLen++] = '7';
     dbuf[gribLen++] = '7';
 
-    if ( llarge )
+    if (llarge)
       {
-	long itemp;
+        long itemp;
         bdsLen = gribLen - 4;
-	/*
-	  If a very large product, the section 4 length field holds
-	  the number of bytes in the product after section 4 upto
-	  the end of the padding bytes.
-	  This is a fixup to get round the restriction on product lengths
-	  due to the count being only 24 bits. It is only possible because
-	  the (default) rounding for GRIB products is 120 bytes.
-	*/
-	while ( gribLen%120 ) dbuf[gribLen++] = 0;
-
-	if ( gribLen != (size_t)recLen )
-	  fprintf(stderr, "Internal problem, recLen and gribLen differ!\n");
-	
-	itemp = gribLen / (-120);
-	itemp = JP23SET - itemp + 1;
-	
-	SetLen3(dbuf, 4, itemp);
+        /*
+          If a very large product, the section 4 length field holds
+          the number of bytes in the product after section 4 upto
+          the end of the padding bytes.
+          This is a fixup to get round the restriction on product lengths
+          due to the count being only 24 bits. It is only possible because
+          the (default) rounding for GRIB products is 120 bytes.
+        */
+        while (gribLen % 120) dbuf[gribLen++] = 0;
+
+        if (gribLen != (size_t) recLen) fprintf(stderr, "Internal problem, recLen and gribLen differ!\n");
 
-	bdsLen = gribLen - bdsLen;
-	    
-	SetLen3(bds, 0, bdsLen);
+        itemp = gribLen / (-120);
+        itemp = JP23SET - itemp + 1;
+
+        SetLen3(dbuf, 4, itemp);
+
+        bdsLen = gribLen - bdsLen;
+
+        SetLen3(bds, 0, bdsLen);
       }
     else
       {
-	SetLen3(dbuf, 4, recLen);
+        SetLen3(dbuf, 4, recLen);
       }
     /*
     fprintf(stderr, "recLen, gribLen, bdsLen %d %d %d\n", recLen, gribLen, bdsLen);
     */
-    if ( llarge )
-      while ( gribLen%120 ) dbuf[gribLen++] = 0;
+    if (llarge)
+      while (gribLen % 120) dbuf[gribLen++] = 0;
     else
-      while ( gribLen & 7 ) dbuf[gribLen++] = 0;
+      while (gribLen & 7) dbuf[gribLen++] = 0;
     /*
     fprintf(stderr, "recLen, gribLen, bdsLen %d %d %d\n", recLen, gribLen, bdsLen);
     */
@@ -6233,19 +6175,21 @@ int  gribUnzip(unsigned char *dbuf, long dbufsize, unsigned char *sbuf, long sbu
   UNUSED(source);
   UNUSED(bdsLen);
   UNUSED(dest);
-  
-  if ( libszwarn )
+
+  if (libszwarn)
     {
       Warning("Decompression disabled, szlib not available!");
       libszwarn = 0;
     }
 #endif
 
-  return (int)gribLen;
+  return (int) gribLen;
 }
 #include <stdio.h>
 #include <math.h>
 
+// clang-format off
+
 
 static void
 scm0_double(double *pdl, double *pdr, double *pfl, double *pfr, int klg);
@@ -8031,29 +7975,29 @@ L900:
  * require-trailing-newline: t
  * End:
  */
-#include <string.h>
-
 
+// clang-format on
+#include <string.h>
 
-int gribVersion(unsigned char *is, size_t buffersize)
+int
+gribVersion(unsigned char *is, size_t buffersize)
 {
-  if ( buffersize < 8 )
-    Error("Buffer too small (current size %d)!", (int) buffersize);
+  if (buffersize < 8) Error("Buffer too small (current size %d)!", (int) buffersize);
 
   return GRIB_EDITION(is);
 }
 
-static 
-double GET_Real(unsigned char *grib)
+static double
+GET_Real(unsigned char *grib)
 {
-  int iexp  = GET_UINT1(grib[0]);
-  int imant = GET_UINT3(grib[1], grib[2], grib[3]);
+  int iexp = GET_UINT1(grib[0]);
+  int imant = (int) (GET_UINT3(grib[1], grib[2], grib[3]));
 
   return decfp2(iexp, imant);
 }
 
-static 
-int decodeIS(unsigned char *is, int *isec0, int *iret)
+static size_t
+decodeIS(unsigned char *is, int *isec0, int *iret)
 {
   // Octets 1 - 4 : The letters G R I B. Four 8 bit fields.
 
@@ -8067,13 +8011,13 @@ int decodeIS(unsigned char *is, int *isec0, int *iret)
   bool ltide = TIDE_START(is);
 
   // Data is not GRIB or pseudo-grib.
-  if ( lgrib == false && lbudg == false && ltide == false )
+  if (lgrib == false && lbudg == false && ltide == false)
     {
       *iret = 305;
       gprintf(__func__, "Input data is not GRIB or pseudo-grib.");
       gprintf(__func__, "Return code = %d", *iret);
     }
-  if ( lbudg || ltide )
+  if (lbudg || ltide)
     {
       *iret = 305;
       gprintf(__func__, "Pseudo-grib data unsupported.");
@@ -8081,79 +8025,79 @@ int decodeIS(unsigned char *is, int *isec0, int *iret)
     }
 
   // Octets 5 - 7 : Length of message. One 24 bit field.
-  ISEC0_GRIB_Len = GRIB1_SECLEN(is);
+  ISEC0_GRIB_Len = (int) (GRIB1_SECLEN(is));
 
   // Octet 8 : GRIB Edition Number. One 8 bit field.
   ISEC0_GRIB_Version = GRIB_EDITION(is);
 
-  if ( ISEC0_GRIB_Version > 1 )
-    Error("GRIB version %d unsupported!", ISEC0_GRIB_Version);
+  if (ISEC0_GRIB_Version > 1) Error("GRIB version %d unsupported!", ISEC0_GRIB_Version);
 
   int grib1offset = ISEC0_GRIB_Version * 4;
 
-  int isLen = 4 + grib1offset;
+  size_t isLen = 4 + (size_t) grib1offset;
 
   return isLen;
 }
 
-static 
-void decodePDS_ECMWF_local_Extension_1(unsigned char *pds, int *isec1)
+static void
+decodePDS_ECMWF_local_Extension_1(unsigned char *pds, int *isec1)
 {
-  isec1[36] = GET_UINT1(pds[40]);         /* extension identifier       */
-  isec1[37] = GET_UINT1(pds[41]);         /* Class                      */
-  isec1[38] = GET_UINT1(pds[42]);         /* Type                       */
-  isec1[39] = GET_UINT2(pds[43],pds[44]); /* Stream                     */
+  isec1[36] = GET_UINT1(pds[40]);                  /* extension identifier       */
+  isec1[37] = GET_UINT1(pds[41]);                  /* Class                      */
+  isec1[38] = GET_UINT1(pds[42]);                  /* Type                       */
+  isec1[39] = (int) (GET_UINT2(pds[43], pds[44])); /* Stream                     */
   /* isec1[40] = GET_UINT4(pds[45],pds[46],pds[47],pds[48]); */
-  memcpy((char*) &isec1[40], &pds[45], 4);
-  isec1[41] = GET_UINT1(pds[49]);         /* Forecast number            */
-  isec1[42] = GET_UINT1(pds[50]);         /* Total number of forecasts  */
+  memcpy((char *) &isec1[40], &pds[45], 4);
+  isec1[41] = GET_UINT1(pds[49]); /* Forecast number            */
+  isec1[42] = GET_UINT1(pds[50]); /* Total number of forecasts  */
 }
 
-static 
-void decodePDS_DWD_local_Extension_254(unsigned char *pds, int *isec1)
+static void
+decodePDS_DWD_local_Extension_254(unsigned char *pds, int *isec1)
 {
   isec1[36] = GET_UINT1(pds[40]); /* extension identifier */
-  for (int i = 0; i < 11; ++i) isec1[37+i] =  GET_UINT1(pds[41+i]);
+  for (int i = 0; i < 11; ++i) isec1[37 + i] = GET_UINT1(pds[41 + i]);
 
-  int isvn = GET_UINT2(pds[52],pds[53]);
-  
-  isec1[48] =  isvn % 0x8000;              /* DWD experiment identifier            */
-  isec1[49] =  isvn >> 15;                 /* DWD run type (0=main, 2=ass, 3=test) */
+  int isvn = (int) (GET_UINT2(pds[52], pds[53]));
+
+  isec1[48] = isvn % 0x8000; /* DWD experiment identifier            */
+  isec1[49] = isvn >> 15;    /* DWD run type (0=main, 2=ass, 3=test) */
 }
 
-static 
-void decodePDS_DWD_local_Extension_253(unsigned char *pds, int *isec1)
+static void
+decodePDS_DWD_local_Extension_253(unsigned char *pds, int *isec1)
 {
   isec1[36] = GET_UINT1(pds[40]); /* extension identifier */
-  for (int i = 0; i < 11; ++i) isec1[37+i] =  GET_UINT1(pds[41+i]);
+  for (int i = 0; i < 11; ++i) isec1[37 + i] = GET_UINT1(pds[41 + i]);
 
-  int isvn = GET_UINT2(pds[52],pds[53]);
-  
-  isec1[48] =  isvn % 0x8000;              /* DWD experiment identifier            */
-  isec1[49] =  isvn >> 15;                 /* DWD run type (0=main, 2=ass, 3=test) */
-  isec1[50] =  GET_UINT1(pds[54]);         /* User id, specified by table          */
-  isec1[51] =  GET_UINT2(pds[55],pds[56]); /* Experiment identifier                */
-  isec1[52] =  GET_UINT2(pds[57],pds[58]); /* Ensemble identification by table     */
-  isec1[53] =  GET_UINT2(pds[59],pds[60]); /* Number of ensemble members           */
-  isec1[54] =  GET_UINT2(pds[61],pds[62]); /* Actual number of ensemble member     */
-  isec1[55] =  GET_UINT1(pds[63]);         /* Model major version number           */
-  isec1[56] =  GET_UINT1(pds[64]);         /* Model minor version number           */
+  int isvn = (int) (GET_UINT2(pds[52], pds[53]));
+
+  isec1[48] = isvn % 0x8000;                       /* DWD experiment identifier            */
+  isec1[49] = isvn >> 15;                          /* DWD run type (0=main, 2=ass, 3=test) */
+  isec1[50] = GET_UINT1(pds[54]);                  /* User id, specified by table          */
+  isec1[51] = (int) (GET_UINT2(pds[55], pds[56])); /* Experiment identifier                */
+  isec1[52] = (int) (GET_UINT2(pds[57], pds[58])); /* Ensemble identification by table     */
+  isec1[53] = (int) (GET_UINT2(pds[59], pds[60])); /* Number of ensemble members           */
+  isec1[54] = (int) (GET_UINT2(pds[61], pds[62])); /* Actual number of ensemble member     */
+  isec1[55] = GET_UINT1(pds[63]);                  /* Model major version number           */
+  isec1[56] = GET_UINT1(pds[64]);                  /* Model minor version number           */
 }
 
-static 
-void decodePDS_MPIM_local_Extension_1(unsigned char *pds, int *isec1)
+static void
+decodePDS_MPIM_local_Extension_1(unsigned char *pds, int *isec1)
 {
-  isec1[36] = GET_UINT1(pds[40]);         /* extension identifier            */
-  isec1[37] = GET_UINT1(pds[41]);         /* type of ensemble forecast       */
-  isec1[38] = GET_UINT2(pds[42],pds[43]); /* individual ensemble member      */
-  isec1[39] = GET_UINT2(pds[44],pds[45]); /* number of forecasts in ensemble */
+  isec1[36] = GET_UINT1(pds[40]);                  /* extension identifier            */
+  isec1[37] = GET_UINT1(pds[41]);                  /* type of ensemble forecast       */
+  isec1[38] = (int) (GET_UINT2(pds[42], pds[43])); /* individual ensemble member      */
+  isec1[39] = (int) (GET_UINT2(pds[44], pds[45])); /* number of forecasts in ensemble */
 }
 
-static 
-int decodePDS(unsigned char *pds, int *isec0, int *isec1)
+static size_t
+decodePDS(unsigned char *pds, int *isec0, int *isec1)
 {
-  int pdsLen = PDS_Len;
+  size_t pdsLen = PDS_Len;
 
+  // clang-format off
   ISEC1_CodeTable      = PDS_CodeTable;
   ISEC1_CenterID       = PDS_CenterID;
   ISEC1_ModelID        = PDS_ModelID;
@@ -8184,7 +8128,7 @@ int decodePDS(unsigned char *pds, int *isec0, int *isec1)
     }
   else
     {
-      ISEC1_Level1 = PDS_Level;
+      ISEC1_Level1 = (int)(PDS_Level);
       ISEC1_Level2 = 0;
     }
 
@@ -8197,7 +8141,7 @@ int decodePDS(unsigned char *pds, int *isec0, int *isec1)
   ISEC1_TimePeriod1    = PDS_TimePeriod1;
   ISEC1_TimePeriod2    = PDS_TimePeriod2;
   ISEC1_TimeRange      = PDS_TimeRange;
-  ISEC1_AvgNum         = PDS_AvgNum;
+  ISEC1_AvgNum         = (int)(PDS_AvgNum);
   ISEC1_AvgMiss        = PDS_AvgMiss;
 
   if ( ISEC0_GRIB_Version == 1 )
@@ -8233,7 +8177,7 @@ int decodePDS(unsigned char *pds, int *isec0, int *isec1)
   ISEC1_LocalFLag = 0;
   if ( pdsLen > 28 )
     {
-      int localextlen = pdsLen-28;
+      size_t localextlen = pdsLen-28;
 
       if ( localextlen > 4000 )
 	{
@@ -8264,22 +8208,48 @@ int decodePDS(unsigned char *pds, int *isec0, int *isec1)
 	    }
 	  else
 	    {
-	      for (int i = 0; i < localextlen; ++i) isec1[24+i] = pds[28+i];
+	      for ( size_t i = 0; i < localextlen; i++ )
+                isec1[24+i] = pds[28+i];
 	    }
 	}
     }
+  // clang-format on
 
   return pdsLen;
 }
 
+static void
+gribPrintSec2_double(int *isec0, int *isec2, double *fsec2)
+{
+  gribPrintSec2DP(isec0, isec2, fsec2);
+}
+static void
+gribPrintSec3_double(int *isec0, int *isec3, double *fsec3)
+{
+  gribPrintSec3DP(isec0, isec3, fsec3);
+}
+static void
+gribPrintSec4_double(int *isec0, int *isec4, double *fsec4)
+{
+  gribPrintSec4DP(isec0, isec4, fsec4);
+}
+static void
+gribPrintSec2_float(int *isec0, int *isec2, float *fsec2)
+{
+  gribPrintSec2SP(isec0, isec2, fsec2);
+}
+static void
+gribPrintSec3_float(int *isec0, int *isec3, float *fsec3)
+{
+  gribPrintSec3SP(isec0, isec3, fsec3);
+}
+static void
+gribPrintSec4_float(int *isec0, int *isec4, float *fsec4)
+{
+  gribPrintSec4SP(isec0, isec4, fsec4);
+}
 
-static void gribPrintSec2_double(int *isec0, int *isec2, double *fsec2) {gribPrintSec2DP(isec0, isec2, fsec2);}
-static void gribPrintSec3_double(int *isec0, int *isec3, double *fsec3) {gribPrintSec3DP(isec0, isec3, fsec3);}
-static void gribPrintSec4_double(int *isec0, int *isec4, double *fsec4) {gribPrintSec4DP(isec0, isec4, fsec4);}
-static void gribPrintSec2_float(int *isec0, int *isec2, float *fsec2) {gribPrintSec2SP(isec0, isec2, fsec2);}
-static void gribPrintSec3_float(int *isec0, int *isec3, float *fsec3) {gribPrintSec3SP(isec0, isec3, fsec3);}
-static void gribPrintSec4_float(int *isec0, int *isec4, float *fsec4) {gribPrintSec4SP(isec0, isec4, fsec4);}
-
+// clang-format off
 
 #ifdef T
 #undef T
@@ -8747,7 +8717,7 @@ void TEMPLATE(decode_array,T)(const unsigned char *restrict igrib, long jlend, i
 #ifdef T
 
 static
-int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2, int *numGridVals)
+size_t TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2, size_t *numGridVals)
 {
   // int imisng = 0;
   bool ReducedGrid = false, VertCoorTab = false;
@@ -8786,17 +8756,19 @@ int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2,
   
   if ( ReducedGrid )
     {
-      const unsigned locnl = GDS_PVPL - 1 + (VertCoorTab * 4 * GDS_NV);
+      const unsigned locnl = GDS_PVPL - 1U + (VertCoorTab * 4U * GDS_NV);
       const unsigned jlenl = (gdsLen - locnl)  >> 1;
       if ( jlenl == GDS_NumLat )
 	{
-	  *numGridVals = 0;
 	  ISEC2_Reduced = true;
-	  for (unsigned i = 0; i < jlenl; ++i)
+          size_t accum = 0;
+	  for ( size_t i = 0; i < jlenl; ++i )
 	    {
-	      ISEC2_ReducedPoints(i) = GET_UINT2(gds[locnl+2*i], gds[locnl+2*i+1]);
-	      *numGridVals += ISEC2_ReducedPoints(i);
+              unsigned rpi = GET_UINT2(gds[locnl+2*i], gds[locnl+2*i+1]);
+              ISEC2_ReducedPoints(i) = (int)rpi;
+              accum += rpi;
 	    }
+          *numGridVals = accum;
 	}
       else
 	{
@@ -8812,20 +8784,20 @@ int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2,
        ISEC2_GridType == GRIB1_GTYPE_GAUSSIAN  ||
        ISEC2_GridType == GRIB1_GTYPE_LATLON_ROT )
     {
-      ISEC2_NumLat    = GDS_NumLat;
+      ISEC2_NumLat    = (int)(GDS_NumLat);
       if ( ! ReducedGrid )
 	{
-	  ISEC2_NumLon = GDS_NumLon;
-	  *numGridVals  = ISEC2_NumLon*ISEC2_NumLat;
+	  ISEC2_NumLon = (int)(GDS_NumLon);
+	  *numGridVals  = (size_t)ISEC2_NumLon*(size_t)ISEC2_NumLat;
 	}
       ISEC2_FirstLat  = GDS_FirstLat;
       ISEC2_FirstLon  = GDS_FirstLon;
       ISEC2_ResFlag   = GDS_ResFlag;
       ISEC2_LastLat   = GDS_LastLat;
       ISEC2_LastLon   = GDS_LastLon;
-      ISEC2_LonIncr   = GDS_LonIncr;
+      ISEC2_LonIncr   = (int)(GDS_LonIncr);
 
-      ISEC2_NumPar    = GDS_NumPar;
+      ISEC2_NumPar    = (int)GDS_NumPar;
       ISEC2_ScanFlag  = GDS_ScanFlag;
       if ( ISEC2_GridType == GRIB1_GTYPE_LATLON_ROT )
 	{
@@ -8851,9 +8823,9 @@ int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2,
     }
   else if ( ISEC2_GridType == GRIB1_GTYPE_LCC )
     {
-      ISEC2_NumLon    = GDS_NumLon;
-      ISEC2_NumLat    = GDS_NumLat;
-      *numGridVals  = ISEC2_NumLon*ISEC2_NumLat;
+      ISEC2_NumLon    = (int)(GDS_NumLon);
+      ISEC2_NumLat    = (int)(GDS_NumLat);
+      *numGridVals  = (size_t)ISEC2_NumLon*(size_t)ISEC2_NumLat;
       ISEC2_FirstLat  = GDS_FirstLat;
       ISEC2_FirstLon  = GDS_FirstLon;
       ISEC2_ResFlag   = GDS_ResFlag;
@@ -8869,12 +8841,12 @@ int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2,
     }
   else if ( ISEC2_GridType == GRIB1_GTYPE_SPECTRAL )
     {
-      ISEC2_PentaJ  = GDS_PentaJ; // Truncation
-      ISEC2_PentaK  = GDS_PentaK;
-      ISEC2_PentaM  = GDS_PentaM;
+      ISEC2_PentaJ  = (int)(GDS_PentaJ); // Truncation
+      ISEC2_PentaK  = (int)(GDS_PentaK);
+      ISEC2_PentaM  = (int)(GDS_PentaM);
       ISEC2_RepType = GDS_RepType;
       ISEC2_RepMode = GDS_RepMode;
-      *numGridVals  = (ISEC2_PentaJ+1)*(ISEC2_PentaJ+2);
+      *numGridVals  = ((size_t)ISEC2_PentaJ+1)*((size_t)ISEC2_PentaJ+2);
       isec2[ 6] = 0;
       isec2[ 7] = 0;
       isec2[ 8] = 0;
@@ -8884,24 +8856,25 @@ int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2,
     }
   else if ( ISEC2_GridType == GRIB1_GTYPE_GME )
     {
-      ISEC2_GME_NI2    = GDS_GME_NI2;
-      ISEC2_GME_NI3    = GDS_GME_NI3;
-      ISEC2_GME_ND     = GDS_GME_ND;
-      ISEC2_GME_NI     = GDS_GME_NI;
+      ISEC2_GME_NI2    = (int)(GDS_GME_NI2);
+      ISEC2_GME_NI3    = (int)(GDS_GME_NI3);
+      ISEC2_GME_ND     = (int)(GDS_GME_ND);
+      ISEC2_GME_NI     = (int)(GDS_GME_NI);
       ISEC2_GME_AFlag  = GDS_GME_AFlag;
       ISEC2_GME_LatPP  = GDS_GME_LatPP;
       ISEC2_GME_LonPP  = GDS_GME_LonPP;
       ISEC2_GME_LonMPL = GDS_GME_LonMPL;
       ISEC2_GME_BFlag  = GDS_GME_BFlag;
-      *numGridVals  = (ISEC2_GME_NI+1)*(ISEC2_GME_NI+1)*10;
+      *numGridVals  = ((size_t)ISEC2_GME_NI+1)*((size_t)ISEC2_GME_NI+1)*10;
       // iret = decodeGDS_TR(gds, gdspos, isec0, isec2, imisng);
     }
   else
     {
       static bool lwarn = true;
-      ISEC2_NumLon = GDS_NumLon;
-      ISEC2_NumLat = GDS_NumLat;
-      *numGridVals  = ISEC2_NumLon*ISEC2_NumLat;
+      unsigned nlon = GDS_NumLon, nlat = GDS_NumLat;
+      ISEC2_NumLon = (int)nlon;
+      ISEC2_NumLat = (int)nlat;
+      *numGridVals  = (size_t)nlon*(size_t)nlat;
       if ( lwarn )
         {
           lwarn = false;
@@ -8923,11 +8896,11 @@ int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2,
       if ( ISEC0_GRIB_Version  == 0 )
 	{
 	  locnv = 32;
-	  ISEC2_NumVCP = (gdsLen - 32) >> 2;
+	  ISEC2_NumVCP = ((int)gdsLen - 32) >> 2;
 	}
       else
 	{
-	  locnv = GDS_PVPL - 1;
+	  locnv = (int)GDS_PVPL - 1;
 	  ISEC2_NumVCP = GDS_NV;
 	}
 #if defined (SX)
@@ -8948,7 +8921,7 @@ int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2,
       for (int i = 0; i < ISEC2_NumVCP; ++i)
 	{
 	  const int iexp  = gds[locnv+4*i];
-	  const int imant = GET_UINT3(gds[locnv+4*i+1], gds[locnv+4*i+2], gds[locnv+4*i+3]);
+	  const int imant = (int)(GET_UINT3(gds[locnv+4*i+1], gds[locnv+4*i+2], gds[locnv+4*i+3]));
 	  fsec2[10+i] = (T)decfp2(iexp,imant);
 	}
 #endif
@@ -8964,9 +8937,9 @@ int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2,
 
 static
 void TEMPLATE(decodeBDS,T)(int decscale, unsigned char *bds, int *isec2, int *isec4, 
-                           T *fsec4, int fsec4len, int dfunc, int bdsLen, int numGridVals, int *iret)
+                           T *fsec4, int fsec4len, int dfunc, size_t bdsLen, size_t numGridVals, int *iret)
 {
-  int ioff = 0;
+  size_t ioff = 0;
   enum { bds_head = 11 };
   T zscale = 0.;
   T fmin = 0.;
@@ -9008,7 +8981,7 @@ void TEMPLATE(decodeBDS,T)(int decscale, unsigned char *bds, int *isec2, int *is
 
   // ----++++ number of unused bits at end of section)
 
-  const int bds_ubits = bds_flag & 0xF;
+  const unsigned bds_ubits = bds_flag & 0xF;
   
   // scale factor (2 bytes)
   const int jscale = BDS_BinScale;
@@ -9016,7 +8989,7 @@ void TEMPLATE(decodeBDS,T)(int decscale, unsigned char *bds, int *isec2, int *is
   // check for missing data indicators.
 
   const int iexp  = bds[ 6];
-  const int imant = GET_UINT3(bds[ 7], bds[ 8], bds[ 9]);
+  const int imant = (int)(GET_UINT3(bds[ 7], bds[ 8], bds[ 9]));
 
   const int imiss = (jscale == 0xFFFF && iexp == 0xFF && imant == 0xFFFFFF);
 
@@ -9030,11 +9003,12 @@ void TEMPLATE(decodeBDS,T)(int decscale, unsigned char *bds, int *isec2, int *is
 
   // get number of bits in each data value.
 
+  unsigned dvbits = BDS_NumBits;
   ISEC4_NumBits = BDS_NumBits;
 
   // octet number of start of packed data calculated from start of block 4 - 1
 
-  unsigned locnd = zoff + bds_head;
+  size_t locnd = zoff + bds_head;
 
   // if data is in spherical harmonic form, distinguish  between simple/complex packing (lcomplex = 0/1)
 
@@ -9075,21 +9049,21 @@ void TEMPLATE(decodeBDS,T)(int decscale, unsigned char *bds, int *isec2, int *is
 
 	  locnd += 4; // 2 + power
 	  locnd += 3; // j, k, m
-	  ioff   = (jup+1)*(jup+2);
+	  ioff   = ((size_t)jup+1)*((size_t)jup+2);
 
 	  if ( dfunc != 'J' )
-	    for (int i = 0; i < ioff; ++i)
+	    for ( size_t i = 0; i < ioff; ++i )
 	      {
 		if ( imiss )
-		  *fpdata++ = 0.0;
+		  fpdata[i] = 0.0;
 		else
 		  {
-		    const int iexp2  = (bds[locnd+4*i]);
-		    const int imant2 = GET_UINT3(bds[locnd+4*i+1], bds[locnd+4*i+2], bds[locnd+4*i+3]);
-		    *fpdata++ = (T)decfp2(iexp2,imant2);
+		    const int iexp2  = (int)(bds[locnd+4*i]);
+		    const int imant2 = (int)(GET_UINT3(bds[locnd+4*i+1], bds[locnd+4*i+2], bds[locnd+4*i+3]));
+		    fpdata[i] = (T)decfp2(iexp2,imant2);
 		  }
 	      }
-	  
+          fpdata += ioff;
 	  locnd += 4*ioff;  /* RealCoef */
 	}
     }
@@ -9109,9 +9083,9 @@ void TEMPLATE(decodeBDS,T)(int decscale, unsigned char *bds, int *isec2, int *is
   // Take into account that spherical harmonics can be packed
   // simple (lcomplex = 0) or complex (lcomplex = 1)
 
-  int jlend = bdsLen - locnd;
+  size_t jlend = bdsLen - locnd;
 
-  if ( ISEC4_NumBits == 0 )
+  if ( dvbits == 0 )
     {
       if ( jlend > 1 )
 	{
@@ -9129,24 +9103,23 @@ void TEMPLATE(decodeBDS,T)(int decscale, unsigned char *bds, int *isec2, int *is
 	  return;
 	}
 
-      jlend = numGridVals;
-      jlend -= ioff;
+      jlend = numGridVals - ioff;
     }
   else
     {
-      jlend = (int) (((long)jlend*8 - bds_ubits) / ISEC4_NumBits);
+      jlend = (jlend*8 - bds_ubits) / dvbits;
     }
 
-  ISEC4_NumValues        = jlend + ioff;
+  ISEC4_NumValues        = (int)(jlend + ioff);
   ISEC4_NumNonMissValues = 0;
 
   if ( lcompress )
     {
       const size_t len = ((size_t) ((bds[17]<<16)+(bds[18]<<8)+bds[19]));
 
-      ISEC4_NumValues = (int)(len*8/(size_t)ISEC4_NumBits);
+      ISEC4_NumValues = (int)(len*8/dvbits);
 
-      if ( lspherc ) ISEC4_NumValues += lcomplex ? ioff : 1;
+      if ( lspherc ) ISEC4_NumValues += lcomplex ? (int)ioff : 1;
     }
 
   if ( dfunc == 'J' ) return;
@@ -9162,12 +9135,12 @@ void TEMPLATE(decodeBDS,T)(int decscale, unsigned char *bds, int *isec2, int *is
       return;
     }
 
-  if ( imiss ) memset((char *)fpdata, 0, (size_t)jlend*sizeof(T));
+  if ( imiss ) memset((char *)fpdata, 0, jlend*sizeof(T));
   else
     {
       igrib += locnd;
 
-      TEMPLATE(decode_array,T)(igrib, jlend, ISEC4_NumBits, fmin, zscale, fpdata);
+      TEMPLATE(decode_array,T)(igrib, (long)jlend, ISEC4_NumBits, fmin, zscale, fpdata);
     }
 
   if ( lspherc && lcomplex )
@@ -9216,9 +9189,9 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
   // IS Indicator Section (Section 0)
   // ----------------------------------------------------------------
   UCHAR *is = (UCHAR *) &kgrib[0];
-  int isLen = decodeIS(is, isec0, iret);
+  size_t isLen = decodeIS(is, isec0, iret);
 
-  int gribLen = ISEC0_GRIB_Len;
+  size_t gribLen = (size_t)ISEC0_GRIB_Len;
 
   /*
     When decoding or calculating length, previous editions
@@ -9301,13 +9274,13 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
   // PDS Product Definition Section (Section 1)
   // ----------------------------------------------------------------
   UCHAR *pds = is + isLen;
-  int pdsLen = decodePDS(pds, isec0, isec1);
+  size_t pdsLen = decodePDS(pds, isec0, isec1);
 
   // ----------------------------------------------------------------
   // GDS Grid Description Section (Section 2)
   // ----------------------------------------------------------------
-  int numGridVals = 0;
-  int gdsLen = 0;
+  size_t numGridVals = 0;
+  size_t gdsLen = 0;
   const bool gdsIncluded = ISEC1_Sec2Or3Flag & 128;
   if ( gdsIncluded )
     {
@@ -9319,7 +9292,7 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
   // BMS Bit-Map Section Section (Section 3)
   // ----------------------------------------------------------------
   isec3[0] = 0;
-  int bmsLen = 0, bitmapSize = 0, imaskSize = 0;
+  size_t bmsLen = 0, bitmapSize = 0, imaskSize = 0;
   const bool bmsIncluded = ISEC1_Sec2Or3Flag & 64;
   if ( bmsIncluded )
     {
@@ -9334,7 +9307,7 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
   // BDS Binary Data Section (Section 4)
   // ----------------------------------------------------------------
   UCHAR *bds = is + isLen + pdsLen + gdsLen + bmsLen;
-  int bdsLen = BDS_Len;
+  unsigned bdsLen = BDS_Len;
   /*
     If a very large product, the section 4 length field holds
     the number of bytes in the product after section 4 upto
@@ -9348,8 +9321,8 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
     {
       gribLen &= JP23SET;
       gribLen *= 120;
-      ISEC0_GRIB_Len = gribLen;
-      bdsLen = correct_bdslen(bdsLen, gribLen, isLen+pdsLen+gdsLen+bmsLen);
+      ISEC0_GRIB_Len = (int)gribLen;
+      bdsLen = correct_bdslen(bdsLen, (int)gribLen, (long)(isLen+pdsLen+gdsLen+bmsLen));
     }
 
   TEMPLATE(decodeBDS,T)(ISEC1_DecScaleFactor, bds, isec2, isec4, fsec4, fsec4len, dfunc, bdsLen, numGridVals, iret);
@@ -9369,9 +9342,9 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
 	  }
 
       // ISEC4_NumNonMissValues = ISEC4_NumValues;
-      ISEC4_NumValues = bitmapSize;
+      ISEC4_NumValues = (int)bitmapSize;
 
-      if ( dfunc != 'J' || bitmapSize == ISEC4_NumNonMissValues )
+      if ( dfunc != 'J' || bitmapSize == (size_t)ISEC4_NumNonMissValues )
 	{
 	  GRIBPACK bitmap;
 	  /*
@@ -9402,7 +9375,7 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
 #ifdef __uxpch__
 #pragma loop novrec
 #endif
-	  for (int i = imaskSize/8-1; i >= 0; --i)
+	  for ( size_t i = imaskSize/8-1; i != (size_t)-1; --i )
 	    {
 	      bitmap = pbitmap[i];
 	      imask[i*8+0] = 1 & (bitmap >> 7);
@@ -9496,15 +9469,15 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
     }
 
   if ( ISEC0_GRIB_Version == 1 ) isLen = 8;
-  const int esLen = 4;
+  enum { esLen = 4 };
   gribLen = isLen + pdsLen + gdsLen + bmsLen + bdsLen + esLen;
 
-  if ( !llarge && ISEC0_GRIB_Len && ISEC0_GRIB_Len < gribLen )
-    Warning("Inconsistent length of GRIB message (grib_message_size=%d < grib_record_size=%d)!", ISEC0_GRIB_Len, gribLen);
+  if ( !llarge && ISEC0_GRIB_Len && (size_t)ISEC0_GRIB_Len < gribLen )
+    Warning("Inconsistent length of GRIB message (grib_message_size=%d < grib_record_size=%zu)!", ISEC0_GRIB_Len, gribLen);
 
-  ISEC0_GRIB_Len = gribLen;
+  ISEC0_GRIB_Len = (int)gribLen;
 
-  *kword = (int)(((size_t)gribLen + sizeof(int) - 1) / sizeof(int));
+  *kword = (int)((gribLen + sizeof(int) - 1) / sizeof(int));
 
   // ----------------------------------------------------------------
   // Section 9 . Abort/return to calling routine.
@@ -9552,7 +9525,7 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
 #ifdef T
 
 static
-int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2, int *numGridVals)
+size_t TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2, size_t *numGridVals)
 {
   // int imisng = 0;
   bool ReducedGrid = false, VertCoorTab = false;
@@ -9591,17 +9564,19 @@ int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2,
   
   if ( ReducedGrid )
     {
-      const unsigned locnl = GDS_PVPL - 1 + (VertCoorTab * 4 * GDS_NV);
+      const unsigned locnl = GDS_PVPL - 1U + (VertCoorTab * 4U * GDS_NV);
       const unsigned jlenl = (gdsLen - locnl)  >> 1;
       if ( jlenl == GDS_NumLat )
 	{
-	  *numGridVals = 0;
 	  ISEC2_Reduced = true;
-	  for (unsigned i = 0; i < jlenl; ++i)
+          size_t accum = 0;
+	  for ( size_t i = 0; i < jlenl; ++i )
 	    {
-	      ISEC2_ReducedPoints(i) = GET_UINT2(gds[locnl+2*i], gds[locnl+2*i+1]);
-	      *numGridVals += ISEC2_ReducedPoints(i);
+              unsigned rpi = GET_UINT2(gds[locnl+2*i], gds[locnl+2*i+1]);
+              ISEC2_ReducedPoints(i) = (int)rpi;
+              accum += rpi;
 	    }
+          *numGridVals = accum;
 	}
       else
 	{
@@ -9617,20 +9592,20 @@ int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2,
        ISEC2_GridType == GRIB1_GTYPE_GAUSSIAN  ||
        ISEC2_GridType == GRIB1_GTYPE_LATLON_ROT )
     {
-      ISEC2_NumLat    = GDS_NumLat;
+      ISEC2_NumLat    = (int)(GDS_NumLat);
       if ( ! ReducedGrid )
 	{
-	  ISEC2_NumLon = GDS_NumLon;
-	  *numGridVals  = ISEC2_NumLon*ISEC2_NumLat;
+	  ISEC2_NumLon = (int)(GDS_NumLon);
+	  *numGridVals  = (size_t)ISEC2_NumLon*(size_t)ISEC2_NumLat;
 	}
       ISEC2_FirstLat  = GDS_FirstLat;
       ISEC2_FirstLon  = GDS_FirstLon;
       ISEC2_ResFlag   = GDS_ResFlag;
       ISEC2_LastLat   = GDS_LastLat;
       ISEC2_LastLon   = GDS_LastLon;
-      ISEC2_LonIncr   = GDS_LonIncr;
+      ISEC2_LonIncr   = (int)(GDS_LonIncr);
 
-      ISEC2_NumPar    = GDS_NumPar;
+      ISEC2_NumPar    = (int)GDS_NumPar;
       ISEC2_ScanFlag  = GDS_ScanFlag;
       if ( ISEC2_GridType == GRIB1_GTYPE_LATLON_ROT )
 	{
@@ -9656,9 +9631,9 @@ int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2,
     }
   else if ( ISEC2_GridType == GRIB1_GTYPE_LCC )
     {
-      ISEC2_NumLon    = GDS_NumLon;
-      ISEC2_NumLat    = GDS_NumLat;
-      *numGridVals  = ISEC2_NumLon*ISEC2_NumLat;
+      ISEC2_NumLon    = (int)(GDS_NumLon);
+      ISEC2_NumLat    = (int)(GDS_NumLat);
+      *numGridVals  = (size_t)ISEC2_NumLon*(size_t)ISEC2_NumLat;
       ISEC2_FirstLat  = GDS_FirstLat;
       ISEC2_FirstLon  = GDS_FirstLon;
       ISEC2_ResFlag   = GDS_ResFlag;
@@ -9674,12 +9649,12 @@ int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2,
     }
   else if ( ISEC2_GridType == GRIB1_GTYPE_SPECTRAL )
     {
-      ISEC2_PentaJ  = GDS_PentaJ; // Truncation
-      ISEC2_PentaK  = GDS_PentaK;
-      ISEC2_PentaM  = GDS_PentaM;
+      ISEC2_PentaJ  = (int)(GDS_PentaJ); // Truncation
+      ISEC2_PentaK  = (int)(GDS_PentaK);
+      ISEC2_PentaM  = (int)(GDS_PentaM);
       ISEC2_RepType = GDS_RepType;
       ISEC2_RepMode = GDS_RepMode;
-      *numGridVals  = (ISEC2_PentaJ+1)*(ISEC2_PentaJ+2);
+      *numGridVals  = ((size_t)ISEC2_PentaJ+1)*((size_t)ISEC2_PentaJ+2);
       isec2[ 6] = 0;
       isec2[ 7] = 0;
       isec2[ 8] = 0;
@@ -9689,24 +9664,25 @@ int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2,
     }
   else if ( ISEC2_GridType == GRIB1_GTYPE_GME )
     {
-      ISEC2_GME_NI2    = GDS_GME_NI2;
-      ISEC2_GME_NI3    = GDS_GME_NI3;
-      ISEC2_GME_ND     = GDS_GME_ND;
-      ISEC2_GME_NI     = GDS_GME_NI;
+      ISEC2_GME_NI2    = (int)(GDS_GME_NI2);
+      ISEC2_GME_NI3    = (int)(GDS_GME_NI3);
+      ISEC2_GME_ND     = (int)(GDS_GME_ND);
+      ISEC2_GME_NI     = (int)(GDS_GME_NI);
       ISEC2_GME_AFlag  = GDS_GME_AFlag;
       ISEC2_GME_LatPP  = GDS_GME_LatPP;
       ISEC2_GME_LonPP  = GDS_GME_LonPP;
       ISEC2_GME_LonMPL = GDS_GME_LonMPL;
       ISEC2_GME_BFlag  = GDS_GME_BFlag;
-      *numGridVals  = (ISEC2_GME_NI+1)*(ISEC2_GME_NI+1)*10;
+      *numGridVals  = ((size_t)ISEC2_GME_NI+1)*((size_t)ISEC2_GME_NI+1)*10;
       // iret = decodeGDS_TR(gds, gdspos, isec0, isec2, imisng);
     }
   else
     {
       static bool lwarn = true;
-      ISEC2_NumLon = GDS_NumLon;
-      ISEC2_NumLat = GDS_NumLat;
-      *numGridVals  = ISEC2_NumLon*ISEC2_NumLat;
+      unsigned nlon = GDS_NumLon, nlat = GDS_NumLat;
+      ISEC2_NumLon = (int)nlon;
+      ISEC2_NumLat = (int)nlat;
+      *numGridVals  = (size_t)nlon*(size_t)nlat;
       if ( lwarn )
         {
           lwarn = false;
@@ -9728,11 +9704,11 @@ int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2,
       if ( ISEC0_GRIB_Version  == 0 )
 	{
 	  locnv = 32;
-	  ISEC2_NumVCP = (gdsLen - 32) >> 2;
+	  ISEC2_NumVCP = ((int)gdsLen - 32) >> 2;
 	}
       else
 	{
-	  locnv = GDS_PVPL - 1;
+	  locnv = (int)GDS_PVPL - 1;
 	  ISEC2_NumVCP = GDS_NV;
 	}
 #if defined (SX)
@@ -9753,7 +9729,7 @@ int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2,
       for (int i = 0; i < ISEC2_NumVCP; ++i)
 	{
 	  const int iexp  = gds[locnv+4*i];
-	  const int imant = GET_UINT3(gds[locnv+4*i+1], gds[locnv+4*i+2], gds[locnv+4*i+3]);
+	  const int imant = (int)(GET_UINT3(gds[locnv+4*i+1], gds[locnv+4*i+2], gds[locnv+4*i+3]));
 	  fsec2[10+i] = (T)decfp2(iexp,imant);
 	}
 #endif
@@ -9769,9 +9745,9 @@ int TEMPLATE(decodeGDS,T)(unsigned char  *gds, int *isec0, int *isec2, T *fsec2,
 
 static
 void TEMPLATE(decodeBDS,T)(int decscale, unsigned char *bds, int *isec2, int *isec4, 
-                           T *fsec4, int fsec4len, int dfunc, int bdsLen, int numGridVals, int *iret)
+                           T *fsec4, int fsec4len, int dfunc, size_t bdsLen, size_t numGridVals, int *iret)
 {
-  int ioff = 0;
+  size_t ioff = 0;
   enum { bds_head = 11 };
   T zscale = 0.;
   T fmin = 0.;
@@ -9813,7 +9789,7 @@ void TEMPLATE(decodeBDS,T)(int decscale, unsigned char *bds, int *isec2, int *is
 
   // ----++++ number of unused bits at end of section)
 
-  const int bds_ubits = bds_flag & 0xF;
+  const unsigned bds_ubits = bds_flag & 0xF;
   
   // scale factor (2 bytes)
   const int jscale = BDS_BinScale;
@@ -9821,7 +9797,7 @@ void TEMPLATE(decodeBDS,T)(int decscale, unsigned char *bds, int *isec2, int *is
   // check for missing data indicators.
 
   const int iexp  = bds[ 6];
-  const int imant = GET_UINT3(bds[ 7], bds[ 8], bds[ 9]);
+  const int imant = (int)(GET_UINT3(bds[ 7], bds[ 8], bds[ 9]));
 
   const int imiss = (jscale == 0xFFFF && iexp == 0xFF && imant == 0xFFFFFF);
 
@@ -9835,11 +9811,12 @@ void TEMPLATE(decodeBDS,T)(int decscale, unsigned char *bds, int *isec2, int *is
 
   // get number of bits in each data value.
 
+  unsigned dvbits = BDS_NumBits;
   ISEC4_NumBits = BDS_NumBits;
 
   // octet number of start of packed data calculated from start of block 4 - 1
 
-  unsigned locnd = zoff + bds_head;
+  size_t locnd = zoff + bds_head;
 
   // if data is in spherical harmonic form, distinguish  between simple/complex packing (lcomplex = 0/1)
 
@@ -9880,21 +9857,21 @@ void TEMPLATE(decodeBDS,T)(int decscale, unsigned char *bds, int *isec2, int *is
 
 	  locnd += 4; // 2 + power
 	  locnd += 3; // j, k, m
-	  ioff   = (jup+1)*(jup+2);
+	  ioff   = ((size_t)jup+1)*((size_t)jup+2);
 
 	  if ( dfunc != 'J' )
-	    for (int i = 0; i < ioff; ++i)
+	    for ( size_t i = 0; i < ioff; ++i )
 	      {
 		if ( imiss )
-		  *fpdata++ = 0.0;
+		  fpdata[i] = 0.0;
 		else
 		  {
-		    const int iexp2  = (bds[locnd+4*i]);
-		    const int imant2 = GET_UINT3(bds[locnd+4*i+1], bds[locnd+4*i+2], bds[locnd+4*i+3]);
-		    *fpdata++ = (T)decfp2(iexp2,imant2);
+		    const int iexp2  = (int)(bds[locnd+4*i]);
+		    const int imant2 = (int)(GET_UINT3(bds[locnd+4*i+1], bds[locnd+4*i+2], bds[locnd+4*i+3]));
+		    fpdata[i] = (T)decfp2(iexp2,imant2);
 		  }
 	      }
-	  
+          fpdata += ioff;
 	  locnd += 4*ioff;  /* RealCoef */
 	}
     }
@@ -9914,9 +9891,9 @@ void TEMPLATE(decodeBDS,T)(int decscale, unsigned char *bds, int *isec2, int *is
   // Take into account that spherical harmonics can be packed
   // simple (lcomplex = 0) or complex (lcomplex = 1)
 
-  int jlend = bdsLen - locnd;
+  size_t jlend = bdsLen - locnd;
 
-  if ( ISEC4_NumBits == 0 )
+  if ( dvbits == 0 )
     {
       if ( jlend > 1 )
 	{
@@ -9934,24 +9911,23 @@ void TEMPLATE(decodeBDS,T)(int decscale, unsigned char *bds, int *isec2, int *is
 	  return;
 	}
 
-      jlend = numGridVals;
-      jlend -= ioff;
+      jlend = numGridVals - ioff;
     }
   else
     {
-      jlend = (int) (((long)jlend*8 - bds_ubits) / ISEC4_NumBits);
+      jlend = (jlend*8 - bds_ubits) / dvbits;
     }
 
-  ISEC4_NumValues        = jlend + ioff;
+  ISEC4_NumValues        = (int)(jlend + ioff);
   ISEC4_NumNonMissValues = 0;
 
   if ( lcompress )
     {
       const size_t len = ((size_t) ((bds[17]<<16)+(bds[18]<<8)+bds[19]));
 
-      ISEC4_NumValues = (int)(len*8/(size_t)ISEC4_NumBits);
+      ISEC4_NumValues = (int)(len*8/dvbits);
 
-      if ( lspherc ) ISEC4_NumValues += lcomplex ? ioff : 1;
+      if ( lspherc ) ISEC4_NumValues += lcomplex ? (int)ioff : 1;
     }
 
   if ( dfunc == 'J' ) return;
@@ -9967,12 +9943,12 @@ void TEMPLATE(decodeBDS,T)(int decscale, unsigned char *bds, int *isec2, int *is
       return;
     }
 
-  if ( imiss ) memset((char *)fpdata, 0, (size_t)jlend*sizeof(T));
+  if ( imiss ) memset((char *)fpdata, 0, jlend*sizeof(T));
   else
     {
       igrib += locnd;
 
-      TEMPLATE(decode_array,T)(igrib, jlend, ISEC4_NumBits, fmin, zscale, fpdata);
+      TEMPLATE(decode_array,T)(igrib, (long)jlend, ISEC4_NumBits, fmin, zscale, fpdata);
     }
 
   if ( lspherc && lcomplex )
@@ -10021,9 +9997,9 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
   // IS Indicator Section (Section 0)
   // ----------------------------------------------------------------
   UCHAR *is = (UCHAR *) &kgrib[0];
-  int isLen = decodeIS(is, isec0, iret);
+  size_t isLen = decodeIS(is, isec0, iret);
 
-  int gribLen = ISEC0_GRIB_Len;
+  size_t gribLen = (size_t)ISEC0_GRIB_Len;
 
   /*
     When decoding or calculating length, previous editions
@@ -10106,13 +10082,13 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
   // PDS Product Definition Section (Section 1)
   // ----------------------------------------------------------------
   UCHAR *pds = is + isLen;
-  int pdsLen = decodePDS(pds, isec0, isec1);
+  size_t pdsLen = decodePDS(pds, isec0, isec1);
 
   // ----------------------------------------------------------------
   // GDS Grid Description Section (Section 2)
   // ----------------------------------------------------------------
-  int numGridVals = 0;
-  int gdsLen = 0;
+  size_t numGridVals = 0;
+  size_t gdsLen = 0;
   const bool gdsIncluded = ISEC1_Sec2Or3Flag & 128;
   if ( gdsIncluded )
     {
@@ -10124,7 +10100,7 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
   // BMS Bit-Map Section Section (Section 3)
   // ----------------------------------------------------------------
   isec3[0] = 0;
-  int bmsLen = 0, bitmapSize = 0, imaskSize = 0;
+  size_t bmsLen = 0, bitmapSize = 0, imaskSize = 0;
   const bool bmsIncluded = ISEC1_Sec2Or3Flag & 64;
   if ( bmsIncluded )
     {
@@ -10139,7 +10115,7 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
   // BDS Binary Data Section (Section 4)
   // ----------------------------------------------------------------
   UCHAR *bds = is + isLen + pdsLen + gdsLen + bmsLen;
-  int bdsLen = BDS_Len;
+  unsigned bdsLen = BDS_Len;
   /*
     If a very large product, the section 4 length field holds
     the number of bytes in the product after section 4 upto
@@ -10153,8 +10129,8 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
     {
       gribLen &= JP23SET;
       gribLen *= 120;
-      ISEC0_GRIB_Len = gribLen;
-      bdsLen = correct_bdslen(bdsLen, gribLen, isLen+pdsLen+gdsLen+bmsLen);
+      ISEC0_GRIB_Len = (int)gribLen;
+      bdsLen = correct_bdslen(bdsLen, (int)gribLen, (long)(isLen+pdsLen+gdsLen+bmsLen));
     }
 
   TEMPLATE(decodeBDS,T)(ISEC1_DecScaleFactor, bds, isec2, isec4, fsec4, fsec4len, dfunc, bdsLen, numGridVals, iret);
@@ -10174,9 +10150,9 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
 	  }
 
       // ISEC4_NumNonMissValues = ISEC4_NumValues;
-      ISEC4_NumValues = bitmapSize;
+      ISEC4_NumValues = (int)bitmapSize;
 
-      if ( dfunc != 'J' || bitmapSize == ISEC4_NumNonMissValues )
+      if ( dfunc != 'J' || bitmapSize == (size_t)ISEC4_NumNonMissValues )
 	{
 	  GRIBPACK bitmap;
 	  /*
@@ -10207,7 +10183,7 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
 #ifdef __uxpch__
 #pragma loop novrec
 #endif
-	  for (int i = imaskSize/8-1; i >= 0; --i)
+	  for ( size_t i = imaskSize/8-1; i != (size_t)-1; --i )
 	    {
 	      bitmap = pbitmap[i];
 	      imask[i*8+0] = 1 & (bitmap >> 7);
@@ -10301,15 +10277,15 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
     }
 
   if ( ISEC0_GRIB_Version == 1 ) isLen = 8;
-  const int esLen = 4;
+  enum { esLen = 4 };
   gribLen = isLen + pdsLen + gdsLen + bmsLen + bdsLen + esLen;
 
-  if ( !llarge && ISEC0_GRIB_Len && ISEC0_GRIB_Len < gribLen )
-    Warning("Inconsistent length of GRIB message (grib_message_size=%d < grib_record_size=%d)!", ISEC0_GRIB_Len, gribLen);
+  if ( !llarge && ISEC0_GRIB_Len && (size_t)ISEC0_GRIB_Len < gribLen )
+    Warning("Inconsistent length of GRIB message (grib_message_size=%d < grib_record_size=%zu)!", ISEC0_GRIB_Len, gribLen);
 
-  ISEC0_GRIB_Len = gribLen;
+  ISEC0_GRIB_Len = (int)gribLen;
 
-  *kword = (int)(((size_t)gribLen + sizeof(int) - 1) / sizeof(int));
+  *kword = (int)((gribLen + sizeof(int) - 1) / sizeof(int));
 
   // ----------------------------------------------------------------
   // Section 9 . Abort/return to calling routine.
@@ -10349,10 +10325,11 @@ void TEMPLATE(grib_decode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
  * mode: c
  * End:
  */
+// clang-format on
 
 // GRIB block 0 - indicator block
-static
-void encodeIS(GRIBPACK *lGrib, long *gribLen)
+static void
+encodeIS(GRIBPACK *lGrib, long *gribLen)
 {
   long z;
   // z = *gribLen;
@@ -10362,19 +10339,19 @@ void encodeIS(GRIBPACK *lGrib, long *gribLen)
   lGrib[2] = 'I';
   lGrib[3] = 'B';
 
-  // lGrib[4]-lGrib[6] contains full length of grib record. 
+  // lGrib[4]-lGrib[6] contains full length of grib record.
   // included before finished CODEGB
 
-  z = 7;   
-  Put1Byte(1); // grib version
+  z = 7;
+  Put1Byte(1);  // grib version
   z = 8;
 
   *gribLen = z;
 }
 
 // GRIB block 5 - end block
-static
-void encodeES(GRIBPACK *lGrib, long *gribLen, long bdsstart)
+static void
+encodeES(GRIBPACK *lGrib, long *gribLen, long bdsstart)
 {
   long z = *gribLen;
 
@@ -10389,40 +10366,40 @@ void encodeES(GRIBPACK *lGrib, long *gribLen, long bdsstart)
       // fprintf(stderr, "Abort: GRIB record too large (max = %d)!\n", JP23SET);
       // exit(1);
       /*
-	If a very large product, the section 4 length field holds
-	the number of bytes in the product after section 4 upto
-	the end of the padding bytes.
-	This is a fixup to get round the restriction on product lengths
-	due to the count being only 24 bits. It is only possible because
-	the (default) rounding for GRIB products is 120 bytes.
+        If a very large product, the section 4 length field holds
+        the number of bytes in the product after section 4 upto
+        the end of the padding bytes.
+        This is a fixup to get round the restriction on product lengths
+        due to the count being only 24 bits. It is only possible because
+        the (default) rounding for GRIB products is 120 bytes.
       */
-      while (z%120) lGrib[z++] = 0;
+      while (z % 120) lGrib[z++] = 0;
 
-      if (z > JP23SET*120)
-	{
-	  fprintf(stderr, "Abort: GRIB1 record too large (size = %ld; max = %d)!\n", z, JP23SET*120);
-	  exit(1);
-	}
+      if (z > JP23SET * 120)
+        {
+          fprintf(stderr, "Abort: GRIB1 record too large (size = %ld; max = %d)!\n", z, JP23SET * 120);
+          exit(1);
+        }
 
       long itemp = z / (-120);
       itemp = JP23SET - itemp + 1;
 
-      lGrib[4] = (GRIBPACK)(itemp >> 16);
-      lGrib[5] = (GRIBPACK)(itemp >>  8);
-      lGrib[6] = (GRIBPACK)itemp;
+      lGrib[4] = (GRIBPACK) (itemp >> 16);
+      lGrib[5] = (GRIBPACK) (itemp >> 8);
+      lGrib[6] = (GRIBPACK) itemp;
 
       bdslen = z - bdslen;
-      lGrib[bdsstart  ] = (GRIBPACK)(bdslen >> 16);
-      lGrib[bdsstart+1] = (GRIBPACK)(bdslen >>  8);
-      lGrib[bdsstart+2] = (GRIBPACK)bdslen;
+      lGrib[bdsstart] = (GRIBPACK) (bdslen >> 16);
+      lGrib[bdsstart + 1] = (GRIBPACK) (bdslen >> 8);
+      lGrib[bdsstart + 2] = (GRIBPACK) bdslen;
     }
   else
     {
-      lGrib[4] = (GRIBPACK)(z >> 16);
-      lGrib[5] = (GRIBPACK)(z >>  8);
-      lGrib[6] = (GRIBPACK)z;
+      lGrib[4] = (GRIBPACK) (z >> 16);
+      lGrib[5] = (GRIBPACK) (z >> 8);
+      lGrib[6] = (GRIBPACK) z;
 
-      while (z%8) lGrib[z++] = 0;
+      while (z % 8) lGrib[z++] = 0;
     }
 
   *gribLen = z;
@@ -10433,35 +10410,37 @@ void encodeES(GRIBPACK *lGrib, long *gribLen, long bdsstart)
 #define DWD_extension_253_len 38
 #define DWD_extension_254_len 26
 #define ECMWF_extension_1_len 24
-#define MPIM_extension_1_len  18
+#define MPIM_extension_1_len 18
 
-static
-long getLocalExtLen(int *isec1)
+static long
+getLocalExtLen(int *isec1)
 {
   long extlen = 0;
 
   if (ISEC1_LocalFLag)
     {
       if (ISEC1_CenterID == 78 || ISEC1_CenterID == 215 || ISEC1_CenterID == 250)
-	{
-	  if      (isec1[36] == 254) extlen = DWD_extension_254_len;
-	  else if (isec1[36] == 253) extlen = DWD_extension_253_len;
-	}
+        {
+          if (isec1[36] == 254)
+            extlen = DWD_extension_254_len;
+          else if (isec1[36] == 253)
+            extlen = DWD_extension_253_len;
+        }
       else if (ISEC1_CenterID == 98)
         {
-	  if (isec1[36] == 1) extlen = ECMWF_extension_1_len;
+          if (isec1[36] == 1) extlen = ECMWF_extension_1_len;
         }
       else if (ISEC1_CenterID == 252)
         {
-	  if (isec1[36] == 1) extlen = MPIM_extension_1_len;
+          if (isec1[36] == 1) extlen = MPIM_extension_1_len;
         }
     }
 
   return extlen;
 }
 
-static
-long getPdsLen(int *isec1)
+static long
+getPdsLen(int *isec1)
 {
   long pdslen = 28;
 
@@ -10470,101 +10449,101 @@ long getPdsLen(int *isec1)
   return pdslen;
 }
 
-static
-void encodePDS_DWD_local_Extension_254(GRIBPACK *lGrib, long *zs, int *isec1)
+static void
+encodePDS_DWD_local_Extension_254(GRIBPACK *lGrib, long *zs, int *isec1)
 {
   long z = *zs;
 
   const long localextlen = getLocalExtLen(isec1);
-  for (long i = 0; i < localextlen-2; i++) Put1Byte(isec1[24+i]);
+  for (long i = 0; i < localextlen - 2; i++) Put1Byte(isec1[24 + i]);
 
-  int isvn = isec1[49] << 15 | isec1[48]; // DWD experiment identifier
-  Put2Byte(isvn);             // DWD run type (0=main, 2=ass, 3=test)
+  int isvn = isec1[49] << 15 | isec1[48];  // DWD experiment identifier
+  Put2Byte(isvn);                          // DWD run type (0=main, 2=ass, 3=test)
 
   *zs = z;
 }
 
-static
-void encodePDS_DWD_local_Extension_253(GRIBPACK *lGrib, long *zs, int *isec1)
+static void
+encodePDS_DWD_local_Extension_253(GRIBPACK *lGrib, long *zs, int *isec1)
 {
   long z = *zs;
 
   const long localextlen = DWD_extension_254_len;
-  for (long i = 0; i < localextlen-2; i++) Put1Byte(isec1[24+i]);
+  for (long i = 0; i < localextlen - 2; i++) Put1Byte(isec1[24 + i]);
 
   int isvn = isec1[49] << 15 | isec1[48]; /* DWD experiment identifier    */
-  Put2Byte(isvn);             /* DWD run type (0=main, 2=ass, 3=test) */
-  Put1Byte(isec1[50]);        /* 55 User id, specified by table       */
-  Put2Byte(isec1[51]);        /* 56 Experiment identifier             */
-  Put2Byte(isec1[52]);        /* 58 Ensemble identification by table  */
-  Put2Byte(isec1[53]);        /* 60 Number of ensemble members        */
-  Put2Byte(isec1[54]);        /* 62 Actual number of ensemble member  */
-  Put1Byte(isec1[55]);        /* 64 Model major version number        */ 
-  Put1Byte(isec1[56]);        /* 65 Model minor version number        */ 
-  Put1Byte(0);                /* 66 Blank for even buffer length      */
+  Put2Byte(isvn);                         /* DWD run type (0=main, 2=ass, 3=test) */
+  Put1Byte(isec1[50]);                    /* 55 User id, specified by table       */
+  Put2Byte(isec1[51]);                    /* 56 Experiment identifier             */
+  Put2Byte(isec1[52]);                    /* 58 Ensemble identification by table  */
+  Put2Byte(isec1[53]);                    /* 60 Number of ensemble members        */
+  Put2Byte(isec1[54]);                    /* 62 Actual number of ensemble member  */
+  Put1Byte(isec1[55]);                    /* 64 Model major version number        */
+  Put1Byte(isec1[56]);                    /* 65 Model minor version number        */
+  Put1Byte(0);                            /* 66 Blank for even buffer length      */
 
   *zs = z;
 }
 
-static
-void encodePDS_ECMWF_local_Extension_1(GRIBPACK *lGrib, long *zs, int *isec1)
+static void
+encodePDS_ECMWF_local_Extension_1(GRIBPACK *lGrib, long *zs, int *isec1)
 {
   long z = *zs;
 
   const long localextlen = getLocalExtLen(isec1);
-  for (long i = 0; i < localextlen-12; i++) Put1Byte(isec1[24+i]);
-                              /* 12 bytes explicitly encoded below:         */
-  Put1Byte(isec1[36]);        /* ECMWF local GRIB use definition identifier */
-                              /*    1=MARS labelling or ensemble fcst. data */
-  Put1Byte(isec1[37]);        /* Class                                      */
-  Put1Byte(isec1[38]);        /* Type                                       */
-  Put2Byte(isec1[39]);        /* Stream                                     */
+  for (long i = 0; i < localextlen - 12; i++) Put1Byte(isec1[24 + i]);
+  /* 12 bytes explicitly encoded below:         */
+  Put1Byte(isec1[36]); /* ECMWF local GRIB use definition identifier */
+                       /*    1=MARS labelling or ensemble fcst. data */
+  Put1Byte(isec1[37]); /* Class                                      */
+  Put1Byte(isec1[38]); /* Type                                       */
+  Put2Byte(isec1[39]); /* Stream                                     */
 
   // Version number or experiment identifier
-  Put1Byte(((unsigned char*) &isec1[40])[0]);
-  Put1Byte(((unsigned char*) &isec1[40])[1]);
-  Put1Byte(((unsigned char*) &isec1[40])[2]);
-  Put1Byte(((unsigned char*) &isec1[40])[3]);
+  Put1Byte(((unsigned char *) &isec1[40])[0]);
+  Put1Byte(((unsigned char *) &isec1[40])[1]);
+  Put1Byte(((unsigned char *) &isec1[40])[2]);
+  Put1Byte(((unsigned char *) &isec1[40])[3]);
 
-  Put1Byte(isec1[41]);        /* Ensemble forecast number                   */
-  Put1Byte(isec1[42]);        /* Total number of forecasts in ensemble      */
-  Put1Byte(0);                /* (Spare)                                    */
+  Put1Byte(isec1[41]); /* Ensemble forecast number                   */
+  Put1Byte(isec1[42]); /* Total number of forecasts in ensemble      */
+  Put1Byte(0);         /* (Spare)                                    */
 
   *zs = z;
 }
 
-static
-void encodePDS_MPIM_local_Extension_1(GRIBPACK *lGrib, long *zs, int *isec1)
+static void
+encodePDS_MPIM_local_Extension_1(GRIBPACK *lGrib, long *zs, int *isec1)
 {
   long z = *zs;
 
   const long localextlen = getLocalExtLen(isec1);
-  for (long i = 0; i < localextlen-6; i++) Put1Byte(isec1[24+i]);
-                              /* 6 bytes explicitly encoded below:          */
-  Put1Byte(isec1[36]);        /* MPIM local GRIB use definition identifier  */
-                              /*    (extension identifier)                  */
-  Put1Byte(isec1[37]);        /* type of ensemble forecast                  */
-  Put2Byte(isec1[38]);        /* individual ensemble member                 */
-  Put2Byte(isec1[39]);        /* number of forecasts in ensemble            */
+  for (long i = 0; i < localextlen - 6; i++) Put1Byte(isec1[24 + i]);
+  /* 6 bytes explicitly encoded below:          */
+  Put1Byte(isec1[36]); /* MPIM local GRIB use definition identifier  */
+                       /*    (extension identifier)                  */
+  Put1Byte(isec1[37]); /* type of ensemble forecast                  */
+  Put2Byte(isec1[38]); /* individual ensemble member                 */
+  Put2Byte(isec1[39]); /* number of forecasts in ensemble            */
 
   *zs = z;
 }
 
 // GRIB BLOCK 1 - PRODUCT DESCRIPTION SECTION
-static
-void encodePDS(GRIBPACK *lpds, long pdsLen, int *isec1)
+static void
+encodePDS(GRIBPACK *lpds, long pdsLen, int *isec1)
 {
   GRIBPACK *lGrib = lpds;
   long z = 0;
   int ival;
 
   int century = ISEC1_Century;
-  int year    = ISEC1_Year;
+  int year = ISEC1_Year;
 
-  if ( century < 0 )
+  if (century < 0)
     {
       century = -century;
-      year    = -year;
+      year = -year;
     }
 
   Put3Byte(pdsLen);               /*  0 Length of Block 1        */
@@ -10575,6 +10554,7 @@ void encodePDS(GRIBPACK *lpds, long pdsLen, int *isec1)
   Put1Byte(ISEC1_Sec2Or3Flag);    /*  7 Block 2 included         */
   Put1Byte(ISEC1_Parameter);      /*  8 Parameter Code           */
   Put1Byte(ISEC1_LevelType);      /*  9 Type of level            */
+  // clang-format off
   if ( (ISEC1_LevelType !=  20) &&
        (ISEC1_LevelType != GRIB1_LTYPE_99)           &&
        (ISEC1_LevelType != GRIB1_LTYPE_ISOBARIC)     &&
@@ -10599,26 +10579,26 @@ void encodePDS(GRIBPACK *lpds, long pdsLen, int *isec1)
     {
       Put2Byte(ISEC1_Level1);     /* 10 Level                    */    
     }
+  // clang-format on
 
-  Put1Int(year);                  /* 12 Year of Century          */
-  Put1Byte(ISEC1_Month);          /* 13 Month                    */
-  Put1Byte(ISEC1_Day);            /* 14 Day                      */
-  Put1Byte(ISEC1_Hour);           /* 15 Hour                     */
-  Put1Byte(ISEC1_Minute);         /* 16 Minute                   */
+  Put1Int(year);          /* 12 Year of Century          */
+  Put1Byte(ISEC1_Month);  /* 13 Month                    */
+  Put1Byte(ISEC1_Day);    /* 14 Day                      */
+  Put1Byte(ISEC1_Hour);   /* 15 Hour                     */
+  Put1Byte(ISEC1_Minute); /* 16 Minute                   */
 
-  Put1Byte(ISEC1_TimeUnit);       /* 17 Time unit                */
-  if ( ISEC1_TimeRange == 10 )
+  Put1Byte(ISEC1_TimeUnit); /* 17 Time unit                */
+  if (ISEC1_TimeRange == 10)
     {
       Put1Byte(ISEC1_TimePeriod1);
       Put1Byte(ISEC1_TimePeriod2);
     }
-  else if ( ISEC1_TimeRange == 113 || ISEC1_TimeRange ==   0 )
+  else if (ISEC1_TimeRange == 113 || ISEC1_TimeRange == 0)
     {
       Put1Byte(ISEC1_TimePeriod1);
       Put1Byte(0);
     }
-  else if ( ISEC1_TimeRange ==   5 || ISEC1_TimeRange ==   4 || 
-	    ISEC1_TimeRange ==   3 || ISEC1_TimeRange ==   2 )
+  else if (ISEC1_TimeRange == 5 || ISEC1_TimeRange == 4 || ISEC1_TimeRange == 3 || ISEC1_TimeRange == 2)
     {
       Put1Byte(ISEC1_TimePeriod1);
       Put1Byte(ISEC1_TimePeriod2);
@@ -10626,40 +10606,42 @@ void encodePDS(GRIBPACK *lpds, long pdsLen, int *isec1)
   else
     {
       Put1Byte(0);
-      Put1Byte(0); 
+      Put1Byte(0);
     }
-  Put1Byte(ISEC1_TimeRange);      /* 20 Timerange flag           */
-  Put2Byte(ISEC1_AvgNum);         /* 21 Average                  */
+  Put1Byte(ISEC1_TimeRange); /* 20 Timerange flag           */
+  Put2Byte(ISEC1_AvgNum);    /* 21 Average                  */
 
-  Put1Byte(ISEC1_AvgMiss);        /* 23 Missing from averages    */
-  Put1Byte(century);              /* 24 Century                  */
-  Put1Byte(ISEC1_SubCenterID);    /* 25 Subcenter                */
-  Put2Int(ISEC1_DecScaleFactor);  /* 26 Decimal scale factor     */
+  Put1Byte(ISEC1_AvgMiss);       /* 23 Missing from averages    */
+  Put1Byte(century);             /* 24 Century                  */
+  Put1Byte(ISEC1_SubCenterID);   /* 25 Subcenter                */
+  Put2Int(ISEC1_DecScaleFactor); /* 26 Decimal scale factor     */
 
-  if ( ISEC1_LocalFLag )
+  if (ISEC1_LocalFLag)
     {
-      if ( ISEC1_CenterID == 78 || ISEC1_CenterID == 215 || ISEC1_CenterID == 250 )
-	{
-	  if      ( isec1[36] == 254 ) encodePDS_DWD_local_Extension_254(lGrib, &z, isec1);
-	  else if ( isec1[36] == 253 ) encodePDS_DWD_local_Extension_253(lGrib, &z, isec1);
-	}
-      else if ( ISEC1_CenterID == 98 )
-	{
-	  if ( isec1[36] == 1 ) encodePDS_ECMWF_local_Extension_1(lGrib, &z, isec1);
-	}
-      else if ( ISEC1_CenterID == 252 )
-	{
-	  if ( isec1[36] == 1 ) encodePDS_MPIM_local_Extension_1(lGrib, &z, isec1);
-	}
+      if (ISEC1_CenterID == 78 || ISEC1_CenterID == 215 || ISEC1_CenterID == 250)
+        {
+          if (isec1[36] == 254)
+            encodePDS_DWD_local_Extension_254(lGrib, &z, isec1);
+          else if (isec1[36] == 253)
+            encodePDS_DWD_local_Extension_253(lGrib, &z, isec1);
+        }
+      else if (ISEC1_CenterID == 98)
+        {
+          if (isec1[36] == 1) encodePDS_ECMWF_local_Extension_1(lGrib, &z, isec1);
+        }
+      else if (ISEC1_CenterID == 252)
+        {
+          if (isec1[36] == 1) encodePDS_MPIM_local_Extension_1(lGrib, &z, isec1);
+        }
       else
-	{
-	  const long localextlen = getLocalExtLen(isec1);
-	  for (long i = 0; i < localextlen; i++) Put1Byte(isec1[24+i]);
-	}
+        {
+          const long localextlen = getLocalExtLen(isec1);
+          for (long i = 0; i < localextlen; i++) Put1Byte(isec1[24 + i]);
+        }
     }
 }
 
-
+// clang-format off
 
 
 #ifdef T
@@ -11841,7 +11823,7 @@ int TEMPLATE(encodeBDS,T)(GRIBPACK *lGrib, long *gribLen, int decscale, int *ise
 
   const long unused_bits = blockLength*8 - (*datstart)*8 - nbpv*(datasize - PackStart);
 
-  Flag += unused_bits;
+  Flag += (int)unused_bits;
 
 
   // Adjust number of bits per value if full integer length to avoid hitting most significant bit (sign bit).
@@ -12413,7 +12395,7 @@ int TEMPLATE(encodeBDS,T)(GRIBPACK *lGrib, long *gribLen, int decscale, int *ise
 
   const long unused_bits = blockLength*8 - (*datstart)*8 - nbpv*(datasize - PackStart);
 
-  Flag += unused_bits;
+  Flag += (int)unused_bits;
 
 
   // Adjust number of bits per value if full integer length to avoid hitting most significant bit (sign bit).
@@ -12646,14 +12628,16 @@ void TEMPLATE(grib_encode,T)(int *isec0, int *isec1, int *isec2, T *fsec2, int *
  * mode: c
  * End:
  */
+// clang-format on
 
 void encode_dummy(void);
-void encode_dummy(void)
+void
+encode_dummy(void)
 {
   (void) encode_array_unrolled_double(0, 0, 0, NULL, NULL, 0, 0, NULL);
   (void) encode_array_unrolled_float(0, 0, 0, NULL, NULL, 0, 0, NULL);
 }
-static const char grb_libvers[] = "2.1.1";
+static const char grb_libvers[] = "2.2.0";
 const char *
 cgribexLibraryVersion(void)
 {
@@ -12663,5 +12647,3 @@ cgribexLibraryVersion(void)
 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5)
 #pragma GCC diagnostic pop
 #endif
-
-// clang-format on
diff --git a/src/file.c b/src/file.c
index 58c2c10a558799cee48c4caef92f21b76a8fbe04..2086da8a5643a461bf96f56909a7f5dba5fc8578 100644
--- a/src/file.c
+++ b/src/file.c
@@ -1,10 +1,16 @@
+// strdup() from string.h
+#ifdef __STDC_ALLOC_LIB__
+#define __STDC_WANT_LIB_EXT2__ 1
+#else
+#undef _POSIX_C_SOURCE
+#define _POSIX_C_SOURCE 200809L
+#endif
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif
 
 #include <assert.h>
 #include <ctype.h>
@@ -33,14 +39,14 @@
 #define O_BINARY 0
 #endif
 
-#ifndef strdup
-char *strdup(const char *s);
-#endif
-
 #ifdef HAVE_MMAP
 #include <sys/mman.h>  // mmap() is defined in this header
 #endif
 
+#ifndef SSIZE_MAX
+#define SSIZE_MAX LONG_MAX
+#endif
+
 #define MAX_FILES 8192
 static int _file_max = MAX_FILES;
 
@@ -52,7 +58,7 @@ static bool _file_init = false;
 #include <pthread.h>
 
 static pthread_once_t _file_init_thread = PTHREAD_ONCE_INIT;
-static pthread_mutex_t _file_mutex;
+static pthread_mutex_t _file_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 #define FILE_LOCK() pthread_mutex_lock(&_file_mutex)
 #define FILE_UNLOCK() pthread_mutex_unlock(&_file_mutex)
@@ -432,10 +438,7 @@ fileGetPos(int fileID)
   bfile_t *fileptr = file_to_pointer(fileID);
   if (fileptr)
     {
-      if (fileptr->mode == 'r' && fileptr->type == FILE_TYPE_OPEN)
-        filepos = fileptr->position;
-      else
-        filepos = ftell(fileptr->fp);
+      filepos = (fileptr->mode == 'r' && fileptr->type == FILE_TYPE_OPEN) ? fileptr->position : ftell(fileptr->fp);
     }
 
   if (FileDebug) Message("Position %ld", filepos);
@@ -449,11 +452,7 @@ file_set_buffer_pos(bfile_t *fileptr)
   off_t position = fileptr->position;
   if (position < fileptr->bufferStart || position > fileptr->bufferEnd)
     {
-      if (fileptr->bufferType == FILE_BUFTYPE_STD)
-        fileptr->bufferPos = position;
-      else
-        fileptr->bufferPos = position - position % file_pagesize();
-
+      fileptr->bufferPos = (fileptr->bufferType == FILE_BUFTYPE_STD) ? position : position - position % file_pagesize();
       fileptr->bufferCnt = 0;
       fileptr->bufferPtr = NULL;
 
@@ -520,8 +519,7 @@ fileSetPos(int fileID, off_t offset, int whence)
   else
     status = fseek(fileptr->fp, offset, whence);
 
-  if (fileptr->position < fileptr->size)
-    if ((fileptr->flag & FILE_EOF) != 0) fileptr->flag -= FILE_EOF;
+  if ((fileptr->position < fileptr->size) && ((fileptr->flag & FILE_EOF) != 0)) fileptr->flag -= FILE_EOF;
 
   return status;
 }
@@ -620,14 +618,9 @@ file_getenv(const char *envName)
 static void
 file_initialize(void)
 {
-#ifdef HAVE_LIBPTHREAD
-  // initialize global API mutex lock
-  pthread_mutex_init(&_file_mutex, NULL);
-#endif
-
   long value;
 
-  FileInfo = file_getenv("FILE_INFO") > 0;
+  FileInfo = (file_getenv("FILE_INFO") > 0);
 
   value = file_getenv("FILE_DEBUG");
   if (value >= 0) FileDebug = (int) value;
@@ -1062,7 +1055,6 @@ int
 fileClose_serial(int fileID)
 #endif
 {
-  int ret;
   double rout = 0;
 
   bfile_t *fileptr = file_to_pointer(fileID);
@@ -1130,21 +1122,18 @@ fileClose_serial(int fileID)
 
   if (fileptr->type == FILE_TYPE_FOPEN)
     {
-      ret = fclose(fileptr->fp);
-      if (ret == EOF) SysError("EOF returned for close of %s!", name);
+      if (fclose(fileptr->fp) == EOF) SysError("EOF returned for close of %s!", name);
     }
   else
     {
 #ifdef HAVE_MMAP
       if (fileptr->buffer && fileptr->mappedSize)
         {
-          ret = munmap(fileptr->buffer, fileptr->mappedSize);
-          if (ret == -1) SysError("munmap error for close %s", fileptr->name);
+          if (munmap(fileptr->buffer, fileptr->mappedSize) == -1) SysError("munmap error for close %s", fileptr->name);
           fileptr->buffer = NULL;
         }
 #endif
-      ret = close(fileptr->fd);
-      if (ret == -1) SysError("EOF returned for close of %s!", name);
+      if (close(fileptr->fd) == -1) SysError("EOF returned for close of %s!", name);
     }
 
   if (fileptr->name) free((void *) fileptr->name);
diff --git a/src/get_num_missvals.c b/src/get_num_missvals.c
index 7f4b15e538754aaa2fbe4bba68b857de54997db2..5811799e930a9fdec9833d3cdddd19017a68c18c 100644
--- a/src/get_num_missvals.c
+++ b/src/get_num_missvals.c
@@ -4,7 +4,7 @@
 size_t
 get_num_missvalsSP(size_t size, float *data, float missval)
 {
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
 
   if (DBL_IS_NAN(missval))
     {
@@ -12,7 +12,7 @@ get_num_missvalsSP(size_t size, float *data, float missval)
         if (DBL_IS_EQUAL(data[i], missval))
           {
             data[i] = missval;
-            nmiss++;
+            numMissVals++;
           }
     }
   else
@@ -21,17 +21,17 @@ get_num_missvalsSP(size_t size, float *data, float missval)
         if (IS_EQUAL(data[i], missval))
           {
             data[i] = missval;
-            nmiss++;
+            numMissVals++;
           }
     }
 
-  return nmiss;
+  return numMissVals;
 }
 
 size_t
 get_num_missvalsDP(size_t size, double *data, double missval)
 {
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
 
   if (DBL_IS_NAN(missval))
     {
@@ -39,7 +39,7 @@ get_num_missvalsDP(size_t size, double *data, double missval)
         if (DBL_IS_EQUAL(data[i], missval) || DBL_IS_EQUAL(data[i], (float) missval))
           {
             data[i] = missval;
-            nmiss++;
+            numMissVals++;
           }
     }
   else
@@ -48,17 +48,17 @@ get_num_missvalsDP(size_t size, double *data, double missval)
         if (IS_EQUAL(data[i], missval) || IS_EQUAL(data[i], (float) missval))
           {
             data[i] = missval;
-            nmiss++;
+            numMissVals++;
           }
     }
 
-  return nmiss;
+  return numMissVals;
 }
 
 size_t
 get_cplx_num_missvalsSP(size_t size, float *data, float missval)
 {
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
 
   if (DBL_IS_NAN(missval))
     {
@@ -66,7 +66,7 @@ get_cplx_num_missvalsSP(size_t size, float *data, float missval)
         if (DBL_IS_EQUAL(data[i], missval))
           {
             data[i] = missval;
-            nmiss++;
+            numMissVals++;
           }
     }
   else
@@ -75,17 +75,17 @@ get_cplx_num_missvalsSP(size_t size, float *data, float missval)
         if (IS_EQUAL(data[i], missval))
           {
             data[i] = missval;
-            nmiss++;
+            numMissVals++;
           }
     }
 
-  return nmiss;
+  return numMissVals;
 }
 
 size_t
 get_cplx_num_missvalsDP(size_t size, double *data, double missval)
 {
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
 
   if (DBL_IS_NAN(missval))
     {
@@ -93,7 +93,7 @@ get_cplx_num_missvalsDP(size_t size, double *data, double missval)
         if (DBL_IS_EQUAL(data[i], missval) || DBL_IS_EQUAL(data[i], (float) missval))
           {
             data[i] = missval;
-            nmiss++;
+            numMissVals++;
           }
     }
   else
@@ -102,9 +102,9 @@ get_cplx_num_missvalsDP(size_t size, double *data, double missval)
         if (IS_EQUAL(data[i], missval) || IS_EQUAL(data[i], (float) missval))
           {
             data[i] = missval;
-            nmiss++;
+            numMissVals++;
           }
     }
 
-  return nmiss;
+  return numMissVals;
 }
diff --git a/src/getline.c b/src/getline.c
index 7a29503abacfb3ea6df00b2f379d9728e52db3a6..8b06e5f627326f99899449e4c177c65b870119f8 100644
--- a/src/getline.c
+++ b/src/getline.c
@@ -32,6 +32,10 @@
 #include <string.h>
 #include <limits.h>
 
+#ifndef SSIZE_MAX
+#define SSIZE_MAX LONG_MAX
+#endif
+
 /* read until end of line or end of file */
 ssize_t
 getline(char **linebuf, size_t *linebuf_size, FILE *fp)
diff --git a/src/grb_read.c b/src/grb_read.c
index 06d6c3458baf6e85b07eeda3ae0bccbbb1f017a9..70b793409d8743d3404e1ceb356c17fcadf5cf7e 100644
--- a/src/grb_read.c
+++ b/src/grb_read.c
@@ -20,7 +20,7 @@
 
 static int
 grb_decode(int filetype, int memType, int datatype, void *cgribexp, void *gribbuffer, size_t gribsize, void *data, size_t datasize,
-           int unreduced, size_t *nmiss, double missval)
+           int unreduced, size_t *numMissVals, double missval)
 {
   int status = 0;
 
@@ -31,7 +31,7 @@ grb_decode(int filetype, int memType, int datatype, void *cgribexp, void *gribbu
       extern int cdiNAdditionalGRIBKeys;
       if (cdiNAdditionalGRIBKeys > 0) Error("CGRIBEX decode does not support reading of additional GRIB keys!");
 #endif
-      status = cgribexDecode(memType, cgribexp, gribbuffer, gribsize, data, datasize, unreduced, nmiss, missval);
+      status = cgribexDecode(memType, cgribexp, gribbuffer, gribsize, data, datasize, unreduced, numMissVals, missval);
     }
   else
 #endif
@@ -43,7 +43,7 @@ grb_decode(int filetype, int memType, int datatype, void *cgribexp, void *gribbu
 
       // if (useFloatInterface) printf("gribapi read: useFloatInterface\n");
 
-      status = gribapiDecode(memTypeX, gribbuffer, gribsize, datap, datasize, unreduced, nmiss, missval);
+      status = gribapiDecode(memTypeX, gribbuffer, gribsize, datap, datasize, unreduced, numMissVals, missval);
 
       if (!useFloatInterface && memType == MEMTYPE_FLOAT)
         {
@@ -112,7 +112,7 @@ typedef struct JobArgs
 {
   int recID, tsID, *outZip, filetype, memType, datatype, unreduced;
   void *cgribexp, *gribbuffer, *data;
-  size_t recsize, gridsize, nmiss;
+  size_t recsize, gridsize, numMissVals;
   double missval;
 } JobArgs;
 
@@ -122,7 +122,7 @@ grb_decode_record(void *untypedArgs)
   JobArgs *args = (JobArgs *) untypedArgs;
   *args->outZip = grib1_unzip_record(args->gribbuffer, &args->recsize);
   grb_decode(args->filetype, args->memType, args->datatype, args->cgribexp, args->gribbuffer, args->recsize, args->data,
-             args->gridsize, args->unreduced, &args->nmiss, args->missval);
+             args->gridsize, args->unreduced, &args->numMissVals, args->missval);
   return 0;
 }
 
@@ -144,10 +144,11 @@ grb_read_raw_data(stream_t *streamptr, int tsID, int recID, int memType, void *g
   if (streamptr->protocol == CDI_PROTOCOL_FDB)
     {
 #ifdef HAVE_LIBFDB5
-      void *fdbItem = streamptr->tsteps[tsID].records[recID].fdbItem;
-      if (!fdbItem) Error("fdbItem not available!");
+      int fdbItemIndex = streamptr->tsteps[tsID].records[recID].fdbItemIndex;
+      if (fdbItemIndex == -1) Error("fdbItem not available!");
 
-      recsize = fdb_read_record(streamptr->protocolData, fdbItem, &(streamptr->record->buffersize), &gribbuffer);
+      recsize = cdi_fdb_read_record(streamptr->protocolData, &(streamptr->fdbKeyValueList[fdbItemIndex]),
+                                    &(streamptr->record->buffersize), &gribbuffer);
 #endif
     }
   else
@@ -177,7 +178,7 @@ grb_read_raw_data(stream_t *streamptr, int tsID, int recID, int memType, void *g
     .data = data,
     .recsize = recsize,
     .gridsize = gridsize,
-    .nmiss = 0,
+    .numMissVals = 0,
     .missval = vlistInqVarMissval(vlistID, varID),
     .datatype = vlistInqVarDatatype(vlistID, varID),
   };
@@ -188,7 +189,7 @@ grb_read_and_decode_record(stream_t *streamptr, int recID, int memType, void *da
 {
   JobArgs args = grb_read_raw_data(streamptr, streamptr->curTsID, recID, memType, streamptr->record->buffer, data, resetFilePos);
   grb_decode_record(&args);
-  return args.nmiss;
+  return args.numMissVals;
 }
 
 typedef struct JobDescriptor
@@ -206,11 +207,11 @@ JobDescriptor_startJob(AsyncManager *jobManager, JobDescriptor *me, stream_t *st
 }
 
 static void
-JobDescriptor_finishJob(AsyncManager *jobManager, JobDescriptor *me, void *data, size_t *nmiss)
+JobDescriptor_finishJob(AsyncManager *jobManager, JobDescriptor *me, void *data, size_t *numMissVals)
 {
   if (AsyncWorker_wait(jobManager, me->job)) xabort("error executing job in worker thread");
   memcpy(data, me->args.data, me->args.gridsize * ((me->args.memType == MEMTYPE_FLOAT) ? sizeof(float) : sizeof(double)));
-  *nmiss = me->args.nmiss;
+  *numMissVals = me->args.numMissVals;
 
   Free(me->args.gribbuffer);
   Free(me->args.data);
@@ -261,7 +262,7 @@ read_next_record(AsyncManager *jobManager, JobDescriptor *jd, stream_t *streampt
 }
 
 static void
-grb_read_next_record(stream_t *streamptr, int recID, int memType, void *data, size_t *nmiss)
+grb_read_next_record(stream_t *streamptr, int recID, int memType, void *data, size_t *numMissVals)
 {
   bool jobFound = false;
 
@@ -298,37 +299,37 @@ grb_read_next_record(stream_t *streamptr, int recID, int memType, void *data, si
           if (jd->args.recID == recID && jd->args.tsID == tsID)
             {
               jobFound = true;
-              JobDescriptor_finishJob(jobManager, jd, data, nmiss);
+              JobDescriptor_finishJob(jobManager, jd, data, numMissVals);
               if (streamptr->nextGlobalRecId < streamptr->maxGlobalRecs) read_next_record(jobManager, jd, streamptr, memType);
             }
         }
     }
 
   // perform the work synchronously if we didn't start a job for it yet
-  if (!jobFound) *nmiss = grb_read_and_decode_record(streamptr, recID, memType, data, false);
+  if (!jobFound) *numMissVals = grb_read_and_decode_record(streamptr, recID, memType, data, false);
 }
 
 void
-grb_read_record(stream_t *streamptr, int memType, void *data, size_t *nmiss)
+grb_read_record(stream_t *streamptr, int memType, void *data, size_t *numMissVals)
 {
   int tsID = streamptr->curTsID;
   int vrecID = streamptr->tsteps[tsID].curRecID;
   int recID = streamptr->tsteps[tsID].recIDs[vrecID];
 
-  grb_read_next_record(streamptr, recID, memType, data, nmiss);
+  grb_read_next_record(streamptr, recID, memType, data, numMissVals);
 }
 
 void
-grb_read_var_slice(stream_t *streamptr, int varID, int levelID, int memType, void *data, size_t *nmiss)
+grb_read_var_slice(stream_t *streamptr, int varID, int levelID, int memType, void *data, size_t *numMissVals)
 {
   int isub = subtypeInqActiveIndex(streamptr->vars[varID].subtypeID);
   int recID = streamptr->vars[varID].recordTable[isub].recordID[levelID];
 
-  *nmiss = grb_read_and_decode_record(streamptr, recID, memType, data, true);
+  *numMissVals = grb_read_and_decode_record(streamptr, recID, memType, data, true);
 }
 
 void
-grb_read_var(stream_t *streamptr, int varID, int memType, void *data, size_t *nmiss)
+grb_read_var(stream_t *streamptr, int varID, int memType, void *data, size_t *numMissVals)
 {
   int vlistID = streamptr->vlistID;
   int fileID = streamptr->fileID;
@@ -343,14 +344,14 @@ grb_read_var(stream_t *streamptr, int varID, int memType, void *data, size_t *nm
 
   if (CDI_Debug) Message("nlevs = %d gridID = %d gridsize = %zu", nlevs, gridID, gridsize);
 
-  *nmiss = 0;
+  *numMissVals = 0;
   for (int levelID = 0; levelID < nlevs; levelID++)
     {
       int recID = streamptr->vars[varID].recordTable[isub].recordID[levelID];
       size_t offset = levelID * gridsize;
       void *datap = (memType == MEMTYPE_FLOAT) ? (void *) ((float *) data + offset) : (void *) ((double *) data + offset);
 
-      *nmiss += grb_read_and_decode_record(streamptr, recID, memType, datap, false);
+      *numMissVals += grb_read_and_decode_record(streamptr, recID, memType, datap, false);
     }
 
   fileSetPos(fileID, currentfilepos, SEEK_SET);
diff --git a/src/grb_write.c b/src/grb_write.c
index e71004aa00c719a210c39a4cadbc6d04faf18481..f61138b49e7b787caeb694ad94e3cbe5b1b78770 100644
--- a/src/grb_write.c
+++ b/src/grb_write.c
@@ -25,8 +25,8 @@
 
 static size_t
 grb_encode(int filetype, int memType, int datatype, int varID, int levelID, int vlistID, int gridID, int zaxisID,
-           CdiDateTime vDateTime, int tsteptype, int numavg, size_t datasize, const void *data, size_t nmiss, void **gribbuffer,
-           int comptype, void *gribContainers)
+           CdiDateTime vDateTime, int tsteptype, int numavg, size_t datasize, const void *data, size_t numMissVals,
+           void **gribbuffer, int comptype, void *gribContainers)
 {
   size_t nbytes = 0;
 
@@ -36,8 +36,8 @@ grb_encode(int filetype, int memType, int datatype, int varID, int levelID, int
       size_t gribbuffersize = datasize * 4 + 3000;
       *gribbuffer = Malloc(gribbuffersize);
 
-      nbytes = cgribexEncode(memType, varID, levelID, vlistID, gridID, zaxisID, vDateTime, tsteptype, numavg, datasize, data, nmiss,
-                             *gribbuffer, gribbuffersize);
+      nbytes = cgribexEncode(memType, varID, levelID, vlistID, gridID, zaxisID, vDateTime, tsteptype, numavg, datasize, data,
+                             numMissVals, *gribbuffer, gribbuffersize);
     }
   else
 #endif
@@ -67,7 +67,7 @@ grb_encode(int filetype, int memType, int datatype, int varID, int levelID, int
 
       size_t gribbuffersize;
       nbytes = gribapiEncode(memTypeX, varID, levelID, vlistID, gridID, zaxisID, vDateTime, tsteptype, numavg, (long) datasize,
-                             datap, nmiss, gribbuffer, &gribbuffersize, comptype, gribContainer);
+                             datap, numMissVals, gribbuffer, &gribbuffersize, comptype, gribContainer);
 
       if (!useFloatInterface && memType == MEMTYPE_FLOAT) Free((void *) datap);
     }
@@ -122,57 +122,59 @@ cdi_fdb_store(void *fdbHandle, const char *filename, void *gribbuffer, size_t nb
 {
 #ifdef HAVE_LIBFDB5
   size_t len = strlen(filename);
-  if (len == 4) Error("FDB keys missing!");
+  if (len == 6) Error("FDB keys missing!");
 
-  KeyValueEntry keyValue;
+  KeyValueItem keyValue;
   keyValue.item = NULL;
-  decode_fdbitem(filename + 4, &keyValue);
+  decode_fdbitem(filename + 6, &keyValue);
 
   if (keyValue.numKeys == 0) Error("FDB keys missing!");
 
-  const char *class = NULL;
-  const char *stream = NULL;
-  const char *expver = NULL;
-  for (int i = 0; i < keyValue.numKeys; i++)
+  bool expverDefined = false;
+  bool classDefined = false;
+  for (int k = 0; k < keyValue.numKeys; k++)
     {
       // clang-format off
-      if      (str_is_equal(keyValue.keys[i], "class"))  class = keyValue.values[i];
-      else if (str_is_equal(keyValue.keys[i], "stream")) stream = keyValue.values[i];
-      else if (str_is_equal(keyValue.keys[i], "expver")) expver = keyValue.values[i];
-      else Error("Unsupported FDB parameter: %s=%s", keyValue.keys[i], keyValue.values[i]);
+      if      (!expverDefined && str_is_equal(keyValue.keys[k], "expver")) expverDefined = true;
+      else if (!classDefined  && str_is_equal(keyValue.keys[k], "class")) classDefined = true;
       // clang-format on
     }
 
-  if (!class) Error("FDB parameter <class> undefined!");
-  if (!stream) Error("FDB parameter <stream> undefined!");
-  if (!expver) Error("FDB parameter <expver> undefined!");
+  if (!expverDefined) Error("FDB parameter <expver> undefined!");
+  if (!classDefined) Error("FDB parameter <class> undefined!");
 
   if (CDI_Debug)
     {
-      printf("{class=%s,expver=%s,stream=%s,date=%s,time=%s,domain=g}", class, expver, stream, fdbKeys->date, fdbKeys->time);
+      printf("{");
+      for (int k = 0; k < keyValue.numKeys; k++)
+        {
+          printf("%s%s=%s", (k > 0) ? "," : "", keyValue.keys[k], keyValue.values[k]);
+        }
+      printf(",date=%s,time=%s,domain=g}", fdbKeys->date, fdbKeys->time);
       printf("{type=an,levtype=%s}{step=0,", fdbKeys->levtype);
       if (fdbKeys->levelist[0]) printf("levelist=%s,", fdbKeys->levelist);
       printf("param=%s},length=%zu\n", fdbKeys->param, nbytes);
     }
 
   fdb_key_t *key;
-  fdb_new_key(&key);
-  fdb_key_add(key, "class", class);
-  fdb_key_add(key, "expver", expver);
-  fdb_key_add(key, "stream", stream);
-  fdb_key_add(key, "domain", "g");
-  fdb_key_add(key, "date", fdbKeys->date);
-  fdb_key_add(key, "time", fdbKeys->time);
-  fdb_key_add(key, "type", "an");
-  fdb_key_add(key, "levtype", fdbKeys->levtype);
-  fdb_key_add(key, "step", "0");
-  fdb_key_add(key, "param", fdbKeys->param);
-  if (fdbKeys->levelist[0]) fdb_key_add(key, "levelist", fdbKeys->levelist);
-
-  fdb_archive(fdbHandle, key, gribbuffer, nbytes);
+  check_fdb_error(fdb_new_key(&key));
+  for (int k = 0; k < keyValue.numKeys; k++)
+    {
+      check_fdb_error(fdb_key_add(key, keyValue.keys[k], keyValue.values[k]));
+    }
+  check_fdb_error(fdb_key_add(key, "domain", "g"));
+  check_fdb_error(fdb_key_add(key, "date", fdbKeys->date));
+  check_fdb_error(fdb_key_add(key, "time", fdbKeys->time));
+  check_fdb_error(fdb_key_add(key, "type", "an"));
+  check_fdb_error(fdb_key_add(key, "levtype", fdbKeys->levtype));
+  check_fdb_error(fdb_key_add(key, "step", "0"));
+  check_fdb_error(fdb_key_add(key, "param", fdbKeys->param));
+  if (fdbKeys->levelist[0]) check_fdb_error(fdb_key_add(key, "levelist", fdbKeys->levelist));
+
+  check_fdb_error(fdb_archive(fdbHandle, key, gribbuffer, nbytes));
   // alternative: write to tmpfile and use C++ code from fdb_write
 
-  fdb_delete_key(key);
+  check_fdb_error(fdb_delete_key(key));
 #endif
 }
 
@@ -233,11 +235,12 @@ grbCopyRecord(stream_t *streamptr2, stream_t *streamptr1)
   if (streamptr1->protocol == CDI_PROTOCOL_FDB)
     {
 #ifdef HAVE_LIBFDB5
-      void *fdbItem = streamptr1->tsteps[tsID].records[recID].fdbItem;
-      if (!fdbItem) Error("fdbItem not available!");
+      int fdbItemIndex = streamptr1->tsteps[tsID].records[recID].fdbItemIndex;
+      if (fdbItemIndex == -1) Error("fdbItem not available!");
 
       size_t buffersize = 0;
-      recsize = fdb_read_record(streamptr1->protocolData, fdbItem, &buffersize, &gribbuffer);
+      recsize
+          = cdi_fdb_read_record(streamptr1->protocolData, &(streamptr1->fdbKeyValueList[fdbItemIndex]), &buffersize, &gribbuffer);
 
       // round up recsize to next multiple of 8
       size_t gribbuffersize = ((recsize + 7U) & ~7U);
@@ -311,7 +314,7 @@ grbCopyRecord(stream_t *streamptr2, stream_t *streamptr1)
 
       if (scanModeIN != cdiGribDataScanningMode.value)
         {
-          size_t nmiss = 0;
+          size_t numMissVals = 0;
 
           int vlistID = streamptr1->vlistID;
           int varID = record->varID;
@@ -324,7 +327,7 @@ grbCopyRecord(stream_t *streamptr2, stream_t *streamptr1)
 
           if (cdiDebugExt >= 20) Message(" processing varID %d; levelID %d", varID, levelID);
 
-          grb_write_var_slice(streamptr2, varID, levelID, MEMTYPE_DOUBLE, (const void *) data, nmiss);
+          grb_write_var_slice(streamptr2, varID, levelID, MEMTYPE_DOUBLE, (const void *) data, numMissVals);
 
           free(data);
         }
@@ -374,7 +377,7 @@ grbCopyRecord(stream_t *streamptr2, stream_t *streamptr1)
 }
 
 void
-grb_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, const void *data, size_t nmiss)
+grb_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, const void *data, size_t numMissVals)
 {
   int filetype = streamptr->filetype;
   int fileID = streamptr->fileID;
@@ -403,7 +406,7 @@ grb_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, co
 
   void *gribbuffer = NULL;
   size_t nbytes = grb_encode(filetype, memtype, datatype, varID, levelID, vlistID, gridID, zaxisID, vDateTime, tsteptype, numavg,
-                             datasize, data, nmiss, &gribbuffer, comptype, streamptr->gribContainers);
+                             datasize, data, numMissVals, &gribbuffer, comptype, streamptr->gribContainers);
 
   if (filetype == CDI_FILETYPE_GRB && (comptype == CDI_COMPRESS_SZIP || comptype == CDI_COMPRESS_AEC))
     nbytes = grbSzip(filetype, gribbuffer, nbytes);
@@ -455,7 +458,7 @@ grb_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, co
 }
 
 void
-grb_write_var(stream_t *streamptr, int varID, int memtype, const void *data, size_t nmiss)
+grb_write_var(stream_t *streamptr, int varID, int memtype, const void *data, size_t numMissVals)
 {
   int vlistID = streamptr->vlistID, gridID = vlistInqVarGrid(vlistID, varID), zaxisID = vlistInqVarZaxis(vlistID, varID),
       nlevs = zaxisInqSize(zaxisID);
@@ -468,31 +471,31 @@ grb_write_var(stream_t *streamptr, int varID, int memtype, const void *data, siz
       {
         const float *restrict fdata = ((const float *) data) + levelID * gridsize;
 
-        size_t nmiss_slice = 0;
-        if (nmiss)
-          for (size_t i = 0; i < chunkLen; ++i) nmiss_slice += DBL_IS_EQUAL(fdata[i], missval);
+        size_t numMissVals_slice = 0;
+        if (numMissVals)
+          for (size_t i = 0; i < chunkLen; ++i) numMissVals_slice += DBL_IS_EQUAL(fdata[i], missval);
 
-        grb_write_var_slice(streamptr, varID, levelID, memtype, fdata, nmiss_slice);
+        grb_write_var_slice(streamptr, varID, levelID, memtype, fdata, numMissVals_slice);
       }
   else
     for (int levelID = 0; levelID < nlevs; levelID++)
       {
         const double *restrict ddata = ((const double *) data) + levelID * gridsize;
 
-        size_t nmiss_slice = 0;
-        if (nmiss)
-          for (size_t i = 0; i < chunkLen; ++i) nmiss_slice += DBL_IS_EQUAL(ddata[i], missval);
+        size_t numMissVals_slice = 0;
+        if (numMissVals)
+          for (size_t i = 0; i < chunkLen; ++i) numMissVals_slice += DBL_IS_EQUAL(ddata[i], missval);
 
-        grb_write_var_slice(streamptr, varID, levelID, memtype, ddata, nmiss_slice);
+        grb_write_var_slice(streamptr, varID, levelID, memtype, ddata, numMissVals_slice);
       }
 }
 
 void
-grb_write_record(stream_t *streamptr, int memtype, const void *data, size_t nmiss)
+grb_write_record(stream_t *streamptr, int memtype, const void *data, size_t numMissVals)
 {
   int varID = streamptr->record->varID;
   int levelID = streamptr->record->levelID;
-  grb_write_var_slice(streamptr, varID, levelID, memtype, data, nmiss);
+  grb_write_var_slice(streamptr, varID, levelID, memtype, data, numMissVals);
 }
 
 #endif
diff --git a/src/gribapi_utilities.c b/src/gribapi_utilities.c
index ca9f8e090a75eaebbf13dba0b18ab58622d0fd1c..03734fb55d2f8508da73403b82639360ec181b96 100644
--- a/src/gribapi_utilities.c
+++ b/src/gribapi_utilities.c
@@ -836,7 +836,7 @@ gribapiGetGridUnstructured(grib_handle *gh, grid_t *grid, size_t numberOfPoints)
         if ( grib_get_string(gh, "gridDescriptionFile", reference_link, &len) == 0 )
         {
         if ( strncmp(reference_link, "file://", 7) == 0 )
-        grid->reference = strdupx(reference_link);
+        grid->reference = strdup(reference_link);
         }
       */
       size_t len = (size_t) CDI_UUID_SIZE;
diff --git a/src/institution.c b/src/institution.c
index 89c4f278f6d7f75120bb942949c3bfb8225e37c5..b93bd7580d90f57bcad1b959748d97384b47637f 100644
--- a/src/institution.c
+++ b/src/institution.c
@@ -158,8 +158,8 @@ instituteNewEntry(cdiResH resH, int center, int subcenter, const char *name, con
     }
   instituteptr->center = center;
   instituteptr->subcenter = subcenter;
-  if (name && *name) instituteptr->name = strdupx(name);
-  if (longname && *longname) instituteptr->longname = strdupx(longname);
+  if (name && *name) instituteptr->name = strdup(name);
+  if (longname && *longname) instituteptr->longname = strdup(longname);
   return instituteptr;
 }
 
diff --git a/src/iterator.c b/src/iterator.c
index 9825f62c4f0a4c74dcd78ef440709a25b5bd47b6..6c152276472c312759391ae8bf5c989ccb0b7f8f 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -301,8 +301,9 @@ cdiIterator_serialize(CdiIterator *me)
     }
 
   const char *ftypeStr = fileType2String(me->filetype), *advStr = me->isAdvanced ? kAdvancedString : kUnadvancedString;
-  char *result = (char *) Malloc(strlen(ftypeStr) + 1 + strlen(advStr) + 1 + strlen(subclassDescription) + 1);
-  sprintf(result, "%s %s %s", ftypeStr, advStr, subclassDescription);
+  size_t len = strlen(ftypeStr) + 1 + strlen(advStr) + 1 + strlen(subclassDescription) + 1;
+  char *result = (char *) Malloc(len);
+  snprintf(result, len, "%s %s %s", ftypeStr, advStr, subclassDescription);
   Free(subclassDescription);
   return result;
 }
@@ -955,18 +956,18 @@ cdiIterator_inqGridId(CdiIterator *me)
 @Function cdiIterator_readField
 @Title Read the whole field into a double buffer
 
-@Prototype void cdiIterator_readField(CdiIterator *me, double *buffer, SizeType *nmiss)
+@Prototype void cdiIterator_readField(CdiIterator *me, double *buffer, SizeType *numMissVals)
 @Parameter
     @item iterator The iterator to operate on.
     @item buffer A pointer to the double array that the data should be written to.
-    @item nmiss A pointer to a variable where the count of missing values will be stored. May be NULL.
+    @item numMissVals A pointer to a variable where the count of missing values will be stored. May be NULL.
 
 @Description
     It is assumed that the caller first analyses the return value of cdiIterator_inqGridId to determine the required size of the
 buffer. Failing to do so results in undefined behavior. You have been warned.
 */
 void
-cdiIterator_readField(CdiIterator *me, double *buffer, SizeType *nmiss)
+cdiIterator_readField(CdiIterator *me, double *buffer, SizeType *numMissVals)
 {
   size_t numMiss = 0;
   sanityCheck(me);
@@ -994,25 +995,25 @@ cdiIterator_readField(CdiIterator *me, double *buffer, SizeType *nmiss)
     default: Error(kUnexpectedFileTypeMessage);
     }
 
-  *nmiss = (SizeType) numMiss;
+  *numMissVals = (SizeType) numMiss;
 }
 
 /*
 @Function cdiIterator_readFieldF
 @Title Read the whole field into a double buffer
 
-@Prototype void cdiIterator_readFieldF(CdiIterator  me, float *buffer, SizeType *nmiss)
+@Prototype void cdiIterator_readFieldF(CdiIterator  me, float *buffer, SizeType *numMissVals)
 @Parameter
     @item iterator The iterator to operate on.
     @item buffer   A pointer to the double array that the data should be written to.
-    @item nmiss    A pointer to a variable where the count of missing values will be stored. May be NULL.
+    @item numMissVals    A pointer to a variable where the count of missing values will be stored. May be NULL.
 
 @Description
     It is assumed that the caller first analyses the return value of cdiIterator_inqGridId to determine the required size of the
 buffer. Failing to do so results in undefined behavior. You have been warned.
 */
 void
-cdiIterator_readFieldF(CdiIterator *me, float *buffer, SizeType *nmiss)
+cdiIterator_readFieldF(CdiIterator *me, float *buffer, SizeType *numMissVals)
 {
   size_t numMiss = 0;
   sanityCheck(me);
@@ -1040,7 +1041,7 @@ cdiIterator_readFieldF(CdiIterator *me, float *buffer, SizeType *nmiss)
     default: Error(kUnexpectedFileTypeMessage);
     }
 
-  *nmiss = (SizeType) numMiss;
+  *numMissVals = (SizeType) numMiss;
 }
 
 /*
diff --git a/src/iterator_fallback.c b/src/iterator_fallback.c
index 796e4e0fa41fbab5c0c25a0284e602e3ce7eec51..4be6f2faf6ed456d1bcfaac4e4ba939de8de165c 100644
--- a/src/iterator_fallback.c
+++ b/src/iterator_fallback.c
@@ -115,9 +115,10 @@ cdiFallbackIterator_serialize(CdiIterator *super)
   CdiFallbackIterator *me = (CdiFallbackIterator *) (void *) super;
 
   char *escapedPath = cdiEscapeSpaces(me->path);
-  char *result = (char *) Malloc(strlen(escapedPath) + 7 * (3 * sizeof(int) * CHAR_BIT / 8 + 1) + 1);
-  sprintf(result, "%s %d %d %d %d %d %d %d", escapedPath, me->variableCount, me->curVariable, me->curLevelCount, me->curLevel,
-          me->curSubtypeCount, me->curSubtype, me->curTimestep);
+  size_t len = strlen(escapedPath) + 7 * (3 * sizeof(int) * CHAR_BIT / 8 + 1) + 1;
+  char *result = (char *) Malloc(len);
+  snprintf(result, len, "%s %d %d %d %d %d %d %d", escapedPath, me->variableCount, me->curVariable, me->curLevelCount, me->curLevel,
+           me->curSubtypeCount, me->curSubtype, me->curTimestep);
   Free(escapedPath);
   return result;
 }
@@ -243,8 +244,9 @@ cdiFallbackIterator_inqTime(CdiIterator *super, CdiTimeType timeType)
   int year, month, day, hour, minute, second;
   cdiDecodeDate(date, &year, &month, &day);
   cdiDecodeTime(time, &hour, &minute, &second);
-  char *result = (char *) Malloc(4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 4 + 1);
-  sprintf(result, "%04d-%02d-%02dT%02d:%02d:%02d.000", year, month, day, hour, minute, second);
+  size_t len = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 4 + 1;
+  char *result = (char *) Malloc(len);
+  snprintf(result, len, "%04d-%02d-%02dT%02d:%02d:%02d.000", year, month, day, hour, minute, second);
   return result;
 }
 
@@ -401,21 +403,21 @@ cdiFallbackIterator_copyVariableName(CdiIterator *super)
 }
 
 void
-cdiFallbackIterator_readField(CdiIterator *super, double *buffer, size_t *nmiss)
+cdiFallbackIterator_readField(CdiIterator *super, double *buffer, size_t *numMissVals)
 {
   CdiFallbackIterator *me = (CdiFallbackIterator *) (void *) super;
   SizeType missingValues = 0;
   streamReadVarSlice(me->streamId, me->curVariable, me->curLevel, buffer, &missingValues);
-  if (nmiss) *nmiss = (size_t) missingValues;
+  if (numMissVals) *numMissVals = (size_t) missingValues;
 }
 
 void
-cdiFallbackIterator_readFieldF(CdiIterator *super, float *buffer, size_t *nmiss)
+cdiFallbackIterator_readFieldF(CdiIterator *super, float *buffer, size_t *numMissVals)
 {
   CdiFallbackIterator *me = (CdiFallbackIterator *) (void *) super;
   SizeType missingValues = 0;
   streamReadVarSliceF(me->streamId, me->curVariable, me->curLevel, buffer, &missingValues);
-  if (nmiss) *nmiss = (size_t) missingValues;
+  if (numMissVals) *numMissVals = (size_t) missingValues;
 }
 
 /*
diff --git a/src/iterator_fallback.h b/src/iterator_fallback.h
index e3c9514308ed3498898c2e9f50573b742d8457b1..1828a4992aa88b076eaa80462c9d6194ed16758d 100644
--- a/src/iterator_fallback.h
+++ b/src/iterator_fallback.h
@@ -37,8 +37,8 @@ char *cdiFallbackIterator_copyVariableName(CdiIterator *me);
 int cdiFallbackIterator_inqTile(CdiIterator *me, int *outTileIndex, int *outTileAttribute);
 int cdiFallbackIterator_inqTileCount(CdiIterator *me, int *outTileCount, int *outTileAttributeCount);
 
-void cdiFallbackIterator_readField(CdiIterator *me, double *buffer, size_t *nmiss);
-void cdiFallbackIterator_readFieldF(CdiIterator *me, float *buffer, size_t *nmiss);
+void cdiFallbackIterator_readField(CdiIterator *me, double *buffer, size_t *numMissVals);
+void cdiFallbackIterator_readFieldF(CdiIterator *me, float *buffer, size_t *numMissVals);
 
 void cdiFallbackIterator_delete(CdiIterator *super);
 
diff --git a/src/iterator_grib.c b/src/iterator_grib.c
index 16d82692be760f42e9556ec2386cea0246bb53be..7b5219f0a6421294e95cfc08aeaa16072b62da2f 100644
--- a/src/iterator_grib.c
+++ b/src/iterator_grib.c
@@ -145,8 +145,9 @@ cdiGribIterator_serialize(CdiIterator *super)
 
   const char *path = cdiInputFile_getPath(me->file);
   char *escapedPath = cdiEscapeSpaces(path);
-  char *result = (char *) Malloc(strlen(escapedPath) + 3 * sizeof(int) * CHAR_BIT / 8);
-  sprintf(result, "%s %zu", escapedPath, (size_t) me->fileOffset);
+  size_t len = strlen(escapedPath) + 3 * sizeof(int) * CHAR_BIT / 8;
+  char *result = (char *) Malloc(len);
+  snprintf(result, len, "%s %zu", escapedPath, (size_t) me->fileOffset);
   Free(escapedPath);
   return result;
 }
@@ -618,28 +619,28 @@ cdiGribIterator_copyVariableName(CdiIterator *super)
 }
 
 void
-cdiGribIterator_readField(CdiIterator *super, double *buffer, size_t *nmiss)
+cdiGribIterator_readField(CdiIterator *super, double *buffer, size_t *numMissVals)
 {
   CdiGribIterator *me = (CdiGribIterator *) (void *) super;
 
   GRIB_CHECK(my_grib_set_double(me->gribHandle, "missingValue", CDI_Default_Missval), 0);
   gribGetDoubleArray(me->gribHandle, "values", buffer);
   long gridType = gribGetLong(me->gribHandle, "gridDefinitionTemplateNumber");
-  if (nmiss)
+  if (numMissVals)
     {
       // The condition excludes harmonic data.
-      *nmiss = (gridType >= 50 && gridType <= 53) ? (size_t) 0 : (size_t) gribGetLong(me->gribHandle, "numberOfMissing");
+      *numMissVals = (gridType >= 50 && gridType <= 53) ? (size_t) 0 : (size_t) gribGetLong(me->gribHandle, "numberOfMissing");
     }
 }
 
 void
-cdiGribIterator_readFieldF(CdiIterator *super, float *buffer, size_t *nmiss)
+cdiGribIterator_readFieldF(CdiIterator *super, float *buffer, size_t *numMissVals)
 {
   CdiGribIterator *me = (CdiGribIterator *) (void *) super;
 
   size_t valueCount = gribGetArraySize(me->gribHandle, "values");
   double *temp = (double *) Malloc(valueCount * sizeof(*temp));
-  cdiGribIterator_readField(super, temp, nmiss);
+  cdiGribIterator_readField(super, temp, numMissVals);
   for (size_t i = valueCount; i--;) buffer[i] = (float) temp[i];
   Free(temp);
 }
diff --git a/src/iterator_grib.h b/src/iterator_grib.h
index a71617f962e29f7234d44417a5efb6f794cc8707..a524c8cd5bfa6070e55eaa2e33dca7f8d110f08a 100644
--- a/src/iterator_grib.h
+++ b/src/iterator_grib.h
@@ -39,8 +39,8 @@ int cdiGribIterator_inqTile(CdiIterator *me, int *outTileIndex, int *outTileAttr
 int cdiGribIterator_inqTileCount(CdiIterator *me, int *outTileCount, int *outTileAttributeCount);
 char *cdiGribIterator_copyVariableName(CdiIterator *me);
 
-void cdiGribIterator_readField(CdiIterator *me, double *buffer, size_t *nmiss);
-void cdiGribIterator_readFieldF(CdiIterator *me, float *buffer, size_t *nmiss);
+void cdiGribIterator_readField(CdiIterator *me, double *buffer, size_t *numMissVals);
+void cdiGribIterator_readFieldF(CdiIterator *me, float *buffer, size_t *numMissVals);
 
 #endif
 
diff --git a/src/make_cdilib b/src/make_cdilib
index 48b869f1dfaba9806f149311770f10a9ec70f8fc..7a15729cf5a1a090e3ed7a2c76d31d52dce23452 100755
--- a/src/make_cdilib
+++ b/src/make_cdilib
@@ -226,8 +226,8 @@ echo "#undef malloc"  >>  ${PROG}
 echo "#undef calloc"  >>  ${PROG}
 echo "#undef free"    >>  ${PROG}
 echo "#undef DOUBLE_PRECISION" >>  ${PROG}
-cat $srcdir/cfortran.h   >>  ${PROG}
+cat `if test -f cfortran.h; then echo cfortran.h; else echo $srcdir/cfortran.h; fi;` >> ${PROG}
 echo "#endif" >>  ${PROG}
-cat $srcdir/cdiFortran.c >>  ${PROG}
+cat `if test -f cdiFortran.c; then echo cdiFortran.c; else echo $srcdir/cdiFortran.c; fi;` >> ${PROG}
 
 exit
diff --git a/src/model.c b/src/model.c
index 9cf80506230c7a017de5b0552f19eec101355093..ef3ad02235d378a760bbda88f4262249f26e4ae0 100644
--- a/src/model.c
+++ b/src/model.c
@@ -54,7 +54,7 @@ modelNewEntry(cdiResH resH, int instID, int modelgribID, const char *name)
     }
   modelptr->instID = instID;
   modelptr->modelgribID = modelgribID;
-  if (name && *name) modelptr->name = strdupx(name);
+  if (name && *name) modelptr->name = strdup(name);
 
   return (modelptr);
 }
diff --git a/src/pio_client.c b/src/pio_client.c
index e56f14b988fd579bfb47d68f963221d40d09a883..a312c53f6a54055be5faa39cdbbe1860d0348b2a 100644
--- a/src/pio_client.c
+++ b/src/pio_client.c
@@ -303,7 +303,7 @@ cdiPioStreamDefDecomposedVlist(int streamID, int vlistID, const Xt_idxlist partD
 }
 
 static void
-cdiPioClientStreamWriteVar_(int streamID, int varID, int memtype, const void *data, size_t nmiss)
+cdiPioClientStreamWriteVar_(int streamID, int varID, int memtype, const void *data, size_t numMissVals)
 {
   struct partDescPreset clientDeco = cdiPioGetStreamPartDescPreset(streamID);
   int vlistID = streamInqVlist(streamID);
@@ -316,7 +316,7 @@ cdiPioClientStreamWriteVar_(int streamID, int varID, int memtype, const void *da
     xabort("data type does not match pre-declared conversion for stream %d,"
            " variable ID %d!",
            streamID, varID);
-  cdiPioStreamWriteVarPart_(streamID, varID, memtype, data, nmiss, clientDeco.lists[varID]);
+  cdiPioStreamWriteVarPart_(streamID, varID, memtype, data, numMissVals, clientDeco.lists[varID]);
 }
 
 static struct cdiPioIdxlistCache *clientIdxlistCache;
@@ -324,7 +324,7 @@ static size_t neededClientIdxlistCacheSize;
 static struct idList seenStreamWriteVarChunk;
 
 static void
-cdiPioClientStreamWriteVarChunk_(int streamID, int varID, int memtype, const int rect[][2], const void *data, size_t nmiss)
+cdiPioClientStreamWriteVarChunk_(int streamID, int varID, int memtype, const int rect[][2], const void *data, size_t numMissVals)
 {
   if (memtype != MEMTYPE_DOUBLE && memtype != MEMTYPE_FLOAT) Error("Writing of type data %d not implemented!", memtype);
   int vlistID = streamInqVlist(streamID);
@@ -357,7 +357,7 @@ cdiPioClientStreamWriteVarChunk_(int streamID, int varID, int memtype, const int
                              const int sliceShape[])
       = (ndims == 3) ? cdiPioIdxlistCacheAddSection3D : cdiPioIdxlistCacheAddSection2D;
   Xt_idxlist chunkDesc = cacheSection(clientIdxlistCache, varShapeXt, origin, chunkShape);
-  cdiPioBufferPartData(streamID, varID, memtype, data, nmiss, chunkDesc);
+  cdiPioBufferPartData(streamID, varID, memtype, data, numMissVals, chunkDesc);
 }
 
 #if defined HAVE_LIBNETCDF
diff --git a/src/pio_interface.c b/src/pio_interface.c
index 32c88efe95290d734a7455ee7cf9c0708e06ad04..4bbc950d60e755e0f9d10bc52f37de10ad83005a 100644
--- a/src/pio_interface.c
+++ b/src/pio_interface.c
@@ -38,6 +38,10 @@
 #include "vlist.h"
 #include "vlist_var.h"
 
+#ifndef SSIZE_MAX
+#define SSIZE_MAX LONG_MAX
+#endif
+
 static struct rdmaWin
 {
   size_t size;
@@ -313,7 +317,7 @@ cdiPioRDMAProgress(void)
 }
 
 static void
-cdiPioBufferPartData_(int streamID, int varID, int memtype, const void *packData, valPackFunc packDataFunc, size_t nmiss,
+cdiPioBufferPartData_(int streamID, int varID, int memtype, const void *packData, valPackFunc packDataFunc, size_t numMissVals,
                       Xt_idxlist partDesc)
 {
   size_t streamIdx = indexOfID(&openStreams, streamID);
@@ -321,7 +325,7 @@ cdiPioBufferPartData_(int streamID, int varID, int memtype, const void *packData
   xassert(varID >= 0 && varID < streamInqNvars(streamID));
   collWaitAll();
   int dataHeaderID = memtype == MEMTYPE_DOUBLE ? DATA_HEADER_DOUBLE : DATA_HEADER_FLOAT;
-  struct winHeaderEntry dataHeader = { .id = dataHeaderID, .specific.dataRecord = { varID, nmiss }, .offset = -1 };
+  struct winHeaderEntry dataHeader = { .id = dataHeaderID, .specific.dataRecord = { varID, numMissVals }, .offset = -1 };
   modelWinEnqueue(streamIdx, dataHeader, packData, packDataFunc);
   {
     struct winHeaderEntry partHeader = { .id = PARTDESCMARKER, .offset = 0 };
@@ -339,11 +343,11 @@ memtype2ElemSize(int memtype)
 }
 
 void
-cdiPioBufferPartData(int streamID, int varID, int memtype, const void *data, size_t nmiss, Xt_idxlist partDesc)
+cdiPioBufferPartData(int streamID, int varID, int memtype, const void *data, size_t numMissVals, Xt_idxlist partDesc)
 {
   size_t chunk = (size_t) (xt_idxlist_get_num_indices(partDesc));
   size_t elemSize = memtype2ElemSize(memtype);
-  cdiPioBufferPartData_(streamID, varID, memtype, &(struct memCpyDataDesc){ data, chunk * elemSize }, memcpyPackFunc, nmiss,
+  cdiPioBufferPartData_(streamID, varID, memtype, &(struct memCpyDataDesc){ data, chunk * elemSize }, memcpyPackFunc, numMissVals,
                         partDesc);
 }
 
@@ -390,7 +394,7 @@ scatterGatherPackFunc(void *dataDesc, void *buf, int size, int *pos, void *conte
 
 void
 cdiPioBufferPartDataGather(int streamID, int varID, int memtype, const void *data, int numBlocks, const int blocklengths[],
-                           const int displacements[], size_t nmiss, Xt_idxlist partDesc)
+                           const int displacements[], size_t numMissVals, Xt_idxlist partDesc)
 {
   xassert(numBlocks >= 0);
   cdiPioBufferPartData_(streamID, varID, memtype,
@@ -400,7 +404,7 @@ cdiPioBufferPartDataGather(int streamID, int varID, int memtype, const void *dat
                                                      .elemSize = memtype2ElemSize(memtype),
                                                      .numBlocks = (unsigned) numBlocks,
                                                      .numElems = (unsigned) xt_idxlist_get_num_indices(partDesc) },
-                        scatterGatherPackFunc, nmiss, partDesc);
+                        scatterGatherPackFunc, numMissVals, partDesc);
 }
 
 /************************************************************************/
@@ -743,36 +747,36 @@ pioWriteTimestep(void)
 }
 
 void
-cdiPioStreamWriteVarPart_(int streamID, int varID, int memtype, const void *data, int nmiss, Xt_idxlist partDesc)
+cdiPioStreamWriteVarPart_(int streamID, int varID, int memtype, const void *data, int numMissVals, Xt_idxlist partDesc)
 {
   if (CDI_Debug) Message("streamID = %d varID = %d", streamID, varID);
 
   int chunk = xt_idxlist_get_num_indices(partDesc);
   xassert(chunk == 0 || data);
 
-  void (*myStreamWriteVarPart)(int streamID, int varID, int memtype, const void *data, int nmiss, Xt_idxlist partDesc)
+  void (*myStreamWriteVarPart)(int streamID, int varID, int memtype, const void *data, int numMissVals, Xt_idxlist partDesc)
       = (void (*)(int, int, int, const void *, int, Xt_idxlist)) namespaceSwitchGet(NSSWITCH_STREAM_WRITE_VAR_PART_).func;
 
   if (!myStreamWriteVarPart) xabort("local part writing is unsupported!");
 
-  myStreamWriteVarPart(streamID, varID, memtype, data, nmiss, partDesc);
+  myStreamWriteVarPart(streamID, varID, memtype, data, numMissVals, partDesc);
 }
 
 void
-streamWriteVarPart(int streamID, int varID, const double *data, int nmiss, Xt_idxlist partDesc)
+streamWriteVarPart(int streamID, int varID, const double *data, int numMissVals, Xt_idxlist partDesc)
 {
-  cdiPioStreamWriteVarPart_(streamID, varID, MEMTYPE_DOUBLE, data, nmiss, partDesc);
+  cdiPioStreamWriteVarPart_(streamID, varID, MEMTYPE_DOUBLE, data, numMissVals, partDesc);
 }
 
 void
-streamWriteVarPartF(int streamID, int varID, const float *data, int nmiss, Xt_idxlist partDesc)
+streamWriteVarPartF(int streamID, int varID, const float *data, int numMissVals, Xt_idxlist partDesc)
 {
-  cdiPioStreamWriteVarPart_(streamID, varID, MEMTYPE_FLOAT, data, nmiss, partDesc);
+  cdiPioStreamWriteVarPart_(streamID, varID, MEMTYPE_FLOAT, data, numMissVals, partDesc);
 }
 
 static void
 streamWriteScatteredVarPart_(int streamID, int varID, int memtype, const void *data, int numBlocks, const int blocklengths[],
-                             const int displacements[], int nmiss, Xt_idxlist partDesc)
+                             const int displacements[], int numMissVals, Xt_idxlist partDesc)
 {
   if (CDI_Debug) Message("streamID = %d varID = %d", streamID, varID);
 
@@ -780,28 +784,29 @@ streamWriteScatteredVarPart_(int streamID, int varID, int memtype, const void *d
   xassert(chunk == 0 || data);
 
   void (*myStreamWriteScatteredVarPart)(int streamID, int varID, int memtype, const void *data, int numBlocks,
-                                        const int blocklengths[], const int displacements[], int nmiss, Xt_idxlist partDesc)
+                                        const int blocklengths[], const int displacements[], int numMissVals, Xt_idxlist partDesc)
       = (void (*)(int, int, int, const void *, int, const int[], const int[], int, Xt_idxlist)) namespaceSwitchGet(
             NSSWITCH_STREAM_WRITE_SCATTERED_VAR_PART_)
             .func;
 
   if (!myStreamWriteScatteredVarPart) xabort("local part writing is unsupported!");
 
-  myStreamWriteScatteredVarPart(streamID, varID, memtype, data, numBlocks, blocklengths, displacements, nmiss, partDesc);
+  myStreamWriteScatteredVarPart(streamID, varID, memtype, data, numBlocks, blocklengths, displacements, numMissVals, partDesc);
 }
 
 void
 streamWriteScatteredVarPart(int streamID, int varID, const double *data, int numBlocks, const int blocklengths[],
-                            const int displacements[], int nmiss, Xt_idxlist partDesc)
+                            const int displacements[], int numMissVals, Xt_idxlist partDesc)
 {
-  streamWriteScatteredVarPart_(streamID, varID, MEMTYPE_DOUBLE, data, numBlocks, blocklengths, displacements, nmiss, partDesc);
+  streamWriteScatteredVarPart_(streamID, varID, MEMTYPE_DOUBLE, data, numBlocks, blocklengths, displacements, numMissVals,
+                               partDesc);
 }
 
 void
 streamWriteScatteredVarPartF(int streamID, int varID, const float *data, int numBlocks, const int blocklengths[],
-                             const int displacements[], int nmiss, Xt_idxlist partDesc)
+                             const int displacements[], int numMissVals, Xt_idxlist partDesc)
 {
-  streamWriteScatteredVarPart_(streamID, varID, MEMTYPE_FLOAT, data, numBlocks, blocklengths, displacements, nmiss, partDesc);
+  streamWriteScatteredVarPart_(streamID, varID, MEMTYPE_FLOAT, data, numBlocks, blocklengths, displacements, numMissVals, partDesc);
 }
 
 /*
diff --git a/src/pio_interface.h b/src/pio_interface.h
index 24c6b548e13ba11ce5058fc49a154114e1e8ad9f..354ad9187d962593a7068db0c99c4ae924713700 100644
--- a/src/pio_interface.h
+++ b/src/pio_interface.h
@@ -11,9 +11,9 @@
 #include "resource_handle.h"
 #include "pio_rpc.h"
 
-void cdiPioBufferPartData(int streamID, int varID, int memtype, const void *data, size_t nmiss, Xt_idxlist partDesc);
+void cdiPioBufferPartData(int streamID, int varID, int memtype, const void *data, size_t numMissVals, Xt_idxlist partDesc);
 void cdiPioBufferPartDataGather(int streamID, int varID, int memtype, const void *data, int numBlocks, const int blocklengths[],
-                                const int displacements[], size_t nmiss, Xt_idxlist partDesc);
+                                const int displacements[], size_t numMissVals, Xt_idxlist partDesc);
 
 void pioBufferFuncCall(int streamID, struct winHeaderEntry header, const void *data, valPackFunc dataPackFunc);
 
@@ -27,7 +27,7 @@ void memcpyPackFunc(void *dataDesc, void *buf, int size, int *pos, void *context
 
 extern float cdiPIOpartInflate_;
 
-void cdiPioStreamWriteVarPart_(int streamID, int varID, int memtype, const void *data, int nmiss, Xt_idxlist partDesc);
+void cdiPioStreamWriteVarPart_(int streamID, int varID, int memtype, const void *data, int numMissVals, Xt_idxlist partDesc);
 
 void cdiPioClientStreamWinInit(int streamID);
 void cdiPioClientStreamWinCreate(int streamID, struct collSpec *cspec);
diff --git a/src/pio_rpc.h b/src/pio_rpc.h
index bb6da5e59c929b4d2d7f84d111307c617842cf59..50dfcc7db889bf794cc1bd1b60439d9ed956cc0a 100644
--- a/src/pio_rpc.h
+++ b/src/pio_rpc.h
@@ -54,7 +54,7 @@ struct headerSize
 
 struct dataRecord
 {
-  int varID, nmiss;
+  int varID, numMissVals;
 };
 
 union funcArgs
diff --git a/src/pio_server.c b/src/pio_server.c
index c3888db345bc4c16c94a168dd3d6b6bbff11fa27..b013c79fb84cb9f3c1845331e33b9131ba8fbe21 100644
--- a/src/pio_server.c
+++ b/src/pio_server.c
@@ -797,35 +797,35 @@ buildVarSlicesIdxList(int vlistID, int varID, int startLvl, int numLvl, const st
 static inline size_t
 countMemMissingDouble(size_t n, const double *restrict data, double missVal)
 {
-  size_t nmiss = 0;
-  for (size_t i = 0; i < n; ++i) nmiss += (data[i] == missVal);
-  return nmiss;
+  size_t numMissVals = 0;
+  for (size_t i = 0; i < n; ++i) numMissVals += (data[i] == missVal);
+  return numMissVals;
 }
 
 static inline size_t
 countMemMissingFloat(size_t n, const float *restrict data, double missVal)
 {
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
   float missValF = (float) missVal;
   if (missValF == missVal)
-    for (size_t i = 0; i < n; ++i) nmiss += (data[i] == missValF);
-  return nmiss;
+    for (size_t i = 0; i < n; ++i) numMissVals += (data[i] == missValF);
+  return numMissVals;
 }
 
 static size_t
 countVarChunkMissingVals(int vlistID, int varID, struct streamMapping *mapping, size_t chunkLen, int conversion,
                          const void *restrict data)
 {
-  size_t nmiss = 0;
+  size_t numMissVals = 0;
   if (mapping->hasMissing[varID])
     {
       double missVal = vlistInqVarMissval(vlistID, varID);
       if (conversion == DATA_HEADER_DOUBLE)
-        nmiss = countMemMissingDouble(chunkLen, data, missVal);
+        numMissVals = countMemMissingDouble(chunkLen, data, missVal);
       else
-        nmiss = countMemMissingFloat(chunkLen, data, missVal);
+        numMissVals = countMemMissingFloat(chunkLen, data, missVal);
     }
-  return nmiss;
+  return numMissVals;
 }
 
 static inline void
@@ -956,13 +956,13 @@ writeNetCDFStreamParallel(size_t streamIdx, struct streamMapping *mapping, void
 
         {
           /* count missing values if appropriate */
-          size_t nmiss
+          size_t numMissVals
               = countVarChunkMissingVals(vlistID, varID, mapping, (size_t) (PPM_extents_size(3, varChunk)), conversion, data);
           /* write chunk */
           if (conversion == DATA_HEADER_DOUBLE)
-            streamWriteVarChunk(streamID, varID, (const int(*)[2]) myChunk, data, (int) nmiss);
+            streamWriteVarChunk(streamID, varID, (const int(*)[2]) myChunk, data, (int) numMissVals);
           else
-            streamWriteVarChunkF(streamID, varID, (const int(*)[2]) myChunk, data, (int) nmiss);
+            streamWriteVarChunkF(streamID, varID, (const int(*)[2]) myChunk, data, (int) numMissVals);
         }
       }
 }
@@ -1090,11 +1090,11 @@ writeNetCDFStreamSerial(size_t streamIdx, struct streamMapping *mapping, void **
         if (!conf->cacheRedists) xt_redist_delete(gatherRedist);
         if (writerRank == collRank)
           {
-            size_t nmiss = countVarChunkMissingVals(vlistID, varID, mapping, varSize, conversion, data);
+            size_t numMissVals = countVarChunkMissingVals(vlistID, varID, mapping, varSize, conversion, data);
             if (conversion == DATA_HEADER_DOUBLE)
-              streamWriteVar(streamID, varID, data, (int) nmiss);
+              streamWriteVar(streamID, varID, data, (int) numMissVals);
             else
-              streamWriteVarF(streamID, varID, data, (int) nmiss);
+              streamWriteVarF(streamID, varID, data, (int) numMissVals);
           }
       }
 }
@@ -1164,7 +1164,7 @@ streamMappingNew(size_t streamIdx, const struct winHeaderEntry *winDict, const s
       /* FIXME: this could better be ensured on client */
       xassert(varID < numVars && varID >= 0 && varMap[varID] == 0);
       varMap[varID] = headerIdx;
-      hasMissing[varID] += winDict[headerIdx].specific.dataRecord.nmiss;
+      hasMissing[varID] += winDict[headerIdx].specific.dataRecord.numMissVals;
     }
   /* set numLvlsW[i] to 1 if varMap[i] != 0 on any collector,
    * also sets hasMissing_[i] to global reduction of hasMissing[i] */
@@ -1533,11 +1533,13 @@ writeGribStream(size_t streamIdx, struct streamMapping *mapping, void **data_, s
               int conversion = winDict[headerIdx].id;
               size_t elemSize = conversion == DATA_HEADER_FLOAT ? sizeof(float) : sizeof(double);
               size_t nvals = recordSize / elemSize;
-              size_t nmiss = countVarChunkMissingVals(vlistID, varID, mapping, nvals, conversion, data + recordDataOfs);
+              size_t numMissVals = countVarChunkMissingVals(vlistID, varID, mapping, nvals, conversion, data + recordDataOfs);
               if (conversion == DATA_HEADER_DOUBLE)
-                streamWriteVarSlice(streamID, varID, level, (const double *) (const void *) (data + recordDataOfs), (int) nmiss);
+                streamWriteVarSlice(streamID, varID, level, (const double *) (const void *) (data + recordDataOfs),
+                                    (int) numMissVals);
               else
-                streamWriteVarSliceF(streamID, varID, level, (const float *) (const void *) (data + recordDataOfs), (int) nmiss);
+                streamWriteVarSliceF(streamID, varID, level, (const float *) (const void *) (data + recordDataOfs),
+                                     (int) numMissVals);
               recordDataOfs += recordSize;
             }
           aggBufFlush(streamID, fileID, cdiPioFileWrite);
diff --git a/src/servicelib.c b/src/servicelib.c
index 368d72cddb8e85b45eaf6eaf97d3ecbddd4f11f1..26fdcad9311582f51fa4981b15492c0e3d35c47a 100644
--- a/src/servicelib.c
+++ b/src/servicelib.c
@@ -50,7 +50,7 @@ srvDebug(int debug)
 }
 
 static void
-srvLibInit()
+srvLibInit(void)
 {
   const char *envName = "SRV_PRECISION";
 
diff --git a/src/stream.c b/src/stream.c
index e302be77389f23fd8ce12e4132dfbb738426ae94..64402f3dbfb7355067d6a39b9f59042edcb21e38 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -81,7 +81,7 @@ cdiGetFiletype(const char *uri, int *byteorder)
   switch (protocol)
     {
     case CDI_PROTOCOL_ACROSS: return CDI_FILETYPE_GRB2;
-    case CDI_PROTOCOL_FDB:    return CDI_FILETYPE_UNDEF;
+    case CDI_PROTOCOL_FDB:    return CDI_FILETYPE_GRB2;
     case CDI_PROTOCOL_OTHER:  return CDI_FILETYPE_NC4;  // support for NetCDF remote types and ESDM
     case CDI_PROTOCOL_FILE:
       // handled below;
@@ -493,7 +493,7 @@ cdiStreamOpenDefaultDelegate(const char *uri, char filemode, int filetype, strea
 #endif
 
     case CDI_PROTOCOL_FDB:
-#if defined(HAVE_FDB5) && defined(HAVE_LIBGRIB_API)
+#if defined(HAVE_LIBFDB5) && defined(HAVE_LIBGRIB_API)
 
       if (filetype != CDI_FILETYPE_GRB && filetype != CDI_FILETYPE_GRB2)
         {
@@ -501,8 +501,8 @@ cdiStreamOpenDefaultDelegate(const char *uri, char filemode, int filetype, strea
           return CDI_EUFTYPE;
         }
 
-      fdb_initialise();
-      fdb_new_handle((fdb_handle_t **) &(streamptr->protocolData));
+      check_fdb_error(fdb_initialise());
+      check_fdb_error(fdb_new_handle((fdb_handle_t **) &(streamptr->protocolData)));
       streamptr->filetype = filetype;
       if (recordBufIsToBeCreated)
         {
@@ -511,9 +511,9 @@ cdiStreamOpenDefaultDelegate(const char *uri, char filemode, int filetype, strea
         }
       return 88;
 
-#else  // !(defined(HAVE_FDB5) && defined(HAVE_LIBGRIB_API))
+#else  // !(defined(HAVE_LIBFDB5) && defined(HAVE_LIBGRIB_API))
 
-#ifdef HAVE_FDB5
+#ifdef HAVE_LIBFDB5
       Warning("ecCodes support not compiled in (Needed for FDB5)!");
 #else
       Warning("FDB5 support not compiled in!");
@@ -663,6 +663,18 @@ streamOpenID(const char *filename, char filemode, int filetype, int resH)
   stream_t *streamptr = stream_new_entry(resH);
   int streamID = CDI_ESYSTEM;
 
+#ifndef HAVE_NC4HDF5_THREADSAFE
+  if (CDI_Threadsafe)
+    {
+#ifndef HAVE_LIBPTHREAD
+      Error("CDI threadsafe failed, pthread support not compiled!");
+#endif
+      if (filetype == CDI_FILETYPE_NC4 || filetype == CDI_FILETYPE_NC4C) streamptr->lockIO = true;
+    }
+#endif
+
+  if (streamptr->lockIO) CDI_IO_LOCK();
+
   int (*streamOpenDelegate)(const char *filename, char filemode, int filetype, stream_t *streamptr, int recordBufIsToBeCreated)
       = (int (*)(const char *, char, int, stream_t *, int)) namespaceSwitchGet(NSSWITCH_STREAM_OPEN_BACKEND).func;
 
@@ -680,10 +692,12 @@ streamOpenID(const char *filename, char filemode, int filetype, int resH)
       if (streamID < 0) return CDI_ELIMIT;
 
       streamptr->filemode = filemode;
-      streamptr->filename = strdupx(filename);
+      streamptr->filename = strdup(filename);
       streamptr->fileID = fileID;
     }
 
+  if (streamptr->lockIO) CDI_IO_UNLOCK();
+
   return streamID;
 }
 
@@ -717,7 +731,7 @@ streamOpenA(const char *filename, const char *filemode, int filetype)
   int streamID = streamptr->self;
 
   streamptr->filemode = tolower(*filemode);
-  streamptr->filename = strdupx(filename);
+  streamptr->filename = strdup(filename);
   streamptr->fileID = fileID;
 
   streamptr->vlistID = vlistCreate();
@@ -1002,12 +1016,12 @@ streamDefaultValue(stream_t *streamptr)
   streamptr->globalatts = 0;
   streamptr->localatts = 0;
   streamptr->unreduced = cdiDataUnreduced;
-  streamptr->sortname = cdiSortName > 0;
-  streamptr->sortparam = cdiSortParam > 0;
   streamptr->have_missval = cdiHaveMissval;
   streamptr->comptype = CDI_COMPRESS_NONE;
   streamptr->complevel = 0;
   streamptr->shuffle = 0;
+  streamptr->sortname = (cdiSortName > 0);
+  streamptr->lockIO = CDI_Lock_IO;
   // netcdf4/HDF5 filter
   streamptr->filterId = 0;
   streamptr->numParams = 0;
@@ -1053,6 +1067,11 @@ streamDefaultValue(stream_t *streamptr)
   streamptr->jobManager = NULL;
 
   streamptr->protocolData = NULL;
+
+#ifdef HAVE_LIBFDB5
+  streamptr->fdbNumItems = 0;
+  streamptr->fdbKeyValueList = NULL;
+#endif
 }
 
 static stream_t *
@@ -1096,7 +1115,7 @@ cdiStreamCloseDefaultDelegate(stream_t *streamptr, int recordBufIsToBeDeleted)
 
     case CDI_PROTOCOL_FDB:
 #ifdef HAVE_LIBFDB5
-      if (streamptr->protocolData) fdb_delete_handle(streamptr->protocolData);
+      if (streamptr->protocolData) check_fdb_error(fdb_delete_handle(streamptr->protocolData));
       streamptr->protocolData = NULL;
 #endif
       return;
@@ -1237,9 +1256,6 @@ streamDestroy(stream_t *streamptr)
       for (int index = 0; index < maxSteps; ++index)
         {
           tsteps_t *tstep = &(streamptr->tsteps[index]);
-#ifdef HAVE_LIBFDB5
-          if (tstep->records && tstep->records->fdbItem) free(tstep->records->fdbItem);
-#endif
           if (tstep->records) Free(tstep->records);
           if (tstep->recIDs) Free(tstep->recIDs);
           taxisDestroyKernel(&(tstep->taxis));
@@ -1249,6 +1265,15 @@ streamDestroy(stream_t *streamptr)
       streamptr->tsteps = NULL;
     }
 
+#ifdef HAVE_LIBFDB5
+  if (streamptr->fdbKeyValueList)
+    {
+      cdi_fdb_delete_kvlist(streamptr->fdbNumItems, streamptr->fdbKeyValueList);
+      streamptr->fdbNumItems = 0;
+      streamptr->fdbKeyValueList = NULL;
+    }
+#endif
+
   if (vlistID != -1)
     {
       int taxisID = (streamptr->filemode != 'w') ? vlistInqTaxis(vlistID) : -1;
@@ -1287,10 +1312,15 @@ streamClose(int streamID)
 {
   stream_t *streamptr = stream_to_pointer(streamID);
 
+  bool lockIO = streamptr->lockIO;
+  if (lockIO) CDI_IO_LOCK();
+
   if (CDI_Debug) Message("streamID = %d filename = %s", streamID, streamptr->filename);
   streamDestroy(streamptr);
   reshRemove(streamID, &streamOps);
   if (CDI_Debug) Message("Removed stream %d from stream list", streamID);
+
+  if (lockIO) CDI_IO_UNLOCK();
 }
 
 void
@@ -1418,9 +1448,16 @@ int
 streamDefTimestep(int streamID, int tsID)
 {
   stream_t *streamptr = stream_to_pointer(streamID);
+
+  if (streamptr->lockIO) CDI_IO_LOCK();
+
   int (*myStreamDefTimestep_)(stream_t * streamptr, int tsID)
       = (int (*)(stream_t *, int)) namespaceSwitchGet(NSSWITCH_STREAM_DEF_TIMESTEP_).func;
-  return myStreamDefTimestep_(streamptr, tsID);
+  int status = myStreamDefTimestep_(streamptr, tsID);
+
+  if (streamptr->lockIO) CDI_IO_UNLOCK();
+
+  return status;
 }
 
 int
@@ -1476,6 +1513,8 @@ streamInqTimestep(int streamID, int tsID)
 
   if (CDI_Debug) Message("streamID = %d  tsID = %d  filetype = %d", streamID, tsID, filetype);
 
+  if (streamptr->lockIO) CDI_IO_LOCK();
+
   switch (cdiBaseFiletype(filetype))
     {
 #ifdef HAVE_LIBGRIB
@@ -1527,6 +1566,8 @@ streamInqTimestep(int streamID, int tsID)
       }
     }
 
+  if (streamptr->lockIO) CDI_IO_UNLOCK();
+
   int taxisID = vlistInqTaxis(vlistID);
   if (taxisID == -1) Error("Timestep undefined for fileID = %d", streamID);
 
@@ -1576,10 +1617,14 @@ cdiStreamDefVlist_(int streamID, int vlistID)
 
   if (streamptr->vlistID == CDI_UNDEFID)
     {
+      if (streamptr->lockIO) CDI_IO_LOCK();
+
       int vlistCopy = vlistDuplicate(vlistID);
       cdiVlistMakeInternal(vlistCopy);
       cdiVlistMakeImmutable(vlistID);
       cdiStreamSetupVlist(streamptr, vlistCopy);
+
+      if (streamptr->lockIO) CDI_IO_UNLOCK();
     }
   else
     Warning("vlist already defined for %s!", streamptr->filename);
diff --git a/src/stream_cdf.h b/src/stream_cdf.h
index 088348664ac6e41182ce8b0571de008e1fe389e2..db783aa54e401921b3429cd2c97c54003501bb5c 100644
--- a/src/stream_cdf.h
+++ b/src/stream_cdf.h
@@ -36,16 +36,16 @@ void cdfCopyRecord(stream_t *streamptr2, stream_t *streamptr1);
 
 void cdfDefineAttributes(int filetype, int vlistID, int varID, int fileID, int ncvarID);
 
-void cdf_read_record(stream_t *streamptr, int memtype, void *data, size_t *nmiss);
-void cdf_write_record(stream_t *streamptr, int memtype, const void *data, size_t nmiss);
+void cdf_read_record(stream_t *streamptr, int memtype, void *data, size_t *numMissVals);
+void cdf_write_record(stream_t *streamptr, int memtype, const void *data, size_t numMissVals);
 
-void cdf_read_var(stream_t *streamptr, int varID, int memtype, void *data, size_t *nmiss);
-void cdf_write_var(stream_t *streamptr, int varID, int memtype, const void *data, size_t nmiss);
+void cdf_read_var(stream_t *streamptr, int varID, int memtype, void *data, size_t *numMissVals);
+void cdf_write_var(stream_t *streamptr, int varID, int memtype, const void *data, size_t numMissVals);
 
-void cdf_read_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, void *data, size_t *nmiss);
-void cdf_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, const void *data, size_t nmiss);
+void cdf_read_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, void *data, size_t *numMissVals);
+void cdf_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, const void *data, size_t numMissVals);
 
-void cdf_write_var_chunk(stream_t *streamptr, int varID, int memtype, const int rect[][2], const void *data, size_t nmiss);
+void cdf_write_var_chunk(stream_t *streamptr, int varID, int memtype, const int rect[][2], const void *data, size_t numMissVals);
 
 void cdfDefVarDeflate(int ncid, int ncvarid, int shuffle, int deflateLevel);
 void cdfDefTime(stream_t *streamptr);
diff --git a/src/stream_cdf_i.c b/src/stream_cdf_i.c
index b1ef6f1859a3a4eea8fefb1f5493f861167207e6..e75a983c631c1b89c54a19069eeb77b67c18f0fd 100644
--- a/src/stream_cdf_i.c
+++ b/src/stream_cdf_i.c
@@ -37,10 +37,10 @@ static int axisTypeChar[] = { '?', 'X', 'Y', 'Z', 'E', 'T' };
 
 typedef struct
 {
-  int dimid;
-  int ncvarid;
-  int dimtype;
-  size_t len;
+  int dimid;    // NetCDF dim ID
+  int ncvarid;  // NetCDF var ID
+  int dimtype;  // AxisType
+  size_t len;   // Dimension size
   char name[CDI_MAX_NAME];
 } ncdim_t;
 
@@ -100,8 +100,8 @@ typedef struct
   int gmapid;
   int positive;
   int ndims;
-  int dimids[MAX_DIMS_CDF];
-  int dimtype[MAX_DIMS_CDF];
+  int dimids[MAX_DIMS_CDF];    // Netcdf dimension IDs
+  int dimtypes[MAX_DIMS_CDF];  // AxisType
   size_t chunks[MAX_DIMS_CDF];
   bool isChunked;
   int chunkType;
@@ -635,7 +635,7 @@ init_ncvars(int nvars, ncvar_t *ncvars, int ncid)
       ncvar->positive = 0;
       ncvar->ndims = 0;
       for (int i = 0; i < MAX_DIMS_CDF; ++i) ncvar->dimids[i] = CDI_UNDEFID;
-      for (int i = 0; i < MAX_DIMS_CDF; ++i) ncvar->dimtype[i] = CDI_UNDEFID;
+      for (int i = 0; i < MAX_DIMS_CDF; ++i) ncvar->dimtypes[i] = CDI_UNDEFID;
       for (int i = 0; i < MAX_DIMS_CDF; ++i) ncvar->chunks[i] = 0;
       ncvar->isChunked = false;
       ncvar->chunkType = CDI_UNDEFID;
@@ -689,13 +689,13 @@ cdf_set_var(ncvar_t *ncvar, int varStatus)
 static void
 cdf_set_dim(ncvar_t *ncvar, int dimid, int dimtype)
 {
-  if (ncvar->dimtype[dimid] != CDI_UNDEFID && ncvar->dimtype[dimid] != dimtype)
+  if (ncvar->dimtypes[dimid] != CDI_UNDEFID && ncvar->dimtypes[dimid] != dimtype)
     {
       Warning("Inconsistent dimension definition for %s! dimid=%d  type=%c  newtype=%c", ncvar->name, dimid,
-              axisTypeChar[ncvar->dimtype[dimid]], axisTypeChar[dimtype]);
+              axisTypeChar[ncvar->dimtypes[dimid]], axisTypeChar[dimtype]);
     }
 
-  ncvar->dimtype[dimid] = dimtype;
+  ncvar->dimtypes[dimid] = dimtype;
 }
 
 static void
@@ -818,7 +818,7 @@ isHybridSigmaPressureCoordinate(int ncid, int ncvarid, ncvar_t *ncvars, const nc
               int dimid2 = ncvars[avarid2].dimids[0];
               size_t dimlen2 = ncdims[dimid2].len;
 
-              if ((ndims2 == 2 && dimid == ncvars[avarid2].dimids[0]) || (ndims2 == 1 && dimlen == dimlen2 - 1))
+              if ((ndims2 == 2 && dimid == dimid2) || (ndims2 == 1 && dimlen == dimlen2 - 1))
                 {
                   double px = 1;
                   if (p0varid1 != -1 && p0varid1 == p0varid2) cdf_get_var_double(ncid, p0varid2, &px);
@@ -926,12 +926,12 @@ cdf_print_vars(const ncvar_t *ncvars, int nvars, const char *oname)
           axis[ndim++] = ':';
           for (int i = 0; i < ncvar->ndims; i++)
             {
-              if      (ncvar->dimtype[i] == T_AXIS) axis[ndim++] = TAXIS;
-              else if (ncvar->dimtype[i] == Z_AXIS) axis[ndim++] = ZAXIS;
-              else if (ncvar->dimtype[i] == E_AXIS) axis[ndim++] = EAXIS;
-              else if (ncvar->dimtype[i] == Y_AXIS) axis[ndim++] = YAXIS;
-              else if (ncvar->dimtype[i] == X_AXIS) axis[ndim++] = XAXIS;
-              else                                  axis[ndim++] = '?';
+              if      (ncvar->dimtypes[i] == T_AXIS) axis[ndim++] = TAXIS;
+              else if (ncvar->dimtypes[i] == Z_AXIS) axis[ndim++] = ZAXIS;
+              else if (ncvar->dimtypes[i] == E_AXIS) axis[ndim++] = EAXIS;
+              else if (ncvar->dimtypes[i] == Y_AXIS) axis[ndim++] = YAXIS;
+              else if (ncvar->dimtypes[i] == X_AXIS) axis[ndim++] = XAXIS;
+              else                                   axis[ndim++] = '?';
             }
         }
       else
@@ -990,26 +990,24 @@ cdfScanAttrAxis(ncvar_t *ncvars, ncdim_t *ncdims, int ncvarid, const char *attst
         case 'Z':
           ncvar->zdim = dimidsp[attlen];
           dimtype = Z_AXIS;
-
-          setVar = ncvar->ndims == 1;
+          setVar = (ncvar->ndims == 1);
           break;
         case 'y':
         case 'Y':
           ncvar->ydim = dimidsp[attlen];
           dimtype = Y_AXIS;
-
-          setVar = ncvar->ndims == 1;
+          setVar = (ncvar->ndims == 1);
           break;
         case 'x':
         case 'X':
           ncvar->xdim = dimidsp[attlen];
           dimtype = X_AXIS;
-
-          setVar = ncvar->ndims == 1;
+          setVar = (ncvar->ndims == 1);
           break;
         default: continue;
         }
       cdf_set_dim(ncvar, attlen, dimtype);
+
       if (setVar)
         {
           cdf_set_var(ncvar, CoordVar);
@@ -1059,7 +1057,7 @@ set_extra_attr(char *buf, int nvdims, const size_t *chunks)
   pos += sizeof(prefix) - 1;
   for (int i = nvdims - 1; i >= 0; --i)
     {
-      pos += (size_t) (sprintf(buf + pos, "%zu%s", chunks[i], i > 0 ? "x" : ""));
+      pos += (size_t) (snprintf(buf + pos, CDI_MAX_NAME - pos, "%zu%s", chunks[i], i > 0 ? "x" : ""));
     }
   buf[pos] = ' ';
   buf[pos + 1] = 0;
@@ -1107,7 +1105,7 @@ cdfScanVarAttr(int nvars, ncvar_t *ncvars, int ndims, ncdim_t *ncdims, int timed
             }
       strcpy(ncvar->name, name);
 
-      for (int ncdimid = 0; ncdimid < nvdims; ncdimid++) ncvar->dimtype[ncdimid] = -1;
+      for (int ncdimid = 0; ncdimid < nvdims; ncdimid++) ncvar->dimtypes[ncdimid] = -1;
 
       ncvar->xtype = xtype;
       ncvar->ndims = nvdims;
@@ -1618,7 +1616,7 @@ cdf_set_chunk_info(stream_t *streamptr, int nvars, ncvar_t *ncvars)
               size_t chunkSize = ncvar->chunks[i];
               if (chunkSize > 1)
                 {
-                  int dimType = ncvar->dimtype[i];
+                  int dimType = ncvar->dimtypes[i];
                   // clang-format off
                   if      (dimType == T_AXIS && chunkSize > streamptr->chunkSizeTdim) streamptr->chunkSizeTdim = chunkSize;
                   else if (dimType == Z_AXIS && chunkSize > streamptr->chunkSizeZdim) streamptr->chunkSizeZdim = chunkSize;
@@ -1696,7 +1694,7 @@ cdf_set_dimtype(int nvars, ncvar_t *ncvars, ncdim_t *ncdims)
           if (CDI_Debug)
             {
               Message("var %d %s", ncvarid, ncvar->name);
-              for (int i = 0; i < ndims; i++) printf("  dim%d type=%d  ", i, ncvar->dimtype[i]);
+              for (int i = 0; i < ndims; i++) printf("  dim%d type=%d  ", i, ncvar->dimtypes[i]);
               printf("\n");
             }
         }
@@ -1712,7 +1710,7 @@ cdf_set_dimtype(int nvars, ncvar_t *ncvars, ncdim_t *ncdims)
           int ndims = ncvar->ndims;
           for (int i = 0; i < ndims; i++)
             {
-              int dimtype = ncvar->dimtype[i];
+              int dimtype = ncvar->dimtypes[i];
               lxdim |= (dimtype == X_AXIS);
               lydim |= (dimtype == Y_AXIS);
               lzdim |= (dimtype == Z_AXIS);
@@ -1728,7 +1726,7 @@ cdf_set_dimtype(int nvars, ncvar_t *ncvars, ncdim_t *ncdims)
           if (lxdim && (lydim || ncvar->gridtype == GRID_UNSTRUCTURED))
             for (int i = ndims - 1; i >= 0; i--)
               {
-                if (ncvar->dimtype[i] == -1 && !lzdim)
+                if (ncvar->dimtypes[i] == -1 && !lzdim)
                   {
                     if (lcdim)
                       {
@@ -1752,7 +1750,7 @@ cdf_set_dimtype(int nvars, ncvar_t *ncvars, ncdim_t *ncdims)
       int ndims = ncvar->ndims;
       for (int i = 0; i < ndims; i++)
         {
-          if (ncvar->dimtype[i] == CDI_UNDEFID)
+          if (ncvar->dimtypes[i] == CDI_UNDEFID)
             {
               int ncdimid = ncvar->dimids[i];
               if (ncdims[ncdimid].dimtype == Z_AXIS)
@@ -1774,7 +1772,7 @@ cdf_set_dimtype(int nvars, ncvar_t *ncvars, ncdim_t *ncdims)
           int ndims = ncvar->ndims;
           for (int i = 0; i < ndims; i++)
             {
-              int dimtype = ncvar->dimtype[i];
+              int dimtype = ncvar->dimtypes[i];
               lxdim |= (dimtype == X_AXIS);
               lydim |= (dimtype == Y_AXIS);
               lzdim |= (dimtype == Z_AXIS);
@@ -1790,7 +1788,7 @@ cdf_set_dimtype(int nvars, ncvar_t *ncvars, ncdim_t *ncdims)
           //   if ( ndims > 1 )
           for (int i = ndims - 1; i >= 0; i--)
             {
-              if (ncvar->dimtype[i] == -1)
+              if (ncvar->dimtypes[i] == -1)
                 {
                   int dimtype;
                   if (!lxdim)
@@ -2200,19 +2198,19 @@ cdf_copy_grid_axis_attr(ncvar_t *ncvar, struct gridaxis_t *gridaxis)
 }
 
 static int
-cdf_get_xydimid(int ndims, int *dimids, int *dimtype, int *xdimid, int *ydimid)
+cdf_get_xydimid(int ndims, int *dimids, int *dimtypes, int *xdimid, int *ydimid)
 {
   int nxdims = 0, nydims = 0;
   int xdimids[2] = { -1, -1 }, ydimids[2] = { -1, -1 };
 
   for (int i = 0; i < ndims; i++)
     {
-      if (dimtype[i] == X_AXIS && nxdims < 2)
+      if (dimtypes[i] == X_AXIS && nxdims < 2)
         {
           xdimids[nxdims] = dimids[i];
           nxdims++;
         }
-      else if (dimtype[i] == Y_AXIS && nydims < 2)
+      else if (dimtypes[i] == Y_AXIS && nydims < 2)
         {
           ydimids[nydims] = dimids[i];
           nydims++;
@@ -2757,9 +2755,9 @@ cdf_read_coordinates(stream_t *streamptr, struct cdfLazyGrid *lazyGrid, ncvar_t
   if (grid->size == 0)
     {
       int ndims = ncvar->ndims;
-      int *dimtype = ncvar->dimtype;
-      if (ndims == 0 || (ndims == 1 && dimtype[0] == T_AXIS) || (ndims == 1 && dimtype[0] == Z_AXIS)
-          || (ndims == 2 && dimtype[0] == T_AXIS && dimtype[1] == Z_AXIS))
+      int *dimtypes = ncvar->dimtypes;
+      if (ndims == 0 || (ndims == 1 && dimtypes[0] == T_AXIS) || (ndims == 1 && dimtypes[0] == Z_AXIS)
+          || (ndims == 2 && dimtypes[0] == T_AXIS && dimtypes[1] == Z_AXIS))
         {
           grid->type = GRID_GENERIC;
           grid->size = 1;
@@ -2781,7 +2779,7 @@ static bool
 cdf_set_unstructured_par(ncvar_t *ncvar, grid_t *grid, int *xdimid, int *ydimid, GridInfo *gridInfo, bool readPart)
 {
   int ndims = ncvar->ndims;
-  int *dimtype = ncvar->dimtype;
+  int *dimtypes = ncvar->dimtypes;
 
   int zdimid = CDI_UNDEFID;
   int xdimidx = CDI_UNDEFID, ydimidx = CDI_UNDEFID;
@@ -2789,9 +2787,9 @@ cdf_set_unstructured_par(ncvar_t *ncvar, grid_t *grid, int *xdimid, int *ydimid,
   for (int i = 0; i < ndims; i++)
     {
       // clang-format off
-      if      (dimtype[i] == X_AXIS) xdimidx = i;
-      else if (dimtype[i] == Y_AXIS) ydimidx = i;
-      else if (dimtype[i] == Z_AXIS) zdimid = ncvar->dimids[i];
+      if      (dimtypes[i] == X_AXIS) xdimidx = i;
+      else if (dimtypes[i] == Y_AXIS) ydimidx = i;
+      else if (dimtypes[i] == Z_AXIS) zdimid = ncvar->dimids[i];
       // clang-format on
     }
 
@@ -2799,14 +2797,14 @@ cdf_set_unstructured_par(ncvar_t *ncvar, grid_t *grid, int *xdimid, int *ydimid,
     {
       if (grid->x.size > grid->y.size && grid->y.size < 1000)
         {
-          dimtype[ydimidx] = Z_AXIS;
+          dimtypes[ydimidx] = Z_AXIS;
           *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;
+          dimtypes[xdimidx] = Z_AXIS;
           *xdimid = *ydimid;
           *ydimid = CDI_UNDEFID;
           grid->size = grid->y.size;
@@ -2857,22 +2855,22 @@ cdf_set_grid_to_similar_vars(ncvar_t *ncvar1, ncvar_t *ncvar2, int gridtype, int
       int xdimid2 = CDI_UNDEFID, ydimid2 = CDI_UNDEFID, zdimid2 = CDI_UNDEFID;
       int xdimidx = CDI_UNDEFID, ydimidx = CDI_UNDEFID;
 
-      const int *dimtype2 = ncvar2->dimtype;
+      const int *dimtypes2 = ncvar2->dimtypes;
       const int *dimids2 = ncvar2->dimids;
       int ndims2 = ncvar2->ndims;
       for (int i = 0; i < ndims2; i++)
         {
-          if (dimtype2[i] == X_AXIS)
+          if (dimtypes2[i] == X_AXIS)
             {
               xdimid2 = dimids2[i];
               xdimidx = i;
             }
-          else if (dimtype2[i] == Y_AXIS)
+          else if (dimtypes2[i] == Y_AXIS)
             {
               ydimid2 = dimids2[i];
               ydimidx = i;
             }
-          else if (dimtype2[i] == Z_AXIS)
+          else if (dimtypes2[i] == Z_AXIS)
             {
               zdimid2 = dimids2[i];
             }
@@ -2884,13 +2882,13 @@ cdf_set_grid_to_similar_vars(ncvar_t *ncvar1, ncvar_t *ncvar2, int gridtype, int
             {
               if (xdimid == xdimid2 && ydimid2 != CDI_UNDEFID && zdimid2 == CDI_UNDEFID)
                 {
-                  ncvar2->dimtype[ydimidx] = Z_AXIS;
+                  ncvar2->dimtypes[ydimidx] = Z_AXIS;
                   ydimid2 = CDI_UNDEFID;
                 }
 
               if (xdimid == ydimid2 && xdimid2 != CDI_UNDEFID && zdimid2 == CDI_UNDEFID)
                 {
-                  ncvar2->dimtype[xdimidx] = Z_AXIS;
+                  ncvar2->dimtypes[xdimidx] = Z_AXIS;
                   xdimid2 = ydimid2;
                   ydimid2 = CDI_UNDEFID;
                 }
@@ -2947,11 +2945,11 @@ cdf_define_all_grids(stream_t *streamptr, ncgrid_t *ncgrid, int vlistID, ncdim_t
           GridPart gridPart;
           gridpart_init(&gridPart);
           int ndims = ncvar->ndims;
-          int *dimtype = ncvar->dimtype;
+          int *dimtypes = ncvar->dimtypes;
           int vdimid = CDI_UNDEFID;
           struct addIfNewRes projAdded = { .Id = CDI_UNDEFID, .isNew = 0 }, gridAdded = { .Id = CDI_UNDEFID, .isNew = 0 };
           int xdimid = CDI_UNDEFID, ydimid = CDI_UNDEFID;
-          int nydims = cdf_get_xydimid(ndims, ncvar->dimids, dimtype, &xdimid, &ydimid);
+          int nydims = cdf_get_xydimid(ndims, ncvar->dimids, dimtypes, &xdimid, &ydimid);
 
           int xaxisid = (xdimid != CDI_UNDEFID) ? ncdims[xdimid].ncvarid : CDI_UNDEFID;
           int yaxisid = (ydimid != CDI_UNDEFID) ? ncdims[ydimid].ncvarid : CDI_UNDEFID;
@@ -3179,7 +3177,7 @@ cdf_define_all_zaxes(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int nvar
             {
               for (int i = 0; i < ndims; i++)
                 {
-                  if (ncvar->dimtype[i] == Z_AXIS) zdimid = ncvar->dimids[i];
+                  if (ncvar->dimtypes[i] == Z_AXIS) zdimid = ncvar->dimids[i];
                 }
 
               if (zdimid != CDI_UNDEFID)
@@ -3230,7 +3228,7 @@ cdf_define_all_zaxes(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int nvar
                 }
 #endif
 
-              if (zaxisType == ZAXIS_HYBRID && ncvars[zvarid].vct)
+              if ((zaxisType == ZAXIS_HYBRID || zaxisType == ZAXIS_HYBRID_HALF) && ncvars[zvarid].vct)
                 {
                   vct = ncvars[zvarid].vct;
                   vctsize = ncvars[zvarid].vctsize;
@@ -3358,7 +3356,7 @@ cdf_define_all_zaxes(stream_t *streamptr, int vlistID, ncdim_t *ncdims, int nvar
                 ndims = ncvars[ncvarid2].ndims;
                 for (int i = 0; i < ndims; i++)
                   {
-                    if (ncvars[ncvarid2].dimtype[i] == Z_AXIS) zdimid2 = ncvars[ncvarid2].dimids[i];
+                    if (ncvars[ncvarid2].dimtypes[i] == Z_AXIS) zdimid2 = ncvars[ncvarid2].dimids[i];
                   }
 
                 if (zdimid == zdimid2 /* && zvarid == zvarid2 */)
@@ -3497,8 +3495,8 @@ cdf_xtype_to_numbytes(int xtype)
   else if (xtype == NC_FLOAT )  numBytes = 4;
 #ifdef HAVE_NETCDF4
   else if (xtype == NC_UBYTE )  numBytes = 1;
-  else if (xtype == NC_LONG  )  numBytes = 4;
   else if (xtype == NC_USHORT)  numBytes = 2;
+  else if (xtype == NC_LONG  )  numBytes = 4;
   else if (xtype == NC_UINT  )  numBytes = 4;
 #endif
   // clang-format on
@@ -3513,12 +3511,14 @@ calc_chunk_cache_size(int timedimid, ncvar_t *ncvar)
 
   chunkCacheSize *= (ncvar->dimids[0] == timedimid) ? ncvar->chunks[0] : 1;
 
-  int zdimid = CDI_UNDEFID;
   for (int i = 0; i < ncvar->ndims; i++)
     {
-      if (ncvar->dimtype[i] == Z_AXIS) zdimid = ncvar->dimids[i];
+      if (ncvar->dimtypes[i] == Z_AXIS)
+        {
+          chunkCacheSize *= ncvar->chunks[i];
+          break;
+        }
     }
-  if (zdimid != CDI_UNDEFID) chunkCacheSize *= ncvar->chunks[zdimid];
 
   chunkCacheSize *= cdf_xtype_to_numbytes(ncvar->xtype);
 
@@ -3624,7 +3624,7 @@ cdf_define_all_vars(stream_t *streamptr, int vlistID, int instID, int modelID, i
       const int *dimids = ncvar->dimids;
 
       int ixyz = 0;
-      if (gridInqType(gridID) == GRID_UNSTRUCTURED && ndims - iodim <= 2 && (ydimid == xdimid || ydimid == CDI_UNDEFID))
+      if ((ndims - iodim) <= 2 && (ydimid == xdimid || ydimid == CDI_UNDEFID))
         {
           ixyz = (xdimid == ncdims[dimids[ndims - 1]].dimid) ? 321 : 213;
         }
@@ -3634,9 +3634,9 @@ cdf_define_all_vars(stream_t *streamptr, int vlistID, int instID, int modelID, i
             {
               int dimid = ncdims[dimids[idim]].dimid;
               // clang-format off
-              if      (xdimid == dimid) ixyz += 1*ipow10[ndims-idim-1];
-              else if (ydimid == dimid) ixyz += 2*ipow10[ndims-idim-1];
-              else if (zdimid == dimid) ixyz += 3*ipow10[ndims-idim-1];
+              if      (xdimid == dimid) ixyz += 1 * ipow10[ndims - idim - 1];
+              else if (ydimid == dimid) ixyz += 2 * ipow10[ndims - idim - 1];
+              else if (zdimid == dimid) ixyz += 3 * ipow10[ndims - idim - 1];
               // clang-format on
             }
         }
@@ -4198,11 +4198,11 @@ cdfVerifyVars(int nvars, ncvar_t *ncvars, ncdim_t *ncdims)
           for (int i = 0; i < ncvar->ndims; ++i)
             {
               // clang-format off
-              if      (ncvar->dimtype[i] == T_AXIS) ndims++;
-              else if (ncvar->dimtype[i] == E_AXIS) ndims++;
-              else if (ncvar->dimtype[i] == Z_AXIS) ndims++;
-              else if (ncvar->dimtype[i] == Y_AXIS) ndims++;
-              else if (ncvar->dimtype[i] == X_AXIS) ndims++;
+              if      (ncvar->dimtypes[i] == T_AXIS) ndims++;
+              else if (ncvar->dimtypes[i] == E_AXIS) ndims++;
+              else if (ncvar->dimtypes[i] == Z_AXIS) ndims++;
+              else if (ncvar->dimtypes[i] == Y_AXIS) ndims++;
+              else if (ncvar->dimtypes[i] == X_AXIS) ndims++;
               // clang-format on
             }
 
@@ -4215,7 +4215,7 @@ cdfVerifyVars(int nvars, ncvar_t *ncvars, ncdim_t *ncdims)
           int zdimid = -1;
           for (int i = 0; i < ncvar->ndims; ++i)
             {
-              if (ncvar->dimtype[i] == Z_AXIS) zdimid = ncvar->dimids[i];
+              if (ncvar->dimtypes[i] == Z_AXIS) zdimid = ncvar->dimids[i];
             }
 
           int zaxisID = ncvar->zaxisID;
@@ -4249,10 +4249,18 @@ wrf_read_timestep(int fileID, int nctimevarid, int tsID)
 }
 
 static double
-get_timevalue(int fileID, int nctimevarid, size_t ncStepIndex)
+get_timevalue(int fileID, int nctimevarid, size_t ncStepIndex, double *timevarBuffer)
 {
   double timevalue = 0.0;
-  cdf_get_var1_double(fileID, nctimevarid, &ncStepIndex, &timevalue);
+
+  if (timevarBuffer)
+    {
+      timevalue = timevarBuffer[ncStepIndex];
+    }
+  else
+    {
+      cdf_get_var1_double(fileID, nctimevarid, &ncStepIndex, &timevalue);
+    }
 
   if (timevalue >= NC_FILL_DOUBLE || timevalue < -NC_FILL_DOUBLE) timevalue = 0.0;
 
@@ -4295,8 +4303,11 @@ cdf_read_timesteps(int numTimesteps, stream_t *streamptr, taxis_t *taxis0)
             }
           else
             {
+              double *timevarBuffer = (double *) malloc(numTimesteps * sizeof(double));
+              cdf_get_var_double(fileID, nctimevarid, timevarBuffer);
               for (int tsID = 0; tsID < numTimesteps; ++tsID)
-                vDateTimeList[tsID] = cdi_decode_timeval(get_timevalue(fileID, nctimevarid, tsID), taxis0);
+                vDateTimeList[tsID] = cdi_decode_timeval(get_timevalue(fileID, nctimevarid, tsID, timevarBuffer), taxis0);
+              if (timevarBuffer) free(timevarBuffer);
             }
         }
 
@@ -4367,7 +4378,7 @@ cdf_read_timesteps(int numTimesteps, stream_t *streamptr, taxis_t *taxis0)
                 {
                   size_t ncStepIndex = streamptr->tsteps[tsID].ncStepIndex;
                   taxis_t *taxis = &streamptr->tsteps[tsID].taxis;
-                  cdi_set_forecast_period(get_timevalue(fileID, leadtimeid, ncStepIndex), taxis);
+                  cdi_set_forecast_period(get_timevalue(fileID, leadtimeid, ncStepIndex, NULL), taxis);
                 }
             }
         }
diff --git a/src/stream_cdf_o.c b/src/stream_cdf_o.c
index 19e2cfb99c931be870870c8c36a1d646828de862..cc6126fd8f20cf948bbd6291c6acd4bb614db982 100644
--- a/src/stream_cdf_o.c
+++ b/src/stream_cdf_o.c
@@ -32,9 +32,9 @@ cdfCopyRecord(stream_t *streamptr2, stream_t *streamptr1)
 
   void *data = Malloc(datasize * ((memtype == MEMTYPE_DOUBLE) ? sizeof(double) : sizeof(float)));
 
-  size_t nmiss;
-  cdf_read_record(streamptr1, memtype, data, &nmiss);
-  cdf_write_record(streamptr2, memtype, data, nmiss);
+  size_t numMissVals;
+  cdf_read_record(streamptr1, memtype, data, &numMissVals);
+  cdf_write_record(streamptr2, memtype, data, numMissVals);
 
   Free(data);
 }
@@ -70,7 +70,7 @@ cdfDefComplex(stream_t *streamptr, int gridID, int gridIndex)
     size_t dimlen = 2;
     int fileID = streamptr->fileID;
 
-    bool switchNCMode = streamptr->ncmode == 2;
+    bool switchNCMode = (streamptr->ncmode == 2);
     if (switchNCMode)
       {
         streamptr->ncmode = 1;
@@ -131,7 +131,7 @@ cdfGridInqHalfSize(int gridID)
 }
 
 static void
-cdfDefSPorFC(stream_t *streamptr, int gridID, int gridIndex, char *restrict axisname, int gridRefType)
+cdfDefSPorFC(stream_t *streamptr, int gridID, int gridIndex, char *axisname, size_t maxlen, int gridRefType)
 {
   ncgrid_t *ncgrid = streamptr->ncgrid;
 
@@ -145,12 +145,10 @@ cdfDefSPorFC(stream_t *streamptr, int gridID, int gridIndex, char *restrict axis
   if (dimID == CDI_UNDEFID)
     {
       int fileID = streamptr->fileID;
-      if (iz == 0)
-        axisname[3] = '\0';
-      else
-        sprintf(&axisname[3], "%1d", iz + 1);
+      size_t len = strlen(axisname);
+      if (iz) snprintf(axisname + len, maxlen - len, "%1d", iz + 1);
 
-      bool switchNCMode = streamptr->ncmode == 2;
+      bool switchNCMode = (streamptr->ncmode == 2);
       if (switchNCMode)
         {
           streamptr->ncmode = 1;
@@ -174,15 +172,15 @@ static void
 cdfDefSP(stream_t *streamptr, int gridID, int gridIndex)
 {
   // char longname[] = "Spherical harmonic coefficient";
-  char axisname[5] = "nspX";
-  cdfDefSPorFC(streamptr, gridID, gridIndex, axisname, GRID_SPECTRAL);
+  char axisname[5] = "nsp";
+  cdfDefSPorFC(streamptr, gridID, gridIndex, axisname, sizeof(axisname), GRID_SPECTRAL);
 }
 
 static void
 cdfDefFC(stream_t *streamptr, int gridID, int gridIndex)
 {
-  char axisname[5] = "nfcX";
-  cdfDefSPorFC(streamptr, gridID, gridIndex, axisname, GRID_FOURIER);
+  char axisname[5] = "nfc";
+  cdfDefSPorFC(streamptr, gridID, gridIndex, axisname, sizeof(axisname), GRID_FOURIER);
 }
 
 static const struct cdfDefGridAxisInqs
@@ -268,11 +266,11 @@ cdfDefTrajLatLon(stream_t *streamptr, int gridID, int gridIndex, const struct cd
     {
       int dimNcID = streamptr->basetime.ncvarid;
       int fileID = streamptr->fileID;
-      bool switchNCMode = streamptr->ncmode == 2;
+
+      bool switchNCMode = (streamptr->ncmode == 2);
       if (switchNCMode)
         {
           cdf_redef(fileID);
-          switchNCMode = true;
           streamptr->ncmode = 1;
         }
 
@@ -282,6 +280,7 @@ cdfDefTrajLatLon(stream_t *streamptr, int gridID, int gridIndex, const struct cd
       cdiInqKeyString(gridID, axistype, CDI_KEY_NAME, axisname, &length);
       cdf_def_var(fileID, axisname, xtype, 1, &dimNcID, &ncvarid);
       cdfPutGridStdAtts(fileID, ncvarid, gridID, inqs->axisSym);
+
       if (switchNCMode)
         {
           cdf_enddef(fileID, streamptr->self);
@@ -319,7 +318,7 @@ checkDimName(int fileID, size_t dimlen, char *dimname)
 
   do
     {
-      if (iz) sprintf(name + len, "_%u", iz + 1);
+      if (iz) snprintf(name + len, CDI_MAX_NAME - len, "_%u", iz + 1);
 
       int dimid0;
       int status = nc_inq_dimid(fileID, name, &dimid0);
@@ -335,7 +334,7 @@ checkDimName(int fileID, size_t dimlen, char *dimname)
     }
   while (iz <= 99);
 
-  if (iz) sprintf(dimname + len, "_%u", iz + 1);
+  if (iz) snprintf(dimname + len, CDI_MAX_NAME - len, "_%u", iz + 1);
 
   return dimid;
 }
@@ -354,7 +353,7 @@ checkGridName(char *axisname, int fileID)
 
   do
     {
-      if (iz) sprintf(axisname2 + axisnameLen, "_%u", iz + 1);
+      if (iz) snprintf(axisname2 + axisnameLen, CDI_MAX_NAME - axisnameLen, "_%u", iz + 1);
 
       if (nc_inq_varid(fileID, axisname2, &ncdimid) != NC_NOERR) break;
 
@@ -362,7 +361,7 @@ checkGridName(char *axisname, int fileID)
     }
   while (iz <= 99);
 
-  if (iz) sprintf(axisname + axisnameLen, "_%u", iz + 1);
+  if (iz) snprintf(axisname + axisnameLen, CDI_MAX_NAME - axisnameLen, "_%u", iz + 1);
 }
 
 static int
@@ -377,7 +376,7 @@ checkZaxisName(char *axisname, int fileID, int vlistID, int zaxisID, int nzaxis)
   memcpy(axisname2, axisname, axisnameLen + 1);
   do
     {
-      if (iz) sprintf(axisname2 + axisnameLen, "_%u", iz + 1);
+      if (iz) snprintf(axisname2 + axisnameLen, CDI_MAX_NAME - axisnameLen, "_%u", iz + 1);
 
       int ncdimid;
       int status = nc_inq_varid(fileID, axisname2, &ncdimid);
@@ -398,12 +397,13 @@ checkZaxisName(char *axisname, int fileID, int vlistID, int zaxisID, int nzaxis)
             }
           break;
         }
+
     nextSuffix:
       ++iz;
     }
   while (iz <= 99);
 
-  if (iz) sprintf(axisname + axisnameLen, "_%u", iz + 1);
+  if (iz) snprintf(axisname + axisnameLen, CDI_MAX_NAME - axisnameLen, "_%u", iz + 1);
 
   return (int) iz;
 }
@@ -565,7 +565,7 @@ cdfDefAxisCommon(stream_t *streamptr, int gridID, int gridIndex, int ndims, bool
   char dimname[CDI_MAX_NAME + 3];
   dimname[0] = 0;
   int length = sizeof(dimname);
-  if (ndims && hasVals) cdiInqKeyString(gridID, axisKey, CDI_KEY_DIMNAME, dimname, &length);
+  if (ndims && !hasVals) cdiInqKeyString(gridID, axisKey, CDI_KEY_DIMNAME, dimname, &length);
 
   for (int index = 0; index < gridIndex; ++index)
     {
@@ -606,7 +606,7 @@ cdfDefAxisCommon(stream_t *streamptr, int gridID, int gridIndex, int ndims, bool
       checkGridName(axisname, fileID);
       size_t axisnameLen = strlen(axisname);
 
-      bool switchNCMode = streamptr->ncmode == 2 && (hasVals || ndims);
+      bool switchNCMode = (streamptr->ncmode == 2 && (hasVals || ndims));
       if (switchNCMode)
         {
           cdf_redef(fileID);
@@ -646,7 +646,7 @@ cdfDefAxisCommon(stream_t *streamptr, int gridID, int gridIndex, int ndims, bool
           mycdfPostDefActionGridProp(streamptr, gridID, ncvarid, gridAxisInq->valsQueryKey, &delayed);
 
           bool genBounds = false, hasBounds = gridInqPropPresence(gridID, gridAxisInq->bndsQueryKey);
-          bool grid_is_cyclic = gridIsCircular(gridID) > 0;
+          bool grid_is_cyclic = (gridIsCircular(gridID) > 0);
           double *restrict pbounds;
           size_t nvertex = gridInqNvertex(gridID);
           if (CDI_CMOR_Mode && grid_is_cyclic && !hasBounds)
@@ -696,6 +696,7 @@ cdfDefAxisCommon(stream_t *streamptr, int gridID, int gridIndex, int ndims, bool
 
   ncgrid[gridIndex].gridID = gridID;
   ncgrid[gridIndex].ncIDs[(axisLetter == 'X') ? CDF_DIMID_X : CDF_DIMID_Y] = dimID;
+
   return delayed;
 }
 
@@ -830,7 +831,8 @@ cdfDefIrregularGridCommon(stream_t *streamptr, int gridID, size_t xsize, size_t
   int xdimID = CDI_UNDEFID;
   int ydimID = CDI_UNDEFID;
   int fileID = streamptr->fileID;
-  bool switchNCMode = streamptr->ncmode == 2;
+
+  bool switchNCMode = (streamptr->ncmode == 2);
   if (switchNCMode)
     {
       cdf_redef(fileID);
@@ -1082,7 +1084,7 @@ cdf_def_vct_echam(stream_t *streamptr, int zaxisID)
 
       int fileID = streamptr->fileID;
 
-      bool switchNCMode = streamptr->ncmode == 2;
+      bool switchNCMode = (streamptr->ncmode == 2);
       if (switchNCMode)
         {
           streamptr->ncmode = 1;
@@ -1184,7 +1186,7 @@ cdf_def_vct_cf(stream_t *streamptr, int zaxisID, int nclevID, int ncbndsID, int
 
       int fileID = streamptr->fileID;
 
-      bool switchNCMode = streamptr->ncmode == 2;
+      bool switchNCMode = (streamptr->ncmode == 2);
       if (switchNCMode)
         {
           cdf_redef(fileID);
@@ -1295,7 +1297,8 @@ cdf_def_zaxis_hybrid_echam(stream_t *streamptr, int type, int *ncvaridp, int zax
 {
   int fileID = streamptr->fileID;
   struct cdfPostDefActionList *delayed = NULL;
-  bool switchNCMode = streamptr->ncmode == 2;
+
+  bool switchNCMode = (streamptr->ncmode == 2);
   if (switchNCMode)
     {
       streamptr->ncmode = 1;
@@ -1367,7 +1370,8 @@ cdf_def_zaxis_hybrid_cf(stream_t *streamptr, int type, int *ncvaridp, int zaxisI
   if (psname[0] == 0) strcpy(psname, "ps");
 
   int fileID = streamptr->fileID;
-  bool switchNCMode = streamptr->ncmode == 2;
+
+  bool switchNCMode = (streamptr->ncmode == 2);
   if (switchNCMode)
     {
       streamptr->ncmode = 1;
@@ -1493,9 +1497,10 @@ cdf_def_zaxis_hybrid_cf(stream_t *streamptr, int type, int *ncvaridp, int zaxisI
         cdf_put_att_text(fileID, ncvarid, "bounds", axisnameLen, axisname);
         size_t formulatermsLen;
         if (p0status == 0)
-          formulatermsLen = (size_t) (sprintf(txt, "%s%s %s%s", "a: a_bnds b: b_bnds p0: ", p0name, "ps: ", psname));
+          formulatermsLen = (size_t) (snprintf(txt, sizeof(txt), "%s%s %s%s", "a: a_bnds b: b_bnds p0: ", p0name, "ps: ", psname));
         else
-          formulatermsLen = (size_t) (sprintf(txt, "%s%s", "ap: ap_bnds b: b_bnds ps: ", psname));
+          formulatermsLen = (size_t) (snprintf(txt, sizeof(txt), "%s%s", "ap: ap_bnds b: b_bnds ps: ", psname));
+
         {
           static const char sname[] = "standard_name", sname_v[] = "atmosphere_hybrid_sigma_pressure_coordinate",
                             formulaterms[] = "formula_terms", units[] = "units";
@@ -1571,7 +1576,8 @@ cdfDefZaxisUUID(stream_t *streamptr, int zaxisID)
       if (cdiUUID2Str(uuid, uuidStr) == uuidNumHexChars)
         {
           int fileID = streamptr->fileID;
-          bool switchNCMode = streamptr->ncmode == 2;
+
+          bool switchNCMode = (streamptr->ncmode == 2);
           if (switchNCMode)
             {
               streamptr->ncmode = 1;
@@ -1579,6 +1585,7 @@ cdfDefZaxisUUID(stream_t *streamptr, int zaxisID)
             }
 
           cdf_put_att_text(fileID, NC_GLOBAL, "uuidOfVGrid", uuidNumHexChars, uuidStr);
+
           if (switchNCMode)
             {
               cdf_enddef(fileID, streamptr->self);
@@ -1842,6 +1849,7 @@ cdf_def_mapping(stream_t *streamptr, int gridID)
   if (!gmapvarname[0]) strcpy(gmapvarname, "crs");
 
   int fileID = streamptr->fileID;
+
   bool switchNCMode = (streamptr->ncmode == 2);
   if (switchNCMode)
     {
@@ -1953,7 +1961,7 @@ cdfDefCharacter(stream_t *streamptr, int gridID, int gridIndex, int cdiAxisID, i
 }
 
 static void
-cdfDefRgrid(stream_t *streamptr, int gridID, int gridIndex)
+cdfDefReducedGrid(stream_t *streamptr, int gridID, int gridIndex)
 {
   ncgrid_t *ncgrid = streamptr->ncgrid;
 
@@ -1971,11 +1979,9 @@ cdfDefRgrid(stream_t *streamptr, int gridID, int gridIndex)
       {
         int fileID = streamptr->fileID;
 
-        char axisname[16] = "rgridX";
-        if (iz == 0)
-          axisname[5] = '\0';
-        else
-          sprintf(&axisname[5], "%1d", iz + 1);
+        char axisname[16] = "rgrid";
+        size_t len = strlen(axisname);
+        if (iz) snprintf(axisname + len, sizeof(axisname) - len, "%1d", iz + 1);
 
         bool switchNCMode = (streamptr->ncmode == 2);
         if (switchNCMode)
@@ -2008,29 +2014,19 @@ cdfDefRgrid(stream_t *streamptr, int gridID, int gridIndex)
       {
         int fileID = streamptr->fileID;
 
-        char axisname[32] = "reduced_pointsX";
-        if (iz == 0)
-          axisname[14] = '\0';
-        else
-          sprintf(&axisname[14], "%1d", iz + 1);
+        char axisname[32] = "reduced_points";
+        size_t len = strlen(axisname);
+        if (iz) snprintf(axisname + len, sizeof(axisname) - len, "%1d", iz + 1);
 
-        bool switchNCMode = (streamptr->ncmode == 2);
-        if (switchNCMode)
-          {
-            streamptr->ncmode = 1;
-            cdf_redef(fileID);
-          }
+        if (streamptr->ncmode == 2) cdf_redef(fileID);
 
         cdf_def_dim(fileID, axisname, dimlen, &dimID);
 
         int ncvarid = CDI_UNDEFID;
         cdf_def_var(fileID, axisname, NC_INT, 1, &dimID, &ncvarid);
 
-        if (switchNCMode || streamptr->ncmode == 1)
-          {
-            cdf_enddef(fileID, streamptr->self);
-            streamptr->ncmode = 2;
-          }
+        cdf_enddef(fileID, streamptr->self);
+        streamptr->ncmode = 2;
 
         int *reducedPoints = (int *) Malloc(dimlen * sizeof(int));
         gridInqReducedPoints(gridID, reducedPoints);
@@ -2078,7 +2074,7 @@ cdf_define_generic_dim(stream_t *streamptr, int gridID, int gridIndex)
 
       if (dimID == CDI_UNDEFID)
         {
-          bool switchNCMode = streamptr->ncmode == 2;
+          bool switchNCMode = (streamptr->ncmode == 2);
           if (switchNCMode)
             {
               streamptr->ncmode = 1;
@@ -2181,8 +2177,13 @@ cdf_define_grid(stream_t *streamptr, int gridID, int gridIndex)
     }
   else if (gridtype == GRID_GAUSSIAN_REDUCED)
     {
-      cdfDefRgrid(streamptr, gridID, gridIndex);
-      if (gridInqYsize(gridID)) cdfDefYaxis(streamptr, gridID, gridIndex, 1, true);
+      cdfDefReducedGrid(streamptr, gridID, gridIndex);
+      if (gridInqYsize(gridID))
+        {
+          struct cdfPostDefActionList *ydelayed = cdfDefYaxis(streamptr, gridID, gridIndex, 1, true);
+          delayed = cdfPostDefActionConcat(delayed, ydelayed);
+          Free(ydelayed);
+        }
     }
   else if (gridtype == GRID_SPECTRAL)
     {
@@ -2205,12 +2206,20 @@ cdf_define_grid(stream_t *streamptr, int gridID, int gridIndex)
       if ((strlen = gridInqXIsc(gridID)))
         cdfDefCharacter(streamptr, gridID, gridIndex, CDI_XAXIS, strlen);
       else if (gridInqXsize(gridID))
-        cdfDefXaxis(streamptr, gridID, gridIndex, 1, false);
+        {
+          struct cdfPostDefActionList *xdelayed = cdfDefXaxis(streamptr, gridID, gridIndex, 1, false);
+          delayed = cdfPostDefActionConcat(delayed, xdelayed);
+          Free(xdelayed);
+        }
 
       if ((strlen = gridInqYIsc(gridID)))
         cdfDefCharacter(streamptr, gridID, gridIndex, CDI_YAXIS, strlen);
       else if (gridInqYsize(gridID))
-        cdfDefYaxis(streamptr, gridID, gridIndex, 1, false);
+        {
+          struct cdfPostDefActionList *ydelayed = cdfDefYaxis(streamptr, gridID, gridIndex, 1, false);
+          delayed = cdfPostDefActionConcat(delayed, ydelayed);
+          Free(ydelayed);
+        }
     }
   else
     {
diff --git a/src/stream_cdf_time.c b/src/stream_cdf_time.c
index 84c699535ccd0163f070d455ee97f17c18ccab30..29851b32ede1217e9479e5c3ac27ca50177bb4ca 100644
--- a/src/stream_cdf_time.c
+++ b/src/stream_cdf_time.c
@@ -88,7 +88,8 @@ cdfGetTimeUnits(taxis_t *taxis)
             timeunit = TUNIT_HOUR;
 
           char *unitstr_ = ptaxisAllocUnits(taxis, CDF_MAX_TIME_UNIT_STR);
-          sprintf(unitstr_, "%s since %d-%d-%d %02d:%02d:%02d", tunitNamePtr(timeunit), year, month, day, hour, minute, second);
+          snprintf(unitstr_, CDF_MAX_TIME_UNIT_STR, "%s since %d-%d-%d %02d:%02d:%02d", tunitNamePtr(timeunit), year, month, day,
+                   hour, minute, second);
           unitstr = unitstr_;
         }
     }
@@ -270,7 +271,7 @@ cdfDefTimestep(stream_t *streamptr, int tsID, size_t valCount)
   const size_t start[2] = { [0] = (size_t) tsID, [1] = 0 }, count[2] = { [0] = valCount, [1] = 2 * valCount };
 
   double timeValue[2] = { cdi_encode_timeval(taxis->vDateTime, &streamptr->tsteps[0].taxis) };
-  if (CDI_Debug) Message("tsID = %d  timeValue = %f", tsID, timeValue);
+  if (CDI_Debug) Message("tsID = %d  timeValue = %f", tsID, timeValue[0]);
 
   int ncvarid = streamptr->basetime.ncvarid;
   cdf_put_vara_double(fileID, ncvarid, start, count, timeValue);
diff --git a/src/stream_cgribex.c b/src/stream_cgribex.c
index 932a96eb02442c1a32200feb35d8b8e4db125e36..41c5a87a4966dcb990cad73c5996fe82298ae8a0 100644
--- a/src/stream_cgribex.c
+++ b/src/stream_cgribex.c
@@ -37,6 +37,7 @@ typedef struct
   int level2;
   int ltype;
   int tsteptype;
+  size_t gridsize;
 } compvar_t;
 
 typedef struct
@@ -536,6 +537,12 @@ cgribexDefProjLCC(int *isec2, int gridID)
   gridDefParamsLCC(gridID, gpp);
 }
 
+static size_t
+cgribexGetGridsize(const int *isec4)
+{
+  return ISEC4_NumValues;
+}
+
 static void
 cgribexAddRecord(stream_t *streamptr, cgribexrec_t *cgribexp, int param, size_t recsize, off_t position, int comptype, int lmv,
                  int iret)
@@ -569,6 +576,7 @@ cgribexAddRecord(stream_t *streamptr, cgribexrec_t *cgribexp, int param, size_t
   record->ilevel2 = level2;
   record->ltype = leveltype;
   record->tsteptype = (short) tsteptype;
+  record->gridsize = cgribexGetGridsize(cgribexp->sec4);
 
   grid_t *gridptr = (grid_t *) Malloc(sizeof(*gridptr));
   bool uvRelativeToGrid = cgribexGetGrid(streamptr, isec2, isec4, gridptr, iret);
@@ -713,7 +721,7 @@ cgribexDecodeHeader(cgribexrec_t *cgribexp, int *gribbuffer, int recsize, int *l
 }
 
 static compvar_t
-cgribexVarSet(int param, int level1, int level2, int leveltype, int trange)
+cgribexVarSet(int param, int level1, int level2, int leveltype, int trange, size_t gridsize)
 {
   int tsteptype = cgribexGetTsteptype(trange);
 
@@ -723,6 +731,7 @@ cgribexVarSet(int param, int level1, int level2, int leveltype, int trange)
   compVar.level2 = level2;
   compVar.ltype = leveltype;
   compVar.tsteptype = tsteptype;
+  compVar.gridsize = gridsize;
 
   return compVar;
 }
@@ -735,7 +744,7 @@ cgribexVarCompare(const compvar_t *compVar, const record_t *record, int flag)
   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)
-                | (compVar->ltype != record->ltype) | tstepDiff;
+                | (compVar->ltype != record->ltype) | (compVar->gridsize != record->gridsize) | tstepDiff;
   return rstatus;
 }
 
@@ -852,7 +861,8 @@ cgribexScanTimestep1(stream_t *streamptr)
         {
           if (cdiDateTime_isLT(sDateTime, taxis->sDateTime)) taxis->sDateTime = sDateTime;
 
-          compvar_t compVar = cgribexVarSet(param, level1, level2, leveltype, ISEC1_TimeRange);
+          size_t gridsize = cgribexGetGridsize(cgribexp->sec4);
+          compvar_t compVar = cgribexVarSet(param, level1, level2, leveltype, ISEC1_TimeRange, gridsize);
           record_t *records = streamptr->tsteps[tsID].records;
           for (recID = 0; recID < nrecs; recID++)
             {
@@ -1026,7 +1036,8 @@ cgribexScanTimestep2(stream_t *streamptr)
             taxis->numavg = ISEC1_AvgNum;
         }
 
-      compvar_t compVar = cgribexVarSet(param, level1, level2, leveltype, ISEC1_TimeRange);
+      size_t gridsize = cgribexGetGridsize(cgribexp->sec4);
+      compvar_t compVar = cgribexVarSet(param, level1, level2, leveltype, ISEC1_TimeRange, gridsize);
 
       for (recID = 0; recID < nrecords; recID++)
         {
@@ -1218,7 +1229,8 @@ cgribexScanTimestep(stream_t *streamptr)
                 taxis->numavg = ISEC1_AvgNum;
             }
 
-          compvar_t compVar = cgribexVarSet(param, level1, level2, leveltype, ISEC1_TimeRange);
+          size_t gridsize = cgribexGetGridsize(cgribexp->sec4);
+          compvar_t compVar = cgribexVarSet(param, level1, level2, leveltype, ISEC1_TimeRange, gridsize);
 
           for (vrecID = 0; vrecID < nrecs; vrecID++)
             {
@@ -1325,7 +1337,7 @@ cgribexScanTimestep(stream_t *streamptr)
 
 int
 cgribexDecode(int memtype, void *cgribex, void *gribbuffer, size_t gribsize, void *data, size_t datasize, int unreduced,
-              size_t *nmiss, double missval)
+              size_t *numMissVals, double missval)
 {
   int status = 0;
 
@@ -1355,14 +1367,14 @@ cgribexDecode(int memtype, void *cgribex, void *gribbuffer, size_t gribsize, voi
     gribExDP(isec0, isec1, isec2, fsec2, isec3, fsec3, isec4, (double *) data, (int) datasize, (int *) gribbuffer, (int) gribsize,
              &iword, hoper, &iret);
 
-  *nmiss = (ISEC1_Sec2Or3Flag & 64) ? ISEC4_NumValues - ISEC4_NumNonMissValues : 0;
+  *numMissVals = (ISEC1_Sec2Or3Flag & 64) ? ISEC4_NumValues - ISEC4_NumNonMissValues : 0;
 
   if (ISEC1_CenterID == 215 && (isec1[34] != 0 && isec1[34] != 255))
     {
       double undef_pds, undef_eps;
       MCH_get_undef(isec1, &undef_pds, &undef_eps);
 
-      *nmiss = 0;
+      *numMissVals = 0;
       if (memtype == MEMTYPE_FLOAT)
         {
           float *restrict dataf = (float *) data;
@@ -1370,7 +1382,7 @@ cgribexDecode(int memtype, void *cgribex, void *gribbuffer, size_t gribsize, voi
             if ((fabs(dataf[i] - undef_pds) < undef_eps) || IS_EQUAL(dataf[i], FSEC3_MissVal))
               {
                 dataf[i] = (float) missval;
-                (*nmiss)++;
+                (*numMissVals)++;
               }
         }
       else
@@ -1380,7 +1392,7 @@ cgribexDecode(int memtype, void *cgribex, void *gribbuffer, size_t gribsize, voi
             if ((fabs(datad[i] - undef_pds) < undef_eps) || IS_EQUAL(datad[i], FSEC3_MissVal))
               {
                 datad[i] = missval;
-                (*nmiss)++;
+                (*numMissVals)++;
               }
         }
     }
@@ -2048,7 +2060,7 @@ cgribexDefEnsembleVar(int *isec1, int vlistID, int varID)
 
 size_t
 cgribexEncode(int memtype, int varID, int levelID, int vlistID, int gridID, int zaxisID, CdiDateTime vDateTime, int tsteptype,
-              int numavg, size_t datasize, const void *data, size_t nmiss, void *gribbuffer, size_t gribbuffersize)
+              int numavg, size_t datasize, const void *data, size_t numMissVals, void *gribbuffer, size_t gribbuffersize)
 {
   cgribexrec_t *cgribexp = (cgribexrec_t *) cgribexNew();
 
@@ -2102,7 +2114,7 @@ cgribexEncode(int memtype, int varID, int levelID, int vlistID, int gridID, int
   ISEC4_NumValues = (int) datasize;
   ISEC4_NumBits = grbBitsPerValue(datatype);
 
-  if (nmiss > 0)
+  if (numMissVals > 0)
     {
       FSEC3_MissVal = vlistInqVarMissval(vlistID, varID);
       ISEC1_Sec2Or3Flag |= 64;
diff --git a/src/stream_cgribex.h b/src/stream_cgribex.h
index 89cc57d5dc9850bf6069f07d55425c1ca539c10a..60ed867ed10b33b6b09b3b077c5d4775b925526c 100644
--- a/src/stream_cgribex.h
+++ b/src/stream_cgribex.h
@@ -9,10 +9,10 @@ int cgribexScanTimestep2(stream_t *streamptr);
 int cgribexScanTimestep(stream_t *streamptr);
 
 int cgribexDecode(int memtype, void *cgribexp, void *gribbuffer, size_t gribsize, void *data, size_t datasize, int unreduced,
-                  size_t *nmiss, double missval);
+                  size_t *numMissVals, double missval);
 
 size_t cgribexEncode(int memtype, int varID, int levelID, int vlistID, int gridID, int zaxisID, CdiDateTime vDateTime,
-                     int tsteptype, int numavg, size_t datasize, const void *data, size_t nmiss, void *gribbuffer,
+                     int tsteptype, int numavg, size_t datasize, const void *data, size_t numMissVals, void *gribbuffer,
                      size_t gribbuffersize);
 
 void *cgribex_handle_new_from_meassage(void *gribbuffer, size_t recsize);
diff --git a/src/stream_ext.c b/src/stream_ext.c
index 9184f631adb5267ebc90a8cc86a5356e3530ee12..f55f2c0d4c9eabdab147649df7ea234afe1548c0 100644
--- a/src/stream_ext.c
+++ b/src/stream_ext.c
@@ -76,7 +76,7 @@ int extInqRecord(stream_t *streamptr, int *varID, int *levelID)
 */
 
 static void
-ext_read_recordSP(stream_t *streamptr, float *data, size_t *nmiss)
+ext_read_recordSP(stream_t *streamptr, float *data, size_t *numMissVals)
 {
   int vlistID = streamptr->vlistID;
   int fileID = streamptr->fileID;
@@ -100,15 +100,15 @@ ext_read_recordSP(stream_t *streamptr, float *data, size_t *nmiss)
   size_t size = gridInqSize(vlistInqVarGrid(vlistID, varID));
 
   if (vlistInqVarNumber(vlistID, varID) == CDI_REAL)
-    *nmiss = get_num_missvalsSP(size, data, missval);
+    *numMissVals = get_num_missvalsSP(size, data, missval);
   else
-    *nmiss = get_cplx_num_missvalsSP(size, data, missval);
+    *numMissVals = get_cplx_num_missvalsSP(size, data, missval);
 
   streamptr->numvals += size;
 }
 
 static void
-ext_read_recordDP(stream_t *streamptr, double *data, size_t *nmiss)
+ext_read_recordDP(stream_t *streamptr, double *data, size_t *numMissVals)
 {
   int vlistID = streamptr->vlistID;
   int fileID = streamptr->fileID;
@@ -132,20 +132,20 @@ ext_read_recordDP(stream_t *streamptr, double *data, size_t *nmiss)
   size_t size = gridInqSize(vlistInqVarGrid(vlistID, varID));
 
   if (vlistInqVarNumber(vlistID, varID) == CDI_REAL)
-    *nmiss = get_num_missvalsDP(size, data, missval);
+    *numMissVals = get_num_missvalsDP(size, data, missval);
   else
-    *nmiss = get_cplx_num_missvalsDP(size, data, missval);
+    *numMissVals = get_cplx_num_missvalsDP(size, data, missval);
 
   streamptr->numvals += size;
 }
 
 void
-ext_read_record(stream_t *streamptr, int memtype, void *data, size_t *nmiss)
+ext_read_record(stream_t *streamptr, int memtype, void *data, size_t *numMissVals)
 {
   if (memtype == MEMTYPE_DOUBLE)
-    ext_read_recordDP(streamptr, (double *) data, nmiss);
+    ext_read_recordDP(streamptr, (double *) data, numMissVals);
   else
-    ext_read_recordSP(streamptr, (float *) data, nmiss);
+    ext_read_recordSP(streamptr, (float *) data, numMissVals);
 }
 
 void
@@ -550,7 +550,7 @@ extInqTimestep(stream_t *streamptr, int tsID)
 }
 
 void
-extReadVarSliceDP(stream_t *streamptr, int varID, int levID, double *data, size_t *nmiss)
+extReadVarSliceDP(stream_t *streamptr, int varID, int levID, double *data, size_t *numMissVals)
 {
   if (CDI_Debug) Message("streamID = %d  varID = %d  levID = %d", streamptr->self, varID, levID);
 
@@ -576,13 +576,13 @@ extReadVarSliceDP(stream_t *streamptr, int varID, int levID, double *data, size_
   fileSetPos(fileID, currentfilepos, SEEK_SET);
 
   if (vlistInqVarNumber(vlistID, varID) == CDI_REAL)
-    *nmiss = get_num_missvalsDP(gridsize, data, missval);
+    *numMissVals = get_num_missvalsDP(gridsize, data, missval);
   else
-    *nmiss = get_cplx_num_missvalsDP(gridsize, data, missval);
+    *numMissVals = get_cplx_num_missvalsDP(gridsize, data, missval);
 }
 
 void
-extReadVarDP(stream_t *streamptr, int varID, double *data, size_t *nmiss)
+extReadVarDP(stream_t *streamptr, int varID, double *data, size_t *numMissVals)
 {
   if (CDI_Debug) Message("streamID = %d  varID = %d", streamptr->self, varID);
 
@@ -590,7 +590,8 @@ extReadVarDP(stream_t *streamptr, int varID, double *data, size_t *nmiss)
   size_t gridsize = gridInqSize(vlistInqVarGrid(vlistID, varID));
   size_t nlevs = (size_t) streamptr->vars[varID].recordTable[0].nlevs;
 
-  for (size_t levID = 0; levID < nlevs; levID++) extReadVarSliceDP(streamptr, varID, (int) levID, &data[levID * gridsize], nmiss);
+  for (size_t levID = 0; levID < nlevs; levID++)
+    extReadVarSliceDP(streamptr, varID, (int) levID, &data[levID * gridsize], numMissVals);
 }
 
 void
diff --git a/src/stream_ext.h b/src/stream_ext.h
index 714e35c40f66ff01ce6041f49fdfa972c3713e86..3f6d3516066c1b0bf4c1c9bf9b5ed09fafd7d17d 100644
--- a/src/stream_ext.h
+++ b/src/stream_ext.h
@@ -11,13 +11,13 @@ int extInqTimestep(stream_t *streamptr, int tsID);
 int extInqRecord(stream_t *streamptr, int *varID, int *levelID);
 void extDefRecord(stream_t *streamptr);
 void extCopyRecord(stream_t *streamptr2, stream_t *streamptr1);
-void ext_read_record(stream_t *streamptr, int memtype, void *data, size_t *nmiss);
+void ext_read_record(stream_t *streamptr, int memtype, void *data, size_t *numMissVals);
 void ext_write_record(stream_t *streamptr, int memtype, const void *data);
 
-void extReadVarDP(stream_t *streamptr, int varID, double *data, size_t *nmiss);
+void extReadVarDP(stream_t *streamptr, int varID, double *data, size_t *numMissVals);
 void extWriteVarDP(stream_t *streamptr, int varID, const double *data);
 
-void extReadVarSliceDP(stream_t *streamptr, int varID, int levelID, double *data, size_t *nmiss);
+void extReadVarSliceDP(stream_t *streamptr, int varID, int levelID, double *data, size_t *numMissVals);
 void extWriteVarSliceDP(stream_t *streamptr, int varID, int levelID, const double *data);
 
 #endif /* _STREAM_EXT_H */
diff --git a/src/stream_grb.c b/src/stream_grb.c
index 3ec32b16d8dae74783f8c5f92c0cfa6f9b57149e..0e9a29f777f3a6677d8d74cf0fe06a5df87e60da 100644
--- a/src/stream_grb.c
+++ b/src/stream_grb.c
@@ -350,8 +350,7 @@ fdbInqContents(stream_t *streamptr)
 {
   streamptr->curTsID = 0;
 #ifdef HAVE_LIBGRIB_API
-  int status = fdbScanTimesteps(streamptr);
-  return status;
+  return fdbScanTimesteps(streamptr);
 #else
   return -1;
 #endif
diff --git a/src/stream_grb.h b/src/stream_grb.h
index cb8797ab659dcf9658e122d8eeb9986f15b57d05..a007c9ca93721b2a6cb2039403413fbfb102acaf 100644
--- a/src/stream_grb.h
+++ b/src/stream_grb.h
@@ -33,15 +33,15 @@ int grbInqTimestep(stream_t *streamptr, int tsID);
 
 int grbInqRecord(stream_t *streamptr, int *varID, int *levelID);
 void grbDefRecord(stream_t *streamptr);
-void grb_read_record(stream_t *streamptr, int memtype, void *data, size_t *nmiss);
-void grb_write_record(stream_t *streamptr, int memtype, const void *data, size_t nmiss);
+void grb_read_record(stream_t *streamptr, int memtype, void *data, size_t *numMissVals);
+void grb_write_record(stream_t *streamptr, int memtype, const void *data, size_t numMissVals);
 void grbCopyRecord(stream_t *streamptr2, stream_t *streamptr1);
 
-void grb_read_var(stream_t *streamptr, int varID, int memtype, void *data, size_t *nmiss);
-void grb_write_var(stream_t *streamptr, int varID, int memtype, const void *data, size_t nmiss);
+void grb_read_var(stream_t *streamptr, int varID, int memtype, void *data, size_t *numMissVals);
+void grb_write_var(stream_t *streamptr, int varID, int memtype, const void *data, size_t numMissVals);
 
-void grb_read_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, void *data, size_t *nmiss);
-void grb_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, const void *data, size_t nmiss);
+void grb_read_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, void *data, size_t *numMissVals);
+void grb_write_var_slice(stream_t *streamptr, int varID, int levelID, int memtype, const void *data, size_t numMissVals);
 
 int grib1ltypeToZaxisType(int grib_ltype);
 int grib2ltypeToZaxisType(int grib_ltype);
diff --git a/src/stream_gribapi.c b/src/stream_gribapi.c
index dafaf62e08fc9460eafc290ea86e451d19d308f1..5bbdafb8a5040199f96aa6cc6ae3ea4fbe92271b 100644
--- a/src/stream_gribapi.c
+++ b/src/stream_gribapi.c
@@ -414,11 +414,11 @@ param_to_name(int param, char *name)
   cdiDecodeParam(param, &pnum, &pcat, &pdis);
   if (pdis == 255)
     {
-      sprintf(name, "code%d", pnum);
+      snprintf(name, 256, "code%d", pnum);
     }
   else
     {
-      sprintf(name, "param%d.%d.%d", pnum, pcat, pdis);
+      snprintf(name, 256, "param%d.%d.%d", pnum, pcat, pdis);
     }
 }
 
@@ -699,7 +699,7 @@ gribapiDefProjHEALPIX(grib_handle *gh, int gridID)
 static void
 gribapiAddRecord(stream_t *streamptr, int param, grib_handle *gh, size_t recsize, off_t position, int datatype, int comptype,
                  const char *varname, int leveltype1, int leveltype2, int lbounds, int level1, int level2, int level_sf,
-                 int level_unit, VarScanKeys *scanKeys, const var_tile_t *tiles, bool lread_additional_keys, void *fdbItem)
+                 int level_unit, VarScanKeys *scanKeys, const var_tile_t *tiles, bool lread_additional_keys, int fdbItemIndex)
 {
   int vlistID = streamptr->vlistID;
   int tsID = streamptr->curTsID;
@@ -721,9 +721,9 @@ gribapiAddRecord(stream_t *streamptr, int param, grib_handle *gh, size_t recsize
   record->scanKeys = *scanKeys;
   record->tiles = tiles ? *tiles : dummy_tiles;
 #ifdef HAVE_LIBFDB5
-  record->fdbItem = fdbItem;
+  record->fdbItemIndex = fdbItemIndex;
 #else
-  (void) fdbItem;
+  (void) fdbItemIndex;
 #endif
 
   strncpy(record->varname, varname, sizeof(record->varname) - 1);
@@ -743,21 +743,21 @@ gribapiAddRecord(stream_t *streamptr, int param, grib_handle *gh, size_t recsize
 
   // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
 
-  grid_t *grid = (grid_t *) Malloc(sizeof(*grid));
-  bool uvRelativeToGrid = gribapiGetGrid(gh, grid);
+  grid_t *gridptr = (grid_t *) Malloc(sizeof(*gridptr));
+  bool uvRelativeToGrid = gribapiGetGrid(gh, gridptr);
 
-  struct addIfNewRes gridAdded = cdiVlistAddGridIfNew(vlistID, grid, 0);
+  struct addIfNewRes gridAdded = cdiVlistAddGridIfNew(vlistID, gridptr, 0);
   int gridID = gridAdded.Id;
   // clang-format off
   if (!gridAdded.isNew)
     {
-      grid_free(grid);
-      Free(grid);
+      grid_free(gridptr);
+      Free(gridptr);
     }
-  else if (grid->projtype == CDI_PROJ_RLL)     gribapiDefProjRLL(gh, gridID);
-  else if (grid->projtype == CDI_PROJ_LCC)     gribapiDefProjLCC(gh, gridID);
-  else if (grid->projtype == CDI_PROJ_STERE)   gribapiDefProjSTERE(gh, gridID);
-  else if (grid->projtype == CDI_PROJ_HEALPIX) gribapiDefProjHEALPIX(gh, gridID);
+  else if (gridptr->projtype == CDI_PROJ_RLL)     gribapiDefProjRLL(gh, gridID);
+  else if (gridptr->projtype == CDI_PROJ_LCC)     gribapiDefProjLCC(gh, gridID);
+  else if (gridptr->projtype == CDI_PROJ_STERE)   gribapiDefProjSTERE(gh, gridID);
+  else if (gridptr->projtype == CDI_PROJ_HEALPIX) gribapiDefProjHEALPIX(gh, gridID);
   // clang-format on
 
   int zaxistype = gribapi_get_zaxis_type(gribEditionNumber(gh), leveltype1);
@@ -786,7 +786,7 @@ gribapiAddRecord(stream_t *streamptr, int param, grib_handle *gh, size_t recsize
         unsigned char uuid[CDI_UUID_SIZE];
         long lpar;
         GRIB_CHECK(grib_get_long(gh, "NV", &lpar), 0);
-        if (lpar != 6) fprintf(stderr, "Warning ...\n");
+        // if (lpar != 6) fprintf(stderr, "Warning ...\n");
         GRIB_CHECK(grib_get_long(gh, "nlev", &lpar), 0);
         int nhlev = (int) lpar;
         GRIB_CHECK(grib_get_long(gh, "numberOfVGridUsed", &lpar), 0);
@@ -1032,41 +1032,45 @@ fdbScanTimesteps(stream_t *streamptr)
 
   fdb_handle_t *fdbHandle = streamptr->protocolData;
 
-  char **fdbItemList = NULL;
-  fdb_request_t *request = create_fdb_request(streamptr->filename);
-  int numItems = fdb_fill_itemlist(fdbHandle, request, &fdbItemList);
+  fdb_request_t *request = cdi_create_fdb_request(streamptr->filename);
+
+  fdbKeyValueEntry *keyValueList = NULL;
+  int numItems = cdi_fdb_fill_kvlist(fdbHandle, request, &keyValueList);
   fdb_delete_request(request);
-  if (numItems == 0) Error("FDB request does not find any database entries!");
+  if (numItems == 0) Error("FDB request doesn't find any database entries!");
   if (CDI_Debug)
-    for (int i = 0; i < numItems; ++i) printf("item[%d] = %s\n", i + 1, fdbItemList[i]);
+    {
+      printf("Original FDB items:\n");
+      print_keyvalueList(numItems, keyValueList);
+    }
 
-  KeyValueEntry *keyValueList = (KeyValueEntry *) malloc(numItems * sizeof(KeyValueEntry));
-  for (int i = 0; i < numItems; ++i) keyValueList[i].item = NULL;
+  // if (check_keyvalueList(numItems, keyValueList) != 0) return CDI_EUFSTRUCT;
 
   RecordInfoEntry *recordInfoList = (RecordInfoEntry *) malloc(numItems * sizeof(RecordInfoEntry));
-  for (int i = 0; i < numItems; ++i) record_info_entry_init(&recordInfoList[i]);
+  if (decode_keyvalue(numItems, keyValueList, recordInfoList) != 0) return CDI_EUFSTRUCT;
 
-  for (int i = 0; i < numItems; ++i) decode_fdbitem(fdbItemList[i], &keyValueList[i]);
-  if (check_keyvalueList(numItems, keyValueList) != 0) return 1;
-  for (int i = 0; i < numItems; ++i) decode_keyvalue(&keyValueList[i], &recordInfoList[i]);
+  cdi_fdb_sort_datetime(numItems, recordInfoList);
+
+  if (CDI_Debug)
+    {
+      printf("Sorted FDB items:\n");
+      print_keyvalueList_sorted(numItems, keyValueList, recordInfoList);
+    }
 
   int numRecords = get_num_records(numItems, recordInfoList);
+  if (numRecords == 0) return CDI_EUFSTRUCT;
+
   int numTimesteps = numItems / numRecords;
   if (CDI_Debug) Message("numRecords=%d  numTimesteps=%d", numRecords, numTimesteps);
 
-  for (int i = 0; i < numItems; ++i)
-    if (keyValueList[i].item) free(keyValueList[i].item);
-  free(keyValueList);
-
-  if (numRecords == 0) return CDI_EUFSTRUCT;
-
   int *timestepRecordOffset = (int *) malloc(numTimesteps * sizeof(int));
   for (int i = 0; i < numTimesteps; i++) timestepRecordOffset[i] = i * numRecords;
   numTimesteps = remove_duplicate_timesteps(recordInfoList, numRecords, numTimesteps, timestepRecordOffset);
   if (CDI_Debug) Message("numRecords=%d  numTimesteps=%d", numRecords, numTimesteps);
+  // Message("numRecords=%d  numTimesteps=%d", numRecords, numTimesteps);
 
-  CdiDateTime vDateTime0;
-  cdiDateTime_init(&vDateTime0);
+  // CdiDateTime vDateTime0;
+  // cdiDateTime_init(&vDateTime0);
   int fcast = 0;
 
   streamptr->curTsID = 0;
@@ -1078,7 +1082,11 @@ fdbScanTimesteps(stream_t *streamptr)
 
   for (int recID = 0; recID < numRecords; recID++)
     {
-      long recsize = fdb_read_record(fdbHandle, fdbItemList[recID], &buffersize, &gribbuffer);
+      int fdbItem = recordInfoList[recID].fdbIndex;
+      int vdate = recordInfoList[recID].date;
+      int vtime = recordInfoList[recID].time * 100;
+
+      long recsize = cdi_fdb_read_record(fdbHandle, &keyValueList[fdbItem], &buffersize, &gribbuffer);
 
       int datatype, comptype = 0;
       gh = gribapiGetDiskRepresentation(recsize, &buffersize, &gribbuffer, &datatype, &comptype);
@@ -1096,13 +1104,15 @@ fdbScanTimesteps(stream_t *streamptr)
       int param = gribapiGetParam(gh);
 
       CdiDateTime sDateTime;
-      CdiDateTime vDateTime = gribapiGetValidityDateTime(gh, &sDateTime);
+      // CdiDateTime vDateTime = gribapiGetValidityDateTime(gh, &sDateTime);
+      CdiDateTime vDateTime = cdiDateTime_set(vdate, vtime);
+      sDateTime = vDateTime;
 
       VarScanKeys scanKeys = gribapiGetScanKeys(gh);
 
       if (recID == 0)
         {
-          vDateTime0 = vDateTime;
+          // vDateTime0 = vDateTime;
           taxis->rDateTime = gribapiGetDataDateTime(gh);
           fcast = gribapiTimeIsFC(gh);
           if (fcast) taxis->unit = gribapi_get_timeunits(gh);
@@ -1122,8 +1132,7 @@ fdbScanTimesteps(stream_t *streamptr)
       var_tile_t *ptiles = memcmp(&tiles, &dummy_tiles, sizeof(var_tile_t)) ? &tiles : NULL;
       int recpos = 0;
       gribapiAddRecord(streamptr, param, gh, recsize, recpos, datatype, comptype, varname, leveltype1, leveltype2, lbounds, level1,
-                       level2, level_sf, level_unit, &scanKeys, ptiles, true, fdbItemList[recID]);
-      fdbItemList[recID] = NULL;
+                       level2, level_sf, level_unit, &scanKeys, ptiles, true, fdbItem);
 
       grib_handle_delete(gh);
       gh = NULL;
@@ -1178,8 +1187,7 @@ fdbScanTimesteps(stream_t *streamptr)
 
           records[recID].position = 0;
           records[recID].size = 0;
-          records[recID].fdbItem = fdbItemList[recordOffset + recID];
-          fdbItemList[recordOffset + recID] = NULL;
+          records[recID].fdbItemIndex = recordInfoList[recordOffset + recID].fdbIndex;
         }
 
       if (tsID == 1) streamptr->tsteps[1].nrecs = numRecords;
@@ -1188,10 +1196,9 @@ fdbScanTimesteps(stream_t *streamptr)
   streamptr->rtsteps = numTimesteps;
   streamptr->ntsteps = numTimesteps;
 
-  for (int i = 0; i < numItems; ++i)
-    if (fdbItemList[i]) free(fdbItemList[i]);
+  streamptr->fdbNumItems = numItems;
+  streamptr->fdbKeyValueList = keyValueList;
 
-  if (fdbItemList) free(fdbItemList);
   if (recordInfoList) free(recordInfoList);
   if (timestepRecordOffset) free(timestepRecordOffset);
 #endif
@@ -1311,7 +1318,7 @@ gribapiScanTimestep1(stream_t *streamptr)
 
       var_tile_t *ptiles = memcmp(&tiles, &dummy_tiles, sizeof(var_tile_t)) ? &tiles : NULL;
       gribapiAddRecord(streamptr, param, gh, recsize, recpos, datatype, comptype, varname, leveltype1, leveltype2, lbounds, level1,
-                       level2, level_sf, level_unit, &scanKeys, ptiles, true, NULL);
+                       level2, level_sf, level_unit, &scanKeys, ptiles, true, -1);
 
       grib_handle_delete(gh);
       gh = NULL;
@@ -1760,7 +1767,7 @@ unpack_alternative_rows(grib_handle *gh, int memType, void *data)
 }
 
 int
-gribapiDecode(int memType, void *gribbuffer, size_t gribsize, void *data, size_t gridsize, int unreduced, size_t *nmiss,
+gribapiDecode(int memType, void *gribbuffer, size_t gribsize, void *data, size_t gridsize, int unreduced, size_t *numMissVals,
               double missval)
 {
   int status = 0;
@@ -1786,7 +1793,7 @@ gribapiDecode(int memType, void *gribbuffer, size_t gribsize, void *data, size_t
   // GRIB_CHECK(grib_get_long(gh, "numberOfPoints", &numberOfPoints), 0);
   // printf("values_size = %d  numberOfPoints = %ld\n", datasize, numberOfPoints);
 
-  if (gridsize != datasize) Error("Internal problem: gridsize(%zu) != datasize(%zu)!", gridsize, datasize);
+  if (datasize != gridsize) Error("numberOfPoint (%zu) and gridSize (%zu) differ!", datasize, gridsize);
   size_t dummy = datasize;
 
   if (memType == MEMTYPE_FLOAT)
@@ -1813,12 +1820,12 @@ gribapiDecode(int memType, void *gribbuffer, size_t gribsize, void *data, size_t
   GRIB_CHECK(grib_get_long(gh, "gridDefinitionTemplateNumber", &lpar), 0);
   int gridtype = (int) lpar;
 
-  *nmiss = 0;
+  *numMissVals = 0;
   if (gridtype < 50 || gridtype > 53)
     {
       GRIB_CHECK(grib_get_long(gh, "numberOfMissing", &lpar), 0);
-      *nmiss = (int) lpar;
-      // printf("gridtype %d, nmiss %d\n", gridtype, nmiss);
+      *numMissVals = (int) lpar;
+      // printf("gridtype %d, numMissVals %d\n", gridtype, numMissVals);
     }
 
   grib_handle_delete(gh);
@@ -3283,8 +3290,8 @@ gribapiSetExtMode(grib_handle *gh, int gridID, size_t datasize, const void *data
 
 size_t
 gribapiEncode(int memType, int varID, int levelID, int vlistID, int gridID, int zaxisID, CdiDateTime vDateTime, int tsteptype,
-              int numavg, size_t datasize, const void *data, size_t nmiss, void **gribbuffer, size_t *gribbuffersize, int comptype,
-              void *gribContainer)
+              int numavg, size_t datasize, const void *data, size_t numMissVals, void **gribbuffer, size_t *gribbuffersize,
+              int comptype, void *gribContainer)
 {
   long editionNumber = 2;
 
@@ -3452,7 +3459,7 @@ gribapiEncode(int memType, int varID, int levelID, int vlistID, int gridID, int
       }
   }
 
-  if (nmiss > 0)
+  if (numMissVals > 0)
     {
       GRIB_CHECK(my_grib_set_long(gh, "bitmapPresent", 1), 0);
       GRIB_CHECK(my_grib_set_double(gh, "missingValue", vlistInqVarMissval(vlistID, varID)), 0);
@@ -3473,7 +3480,7 @@ gribapiEncode(int memType, int varID, int levelID, int vlistID, int gridID, int
       GRIB_CHECK(grib_set_double_array(gh, "values", (const double *) data, datasize), 0);
     }
 
-  if (nmiss)
+  if (numMissVals)
     {
       long numberOfMissing = -1;
       GRIB_CHECK(grib_get_long(gh, "numberOfMissing", &numberOfMissing), 0);
diff --git a/src/stream_gribapi.h b/src/stream_gribapi.h
index 7e1300fee2e482f3e57ac4c3de806fdb47a06e42..130530e08b013a44c945357c8f85c314ef24aa31 100644
--- a/src/stream_gribapi.h
+++ b/src/stream_gribapi.h
@@ -11,11 +11,11 @@ int gribapiScanTimestep1(stream_t *streamptr);
 int gribapiScanTimestep2(stream_t *streamptr);
 int gribapiScanTimestep(stream_t *streamptr);
 
-int gribapiDecode(int memType, void *gribbuffer, size_t gribsize, void *data, size_t datasize, int unreduced, size_t *nmiss,
+int gribapiDecode(int memType, void *gribbuffer, size_t gribsize, void *data, size_t datasize, int unreduced, size_t *numMissVals,
                   double missval);
 
 size_t gribapiEncode(int memType, int varID, int levelID, int vlistID, int gridID, int zaxisID, CdiDateTime vDateTime,
-                     int tsteptype, int numavg, size_t datasize, const void *data, size_t nmiss, void **gribbuffer,
+                     int tsteptype, int numavg, size_t datasize, const void *data, size_t numMissVals, void **gribbuffer,
                      size_t *gribbuffersize, int ljpeg, void *gribContainer);
 
 int gribapiGetScanningMode(grib_handle *gh);
diff --git a/src/stream_ieg.c b/src/stream_ieg.c
index 537286cac728f8d3d2ede9973a6931ca79d22eb6..33abc8c41626a23d8d011334195c8304b390d531 100644
--- a/src/stream_ieg.c
+++ b/src/stream_ieg.c
@@ -36,7 +36,7 @@ iegDefDatatype(int datatype)
 }
 
 static void
-ieg_read_recordSP(stream_t *streamptr, float *data, size_t *nmiss)
+ieg_read_recordSP(stream_t *streamptr, float *data, size_t *numMissVals)
 {
   int vlistID = streamptr->vlistID;
   int fileID = streamptr->fileID;
@@ -57,13 +57,13 @@ ieg_read_recordSP(stream_t *streamptr, float *data, size_t *nmiss)
   double missval = vlistInqVarMissval(vlistID, varID);
   size_t size = gridInqSize(vlistInqVarGrid(vlistID, varID));
 
-  *nmiss = get_num_missvalsSP(size, data, missval);
+  *numMissVals = get_num_missvalsSP(size, data, missval);
 
   streamptr->numvals += size;
 }
 
 static void
-ieg_read_recordDP(stream_t *streamptr, double *data, size_t *nmiss)
+ieg_read_recordDP(stream_t *streamptr, double *data, size_t *numMissVals)
 {
   int vlistID = streamptr->vlistID;
   int fileID = streamptr->fileID;
@@ -84,18 +84,18 @@ ieg_read_recordDP(stream_t *streamptr, double *data, size_t *nmiss)
   double missval = vlistInqVarMissval(vlistID, varID);
   size_t size = gridInqSize(vlistInqVarGrid(vlistID, varID));
 
-  *nmiss = get_num_missvalsDP(size, data, missval);
+  *numMissVals = get_num_missvalsDP(size, data, missval);
 
   streamptr->numvals += size;
 }
 
 void
-ieg_read_record(stream_t *streamptr, int memtype, void *data, size_t *nmiss)
+ieg_read_record(stream_t *streamptr, int memtype, void *data, size_t *numMissVals)
 {
   if (memtype == MEMTYPE_DOUBLE)
-    ieg_read_recordDP(streamptr, (double *) data, nmiss);
+    ieg_read_recordDP(streamptr, (double *) data, numMissVals);
   else
-    ieg_read_recordSP(streamptr, (float *) data, nmiss);
+    ieg_read_recordSP(streamptr, (float *) data, numMissVals);
 }
 
 static int
@@ -1006,7 +1006,7 @@ iegInqTimestep(stream_t *streamptr, int tsID)
 }
 
 void
-iegReadVarSliceDP(stream_t *streamptr, int varID, int levID, double *data, size_t *nmiss)
+iegReadVarSliceDP(stream_t *streamptr, int varID, int levID, double *data, size_t *numMissVals)
 {
   if (CDI_Debug) Message("streamID = %d  varID = %d  levID = %d", streamptr->self, varID, levID);
 
@@ -1029,11 +1029,11 @@ iegReadVarSliceDP(stream_t *streamptr, int varID, int levID, double *data, size_
 
   fileSetPos(fileID, currentfilepos, SEEK_SET);
 
-  *nmiss = get_num_missvalsDP(gridsize, data, missval);
+  *numMissVals = get_num_missvalsDP(gridsize, data, missval);
 }
 
 void
-iegReadVarDP(stream_t *streamptr, int varID, double *data, size_t *nmiss)
+iegReadVarDP(stream_t *streamptr, int varID, double *data, size_t *numMissVals)
 {
   if (CDI_Debug) Message("streamID = %d  varID = %d", streamptr->self, varID);
 
@@ -1041,7 +1041,8 @@ iegReadVarDP(stream_t *streamptr, int varID, double *data, size_t *nmiss)
   size_t gridsize = gridInqSize(vlistInqVarGrid(vlistID, varID));
   size_t nlevs = (size_t) streamptr->vars[varID].recordTable[0].nlevs;
 
-  for (size_t levID = 0; levID < nlevs; levID++) iegReadVarSliceDP(streamptr, varID, (int) levID, &data[levID * gridsize], nmiss);
+  for (size_t levID = 0; levID < nlevs; levID++)
+    iegReadVarSliceDP(streamptr, varID, (int) levID, &data[levID * gridsize], numMissVals);
 }
 
 void
diff --git a/src/stream_ieg.h b/src/stream_ieg.h
index 82a04ca3b4f02bb6db5ef6461ccd5b822e639b59..a099cdaf4b96dbb58248c015a23926a2685d5d80 100644
--- a/src/stream_ieg.h
+++ b/src/stream_ieg.h
@@ -11,13 +11,13 @@ int iegInqTimestep(stream_t *streamptr, int tsID);
 int iegInqRecord(stream_t *streamptr, int *varID, int *levelID);
 void iegDefRecord(stream_t *streamptr);
 void iegCopyRecord(stream_t *streamptr2, stream_t *streamptr1);
-void ieg_read_record(stream_t *streamptr, int memtype, void *data, size_t *nmiss);
+void ieg_read_record(stream_t *streamptr, int memtype, void *data, size_t *numMissVals);
 void ieg_write_record(stream_t *streamptr, int memtype, const void *data);
 
-void iegReadVarDP(stream_t *streamptr, int varID, double *data, size_t *nmiss);
+void iegReadVarDP(stream_t *streamptr, int varID, double *data, size_t *numMissVals);
 void iegWriteVarDP(stream_t *streamptr, int varID, const double *data);
 
-void iegReadVarSliceDP(stream_t *streamptr, int varID, int levelID, double *data, size_t *nmiss);
+void iegReadVarSliceDP(stream_t *streamptr, int varID, int levelID, double *data, size_t *numMissVals);
 void iegWriteVarSliceDP(stream_t *streamptr, int varID, int levelID, const double *data);
 
 #endif /* _STREAM_IEG_H */
diff --git a/src/stream_read.c b/src/stream_read.c
index e42dd0327dbcf45a4b9d05d097c1bacc723b7867..fedafb2ac7b488453fbfbf9319a5069acb4b346c 100644
--- a/src/stream_read.c
+++ b/src/stream_read.c
@@ -13,7 +13,7 @@
 
 // the single image implementation
 static int
-cdiStreamReadVar(int streamID, int varID, int memtype, void *data, size_t *nmiss)
+cdiStreamReadVar(int streamID, int varID, int memtype, void *data, size_t *numMissVals)
 {
   // May fail if memtype == MEMTYPE_FLOAT and the file format does not support single precision reading.
   // A value > 0 is returned in this case, otherwise it returns zero.
@@ -22,31 +22,31 @@ cdiStreamReadVar(int streamID, int varID, int memtype, void *data, size_t *nmiss
   if (CDI_Debug) Message("streamID = %d  varID = %d", streamID, varID);
 
   check_parg(data);
-  check_parg(nmiss);
+  check_parg(numMissVals);
 
   stream_t *streamptr = stream_to_pointer(streamID);
   const int filetype = streamptr->filetype;
 
-  *nmiss = 0;
+  *numMissVals = 0;
 
   if (memtype == MEMTYPE_FLOAT && cdiFiletypeIsExse(filetype)) return 1;
 
   switch (cdiBaseFiletype(filetype))
     {
 #ifdef HAVE_LIBGRIB
-    case CDI_FILETYPE_GRIB: grb_read_var(streamptr, varID, memtype, data, nmiss); break;
+    case CDI_FILETYPE_GRIB: grb_read_var(streamptr, varID, memtype, data, numMissVals); break;
 #endif
 #ifdef HAVE_LIBSERVICE
-    case CDI_FILETYPE_SRV: srvReadVarDP(streamptr, varID, (double *) data, nmiss); break;
+    case CDI_FILETYPE_SRV: srvReadVarDP(streamptr, varID, (double *) data, numMissVals); break;
 #endif
 #ifdef HAVE_LIBEXTRA
-    case CDI_FILETYPE_EXT: extReadVarDP(streamptr, varID, (double *) data, nmiss); break;
+    case CDI_FILETYPE_EXT: extReadVarDP(streamptr, varID, (double *) data, numMissVals); break;
 #endif
 #ifdef HAVE_LIBIEG
-    case CDI_FILETYPE_IEG: iegReadVarDP(streamptr, varID, (double *) data, nmiss); break;
+    case CDI_FILETYPE_IEG: iegReadVarDP(streamptr, varID, (double *) data, numMissVals); break;
 #endif
 #ifdef HAVE_LIBNETCDF
-    case CDI_FILETYPE_NETCDF: cdf_read_var(streamptr, varID, memtype, data, nmiss); break;
+    case CDI_FILETYPE_NETCDF: cdf_read_var(streamptr, varID, memtype, data, numMissVals); break;
 #endif
     default: Error("%s support not compiled in!", strfiletype(filetype));
     }
@@ -58,13 +58,13 @@ cdiStreamReadVar(int streamID, int varID, int memtype, void *data, size_t *nmiss
 @Function  streamReadVar
 @Title     Read a variable
 
-@Prototype void streamReadVar(int streamID, int varID, double *data, SizeType *nmiss)
+@Prototype void streamReadVar(int streamID, int varID, double *data, SizeType *numMissVals)
 @Parameter
     @Item  streamID  Stream ID, from a previous call to @fref{streamOpenRead}.
     @Item  varID     Variable identifier.
     @Item  data      Pointer to the location into which the data values are read.
                      The caller must allocate space for the returned values.
-    @Item  nmiss     Number of missing values.
+    @Item  numMissVals     Number of missing values.
 
 @Description
 The function streamReadVar reads all the values of one time step of a variable
@@ -72,24 +72,24 @@ from an open dataset.
 @EndFunction
 */
 void
-streamReadVar(int streamID, int varID, double *data, SizeType *nmiss)
+streamReadVar(int streamID, int varID, double *data, SizeType *numMissVals)
 {
   size_t numMiss = 0;
   cdiStreamReadVar(streamID, varID, MEMTYPE_DOUBLE, data, &numMiss);
-  *nmiss = (SizeType) numMiss;
+  *numMissVals = (SizeType) numMiss;
 }
 
 /*
 @Function  streamReadVarF
 @Title     Read a variable
 
-@Prototype void streamReadVar(int streamID, int varID, float *data, SizeType *nmiss)
+@Prototype void streamReadVar(int streamID, int varID, float *data, SizeType *numMissVals)
 @Parameter
     @Item  streamID  Stream ID, from a previous call to @fref{streamOpenRead}.
     @Item  varID     Variable identifier.
     @Item  data      Pointer to the location into which the data values are read.
                      The caller must allocate space for the returned values.
-    @Item  nmiss     Number of missing values.
+    @Item  numMissVals     Number of missing values.
 
 @Description
 The function streamReadVar reads all the values of one time step of a variable
@@ -97,7 +97,7 @@ from an open dataset.
 @EndFunction
 */
 void
-streamReadVarF(int streamID, int varID, float *data, SizeType *nmiss)
+streamReadVarF(int streamID, int varID, float *data, SizeType *numMissVals)
 {
   size_t numMiss = 0;
   if (cdiStreamReadVar(streamID, varID, MEMTYPE_FLOAT, data, &numMiss))
@@ -107,15 +107,15 @@ streamReadVarF(int streamID, int varID, float *data, SizeType *nmiss)
       size_t elementCount = gridInqSize(vlistInqVarGrid(streamInqVlist(streamID), varID));
       elementCount *= (size_t) zaxisInqSize(vlistInqVarZaxis(streamInqVlist(streamID), varID));
       double *conversionBuffer = (double *) Malloc(elementCount * sizeof(*conversionBuffer));
-      streamReadVar(streamID, varID, conversionBuffer, nmiss);
+      streamReadVar(streamID, varID, conversionBuffer, numMissVals);
       for (size_t i = elementCount; i--;) data[i] = (float) conversionBuffer[i];
       Free(conversionBuffer);
     }
-  *nmiss = (SizeType) numMiss;
+  *numMissVals = (SizeType) numMiss;
 }
 
 static int
-cdiStreamReadVarSlice(int streamID, int varID, int levelID, int memtype, void *data, size_t *nmiss)
+cdiStreamReadVarSlice(int streamID, int varID, int levelID, int memtype, void *data, size_t *numMissVals)
 {
   // May fail if memtype == MEMTYPE_FLOAT and the file format does not support single precision reading.
   // A value > 0 is returned in this case, otherwise it returns zero.
@@ -124,31 +124,31 @@ cdiStreamReadVarSlice(int streamID, int varID, int levelID, int memtype, void *d
   if (CDI_Debug) Message("streamID = %d  varID = %d", streamID, varID);
 
   check_parg(data);
-  check_parg(nmiss);
+  check_parg(numMissVals);
 
   stream_t *streamptr = stream_to_pointer(streamID);
   const int filetype = streamptr->filetype;
 
-  *nmiss = 0;
+  *numMissVals = 0;
 
   if (memtype == MEMTYPE_FLOAT && cdiFiletypeIsExse(filetype)) return 1;
 
   switch (cdiBaseFiletype(filetype))
     {
 #ifdef HAVE_LIBGRIB
-    case CDI_FILETYPE_GRIB: grb_read_var_slice(streamptr, varID, levelID, memtype, data, nmiss); break;
+    case CDI_FILETYPE_GRIB: grb_read_var_slice(streamptr, varID, levelID, memtype, data, numMissVals); break;
 #endif
 #ifdef HAVE_LIBSERVICE
-    case CDI_FILETYPE_SRV: srvReadVarSliceDP(streamptr, varID, levelID, (double *) data, nmiss); break;
+    case CDI_FILETYPE_SRV: srvReadVarSliceDP(streamptr, varID, levelID, (double *) data, numMissVals); break;
 #endif
 #ifdef HAVE_LIBEXTRA
-    case CDI_FILETYPE_EXT: extReadVarSliceDP(streamptr, varID, levelID, (double *) data, nmiss); break;
+    case CDI_FILETYPE_EXT: extReadVarSliceDP(streamptr, varID, levelID, (double *) data, numMissVals); break;
 #endif
 #ifdef HAVE_LIBIEG
-    case CDI_FILETYPE_IEG: iegReadVarSliceDP(streamptr, varID, levelID, (double *) data, nmiss); break;
+    case CDI_FILETYPE_IEG: iegReadVarSliceDP(streamptr, varID, levelID, (double *) data, numMissVals); break;
 #endif
 #ifdef HAVE_LIBNETCDF
-    case CDI_FILETYPE_NETCDF: cdf_read_var_slice(streamptr, varID, levelID, memtype, data, nmiss); break;
+    case CDI_FILETYPE_NETCDF: cdf_read_var_slice(streamptr, varID, levelID, memtype, data, numMissVals); break;
 #endif
     default: Error("%s support not compiled in!", strfiletype(filetype));
     }
@@ -160,14 +160,14 @@ cdiStreamReadVarSlice(int streamID, int varID, int levelID, int memtype, void *d
 @Function  streamReadVarSlice
 @Title     Read a horizontal slice of a variable
 
-@Prototype void streamReadVarSlice(int streamID, int varID, int levelID, double *data, SizeType *nmiss)
+@Prototype void streamReadVarSlice(int streamID, int varID, int levelID, double *data, SizeType *numMissVals)
 @Parameter
     @Item  streamID  Stream ID, from a previous call to @fref{streamOpenRead}.
     @Item  varID     Variable identifier.
     @Item  levelID   Level identifier.
     @Item  data      Pointer to the location into which the data values are read.
                      The caller must allocate space for the returned values.
-    @Item  nmiss     Number of missing values.
+    @Item  numMissVals     Number of missing values.
 
 @Description
 The function streamReadVarSlice reads all the values of a horizontal slice of a variable
@@ -175,7 +175,7 @@ from an open dataset.
 @EndFunction
 */
 void
-streamReadVarSlice(int streamID, int varID, int levelID, double *data, SizeType *nmiss)
+streamReadVarSlice(int streamID, int varID, int levelID, double *data, SizeType *numMissVals)
 {
   size_t numMiss = 0;
   if (cdiStreamReadVarSlice(streamID, varID, levelID, MEMTYPE_DOUBLE, data, &numMiss))
@@ -184,21 +184,21 @@ streamReadVarSlice(int streamID, int varID, int levelID, double *data, SizeType
       size_t elementCount = gridInqSize(vlistInqVarGrid(streamInqVlist(streamID), varID));
       memset(data, 0, elementCount * sizeof(*data));
     }
-  *nmiss = (SizeType) numMiss;
+  *numMissVals = (SizeType) numMiss;
 }
 
 /*
 @Function  streamReadVarSliceF
 @Title     Read a horizontal slice of a variable
 
-@Prototype void streamReadVarSliceF(int streamID, int varID, int levelID, float *data, SizeType *nmiss)
+@Prototype void streamReadVarSliceF(int streamID, int varID, int levelID, float *data, SizeType *numMissVals)
 @Parameter
     @Item  streamID  Stream ID, from a previous call to @fref{streamOpenRead}.
     @Item  varID     Variable identifier.
     @Item  levelID   Level identifier.
     @Item  data      Pointer to the location into which the data values are read.
                      The caller must allocate space for the returned values.
-    @Item  nmiss     Number of missing values.
+    @Item  numMissVals     Number of missing values.
 
 @Description
 The function streamReadVarSliceF reads all the values of a horizontal slice of a variable
@@ -206,7 +206,7 @@ from an open dataset.
 @EndFunction
 */
 void
-streamReadVarSliceF(int streamID, int varID, int levelID, float *data, SizeType *nmiss)
+streamReadVarSliceF(int streamID, int varID, int levelID, float *data, SizeType *numMissVals)
 {
   size_t numMiss = 0;
   if (cdiStreamReadVarSlice(streamID, varID, levelID, MEMTYPE_FLOAT, data, &numMiss))
@@ -215,56 +215,60 @@ streamReadVarSliceF(int streamID, int varID, int levelID, float *data, SizeType
       // we fall back to double precision reading, converting the data on the fly.
       size_t elementCount = gridInqSize(vlistInqVarGrid(streamInqVlist(streamID), varID));
       double *conversionBuffer = (double *) Malloc(elementCount * sizeof(*conversionBuffer));
-      streamReadVarSlice(streamID, varID, levelID, conversionBuffer, nmiss);
+      streamReadVarSlice(streamID, varID, levelID, conversionBuffer, numMissVals);
       for (size_t i = elementCount; i--;) data[i] = (float) conversionBuffer[i];
       Free(conversionBuffer);
     }
-  *nmiss = (SizeType) numMiss;
+  *numMissVals = (SizeType) numMiss;
 }
 
 static void
-stream_read_record(int streamID, int memtype, void *data, size_t *nmiss)
+stream_read_record(int streamID, int memtype, void *data, size_t *numMissVals)
 {
   check_parg(data);
-  check_parg(nmiss);
+  check_parg(numMissVals);
 
   stream_t *streamptr = stream_to_pointer(streamID);
 
-  *nmiss = 0;
+  if (streamptr->lockIO) CDI_IO_LOCK();
+
+  *numMissVals = 0;
 
   switch (cdiBaseFiletype(streamptr->filetype))
     {
 #ifdef HAVE_LIBGRIB
-    case CDI_FILETYPE_GRIB: grb_read_record(streamptr, memtype, data, nmiss); break;
+    case CDI_FILETYPE_GRIB: grb_read_record(streamptr, memtype, data, numMissVals); break;
 #endif
 #ifdef HAVE_LIBSERVICE
-    case CDI_FILETYPE_SRV: srv_read_record(streamptr, memtype, data, nmiss); break;
+    case CDI_FILETYPE_SRV: srv_read_record(streamptr, memtype, data, numMissVals); break;
 #endif
 #ifdef HAVE_LIBEXTRA
-    case CDI_FILETYPE_EXT: ext_read_record(streamptr, memtype, data, nmiss); break;
+    case CDI_FILETYPE_EXT: ext_read_record(streamptr, memtype, data, numMissVals); break;
 #endif
 #ifdef HAVE_LIBIEG
-    case CDI_FILETYPE_IEG: ieg_read_record(streamptr, memtype, data, nmiss); break;
+    case CDI_FILETYPE_IEG: ieg_read_record(streamptr, memtype, data, numMissVals); break;
 #endif
 #ifdef HAVE_LIBNETCDF
-    case CDI_FILETYPE_NETCDF: cdf_read_record(streamptr, memtype, data, nmiss); break;
+    case CDI_FILETYPE_NETCDF: cdf_read_record(streamptr, memtype, data, numMissVals); break;
 #endif
     default: Error("%s support not compiled in!", strfiletype(streamptr->filetype));
     }
+
+  if (streamptr->lockIO) CDI_IO_UNLOCK();
 }
 
 void
-streamReadRecord(int streamID, double *data, SizeType *nmiss)
+streamReadRecord(int streamID, double *data, SizeType *numMissVals)
 {
   size_t numMiss = 0;
   stream_read_record(streamID, MEMTYPE_DOUBLE, (void *) data, &numMiss);
-  *nmiss = (SizeType) numMiss;
+  *numMissVals = (SizeType) numMiss;
 }
 
 void
-streamReadRecordF(int streamID, float *data, SizeType *nmiss)
+streamReadRecordF(int streamID, float *data, SizeType *numMissVals)
 {
   size_t numMiss = 0;
   stream_read_record(streamID, MEMTYPE_FLOAT, (void *) data, &numMiss);
-  *nmiss = (SizeType) numMiss;
+  *numMissVals = (SizeType) numMiss;
 }
diff --git a/src/stream_record.c b/src/stream_record.c
index 59f10059e8f371c31d5a90a0c7ad916617155ecb..807f9eecd2cc1b7ca15202468d5b62a14cb19cb0 100644
--- a/src/stream_record.c
+++ b/src/stream_record.c
@@ -32,7 +32,7 @@ recordInitEntry(record_t *record)
   varScanKeysInit(&record->scanKeys);
   memset(&record->tiles, 0, sizeof(record->tiles));
 #ifdef HAVE_LIBFDB5
-  record->fdbItem = NULL;
+  record->fdbItemIndex = -1;
 #endif
 }
 
@@ -291,7 +291,7 @@ cdi_create_records(stream_t *streamptr, int tsID)
   destTstep->recordSize = (int) maxrecords;
   destTstep->nallrecs = (int) nrecords;
 #ifdef HAVE_LIBFDB5
-  destTstep->records->fdbItem = NULL;
+  destTstep->records->fdbItemIndex = -1;
 #endif
 
   if (tsID == 0)
diff --git a/src/stream_srv.c b/src/stream_srv.c
index 42298c2085268724e859d9871293bbd0fb224799..888959daba9b7e02ad4962b29b2b8d530df81b60 100644
--- a/src/stream_srv.c
+++ b/src/stream_srv.c
@@ -72,7 +72,7 @@ int srvInqRecord(stream_t *streamptr, int *varID, int *levelID)
 */
 
 static void
-srv_read_recordSP(stream_t *streamptr, float *data, size_t *nmiss)
+srv_read_recordSP(stream_t *streamptr, float *data, size_t *numMissVals)
 {
   int vlistID = streamptr->vlistID;
   int fileID = streamptr->fileID;
@@ -95,13 +95,13 @@ srv_read_recordSP(stream_t *streamptr, float *data, size_t *nmiss)
   double missval = vlistInqVarMissval(vlistID, varID);
   size_t size = gridInqSize(vlistInqVarGrid(vlistID, varID));
 
-  *nmiss = get_num_missvalsSP(size, data, (float) missval);
+  *numMissVals = get_num_missvalsSP(size, data, (float) missval);
 
   streamptr->numvals += size;
 }
 
 static void
-srv_read_recordDP(stream_t *streamptr, double *data, size_t *nmiss)
+srv_read_recordDP(stream_t *streamptr, double *data, size_t *numMissVals)
 {
   int vlistID = streamptr->vlistID;
   int fileID = streamptr->fileID;
@@ -124,18 +124,18 @@ srv_read_recordDP(stream_t *streamptr, double *data, size_t *nmiss)
   double missval = vlistInqVarMissval(vlistID, varID);
   size_t size = gridInqSize(vlistInqVarGrid(vlistID, varID));
 
-  *nmiss = get_num_missvalsDP(size, data, missval);
+  *numMissVals = get_num_missvalsDP(size, data, missval);
 
   streamptr->numvals += size;
 }
 
 void
-srv_read_record(stream_t *streamptr, int memtype, void *data, size_t *nmiss)
+srv_read_record(stream_t *streamptr, int memtype, void *data, size_t *numMissVals)
 {
   if (memtype == MEMTYPE_DOUBLE)
-    srv_read_recordDP(streamptr, (double *) data, nmiss);
+    srv_read_recordDP(streamptr, (double *) data, numMissVals);
   else
-    srv_read_recordSP(streamptr, (float *) data, nmiss);
+    srv_read_recordSP(streamptr, (float *) data, numMissVals);
 }
 
 void
@@ -559,7 +559,7 @@ srvInqTimestep(stream_t *streamptr, int tsID)
 }
 
 void
-srvReadVarSliceDP(stream_t *streamptr, int varID, int levID, double *data, size_t *nmiss)
+srvReadVarSliceDP(stream_t *streamptr, int varID, int levID, double *data, size_t *numMissVals)
 {
   if (CDI_Debug) Message("streamID = %d  varID = %d  levID = %d", streamptr->self, varID, levID);
 
@@ -586,11 +586,11 @@ srvReadVarSliceDP(stream_t *streamptr, int varID, int levID, double *data, size_
   double missval = vlistInqVarMissval(vlistID, varID);
   size_t size = gridInqSize(vlistInqVarGrid(vlistID, varID));
 
-  *nmiss = get_num_missvalsDP(size, data, missval);
+  *numMissVals = get_num_missvalsDP(size, data, missval);
 }
 
 void
-srvReadVarDP(stream_t *streamptr, int varID, double *data, size_t *nmiss)
+srvReadVarDP(stream_t *streamptr, int varID, double *data, size_t *numMissVals)
 {
   if (CDI_Debug) Message("streamID = %d  varID = %d", streamptr->self, varID);
 
@@ -598,7 +598,8 @@ srvReadVarDP(stream_t *streamptr, int varID, double *data, size_t *nmiss)
   size_t gridsize = gridInqSize(vlistInqVarGrid(vlistID, varID));
   size_t nlevs = (size_t) streamptr->vars[varID].recordTable[0].nlevs;
 
-  for (size_t levID = 0; levID < nlevs; levID++) srvReadVarSliceDP(streamptr, varID, (int) levID, &data[levID * gridsize], nmiss);
+  for (size_t levID = 0; levID < nlevs; levID++)
+    srvReadVarSliceDP(streamptr, varID, (int) levID, &data[levID * gridsize], numMissVals);
 }
 
 void
diff --git a/src/stream_srv.h b/src/stream_srv.h
index feac6841b9c7b32f0b77ed4ee74e4dabd55b07da..adeaf679200828310ac9245e697cac6e25f75b7b 100644
--- a/src/stream_srv.h
+++ b/src/stream_srv.h
@@ -11,13 +11,13 @@ int srvInqTimestep(stream_t *streamptr, int tsID);
 int srvInqRecord(stream_t *streamptr, int *varID, int *levelID);
 void srvDefRecord(stream_t *streamptr);
 void srvCopyRecord(stream_t *streamptr2, stream_t *streamptr1);
-void srv_read_record(stream_t *streamptr, int memtype, void *data, size_t *nmiss);
+void srv_read_record(stream_t *streamptr, int memtype, void *data, size_t *numMissVals);
 void srv_write_record(stream_t *streamptr, int memtype, const void *data);
 
-void srvReadVarDP(stream_t *streamptr, int varID, double *data, size_t *nmiss);
+void srvReadVarDP(stream_t *streamptr, int varID, double *data, size_t *numMissVals);
 void srvWriteVarDP(stream_t *streamptr, int varID, const double *data);
 
-void srvReadVarSliceDP(stream_t *streamptr, int varID, int levelID, double *data, size_t *nmiss);
+void srvReadVarSliceDP(stream_t *streamptr, int varID, int levelID, double *data, size_t *numMissVals);
 void srvWriteVarSliceDP(stream_t *streamptr, int varID, int levelID, const double *data);
 
 #endif /* _STREAM_SRV_H */
diff --git a/src/stream_write.c b/src/stream_write.c
index 226db5f9c1ca15fe8c10e8752e5f273cfd19180c..a2b34e90fe6f8cd9693ab3a2d49e84a00295ce49 100644
--- a/src/stream_write.c
+++ b/src/stream_write.c
@@ -14,7 +14,7 @@
 
 // the single image implementation
 int
-cdiStreamWriteVar_(int streamID, int varID, int memtype, const void *data, SizeType nmiss)
+cdiStreamWriteVar_(int streamID, int varID, int memtype, const void *data, SizeType numMissVals)
 {
   // May fail if memtype == MEMTYPE_FLOAT and the file format does not support single precision writing.
   // A value > 0 is returned in this case, otherwise it returns zero.
@@ -37,7 +37,7 @@ cdiStreamWriteVar_(int streamID, int varID, int memtype, const void *data, SizeT
   switch (cdiBaseFiletype(filetype))
     {
 #ifdef HAVE_LIBGRIB
-    case CDI_FILETYPE_GRIB: grb_write_var(streamptr, varID, memtype, data, nmiss); break;
+    case CDI_FILETYPE_GRIB: grb_write_var(streamptr, varID, memtype, data, numMissVals); break;
 #endif
 #ifdef HAVE_LIBSERVICE
     case CDI_FILETYPE_SRV: srvWriteVarDP(streamptr, varID, (double *) data); break;
@@ -49,7 +49,7 @@ cdiStreamWriteVar_(int streamID, int varID, int memtype, const void *data, SizeT
     case CDI_FILETYPE_IEG: iegWriteVarDP(streamptr, varID, (double *) data); break;
 #endif
 #ifdef HAVE_LIBNETCDF
-    case CDI_FILETYPE_NETCDF: cdf_write_var(streamptr, varID, memtype, data, nmiss); break;
+    case CDI_FILETYPE_NETCDF: cdf_write_var(streamptr, varID, memtype, data, numMissVals); break;
 #endif
     default: Error("%s support not compiled in!", strfiletype(filetype));
     }
@@ -61,12 +61,12 @@ cdiStreamWriteVar_(int streamID, int varID, int memtype, const void *data, SizeT
 @Function  streamWriteVar
 @Title     Write a variable
 
-@Prototype void streamWriteVar(int streamID, int varID, const double *data, SizeType nmiss)
+@Prototype void streamWriteVar(int streamID, int varID, const double *data, SizeType numMissVals)
 @Parameter
     @Item  streamID  Stream ID, from a previous call to @fref{streamOpenWrite}.
     @Item  varID     Variable identifier.
     @Item  data      Pointer to a block of double precision floating point data values to be written.
-    @Item  nmiss     Number of missing values.
+    @Item  numMissVals     Number of missing values.
 
 @Description
 The function streamWriteVar writes the values of one time step of a variable to an open dataset.
@@ -74,24 +74,24 @@ The values are converted to the external data type of the variable, if necessary
 @EndFunction
 */
 void
-streamWriteVar(int streamID, int varID, const double *data, SizeType nmiss)
+streamWriteVar(int streamID, int varID, const double *data, SizeType numMissVals)
 {
-  void (*myCdiStreamWriteVar_)(int streamID, int varID, int memtype, const void *data, SizeType nmiss)
+  void (*myCdiStreamWriteVar_)(int streamID, int varID, int memtype, const void *data, SizeType numMissVals)
       = (void (*)(int, int, int, const void *, SizeType)) namespaceSwitchGet(NSSWITCH_STREAM_WRITE_VAR_).func;
 
-  myCdiStreamWriteVar_(streamID, varID, MEMTYPE_DOUBLE, (const void *) data, nmiss);
+  myCdiStreamWriteVar_(streamID, varID, MEMTYPE_DOUBLE, (const void *) data, numMissVals);
 }
 
 /*
 @Function  streamWriteVarF
 @Title     Write a variable
 
-@Prototype void streamWriteVarF(int streamID, int varID, const float *data, SizeType nmiss)
+@Prototype void streamWriteVarF(int streamID, int varID, const float *data, SizeType numMissVals)
 @Parameter
     @Item  streamID  Stream ID, from a previous call to @fref{streamOpenWrite}.
     @Item  varID     Variable identifier.
     @Item  data      Pointer to a block of single precision floating point data values to be written.
-    @Item  nmiss     Number of missing values.
+    @Item  numMissVals     Number of missing values.
 
 @Description
 The function streamWriteVarF writes the values of one time step of a variable to an open dataset.
@@ -99,12 +99,12 @@ The values are converted to the external data type of the variable, if necessary
 @EndFunction
 */
 void
-streamWriteVarF(int streamID, int varID, const float *data, SizeType nmiss)
+streamWriteVarF(int streamID, int varID, const float *data, SizeType numMissVals)
 {
-  int (*myCdiStreamWriteVar_)(int streamID, int varID, int memtype, const void *data, SizeType nmiss)
+  int (*myCdiStreamWriteVar_)(int streamID, int varID, int memtype, const void *data, SizeType numMissVals)
       = (int (*)(int, int, int, const void *, SizeType)) namespaceSwitchGet(NSSWITCH_STREAM_WRITE_VAR_).func;
 
-  if (myCdiStreamWriteVar_(streamID, varID, MEMTYPE_FLOAT, (const void *) data, nmiss))
+  if (myCdiStreamWriteVar_(streamID, varID, MEMTYPE_FLOAT, (const void *) data, numMissVals))
     {
       // In case the file format does not support single precision writing,
       // we fall back to double precision writing, converting the data on the fly.
@@ -113,13 +113,13 @@ streamWriteVarF(int streamID, int varID, const float *data, SizeType nmiss)
       elementCount *= (SizeType) zaxisInqSize(vlistInqVarZaxis(vlistID, varID));
       double *conversionBuffer = (double *) Malloc(elementCount * sizeof(*conversionBuffer));
       for (SizeType i = elementCount; i--;) conversionBuffer[i] = (double) data[i];
-      myCdiStreamWriteVar_(streamID, varID, MEMTYPE_DOUBLE, (const void *) conversionBuffer, nmiss);
+      myCdiStreamWriteVar_(streamID, varID, MEMTYPE_DOUBLE, (const void *) conversionBuffer, numMissVals);
       Free(conversionBuffer);
     }
 }
 
 static int
-cdiStreamWriteVarSlice(int streamID, int varID, int levelID, int memtype, const void *data, SizeType nmiss)
+cdiStreamWriteVarSlice(int streamID, int varID, int levelID, int memtype, const void *data, SizeType numMissVals)
 {
   // May fail if memtype == MEMTYPE_FLOAT and the file format does not support single precision writing.
   // A value > 0 is returned in this case, otherwise it returns zero.
@@ -142,7 +142,7 @@ cdiStreamWriteVarSlice(int streamID, int varID, int levelID, int memtype, const
   switch (cdiBaseFiletype(filetype))
     {
 #ifdef HAVE_LIBGRIB
-    case CDI_FILETYPE_GRIB: grb_write_var_slice(streamptr, varID, levelID, memtype, data, nmiss); break;
+    case CDI_FILETYPE_GRIB: grb_write_var_slice(streamptr, varID, levelID, memtype, data, numMissVals); break;
 #endif
 #ifdef HAVE_LIBSERVICE
     case CDI_FILETYPE_SRV: srvWriteVarSliceDP(streamptr, varID, levelID, (double *) data); break;
@@ -154,7 +154,7 @@ cdiStreamWriteVarSlice(int streamID, int varID, int levelID, int memtype, const
     case CDI_FILETYPE_IEG: iegWriteVarSliceDP(streamptr, varID, levelID, (double *) data); break;
 #endif
 #ifdef HAVE_LIBNETCDF
-    case CDI_FILETYPE_NETCDF: cdf_write_var_slice(streamptr, varID, levelID, memtype, data, nmiss); break;
+    case CDI_FILETYPE_NETCDF: cdf_write_var_slice(streamptr, varID, levelID, memtype, data, numMissVals); break;
 #endif
     default: Error("%s support not compiled in!", strfiletype(filetype));
     }
@@ -166,13 +166,13 @@ cdiStreamWriteVarSlice(int streamID, int varID, int levelID, int memtype, const
 @Function  streamWriteVarSlice
 @Title     Write a horizontal slice of a variable
 
-@Prototype void streamWriteVarSlice(int streamID, int varID, int levelID, const double *data, SizeType nmiss)
+@Prototype void streamWriteVarSlice(int streamID, int varID, int levelID, const double *data, SizeType numMissVals)
 @Parameter
     @Item  streamID  Stream ID, from a previous call to @fref{streamOpenWrite}.
     @Item  varID     Variable identifier.
     @Item  levelID   Level identifier.
     @Item  data      Pointer to a block of double precision floating point data values to be written.
-    @Item  nmiss     Number of missing values.
+    @Item  numMissVals     Number of missing values.
 
 @Description
 The function streamWriteVarSlice writes the values of a horizontal slice of a variable to an open dataset.
@@ -180,22 +180,22 @@ The values are converted to the external data type of the variable, if necessary
 @EndFunction
 */
 void
-streamWriteVarSlice(int streamID, int varID, int levelID, const double *data, SizeType nmiss)
+streamWriteVarSlice(int streamID, int varID, int levelID, const double *data, SizeType numMissVals)
 {
-  cdiStreamWriteVarSlice(streamID, varID, levelID, MEMTYPE_DOUBLE, (const void *) data, nmiss);
+  cdiStreamWriteVarSlice(streamID, varID, levelID, MEMTYPE_DOUBLE, (const void *) data, numMissVals);
 }
 
 /*
 @Function  streamWriteVarSliceF
 @Title     Write a horizontal slice of a variable
 
-@Prototype void streamWriteVarSliceF(int streamID, int varID, int levelID, const float *data, SizeType nmiss)
+@Prototype void streamWriteVarSliceF(int streamID, int varID, int levelID, const float *data, SizeType numMissVals)
 @Parameter
     @Item  streamID  Stream ID, from a previous call to @fref{streamOpenWrite}.
     @Item  varID     Variable identifier.
     @Item  levelID   Level identifier.
     @Item  data      Pointer to a block of single precision floating point data values to be written.
-    @Item  nmiss     Number of missing values.
+    @Item  numMissVals     Number of missing values.
 
 @Description
 The function streamWriteVarSliceF writes the values of a horizontal slice of a variable to an open dataset.
@@ -203,41 +203,43 @@ The values are converted to the external data type of the variable, if necessary
 @EndFunction
 */
 void
-streamWriteVarSliceF(int streamID, int varID, int levelID, const float *data, SizeType nmiss)
+streamWriteVarSliceF(int streamID, int varID, int levelID, const float *data, SizeType numMissVals)
 {
-  if (cdiStreamWriteVarSlice(streamID, varID, levelID, MEMTYPE_FLOAT, (const void *) data, nmiss))
+  if (cdiStreamWriteVarSlice(streamID, varID, levelID, MEMTYPE_FLOAT, (const void *) data, numMissVals))
     {
       // In case the file format does not support single precision writing,
       // we fall back to double precision writing, converting the data on the fly.
       const SizeType elementCount = gridInqSize(vlistInqVarGrid(streamInqVlist(streamID), varID));
       double *conversionBuffer = (double *) Malloc(elementCount * sizeof(*conversionBuffer));
       for (SizeType i = elementCount; i--;) conversionBuffer[i] = (double) data[i];
-      streamWriteVarSlice(streamID, varID, levelID, conversionBuffer, nmiss);
+      streamWriteVarSlice(streamID, varID, levelID, conversionBuffer, numMissVals);
       Free(conversionBuffer);
     }
 }
 
 void
-streamWriteVarChunk(int streamID, int varID, const int rect[][2], const double *data, SizeType nmiss)
+streamWriteVarChunk(int streamID, int varID, const int rect[][2], const double *data, SizeType numMissVals)
 {
-  void (*myCdiStreamWriteVarChunk_)(int streamID, int varID, int memtype, const int rect[3][2], const void *data, SizeType nmiss)
+  void (*myCdiStreamWriteVarChunk_)(int streamID, int varID, int memtype, const int rect[3][2], const void *data,
+                                    SizeType numMissVals)
       = (void (*)(int, int, int, const int[3][2], const void *, SizeType)) namespaceSwitchGet(NSSWITCH_STREAM_WRITE_VAR_CHUNK_)
             .func;
-  myCdiStreamWriteVarChunk_(streamID, varID, MEMTYPE_DOUBLE, rect, data, nmiss);
+  myCdiStreamWriteVarChunk_(streamID, varID, MEMTYPE_DOUBLE, rect, data, numMissVals);
 }
 
 void
-streamWriteVarChunkF(int streamID, int varID, const int rect[][2], const float *data, SizeType nmiss)
+streamWriteVarChunkF(int streamID, int varID, const int rect[][2], const float *data, SizeType numMissVals)
 {
-  void (*myCdiStreamWriteVarChunk_)(int streamID, int varID, int memtype, const int rect[3][2], const void *data, SizeType nmiss)
+  void (*myCdiStreamWriteVarChunk_)(int streamID, int varID, int memtype, const int rect[3][2], const void *data,
+                                    SizeType numMissVals)
       = (void (*)(int, int, int, const int[3][2], const void *, SizeType)) namespaceSwitchGet(NSSWITCH_STREAM_WRITE_VAR_CHUNK_)
             .func;
-  myCdiStreamWriteVarChunk_(streamID, varID, MEMTYPE_FLOAT, rect, data, nmiss);
+  myCdiStreamWriteVarChunk_(streamID, varID, MEMTYPE_FLOAT, rect, data, numMissVals);
 }
 
 // single image implementation
 void
-cdiStreamWriteVarChunk_(int streamID, int varID, int memtype, const int rect[][2], const void *data, SizeType nmiss)
+cdiStreamWriteVarChunk_(int streamID, int varID, int memtype, const int rect[][2], const void *data, SizeType numMissVals)
 {
   if (CDI_Debug) Message("streamID = %d varID = %d", streamID, varID);
 
@@ -266,23 +268,25 @@ cdiStreamWriteVarChunk_(int streamID, int varID, int memtype, const int rect[][2
       break;
 #endif
 #ifdef HAVE_LIBNETCDF
-    case CDI_FILETYPE_NETCDF: cdf_write_var_chunk(streamptr, varID, memtype, rect, data, nmiss); break;
+    case CDI_FILETYPE_NETCDF: cdf_write_var_chunk(streamptr, varID, memtype, rect, data, numMissVals); break;
 #endif
     default: Error("%s support not compiled in!", strfiletype(filetype)); break;
     }
 }
 
 static void
-stream_write_record(int streamID, int memtype, const void *data, SizeType nmiss)
+stream_write_record(int streamID, int memtype, const void *data, SizeType numMissVals)
 {
   check_parg(data);
 
   stream_t *streamptr = stream_to_pointer(streamID);
 
+  if (streamptr->lockIO) CDI_IO_LOCK();
+
   switch (cdiBaseFiletype(streamptr->filetype))
     {
 #ifdef HAVE_LIBGRIB
-    case CDI_FILETYPE_GRIB: grb_write_record(streamptr, memtype, data, nmiss); break;
+    case CDI_FILETYPE_GRIB: grb_write_record(streamptr, memtype, data, numMissVals); break;
 #endif
 #ifdef HAVE_LIBSERVICE
     case CDI_FILETYPE_SRV: srv_write_record(streamptr, memtype, data); break;
@@ -294,21 +298,23 @@ stream_write_record(int streamID, int memtype, const void *data, SizeType nmiss)
     case CDI_FILETYPE_IEG: ieg_write_record(streamptr, memtype, data); break;
 #endif
 #ifdef HAVE_LIBNETCDF
-    case CDI_FILETYPE_NETCDF: cdf_write_record(streamptr, memtype, data, nmiss); break;
+    case CDI_FILETYPE_NETCDF: cdf_write_record(streamptr, memtype, data, numMissVals); break;
 #endif
     default: Error("%s support not compiled in!", strfiletype(streamptr->filetype));
     }
+
+  if (streamptr->lockIO) CDI_IO_UNLOCK();
 }
 
 /*
 @Function  streamWriteRecord
 @Title     Write a horizontal slice of a variable
 
-@Prototype void streamWriteRecord(int streamID, const double *data, SizeType nmiss)
+@Prototype void streamWriteRecord(int streamID, const double *data, SizeType numMissVals)
 @Parameter
     @Item  streamID  Stream ID, from a previous call to @fref{streamOpenWrite}.
     @Item  data      Pointer to a block of double precision floating point data values to be written.
-    @Item  nmiss     Number of missing values.
+    @Item  numMissVals     Number of missing values.
 
 @Description
 The function streamWriteRecord writes the values of a horizontal slice (record) of a variable to an open dataset.
@@ -316,13 +322,13 @@ The values are converted to the external data type of the variable, if necessary
 @EndFunction
 */
 void
-streamWriteRecord(int streamID, const double *data, SizeType nmiss)
+streamWriteRecord(int streamID, const double *data, SizeType numMissVals)
 {
-  stream_write_record(streamID, MEMTYPE_DOUBLE, (const void *) data, nmiss);
+  stream_write_record(streamID, MEMTYPE_DOUBLE, (const void *) data, numMissVals);
 }
 
 void
-streamWriteRecordF(int streamID, const float *data, SizeType nmiss)
+streamWriteRecordF(int streamID, const float *data, SizeType numMissVals)
 {
-  stream_write_record(streamID, MEMTYPE_FLOAT, (const void *) data, nmiss);
+  stream_write_record(streamID, MEMTYPE_FLOAT, (const void *) data, numMissVals);
 }
diff --git a/src/table.c b/src/table.c
index 48a776b4a0f1e76a89c8bbc4e1a54671fdd43607..07cff4e2a363d07a29a3decb66fd23c1f90cb815 100644
--- a/src/table.c
+++ b/src/table.c
@@ -51,17 +51,17 @@ tableDefEntry(int tableID, int id, int ltype, const char *name, const char *long
 
   if (name && name[0])
     {
-      parTable[tableID].pars[item].name = strdupx(name);
+      parTable[tableID].pars[item].name = strdup(name);
       parTable[tableID].pars[item].dupflags |= TABLE_DUP_NAME;
     }
   if (longname && longname[0])
     {
-      parTable[tableID].pars[item].longname = strdupx(longname);
+      parTable[tableID].pars[item].longname = strdup(longname);
       parTable[tableID].pars[item].dupflags |= TABLE_DUP_LONGNAME;
     }
   if (units && units[0])
     {
-      parTable[tableID].pars[item].units = strdupx(units);
+      parTable[tableID].pars[item].units = strdup(units);
       parTable[tableID].pars[item].dupflags |= TABLE_DUP_UNITS;
     }
 }
@@ -97,7 +97,7 @@ static void
 tableGetPath(void)
 {
   char *path = getenv("TABLEPATH");
-  if (path) tablePath = strdupx(path);
+  if (path) tablePath = strdup(path);
   // printf("tablePath = %s\n", tablePath);
 }
 
@@ -385,7 +385,7 @@ tableFromEnv(int modelID, int tablenum)
   }
   tablenameLen = strlen(name2Use);
   memcpy(tablename, name2Use, tablenameLen);
-  if (tablenum) tablenameLen += (size_t) (sprintf(tablename + tablenameLen, "_%03d", tablenum));
+  if (tablenum) tablenameLen += (size_t) (snprintf(tablename + tablenameLen, 256 - tablenameLen, "_%03d", tablenum));
   size_t lenp = 0, lenf = tablenameLen;
   if (tablePath) lenp = strlen(tablePath);
   // if (tablePath) printf("tablePath = %s\n", tablePath);
@@ -500,7 +500,7 @@ tableDef(int modelID, int tablenum, const char *tablename)
 
       parTable[tableID].modelID = modelID;
       parTable[tableID].number = tablenum;
-      if (tablename) parTable[tableID].name = strdupx(tablename);
+      if (tablename) parTable[tableID].name = strdup(tablename);
 
       parTable[tableID].pars = (param_type *) Malloc(MAX_PARS * sizeof(param_type));
     }
diff --git a/src/taxis.c b/src/taxis.c
index bcb28982fe314ff60a1a27ee1138c74d4fd4ad77..facf2778d9a2f97bb0890e01921fa5d38c2da6bf 100644
--- a/src/taxis.c
+++ b/src/taxis.c
@@ -1089,9 +1089,24 @@ datetime2rtimeval(CdiDateTime vDateTime, const taxis_t *taxis)
   return value;
 }
 
+// convert absolute seconds to CdiDateTime
+static CdiDateTime
+seconds2datetime(double timevalue)
+{
+  int calendar = CALENDAR_STANDARD;
+  int64_t seconds = (int64_t) timevalue;
+
+  CdiDateTime datetime0;
+  datetime0.date = cdiDate_encode(1, 1, 1);
+  datetime0.time = cdiTime_encode(0, 0, 0, 0);
+  JulianDate julianDate = julianDate_encode(calendar, datetime0);
+
+  return julianDate_decode(calendar, julianDate_add_seconds(julianDate, seconds));
+}
+
 // convert absolute time value to CdiDateTime
 static CdiDateTime
-atimeval2datetime(double timevalue)
+absTimeval2datetime(double timevalue)
 {
   int64_t vdate = (int64_t) timevalue;
   double tmpval = (timevalue - vdate) * 86400.0;
@@ -1120,17 +1135,16 @@ split_timevalue(double timevalue, int timeunit)
 
   if (timeunit == TUNIT_SECOND)
     {
-      timevalue /= 86400;
-      datetime = atimeval2datetime(timevalue);
+      datetime = seconds2datetime(timevalue);
     }
   else if (timeunit == TUNIT_HOUR)
     {
       timevalue /= 24;
-      datetime = atimeval2datetime(timevalue);
+      datetime = absTimeval2datetime(timevalue);
     }
   else if (timeunit == TUNIT_DAY)
     {
-      datetime = atimeval2datetime(timevalue);
+      datetime = absTimeval2datetime(timevalue);
     }
   else if (timeunit == TUNIT_MONTH)
     {
@@ -1267,6 +1281,23 @@ cdi_decode_timeval(double timevalue, const taxis_t *taxis)
   return (taxis->type == TAXIS_ABSOLUTE) ? split_timevalue(timevalue, taxis->unit) : rtimeval2datetime(timevalue, taxis);
 }
 
+static int64_t
+datetime2seconds(CdiDateTime datetime)
+{
+  int calendar = CALENDAR_STANDARD;
+
+  CdiDateTime datetime0;
+  datetime0.date = cdiDate_encode(1, 1, 1);
+  datetime0.time = cdiTime_encode(0, 0, 0, 0);
+  JulianDate julianDate0 = julianDate_encode(calendar, datetime0);
+  JulianDate julianDate = julianDate_encode(calendar, datetime);
+
+  int64_t days = julianDate.julianDay - julianDate0.julianDay;
+  int64_t seconds = days * 86400 + julianDate.secondOfDay;
+
+  return seconds;
+}
+
 double
 cdi_encode_timeval(CdiDateTime datetime, taxis_t *taxis)
 {
@@ -1285,9 +1316,7 @@ cdi_encode_timeval(CdiDateTime datetime, taxis_t *taxis)
         }
       else if (taxis->unit == TUNIT_SECOND)
         {
-          int hour, minute, second, ms;
-          cdiTime_decode(datetime.time, &hour, &minute, &second, &ms);
-          timeValue = hour * 3600 + minute * 60 + second;
+          timeValue = datetime2seconds(datetime);
         }
       else
         {
diff --git a/src/util.c b/src/util.c
index 3e6a65d3f01308f5c9cd953bc668964035715563..7eada35c9d6a99e63190757c9da6c3011af3e68e 100644
--- a/src/util.c
+++ b/src/util.c
@@ -23,8 +23,8 @@ cdiUUID2Str(const unsigned char *uuid, char *uuidstr)
 {
   if (uuid == NULL || uuidstr == NULL) return 0;
 
-  int iret = sprintf(uuidstr, uuidFmt, uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7], uuid[8], uuid[9],
-                     uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
+  int iret = snprintf(uuidstr, uuidNumHexChars + 1, uuidFmt, uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7],
+                      uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]);
 
   if (iret != uuidNumHexChars)
     {
diff --git a/src/varscan.c b/src/varscan.c
index b00cda6f78178fb080a7adba32ed8faf6967c0cc..dfded42ddac68b1484c58c8c6c0bd30872a2f657 100644
--- a/src/varscan.c
+++ b/src/varscan.c
@@ -119,8 +119,8 @@ varGetEntry(int param, int gridID, int zaxistype, int ltype1, int tsteptype, con
       // testing for "param" implicitly checks if we are beyond the current vartable size:
       if (vartable[varID].param == param)
         {
-          const int no_of_tiles = tiles ? tiles->numberOfTiles : -1;
-          const int vt_no_of_tiles
+          int no_of_tiles = tiles ? tiles->numberOfTiles : -1;
+          int vt_no_of_tiles
               = vartable[varID].tiles ? subtypeGetGlobalDataP(vartable[varID].tiles, SUBTYPE_ATT_NUMBER_OF_TILES) : -1;
           if ((vartable[varID].zaxistype == zaxistype) && (vartable[varID].ltype1 == ltype1)
               && (vartable[varID].tsteptype == tsteptype)
@@ -411,7 +411,7 @@ varAddRecord(int recID, int param, int gridID, int zaxistype, int hasBounds, int
   if (prec > vartable[varID].prec) vartable[varID].prec = prec;
 
   // append current tile to tile subtype info.
-  const int this_tile = varInsertTileSubtype(&vartable[varID], tiles);
+  int this_tile = varInsertTileSubtype(&vartable[varID], tiles);
   int tileID = tileGetEntry(varID, this_tile);
   if (tile_index) (*tile_index) = this_tile;
   if (tileID == CDI_UNDEFID)
@@ -422,7 +422,7 @@ varAddRecord(int recID, int param, int gridID, int zaxistype, int hasBounds, int
     }
 
   // append current level to level table info
-  const int levelID = levelNewEntry(varID, level1, level2, tileID);
+  int levelID = levelNewEntry(varID, level1, level2, tileID);
   if (CDI_Debug)
     Message("vartable[%d].recordTable[%d].levelTable[%d].recID = %d; level1,2=%d,%d", varID, tileID, levelID, recID, level1,
             level2);
@@ -500,7 +500,7 @@ cdi_generate_cmp_varname(const void *s1, const void *s2)
 void
 cdi_generate_vars(stream_t *streamptr)
 {
-  const int vlistID = streamptr->vlistID;
+  int vlistID = streamptr->vlistID;
 
   int *varids = (int *) Malloc(varTableUsed * sizeof(int));
   for (int varID = 0; varID < varTableUsed; varID++) varids[varID] = (int) varID;
@@ -532,26 +532,26 @@ cdi_generate_vars(stream_t *streamptr)
   */
   for (int index = 0; index < varTableUsed; index++)
     {
-      const int varid = varids[index];
+      int varid = varids[index];
 
-      const int gridID = vartable[varid].gridID;
-      const int param = vartable[varid].param;
-      const int ltype1 = vartable[varid].ltype1;
-      const int ltype2 = vartable[varid].ltype2;
+      int gridID = vartable[varid].gridID;
+      int param = vartable[varid].param;
+      int ltype1 = vartable[varid].ltype1;
+      int ltype2 = vartable[varid].ltype2;
       int zaxistype = vartable[varid].zaxistype;
       if (ltype1 == 0 && zaxistype == ZAXIS_GENERIC && cdiDefaultLeveltype != -1) zaxistype = cdiDefaultLeveltype;
-      const int hasBounds = vartable[varid].hasBounds;
-      const int prec = vartable[varid].prec;
+      int hasBounds = vartable[varid].hasBounds;
+      int prec = vartable[varid].prec;
       int instID = vartable[varid].instID;
       int modelID = vartable[varid].modelID;
       int tableID = vartable[varid].tableID;
-      const int tsteptype = vartable[varid].tsteptype;
-      const int comptype = vartable[varid].comptype;
+      int tsteptype = vartable[varid].tsteptype;
+      int comptype = vartable[varid].comptype;
 
-      const double level_sf = (vartable[varid].level_sf != 0) ? (1.0 / vartable[varid].level_sf) : 1;
+      double level_sf = (vartable[varid].level_sf != 0) ? (1.0 / vartable[varid].level_sf) : 1;
 
       /* consistency check: test if all subtypes have the same levels: */
-      const int nlevels = vartable[varid].recordTable[0].nlevels;
+      int nlevels = vartable[varid].recordTable[0].nlevels;
       for (int isub = 1; isub < vartable[varid].nsubtypes; isub++)
         {
           if (vartable[varid].recordTable[isub].nlevels != nlevels)
@@ -688,12 +688,12 @@ cdi_generate_vars(stream_t *streamptr)
         {
           resize_opt_grib_entries(&vlistptr->vars[varID], vlistptr->vars[varID].opt_grib_nentries + 1);
           vlistptr->vars[varID].opt_grib_nentries += 1;
-          const int idx = vlistptr->vars[varID].opt_grib_nentries - 1;
+          int idx = vlistptr->vars[varID].opt_grib_nentries - 1;
 
           vlistptr->vars[varID].opt_grib_kvpair[idx] = vartable[varid].opt_grib_kvpair[i];
           vlistptr->vars[varID].opt_grib_kvpair[idx].keyword = NULL;
           if (vartable[varid].opt_grib_kvpair[i].keyword)
-            vlistptr->vars[varID].opt_grib_kvpair[idx].keyword = strdupx(vartable[varid].opt_grib_kvpair[i].keyword);
+            vlistptr->vars[varID].opt_grib_kvpair[idx].keyword = strdup(vartable[varid].opt_grib_kvpair[i].keyword);
           vlistptr->vars[varID].opt_grib_kvpair[i].update = true;
         }
       // note: if the key is not defined, we do not throw an error!
@@ -784,13 +784,13 @@ zaxis_compare(int zaxisID, int zaxistype, int nlevels, const double *levels, con
   int ltype1_0 = 0, ltype2_0 = -1;
   cdiInqKeyInt(zaxisID, CDI_GLOBAL, CDI_KEY_TYPEOFFIRSTFIXEDSURFACE, &ltype1_0);
   cdiInqKeyInt(zaxisID, CDI_GLOBAL, CDI_KEY_TYPEOFSECONDFIXEDSURFACE, &ltype2_0);
-  const bool ltype1IsEqual = (ltype1 == ltype1_0);
-  const bool ltype2IsEqual = (ltype2 == ltype2_0);
-  const bool hasBounds = (lbounds && ubounds);
+  bool ltype1IsEqual = (ltype1 == ltype1_0);
+  bool ltype2IsEqual = (ltype2 == ltype2_0);
+  bool hasBounds = (lbounds && ubounds);
 
   if (ltype1IsEqual && ltype2IsEqual && (zaxistype == zaxisInqType(zaxisID) || zaxistype == ZAXIS_GENERIC))
     {
-      const bool hasBoundsZ = (zaxisInqLbounds(zaxisID, NULL) > 0 && zaxisInqUbounds(zaxisID, NULL) > 0);
+      bool hasBoundsZ = (zaxisInqLbounds(zaxisID, NULL) > 0 && zaxisInqUbounds(zaxisID, NULL) > 0);
       if (nlevels == zaxisInqSize(zaxisID) && hasBoundsZ == hasBounds)
         {
           const double *dlevels = zaxisInqLevelsPtr(zaxisID);
@@ -1091,7 +1091,7 @@ varDefOptGribInt(int varID, int tile_index, long lval, const char *keyword)
     }
   vartable[varID].opt_grib_kvpair[idx].data_type = t_int;
   vartable[varID].opt_grib_kvpair[idx].int_val = (int) lval;
-  vartable[varID].opt_grib_kvpair[idx].keyword = strdupx(keyword);
+  vartable[varID].opt_grib_kvpair[idx].keyword = strdup(keyword);
   vartable[varID].opt_grib_kvpair[idx].subtype_index = tile_index;
 }
 #endif
@@ -1121,7 +1121,7 @@ varDefOptGribDbl(int varID, int tile_index, double dval, const char *keyword)
     }
   vartable[varID].opt_grib_kvpair[idx].data_type = t_double;
   vartable[varID].opt_grib_kvpair[idx].dbl_val = dval;
-  vartable[varID].opt_grib_kvpair[idx].keyword = strdupx(keyword);
+  vartable[varID].opt_grib_kvpair[idx].keyword = strdup(keyword);
   vartable[varID].opt_grib_kvpair[idx].subtype_index = tile_index;
 }
 #endif
diff --git a/src/vlist.c b/src/vlist.c
index 743d5bd1833594c7151fc78b67a942826f7d25e9..1190212745134ca8cf081b30b811084360cf88a4 100644
--- a/src/vlist.c
+++ b/src/vlist.c
@@ -299,7 +299,7 @@ var_copy_entries(var_t *var2, var_t *var1)
       if (var1->opt_grib_kvpair[i].keyword != NULL)
         {
           var2->opt_grib_kvpair[i] = var1->opt_grib_kvpair[i];
-          var2->opt_grib_kvpair[i].keyword = strdupx(var1->opt_grib_kvpair[i].keyword);
+          var2->opt_grib_kvpair[i].keyword = strdup(var1->opt_grib_kvpair[i].keyword);
           var2->opt_grib_kvpair[i].update = true;
           if (CDI_Debug) Message("done.");
         }
@@ -446,7 +446,7 @@ vlist_generate_zaxis(int vlistID, int zaxistype, int nlevels, const double *leve
   vlist_t *vlistptr = vlist_to_pointer(vlistID);
   int nzaxis = vlistptr->nzaxis;
 
-  const bool hasBounds = (lbounds && ubounds);
+  bool hasBounds = (lbounds && ubounds);
 
   for (int index = 0; index < nzaxis; ++index)
     {
@@ -465,6 +465,8 @@ vlist_generate_zaxis(int vlistID, int zaxistype, int nlevels, const double *leve
       query.zaxistype = zaxistype;
       query.nlevels = nlevels;
       query.levels = levels;
+      query.lbounds = lbounds;
+      query.ubounds = ubounds;
 
       if ((zaxisglobdefined = (cdiResHFilterApply(getZaxisOps(), vgzZAxisSearch, &query) == CDI_APPLY_STOP)))
         zaxisID = query.resIDValue;
@@ -963,23 +965,15 @@ vlistNrecs(int vlistID)
 int
 vlistNumber(int vlistID)
 {
-  int number, number2;
   vlist_t *vlistptr = vlist_to_pointer(vlistID);
 
   int datatype = vlistptr->vars[0].datatype;
-  if (datatype == CDI_DATATYPE_CPX32 || datatype == CDI_DATATYPE_CPX64)
-    number = CDI_COMP;
-  else
-    number = CDI_REAL;
+  int number = (datatype == CDI_DATATYPE_CPX32 || datatype == CDI_DATATYPE_CPX64) ? CDI_COMP : CDI_REAL;
 
   for (int varID = 1; varID < vlistptr->nvars; varID++)
     {
       datatype = vlistptr->vars[varID].datatype;
-      if (datatype == CDI_DATATYPE_CPX32 || datatype == CDI_DATATYPE_CPX64)
-        number2 = CDI_COMP;
-      else
-        number2 = CDI_REAL;
-
+      int number2 = (datatype == CDI_DATATYPE_CPX32 || datatype == CDI_DATATYPE_CPX64) ? CDI_COMP : CDI_REAL;
       if (number2 != number)
         {
           number = CDI_BOTH;
@@ -1070,10 +1064,10 @@ vlistNtsteps(int vlistID)
 static void
 vlistPrintKernel(vlist_t *vlistptr, FILE *fp)
 {
-  const int vlistID = vlistptr->self;
+  int vlistID = vlistptr->self;
   fprintf(fp, "#\n# vlistID %d\n#\n", vlistID);
 
-  const int nvars = vlistptr->nvars;
+  int nvars = vlistptr->nvars;
 
   fprintf(fp,
           "nvars    : %d\n"
diff --git a/src/vlist_var.c b/src/vlist_var.c
index d99e787712117b8d012575d2ad2692951dad82df..bce417e7f27a10f66fe9a1ac537e3af51de2a68b 100644
--- a/src/vlist_var.c
+++ b/src/vlist_var.c
@@ -431,11 +431,11 @@ vlistInqVarName(int vlistID, int varID, char *name)
           int code = pnum;
           int tableID = varptr->tableID;
           tableInqEntry(tableID, code, -1, name, NULL, NULL);
-          if (!name[0]) sprintf(name, "var%d", code);
+          if (!name[0]) snprintf(name, 8, "var%d", code);
         }
       else
         {
-          sprintf(name, "param%d.%d.%d", pnum, pcat, pdis);
+          snprintf(name, CDI_MAX_NAME, "param%d.%d.%d", pnum, pcat, pdis);
         }
     }
 }
@@ -480,14 +480,16 @@ vlistCopyVarName(int vlistID, int varID)
       else
         {
           // No luck, fall back to outputting a name of the format "var<num>".
-          result = (char *) Malloc(3 + 3 * sizeof(int) * CHAR_BIT / 8 + 2);
-          sprintf(result, "var%d", number);
+          size_t len = 3 + 3 * sizeof(int) * CHAR_BIT / 8 + 2;
+          result = (char *) Malloc(len);
+          snprintf(result, len, "var%d", number);
         }
     }
   else
     {
-      result = (char *) Malloc(5 + 2 + 3 * (3 * sizeof(int) * CHAR_BIT + 1) + 1);
-      sprintf(result, "param%d.%d.%d", number, category, discipline);
+      size_t len = 5 + 2 + 3 * (3 * sizeof(int) * CHAR_BIT + 1) + 1;
+      result = (char *) Malloc(len);
+      snprintf(result, len, "param%d.%d.%d", number, category, discipline);
     }
   // Finally, we fall back to outputting a name of the format "param<num>.<cat>.<dis>".
   return result;
diff --git a/src/vlist_var_key.c b/src/vlist_var_key.c
index 651e319811c440a7bc602ebc78856d10c7d372fd..10fe1602daf3bbf206ebc470c030f89d0df87294 100644
--- a/src/vlist_var_key.c
+++ b/src/vlist_var_key.c
@@ -40,7 +40,7 @@ vlistDefVarIntKey(int vlistID, int varID, const char *name, int value)
       vlistptr->vars[varID].opt_grib_kvpair[idx].int_val = value;
       vlistptr->vars[varID].opt_grib_kvpair[idx].update = true;
       if (name)
-        vlistptr->vars[varID].opt_grib_kvpair[idx].keyword = strdupx(name);
+        vlistptr->vars[varID].opt_grib_kvpair[idx].keyword = strdup(name);
       else
         Error("Internal error, name undefined!");
     }
@@ -103,7 +103,7 @@ vlistDefVarDblKey(int vlistID, int varID, const char *name, double value)
       vlistptr->vars[varID].opt_grib_kvpair[idx].dbl_val = value;
       vlistptr->vars[varID].opt_grib_kvpair[idx].update = true;
       if (name)
-        vlistptr->vars[varID].opt_grib_kvpair[idx].keyword = strdupx(name);
+        vlistptr->vars[varID].opt_grib_kvpair[idx].keyword = strdup(name);
       else
         Error("Internal error, name undefined!");
     }
@@ -151,7 +151,7 @@ cdiDefAdditionalKey(const char *name)
   cdiNAdditionalGRIBKeys++;
   if (idx >= MAX_OPT_GRIB_ENTRIES) Error("Too many additional keywords!");
   if (name)
-    cdiAdditionalGRIBKeys[idx] = strdupx(name);
+    cdiAdditionalGRIBKeys[idx] = strdup(name);
   else
     Error("Internal error!");
 #else
diff --git a/tests/pio_cksum_asynch.run.in b/tests/pio_cksum_asynch.run.in
index 995acf60449275df8e8bc41d5b2606c5f9d269d1..b5e72a9375792dc81f6c888c079cd82ff9ea225c 100644
--- a/tests/pio_cksum_asynch.run.in
+++ b/tests/pio_cksum_asynch.run.in
@@ -1,7 +1,8 @@
 #!@SHELL@
 
 pio_write_args="-p PIO_ASYNCH -w 3"
-mpi_task_num=7
+#mpi_task_num=7
+mpi_task_num=6
 
 . ./pio_write.run
 ( . ./pio_write_dist_array.run ) || test $? -eq 77
diff --git a/tests/pio_cksum_grb2.run.in b/tests/pio_cksum_grb2.run.in
index 09741ee26b7ef517881696f6e3a72ebe26aa62da..858d0f0823c9b45c1903eb319b7a894e0416b4c9 100644
--- a/tests/pio_cksum_grb2.run.in
+++ b/tests/pio_cksum_grb2.run.in
@@ -2,7 +2,8 @@
 
 suffix=grb2
 pio_write_args="-w 3"
-mpi_task_num=8
+#mpi_task_num=8
+mpi_task_num=6
 
 . ./pio_write.run
 ( . ./pio_write_dist_array.run ) || test $? -eq 77
diff --git a/tests/pio_cksum_nc2.run.in b/tests/pio_cksum_nc2.run.in
index 70159cd3351885574154fdca143b605c9d730385..fa606ee4e1b7356795943dadbe5dd88eb2e6a839 100644
--- a/tests/pio_cksum_nc2.run.in
+++ b/tests/pio_cksum_nc2.run.in
@@ -2,7 +2,8 @@
 
 suffix=nc2
 pio_write_args="-w 3 -qtaxis-type=relative"
-mpi_task_num=8
+#mpi_task_num=8
+mpi_task_num=6
 
 . ./pio_write.run
 ( . ./pio_write_dist_array.run ) || test $? -eq 77
diff --git a/tests/pio_cksum_nc4.run.in b/tests/pio_cksum_nc4.run.in
index 940b7dc352c61b4aace6cdfaecaa6a9f01804f44..7583f31ee60ab3f3c5f7e9020518a34af5ff3d60 100644
--- a/tests/pio_cksum_nc4.run.in
+++ b/tests/pio_cksum_nc4.run.in
@@ -2,7 +2,8 @@
 
 suffix=nc4
 pio_write_args="-w 3 -qtaxis-type=relative"
-mpi_task_num=7
+#mpi_task_num=7
+mpi_task_num=6
 
 . ./pio_write.run
 ( . ./pio_write_dist_array.run ) || test $? -eq 77
diff --git a/tests/pio_cksum_writer.run.in b/tests/pio_cksum_writer.run.in
index 63dd4eae7f65c4957c35974f0438d28b2bdfc52b..00f71cc25955d0373100b0e6e11e7a44a1602ce4 100644
--- a/tests/pio_cksum_writer.run.in
+++ b/tests/pio_cksum_writer.run.in
@@ -1,7 +1,9 @@
 #!@SHELL@
 
-pio_write_args="-p PIO_WRITER -w 4"
-mpi_task_num=8
+#pio_write_args="-p PIO_WRITER -w 4"
+pio_write_args="-p PIO_WRITER -w 3"
+#mpi_task_num=8
+mpi_task_num=6
 
 . ./pio_write.run
 ( . ./pio_write_dist_array.run ) || test $? -eq 77
diff --git a/tests/test_cdf_read.c b/tests/test_cdf_read.c
index 603eec984bc01e12f8db4d9f4ddb842748448e7e..35390ec56ee674e181929c3e0435e77d418cb25a 100644
--- a/tests/test_cdf_read.c
+++ b/tests/test_cdf_read.c
@@ -84,8 +84,9 @@ printAtts(int vlistID)
           int rc = cdiInqAtt(vlistID, varID, (int) attIdx, attName, &attType, &attLen);
           {
             const char *varDesc
-                = varIdx > 0 ? (sprintf((char *) buf, "%s%d", varDescPrefix, vlistInqVarCode(vlistID, varID)), (char *) buf)
-                             : globDesc;
+                = varIdx > 0
+                      ? (snprintf((char *) buf, bufSize, "%s%d", varDescPrefix, vlistInqVarCode(vlistID, varID)), (char *) buf)
+                      : globDesc;
             printf("%s attribute \"%s\", value: ", varDesc, attName);
           }
           if (attLen < 0)