Skip to content

GMTDataArrayAccessor: Support applying grid operations on the current xarray.DataArray object #3854

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

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9c74046
Extend GMTDataAccessor to support grid operations
seisman Mar 15, 2025
fe7612c
Merge branch 'main' into accessor/gridop
seisman Mar 18, 2025
8b95908
Merge branch 'main' into accessor/gridop
seisman Mar 24, 2025
4853128
Add more grid operations to GMTDataArrayAccessor
seisman Mar 24, 2025
8d039aa
Merge branch 'main' into accessor/gridop
seisman Mar 28, 2025
505c9d0
Add docstrings for the new methods
seisman Mar 28, 2025
f49c3d0
Fix docstrings
seisman Mar 28, 2025
58ee6a1
Fix docstrings
seisman Mar 28, 2025
c43e6c6
Merge branch 'main' into accessor/gridop
seisman May 2, 2025
d2527ae
Merge branch 'main' into accessor/gridop
seisman May 4, 2025
49490d5
Update docstrings
seisman May 4, 2025
db42b7a
Add equalize_hist method
seisman May 4, 2025
de29785
Merge branch 'main' into accessor/gridop
seisman May 8, 2025
a341d39
Merge branch 'main' into accessor/gridop
seisman May 10, 2025
ebed146
Merge branch 'main' into accessor/gridop
seisman May 28, 2025
e7445ea
Merge branch 'main' into accessor/gridop
seisman Jun 4, 2025
48c84fe
Merge branch 'main' into accessor/gridop
seisman Jun 24, 2025
d602f71
Merge branch 'main' into accessor/gridop
seisman Jun 25, 2025
397504b
Use functools.wraps
seisman Jun 25, 2025
c3c362c
Add two tests
seisman Jun 25, 2025
3399025
Fix
seisman Jun 25, 2025
1b2aafb
Add a period
seisman Jun 25, 2025
e124192
Fix a typo
seisman Jun 25, 2025
98a322d
Merge branch 'main' into accessor/gridop
seisman Jul 2, 2025
e0ca40b
Merge branch 'main' into accessor/gridop
seisman Jul 13, 2025
d26b56d
Merge remote-tracking branch 'origin/accessor/gridop' into accessor/g…
seisman Jul 13, 2025
5a5fa16
Avoid two unused fixtures
seisman Jul 14, 2025
d6aa952
Merge branch 'main' into accessor/gridop
seisman Jul 15, 2025
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
50 changes: 50 additions & 0 deletions pygmt/tests/test_xarray_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@
from pygmt.datasets import load_earth_relief
from pygmt.enums import GridRegistration, GridType
from pygmt.exceptions import GMTValueError
from pygmt.helpers.testing import load_static_earth_relief

_HAS_NETCDF4 = bool(importlib.util.find_spec("netCDF4"))


@pytest.fixture(scope="module", name="grid")
def fixture_grid():
"""
Load the grid data from the sample earth_relief file.
"""
return load_static_earth_relief()


def test_xarray_accessor_gridline_cartesian():
"""
Check that the accessor returns the correct registration and gtype values for a
Expand Down Expand Up @@ -169,3 +178,44 @@ def test_xarray_accessor_tiled_grid_slice_and_add():
added_grid.gmt.gtype = GridType.GEOGRAPHIC
assert added_grid.gmt.registration is GridRegistration.PIXEL
assert added_grid.gmt.gtype is GridType.GEOGRAPHIC


def test_xarray_accessor_clip(grid):
"""
Check that the accessor has the clip method and that it works correctly.

This test is adapted from the `test_grdclip_no_outgrid` test.
"""
clipped_grid = grid.gmt.clip(
below=[550, -1000], above=[700, 1000], region=[-53, -49, -19, -16]
)

expected_clipped_grid = xr.DataArray(
data=[
[1000.0, 570.5, -1000.0, -1000.0],
[1000.0, 1000.0, 571.5, 638.5],
[555.5, 556.0, 580.0, 1000.0],
],
coords={"lon": [-52.5, -51.5, -50.5, -49.5], "lat": [-18.5, -17.5, -16.5]},
dims=["lat", "lon"],
)
xr.testing.assert_allclose(a=clipped_grid, b=expected_clipped_grid)


def test_xarray_accessor_equalize(grid):
"""
Check that the accessor has the equalize_hist method and that it works correctly.

This test is adapted from the `test_equalize_grid_no_outgrid` test.
"""
equalized_grid = grid.gmt.equalize_hist(divisions=2, region=[-52, -48, -22, -18])

expected_equalized_grid = xr.DataArray(
data=[[0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 1, 1], [1, 1, 1, 1]],
coords={
"lon": [-51.5, -50.5, -49.5, -48.5],
"lat": [-21.5, -20.5, -19.5, -18.5],
},
dims=["lat", "lon"],
)
xr.testing.assert_allclose(a=equalized_grid, b=expected_equalized_grid)
59 changes: 58 additions & 1 deletion pygmt/xarray/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@
"""

import contextlib
import functools
from pathlib import Path

import xarray as xr
from pygmt.enums import GridRegistration, GridType
from pygmt.exceptions import GMTValueError
from pygmt.src.grdinfo import grdinfo
from pygmt.src import (
dimfilter,
grdclip,
grdcut,
grdfill,
grdfilter,
grdgradient,
grdhisteq,
grdinfo,
grdproject,
grdsample,
grdtrack,
)


@xr.register_dataarray_accessor("gmt")
Expand All @@ -23,6 +36,11 @@ class GMTDataArrayAccessor:
- ``registration``: Grid registration type :class:`pygmt.enums.GridRegistration`.
- ``gtype``: Grid coordinate system type :class:`pygmt.enums.GridType`.

The *gmt* accessor also provides a set of grid-operation methods that enables
applying GMT's grid processing functionalities directly to the current
:class:`xarray.DataArray` object. See the summary table below for the list of
available methods.

Notes
-----
When accessed the first time, the *gmt* accessor will first be initialized to the
Expand Down Expand Up @@ -150,6 +168,19 @@ class GMTDataArrayAccessor:
>>> zval.gmt.gtype = GridType.GEOGRAPHIC
>>> zval.gmt.registration, zval.gmt.gtype
(<GridRegistration.GRIDLINE: 0>, <GridType.GEOGRAPHIC: 1>)

Instead of calling a grid-processing function and passing the
:class:`xarray.DataArray` object as an input, you can call the corresponding method
directly on the object. For example, the following two are equivalent:

>>> from pygmt.datasets import load_earth_relief
>>> grid = load_earth_relief(resolution="30m", region=[10, 30, 15, 25])
>>> # Create a new grid from an input grid. Set all values below 1,000 to 0 and all
>>> # values above 1,500 to 10,000.
>>> # Option 1:
>>> new_grid = pygmt.grdclip(grid=grid, below=[1000, 0], above=[1500, 10000])
>>> # Option 2:
>>> new_grid = grid.gmt.clip(below=[1000, 0], above=[1500, 10000])
"""

def __init__(self, xarray_obj: xr.DataArray):
Expand Down Expand Up @@ -200,3 +231,29 @@ def gtype(self, value: GridType | int):
value, description="grid coordinate system type", choices=GridType
)
self._gtype = GridType(value)

@staticmethod
def _make_method(func):
"""
Create a wrapper method for PyGMT grid-processing methods.

The :class:`xarray.DataArray` object is passed as the first argument.
"""

@functools.wraps(func)
def wrapper(self, *args, **kwargs):
return func(self._obj, *args, **kwargs)

return wrapper

# Accessor methods for grid operations.
clip = _make_method(grdclip)
cut = _make_method(grdcut)
dimfilter = _make_method(dimfilter)
equalize_hist = _make_method(grdhisteq.equalize_grid)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, I would prefer matching the original GMT grdhisteq name. But no strong opinion on this.

Suggested change
equalize_hist = _make_method(grdhisteq.equalize_grid)
histeq = _make_method(grdhisteq.equalize_grid)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistency vs. readability. That's a question.

The name equalize_hist comes from https://scikit-image.org/docs/stable/api/skimage.exposure.html#skimage.exposure.equalize_hist.

What do @GenericMappingTools/pygmt-maintainers think about the name? If we prefer readability, perhaps we also need to change dimfilter to directional_filter?

fill = _make_method(grdfill)
filter = _make_method(grdfilter)
gradient = _make_method(grdgradient)
project = _make_method(grdproject)
sample = _make_method(grdsample)
track = _make_method(grdtrack)
Loading