Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pygmt.config doesn't work with values that have whitespaces #2298

Closed
seisman opened this issue Jan 4, 2023 · 2 comments · Fixed by #2331
Closed

pygmt.config doesn't work with values that have whitespaces #2298

seisman opened this issue Jan 4, 2023 · 2 comments · Fixed by #2331
Labels
bug Something isn't working
Milestone

Comments

@seisman
Copy link
Member

seisman commented Jan 4, 2023

Description of the problem

Some GMT defaults can accept values that have whitespaces. For example, FORMAT_TIME_STAMP's default value is %Y %b %d %H:%M:%S.

See the example below. pygmt.config doesn't work with values that contain whitespaces. We need to do some work like #1487.

Minimal Complete Verifiable Example

import pygmt

fig = pygmt.Figure()
with pygmt.config(FORMAT_TIME_STAMP="%Y %b %d %H:%M:%S"):
    fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame=True, timestamp=True)
fig.show()

Full error message

gmtset [ERROR]: Unrecognized keyword %b.
gmtset [WARNING]: Last GMT Defaults parameter from command options had no value
gmtset [ERROR]:  1 GMT Defaults conversion errors from command options
Traceback (most recent call last):
  File "/home/seisman/Syncthing/test.py", line 5, in <module>
    fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame=True, timestamp=True)
  File "/home/seisman/OSS/gmt/pygmt/pygmt/src/config.py", line 213, in __exit__
    lib.call_module(module="set", args=arg_str)
  File "/home/seisman/OSS/gmt/pygmt/pygmt/clib/session.py", line 510, in call_module
    raise GMTCLibError(
pygmt.exceptions.GMTCLibError: Module 'set' failed with status code 72:
gmtset [ERROR]: Unrecognized keyword %b.
gmtset [ERROR]:  1 GMT Defaults conversion errors from command options

System information

PyGMT information:
  version: v0.8.1.dev9+g9852724e
System information:
  python: 3.9.15 | packaged by conda-forge | (main, Nov 22 2022, 15:55:03)  [GCC 10.4.0]
  executable: /home/seisman/opt/miniconda/bin/python3.9
  machine: Linux-6.0.9-300.fc37.x86_64-x86_64-with-glibc2.36
Dependency information:
  numpy: 1.23.4
  pandas: 1.5.1
  xarray: 2022.10.0
  netCDF4: 1.6.1
  packaging: 21.3
  geopandas: None
  ghostscript: 9.56.1
GMT library information:
  binary version: 6.5.0_8b8bc2e_2023.01.01
  cores: 80
  grid layout: rows
  library path: /home/seisman/opt/GMT-master/lib64/libgmt.so
  padding: 2
  plugin dir: /home/seisman/opt/GMT-master/lib64/gmt/plugins
  share dir: /home/seisman/opt/GMT-master/share
  version: 6.5.0
@seisman seisman added the bug Something isn't working label Jan 4, 2023
@seisman seisman added this to the 0.9.0 milestone Jan 4, 2023
@seisman seisman added the help wanted Helping hands are appreciated label Jan 4, 2023
@seisman
Copy link
Member Author

seisman commented Jan 19, 2023

Actually, whitespaces are allowed in pygmt.config arguments, thanks to c29e632. The issue exists when the defaults values (or the old values) of the configurations contain whitespaces.

Here is a better example:

import pygmt

fig = pygmt.Figure()
with pygmt.config(FORMAT_TIME_STAMP="%Y"):
    fig.basemap(region=[0, 10, 0, 10], projection="X10c/10c", frame=True, timestamp=True)
fig.show()

The default value of FORMAT_TIME_STAMP is %Y %b %d %H:%M:%S. When exiting the context manager, pygmt.config calls the gmtset module to reset it to the old value. That's where it fails.

The error message is:

gmtset [ERROR]: Unrecognized keyword %b.
gmtset [WARNING]: Last GMT Defaults parameter from command options had no value
gmtset [ERROR]:  1 GMT Defaults conversion errors from command options
Traceback (most recent call last):
  File "/Users/seisman/OSS/gmt/pygmt/workspace/test2.py", line 4, in <module>
    with pygmt.config(FORMAT_TIME_STAMP="%Y"):
  File "/Users/seisman/OSS/gmt/pygmt/pygmt/src/config.py", line 213, in __exit__
    lib.call_module(module="set", args=arg_str)
  File "/Users/seisman/OSS/gmt/pygmt/pygmt/clib/session.py", line 510, in call_module
    raise GMTCLibError(
pygmt.exceptions.GMTCLibError: Module 'set' failed with status code 72:
gmtset [ERROR]: Unrecognized keyword %b.
gmtset [ERROR]:  1 GMT Defaults conversion errors from command options

@seisman
Copy link
Member Author

seisman commented Jan 19, 2023

Here is another example showing the bug:

import pygmt

fig = pygmt.Figure()
pygmt.config(FORMAT_DATE_MAP="yyyy mm dd")
with pygmt.config(FORMAT_DATE_MAP="o dd"):
    fig.basemap(
        region=["1969-7-21T", "1969-7-23T", 0, 1],
        projection="X2.5c/0.1c",
        frame=["sxa1D", "S"],
    )
fig.show()

The error message is:

gmtset [ERROR]: Unrecognized keyword mm.
gmtset [ERROR]:  1 GMT Defaults conversion errors from command options
Traceback (most recent call last):
  File "/Users/seisman/OSS/gmt/pygmt/workspace/test2.py", line 5, in <module>
    with pygmt.config(FORMAT_DATE_MAP="o dd"):
  File "/Users/seisman/OSS/gmt/pygmt/pygmt/src/config.py", line 213, in __exit__
    lib.call_module(module="set", args=arg_str)
  File "/Users/seisman/OSS/gmt/pygmt/pygmt/clib/session.py", line 510, in call_module
    raise GMTCLibError(
pygmt.exceptions.GMTCLibError: Module 'set' failed with status code 72:
gmtset [ERROR]: Unrecognized keyword mm.
gmtset [ERROR]:  1 GMT Defaults conversion errors from command options

The test is modified from the existing test

def test_config_format_date_map():
so that I can reuse this test as a regression test for this issue. The new test is added in #2331.

@weiji14 weiji14 changed the title pygmt.config doens't work with values that have whitespaces pygmt.config doesn't work with values that have whitespaces Jan 24, 2023
@seisman seisman removed the help wanted Helping hands are appreciated label Jan 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
1 participant