Skip to content

Commit 12f9776

Browse files
weiji14seisman
andauthored
GMTDataArrayAccessor: Store first tile as source encoding for tiled grids (#3950)
Co-authored-by: Dongdong Tian <[email protected]>
1 parent 36b9b11 commit 12f9776

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

pygmt/datasets/load_remote_dataset.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -592,11 +592,12 @@ def _load_remote_dataset(
592592
kind=dataset.kind, outgrid=None, vfname=voutgrd
593593
)
594594

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

601602
# Add some metadata to the grid
602603
grid.attrs["description"] = dataset.description

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.
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)