Skip to content

Commit a12b024

Browse files
authored
cyclic_dataarray: extrapolate coords (regression) (#58)
* cyclic_dataarray: extrapolate coords (regression) * changelog
1 parent 40b30d8 commit a12b024

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
### Bug fixes
1010

11+
- Fix a regression introduced in [#33](https://github.com/mathause/mplotutils/pull/33):
12+
`cyclic_dataarray` now correctly extrapolates the coordinates
13+
([#58](https://github.com/mathause/mplotutils/pull/58)).
14+
1115
### Internal changes
1216

1317

mplotutils/cartopy_utils.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,22 @@ def cyclic_dataarray(obj, coord="lon"):
112112
113113
"""
114114

115-
return obj.pad({coord: (0, 1)}, mode="wrap")
115+
if coord not in obj.coords:
116+
raise KeyError(f"Did not find '{coord}' in obj")
117+
118+
obj = obj.pad({coord: (0, 1)}, mode="wrap")
119+
120+
# extrapolate the coords
121+
lon = obj[coord]
122+
123+
diff = lon.isel({coord: slice(None, -1)}).diff(coord)
124+
125+
if not np.allclose(diff, diff[0]):
126+
raise ValueError(f"The coordinate '{coord}' must be equally spaced")
127+
128+
lon.data[-1] = lon[-2] + diff[0]
129+
130+
return obj.assign_coords({coord: lon})
116131

117132

118133
@_deprecate_positional_args("0.3")

mplotutils/tests/test_cyclic_dataarray.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@
44
from mplotutils import cyclic_dataarray
55

66

7+
@pytest.mark.parametrize("as_dataset", (True, False))
8+
def test_cyclic_dataarray_missing_coord(as_dataset):
9+
10+
da = xr.DataArray([1, 2, 3], dims=("x"), coords={"x": [0, 1, 2]}, name="data")
11+
data = da.to_dataset() if as_dataset else da
12+
13+
with pytest.raises(KeyError, match="Did not find 'lon' in obj"):
14+
cyclic_dataarray(data)
15+
16+
with pytest.raises(KeyError, match="Did not find 'longitude' in obj"):
17+
cyclic_dataarray(data, "longitude")
18+
19+
20+
@pytest.mark.parametrize("as_dataset", (True, False))
21+
def test_cyclic_dataarray_not_equally_spaced(as_dataset):
22+
23+
da = xr.DataArray([1, 2, 3], dims=("x"), coords={"x": [0, 1, 2.1]}, name="data")
24+
data = da.to_dataset() if as_dataset else da
25+
26+
with pytest.raises(ValueError, match=".*must be equally spaced"):
27+
cyclic_dataarray(data, coord="x")
28+
29+
730
@pytest.mark.parametrize("as_dataset", (True, False))
831
def test_cyclic_dataarray(as_dataset):
932

@@ -14,7 +37,7 @@ def test_cyclic_dataarray(as_dataset):
1437

1538
expected = [[1, 2, 3, 1], [4, 5, 6, 4]]
1639
da_expected = xr.DataArray(
17-
expected, dims=("y", "x"), coords={"y": [1, 2], "x": [0, 1, 2, 0]}, name="data"
40+
expected, dims=("y", "x"), coords={"y": [1, 2], "x": [0, 1, 2, 3]}, name="data"
1841
)
1942

2043
data = da.to_dataset() if as_dataset else da

0 commit comments

Comments
 (0)