Skip to content

Commit d0befd5

Browse files
committed
Allow passing None explicitly to pygmt functions Part 3
1 parent 15c3b6e commit d0befd5

13 files changed

+26
-35
lines changed

pygmt/figure.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def psconvert(self, icc_gray=False, **kwargs):
222222
"""
223223
kwargs = self._preprocess(**kwargs)
224224
# Default cropping the figure to True
225-
if "A" not in kwargs:
225+
if kwargs.get("A") is None:
226226
kwargs["A"] = ""
227227

228228
if icc_gray:
@@ -231,7 +231,7 @@ def psconvert(self, icc_gray=False, **kwargs):
231231
" and will be removed in v0.8.0."
232232
)
233233
warnings.warn(msg, category=FutureWarning, stacklevel=2)
234-
if "N" not in kwargs:
234+
if kwargs.get("N") is None:
235235
kwargs["N"] = "+i"
236236
else:
237237
kwargs["N"] += "+i"

pygmt/src/grdclip.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,8 @@ def grdclip(grid, **kwargs):
104104
with Session() as lib:
105105
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
106106
with file_context as infile:
107-
if "G" not in kwargs: # if outgrid is unset, output to tempfile
108-
kwargs.update({"G": tmpfile.name})
109-
outgrid = kwargs["G"]
107+
if (outgrid := kwargs.get("G")) is None:
108+
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
110109
lib.call_module("grdclip", build_arg_string(kwargs, infile=infile))
111110

112111
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/grdcut.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,8 @@ def grdcut(grid, **kwargs):
106106
with Session() as lib:
107107
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
108108
with file_context as infile:
109-
if "G" not in kwargs: # if outgrid is unset, output to tempfile
110-
kwargs.update({"G": tmpfile.name})
111-
outgrid = kwargs["G"]
109+
if (outgrid := kwargs.get("G")) is None:
110+
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
112111
lib.call_module("grdcut", build_arg_string(kwargs, infile=infile))
113112

114113
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/grdfill.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,14 @@ def grdfill(grid, **kwargs):
6363
- None if ``outgrid`` is set (grid output will be stored in file set by
6464
``outgrid``)
6565
"""
66-
if "A" not in kwargs and "L" not in kwargs:
66+
if kwargs.get("A") is None and kwargs.get("L") is None:
6767
raise GMTInvalidInput("At least parameter 'mode' or 'L' must be specified.")
6868
with GMTTempFile(suffix=".nc") as tmpfile:
6969
with Session() as lib:
7070
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
7171
with file_context as infile:
72-
if "G" not in kwargs: # if outgrid is unset, output to tempfile
73-
kwargs.update({"G": tmpfile.name})
74-
outgrid = kwargs["G"]
72+
if (outgrid := kwargs.get("G")) is None:
73+
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
7574
lib.call_module("grdfill", build_arg_string(kwargs, infile=infile))
7675

7776
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/grdfilter.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,8 @@ def grdfilter(grid, **kwargs):
145145
with Session() as lib:
146146
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
147147
with file_context as infile:
148-
if "G" not in kwargs: # if outgrid is unset, output to tempfile
149-
kwargs.update({"G": tmpfile.name})
150-
outgrid = kwargs["G"]
148+
if (outgrid := kwargs.get("G")) is None:
149+
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
151150
lib.call_module("grdfilter", build_arg_string(kwargs, infile=infile))
152151

153152
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/grdlandmask.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,13 @@ def grdlandmask(**kwargs):
101101
>>> # and a y-range of 30 to 35
102102
>>> landmask = pygmt.grdlandmask(spacing=1, region=[125, 130, 30, 35])
103103
"""
104-
if "I" not in kwargs or "R" not in kwargs:
104+
if kwargs.get("I") is None or kwargs.get("R") is None:
105105
raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.")
106106

107107
with GMTTempFile(suffix=".nc") as tmpfile:
108108
with Session() as lib:
109-
if "G" not in kwargs: # if outgrid is unset, output to tempfile
110-
kwargs.update({"G": tmpfile.name})
111-
outgrid = kwargs["G"]
109+
if (outgrid := kwargs.get("G")) is None:
110+
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
112111
lib.call_module("grdlandmask", build_arg_string(kwargs))
113112

114113
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/grdproject.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,14 @@ def grdproject(grid, **kwargs):
112112
>>> # to "rectangular"
113113
>>> new_grid = pygmt.grdproject(grid=grid, projection="M10c", inverse=True)
114114
"""
115-
if "J" not in kwargs:
115+
if kwargs.get("J") is None:
116116
raise GMTInvalidInput("The projection must be specified.")
117117
with GMTTempFile(suffix=".nc") as tmpfile:
118118
with Session() as lib:
119119
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
120120
with file_context as infile:
121-
if "G" not in kwargs: # if outgrid is unset, output to tempfile
122-
kwargs.update({"G": tmpfile.name})
123-
outgrid = kwargs["G"]
121+
if (outgrid := kwargs.get("G")) is None:
122+
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
124123
lib.call_module("grdproject", build_arg_string(kwargs, infile=infile))
125124

126125
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/grdsample.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ def grdsample(grid, **kwargs):
9696
with Session() as lib:
9797
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
9898
with file_context as infile:
99-
if "G" not in kwargs: # if outgrid is unset, output to tempfile
100-
kwargs.update({"G": tmpfile.name})
101-
outgrid = kwargs["G"]
99+
if (outgrid := kwargs.get("G")) is None:
100+
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
102101
lib.call_module("grdsample", build_arg_string(kwargs, infile=infile))
103102

104103
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def info(data, **kwargs):
9090
)
9191
result = tmpfile.read()
9292

93-
if any(arg in kwargs for arg in ["C", "I", "T"]):
93+
if any(kwargs.get(arg) is not None for arg in ["C", "I", "T"]):
9494
# Converts certain output types into a numpy array
9595
# instead of a raw string that is less useful.
9696
if result.startswith(("-R", "-T")): # e.g. -R0/1/2/3 or -T0/9/1

pygmt/src/legend.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ def legend(self, spec=None, position="JTR+jTR+o0.2c", box="+gwhite+p1p", **kwarg
7474
"""
7575
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
7676

77-
if "D" not in kwargs:
77+
if kwargs.get("D") is None:
7878
kwargs["D"] = position
79-
80-
if "F" not in kwargs:
79+
if kwargs.get("F") is None:
8180
kwargs["F"] = box
8281

8382
with Session() as lib:

pygmt/src/sph2grd.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ def sph2grd(data, **kwargs):
7979
with Session() as lib:
8080
file_context = lib.virtualfile_from_data(check_kind="vector", data=data)
8181
with file_context as infile:
82-
if "G" not in kwargs: # if outgrid is unset, output to tempfile
83-
kwargs.update({"G": tmpfile.name})
84-
outgrid = kwargs["G"]
82+
if (outgrid := kwargs.get("G")) is None:
83+
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
8584
lib.call_module("sph2grd", build_arg_string(kwargs, infile=infile))
8685

8786
return load_dataarray(outgrid) if outgrid == tmpfile.name else None

pygmt/src/sphdistance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def sphdistance(data=None, x=None, y=None, **kwargs):
100100
- None if ``outgrid`` is set (grid output will be stored in file set by
101101
``outgrid``)
102102
"""
103-
if "I" not in kwargs or "R" not in kwargs:
103+
if kwargs.get("I") is None or kwargs.get("R") is None:
104104
raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.")
105105
with GMTTempFile(suffix=".nc") as tmpfile:
106106
with Session() as lib:

pygmt/src/xyz2grd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def xyz2grd(data=None, x=None, y=None, z=None, **kwargs):
148148
... x=xx, y=yy, z=zz, spacing=(1.0, 0.5), region=[0, 3, 10, 13]
149149
... )
150150
"""
151-
if "I" not in kwargs or "R" not in kwargs:
151+
if kwargs.get("I") is None or kwargs.get("R") is None:
152152
raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.")
153153

154154
with GMTTempFile(suffix=".nc") as tmpfile:

0 commit comments

Comments
 (0)