/
bindings.py
63 lines (50 loc) · 1.71 KB
/
bindings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#
# (C) Copyright 2017- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation nor
# does it submit to any jurisdiction.
#
# Authors:
# Alessandro Amici - B-Open - https://bopen.eu
#
from __future__ import absolute_import, division, print_function, unicode_literals
import logging
import pkgutil
import os
import cffi
__version__ = "1.0.1"
LOG = logging.getLogger(__name__)
try:
from ._bindings import ffi, lib
except ModuleNotFoundError:
ffi = cffi.FFI()
CDEF = pkgutil.get_data(__name__, "grib_api.h")
CDEF += pkgutil.get_data(__name__, "eccodes.h")
ffi.cdef(CDEF.decode("utf-8").replace("\r", "\n"))
LIBNAMES = ["eccodes", "libeccodes.so", "libeccodes"]
try:
import ecmwflibs
LIBNAMES.insert(0, ecmwflibs.find("eccodes"))
except Exception:
pass
if os.environ.get("ECCODES_DIR"):
eccdir = os.environ["ECCODES_DIR"]
LIBNAMES.insert(0, os.path.join(eccdir, "lib/libeccodes.so"))
LIBNAMES.insert(1, os.path.join(eccdir, "lib64/libeccodes.so"))
lib = None
for libname in LIBNAMES:
try:
lib = ffi.dlopen(libname)
LOG.info("ecCodes library found using name '%s'.", libname)
break
except OSError:
LOG.info("ecCodes library not found using name '%s'.", libname)
pass
if lib is None:
raise RuntimeError(f"ecCodes library not found using {LIBNAMES}")
# default encoding for ecCodes strings
ENC = "ascii"