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

Wrap grdhisteq #1433

Merged
merged 61 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
b48cad9
Add grdhisteq processing function
maxrjones Jul 29, 2021
1cf0a95
Merge branch 'main' into grdhisteq
maxrjones Aug 10, 2021
1dbfff5
Merge branch 'main' into grdhisteq
maxrjones Sep 19, 2021
e248dcd
Apply suggestions from code review
maxrjones Sep 19, 2021
e36c93a
Support table or grid output
maxrjones Sep 19, 2021
67f4e41
Only accept int for divisions
maxrjones Sep 19, 2021
556dc47
Add more code supporting table or grid output
maxrjones Sep 19, 2021
40fbbbe
Update test
maxrjones Sep 19, 2021
e04ef8a
Update docstring
maxrjones Sep 19, 2021
6602098
Improve handling of tabular output
maxrjones Sep 20, 2021
94fd588
Add more tests
maxrjones Sep 20, 2021
cc6fde1
Fix function names
maxrjones Sep 20, 2021
9cd2cbc
Remove -h common option
maxrjones Sep 20, 2021
29f14e7
Merge branch 'main' into grdhisteq
maxrjones Sep 20, 2021
68e23dd
Support outgrid=None
maxrjones Sep 20, 2021
7512225
Update implementation to require outfile or outgrid
maxrjones Sep 20, 2021
b8635ee
Merge branch 'main' into grdhisteq
maxrjones Oct 1, 2021
a021d30
Try out class structure for grid versus table output
maxrjones Oct 1, 2021
d908c99
Merge branch 'main' into grdhisteq
maxrjones Oct 4, 2021
c528c66
Merge branch 'main' into grdhisteq
maxrjones Dec 27, 2021
2078120
Remove workaround for GMT 6.2 bug
maxrjones Dec 27, 2021
a08c724
Apply suggestions from code review
maxrjones Feb 4, 2022
222cf82
Merge branch 'main' into grdhisteq
maxrjones Feb 4, 2022
2b69196
Merge branch 'main' into grdhisteq
maxrjones Feb 25, 2022
e3d0678
Separate out parsing of D and G options
maxrjones Feb 25, 2022
6f3e23f
Update tests to use static_earth_relief
maxrjones Feb 25, 2022
b29ef79
Fix docstring
maxrjones Feb 25, 2022
d985b98
Do not use aliases in public grdhisteq functions
maxrjones Feb 27, 2022
a41423a
Format
maxrjones Feb 27, 2022
b84c745
Add default column names
maxrjones Feb 28, 2022
0338837
Format
maxrjones Feb 28, 2022
ed5a9ff
Add inline examples
maxrjones Feb 28, 2022
02ed8ce
Merge branch 'main' into grdhisteq
maxrjones Feb 28, 2022
0d1e6fb
Format
maxrjones Feb 28, 2022
c6d5368
Format
maxrjones Feb 28, 2022
eec3dc7
Merge branch 'main' into grdhisteq
maxrjones Mar 2, 2022
7031d93
Fix tests
maxrjones Mar 2, 2022
d479a29
Apply suggestions from code review
maxrjones Mar 3, 2022
325f72c
Remove two extra blank lines
maxrjones Mar 3, 2022
d7c22e0
Fix wording
maxrjones Mar 3, 2022
7cccc33
Apply suggestions from code review
maxrjones Mar 3, 2022
d6d8a69
Update dtypes
maxrjones Mar 3, 2022
4077f14
Merge branch 'main' into grdhisteq
maxrjones Mar 3, 2022
76ba9f0
Add note about weighted equalization
maxrjones Mar 3, 2022
32757d8
Try to improve wording in example
maxrjones Mar 3, 2022
938b729
Apply suggestions from code review
maxrjones Mar 3, 2022
71ed9bb
Set index for DataFrame output
maxrjones Mar 3, 2022
492980f
Update docstring example
maxrjones Mar 3, 2022
09f1385
Add test for invalid input
maxrjones Mar 3, 2022
77937cf
Add test for numpy output
maxrjones Mar 3, 2022
c5afdcc
Apply suggestions from code review
maxrjones Mar 4, 2022
245c560
Increase code coverage
maxrjones Mar 4, 2022
08f3d47
Merge branch 'main' into grdhisteq
maxrjones Mar 4, 2022
c5e430f
Remove redundant docs
maxrjones Mar 8, 2022
5017440
Do not pass tmpfile to _grdhisteq
maxrjones Mar 8, 2022
8b5fc49
Make output_type a required argument
maxrjones Mar 8, 2022
ace9916
Merge branch 'main' into grdhisteq
maxrjones Mar 8, 2022
75f61e3
Use pytest-doctestplus for skipping doctests
maxrjones Mar 8, 2022
ad23923
Apply suggestions from code review
maxrjones Mar 8, 2022
25c1a28
Only allow header argument for file output
maxrjones Mar 8, 2022
8df3492
Merge branch 'main' into grdhisteq
maxrjones Mar 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Operations on grids:
grdcut
grdfill
grdfilter
grdhisteq
grdlandmask
grdgradient
grdsample
Expand Down
1 change: 1 addition & 0 deletions pygmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
grdfill,
grdfilter,
grdgradient,
grdhisteq,
grdinfo,
grdlandmask,
grdsample,
Expand Down
1 change: 1 addition & 0 deletions pygmt/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from pygmt.src.grdfill import grdfill
from pygmt.src.grdfilter import grdfilter
from pygmt.src.grdgradient import grdgradient
from pygmt.src.grdhisteq import grdhisteq
from pygmt.src.grdimage import grdimage
from pygmt.src.grdinfo import grdinfo
from pygmt.src.grdlandmask import grdlandmask
Expand Down
74 changes: 74 additions & 0 deletions pygmt/src/grdhisteq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""
grdhisteq - Compute directional gradients from a grid.
maxrjones marked this conversation as resolved.
Show resolved Hide resolved
"""

import xarray as xr
from pygmt.clib import Session
from pygmt.helpers import (
GMTTempFile,
build_arg_string,
fmt_docstring,
kwargs_to_strings,
use_alias,
)
maxrjones marked this conversation as resolved.
Show resolved Hide resolved


@fmt_docstring
@use_alias(
C="divisions",
G="outgrid",
R="region",
N="gaussian",
Q="quadratic",
V="verbose",
n="interpolation",
maxrjones marked this conversation as resolved.
Show resolved Hide resolved
)
@kwargs_to_strings(R="sequence")
def grdhisteq(grid, **kwargs):
r"""
Perform histogram equalization for a grid.
maxrjones marked this conversation as resolved.
Show resolved Hide resolved

Full option list at :gmt-docs:`grdhisteq.html`
maxrjones marked this conversation as resolved.
Show resolved Hide resolved

{aliases}

Parameters
----------
grid : str or xarray.DataArray
The file name of the input grid or the grid loaded as a DataArray.
outgrid : str or None
The name of the output netCDF file with extension .nc to store the grid
in.
divisions : int or str
The number of divisions of data range to make [Default is 16].
maxrjones marked this conversation as resolved.
Show resolved Hide resolved

{R}
{V}
maxrjones marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
ret: xarray.DataArray or None
Return type depends on whether the ``outgrid`` parameter is set:

- :class:`xarray.DataArray` if ``outgrid`` is not set
- None if ``outgrid`` is set (grid output will be stored in file set by
``outgrid``)
maxrjones marked this conversation as resolved.
Show resolved Hide resolved
"""
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
with file_context as infile:
if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile
kwargs.update({"G": tmpfile.name})
outgrid = kwargs["G"]
arg_str = " ".join([infile, build_arg_string(kwargs)])
lib.call_module("grdhisteq", arg_str)

if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
with xr.open_dataarray(outgrid) as dataarray:
result = dataarray.load()
_ = result.gmt # load GMTDataArray accessor information
else:
result = None # if user sets an outgrid, return None

return result
maxrjones marked this conversation as resolved.
Show resolved Hide resolved
42 changes: 42 additions & 0 deletions pygmt/tests/test_grdhisteq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Tests for grdhisteq.
"""
import os

import numpy.testing as npt
import pytest
from pygmt import grdhisteq, grdinfo
maxrjones marked this conversation as resolved.
Show resolved Hide resolved
from pygmt.datasets import load_earth_relief
from pygmt.helpers import GMTTempFile


@pytest.fixture(scope="module", name="grid")
def fixture_grid():
"""
Load the grid data from the sample earth_relief file.
"""
return load_earth_relief(resolution="01d", region=[-5, 5, -5, 5])


def test_grdhisteq_outgrid(grid):
"""
Test the azimuth and direction parameters for grdhisteq with a set outgrid.
"""
with GMTTempFile(suffix=".nc") as tmpfile:
result = grdhisteq(grid=grid, outgrid=tmpfile.name)
maxrjones marked this conversation as resolved.
Show resolved Hide resolved
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
result = (
grdinfo(grid=tmpfile.name, force_scan="a", per_column="n").strip().split()
)
maxrjones marked this conversation as resolved.
Show resolved Hide resolved


def test_grdhisteq_no_outgrid(grid):
"""
Test the azimuth and direction parameters for grdhisteq with no set
outgrid.
"""
temp_grid = grdhisteq(grid=grid)
maxrjones marked this conversation as resolved.
Show resolved Hide resolved
assert temp_grid.dims == ("lat", "lon")
assert temp_grid.gmt.gtype == 1 # Geographic grid
assert temp_grid.gmt.registration == 1 # Pixel registration
maxrjones marked this conversation as resolved.
Show resolved Hide resolved