def test_create_xarray_dataarray(self): img = np.zeros((360, 10), dtype=np.float32) r = np.arange(0, 100000, 10000) az = np.arange(0, 360) th = np.zeros_like(az) proj = georef.epsg_to_osr(4326) with pytest.raises(TypeError): georef.create_xarray_dataarray(img) georef.create_xarray_dataarray(img, r, az, th, proj=proj)
def setUp(self): img = np.zeros((360, 10), dtype=np.float32) img[2, 2] = 10 # isolated pixel img[5, 6:8] = 10 # line img[20, :] = 5 # spike img[60:120, 2:7] = 11 # precip field self.r = np.arange(0, 100000, 10000) self.az = np.arange(0, 360) self.el = np.arange(0, 90) self.th = np.zeros_like(self.az) self.az1 = np.ones_like(self.el) * 225 self.img = img self.proj = georef.create_osr("dwd-radolan") self.da_ppi = georef.create_xarray_dataarray(img, self.r, self.az, self.th) self.da_ppi = georef.georeference_dataset(self.da_ppi, proj=None) self.da_rhi = georef.create_xarray_dataarray(img[0:90], self.r, self.az1, self.el) self.da_rhi = georef.georeference_dataset(self.da_rhi, proj=None)
def plot_rhi(data, r=None, th=None, th_res=None, az=0, site=None, proj=None, rf=1., fig=None, ax=111, **kwargs): """Plots a Range Height Indicator (RHI). This is a small wrapper around xarray dataarray. The radar data, coordinates and metadata is transformed into an xarray dataarray. Using the wradlib dataarray accessor the dataarray is enabled to plot polar data. Using ``cg=True`` the plotting is done in a curvelinear grid axes. Additional data can be plotted in polar coordinates or cartesian coordinates depending which axes object is used. ``**kwargs`` may be used to try to influence the :func:`matplotlib.pyplot.pcolormesh`, :func:`matplotlib.pyplot.contour` and :func:`matplotlib.pyplot.contourf` routines under the hood. Parameters ---------- data : :class:`numpy:numpy.ndarray` The data to be plotted. It is assumed that the first dimension is over the elevation angles, while the second dimension is over the range bins r : :class:`numpy:numpy.ndarray` The ranges. Units may be chosen arbitrarily. If None, a default is calculated from the dimensions of ``data``. rf: float If present, factor for scaling range axis, defaults to 1. th : :class:`numpy:numpy.ndarray` The elevation angles in degrees. If None, a default is calculated from the dimensions of ``data``. th_res : float or :class:`numpy:numpy.ndarray` of same shape as ``th`` In RHI's it happens that the elevation angles are spaced wider than the beam width. If this beam width (in degrees) is given in ``th_res``, plot_rhi will plot the beams accordingly. Otherwise the behavior of :func:`matplotlib.pyplot.pcolormesh` assumes all beams to be adjacent to each other, which might lead to unexpected results. az : float or :class:`numpy:numpy.ndarray` of same shape as ``th`` site : tuple Tuple of coordinates of the radar site. If ``proj`` is not used, this simply becomes the offset for the origin of the coordinate system. If ``proj`` is used, values must be given as (longitude, latitude, altitude) tuple of geographical coordinates. proj : osr spatial reference object GDAL OSR Spatial Reference Object describing projection If this parameter is not None, ``site`` must be set. Then the function will attempt to georeference the radar bins in the coordinate system defined by the projection string. fig : :class:`matplotlib:matplotlib.figure.Figure` If given, the RHI will be plotted into this figure object. Axes are created as needed. If None, a new figure object will be created or current figure will be used, depending on ``ax``. ax : :class:`matplotlib:matplotlib.axes.Axes` | matplotlib grid definition If matplotlib Axes object is given, the RHI will be plotted into this axes object. If matplotlib grid definition is given (nrows/ncols/plotnumber), axis are created in the specified place. Defaults to '111', only one subplot/axis. func : str Name of plotting function to be used under the hood. Defaults to 'pcolormesh'. 'contour' and 'contourf' can be selected too. See also -------- :func:`wradlib.vis.create_cg` : creation of curvelinear grid axes objects Returns ------- ax : :class:`matplotlib:matplotlib.axes.Axes` The axes object into which the RHI was plotted. pm : :class:`matplotlib:matplotlib.collections.QuadMesh` | \ :class:`matplotlib:matplotlib.contour.QuadContourSet` The result of the plotting function. Necessary, if you want to add a colorbar to the plot. Note ---- If proj=``cg``, the ``cgax`` - curvelinear Axes (r-theta-grid) is returned. ``caax`` - Cartesian Axes (x-y-grid) and ``paax`` - parasite axes object for plotting polar data can be derived like this:: caax = cgax.parasites[0] paax = cgax.parasites[1] The function :func:`~wradlib.vis.create_cg` uses the Matplotlib AXISARTIST namespace `mpl_toolkits.axisartist`_. Here are some limitations to normal Matplotlib Axes. See `AxesGridToolkitUserGuide`_. Examples -------- See :ref:`/notebooks/visualisation/wradlib_plot_curvelinear_grids.ipynb`. .. _mpl_toolkits.axisartist: https://matplotlib.org/mpl_toolkits/axes_grid/users/axisartist.html .. _AxesGridToolkitUserGuide: https://matplotlib.org/mpl_toolkits/axes_grid/users/index.html """ # kwargs handling kwargs['zorder'] = kwargs.pop('zorder', 0) func = kwargs.pop('func', 'pcolormesh') # re/ke kwargs handling kw_spherical = { 're': kwargs.pop('re', None), 'ke': kwargs.pop('ke', 4. / 3.) } if th is None: th = np.linspace(0., 90., num=data.shape[0], endpoint=True) th += (th[1] - th[0]) / 2. if th_res is not None: # we are given a beam resolution and thus may not just glue each # beam to its neighbor # solving this still with the efficient pcolormesh but interlacing # the data with masked values, simulating the gap between beams # make a temporary data array with one dimension twice the size of # the original img = np.ma.empty((data.shape[0], data.shape[1] * 2)) # mask everything img.mask = np.ma.masked # set the data in the first half of the temporary array # this automatically unsets the mask img[:, :data.shape[1]] = data # reshape so that data and masked lines interlace each other img = img.reshape((-1, data.shape[1])) # produce lower and upper y coordinates for the actual data yl = th - th_res * 0.5 yu = th + th_res * 0.5 # glue them together to achieve the proper dimensions for the # interlaced array th = np.concatenate([yl[None, :], yu[None, :]], axis=0).T.ravel() else: img = data if r is None: if proj and proj != 'cg': warnings.warn("Parameter `r` is None, falling back to `proj=None`." "If using projection, r must be given as " "array with units m.") proj = None r = np.arange(data.shape[1], dtype=np.float) r += (r[1] - r[0]) / 2. if np.isscalar(az): az = np.ones_like(th) * az da = georef.create_xarray_dataarray(img, r=r, phi=az, theta=th, site=site, proj=proj, sweep_mode='rhi', rf=rf, **kw_spherical) da = georef.georeference_dataset(da, proj=proj) # fallback to proj=None for GDAL OSR if isinstance(proj, osr.SpatialReference): proj = None pm = da.wradlib.plot_rhi(ax=ax, fig=fig, func=func, proj=proj, **kwargs) return pl.gca(), pm
def plot_ppi(data, r=None, az=None, elev=0., site=None, proj=None, fig=None, ax=111, func='pcolormesh', rf=1., **kwargs): """Plots a Plan Position Indicator (PPI). This is a small wrapper around xarray dataarray. The radar data, coordinates and metadata is transformed into an xarray dataarray. Using the wradlib dataarray accessor the dataarray is enabled to plot polar data. Using ``proj=cg`` the plotting is done in a curvelinear grid axes. Additional data can be plotted in polar coordinates or cartesian coordinates depending which axes object is used. ``**kwargs`` may be used to try to influence the :func:`matplotlib.pyplot.pcolormesh`, :func:`matplotlib.pyplot.contour`, :func:`matplotlib.pyplot.contourf` and :func:`wradlib.georef.polar.spherical_to_proj` routines under the hood. Concerning the values of ``r``, ``az``, ``elev``, ``r`` should give the location of the center of each range bin, ``az`` and ``elev`` should give the angle at the center of the beam. Parameters ---------- data : :class:`numpy:numpy.ndarray` The data to be plotted. It is assumed that the first dimension is over the azimuth angles, while the second dimension is over the range bins r : :class:`numpy:numpy.ndarray` The ranges. Units may be chosen arbitrarily, unless proj is set. In that case the units must be meters. If None, a default is calculated from the dimensions of ``data``. rf: float If present, factor for scaling range axes, defaults to 1. az : :class:`numpy:numpy.ndarray` The azimuth angles in degrees. If None, a default is calculated from the dimensions of ``data``. elev : float or array of same shape as ``az`` Elevation angle of the scan or individual azimuths. May improve georeferencing coordinates for larger elevation angles. site : tuple or None Tuple of coordinates of the radar site. If ``proj`` is not used, this simply becomes the offset for the origin of the coordinate system. If ``proj`` is used, values must be given as (longitude, latitude, altitude) tuple of geographical coordinates. Defaults to None. proj : GDAL OSR SRS | cartopy CRS | curvelinear grid dict | None GDAL OSR Spatial Reference Object describing projection If this parameter is not None, ``site`` must be set. Then the function will attempt to georeference the radar bins and display the PPI in the coordinate system defined by the projection string. fig : :class:`matplotlib:matplotlib.figure.Figure` If given, the PPI will be plotted into this figure object. Axes are created as needed. If None, a new figure object will be created or current figure will be used, depending on ``ax``. ax : :class:`matplotlib:matplotlib.axes.Axes` | matplotlib grid definition If matplotlib Axes object is given, the PPI will be plotted into this axes object. If matplotlib grid definition is given (nrows/ncols/plotnumber), axis are created in the specified place. Defaults to '111', only one subplot/axis. func : str Name of plotting function to be used under the hood. Defaults to 'pcolormesh'. 'contour' and 'contourf' can be selected too. See also -------- :func:`wradlib.georef.projection.reproject` :func:`wradlib.georef.projection.create_osr` Returns ------- ax : :class:`matplotlib:matplotlib.axes.Axes` The axes object into which the PPI was plotted pm : :class:`matplotlib:matplotlib.collections.QuadMesh` | \ :class:`matplotlib:matplotlib.contour.QuadContourSet` The result of the plotting function. Necessary, if you want to add a colorbar to the plot. Note ---- If proj=``cg``, the ``cgax`` - curvelinear Axes (r-theta-grid) is returned. ``caax`` - Cartesian Axes (x-y-grid) and ``paax`` - parasite axes object for plotting polar data can be derived like this:: caax = cgax.parasites[0] paax = cgax.parasites[1] The function :func:`~wradlib.vis.create_cg` uses the Matplotlib AXISARTIST namespace `mpl_toolkits.axisartist`_. Here are some limitations to normal Matplotlib Axes. See `AxesGridToolkitUserGuide`_. Examples -------- See :ref:`/notebooks/visualisation/wradlib_plot_ppi_example.ipynb`, and :ref:`/notebooks/visualisation/wradlib_plot_curvelinear_grids.ipynb`. .. _mpl_toolkits.axisartist: https://matplotlib.org/mpl_toolkits/axes_grid/users/axisartist.html .. _AxesGridToolkitUserGuide: https://matplotlib.org/mpl_toolkits/axes_grid/users/index.html """ # check coordinate tuple if site and len(site) < 3: raise ValueError("WRADLIB: `site` need to be a tuple of coordinates " "(longitude, latitude, altitude).") # site must be given, if proj is OSR if isinstance(proj, osr.SpatialReference) and site is None: raise TypeError("WRADLIB: If `proj` is Spatial Reference System " "(GDAL OSR SRS) site need to be given " "as tuple of (longitude, latitude, altitude)") # site given without proj if site and not proj: warnings.warn("WRADLIB: site is given without `proj`, it will be used " "as simple xy-offset") # re/ke kwargs handling kw_spherical = { 're': kwargs.pop('re', None), 'ke': kwargs.pop('ke', 4. / 3.) } if az is None: az = np.arange(data.shape[0], dtype=np.float) az += (az[1] - az[0]) / 2. if r is None: if proj and proj != 'cg': warnings.warn("Parameter `r` is None, falling back to `proj=None`." "If using projection, r must be given as " "array with units m.") proj = None r = np.arange(data.shape[1], dtype=np.float) r += (r[1] - r[0]) / 2. if np.isscalar(elev): elev = np.ones_like(az) * elev da = georef.create_xarray_dataarray(data, r=r, phi=az, theta=elev, site=site, proj=proj, sweep_mode='azimuth_surveillance', rf=rf, **kw_spherical) da = georef.georeference_dataset(da, proj=proj) # fallback to proj=None for GDAL OSR if isinstance(proj, osr.SpatialReference): proj = None pm = da.wradlib.plot_ppi(ax=ax, fig=fig, func=func, proj=proj, **kwargs) return pl.gca(), pm
class TestPolarPlot: img = np.zeros((360, 10), dtype=np.float32) img[2, 2] = 10 # isolated pixel img[5, 6:8] = 10 # line img[20, :] = 5 # spike img[60:120, 2:7] = 11 # precip field r = np.arange(0, 100000, 10000) az = np.arange(0, 360) el = np.arange(0, 90) th = np.zeros_like(az) az1 = np.ones_like(el) * 225 img = img proj = georef.create_osr("dwd-radolan") da_ppi = georef.create_xarray_dataarray(img, r, az, th) da_ppi = georef.georeference_dataset(da_ppi, proj=None) da_rhi = georef.create_xarray_dataarray(img[0:90], r, az1, el) da_rhi = georef.georeference_dataset(da_rhi, proj=None) def test_plot_ppi(self): ax, pm = vis.plot_ppi(self.img, re=6371000.0, ke=(4.0 / 3.0)) ax, pm = vis.plot_ppi(self.img, self.r, self.az, re=6371000.0, ke=(4.0 / 3.0)) ax, pm = vis.plot_ppi( self.img, self.r, self.az, re=6371000.0, ke=(4.0 / 3.0), ax=ax ) ax, pm = vis.plot_ppi( self.img, self.r, self.az, re=6371000.0, ke=(4.0 / 3.0), ax=212 ) ax, pm = vis.plot_ppi(self.img) vis.plot_ppi_crosshair(site=(0, 0, 0), ranges=[2, 4, 8]) vis.plot_ppi_crosshair( site=(0, 0, 0), ranges=[2, 4, 8], angles=[0, 45, 90, 180, 270], line=dict(color="white", linestyle="solid"), ) ax, pm = vis.plot_ppi(self.img, self.r, site=(10.0, 45.0, 0.0), proj=self.proj) vis.plot_ppi_crosshair( site=(10.0, 45.0, 0.0), ranges=[2, 4, 8], angles=[0, 45, 90, 180, 270], proj=self.proj, line=dict(color="white", linestyle="solid"), ) ax, pm = vis.plot_ppi(self.img, func="contour") ax, pm = vis.plot_ppi(self.img, func="contourf") ax, pm = vis.plot_ppi(self.img, self.r, self.az, proj=self.proj, site=(0, 0, 0)) with pytest.warns(UserWarning): ax, pm = vis.plot_ppi(self.img, site=(10.0, 45.0, 0.0), proj=self.proj) with pytest.warns(UserWarning): ax, pm = vis.plot_ppi(self.img, proj=None, site=(0, 0, 0)) with pytest.raises(TypeError): ax, pm = vis.plot_ppi(self.img, proj=self.proj) with pytest.raises(ValueError): ax, pm = vis.plot_ppi(self.img, site=(0, 0), proj=self.proj) with pytest.raises(ValueError): vis.plot_ppi_crosshair(site=(0, 0), ranges=[2, 4, 8]) def test_plot_ppi_xarray(self): self.da_ppi.wradlib.rays self.da_ppi.wradlib.plot() self.da_ppi.wradlib.plot_ppi() self.da_ppi.wradlib.contour() self.da_ppi.wradlib.contourf() self.da_ppi.wradlib.pcolormesh() self.da_ppi.wradlib.plot(proj="cg") self.da_ppi.wradlib.plot_ppi(proj="cg") self.da_ppi.wradlib.contour(proj="cg") self.da_ppi.wradlib.contourf(proj="cg") self.da_ppi.wradlib.pcolormesh(proj="cg") with pytest.raises(TypeError): self.da_ppi.wradlib.pcolormesh(proj=self.proj) fig = pl.figure() ax = fig.add_subplot(111) with pytest.raises(TypeError): self.da_ppi.wradlib.pcolormesh(proj={"rot": 0, "scale": 1}, ax=ax) @pytest.mark.skipif("cartopy" not in sys.modules, reason="without Cartopy") def test_plot_ppi_cartopy(self): if cartopy: if (LooseVersion(cartopy.__version__) < LooseVersion("0.18.0")) and ( LooseVersion(mpl.__version__) >= LooseVersion("3.3.0") ): pytest.skip("fails for cartopy < 0.18.0 and matplotlib >= 3.3.0") site = (7, 45, 0.0) map_proj = cartopy.crs.Mercator(central_longitude=site[1]) ax, pm = vis.plot_ppi(self.img, self.r, self.az, proj=map_proj) assert isinstance(ax, cartopy.mpl.geoaxes.GeoAxes) fig = pl.figure(figsize=(10, 10)) ax = fig.add_subplot(111, projection=map_proj) self.da_ppi.wradlib.plot_ppi(ax=ax) ax.gridlines(draw_labels=True) def test_plot_rhi(self): ax, pm = vis.plot_rhi(self.img[0:90, :]) ax, pm = vis.plot_rhi(self.img[0:90, :], th_res=0.5) ax, pm = vis.plot_rhi(self.img[0:90, :], th_res=0.5, ax=212) ax, pm = vis.plot_rhi(self.img[0:90, :], r=np.arange(10), th=np.arange(90)) ax, pm = vis.plot_rhi(self.img[0:90, :], func="contour") ax, pm = vis.plot_rhi(self.img[0:90, :], func="contourf") ax, pm = vis.plot_rhi( self.img[0:90, :], r=np.arange(10), th=np.arange(90), proj=self.proj, site=(0, 0, 0), ) def test_plot_rhi_xarray(self): assert ( repr(self.da_rhi.wradlib).split("\n", 1)[1] == repr(self.da_rhi).split("\n", 1)[1] ) self.da_rhi.wradlib.rays self.da_rhi.wradlib.plot() self.da_rhi.wradlib.plot_rhi() self.da_rhi.wradlib.contour() self.da_rhi.wradlib.contourf() self.da_rhi.wradlib.pcolormesh() self.da_rhi.wradlib.plot(proj="cg") self.da_rhi.wradlib.plot_rhi(proj="cg") self.da_rhi.wradlib.contour(proj="cg") self.da_rhi.wradlib.contourf(proj="cg") self.da_rhi.wradlib.pcolormesh(proj="cg") def test_plot_cg_ppi(self): cgax, pm = vis.plot_ppi(self.img, elev=2.0, proj="cg") cgax, pm = vis.plot_ppi(self.img, elev=2.0, proj="cg", site=(0, 0, 0)) cgax, pm = vis.plot_ppi(self.img, elev=2.0, proj="cg", ax=cgax) fig, ax = pl.subplots(2, 2) with pytest.raises(TypeError): vis.plot_ppi(self.img, elev=2.0, proj="cg", ax=ax[0, 0]) cgax, pm = vis.plot_ppi(self.img, elev=2.0, proj="cg", ax=111) cgax, pm = vis.plot_ppi(self.img, elev=2.0, proj="cg", ax=121) cgax, pm = vis.plot_ppi(self.img, proj="cg") cgax, pm = vis.plot_ppi(self.img, func="contour", proj="cg") cgax, pm = vis.plot_ppi(self.img, func="contourf", proj="cg") cgax, pm = vis.plot_ppi(self.img, func="contourf", proj="cg") def test_plot_cg_rhi(self): cgax, pm = vis.plot_rhi(self.img[0:90, :], proj="cg") cgax, pm = vis.plot_rhi(self.img[0:90, :], proj="cg", ax=cgax) fig, ax = pl.subplots(2, 2) with pytest.raises(TypeError): vis.plot_rhi(self.img[0:90, :], proj="cg", ax=ax[0, 0]) cgax, pm = vis.plot_rhi(self.img[0:90, :], th_res=0.5, proj="cg") cgax, pm = vis.plot_rhi(self.img[0:90, :], proj="cg") cgax, pm = vis.plot_rhi( self.img[0:90, :], r=np.arange(10), th=np.arange(90), proj="cg" ) cgax, pm = vis.plot_rhi(self.img[0:90, :], func="contour", proj="cg") cgax, pm = vis.plot_rhi(self.img[0:90, :], func="contourf", proj="cg") def test_create_cg(self): cgax, caax, paax = vis.create_cg() cgax, caax, paax = vis.create_cg(subplot=121)