6
6
import numpy as np
7
7
import pytest
8
8
import xarray as xr
9
- from pygmt import grdcut , grdinfo
9
+ from pygmt import grdcut , 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
@@ -20,67 +20,64 @@ def fixture_grid():
20
20
return load_earth_relief (registration = "pixel" )
21
21
22
22
23
- def test_grdcut_file_in_file_out ():
23
+ @pytest .fixture (scope = "module" , name = "expected_grid" )
24
+ def fixture_grid_result ():
25
+ """
26
+ Load the expected grdcut grid result.
27
+ """
28
+ return xr .DataArray (
29
+ 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 ],
33
+ ],
34
+ coords = dict (lon = [- 2.5 , - 1.5 , - 0.5 , 0.5 ], lat = [2.5 , 3.5 , 4.5 ]),
35
+ dims = ["lat" , "lon" ],
36
+ )
37
+
38
+
39
+ def test_grdcut_file_in_file_out (expected_grid ):
24
40
"""
25
41
grdcut an input grid file, and output to a grid file.
26
42
"""
27
43
with GMTTempFile (suffix = ".nc" ) as tmpfile :
28
- result = grdcut ("@earth_relief_01d" , outgrid = tmpfile .name , region = "0/180/0/90" )
44
+ result = grdcut ("@earth_relief_01d" , outgrid = tmpfile .name , region = [ - 3 , 1 , 2 , 5 ] )
29
45
assert result is None # return value is None
30
46
assert os .path .exists (path = tmpfile .name ) # check that outgrid exists
31
- result = grdinfo (tmpfile .name , per_column = True )
32
- assert result == "0 180 0 90 -8182 5651.5 1 1 180 90 1 1 \n "
47
+ temp_grid = load_dataarray (tmpfile .name )
48
+ xr . testing . assert_allclose ( a = temp_grid , b = expected_grid )
33
49
34
50
35
- def test_grdcut_file_in_dataarray_out ():
51
+ def test_grdcut_file_in_dataarray_out (expected_grid ):
36
52
"""
37
53
grdcut an input grid file, and output as DataArray.
38
54
"""
39
- outgrid = grdcut ("@earth_relief_01d" , region = "0/180/0/90" )
55
+ outgrid = grdcut ("@earth_relief_01d" , region = [ - 3 , 1 , 2 , 5 ] )
40
56
assert isinstance (outgrid , xr .DataArray )
41
57
assert outgrid .gmt .registration == 1 # Pixel registration
42
58
assert outgrid .gmt .gtype == 1 # Geographic type
43
59
# check information of the output grid
44
- # the '@earth_relief_01d' is in pixel registration, so the grid range is
45
- # not exactly 0/180/0/90
46
- assert outgrid .coords ["lat" ].data .min () == 0.5
47
- assert outgrid .coords ["lat" ].data .max () == 89.5
48
- assert outgrid .coords ["lon" ].data .min () == 0.5
49
- assert outgrid .coords ["lon" ].data .max () == 179.5
50
- assert outgrid .data .min () == - 8182.0
51
- assert outgrid .data .max () == 5651.5
52
- assert outgrid .sizes ["lat" ] == 90
53
- assert outgrid .sizes ["lon" ] == 180
60
+ xr .testing .assert_allclose (a = outgrid , b = expected_grid )
54
61
55
62
56
- def test_grdcut_dataarray_in_file_out (grid ):
63
+ def test_grdcut_dataarray_in_file_out (grid , expected_grid ):
57
64
"""
58
65
grdcut an input DataArray, and output to a grid file.
59
66
"""
60
67
with GMTTempFile (suffix = ".nc" ) as tmpfile :
61
- result = grdcut (grid , outgrid = tmpfile .name , region = "0/180/0/90" )
68
+ result = grdcut (grid , outgrid = tmpfile .name , region = [ - 3 , 1 , 2 , 5 ] )
62
69
assert result is None # grdcut returns None if output to a file
63
- result = grdinfo (tmpfile .name , per_column = True )
64
- assert result == "0 180 0 90 -8182 5651.5 1 1 180 90 1 1 \n "
70
+ temp_grid = load_dataarray (tmpfile .name )
71
+ xr . testing . assert_allclose ( a = temp_grid , b = expected_grid )
65
72
66
73
67
- def test_grdcut_dataarray_in_dataarray_out (grid ):
74
+ def test_grdcut_dataarray_in_dataarray_out (grid , expected_grid ):
68
75
"""
69
76
grdcut an input DataArray, and output as DataArray.
70
77
"""
71
- outgrid = grdcut (grid , region = "0/180/0/90" )
78
+ outgrid = grdcut (grid , region = [ - 3 , 1 , 2 , 5 ] )
72
79
assert isinstance (outgrid , xr .DataArray )
73
- # check information of the output grid
74
- # the '@earth_relief_01d' is in pixel registration, so the grid range is
75
- # not exactly 0/180/0/90
76
- assert outgrid .coords ["lat" ].data .min () == 0.5
77
- assert outgrid .coords ["lat" ].data .max () == 89.5
78
- assert outgrid .coords ["lon" ].data .min () == 0.5
79
- assert outgrid .coords ["lon" ].data .max () == 179.5
80
- assert outgrid .data .min () == - 8182.0
81
- assert outgrid .data .max () == 5651.5
82
- assert outgrid .sizes ["lat" ] == 90
83
- assert outgrid .sizes ["lon" ] == 180
80
+ xr .testing .assert_allclose (a = outgrid , b = expected_grid )
84
81
85
82
86
83
def test_grdcut_fails ():
0 commit comments