Interactive and dynamically created dict of widgets to xarray sel

@Marc Thank you again for the feedback reduced the files to only 4 data_vars and converted them to .nc files which can easily be opened with xarray.open_dataset(PATH_TO_FILE). They are downloaded using gdown in the script but can also be found here ( File 1, File 2, File 1 and File 1 ). I have included a python script which should work now and only download 200MB (unless you uncomment all 4 files) and a Google Colab Notebook which I could not get to show the interactive Panel objects even if I followed your example in another Discourse Question. Thank you very much for you help I am trying to solve it as well and am experimenting with Param as well now, but I would really appreciate some help in getting this working.

import gdown
import hvplot.xarray
import xarray as xr
import panel as pn
import panel.widgets as pnw
from glob import glob
import cartopy
import cartopy.crs as ccrs
import param
from panel.interact import interact
import hvplot
import numpy as np
from pathlib import Path
import collections 
collections.Callable = collections.abc.Callable

# Download at least 2 example files
gdown.download(id='1vI5xH-kmA4OkoqJJz9keze-vN3cYCIf4', output='2022070700_231_example.nc', quiet=False) # 90 MB only
# gdown.download(id='1aQKSMFoUgoGPisA-VnbkIxa3w6YaLrkm', output='2022070700_234_example.nc', quiet=False) # 220 MB Omitted by default
gdown.download(id='1RnWmfiW3U7VYS4Qoqo7_uFdeAIXAajye', output='2022070700_237_example.nc', quiet=False) # 90 MB only
# gdown.download(id='17JqTO_W62oN3HbBhoHZ9GS4aQOO59oL4', output='2022070700_240_example.nc', quiet=False) # 220 MB Omitted by default

glob_path = Path.cwd()
file_list = [str(pp) for pp in glob_path.glob("2022*")]
file_list = [pp.split('\\')[-1] for pp in file_list]

xds = xr.open_dataset(file_list[0])

select_file = pnw.Select(name="File", options=file_list)
select_field = pnw.Select(name="Field", options=list(xds.data_vars))
cmap_sel = pnw.Select(name="Colormap", options=['cool','hot','jet','viridis','brg','rainbow'])
rasterize_toggle = pnw.Toggle(name='Rasterize', button_type='success')

@pn.depends(select_file,select_field) 
def load_file(select_file,select_field):
    xds = xr.open_dataset(select_file)
    sel_dict = {}
    for (i,d) in enumerate(xds[select_field].dims):
        for (j,c) in enumerate(xds[select_field].coords):
            if d == c and (d != 'lon' and d != 'lat'):
                if xds[select_field].coords[c].dtype == np.dtype('<M8[ns]'):
                    time_list = []
                    for i, el in np.ndenumerate(xds[select_field].coords['time'].values):
                        time_list.append(np.datetime_as_string(el, timezone='UTC'))
                    time_slider = pnw.DiscreteSlider(options=time_list)
                    sel_dict[d] = time_slider
                else:
                    sel_dict[d] = pnw.DiscreteSlider(options=xds[select_field].coords[c].values.tolist())
    return xds[select_field].interactive.sel(**sel_dict).hvplot(kind='quadmesh', rasterize=True, data_aspect=1, frame_height=800,cmap='jet', crs=ccrs.PlateCarree(), projection=ccrs.PlateCarree(), project=True, geo=True, coastline=True, global_extent=True)

pn.Row(pn.Card(select_file,select_field),load_file)
1 Like