6
6
import pandas as pd
7
7
import pytest
8
8
import xarray as xr
9
+ from numpy import testing as npt
9
10
from pygmt import surface , which
10
11
from pygmt .exceptions import GMTInvalidInput
11
12
from pygmt .helpers import GMTTempFile , data_kind
@@ -22,27 +23,41 @@ def fixture_fname():
22
23
@pytest .fixture (scope = "module" , name = "data" )
23
24
def fixture_data (fname ):
24
25
"""
25
- Load Table 5.11 in Davis: Statistics and Data Analysis in Geology, J.
26
- Wiley
26
+ Load Table 5.11 in Davis: Statistics and Data Analysis in Geology.
27
27
"""
28
28
return pd .read_csv (fname , sep = r"\s+" , header = None , names = ["x" , "y" , "z" ])
29
29
30
+
30
31
@pytest .fixture (scope = "module" , name = "region" )
31
32
def fixture_region ():
32
33
"""
33
34
Define the data region.
34
35
"""
35
- return [0 , 6.5 , - 0.2 , 6.5 ]
36
+ return [- 0.2 , 6.6 , - 0.2 , 6.2 ]
37
+
38
+
39
+ def check_values (grid ):
40
+ """
41
+ Check the attributes and values of the DataArray returned by surface.
42
+ """
43
+ assert isinstance (grid , xr .DataArray )
44
+ assert grid .gmt .registration == 0 # Gridline registration
45
+ assert grid .gmt .gtype == 0 # Cartesian type
46
+ assert grid .coords ["y" ].data .min () == - 0.2
47
+ assert grid .coords ["y" ].data .max () == 6.2
48
+ assert grid .coords ["x" ].data .min () == - 0.2
49
+ assert grid .coords ["x" ].data .max () == 6.6
50
+ npt .assert_allclose (grid [0 , 0 ].values , 1011.1221 )
51
+ assert grid .sizes ["y" ] == 33
52
+ assert grid .sizes ["x" ] == 35
36
53
37
54
38
55
def test_surface_input_file (fname , region ):
39
56
"""
40
57
Run surface by passing in a filename.
41
58
"""
42
59
output = surface (data = fname , spacing = "0.2" , region = region )
43
- assert isinstance (output , xr .DataArray )
44
- assert output .gmt .registration == 0 # Gridline registration
45
- assert output .gmt .gtype == 0 # Cartesian type
60
+ check_values (output )
46
61
47
62
48
63
def test_surface_input_data_array (data , region ):
@@ -51,7 +66,7 @@ def test_surface_input_data_array(data, region):
51
66
"""
52
67
data = data .values # convert pandas.DataFrame to numpy.ndarray
53
68
output = surface (data = data , spacing = "0.2" , region = region )
54
- assert isinstance (output , xr . DataArray )
69
+ check_values (output )
55
70
56
71
57
72
def test_surface_input_xyz (data , region ):
@@ -65,7 +80,7 @@ def test_surface_input_xyz(data, region):
65
80
spacing = "0.2" ,
66
81
region = region ,
67
82
)
68
- assert isinstance (output , xr . DataArray )
83
+ check_values (output )
69
84
70
85
71
86
def test_surface_wrong_kind_of_input (data , region ):
@@ -84,13 +99,11 @@ def test_surface_with_outgrid_param(data, region):
84
99
"""
85
100
data = data .values # convert pandas.DataFrame to numpy.ndarray
86
101
with GMTTempFile (suffix = ".nc" ) as tmpfile :
87
- output = surface (
88
- data = data , spacing = "0.2" , region = region , outgrid = tmpfile .name
89
- )
102
+ output = surface (data = data , spacing = "0.2" , region = region , outgrid = tmpfile .name )
90
103
assert output is None # check that output is None since outgrid is set
91
104
assert os .path .exists (path = tmpfile .name ) # check that outgrid exists at path
92
105
with xr .open_dataarray (tmpfile .name ) as grid :
93
- assert isinstance (grid , xr . DataArray ) # ensure netcdf grid loads ok
106
+ check_values (grid )
94
107
95
108
96
109
def test_surface_deprecate_outfile_to_outgrid (data , region ):
@@ -106,9 +119,8 @@ def test_surface_deprecate_outfile_to_outgrid(data, region):
106
119
)
107
120
assert output is None # check that output is None since outfile is set
108
121
assert os .path .exists (path = tmpfile .name ) # check that file exists at path
109
-
110
122
with xr .open_dataarray (tmpfile .name ) as grid :
111
- assert isinstance (grid , xr . DataArray ) # ensure netcdf grid loads ok
123
+ check_values (grid )
112
124
assert len (record ) == 1 # check that only one warning was raised
113
125
114
126
@@ -122,9 +134,7 @@ def test_surface_short_aliases(data, region):
122
134
with GMTTempFile (suffix = ".nc" ) as tmpfile :
123
135
output = surface (data = data , I = "0.2" , R = region , G = tmpfile .name )
124
136
assert output is None # check that output is None since outgrid is set
125
- assert os .path .exists (
126
- path = tmpfile .name
127
- ) # check that outgrid exists at path
137
+ assert os .path .exists (path = tmpfile .name ) # check that file exists at path
128
138
with xr .open_dataarray (tmpfile .name ) as grid :
129
- assert isinstance (grid , xr . DataArray ) # ensure netcdf grid loads ok
139
+ check_values (grid )
130
140
assert len (record ) == 3
0 commit comments