-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_cyclic_dataarray.py
More file actions
53 lines (37 loc) · 1.8 KB
/
Copy pathtest_cyclic_dataarray.py
File metadata and controls
53 lines (37 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import pytest
import xarray as xr
from mplotutils import cyclic_dataarray
@pytest.mark.parametrize("as_dataset", (True, False))
def test_cyclic_dataarray_missing_coord(as_dataset):
da = xr.DataArray([1, 2, 3], dims=("x"), coords={"x": [0, 1, 2]}, name="data")
data = da.to_dataset() if as_dataset else da
with pytest.raises(KeyError, match="Did not find 'lon' in obj"):
cyclic_dataarray(data)
with pytest.raises(KeyError, match="Did not find 'longitude' in obj"):
cyclic_dataarray(data, "longitude")
@pytest.mark.parametrize("as_dataset", (True, False))
def test_cyclic_dataarray_not_equally_spaced(as_dataset):
da = xr.DataArray([1, 2, 3], dims=("x"), coords={"x": [0, 1, 2.1]}, name="data")
data = da.to_dataset() if as_dataset else da
with pytest.raises(ValueError, match=".*must be equally spaced"):
cyclic_dataarray(data, coord="x")
@pytest.mark.parametrize("as_dataset", (True, False))
def test_cyclic_dataarray(as_dataset):
data = [[1, 2, 3], [4, 5, 6]]
y = xr.Variable("y", [1, 2], attrs={"foo": "bar"})
x = xr.Variable("x", [0, 1, 2], attrs={"foo": "bar"})
da = xr.DataArray(data, dims=("y", "x"), coords={"y": y, "x": x}, name="data")
expected = [[1, 2, 3, 1], [4, 5, 6, 4]]
x = xr.Variable("x", [0, 1, 2, 3], attrs={"foo": "bar"})
da_expected = xr.DataArray(
expected, dims=("y", "x"), coords={"y": y, "x": x}, name="data"
)
data = da.to_dataset() if as_dataset else da
expected = da_expected.to_dataset() if as_dataset else da_expected
result = cyclic_dataarray(data, "x")
xr.testing.assert_identical(result, expected)
# per default use 'lon'
data = data.rename(x="lon")
expected = expected.rename(x="lon")
result = cyclic_dataarray(data)
xr.testing.assert_identical(result, expected)