-
Notifications
You must be signed in to change notification settings - Fork 228
Wrap grdvolume #1299
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
Merged
Wrap grdvolume #1299
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
efa0519
Create grdvolume.py
willschlitzer 19f2fe7
add grdvolume to imports
willschlitzer d3ed614
add unit and slice parameters
willschlitzer 68fab7c
add formatting to return array or DataFrame
willschlitzer 07860a9
Merge branch 'master' into wrap-grdvolume
willschlitzer 89eb397
add test_grdvolume.py
willschlitzer 57633c3
add test_grdvolume_format
willschlitzer 97adf31
add test_grdvolume_invalid_format
willschlitzer 75ff3a2
add docstring for grdvolume
willschlitzer c94288f
update data_format docstring
willschlitzer 7eb90b5
update data_format docstring to give one-line description
willschlitzer 71ff162
Merge branch 'main' into wrap-grdvolume
willschlitzer 64279fb
update table output format
willschlitzer d4e33ac
run make format and make check
willschlitzer c4f9ac9
remove unused numpy import
willschlitzer d32ef36
Merge branch 'main' into wrap-grdvolume
willschlitzer 00178bb
Apply suggestions from code review
willschlitzer d636c58
fixing syntax error
willschlitzer e45ecc0
adding contour docstring
willschlitzer 0e8195c
change accepted formats for contour in docstring
willschlitzer b80edb4
change size of grid file for tests
willschlitzer c493776
Apply suggestions from code review
willschlitzer 7be5047
fix spacing
willschlitzer b4ae9db
changing test coordinates to be on land
willschlitzer d900d7c
Merge branch 'main' into wrap-grdvolume
willschlitzer eb02866
add test_grdvolume_no_output
willschlitzer d7c98cd
add test_grdvolume_outgrid
willschlitzer 0509f02
add test_grdvolume_no_outfile
willschlitzer 5150a85
Apply suggestions from code review
willschlitzer dd478e9
Update pygmt/src/grdvolume.py
willschlitzer 52b22c7
Merge branch 'main' into wrap-grdvolume
willschlitzer ece4f8f
Apply suggestions from code review
willschlitzer 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 |
---|---|---|
|
@@ -104,6 +104,7 @@ Operations on grids: | |
grdproject | ||
grdsample | ||
grdtrack | ||
grdvolume | ||
|
||
Crossover analysis with x2sys: | ||
|
||
|
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 |
---|---|---|
|
@@ -46,6 +46,7 @@ | |
grdproject, | ||
grdsample, | ||
grdtrack, | ||
grdvolume, | ||
info, | ||
makecpt, | ||
nearneighbor, | ||
|
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,103 @@ | ||
""" | ||
grdvolume - Calculate grid volume and area constrained by a contour. | ||
""" | ||
import pandas as pd | ||
from pygmt.clib import Session | ||
from pygmt.exceptions import GMTInvalidInput | ||
from pygmt.helpers import ( | ||
GMTTempFile, | ||
build_arg_string, | ||
fmt_docstring, | ||
kwargs_to_strings, | ||
use_alias, | ||
) | ||
|
||
|
||
@fmt_docstring | ||
@use_alias( | ||
C="contour", | ||
R="region", | ||
S="unit", | ||
V="verbose", | ||
) | ||
@kwargs_to_strings(C="sequence", R="sequence") | ||
def grdvolume(grid, output_type="pandas", outfile=None, **kwargs): | ||
r""" | ||
Determine the volume between the surface of a grid and a plane. | ||
|
||
Read a 2-D grid file and calculate the volume contained below the surface | ||
and above the plane specified by the given contour (or zero if not given) | ||
and return the contour, area, volume, and maximum mean height | ||
(volume/area). Alternatively, a range of contours can be specified to | ||
return the volume and area inside the contour for all contour values. | ||
|
||
Full option list at :gmt-docs:`grdvolume.html` | ||
|
||
{aliases} | ||
|
||
Parameters | ||
---------- | ||
grid : str or xarray.DataArray | ||
The file name of the input grid or the grid loaded as a DataArray. | ||
output_type : str | ||
Determine the format the output data will be returned in [Default is | ||
``pandas``]: | ||
|
||
- ``numpy`` - :class:`numpy.ndarray` | ||
- ``pandas``- :class:`pandas.DataFrame` | ||
- ``file`` - ASCII file (requires ``outfile``) | ||
outfile : str | ||
The file name for the output ASCII file. | ||
contour : str or int or float or list | ||
*cval*\|\ *low/high/delta*\|\ **r**\ *low/high*\|\ **r**\ *cval*. | ||
Find area, volume and mean height (volume/area) inside and above the | ||
*cval* contour. Alternatively, search using all contours from *low* to | ||
*high* in steps of *delta*. [Default returns area, volume and mean | ||
height of the entire grid]. The area is measured in the plane of the | ||
contour. Adding the **r** prefix computes the volume below the grid | ||
surface and above the planes defined by *low* and *high*, or below | ||
*cval* and grid's minimum. Note that this is an *outside* volume | ||
whilst the other forms compute an *inside* (below the surface) area | ||
volume. Use this form to compute for example the volume of water | ||
between two contours. If no *contour* is given then there is no contour | ||
and the entire grid area, volume and the mean height is returned and | ||
*cval* will be reported as 0. | ||
{R} | ||
{V} | ||
|
||
Returns | ||
------- | ||
ret : pandas.DataFrame or numpy.ndarray or None | ||
Return type depends on ``outfile`` and ``output_type``: | ||
|
||
- None if ``outfile`` is set (output will be stored in file set by | ||
``outfile``) | ||
- :class:`pandas.DataFrame` or :class:`numpy.ndarray` if ``outfile`` | ||
is not set (depends on ``output_type`` [Default is | ||
class:`pandas.DataFrame`]) | ||
""" | ||
if output_type not in ["numpy", "pandas", "file"]: | ||
raise GMTInvalidInput( | ||
"""Must specify format as either numpy, pandas, or file.""" | ||
) | ||
if output_type == "file" and outfile is None: | ||
raise GMTInvalidInput("""Must specify outfile for ASCII output.""") | ||
|
||
with GMTTempFile() as tmpfile: | ||
with Session() as lib: | ||
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid) | ||
with file_context as infile: | ||
if outfile is None: | ||
outfile = tmpfile.name | ||
arg_str = " ".join([infile, build_arg_string(kwargs), "->" + outfile]) | ||
lib.call_module("grdvolume", arg_str) | ||
|
||
# Read temporary csv output to a pandas table | ||
if outfile == tmpfile.name: # if user did not set outfile, return pd.DataFrame | ||
result = pd.read_csv(tmpfile.name, sep="\t", header=None, comment=">") | ||
elif outfile != tmpfile.name: # return None if outfile set, output in outfile | ||
result = None | ||
|
||
if output_type == "numpy": | ||
result = result.to_numpy() | ||
return result |
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,112 @@ | ||
""" | ||
Tests for grdvolume. | ||
""" | ||
import os | ||
|
||
import numpy as np | ||
import numpy.testing as npt | ||
import pandas as pd | ||
import pytest | ||
from pygmt import grdvolume | ||
from pygmt.datasets import load_earth_relief | ||
from pygmt.exceptions import GMTInvalidInput | ||
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=[-100, -95, 34, 39]) | ||
|
||
|
||
@pytest.fixture(scope="module", name="data") | ||
def fixture_data(): | ||
""" | ||
Load the expected grdvolume data result as a numpy array. | ||
""" | ||
data = np.array( | ||
[ | ||
[ | ||
2.00000000e02, | ||
1.59920815e11, | ||
3.16386172e13, | ||
1.97839269e02, | ||
], | ||
[ | ||
2.50000000e02, | ||
1.44365835e11, | ||
2.38676788e13, | ||
1.65327751e02, | ||
], | ||
[ | ||
3.00000000e02, | ||
1.23788259e11, | ||
1.71278707e13, | ||
1.38364259e02, | ||
], | ||
[ | ||
3.50000000e02, | ||
9.79597525e10, | ||
1.15235913e13, | ||
1.17635978e02, | ||
], | ||
[ | ||
4.00000000e02, | ||
7.26646663e10, | ||
7.22303463e12, | ||
9.94022955e01, | ||
], | ||
] | ||
) | ||
return data | ||
|
||
|
||
def test_grdvolume_format(grid): | ||
""" | ||
Test that correct formats are returned. | ||
""" | ||
grdvolume_default = grdvolume(grid=grid) | ||
assert isinstance(grdvolume_default, pd.DataFrame) | ||
grdvolume_array = grdvolume(grid=grid, output_type="numpy") | ||
assert isinstance(grdvolume_array, np.ndarray) | ||
grdvolume_df = grdvolume(grid=grid, output_type="pandas") | ||
assert isinstance(grdvolume_df, pd.DataFrame) | ||
|
||
|
||
def test_grdvolume_invalid_format(grid): | ||
""" | ||
Test that grdvolume fails with incorrect output_type argument. | ||
""" | ||
with pytest.raises(GMTInvalidInput): | ||
grdvolume(grid=grid, output_type=1) | ||
|
||
|
||
def test_grdvolume_no_outfile(grid): | ||
""" | ||
Test that grdvolume fails when output_type set to 'file' but no outfile is | ||
specified. | ||
""" | ||
with pytest.raises(GMTInvalidInput): | ||
grdvolume(grid=grid, output_type="file") | ||
|
||
|
||
def test_grdvolume_no_outgrid(grid, data): | ||
""" | ||
Test the expected output of grdvolume with no output file set. | ||
""" | ||
test_output = grdvolume(grid=grid, contour=[200, 400, 50], output_type="numpy") | ||
npt.assert_allclose(test_output, data) | ||
|
||
|
||
def test_grdvolume_outgrid(grid): | ||
""" | ||
Test the expected output of grdvolume with an output file set. | ||
""" | ||
with GMTTempFile(suffix=".csv") as tmpfile: | ||
result = grdvolume( | ||
grid=grid, contour=[200, 400, 50], output_type="file", outfile=tmpfile.name | ||
) | ||
assert result is None # return value is None | ||
assert os.path.exists(path=tmpfile.name) # check that outfile exists |
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.