From ea19700e63bf9036585ad747383c4e4f22c8a62b Mon Sep 17 00:00:00 2001
From: Philippe WAUTELET <philippe.wautelet@aero.obs-mip.fr>
Date: Mon, 29 Mar 2021 15:59:19 +0200
Subject: [PATCH] Philippe 29/03/2021: grib_api: can be compiled instead of
 ecCodes (with option MNH_GRIBAPI=yes)

---
 .gitignore                            |  2 ++
 conf/profile_mesonh.ihm               |  7 ++++++-
 src/LIB/grib_api-1.26.0-Source.tar.gz |  3 +++
 src/Makefile                          | 26 +++++++++++++++++++++++++-
 src/Makefile.MESONH.mk                | 20 ++++++++++++++++++++
 src/Rules.AIX64.mk                    |  6 ++++++
 src/Rules.BG.mk                       |  6 ++++++
 src/Rules.BGQ.mk                      |  6 ++++++
 src/Rules.LXNAGfor.mk                 |  6 ++++++
 src/Rules.LXarm.mk                    |  6 ++++++
 src/Rules.LXcray.mk                   |  6 ++++++
 src/Rules.LXg95.mk                    |  6 ++++++
 src/Rules.LXgfortran.mk               |  7 +++++++
 src/Rules.LXifort.mk                  |  6 ++++++
 src/Rules.LXpathf95.mk                |  6 ++++++
 src/Rules.LXpgi.mk                    |  6 ++++++
 src/Rules.SX8.mk                      |  6 ++++++
 src/configure                         |  5 +++++
 18 files changed, 134 insertions(+), 2 deletions(-)
 create mode 100644 src/LIB/grib_api-1.26.0-Source.tar.gz

diff --git a/.gitignore b/.gitignore
index 08b2e983b..30618d0f3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -41,6 +41,8 @@ pub/ncl_ncarg*/
 src/dir_obj-*
 src/LIB/eccodes*
 !src/LIB/eccodes*.tar.gz
+src/LIB/grib_api*
+!src/LIB/grib_api*.tar.gz
 src/LIB/hdf5*
 !src/LIB/hdf5*.tar.gz
 src/LIB/libaec*
diff --git a/conf/profile_mesonh.ihm b/conf/profile_mesonh.ihm
index 2db28c701..0311dd184 100755
--- a/conf/profile_mesonh.ihm
+++ b/conf/profile_mesonh.ihm
@@ -1,5 +1,5 @@
 #!/bin/bash
-#MNH_LIC Copyright 1994-2020 CNRS, Meteo-France and Universite Paul Sabatier
+#MNH_LIC Copyright 1994-2021 CNRS, Meteo-France and Universite Paul Sabatier
 #MNH_LIC This is part of the Meso-NH software governed by the CeCILL-C licence
 #MNH_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
 #MNH_LIC for details. version 1.
@@ -76,6 +76,11 @@ export VERSION_LIBAEC=${VERSION_LIBAEC}
 #
 export MNH_IOCDF4=${MNH_IOCDF4}
 #
+# Version of GRIBAPI
+#
+export VER_GRIBAPI=${VER_GRIBAPI}
+export VERSION_GRIBAPI=${VERSION_GRIBAPI}
+#
 # Version of ecCodes
 #
 export VERSION_ECCODES=${VERSION_ECCODES}
diff --git a/src/LIB/grib_api-1.26.0-Source.tar.gz b/src/LIB/grib_api-1.26.0-Source.tar.gz
new file mode 100644
index 000000000..149f53758
--- /dev/null
+++ b/src/LIB/grib_api-1.26.0-Source.tar.gz
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:0ca45544c54a3a67de78a62663d4be6be8d9c95a63f1bd30fcb4c14588ac9501
+size 4857336
diff --git a/src/Makefile b/src/Makefile
index 8d7244994..03c5d4492 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,4 +1,4 @@
-#MNH_LIC Copyright 1994-2020 CNRS, Meteo-France and Universite Paul Sabatier
+#MNH_LIC Copyright 1994-2021 CNRS, Meteo-France and Universite Paul Sabatier
 #MNH_LIC This is part of the Meso-NH software governed by the CeCILL-C licence
 #MNH_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
 #MNH_LIC for details. version 1.
@@ -177,7 +177,11 @@ DEP_ALL_USER   = $(sort $(filter-out $(IGNORE_DEP_USER)  ,$(DEP_USER)) )
 .PHONY : all objdirmaster libmaster bibmaster objmaster progmaster
 .PHONY : installmaster cleanlibmaster cleanmaster cleanobjmaster cleanprogmaster
 
+ifeq "$(MNH_GRIBAPI)" "yes"
+all : gribapi progmaster
+else
 all : eccodes_lib progmaster
+endif
 
 objdirmaster :  $(OBJDIR_MASTER)/.dummy
 
@@ -196,7 +200,11 @@ ifeq "$(VER_OASIS)" "OASISAUTO"
 depmaster : oasis
 endif
 
+ifeq "$(MNH_GRIBAPI)" "yes"
+depmaster : gribapi
+else
 depmaster : eccodes_lib
+endif
 
 filedepallmaster :  $(DEP_ALL_MASTER)
 	find  $(OBJDIR_MASTER) -follow -name "*.D" >  $(OBJDIR_MASTER)/filemaster
@@ -290,6 +298,22 @@ cleanproguser :
 
 ##########################################################
 #                                                        #
+# EXTRA LIB : GRIBAPI                                    #
+#                                                        #
+##########################################################
+ifneq "$(findstring 64,$(shell uname -m))" ""
+A64=A64
+endif
+gribapi : $(GRIBAPI_INC)
+$(GRIBAPI_INC) :
+	cd ${DIR_GRIBAPI} && ./configure --disable-shared --disable-jpeg --prefix=${GRIBAPI_PATH} CC="$(CC)" \
+	FC="$(FC)" FCFLAGS="$(GRIB_FLAGS)" ${GRIBAPI_CONF} && $(MAKE) -j 1 clean && \
+	$(MAKE) -j 1 && $(MAKE) -j 1 install && $(MAKE) -j 1 clean
+
+gribapi_clean :
+	- [  -d ${GRIBAPI_PATH} ] && rm -fr ${GRIBAPI_PATH}
+##########################################################
+#                                                        #
 # EXTRA LIB : ecCodes                                    #
 #                                                        #
 ##########################################################
diff --git a/src/Makefile.MESONH.mk b/src/Makefile.MESONH.mk
index 9ad6ff289..c7ee64669 100644
--- a/src/Makefile.MESONH.mk
+++ b/src/Makefile.MESONH.mk
@@ -416,9 +416,28 @@ endif
 
 ARCH_XYZ    := $(ARCH_XYZ)-$(VER_MPI)
 
+##########################################################
+#           Librairie GRIBAPI                            #
+##########################################################
+ifeq "$(MNH_GRIBAPI)" "yes"
+DIR_GRIBAPI?=${SRC_MESONH}/src/LIB/grib_api-${VERSION_GRIBAPI}
+GRIBAPI_PATH?=${OBJDIR_MASTER}/GRIBAPI-${VERSION_GRIBAPI}
+GRIBAPI_INC?=${GRIBAPI_PATH}/include/grib_api.mod
+#
+ifdef DIR_GRIBAPI
+INC_GRIBAPI   ?= -I${GRIBAPI_PATH}/include
+LIB_GRIBAPI   ?= -L${GRIBAPI_PATH}/lib -L${GRIBAPI_PATH}/lib64 -lgrib_api_f90 -lgrib_api
+INC           += $(INC_GRIBAPI)
+LIBS          += $(LIB_GRIBAPI)
+VPATH         += $(GRIBAPI_PATH)/include
+R64_GRIBAPI=R64
+endif
+endif
+
 ##########################################################
 #           ecCodes library                              #
 ##########################################################
+ifneq "$(MNH_GRIBAPI)" "yes"
 DIR_ECCODES_SRC?=${SRC_MESONH}/src/LIB/eccodes-${VERSION_ECCODES}-Source
 DIR_ECCODES_BUILD?=${SRC_MESONH}/src/LIB/eccodes-${VERSION_ECCODES}-${ARCH}-R${MNH_REAL}I${MNH_INT}
 DIR_ECCODES_INSTALL?=${OBJDIR_MASTER}/ECCODES-${VERSION_ECCODES}
@@ -431,6 +450,7 @@ INC           += $(INC_ECCODES)
 LIBS          += $(LIB_ECCODES)
 VPATH         += $(DIR_ECCODES_INSTALL)/include
 endif
+endif
 
 ##########################################################
 #           Librairie OASIS                              #
diff --git a/src/Rules.AIX64.mk b/src/Rules.AIX64.mk
index a6903634a..5f750fdcd 100644
--- a/src/Rules.AIX64.mk
+++ b/src/Rules.AIX64.mk
@@ -106,6 +106,12 @@ MNH_COMPRESS=yes
 #if MNH_S4PY exists => compile the libs4py library (for epygram)
 #MNH_S4PY=no
 #
+## ecCodes or grib_api selection
+#MNH_GRIBAPI: if set to no:  use ecCodes
+#             if set to yes: use grib_api (deprecated library)
+#
+MNH_GRIBAPI=no
+#
 ##########################################################
 #                                                        #
 # Source of MESONH PACKAGE  Distribution                 #
diff --git a/src/Rules.BG.mk b/src/Rules.BG.mk
index 7e66bdb4a..7f4261c75 100644
--- a/src/Rules.BG.mk
+++ b/src/Rules.BG.mk
@@ -117,6 +117,12 @@ MNH_IOLFI=yes
 #if MNH_S4PY exists => compile the libs4py library (for epygram)
 #MNH_S4PY=no
 #
+## ecCodes or grib_api selection
+#MNH_GRIBAPI: if set to no:  use ecCodes
+#             if set to yes: use grib_api (deprecated library)
+#
+MNH_GRIBAPI=no
+#
 ##########################################################
 #                                                        #
 # Source of MESONH PACKAGE  Distribution                 #
diff --git a/src/Rules.BGQ.mk b/src/Rules.BGQ.mk
index 28744cc11..1fd0a07bd 100644
--- a/src/Rules.BGQ.mk
+++ b/src/Rules.BGQ.mk
@@ -173,6 +173,12 @@ MNH_IOLFI=yes
 #if MNH_S4PY exists => compile the libs4py library (for epygram)
 #MNH_S4PY=no
 #
+## ecCodes or grib_api selection
+#MNH_GRIBAPI: if set to no:  use ecCodes
+#             if set to yes: use grib_api (deprecated library)
+#
+MNH_GRIBAPI=no
+#
 ##########################################################
 #                                                        #
 # Source of MESONH PACKAGE  Distribution                 #
diff --git a/src/Rules.LXNAGfor.mk b/src/Rules.LXNAGfor.mk
index 28e64bd5c..f623af39c 100644
--- a/src/Rules.LXNAGfor.mk
+++ b/src/Rules.LXNAGfor.mk
@@ -99,6 +99,12 @@ MNH_COMPRESS=yes
 #if MNH_S4PY exists => compile the libs4py library (for epygram)
 #MNH_S4PY=no
 #
+## ecCodes or grib_api selection
+#MNH_GRIBAPI: if set to no:  use ecCodes
+#             if set to yes: use grib_api (deprecated library)
+#
+MNH_GRIBAPI=no
+#
 ##########################################################
 #                                                        #
 # Source of MESONH PACKAGE  Distribution                 #
diff --git a/src/Rules.LXarm.mk b/src/Rules.LXarm.mk
index 539936b92..5ae81163a 100644
--- a/src/Rules.LXarm.mk
+++ b/src/Rules.LXarm.mk
@@ -122,6 +122,12 @@ MNH_COMPRESS=yes
 #if MNH_S4PY exists => compile the libs4py library (for epygram)
 #MNH_S4PY=no
 #
+## ecCodes or grib_api selection
+#MNH_GRIBAPI: if set to no:  use ecCodes
+#             if set to yes: use grib_api (deprecated library)
+#
+MNH_GRIBAPI=no
+#
 ##########################################################
 #                                                        #
 # Source of MESONH PACKAGE  Distribution                 #
diff --git a/src/Rules.LXcray.mk b/src/Rules.LXcray.mk
index c6c34f133..8b5a2ec2e 100644
--- a/src/Rules.LXcray.mk
+++ b/src/Rules.LXcray.mk
@@ -111,6 +111,12 @@ MNH_COMPRESS=yes
 #if MNH_S4PY exists => compile the libs4py library (for epygram)
 #MNH_S4PY=no
 #
+## ecCodes or grib_api selection
+#MNH_GRIBAPI: if set to no:  use ecCodes
+#             if set to yes: use grib_api (deprecated library)
+#
+MNH_GRIBAPI=no
+#
 ##########################################################
 #                                                        #
 # Source of MESONH PACKAGE  Distribution                 #
diff --git a/src/Rules.LXg95.mk b/src/Rules.LXg95.mk
index b147aeeb2..305f9f56c 100644
--- a/src/Rules.LXg95.mk
+++ b/src/Rules.LXg95.mk
@@ -87,6 +87,12 @@ MNH_COMPRESS=yes
 #if MNH_S4PY exists => compile the libs4py library (for epygram)
 #MNH_S4PY=no
 #
+## ecCodes or grib_api selection
+#MNH_GRIBAPI: if set to no:  use ecCodes
+#             if set to yes: use grib_api (deprecated library)
+#
+MNH_GRIBAPI=no
+#
 ##########################################################
 #                                                        #
 # Source of MESONH PACKAGE  Distribution                 #
diff --git a/src/Rules.LXgfortran.mk b/src/Rules.LXgfortran.mk
index 2c4401b01..73839121f 100644
--- a/src/Rules.LXgfortran.mk
+++ b/src/Rules.LXgfortran.mk
@@ -116,6 +116,12 @@ MNH_COMPRESS=yes
 #if MNH_S4PY exists => compile the libs4py library (for epygram)
 #MNH_S4PY=no
 #
+## ecCodes or grib_api selection
+#MNH_GRIBAPI: if set to no:  use ecCodes
+#             if set to yes: use grib_api (deprecated library)
+#
+MNH_GRIBAPI=no
+#
 #
 # Force -fallow-argument-mismatch option for gcc >= 10.1
 # Necessary because some subroutines may be called with different datatypes
@@ -124,6 +130,7 @@ MNH_COMPRESS=yes
 #
 ifeq ($(shell test $(GFV) -ge 1010 ; echo $$?),0)
 OPT_BASE += -fallow-argument-mismatch
+GRIB_FLAGS += -fallow-argument-mismatch
 NETCDF_SUPPFLAGS += -fallow-argument-mismatch
 ECCODES_FFLAGS += -fallow-argument-mismatch
 endif
diff --git a/src/Rules.LXifort.mk b/src/Rules.LXifort.mk
index 903e4d236..7538eee8f 100644
--- a/src/Rules.LXifort.mk
+++ b/src/Rules.LXifort.mk
@@ -211,6 +211,12 @@ MNH_COMPRESS=yes
 #if MNH_S4PY exists => compile the libs4py library (for epygram)
 #MNH_S4PY=no
 #
+## ecCodes or grib_api selection
+#MNH_GRIBAPI: if set to no:  use ecCodes
+#             if set to yes: use grib_api (deprecated library)
+#
+MNH_GRIBAPI=no
+#
 ##########################################################
 #                                                        #
 # Source of MESONH PACKAGE  Distribution                 #
diff --git a/src/Rules.LXpathf95.mk b/src/Rules.LXpathf95.mk
index a59f82f16..932b438aa 100644
--- a/src/Rules.LXpathf95.mk
+++ b/src/Rules.LXpathf95.mk
@@ -67,6 +67,12 @@ MNH_COMPRESS=yes
 #if MNH_S4PY exists => compile the libs4py library (for epygram)
 #MNH_S4PY=no
 #
+## ecCodes or grib_api selection
+#MNH_GRIBAPI: if set to no:  use ecCodes
+#             if set to yes: use grib_api (deprecated library)
+#
+MNH_GRIBAPI=no
+#
 ##########################################################
 #                                                        #
 # Source of MESONH PACKAGE  Distribution                 #
diff --git a/src/Rules.LXpgi.mk b/src/Rules.LXpgi.mk
index 21aea204e..efdb4569f 100644
--- a/src/Rules.LXpgi.mk
+++ b/src/Rules.LXpgi.mk
@@ -122,6 +122,12 @@ MNH_COMPRESS=yes
 #if MNH_S4PY exists => compile the libs4py library (for epygram)
 #MNH_S4PY=no
 #
+## ecCodes or grib_api selection
+#MNH_GRIBAPI: if set to no:  use ecCodes
+#             if set to yes: use grib_api (deprecated library)
+#
+MNH_GRIBAPI=no
+#
 ##########################################################
 #                                                        #
 # Source of MESONH PACKAGE  Distribution                 #
diff --git a/src/Rules.SX8.mk b/src/Rules.SX8.mk
index 085a11728..79e63f46c 100644
--- a/src/Rules.SX8.mk
+++ b/src/Rules.SX8.mk
@@ -109,6 +109,12 @@ MNH_IOLFI=yes
 #if MNH_S4PY exists => compile the libs4py library (for epygram)
 #MNH_S4PY=no
 #
+## ecCodes or grib_api selection
+#MNH_GRIBAPI: if set to no:  use ecCodes
+#             if set to yes: use grib_api (deprecated library)
+#
+MNH_GRIBAPI=no
+#
 ##########################################################
 #                                                        #
 # Source of MESONH PACKAGE  Distribution                 #
diff --git a/src/configure b/src/configure
index 45c0474c6..d15f5aa06 100755
--- a/src/configure
+++ b/src/configure
@@ -17,6 +17,7 @@ export VERSION_HDF=${VERSION_HDF:-"1.12.0"}
 export VERSION_CDFC=${VERSION_CDFC:-"4.7.4"}
 export VERSION_CDFCXX=${VERSION_CDFCXX:-"4.3.1"}
 export VERSION_CDFF=${VERSION_CDFF:-"4.5.3"}
+export VERSION_GRIBAPI=${VERSION_GRIBAPI:-"1.26.0-Source"}
 export VERSION_ECCODES=${VERSION_ECCODES:-"2.18.0"}
 export MNH_INT=${MNH_INT:-"4"}
 export LFI_INT=${LFI_INT:-8}
@@ -550,6 +551,10 @@ if [ "x${MNH_MEGAN}" == "x1" ] ; then
 ( cd $LOCAL/src/LIB ; [ ! -d MEGAN ] && tar xvfz megan.tar.gz )
 fi
 #
+#  Install GRIBAPI
+#
+cd $LOCAL/src/LIB ; [ ! -d grib_api-${VERSION_GRIBAPI} ] && [ -f grib_api-${VERSION_GRIBAPI}.tar.gz ] && gunzip -c grib_api-${VERSION_GRIBAPI}.tar.gz |tar -xvf -
+#
 #  Install ecCodes
 #
 cd $LOCAL/src/LIB ; [ ! -d eccodes-${VERSION_ECCODES}-Source ] && [ -f eccodes-${VERSION_ECCODES}-Source.tar.gz ] && gunzip -c eccodes-${VERSION_ECCODES}-Source.tar.gz |tar -xvf -
-- 
GitLab