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

Arguments with spaces in strings #247

Closed
seisman opened this issue Nov 26, 2018 · 3 comments · Fixed by #1487
Closed

Arguments with spaces in strings #247

seisman opened this issue Nov 26, 2018 · 3 comments · Fixed by #1487
Labels
bug Something isn't working help wanted Helping hands are appreciated
Milestone

Comments

@seisman
Copy link
Member

seisman commented Nov 26, 2018

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
@leouieda
Copy link
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.

@leouieda leouieda added the bug Something isn't working label Jan 22, 2019
@seisman seisman mentioned this issue Jan 9, 2020
@weiji14 weiji14 added the help wanted Helping hands are appreciated label Dec 5, 2020
weiji14 added a commit that referenced this issue Feb 6, 2021
weiji14 added a commit that referenced this issue Feb 14, 2021
Wrapping the `subplot` function, in a `with` statement!
Original GMT `subplot` function can be found at
https://docs.generic-mapping-tools.org/6.1/subplot.html.
This is the 3rd attempt at implementing `subplot` in PyGMT,
with commits heavily re-adapted/cherry-picked from
#412 and
#427.

* Add fig.subplot and fig.set_panel to API docs
* Alias fixedlabel (A), clearance (C), verbose (V) for set_panel
* Turn fig.set_panel into a context manager
* Alias projection (J), region (R), verbose (V), x/yshift (X/Y) for subplot
* Allow for spaces in title and labels without needing double quotes

Mitigates against #247.

* Allow for list inputs into fig.set_panel(panel=...)
* Rename sca to set_panel and ax to panel
* Fix bug that prevented boolean to -A from working
* Add note that subplot panel is activated until further notice
* Validate subplot nrows/ncols and figsize/subsize argument inputs
* Revise advanced subplot layout subsection to use two subplots calls

Co-authored-by: Dongdong Tian <[email protected]>
@seisman
Copy link
Member Author

seisman commented Mar 25, 2021

NOTE: the list2cmdline function may be helpful.

@weiji14
Copy link
Member

weiji14 commented Sep 6, 2021

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.

@seisman seisman added this to the 0.6.0 milestone Mar 13, 2022
sixy6e pushed a commit to sixy6e/pygmt that referenced this issue Dec 21, 2022
Wrapping the `subplot` function, in a `with` statement!
Original GMT `subplot` function can be found at
https://docs.generic-mapping-tools.org/6.1/subplot.html.
This is the 3rd attempt at implementing `subplot` in PyGMT,
with commits heavily re-adapted/cherry-picked from
GenericMappingTools#412 and
GenericMappingTools#427.

* Add fig.subplot and fig.set_panel to API docs
* Alias fixedlabel (A), clearance (C), verbose (V) for set_panel
* Turn fig.set_panel into a context manager
* Alias projection (J), region (R), verbose (V), x/yshift (X/Y) for subplot
* Allow for spaces in title and labels without needing double quotes

Mitigates against GenericMappingTools#247.

* Allow for list inputs into fig.set_panel(panel=...)
* Rename sca to set_panel and ax to panel
* Fix bug that prevented boolean to -A from working
* Add note that subplot panel is activated until further notice
* Validate subplot nrows/ncols and figsize/subsize argument inputs
* Revise advanced subplot layout subsection to use two subplots calls

Co-authored-by: Dongdong Tian <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Helping hands are appreciated
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants