Skip to content
Merged
Changes from all commits
Commits
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
69 changes: 29 additions & 40 deletions pygmt/tests/test_grdvolume.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@
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
from pygmt.helpers.testing import load_static_earth_relief


@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])
return load_static_earth_relief()


@pytest.fixture(scope="module", name="region")
def fixture_region():
"""
Set the data region for the tests.
"""
return [-53, -50, -22, -20]


@pytest.fixture(scope="module", name="data")
Expand All @@ -28,50 +36,25 @@ def fixture_data():
"""
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,
],
[2.00000000e02, 2.30079975e10, 3.92142453e12, 1.70437454e02],
[2.50000000e02, 2.30079975e10, 2.77102465e12, 1.20437454e02],
[3.00000000e02, 2.30079975e10, 1.62062477e12, 7.04374542e01],
[3.50000000e02, 1.76916116e10, 4.53991397e11, 2.56613930e01],
[4.00000000e02, 2.81602292e09, 2.34764859e10, 8.33675242e00],
]
)
return data


def test_grdvolume_format(grid):
def test_grdvolume_format(grid, region):
"""
Test that correct formats are returned.
"""
grdvolume_default = grdvolume(grid=grid)
grdvolume_default = grdvolume(grid=grid, region=region)
assert isinstance(grdvolume_default, pd.DataFrame)
grdvolume_array = grdvolume(grid=grid, output_type="numpy")
grdvolume_array = grdvolume(grid=grid, output_type="numpy", region=region)
assert isinstance(grdvolume_array, np.ndarray)
grdvolume_df = grdvolume(grid=grid, output_type="pandas")
grdvolume_df = grdvolume(grid=grid, output_type="pandas", region=region)
assert isinstance(grdvolume_df, pd.DataFrame)


Expand All @@ -92,21 +75,27 @@ def test_grdvolume_no_outfile(grid):
grdvolume(grid=grid, output_type="file")


def test_grdvolume_no_outgrid(grid, data):
def test_grdvolume_no_outgrid(grid, data, region):
"""
Test the expected output of grdvolume with no output file set.
"""
test_output = grdvolume(grid=grid, contour=[200, 400, 50], output_type="numpy")
test_output = grdvolume(
grid=grid, contour=[200, 400, 50], output_type="numpy", region=region
)
npt.assert_allclose(test_output, data)


def test_grdvolume_outgrid(grid):
def test_grdvolume_outgrid(grid, region):
"""
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
grid=grid,
contour=[200, 400, 50],
output_type="file",
outfile=tmpfile.name,
region=region,
)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outfile exists