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