Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
weiji14 committed May 14, 2025
commit 7fa7d79e5569c14dea39660fc9ea683f9f45712c
37 changes: 24 additions & 13 deletions pygmt/tests/test_xarray_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,13 @@ def test_xarray_accessor_sliced_datacube():
Path(fname).unlink()


def test_xarray_accessor_grid_source_file_not_exist():
def test_xarray_accessor_tiled_grid_slice_and_add():
"""
Check that the accessor fallbacks to the default registration and gtype when the
grid source file (i.e., grid.encoding["source"]) doesn't exist.
Check that the accessor works to get the registration and gtype when the grid source
file is from a tiled grid, that slicing doesn't affect registration/gtype, but math
operations do return the default registration/gtype as a fallback.

Unit test to track https://github.com/GenericMappingTools/pygmt/issues/524
"""
# Load the 05m earth relief grid, which is stored as tiles.
grid = load_earth_relief(
Expand All @@ -144,17 +147,25 @@ def test_xarray_accessor_grid_source_file_not_exist():
# Registration and gtype are correct.
assert grid.gmt.registration is GridRegistration.PIXEL
assert grid.gmt.gtype is GridType.GEOGRAPHIC
# The source grid file is undefined.
assert grid.encoding.get("source") is None
# The source grid file for tiled grids is the first tile
assert grid.encoding["source"].endswith("S90E000.earth_relief_05m_p.nc")

# For a sliced grid, fallback to default registration and gtype, because the source
# grid file doesn't exist.
# For a sliced grid, ensure we don't fallback to the default registration (gridline)
# and gtype (cartesian), because the source grid file should still exist.
sliced_grid = grid[1:3, 1:3]
assert sliced_grid.gmt.registration is GridRegistration.GRIDLINE
assert sliced_grid.gmt.gtype is GridType.CARTESIAN

# Still possible to manually set registration and gtype.
sliced_grid.gmt.registration = GridRegistration.PIXEL
sliced_grid.gmt.gtype = GridType.GEOGRAPHIC
assert sliced_grid.encoding["source"].endswith("S90E000.earth_relief_05m_p.nc")
assert sliced_grid.gmt.registration is GridRegistration.PIXEL
assert sliced_grid.gmt.gtype is GridType.GEOGRAPHIC

# For a grid that underwent mathematical operations, fallback to default
# registration and gtype, because the source grid file doesn't exist.
added_grid = sliced_grid + 9
assert added_grid.encoding == {}
assert added_grid.gmt.registration is GridRegistration.GRIDLINE
assert added_grid.gmt.gtype is GridType.CARTESIAN

# Still possible to manually set registration and gtype.
added_grid.gmt.registration = GridRegistration.PIXEL
added_grid.gmt.gtype = GridType.GEOGRAPHIC
assert added_grid.gmt.registration is GridRegistration.PIXEL
assert added_grid.gmt.gtype is GridType.GEOGRAPHIC