Skip to content

Arguments with spaces in strings #247

Closed

Description

@seisman

Description of the problem

Full code that generated the error

I tried to plot a basemap with an axis labelled as "Crust Age". I tried in three different ways:

import gmt

fig = gmt.Figure()
# 1st way: FAIL
fig.basemap(R=[0, 100, 0, 50], J="X10c/5c", B='x20+lCrust Age')
# 2nd way: SUCCEED
fig.basemap(R=[0, 100, 0, 50], J="X10c/5c", B='x20+l"Crust Age"')
# 3rd way: FAIL
fig.basemap(R=[0, 100, 0, 50], J="X10c/5c", B="x20+l'Crust Age'")
fig.savefig("map.pdf")

Full error message

psbasemap [ERROR]: Unrecognized option -<
Traceback (most recent call last):
  File "test.sh", line 6, in <module>
    fig.basemap(R=[0, 100, 0, 50], J="X10c/5c", B="x20+l'Crust Age'")
  File "/Users/seisman/Gits/gmt/gmt-python/gmt/helpers/decorators.py", line 199, in new_module
    return module_func(*args, **kwargs)
  File "/Users/seisman/Gits/gmt/gmt-python/gmt/helpers/decorators.py", line 294, in new_module
    return module_func(*args, **kwargs)
  File "/Users/seisman/Gits/gmt/gmt-python/gmt/base_plotting.py", line 472, in basemap
    lib.call_module("basemap", build_arg_string(kwargs))
  File "/Users/seisman/Gits/gmt/gmt-python/gmt/clib/session.py", line 490, in call_module
    module, status, self._error_message
gmt.exceptions.GMTCLibError: Module 'basemap' failed with status code 71:
psbasemap [ERROR]: Unrecognized option -<

The 1st way is equivalent to command line

gmt basemap -R0/100/0/50 -JX10c/5c -Bx20+lCrust Age 

The command fails because "Age" is thought to be a file. However, the error message is almost useless to help users find the error. Do you think it's technically possible to automatically add quotes around strings with spaces if users forget to do that?

The 2nd way works as expected. However, I don't understand why the 3rd way fails? The equivalent cmd version works:

gmt basemap -R0/100/0/50 -JX10c/5c -Bx20+l'Crust Age' -pdf map

System information

  • Operating system: macOS
  • Python installation (Anaconda, system, ETS): Anaconda
  • Version of GMT: 6.0.0_15a9160
  • Version of Python: 3.7.0
  • Version of this package: latest

Activity

  1. leouieda commented on Nov 28, 2018

    @leouieda
    Member

    Do you think it's technically possible to automatically add quotes around strings with spaces if users forget to do that?

    This is something we'll have to work on. I really don't like the way that -B works right now. It's very non-intuitive and requires the user to specify it multiple times. There shouldn't be a single argument for setting labels, titles, gridlines, tickmarks, etc. See #249. I think that's a better solution than trying to guess what the user is doing with their ticks.

  2. added
    bug
    on Jan 22, 2019
  3. added
    help wanted
    on Dec 5, 2020
  4. added a commit that references this issue on Feb 14, 2021
  5. seisman commented on Mar 25, 2021

    @seisman
    MemberAuthor

    NOTE: the list2cmdline function may be helpful.

  6. weiji14 commented on Sep 6, 2021

    @weiji14
    Member

    Just had an idea from #1474 (comment), how about we find and replace space characters ( ) to use the octal code for space (\040)? Reference from https://docs.generic-mapping-tools.org/6.2/cookbook/octal-codes.html. Proof of concept below using PyGMT v0.4.1:

    import pygmt
    
    fig = pygmt.Figure()
    # 1st way: SUCCEED! without needing double quotes
    fig.basemap(region=[0, 2, 4, 8], projection="X2c", frame=r"WSne+tPlot\040Zero")
    # 2nd way: SUCCEED as usual
    fig.basemap(frame=r'WSne+t"Plot\040One"', xshift="3c")
    # 3rd way: SUCCEED! But with single quotes
    fig.basemap(frame=r"WSne+t'Plot\040Two'", xshift="3c")
    fig.savefig("plots_with_spaces.png")
    fig.show()

    plots_with_spaces

    Caveat is that we will need to use raw-strings r'Some\040string', but I think this is definitely a good way to go.

  7. added this to the 0.6.0 milestone on Mar 13, 2022
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

    bughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions