1
1
"""
2
2
grdtrack - Sample grids at specified (x,y) locations.
3
3
"""
4
+ import warnings
5
+
4
6
import pandas as pd
7
+ import xarray as xr
5
8
from pygmt .clib import Session
6
9
from pygmt .exceptions import GMTInvalidInput
7
10
from pygmt .helpers import (
11
14
kwargs_to_strings ,
12
15
use_alias ,
13
16
)
17
+ from pygmt .src .which import which
14
18
15
19
__doctest_skip__ = ["grdtrack" ]
16
20
43
47
w = "wrap" ,
44
48
)
45
49
@kwargs_to_strings (R = "sequence" , S = "sequence" , i = "sequence_comma" , o = "sequence_comma" )
46
- def grdtrack (points , grid , newcolname = None , outfile = None , ** kwargs ):
50
+ def grdtrack (grid , points = None , newcolname = None , outfile = None , ** kwargs ):
47
51
r"""
48
52
Sample grids at specified (x,y) locations.
49
53
@@ -67,14 +71,14 @@ def grdtrack(points, grid, newcolname=None, outfile=None, **kwargs):
67
71
68
72
Parameters
69
73
----------
70
- points : str or {table-like}
71
- Pass in either a file name to an ASCII data table, a 2D
72
- {table-classes}.
73
-
74
74
grid : xarray.DataArray or str
75
75
Gridded array from which to sample values from, or a filename (netcdf
76
76
format).
77
77
78
+ points : str or {table-like}
79
+ Pass in either a file name to an ASCII data table, a 2D
80
+ {table-classes}.
81
+
78
82
newcolname : str
79
83
Required if ``points`` is a :class:`pandas.DataFrame`. The name for the
80
84
new column in the track :class:`pandas.DataFrame` table where the
@@ -283,26 +287,65 @@ def grdtrack(points, grid, newcolname=None, outfile=None, **kwargs):
283
287
... points=points, grid=grid, newcolname="bathymetry"
284
288
... )
285
289
"""
290
+ # pylint: disable=too-many-branches
291
+ if points is not None and kwargs .get ("E" ) is not None :
292
+ raise GMTInvalidInput ("Can't set both 'points' and 'profile'." )
293
+
294
+ if points is None and kwargs .get ("E" ) is None :
295
+ raise GMTInvalidInput ("Must give 'points' or set 'profile'." )
296
+
286
297
if hasattr (points , "columns" ) and newcolname is None :
287
298
raise GMTInvalidInput ("Please pass in a str to 'newcolname'" )
288
299
300
+ # Backward compatibility with old parameter order "points, grid".
301
+ # deprecated_version="0.7.0", remove_version="v0.9.0"
302
+ is_a_grid = True
303
+ if not isinstance (grid , (xr .DataArray , str )):
304
+ is_a_grid = False
305
+ elif isinstance (grid , str ):
306
+ try :
307
+ xr .open_dataarray (which (grid , download = "a" ), engine = "netcdf4" ).close ()
308
+ is_a_grid = True
309
+ except (ValueError , OSError ):
310
+ is_a_grid = False
311
+ if not is_a_grid :
312
+ msg = (
313
+ "Positional parameters 'points, grid' of pygmt.grdtrack() has changed "
314
+ "to 'grid, points=None' since v0.7.0. It's likely that you're NOT "
315
+ "passing a valid grid as the first positional argument or "
316
+ "are passing an invalid grid to the 'grid' parameter. "
317
+ "Please check the order of arguments with the latest documentation. "
318
+ "This warning will be removed in v0.9.0."
319
+ )
320
+ grid , points = points , grid
321
+ warnings .warn (msg , category = FutureWarning , stacklevel = 1 )
322
+
289
323
with GMTTempFile (suffix = ".csv" ) as tmpfile :
290
324
with Session () as lib :
291
- # Choose how data will be passed into the module
292
- table_context = lib .virtualfile_from_data (check_kind = "vector" , data = points )
293
325
# Store the xarray.DataArray grid in virtualfile
294
326
grid_context = lib .virtualfile_from_data (check_kind = "raster" , data = grid )
295
327
296
- # Run grdtrack on the temporary (csv) points table
297
- # and (netcdf) grid virtualfile
298
- with table_context as csvfile :
299
- with grid_context as grdfile :
300
- kwargs .update ({"G" : grdfile })
301
- if outfile is None : # Output to tmpfile if outfile is not set
302
- outfile = tmpfile .name
328
+ with grid_context as grdfile :
329
+ kwargs .update ({"G" : grdfile })
330
+ if outfile is None : # Output to tmpfile if outfile is not set
331
+ outfile = tmpfile .name
332
+
333
+ if points is not None :
334
+ # Choose how data will be passed into the module
335
+ table_context = lib .virtualfile_from_data (
336
+ check_kind = "vector" , data = points
337
+ )
338
+ with table_context as csvfile :
339
+ lib .call_module (
340
+ module = "grdtrack" ,
341
+ args = build_arg_string (
342
+ kwargs , infile = csvfile , outfile = outfile
343
+ ),
344
+ )
345
+ else :
303
346
lib .call_module (
304
347
module = "grdtrack" ,
305
- args = build_arg_string (kwargs , infile = csvfile , outfile = outfile ),
348
+ args = build_arg_string (kwargs , outfile = outfile ),
306
349
)
307
350
308
351
# Read temporary csv output to a pandas table
0 commit comments