Skip to content

Commit

Permalink
Adding support for Gzip GRIB2 files.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
EricEngle-NOAA committed Jan 17, 2024
1 parent cbe97d7 commit e35ab35
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion grib2io/_grib2io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e35ab35

Please sign in to comment.