-
Notifications
You must be signed in to change notification settings - Fork 214
/
test_dimfilter.py
81 lines (70 loc) · 2.34 KB
/
test_dimfilter.py
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
78
79
80
81
"""
Test pygmt.dimfilter.
"""
from pathlib import Path
import pytest
import xarray as xr
from pygmt import dimfilter, load_dataarray
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import GMTTempFile
from pygmt.helpers.testing import load_static_earth_relief
@pytest.fixture(scope="module", name="grid")
def fixture_grid():
"""
Load the grid data from the static_earth_relief file.
"""
return load_static_earth_relief()
@pytest.fixture(scope="module", name="expected_grid")
def fixture_expected_grid():
"""
Load the expected dimfilter grid result.
"""
return xr.DataArray(
data=[
[346.0, 344.5, 349.0, 349.0],
[344.5, 318.5, 344.5, 394.0],
[344.5, 356.5, 345.5, 352.5],
[367.5, 349.0, 385.5, 349.0],
[435.0, 385.5, 413.5, 481.5],
],
coords={
"lon": [-54.5, -53.5, -52.5, -51.5],
"lat": [-23.5, -22.5, -21.5, -20.5, -19.5],
},
dims=["lat", "lon"],
)
def test_dimfilter_outgrid(grid, expected_grid):
"""
Test the required parameters for dimfilter with a set outgrid.
"""
with GMTTempFile(suffix=".nc") as tmpfile:
result = dimfilter(
grid=grid,
outgrid=tmpfile.name,
filter="m600",
distance=4,
sectors="l6",
region=[-55, -51, -24, -19],
)
assert result is None # return value is None
assert Path(tmpfile.name).stat().st_size > 0 # check that outgrid exists
temp_grid = load_dataarray(tmpfile.name)
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
def test_dimfilter_no_outgrid(grid, expected_grid):
"""
Test the required parameters for dimfilter with no set outgrid.
"""
result = dimfilter(
grid=grid, filter="m600", distance=4, sectors="l6", region=[-55, -51, -24, -19]
)
assert result.dims == ("lat", "lon")
assert result.gmt.gtype == 1 # Geographic grid
assert result.gmt.registration == 1 # Pixel registration
xr.testing.assert_allclose(a=result, b=expected_grid)
def test_dimfilter_fails(grid):
"""
Check that dimfilter fails correctly when not all of sectors, filters, and
distance are specified.
"""
with pytest.raises(GMTInvalidInput):
dimfilter(grid=grid, sectors="l6", distance=4)