1+ import panel as pn
2+ import xarray as xr
3+ import holoviews as hv
4+ from holoviews import opts
5+ import hvplot .xarray
6+
7+ hv .extension ("bokeh" )
8+
9+ ## Load data
10+
11+ rda_url = 'https://data.rda.ucar.edu/'
12+ annual_means = rda_url + 'pythia_era5_24/annual_means/'
13+ xrds = xr .open_dataset (annual_means + "temp_2m_annual_1940_2023.zarr" , engine = 'zarr' )
14+ xrds .load ()
15+
16+ xrds ['VAR_2T_ANOM_FROM_1940' ] = xrds ['VAR_2T' ] - xrds ['VAR_2T' ][0 ]
17+
18+ ## Widget configuration
19+ w_time = pn .widgets .IntSlider (name = 'Year' , start = 0 , end = 83 )
20+
21+ w_var = pn .widgets .Select (name = 'Data Variable' , options = list (xrds .data_vars ))
22+
23+ dataset_controls = pn .WidgetBox (
24+ '## Dataset Controls' ,
25+ w_var ,
26+ )
27+
28+ w_cmap = pn .widgets .Select (name = 'Colormap' , options = ['inferno' , 'plasma' , 'coolwarm' ])
29+
30+ w_plot_type = pn .widgets .Select (name = 'Plot Type' , options = ['Color Plot' , 'Contour' , 'Filled Contour' ])
31+
32+ plot_controls = pn .WidgetBox (
33+ '## Plot Controls' ,
34+ w_plot_type ,
35+ w_cmap ,
36+ )
37+
38+ w_player = pn .widgets .Player (
39+ value = 0 ,
40+ start = 0 ,
41+ end = 83 ,
42+ name = "Year" ,
43+ loop_policy = "loop" ,
44+ interval = 300 ,
45+ align = "center" ,
46+ width_policy = 'fit'
47+ )
48+
49+ ## Function to create the plot
50+
51+ def plot_ds (time , var , cmap , plot_type ):
52+ clim = (xrds [var ].values .min (), xrds [var ].values .max ())
53+
54+ if plot_type == "Color Plot" :
55+ return xrds [var ].isel (time = time ).hvplot (cmap = cmap ,
56+ title = str (f"{ var } year { time } " ),
57+ clim = clim ,
58+ dynamic = False ,
59+ rasterize = True ,
60+ precompute = True ,
61+ ).opts (framewise = False )
62+
63+ elif plot_type == "Contour" :
64+ return xrds [var ].isel (time = time ).hvplot .contour (cmap = cmap ,
65+ dynamic = False ,
66+ rasterize = True ,
67+ title = str (f"{ var } Year: { time } " ),
68+ clim = clim ,
69+ precompute = True ,).opts (framewise = False )
70+ elif plot_type == "Filled Contour" :
71+ return xrds [var ].isel (time = time ).hvplot .contourf (cmap = cmap ,
72+ dynamic = False ,
73+ rasterize = True ,
74+ title = str (f"{ var } Year: { time } " ),
75+ clim = (200 , 300 ),
76+ precompute = True ,).opts (framewise = False )
77+
78+ ## Panel App configuration
79+
80+ controls = pn .Column (dataset_controls , plot_controls )
81+
82+ app = pn .Row (
83+ controls ,
84+ pn .Column (pn .panel (
85+ hv .DynamicMap (pn .bind (
86+ plot_ds ,
87+ time = w_player ,
88+ var = w_var ,
89+ cmap = w_cmap ,
90+ plot_type = w_plot_type
91+ )
92+ )
93+ ),
94+ w_player )
95+ ).servable ()
0 commit comments