Skip to content

Commit 07f957e

Browse files
GMTDataArrayAccessor: Add inline examples for setting GMT specific properties (#2370)Co-authored-by: Michael Grund <[email protected]>
Co-authored-by: Michael Grund <[email protected]>
1 parent 2ecf8e4 commit 07f957e

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

pygmt/accessors.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,43 @@
99
@xr.register_dataarray_accessor("gmt")
1010
class GMTDataArrayAccessor:
1111
"""
12-
This is the GMT extension for :class:`xarray.DataArray`.
12+
GMT extension for :class:`xarray.DataArray`.
1313
14-
You can access various GMT specific metadata about your grid as follows:
14+
The extension provides easy ways to access and change the GMT specific
15+
properties about grids. Currently, two properties are available:
16+
17+
- ``registration``: Gridline (0) or Pixel (1) registration
18+
- ``gtype``: Cartesian (0) or Geographic (1) coordinate system
19+
20+
You can access these GMT specific properties about your grid as follows:
1521
1622
>>> from pygmt.datasets import load_earth_relief
1723
>>> # Use the global Earth relief grid with 1 degree spacing
1824
>>> grid = load_earth_relief(resolution="01d", registration="pixel")
19-
2025
>>> # See if grid uses Gridline (0) or Pixel (1) registration
2126
>>> grid.gmt.registration
2227
1
2328
>>> # See if grid uses Cartesian (0) or Geographic (1) coordinate system
2429
>>> grid.gmt.gtype
2530
1
31+
32+
You can also set the GMT specific properties for grids created by yourself:
33+
34+
>>> import numpy as np
35+
>>> import pygmt
36+
>>> import xarray as xr
37+
>>> # create a DataArray in gridline coordinates of sin(lon) * cos(lat)
38+
>>> interval = 2.5
39+
>>> lat = np.arange(90, -90 - interval, -interval)
40+
>>> lon = np.arange(0, 360 + interval, interval)
41+
>>> longrid, latgrid = np.meshgrid(lon, lat)
42+
>>> data = np.sin(np.deg2rad(longrid)) * np.cos(np.deg2rad(latgrid))
43+
>>> grid = xr.DataArray(
44+
... data, coords=[("latitude", lat), ("longitude", lon)]
45+
... )
46+
>>> # set it to a gridline-registered geographic grid
47+
>>> grid.gmt.registration = 0
48+
>>> grid.gmt.gtype = 1
2649
"""
2750

2851
def __init__(self, xarray_obj):
@@ -51,8 +74,8 @@ def registration(self, value):
5174
self._registration = value
5275
else:
5376
raise GMTInvalidInput(
54-
f"Invalid grid registration value: {value}, should be a boolean of "
55-
"either 0 for Gridline registration or 1 for Pixel registration"
77+
f"Invalid grid registration value: {value}, should be either "
78+
"0 for Gridline registration or 1 for Pixel registration."
5679
)
5780

5881
@property
@@ -69,6 +92,6 @@ def gtype(self, value):
6992
self._gtype = value
7093
else:
7194
raise GMTInvalidInput(
72-
f"Invalid coordinate system type: {value}, should be a boolean of "
73-
"either 0 for Cartesian or 1 for Geographic"
95+
f"Invalid coordinate system type: {value}, should be "
96+
"either 0 for Cartesian or 1 for Geographic."
7497
)

0 commit comments

Comments
 (0)