From 55067161a9b4a8095ca298c3ccf78dad84b7926b Mon Sep 17 00:00:00 2001 From: Sandor Kertesz Date: Mon, 14 Jul 2025 10:34:00 +0100 Subject: [PATCH] Allow using pathlib.Path in Xarray engine with open_dataset (#741) * Allow using pathlib.Path in Xarray engine with open_dataset --- docs/release_notes/version_0.15_updates.rst | 9 +++++ src/earthkit/data/utils/xarray/engine.py | 5 ++- tests/xr_engine/test_xr_engine.py | 41 ++++++++++----------- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/docs/release_notes/version_0.15_updates.rst b/docs/release_notes/version_0.15_updates.rst index 52f37d4ac..988506142 100644 --- a/docs/release_notes/version_0.15_updates.rst +++ b/docs/release_notes/version_0.15_updates.rst @@ -2,6 +2,15 @@ Version 0.15 Updates ///////////////////////// +Version 0.15.1 +=============== + +Fixes ++++++++++++++++++ + +- Fixed issue when :func:`xarray.open_dataset` did not work with the "earthkit" engine when a ``pathlib.Path`` object specified the input data (:pr:`741`). + + Version 0.15.0 =============== diff --git a/src/earthkit/data/utils/xarray/engine.py b/src/earthkit/data/utils/xarray/engine.py index 2b5975c90..430534c39 100644 --- a/src/earthkit/data/utils/xarray/engine.py +++ b/src/earthkit/data/utils/xarray/engine.py @@ -363,11 +363,14 @@ def guess_can_open(cls, filename_or_obj): @staticmethod def _fieldlist(filename_or_obj, source_type): + import os + import pathlib + from earthkit.data.core import Base if isinstance(filename_or_obj, Base): ds = filename_or_obj - elif isinstance(filename_or_obj, str): + elif isinstance(filename_or_obj, (str, os.PathLike, pathlib.Path)): from earthkit.data import from_source ds = from_source(source_type, filename_or_obj) diff --git a/tests/xr_engine/test_xr_engine.py b/tests/xr_engine/test_xr_engine.py index 7c574b61a..73618284c 100644 --- a/tests/xr_engine/test_xr_engine.py +++ b/tests/xr_engine/test_xr_engine.py @@ -10,6 +10,7 @@ # import os +import pathlib import sys import numpy as np @@ -36,32 +37,28 @@ def test_xr_engine_kwargs_unchanged(engine): @pytest.mark.cache -@pytest.mark.parametrize( - "file", - [ - "pl.grib", - # "era5-levels-members", - # "fields_with_missing_values", - # "lambert_grid", - # "reduced_gg", - # "regular_gg_sfc", - # "regular_gg_pl", - # "regular_gg_ml", - # "regular_gg_ml_g2", - # "regular_ll_sfc", - # "regular_ll_msl", - # "scanning_mode_64", - # "single_gridpoint", - # "spherical_harmonics", - # "t_analysis_and_fc_0", - ], -) -def test_xr_engine_basic(file): - ds = from_source("url", earthkit_remote_test_data_file("test-data", "xr_engine", "level", file)) +def test_xr_engine_basic(): + ds = from_source("url", earthkit_remote_test_data_file("test-data", "xr_engine", "level", "pl.grib")) res = ds.to_xarray() assert res is not None +@pytest.mark.cache +@pytest.mark.parametrize("path_maker", [lambda x: x, lambda x: pathlib.Path(x)]) +def test_xr_engine_open_dataset_path(path_maker): + + ds = from_source("sample", "pl.grib") + path = path_maker(ds.path) + + import xarray as xr + + res = xr.open_dataset( + path, + engine="earthkit", + ) + assert res is not None + + @pytest.mark.cache @pytest.mark.parametrize("api", ["earthkit", "xr"]) def test_xr_engine_detailed_check_1(api):