forked from AntSimi/py-eddy-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_grid.py
More file actions
77 lines (59 loc) · 1.8 KB
/
test_grid.py
File metadata and controls
77 lines (59 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from matplotlib.path import Path
from numpy import array, ma
from pytest import approx
from py_eddy_tracker.data import get_path
from py_eddy_tracker.dataset.grid import RegularGridDataset
G = RegularGridDataset(get_path("mask_1_60.nc"), "lon", "lat")
X = 0.025
contour = Path(
(
(-X, 0),
(X, 0),
(X, X),
(-X, X),
(-X, 0),
)
)
# contour
def test_contour_lon():
assert (contour.lon == (-X, X, X, -X, -X)).all()
def test_contour_lat():
assert (contour.lat == (0, 0, X, X, 0)).all()
def test_contour_mean():
assert (contour.mean_coordinates == (0, X / 2)).all()
def test_contour_fit_circle():
x, y, r, err = contour.fit_circle()
assert x == approx(0)
assert y == approx(X / 2)
assert r == approx(3108, rel=1e-1)
assert err == approx(49.1, rel=1e-1)
def test_pixels_in():
i, j = contour.pixels_in(G)
assert (i == (21599, 0, 1)).all()
assert (j == (5401, 5401, 5401)).all()
def test_contour_grid_slice():
assert contour.bbox_slice == ((21598, 4), (5400, 5404))
# grid
def test_bounds():
x0, x1, y0, y1 = G.bounds
assert x0 == -1 / 120.0 and x1 == 360 - 1 / 120
assert y0 == approx(-90 - 1 / 120.0) and y1 == approx(90 - 1 / 120)
def test_interp():
# Fake grid
g = RegularGridDataset.with_array(
coordinates=("x", "y"),
datas=dict(
z=ma.array(((0, 1), (2, 3)), dtype="f4"),
x=array((0, 20)),
y=array((0, 10)),
),
centered=True,
)
x0, y0 = array((10,)), array((5,))
x1, y1 = array((15,)), array((5,))
# Interp nearest
assert g.interp("z", x0, y0, method="nearest") == 0
assert g.interp("z", x1, y1, method="nearest") == 2
# Interp bilinear
assert g.interp("z", x0, y0) == 1.5
assert g.interp("z", x1, y1) == 2