uxarray.UxDataArray.to_raster

uxarray.UxDataArray.to_raster#

UxDataArray.to_raster(ax, *, pixel_ratio=None, pixel_mapping=None, return_pixel_mapping=False)#

Rasterizes a data variable stored on the faces of an unstructured grid onto the pixels of the provided Cartopy GeoAxes.

Parameters:
  • ax (GeoAxes) – A Cartopy GeoAxes onto which the data will be rasterized. Each pixel in this axes will be sampled against the unstructured grid’s face geometry.

  • pixel_ratio (float, default=1.0) – A scaling factor to adjust the resolution of the rasterization. A value greater than 1 increases the resolution (sharpens the image), while a value less than 1 will result in a coarser rasterization. The resolution also depends on what the figure’s DPI setting is prior to calling to_raster(). You can control DPI with the dpi keyword argument when creating the figure, or by using set_dpi() after creation.

  • pixel_mapping (xr.DataArray or array-like, optional) – Precomputed mapping from pixels within the Cartopy GeoAxes boundary to grid face indices (1-dimensional).

  • return_pixel_mapping (bool, default=False) – If True, the pixel mapping will be returned in addition to the raster, and then you can pass it via the pixel_mapping parameter for future rasterizations using the same or equivalent uxgrid and ax. Note that this is also specific to the pixel ratio setting.

Returns:

  • raster (numpy.ndarray, shape (ny, nx)) – Array of resampled data values corresponding to each pixel.

  • pixel_mapping (xr.DataArray, shape (n,)) – If return_pixel_mapping=True, the computed pixel mapping is returned so that you can reuse it. Axes and pixel ratio info are included as attributes.

Notes

  • This method currently employs a nearest-neighbor resampling approach. For every pixel in the GeoAxes, it finds the face of the unstructured grid that contains the pixel’s geographic coordinate and colors that pixel with the face’s data value.

  • If a pixel does not intersect any face (i.e., lies outside the grid domain), it will be left empty (transparent).

Examples

>>> import cartopy.crs as ccrs
>>> import matplotlib.pyplot as plt

Create a GeoAxes with a Robinson projection and global extent

>>> fig, ax = plt.subplots(subplot_kw={"projection": ccrs.Robinson()})
>>> ax.set_global()

Rasterize data onto the GeoAxes

>>> raster = uxds["psi"].to_raster(ax=ax)

Use imshow() to visualize the raster

>>> ax.imshow(raster, origin="lower", extent=ax.get_xlim() + ax.get_ylim())