|
14 | 14 | from pygmt.datasets import load_earth_relief
|
15 | 15 | from pygmt.enums import GridRegistration, GridType
|
16 | 16 | from pygmt.exceptions import GMTValueError
|
| 17 | +from pygmt.helpers.testing import load_static_earth_relief |
17 | 18 |
|
18 | 19 | _HAS_NETCDF4 = bool(importlib.util.find_spec("netCDF4"))
|
19 | 20 |
|
20 | 21 |
|
| 22 | +@pytest.fixture(scope="module", name="grid") |
| 23 | +def fixture_grid(): |
| 24 | + """ |
| 25 | + Load the grid data from the sample earth_relief file. |
| 26 | + """ |
| 27 | + return load_static_earth_relief() |
| 28 | + |
| 29 | + |
21 | 30 | def test_xarray_accessor_gridline_cartesian():
|
22 | 31 | """
|
23 | 32 | Check that the accessor returns the correct registration and gtype values for a
|
@@ -169,3 +178,44 @@ def test_xarray_accessor_tiled_grid_slice_and_add():
|
169 | 178 | added_grid.gmt.gtype = GridType.GEOGRAPHIC
|
170 | 179 | assert added_grid.gmt.registration is GridRegistration.PIXEL
|
171 | 180 | assert added_grid.gmt.gtype is GridType.GEOGRAPHIC
|
| 181 | + |
| 182 | + |
| 183 | +def test_xarray_accessor_clip(grid): |
| 184 | + """ |
| 185 | + Check that the accessor has the clip method and that it works correctly. |
| 186 | +
|
| 187 | + This test is adapted from the `test_grdclip_no_outgrid` test. |
| 188 | + """ |
| 189 | + clipped_grid = grid.gmt.clip( |
| 190 | + below=[550, -1000], above=[700, 1000], region=[-53, -49, -19, -16] |
| 191 | + ) |
| 192 | + |
| 193 | + expected_clipped_grid = xr.DataArray( |
| 194 | + data=[ |
| 195 | + [1000.0, 570.5, -1000.0, -1000.0], |
| 196 | + [1000.0, 1000.0, 571.5, 638.5], |
| 197 | + [555.5, 556.0, 580.0, 1000.0], |
| 198 | + ], |
| 199 | + coords={"lon": [-52.5, -51.5, -50.5, -49.5], "lat": [-18.5, -17.5, -16.5]}, |
| 200 | + dims=["lat", "lon"], |
| 201 | + ) |
| 202 | + xr.testing.assert_allclose(a=clipped_grid, b=expected_clipped_grid) |
| 203 | + |
| 204 | + |
| 205 | +def test_xarray_accessor_histeq(grid): |
| 206 | + """ |
| 207 | + Check that the accessor has the histeq method and that it works correctly. |
| 208 | +
|
| 209 | + This test is adapted from the `test_equalize_grid_no_outgrid` test. |
| 210 | + """ |
| 211 | + equalized_grid = grid.gmt.histeq(divisions=2, region=[-52, -48, -22, -18]) |
| 212 | + |
| 213 | + expected_equalized_grid = xr.DataArray( |
| 214 | + data=[[0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 1, 1], [1, 1, 1, 1]], |
| 215 | + coords={ |
| 216 | + "lon": [-51.5, -50.5, -49.5, -48.5], |
| 217 | + "lat": [-21.5, -20.5, -19.5, -18.5], |
| 218 | + }, |
| 219 | + dims=["lat", "lon"], |
| 220 | + ) |
| 221 | + xr.testing.assert_allclose(a=equalized_grid, b=expected_equalized_grid) |
0 commit comments