-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap the `grdcut` function. Ideally it should take a grid file or a `xarray.DataArray` as input, and store output as a grid file or return a `xarray.DataArray`. Supported features: - [X] input: grid file; output: grid file - [X] input: grid file; output: `xarray.DataArray` - [ ] input: `xarray.DataArray`; output: grid file - [ ] input: `xarray.DataArray`; output: `xarray.DataArray` Passing `xarray.DataArray` to `grdcut` is not implenmented in the GMT 6.0.0. See GenericMappingTools/gmt#3532 for a feature request to the upstream GMT.
- Loading branch information
Showing
4 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,7 @@ Operations on grids: | |
|
||
grdinfo | ||
grdtrack | ||
grdcut | ||
|
||
GMT Defaults | ||
------------ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
Tests for grdcut | ||
""" | ||
import os | ||
import numpy as np | ||
import pytest | ||
import xarray as xr | ||
|
||
from .. import grdcut, grdinfo | ||
from ..datasets import load_earth_relief | ||
from ..exceptions import GMTInvalidInput | ||
from ..helpers import GMTTempFile | ||
|
||
|
||
def test_grdcut_file_in_file_out(): | ||
"grduct an input grid file, and output to a grid file" | ||
with GMTTempFile(suffix=".nc") as tmpfile: | ||
result = grdcut("@earth_relief_01d", outgrid=tmpfile.name, region="0/180/0/90") | ||
assert result is None # return value is None | ||
assert os.path.exists(path=tmpfile.name) # check that outgrid exists | ||
result = grdinfo(tmpfile.name, C=True) | ||
assert result == "0 180 0 90 -8182 5651.5 1 1 180 90 1\n" | ||
|
||
|
||
def test_grdcut_file_in_dataarray_out(): | ||
"grdcut an input grid file, and output as DataArray" | ||
outgrid = grdcut("@earth_relief_01d", region="0/180/0/90") | ||
assert isinstance(outgrid, xr.DataArray) | ||
# check information of the output grid | ||
# the '@earth_relief_01d' is in pixel registration, so the grid range is | ||
# not exactly 0/180/0/90 | ||
assert outgrid.coords["lat"].data.min() == 0.5 | ||
assert outgrid.coords["lat"].data.max() == 89.5 | ||
assert outgrid.coords["lon"].data.min() == 0.5 | ||
assert outgrid.coords["lon"].data.max() == 179.5 | ||
assert outgrid.data.min() == -8182.0 | ||
assert outgrid.data.max() == 5651.5 | ||
assert outgrid.sizes["lat"] == 90 | ||
assert outgrid.sizes["lon"] == 180 | ||
|
||
|
||
def test_grdcut_dataarray_in_file_out(): | ||
"grdcut an input DataArray, and output to a grid file" | ||
# Not supported yet. | ||
# See https://github.com/GenericMappingTools/gmt/pull/3532 | ||
|
||
|
||
def test_grdcut_dataarray_in_dataarray_out(): | ||
"grdcut an input DataArray, and output to a grid file" | ||
# Not supported yet. | ||
# See https://github.com/GenericMappingTools/gmt/pull/3532 | ||
|
||
|
||
def test_grdcut_dataarray_in_fail(): | ||
"Make sure that grdcut fails correctly if DataArray is the input grid" | ||
with pytest.raises(GMTInvalidInput): | ||
grid = load_earth_relief() | ||
grdcut(grid, region="0/180/0/90") | ||
|
||
|
||
def test_grdcut_fails(): | ||
"Check that grdcut fails correctly" | ||
with pytest.raises(GMTInvalidInput): | ||
grdcut(np.arange(10).reshape((5, 2))) |