Skip to content

POC: Extend GMTDataArrayAccessor to support grid operations #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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

seisman
Copy link
Member

@seisman seisman commented Mar 15, 2025

Address #499 (comment).

This is a POC PR to show how GMTDataArrayAccessor can access GMT's grid-operation modules, i.e., grid.gmt.fill(**kwargs) instead of pygmt.grdfill(grid, **kwargs):

In [1]: import pygmt

In [2]: from pygmt.helpers.testing import load_static_earth_relief

In [3]: import numpy as np

In [4]: grid = load_static_earth_relief()

In [5]: grid[3:6, 3:5] = np.nan

In [6]: grid
Out[6]: 
<xarray.DataArray 'z' (lat: 14, lon: 8)> Size: 448B
array([[347.5, 344.5, 386. , 640.5, 617. , 579. , 646.5, 671. ],
       [383. , 284.5, 344.5, 394. , 491. , 556.5, 578.5, 618.5],
       [373. , 367.5, 349. , 352.5, 419.5, 428. , 570. , 667.5],
       [557. , 435. , 385.5,   nan,   nan, 496. , 519.5, 833.5],
       [561.5, 539. , 446.5,   nan,   nan, 553. , 726.5, 981. ],
       [310. , 521.5, 757. ,   nan,   nan, 524. , 686.5, 794. ],
       [521.5, 682.5, 796. , 886. , 571.5, 638.5, 739.5, 881.5],
       [308. , 595.5, 555.5, 556. , 580. , 770. , 927. , 920. ],
       [601. , 526.5, 535. , 299. , 398.5, 645. , 797.5, 964. ],
       [494.5, 488.5, 357. , 254.5, 286. , 484.5, 653.5, 930. ],
       [450.5, 395.5, 366. , 248. , 250. , 354.5, 550. , 797.5],
       [345.5, 320. , 335. , 292. , 207.5, 247. , 325. , 346.5],
       [349. , 313. , 325.5, 247. , 191. , 225. , 260. , 452.5],
       [347.5, 331.5, 309. , 282. , 190. , 208. , 299.5, 348. ]],
      dtype=float32)
Coordinates:
  * lon      (lon) float64 64B -54.5 -53.5 -52.5 -51.5 -50.5 -49.5 -48.5 -47.5
  * lat      (lat) float64 112B -23.5 -22.5 -21.5 -20.5 ... -12.5 -11.5 -10.5
Attributes:
    long_name:     elevation (m)
    actual_range:  [190. 981.]

In [7]: grid.gmt.fill(mode="c20")
Out[7]: 
<xarray.DataArray 'z' (lat: 14, lon: 8)> Size: 448B
array([[347.5, 344.5, 386. , 640.5, 617. , 579. , 646.5, 671. ],
       [383. , 284.5, 344.5, 394. , 491. , 556.5, 578.5, 618.5],
       [373. , 367.5, 349. , 352.5, 419.5, 428. , 570. , 667.5],
       [557. , 435. , 385.5,  20. ,  20. , 496. , 519.5, 833.5],
       [561.5, 539. , 446.5,  20. ,  20. , 553. , 726.5, 981. ],
       [310. , 521.5, 757. ,  20. ,  20. , 524. , 686.5, 794. ],
       [521.5, 682.5, 796. , 886. , 571.5, 638.5, 739.5, 881.5],
       [308. , 595.5, 555.5, 556. , 580. , 770. , 927. , 920. ],
       [601. , 526.5, 535. , 299. , 398.5, 645. , 797.5, 964. ],
       [494.5, 488.5, 357. , 254.5, 286. , 484.5, 653.5, 930. ],
       [450.5, 395.5, 366. , 248. , 250. , 354.5, 550. , 797.5],
       [345.5, 320. , 335. , 292. , 207.5, 247. , 325. , 346.5],
       [349. , 313. , 325.5, 247. , 191. , 225. , 260. , 452.5],
       [347.5, 331.5, 309. , 282. , 190. , 208. , 299.5, 348. ]],
      dtype=float32)
Coordinates:
  * lat      (lat) float64 112B -23.5 -22.5 -21.5 -20.5 ... -12.5 -11.5 -10.5
  * lon      (lon) float64 64B -54.5 -53.5 -52.5 -51.5 -50.5 -49.5 -48.5 -47.5
Attributes:
    Conventions:   CF-1.7
    title:         
    history:       gmt grdfill @GMTAPI@-S-I-G-M-G-N-000000 -Ac20 -G@GMTAPI@-S...
    description:   
    actual_range:  [190. 981.]
    long_name:     z

Comment on lines +199 to +205
def clip(self, **kwargs) -> xr.DataArray:
"""
Clip the range of grid values.

See the :func:`pygmt.grdclip` function for available parameters.
"""
return grdclip(grid=self._obj, **kwargs)
Copy link
Member

Choose a reason for hiding this comment

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

Probably best to keep the method name as .gmt.grdclip instead of .gmt.clip, because there is also https://docs.generic-mapping-tools.org/6.5/clip.html (same with the other grd* methods below), and who knows if we might use pandas accessors at some point.

Copy link
Member Author

Choose a reason for hiding this comment

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

Even if we decide to implement the pandas accessors in the future (actually, I feel it's a good idea), there are still no conflicts since the two accessors are in different namespaces. E.g.,

  • da.gmt.clip: Clip operation on a xr.DataArray grid/image
  • df.gmt.clip: Clip operation on a pd.DataFrame object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants