Skip to content

Commit e171fe2

Browse files
Fix crash when add_earthkit_attrs=False used in Xarray engine (#696)
* Fix crash when add_earthkit_attrs=False used in Xarray engine
1 parent 4699084 commit e171fe2

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/earthkit/data/utils/xarray/builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def prepare_tensor(self, ds, dims, name):
407407
elif num > 1 or not self.profile.dims.squeeze or d.name in self.profile.dims.ensure_dims:
408408
tensor_dims.append(d)
409409
tensor_coords[d.key] = vals[d.key]
410-
if d.key in component_vals:
410+
if component_vals and d.key in component_vals:
411411
tensor_coords_component[d.key] = component_vals[d.key]
412412

413413
# check if the dims/coords are consistent with the tensors of

tests/xr_engine/test_xr_engine.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ def test_xr_engine_single_field():
542542

543543
@pytest.mark.cache
544544
@pytest.mark.parametrize("add", [False, True])
545-
def test_xr_engine_add_earthkit_attrs(add):
545+
def test_xr_engine_add_earthkit_attrs_1(add):
546546
ds_ek = from_source("url", earthkit_remote_test_data_file("test-data/xr_engine/level/pl.grib"))
547547
ds_ek = ds_ek[0]
548548

@@ -558,3 +558,16 @@ def test_xr_engine_add_earthkit_attrs(add):
558558
assert "_earthkit" in ds["t"].attrs
559559
else:
560560
assert "_earthkit" not in ds["t"].attrs
561+
562+
563+
@pytest.mark.cache
564+
def test_xr_engine_add_earthkit_attrs_2():
565+
ds_ek = from_source("url", earthkit_remote_test_data_file("test-data/xr_engine/level/pl.grib"))
566+
ds_ek = ds_ek[0]
567+
568+
ds = ds_ek.to_xarray(
569+
add_earthkit_attrs=False,
570+
)
571+
572+
assert ds
573+
assert "_earthkit" not in ds["t"].attrs

0 commit comments

Comments
 (0)