Skip to content

Commit cc26838

Browse files
willschlitzercore-manweiji14
authored
Wrap grdfill (#1276)
Wrap the gmt module grdfill and create test_grdfill.py. Co-authored-by: Yao Jiayuan <coreman.seism@gmail.com> Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
1 parent c411377 commit cc26838

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Operations on grids:
9090

9191
grdclip
9292
grdcut
93+
grdfill
9394
grdfilter
9495
grdtrack
9596

pygmt/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
grd2cpt,
3636
grdclip,
3737
grdcut,
38+
grdfill,
3839
grdfilter,
3940
grdinfo,
4041
grdtrack,

pygmt/src/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pygmt.src.grdclip import grdclip
1414
from pygmt.src.grdcontour import grdcontour
1515
from pygmt.src.grdcut import grdcut
16+
from pygmt.src.grdfill import grdfill
1617
from pygmt.src.grdfilter import grdfilter
1718
from pygmt.src.grdimage import grdimage
1819
from pygmt.src.grdinfo import grdinfo

pygmt/src/grdfill.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
grdfill - Fill blank areas from a grid.
3+
"""
4+
5+
import xarray as xr
6+
from pygmt.clib import Session
7+
from pygmt.helpers import (
8+
GMTTempFile,
9+
build_arg_string,
10+
fmt_docstring,
11+
kwargs_to_strings,
12+
use_alias,
13+
)
14+
15+
16+
@fmt_docstring
17+
@use_alias(
18+
A="mode",
19+
G="outgrid",
20+
R="region",
21+
)
22+
@kwargs_to_strings(R="sequence")
23+
def grdfill(grid, **kwargs):
24+
r"""
25+
Fill blank areas from a grid file.
26+
27+
Read a grid that presumably has unfilled holes that the user
28+
wants to fill in some fashion. Holes are identified by NaN values but
29+
this criteria can be changed. There are several different algorithms that
30+
can be used to replace the hole values.
31+
32+
Full option list at :gmt-docs:`grdfill.html`
33+
34+
{aliases}
35+
36+
Parameters
37+
----------
38+
grid : str or xarray.DataArray
39+
The file name of the input grid or the grid loaded as a DataArray.
40+
outgrid : str or None
41+
The name of the output netCDF file with extension .nc to store the grid
42+
in.
43+
mode : str
44+
Specify the hole-filling algorithm to use. Choose from **c** for
45+
constant fill and append the constant value, **n** for nearest
46+
neighbor (and optionally append a search radius in
47+
pixels [default radius is :math:`r^2 = \sqrt{{ X^2 + Y^2 }}`,
48+
where (*X,Y*) are the node dimensions of the grid]).
49+
{R}
50+
51+
Returns
52+
-------
53+
ret: xarray.DataArray or None
54+
Return type depends on whether the ``outgrid`` parameter is set:
55+
56+
- :class:`xarray.DataArray` if ``outgrid`` is not set
57+
- None if ``outgrid`` is set (grid output will be stored in file set by
58+
``outgrid``)
59+
"""
60+
with GMTTempFile(suffix=".nc") as tmpfile:
61+
with Session() as lib:
62+
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
63+
with file_context as infile:
64+
if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile
65+
kwargs.update({"G": tmpfile.name})
66+
outgrid = kwargs["G"]
67+
arg_str = " ".join([infile, build_arg_string(kwargs)])
68+
lib.call_module("grdfill", arg_str)
69+
70+
if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
71+
with xr.open_dataarray(outgrid) as dataarray:
72+
result = dataarray.load()
73+
_ = result.gmt # load GMTDataArray accessor information
74+
else:
75+
result = None # if user sets an outgrid, return None
76+
77+
return result

pygmt/tests/test_grdfill.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Tests for grdclip.
3+
"""
4+
import os
5+
6+
import numpy as np
7+
import pytest
8+
import xarray as xr
9+
from pygmt import grdfill, grdinfo
10+
from pygmt.datasets import load_earth_relief
11+
from pygmt.helpers import GMTTempFile
12+
13+
14+
@pytest.fixture(scope="module", name="grid")
15+
def fixture_grid():
16+
"""
17+
Load the grid data from the sample earth_relief file and set value(s) to
18+
NaN.
19+
"""
20+
grid = load_earth_relief(registration="pixel", region=[-5, 5, -5, 5])
21+
grid[3:5, 3:5] = np.nan
22+
grid[5:7, 5:7] = np.inf
23+
return grid
24+
25+
26+
def test_grdfill_dataarray_out(grid):
27+
"""
28+
grdfill with a DataArray output.
29+
"""
30+
result = grdfill(grid=grid, mode="c20")
31+
# check information of the output grid
32+
assert isinstance(result, xr.DataArray)
33+
assert result[4, 4] == 20
34+
assert result[5, 5] == np.inf
35+
assert not result.isnull().all() # check that no NaN values exists
36+
assert result.gmt.gtype == 1 # Geographic grid
37+
assert result.gmt.registration == 1 # Pixel registration
38+
39+
40+
def test_grdfill_file_out(grid):
41+
"""
42+
grdfill with an outgrid set.
43+
"""
44+
with GMTTempFile(suffix=".nc") as tmpfile:
45+
result = grdfill(grid=grid, mode="c20", outgrid=tmpfile.name)
46+
assert result is None # return value is None
47+
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
48+
result = grdinfo(tmpfile.name, per_column=True).strip()
49+
assert result == "-5 5 -5 5 -5130.5 inf 1 1 10 10 1 1"

0 commit comments

Comments
 (0)