7
7
import pytest
8
8
import xarray as xr
9
9
from pygmt import grdcut , load_dataarray
10
- from pygmt .datasets import load_earth_relief
11
10
from pygmt .exceptions import GMTInvalidInput
12
11
from pygmt .helpers import GMTTempFile
12
+ from pygmt .helpers .testing import load_static_earth_relief
13
13
14
14
15
15
@pytest .fixture (scope = "module" , name = "grid" )
16
16
def fixture_grid ():
17
17
"""
18
18
Load the grid data from the sample earth_relief file.
19
19
"""
20
- return load_earth_relief (registration = "pixel" )
20
+ return load_static_earth_relief ()
21
+
22
+
23
+ @pytest .fixture (scope = "module" , name = "region" )
24
+ def fixture_region ():
25
+ """
26
+ Set the data region.
27
+ """
28
+ return [- 53 , - 49 , - 20 , - 17 ]
21
29
22
30
23
31
@pytest .fixture (scope = "module" , name = "expected_grid" )
@@ -27,55 +35,31 @@ def fixture_grid_result():
27
35
"""
28
36
return xr .DataArray (
29
37
data = [
30
- [- 5069 .5 , - 5105.0 , - 4937.0 , - 4708 .0 ],
31
- [- 4115.5 , - 4996.0 , - 4762.0 , - 4599 .0 ],
32
- [- 656 .0 , - 160 .0 , - 3484 .5 , - 3897 .5 ],
38
+ [446 .5 , 481.5 , 439.5 , 553 .0 ],
39
+ [757.0 , 570.5 , 538.5 , 524 .0 ],
40
+ [796 .0 , 886 .0 , 571 .5 , 638 .5 ],
33
41
],
34
- coords = dict (lon = [- 2 .5 , - 1 .5 , - 0 .5 , 0 .5 ], lat = [2 .5 , 3 .5 , 4 .5 ]),
42
+ coords = dict (lon = [- 52 .5 , - 51 .5 , - 50 .5 , - 49 .5 ], lat = [- 19 .5 , - 18 .5 , - 17 .5 ]),
35
43
dims = ["lat" , "lon" ],
36
44
)
37
45
38
46
39
- def test_grdcut_file_in_file_out (expected_grid ):
40
- """
41
- grdcut an input grid file, and output to a grid file.
42
- """
43
- with GMTTempFile (suffix = ".nc" ) as tmpfile :
44
- result = grdcut ("@earth_relief_01d" , outgrid = tmpfile .name , region = [- 3 , 1 , 2 , 5 ])
45
- assert result is None # return value is None
46
- assert os .path .exists (path = tmpfile .name ) # check that outgrid exists
47
- temp_grid = load_dataarray (tmpfile .name )
48
- xr .testing .assert_allclose (a = temp_grid , b = expected_grid )
49
-
50
-
51
- def test_grdcut_file_in_dataarray_out (expected_grid ):
52
- """
53
- grdcut an input grid file, and output as DataArray.
54
- """
55
- outgrid = grdcut ("@earth_relief_01d" , region = [- 3 , 1 , 2 , 5 ])
56
- assert isinstance (outgrid , xr .DataArray )
57
- assert outgrid .gmt .registration == 1 # Pixel registration
58
- assert outgrid .gmt .gtype == 1 # Geographic type
59
- # check information of the output grid
60
- xr .testing .assert_allclose (a = outgrid , b = expected_grid )
61
-
62
-
63
- def test_grdcut_dataarray_in_file_out (grid , expected_grid ):
47
+ def test_grdcut_dataarray_in_file_out (grid , expected_grid , region ):
64
48
"""
65
49
grdcut an input DataArray, and output to a grid file.
66
50
"""
67
51
with GMTTempFile (suffix = ".nc" ) as tmpfile :
68
- result = grdcut (grid , outgrid = tmpfile .name , region = [ - 3 , 1 , 2 , 5 ] )
52
+ result = grdcut (grid , outgrid = tmpfile .name , region = region )
69
53
assert result is None # grdcut returns None if output to a file
70
54
temp_grid = load_dataarray (tmpfile .name )
71
55
xr .testing .assert_allclose (a = temp_grid , b = expected_grid )
72
56
73
57
74
- def test_grdcut_dataarray_in_dataarray_out (grid , expected_grid ):
58
+ def test_grdcut_dataarray_in_dataarray_out (grid , expected_grid , region ):
75
59
"""
76
60
grdcut an input DataArray, and output as DataArray.
77
61
"""
78
- outgrid = grdcut (grid , region = [ - 3 , 1 , 2 , 5 ] )
62
+ outgrid = grdcut (grid , region = region )
79
63
assert isinstance (outgrid , xr .DataArray )
80
64
xr .testing .assert_allclose (a = outgrid , b = expected_grid )
81
65
0 commit comments