|
3 | 3 | """
|
4 | 4 | import os
|
5 | 5 |
|
6 |
| -import numpy.testing as npt |
7 | 6 | import pytest
|
8 |
| -from pygmt import grdgradient, grdinfo |
9 |
| -from pygmt.datasets import load_earth_relief |
| 7 | +import xarray as xr |
| 8 | +from pygmt import grdgradient, load_dataarray |
10 | 9 | from pygmt.exceptions import GMTInvalidInput
|
11 | 10 | from pygmt.helpers import GMTTempFile
|
| 11 | +from pygmt.helpers.testing import load_static_earth_relief |
12 | 12 |
|
13 | 13 |
|
14 | 14 | @pytest.fixture(scope="module", name="grid")
|
15 | 15 | def fixture_grid():
|
16 | 16 | """
|
17 |
| - Load the grid data from the sample earth_relief file. |
| 17 | + Load the grid data from the static_earth_relief file. |
18 | 18 | """
|
19 |
| - return load_earth_relief(resolution="01d", region=[-5, 5, -5, 5]) |
| 19 | + return load_static_earth_relief() |
20 | 20 |
|
21 | 21 |
|
22 |
| -def test_grdgradient_outgrid(grid): |
| 22 | +@pytest.fixture(scope="module", name="expected_grid") |
| 23 | +def fixture_grid_result(): |
| 24 | + """ |
| 25 | + Load the expected grdgradient grid result. |
| 26 | + """ |
| 27 | + return xr.DataArray( |
| 28 | + data=[ |
| 29 | + [-1.5974800e-03, -9.9056680e-04, -6.1276241e-04, -3.6172546e-04], |
| 30 | + [-1.5880326e-03, -1.6113354e-03, -5.4624723e-04, -5.0047837e-04], |
| 31 | + [7.2569086e-04, 2.4801277e-04, 1.8859128e-05, -1.2269041e-03], |
| 32 | + ], |
| 33 | + coords=dict( |
| 34 | + lon=[-52.5, -51.5, -50.5, -49.5], |
| 35 | + lat=[-19.5, -18.5, -17.5], |
| 36 | + ), |
| 37 | + dims=["lat", "lon"], |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def test_grdgradient_outgrid(grid, expected_grid): |
23 | 42 | """
|
24 | 43 | Test the azimuth and direction parameters for grdgradient with a set
|
25 | 44 | outgrid.
|
26 | 45 | """
|
27 | 46 | with GMTTempFile(suffix=".nc") as tmpfile:
|
28 |
| - result = grdgradient(grid=grid, outgrid=tmpfile.name, azimuth=10, direction="c") |
| 47 | + result = grdgradient( |
| 48 | + grid=grid, outgrid=tmpfile.name, azimuth=10, region=[-53, -49, -20, -17] |
| 49 | + ) |
29 | 50 | assert result is None # return value is None
|
30 | 51 | assert os.path.exists(path=tmpfile.name) # check that outgrid exists
|
31 |
| - result = ( |
32 |
| - grdinfo(grid=tmpfile.name, force_scan="a", per_column="n").strip().split() |
33 |
| - ) |
34 |
| - npt.assert_allclose(float(result[4]), -0.0045060496) # min |
35 |
| - npt.assert_allclose(float(result[5]), 0.0575332976) # max |
36 |
| - # Check spherically weighted statistics below |
37 |
| - npt.assert_allclose(float(result[10]), 0.000384754501283) # median |
38 |
| - npt.assert_allclose(float(result[12]), 0.00285958005568) # mean |
| 52 | + temp_grid = load_dataarray(tmpfile.name) |
| 53 | + xr.testing.assert_allclose(a=temp_grid, b=expected_grid) |
39 | 54 |
|
40 | 55 |
|
41 |
| -def test_grdgradient_no_outgrid(grid): |
| 56 | +def test_grdgradient_no_outgrid(grid, expected_grid): |
42 | 57 | """
|
43 | 58 | Test the azimuth and direction parameters for grdgradient with no set
|
44 | 59 | outgrid.
|
45 | 60 | """
|
46 |
| - temp_grid = grdgradient(grid=grid, azimuth=10, direction="c") |
47 |
| - assert temp_grid.dims == ("lat", "lon") |
48 |
| - assert temp_grid.gmt.gtype == 1 # Geographic grid |
49 |
| - assert temp_grid.gmt.registration == 1 # Pixel registration |
50 |
| - npt.assert_allclose(temp_grid.min(), -0.0045060496) |
51 |
| - npt.assert_allclose(temp_grid.max(), 0.0575332976) |
52 |
| - npt.assert_allclose(temp_grid.median(), 0.0004889865522272885) |
53 |
| - npt.assert_allclose(temp_grid.mean(), 0.0028633063193410635) |
| 61 | + result = grdgradient(grid=grid, azimuth=10, region=[-53, -49, -20, -17]) |
| 62 | + # check information of the output grid |
| 63 | + assert isinstance(result, xr.DataArray) |
| 64 | + assert result.gmt.gtype == 1 # Geographic grid |
| 65 | + assert result.gmt.registration == 1 # Pixel registration |
| 66 | + # check information of the output grid |
| 67 | + xr.testing.assert_allclose(a=result, b=expected_grid) |
54 | 68 |
|
55 | 69 |
|
56 | 70 | def test_grdgradient_fails(grid):
|
|
0 commit comments