6
6
import numpy as np
7
7
import pytest
8
8
import xarray as xr
9
- from pygmt import grdfill , grdinfo
9
+ from pygmt import grdfill , load_dataarray
10
10
from pygmt .datasets import load_earth_relief
11
11
from pygmt .exceptions import GMTInvalidInput
12
12
from pygmt .helpers import GMTTempFile
@@ -18,36 +18,56 @@ def fixture_grid():
18
18
Load the grid data from the sample earth_relief file and set value(s) to
19
19
NaN.
20
20
"""
21
- grid = load_earth_relief (registration = "pixel" , region = [- 5 , 5 , - 5 , 5 ])
22
- grid [3 : 5 , 3 : 5 ] = np .nan
23
- grid [5 : 7 , 5 : 7 ] = np .inf
21
+ grid = load_earth_relief (registration = "pixel" , region = [125 , 130 , - 25 , - 20 ])
22
+ grid [2 : 4 , 1 : 3 ] = np .nan
23
+ grid [0 : 2 , 2 : 4 ] = np .inf
24
24
return grid
25
25
26
26
27
- def test_grdfill_dataarray_out (grid ):
27
+ @pytest .fixture (scope = "module" , name = "expected_grid" )
28
+ def fixture_grid_result ():
28
29
"""
29
- grdfill with a DataArray output.
30
+ Load the expected grdfill grid result.
31
+ """
32
+ return xr .DataArray (
33
+ data = [
34
+ [442.5 , 439.0 , np .inf , np .inf , 508.0 ],
35
+ [393.0 , 364.5 , np .inf , np .inf , 506.5 ],
36
+ [362.0 , 20.0 , 20.0 , 373.5 , 402.5 ],
37
+ [321.5 , 20.0 , 20.0 , 356.0 , 422.5 ],
38
+ [282.5 , 318.0 , 326.5 , 379.5 , 383.5 ],
39
+ ],
40
+ coords = dict (
41
+ lon = [125.5 , 126.5 , 127.5 , 128.5 , 129.5 ],
42
+ lat = [- 24.5 , - 23.5 , - 22.5 , - 21.5 , - 20.5 ],
43
+ ),
44
+ dims = ["lat" , "lon" ],
45
+ )
46
+
47
+
48
+ def test_grdfill_dataarray_out (grid , expected_grid ):
49
+ """
50
+ Test grdfill with a DataArray output.
30
51
"""
31
52
result = grdfill (grid = grid , mode = "c20" )
32
53
# check information of the output grid
33
54
assert isinstance (result , xr .DataArray )
34
- assert result [4 , 4 ] == 20
35
- assert result [5 , 5 ] == np .inf
36
- assert not result .isnull ().all () # check that no NaN values exists
37
55
assert result .gmt .gtype == 1 # Geographic grid
38
56
assert result .gmt .registration == 1 # Pixel registration
57
+ # check information of the output grid
58
+ xr .testing .assert_allclose (a = result , b = expected_grid )
39
59
40
60
41
- def test_grdfill_file_out (grid ):
61
+ def test_grdfill_file_out (grid , expected_grid ):
42
62
"""
43
- grdfill with an outgrid set.
63
+ Test grdfill with an outgrid set.
44
64
"""
45
65
with GMTTempFile (suffix = ".nc" ) as tmpfile :
46
66
result = grdfill (grid = grid , mode = "c20" , outgrid = tmpfile .name )
47
67
assert result is None # return value is None
48
68
assert os .path .exists (path = tmpfile .name ) # check that outgrid exists
49
- result = grdinfo (tmpfile .name , per_column = True ). strip ( )
50
- assert result == "-5 5 -5 5 -5130.5 inf 1 1 10 10 1 1"
69
+ temp_grid = load_dataarray (tmpfile .name )
70
+ xr . testing . assert_allclose ( a = temp_grid , b = expected_grid )
51
71
52
72
53
73
def test_grdfill_required_args (grid ):
0 commit comments