|
3 | 3 | """ |
4 | 4 | import os |
5 | 5 |
|
6 | | -import numpy.testing as npt |
7 | 6 | import pytest |
8 | | -from pygmt import grdinfo, grdproject |
9 | | -from pygmt.datasets import load_earth_relief |
| 7 | +import xarray as xr |
| 8 | +from pygmt import grdproject, 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_grdproject_file_out(grid): |
| 22 | +@pytest.fixture(scope="module", name="expected_grid") |
| 23 | +def fixture_grid_result(): |
| 24 | + """ |
| 25 | + Load the expected grdproject grid result. |
| 26 | + """ |
| 27 | + return xr.DataArray( |
| 28 | + data=[ |
| 29 | + [427.85062, 431.05698, 452.34268], |
| 30 | + [563.92957, 540.5212, 501.46896], |
| 31 | + [740.80133, 679.1116, 554.78534], |
| 32 | + [794.233, 829.4449, 764.12225], |
| 33 | + [749.37445, 834.55994, 831.2627], |
| 34 | + ], |
| 35 | + coords=dict( |
| 36 | + x=[1.666667, 5.0, 8.333333], |
| 37 | + y=[1.572432, 4.717295, 7.862158, 11.007022, 14.151885], |
| 38 | + ), |
| 39 | + dims=["y", "x"], |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def test_grdproject_file_out(grid, expected_grid): |
23 | 44 | """ |
24 | 45 | grdproject with an outgrid set. |
25 | 46 | """ |
26 | 47 | with GMTTempFile(suffix=".nc") as tmpfile: |
27 | | - result = grdproject(grid=grid, projection="M10c", outgrid=tmpfile.name) |
| 48 | + result = grdproject( |
| 49 | + grid=grid, |
| 50 | + projection="M10c", |
| 51 | + outgrid=tmpfile.name, |
| 52 | + spacing=3, |
| 53 | + region=[-53, -51, -20, -17], |
| 54 | + ) |
28 | 55 | assert result is None # return value is None |
29 | 56 | assert os.path.exists(path=tmpfile.name) # check that outgrid exists |
30 | | - result = grdinfo(tmpfile.name, per_column=True).strip().split() |
31 | | - npt.assert_allclose(float(result[0]), 0) # x min |
32 | | - npt.assert_allclose(float(result[1]), 10) # x max |
33 | | - npt.assert_allclose(float(result[2]), 0, atol=1.0e-10) # y min |
34 | | - npt.assert_allclose(float(result[3]), 9.94585661273) # y max |
35 | | - npt.assert_allclose(float(result[4]), -5130.48193359) # min |
36 | | - npt.assert_allclose(float(result[5]), -152.585281372) # max |
| 57 | + temp_grid = load_dataarray(tmpfile.name) |
| 58 | + xr.testing.assert_allclose(a=temp_grid, b=expected_grid) |
37 | 59 |
|
38 | 60 |
|
39 | | -def test_grdproject_no_outgrid(grid): |
| 61 | +def test_grdproject_no_outgrid(grid, expected_grid): |
40 | 62 | """ |
41 | 63 | Test grdproject with no set outgrid. |
42 | 64 | """ |
43 | 65 | assert grid.gmt.gtype == 1 # Geographic grid |
44 | | - temp_grid = grdproject(grid=grid, projection="M10c") |
45 | | - assert temp_grid.dims == ("y", "x") |
46 | | - assert temp_grid.gmt.gtype == 0 # Rectangular grid |
47 | | - assert temp_grid.gmt.registration == 1 # Pixel registration |
48 | | - npt.assert_allclose(temp_grid.min(), -5130.482) |
49 | | - npt.assert_allclose(temp_grid.max(), -152.58528) |
50 | | - npt.assert_allclose(temp_grid.median(), -4578.288) |
51 | | - npt.assert_allclose(temp_grid.mean(), -4354.3296) |
| 66 | + result = grdproject( |
| 67 | + grid=grid, projection="M10c", spacing=3, region=[-53, -51, -20, -17] |
| 68 | + ) |
| 69 | + assert result.gmt.gtype == 0 # Rectangular grid |
| 70 | + assert result.gmt.registration == 1 # Pixel registration |
| 71 | + # check information of the output grid |
| 72 | + xr.testing.assert_allclose(a=result, b=expected_grid) |
52 | 73 |
|
53 | 74 |
|
54 | 75 | def test_grdproject_fails(grid): |
|
0 commit comments