diff --git a/pygmt/datasets/load_remote_dataset.py b/pygmt/datasets/load_remote_dataset.py index c6f0471b8ee..e0a97a9ea73 100644 --- a/pygmt/datasets/load_remote_dataset.py +++ b/pygmt/datasets/load_remote_dataset.py @@ -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 diff --git a/pygmt/tests/test_xarray_accessor.py b/pygmt/tests/test_xarray_accessor.py index 11b184110f6..5422f6defcb 100644 --- a/pygmt/tests/test_xarray_accessor.py +++ b/pygmt/tests/test_xarray_accessor.py @@ -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( @@ -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