Skip to content

Commit c6036c4

Browse files
willschlitzerMeghan Jones
andauthored
Change test_grdtrack.py to use static_earth_relief (#1762)
Co-authored-by: Meghan Jones <[email protected]>
1 parent 02b770e commit c6036c4

File tree

2 files changed

+62
-60
lines changed

2 files changed

+62
-60
lines changed

pygmt/tests/data/track.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-51.613 -17.93
2+
-48.917 -22.434
3+
-50.444 -16.358
4+
-50.721 -16.628
5+
-51.394 -12.196
6+
-50.207 -18.404
7+
-52.56 -16.977
8+
-51.866 -19.794
9+
-48.001 -14.144
10+
-54.438 -19.193

pygmt/tests/test_grdtrack.py

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,110 +3,104 @@
33
"""
44
import os
55

6+
import numpy as np
67
import numpy.testing as npt
78
import pandas as pd
89
import pytest
9-
from pygmt import grdtrack, which
10-
from pygmt.datasets import load_earth_relief, load_sample_data
10+
from pygmt import grdtrack
1111
from pygmt.exceptions import GMTInvalidInput
12-
from pygmt.helpers import data_kind
12+
from pygmt.helpers import GMTTempFile, data_kind
13+
from pygmt.helpers.testing import load_static_earth_relief
1314

1415
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
15-
TEMP_TRACK = os.path.join(TEST_DATA_DIR, "tmp_track.txt")
16+
POINTS_DATA = os.path.join(TEST_DATA_DIR, "track.txt")
1617

1718

1819
@pytest.fixture(scope="module", name="dataarray")
1920
def fixture_dataarray():
2021
"""
2122
Load the grid data from the sample earth_relief file.
2223
"""
23-
return load_earth_relief(registration="gridline").sel(
24-
lat=slice(-49, -42), lon=slice(-118, -107)
25-
)
24+
return load_static_earth_relief()
2625

2726

28-
@pytest.fixture(scope="module", name="dataframe")
29-
def fixture_dataframe():
27+
@pytest.fixture(scope="module", name="expected_array")
28+
def fixture_numpy_array():
3029
"""
31-
Load the ocean ridge file.
30+
Load a numpy array with x, y, and bathymetry data.
3231
"""
33-
return load_sample_data(name="ocean_ridge_points")
32+
array = [
33+
[-51.613, -17.93, 796.59434514],
34+
[-48.917, -22.434, 566.49184359],
35+
[-50.444, -16.358, 571.1492788],
36+
[-50.721, -16.628, 578.76116859],
37+
[-51.394, -12.196, 274.43205501],
38+
[-50.207, -18.404, 532.11444935],
39+
[-52.56, -16.977, 670.16934401],
40+
[-51.866, -19.794, 426.77300768],
41+
[-48.001, -14.144, 741.35824074],
42+
[-54.438, -19.193, 490.02716679],
43+
]
44+
return array
3445

3546

36-
@pytest.fixture(scope="module", name="csvfile")
37-
def fixture_csvfile():
38-
"""
39-
Load the csvfile.
40-
"""
41-
return which("@ridge.txt", download="c")
42-
43-
44-
@pytest.fixture(scope="module", name="ncfile")
45-
def fixture_ncfile():
47+
@pytest.fixture(scope="module", name="dataframe")
48+
def fixture_dataframe():
4649
"""
47-
Load the ncfile.
50+
Load a pandas DataFrame with points.
4851
"""
49-
return which("@earth_relief_01d", download="a")
52+
return pd.read_csv(
53+
POINTS_DATA, sep=r"\s+", header=None, names=["longitude", "latitude"]
54+
)
5055

5156

52-
def test_grdtrack_input_dataframe_and_dataarray(dataarray, dataframe):
57+
def test_grdtrack_input_dataframe_and_dataarray(dataarray, dataframe, expected_array):
5358
"""
5459
Run grdtrack by passing in a pandas.DataFrame and xarray.DataArray as
5560
inputs.
5661
"""
5762
output = grdtrack(points=dataframe, grid=dataarray, newcolname="bathymetry")
5863
assert isinstance(output, pd.DataFrame)
5964
assert output.columns.to_list() == ["longitude", "latitude", "bathymetry"]
60-
npt.assert_allclose(output.iloc[0], [-110.9536, -42.2489, -2797.394987])
61-
62-
return output
65+
npt.assert_allclose(np.array(output), expected_array)
6366

6467

65-
def test_grdtrack_input_csvfile_and_dataarray(dataarray, csvfile):
68+
def test_grdtrack_input_csvfile_and_dataarray(dataarray, expected_array):
6669
"""
6770
Run grdtrack by passing in a csvfile and xarray.DataArray as inputs.
6871
"""
69-
try:
70-
output = grdtrack(points=csvfile, grid=dataarray, outfile=TEMP_TRACK)
72+
with GMTTempFile() as tmpfile:
73+
output = grdtrack(points=POINTS_DATA, grid=dataarray, outfile=tmpfile.name)
7174
assert output is None # check that output is None since outfile is set
72-
assert os.path.exists(path=TEMP_TRACK) # check that outfile exists at path
75+
assert os.path.exists(path=tmpfile.name) # check that outfile exists at path
76+
output = np.loadtxt(tmpfile.name)
77+
npt.assert_allclose(np.array(output), expected_array)
7378

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

79-
return output
80-
81-
82-
def test_grdtrack_input_dataframe_and_ncfile(dataframe, ncfile):
80+
def test_grdtrack_input_dataframe_and_ncfile(dataframe, expected_array):
8381
"""
8482
Run grdtrack by passing in a pandas.DataFrame and netcdf file as inputs.
8583
"""
86-
87-
output = grdtrack(points=dataframe, grid=ncfile, newcolname="bathymetry")
84+
output = grdtrack(
85+
points=dataframe, grid="@static_earth_relief.nc", newcolname="bathymetry"
86+
)
8887
assert isinstance(output, pd.DataFrame)
8988
assert output.columns.to_list() == ["longitude", "latitude", "bathymetry"]
90-
npt.assert_allclose(output.iloc[0], [-32.2971, 37.4118, -1939.748245])
91-
92-
return output
89+
npt.assert_allclose(np.array(output), expected_array)
9390

9491

95-
def test_grdtrack_input_csvfile_and_ncfile(csvfile, ncfile):
92+
def test_grdtrack_input_csvfile_and_ncfile(expected_array):
9693
"""
9794
Run grdtrack by passing in a csvfile and netcdf file as inputs.
9895
"""
99-
try:
100-
output = grdtrack(points=csvfile, grid=ncfile, outfile=TEMP_TRACK)
96+
with GMTTempFile() as tmpfile:
97+
output = grdtrack(
98+
points=POINTS_DATA, grid="@static_earth_relief.nc", outfile=tmpfile.name
99+
)
101100
assert output is None # check that output is None since outfile is set
102-
assert os.path.exists(path=TEMP_TRACK) # check that outfile exists at path
103-
104-
track = pd.read_csv(TEMP_TRACK, sep="\t", header=None, comment=">")
105-
npt.assert_allclose(track.iloc[0], [-32.2971, 37.4118, -1939.748245])
106-
finally:
107-
os.remove(path=TEMP_TRACK)
108-
109-
return output
101+
assert os.path.exists(path=tmpfile.name) # check that outfile exists at path
102+
output = np.loadtxt(tmpfile.name)
103+
npt.assert_allclose(np.array(output), expected_array)
110104

111105

112106
def test_grdtrack_wrong_kind_of_points_input(dataarray, dataframe):
@@ -141,11 +135,9 @@ def test_grdtrack_without_newcolname_setting(dataarray, dataframe):
141135
grdtrack(points=dataframe, grid=dataarray)
142136

143137

144-
def test_grdtrack_without_outfile_setting(csvfile, ncfile):
138+
def test_grdtrack_without_outfile_setting(dataarray, dataframe):
145139
"""
146140
Run grdtrack by not passing in outfile parameter setting.
147141
"""
148-
output = grdtrack(points=csvfile, grid=ncfile)
149-
npt.assert_allclose(output.iloc[0], [-32.2971, 37.4118, -1939.748245])
150-
151-
return output
142+
with pytest.raises(GMTInvalidInput):
143+
grdtrack(points=dataframe, grid=dataarray)

0 commit comments

Comments
 (0)