2
2
GMT modules for Sampling of 1-D and 2-D Data
3
3
"""
4
4
import pandas as pd
5
- import xarray as xr
6
5
7
6
from .clib import Session
8
7
from .helpers import (
16
15
17
16
18
17
@fmt_docstring
19
- def grdtrack (
20
- points : pd .DataFrame , grid : xr .DataArray , newcolname : str = None , ** kwargs
21
- ):
18
+ def grdtrack (points , grid , newcolname = None , outfile = None , ** kwargs ):
22
19
"""
23
20
Sample grids at specified (x,y) locations.
24
21
@@ -33,33 +30,40 @@ def grdtrack(
33
30
34
31
Parameters
35
32
----------
36
- points: pandas.DataFrame
37
- Table with (x, y) or (lon, lat) values in the first two columns. More columns
38
- may be present.
33
+ points: pandas.DataFrame or file (csv, txt, etc)
34
+ Either a table with (x, y) or (lon, lat) values in the first two columns,
35
+ or a data file name. More columns may be present.
39
36
40
37
grid: xarray.DataArray or file (netcdf)
41
38
Gridded array from which to sample values from.
42
39
43
40
newcolname: str
44
- Name for the new column in the table where the sampled values will be placed.
41
+ Required if 'points' is a pandas.DataFrame. The name for the new column in the
42
+ track pandas.DataFrame table where the sampled values will be placed.
43
+
44
+ outfile: str
45
+ Required if 'points' is a file. The file name for the output ASCII file.
45
46
46
47
Returns
47
48
-------
48
- track: pandas.DataFrame
49
- Table with (x, y, ..., newcolname) or (lon, lat, ..., newcolname) values.
49
+ track: pandas.DataFrame or None
50
+ Return type depends on whether the outfile parameter is set:
51
+ - pandas.DataFrame table with (x, y, ..., newcolname) if outfile is not set
52
+ - None if outfile is set (track output will be stored in outfile)
50
53
51
54
"""
52
55
53
- try :
54
- assert isinstance (newcolname , str )
55
- except AssertionError :
56
- raise GMTInvalidInput ("Please pass in a str to 'newcolname'" )
57
-
58
56
with GMTTempFile (suffix = ".csv" ) as tmpfile :
59
57
with Session () as lib :
60
58
# Store the pandas.DataFrame points table in virtualfile
61
59
if data_kind (points ) == "matrix" :
60
+ if newcolname is None :
61
+ raise GMTInvalidInput ("Please pass in a str to 'newcolname'" )
62
62
table_context = lib .virtualfile_from_matrix (points .values )
63
+ elif data_kind (points ) == "file" :
64
+ if outfile is None :
65
+ raise GMTInvalidInput ("Please pass in a str to 'outfile'" )
66
+ table_context = dummy_context (points )
63
67
else :
64
68
raise GMTInvalidInput (f"Unrecognized data type { type (points )} " )
65
69
@@ -75,13 +79,18 @@ def grdtrack(
75
79
with table_context as csvfile :
76
80
with grid_context as grdfile :
77
81
kwargs .update ({"G" : grdfile })
82
+ if outfile is None : # Output to tmpfile if outfile is not set
83
+ outfile = tmpfile .name
78
84
arg_str = " " .join (
79
- [csvfile , build_arg_string (kwargs ), "->" + tmpfile . name ]
85
+ [csvfile , build_arg_string (kwargs ), "->" + outfile ]
80
86
)
81
87
lib .call_module (module = "grdtrack" , args = arg_str )
82
88
83
89
# Read temporary csv output to a pandas table
84
- column_names = points .columns .to_list () + [newcolname ]
85
- result = pd .read_csv (tmpfile .name , sep = "\t " , names = column_names )
90
+ if outfile == tmpfile .name : # if user did not set outfile, return pd.DataFrame
91
+ column_names = points .columns .to_list () + [newcolname ]
92
+ result = pd .read_csv (tmpfile .name , sep = "\t " , names = column_names )
93
+ elif outfile != tmpfile .name : # return None if outfile set, output in outfile
94
+ result = None
86
95
87
96
return result
0 commit comments