Skip to content

Commit

Permalink
Work around a portability bug in ecCodes by re-implementing a function.
Browse files Browse the repository at this point in the history
Fix #7.
  • Loading branch information
alexamici committed Feb 22, 2019
1 parent d689efe commit 8d92277
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Changelog for cfgrib
0.9.6 (unreleased)
------------------

- Add read-only support on *Windows* after you install *ecCodes* on *conda*.
- Add support for *Windows* by installing *ecCodes* via *conda*.
See: `#7 <https://github.com/ecmwf/cfgrib/issues/7>`_
- Do not set explicit timezone in ``units`` to avoid crashing some versions of xarray.
See: `#44 <https://github.com/ecmwf/cfgrib/issues/44>`_.
Expand Down
7 changes: 3 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ Work in progress:
see `#40 <https://github.com/ecmwf/cfgrib/issues/40>`_.
- **Alpha** supports writing the index of a GRIB file to disk, to save a full-file scan on open,
see `#33 <https://github.com/ecmwf/cfgrib/issues/33>`_.
- **Alpha** support writing carefully-crafted ``xarray.Dataset``'s to a GRIB1 or GRIB2 file on Linux and MacOS,
see the *Advanced write usage* section below,
`#18 <https://github.com/ecmwf/cfgrib/issues/18>`_ and
`#7 <https://github.com/ecmwf/cfgrib/issues/7>`_,
- **Alpha** support writing carefully-crafted ``xarray.Dataset``'s to a GRIB1 or GRIB2 file,
see the *Advanced write usage* section below and
`#18 <https://github.com/ecmwf/cfgrib/issues/18>`_.
- **Alpha** support translating coordinates to different data models and naming conventions,
`#24 <https://github.com/ecmwf/cfgrib/issues/24>`_.

Expand Down
25 changes: 25 additions & 0 deletions cfgrib/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,33 @@ def codes_get_api_version():
return "%d.%d.%d" % (major, minor, patch)


def portable_handle_new_from_samples(samplename, product_kind):
#
# re-implement codes_grib_handle_new_from_samples in a portable way.
# imports are here not to pollute the head of the file with (hopfully!) temporary stuff
#
import os.path
import platform
handle = ffi.NULL
if platform.platform().startswith('Windows'):
samplepath = os.path.join(ffi.string(lib.codes_samples_path(ffi.NULL)), samplename + b'.tmpl')
try:
with open(samplepath) as file:
handle = codes_handle_new_from_file(file, product_kind)
except FileNotFoundError:
pass
return handle


def codes_new_from_samples(samplename, product_kind=CODES_PRODUCT_GRIB):
# type: (bytes, int) -> cffi.FFI.CData

# work around an ecCodes bug on Windows, hopefully this will go away soon
handle = portable_handle_new_from_samples(samplename, product_kind)
if handle != ffi.NULL:
return handle
# end of work-around

if product_kind == CODES_PRODUCT_GRIB:
handle = lib.codes_grib_handle_new_from_samples(ffi.NULL, samplename)
elif product_kind == CODES_PRODUCT_BUFR:
Expand Down
2 changes: 2 additions & 0 deletions cfgrib/eccodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,8 @@ void codes_grib_multi_support_on(codes_context* c);
*/
void codes_grib_multi_support_off(codes_context* c);

char* codes_samples_path(const codes_context *c);

/**
* Get the API version
*
Expand Down

0 comments on commit 8d92277

Please sign in to comment.