GenericMappingTools / pygmt

A Python interface for the Generic Mapping Tools.
https://www.pygmt.org
BSD 3-Clause "New" or "Revised" License
758 stars 220 forks source link

Allow for overriding default gmt values (--PAR=value) in pygmt routines #455

Closed MarkWieczorek closed 4 years ago

MarkWieczorek commented 4 years ago

gmt routines allows to temporarily override a default value by adding the string --PAR=value to a gmt function call. For example, this is useful for quickly changing font sizes of labels and annotations.

Though pygmt routines pass kwargs that are reformatted by the functionbuild_arg_string, this does not correctly deal with overriding default parameters. In particular, build_arg_string only adds a single dash, and not two, to the output.

seisman commented 4 years ago

Instead of the --PAR=value, we already implement the similar feature in #293.

pygmt.config(PARAMETER=value)  # Set global settings

# Locally set a value that will revert back to the default
with pygmt.config(PROJ_ELLIPSOID=value):  # set local settings
    ...

pygmt.whatever(...)
MarkWieczorek commented 4 years ago

This will work. But it would help streamline things if you were allowed to temporarily override a parameter in a pygmt function. Otherwise, you need to set the variables before the call, and then unset them afterwards. If you don't plan on implementing this, just feel free to close this issue.

seisman commented 4 years ago

Otherwise, you need to set the variables before the call, and then unset them afterwards.

No. I have to confess that the pygmt.config() "function" is not well documented. Here is an example showing how pygmt.config() works. The settings in the with block are temporary and are automatically "unset" afterward.

import pygmt

fig = pygmt.Figure()
pygmt.config(FONT_ANNOT_PRIMARY='blue')
fig.basemap(
    region="0/10/0/10", projection="X5c", frame=["af", '+t"Blue Annotation"']
)

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

fig.basemap(
    region="0/10/0/10",
    projection="X5c",
    frame=["af", '+t"Blue Annotation"'],
    X="8c",
)
fig.savefig("config.png")

This is what the figure looks like: image

MarkWieczorek commented 4 years ago

I didn't realize that you could use config as a context manager. If we could highlight this in a tutorial and/or documentation, then there's no reason to do what I originally asked.