Skip to content

Allow passing None explicitly to pygmt functions Part 3 #1872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def psconvert(self, icc_gray=False, **kwargs):
"""
kwargs = self._preprocess(**kwargs)
# Default cropping the figure to True
if "A" not in kwargs:
if kwargs.get("A") is None:
kwargs["A"] = ""

if icc_gray:
Expand All @@ -231,7 +231,7 @@ def psconvert(self, icc_gray=False, **kwargs):
" and will be removed in v0.8.0."
)
warnings.warn(msg, category=FutureWarning, stacklevel=2)
if "N" not in kwargs:
if kwargs.get("N") is None:
kwargs["N"] = "+i"
else:
kwargs["N"] += "+i"
Expand Down
5 changes: 2 additions & 3 deletions pygmt/src/grdclip.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ def grdclip(grid, **kwargs):
with Session() as lib:
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
with file_context as infile:
if "G" not in kwargs: # if outgrid is unset, output to tempfile
kwargs.update({"G": tmpfile.name})
outgrid = kwargs["G"]
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module("grdclip", build_arg_string(kwargs, infile=infile))

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
5 changes: 2 additions & 3 deletions pygmt/src/grdcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ def grdcut(grid, **kwargs):
with Session() as lib:
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
with file_context as infile:
if "G" not in kwargs: # if outgrid is unset, output to tempfile
kwargs.update({"G": tmpfile.name})
outgrid = kwargs["G"]
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module("grdcut", build_arg_string(kwargs, infile=infile))

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
7 changes: 3 additions & 4 deletions pygmt/src/grdfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ def grdfill(grid, **kwargs):
- None if ``outgrid`` is set (grid output will be stored in file set by
``outgrid``)
"""
if "A" not in kwargs and "L" not in kwargs:
if kwargs.get("A") is None and kwargs.get("L") is None:
raise GMTInvalidInput("At least parameter 'mode' or 'L' must be specified.")
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
with file_context as infile:
if "G" not in kwargs: # if outgrid is unset, output to tempfile
kwargs.update({"G": tmpfile.name})
outgrid = kwargs["G"]
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module("grdfill", build_arg_string(kwargs, infile=infile))

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
5 changes: 2 additions & 3 deletions pygmt/src/grdfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,8 @@ def grdfilter(grid, **kwargs):
with Session() as lib:
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
with file_context as infile:
if "G" not in kwargs: # if outgrid is unset, output to tempfile
kwargs.update({"G": tmpfile.name})
outgrid = kwargs["G"]
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module("grdfilter", build_arg_string(kwargs, infile=infile))

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
7 changes: 3 additions & 4 deletions pygmt/src/grdlandmask.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,13 @@ def grdlandmask(**kwargs):
>>> # and a y-range of 30 to 35
>>> landmask = pygmt.grdlandmask(spacing=1, region=[125, 130, 30, 35])
"""
if "I" not in kwargs or "R" not in kwargs:
if kwargs.get("I") is None or kwargs.get("R") is None:
raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.")

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

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
7 changes: 3 additions & 4 deletions pygmt/src/grdproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,14 @@ def grdproject(grid, **kwargs):
>>> # to "rectangular"
>>> new_grid = pygmt.grdproject(grid=grid, projection="M10c", inverse=True)
"""
if "J" not in kwargs:
if kwargs.get("J") is None:
raise GMTInvalidInput("The projection must be specified.")
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
with file_context as infile:
if "G" not in kwargs: # if outgrid is unset, output to tempfile
kwargs.update({"G": tmpfile.name})
outgrid = kwargs["G"]
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module("grdproject", build_arg_string(kwargs, infile=infile))

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
5 changes: 2 additions & 3 deletions pygmt/src/grdsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ def grdsample(grid, **kwargs):
with Session() as lib:
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
with file_context as infile:
if "G" not in kwargs: # if outgrid is unset, output to tempfile
kwargs.update({"G": tmpfile.name})
outgrid = kwargs["G"]
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module("grdsample", build_arg_string(kwargs, infile=infile))

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
2 changes: 1 addition & 1 deletion pygmt/src/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def info(data, **kwargs):
)
result = tmpfile.read()

if any(arg in kwargs for arg in ["C", "I", "T"]):
if any(kwargs.get(arg) is not None for arg in ["C", "I", "T"]):
# Converts certain output types into a numpy array
# instead of a raw string that is less useful.
if result.startswith(("-R", "-T")): # e.g. -R0/1/2/3 or -T0/9/1
Expand Down
5 changes: 2 additions & 3 deletions pygmt/src/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ def legend(self, spec=None, position="JTR+jTR+o0.2c", box="+gwhite+p1p", **kwarg
"""
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access

if "D" not in kwargs:
if kwargs.get("D") is None:
kwargs["D"] = position

if "F" not in kwargs:
if kwargs.get("F") is None:
kwargs["F"] = box

with Session() as lib:
Expand Down
5 changes: 2 additions & 3 deletions pygmt/src/sph2grd.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ def sph2grd(data, **kwargs):
with Session() as lib:
file_context = lib.virtualfile_from_data(check_kind="vector", data=data)
with file_context as infile:
if "G" not in kwargs: # if outgrid is unset, output to tempfile
kwargs.update({"G": tmpfile.name})
outgrid = kwargs["G"]
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module("sph2grd", build_arg_string(kwargs, infile=infile))

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
2 changes: 1 addition & 1 deletion pygmt/src/sphdistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def sphdistance(data=None, x=None, y=None, **kwargs):
- None if ``outgrid`` is set (grid output will be stored in file set by
``outgrid``)
"""
if "I" not in kwargs or "R" not in kwargs:
if kwargs.get("I") is None or kwargs.get("R") is None:
raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.")
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/xyz2grd.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def xyz2grd(data=None, x=None, y=None, z=None, **kwargs):
... x=xx, y=yy, z=zz, spacing=(1.0, 0.5), region=[0, 3, 10, 13]
... )
"""
if "I" not in kwargs or "R" not in kwargs:
if kwargs.get("I") is None or kwargs.get("R") is None:
raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.")

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