|
3 | 3 | """ |
4 | 4 | import os |
5 | 5 |
|
| 6 | +import numpy as np |
6 | 7 | import numpy.testing as npt |
7 | 8 | import pandas as pd |
8 | 9 | import pytest |
9 | | -from pygmt import grdtrack, which |
10 | | -from pygmt.datasets import load_earth_relief, load_sample_data |
| 10 | +from pygmt import grdtrack |
11 | 11 | 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 |
13 | 14 |
|
14 | 15 | 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") |
16 | 17 |
|
17 | 18 |
|
18 | 19 | @pytest.fixture(scope="module", name="dataarray") |
19 | 20 | def fixture_dataarray(): |
20 | 21 | """ |
21 | 22 | Load the grid data from the sample earth_relief file. |
22 | 23 | """ |
23 | | - return load_earth_relief(registration="gridline").sel( |
24 | | - lat=slice(-49, -42), lon=slice(-118, -107) |
25 | | - ) |
| 24 | + return load_static_earth_relief() |
26 | 25 |
|
27 | 26 |
|
28 | | -@pytest.fixture(scope="module", name="dataframe") |
29 | | -def fixture_dataframe(): |
| 27 | +@pytest.fixture(scope="module", name="expected_array") |
| 28 | +def fixture_numpy_array(): |
30 | 29 | """ |
31 | | - Load the ocean ridge file. |
| 30 | + Load a numpy array with x, y, and bathymetry data. |
32 | 31 | """ |
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 |
34 | 45 |
|
35 | 46 |
|
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(): |
46 | 49 | """ |
47 | | - Load the ncfile. |
| 50 | + Load a pandas DataFrame with points. |
48 | 51 | """ |
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 | + ) |
50 | 55 |
|
51 | 56 |
|
52 | | -def test_grdtrack_input_dataframe_and_dataarray(dataarray, dataframe): |
| 57 | +def test_grdtrack_input_dataframe_and_dataarray(dataarray, dataframe, expected_array): |
53 | 58 | """ |
54 | 59 | Run grdtrack by passing in a pandas.DataFrame and xarray.DataArray as |
55 | 60 | inputs. |
56 | 61 | """ |
57 | 62 | output = grdtrack(points=dataframe, grid=dataarray, newcolname="bathymetry") |
58 | 63 | assert isinstance(output, pd.DataFrame) |
59 | 64 | 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) |
63 | 66 |
|
64 | 67 |
|
65 | | -def test_grdtrack_input_csvfile_and_dataarray(dataarray, csvfile): |
| 68 | +def test_grdtrack_input_csvfile_and_dataarray(dataarray, expected_array): |
66 | 69 | """ |
67 | 70 | Run grdtrack by passing in a csvfile and xarray.DataArray as inputs. |
68 | 71 | """ |
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) |
71 | 74 | 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) |
73 | 78 |
|
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) |
78 | 79 |
|
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): |
83 | 81 | """ |
84 | 82 | Run grdtrack by passing in a pandas.DataFrame and netcdf file as inputs. |
85 | 83 | """ |
86 | | - |
87 | | - output = grdtrack(points=dataframe, grid=ncfile, newcolname="bathymetry") |
| 84 | + output = grdtrack( |
| 85 | + points=dataframe, grid="@static_earth_relief.nc", newcolname="bathymetry" |
| 86 | + ) |
88 | 87 | assert isinstance(output, pd.DataFrame) |
89 | 88 | 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) |
93 | 90 |
|
94 | 91 |
|
95 | | -def test_grdtrack_input_csvfile_and_ncfile(csvfile, ncfile): |
| 92 | +def test_grdtrack_input_csvfile_and_ncfile(expected_array): |
96 | 93 | """ |
97 | 94 | Run grdtrack by passing in a csvfile and netcdf file as inputs. |
98 | 95 | """ |
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 | + ) |
101 | 100 | 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) |
110 | 104 |
|
111 | 105 |
|
112 | 106 | def test_grdtrack_wrong_kind_of_points_input(dataarray, dataframe): |
@@ -141,11 +135,9 @@ def test_grdtrack_without_newcolname_setting(dataarray, dataframe): |
141 | 135 | grdtrack(points=dataframe, grid=dataarray) |
142 | 136 |
|
143 | 137 |
|
144 | | -def test_grdtrack_without_outfile_setting(csvfile, ncfile): |
| 138 | +def test_grdtrack_without_outfile_setting(dataarray, dataframe): |
145 | 139 | """ |
146 | 140 | Run grdtrack by not passing in outfile parameter setting. |
147 | 141 | """ |
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