def setUp(self): filename = 'geo/bonn_new.tif' geofile = util.get_wradlib_data_file(filename) self.ds = open_raster(geofile) (self.data, self.coords, self.proj) = georef.extract_raster_dataset(self.ds) self.x_sp, self.y_sp = self.coords[1, 1] - self.coords[0, 0]
def setUp(self): filename = 'geo/bonn_new.tif' geofile = util.get_wradlib_data_file(filename) self.ds = wradlib.io.open_raster(geofile) (self.data, self.coords, self.proj) = georef.extract_raster_dataset(self.ds) filename = 'hdf5/belgium.comp.hdf' geofile = util.get_wradlib_data_file(filename) self.ds2 = wradlib.io.open_raster(geofile) (self.data2, self.coords2, self.proj2) = georef.extract_raster_dataset(self.ds, mode="edge") self.corner_gdalinfo = np.array([[3e5, 1e6], [3e5, 3e5], [1e6, 3e5], [1e6, 1e6]]) self.corner_geo_gdalinfo = np.array([[-0.925465, 53.6928559], [-0.266697, 47.4167912], [9.0028805, 47.4160381], [9.6641599, 53.6919969]])
def test_create_raster_dataset(self): data, coords = georef.set_raster_origin(self.data.copy(), self.coords.copy(), 'upper') ds = georef.create_raster_dataset(data, coords, projection=self.proj, nodata=-32768) data, coords, proj = georef.extract_raster_dataset(ds) np.testing.assert_array_equal(data, self.data) np.testing.assert_array_almost_equal(coords, self.coords) self.assertEqual(proj.ExportToWkt(), self.proj.ExportToWkt())
def test_create_raster_dataset(self): data, coords = georef.set_raster_origin( self.data.copy(), self.coords.copy(), "upper" ) ds = georef.create_raster_dataset( data, coords, projection=self.proj, nodata=-32768 ) data, coords, proj = georef.extract_raster_dataset(ds) np.testing.assert_array_equal(data, self.data) np.testing.assert_array_almost_equal(coords, self.coords) assert proj.ExportToWkt() == self.proj.ExportToWkt() data, coords = georef.set_raster_origin( self.data2.copy(), self.coords2.copy(), "upper" ) ds = georef.create_raster_dataset( data, coords, projection=self.proj, nodata=-32768 ) data, coords, proj = georef.extract_raster_dataset(ds, mode="edge") np.testing.assert_array_equal(data, self.data2) np.testing.assert_array_almost_equal(coords, self.coords2) assert proj.ExportToWkt() == self.proj.ExportToWkt()
def test_extract_raster_dataset(self): data, coords, proj = georef.extract_raster_dataset(self.ds)
def plot_scan_strategy( ranges, elevs, sitecoords, beamwidth=1.0, vert_res=500.0, maxalt=10000.0, range_res=None, maxrange=None, units="m", terrain=None, az=0.0, cg=False, ax=111, cmap="tab10", ): """Plot the vertical scanning strategy. Parameters ---------- ranges : sequence of floats array of ranges elevs : sequence of floats elevation angles sitecoords : sequence of floats radar site coordinates (longitude, latitude, altitude) beamwidth : float 3dB width of the radar beam, defaults to 1.0 deg. vert_res : float Vertical resolution in [m]. maxalt : float Maximum altitude in [m]. range_res : float Horizontal resolution in [m]. maxrange : float Maximum range in [m]. units : str Units to plot in, can be 'm' or 'km'. Defaults to 'm'. terrain : bool | :class:`numpy:numpy.ndarray` If True, downloads srtm data and add orography for given `az`. az : float Used to specify azimuth for terrain plots. cg : bool If True, plot in curvelinear grid, defaults to False (cartesian grid). ax : :class:`matplotlib:matplotlib.axes.Axes` | matplotlib grid definition If matplotlib Axes object is given, the scan strategy 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. cmap : str matplotlib colormap string. Returns ------- ax : :class:`matplotlib:matplotlib.axes.Axes` | matplotlib toolkit axisartist Axes object matplotlib Axes or curvelinear Axes (r-theta-grid) depending on keyword argument `cg`. """ if units == "m": scale = 1.0 elif units == "km": scale = 1000.0 else: raise ValueError( f"wradlib: unknown value for keyword argument units={units}") az = np.array([az]) if maxrange is None: maxrange = ranges.max() xyz, rad = georef.spherical_to_xyz(ranges, az, elevs, sitecoords, squeeze=True) add_title = "" if terrain is True: add_title += f" - Azimuth {az[0]}°" ll = georef.reproject(xyz, projection_source=rad) # (down-)load srtm data ds = io.get_srtm( [ ll[..., 0].min(), ll[..., 0].max(), ll[..., 1].min(), ll[..., 1].max() ], download={}, ) rastervalues, rastercoords, proj = georef.extract_raster_dataset( ds, nodata=-32768.0) # map rastervalues to polar grid points terrain = ipol.cart_to_irregular_spline(rastercoords, rastervalues, ll[-1, ..., :2], order=3, prefilter=False) if ax == 111: fig = pl.figure(figsize=(16, 8)) else: fig = pl.gcf() legend2 = {} if cg is True: ax, caax, paax = create_cg(fig=fig, subplot=ax, rot=0, scale=1) # for nice plotting we assume earth_radius = 6370000 m er = 6370000 # calculate beam_height and arc_distance for ke=1 # means line of sight ade = georef.bin_distance(ranges, 0, sitecoords[2], re=er, ke=1.0) nn0 = np.zeros_like(ranges) ecp = nn0 + er # theta (arc_distance sector angle) thetap = -np.degrees(ade / er) + 90.0 # zero degree elevation with standard refraction (bes, ) = paax.plot(thetap, ecp, "-k", linewidth=3, label="_MSL", zorder=3) legend2["MSL"] = bes if terrain is not None: paax.fill_between(thetap, ecp.min() - 2500, ecp + terrain, color="0.75", zorder=2) # axes layout ax.set_xlim(0, np.max(ade)) ax.set_ylim([ecp.min() - maxalt / 5, ecp.max() + maxalt]) caax.grid(True, axis="x") ax.grid(True, axis="y") ax.axis["top"].toggle(all=False) gh = ax.get_grid_helper() yrange = maxalt + maxalt / 5 nbins = ((yrange // vert_res) * 2 + 1) // np.sqrt(2) gh.grid_finder.grid_locator2._nbins = nbins else: ax = fig.add_subplot(ax) paax = ax caax = ax if terrain is not None: paax.fill_between(ranges, 0, terrain, color="0.75", zorder=2) ax.set_xlim(0.0, maxrange) ax.set_ylim(0.0, maxalt) ax.grid() # axes ticks and formatting if range_res is not None: xloc = range_res caax.xaxis.set_major_locator(MultipleLocator(xloc)) else: caax.xaxis.set_major_locator(MaxNLocator()) yloc = vert_res caax.yaxis.set_major_locator(MultipleLocator(yloc)) import functools hform = functools.partial(_height_formatter, cg=cg, scale=scale) rform = functools.partial(_range_formatter, scale=scale) caax.yaxis.set_major_formatter(FuncFormatter(hform)) caax.xaxis.set_major_formatter(FuncFormatter(rform)) # color management from cycler import cycler NUM_COLORS = len(elevs) cmap = pl.get_cmap(cmap) if cmap.N >= 256: colors = [cmap(1.0 * i / NUM_COLORS) for i in range(NUM_COLORS)] else: colors = cmap.colors cycle = cycler(color=colors) paax.set_prop_cycle(cycle) # plot beams for i, el in enumerate(elevs): alt = xyz[i, ..., 2] groundrange = np.sqrt(xyz[i, ..., 0]**2 + xyz[i, ..., 1]**2) if cg: plrange = thetap plalt = ecp + alt beamradius = util.half_power_radius(ranges, beamwidth) else: plrange = np.insert(groundrange, 0, 0) plalt = np.insert(alt, 0, sitecoords[2]) beamradius = util.half_power_radius(plrange, beamwidth) _, center, edge = _plot_beam(plrange, plalt, beamradius, label=f"{el:4.1f}°", ax=paax) # legend 1 handles, labels = ax.get_legend_handles_labels() leg1 = ax.legend( handles, labels, prop={"family": "monospace"}, loc="upper left", bbox_to_anchor=(1.04, 1), borderaxespad=0, ) # legend 2 legend2["Center"] = center[0] legend2["3 dB"] = edge[0] ax.legend( legend2.values(), legend2.keys(), prop={"family": "monospace"}, loc="lower left", bbox_to_anchor=(1.04, 0), borderaxespad=0, ) # add legend 1 ax.add_artist(leg1) # set axes labels ax.set_title(f"Radar Scan Strategy - {sitecoords}" + add_title) caax.set_xlabel(f"Range ({units})") caax.set_ylabel(f"Altitude ({units})") return ax
def test_extract_raster_dataset(self): ds = self.ds data, coords, proj = georef.extract_raster_dataset(ds) assert coords.shape[-1] == 2 data, coords, proj = georef.extract_raster_dataset(ds, mode="edge") assert coords.shape[-1] == 2
class TestGdal: filename = "geo/bonn_new.tif" geofile = util.get_wradlib_data_file(filename) ds = wradlib.io.open_raster(geofile) (data, coords, proj) = georef.extract_raster_dataset(ds) filename = "hdf5/belgium.comp.hdf" geofile = util.get_wradlib_data_file(filename) ds2 = wradlib.io.open_raster(geofile) (data2, coords2, proj2) = georef.extract_raster_dataset(ds, mode="edge") corner_gdalinfo = np.array([[3e5, 1e6], [3e5, 3e5], [1e6, 3e5], [1e6, 1e6]]) corner_geo_gdalinfo = np.array( [ [-0.925465, 53.6928559], [-0.266697, 47.4167912], [9.0028805, 47.4160381], [9.6641599, 53.6919969], ] ) def test_read_gdal_coordinates(self): center_coords = georef.read_gdal_coordinates(self.ds) assert center_coords.shape[-1] == 2 edge_coords = georef.read_gdal_coordinates(self.ds, mode="edge") ul_center = (edge_coords[0, 0] + edge_coords[1, 1]) / 2 np.testing.assert_array_almost_equal(center_coords[0, 0], ul_center) def test_read_gdal_projection(self): georef.read_gdal_projection(self.ds) def test_read_gdal_values(self): georef.read_gdal_values(self.ds) georef.read_gdal_values(self.ds, nodata=9999.0) def test_reproject_raster_dataset(self): georef.reproject_raster_dataset( self.ds, spacing=0.005, resample=gdal.GRA_Bilinear, align=True ) georef.reproject_raster_dataset( self.ds, size=(1000, 1000), resample=gdal.GRA_Bilinear, align=True ) with pytest.raises(NameError): georef.reproject_raster_dataset(self.ds) dst = georef.epsg_to_osr(25832) georef.reproject_raster_dataset( self.ds, spacing=100.0, resample=gdal.GRA_Bilinear, align=True, projection_target=dst, ) def test_create_raster_dataset(self): data, coords = georef.set_raster_origin( self.data.copy(), self.coords.copy(), "upper" ) ds = georef.create_raster_dataset( data, coords, projection=self.proj, nodata=-32768 ) data, coords, proj = georef.extract_raster_dataset(ds) np.testing.assert_array_equal(data, self.data) np.testing.assert_array_almost_equal(coords, self.coords) assert proj.ExportToWkt() == self.proj.ExportToWkt() data, coords = georef.set_raster_origin( self.data2.copy(), self.coords2.copy(), "upper" ) ds = georef.create_raster_dataset( data, coords, projection=self.proj, nodata=-32768 ) data, coords, proj = georef.extract_raster_dataset(ds, mode="edge") np.testing.assert_array_equal(data, self.data2) np.testing.assert_array_almost_equal(coords, self.coords2) assert proj.ExportToWkt() == self.proj.ExportToWkt() def test_set_raster_origin(self): testfunc = georef.set_raster_origin data, coords = testfunc(self.data.copy(), self.coords.copy(), "upper") np.testing.assert_array_equal(data, self.data) np.testing.assert_array_equal(coords, self.coords) data, coords = testfunc(self.data.copy(), self.coords.copy(), "lower") np.testing.assert_array_equal(data, np.flip(self.data, axis=-2)) np.testing.assert_array_equal(coords, np.flip(self.coords, axis=-3)) data, coords = testfunc( self.data.copy()[:, :3600], self.coords.copy()[:3600, :3600], "lower" ) np.testing.assert_array_equal(data, np.flip(self.data[:, :3600], axis=-2)) np.testing.assert_array_equal( coords, np.flip(self.coords[:3600, :3600], axis=-3) ) def test_set_raster_indexing(self): data, coords = georef.set_raster_origin( self.data.copy(), self.coords.copy(), "lower" ) data, coords = georef.set_raster_indexing(data, coords, "ij") np.testing.assert_array_equal( data, np.swapaxes(np.flip(self.data, axis=-2), 0, 1) ) np.testing.assert_array_equal( coords, np.swapaxes(np.flip(self.coords, axis=-3), 0, 1) ) data, coords = georef.set_raster_indexing(data, coords, "xy") np.testing.assert_array_equal(data, np.flip(self.data, axis=-2)) np.testing.assert_array_equal(coords, np.flip(self.coords, axis=-3)) def test_set_coordinate_indexing(self): coords = georef.set_coordinate_indexing(self.coords.copy(), "ij") np.testing.assert_array_equal(coords, np.swapaxes(self.coords, 0, 1)) coords = georef.set_coordinate_indexing(self.coords.copy(), "xy") np.testing.assert_array_equal(coords, self.coords) def test_extract_raster_dataset(self): ds = self.ds data, coords, proj = georef.extract_raster_dataset(ds) assert coords.shape[-1] == 2 data, coords, proj = georef.extract_raster_dataset(ds, mode="edge") assert coords.shape[-1] == 2 @pytest.mark.skipif(sys.platform.startswith("win"), reason="known break on windows") def test_get_raster_elevation(self): georef.get_raster_elevation(self.ds, download={"region": "Eurasia"}) def test_get_raster_extent(self): extent = georef.get_raster_extent(self.ds2) window_map = np.array([3e5, 1e6, 3e5, 1e6]) np.testing.assert_array_almost_equal(extent, window_map, decimal=3) extent = georef.get_raster_extent(self.ds2, geo=True) window_geo = np.array([-0.925465, 9.6641599, 47.4160381, 53.6928559]) np.testing.assert_array_almost_equal(extent, window_geo) extent = georef.get_raster_extent(self.ds2, window=False) np.testing.assert_array_almost_equal(extent, self.corner_gdalinfo, decimal=3) extent = georef.get_raster_extent(self.ds2, geo=True, window=False) np.testing.assert_array_almost_equal(extent, self.corner_geo_gdalinfo) @pytest.mark.skipif(sys.platform.startswith("win"), reason="known break on windows") def test_merge_raster_datasets(self): download = {"region": "Eurasia"} datasets = wradlib.io.dem.get_srtm( [3, 4, 47, 48], merge=False, download=download ) georef.merge_raster_datasets(datasets) def test_raster_to_polyvert(self): ds = self.ds polyvert = georef.raster_to_polyvert(ds) nx = ds.RasterXSize ny = ds.RasterYSize assert polyvert.shape == (ny, nx, 5, 2)
def test_extract_raster_dataset(self): ds = self.ds data, coords, proj = georef.extract_raster_dataset(ds) self.assertEqual(coords.shape[-1], 2) data, coords, proj = georef.extract_raster_dataset(ds, mode="edge") self.assertEqual(coords.shape[-1], 2)