Skip to content

Commit 76ecd80

Browse files
committed
Figure.clip: Initial implementation
1 parent 2992d22 commit 76ecd80

File tree

5 files changed

+122
-2
lines changed

5 files changed

+122
-2
lines changed

doc/_templates/autosummary/class.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
.. rubric:: Attributes
99

1010
{% for item in attributes %}
11-
.. autoproperty::
12-
{{ objname }}.{{ item }}
11+
.. autoproperty:: {{ objname }}.{{ item }}
12+
:no-index:
1313
{% endfor %}
1414
{% endif %}
1515

doc/api/index.rst

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Plotting map elements
2525
:toctree: generated
2626

2727
Figure.basemap
28+
Figure.clip
2829
Figure.coast
2930
Figure.colorbar
3031
Figure.hlines
@@ -218,6 +219,7 @@ Miscellaneous
218219

219220
which
220221
show_versions
222+
src.ClipAccessor
221223

222224
Datasets
223225
--------

pygmt/figure.py

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import Literal, overload
1010

1111
from pygmt._typing import PathLike
12+
from pygmt.src import ClipAccessor
1213

1314
try:
1415
import IPython
@@ -137,6 +138,15 @@ def region(self) -> np.ndarray:
137138
wesn = lib.extract_region()
138139
return wesn
139140

141+
@property
142+
def clip(self) -> ClipAccessor:
143+
"""
144+
Set up a clipping path and only plot data inside/outside the clipped path.
145+
146+
See :class:`pygmt.src.clip.ClipAccessor <ClipAccessor>` for the usage.
147+
"""
148+
return ClipAccessor()
149+
140150
def savefig(
141151
self,
142152
fname: PathLike,

pygmt/src/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pygmt.src.basemap import basemap
66
from pygmt.src.binstats import binstats
77
from pygmt.src.blockm import blockmean, blockmedian, blockmode
8+
from pygmt.src.clip import ClipAccessor
89
from pygmt.src.coast import coast
910
from pygmt.src.colorbar import colorbar
1011
from pygmt.src.config import config

pygmt/src/clip.py

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""
2+
Clip.
3+
"""
4+
5+
from pygmt.clib import Session
6+
from pygmt.helpers import build_arg_list
7+
8+
9+
def _clip_method(clip_type):
10+
"""
11+
Return the clip method for the given clip type.
12+
"""
13+
return {
14+
"polygon": "clip",
15+
"solar": "solar",
16+
"mask": "mask",
17+
"land": "coast",
18+
"water": "coast",
19+
"dcw": "coast",
20+
}[clip_type]
21+
22+
23+
class ClipAccessor:
24+
"""
25+
Clip.
26+
"""
27+
28+
def __init__(self):
29+
self.type = None
30+
self.data = None
31+
self.args_enter = ""
32+
self.args_exit = ""
33+
34+
def land(self):
35+
"""
36+
Clip the data to the land.
37+
"""
38+
self.type = "land"
39+
self.data = None
40+
self.args_enter = build_arg_list({"G": True})
41+
self.args_exit = build_arg_list({"Q": True})
42+
return self
43+
44+
def water(self):
45+
"""
46+
Clip the data to the water.
47+
"""
48+
self.type = "water"
49+
self.data = None
50+
self.args_enter = build_arg_list({"S": True})
51+
self.args_exit = build_arg_list({"Q": True})
52+
return self
53+
54+
def dcw(self):
55+
"""
56+
Clip based on the Digital Chart of the World.
57+
"""
58+
raise NotImplementedError
59+
60+
def polygon(self, x, y):
61+
"""
62+
Clip the data to a polygon.
63+
64+
Parameters
65+
----------
66+
x/y
67+
Coordinates of polygon.
68+
"""
69+
self.type = "polygon"
70+
self.data = (x, y)
71+
self.args_enter = []
72+
self.args_exit = ["-C"]
73+
return self
74+
75+
def solar(self):
76+
"""
77+
Clip the data to the solar terminator.
78+
"""
79+
raise NotImplementedError
80+
81+
def mask(self):
82+
"""
83+
Clip the data to a mask.
84+
"""
85+
raise NotImplementedError
86+
87+
def __enter__(self):
88+
"""
89+
Enter the context manager.
90+
"""
91+
module = _clip_method(self.type)
92+
93+
with Session() as lib:
94+
if module == "clip":
95+
with lib.virtualfile_in(x=self.data[0], y=self.data[1]) as vintbl:
96+
lib.call_module(module, args=[vintbl])
97+
else:
98+
lib.call_module(module, args=self.args_enter)
99+
return self
100+
101+
def __exit__(self, exc_type, exc_value, traceback):
102+
"""
103+
Exit the context manager.
104+
"""
105+
module = _clip_method(self.type)
106+
with Session() as lib:
107+
lib.call_module(module, args=self.args_exit)

0 commit comments

Comments
 (0)