Skip to content

Commit 968ae5a

Browse files
committed
Bugfix
1 parent 5ed9838 commit 968ae5a

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

cf_xarray/coding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def decode_compress_to_multi_index(encoded, idxnames=None):
105105

106106
names = encoded[idxname].attrs["compress"].split(" ")
107107
shape = [encoded.sizes[dim] for dim in names]
108-
indices = np.unravel_index(encoded.landpoint.data, shape)
108+
indices = np.unravel_index(encoded[idxname].data, shape)
109109
arrays = [encoded[dim].data[index] for dim, index in zip(names, indices)]
110110
mindex = pd.MultiIndex.from_arrays(arrays, names=names)
111111

cf_xarray/tests/test_coding.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818
),
1919
],
2020
)
21-
@pytest.mark.parametrize("idxnames", ["landpoint", ("landpoint",), None])
21+
@pytest.mark.parametrize("idxnames", ["foo", "landpoint", ("landpoint",), None])
2222
def test_compression_by_gathering_multi_index_roundtrip(mindex, idxnames):
23+
dim = "foo" if idxnames == "foo" else "landpoint"
2324
dataset = xr.Dataset(
24-
data_vars={"landsoilt": ("landpoint", np.random.randn(4), {"foo": "bar"})},
25+
data_vars={"landsoilt": (dim, np.random.randn(4), {"foo": "bar"})},
2526
coords={
26-
"landpoint": ("landpoint", mindex, {"long_name": "land point number"}),
27-
"coord1": ("landpoint", [1, 2, 3, 4], {"foo": "baz"}),
27+
dim: (dim, mindex, {"long_name": "land point number"}),
28+
"coord1": (dim, [1, 2, 3, 4], {"foo": "baz"}),
2829
},
2930
attrs={"dataset": "test dataset"},
3031
)
@@ -33,9 +34,9 @@ def test_compression_by_gathering_multi_index_roundtrip(mindex, idxnames):
3334

3435
encoded = cfxr.encode_multi_index_as_compress(dataset, idxnames)
3536
roundtrip = cfxr.decode_compress_to_multi_index(encoded, idxnames)
36-
assert "compress" not in roundtrip["landpoint"].encoding
37+
assert "compress" not in roundtrip[dim].encoding
3738
xr.testing.assert_identical(roundtrip, dataset)
3839

39-
dataset["landpoint"].attrs["compress"] = "lat lon"
40+
dataset[dim].attrs["compress"] = "lat lon"
4041
with pytest.raises(ValueError):
4142
cfxr.encode_multi_index_as_compress(dataset, idxnames)

0 commit comments

Comments
 (0)