"""
config - set GMT defaults globally or locally.
"""
from pygmt.clib import Session
[docs]class config: # pylint: disable=invalid-name
"""
Set GMT defaults globally or locally.
Change GMT defaults globally::
pygmt.config(PARAMETER=value)
Change GMT defaults locally by using it as a context manager::
with pygmt.config(PARAMETER=value):
...
Full GMT defaults list at :gmt-docs:`gmt.conf.html`
"""
def __init__(self, **kwargs):
# Save values so that we can revert to their initial values
self.old_defaults = {}
self.special_params = {
"FONT": [
"FONT_ANNOT_PRIMARY",
"FONT_ANNOT_SECONDARY",
"FONT_HEADING",
"FONT_LABEL",
"FONT_TAG",
"FONT_TITLE",
],
"FONT_ANNOT": ["FONT_ANNOT_PRIMARY", "FONT_ANNOT_SECONDARY"],
"FORMAT_TIME_MAP": ["FORMAT_TIME_PRIMARY_MAP", "FORMAT_TIME_SECONDARY_MAP"],
"MAP_ANNOT_OFFSET": [
"MAP_ANNOT_OFFSET_PRIMARY",
"MAP_ANNOT_OFFSET_SECONDARY",
],
"MAP_GRID_CROSS_SIZE": [
"MAP_GRID_CROSS_SIZE_PRIMARY",
"MAP_GRID_CROSS_SIZE_SECONDARY",
],
"MAP_GRID_PEN": ["MAP_GRID_PEN_PRIMARY", "MAP_GRID_PEN_SECONDARY"],
"MAP_TICK_LENGTH": ["MAP_TICK_LENGTH_PRIMARY", "MAP_TICK_LENGTH_SECONDARY"],
"MAP_TICK_PEN": ["MAP_TICK_PEN_PRIMARY", "MAP_TICK_PEN_SECONDARY"],
}
with Session() as lib:
for key in kwargs:
if key in self.special_params:
for k in self.special_params[key]:
self.old_defaults[k] = lib.get_default(k)
else:
self.old_defaults[key] = lib.get_default(key)
# call gmt set to change GMT defaults
arg_str = " ".join([f"{key}={value}" for key, value in kwargs.items()])
with Session() as lib:
lib.call_module("set", arg_str)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
# revert to initial values
arg_str = " ".join(
[f"{key}={value}" for key, value in self.old_defaults.items()]
)
with Session() as lib:
lib.call_module("set", arg_str)