Skip to content

Commit c102784

Browse files
committed
Merge branch 'master' into docstring-formatter
2 parents 09d1d5c + 9a5858f commit c102784

File tree

6 files changed

+173
-2
lines changed

6 files changed

+173
-2
lines changed

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2017-2020 The PyGMT Developers
1+
Copyright (c) 2017-2021 The PyGMT Developers
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without modification,

pygmt/base_plotting.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pygmt.clib import Session
1010
from pygmt.exceptions import GMTError, GMTInvalidInput
1111
from pygmt.helpers import (
12+
args_in_kwargs,
1213
build_arg_string,
1314
data_kind,
1415
dummy_context,
@@ -63,6 +64,7 @@ def _preprocess(self, **kwargs): # pylint: disable=no-self-use
6364
C="lakes",
6465
B="frame",
6566
D="resolution",
67+
E="dcw",
6668
I="rivers",
6769
L="map_scale",
6870
N="borders",
@@ -141,12 +143,36 @@ def coast(self, **kwargs):
141143
shorelines : str
142144
``'[level/]pen'``
143145
Draw shorelines [Default is no shorelines]. Append pen attributes.
146+
dcw : str or list
147+
*code1,code2,…*\ [**+l**\|\ **L**\ ][**+g**\ *fill*\ ]
148+
[**+p**\ *pen*\ ][**+z**]
149+
Select painting or dumping country polygons from the
150+
`Digital Chart of the World
151+
<https://en.wikipedia.org/wiki/Digital_Chart_of_the_World>`__.
152+
Append one or more comma-separated countries using the 2-character
153+
`ISO 3166-1 alpha-2 convention
154+
<https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2>`__.
155+
To select a state of a country (if available), append
156+
.\ *state*, (e.g, US.TX for Texas). To specify a whole continent,
157+
prepend **=** to any of the continent codes (e.g. =EU for Europe).
158+
Append **+p**\ *pen* to draw polygon outlines
159+
(default is no outline) and **+g**\ *fill* to fill them
160+
(default is no fill). Append **+l**\|\ **+L** to *=continent* to
161+
only list countries in that continent; repeat if more than one
162+
continent is requested. Append **+z** to place the country code in
163+
the segment headers via **-Z**\ *code* settings.To apply different
164+
settings to different countries, pass a list of string arguments.
144165
{XY}
145166
{p}
146167
{t}
147168
148169
"""
149170
kwargs = self._preprocess(**kwargs)
171+
if not args_in_kwargs(args=["C", "G", "S", "I", "N", "Q", "W"], kwargs=kwargs):
172+
raise GMTInvalidInput(
173+
"""At least one of the following arguments must be specified:
174+
lakes, land, water, rivers, borders, Q, or shorelines"""
175+
)
150176
with Session() as lib:
151177
lib.call_module("coast", build_arg_string(kwargs))
152178

pygmt/helpers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pygmt.helpers.decorators import fmt_docstring, kwargs_to_strings, use_alias
55
from pygmt.helpers.tempfile import GMTTempFile, unique_name
66
from pygmt.helpers.utils import (
7+
args_in_kwargs,
78
build_arg_string,
89
data_kind,
910
dummy_context,

pygmt/helpers/utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,29 @@ def launch_external_viewer(fname):
220220
subprocess.run(["open", fname], check=False, **run_args)
221221
else:
222222
webbrowser.open_new_tab("file://{}".format(fname))
223+
224+
225+
def args_in_kwargs(args, kwargs):
226+
"""
227+
Take a list and a dictionary, and determine if any entries in the list are
228+
keys in the dictionary.
229+
230+
This function is used to determine if at least one of the required
231+
arguments is passed to raise a GMTInvalidInput Error.
232+
233+
Parameters
234+
----------
235+
args : list
236+
List of required arguments, using the GMT short-form aliases.
237+
238+
kwargs : dict
239+
The dictionary of kwargs is the format returned by the _preprocess
240+
function of the BasePlotting class. The keys are the GMT
241+
short-form aliases of the parameters.
242+
243+
Returns
244+
--------
245+
bool
246+
If one of the required arguments is in ``kwargs``.
247+
"""
248+
return any(arg in kwargs for arg in args)

pygmt/tests/test_coast.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
import pytest
55
from pygmt import Figure
6+
from pygmt.exceptions import GMTInvalidInput
67
from pygmt.helpers.testing import check_figures_equal
78

89

@@ -57,6 +58,7 @@ def test_coast_aliases():
5758
Y="a10c",
5859
p="135/25",
5960
t=13,
61+
E="MA+gred",
6062
C="blue",
6163
)
6264
fig_test.coast(
@@ -75,6 +77,7 @@ def test_coast_aliases():
7577
yshift="a10c", # Y
7678
perspective=[135, 25], # p
7779
transparency=13, # t
80+
dcw="MA+gred", # E
7881
lakes="blue", # C
7982
)
8083
return fig_ref, fig_test
@@ -93,3 +96,98 @@ def test_coast_world_mercator():
9396
water="white",
9497
)
9598
return fig
99+
100+
101+
def test_coast_required_args():
102+
"Test if fig.coast fails when not given required arguments"
103+
fig = Figure()
104+
with pytest.raises(GMTInvalidInput):
105+
fig.coast(region="EG")
106+
107+
108+
@check_figures_equal()
109+
def test_coast_dcw_single():
110+
"Test passing a single country code to dcw"
111+
fig_ref, fig_test = Figure(), Figure()
112+
# Use single-character arguments for the reference image
113+
fig_ref.coast(R="-10/15/25/44", J="M15c", B="a", G="brown", E="ES+gbisque+pblue")
114+
fig_test.coast(
115+
region=[-10, 15, 25, 44],
116+
frame="a",
117+
projection="M15c",
118+
land="brown",
119+
dcw="ES+gbisque+pblue",
120+
)
121+
return fig_ref, fig_test
122+
123+
124+
@check_figures_equal()
125+
def test_coast_dcw_multiple():
126+
"Test passing multiple country code to dcw"
127+
fig_ref, fig_test = Figure(), Figure()
128+
# Use single-character arguments for the reference image
129+
fig_ref.coast(R="-10/15/25/44", J="M15c", B="a", G="brown", E="ES,IT+gbisque+pblue")
130+
fig_test.coast(
131+
region=[-10, 15, 25, 44],
132+
frame="a",
133+
projection="M15c",
134+
land="brown",
135+
dcw="ES,IT+gbisque+pblue",
136+
)
137+
return fig_ref, fig_test
138+
139+
140+
@check_figures_equal()
141+
def test_coast_dcw_list():
142+
"Test passing a list of country codes and fill options to dcw"
143+
fig_ref, fig_test = Figure(), Figure()
144+
# Use single-character arguments for the reference image
145+
fig_ref.coast(
146+
R="-10/15/25/44",
147+
J="M15c",
148+
B="a",
149+
G="brown",
150+
E=["ES+gbisque+pgreen", "IT+gcyan+pblue"],
151+
)
152+
fig_test.coast(
153+
region=[-10, 15, 25, 44],
154+
frame="a",
155+
projection="M15c",
156+
land="brown",
157+
dcw=["ES+gbisque+pgreen", "IT+gcyan+pblue"],
158+
)
159+
return fig_ref, fig_test
160+
161+
162+
@check_figures_equal()
163+
def test_coast_dcw_continent():
164+
"Test passing a continent code to dcw"
165+
fig_ref, fig_test = Figure(), Figure()
166+
# Use single-character arguments for the reference image
167+
fig_ref.coast(R="-10/15/25/44", J="M15c", B="a", G="brown", E="=AF+gbisque+pblue")
168+
fig_test.coast(
169+
region=[-10, 15, 25, 44],
170+
frame="a",
171+
projection="M15c",
172+
land="brown",
173+
dcw="=AF+gbisque+pblue",
174+
)
175+
return fig_ref, fig_test
176+
177+
178+
@check_figures_equal()
179+
def test_coast_dcw_state():
180+
"Test passing a US state code to dcw"
181+
fig_ref, fig_test = Figure(), Figure()
182+
# Use single-character arguments for the reference image
183+
fig_ref.coast(
184+
R="-75/-69/40/44", J="M15c", B="a", G="brown", E="US.MA+gbisque+pblue"
185+
)
186+
fig_test.coast(
187+
region=[-75, -69, 40, 44],
188+
frame="a",
189+
projection="M15c",
190+
land="brown",
191+
dcw="US.MA+gbisque+pblue",
192+
)
193+
return fig_ref, fig_test

pygmt/tests/test_helpers.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
import numpy as np
77
import pytest
88
from pygmt.exceptions import GMTInvalidInput
9-
from pygmt.helpers import GMTTempFile, data_kind, kwargs_to_strings, unique_name
9+
from pygmt.helpers import (
10+
GMTTempFile,
11+
args_in_kwargs,
12+
data_kind,
13+
kwargs_to_strings,
14+
unique_name,
15+
)
1016

1117

1218
@pytest.mark.parametrize(
@@ -90,3 +96,17 @@ def test_gmttempfile_read():
9096
ftmp.write("in.dat: N = 2\t<1/3>\t<2/4>\n")
9197
assert tmpfile.read() == "in.dat: N = 2 <1/3> <2/4>\n"
9298
assert tmpfile.read(keep_tabs=True) == "in.dat: N = 2\t<1/3>\t<2/4>\n"
99+
100+
101+
def test_args_in_kwargs():
102+
"Test that args_in_kwargs function returns correct Boolean responses."
103+
kwargs = {"A": 1, "B": 2, "C": 3}
104+
# Passing list of arguments with passing values in the beginning
105+
passing_args_1 = ["B", "C", "D"]
106+
assert args_in_kwargs(args=passing_args_1, kwargs=kwargs)
107+
# Passing list of arguments that starts with failing arguments
108+
passing_args_2 = ["D", "X", "C"]
109+
assert args_in_kwargs(args=passing_args_2, kwargs=kwargs)
110+
# Failing list of arguments
111+
failing_args = ["D", "E", "F"]
112+
assert not args_in_kwargs(args=failing_args, kwargs=kwargs)

0 commit comments

Comments
 (0)