-
Notifications
You must be signed in to change notification settings - Fork 229
GMTDataArrayAccessor: Enable grid operations on the current xarray.DataArray object directly #3854
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
base: main
Are you sure you want to change the base?
Conversation
pygmt/accessors.py
Outdated
def clip(self, **kwargs) -> xr.DataArray: | ||
""" | ||
Clip the range of grid values. | ||
|
||
See the :func:`pygmt.grdclip` function for available parameters. | ||
""" | ||
return grdclip(grid=self._obj, **kwargs) |
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.
Probably best to keep the method name as .gmt.grdclip
instead of .gmt.clip
, because there is also https://docs.generic-mapping-tools.org/6.5/clip.html (same with the other grd* methods below), and who knows if we might use pandas accessors at some point.
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.
Even if we decide to implement the pandas accessors in the future (actually, I feel it's a good idea), there are still no conflicts since the two accessors are in different namespaces. E.g.,
da.gmt.clip
: Clip operation on a xr.DataArray grid/imagedf.gmt.clip
: Clip operation on a pd.DataFrame object
I think this PR is ready for review. I guess I still need to add tests to increase code coverage? |
def dimfilter(self, **kwargs) -> xr.DataArray: | ||
""" | ||
Directional filtering of a grid in the space domain. | ||
|
||
See the :func:`pygmt.dimfilter` function for available parameters. | ||
""" | ||
return dimfilter(grid=self._obj, **kwargs) |
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.
Shorter way to declare new methods is to put this in the __init__
method:
import functools
def __init__(self, xarray_obj: xr.DataArray):
...
self.dimfilter = functools.partial(dimfilter, grid=self._obj)
self.dimfilter.__doc__ = dimfilter.__doc__
This would preserve the full docs too, e.g. output from help(grid.gmt.dimfilter)
Help on partial in module functools:
functools.partial(<function dimfilter at 0x7fd07...e: [190. 981.]
long_name: elevation (m))
Directional filtering of grids in the space domain.
Filter a grid in the space (or time) domain by
dividing the given filter circle into the given number of sectors,
applying one of the selected primary convolution or non-convolution
filters to each sector, and choosing the final outcome according to the
selected secondary filter. It computes distances using Cartesian or
Spherical geometries. The output grid can optionally be generated as a
subregion of the input and/or with a new increment using ``spacing``,
which may add an "extra space" in the input data to prevent edge
effects for the output grid. If the filter is low-pass, then the output
may be less frequently sampled than the input. :func:`pygmt.dimfilter`
will not produce a smooth output as other spatial filters
do because it returns a minimum median out of *N* medians of *N*
sectors. The output can be rough unless the input data are noise-free.
Thus, an additional filtering (e.g., Gaussian via :func:`pygmt.grdfilter`)
of the DiM-filtered data is generally recommended.
Full option list at :gmt-docs:`dimfilter.html`
**Aliases:**
.. hlist::
:columns: 3
- D = distance
- F = filter
- I = spacing
- N = sectors
- R = region
- V = verbose
There might be a nicer way to wrap things (maybe using https://docs.python.org/3/library/functools.html#functools.update_wrapper?), but haven't played around with it too much.
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.
Tried your solution above and found two issues:
self.dimfilter
is an attribute of the accessor, so it's not shown on the documentation (https://pygmt-dev--3854.org.readthedocs.build/en/3854/api/generated/pygmt.GMTDataArrayAccessor.html)- The
help
docs still showgrid
as its first parameter, which may be more confusing?
This is a POC PR to show how GMTDataArrayAccessor can access GMT's grid-operation modules, i.e.,
grid.gmt.fill(**kwargs)
instead ofpygmt.grdfill(grid, **kwargs)
:Address #499 (comment).
Preview: https://pygmt-dev--3854.org.readthedocs.build/en/3854/api/generated/pygmt.GMTDataArrayAccessor.html
Grid-related modules
Processing (grid in, grid out)
equalize_hist
. The name comes from https://scikit-image.org/docs/stable/api/skimage.exposure.html#skimage.exposure.equalize_hist.Plotting
Misc