Skip to content

Commit

Permalink
Deprecate Basemap dependency (#180)
Browse files Browse the repository at this point in the history
* Deprecate Basemap dependency

* Plot coastline

* Support AzimuthalEquidistant projection

* Catch exception when Cartopy is not installed

* Add option to draw lat lon labels

* Support Geostationary projection

* Fix tests

* Fix SyntaxWarning

* Refactor kwargs options

* Add plot_map optional argument

* Fix interface to basemaps (Cartopy is now used by default)
  • Loading branch information
dnerini authored Nov 21, 2020
1 parent 85d650f commit e5e4a2e
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 428 deletions.
6 changes: 3 additions & 3 deletions examples/my_first_nowcast.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@
"\n",
"# Plot the last rainfall field in the \"training\" data.\n",
"# train_precip[-1] -> Last available composite for nowcasting.\n",
"plot_precip_field(train_precip[-1], geodata=metadata, axis=\"off\", map=\"cartopy\")\n",
"plot_precip_field(train_precip[-1], geodata=metadata, axis=\"off\")\n",
"plt.show() # (This line is actually not needed if you are using jupyter notebooks)"
]
},
Expand Down Expand Up @@ -699,7 +699,7 @@
"source": [
"# Plot precipitation at the end of the forecast period.\n",
"plt.figure(figsize=(9, 5), dpi=100)\n",
"plot_precip_field(precip_forecast[-1], geodata=metadata, axis=\"off\", map='cartopy')\n",
"plot_precip_field(precip_forecast[-1], geodata=metadata, axis=\"off\")\n",
"plt.show()"
]
},
Expand Down Expand Up @@ -829,4 +829,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
2 changes: 1 addition & 1 deletion pysteps/nowcasts/sseps.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def forecast(
print("dask imported: %s" % ("yes" if dask_imported else "no"))
print("num workers: %d" % num_workers)

if vel_pert_method is "bps":
if vel_pert_method == "bps":
vp_par = vel_pert_kwargs.get(
"p_pert_par", noise.motion.get_default_params_bps_par()
)
Expand Down
37 changes: 18 additions & 19 deletions pysteps/tests/test_plt_cartopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,42 @@
from pysteps.visualization import plot_precip_field
from pysteps.utils import to_rainrate
from pysteps.tests.helpers import get_precipitation_fields
import matplotlib.pyplot as pl
import matplotlib.pyplot as plt

pytest.importorskip("cartopy")

plt_arg_names = ("source", "plot_map", "drawlonlatlines", "lw")
plt_arg_names = ("source", "map_kwargs", "pass_geodata")

plt_arg_values = [
("mch", "cartopy", False, 0.5),
("mch", "cartopy", True, 1.0),
("bom", "cartopy", True, 0.5),
("fmi", "cartopy", True, 0.5),
("knmi", "cartopy", True, 0.5),
("opera", "cartopy", True, 0.5),
("mrms", "cartopy", True, 0.5),
("mch", {"drawlonlatlines": False, "lw": 0.5, "plot_map": None}, False),
("mch", {"drawlonlatlines": False, "lw": 0.5, "plot_map": "cartopy"}, False),
("mch", {"drawlonlatlines": False, "lw": 0.5}, True),
("mch", {"drawlonlatlines": True, "lw": 1.0}, True),
("bom", {"drawlonlatlines": True, "lw": 0.5}, True),
("fmi", {"drawlonlatlines": True, "lw": 0.5}, True),
("knmi", {"drawlonlatlines": True, "lw": 0.5}, True),
("opera", {"drawlonlatlines": True, "lw": 0.5}, True),
("mrms", {"drawlonlatlines": True, "lw": 0.5}, True),
("saf", {"drawlonlatlines": True, "lw": 0.5}, True),
]


@pytest.mark.parametrize(plt_arg_names, plt_arg_values)
def test_visualization_plot_precip_field(source, plot_map, drawlonlatlines, lw):
def test_visualization_plot_precip_field(source, map_kwargs, pass_geodata):

field, metadata = get_precipitation_fields(0, 0, True, True, None, source)
field = field.squeeze()
field, __ = to_rainrate(field, metadata)

if not pass_geodata:
metadata = None

ax = plot_precip_field(
field,
type="intensity",
geodata=metadata,
plot_map=plot_map,
drawlonlatlines=drawlonlatlines,
lw=lw,
field, type="intensity", geodata=metadata, map_kwargs=map_kwargs,
)
pl.close()


if __name__ == "__main__":

for i, args in enumerate(plt_arg_values):
test_visualization_plot_precip_field(*args)
pl.show()
plt.show()
70 changes: 21 additions & 49 deletions pysteps/tests/test_plt_motionfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,29 @@
"axis",
"step",
"quiver_kwargs",
"plot_map",
"drawlonlatlines",
"lw",
"map_kwargs",
"upscale",
"pass_geodata",
)

arg_values_quiver = [
(None, "off", 10, None, None, False, 0.5, None),
("bom", "on", 10, None, None, False, 0.5, 4000),
("bom", "on", 10, None, "cartopy", True, 0.5, 4000),
("mch", "on", 20, None, "cartopy", False, 0.5, 2000),
("bom", "off", 10, None, "basemap", False, 0.5, 4000),
(None, "off", 10, {}, {"drawlonlatlines": False, "lw": 0.5}, None, False),
("bom", "on", 10, {}, {"drawlonlatlines": False, "lw": 0.5}, 4000, False),
("bom", "on", 10, {}, {"drawlonlatlines": True, "lw": 0.5}, 4000, True),
("mch", "on", 20, {}, {"drawlonlatlines": False, "lw": 0.5}, 2000, True),
]


@pytest.mark.parametrize(arg_names_quiver, arg_values_quiver)
def test_visualization_motionfields_quiver(
source, axis, step, quiver_kwargs, plot_map, drawlonlatlines, lw, upscale,
source, axis, step, quiver_kwargs, map_kwargs, upscale, pass_geodata,
):

if plot_map == "cartopy":
pytest.importorskip("cartopy")
elif plot_map == "basemap":
pytest.importorskip("basemap")

if source is not None:
fields, geodata = get_precipitation_fields(0, 2, False, True, upscale, source)
ax = plot_precip_field(fields[-1], geodata=geodata, plot_map=plot_map,)
if not pass_geodata:
geodata = None
ax = plot_precip_field(fields[-1], geodata=geodata)
oflow_method = motion.get_method("LK")
UV = oflow_method(fields)

Expand All @@ -54,49 +49,35 @@ def test_visualization_motionfields_quiver(
U, V = np.meshgrid(u, v)
UV = np.concatenate([U[None, :], V[None, :]])

__ = quiver(
UV,
ax,
geodata,
axis,
step,
quiver_kwargs,
plot_map=plot_map,
drawlonlatlines=drawlonlatlines,
lw=lw,
)
__ = quiver(UV, ax, geodata, axis, step, quiver_kwargs, map_kwargs=map_kwargs,)


arg_names_streamplot = (
"source",
"axis",
"streamplot_kwargs",
"plot_map",
"drawlonlatlines",
"lw",
"map_kwargs",
"upscale",
"pass_geodata",
)

arg_values_streamplot = [
(None, "off", None, None, False, 0.5, None),
("bom", "on", None, None, False, 0.5, 4000),
("bom", "on", {"density": 0.1}, "cartopy", True, 0.5, 4000),
(None, "off", {}, {"drawlonlatlines": False, "lw": 0.5}, None, False),
("bom", "on", {}, {"drawlonlatlines": False, "lw": 0.5}, 4000, False),
("bom", "on", {"density": 0.5}, {"drawlonlatlines": True, "lw": 0.5}, 4000, True),
]


@pytest.mark.parametrize(arg_names_streamplot, arg_values_streamplot)
def test_visualization_motionfields_streamplot(
source, axis, streamplot_kwargs, plot_map, drawlonlatlines, lw, upscale,
source, axis, streamplot_kwargs, map_kwargs, upscale, pass_geodata
):

if plot_map == "cartopy":
pytest.importorskip("cartopy")
elif plot_map == "basemap":
pytest.importorskip("basemap")

if source is not None:
fields, geodata = get_precipitation_fields(0, 2, False, True, upscale, source)
ax = plot_precip_field(fields[-1], geodata=geodata, plot_map=plot_map,)
if not pass_geodata:
pass_geodata = None
ax = plot_precip_field(fields[-1], geodata=geodata)
oflow_method = motion.get_method("LK")
UV = oflow_method(fields)

Expand All @@ -109,16 +90,7 @@ def test_visualization_motionfields_streamplot(
U, V = np.meshgrid(u, v)
UV = np.concatenate([U[None, :], V[None, :]])

__ = streamplot(
UV,
ax,
geodata,
axis,
streamplot_kwargs,
plot_map=plot_map,
drawlonlatlines=drawlonlatlines,
lw=lw,
)
__ = streamplot(UV, ax, geodata, axis, streamplot_kwargs, map_kwargs=map_kwargs,)


if __name__ == "__main__":
Expand Down
18 changes: 4 additions & 14 deletions pysteps/tests/test_plt_precipfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pysteps.utils import conversion
from pysteps.postprocessing import ensemblestats
from pysteps.tests.helpers import get_precipitation_fields
import matplotlib.pyplot as pl
import matplotlib.pyplot as plt

plt_arg_names = (
"source",
Expand All @@ -28,16 +28,7 @@
("bom", "intensity", None, "pysteps", None, None, True, "on"),
("fmi", "intensity", None, "pysteps", None, None, True, "on"),
("knmi", "intensity", None, "pysteps", None, None, True, "on"),
(
"knmi",
"intensity",
[2e2, -4.1e3, 5e2, -3.8e3],
"pysteps",
None,
None,
True,
"on",
),
("knmi", "intensity", [300, 300, 500, 500], "pysteps", None, None, True, "on",),
("opera", "intensity", None, "pysteps", None, None, True, "on"),
("saf", "intensity", None, "pysteps", None, None, True, "on"),
]
Expand Down Expand Up @@ -70,19 +61,18 @@ def test_visualization_plot_precip_field(
field,
type=type,
bbox=bbox,
geodata=metadata,
geodata=None,
colorscale=colorscale,
probthr=probthr,
units=metadata["unit"],
title=title,
colorbar=colorbar,
axis=axis,
)
pl.close()


if __name__ == "__main__":

for i, args in enumerate(plt_arg_values):
test_visualization_plot_precip_field(*args)
pl.show()
plt.show()
Loading

0 comments on commit e5e4a2e

Please sign in to comment.