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

Change test_grdtrack.py to use static_earth_relief #1762

Merged
merged 14 commits into from
Mar 3, 2022
Merged
115 changes: 41 additions & 74 deletions pygmt/tests/test_grdtrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
"""
import os

import numpy as np
import numpy.testing as npt
import pandas as pd
import pytest
from packaging.version import Version
from pygmt import clib, grdtrack, which
maxrjones marked this conversation as resolved.
Show resolved Hide resolved
from pygmt.datasets import load_earth_relief, load_ocean_ridge_points
from pygmt.datasets import load_sample_data
maxrjones marked this conversation as resolved.
Show resolved Hide resolved
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import data_kind
from pygmt.helpers.testing import load_static_earth_relief

TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
TEMP_TRACK = os.path.join(TEST_DATA_DIR, "tmp_track.txt")
Expand All @@ -24,91 +26,58 @@ def fixture_dataarray():
"""
Load the grid data from the sample earth_relief file.
"""
return load_earth_relief(registration="gridline").sel(
lat=slice(-49, -42), lon=slice(-118, -107)
)
return load_static_earth_relief()


@pytest.fixture(scope="module", name="dataframe")
def fixture_dataframe():
"""
Load the ocean ridge file.
"""
return load_ocean_ridge_points()


@pytest.fixture(scope="module", name="csvfile")
def fixture_csvfile():
@pytest.fixture(scope="module", name="expected_array")
def fixture_numpy_array():
"""
Load the csvfile.
Load a numpy array with x, y, and bathymetry data.
"""
return which("@ridge.txt", download="c")
array = [
[-51.613, -17.93, 796.59434514],
[-48.917, -22.434, 566.49184359],
[-50.444, -16.358, 571.1492788],
[-50.721, -16.628, 578.76116859],
[-51.394, -12.196, 274.43205501],
[-50.207, -18.404, 532.11444935],
[-52.56, -16.977, 670.16934401],
[-51.866, -19.794, 426.77300768],
[-48.001, -14.144, 741.35824074],
[-54.438, -19.193, 490.02716679],
]
return array


@pytest.fixture(scope="module", name="ncfile")
def fixture_ncfile():
@pytest.fixture(scope="module", name="dataframe")
def fixture_dataframe(expected_array):
maxrjones marked this conversation as resolved.
Show resolved Hide resolved
"""
Load the ncfile.
Load a pandas DataFrame with points.
"""
return which("@earth_relief_01d", download="a")
points = [
[-51.613, -17.93],
[-48.917, -22.434],
[-50.444, -16.358],
[-50.721, -16.628],
[-51.394, -12.196],
[-50.207, -18.404],
[-52.56, -16.977],
[-51.866, -19.794],
[-48.001, -14.144],
[-54.438, -19.193],
]
return pd.DataFrame(data=points, columns=["longitude", "latitude"])


def test_grdtrack_input_dataframe_and_dataarray(dataarray, dataframe):
def test_grdtrack_input_dataframe_and_dataarray(dataarray, dataframe, expected_array):
"""
Run grdtrack by passing in a pandas.DataFrame and xarray.DataArray as
inputs.
"""
output = grdtrack(points=dataframe, grid=dataarray, newcolname="bathymetry")
assert isinstance(output, pd.DataFrame)
assert output.columns.to_list() == ["longitude", "latitude", "bathymetry"]
npt.assert_allclose(output.iloc[0], [-110.9536, -42.2489, -2797.394987])

return output


def test_grdtrack_input_csvfile_and_dataarray(dataarray, csvfile):
"""
Run grdtrack by passing in a csvfile and xarray.DataArray as inputs.
"""
try:
output = grdtrack(points=csvfile, grid=dataarray, outfile=TEMP_TRACK)
assert output is None # check that output is None since outfile is set
assert os.path.exists(path=TEMP_TRACK) # check that outfile exists at path

track = pd.read_csv(TEMP_TRACK, sep="\t", header=None, comment=">")
npt.assert_allclose(track.iloc[0], [-110.9536, -42.2489, -2797.394987])
finally:
os.remove(path=TEMP_TRACK)

return output


def test_grdtrack_input_dataframe_and_ncfile(dataframe, ncfile):
"""
Run grdtrack by passing in a pandas.DataFrame and netcdf file as inputs.
"""

output = grdtrack(points=dataframe, grid=ncfile, newcolname="bathymetry")
assert isinstance(output, pd.DataFrame)
assert output.columns.to_list() == ["longitude", "latitude", "bathymetry"]
npt.assert_allclose(output.iloc[0], [-32.2971, 37.4118, -1939.748245])

return output


def test_grdtrack_input_csvfile_and_ncfile(csvfile, ncfile):
"""
Run grdtrack by passing in a csvfile and netcdf file as inputs.
"""
try:
output = grdtrack(points=csvfile, grid=ncfile, outfile=TEMP_TRACK)
assert output is None # check that output is None since outfile is set
assert os.path.exists(path=TEMP_TRACK) # check that outfile exists at path

track = pd.read_csv(TEMP_TRACK, sep="\t", header=None, comment=">")
npt.assert_allclose(track.iloc[0], [-32.2971, 37.4118, -1939.748245])
finally:
os.remove(path=TEMP_TRACK)
maxrjones marked this conversation as resolved.
Show resolved Hide resolved
npt.assert_allclose(np.array(output), expected_array)

return output

Expand Down Expand Up @@ -145,11 +114,9 @@ def test_grdtrack_without_newcolname_setting(dataarray, dataframe):
grdtrack(points=dataframe, grid=dataarray)


def test_grdtrack_without_outfile_setting(csvfile, ncfile):
def test_grdtrack_without_outfile_setting(dataarray, dataframe):
"""
Run grdtrack by not passing in outfile parameter setting.
"""
output = grdtrack(points=csvfile, grid=ncfile)
npt.assert_allclose(output.iloc[0], [-32.2971, 37.4118, -1939.748245])

return output
with pytest.raises(GMTInvalidInput):
grdtrack(points=dataframe, grid=dataarray)