6
6
import numpy as np
7
7
import pytest
8
8
import xarray as xr
9
- from pygmt import grdinfo , xyz2grd
10
- from pygmt .datasets import load_sample_bathymetry
9
+ from pygmt import load_dataarray , xyz2grd
10
+ from pygmt .datasets import load_sample_data
11
11
from pygmt .helpers import GMTTempFile
12
12
13
13
@@ -16,54 +16,52 @@ def fixture_ship_data():
16
16
"""
17
17
Load the data from the sample bathymetry dataset.
18
18
"""
19
- return load_sample_bathymetry ( )
19
+ return load_sample_data ( name = "bathymetry" )
20
20
21
21
22
- def test_xyz2grd_input_file ():
22
+ @pytest .fixture (scope = "module" , name = "expected_grid" )
23
+ def fixture_grid_result ():
23
24
"""
24
- Run xyz2grd by passing in a filename .
25
+ Load the expected xyz2grd grid result .
25
26
"""
26
- output = xyz2grd (data = "@tut_ship.xyz" , spacing = 5 , region = [245 , 255 , 20 , 30 ])
27
- assert isinstance (output , xr .DataArray )
28
- assert output .gmt .registration == 0 # Gridline registration
29
- assert output .gmt .gtype == 0 # Cartesian type
30
- return output
31
-
32
-
33
- def test_xyz2grd_input_array (ship_data ):
34
- """
35
- Run xyz2grd by passing in a numpy array.
36
- """
37
- output = xyz2grd (data = np .array (ship_data ), spacing = 5 , region = [245 , 255 , 20 , 30 ])
38
- assert isinstance (output , xr .DataArray )
39
- assert output .gmt .registration == 0 # Gridline registration
40
- assert output .gmt .gtype == 0 # Cartesian type
41
- return output
27
+ return xr .DataArray (
28
+ data = [
29
+ [- 3651.0608 , - 3015.214 , - 2320.1033 ],
30
+ [- 2546.2512 , - 1977.8754 , - 963.23303 ],
31
+ [- 352.3795 , - 1025.4508 , np .nan ],
32
+ ],
33
+ coords = dict (
34
+ x = [245.0 , 250.0 , 255.0 ],
35
+ y = [20.0 , 25.0 , 30.0 ],
36
+ ),
37
+ dims = ["y" , "x" ],
38
+ )
42
39
43
40
44
- def test_xyz2grd_input_df (ship_data ):
41
+ @pytest .mark .parametrize ("array_func" , [np .array , xr .Dataset ])
42
+ def test_xyz2grd_input_array (array_func , ship_data , expected_grid ):
45
43
"""
46
- Run xyz2grd by passing in a data frame .
44
+ Run xyz2grd by passing in an xarray datset or numpy array .
47
45
"""
48
- output = xyz2grd (data = ship_data , spacing = 5 , region = [245 , 255 , 20 , 30 ])
46
+ output = xyz2grd (data = array_func ( ship_data ) , spacing = 5 , region = [245 , 255 , 20 , 30 ])
49
47
assert isinstance (output , xr .DataArray )
50
48
assert output .gmt .registration == 0 # Gridline registration
51
49
assert output .gmt .gtype == 0 # Cartesian type
52
- return output
50
+ xr . testing . assert_allclose ( a = output , b = expected_grid )
53
51
54
52
55
- def test_xyz2grd_input_array_file_out (ship_data ):
53
+ def test_xyz2grd_input_array_file_out (ship_data , expected_grid ):
56
54
"""
57
55
Run xyz2grd by passing in a numpy array and set an outgrid file.
58
56
"""
59
57
with GMTTempFile (suffix = ".nc" ) as tmpfile :
60
58
result = xyz2grd (
61
- data = np . array ( ship_data ) ,
59
+ data = ship_data ,
62
60
spacing = 5 ,
63
61
region = [245 , 255 , 20 , 30 ],
64
62
outgrid = tmpfile .name ,
65
63
)
66
64
assert result is None # return value is None
67
65
assert os .path .exists (path = tmpfile .name )
68
- result = grdinfo (tmpfile .name , per_column = True ). strip ( )
69
- assert result == "245 255 20 30 -3651.06079102 -352.379486084 5 5 3 3 0 0"
66
+ temp_grid = load_dataarray (tmpfile .name )
67
+ xr . testing . assert_allclose ( a = temp_grid , b = expected_grid )
0 commit comments