Skip to content

Commit 733ef1b

Browse files
committed
Merge branch 'main' into try-micromamba
2 parents bd4037f + a840d9a commit 733ef1b

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

doc/api/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ Low level access (these are mostly used by the :mod:`pygmt.clib` package):
302302
clib.Session.__enter__
303303
clib.Session.__exit__
304304
clib.Session.get_default
305+
clib.Session.get_common
305306
clib.Session.create_data
306307
clib.Session.put_matrix
307308
clib.Session.put_strings

pygmt/clib/session.py

+91
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,97 @@ def get_default(self, name):
494494

495495
return value.value.decode()
496496

497+
def get_common(self, option):
498+
"""
499+
Inquire if a GMT common option has been set and return its current
500+
value if possible.
501+
502+
Parameters
503+
----------
504+
option : str
505+
The GMT common option to check. Valid options are ``"B"``, ``"I"``,
506+
``"J"``, ``"R"``, ``"U"``, ``"V"``, ``"X"``, ``"Y"``, ``"a"``,
507+
``"b"``, ``"f"``, ``"g"``, ``"h"``, ``"i"``, ``"n"``, ``"o"``,
508+
``"p"``, ``"r"``, ``"s"``, ``"t"``, and ``":"``.
509+
510+
Returns
511+
-------
512+
value : bool, int, float, or numpy.ndarray
513+
Whether the option was set or its value.
514+
515+
If the option was not set, return ``False``. Otherwise,
516+
the return value depends on the choice of the option.
517+
518+
- options ``"B"``, ``"J"``, ``"U"``, ``"g"``, ``"n"``, ``"p"``,
519+
and ``"s"``: return ``True`` if set, else ``False`` (bool)
520+
- ``"I"``: 2-element array for the increments (float)
521+
- ``"R"``: 4-element array for the region (float)
522+
- ``"V"``: the verbose level (int)
523+
- ``"X"``: the xshift (float)
524+
- ``"Y"``: the yshift (float)
525+
- ``"a"``: geometry of the dataset (int)
526+
- ``"b"``: return 0 if `-bi` was set and 1 if `-bo` was set (int)
527+
- ``"f"``: return 0 if `-fi` was set and 1 if `-fo` was set (int)
528+
- ``"h"``: whether to delete existing header records (int)
529+
- ``"i"``: number of input columns (int)
530+
- ``"o"``: number of output columns (int)
531+
- ``"r"``: registration type (int)
532+
- ``"t"``: 2-element array for the transparency (float)
533+
- ``":"``: return 0 if `-:i` was set and 1 if `-:o` was set (int)
534+
535+
Examples
536+
--------
537+
>>> with Session() as lib:
538+
... lib.call_module("basemap", "-R0/10/10/15 -JX5i/2.5i -Baf -Ve")
539+
... region = lib.get_common("R")
540+
... projection = lib.get_common("J")
541+
... timestamp = lib.get_common("U")
542+
... verbose = lib.get_common("V")
543+
... lib.call_module("plot", "-T -Xw+1i -Yh-1i")
544+
... xshift = lib.get_common("X") # xshift/yshift are in inches
545+
... yshift = lib.get_common("Y")
546+
...
547+
>>> print(region, projection, timestamp, verbose, xshift, yshift)
548+
[ 0. 10. 10. 15.] True False 3 6.0 1.5
549+
>>> with Session() as lib:
550+
... lib.call_module("basemap", "-R0/10/10/15 -JX5i/2.5i -Baf")
551+
... lib.get_common("A")
552+
...
553+
Traceback (most recent call last):
554+
...
555+
pygmt.exceptions.GMTInvalidInput: Unknown GMT common option flag 'A'.
556+
"""
557+
if option not in "BIJRUVXYabfghinoprst:":
558+
raise GMTInvalidInput(f"Unknown GMT common option flag '{option}'.")
559+
560+
c_get_common = self.get_libgmt_func(
561+
"GMT_Get_Common",
562+
argtypes=[ctp.c_void_p, ctp.c_uint, ctp.POINTER(ctp.c_double)],
563+
restype=ctp.c_int,
564+
)
565+
value = np.empty(6) # numpy array to store the value of the option
566+
status = c_get_common(
567+
self.session_pointer,
568+
ord(option),
569+
value.ctypes.data_as(ctp.POINTER(ctp.c_double)),
570+
)
571+
572+
# GMT_NOTSET (-1) means the option is not set
573+
if status == self["GMT_NOTSET"]:
574+
return False
575+
# option is set and no other value is returned
576+
if status == 0:
577+
return True
578+
# option is set and option values (in double type) are returned via the
579+
# 'value' array. 'status' is number of valid values in the array.
580+
if option in "IRt":
581+
return value[:status]
582+
if option in "XY": # only one valid element in the array
583+
return value[0]
584+
# option is set and the option value (in integer type) is returned via
585+
# the function return value (i.e., 'status')
586+
return status
587+
497588
def call_module(self, module, args):
498589
"""
499590
Call a GMT module with the given arguments.

0 commit comments

Comments
 (0)