diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 0d2f561f..7b2f3dc7 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 `_ - Do not set explicit timezone in ``units`` to avoid crashing some versions of xarray. See: `#44 `_. diff --git a/README.rst b/README.rst index 629ee382..b08eedf2 100644 --- a/README.rst +++ b/README.rst @@ -26,10 +26,9 @@ Work in progress: see `#40 `_. - **Alpha** supports writing the index of a GRIB file to disk, to save a full-file scan on open, see `#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 `_ and - `#7 `_, +- **Alpha** support writing carefully-crafted ``xarray.Dataset``'s to a GRIB1 or GRIB2 file, + see the *Advanced write usage* section below and + `#18 `_. - **Alpha** support translating coordinates to different data models and naming conventions, `#24 `_. diff --git a/cfgrib/bindings.py b/cfgrib/bindings.py index de2ce486..4be094fa 100644 --- a/cfgrib/bindings.py +++ b/cfgrib/bindings.py @@ -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: diff --git a/cfgrib/eccodes.h b/cfgrib/eccodes.h index ab29a086..84bbe9ba 100644 --- a/cfgrib/eccodes.h +++ b/cfgrib/eccodes.h @@ -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 *