From 571cefcb07b5dff495bd818a59aa6463592e60b0 Mon Sep 17 00:00:00 2001 From: Eric Engle Date: Wed, 17 Jan 2024 11:35:22 -0500 Subject: [PATCH] Adding support for Gzip GRIB2 files. This commit added support for reading GRIB2 files that have also been gzipped. Some datasets gzip GRIB2 files even though there is no benefit in doing so. When using the grib2io xarray backend, an error will be raised when using Gzipped GIRB2 files. This is a restiction within xarray. This commit references issue #119 --- grib2io/_grib2io.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/grib2io/_grib2io.py b/grib2io/_grib2io.py index bb48c13..1dccf7a 100644 --- a/grib2io/_grib2io.py +++ b/grib2io/_grib2io.py @@ -111,7 +111,23 @@ def __init__(self, filename, mode='r', **kwargs): mode = mode+'b' if 'w' in mode: mode += '+' if 'a' in mode: mode += '+' - self._filehandle = builtins.open(filename,mode=mode,buffering=ONE_MB) + + # Some GRIB2 files are gzipped, so check for that here, but + # raise error when using xarray backend. + if 'r' in mode: + self._filehandle = builtins.open(filename,mode=mode) + # Gzip files contain a 2-byte header b'\x1f\x8b'. + if self._filehandle.read(2) == b'\x1f\x8b': + self._filehandle.close() + if '_xarray_backend' in kwargs.keys(): + raise RuntimeError('Gzip GRIB2 files are not supported by the Xarray backend.') + import gzip + self._filehandle = gzip.open(filename,mode=mode) + else: + self._filehandle = builtins.open(filename,mode=mode,buffering=ONE_MB) + else: + self._filehandle = builtins.open(filename,mode=mode,buffering=ONE_MB) + self._hasindex = False self._index = {} self.mode = mode