|
4 | 4 | import os
|
5 | 5 |
|
6 | 6 | import pytest
|
7 |
| -from pygmt import grdinfo, grdlandmask |
| 7 | +import xarray as xr |
| 8 | +from pygmt import grdlandmask, load_dataarray |
8 | 9 | from pygmt.exceptions import GMTInvalidInput
|
9 | 10 | from pygmt.helpers import GMTTempFile
|
10 | 11 |
|
11 | 12 |
|
12 |
| -def test_grdlandmask_outgrid(): |
| 13 | +@pytest.fixture(scope="module", name="expected_grid") |
| 14 | +def fixture_grid_result(): |
| 15 | + """ |
| 16 | + Load the expected grdlandmask grid result. |
| 17 | + """ |
| 18 | + return xr.DataArray( |
| 19 | + data=[ |
| 20 | + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], |
| 21 | + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], |
| 22 | + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], |
| 23 | + [0.0, 0.0, 0.0, 0.0, 0.0, 1.0], |
| 24 | + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], |
| 25 | + [0.0, 0.0, 1.0, 1.0, 0.0, 0.0], |
| 26 | + ], |
| 27 | + coords=dict( |
| 28 | + lon=[125.0, 126.0, 127.0, 128.0, 129.0, 130.0], |
| 29 | + lat=[30.0, 31.0, 32.0, 33.0, 34.0, 35.0], |
| 30 | + ), |
| 31 | + dims=["lat", "lon"], |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def test_grdlandmask_outgrid(expected_grid): |
13 | 36 | """
|
14 | 37 | Creates a grid land mask with an outgrid argument.
|
15 | 38 | """
|
16 | 39 | with GMTTempFile(suffix=".nc") as tmpfile:
|
17 |
| - result = grdlandmask(outgrid=tmpfile.name, spacing=1, region=[-5, 5, -5, 5]) |
| 40 | + result = grdlandmask(outgrid=tmpfile.name, spacing=1, region=[125, 130, 30, 35]) |
18 | 41 | assert result is None # return value is None
|
19 | 42 | assert os.path.exists(path=tmpfile.name) # check that outgrid exists
|
20 |
| - result = ( |
21 |
| - grdinfo(grid=tmpfile.name, force_scan=0, per_column="n").strip().split() |
22 |
| - ) |
23 |
| - assert result == ["-5", "5", "-5", "5", "0", "1", "1", "1", "11", "11", "0", "1"] |
| 43 | + temp_grid = load_dataarray(tmpfile.name) |
| 44 | + xr.testing.assert_allclose(a=temp_grid, b=expected_grid) |
24 | 45 |
|
25 | 46 |
|
26 |
| -def test_grdlandmask_no_outgrid(): |
| 47 | +def test_grdlandmask_no_outgrid(expected_grid): |
27 | 48 | """
|
28 | 49 | Test grdlandmask with no set outgrid.
|
29 | 50 | """
|
30 |
| - temp_grid = grdlandmask(spacing=1, region=[-5, 5, -5, 5]) |
31 |
| - assert temp_grid.dims == ("lat", "lon") |
32 |
| - assert temp_grid.gmt.gtype == 1 # Geographic grid |
33 |
| - assert temp_grid.gmt.registration == 0 # Pixel registration |
34 |
| - assert temp_grid.min() == 0 |
35 |
| - assert temp_grid.max() == 1 |
| 51 | + result = grdlandmask(spacing=1, region=[125, 130, 30, 35]) |
| 52 | + # check information of the output grid |
| 53 | + assert isinstance(result, xr.DataArray) |
| 54 | + assert result.gmt.gtype == 1 # Geographic grid |
| 55 | + assert result.gmt.registration == 0 # Gridline registration |
| 56 | + # check information of the output grid |
| 57 | + xr.testing.assert_allclose(a=result, b=expected_grid) |
36 | 58 |
|
37 | 59 |
|
38 | 60 | def test_grdlandmask_fails():
|
|
0 commit comments