Skip to content

Commit cd2c5d7

Browse files
committed
Refactor all grid-related modules to use the Session.virtualfile_to_grid method, except grdcut
1 parent daad3af commit cd2c5d7

17 files changed

+209
-328
lines changed

pygmt/src/binstats.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,13 @@
33
"""
44

55
from pygmt.clib import Session
6-
from pygmt.helpers import (
7-
GMTTempFile,
8-
build_arg_string,
9-
fmt_docstring,
10-
kwargs_to_strings,
11-
use_alias,
12-
)
13-
from pygmt.io import load_dataarray
6+
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
147

158

169
@fmt_docstring
1710
@use_alias(
1811
C="statistic",
1912
E="empty",
20-
G="outgrid",
2113
I="spacing",
2214
N="normalize",
2315
R="region",
@@ -31,7 +23,7 @@
3123
r="registration",
3224
)
3325
@kwargs_to_strings(I="sequence", R="sequence", i="sequence_comma")
34-
def binstats(data, **kwargs):
26+
def binstats(data, outgrid: str | None = None, **kwargs):
3527
r"""
3628
Bin spatial data and determine statistics per bin.
3729
@@ -110,13 +102,13 @@ def binstats(data, **kwargs):
110102
- None if ``outgrid`` is set (grid output will be stored in file set by
111103
``outgrid``)
112104
"""
113-
with GMTTempFile(suffix=".nc") as tmpfile:
114-
with Session() as lib:
115-
with lib.virtualfile_in(check_kind="vector", data=data) as vintbl:
116-
if (outgrid := kwargs.get("G")) is None:
117-
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
118-
lib.call_module(
119-
module="binstats", args=build_arg_string(kwargs, infile=vintbl)
120-
)
121-
122-
return load_dataarray(outgrid) if outgrid == tmpfile.name else None
105+
with Session() as lib:
106+
with (
107+
lib.virtualfile_in(check_kind="vector", data=data) as vintbl,
108+
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
109+
):
110+
kwargs["G"] = voutgrd
111+
lib.call_module(
112+
module="binstats", args=build_arg_string(kwargs, infile=vintbl)
113+
)
114+
return lib.virtualfile_to_grid(vfname=voutgrd, outgrid=outgrid)

pygmt/src/dimfilter.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44

55
from pygmt.clib import Session
66
from pygmt.exceptions import GMTInvalidInput
7-
from pygmt.helpers import (
8-
GMTTempFile,
9-
build_arg_string,
10-
fmt_docstring,
11-
kwargs_to_strings,
12-
use_alias,
13-
)
14-
from pygmt.io import load_dataarray
7+
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
158

169
__doctest_skip__ = ["dimfilter"]
1710

@@ -20,14 +13,13 @@
2013
@use_alias(
2114
D="distance",
2215
F="filter",
23-
G="outgrid",
2416
I="spacing",
2517
N="sectors",
2618
R="region",
2719
V="verbose",
2820
)
2921
@kwargs_to_strings(I="sequence", R="sequence")
30-
def dimfilter(grid, **kwargs):
22+
def dimfilter(grid, outgrid: str | None = None, **kwargs):
3123
r"""
3224
Filter a grid by dividing the filter circle.
3325
@@ -149,13 +141,13 @@ def dimfilter(grid, **kwargs):
149141
distance, filters, or sectors."""
150142
)
151143

152-
with GMTTempFile(suffix=".nc") as tmpfile:
153-
with Session() as lib:
154-
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
155-
if (outgrid := kwargs.get("G")) is None:
156-
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
157-
lib.call_module(
158-
module="dimfilter", args=build_arg_string(kwargs, infile=vingrd)
159-
)
160-
161-
return load_dataarray(outgrid) if outgrid == tmpfile.name else None
144+
with Session() as lib:
145+
with (
146+
lib.virtualfile_in(check_kind="raster", data=grid) as vingrd,
147+
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
148+
):
149+
kwargs["G"] = voutgrd
150+
lib.call_module(
151+
module="dimfilter", args=build_arg_string(kwargs, infile=vingrd)
152+
)
153+
return lib.virtualfile_to_grid(vfname=voutgrd, outgrid=outgrid)

pygmt/src/grdclip.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,13 @@
33
"""
44

55
from pygmt.clib import Session
6-
from pygmt.helpers import (
7-
GMTTempFile,
8-
build_arg_string,
9-
fmt_docstring,
10-
kwargs_to_strings,
11-
use_alias,
12-
)
13-
from pygmt.io import load_dataarray
6+
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
147

158
__doctest_skip__ = ["grdclip"]
169

1710

1811
@fmt_docstring
1912
@use_alias(
20-
G="outgrid",
2113
R="region",
2214
Sa="above",
2315
Sb="below",
@@ -32,7 +24,7 @@
3224
Si="sequence",
3325
Sr="sequence",
3426
)
35-
def grdclip(grid, **kwargs):
27+
def grdclip(grid, outgrid: str | None = None, **kwargs):
3628
r"""
3729
Set values in a grid that meet certain criteria to a new value.
3830
@@ -95,13 +87,13 @@ def grdclip(grid, **kwargs):
9587
>>> [new_grid.data.min(), new_grid.data.max()]
9688
[0.0, 10000.0]
9789
"""
98-
with GMTTempFile(suffix=".nc") as tmpfile:
99-
with Session() as lib:
100-
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
101-
if (outgrid := kwargs.get("G")) is None:
102-
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
103-
lib.call_module(
104-
module="grdclip", args=build_arg_string(kwargs, infile=vingrd)
105-
)
106-
107-
return load_dataarray(outgrid) if outgrid == tmpfile.name else None
90+
with Session() as lib:
91+
with (
92+
lib.virtualfile_in(check_kind="raster", data=grid) as vingrd,
93+
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
94+
):
95+
kwargs["G"] = voutgrd
96+
lib.call_module(
97+
module="grdclip", args=build_arg_string(kwargs, infile=vingrd)
98+
)
99+
return lib.virtualfile_to_grid(vfname=voutgrd, outgrid=outgrid)

pygmt/src/grdfill.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,20 @@
44

55
from pygmt.clib import Session
66
from pygmt.exceptions import GMTInvalidInput
7-
from pygmt.helpers import (
8-
GMTTempFile,
9-
build_arg_string,
10-
fmt_docstring,
11-
kwargs_to_strings,
12-
use_alias,
13-
)
14-
from pygmt.io import load_dataarray
7+
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
158

169
__doctest_skip__ = ["grdfill"]
1710

1811

1912
@fmt_docstring
2013
@use_alias(
2114
A="mode",
22-
G="outgrid",
2315
N="no_data",
2416
R="region",
2517
V="verbose",
2618
)
2719
@kwargs_to_strings(R="sequence")
28-
def grdfill(grid, **kwargs):
20+
def grdfill(grid, outgrid: str | None = None, **kwargs):
2921
r"""
3022
Fill blank areas from a grid file.
3123
@@ -77,13 +69,14 @@ def grdfill(grid, **kwargs):
7769
"""
7870
if kwargs.get("A") is None and kwargs.get("L") is None:
7971
raise GMTInvalidInput("At least parameter 'mode' or 'L' must be specified.")
80-
with GMTTempFile(suffix=".nc") as tmpfile:
81-
with Session() as lib:
82-
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
83-
if (outgrid := kwargs.get("G")) is None:
84-
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
85-
lib.call_module(
86-
module="grdfill", args=build_arg_string(kwargs, infile=vingrd)
87-
)
8872

89-
return load_dataarray(outgrid) if outgrid == tmpfile.name else None
73+
with Session() as lib:
74+
with (
75+
lib.virtualfile_in(check_kind="raster", data=grid) as vingrd,
76+
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
77+
):
78+
kwargs["G"] = voutgrd
79+
lib.call_module(
80+
module="grdfill", args=build_arg_string(kwargs, infile=vingrd)
81+
)
82+
return lib.virtualfile_to_grid(vfname=voutgrd, outgrid=outgrid)

pygmt/src/grdfilter.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,13 @@
33
"""
44

55
from pygmt.clib import Session
6-
from pygmt.helpers import (
7-
GMTTempFile,
8-
build_arg_string,
9-
fmt_docstring,
10-
kwargs_to_strings,
11-
use_alias,
12-
)
13-
from pygmt.io import load_dataarray
6+
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
147

158

169
@fmt_docstring
1710
@use_alias(
1811
D="distance",
1912
F="filter",
20-
G="outgrid",
2113
I="spacing",
2214
N="nans",
2315
R="region",
@@ -28,7 +20,7 @@
2820
x="cores",
2921
)
3022
@kwargs_to_strings(I="sequence", R="sequence")
31-
def grdfilter(grid, **kwargs):
23+
def grdfilter(grid, outgrid: str | None = None, **kwargs):
3224
r"""
3325
Filter a grid in the space (or time) domain.
3426
@@ -132,13 +124,13 @@ def grdfilter(grid, **kwargs):
132124
>>> grid = pygmt.datasets.load_earth_relief()
133125
>>> smooth_field = pygmt.grdfilter(grid=grid, filter="g600", distance="4")
134126
"""
135-
with GMTTempFile(suffix=".nc") as tmpfile:
136-
with Session() as lib:
137-
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
138-
if (outgrid := kwargs.get("G")) is None:
139-
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
140-
lib.call_module(
141-
module="grdfilter", args=build_arg_string(kwargs, infile=vingrd)
142-
)
143-
144-
return load_dataarray(outgrid) if outgrid == tmpfile.name else None
127+
with Session() as lib:
128+
with (
129+
lib.virtualfile_in(check_kind="raster", data=grid) as vingrd,
130+
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
131+
):
132+
kwargs["G"] = voutgrd
133+
lib.call_module(
134+
module="grdfilter", args=build_arg_string(kwargs, infile=vingrd)
135+
)
136+
return lib.virtualfile_to_grid(vfname=voutgrd, outgrid=outgrid)

pygmt/src/grdgradient.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
from pygmt.clib import Session
66
from pygmt.exceptions import GMTInvalidInput
77
from pygmt.helpers import (
8-
GMTTempFile,
98
args_in_kwargs,
109
build_arg_string,
1110
fmt_docstring,
1211
kwargs_to_strings,
1312
use_alias,
1413
)
15-
from pygmt.io import load_dataarray
1614

1715
__doctest_skip__ = ["grdgradient"]
1816

@@ -22,7 +20,6 @@
2220
A="azimuth",
2321
D="direction",
2422
E="radiance",
25-
G="outgrid",
2623
N="normalize",
2724
Q="tiles",
2825
R="region",
@@ -32,7 +29,7 @@
3229
n="interpolation",
3330
)
3431
@kwargs_to_strings(A="sequence", E="sequence", R="sequence")
35-
def grdgradient(grid, **kwargs):
32+
def grdgradient(grid, outgrid: str | None = None, **kwargs):
3633
r"""
3734
Compute the directional derivative of the vector gradient of the data.
3835
@@ -160,20 +157,20 @@ def grdgradient(grid, **kwargs):
160157
>>> # Create a new grid from an input grid, set the azimuth to 10 degrees,
161158
>>> new_grid = pygmt.grdgradient(grid=grid, azimuth=10)
162159
"""
163-
with GMTTempFile(suffix=".nc") as tmpfile:
164-
if kwargs.get("Q") is not None and kwargs.get("N") is None:
165-
raise GMTInvalidInput("""Must specify normalize if tiles is specified.""")
166-
if not args_in_kwargs(args=["A", "D", "E"], kwargs=kwargs):
167-
raise GMTInvalidInput(
168-
"""At least one of the following parameters must be specified:
169-
azimuth, direction, or radiance"""
160+
if kwargs.get("Q") is not None and kwargs.get("N") is None:
161+
raise GMTInvalidInput("""Must specify normalize if tiles is specified.""")
162+
if not args_in_kwargs(args=["A", "D", "E"], kwargs=kwargs):
163+
raise GMTInvalidInput(
164+
"At least one of the following parameters must be specified: "
165+
"azimuth, direction, or radiance."
166+
)
167+
with Session() as lib:
168+
with (
169+
lib.virtualfile_in(check_kind="raster", data=grid) as vingrd,
170+
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
171+
):
172+
kwargs["G"] = voutgrd
173+
lib.call_module(
174+
module="grdgradient", args=build_arg_string(kwargs, infile=vingrd)
170175
)
171-
with Session() as lib:
172-
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
173-
if (outgrid := kwargs.get("G")) is None:
174-
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
175-
lib.call_module(
176-
module="grdgradient", args=build_arg_string(kwargs, infile=vingrd)
177-
)
178-
179-
return load_dataarray(outgrid) if outgrid == tmpfile.name else None
176+
return lib.virtualfile_to_grid(vfname=voutgrd, outgrid=outgrid)

0 commit comments

Comments
 (0)