|
| 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 | + match clip_type: |
| 14 | + case "polygon": |
| 15 | + return "clip" |
| 16 | + case "land": |
| 17 | + return "coast" |
| 18 | + case "water": |
| 19 | + return "coast" |
| 20 | + |
| 21 | + |
| 22 | +class clip: # noqa: N801 |
| 23 | + """ |
| 24 | + Clip. |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self): |
| 28 | + self.type = None |
| 29 | + self.data = None |
| 30 | + self.args_enter = "" |
| 31 | + self.args_exit = "" |
| 32 | + |
| 33 | + def polygon(self, x, y): |
| 34 | + """ |
| 35 | + Clip the data to a polygon. |
| 36 | + """ |
| 37 | + self.type = "polygon" |
| 38 | + self.data = (x, y) |
| 39 | + self.args_enter = [] |
| 40 | + self.args_exit = ["-C"] |
| 41 | + return self |
| 42 | + |
| 43 | + def land(self): |
| 44 | + """ |
| 45 | + Clip the data to the land. |
| 46 | + """ |
| 47 | + self.type = "land" |
| 48 | + self.data = None |
| 49 | + self.args_enter = build_arg_list({"G": True}) |
| 50 | + self.args_exit = build_arg_list({"Q": True}) |
| 51 | + return self |
| 52 | + |
| 53 | + def water(self): |
| 54 | + """ |
| 55 | + Clip the data to the water. |
| 56 | + """ |
| 57 | + self.type = "water" |
| 58 | + self.data = None |
| 59 | + self.args_enter = build_arg_list({"S": True}) |
| 60 | + self.args_exit = build_arg_list({"Q": True}) |
| 61 | + return self |
| 62 | + |
| 63 | + def __enter__(self): |
| 64 | + """ |
| 65 | + Enter the context manager. |
| 66 | + """ |
| 67 | + module = _clip_method(self.type) |
| 68 | + |
| 69 | + with Session() as lib: |
| 70 | + if module == "clip": |
| 71 | + with lib.virtualfile_in(x=self.data[0], y=self.data[1]) as vintbl: |
| 72 | + lib.call_module(module, args=[vintbl]) |
| 73 | + else: |
| 74 | + lib.call_module(module, args=self.args_enter) |
| 75 | + return self |
| 76 | + |
| 77 | + def __exit__(self, exc_type, exc_value, traceback): |
| 78 | + """ |
| 79 | + Exit the context manager. |
| 80 | + """ |
| 81 | + module = _clip_method(self.type) |
| 82 | + with Session() as lib: |
| 83 | + lib.call_module(module, args=self.args_exit) |
0 commit comments