-
Notifications
You must be signed in to change notification settings - Fork 214
/
test_grdlandmask.py
66 lines (57 loc) · 2 KB
/
test_grdlandmask.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
"""
Test pygmt.grdlandmask.
"""
from pathlib import Path
import pytest
import xarray as xr
from pygmt import grdlandmask, load_dataarray
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import GMTTempFile
@pytest.fixture(scope="module", name="expected_grid")
def fixture_expected_grid():
"""
Load the expected grdlandmask grid result.
"""
return xr.DataArray(
data=[
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 1.0, 0.0, 0.0],
],
coords={
"lon": [125.0, 126.0, 127.0, 128.0, 129.0, 130.0],
"lat": [30.0, 31.0, 32.0, 33.0, 34.0, 35.0],
},
dims=["lat", "lon"],
)
def test_grdlandmask_outgrid(expected_grid):
"""
Creates a grid land mask with an outgrid argument.
"""
with GMTTempFile(suffix=".nc") as tmpfile:
result = grdlandmask(outgrid=tmpfile.name, spacing=1, region=[125, 130, 30, 35])
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_grdlandmask_no_outgrid(expected_grid):
"""
Test grdlandmask with no set outgrid.
"""
result = grdlandmask(spacing=1, region=[125, 130, 30, 35])
# check information of the output grid
assert isinstance(result, xr.DataArray)
assert result.gmt.gtype == 1 # Geographic grid
assert result.gmt.registration == 0 # Gridline registration
# check information of the output grid
xr.testing.assert_allclose(a=result, b=expected_grid)
def test_grdlandmask_fails():
"""
Check that grdlandmask fails correctly when region and spacing are not
given.
"""
with pytest.raises(GMTInvalidInput):
grdlandmask()