Skip to content

Commit 170a15d

Browse files
committed
Use enums GridReg and GridType in test_accessor.py
1 parent f2e3439 commit 170a15d

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

Diff for: pygmt/tests/test_accessor.py

+23-22
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pygmt import which
1212
from pygmt.clib import __gmt_version__
1313
from pygmt.datasets import load_earth_relief
14+
from pygmt.enums import GridReg, GridType
1415
from pygmt.exceptions import GMTInvalidInput
1516

1617

@@ -21,8 +22,8 @@ def test_accessor_gridline_cartesian():
2122
"""
2223
fname = which(fname="@test.dat.nc", download="a")
2324
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
2627

2728

2829
def test_accessor_pixel_geographic():
@@ -32,18 +33,18 @@ def test_accessor_pixel_geographic():
3233
"""
3334
fname = which(fname="@earth_relief_01d_p", download="a")
3435
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
3738

3839

3940
def test_accessor_set_pixel_registration():
4041
"""
4142
Check that we can set a grid to be Pixel registered with a registration value of 1.
4243
"""
4344
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
4748

4849

4950
@pytest.mark.benchmark
@@ -53,11 +54,11 @@ def test_accessor_set_geographic_cartesian_roundtrip():
5354
using a gtype of 1, set it to Geographic 0, and then back to Cartesian again 1.
5455
"""
5556
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
6162

6263

6364
def test_accessor_set_non_boolean():
@@ -93,8 +94,8 @@ def test_accessor_sliced_datacube():
9394
with xr.open_dataset(fname) as dataset:
9495
grid = dataset.sel(level=500, month=1, drop=True).z
9596

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
9899
finally:
99100
Path(fname).unlink()
100101

@@ -109,19 +110,19 @@ def test_accessor_grid_source_file_not_exist():
109110
resolution="05m", region=[0, 5, -5, 5], registration="pixel"
110111
)
111112
# 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
114115
# The source grid file is undefined.
115116
assert grid.encoding.get("source") is None
116117

117118
# For a sliced grid, fallback to default registration and gtype,
118119
# because the source grid file doesn't exist.
119120
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
122123

123124
# 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

Comments
 (0)