9
9
@xr .register_dataarray_accessor ("gmt" )
10
10
class GMTDataArrayAccessor :
11
11
"""
12
- This is the GMT extension for :class:`xarray.DataArray`.
12
+ GMT extension for :class:`xarray.DataArray`.
13
13
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:
15
21
16
22
>>> from pygmt.datasets import load_earth_relief
17
23
>>> # Use the global Earth relief grid with 1 degree spacing
18
24
>>> grid = load_earth_relief(resolution="01d", registration="pixel")
19
-
20
25
>>> # See if grid uses Gridline (0) or Pixel (1) registration
21
26
>>> grid.gmt.registration
22
27
1
23
28
>>> # See if grid uses Cartesian (0) or Geographic (1) coordinate system
24
29
>>> grid.gmt.gtype
25
30
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
26
49
"""
27
50
28
51
def __init__ (self , xarray_obj ):
@@ -51,8 +74,8 @@ def registration(self, value):
51
74
self ._registration = value
52
75
else :
53
76
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. "
56
79
)
57
80
58
81
@property
@@ -69,6 +92,6 @@ def gtype(self, value):
69
92
self ._gtype = value
70
93
else :
71
94
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. "
74
97
)
0 commit comments