Repository navigation
Add Scene function to use Hvplot backend visualization #2106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
fa7942d
88d4002
93c14ef
2fa9b87
fcfd481
dfd93ec
c0022f3
249c420
2f2e8a8
ac8a361
d2c80fb
09b8f6e
d0299dd
aed6a91
4687ba4
1aff451
546bb5f
4455ac2
d574eeb
03b8056
590d374
d319a9b
09ad15d
3aba218
9541b70
3369c93
5beb117
874f8d8
2da489b
07b313b
27041a4
fb8ff3b
91f96af
807357a
aad6ea8
fca35cd
7918f37
258fd17
f45e225
d16728b
4d3153a
9b0bcae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,10 +25,11 @@ | |
|
|
||
| import numpy as np | ||
| import xarray as xr | ||
| from holoviews import Overlay | ||
| from pyresample.geometry import AreaDefinition, BaseDefinition, SwathDefinition | ||
| from xarray import DataArray | ||
|
|
||
| from satpy.composites import IncompatibleAreas | ||
| from satpy.composites import IncompatibleAreas, enhance2dataset | ||
| from satpy.composites.config_loader import load_compositor_configs_for_sensors | ||
| from satpy.dataset import DataID, DataQuery, DatasetDict, combine_metadata, dataset_walker, replace_anc | ||
| from satpy.dependency_tree import DependencyTree | ||
|
|
@@ -37,6 +38,12 @@ | |
| from satpy.resample import get_area_def, prepare_resampler, resample_dataset | ||
| from satpy.writers import load_writer | ||
|
|
||
| try: | ||
| import hvplot.xarray # noqa | ||
| except ImportError: | ||
| hvplot.xarray = None | ||
|
|
||
|
|
||
| LOG = logging.getLogger(__name__) | ||
|
|
||
|
|
||
|
|
@@ -1022,6 +1029,70 @@ def to_geoviews(self, gvtype=None, datasets=None, kdims=None, vdims=None, dynami | |
|
|
||
| return gview | ||
|
|
||
| def to_hvplot(self, datasets=None, *args, **kwargs): | ||
| """Convert satpy Scene to Hvplot. | ||
|
|
||
| Args: | ||
| datasets (list): Limit included products to these datasets. | ||
| kwargs: hvplot options list. | ||
|
bornagain1981 marked this conversation as resolved.
Resolved
|
||
|
|
||
| Returns: hvplot object that contains within it the plots of datasets list. | ||
| As default it contains all Scene datasets plots and a plot title is shown. | ||
|
|
||
| Example usage: | ||
|
bornagain1981 marked this conversation as resolved.
Resolved
|
||
| scene_list = ['ash','IR_108'] | ||
| plot = scn.to_hvplot(datasets=scene_list) | ||
|
|
||
| plot.ash+plot.IR_108 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't enough to show the plot, what else needs to be done? I was testing this in IPython command line prompt.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pnuu does it work in your Jupyter Notebook? |
||
|
|
||
| """ | ||
| def _get_crs(xarray_ds): | ||
| return xarray_ds.area.to_cartopy_crs() | ||
|
|
||
| def _get_timestamp(xarray_ds): | ||
| time = xarray_ds.attrs['start_time'] | ||
| return time.strftime('%Y %m %d -- %H:%M UTC') | ||
|
|
||
| def _get_units(xarray_ds, variable): | ||
| return xarray_ds[variable].attrs['units'] | ||
|
|
||
| def _plot_rgb(xarray_ds, variable, **defaults): | ||
| img = enhance2dataset(xarray_ds[variable]) | ||
| return img.hvplot.rgb(bands='bands', title=title, | ||
| clabel='', **defaults) | ||
|
|
||
| def _plot_quadmesh(xarray_ds, variable, **defaults): | ||
| return xarray_ds[variable].hvplot.quadmesh( | ||
| clabel=f'[{_get_units(xarray_ds,variable)}]', title=title, | ||
| **defaults) | ||
|
|
||
| if hvplot.xarray is None: | ||
|
pnuu marked this conversation as resolved.
Resolved
|
||
| raise ImportError("'hvplot' must be installed to use this feature") | ||
|
|
||
| plot = Overlay() | ||
| xarray_ds = self.to_xarray_dataset(datasets) | ||
| ccrs = _get_crs(xarray_ds) | ||
|
|
||
| if datasets is None: | ||
| datasets = list(xarray_ds.keys()) | ||
|
|
||
| defaults = dict(x='x', y='y', data_aspect=1, project=True, geo=True, | ||
| crs=ccrs, projection=ccrs, rasterize=True, coastline='110m', | ||
| cmap='Plasma', responsive=True, dynamic=False, framewise=True, | ||
| colorbar=False, global_extent=False, xlabel='Longitude', | ||
| ylabel='Latitude') | ||
|
|
||
| defaults.update(kwargs) | ||
|
|
||
| for element in datasets: | ||
| title = f'{element} @ {_get_timestamp(xarray_ds)}' | ||
| if xarray_ds[element].shape[0] == 3: | ||
| plot[element] = _plot_rgb(xarray_ds, element, **defaults) | ||
| else: | ||
| plot[element] = _plot_quadmesh(xarray_ds, element, **defaults) | ||
|
|
||
| return plot | ||
|
|
||
| def to_xarray_dataset(self, datasets=None): | ||
| """Merge all xr.DataArrays of a scene to a xr.DataSet. | ||
|
|
||
|
|
||

Uh oh!
There was an error while loading. Please reload this page.