11
11
from pygmt import which
12
12
from pygmt .clib import __gmt_version__
13
13
from pygmt .datasets import load_earth_relief
14
+ from pygmt .enums import GridReg , GridType
14
15
from pygmt .exceptions import GMTInvalidInput
15
16
16
17
@@ -21,8 +22,8 @@ def test_accessor_gridline_cartesian():
21
22
"""
22
23
fname = which (fname = "@test.dat.nc" , download = "a" )
23
24
grid = xr .open_dataarray (fname )
24
- assert grid .gmt .registration == 0 # gridline registration
25
- assert grid .gmt .gtype == 0 # cartesian coordinate type
25
+ assert grid .gmt .registration == GridReg . GRIDLINE
26
+ assert grid .gmt .gtype == GridType . CARTESIAN
26
27
27
28
28
29
def test_accessor_pixel_geographic ():
@@ -32,18 +33,18 @@ def test_accessor_pixel_geographic():
32
33
"""
33
34
fname = which (fname = "@earth_relief_01d_p" , download = "a" )
34
35
grid = xr .open_dataarray (fname , engine = "netcdf4" )
35
- assert grid .gmt .registration == 1 # pixel registration
36
- assert grid .gmt .gtype == 1 # geographic coordinate type
36
+ assert grid .gmt .registration == GridReg . PIXEL
37
+ assert grid .gmt .gtype == GridType . GEOGRAPHIC
37
38
38
39
39
40
def test_accessor_set_pixel_registration ():
40
41
"""
41
42
Check that we can set a grid to be Pixel registered with a registration value of 1.
42
43
"""
43
44
grid = xr .DataArray (data = [[0.1 , 0.2 ], [0.3 , 0.4 ]])
44
- assert grid .gmt .registration == 0 # default to gridline registration
45
- grid .gmt .registration = 1 # set to pixel registration
46
- assert grid .gmt .registration == 1 # ensure changed to pixel registration
45
+ assert grid .gmt .registration == GridReg . GRIDLINE
46
+ grid .gmt .registration = GridReg . PIXEL
47
+ assert grid .gmt .registration == GridReg . PIXEL
47
48
48
49
49
50
@pytest .mark .benchmark
@@ -53,11 +54,11 @@ def test_accessor_set_geographic_cartesian_roundtrip():
53
54
using a gtype of 1, set it to Geographic 0, and then back to Cartesian again 1.
54
55
"""
55
56
grid = xr .DataArray (data = [[0.1 , 0.2 ], [0.3 , 0.4 ]])
56
- assert grid .gmt .gtype == 0 # default to cartesian coordinate type
57
- grid .gmt .gtype = 1 # set to geographic type
58
- assert grid .gmt .gtype == 1 # ensure changed to geographic coordinate type
59
- grid .gmt .gtype = 0 # set back to cartesian type
60
- assert grid .gmt .gtype == 0 # ensure changed to cartesian coordinate type
57
+ assert grid .gmt .gtype == GridType . CARTESIAN
58
+ grid .gmt .gtype = GridType . GEOGRAPHIC
59
+ assert grid .gmt .gtype == GridType . GEOGRAPHIC
60
+ grid .gmt .gtype = GridType . CARTESIAN
61
+ assert grid .gmt .gtype == GridType . CARTESIAN
61
62
62
63
63
64
def test_accessor_set_non_boolean ():
@@ -93,8 +94,8 @@ def test_accessor_sliced_datacube():
93
94
with xr .open_dataset (fname ) as dataset :
94
95
grid = dataset .sel (level = 500 , month = 1 , drop = True ).z
95
96
96
- assert grid .gmt .registration == 0 # gridline registration
97
- assert grid .gmt .gtype == 1 # geographic coordinate type
97
+ assert grid .gmt .registration == GridReg . GRIDLINE
98
+ assert grid .gmt .gtype == GridType . GEOGRAPHIC
98
99
finally :
99
100
Path (fname ).unlink ()
100
101
@@ -109,19 +110,19 @@ def test_accessor_grid_source_file_not_exist():
109
110
resolution = "05m" , region = [0 , 5 , - 5 , 5 ], registration = "pixel"
110
111
)
111
112
# Registration and gtype are correct
112
- assert grid .gmt .registration == 1
113
- assert grid .gmt .gtype == 1
113
+ assert grid .gmt .registration == GridReg . PIXEL
114
+ assert grid .gmt .gtype == GridType . GEOGRAPHIC
114
115
# The source grid file is undefined.
115
116
assert grid .encoding .get ("source" ) is None
116
117
117
118
# For a sliced grid, fallback to default registration and gtype,
118
119
# because the source grid file doesn't exist.
119
120
sliced_grid = grid [1 :3 , 1 :3 ]
120
- assert sliced_grid .gmt .registration == 0
121
- assert sliced_grid .gmt .gtype == 0
121
+ assert sliced_grid .gmt .registration == GridReg . GRIDLINE
122
+ assert sliced_grid .gmt .gtype == GridType . CARTESIAN
122
123
123
124
# Still possible to manually set registration and gtype
124
- sliced_grid .gmt .registration = 1
125
- sliced_grid .gmt .gtype = 1
126
- assert sliced_grid .gmt .registration == 1
127
- assert sliced_grid .gmt .gtype == 1
125
+ sliced_grid .gmt .registration = GridReg . PIXEL
126
+ sliced_grid .gmt .gtype = GridType . GEOGRAPHIC
127
+ assert sliced_grid .gmt .registration == GridReg . PIXEL
128
+ assert sliced_grid .gmt .gtype == GridType . GEOGRAPHIC
0 commit comments