Skip to content

Commit

Permalink
Change test_grdtrack.py to use static_earth_relief (GenericMappingToo…
Browse files Browse the repository at this point in the history
…ls#1762)

Co-authored-by: Meghan Jones <[email protected]>
  • Loading branch information
2 people authored and Josh Sixsmith committed Dec 21, 2022
1 parent 91873b8 commit 88e0c00
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 60 deletions.
10 changes: 10 additions & 0 deletions pygmt/tests/data/track.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-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
112 changes: 52 additions & 60 deletions pygmt/tests/test_grdtrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,110 +3,104 @@
"""
import os

import numpy as np
import numpy.testing as npt
import pandas as pd
import pytest
from pygmt import grdtrack, which
from pygmt.datasets import load_earth_relief, load_sample_data
from pygmt import grdtrack
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import data_kind
from pygmt.helpers import GMTTempFile, 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")
POINTS_DATA = os.path.join(TEST_DATA_DIR, "track.txt")


@pytest.fixture(scope="module", name="dataarray")
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():
@pytest.fixture(scope="module", name="expected_array")
def fixture_numpy_array():
"""
Load the ocean ridge file.
Load a numpy array with x, y, and bathymetry data.
"""
return load_sample_data(name="ocean_ridge_points")
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="csvfile")
def fixture_csvfile():
"""
Load the csvfile.
"""
return which("@ridge.txt", download="c")


@pytest.fixture(scope="module", name="ncfile")
def fixture_ncfile():
@pytest.fixture(scope="module", name="dataframe")
def fixture_dataframe():
"""
Load the ncfile.
Load a pandas DataFrame with points.
"""
return which("@earth_relief_01d", download="a")
return pd.read_csv(
POINTS_DATA, sep=r"\s+", header=None, names=["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
npt.assert_allclose(np.array(output), expected_array)


def test_grdtrack_input_csvfile_and_dataarray(dataarray, csvfile):
def test_grdtrack_input_csvfile_and_dataarray(dataarray, expected_array):
"""
Run grdtrack by passing in a csvfile and xarray.DataArray as inputs.
"""
try:
output = grdtrack(points=csvfile, grid=dataarray, outfile=TEMP_TRACK)
with GMTTempFile() as tmpfile:
output = grdtrack(points=POINTS_DATA, grid=dataarray, outfile=tmpfile.name)
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
assert os.path.exists(path=tmpfile.name) # check that outfile exists at path
output = np.loadtxt(tmpfile.name)
npt.assert_allclose(np.array(output), expected_array)

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):
def test_grdtrack_input_dataframe_and_ncfile(dataframe, expected_array):
"""
Run grdtrack by passing in a pandas.DataFrame and netcdf file as inputs.
"""

output = grdtrack(points=dataframe, grid=ncfile, newcolname="bathymetry")
output = grdtrack(
points=dataframe, grid="@static_earth_relief.nc", 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
npt.assert_allclose(np.array(output), expected_array)


def test_grdtrack_input_csvfile_and_ncfile(csvfile, ncfile):
def test_grdtrack_input_csvfile_and_ncfile(expected_array):
"""
Run grdtrack by passing in a csvfile and netcdf file as inputs.
"""
try:
output = grdtrack(points=csvfile, grid=ncfile, outfile=TEMP_TRACK)
with GMTTempFile() as tmpfile:
output = grdtrack(
points=POINTS_DATA, grid="@static_earth_relief.nc", outfile=tmpfile.name
)
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)

return output
assert os.path.exists(path=tmpfile.name) # check that outfile exists at path
output = np.loadtxt(tmpfile.name)
npt.assert_allclose(np.array(output), expected_array)


def test_grdtrack_wrong_kind_of_points_input(dataarray, dataframe):
Expand Down Expand Up @@ -141,11 +135,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)

0 comments on commit 88e0c00

Please sign in to comment.