Skip to content

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

Closed

Description

@MarkWieczorek

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.

Activity

  1. seisman commented on May 24, 2020

    @seisman
    Member

    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(...)
    
  2. MarkWieczorek commented on May 25, 2020

    @MarkWieczorek
    ContributorAuthor

    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.

  3. seisman commented on May 25, 2020

    @seisman
    Member

    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

  4. MarkWieczorek commented on May 26, 2020

    @MarkWieczorek
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions