|
| 1 | +""" |
| 2 | +Tests for xyz2grd. |
| 3 | +""" |
| 4 | +import os |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import pytest |
| 8 | +import xarray as xr |
| 9 | +from pygmt import grdinfo, xyz2grd |
| 10 | +from pygmt.datasets import load_sample_bathymetry |
| 11 | +from pygmt.helpers import GMTTempFile |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(scope="module", name="ship_data") |
| 15 | +def fixture_ship_data(): |
| 16 | + """ |
| 17 | + Load the data from the sample bathymetry dataset. |
| 18 | + """ |
| 19 | + return load_sample_bathymetry() |
| 20 | + |
| 21 | + |
| 22 | +def test_xyz2grd_input_file(): |
| 23 | + """ |
| 24 | + Run xyz2grd by passing in a filename. |
| 25 | + """ |
| 26 | + output = xyz2grd(table="@tut_ship.xyz", spacing=5, region=[245, 255, 20, 30]) |
| 27 | + assert isinstance(output, xr.DataArray) |
| 28 | + assert output.gmt.registration == 0 # Gridline registration |
| 29 | + assert output.gmt.gtype == 0 # Cartesian type |
| 30 | + return output |
| 31 | + |
| 32 | + |
| 33 | +def test_xyz2grd_input_array(ship_data): |
| 34 | + """ |
| 35 | + Run xyz2grd by passing in a numpy array. |
| 36 | + """ |
| 37 | + output = xyz2grd(table=np.array(ship_data), spacing=5, region=[245, 255, 20, 30]) |
| 38 | + assert isinstance(output, xr.DataArray) |
| 39 | + assert output.gmt.registration == 0 # Gridline registration |
| 40 | + assert output.gmt.gtype == 0 # Cartesian type |
| 41 | + return output |
| 42 | + |
| 43 | + |
| 44 | +def test_xyz2grd_input_df(ship_data): |
| 45 | + """ |
| 46 | + Run xyz2grd by passing in a data frame. |
| 47 | + """ |
| 48 | + output = xyz2grd(table=ship_data, spacing=5, region=[245, 255, 20, 30]) |
| 49 | + assert isinstance(output, xr.DataArray) |
| 50 | + assert output.gmt.registration == 0 # Gridline registration |
| 51 | + assert output.gmt.gtype == 0 # Cartesian type |
| 52 | + return output |
| 53 | + |
| 54 | + |
| 55 | +def test_xyz2grd_input_array_file_out(ship_data): |
| 56 | + """ |
| 57 | + Run xyz2grd by passing in a numpy array and set an outgrid file. |
| 58 | + """ |
| 59 | + with GMTTempFile(suffix=".nc") as tmpfile: |
| 60 | + result = xyz2grd( |
| 61 | + table=np.array(ship_data), |
| 62 | + spacing=5, |
| 63 | + region=[245, 255, 20, 30], |
| 64 | + outgrid=tmpfile.name, |
| 65 | + ) |
| 66 | + assert result is None # return value is None |
| 67 | + assert os.path.exists(path=tmpfile.name) |
| 68 | + result = grdinfo(tmpfile.name, per_column=True).strip() |
| 69 | + assert result == "245 255 20 30 -3651.06079102 -352.379486084 5 5 3 3 0 0" |
0 commit comments