-
Notifications
You must be signed in to change notification settings - Fork 235
Wrap grdhisteq #1433
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
Merged
Wrap grdhisteq #1433
Changes from 2 commits
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
b48cad9
Add grdhisteq processing function
1cf0a95
Merge branch 'main' into grdhisteq
1dbfff5
Merge branch 'main' into grdhisteq
e248dcd
Apply suggestions from code review
e36c93a
Support table or grid output
67f4e41
Only accept int for divisions
556dc47
Add more code supporting table or grid output
40fbbbe
Update test
e04ef8a
Update docstring
6602098
Improve handling of tabular output
94fd588
Add more tests
cc6fde1
Fix function names
9cd2cbc
Remove -h common option
29f14e7
Merge branch 'main' into grdhisteq
68e23dd
Support outgrid=None
7512225
Update implementation to require outfile or outgrid
b8635ee
Merge branch 'main' into grdhisteq
a021d30
Try out class structure for grid versus table output
d908c99
Merge branch 'main' into grdhisteq
c528c66
Merge branch 'main' into grdhisteq
2078120
Remove workaround for GMT 6.2 bug
a08c724
Apply suggestions from code review
222cf82
Merge branch 'main' into grdhisteq
2b69196
Merge branch 'main' into grdhisteq
e3d0678
Separate out parsing of D and G options
6f3e23f
Update tests to use static_earth_relief
b29ef79
Fix docstring
d985b98
Do not use aliases in public grdhisteq functions
a41423a
Format
b84c745
Add default column names
0338837
Format
ed5a9ff
Add inline examples
02ed8ce
Merge branch 'main' into grdhisteq
0d1e6fb
Format
c6d5368
Format
eec3dc7
Merge branch 'main' into grdhisteq
7031d93
Fix tests
d479a29
Apply suggestions from code review
325f72c
Remove two extra blank lines
d7c22e0
Fix wording
7cccc33
Apply suggestions from code review
d6d8a69
Update dtypes
4077f14
Merge branch 'main' into grdhisteq
76ba9f0
Add note about weighted equalization
32757d8
Try to improve wording in example
938b729
Apply suggestions from code review
71ed9bb
Set index for DataFrame output
492980f
Update docstring example
09f1385
Add test for invalid input
77937cf
Add test for numpy output
c5afdcc
Apply suggestions from code review
245c560
Increase code coverage
08f3d47
Merge branch 'main' into grdhisteq
c5e430f
Remove redundant docs
5017440
Do not pass tmpfile to _grdhisteq
8b5fc49
Make output_type a required argument
ace9916
Merge branch 'main' into grdhisteq
75f61e3
Use pytest-doctestplus for skipping doctests
ad23923
Apply suggestions from code review
25c1a28
Only allow header argument for file output
8df3492
Merge branch 'main' into grdhisteq
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,7 @@ Operations on grids: | |
| grdcut | ||
| grdfill | ||
| grdfilter | ||
| grdhisteq | ||
| grdlandmask | ||
| grdgradient | ||
| grdsample | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ | |
| grdfill, | ||
| grdfilter, | ||
| grdgradient, | ||
| grdhisteq, | ||
| grdinfo, | ||
| grdlandmask, | ||
| grdsample, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| """ | ||
| grdhisteq - Compute directional gradients from a grid. | ||
| """ | ||
|
|
||
| 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.
Outdated
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.
Outdated
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.
Outdated
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.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| {R} | ||
| {V} | ||
maxrjones marked this conversation as resolved.
Outdated
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.
Outdated
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.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
Outdated
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.
Outdated
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.
Outdated
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.
Outdated
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.
Outdated
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.