|
| 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