-
Notifications
You must be signed in to change notification settings - Fork 224
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
Wrap makecpt #329
Wrap makecpt #329
Conversation
Initial commit for wrapping the makecpt function raised at #214, tentatively implemented here under the mathops.py file which will hold functions for "Mathematical operations on tables or grids", but with documentation placed under 'Plotting'. Original GMT `makecpt` documentation can be found at https://docs.generic-mapping-tools.org/latest/makecpt.html. Tests are stored under test_makecpt.py, and there are now some basic tests ensuring that we can change the color when plotting points and grids. Current implementation uses 'cmap' and 'series' as aliases for 'C' and 'T' respectively.
Hi @weiji14, it would be nice if we had more coordination with the Julia wrapper. Specially in what regards the keyword names. For example, looking at your example I believe you named the option -T |
Funny you said that, I did think of using ‘range’ at one point (might have looked at your Julia implementation actually) but unfortunately that’s a reserved function in Python so had to come up with another name. Other than that though, I do agree that we should keep things consistent across implementations. Might be good to track this upstream, perhaps at GenericMappingTools/gmt#230? That way we don’t keep reinventing the wheel/aliases each time. As a side note, if you need help with documentation, there’s this hacktoberfest thing coming up that would be terrific for encouraging more contributions. |
Ah, yes The long-format GMT issue is the right place but when we go ahead of it we must baptize the keywords anyway. Thanks for the |
Allow makecpt to save the generated color palette table to a file via the output (H) argument. The 'H' setting in GMT upstream (see https://docs.generic-mapping-tools.org/latest/makecpt.html#h) is actually a flag to force the creation of an output (to stdout) in modern mode. Here we use it to set the filename too. See also GenericMappingTools/gmt#827 and GenericMappingTools/gmt#823
With rainbow cmap checks to test various zlow/zhigh combinations.
Used 'earth' cmap to test various reversed colormap examples.
I think this is ready for review. The Happy to consider any other suggestions too 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks good to me.
Can you also implement the -Z
option? I believe it's also commonly used.
outfile = kwargs.pop("H") | ||
if not outfile or not isinstance(outfile, str): | ||
raise GMTInvalidInput("'output' should be a proper file name.") | ||
arg_str = " ".join([build_arg_string(kwargs), f"-H > {outfile}"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other functions in PyGMT which output to a file use ->
instead of >
. I don't know why but maybe you can try if ->
also works.
arg_str = " ".join([build_arg_string(kwargs), f"-H > {outfile}"]) | |
arg_str = " ".join([build_arg_string(kwargs), f"-H -> {outfile}"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure it'll work, and I did notice it in other parts of the code but never quite understood why ->
is used. Is it meant to be a cross-platform thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I was wrong, ->
doesn't work. The test_makecpt_output_to_cpt_file
function fails with this error message:
def test_makecpt_output_to_cpt_file():
"""
Save the generated static color palette table to a .cpt file
"""
with GMTTempFile(suffix=".cpt") as cptfile:
> makecpt(output=cptfile.name)
../pygmt/tests/test_makecpt.py:82:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../pygmt/helpers/decorators.py:187: in new_module
return module_func(*args, **kwargs)
../pygmt/helpers/decorators.py:282: in new_module
return module_func(*args, **kwargs)
../pygmt/mathops.py:65: in makecpt
lib.call_module(module="makecpt", args=arg_str)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <pygmt.clib.session.Session object at 0x7fe9ae6ad470>, module = 'makecpt', args = ' -H -> /tmp/pygmt-dz2kwkqs.cpt'
def call_module(self, module, args):
"""
Call a GMT module with the given arguments.
Makes a call to ``GMT_Call_Module`` from the C API using mode
``GMT_MODULE_CMD`` (arguments passed as a single string).
Most interactions with the C API are done through this function.
Parameters
----------
module : str
Module name (``'pscoast'``, ``'psbasemap'``, etc).
args : str
String with the command line arguments that will be passed to the
module (for example, ``'-R0/5/0/10 -JM'``).
Raises
------
GMTCLibError
If the returned status code of the function is non-zero.
"""
c_call_module = self.get_libgmt_func(
"GMT_Call_Module",
argtypes=[ctp.c_void_p, ctp.c_char_p, ctp.c_int, ctp.c_void_p],
restype=ctp.c_int,
)
mode = self["GMT_MODULE_CMD"]
status = c_call_module(
self.session_pointer, module.encode(), mode, args.encode()
)
if status != 0:
raise GMTCLibError(
"Module '{}' failed with status code {}:\n{}".format(
> module, status, self._error_message
)
)
E pygmt.exceptions.GMTCLibError: Module 'makecpt' failed with status code 71:
E makecpt [ERROR]: Syntax error: No input files expected unless -E or -S are used
../pygmt/clib/session.py:490: GMTCLibError
--------------------------------------------------------------------------------- Captured stderr call ---------------------------------------------------------------------------------
makecpt [ERROR]: Syntax error: No input files expected unless -E or -S are used
Included one test to create a continuous cpt from blue to white. Also updated link to the full list of GMT's color palette tables due to documentation reorganization during GenericMappingTools/gmt#1594.
Includes GenericMappingTools/pygmt#343 that will hopefully address https://github.com/dependabot/feedback/issues/482. There's also the new `makecpt` wrapper added in GenericMappingTools/pygmt#329. Need to temporarily pin dask at 0.12.3 because of some multiprocessing error. - [GMT](https://github.com/GenericMappingTools/gmt) from 6.0.0rc3 to 6.0.0rc5 - [Release notes](https://github.com/GenericMappingTools/gmt/releases/tag/6.0.0rc5) - [Commits](GenericMappingTools/gmt@6.0.0rc3...6.0.0rc5) - [PyGMT](https://github.com/GenericMappingTools/pygmt) from 0.0.1a0-43-g421e10d to 0.0.1a0-57-g7590d4a. - [Commits](weiji14/pygmt@0.0.1a0-43-g421e10d...0.0.1a0-57-g7590d4a)
Description of proposed changes
Wrapping the
makecpt
function! Tentatively implemented here under themathops.py
file because it is classfied under "Mathematical operations on tables or grids" in GMT upstream, but I've placed the documentation for makecpt under 'Plotting' for now.Example code of how it works:
Produces:
Cross-referencing Julia wrapper/implementation at https://www.generic-mapping-tools.org/GMT.jl/latest/#GMT.makecpt.
Parameters/Aliases to wrap:
...
Fixes #214
Reminders
make format
andmake check
to make sure the code follows the style guide.doc/api/index.rst
.