Skip to content

Commit cde9756

Browse files
committed
GMTDataArrayAccessor: Improve the documentation
1 parent 6496ccb commit cde9756

File tree

1 file changed

+71
-45
lines changed

1 file changed

+71
-45
lines changed

pygmt/xarray/accessor.py

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,33 @@ class GMTDataArrayAccessor:
1717
GMT accessor for :class:`xarray.DataArray`.
1818
1919
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:
2222
2323
- ``registration``: Grid registration type :class:`pygmt.enums.GridRegistration`.
2424
- ``gtype``: Grid coordinate system type :class:`pygmt.enums.GridType`.
2525
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+
2642
Examples
2743
--------
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:
3047
3148
>>> from pygmt.datasets import load_earth_relief
3249
>>> # Use the global Earth relief grid with 1 degree spacing
@@ -37,11 +54,54 @@ class GMTDataArrayAccessor:
3754
>>> # See if grid is in Cartesian or Geographic coordinate system
3855
>>> grid.gmt.gtype
3956
<GridType.GEOGRAPHIC: 1>
57+
>>> grid.encoding["source"] is not None
58+
True
59+
60+
Inplace assignment operators like ``*=`` don't create new instances, so the
61+
properties are still kept:
62+
63+
>>> grid *= 2.0
64+
>>> grid.gmt.registration
65+
<GridRegistration.PIXEL: 1>
66+
>>> grid.gmt.gtype
67+
<GridType.GEOGRAPHIC: 1>
68+
69+
Slice operation creates a new instance, but the source encoding is kept, so the
70+
properties are still kept:
71+
72+
>>> grid_slice = grid[0:30, 50:80]
73+
>>> # grid source encoding is kept in slice operation
74+
>>> grid_slice.encoding["source"] is not None
75+
True
76+
>>> # properties are still kept
77+
>>> grid_slice.gmt.registration
78+
<GridRegistration.PIXEL: 1>
79+
>>> grid_slice.gmt.gtype
80+
<GridType.GEOGRAPHIC: 1>
4081
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:
82+
Other grid operations (e.g., arithmetic operations) create new instances and drops
83+
the source encoding, so the properties will be reset to the default values:
84+
85+
>>> grid2 = grid * 2.0
86+
>>> # grid source encoding is dropped in arithmetic operation.
87+
>>> "source" in grid2.encoding
88+
False
89+
>>> # properties are reset to the default values
90+
>>> grid2.gmt.registration
91+
<GridRegistration.GRIDLINE: 0>
92+
>>> grid2.gmt.gtype
93+
<GridType.CARTESIAN: 0>
94+
>>> # Need to set these properties before passing the grid to PyGMT
95+
>>> grid2.gmt.registration = grid.gmt.registration
96+
>>> grid2.gmt.gtype = grid.gmt.gtype
97+
>>> grid2.gmt.registration
98+
<GridRegistration.PIXEL: 1>
99+
>>> grid2.gmt.gtype
100+
<GridType.GEOGRAPHIC: 1>
101+
102+
For :class:`xarray.DataArray` grids created from scratch, the source encoding is not
103+
available, so the properties will be set to the default values, and you need to
104+
manually set the correct properties before passing it to PyGMT functions:
45105
46106
>>> import numpy as np
47107
>>> import xarray as xr
@@ -67,44 +127,10 @@ class GMTDataArrayAccessor:
67127
>>> grid.gmt.gtype
68128
<GridType.GEOGRAPHIC: 1>
69129
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-
105130
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:
131+
new instances, so these properties are always lost if the source encoding is not
132+
available. The workaround is to assign the :class:`xarray.DataArray` into a
133+
variable:
108134
109135
>>> ds = xr.Dataset({"zval": grid})
110136
>>> ds.zval.gmt.registration

0 commit comments

Comments
 (0)