4
4
5
5
import warnings
6
6
7
+ import numpy as np
7
8
import xarray as xr
8
9
from pygmt .clib import Session
9
10
from pygmt .exceptions import GMTInvalidInput
@@ -76,8 +77,9 @@ def grdfill(
76
77
gridfill : str | xr .DataArray | None = None ,
77
78
neighborfill : float | bool | None = None ,
78
79
splinefill : float | bool | None = None ,
80
+ inquire : bool = False ,
79
81
** kwargs ,
80
- ) -> xr .DataArray | None :
82
+ ) -> xr .DataArray | np . ndarray | None :
81
83
r"""
82
84
Interpolate across holes in a grid.
83
85
@@ -111,6 +113,9 @@ def grdfill(
111
113
hole : float
112
114
Set the node value used to identify a point as a member of a hole [Default is
113
115
NaN].
116
+ inquire
117
+ Output the bounds of each hole. The bounds are 2-D numpy arrays in the form of
118
+ (west, east, south, north). No grid fill takes place and ``outgrid`` is ignored.
114
119
mode : str
115
120
Specify the hole-filling algorithm to use. Choose from **c** for constant fill
116
121
and append the constant value, **n** for nearest neighbor (and optionally append
@@ -136,42 +141,65 @@ def grdfill(
136
141
137
142
Example
138
143
-------
144
+ Fill holes in a bathymetric grid with a constant value of 20.
139
145
>>> import pygmt
140
146
>>> # Load a bathymetric grid with missing data
141
147
>>> earth_relief_holes = pygmt.datasets.load_sample_data(name="earth_relief_holes")
142
148
>>> # Fill the holes with a constant value of 20
143
149
>>> filled_grid = pygmt.grdfill(grid=earth_relief_holes, constantfill=20)
150
+
151
+ Inquire the bounds of each hole.
152
+ >>> pygmt.grdfill(grid=earth_relief_holes, inquire=True)
153
+ array([[1.83333333, 6.16666667, 3.83333333, 8.16666667],
154
+ [6.16666667, 7.83333333, 0.5 , 2.5 ]])
144
155
"""
156
+ _fill_pars = "'constantfill'/'gridfill'/'neighborfill'/'splinefill'"
145
157
# TODO(PyGMT>=0.19.0): Remove the deprecated 'mode' parameter.
146
158
if kwargs .get ("A" ) is not None : # The deprecated 'mode' parameter is given.
147
- warnings . warn (
159
+ msg = (
148
160
"The 'mode' parameter is deprecated since v0.15.0 and will be removed in "
149
- "v0.19.0. Use 'constantfill'/'gridfill'/'neighborfill'/'splinefill' "
150
- "instead." ,
151
- FutureWarning ,
152
- stacklevel = 1 ,
161
+ f"v0.19.0. Use { _fill_pars } instead."
153
162
)
163
+ warnings .warn (msg , FutureWarning , stacklevel = 1 )
154
164
else :
155
165
# Determine the -A option from the fill parameters.
156
166
kwargs ["A" ] = _parse_fill_mode (constantfill , gridfill , neighborfill , splinefill )
157
167
158
- if kwargs .get ("A" ) is None and kwargs .get ("L" ) is None :
159
- msg = "At least parameter 'mode' or 'L' must be specified."
168
+ if kwargs .get ("A" ) and inquire :
169
+ msg = f"Parameters { _fill_pars } and 'inquire' are mutually exclusive."
170
+ raise GMTInvalidInput (msg )
171
+ if not (kwargs .get ("A" ) or inquire ):
172
+ msg = (
173
+ f"Need to specify parameter { _fill_pars } for filling holes or 'inquire' "
174
+ "for inquiring the bounds of each hole."
175
+ )
160
176
raise GMTInvalidInput (msg )
161
177
162
178
with Session () as lib :
163
- with (
164
- lib .virtualfile_in (check_kind = "raster" , data = grid ) as vingrd ,
165
- lib .virtualfile_in (
166
- check_kind = "raster" , data = gridfill , required_data = False
167
- ) as vbggrd ,
168
- lib .virtualfile_out (kind = "grid" , fname = outgrid ) as voutgrd ,
169
- ):
170
- if gridfill is not None :
171
- # Fill by a grid. Append the actual or virtual grid file name.
172
- kwargs ["A" ] = f"g{ vbggrd } "
173
- kwargs ["G" ] = voutgrd
174
- lib .call_module (
175
- module = "grdfill" , args = build_arg_list (kwargs , infile = vingrd )
176
- )
177
- return lib .virtualfile_to_raster (vfname = voutgrd , outgrid = outgrid )
179
+ with lib .virtualfile_in (check_kind = "raster" , data = grid ) as vingrd :
180
+ if inquire : # Inquire mode.
181
+ kwargs ["L" ] = True
182
+ with lib .virtualfile_out (kind = "dataset" ) as vouttbl :
183
+ lib .call_module (
184
+ module = "grdfill" ,
185
+ args = build_arg_list (kwargs , infile = vingrd , outfile = vouttbl ),
186
+ )
187
+ return lib .virtualfile_to_dataset (
188
+ vfname = vouttbl , output_type = "numpy"
189
+ )
190
+
191
+ # Fill mode.
192
+ with (
193
+ lib .virtualfile_in (
194
+ check_kind = "raster" , data = gridfill , required_data = False
195
+ ) as vbggrd ,
196
+ lib .virtualfile_out (kind = "grid" , fname = outgrid ) as voutgrd ,
197
+ ):
198
+ if gridfill is not None :
199
+ # Fill by a grid. Append the actual or virtual grid file name.
200
+ kwargs ["A" ] = f"g{ vbggrd } "
201
+ kwargs ["G" ] = voutgrd
202
+ lib .call_module (
203
+ module = "grdfill" , args = build_arg_list (kwargs , infile = vingrd )
204
+ )
205
+ return lib .virtualfile_to_raster (vfname = voutgrd , outgrid = outgrid )
0 commit comments