Skip to content

Commit

Permalink
Only use xarray engine for grib data
Browse files Browse the repository at this point in the history
  • Loading branch information
sandorkertesz committed Jan 22, 2025
1 parent 4df69c6 commit 19da21a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
13 changes: 13 additions & 0 deletions src/earthkit/data/readers/grib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ def _is_default(magic, content_type):
return (magic is None or len(magic) == 0) and (content_type is None or len(content_type) == 0)


def is_grib_file(path):
from earthkit.data.core.settings import SETTINGS

n_bytes = SETTINGS.get("reader-type-check-bytes")
magic = None
with open(path, "rb") as f:
magic = f.read(n_bytes)

if _match_magic(magic, False):
return True
return _match_magic(magic, True)


def reader(source, path, *, magic=None, deeper_check=False, **kwargs):
if _match_magic(magic, deeper_check):
from .file import GRIBReader
Expand Down
21 changes: 15 additions & 6 deletions src/earthkit/data/utils/xarray/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,23 +293,32 @@ def open_dataset(

@classmethod
def guess_can_open(cls, filename_or_obj):
return True # filename_or_obj.endswith(".grib")
try:
from earthkit.data.core import Base

if isinstance(filename_or_obj, Base):
return True
elif isinstance(filename_or_obj, str):
from earthkit.data.readers.grib import is_grib_file

return is_grib_file(filename_or_obj)
except Exception:
LOG.debug(
"Failed to guess if %s can be opened by the earthkit backend", filename_or_obj, exc_info=True
)

return False

@staticmethod
def _fieldlist(filename_or_obj, source_type):
from earthkit.data.core import Base

if isinstance(filename_or_obj, Base):
ds = filename_or_obj
# TODO: Add Path? or handle with try statement
elif isinstance(filename_or_obj, str):
from earthkit.data import from_source

ds = from_source(source_type, filename_or_obj)
else:
from earthkit.data import from_object

ds = from_object(filename_or_obj)
return ds


Expand Down
10 changes: 10 additions & 0 deletions tests/readers/test_geotiff_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ def test_geotiff_reader_with_multiband():
assert s[0].shape == (294, 315)


@pytest.mark.skipif(NO_RIOXARRAY, reason="rioxarray not available")
@pytest.mark.with_proj
def test_geotiff_bypassing_xr_engine():
import xarray as xr

# the earthkit xarray engine should not try to handle this file but leave it to the rioxarray backend
da = xr.open_dataarray(earthkit_test_data_file("dgm50hs_col_32_368_5616_nw.tif"))
assert da.name == "band_data"


if __name__ == "__main__":
from earthkit.data.testing import main

Expand Down

0 comments on commit 19da21a

Please sign in to comment.