@@ -17,17 +17,35 @@ class GMTDataArrayAccessor:
17
17
GMT accessor for :class:`xarray.DataArray`.
18
18
19
19
The *gmt* accessor extends :class:`xarray.DataArray` to store GMT-specific
20
- properties for grids, which are important for PyGMT to correctly process and plot
21
- the grids . The *gmt* accessor contains the following properties:
20
+ properties for grids and images , which are important for PyGMT to correctly process
21
+ and plot them . The *gmt* accessor contains the following properties:
22
22
23
23
- ``registration``: Grid registration type :class:`pygmt.enums.GridRegistration`.
24
24
- ``gtype``: Grid coordinate system type :class:`pygmt.enums.GridType`.
25
25
26
+ Notes
27
+ -----
28
+ When accessed the first time, the GMT accessor will be first initialized to the
29
+ default values (``GridRegistration.GRIDLINE`` and ``GridType.CARTESIAN``, i.e., a
30
+ gridline-registered, Cartesian grid), and then the properties will be updated with
31
+ the correct grid registration and type determined from the source encoding (i.e.,
32
+ ``grid.encoding["source"]``), if it is available.
33
+
34
+ Due to the limitations of xarray accessors, the GMT accessor is created once per
35
+ :class:`xarray.DataArray` instance. Thus, the GMT accessor will be re-initialized in
36
+ cases where the :class:`xarray.DataArray` is manipulated (e.g., arithmetic and slice
37
+ operations) or when accessing a :class:`xarray.DataArray` from a
38
+ :class:`xarray.Dataset`. In these cases, the GMT-specific properties will result in
39
+ incorrect values if the source encoding is dropped or not found, and users need to
40
+ manually set these properties again.
41
+
26
42
Examples
27
43
--------
28
- For GMT's built-in remote datasets, these GMT-specific properties are automatically
29
- determined and you can access them as follows:
44
+ For grids loaded from a file (e.g., via :func:`xarray.load_dataarray`) and GMT's
45
+ built-in remote datasets, the GMT-specific properties are automatically determined
46
+ and you can access them as follows:
30
47
48
+ >>> from pathlib import Path
31
49
>>> from pygmt.datasets import load_earth_relief
32
50
>>> # Use the global Earth relief grid with 1 degree spacing
33
51
>>> grid = load_earth_relief(resolution="01d", registration="pixel")
@@ -37,11 +55,54 @@ class GMTDataArrayAccessor:
37
55
>>> # See if grid is in Cartesian or Geographic coordinate system
38
56
>>> grid.gmt.gtype
39
57
<GridType.GEOGRAPHIC: 1>
58
+ >>> grid.encoding["source"] is not None
59
+ True
60
+
61
+ Inplace assignment operators like ``*=`` don't create new instances, so the
62
+ properties are still kept:
63
+
64
+ >>> grid *= 2.0
65
+ >>> grid.gmt.registration
66
+ <GridRegistration.PIXEL: 1>
67
+ >>> grid.gmt.gtype
68
+ <GridType.GEOGRAPHIC: 1>
69
+
70
+ Slice operation creates a new instance, but the source encoding is kept, so the
71
+ properties are still kept:
72
+
73
+ >>> grid_slice = grid[0:30, 50:80]
74
+ >>> # grid source encoding is kept in slice operation
75
+ >>> grid_slice.encoding["source"] is not None
76
+ True
77
+ >>> # properties are still kept
78
+ >>> grid_slice.gmt.registration
79
+ <GridRegistration.PIXEL: 1>
80
+ >>> grid_slice.gmt.gtype
81
+ <GridType.GEOGRAPHIC: 1>
40
82
41
- For :class:`xarray.DataArray` grids created by yourself, ``registration`` and
42
- ``gtype`` default to ``GridRegistration.GRIDLINE`` and ``GridType.CARTESIAN`` (i.e.,
43
- a gridline-registered, Cartesian grid). You need to set the correct properties
44
- before passing it to PyGMT functions:
83
+ Other grid operations (e.g., arithmetic operations) create new instances and drops
84
+ the source encoding, so the properties will be reset to the default values:
85
+
86
+ >>> grid2 = grid * 2.0
87
+ >>> # grid source encoding is dropped in arithmetic operation.
88
+ >>> "source" in grid2.encoding
89
+ False
90
+ >>> # properties are reset to the default values
91
+ >>> grid2.gmt.registration
92
+ <GridRegistration.GRIDLINE: 0>
93
+ >>> grid2.gmt.gtype
94
+ <GridType.CARTESIAN: 0>
95
+ >>> # Need to set these properties before passing the grid to PyGMT
96
+ >>> grid2.gmt.registration = grid.gmt.registration
97
+ >>> grid2.gmt.gtype = grid.gmt.gtype
98
+ >>> grid2.gmt.registration
99
+ <GridRegistration.PIXEL: 1>
100
+ >>> grid2.gmt.gtype
101
+ <GridType.GEOGRAPHIC: 1>
102
+
103
+ For :class:`xarray.DataArray` grids created from scratch, the source encoding is not
104
+ available, so the properties will be set to the default values, and you need to
105
+ manually set the correct properties before passing it to PyGMT functions:
45
106
46
107
>>> import numpy as np
47
108
>>> import xarray as xr
@@ -67,44 +128,10 @@ class GMTDataArrayAccessor:
67
128
>>> grid.gmt.gtype
68
129
<GridType.GEOGRAPHIC: 1>
69
130
70
- Notes
71
- -----
72
- Due to the limitations of xarray accessors, the GMT accessors are created once per
73
- :class:`xarray.DataArray` instance. You may lose these GMT-specific properties when
74
- manipulating grids (e.g., arithmetic and slice operations) or when accessing a
75
- :class:`xarray.DataArray` from a :class:`xarray.Dataset`. In these cases, you need
76
- to manually set these properties before passing the grid to PyGMT.
77
-
78
- Inplace assignment operators like ``*=`` don't create new instances, so the
79
- properties are still kept:
80
-
81
- >>> grid *= 2.0
82
- >>> grid.gmt.registration
83
- <GridRegistration.GRIDLINE: 0>
84
- >>> grid.gmt.gtype
85
- <GridType.GEOGRAPHIC: 1>
86
-
87
- Other grid operations (e.g., arithmetic or slice operations) create new instances,
88
- so the properties will be lost:
89
-
90
- >>> # grid2 is a slice of the original grid
91
- >>> grid2 = grid[0:30, 50:80]
92
- >>> # Properties are reset to the default values for new instance
93
- >>> grid2.gmt.registration
94
- <GridRegistration.GRIDLINE: 0>
95
- >>> grid2.gmt.gtype
96
- <GridType.CARTESIAN: 0>
97
- >>> # Need to set these properties before passing the grid to PyGMT
98
- >>> grid2.gmt.registration = grid.gmt.registration
99
- >>> grid2.gmt.gtype = grid.gmt.gtype
100
- >>> grid2.gmt.registration
101
- <GridRegistration.GRIDLINE: 0>
102
- >>> grid2.gmt.gtype
103
- <GridType.GEOGRAPHIC: 1>
104
-
105
131
Accessing a :class:`xarray.DataArray` from a :class:`xarray.Dataset` always creates
106
- new instances, so these properties are always lost. The workaround is to assign the
107
- :class:`xarray.DataArray` into a variable:
132
+ new instances, so these properties are always lost if the source encoding is not
133
+ available. The workaround is to assign the :class:`xarray.DataArray` into a
134
+ variable:
108
135
109
136
>>> ds = xr.Dataset({"zval": grid})
110
137
>>> ds.zval.gmt.registration
0 commit comments