-
Notifications
You must be signed in to change notification settings - Fork 234
clib.Session: Wrap the GMT_Get_Common API function #2500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3363744
clib.Session: Wrap the GMT_Get_Common API function
seisman 7048cde
Improve test coverage
seisman 537a67d
Minor fixes
seisman 49b825c
Fix the broken doctests
seisman 89e1ae8
Merge branch 'main' into wrap-gmt-get-common
seisman 41295c2
Fix docstrings for -b, -f and -:
seisman 9fb950d
Add get_common to API docs
seisman a894e20
Update pygmt/clib/session.py
seisman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -494,6 +494,97 @@ def get_default(self, name): | |
|
|
||
| return value.value.decode() | ||
|
|
||
| def get_common(self, option): | ||
| """ | ||
| Inquire if a GMT common option has been set and return its current | ||
| value if possible. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| option : str | ||
| The GMT common option to check. Valid options are ``"B"``, ``"I"``, | ||
| ``"J"``, ``"R"``, ``"U"``, ``"V"``, ``"X"``, ``"Y"``, ``"a"``, | ||
| ``"b"``, ``"f"``, ``"g"``, ``"h"``, ``"i"``, ``"n"``, ``"o"``, | ||
| ``"p"``, ``"r"``, ``"s"``, ``"t"``, and ``":"``. | ||
|
|
||
| Returns | ||
| ------- | ||
| value : bool, int, float, or numpy.ndarray | ||
| Whether the option was set or its value. | ||
|
|
||
| If the option was not set, return ``False``. Otherwise, | ||
| the return value depends on the choice of the option. | ||
|
|
||
| - options ``"B"``, ``"J"``, ``"U"``, ``"g"``, ``"n"``, ``"p"``, | ||
| and ``"s"``: return ``True`` if set, else ``False`` (bool) | ||
| - ``"I"``: 2-element array for the increments (float) | ||
| - ``"R"``: 4-element array for the region (float) | ||
| - ``"V"``: the verbose level (int) | ||
| - ``"X"``: the xshift (float) | ||
| - ``"Y"``: the yshift (float) | ||
| - ``"a"``: geometry of the dataset (int) | ||
| - ``"b"``: return 0 if `-bi` was set and 1 if `-bo` was set (int) | ||
| - ``"f"``: return 0 if `-fi` was set and 1 if `-fo` was set (int) | ||
| - ``"h"``: whether to delete existing header records (int) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at https://docs.generic-mapping-tools.org/6.4/gmt.html#h-full, is this just checking whether
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. |
||
| - ``"i"``: number of input columns (int) | ||
| - ``"o"``: number of output columns (int) | ||
| - ``"r"``: registration type (int) | ||
| - ``"t"``: 2-element array for the transparency (float) | ||
| - ``":"``: return 0 if `-:i` was set and 1 if `-:o` was set (int) | ||
|
|
||
| Examples | ||
| -------- | ||
| >>> with Session() as lib: | ||
| ... lib.call_module("basemap", "-R0/10/10/15 -JX5i/2.5i -Baf -Ve") | ||
| ... region = lib.get_common("R") | ||
| ... projection = lib.get_common("J") | ||
| ... timestamp = lib.get_common("U") | ||
| ... verbose = lib.get_common("V") | ||
| ... lib.call_module("plot", "-T -Xw+1i -Yh-1i") | ||
| ... xshift = lib.get_common("X") # xshift/yshift are in inches | ||
weiji14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ... yshift = lib.get_common("Y") | ||
| ... | ||
| >>> print(region, projection, timestamp, verbose, xshift, yshift) | ||
| [ 0. 10. 10. 15.] True False 3 6.0 1.5 | ||
| >>> with Session() as lib: | ||
| ... lib.call_module("basemap", "-R0/10/10/15 -JX5i/2.5i -Baf") | ||
| ... lib.get_common("A") | ||
| ... | ||
| Traceback (most recent call last): | ||
| ... | ||
| pygmt.exceptions.GMTInvalidInput: Unknown GMT common option flag 'A'. | ||
| """ | ||
| if option not in "BIJRUVXYabfghinoprst:": | ||
| raise GMTInvalidInput(f"Unknown GMT common option flag '{option}'.") | ||
|
|
||
| c_get_common = self.get_libgmt_func( | ||
| "GMT_Get_Common", | ||
| argtypes=[ctp.c_void_p, ctp.c_uint, ctp.POINTER(ctp.c_double)], | ||
| restype=ctp.c_int, | ||
| ) | ||
| value = np.empty(6) # numpy array to store the value of the option | ||
| status = c_get_common( | ||
| self.session_pointer, | ||
| ord(option), | ||
| value.ctypes.data_as(ctp.POINTER(ctp.c_double)), | ||
| ) | ||
|
|
||
| # GMT_NOTSET (-1) means the option is not set | ||
| if status == self["GMT_NOTSET"]: | ||
| return False | ||
| # option is set and no other value is returned | ||
| if status == 0: | ||
| return True | ||
| # option is set and option values (in double type) are returned via the | ||
| # 'value' array. 'status' is number of valid values in the array. | ||
| if option in "IRt": | ||
| return value[:status] | ||
| if option in "XY": # only one valid element in the array | ||
| return value[0] | ||
| # option is set and the option value (in integer type) is returned via | ||
| # the function return value (i.e., 'status') | ||
| return status | ||
|
|
||
| def call_module(self, module, args): | ||
| """ | ||
| Call a GMT module with the given arguments. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.