-
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
Allow passing xr.DataArray as shading to grdimage #750
Conversation
When I worked on PR #750 based on the implemention of the `grdview` function, I found that the `grdview` codes are a little difficult to understand. The main reason is that `contextlib.ExitStack()` is new to me. Another reason is that these two codes `fname = stack.enter_context(file_context)` and `arg_str = " ".join([fname, build_arg_string(kwargs)])` are separated by the long `if` block.
I will test this later in the weekend. As for the "+d" option, here is what I have been doing in pyshtools. I have two optional parameters:
Using the input value of As you mention, as opposed to using the "+d" option, we could just instead create the gradient map in pygmt using grdgradient (when it is wrapped). I'm not sure if this would be preferable to GMT creating the gradient or not. |
When I worked on PR #750 based on the implemention of the `grdview` function, I found that the `grdview` codes are a little difficult to understand. The main reason is that `contextlib.ExitStack()` is new to me. Another reason is that these two codes `fname = stack.enter_context(file_context)` and `arg_str = " ".join([fname, build_arg_string(kwargs)])` are separated by the long `if` block.
Yes, the grdimage -I documentation could be improved. +nt1+m0 is equivalent to -Nt1+a0 in grdgradient (https://docs.generic-mapping-tools.org/dev/grdgradient.html#n).
Yes, this PR uses the xarary in memory directly, so no temporary file on disk. |
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.
The code implementation is technically correct already in pygmt/src/grdimage.py
, just that the xarray.DataArray grdimage plot appears different to that from a NetCDF grid. What do you think about merging this PR into master (after adding an xfail to the test) so that can Paul test things easier in GenericMappingTools/gmt#5294?
fig_ref.grdimage( | ||
grid="@earth_relief_01d_g", region="GL", cmap="geo", shading=xrgrid, verbose="i" | ||
) | ||
fig_ref.colorbar() | ||
fig_test.grdimage(grid=grid, region="GL", cmap="geo", shading=xrgrid, verbose="i") | ||
fig_test.colorbar() |
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.
Btw, I tried using makecpt
to get both plots to use the same colormap scale, but the images still differ, appears to be some slight offset in the left-right direction. So it's not just a matter of CPT scaling as reported in GenericMappingTools/gmt#5294, but maybe 2 problems.
fig_ref.grdimage( | |
grid="@earth_relief_01d_g", region="GL", cmap="geo", shading=xrgrid, verbose="i" | |
) | |
fig_ref.colorbar() | |
fig_test.grdimage(grid=grid, region="GL", cmap="geo", shading=xrgrid, verbose="i") | |
fig_test.colorbar() | |
makecpt(cmap="geo", series=(-3900, 3200)) | |
fig_ref.grdimage( | |
grid="@earth_relief_01d_g", region="GL", cmap=True, shading=xrgrid, verbose="d" | |
) | |
fig_ref.colorbar() | |
fig_test.grdimage(grid=grid, region="GL", cmap=True, shading=xrgrid, verbose="d") | |
fig_test.colorbar() |
Expected (correct) | Generated (wrong) | Diff |
---|---|---|
Sounds good to me. |
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
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.
Good work! I think we can merge after the tests pass so that the debugging work at GenericMappingTools/gmt#5294 can proceed.
…s#750) * Pass xr.DataArray shading to grdimage * Test grdimage with xarray.DataArray input to both grid and shading * Update grdimage shading docstring to mention xarray.DataArray input * Mark the grdimage shading test as xfail [skip ci] Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Description of proposed changes
Following the comment #618 (comment) and the similar implementation in
grdview
function, this PR allows passing xr.DataArray shading to grdimage.Here are two examples:
The
shading.grd
file is generated using GMT command, which is not available in PyGMT yet:Fixes #618.
ISSUES
If
shading
is given as a grid file, GMT can automatically derive the gradient/intensity/shading by adding +d to the grid file name, e.g.,fig.grdimage(grid="@earth_relief_01d_g", shading="@earth_relief_01d_g+d")
. However, it's impossible whenshading
is given as a xr.DataArray. Perhaps we could add a new argument (e.g.,shading_parameters="+d"
) and automatically append it to the shading file name or virtual file name.TODO
Reminders
make format
andmake check
to make sure the code follows the style guide.doc/api/index.rst
.Notes
/format
in the first line of a comment to lint the code automatically