2
2
grdfill - Interpolate across holes in a grid.
3
3
"""
4
4
5
+ import warnings
6
+
5
7
import xarray as xr
6
8
from pygmt .clib import Session
7
9
from pygmt .exceptions import GMTInvalidInput
16
18
__doctest_skip__ = ["grdfill" ]
17
19
18
20
21
+ def _parse_fill_mode (
22
+ constantfill = None , gridfill = None , neighborfill = None , splinefill = None
23
+ ) -> str | None :
24
+ """
25
+ Parse the fill parameters and return the appropriate string for the -A option.
26
+
27
+ >>> import numpy as np
28
+ >>> import xarray as xr
29
+ >>> _parse_fill_mode(constantfill=20.0)
30
+ 'c20.0'
31
+ >>> _parse_fill_mode(gridfill="bggrid.nc")
32
+ 'g'
33
+ >>> _parse_fill_mode(gridfill=xr.DataArray(np.zeros((10, 10))))
34
+ 'g'
35
+ >>> _parse_fill_mode(neighborfill=20)
36
+ 'n20'
37
+ >>> _parse_fill_mode(neighborfill=True)
38
+ 'n'
39
+ >>> _parse_fill_mode(splinefill=0.5)
40
+ 's0.5'
41
+ >>> _parse_fill_mode(splinefill=True)
42
+ 's'
43
+ >>> _parse_fill_mode(constantfill=20, gridfill="bggrid.nc")
44
+ Traceback (most recent call last):
45
+ ...
46
+ pygmt.exceptions.GMTInvalidInput: The ... parameters are mutually exclusive.
47
+ """
48
+ fill_params = [constantfill , gridfill , neighborfill , splinefill ]
49
+ if sum (param is not None for param in fill_params ) > 1 :
50
+ msg = (
51
+ "The 'constantfill', 'gridfill', 'neighborfill', and 'splinefill' "
52
+ "parameters are mutually exclusive."
53
+ )
54
+ raise GMTInvalidInput (msg )
55
+
56
+ if constantfill is not None :
57
+ return f"c{ constantfill } "
58
+ if gridfill is not None :
59
+ return "g" # Append grid file name later to support xarray.DataArray.
60
+ if neighborfill is not None and neighborfill is not False :
61
+ return "n" if neighborfill is True else f"n{ neighborfill } "
62
+ if splinefill is not None and splinefill is not False :
63
+ return "s" if splinefill is True else f"s{ splinefill } "
64
+ return None
65
+
66
+
19
67
@fmt_docstring
20
68
# TODO(PyGMT>=0.19.0): Remove the deprecated 'no_data' parameter.
21
69
@deprecate_parameter ("no_data" , "hole" , "v0.15.0" , remove_version = "v0.19.0" )
22
70
@use_alias (A = "mode" , N = "hole" , R = "region" , V = "verbose" )
23
71
@kwargs_to_strings (R = "sequence" )
24
- def grdfill (grid , outgrid : str | None = None , ** kwargs ) -> xr .DataArray | None :
72
+ def grdfill (
73
+ grid : str | xr .DataArray ,
74
+ outgrid : str | None = None ,
75
+ constantfill : float | None = None ,
76
+ gridfill : str | xr .DataArray | None = None ,
77
+ neighborfill : float | bool | None = None ,
78
+ splinefill : float | bool | None = None ,
79
+ ** kwargs ,
80
+ ) -> xr .DataArray | None :
25
81
r"""
26
82
Interpolate across holes in a grid.
27
83
@@ -31,25 +87,40 @@ def grdfill(grid, outgrid: str | None = None, **kwargs) -> xr.DataArray | None:
31
87
replace the hole values. If no holes are found the original unchanged grid is
32
88
returned.
33
89
34
- Full option list at :gmt-docs:`grdfill.html`
90
+ Full option list at :gmt-docs:`grdfill.html`.
35
91
36
92
{aliases}
37
93
38
94
Parameters
39
95
----------
40
96
{grid}
41
97
{outgrid}
42
- mode : str
43
- Specify the hole-filling algorithm to use. Choose from **c** for
44
- constant fill and append the constant value, **n** for nearest
45
- neighbor (and optionally append a search radius in
46
- pixels [default radius is :math:`r^2 = \sqrt{{ X^2 + Y^2 }}`,
47
- where (*X,Y*) are the node dimensions of the grid]), or
48
- **s** for bicubic spline (optionally append a *tension*
49
- parameter [Default is no tension]).
98
+ constantfill
99
+ Fill the holes with a constant value. Specify the constant value to use.
100
+ gridfill
101
+ Fill the holes with values sampled from another (possibly coarser) grid. Specify
102
+ the grid (a file name or an :class:`xarray.DataArray`) to use for the fill.
103
+ neighborfill
104
+ Fill the holes with the nearest neighbor. Specify the search radius in pixels.
105
+ If set to ``True``, the default search radius will be used
106
+ (:math:`r^2 = \sqrt{{n^2 + m^2}}`, where (*n,m*) are the node dimensions of the
107
+ grid).
108
+ splinefill
109
+ Fill the holes with a bicubic spline. Specify the tension value to use. If set
110
+ to ``True``, no tension will be used.
50
111
hole : float
51
112
Set the node value used to identify a point as a member of a hole [Default is
52
113
NaN].
114
+ mode : str
115
+ Specify the hole-filling algorithm to use. Choose from **c** for constant fill
116
+ and append the constant value, **n** for nearest neighbor (and optionally append
117
+ a search radius in pixels [default radius is :math:`r^2 = \sqrt{{ X^2 + Y^2 }}`,
118
+ where (*X,Y*) are the node dimensions of the grid]), or **s** for bicubic spline
119
+ (optionally append a *tension* parameter [Default is no tension]).
120
+
121
+ .. deprecated:: 0.15.0
122
+ Use ``constantfill``, ``gridfill``, ``neighborfill``, or ``splinefill``
123
+ instead. The parameter will be removed in v0.19.0.
53
124
{region}
54
125
{verbose}
55
126
@@ -67,19 +138,37 @@ def grdfill(grid, outgrid: str | None = None, **kwargs) -> xr.DataArray | None:
67
138
>>> import pygmt
68
139
>>> # Load a bathymetric grid with missing data
69
140
>>> earth_relief_holes = pygmt.datasets.load_sample_data(name="earth_relief_holes")
70
- >>> # Perform grid filling operations on the sample grid
71
- >>> # Set all empty values to "20"
72
- >>> filled_grid = pygmt.grdfill(grid=earth_relief_holes, mode="c20")
141
+ >>> # Fill the holes with a constant value of 20
142
+ >>> filled_grid = pygmt.grdfill(grid=earth_relief_holes, constantfill=20)
73
143
"""
144
+ # TODO(PyGMT>=0.19.0): Remove the deprecated 'mode' parameter.
145
+ if kwargs .get ("A" ) is not None : # The deprecated 'mode' parameter is given.
146
+ warnings .warn (
147
+ "The 'mode' parameter is deprecated since v0.15.0 and will be removed in "
148
+ "v0.19.0. Use 'constantfill'/'gridfill'/'neighborfill'/'splinefill' "
149
+ "instead." ,
150
+ FutureWarning ,
151
+ stacklevel = 1 ,
152
+ )
153
+ else :
154
+ # Determine the -A option from the fill parameters.
155
+ kwargs ["A" ] = _parse_fill_mode (constantfill , gridfill , neighborfill , splinefill )
156
+
74
157
if kwargs .get ("A" ) is None and kwargs .get ("L" ) is None :
75
158
msg = "At least parameter 'mode' or 'L' must be specified."
76
159
raise GMTInvalidInput (msg )
77
160
78
161
with Session () as lib :
79
162
with (
80
163
lib .virtualfile_in (check_kind = "raster" , data = grid ) as vingrd ,
164
+ lib .virtualfile_in (
165
+ check_kind = "raster" , data = gridfill , required_data = False
166
+ ) as vbggrd ,
81
167
lib .virtualfile_out (kind = "grid" , fname = outgrid ) as voutgrd ,
82
168
):
169
+ if gridfill is not None :
170
+ # Fill by a grid. Append the actual or virtual grid file name.
171
+ kwargs ["A" ] = f"g{ vbggrd } "
83
172
kwargs ["G" ] = voutgrd
84
173
lib .call_module (
85
174
module = "grdfill" , args = build_arg_list (kwargs , infile = vingrd )
0 commit comments