Skip to content

Commit b137613

Browse files
Meghan Jonesweiji14
Meghan Jones
andauthored
Add pygmt.load_dataarray function and refactor modules that return None or xarray.dataarray (#1439)
Add new function pygmt.load_datarray Use pygmt.load_dataarrary for modules that return grids Add documentation Co-authored-by: Wei Ji <[email protected]>
1 parent 4b2bf85 commit b137613

14 files changed

+77
-94
lines changed

doc/api/index.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ Crossover analysis with x2sys:
107107
x2sys_init
108108
x2sys_cross
109109

110+
Input/output
111+
------------
112+
113+
.. autosummary::
114+
:toctree: generated
115+
116+
load_dataarray
117+
110118
GMT Defaults
111119
------------
112120

pygmt/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from pygmt import datasets
2727
from pygmt.accessors import GMTDataArrayAccessor
2828
from pygmt.figure import Figure, set_display
29+
from pygmt.io import load_dataarray
2930
from pygmt.session_management import begin as _begin
3031
from pygmt.session_management import end as _end
3132
from pygmt.src import (

pygmt/datasets/earth_relief.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
55
The grids are available in various resolutions.
66
"""
7-
import xarray as xr
87
from pygmt.exceptions import GMTInvalidInput
98
from pygmt.helpers import kwargs_to_strings
9+
from pygmt.io import load_dataarray
1010
from pygmt.src import grdcut, which
1111

1212

@@ -133,9 +133,7 @@ def load_earth_relief(resolution="01d", region=None, registration=None, use_srtm
133133
f"'region' is required for Earth relief resolution '{resolution}'."
134134
)
135135
fname = which(f"@earth_relief_{resolution}{reg}", download="a")
136-
with xr.open_dataarray(fname, engine="netcdf4") as dataarray:
137-
grid = dataarray.load()
138-
_ = grid.gmt # load GMTDataArray accessor information
136+
grid = load_dataarray(fname, engine="netcdf4")
139137
else:
140138
grid = grdcut(f"@{earth_relief_prefix}{resolution}{reg}", region=region)
141139

pygmt/io.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
PyGMT input/output (I/O) utilities.
3+
"""
4+
import xarray as xr
5+
6+
7+
def load_dataarray(filename_or_obj, **kwargs):
8+
"""
9+
Open, load into memory, and close a DataArray from a file or file-like
10+
object containing a single data variable.
11+
12+
This is a thin wrapper around :py:func:`xarray.open_dataarray`. It differs
13+
from :py:func:`xarray.open_dataarray` in that it loads the DataArray into
14+
memory, gets GMT specific metadata about the grid via
15+
:py:meth:`GMTDataArrayAccessor`, closes the file, and returns the
16+
DataArray. In contrast, :py:func:`xarray.open_dataarray` keeps the file
17+
handle open and lazy loads its contents. All parameters are passed directly
18+
to :py:func:`xarray.open_dataarray`. See that documentation for further
19+
details.
20+
21+
Parameters
22+
----------
23+
filename_or_obj : str or pathlib.Path or file-like or DataStore
24+
Strings and Path objects are interpreted as a path to a netCDF file
25+
or an OpenDAP URL and opened with python-netCDF4, unless the filename
26+
ends with .gz, in which case the file is gunzipped and opened with
27+
scipy.io.netcdf (only netCDF3 supported). Byte-strings or file-like
28+
objects are opened by scipy.io.netcdf (netCDF3) or h5py (netCDF4/HDF).
29+
30+
Returns
31+
-------
32+
datarray : xarray.DataArray
33+
The newly created DataArray.
34+
35+
See Also
36+
--------
37+
xarray.open_dataarray
38+
"""
39+
if "cache" in kwargs:
40+
raise TypeError("cache has no effect in this context")
41+
42+
with xr.open_dataarray(filename_or_obj, **kwargs) as dataarray:
43+
result = dataarray.load()
44+
_ = result.gmt # load GMTDataArray accessor information
45+
46+
return result

pygmt/src/grdclip.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
grdclip - Change the range and extremes of grid values.
33
"""
44

5-
import xarray as xr
65
from pygmt.clib import Session
76
from pygmt.helpers import (
87
GMTTempFile,
@@ -11,6 +10,7 @@
1110
kwargs_to_strings,
1211
use_alias,
1312
)
13+
from pygmt.io import load_dataarray
1414

1515

1616
@fmt_docstring
@@ -88,11 +88,4 @@ def grdclip(grid, **kwargs):
8888
arg_str = " ".join([infile, build_arg_string(kwargs)])
8989
lib.call_module("grdclip", arg_str)
9090

91-
if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
92-
with xr.open_dataarray(outgrid) as dataarray:
93-
result = dataarray.load()
94-
_ = result.gmt # load GMTDataArray accessor information
95-
else:
96-
result = None # if user sets an outgrid, return None
97-
98-
return result
91+
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/grdcut.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
grdcut - Extract subregion from a grid.
33
"""
44

5-
import xarray as xr
65
from pygmt.clib import Session
76
from pygmt.helpers import (
87
GMTTempFile,
@@ -11,6 +10,7 @@
1110
kwargs_to_strings,
1211
use_alias,
1312
)
13+
from pygmt.io import load_dataarray
1414

1515

1616
@fmt_docstring
@@ -98,11 +98,4 @@ def grdcut(grid, **kwargs):
9898
arg_str = " ".join([infile, build_arg_string(kwargs)])
9999
lib.call_module("grdcut", arg_str)
100100

101-
if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
102-
with xr.open_dataarray(outgrid) as dataarray:
103-
result = dataarray.load()
104-
_ = result.gmt # load GMTDataArray accessor information
105-
else:
106-
result = None # if user sets an outgrid, return None
107-
108-
return result
101+
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/grdfill.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
grdfill - Fill blank areas from a grid.
33
"""
44

5-
import xarray as xr
65
from pygmt.clib import Session
76
from pygmt.exceptions import GMTInvalidInput
87
from pygmt.helpers import (
@@ -12,6 +11,7 @@
1211
kwargs_to_strings,
1312
use_alias,
1413
)
14+
from pygmt.io import load_dataarray
1515

1616

1717
@fmt_docstring
@@ -75,11 +75,4 @@ def grdfill(grid, **kwargs):
7575
arg_str = " ".join([infile, build_arg_string(kwargs)])
7676
lib.call_module("grdfill", arg_str)
7777

78-
if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
79-
with xr.open_dataarray(outgrid) as dataarray:
80-
result = dataarray.load()
81-
_ = result.gmt # load GMTDataArray accessor information
82-
else:
83-
result = None # if user sets an outgrid, return None
84-
85-
return result
78+
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/grdfilter.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
grdfilter - Filter a grid in the space (or time) domain.
33
"""
44

5-
import xarray as xr
65
from pygmt.clib import Session
76
from pygmt.helpers import (
87
GMTTempFile,
@@ -11,6 +10,7 @@
1110
kwargs_to_strings,
1211
use_alias,
1312
)
13+
from pygmt.io import load_dataarray
1414

1515

1616
@fmt_docstring
@@ -151,11 +151,4 @@ def grdfilter(grid, **kwargs):
151151
arg_str = " ".join([infile, build_arg_string(kwargs)])
152152
lib.call_module("grdfilter", arg_str)
153153

154-
if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
155-
with xr.open_dataarray(outgrid) as dataarray:
156-
result = dataarray.load()
157-
_ = result.gmt # load GMTDataArray accessor information
158-
else:
159-
result = None # if user sets an outgrid, return None
160-
161-
return result
154+
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/grdgradient.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
grdgradient - Compute directional gradients from a grid.
33
"""
44

5-
import xarray as xr
65
from pygmt.clib import Session
76
from pygmt.exceptions import GMTInvalidInput
87
from pygmt.helpers import (
@@ -13,6 +12,7 @@
1312
kwargs_to_strings,
1413
use_alias,
1514
)
15+
from pygmt.io import load_dataarray
1616

1717

1818
@fmt_docstring
@@ -117,11 +117,4 @@ def grdgradient(grid, **kwargs):
117117
arg_str = " ".join([infile, build_arg_string(kwargs)])
118118
lib.call_module("grdgradient", arg_str)
119119

120-
if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
121-
with xr.open_dataarray(outgrid) as dataarray:
122-
result = dataarray.load()
123-
_ = result.gmt # load GMTDataArray accessor information
124-
else:
125-
result = None # if user sets an outgrid, return None
126-
127-
return result
120+
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/grdlandmask.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
grdlandmask - Create a "wet-dry" mask grid from shoreline data base
33
"""
44

5-
import xarray as xr
65
from pygmt.clib import Session
76
from pygmt.exceptions import GMTInvalidInput
87
from pygmt.helpers import (
@@ -12,6 +11,7 @@
1211
kwargs_to_strings,
1312
use_alias,
1413
)
14+
from pygmt.io import load_dataarray
1515

1616

1717
@fmt_docstring
@@ -104,11 +104,4 @@ def grdlandmask(**kwargs):
104104
arg_str = build_arg_string(kwargs)
105105
lib.call_module("grdlandmask", arg_str)
106106

107-
if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
108-
with xr.open_dataarray(outgrid) as dataarray:
109-
result = dataarray.load()
110-
_ = result.gmt # load GMTDataArray accessor information
111-
else:
112-
result = None # if user sets an outgrid, return None
113-
114-
return result
107+
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/grdproject.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
grdproject - Forward and inverse map transformation of grids.
33
"""
44

5-
import xarray as xr
65
from pygmt.clib import Session
76
from pygmt.exceptions import GMTInvalidInput
87
from pygmt.helpers import (
@@ -12,6 +11,7 @@
1211
kwargs_to_strings,
1312
use_alias,
1413
)
14+
from pygmt.io import load_dataarray
1515

1616

1717
@fmt_docstring
@@ -83,11 +83,4 @@ def grdproject(grid, **kwargs):
8383
arg_str = " ".join([infile, build_arg_string(kwargs)])
8484
lib.call_module("grdproject", arg_str)
8585

86-
if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
87-
with xr.open_dataarray(outgrid) as dataarray:
88-
result = dataarray.load()
89-
_ = result.gmt # load GMTDataArray accessor information
90-
else:
91-
result = None # if user sets an outgrid, return None
92-
93-
return result
86+
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/grdsample.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
grdsample - Resample a grid onto a new lattice
33
"""
44

5-
import xarray as xr
65
from pygmt.clib import Session
76
from pygmt.helpers import (
87
GMTTempFile,
@@ -11,6 +10,7 @@
1110
kwargs_to_strings,
1211
use_alias,
1312
)
13+
from pygmt.io import load_dataarray
1414

1515

1616
@fmt_docstring
@@ -85,11 +85,4 @@ def grdsample(grid, **kwargs):
8585
arg_str = " ".join([infile, build_arg_string(kwargs)])
8686
lib.call_module("grdsample", arg_str)
8787

88-
if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
89-
with xr.open_dataarray(outgrid) as dataarray:
90-
result = dataarray.load()
91-
_ = result.gmt # load GMTDataArray accessor information
92-
else:
93-
result = None # if user sets an outgrid, return None
94-
95-
return result
88+
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/surface.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
surface - Grids table data using adjustable tension continuous curvature
33
splines.
44
"""
5-
import xarray as xr
65
from pygmt.clib import Session
76
from pygmt.exceptions import GMTInvalidInput
87
from pygmt.helpers import (
@@ -15,6 +14,7 @@
1514
kwargs_to_strings,
1615
use_alias,
1716
)
17+
from pygmt.io import load_dataarray
1818

1919

2020
@fmt_docstring
@@ -101,11 +101,4 @@ def surface(x=None, y=None, z=None, data=None, **kwargs):
101101
arg_str = " ".join([infile, build_arg_string(kwargs)])
102102
lib.call_module(module="surface", args=arg_str)
103103

104-
if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
105-
with xr.open_dataarray(outgrid) as dataarray:
106-
result = dataarray.load()
107-
_ = result.gmt # load GMTDataArray accessor information
108-
elif outgrid != tmpfile.name: # if user sets an outgrid, return None
109-
result = None
110-
111-
return result
104+
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/xyz2grd.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
xyz2grd - Convert data table to a grid.
33
"""
4-
import xarray as xr
54
from pygmt.clib import Session
65
from pygmt.helpers import (
76
GMTTempFile,
@@ -10,6 +9,7 @@
109
kwargs_to_strings,
1110
use_alias,
1211
)
12+
from pygmt.io import load_dataarray
1313

1414

1515
@fmt_docstring
@@ -64,11 +64,4 @@ def xyz2grd(table, **kwargs):
6464
arg_str = " ".join([infile, arg_str])
6565
lib.call_module("xyz2grd", arg_str)
6666

67-
if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
68-
with xr.open_dataarray(outgrid) as dataarray:
69-
result = dataarray.load()
70-
_ = result.gmt # load GMTDataArray accessor information
71-
else:
72-
result = None # if user sets an outgrid, return None
73-
74-
return result
67+
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

0 commit comments

Comments
 (0)