Skip to content

Commit 221b60c

Browse files
committed
Refactor test_xarray_accessor_grid_source_file_not_exist
Slicing a tiled grid retains the original source still, but doing math operations like addition cause source to be lost and fallback to default registration/gtype.
1 parent deb2df0 commit 221b60c

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

pygmt/tests/test_xarray_accessor.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,13 @@ def test_xarray_accessor_sliced_datacube():
132132
Path(fname).unlink()
133133

134134

135-
def test_xarray_accessor_grid_source_file_not_exist():
135+
def test_xarray_accessor_tiled_grid_slice_and_add():
136136
"""
137-
Check that the accessor fallbacks to the default registration and gtype when the
138-
grid source file (i.e., grid.encoding["source"]) doesn't exist.
137+
Check that the accessor works to get the registration and gtype when the grid source
138+
file is from a tiled grid, that slicing doesn't affect registration/gtype, but math
139+
operations do return the default registration/gtype as a fallback.
140+
141+
Unit test to track https://github.com/GenericMappingTools/pygmt/issues/524
139142
"""
140143
# Load the 05m earth relief grid, which is stored as tiles.
141144
grid = load_earth_relief(
@@ -144,17 +147,25 @@ def test_xarray_accessor_grid_source_file_not_exist():
144147
# Registration and gtype are correct.
145148
assert grid.gmt.registration is GridRegistration.PIXEL
146149
assert grid.gmt.gtype is GridType.GEOGRAPHIC
147-
# The source grid file is undefined for tiled grids.
148-
assert grid.encoding.get("source") is None
150+
# The source grid file for tiled grids is the first tile
151+
assert grid.encoding["source"].endswith("S90E000.earth_relief_05m_p.nc")
149152

150-
# For a sliced grid, fallback to default registration and gtype, because the source
151-
# grid file doesn't exist.
153+
# For a sliced grid, ensure we don't fallback to the default registration (gridline)
154+
# and gtype (cartesian), because the source grid file should still exist.
152155
sliced_grid = grid[1:3, 1:3]
153-
assert sliced_grid.gmt.registration is GridRegistration.GRIDLINE
154-
assert sliced_grid.gmt.gtype is GridType.CARTESIAN
155-
156-
# Still possible to manually set registration and gtype.
157-
sliced_grid.gmt.registration = GridRegistration.PIXEL
158-
sliced_grid.gmt.gtype = GridType.GEOGRAPHIC
156+
assert sliced_grid.encoding["source"].endswith("S90E000.earth_relief_05m_p.nc")
159157
assert sliced_grid.gmt.registration is GridRegistration.PIXEL
160158
assert sliced_grid.gmt.gtype is GridType.GEOGRAPHIC
159+
160+
# For a grid that underwent mathematical operations, fallback to default
161+
# registration and gtype, because the source grid file doesn't exist.
162+
added_grid = sliced_grid + 9
163+
assert added_grid.encoding == {}
164+
assert added_grid.gmt.registration is GridRegistration.GRIDLINE
165+
assert added_grid.gmt.gtype is GridType.CARTESIAN
166+
167+
# Still possible to manually set registration and gtype.
168+
added_grid.gmt.registration = GridRegistration.PIXEL
169+
added_grid.gmt.gtype = GridType.GEOGRAPHIC
170+
assert added_grid.gmt.registration is GridRegistration.PIXEL
171+
assert added_grid.gmt.gtype is GridType.GEOGRAPHIC

0 commit comments

Comments
 (0)