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

RFC: Wrap grdmath #1527

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

RFC: Wrap grdmath #1527

wants to merge 2 commits into from

Conversation

weiji14
Copy link
Member

@weiji14 weiji14 commented Sep 19, 2021

Description of proposed changes

Initial commit for wrapping the grdmath function which is a "Raster calculator for grids (element by element)".

Preview docs at https://pygmt-git-wrap-grdmath-gmt.vercel.app/api/generated/pygmt.GrdMathCalc.html.

This is an initial proof of concept that is meant to solicit ideas from other PyGMT developers. The current implementation works by building and storing a 'computational graph' of the grid operations in a GrdMathCalc class object. An output NetCDF grid or xarray.DataArray is produced upon calling .compute() or by setting outgrid=True.

Example code:

import pygmt

grdcalc = pygmt.GrdMathCalc()  # instantiate grdmath Calculator object

# Basic square root operation
grid0 = grdcalc.sqrt(ingrid="@earth_relief_01d", outgrid=True, region=[0, 3, 6, 9])

# Complex chained operation
grid1 = grdcalc.sqrt(ingrid="@earth_relief_01d_p")
assert grid1.arg_str == "@earth_relief_01d_p SQRT"
grid2 = grdcalc.std(ingrid="@earth_relief_01d_g")
assert grid2.arg_str == "@earth_relief_01d_g STD"
grid3 = grid1.multiply(ingrid=grid2, region=[0, 3, 6, 9])
assert (
    grid3.arg_str == "@earth_relief_01d_p SQRT @earth_relief_01d_g STD -R0/3/6/9 MUL"
)
output = grid3.compute()

The API here is inspired by many other Python 'array' libraries. Note that there is a Python array API standard that is in the works which should inform the grdmath implementation here. E.g. doing grdcalc.mean should ideally follow the API style of np.mean if possible.

References:

Fixes #916

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If adding new functionality, add an example to docstrings or tutorials.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash commands are:

  • /format: automatically format and lint the code
  • /test-gmt-dev: run full tests on the latest GMT development version

Initial commit for wrapping the grdmath function for #916
which is a "Raster calculator for grids (element by element)".
Original GMT `grdmath` documentation is at
https://docs.generic-mapping-tools.org/6.2/grdmath.html.
Implementation works by building and storing a computational
graph of the grid operations in a GrdMathCalc class object.
An output NetCDF grid or xarray.DataArray is produced
upon calling `.compute()` or by setting `outgrid=True`.
@weiji14 weiji14 added the feature Brand new feature label Sep 19, 2021
@weiji14 weiji14 added this to the 0.6.0 milestone Sep 19, 2021
@weiji14 weiji14 self-assigned this Sep 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Brand new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

grdmath SUB operator
1 participant