Skip to content

Store first tile as source encoding for tiled grids #3950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 5 additions & 4 deletions pygmt/datasets/load_remote_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,11 +592,12 @@ def _load_remote_dataset(
kind=dataset.kind, outgrid=None, vfname=voutgrd
)

# Full path to the grid if not tiled grids.
source = which(fname, download="a") if not resinfo.tiled else None
# Full path to the grid
source: str | list = which(fname, verbose="q")
if resinfo.tiled:
source = sorted(source)[0] # get first grid for tiled grids
# Manually add source to xarray.DataArray encoding to make the GMT accessors work.
if source:
grid.encoding["source"] = source
grid.encoding["source"] = source

# Add some metadata to the grid
grid.attrs["description"] = dataset.description
Expand Down
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
Loading