From 58dc53f7fc56061d60d59ec58fca13a46fd275f4 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Wed, 14 May 2025 15:49:55 +1200 Subject: [PATCH 1/7] Store first tile as source encoding for tiled grids So that GMT accessor info works with tiled grids too. Adapted from https://github.com/GenericMappingTools/pygmt/pull/3932. --- pygmt/datasets/load_remote_dataset.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pygmt/datasets/load_remote_dataset.py b/pygmt/datasets/load_remote_dataset.py index c6f0471b8ee..266b9a8e177 100644 --- a/pygmt/datasets/load_remote_dataset.py +++ b/pygmt/datasets/load_remote_dataset.py @@ -592,8 +592,10 @@ 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, download="a") + if resinfo.tiled: + source = 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 From 7fa7d79e5569c14dea39660fc9ea683f9f45712c Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Wed, 14 May 2025 15:53:54 +1200 Subject: [PATCH 2/7] 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. --- pygmt/tests/test_xarray_accessor.py | 37 +++++++++++++++++++---------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/pygmt/tests/test_xarray_accessor.py b/pygmt/tests/test_xarray_accessor.py index 11b184110f6..2c23a11e44b 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 From bca2016dd4ab8dd5a994e6484458492ff31d5df3 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Wed, 14 May 2025 15:57:26 +1200 Subject: [PATCH 3/7] Sort list of tiled grids alphabetically to be deterministic Resolves inconsistency between CI and local seen at 11436addfa621adc7494032058072eae450eb56e --- pygmt/datasets/load_remote_dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/datasets/load_remote_dataset.py b/pygmt/datasets/load_remote_dataset.py index 266b9a8e177..690c46cea6c 100644 --- a/pygmt/datasets/load_remote_dataset.py +++ b/pygmt/datasets/load_remote_dataset.py @@ -595,7 +595,7 @@ def _load_remote_dataset( # Full path to the grid source: str | list = which(fname, download="a") if resinfo.tiled: - source = source[0] # get first grid for tiled grids + 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 From 6dfa3e4e4f70d4ab59e1e786e655333327f64fc3 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Wed, 14 May 2025 17:05:31 +1200 Subject: [PATCH 4/7] Silence Error: h [ERROR]: Tile @*.earth_*.nc not found errors --- pygmt/datasets/load_remote_dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/datasets/load_remote_dataset.py b/pygmt/datasets/load_remote_dataset.py index 690c46cea6c..04ca23daec1 100644 --- a/pygmt/datasets/load_remote_dataset.py +++ b/pygmt/datasets/load_remote_dataset.py @@ -593,7 +593,7 @@ def _load_remote_dataset( ) # Full path to the grid - source: str | list = which(fname, download="a") + source: str | list = which(fname, download="a", 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. From 524b3ee70af35764e413468ca6f098f963ca8ebb Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Wed, 14 May 2025 17:06:08 +1200 Subject: [PATCH 5/7] Remove `if source:` since source should always be available now --- pygmt/datasets/load_remote_dataset.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pygmt/datasets/load_remote_dataset.py b/pygmt/datasets/load_remote_dataset.py index 04ca23daec1..ff3cb517a50 100644 --- a/pygmt/datasets/load_remote_dataset.py +++ b/pygmt/datasets/load_remote_dataset.py @@ -597,8 +597,7 @@ def _load_remote_dataset( 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 From c59ab531eff001241e69c4065fadb0b15dcca137 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Wed, 14 May 2025 17:29:45 +1200 Subject: [PATCH 6/7] Update pygmt/datasets/load_remote_dataset.py Co-authored-by: Dongdong Tian --- pygmt/datasets/load_remote_dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/datasets/load_remote_dataset.py b/pygmt/datasets/load_remote_dataset.py index ff3cb517a50..e0a97a9ea73 100644 --- a/pygmt/datasets/load_remote_dataset.py +++ b/pygmt/datasets/load_remote_dataset.py @@ -593,7 +593,7 @@ def _load_remote_dataset( ) # Full path to the grid - source: str | list = which(fname, download="a", verbose="q") + 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. From d42c972ef86c48c56a7dd937ee97e7d3d97eeda2 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Wed, 14 May 2025 18:11:53 +1200 Subject: [PATCH 7/7] Update pygmt/tests/test_xarray_accessor.py Co-authored-by: Dongdong Tian --- pygmt/tests/test_xarray_accessor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/tests/test_xarray_accessor.py b/pygmt/tests/test_xarray_accessor.py index 2c23a11e44b..5422f6defcb 100644 --- a/pygmt/tests/test_xarray_accessor.py +++ b/pygmt/tests/test_xarray_accessor.py @@ -157,7 +157,7 @@ def test_xarray_accessor_tiled_grid_slice_and_add(): 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 + # 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 == {}