import numpy as np import xarray as xr import geoviews as gv import datashader as dsh from geoviews import opts gv.extension('bokeh', 'matplotlib') opts.defaults( opts.Image(width=600, height=400, colorbar=True), opts.Feature(apply_ranges=False), opts.QuadMesh(width=600, height=400, colorbar=True))
In geographical applications grids and meshes of different kinds are very common and for visualization and analysis it is often very important to be able to resample them in different ways. Regridding can refer both to upsampling and downsampling a grid or mesh, which is achieved through interpolation and aggregation.
Naive approaches to regridding treat the space as flat, which is often simpler but can also give less accurate results when working with a spherical space such as the earth. In this user guide we will summarize how to work with different grid types including rectilinear, curvilinear grids and trimeshes. Additionally we will discuss different approaches to regridding working based on the assumption of a flat earth (using datashader) and a spherical earth (xESMF).
Rectilinear grids are one of the most standard formats and are defined by regularly sampled coordinates along the two axes. The
air_temperature dataset provided by xarray and used throughout the GeoViews documentation provides a good example.
ds = xr.tutorial.open_dataset('air_temperature').load().isel(time=slice(0, 100)) ds
<xarray.Dataset> Dimensions: (lat: 25, time: 100, lon: 53) Coordinates: * lat (lat) float32 75.0 72.5 70.0 67.5 65.0 ... 25.0 22.5 20.0 17.5 15.0 * lon (lon) float32 200.0 202.5 205.0 207.5 ... 322.5 325.0 327.5 330.0 * time (time) datetime64[ns] 2013-01-01 ... 2013-01-25T18:00:00 Data variables: air (time, lat, lon) float32 241.2 242.5 243.5 ... 295.8 295.4 294.9 Attributes: Conventions: COARDS title: 4x daily NMC reanalysis (1948) description: Data is from NMC initialized reanalysis\n(4x/day). These a... platform: Model references: http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanaly...
As we already know from the Gridded Dataset sections, an xarray of this kind can easily be wrapped in a GeoViews dataset:
gvds = gv.Dataset(ds).redim.range(air=(230, 300)) gvds
|datatype||List||['xarray', 'dataframe', 'dictionary', 'g..||(0, None)|
|kdims||List||[Dimension('lat', label='Latitude', unit..||(0, None)||constant|
|name||String||'Dataset03474'||constant | nullable|
|vdims||List||[Dimension('air', label='4xDaily Air tem..||(0, None)||constant|
We can also easily plot this data by using the
.to method to group the data into a set of
Image elements indexed by ‘time’:
images = gvds.to(gv.Image, ['lon', 'lat'], dynamic=True)
It is important to note that if we look at the longitude coordinates above we can see that they are defined in the range (0, 360), while GeoViews generally expects it to be in the range (-180, 180). To correct this we can apply a simple fix:
ds['lon'] = np.where(ds.lon>180, ds.lon-360, ds.lon)
Now we are ready to display the data. Note that throughout this user guide we will be using Bokeh but we could easily switch to matplotlib if needed.
opts.defaults(opts.Image(cmap='viridis')) images * gv.feature.coastline
HoloViews provides high-level wrappers around the datashader library, which make it possible to quickly resample or aggregate datasets of different kinds. Datashader knows nothing about non-flat coordinate systems, but provides a very fast, parallelized regridding operation for rectilinear grids. Here we will import the
regrid operation and pass it our stack of images from above. While this dataset is fairly small and regridding will actually upsample the image to match the dimensions of the plot,
regrid can very quickly downsample very large datasets.
One important thing to note about the resampling operations we will be working with in this user guide is that they are dynamic and linked to the plot dimensions and axis ranges. This means that whenever we zoom or pan the data will be resampled. If we want to disable this linked behavior and supply an explicit width and height we can disable the streams by passing
streams= as a keyword argument.
from holoviews.operation.datashader import regrid regrid(images) * gv.feature.coastline
The xESMF library is specifically designed to provide an easy way to accurately resample grids defined in geographic coordinate systems and differs significantly from the simpler approach used by datashader, which applies simple upsampling and downsampling. xESMF is a wrapper around the ESMF regridding algorithms, which compute an interpolation weight matrix which is applied to remap the values of the source grid onto the destination grid.
In GeoViews these algorithms are made available via the
weighted_regrid operation, which supports the different interpolation modes including: ‘bilinear’, ‘nearest_s2d’, ‘nearest_d2s’ and ‘conservative’. Since generating the sparse weight matrix takes much longer than applying it the operation will cache the weight matrix on disk for later use; this optimization can be disabled via the
reuse_weights parameter or customized by defining a custom
from geoviews.operation.regrid import weighted_regrid weighted_regrid(images) * gv.feature.coastline