Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ Operations on grids:
grdinfo
grdtrack

GMT Defaults
------------

Operations on GMT defaults:

.. autosummary::
:toctree: generated

config

Miscellaneous
-------------
Expand Down
2 changes: 1 addition & 1 deletion pygmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from .gridding import surface
from .sampling import grdtrack
from .mathops import makecpt
from .modules import info, grdinfo, which
from .modules import config, info, grdinfo, which
from . import datasets


Expand Down
42 changes: 42 additions & 0 deletions pygmt/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,45 @@ def which(fname, **kwargs):
if not path:
raise FileNotFoundError("File '{}' not found.".format(fname))
return path


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 = {}
with Session() as lib:
for key in kwargs:
self.old_defaults[key] = lib.get_default(key)

# call gmt set to change GMT defaults
arg_str = " ".join(
["{}={}".format(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(
["{}={}".format(key, value) for key, value in self.old_defaults.items()]
)
with Session() as lib:
lib.call_module("set", arg_str)
Binary file added pygmt/tests/baseline/test_config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions pygmt/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
Tests for gmt config
"""
import pytest

from .. import Figure, config


@pytest.mark.mpl_image_compare
def test_config():
"""
Test if config works globally and locally.
"""
# Change global settings
config(FONT_ANNOT_PRIMARY="blue")
fig = Figure()
fig.basemap(
region="0/10/0/10", projection="X10c/10c", frame=["af", '+t"Blue Annotation"']
)

with config(FONT_LABEL="red", FONT_ANNOT_PRIMARY="red"):
fig.basemap(
region="0/10/0/10",
projection="X10c/10c",
frame=['xaf+l"red label"', "yaf", '+t"red annotation"'],
X="15c",
)

fig.basemap(
region="0/10/0/10",
projection="X10c/10c",
frame=["af", '+t"Blue Annotation"'],
X="15c",
)
# Revert to default settings
config(FONT_ANNOT_PRIMARY="black")
return fig